Skip to content

Commit e0d326b

Browse files
committed
Wire up tokenizer worker pool via IPC
Added IPC integration to make the worker pool functional: - Added TOKENS_COUNT_BULK IPC channel constant - Updated IPCApi type with tokens.countBulk method - Added tokens API to preload.ts - Registered token handlers in ipcMain.ts - Added worker pool cleanup on app quit The worker pool is now fully wired up and will be called via IPC. Dynamic imports ensure the worker is only loaded when actually used.
1 parent 785d7b4 commit e0d326b

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

src/constants/ipc-constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export const IPC_CHANNELS = {
3838
// Window channels
3939
WINDOW_SET_TITLE: "window:setTitle",
4040

41+
// Token channels
42+
TOKENS_COUNT_BULK: "tokens:countBulk",
43+
4144
// Dynamic channel prefixes
4245
WORKSPACE_CHAT_PREFIX: "workspace:chat:",
4346
WORKSPACE_METADATA: "workspace:metadata",

src/main.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,21 @@ if (gotTheLock) {
436436
}
437437
});
438438

439+
// Cleanup worker threads on quit
440+
app.on("will-quit", () => {
441+
console.log("App will quit - cleaning up worker threads");
442+
void (async () => {
443+
try {
444+
// Dynamic import is acceptable here - only loaded if worker was used
445+
/* eslint-disable-next-line no-restricted-syntax */
446+
const { tokenizerWorkerPool } = await import("@/services/tokenizerWorkerPool");
447+
tokenizerWorkerPool.terminate();
448+
} catch (error) {
449+
console.error("Error terminating worker pool:", error);
450+
}
451+
})();
452+
});
453+
439454
app.on("activate", () => {
440455
// Only create window if app is ready and no window exists
441456
// This prevents "Cannot create BrowserWindow before app is ready" error

src/preload.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ const api: IPCApi = {
110110
window: {
111111
setTitle: (title: string) => ipcRenderer.invoke(IPC_CHANNELS.WINDOW_SET_TITLE, title),
112112
},
113+
tokens: {
114+
countBulk: (model: string, texts: string[]) =>
115+
ipcRenderer.invoke(IPC_CHANNELS.TOKENS_COUNT_BULK, model, texts),
116+
},
113117
};
114118

115119
// Expose the API along with platform/versions

src/services/ipcMain.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export class IpcMain {
140140

141141
this.registerDialogHandlers(ipcMain);
142142
this.registerWindowHandlers(ipcMain);
143+
this.registerTokenHandlers(ipcMain);
143144
this.registerWorkspaceHandlers(ipcMain);
144145
this.registerProviderHandlers(ipcMain);
145146
this.registerProjectHandlers(ipcMain);
@@ -174,6 +175,24 @@ export class IpcMain {
174175
});
175176
}
176177

178+
private registerTokenHandlers(ipcMain: ElectronIpcMain): void {
179+
ipcMain.handle(
180+
IPC_CHANNELS.TOKENS_COUNT_BULK,
181+
async (_event, model: string, texts: string[]) => {
182+
try {
183+
// Offload to worker thread - keeps main process responsive
184+
// Dynamic import is acceptable here - worker pool is lazy-loaded on first use
185+
/* eslint-disable-next-line no-restricted-syntax */
186+
const { tokenizerWorkerPool } = await import("@/services/tokenizerWorkerPool");
187+
return await tokenizerWorkerPool.countTokens(model, texts);
188+
} catch (error) {
189+
log.error(`Failed to count tokens for model ${model}:`, error);
190+
return null; // Tokenizer not loaded or error occurred
191+
}
192+
}
193+
);
194+
}
195+
177196
private registerWorkspaceHandlers(ipcMain: ElectronIpcMain): void {
178197
ipcMain.handle(
179198
IPC_CHANNELS.WORKSPACE_CREATE,

src/types/ipc.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,7 @@ export interface IPCApi {
230230
window: {
231231
setTitle(title: string): Promise<void>;
232232
};
233+
tokens: {
234+
countBulk(model: string, texts: string[]): Promise<number[] | null>;
235+
};
233236
}

0 commit comments

Comments
 (0)