Skip to content

Commit ea1cd39

Browse files
refactor(tui): sanitize streamed markdown once per delta (#127838)
1 parent 353d0ae commit ea1cd39

2 files changed

Lines changed: 26 additions & 17 deletions

File tree

‎src/tui/tui-formatters.ts‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ function formatAssistantErrorFromRecord(record: Record<string, unknown>): string
467467
return formatRawAssistantErrorForUi(errorMessage);
468468
}
469469

470-
function collectSanitizedBlockStrings(params: {
470+
function collectBlockStrings(params: {
471471
content: unknown;
472472
blockType: "text" | "thinking";
473473
valueKey: "text" | "thinking";
@@ -482,7 +482,7 @@ function collectSanitizedBlockStrings(params: {
482482
}
483483
const rec = block as Record<string, unknown>;
484484
if (rec.type === params.blockType && typeof rec[params.valueKey] === "string") {
485-
parts.push(sanitizeRenderableText(rec[params.valueKey] as string));
485+
parts.push(rec[params.valueKey] as string);
486486
}
487487
}
488488
return parts;
@@ -501,7 +501,7 @@ export function extractThinkingFromMessage(message: unknown): string {
501501
if (typeof content === "string") {
502502
return "";
503503
}
504-
const parts = collectSanitizedBlockStrings({
504+
const parts = collectBlockStrings({
505505
content,
506506
blockType: "thinking",
507507
valueKey: "thinking",
@@ -522,22 +522,26 @@ export function extractContentFromMessage(message: unknown): string {
522522

523523
if (record.role === "assistant") {
524524
if (typeof content === "string") {
525-
return sanitizeRenderableText(content).trim();
525+
return content.trim();
526526
}
527527
if (Array.isArray(content)) {
528-
return extractAssistantRenderableContent(record);
528+
const text = (extractAssistantPhaseText(record) ?? "").trim();
529+
const pairingQr = extractPairingQrTerminalText(record);
530+
return (
531+
[text, pairingQr].filter(Boolean).join("\n\n") || formatAssistantErrorFromRecord(record)
532+
);
529533
}
530534
}
531535

532536
if (typeof content === "string") {
533537
return sanitizeRenderableText(content).trim();
534538
}
535539

536-
const parts = collectSanitizedBlockStrings({
540+
const parts = collectBlockStrings({
537541
content,
538542
blockType: "text",
539543
valueKey: "text",
540-
});
544+
}).map(sanitizeRenderableText);
541545
if (parts.length > 0) {
542546
return parts.join("\n").trim();
543547
}
@@ -586,18 +590,14 @@ function extractTextBlocks(content: unknown, opts?: { includeThinking?: boolean
586590
return "";
587591
}
588592

589-
const textParts = collectSanitizedBlockStrings({
590-
content,
591-
blockType: "text",
592-
valueKey: "text",
593-
});
593+
const textParts = collectBlockStrings({ content, blockType: "text", valueKey: "text" }).map(
594+
sanitizeRenderableText,
595+
);
594596
const thinkingParts =
595597
opts?.includeThinking === true
596-
? collectSanitizedBlockStrings({
597-
content,
598-
blockType: "thinking",
599-
valueKey: "thinking",
600-
})
598+
? collectBlockStrings({ content, blockType: "thinking", valueKey: "thinking" }).map(
599+
sanitizeRenderableText,
600+
)
601601
: [];
602602

603603
return composeThinkingAndContent({

‎src/tui/tui-stream-assembler.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ describe("TuiStreamAssembler", () => {
7272
expect(second).toBe("[thinking]\nBrain\n\nHello");
7373
});
7474

75+
it("defers streamed terminal sanitization to the markdown boundary", () => {
76+
const assembler = new TuiStreamAssembler();
77+
const unsafe = "before\x1b]52;c;unsafe\x07after";
78+
79+
expect(assembler.ingestDelta("run-unsafe", messageWithContent([text(unsafe)]), false)).toBe(
80+
unsafe,
81+
);
82+
});
83+
7584
it("omits thinking when showThinking is false", () => {
7685
const assembler = new TuiStreamAssembler();
7786
const output = assembler.ingestDelta(

0 commit comments

Comments
 (0)