-
Notifications
You must be signed in to change notification settings - Fork 15
fix(agent): keep idle event streams alive #2032
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
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 |
|---|---|---|
|
|
@@ -73,6 +73,8 @@ const errorWithClassificationSchema = z.object({ | |
|
|
||
| type MessageCallback = (message: unknown) => void; | ||
|
|
||
| export const SSE_KEEPALIVE_INTERVAL_MS = 25_000; | ||
|
|
||
| class NdJsonTap { | ||
| private decoder = new TextDecoder(); | ||
| private buffer = ""; | ||
|
|
@@ -329,41 +331,73 @@ export class AgentServer { | |
| ); | ||
| } | ||
|
|
||
| let keepaliveInterval: ReturnType<typeof setInterval> | null = null; | ||
| const clearKeepalive = (): void => { | ||
| if (keepaliveInterval) { | ||
| clearInterval(keepaliveInterval); | ||
| keepaliveInterval = null; | ||
| } | ||
| }; | ||
|
|
||
| const stream = new ReadableStream({ | ||
| start: async (controller) => { | ||
| const sseController: SseController = { | ||
| let sseController: SseController | null = null; | ||
| const encoder = new TextEncoder(); | ||
| const detachCurrentSseController = (): void => { | ||
| if (sseController) { | ||
| this.detachSseController(sseController); | ||
| } | ||
| }; | ||
| const enqueueSseFrame = (frame: string): void => { | ||
| try { | ||
| controller.enqueue(encoder.encode(frame)); | ||
| } catch { | ||
| clearKeepalive(); | ||
| detachCurrentSseController(); | ||
| } | ||
| }; | ||
|
|
||
| sseController = { | ||
| send: (data: unknown) => { | ||
| try { | ||
| controller.enqueue( | ||
| new TextEncoder().encode(`data: ${JSON.stringify(data)}\n\n`), | ||
| ); | ||
| } catch { | ||
| this.detachSseController(sseController); | ||
| } | ||
| enqueueSseFrame(`data: ${JSON.stringify(data)}\n\n`); | ||
| }, | ||
| close: () => { | ||
| try { | ||
| clearKeepalive(); | ||
| controller.close(); | ||
| } catch { | ||
| this.detachSseController(sseController); | ||
| detachCurrentSseController(); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| if (!this.session || this.session.payload.run_id !== payload.run_id) { | ||
| await this.initializeSession(payload, sseController); | ||
| } else { | ||
| this.session.sseController = sseController; | ||
| this.session.hasDesktopConnected = true; | ||
| this.replayPendingEvents(); | ||
| } | ||
| keepaliveInterval = setInterval(() => { | ||
| enqueueSseFrame(": keepalive\n\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. I didn't know that it's conventional to omit the message prefix🤯
Contributor
Author
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. ahaha yeah, SSE have specific rules there in the parser, here we basically want to send bytes over the idle socket to avoid read timeouts, if we were to use the other |
||
| }, SSE_KEEPALIVE_INTERVAL_MS); | ||
|
|
||
| try { | ||
| if ( | ||
| !this.session || | ||
| this.session.payload.run_id !== payload.run_id | ||
| ) { | ||
| await this.initializeSession(payload, sseController); | ||
| } else { | ||
| this.session.sseController = sseController; | ||
| this.session.hasDesktopConnected = true; | ||
| this.replayPendingEvents(); | ||
| } | ||
|
|
||
| this.sendSseEvent(sseController, { | ||
| type: "connected", | ||
| run_id: payload.run_id, | ||
| }); | ||
| this.sendSseEvent(sseController, { | ||
| type: "connected", | ||
| run_id: payload.run_id, | ||
| }); | ||
| } catch (error) { | ||
| clearKeepalive(); | ||
| throw error; | ||
| } | ||
| }, | ||
| cancel: () => { | ||
| clearKeepalive(); | ||
| this.logger.debug("SSE connection closed"); | ||
| if (this.session?.sseController) { | ||
| this.session.sseController = null; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.