Files
zn-ai/electron/preload/index.ts
DEV_DSW 210e8eb363 feat: add task management and progress reporting
- Implemented task and subtask structures with progress tracking.
- Added reporting functionality to log progress at various stages in hotel room status management scripts.
- Created a task store to manage tasks and their states, including persistence to local storage.
- Updated UI components to display task lists and handle task actions (retry, remove).
- Removed deprecated TaskCard and TaskList components, replacing them with a new structure for better maintainability.
- Enhanced script execution service to emit progress events for UI updates.
2026-04-16 16:59:49 +08:00

92 lines
3.9 KiB
TypeScript

import { contextBridge, ipcRenderer } from 'electron'
import { IPC_EVENTS } from '@lib/constants';
const api: WindowApi = {
versions: process.versions,
external: {
open: (url: string) => ipcRenderer.invoke('external-open', url)
},
platform: process.platform,
windowMinimize: () => ipcRenderer.invoke('window:minimize'),
windowMaximize: () => ipcRenderer.invoke('window:maximize'),
windowClose: () => ipcRenderer.invoke('window:close'),
windowIsMaximized: () => ipcRenderer.invoke('window:isMaximized'),
viewIsReady: () => ipcRenderer.send(IPC_EVENTS.RENDERER_IS_READY),
app: {
setFrameless: (route?: string) => ipcRenderer.invoke(IPC_EVENTS.APP_SET_FRAMELESS, route),
loadPage: (page: string) => ipcRenderer.invoke(IPC_EVENTS.APP_LOAD_PAGE, page)
},
// 通过 IPC 调用主进程
readFile: (filePath: string) => ipcRenderer.invoke(IPC_EVENTS.READ_FILE, filePath),
// 异步调用(映射为 electron 的 invoke)
invoke: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.invoke(channel, ...args),
// 异步调用(为了兼容老代码)
invokeAsync: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.invoke(channel, ...args),
// 监听主进程消息
on: (event: IPC_EVENTS, callback: (...args: any[]) => void) => {
const subscription = (_event: any, ...args: any[]) => callback(...args)
ipcRenderer.on(event, subscription)
return () => ipcRenderer.removeListener(event, subscription)
},
// 发送消息到主进程
send: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.send(channel, ...args),
// 获取窗口ID
getCurrentWindowId: () => ipcRenderer.sendSync(IPC_EVENTS.GET_WINDOW_ID),
// 发送日志
logger: {
debug: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_DEBUG, message, ...meta),
info: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_INFO, message, ...meta),
warn: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_WARN, message, ...meta),
error: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_ERROR, message, ...meta),
},
// 执行脚本
executeScript: (params: any) => ipcRenderer.invoke(IPC_EVENTS.EXECUTE_SCRIPT, params),
// 任务事件
onTaskProgress: (cb: (payload: any) => void) => {
const subscription = (_event: any, payload: any) => cb(payload);
ipcRenderer.on(IPC_EVENTS.TASK_PROGRESS, subscription);
return () => ipcRenderer.removeListener(IPC_EVENTS.TASK_PROGRESS, subscription);
},
onTaskStarted: (cb: (payload: any) => void) => {
const subscription = (_event: any, payload: any) => cb(payload);
ipcRenderer.on(IPC_EVENTS.TASK_STARTED, subscription);
return () => ipcRenderer.removeListener(IPC_EVENTS.TASK_STARTED, subscription);
},
onTaskCompleted: (cb: (payload: any) => void) => {
const subscription = (_event: any, payload: any) => cb(payload);
ipcRenderer.on(IPC_EVENTS.TASK_COMPLETED, subscription);
return () => ipcRenderer.removeListener(IPC_EVENTS.TASK_COMPLETED, subscription);
},
// 打开渠道
openChannel: (channels: any) => ipcRenderer.invoke(IPC_EVENTS.OPEN_CHANNEL, channels),
// 脚本管理
scriptApi: {
list: () => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_LIST),
get: (id: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_GET, id),
save: (input: any) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_SAVE, input),
delete: (id: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_DELETE, id),
toggle: (id: string, enabled: boolean) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_TOGGLE, id, enabled),
run: (id: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_RUN, id),
startRecording: (url?: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_RECORD_START, url),
stopRecording: () => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_RECORD_STOP),
codegen: (id: string, url?: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_CODEGEN, id, url),
},
}
contextBridge.exposeInMainWorld('api', api)