forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectron-api.ts
More file actions
311 lines (284 loc) · 16.7 KB
/
Copy pathelectron-api.ts
File metadata and controls
311 lines (284 loc) · 16.7 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import type {
AssetLibraryListResult,
AssetLibraryOpenRequest,
AssetLibraryOpenResult,
AssetLibraryReadRequest,
AssetLibraryReadResult,
} from '../../src/shared/types/assetLibrary'
export interface IpcRendererLike {
invoke(channel: string, ...args: unknown[]): Promise<unknown>
send(channel: string, ...args: unknown[]): void
on(channel: string, listener: (...args: unknown[]) => void): void
removeAllListeners(channel: string): void
}
export interface WebFrameLike {
setZoomFactor(factor: number): void
}
export function createElectronApi(ipcRenderer: IpcRendererLike, webFrame: WebFrameLike) {
return {
// Window controls
window: {
minimize: () => ipcRenderer.send('window:minimize'),
maximize: () => ipcRenderer.send('window:maximize'),
close: () => ipcRenderer.send('window:close'),
isMaximized: () => ipcRenderer.invoke('window:isMaximized') as Promise<boolean>,
onMaximizeChange: (cb: (isMaximized: boolean) => void) => {
ipcRenderer.on('window:maximizeChanged', (_event, isMaximized) => cb(isMaximized as boolean))
},
offMaximizeChange: () => ipcRenderer.removeAllListeners('window:maximizeChanged'),
},
// Native OS notifications (Windows toast / macOS Notification Center / Linux)
notifications: {
show: (title: string, body: string): Promise<{ success: boolean; error?: string }> =>
ipcRenderer.invoke('notifications:show', title, body) as Promise<{ success: boolean; error?: string }>,
},
// Renderer UI (zoom whole page — scales every px/rem consistently)
ui: { setZoomFactor: (factor: number) => webFrame.setZoomFactor(factor) },
// Shell utilities
shell: { openExternal: (url: string) => ipcRenderer.invoke('shell:openExternal', url) },
// System info
system: {
memory: (): Promise<{ total: number; used: number; available: number }> =>
ipcRenderer.invoke('system:memory') as Promise<{ total: number; used: number; available: number }>,
gpuMemory: (): Promise<{ total: number; used: number; available: number } | null> =>
ipcRenderer.invoke('system:gpuMemory') as Promise<{ total: number; used: number; available: number } | null>,
},
// Python / FastAPI bridge
python: {
start: (): Promise<{ success: boolean; port?: number; error?: string }> =>
ipcRenderer.invoke('python:start') as Promise<{ success: boolean; port?: number; error?: string }>,
status: (): Promise<{ ready: boolean; apiUrl: string }> =>
ipcRenderer.invoke('python:status') as Promise<{ ready: boolean; apiUrl: string }>,
onCrashed: (cb: (data: { code: number | null }) => void) => {
ipcRenderer.on('python:crashed', (_event, data) => cb(data as { code: number | null }))
},
offCrashed: () => ipcRenderer.removeAllListeners('python:crashed'),
onLog: (cb: (line: string) => void) => {
ipcRenderer.on('python:log', (_event, line) => cb(line as string))
},
offLog: () => ipcRenderer.removeAllListeners('python:log'),
},
// File system dialogs + local file reading
fs: {
selectImage: (): Promise<string | null> =>
ipcRenderer.invoke('fs:selectImage') as Promise<string | null>,
selectMeshFile: (): Promise<string | null> =>
ipcRenderer.invoke('fs:selectMeshFile') as Promise<string | null>,
saveModel: (defaultName: string): Promise<string | null> =>
ipcRenderer.invoke('fs:saveModel', defaultName) as Promise<string | null>,
readFileBase64: (filePath: string): Promise<string> =>
ipcRenderer.invoke('fs:readFileBase64', filePath) as Promise<string>,
selectDirectory: (defaultPath?: string): Promise<string | null> =>
ipcRenderer.invoke('fs:selectDirectory', defaultPath) as Promise<string | null>,
savePath: (args: { filters: { name: string; extensions: string[] }[]; defaultPath?: string }): Promise<string | null> =>
ipcRenderer.invoke('fs:savePath', args) as Promise<string | null>,
listDir: (dirPath: string): Promise<string[]> =>
ipcRenderer.invoke('fs:listDir', dirPath) as Promise<string[]>,
listFiles: (dirPath: string, extensions?: string[]): Promise<string[]> =>
ipcRenderer.invoke('fs:listFiles', dirPath, extensions) as Promise<string[]>,
selectTextFile: (): Promise<string | null> =>
ipcRenderer.invoke('fs:selectTextFile') as Promise<string | null>,
moveDirectory: (args: { src: string; dest: string }): Promise<{ success: boolean; error?: string }> =>
ipcRenderer.invoke('fs:moveDirectory', args) as Promise<{ success: boolean; error?: string }>,
deleteDirectory: (dirPath: string): Promise<{ success: boolean; error?: string }> =>
ipcRenderer.invoke('fs:deleteDirectory', dirPath) as Promise<{ success: boolean; error?: string }>,
readScreenshotDataUrl: (filename: string): Promise<string> =>
ipcRenderer.invoke('fs:readScreenshotDataUrl', filename) as Promise<string>,
},
// Settings
settings: {
get: (): Promise<{ modelsDir: string; workspaceDir: string; workflowsDir: string; extensionsDir: string; hfToken?: string }> =>
ipcRenderer.invoke('settings:get') as Promise<{ modelsDir: string; workspaceDir: string; workflowsDir: string; extensionsDir: string; hfToken?: string }>,
set: (patch: { modelsDir?: string; workspaceDir?: string; workflowsDir?: string; extensionsDir?: string; hfToken?: string }): Promise<{ modelsDir: string; workspaceDir: string; workflowsDir: string; extensionsDir: string; hfToken?: string }> =>
ipcRenderer.invoke('settings:set', patch) as Promise<{ modelsDir: string; workspaceDir: string; workflowsDir: string; extensionsDir: string; hfToken?: string }>,
},
// Cache
cache: {
clear: (): Promise<{ success: boolean; error?: string }> =>
ipcRenderer.invoke('cache:clear') as Promise<{ success: boolean; error?: string }>,
},
// API helpers (calls FastAPI from the main process)
api: {
updatePaths: (patch: { modelsDir?: string; workspaceDir?: string; extensionsDir?: string }): Promise<{ success: boolean; error?: string }> =>
ipcRenderer.invoke('api:updatePaths', patch) as Promise<{ success: boolean; error?: string }>,
},
// Model management
model: {
export: (args: { outputUrl: string; format: string }) => ipcRenderer.invoke('model:export', args),
listDownloaded: () => ipcRenderer.invoke('model:listDownloaded'),
isDownloaded: (modelId: string, downloadCheck?: string) => ipcRenderer.invoke('model:isDownloaded', modelId, downloadCheck),
download: (repoId: string, modelId: string, skipPrefixes?: string[], includePrefixes?: string[]) =>
ipcRenderer.invoke('model:download', { repoId, modelId, skipPrefixes, includePrefixes }),
pauseDownload: (modelId: string) => ipcRenderer.invoke('model:pauseDownload', modelId),
cancelDownload: (modelId: string) => ipcRenderer.invoke('model:cancelDownload', modelId),
delete: (modelId: string) => ipcRenderer.invoke('model:delete', modelId),
unloadAll: () => ipcRenderer.invoke('model:unloadAll'),
showInFolder: (modelId: string) => ipcRenderer.invoke('model:showInFolder', modelId),
activeDownloads: (): Promise<{ modelId: string; percent: number; file?: string; fileIndex?: number; totalFiles?: number }[]> =>
ipcRenderer.invoke('model:activeDownloads') as Promise<{ modelId: string; percent: number; file?: string; fileIndex?: number; totalFiles?: number }[]>,
onProgress: (cb: (data: {
modelId: string
percent: number
file?: string
fileIndex?: number
totalFiles?: number
status?: string
bytesDownloaded?: number
totalBytes?: number
stalledSeconds?: number
paused?: boolean
cancelled?: boolean
}) => void) => {
ipcRenderer.on('model:downloadProgress', (_event, data) => cb(data as {
modelId: string
percent: number
file?: string
fileIndex?: number
totalFiles?: number
status?: string
bytesDownloaded?: number
totalBytes?: number
stalledSeconds?: number
paused?: boolean
cancelled?: boolean
}))
},
offProgress: () => ipcRenderer.removeAllListeners('model:downloadProgress'),
},
// App metadata
app: {
info: (): Promise<{ version: string; userData: string; modelsDir: string; apiUrl: string; platform: string; arch: string }> =>
ipcRenderer.invoke('app:info') as Promise<{ version: string; userData: string; modelsDir: string; apiUrl: string; platform: string; arch: string }>,
onError: (cb: (message: string) => void) => {
ipcRenderer.on('app:error', (_event, message) => cb(message as string))
},
offError: () => ipcRenderer.removeAllListeners('app:error'),
},
// Logging
log: {
error: (message: string) => ipcRenderer.send('log:error', message),
getPath: (): Promise<string> => ipcRenderer.invoke('log:getPath') as Promise<string>,
readAll: (session?: string): Promise<Record<string, string>> => ipcRenderer.invoke('log:readAll', session) as Promise<Record<string, string>>,
listSessions: (): Promise<string[]> => ipcRenderer.invoke('log:listSessions') as Promise<string[]>,
},
// Workspace filesystem-based persistence
workspace: {
listCollections: (): Promise<string[]> =>
ipcRenderer.invoke('workspace:listCollections') as Promise<string[]>,
createCollection: (name: string): Promise<void> =>
ipcRenderer.invoke('workspace:createCollection', name) as Promise<void>,
renameCollection: (oldName: string, newName: string): Promise<void> =>
ipcRenderer.invoke('workspace:renameCollection', { oldName, newName }) as Promise<void>,
deleteCollection: (name: string): Promise<void> =>
ipcRenderer.invoke('workspace:deleteCollection', name) as Promise<void>,
listJobs: (collection: string): Promise<unknown[]> =>
ipcRenderer.invoke('workspace:listJobs', collection) as Promise<unknown[]>,
saveJobMeta: (collection: string, filename: string, meta: unknown): Promise<void> =>
ipcRenderer.invoke('workspace:saveJobMeta', { collection, filename, meta }) as Promise<void>,
deleteJob: (collection: string, filename: string): Promise<void> =>
ipcRenderer.invoke('workspace:deleteJob', { collection, filename }) as Promise<void>,
library: {
list: (): Promise<AssetLibraryListResult> => ipcRenderer.invoke('workspace:library:list') as Promise<AssetLibraryListResult>,
read: (request: AssetLibraryReadRequest): Promise<AssetLibraryReadResult> => ipcRenderer.invoke('workspace:library:read', request) as Promise<AssetLibraryReadResult>,
open: (request: AssetLibraryOpenRequest): Promise<AssetLibraryOpenResult> => ipcRenderer.invoke('workspace:library:open', request) as Promise<AssetLibraryOpenResult>,
},
},
// Extensions
extensions: {
list: (): Promise<unknown[]> =>
ipcRenderer.invoke('extensions:list') as Promise<unknown[]>,
installFromGitHub: (url: string): Promise<{
success: boolean; error?: string; cancelled?: boolean
extensionId?: string
extension?: unknown
}> => ipcRenderer.invoke('extensions:installFromGitHub', url) as Promise<{
success: boolean; error?: string; cancelled?: boolean
extensionId?: string
extension?: unknown
}>,
installFromLocal: (): Promise<{
success: boolean; error?: string; cancelled?: boolean
needsRepair?: boolean
extensionId?: string
extension?: unknown
localPath?: string
}> => ipcRenderer.invoke('extensions:installFromLocal') as Promise<{
success: boolean; error?: string; cancelled?: boolean
needsRepair?: boolean
extensionId?: string
extension?: unknown
localPath?: string
}>,
uninstall: (extensionId: string): Promise<{ success: boolean; error?: string }> =>
ipcRenderer.invoke('extensions:uninstall', extensionId) as Promise<{ success: boolean; error?: string }>,
repair: (extensionId: string): Promise<{ success: boolean; error?: string }> =>
ipcRenderer.invoke('extensions:repair', extensionId) as Promise<{ success: boolean; error?: string }>,
reload: (): Promise<{ success: boolean; error?: string }> =>
ipcRenderer.invoke('extensions:reload') as Promise<{ success: boolean; error?: string }>,
runProcess: (
extensionId: string,
input: { filePath?: string; text?: string; texts?: (string | undefined)[]; nodeId?: string },
params: Record<string, unknown>,
): Promise<{ success: boolean; result?: { filePath?: string; text?: string }; error?: string }> =>
ipcRenderer.invoke('extensions:runProcess', extensionId, input, params) as Promise<{ success: boolean; result?: { filePath?: string; text?: string }; error?: string }>,
onInstallProgress: (cb: (data: {
step: 'downloading' | 'extracting' | 'validating' | 'setting_up' | 'done' | 'error'
percent?: number
extensionId?: string
message?: string
}) => void) => {
ipcRenderer.on('extensions:installProgress', (_event, data) => cb(data as {
step: 'downloading' | 'extracting' | 'validating' | 'setting_up' | 'done' | 'error'
percent?: number
extensionId?: string
message?: string
}))
},
offInstallProgress: () => ipcRenderer.removeAllListeners('extensions:installProgress'),
},
// Workflows
workflows: {
list: (): Promise<unknown[]> => ipcRenderer.invoke('workflows:list') as Promise<unknown[]>,
save: (workflow: { id: string; [key: string]: unknown }): Promise<{ success: boolean; error?: string }> => ipcRenderer.invoke('workflows:save', workflow) as Promise<{ success: boolean; error?: string }>,
delete: (id: string): Promise<{ success: boolean; error?: string }> => ipcRenderer.invoke('workflows:delete', id) as Promise<{ success: boolean; error?: string }>,
import: (): Promise<{ success: boolean; error?: string; workflow?: unknown }> => ipcRenderer.invoke('workflows:import') as Promise<{ success: boolean; error?: string; workflow?: unknown }>,
export: (workflow: { id: string; name?: string; [key: string]: unknown }): Promise<{ success: boolean; error?: string }> => ipcRenderer.invoke('workflows:export', workflow) as Promise<{ success: boolean; error?: string }>,
},
// Auto-updater
updater: {
check: (): Promise<{ success: boolean }> =>
ipcRenderer.invoke('updater:check') as Promise<{ success: boolean }>,
quitAndInstall: (): Promise<void> =>
ipcRenderer.invoke('updater:quitAndInstall') as Promise<void>,
onApplying: (cb: (data: { version: string }) => void) => {
ipcRenderer.on('updater:applying', (_event, data) => cb(data as { version: string }))
},
offApplying: () => ipcRenderer.removeAllListeners('updater:applying'),
onMajorMinorAvailable: (cb: (data: { version: string }) => void) => {
ipcRenderer.on('updater:major-minor-available', (_event, data) => cb(data as { version: string }))
},
offMajorMinorAvailable: () => ipcRenderer.removeAllListeners('updater:major-minor-available'),
},
// First-run setup
setup: {
check: (): Promise<{ needed: boolean; defaultDataDir: string; platform: string; arch: string }> =>
ipcRenderer.invoke('setup:check') as Promise<{ needed: boolean; defaultDataDir: string; platform: string; arch: string }>,
run: (): Promise<{ success: boolean; error?: string }> =>
ipcRenderer.invoke('setup:run') as Promise<{ success: boolean; error?: string }>,
saveDataDir: (baseDir: string): Promise<void> =>
ipcRenderer.invoke('setup:saveDataDir', { baseDir }) as Promise<void>,
onProgress: (cb: (data: { step: string; percent: number; currentPackage?: string }) => void) => {
ipcRenderer.on('setup:progress', (_event, data) => cb(data as { step: string; percent: number; currentPackage?: string }))
},
offProgress: () => ipcRenderer.removeAllListeners('setup:progress'),
onComplete: (cb: () => void) => {
ipcRenderer.on('setup:complete', () => cb())
},
offComplete: () => ipcRenderer.removeAllListeners('setup:complete'),
onError: (cb: (data: { message: string }) => void) => {
ipcRenderer.on('setup:error', (_event, data) => cb(data as { message: string }))
},
offError: () => ipcRenderer.removeAllListeners('setup:error'),
},
}
}