功能说明:\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 等关键路径
333 lines
8.5 KiB
TypeScript
333 lines
8.5 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
isWriteFileInFlight,
|
|
latestInFlightWriteFilePreview,
|
|
resolveWriteFilePath,
|
|
} from "./write-file-in-flight";
|
|
|
|
test("latestInFlightWriteFilePreview opens a write_file that is not the last tool call", () => {
|
|
const preview = latestInFlightWriteFilePreview([
|
|
{
|
|
id: "m1",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{
|
|
id: "w1",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/report.md",
|
|
content: "# 标题\n",
|
|
},
|
|
},
|
|
{ id: "p1", name: "present_files", args: { paths: ["/mnt/user-data/outputs/report.md"] } },
|
|
],
|
|
},
|
|
]);
|
|
assert.ok(preview);
|
|
assert.equal(preview?.previewKey, "m1:w1");
|
|
assert.match(preview?.artifactPath ?? "", /write-file:/);
|
|
assert.match(preview?.artifactPath ?? "", /report\.md/);
|
|
});
|
|
|
|
test("latestInFlightWriteFilePreview keeps markdown when a later json write is in flight", () => {
|
|
const preview = latestInFlightWriteFilePreview([
|
|
{
|
|
id: "m1",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{
|
|
id: "w1",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/report.md",
|
|
content: "# 标题\n",
|
|
},
|
|
},
|
|
{
|
|
id: "w2",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/task-reports.json",
|
|
content: "[{",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
]);
|
|
assert.ok(preview);
|
|
assert.equal(preview?.previewKey, "m1:w1");
|
|
assert.match(preview?.artifactPath ?? "", /report\.md/);
|
|
});
|
|
|
|
test("latestInFlightWriteFilePreview skips intercepted custom outputs markdown in writing mode", () => {
|
|
const messages = [
|
|
{
|
|
id: "m1",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{
|
|
id: "w1",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/ai-agent-architecture-paper.md",
|
|
content: "# 草稿\n",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
];
|
|
assert.equal(latestInFlightWriteFilePreview(messages, true), null);
|
|
});
|
|
|
|
test("latestInFlightWriteFilePreview keeps custom outputs markdown live in normal mode", () => {
|
|
const preview = latestInFlightWriteFilePreview([
|
|
{
|
|
id: "m1",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{
|
|
id: "w1",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/ai-agent-architecture-paper.md",
|
|
content: "# 草稿\n",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
]);
|
|
assert.ok(preview);
|
|
assert.match(preview?.artifactPath ?? "", /ai-agent-architecture-paper\.md/);
|
|
});
|
|
|
|
test("latestInFlightWriteFilePreview skips report.md while the first-turn pipeline is active", () => {
|
|
const preview = latestInFlightWriteFilePreview(
|
|
[
|
|
{ type: "human", content: "写一篇论文" },
|
|
{
|
|
id: "m-pipe",
|
|
type: "ai",
|
|
tool_calls: [{ id: "p1", name: "deep_research_report", args: { topic: "课题" } }],
|
|
},
|
|
{
|
|
id: "m-write",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{
|
|
id: "w1",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/report.md",
|
|
content: "# 我自己写\n",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
true,
|
|
);
|
|
assert.equal(preview, null);
|
|
});
|
|
|
|
test("latestInFlightWriteFilePreview still opens the injected research stub", () => {
|
|
const preview = latestInFlightWriteFilePreview(
|
|
[
|
|
{
|
|
id: "research-report-j1",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{
|
|
id: "research-report-j1-write",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/report.md",
|
|
content: "# 报告\n",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
true,
|
|
);
|
|
assert.ok(preview);
|
|
assert.match(preview?.artifactPath ?? "", /report\.md/);
|
|
});
|
|
|
|
test("writing mode still previews follow-up-turn writes after a later user message", () => {
|
|
// 续写轮(成功之后用户又发了新消息):真实 write_file 编辑照常预览。
|
|
const preview = latestInFlightWriteFilePreview(
|
|
[
|
|
{ type: "human", content: "写一篇论文" },
|
|
{
|
|
id: "m-pipe",
|
|
type: "ai",
|
|
tool_calls: [{ id: "p1", name: "deep_research_report", args: { topic: "课题" } }],
|
|
},
|
|
{
|
|
id: "m-result",
|
|
type: "tool",
|
|
tool_call_id: "p1",
|
|
name: "deep_research_report",
|
|
content: "研究报告已完成:已写入 report.md 并展示给用户。",
|
|
},
|
|
{ type: "human", content: "把第二章改写得更学术一些" },
|
|
{
|
|
id: "m-edit",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{
|
|
id: "w2",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/report.md",
|
|
content: "# 修订后的报告\n",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
true,
|
|
);
|
|
assert.ok(preview);
|
|
assert.equal(preview?.previewKey, "m-edit:w2");
|
|
});
|
|
|
|
test("normal mode keeps previewing pathless streaming drafts", () => {
|
|
const preview = latestInFlightWriteFilePreview([
|
|
{
|
|
id: "m1",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{ id: "w1", name: "write_file", args: { content: "# 没有路径的流式草稿\n" } },
|
|
],
|
|
},
|
|
]);
|
|
assert.ok(preview);
|
|
assert.match(preview?.artifactPath ?? "", /\.md/);
|
|
});
|
|
|
|
test("latestInFlightWriteFilePreview ignores workspace helper files", () => {
|
|
const preview = latestInFlightWriteFilePreview([
|
|
{
|
|
id: "m1",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{
|
|
id: "w1",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/workspace/scan_structure.py",
|
|
content: "print('scan')",
|
|
},
|
|
},
|
|
{
|
|
id: "w2",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/workspace/draft.html",
|
|
content: "<!doctype html><html></html>",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
]);
|
|
assert.equal(preview, null);
|
|
});
|
|
|
|
test("latestInFlightWriteFilePreview does not auto-open a json-only write", () => {
|
|
const preview = latestInFlightWriteFilePreview([
|
|
{
|
|
id: "m1",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{
|
|
id: "w1",
|
|
name: "write_file",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/task-reports.json",
|
|
content: "[{}]",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
]);
|
|
assert.equal(preview, null);
|
|
});
|
|
|
|
test("latestInFlightWriteFilePreview skips write_file that already has a tool result", () => {
|
|
const preview = latestInFlightWriteFilePreview([
|
|
{
|
|
id: "m1",
|
|
type: "ai",
|
|
tool_calls: [
|
|
{ id: "w1", name: "write_file", args: { path: "/mnt/user-data/outputs/report.md" } },
|
|
],
|
|
},
|
|
{ type: "tool", tool_call_id: "w1" },
|
|
]);
|
|
assert.equal(preview, null);
|
|
});
|
|
|
|
test("resolveWriteFilePath reads the virtual URL pathname", () => {
|
|
assert.equal(
|
|
resolveWriteFilePath("write-file:report.md?message_id=m1&tool_call_id=t1"),
|
|
"report.md",
|
|
);
|
|
assert.equal(resolveWriteFilePath("mnt/report.md"), "mnt/report.md");
|
|
});
|
|
|
|
test("isWriteFileInFlight is true only until that file's tool result arrives", () => {
|
|
const inFlight = {
|
|
messages: [
|
|
{
|
|
type: "ai",
|
|
tool_calls: [
|
|
{ id: "w1", name: "write_file", args: { path: "report.md" } },
|
|
],
|
|
},
|
|
],
|
|
};
|
|
assert.equal(isWriteFileInFlight(inFlight, "write-file:report.md?tool_call_id=w1"), true);
|
|
|
|
const completed = {
|
|
messages: [
|
|
{
|
|
type: "ai",
|
|
tool_calls: [
|
|
{ id: "w1", name: "write_file", args: { path: "report.md" } },
|
|
],
|
|
},
|
|
{ type: "tool", tool_call_id: "w1", name: "write_file" },
|
|
],
|
|
};
|
|
assert.equal(
|
|
isWriteFileInFlight(completed, "write-file:report.md?tool_call_id=w1"),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("a later thread turn does not treat a finished write as in-flight", () => {
|
|
const laterTurn = {
|
|
messages: [
|
|
{
|
|
type: "ai",
|
|
tool_calls: [
|
|
{ id: "w1", name: "write_file", args: { path: "report.md" } },
|
|
],
|
|
},
|
|
{ type: "tool", tool_call_id: "w1", name: "write_file" },
|
|
{ type: "human", content: "按方案 A" },
|
|
{
|
|
type: "ai",
|
|
tool_calls: [{ id: "c2", name: "web_search", args: { query: "x" } }],
|
|
},
|
|
],
|
|
};
|
|
assert.equal(
|
|
isWriteFileInFlight(laterTurn, "write-file:report.md?tool_call_id=w1"),
|
|
false,
|
|
);
|
|
});
|