fix(tui): narrow stream/final merge fallback

This commit is contained in:
Tseka Luk
2026-02-14 00:48:13 +08:00
committed by Peter Steinberger
parent 674a6765c5
commit cc93254742
2 changed files with 34 additions and 2 deletions

View File

@@ -141,4 +141,27 @@ describe("TuiStreamAssembler", () => {
expect(finalText).toBe("Before tool call\nAfter tool call");
});
it("prefers non-empty final payload when it is not a dropped block regression", () => {
const assembler = new TuiStreamAssembler();
assembler.ingestDelta(
"run-7",
{
role: "assistant",
content: [{ type: "text", text: "NOT OK" }],
},
false,
);
const finalText = assembler.finalize(
"run-7",
{
role: "assistant",
content: [{ type: "text", text: "OK" }],
},
false,
);
expect(finalText).toBe("OK");
});
});

View File

@@ -23,8 +23,17 @@ function mergeTextPreferRicher(currentText: string, nextText: string): string {
if (next.includes(current)) {
return next;
}
if (current.includes(next)) {
return current;
// Keep streamed text only when the newer payload looks like a dropped
// leading/trailing block (the known regression shape), not any substring.
const currentLines = current.split("\n");
const nextLines = next.split("\n");
if (currentLines.length > nextLines.length) {
const isDroppedLeadingBlock =
currentLines.slice(currentLines.length - nextLines.length).join("\n") === next;
const isDroppedTrailingBlock = currentLines.slice(0, nextLines.length).join("\n") === next;
if (isDroppedLeadingBlock || isDroppedTrailingBlock) {
return current;
}
}
return next;
}