Open History to see the last commit
1import { contextBridge, ipcRenderer } from "electron";
2
3import { IPC_CHANNELS, type NativeApi } from "@acme/contracts";
4
5const nativeApi: NativeApi = {
6 todos: {
7 list: () => ipcRenderer.invoke(IPC_CHANNELS.todosList),
8 add: (input) => ipcRenderer.invoke(IPC_CHANNELS.todosAdd, input),
9 toggle: (id) => ipcRenderer.invoke(IPC_CHANNELS.todosToggle, id),
10 remove: (id) => ipcRenderer.invoke(IPC_CHANNELS.todosRemove, id),
11 },
12 dialogs: {
13 pickFolder: () => ipcRenderer.invoke(IPC_CHANNELS.dialogPickFolder),
14 },
15 terminal: {
16 run: (input) => ipcRenderer.invoke(IPC_CHANNELS.terminalRun, input),
17 },
18 agent: {
19 spawn: (config) => ipcRenderer.invoke(IPC_CHANNELS.agentSpawn, config),
20 kill: (sessionId) => ipcRenderer.invoke(IPC_CHANNELS.agentKill, sessionId),
21 write: (sessionId, data) => ipcRenderer.invoke(IPC_CHANNELS.agentWrite, sessionId, data),
22 onOutput: (callback) => {
23 const listener = (_event: Electron.IpcRendererEvent, chunk: unknown) =>
24 callback(chunk as Parameters<typeof callback>[0]);
25 ipcRenderer.on(IPC_CHANNELS.agentOutput, listener);
26 return () => ipcRenderer.removeListener(IPC_CHANNELS.agentOutput, listener);
27 },
28 onExit: (callback) => {
29 const listener = (_event: Electron.IpcRendererEvent, exit: unknown) =>
30 callback(exit as Parameters<typeof callback>[0]);
31 ipcRenderer.on(IPC_CHANNELS.agentExit, listener);
32 return () => ipcRenderer.removeListener(IPC_CHANNELS.agentExit, listener);
33 },
34 },
35 providers: {
36 startSession: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerSessionStart, input),
37 sendTurn: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerTurnStart, input),
38 interruptTurn: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerTurnInterrupt, input),
39 respondToRequest: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerRequestRespond, input),
40 stopSession: (input) => ipcRenderer.invoke(IPC_CHANNELS.providerSessionStop, input),
41 listSessions: () => ipcRenderer.invoke(IPC_CHANNELS.providerSessionList),
42 onEvent: (callback) => {
43 const listener = (_event: Electron.IpcRendererEvent, payload: unknown) =>
44 callback(payload as Parameters<typeof callback>[0]);
45 ipcRenderer.on(IPC_CHANNELS.providerEvent, listener);
46 return () => ipcRenderer.removeListener(IPC_CHANNELS.providerEvent, listener);
47 },
48 },
49 shell: {
50 openInEditor: (cwd: string, editor: string) =>
51 ipcRenderer.invoke(IPC_CHANNELS.shellOpenInEditor, cwd, editor),
52 },
53};
54
55contextBridge.exposeInMainWorld("nativeApi", nativeApi);