-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(stream): keep chat SSE alive during long LM Studio generations #1051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,57 +155,72 @@ export const POST = async (req: Request) => { | |
| const responseStream = new TransformStream(); | ||
| const writer = responseStream.writable.getWriter(); | ||
| const encoder = new TextEncoder(); | ||
| const keepAliveMs = 15_000; | ||
| let streamClosed = false; | ||
| let keepAliveInterval: ReturnType<typeof setInterval> | undefined; | ||
|
|
||
| const safeWrite = (payload: Record<string, unknown>) => { | ||
| if (streamClosed) return; | ||
|
|
||
| try { | ||
| writer.write(encoder.encode(JSON.stringify(payload) + '\n')); | ||
| } catch (error) { | ||
| console.warn('Failed to write chat stream payload:', error); | ||
| } | ||
| }; | ||
|
|
||
| const safeClose = () => { | ||
| if (streamClosed) return; | ||
|
|
||
| streamClosed = true; | ||
|
|
||
| if (keepAliveInterval) { | ||
| clearInterval(keepAliveInterval); | ||
| } | ||
|
|
||
| try { | ||
| writer.close(); | ||
| } catch (error) { | ||
| console.warn('Failed to close chat stream:', error); | ||
| } | ||
| }; | ||
|
|
||
| keepAliveInterval = setInterval(() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Keepalive interval/stream can leak indefinitely when Prompt for AI agents |
||
| safeWrite({ type: 'keepAlive' }); | ||
| }, keepAliveMs); | ||
|
|
||
| safeWrite({ type: 'keepAlive' }); | ||
|
|
||
| const disconnect = session.subscribe((event: string, data: any) => { | ||
| if (event === 'data') { | ||
| if (data.type === 'block') { | ||
| writer.write( | ||
| encoder.encode( | ||
| JSON.stringify({ | ||
| type: 'block', | ||
| block: data.block, | ||
| }) + '\n', | ||
| ), | ||
| ); | ||
| safeWrite({ | ||
| type: 'block', | ||
| block: data.block, | ||
| }); | ||
| } else if (data.type === 'updateBlock') { | ||
| writer.write( | ||
| encoder.encode( | ||
| JSON.stringify({ | ||
| type: 'updateBlock', | ||
| blockId: data.blockId, | ||
| patch: data.patch, | ||
| }) + '\n', | ||
| ), | ||
| ); | ||
| safeWrite({ | ||
| type: 'updateBlock', | ||
| blockId: data.blockId, | ||
| patch: data.patch, | ||
| }); | ||
| } else if (data.type === 'researchComplete') { | ||
| writer.write( | ||
| encoder.encode( | ||
| JSON.stringify({ | ||
| type: 'researchComplete', | ||
| }) + '\n', | ||
| ), | ||
| ); | ||
| safeWrite({ | ||
| type: 'researchComplete', | ||
| }); | ||
| } | ||
| } else if (event === 'end') { | ||
| writer.write( | ||
| encoder.encode( | ||
| JSON.stringify({ | ||
| type: 'messageEnd', | ||
| }) + '\n', | ||
| ), | ||
| ); | ||
| writer.close(); | ||
| safeWrite({ | ||
| type: 'messageEnd', | ||
| }); | ||
| safeClose(); | ||
| session.removeAllListeners(); | ||
| } else if (event === 'error') { | ||
| writer.write( | ||
| encoder.encode( | ||
| JSON.stringify({ | ||
| type: 'error', | ||
| data: data.data, | ||
| }) + '\n', | ||
| ), | ||
| ); | ||
| writer.close(); | ||
| safeWrite({ | ||
| type: 'error', | ||
| data: data.data, | ||
| }); | ||
| safeClose(); | ||
| session.removeAllListeners(); | ||
| } | ||
| }); | ||
|
|
@@ -234,7 +249,7 @@ export const POST = async (req: Request) => { | |
|
|
||
| req.signal.addEventListener('abort', () => { | ||
| disconnect(); | ||
| writer.close(); | ||
| safeClose(); | ||
| }); | ||
|
|
||
| return new Response(responseStream.readable, { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,64 +16,79 @@ export const POST = async ( | |||||||||||||
| const responseStream = new TransformStream(); | ||||||||||||||
| const writer = responseStream.writable.getWriter(); | ||||||||||||||
| const encoder = new TextEncoder(); | ||||||||||||||
| const keepAliveMs = 15_000; | ||||||||||||||
| let streamClosed = false; | ||||||||||||||
| let keepAliveInterval: ReturnType<typeof setInterval> | undefined; | ||||||||||||||
|
|
||||||||||||||
| const safeWrite = (payload: Record<string, unknown>) => { | ||||||||||||||
| if (streamClosed) return; | ||||||||||||||
|
|
||||||||||||||
| try { | ||||||||||||||
| writer.write(encoder.encode(JSON.stringify(payload) + '\n')); | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents
Suggested change
|
||||||||||||||
| } catch (error) { | ||||||||||||||
| console.warn('Failed to write reconnect stream payload:', error); | ||||||||||||||
| } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| const safeClose = () => { | ||||||||||||||
| if (streamClosed) return; | ||||||||||||||
|
|
||||||||||||||
| streamClosed = true; | ||||||||||||||
|
|
||||||||||||||
| if (keepAliveInterval) { | ||||||||||||||
| clearInterval(keepAliveInterval); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| try { | ||||||||||||||
| writer.close(); | ||||||||||||||
| } catch (error) { | ||||||||||||||
| console.warn('Failed to close reconnect stream:', error); | ||||||||||||||
| } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| keepAliveInterval = setInterval(() => { | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Keepalive interval is started before Prompt for AI agents |
||||||||||||||
| safeWrite({ type: 'keepAlive' }); | ||||||||||||||
| }, keepAliveMs); | ||||||||||||||
|
|
||||||||||||||
| safeWrite({ type: 'keepAlive' }); | ||||||||||||||
|
|
||||||||||||||
| const disconnect = session.subscribe((event, data) => { | ||||||||||||||
| if (event === 'data') { | ||||||||||||||
| if (data.type === 'block') { | ||||||||||||||
| writer.write( | ||||||||||||||
| encoder.encode( | ||||||||||||||
| JSON.stringify({ | ||||||||||||||
| type: 'block', | ||||||||||||||
| block: data.block, | ||||||||||||||
| }) + '\n', | ||||||||||||||
| ), | ||||||||||||||
| ); | ||||||||||||||
| safeWrite({ | ||||||||||||||
| type: 'block', | ||||||||||||||
| block: data.block, | ||||||||||||||
| }); | ||||||||||||||
| } else if (data.type === 'updateBlock') { | ||||||||||||||
| writer.write( | ||||||||||||||
| encoder.encode( | ||||||||||||||
| JSON.stringify({ | ||||||||||||||
| type: 'updateBlock', | ||||||||||||||
| blockId: data.blockId, | ||||||||||||||
| patch: data.patch, | ||||||||||||||
| }) + '\n', | ||||||||||||||
| ), | ||||||||||||||
| ); | ||||||||||||||
| safeWrite({ | ||||||||||||||
| type: 'updateBlock', | ||||||||||||||
| blockId: data.blockId, | ||||||||||||||
| patch: data.patch, | ||||||||||||||
| }); | ||||||||||||||
| } else if (data.type === 'researchComplete') { | ||||||||||||||
| writer.write( | ||||||||||||||
| encoder.encode( | ||||||||||||||
| JSON.stringify({ | ||||||||||||||
| type: 'researchComplete', | ||||||||||||||
| }) + '\n', | ||||||||||||||
| ), | ||||||||||||||
| ); | ||||||||||||||
| safeWrite({ | ||||||||||||||
| type: 'researchComplete', | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
| } else if (event === 'end') { | ||||||||||||||
| writer.write( | ||||||||||||||
| encoder.encode( | ||||||||||||||
| JSON.stringify({ | ||||||||||||||
| type: 'messageEnd', | ||||||||||||||
| }) + '\n', | ||||||||||||||
| ), | ||||||||||||||
| ); | ||||||||||||||
| writer.close(); | ||||||||||||||
| safeWrite({ | ||||||||||||||
| type: 'messageEnd', | ||||||||||||||
| }); | ||||||||||||||
| safeClose(); | ||||||||||||||
| disconnect(); | ||||||||||||||
| } else if (event === 'error') { | ||||||||||||||
| writer.write( | ||||||||||||||
| encoder.encode( | ||||||||||||||
| JSON.stringify({ | ||||||||||||||
| type: 'error', | ||||||||||||||
| data: data.data, | ||||||||||||||
| }) + '\n', | ||||||||||||||
| ), | ||||||||||||||
| ); | ||||||||||||||
| writer.close(); | ||||||||||||||
| safeWrite({ | ||||||||||||||
| type: 'error', | ||||||||||||||
| data: data.data, | ||||||||||||||
| }); | ||||||||||||||
| safeClose(); | ||||||||||||||
| disconnect(); | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| req.signal.addEventListener('abort', () => { | ||||||||||||||
| disconnect(); | ||||||||||||||
| writer.close(); | ||||||||||||||
| safeClose(); | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| return new Response(responseStream.readable, { | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
writer.write()rejections are not handled; synchronoustry/catchinsafeWritedoes not catch async stream write failures.Prompt for AI agents