添加进度面板,优化消息处理逻辑

This commit is contained in:
2026-04-29 16:55:14 +08:00
parent 30d85173ee
commit 2c1afdc97c
6 changed files with 174 additions and 3 deletions
+27
View File
@@ -70,6 +70,33 @@ describe("streamAgentChat", () => {
]);
});
it("parses progress events", async () => {
apiFetch.mockResolvedValue({
ok: true,
body: makeStream([
'event: progress\ndata: {"session_id":"s1","id":"p1","phase":"tool","status":"running","title":"正在调用后端数据查询","detail":"GET /api/v1/demo"}\n\n',
'event: done\ndata: {"session_id":"s1"}\n\n',
]),
});
const events: Array<{ type: string; title?: string; status?: string; detail?: string }> = [];
await streamAgentChat({
message: "hi",
onEvent: (event) => events.push(event),
});
expect(events[0]).toEqual({
type: "progress",
sessionId: "s1",
id: "p1",
phase: "tool",
status: "running",
title: "正在调用后端数据查询",
detail: "GET /api/v1/demo",
});
});
it("emits error when response is not ok", async () => {
apiFetch.mockResolvedValue({
ok: false,
+23
View File
@@ -4,6 +4,15 @@ import { config } from "@config/config";
export type StreamEvent =
| { type: "token"; sessionId: string; content: string }
| { type: "done"; sessionId: string }
| {
type: "progress";
sessionId: string;
id: string;
phase: string;
status: "running" | "completed" | "error";
title: string;
detail?: string;
}
| {
type: "error";
sessionId?: string;
@@ -121,6 +130,10 @@ export const streamAgentChat = async ({
detail?: string;
tool?: string;
params?: Record<string, unknown>;
id?: string;
phase?: string;
status?: "running" | "completed" | "error";
title?: string;
};
if (event === "token") {
onEvent({
@@ -128,6 +141,16 @@ export const streamAgentChat = async ({
sessionId: parsed.session_id ?? "",
content: parsed.content ?? "",
});
} else if (event === "progress") {
onEvent({
type: "progress",
sessionId: parsed.session_id ?? "",
id: parsed.id ?? `${parsed.phase ?? "progress"}-${Date.now()}`,
phase: parsed.phase ?? "progress",
status: parsed.status ?? "running",
title: parsed.title ?? "正在处理",
detail: parsed.detail,
});
} else if (event === "done") {
onEvent({
type: "done",