feat(frontend): 支持 Roundtable 写文件流式预览与消息流聚合展示

功能说明:\n- 为写文件工具链增加流式预览状态模型(in-flight、chunk 归并、已完成)\n- 在消息面板中支持 write_file 流式 artifact 的动态插入/更新与完成态呈现\n- 优化 artifact 详情页的文件名与来源显示,兼容流式生成中文件路径\n- 在 Roundtable 工作流 step2 流程中注入与监听 write_file 事件,支持多阶段消息统一展示\n- 补充单测覆盖:streaming-write-preview、write-file-in-flight、streaming-artifacts 等关键路径
This commit is contained in:
董宏凯 committed 2026-09-02 12:52:33 +08:00
1 parent 553f72604a
commit f5bf7897b9
11 files changed
+266 -44

No files matched your search

@@ -36,6 +36,7 @@ import { uploadFiles } from "@/core/uploads/api";
import { buildWriteFileVirtualUrl } from "@/core/artifacts/streaming-write-preview";
import {
collectRoundtableDeliverablePaths,
mergeRoundtableStreamingToolArgs,
newestChangedRoundtableArtifact,
resolveRoundtableStreamingWritePreview,
roundtableArtifactDiskSnapshot,
@@ -48,15 +49,21 @@ type RunFileRef = NonNullable<StreamMultiAgentRequest["files"]>[number];
type SeatDelivery = { name: string; content: string };
/**
* `agent_orchestration` 可在一轮总控中重复调用。用 tool call id 区分每一笔派活,
* 同一 id 的 task 参数则持续覆盖更新,供步骤卡实时显示。
* `agent_orchestration` / 写文件工具可在一轮内重复调用。用 tool call id 区分每一笔
* 派活或写入,避免用户点击较早的步骤时误打开后来同名调用的文件。
*/
function streamStepKey(
phase: StreamPhase,
toolName?: string,
toolCallId?: string,
): string {
if (phase === "tool_calling" && toolName === "agent_orchestration" && toolCallId) {
if (
phase === "tool_calling" &&
(toolName === "agent_orchestration" ||
toolName === "write_file" ||
toolName === "str_replace") &&
toolCallId
) {
return `${phase}:${toolName}:${toolCallId}`;
}
return `${phase}:${toolName ?? ""}`;
@@ -912,7 +919,12 @@ export function useStep2Orchestration(
sourceToolCallId:
sourceToolCallId ?? (idx >= 0 ? baseMsg.tool_calls[idx].sourceToolCallId : undefined),
name: toolName,
args,
// 进度步骤只保存可见参数(通常只有 path),不能让一次手动点击把先前
// 流进来的 content 覆盖掉;同理也要保住 content-first 帧随后补齐的 path。
args:
idx >= 0
? mergeRoundtableStreamingToolArgs(baseMsg.tool_calls[idx].args, args)
: { ...args },
};
const nextToolCalls =
idx >= 0
@@ -1067,12 +1079,16 @@ export function useStep2Orchestration(
const findSeatArtifactSource = useCallback(
(
rawPath: string,
preferredToolCallId?: string,
): {
seatThreadId: string;
messageId: string;
toolCallId: string;
/** stub 里捕获的文件内容(仅 write_file 有;str_replace / 跨账号草稿恢复时常为 undefined)。 */
content?: string;
isWriteFile: boolean;
/** 当前 write_file 尚未收到对应 tool result,真实文件可能还没有落盘。 */
inFlight: boolean;
} | null => {
const target = rawPath.trim();
if (!target) return null;
@@ -1081,30 +1097,70 @@ export function useStep2Orchestration(
const c = (tc.args as { content?: unknown })?.content;
return typeof c === "string" ? c : undefined;
};
let exactMatch:
| {
seatThreadId: string;
messageId: string;
toolCallId: string;
content?: string;
isWriteFile: boolean;
inFlight: boolean;
}
| null = null;
let basenameMatch:
| { seatThreadId: string; messageId: string; toolCallId: string; content?: string }
| {
seatThreadId: string;
messageId: string;
toolCallId: string;
content?: string;
isWriteFile: boolean;
inFlight: boolean;
}
| null = null;
for (const [seatThreadId, msgs] of Object.entries(
seatStubMessagesRef.current,
)) {
const msg = msgs?.[0];
if (!msg) continue;
for (const tc of msg.tool_calls) {
// 同一路径可能在同一轮被重写;倒序保证文件卡/步骤优先打开最后一次写入。
for (let toolIndex = msg.tool_calls.length - 1; toolIndex >= 0; toolIndex -= 1) {
const tc = msg.tool_calls[toolIndex];
if (tc.name !== "write_file" && tc.name !== "str_replace") continue;
const p =
typeof (tc.args as { path?: unknown })?.path === "string"
? ((tc.args as { path?: string }).path as string)
: "";
if (!p) continue;
const isWriteFile = tc.name === "write_file";
const inFlight =
isWriteFile &&
!msgs.some(
(message) => message.type === "tool" && message.tool_call_id === tc.id,
);
const source = {
seatThreadId,
messageId: msg.id,
toolCallId: tc.id,
content: readContent(tc),
isWriteFile,
inFlight,
};
if (p === target) {
return { seatThreadId, messageId: msg.id, toolCallId: tc.id, content: readContent(tc) };
if (
preferredToolCallId &&
(tc.id === preferredToolCallId || tc.sourceToolCallId === preferredToolCallId)
) {
return source;
}
if (!exactMatch) exactMatch = source;
continue;
}
if (!basenameMatch && (p.split("/").pop() ?? p) === targetBase) {
basenameMatch = { seatThreadId, messageId: msg.id, toolCallId: tc.id, content: readContent(tc) };
basenameMatch = source;
}
}
}
return basenameMatch;
return exactMatch ?? basenameMatch;
},
[],
);
@@ -1119,11 +1175,15 @@ export function useStep2Orchestration(
if (!normalized) return;
// present_files 呈现的文件多半是子智能体 write_file 写入的:真实内容/thread 在 stub 里。
// 先反查 stub:**仅当 stub 内存里确有内容**(流式期间 / 草稿恢复时捕获了 write_file content)
// 才走虚拟预览——关闭沙箱后再点也能显示,且不受总控 coordinator thread 在 artifacts 路由
// not found 的影响。
// 先反查 stub:write_file 尚未完成时即使正文还没有抵达,也必须留在虚拟预览。
// 真实文件尚未写盘时错误走 HTTP 会造成“点文件 → 加载 → 找不到文件”;正文到达后
// stub 的同一 tool call 会自动刷新预览。完成且无正文的旧草稿才降级走 HTTP。
const seatSource = findSeatArtifactSource(normalized);
if (seatSource && typeof seatSource.content === "string" && seatSource.content.length > 0) {
if (
seatSource &&
seatSource.isWriteFile &&
(seatSource.inFlight || typeof seatSource.content === "string")
) {
const virtualUrl = buildWriteFileVirtualUrl({
path: normalized,
messageId: seatSource.messageId,
@@ -1270,18 +1330,41 @@ export function useStep2Orchestration(
) => {
const seatThreadId = dialogue.agentThreadId;
if (!seatThreadId) return;
const [, toolName] = step.key.split(":");
if (toolName === "write_file" || toolName === "str_replace") {
const [, toolName, sourceToolCallId] = step.key.split(":");
if (toolName === "write_file") {
if (!step.args) return;
const path = resolveRoundtableStreamingWritePreview(toolName, step.args);
if (!path) return;
const { messageId, toolCallId } = upsertSeatToolCall(
// 点击步骤优先复用已由 upstream frame 建好的同一条 stub,绝不再次用
// `step.args`(只有路径)写回它。否则会把正在接收的正文覆盖为空。
const source = findSeatArtifactSource(path, sourceToolCallId);
if (
source &&
source.isWriteFile &&
(source.inFlight || typeof source.content === "string")
) {
const virtualUrl = buildWriteFileVirtualUrl({
path,
messageId: source.messageId,
toolCallId: source.toolCallId,
});
openSeatVirtualArtifact(source.seatThreadId, virtualUrl, false);
return;
}
// 极早期还没拿到 upstream frame 的兜底:先建虚拟调用,后续帧会合并内容。
const { messageId, toolCallId } = upsertSeatToolCall(seatThreadId, toolName, step.args);
openSeatVirtualArtifact(
seatThreadId,
toolName,
step.args,
buildWriteFileVirtualUrl({ path, messageId, toolCallId }),
false,
);
const virtualUrl = buildWriteFileVirtualUrl({ path, messageId, toolCallId });
openSeatVirtualArtifact(seatThreadId, virtualUrl, false);
return;
}
// str_replace 没有完整 content,不适合虚拟 write_file 预览;它的文件已存在,
// 直接走真实产物读取,避免打开一个永远为空的虚拟编辑器。
if (toolName === "str_replace") {
const path = resolveRoundtableStreamingWritePreview(toolName, step.args);
if (path) openArtifactPreview(seatThreadId, path);
return;
}
// read_file 不再可点(见 MessageStepsCard 的 isArtifactStep):智能体调用技能
@@ -1298,6 +1381,7 @@ export function useStep2Orchestration(
},
[
buildWriteFileVirtualUrl,
findSeatArtifactSource,
openArtifactPreview,
openSeatVirtualArtifact,
upsertSeatToolCall,
@@ -2192,7 +2276,7 @@ export function useStep2Orchestration(
onPhaseChange: (phase, toolName, args, toolCallId) => {
const label = phaseLabel(phase, "special", toolName);
setDialogueConfidence(a.bubbleId, label);
appendStepIfNew(a.bubbleId, label, `${phase}:${toolName ?? ""}`, args);
appendStepIfNew(a.bubbleId, label, streamStepKey(phase, toolName, toolCallId), args);
handleArtifactToolPhase(phase, toolName, args, a.seatThreadId, toolCallId);
},
onUpstreamFrame: (frame) => {
@@ -2662,7 +2746,7 @@ export function useStep2Orchestration(
onPhaseChange: (phase, toolName, args, toolCallId) => {
const label = phaseLabel(phase, "special", toolName);
setDialogueConfidence(subBubbleId, label);
appendStepIfNew(subBubbleId, label, `${phase}:${toolName ?? ""}`, args);
appendStepIfNew(subBubbleId, label, streamStepKey(phase, toolName, toolCallId), args);
handleArtifactToolPhase(phase, toolName, args, seatThreadId, toolCallId);
},
onUpstreamFrame: (frame) => {
@@ -3081,7 +3165,7 @@ export function useStep2Orchestration(
onPhaseChange: (phase, toolName, args, toolCallId) => {
const label = phaseLabel(phase, "special", toolName);
setDialogueConfidence(bubbleId, label);
appendStepIfNew(bubbleId, label, `${phase}:${toolName ?? ""}`, args);
appendStepIfNew(bubbleId, label, streamStepKey(phase, toolName, toolCallId), args);
handleArtifactToolPhase(phase, toolName, args, seatThreadId, toolCallId);
},
onUpstreamFrame: (frame) => {
@@ -3293,7 +3377,7 @@ export function useStep2Orchestration(
onPhaseChange: (phase, toolName, args, toolCallId) => {
const label = phaseLabel(phase, "special", toolName);
setDialogueConfidence(bubbleId, label);
appendStepIfNew(bubbleId, label, `${phase}:${toolName ?? ""}`, args);
appendStepIfNew(bubbleId, label, streamStepKey(phase, toolName, toolCallId), args);
handleArtifactToolPhase(phase, toolName, args, seatThreadId, toolCallId);
},
onUpstreamFrame: (frame) => {