Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apps/web/src/components/chat/blocks/subagentParse.regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,20 @@ describe("parseSubagentResults(正则化后)", () => {
}
});
});

it("folds plans and subagent results from a single callback in order", () => {
const text = '<ginno_plan_update plan="plan-1">Status &amp; details</ginno_plan_update>\n'
+ '<ginno_subagent_result session="s-1">Result</ginno_subagent_result>';
expect(foldSubagentResultBlocks([{ kind: "text", text }])).toMatchObject([
{ kind: "plan_update", planId: "plan-1", text: "Status & details" },
{ kind: "subagent_result", sessionId: "s-1", summary: "Result" },
]);
});


it("accepts legacy Worker attributes and preserves the callback message ID", () => {
const text = "<ginno_worker_result session='w1' msg_id='m1' from='Worker &amp; Co'>Report</ginno_worker_result>";
expect(foldSubagentResultBlocks([{ kind: "text", text }])).toMatchObject([
{ kind: "worker_result", sessionId: "w1", msgId: "m1", from: "Worker & Co", summary: "Report" },
]);
});
126 changes: 50 additions & 76 deletions apps/web/src/components/chat/blocks/subagentParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ export function parseSubagentResults(
return out;
}

const RETURN_ENV_RE = /<(ginno_subagent_result|ginno_plan_update|ginno_worker_result)\b([^>]*)>([\s\S]*?)<\/\1>/g;

function parseReturnCards(text: string): Block[] {
const cards: Block[] = [];
for (const match of text.matchAll(RETURN_ENV_RE)) {
const [raw, tag, attrs, body] = match;
if (tag === "ginno_plan_update") {
const status = decodeEntities(body).trim();
cards.push({
kind: "plan_update",
planId: decodeEntities(/plan="([^"]*)"/.exec(attrs)?.[1] ?? ""),
text: status.split("You can:", 1)[0].trim() || status,
});
} else if (tag === "ginno_worker_result") {
for (const result of parseWorkerResults(raw)) {
cards.push({ kind: "worker_result", ...result });
}
} else {
for (const result of parseSubagentResults(raw)) {
cards.push({ kind: "subagent_result", ...result, goal: result.goal || undefined });
}
}
}
return cards;
}

/** 单条结果标签(非合并场景)。 */
export function parseSubagentResult(
text: string,
Expand Down Expand Up @@ -131,33 +157,25 @@ export function parseWorkerResults(
viaFile: boolean;
filePath: string;
}> {
const doc = parseEnvelope(text);
if (!doc) return [];
const out: Array<{
sessionId: string;
msgId: string;
from: string;
status: string;
goal: string;
summary: string;
digest: boolean;
viaFile: boolean;
filePath: string;
}> = [];
for (const el of Array.from(doc.getElementsByTagName("ginno_worker_result"))) {
out.push({
sessionId: el.getAttribute("session") ?? "",
msgId: el.getAttribute("msg_id") ?? "",
from: el.getAttribute("from") ?? "",
status: el.getAttribute("status") ?? "",
goal: el.getAttribute("goal") ?? "",
summary: (el.textContent ?? "").trim(),
digest: el.getAttribute("kind") === "digest",
viaFile: el.getAttribute("via_file") === "1",
filePath: el.getAttribute("file") ?? "",
});
}
return out;
return Array.from(text.matchAll(/<ginno_worker_result\b([^>]*)>([\s\S]*?)<\/ginno_worker_result>/g),
([, attrs, body]) => {
const attr = (name: string) => {
const match = new RegExp(String.raw`(?:^|\s)${name}\s*=\s*(?:"([^"]*)"|'([^']*)')`).exec(attrs);
return decodeEntities(match?.[1] ?? match?.[2] ?? "");
};
return {
sessionId: attr("session"),
msgId: attr("msg_id"),
from: attr("from"),
status: attr("status"),
goal: attr("goal"),
summary: decodeEntities(body).trim(),
digest: attr("kind") === "digest",
viaFile: attr("via_file") === "1",
filePath: attr("file"),
};
},
);
}

/** assign_worker 注入的 <ginno_worker_assign>:worker 会话侧的任务简报
Expand Down Expand Up @@ -219,54 +237,22 @@ export function foldSubagentResultBlocks(blocks: Block[]): Block[] {
out.push(b);
continue;
}
// 清单状态信封(plan-orchestration §4):折专用清单卡(同一注入通道,
// child key 是 plan-<id>),与结果卡区分开。
const planDoc = parseEnvelope(b.text);
const planEls = planDoc
? Array.from(planDoc.getElementsByTagName("ginno_plan_update"))
: [];
if (planEls.length) {
for (const el of planEls) {
const raw = (el.textContent ?? "").trim();
const body = raw.split("You can:", 1)[0].trim() || raw;
out.push({
kind: "plan_update",
planId: el.getAttribute("plan") ?? "",
text: body,
});
}
const returns = parseReturnCards(b.text);
if (returns.length) {
out.push(...returns);
continue;
}
const brief = parseSubagentBrief(b.text);
if (brief) {
out.push({ kind: "subagent_brief", ...brief });
continue;
}
// worker 协议信封(worker-protocol-design §7):员工任务简报 / 回传
// 折专用卡——检查在 subagent 结果信封之前(两类信封互斥)。
// worker 协议信封(worker-protocol-design §7):员工任务简报。
const wAssign = parseWorkerAssign(b.text);
if (wAssign) {
out.push({ kind: "worker_assign", ...wAssign });
continue;
}
const wResults = parseWorkerResults(b.text);
if (wResults.length) {
for (const r of wResults) {
out.push({
kind: "worker_result",
sessionId: r.sessionId,
msgId: r.msgId || undefined,
from: r.from || undefined,
status: r.status || undefined,
goal: r.goal || undefined,
summary: r.summary,
digest: r.digest,
viaFile: r.viaFile || undefined,
filePath: r.filePath || undefined,
});
}
continue;
}
// 旧版委托注入(2026-10-05 信封化之前落库的):裸机器头 + delegation=
// 溯源行的 HumanMessage——同样折成结果卡(sessionId 取溯源行),重载后
// 不再以原始文本气泡出现。goal 无从恢复,卡片标题退回 backend。
Expand All @@ -290,19 +276,7 @@ export function foldSubagentResultBlocks(blocks: Block[]): Block[] {
});
continue;
}
const results = parseSubagentResults(b.text);
if (!results.length) {
out.push(b);
continue;
}
for (const r of results) {
out.push({
kind: "subagent_result",
sessionId: r.sessionId,
goal: r.goal || undefined,
summary: r.summary,
});
}
out.push(b);
}
return out;
}
Expand Down
21 changes: 20 additions & 1 deletion apps/web/src/components/chat/engine/handlers/02-turn-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { EngineCtx } from "../ctx";
import type { ServerEvent } from "./types";
import { omitRepeatedResults } from "../../blocks/subagentParse";
import { foldSubagentResultBlocks } from "@/components/chat/blocks";
import { foldSubagentResultBlocks, type Block } from "@/components/chat/blocks";
import type { ChatMsg } from "../../streamCore";
import { mid } from "../../streamCore";
import { ensureLive, markDelivered, upsertTurnMeta } from "../liveBubble";
Expand Down Expand Up @@ -68,6 +68,25 @@ export function onTurnStart(ctx: EngineCtx, sid: string, ev: ServerEvent) {
: msg,
);
}
if (srvTurn && Array.isArray(ev.task_events)) {
const blocks: Block[] = ev.task_events.flatMap((task) =>
task && typeof task.task === "string" ? [{
kind: "task_event" as const,
taskId: task.task,
ev: String(task.kind || ""),
title: String(task.title || ""),
exit: task.exit ?? null,
dur: String(task.dur || ""),
}] : [],
);
const eventId = `${srvTurn}-task-events`;
const store = ctx.storeRef.current[sid] ?? [];
if (blocks.length && !store.some((msg) => msg.id === eventId)) {
ctx.storeRef.current[sid] = store.flatMap((msg) =>
msg.id === id ? [{ id: eventId, role: "system" as const, blocks }, msg] : [msg],
);
}
}
// 渠道轮次回显(engine 端 echo_user 标记,turn.start 带 text/images):
// 桌面端没有 composer 的乐观气泡,IM 侧输入只能靠服务端回传才显示。
// id 与 turnId 同用服务端 turnId——与 history 的 user 消息 id==turnId
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/components/chat/stream/TranscriptList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ export function TranscriptList({
// (它不认识这些块,会渲染成空气泡)。worker 协议信封
// (<ginno_worker_result>/<ginno_worker_assign>)同通道同处理。
<div key={m.id} className="flex flex-col items-center gap-2">
<div className="w-full max-w-[85%]">
<div className="flex w-full max-w-[85%] flex-col gap-2">
<IndependentEvents blocks={m.blocks} />
<SubagentBlocks
session={session?.type === "subagent" ? session : undefined}
blocks={m.blocks.filter(
Expand Down
Loading
Loading