Skip to content

Commit c008e05

Browse files
fix: make SSE client robust to premature [DONE] in agentic proxy chains
1 parent 9b8d292 commit c008e05

File tree

1 file changed

+14
-19
lines changed
  • tools/server/webui/src/lib/services

1 file changed

+14
-19
lines changed

tools/server/webui/src/lib/services/chat.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ export class ChatService {
266266
let fullReasoningContent = '';
267267
let hasReceivedData = false;
268268
let lastTimings: ChatMessageTimings | undefined;
269+
let streamFinished = false;
269270

270271
try {
271272
let chunk = '';
@@ -281,18 +282,8 @@ export class ChatService {
281282
if (line.startsWith('data: ')) {
282283
const data = line.slice(6);
283284
if (data === '[DONE]') {
284-
if (!hasReceivedData && aggregatedContent.length === 0) {
285-
const contextError = new Error(
286-
'The request exceeds the available context size. Try increasing the context size or enable context shift.'
287-
);
288-
contextError.name = 'ContextError';
289-
onError?.(contextError);
290-
return;
291-
}
292-
293-
onComplete?.(aggregatedContent, fullReasoningContent || undefined, lastTimings);
294-
295-
return;
285+
streamFinished = true;
286+
continue;
296287
}
297288

298289
try {
@@ -330,13 +321,17 @@ export class ChatService {
330321
}
331322
}
332323

333-
if (!hasReceivedData && aggregatedContent.length === 0) {
334-
const contextError = new Error(
335-
'The request exceeds the available context size. Try increasing the context size or enable context shift.'
336-
);
337-
contextError.name = 'ContextError';
338-
onError?.(contextError);
339-
return;
324+
if (streamFinished) {
325+
if (!hasReceivedData && aggregatedContent.length === 0) {
326+
const contextError = new Error(
327+
'The request exceeds the available context size. Try increasing the context size or enable context shift.'
328+
);
329+
contextError.name = 'ContextError';
330+
onError?.(contextError);
331+
return;
332+
}
333+
334+
onComplete?.(aggregatedContent, fullReasoningContent || undefined, lastTimings);
340335
}
341336
} catch (error) {
342337
const err = error instanceof Error ? error : new Error('Stream error');

0 commit comments

Comments
 (0)