fix: unify chat process and reply presentation

This commit is contained in:
2026-10-29 17:54:34 +08:00
parent 7621bf0f86
commit 9474788db6
46 changed files with 3160 additions and 500 deletions
@@ -2,6 +2,21 @@ import { describe, expect, it } from "vitest";
import { applySessionStreamEvent, toAgentChatMessages, toAgentUiMessages } from "./agent-session-message-state";
describe("task context in persisted answers", () => {
it("restores submitted answer cards only from snapshots bound to their user message", () => {
const reply = { version: "agent-interaction-request@1", interaction_id: "question", message_id: "source", turn_id: "turn", call_id: "call",
status: "answered", revision: 1, draft_revision: 0, draft: [], created_at: "2026-10-28T10:00:00Z",
questions: [{ question_id: "q", title: "核查时段", body: "请选择", mode: "single", required: true, allow_custom: false, options: [{ id: "a", label: "前一天" }] }],
answers: [{ question_id: "q", selected_option_ids: ["a"], selected_options: [{ id: "a", label: "前一天" }], custom_text: "" }],
delivery: { native_message_id: "reply", revision: 2, status: "delivered" } };
const parts = [{ type: "text", text: "核查时段: 前一天" }, { type: "data-interaction_reply", data: reply }];
const messages = toAgentChatMessages(toAgentUiMessages([
{ id: "reply", role: "user", parts }, { id: "other", role: "user", parts },
{ id: "reply", role: "assistant", parts },
{ id: "pending", role: "user", parts: [{ type: "data-interaction_reply", data: { ...reply, status: "pending", delivery: { ...reply.delivery, native_message_id: "pending" } } }] }
]), {}, {});
expect(messages[0].interactionReply).toEqual(reply);
expect(messages.slice(1).every((message) => message.interactionReply === undefined)).toBe(true);
});
it("inserts consumed user input once before the active assistant in live and resumed streams", () => {
const messages = toAgentUiMessages([
{ id: "original", role: "user", parts: [{ type: "text", text: "原问题" }] },
@@ -60,6 +75,20 @@ describe("task context in persisted answers", () => {
describe("rich answer replay", () => {
const block = { version: "answer-ui@1", block_id: "reading", fallback_text: "演练读数", spec: { root: "metric", elements: { metric: { type: "Metric", props: { label: "压力", value: "0.3", unit: "MPa", source: "演练数据" }, children: [] } } } };
it("restores anchored final content without process UI, duplicate blocks or internal reference markers", () => {
const messages = toAgentUiMessages([{ id: "answer", role: "assistant", parts: [
{ type: "data-answer_ui", data: { ...block, block_id: "query", phase: "process", fallback_text: "查询过程资料" } },
{ type: "data-answer_ui", data: { ...block, phase: "final" } },
{ type: "data-answer_ui", data: { ...block, phase: "final", fallback_text: "更新后的最终读数" } },
{ type: "data-final_answer", data: { content: "最终说明\n\n[[ui:reading]]\n\n后续解读\n\n[[ui:query]]", ui_block_ids: ["reading", "query"] } },
] }]);
const restored = toAgentChatMessages(messages, {}, {})[0];
expect(restored.answerParts?.filter((part) => part.type === "answer-ui")).toHaveLength(2);
expect(restored.content).toContain("更新后的最终读数");
expect(restored.content).not.toContain("查询过程资料");
expect(restored.content).not.toContain("[[ui:");
expect(restored.content.indexOf("更新后的最终读数")).toBeLessThan(restored.content.indexOf("后续解读"));
});
it("keeps text, component and summary in their original order across history restoration", () => {
const messages = toAgentUiMessages([{ id: "answer", role: "assistant", parts: [
{ type: "text", text: "下面是演练数据。" },