:lightning: optimize chat streaming

This commit is contained in:
2026-03-14 14:00:45 +10:00
parent 1a71a94831
commit 31a359e639
5 changed files with 53 additions and 17 deletions

View File

@@ -135,11 +135,27 @@ export function registerIpcHandlers(): void {
ipcMain.handle('ai:streamMessage', async (event, payload: AIPayload) => {
try {
const storyBibleContent = (await readStoryBibleFile()) ?? undefined
let pending = ''
let flushTimer: ReturnType<typeof setTimeout> | null = null
const flush = (): void => {
if (flushTimer) { clearTimeout(flushTimer); flushTimer = null }
if (pending && !event.sender.isDestroyed()) {
event.sender.send('ai:chunk', pending)
pending = ''
}
}
await streamMessage(payload, storyBibleContent, (chunk: string) => {
if (!event.sender.isDestroyed()) {
event.sender.send('ai:chunk', chunk)
pending += chunk
if (!flushTimer) {
flushTimer = setTimeout(flush, 30)
}
})
flush()
if (!event.sender.isDestroyed()) {
event.sender.send('ai:done')
}