forked from doctly/switchboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
199 lines (182 loc) · 9.95 KB
/
Copy pathpreload.js
File metadata and controls
199 lines (182 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const { contextBridge, ipcRenderer, webUtils } = require('electron');
// Brand strings are resolved in the main process and fetched synchronously
// at preload time (sandbox-compatible). Usage in the renderer:
// const label = (window.api.strings && window.api.strings.sidebar_sessions) || 'Sessions';
let brandingStringsSnapshot = {};
try {
const raw = ipcRenderer.sendSync('branding:getStrings');
if (raw && typeof raw === 'object') brandingStringsSnapshot = Object.freeze({ ...raw });
} catch {}
contextBridge.exposeInMainWorld('api', {
strings: brandingStringsSnapshot,
// Disk logging bridge — renderer messages go through main and into
// electron-log (<userData>/logs/main.log). Use this instead of
// console.* so logs survive even when DevTools won't open.
log: {
debug: (msg, meta) => ipcRenderer.send('renderer-log', 'debug', msg, meta),
info: (msg, meta) => ipcRenderer.send('renderer-log', 'info', msg, meta),
warn: (msg, meta) => ipcRenderer.send('renderer-log', 'warn', msg, meta),
error: (msg, meta) => ipcRenderer.send('renderer-log', 'error', msg, meta),
},
getLogPath: () => ipcRenderer.invoke('get-log-path'),
// Invoke (request-response)
getPlans: () => ipcRenderer.invoke('get-plans'),
readPlan: (filename) => ipcRenderer.invoke('read-plan', filename),
savePlan: (filePath, content) => ipcRenderer.invoke('save-plan', filePath, content),
getStats: () => ipcRenderer.invoke('get-stats'),
refreshStats: () => ipcRenderer.invoke('refresh-stats'),
getMemories: () => ipcRenderer.invoke('get-memories'),
readMemory: (filePath) => ipcRenderer.invoke('read-memory', filePath),
saveMemory: (filePath, content) => ipcRenderer.invoke('save-memory', filePath, content),
reloadShellEnv: () => ipcRenderer.invoke('reload-shell-env'),
getProjects: (showArchived) => ipcRenderer.invoke('get-projects', showArchived),
getActiveSessions: () => ipcRenderer.invoke('get-active-sessions'),
getActiveTerminals: () => ipcRenderer.invoke('get-active-terminals'),
stopSession: (id) => ipcRenderer.invoke('stop-session', id),
toggleStar: (id) => ipcRenderer.invoke('toggle-star', id),
renameSession: (id, name) => ipcRenderer.invoke('rename-session', id, name),
archiveSession: (id, archived) => ipcRenderer.invoke('archive-session', id, archived),
openTerminal: (id, projectPath, isNew, sessionOptions) => ipcRenderer.invoke('open-terminal', id, projectPath, isNew, sessionOptions),
search: (type, query, titleOnly) => ipcRenderer.invoke('search', type, query, titleOnly),
readSessionJsonl: (sessionId) => ipcRenderer.invoke('read-session-jsonl', sessionId),
// Settings
getSetting: (key) => ipcRenderer.invoke('get-setting', key),
setSetting: (key, value) => ipcRenderer.invoke('set-setting', key, value),
deleteSetting: (key) => ipcRenderer.invoke('delete-setting', key),
getEffectiveSettings: (projectPath) => ipcRenderer.invoke('get-effective-settings', projectPath),
getScheduleCreatorCommand: () => ipcRenderer.invoke('get-schedule-creator-command'),
createScheduleSession: (projectPath) => ipcRenderer.invoke('create-schedule-session', projectPath),
runScheduleNow: (filePath) => ipcRenderer.invoke('run-schedule-now', filePath),
getShellProfiles: () => ipcRenderer.invoke('get-shell-profiles'),
// Claude profiles (env-var bundles applied at session spawn)
profiles: {
list: () => ipcRenderer.invoke('profiles:list'),
save: (profile) => ipcRenderer.invoke('profiles:save', profile),
delete: (id) => ipcRenderer.invoke('profiles:delete', id),
setDefault: (id) => ipcRenderer.invoke('profiles:set-default', id),
},
// Map of sessionId → profileId. Populated at session spawn; renderer reads
// the whole map at startup to drive sidebar icon badges.
sessionProfiles: {
getAll: () => ipcRenderer.invoke('session-profiles:get-all'),
},
// Agent Teams orchestration — run/task state is file-based in each
// project's .switchboard dir; main watches it and pushes snapshots.
orchestration: {
watchProjects: (projectPaths) => ipcRenderer.invoke('orch:watch-projects', projectPaths),
getState: () => ipcRenderer.invoke('orch:get-state'),
getRun: (projectPath, runId) => ipcRenderer.invoke('orch:get-run', projectPath, runId),
readTaskFile: (projectPath, runId, taskId, which) => ipcRenderer.invoke('orch:read-task-file', projectPath, runId, taskId, which),
createRun: (projectPath, opts) => ipcRenderer.invoke('orch:create-run', projectPath, opts),
runAction: (projectPath, runId, action) => ipcRenderer.invoke('orch:run-action', projectPath, runId, action),
taskAction: (projectPath, runId, taskId, action) => ipcRenderer.invoke('orch:task-action', projectPath, runId, taskId, action),
onUpdated: (cb) => {
const handler = (_e, state) => cb(state);
ipcRenderer.on('orchestration-updated', handler);
return () => ipcRenderer.removeListener('orchestration-updated', handler);
},
},
// Pre-aggregated per-backend analytics computed by a worker thread from
// the JSONL session history. getCache returns instantly (cached read);
// refresh kicks the worker; analytics-updated fires when fresh data is ready.
analytics: {
getCache: () => ipcRenderer.invoke('analytics:get-cache'),
refresh: (opts) => ipcRenderer.invoke('analytics:refresh', opts || {}),
onUpdated: (cb) => {
const handler = () => cb();
ipcRenderer.on('analytics-updated', handler);
return () => ipcRenderer.removeListener('analytics-updated', handler);
},
},
// Local whisper.cpp HTTP server: status + lifecycle control + scheduled-task
// (logon-trigger) install for "always available" mode.
whisper: {
status: () => ipcRenderer.invoke('whisper:status'),
ping: () => ipcRenderer.invoke('whisper:ping'),
start: () => ipcRenderer.invoke('whisper:start'),
stop: () => ipcRenderer.invoke('whisper:stop'),
restart: () => ipcRenderer.invoke('whisper:restart'),
updateSettings: (next) => ipcRenderer.invoke('whisper:update-settings', next),
onState: (cb) => {
const handler = (_e, s) => cb(s);
ipcRenderer.on('whisper-state', handler);
return () => ipcRenderer.removeListener('whisper-state', handler);
},
},
browseFolder: () => ipcRenderer.invoke('browse-folder'),
addProject: (projectPath) => ipcRenderer.invoke('add-project', projectPath),
removeProject: (projectPath) => ipcRenderer.invoke('remove-project', projectPath),
openExternal: (url) => ipcRenderer.invoke('open-external', url),
// Send (fire-and-forget)
sendInput: (id, data) => ipcRenderer.send('terminal-input', id, data),
resizeTerminal: (id, cols, rows) => ipcRenderer.send('terminal-resize', id, cols, rows),
closeTerminal: (id) => ipcRenderer.send('close-terminal', id),
voiceLog: (level, event, data) => ipcRenderer.send('voice-log', level, event, data),
voiceSaveWav: (bytes) => ipcRenderer.invoke('voice-save-wav', bytes),
// Streamed-to-disk recording — main process owns the file lifecycle.
// start() returns { ok, recordingId, path }; chunk(...) is fire-and-
// forget; stop() and cancel() return { ok, ... }. transcribeFile()
// submits a recorded file to whisper-server and returns { ok, transcript }.
voiceRecord: {
start: () => ipcRenderer.invoke('voice-record-start'),
chunk: (id, bytes) => ipcRenderer.send('voice-record-chunk', id, bytes),
stop: (id) => ipcRenderer.invoke('voice-record-stop', id),
cancel: (id) => ipcRenderer.invoke('voice-record-cancel', id),
transcribeFile: (path, opts) => ipcRenderer.invoke('voice-transcribe-file', path, opts || {}),
},
// Listeners (main → renderer)
onTerminalData: (callback) => {
ipcRenderer.on('terminal-data', (_event, sessionId, data) => callback(sessionId, data));
},
onSessionDetected: (callback) => {
ipcRenderer.on('session-detected', (_event, tempId, realId) => callback(tempId, realId));
},
onProcessExited: (callback) => {
ipcRenderer.on('process-exited', (_event, sessionId, exitCode) => callback(sessionId, exitCode));
},
onTerminalNotification: (callback) => {
ipcRenderer.on('terminal-notification', (_event, sessionId, message) => callback(sessionId, message));
},
onCliBusyState: (callback) => {
ipcRenderer.on('cli-busy-state', (_event, sessionId, busy) => callback(sessionId, busy));
},
onSessionForked: (callback) => {
ipcRenderer.on('session-forked', (_event, oldId, newId) => callback(oldId, newId));
},
onProjectsChanged: (callback) => {
ipcRenderer.on('projects-changed', () => callback());
},
onStatusUpdate: (callback) => {
ipcRenderer.on('status-update', (_event, text, type) => callback(text, type));
},
// File drag-and-drop
getPathForFile: (file) => webUtils.getPathForFile(file),
// Platform
platform: process.platform,
// App version
getAppVersion: () => ipcRenderer.invoke('get-app-version'),
// MCP bridge (main → renderer)
onMcpOpenDiff: (callback) => {
ipcRenderer.on('mcp-open-diff', (_event, sessionId, diffId, data) => callback(sessionId, diffId, data));
},
onMcpOpenFile: (callback) => {
ipcRenderer.on('mcp-open-file', (_event, sessionId, data) => callback(sessionId, data));
},
onMcpCloseAllDiffs: (callback) => {
ipcRenderer.on('mcp-close-all-diffs', (_event, sessionId) => callback(sessionId));
},
onMcpCloseTab: (callback) => {
ipcRenderer.on('mcp-close-tab', (_event, sessionId, diffId) => callback(sessionId, diffId));
},
// MCP bridge (renderer → main)
mcpDiffResponse: (sessionId, diffId, action, editedContent) => {
ipcRenderer.send('mcp-diff-response', sessionId, diffId, action, editedContent);
},
readFileForPanel: (filePath) => ipcRenderer.invoke('read-file-for-panel', filePath),
saveFileForPanel: (filePath, content) => ipcRenderer.invoke('save-file-for-panel', filePath, content),
watchFile: (filePath) => ipcRenderer.invoke('watch-file', filePath),
unwatchFile: (filePath) => ipcRenderer.invoke('unwatch-file', filePath),
onFileChanged: (callback) => {
ipcRenderer.on('file-changed', (_event, filePath) => callback(filePath));
},
});