Skip to content

Commit 10b2c23

Browse files
committed
Fix final response chat rendering
1 parent a73b4c0 commit 10b2c23

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

web/src/lib/protocol.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,9 @@ function formatPartialJSON(raw: string): string {
10461046
// found.
10471047
export function extractResponseJSON(rawJson: string): StreamExtraction | null {
10481048
let obj: unknown;
1049+
const jsonText = stripLoggedResponseEnvelope(rawJson);
10491050
try {
1050-
obj = JSON.parse(rawJson);
1051+
obj = JSON.parse(jsonText);
10511052
} catch {
10521053
return null;
10531054
}
@@ -1225,6 +1226,20 @@ export function extractResponseJSON(rawJson: string): StreamExtraction | null {
12251226
return out.detected ? out : null;
12261227
}
12271228

1229+
function stripLoggedResponseEnvelope(text: string): string {
1230+
const trimmed = text.trim();
1231+
if (/^[\[{]/.test(trimmed)) return trimmed;
1232+
1233+
const normalized = text.replace(/\r\n/g, "\n");
1234+
if (!/^\s*Status:\s*\d{3}\b/m.test(normalized)) return trimmed;
1235+
1236+
const bodySeparator = /\n[ \t]*\n/.exec(normalized);
1237+
const body = bodySeparator
1238+
? normalized.slice(bodySeparator.index + bodySeparator[0].length).trim()
1239+
: "";
1240+
return body && /^[\[{]/.test(body) ? body : trimmed;
1241+
}
1242+
12281243
function appendResponseMarkdown(out: StreamExtraction, chunk: string) {
12291244
if (!chunk) return;
12301245
out.content += (out.content ? "\n\n" : "") + chunk;

0 commit comments

Comments
 (0)