Repository navigation
fix(sse-heartbeat): shape-aware keepalives keep streams alive through stricter proxies #2233
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
Merged
diegosouzapw
merged 10 commits into
cheaper-inference:release/v3.8.0
from
NomenAK:feat/sse-heartbeat-shape-aware-2026-05-13
May 14, 2026
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
41946c4
deps: bump mermaid from 11.14.0 to 11.15.0 (#2178)
dependabot[bot] 9d62a0b
chore(timeouts): tighten idle stream ceiling to 5min, surface SSE_HEA…
0854fa9
chore(open-sse): expose SSE_HEARTBEAT_INTERVAL_MS constant
d6c5abd
test(sse-heartbeat): assert shape-aware keepalive output
a533e16
feat(sse-heartbeat): emit shape-aware keepalive (anthropic/openai/res…
890c3ee
feat(chatCore): pass client format to heartbeat for proxy-safe keepalive
4cf341b
feat(responsesHandler): emit response.in_progress keepalives
62ee540
test(sse-heartbeat): integration coverage for shape + strip non-colli…
4483430
review(sse-heartbeat): apply gemini suggestions — identity passthroug…
db66a7b
Merge release/v3.8.0 into PR #2233 branch + revert idle timeout default
diegosouzapw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,79 @@ | ||
| export const DEFAULT_SSE_HEARTBEAT_INTERVAL_MS = 15_000; | ||
|
|
||
| export const HEARTBEAT_SHAPES = { | ||
| COMMENT: "comment", | ||
| ANTHROPIC_PING: "anthropic-ping", | ||
| OPENAI_CHUNK: "openai-chunk", | ||
| OPENAI_RESPONSES_IN_PROGRESS: "openai-responses-in-progress", | ||
| } as const; | ||
|
|
||
| export type HeartbeatShape = (typeof HEARTBEAT_SHAPES)[keyof typeof HEARTBEAT_SHAPES]; | ||
|
|
||
| export const DEFAULT_SSE_HEARTBEAT_SHAPE: HeartbeatShape = HEARTBEAT_SHAPES.COMMENT; | ||
|
|
||
| export function shapeForClientFormat( | ||
| clientResponseFormat: string | undefined | null, | ||
| ): HeartbeatShape { | ||
| switch (clientResponseFormat) { | ||
| case "claude": | ||
| return HEARTBEAT_SHAPES.ANTHROPIC_PING; | ||
| case "openai": | ||
| return HEARTBEAT_SHAPES.OPENAI_CHUNK; | ||
| case "openai-responses": | ||
| return HEARTBEAT_SHAPES.OPENAI_RESPONSES_IN_PROGRESS; | ||
| default: | ||
| return HEARTBEAT_SHAPES.COMMENT; | ||
| } | ||
| } | ||
|
|
||
| function buildHeartbeatPayload( | ||
| shape: HeartbeatShape, | ||
| opts: { chunkId?: string; chunkModel?: string } = {}, | ||
| ): string { | ||
| switch (shape) { | ||
| case HEARTBEAT_SHAPES.ANTHROPIC_PING: | ||
| return "event: ping\ndata: {}\n\n"; | ||
| case HEARTBEAT_SHAPES.OPENAI_RESPONSES_IN_PROGRESS: | ||
| return 'data: {"type":"response.in_progress"}\n\n'; | ||
| case HEARTBEAT_SHAPES.OPENAI_CHUNK: { | ||
| const payload = { | ||
| id: opts.chunkId ?? "omniroute-keepalive", | ||
| object: "chat.completion.chunk", | ||
| created: Math.floor(Date.now() / 1000), | ||
| model: opts.chunkModel ?? "omniroute", | ||
| choices: [{ index: 0, delta: {}, finish_reason: null }], | ||
| }; | ||
| return `data: ${JSON.stringify(payload)}\n\n`; | ||
| } | ||
| case HEARTBEAT_SHAPES.COMMENT: | ||
| default: | ||
| return `: keepalive ${new Date().toISOString()}\n\n`; | ||
| } | ||
| } | ||
|
|
||
| type SseHeartbeatTransformOptions = { | ||
| intervalMs?: number; | ||
| signal?: AbortSignal; | ||
| shape?: HeartbeatShape; | ||
| chunkId?: string; | ||
| chunkModel?: string; | ||
| }; | ||
|
|
||
| export function createSseHeartbeatTransform({ | ||
| intervalMs = DEFAULT_SSE_HEARTBEAT_INTERVAL_MS, | ||
| signal, | ||
| }: SseHeartbeatTransformOptions = {}) { | ||
| shape = DEFAULT_SSE_HEARTBEAT_SHAPE, | ||
| chunkId, | ||
| chunkModel, | ||
| }: SseHeartbeatTransformOptions = {}): TransformStream<Uint8Array, Uint8Array> { | ||
| if (!Number.isFinite(intervalMs) || intervalMs <= 0) { | ||
| return new TransformStream<Uint8Array, Uint8Array>({ | ||
| transform(chunk, controller) { | ||
| controller.enqueue(chunk); | ||
| }, | ||
| }); | ||
|
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. |
||
| } | ||
|
|
||
| let intervalId: ReturnType<typeof setInterval> | undefined; | ||
| const encoder = new TextEncoder(); | ||
|
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. |
||
|
|
||
|
|
@@ -27,7 +92,9 @@ export function createSseHeartbeatTransform({ | |
| } | ||
|
|
||
| try { | ||
| controller.enqueue(encoder.encode(`: keepalive ${new Date().toISOString()}\n\n`)); | ||
| controller.enqueue( | ||
| encoder.encode(buildHeartbeatPayload(shape, { chunkId, chunkModel })), | ||
| ); | ||
| } catch { | ||
| stop(); | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This adds a second heartbeat transform to the stream. The
handleChatCorefunction already applies an SSE heartbeat transform at the end of its pipeline (seechatCore.ts:4276). SincehandleChatCoreis called withconvertedBody.stream = true, it will always attach a heartbeat ifSSE_HEARTBEAT_INTERVAL_MSis enabled. This redundancy results in multiple keepalive events being sent to the client and may cause issues if the first heartbeat is processed bytransformStreamin an unexpected way.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.
Thanks for catching this! You're right there's a redundancy. The first heartbeat wrap (in
chatCore.ts:4276) emits shapecommentwhen called from this path — becauseresponsesHandlercallshandleChatCorewithclientRawRequest: null, soclientResponseFormatfalls back to the default. The second wrap here (responsesHandler.ts) overrides withOPENAI_RESPONSES_IN_PROGRESSto match the post-transform shape that downstream clients actually parse.Two ways to clean this up:
clientResponseFormat: "openai-responses"throughconvertedBodysochatCore's heartbeat picks the right shape directly, then remove this second wrap.omitHeartbeat: trueoption tohandleChatCoreand keep this wrap as the single authoritative emitter.I have a slight lean toward option 1 since it keeps a single point of control, but it touches the
convertedBodycontract slightly. Happy to do either — which would you prefer?