Skip to content

Commit 81b02ea

Browse files
author
Yuriy Bezsonov
committed
feat(aiagent): improve SSE streaming response parsing for multi-line data
1 parent 5c75740 commit 81b02ea

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

  • apps/aiagent/src/main/resources/static

apps/aiagent/src/main/resources/static/chat.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ async function processStreamingResponse(response, loadingId = null) {
297297
let lastUpdateTime = Date.now();
298298
let pauseIndicator = null;
299299
let isSSE = null;
300+
let eventDataLines = []; // Track data lines within current event
300301

301302
const pauseCheckInterval = setInterval(() => {
302303
const timeSinceUpdate = Date.now() - lastUpdateTime;
@@ -333,8 +334,13 @@ async function processStreamingResponse(response, loadingId = null) {
333334

334335
for (const line of lines) {
335336
if (line.startsWith('data:')) {
336-
const content = line.substring(5);
337-
fullResponse += (content === '' || content === '\r') ? '\n' : content;
337+
eventDataLines.push(line.substring(5));
338+
} else if (line === '' || line === '\r') {
339+
// Blank line = end of event, join data lines with \n per SSE spec
340+
if (eventDataLines.length > 0) {
341+
fullResponse += eventDataLines.join('\n');
342+
eventDataLines = [];
343+
}
338344
}
339345
}
340346
} else {
@@ -355,9 +361,9 @@ async function processStreamingResponse(response, loadingId = null) {
355361
}
356362
}
357363

358-
// Flush remaining SSE buffer
359-
if (isSSE && buffer && buffer.startsWith('data:')) {
360-
fullResponse += buffer.substring(5);
364+
// Flush remaining SSE data
365+
if (eventDataLines.length > 0) {
366+
fullResponse += eventDataLines.join('\n');
361367
}
362368
} finally {
363369
clearInterval(pauseCheckInterval);

0 commit comments

Comments
 (0)