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 qs from 'query-string';
const extractStreamRegx = /data:\s*({.*?})(?=\n|$)/g;
const extractStreamRegx = /(data|error):\s*({.*?})(?=\n|$)/g;
const extractJSON = (dataStr: string) => {
let match;
@@ -12,11 +12,16 @@ const extractJSON = (dataStr: string) => {
}
while ((match = extractStreamRegx.exec(dataStr)) !== null) {
try {
const jsonData = JSON.parse(match[1]);
results.push(jsonData);
} catch (error) {
console.error('JSON parse error:', error, 'for match:', match[1]);
const type = match[1]; // "data" or "error"
console.log('type========', type);
const jsonData = JSON.parse(match[2]);
if (type === 'error') {
results.push({ error: jsonData });
} else {
results.push(jsonData);
}
} catch (err) {
console.error('JSON parse error:', err, 'for match:', match[2]);
continue;
}
}
@@ -162,21 +167,24 @@ export const readStreamData = async (
}, throttleDelay);
let isReading = true;
let textBuffer = ''; // cache incomplete line
while (isReading) {
const { done, value } = await reader.read();
try {
const chunk = decoder.decode(value, { stream: true });
textBuffer += decoder.decode(value, { stream: true });
if (chunk.startsWith('error:')) {
const errorStr = chunk.slice(7).trim();
if (textBuffer.startsWith('error:')) {
const errorStr = textBuffer.slice(7).trim();
const jsonData = JSON.parse(errorStr);
bufferManager.add({ error: jsonData });
textBuffer = ''; // Clear buffer after processing error
} else {
extractJSON(chunk).forEach((data) => {
extractJSON(textBuffer).forEach((data) => {
bufferManager.add(data);
});
textBuffer = ''; // Clear buffer after processing
}
throttledCallback();
@@ -185,6 +193,10 @@ export const readStreamData = async (
}
if (done) {
textBuffer += decoder.decode();
if (textBuffer) {
extractJSON(textBuffer).forEach((data) => bufferManager.add(data));
}
isReading = false;
bufferManager.flush();
break;