Skip to content

Commit 3e7240f

Browse files
foxy1402Copilot
andcommitted
Improve SSE stream stability for IDE tools
- Disable socket timeout and enable keepalive for long streams - Explicitly flush response buffer after initial chunk - Log when client disconnects before receiving data - Track if any data was received to diagnose premature closes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 41cdb55 commit 3e7240f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/routes/chat.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ router.post('/', (req, res) => {
7979
if (stream) {
8080
setSSEHeaders(res);
8181

82+
// Disable socket timeout and enable keepalive for long-running streams
83+
req.socket.setTimeout(0);
84+
req.socket.setKeepAlive(true);
85+
8286
// Send initial empty delta immediately to establish SSE stream (required for IDE tools)
8387
const initialChunk = {
8488
id,
@@ -93,14 +97,19 @@ router.post('/', (req, res) => {
9397
};
9498
res.write(`data: ${JSON.stringify(initialChunk)}\n\n`);
9599

100+
// Explicitly flush the response buffer
101+
if (typeof res.flush === 'function') res.flush();
102+
96103
let lastFinishReason = 'stop';
104+
let hasReceivedData = false;
97105

98106
const child = runQoderRequest({
99107
prompt,
100108
model,
101109
flags,
102110
timeoutMs: QODER_TIMEOUT_MS,
103111
onChunk: (data) => {
112+
hasReceivedData = true;
104113
const content = extractTextContent(data.message);
105114
const toolCalls = extractToolCalls(data.message?.content);
106115
const finishReason = data.message?.stop_reason || null;
@@ -129,7 +138,12 @@ router.post('/', (req, res) => {
129138
},
130139
});
131140

132-
req.on('close', () => child.kill());
141+
req.on('close', () => {
142+
if (!hasReceivedData) {
143+
console.log('[chat/completions] Client disconnected before receiving any data');
144+
}
145+
child.kill();
146+
});
133147
} else {
134148
let fullContent = '';
135149
let finishReason = 'stop';

0 commit comments

Comments
 (0)