fix: prefer final-answer text in web chat previews

This commit is contained in:
Peter Steinberger
2026-04-05 20:27:16 +01:00
parent e92f55302b
commit a4f16f572c
6 changed files with 268 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { extractFirstTextBlock } from "./chat-message-content.js";
import { extractAssistantVisibleText, extractFirstTextBlock } from "./chat-message-content.js";
describe("shared/chat-message-content", () => {
it("extracts the first text block from array content", () => {
@@ -47,3 +47,66 @@ describe("shared/chat-message-content", () => {
expect(extractFirstTextBlock({ content: [{ text: 1 }, { text: "later" }] })).toBeUndefined();
});
});
describe("extractAssistantVisibleText", () => {
it("prefers final_answer text over commentary text", () => {
expect(
extractAssistantVisibleText({
role: "assistant",
content: [
{
type: "text",
text: "thinking like caveman",
textSignature: JSON.stringify({ v: 1, id: "msg_commentary", phase: "commentary" }),
},
{
type: "text",
text: "Actual final answer",
textSignature: JSON.stringify({ v: 1, id: "msg_final", phase: "final_answer" }),
},
],
}),
).toBe("Actual final answer");
});
it("does not fall back to commentary-only text", () => {
expect(
extractAssistantVisibleText({
role: "assistant",
content: [
{
type: "text",
text: "thinking like caveman",
textSignature: JSON.stringify({ v: 1, id: "msg_commentary", phase: "commentary" }),
},
],
}),
).toBeUndefined();
});
it("falls back to unphased legacy text", () => {
expect(
extractAssistantVisibleText({
role: "assistant",
content: [{ type: "text", text: "Legacy answer" }],
}),
).toBe("Legacy answer");
});
it("does not mix unphased legacy text into final_answer output", () => {
expect(
extractAssistantVisibleText({
role: "assistant",
phase: "final_answer",
content: [
{ type: "text", text: "Legacy answer" },
{
type: "text",
text: "Actual final answer",
textSignature: JSON.stringify({ v: 1, id: "msg_final", phase: "final_answer" }),
},
],
}),
).toBe("Actual final answer");
});
});