PRA-only-demo/src/preload/index.ts

60 lines
3.1 KiB
TypeScript

import { contextBridge, ipcRenderer } from "electron";
import type { AppInfo, AppSettings, Catalog, FlowDefinition, FlowSaveOptions, FlowSaveResult, FlowSummary, RunEvent, VisionLabDetectRequest, VisionLabDetection, VisionLabSource } from "../shared/types";
const api = {
app: {
info: (): Promise<AppInfo> => ipcRenderer.invoke("app:info")
},
catalog: {
get: (): Promise<Catalog> => ipcRenderer.invoke("catalog:get")
},
desktop: {
pickElement: () => ipcRenderer.invoke("desktop:pick-element"),
pickWindowList: () => ipcRenderer.invoke('desktop:pick-window-list'),
pickRegion: () => ipcRenderer.invoke('desktop:pick-region')
},
flows: {
list: (): Promise<FlowSummary[]> => ipcRenderer.invoke("flows:list"),
create: (name?: string): Promise<FlowDefinition> => ipcRenderer.invoke("flows:create", name),
createDemo: (): Promise<FlowDefinition> => ipcRenderer.invoke("flows:create-demo"),
createExamples: (): Promise<FlowSummary[]> => ipcRenderer.invoke("flows:create-examples"),
get: (id: string): Promise<FlowDefinition> => ipcRenderer.invoke("flows:get", id),
save: (flow: FlowDefinition, opts?: FlowSaveOptions): Promise<FlowSaveResult> => ipcRenderer.invoke("flows:save", flow, opts),
delete: (id: string): Promise<boolean> => ipcRenderer.invoke("flows:delete", id)
},
dialog: {
open: (options?: { title?: string; filters?: Array<{ name: string; extensions: string[] }>; directories?: boolean }): Promise<string | null> =>
ipcRenderer.invoke("dialog:open", options || {}),
save: (options?: { title?: string; defaultPath?: string; filters?: Array<{ name: string; extensions: string[] }> }): Promise<string | null> =>
ipcRenderer.invoke("dialog:save", options || {})
},
browser: {
pickElement: (options?: { url?: string }): Promise<import("../shared/types").ElementPickResult> =>
ipcRenderer.invoke("browser:pick-element", options || {})
},
visionLab: {
sources: (): Promise<VisionLabSource[]> => ipcRenderer.invoke("vision-lab:sources"),
detect: (request: VisionLabDetectRequest): Promise<VisionLabDetection> => ipcRenderer.invoke("vision-lab:detect", request),
defaultModel: (): Promise<string> => ipcRenderer.invoke("vision-lab:default-model")
},
settings: {
get: (): Promise<AppSettings> => ipcRenderer.invoke("settings:get"),
update: (patch: Partial<AppSettings>): Promise<AppSettings> => ipcRenderer.invoke("settings:update", patch || {})
},
run: {
start: (flow: FlowDefinition): Promise<{ runId: string }> => ipcRenderer.invoke("run:start", flow),
stop: (runId?: string): Promise<boolean> => ipcRenderer.invoke("run:stop", runId),
capabilities: (): Promise<{ count: number; ids: string[] }> => ipcRenderer.invoke("run:capabilities"),
onEvent: (callback: (event: RunEvent) => void): (() => void) => {
const listener = (_event: Electron.IpcRendererEvent, payload: RunEvent) => callback(payload);
ipcRenderer.on("run:event", listener);
return () => ipcRenderer.removeListener("run:event", listener);
}
}
};
contextBridge.exposeInMainWorld("rpa", api);
export type RpaApi = typeof api;