feat(agent): render local interactive answers and fold completed process

This commit is contained in:
2026-10-12 18:54:18 +08:00
parent e0eb6a721e
commit 2c5bf15cce
30 changed files with 1610 additions and 75 deletions
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { toAgentChatMessages, toAgentUiMessages } from "./agent-session-message-state";
import { applySessionStreamEvent, toAgentChatMessages, toAgentUiMessages } from "./agent-session-message-state";
describe("task context in persisted answers", () => {
it("keeps the report reference of each answer when history is restored", () => {
@@ -44,3 +44,42 @@ describe("task context in persisted answers", () => {
expect(messages[0]?.content).toBe("报告引用未记录。");
});
});
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("keeps text, component and summary in their original order across history restoration", () => {
const messages = toAgentUiMessages([{ id: "answer", role: "assistant", parts: [
{ type: "text", text: "下面是演练数据。" },
{ type: "data-answer_ui", id: "reading", data: block },
{ type: "text", text: "需要现场核验。" },
{ type: "data-final_answer", data: { content: "结论仍待确认。", ui_block_ids: ["reading"] } },
] }]);
const restored = toAgentChatMessages(messages, {}, {})[0];
expect(restored.answerParts?.map((part) => part.type)).toEqual(["text", "answer-ui", "text"]);
expect(restored.content).toContain("演练读数");
expect(restored.answerParts).toContainEqual({ type: "text", text: "需要现场核验。" });
expect(restored.content).not.toContain("需要现场核验。");
});
it("updates a block once and appends later tokens after it without duplicating final text", () => {
let messages = toAgentUiMessages([{ id: "answer", role: "assistant", parts: [{ type: "text", text: "演练:" }] }]);
messages = applySessionStreamEvent(messages, { type: "answer_ui", data: block });
messages = applySessionStreamEvent(messages, { type: "answer_ui", data: { ...block, fallback_text: "新读数" } });
messages = applySessionStreamEvent(messages, { type: "token", data: { content: "仍待核验。", ui_block_ids: ["reading"] } });
messages = applySessionStreamEvent(messages, { type: "final_answer", data: { content: "仍待核验。", ui_block_ids: ["reading"] } });
const answer = toAgentChatMessages(messages, {}, {})[0];
expect(answer.answerParts?.map((part) => part.type)).toEqual(["text", "answer-ui", "text"]);
expect(answer.content.match(/新读数/g)).toHaveLength(1);
expect(answer.content.match(/仍待核验/g)).toHaveLength(1);
});
it("falls back to text when an old or malformed UI block cannot be rendered", () => {
const messages = toAgentUiMessages([{ id: "answer", role: "assistant", parts: [{ type: "data-answer_ui", data: { ...block, version: "unknown" } }] }]);
expect(toAgentChatMessages(messages, {}, {})[0].answerParts).toEqual([{ type: "text", text: "演练读数" }]);
});
});
it("keeps a malformed persisted data payload from crashing the conversation", () => {
for (const data of [null, undefined, "invalid"]) {
const messages = toAgentUiMessages([{ id: "answer", role: "assistant", parts: [{ type: "data-answer_ui", data }] }]);
expect(toAgentChatMessages(messages, {}, {})[0].content).toBe("这段结构化内容无法显示。");
}
});
@@ -1,6 +1,8 @@
import type { UIMessage } from "ai";
import type { Dispatch, SetStateAction } from "react";
import {
parseAnswerUiBlock,
type AnswerUiPart,
applyPermissionResponse,
agentTaskContextReferenceSchema,
applyQuestionResponse,
@@ -20,6 +22,7 @@ type AgentUiDataParts = {
task_context: Record<string, unknown>;
execution_plan: Record<string, unknown>;
progress: Record<string, unknown>;
final_answer: Record<string, unknown>;
todo_update: Record<string, unknown>;
permission_request: Record<string, unknown>;
permission_response: Record<string, unknown>;
@@ -29,6 +32,7 @@ type AgentUiDataParts = {
tool_call: Record<string, unknown>;
frontend_action: Record<string, unknown>;
frontend_action_result: Record<string, unknown>;
answer_ui: Record<string, unknown>;
ui_envelope: Record<string, unknown>;
session_title: Record<string, unknown>;
};
@@ -115,10 +119,12 @@ export function toAgentChatMessages(
return [];
}
const answerParts = collectAnswerParts(message);
let next: AgentChatMessage = {
id: message.id,
role: message.role,
content: collectMessageText(message)
content: answerParts ? answerParts.map((part) => part.type === "text" ? part.text : part.block.fallback_text).join("\n\n") : collectMessageText(message),
...(answerParts ? { answerParts } : {})
};
for (const part of message.parts) {
@@ -128,6 +134,13 @@ export function toAgentChatMessages(
if (part.type === "data-task_context") {
const parsed = agentTaskContextReferenceSchema.safeParse(part.data);
if (parsed.success) next = { ...next, taskContext: parsed.data };
} else if (part.type === "data-final_answer") {
const content = getDataString(part.data, "content");
if (content?.trim()) {
const refs = part.data?.ui_block_ids;
const uiBlockIds = Array.isArray(refs) ? [...new Set(refs.filter((id): id is string => typeof id === "string"))].slice(0, 48) : [];
next = { ...next, finalAnswer: { content, uiBlockIds } };
}
} else if (part.type === "data-progress") {
next = {
...next,
@@ -164,6 +177,10 @@ export function toAgentChatMessages(
}
}
if (next.finalAnswer) {
const selected = new Set(next.finalAnswer.uiBlockIds);
next.content = [next.finalAnswer.content, ...(next.answerParts ?? []).flatMap((part) => part.type === "answer-ui" && selected.has(part.block.block_id) ? [part.block.fallback_text] : [])].join("\n\n");
}
return [applyQuestionOverrides(applyPermissionOverrides(next, permissionOverrides), questionOverrides)];
});
}
@@ -189,6 +206,7 @@ export function applySessionStreamEvent(messages: AgentUiMessage[], event: Agent
}
if (
event.type === "final_answer" ||
event.type === "progress" ||
event.type === "execution_plan" ||
event.type === "todo_update" ||
@@ -196,6 +214,7 @@ export function applySessionStreamEvent(messages: AgentUiMessage[], event: Agent
event.type === "permission_response" ||
event.type === "question_request" ||
event.type === "question_response" ||
event.type === "answer_ui" ||
event.type === "ui_envelope" ||
event.type === "session_title"
) {
@@ -374,11 +393,15 @@ function createAssistantUiMessage(): AgentUiMessage {
}
function appendTextToUiMessage(message: AgentUiMessage, text: string): AgentUiMessage {
const textIndex = message.parts.findIndex((part) => part.type === "text");
let textIndex = -1;
for (let index = message.parts.length - 1; index >= 0; index -= 1) {
if (message.parts[index].type === "text") { textIndex = index; break; }
}
if (message.parts.slice(textIndex + 1).some((part) => part.type === "data-answer_ui")) textIndex = -1;
if (textIndex === -1) {
return {
...message,
parts: [{ type: "text", text }, ...message.parts]
parts: [...message.parts, { type: "text", text }]
};
}
@@ -401,7 +424,8 @@ function upsertUiDataPart(
const id =
getDataString(data, "id") ??
getDataString(data, "request_id") ??
getDataString(data, "envelope_id");
getDataString(data, "envelope_id") ??
getDataString(data, "block_id");
const existingIndex =
id === undefined
? -1
@@ -479,3 +503,20 @@ function collectMessageText(message: AgentUiMessage) {
function isAgentDataPart(part: AgentUiMessage["parts"][number]): part is AgentDataPart {
return typeof part.type === "string" && part.type.startsWith("data-") && "data" in part;
}
function collectAnswerParts(message: AgentUiMessage): AnswerUiPart[] | undefined {
if (!message.parts.some((part) => part.type === "data-answer_ui" || part.type === "data-final_answer")) return undefined;
const result: AnswerUiPart[] = [];
for (const part of message.parts) {
if (part.type === "text" && part.text) result.push({ type: "text", text: part.text });
if (part.type === "data-answer_ui") {
const data = typeof part.data === "object" && part.data !== null ? part.data : {};
const { version, block_id, fallback_text, spec } = data;
const block = parseAnswerUiBlock({ version, block_id, fallback_text, spec });
if (block) result.push({ type: "answer-ui", block });
else result.push({ type: "text", text: typeof fallback_text === "string" ? fallback_text.slice(0, 4000) : "这段结构化内容无法显示。" });
}
}
return result;
}