fix: do not extract the error message from chat chunk
This commit is contained in:
@@ -56,4 +56,4 @@ const HighlightCode: React.FC<{
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default React.memo(HighlightCode);
|
export default HighlightCode;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user