fix: do not extract the error message from chat chunk

This commit is contained in:
jialin
2025-08-18 18:22:46 +08:00
parent eb2cae7a77
commit 3f243129a4
2 changed files with 23 additions and 11 deletions
+1 -1
View File
@@ -56,4 +56,4 @@ const HighlightCode: React.FC<{
); );
}; };
export default React.memo(HighlightCode); export default HighlightCode;
+22 -10
View File
@@ -1,7 +1,7 @@
import { throttle } from 'lodash'; import { throttle } from 'lodash';
import qs from 'query-string'; import qs from 'query-string';
const extractStreamRegx = /data:\s*({.*?})(?=\n|$)/g; const extractStreamRegx = /(data|error):\s*({.*?})(?=\n|$)/g;
const extractJSON = (dataStr: string) => { const extractJSON = (dataStr: string) => {
let match; let match;
@@ -12,11 +12,16 @@ const extractJSON = (dataStr: string) => {
} }
while ((match = extractStreamRegx.exec(dataStr)) !== null) { while ((match = extractStreamRegx.exec(dataStr)) !== null) {
try { try {
const jsonData = JSON.parse(match[1]); const type = match[1]; // "data" or "error"
results.push(jsonData); console.log('type========', type);
} catch (error) { const jsonData = JSON.parse(match[2]);
console.error('JSON parse error:', error, 'for match:', match[1]); if (type === 'error') {
results.push({ error: jsonData });
} else {
results.push(jsonData);
}
} catch (err) {
console.error('JSON parse error:', err, 'for match:', match[2]);
continue; continue;
} }
} }
@@ -162,21 +167,24 @@ export const readStreamData = async (
}, throttleDelay); }, throttleDelay);
let isReading = true; let isReading = true;
let textBuffer = ''; // cache incomplete line
while (isReading) { while (isReading) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
try { try {
const chunk = decoder.decode(value, { stream: true }); textBuffer += decoder.decode(value, { stream: true });
if (chunk.startsWith('error:')) { if (textBuffer.startsWith('error:')) {
const errorStr = chunk.slice(7).trim(); const errorStr = textBuffer.slice(7).trim();
const jsonData = JSON.parse(errorStr); const jsonData = JSON.parse(errorStr);
bufferManager.add({ error: jsonData }); bufferManager.add({ error: jsonData });
textBuffer = ''; // Clear buffer after processing error
} else { } else {
extractJSON(chunk).forEach((data) => { extractJSON(textBuffer).forEach((data) => {
bufferManager.add(data); bufferManager.add(data);
}); });
textBuffer = ''; // Clear buffer after processing
} }
throttledCallback(); throttledCallback();
@@ -185,6 +193,10 @@ export const readStreamData = async (
} }
if (done) { if (done) {
textBuffer += decoder.decode();
if (textBuffer) {
extractJSON(textBuffer).forEach((data) => bufferManager.add(data));
}
isReading = false; isReading = false;
bufferManager.flush(); bufferManager.flush();
break; break;