fix(webui): keep edit diffs visible and distinguish file creation (#6049)

This commit is contained in:
chengyongru authored and GitHub committed 2026-10-04 16:55:48 +08:00
1 parent acdae3d0ae
commit ed022e447f
17 files changed
+181 -36

No files matched your search

@@ -156,7 +156,7 @@ describe("AgentActivityCluster", () => {
expect(screen.getByText(`正在读取文件 ${path}`)).toBeInTheDocument();
expect(screen.getByText("正在使用 Blender · --json --background scene.blend")).toBeInTheDocument();
expect(screen.getByText("正在打开 example.com · Browserbase")).toBeInTheDocument();
expect(screen.getByText("已编辑")).toBeInTheDocument();
expect(screen.getByText("编辑")).toBeInTheDocument();
expect(screen.getByTestId("activity-file-reference")).toHaveTextContent(path);
await act(async () => setAppLanguage("ja"));
@@ -166,7 +166,7 @@ describe("AgentActivityCluster", () => {
expect(screen.getByText("Blender を使用中 · --json --background scene.blend")).toBeInTheDocument();
expect(screen.getByText("example.com · 開いています · Browserbase")).toBeInTheDocument();
expect(screen.getByTestId("activity-file-reference").closest('[data-testid="activity-step"]'))
.toHaveTextContent(`${path} · 編集しました`);
.toHaveTextContent(`${path} · 編集`);
});
it("loads deferred trace details only after completed activity is expanded", async () => {
@@ -661,6 +661,7 @@ describe("AgentActivityCluster", () => {
expect(screen.getByTestId("file-edit-diff")).toBeInTheDocument();
expect(screen.getByText("return <Old />;")).toBeInTheDocument();
expect(screen.getByText("return <New />;")).toBeInTheDocument();
expect(screen.getByText("Edited")).toBeInTheDocument();
expect(screen.getByTestId("activity-file-reference")).toHaveTextContent("src/app.tsx");
expect(screen.getAllByTestId("activity-diff-pair")).toHaveLength(1);
@@ -668,6 +669,7 @@ describe("AgentActivityCluster", () => {
writeLocalPreferences({ ...DEFAULT_LOCAL_PREFS, fileEditDisplayMode: "summary" });
});
expect(screen.queryByTestId("file-edit-diff")).not.toBeInTheDocument();
expect(screen.getByText("Edited")).toBeInTheDocument();
act(() => {
writeLocalPreferences({ ...DEFAULT_LOCAL_PREFS, fileEditDisplayMode: "diff" });
@@ -937,6 +939,39 @@ describe("AgentActivityCluster", () => {
}
});
it.each([false, true])("labels created files with or without text diffs (hasDiff: %s)", (hasDiff) => {
localStorage.setItem(
"nanobot-webui.settings-preferences",
JSON.stringify({ fileEditDisplayMode: "diff" }),
);
try {
render(
<AgentActivityCluster
messages={[{
id: "create", role: "tool", kind: "trace", content: "edit_file()", createdAt: 1,
traces: ["edit_file()"],
fileEdits: [{
call_id: "call-create", tool: "edit_file", path: "tmp.txt", phase: "end",
added: hasDiff ? 1 : 0, deleted: 0, status: "done", operation: "create",
...(hasDiff ? { diff: unifiedFileDiff([
"--- tmp.txt", "+++ tmp.txt", "@@ -0,0 +1 @@", "+hello",
]) } : {}),
}],
}]}
isTurnStreaming={false}
hasBodyBelow={false}
/>,
);
expect(screen.getByText("Created")).toBeInTheDocument();
expect(screen.getByLabelText("Created tmp.txt")).toBeInTheDocument();
expect(screen.queryByText("Edited")).not.toBeInTheDocument();
expect(screen.getByTestId("activity-file-reference")).toHaveTextContent("tmp.txt");
expect(!!screen.queryByTestId("file-edit-diff")).toBe(hasDiff);
} finally {
localStorage.removeItem("nanobot-webui.settings-preferences");
}
});
it("labels whole-file deletes as deleted instead of edited", () => {
render(
<AgentActivityCluster
+65
View File
@@ -52,6 +52,71 @@ function openMessageBlockMenu(root: ParentNode): {
}
describe("ThreadMessages", () => {
it.each(["diff", "collapsed_diff"] as const)(
"keeps completed %s edits visible when answer activity is toggled",
(fileEditDisplayMode) => {
localStorage.setItem(
"nanobot-webui.settings-preferences",
JSON.stringify({ fileEditDisplayMode }),
);
const messages: UIMessage[] = [
{ id: "user", role: "user", content: "edit the file", createdAt: 1 },
{ id: "before", role: "assistant", content: "", reasoning: "Before edit", createdAt: 2 },
{
id: "edit", role: "tool", kind: "trace", content: "edit_file()", createdAt: 3,
traces: ["edit_file()"],
fileEdits: [{
call_id: "edit-call", tool: "edit_file", path: "src/app.tsx", phase: "end",
added: 1, deleted: 1, status: "done",
diff: {
format: "unified", context: 3, truncated: false,
text: "--- a/src/app.tsx\n+++ b/src/app.tsx\n@@ -1 +1 @@\n-old\n+new\n",
},
}],
},
{ id: "after", role: "assistant", content: "", reasoning: "After edit", createdAt: 4 },
{ id: "answer", role: "assistant", content: "File updated", latencyMs: 5_000, createdAt: 5 },
];
try {
const { container, rerender } = render(<ThreadMessages messages={messages} isStreaming />);
rerender(<ThreadMessages messages={messages} isStreaming={false} />);
if (fileEditDisplayMode === "collapsed_diff") {
fireEvent.click(screen.getByTestId("file-edit-diff-toggle"));
}
const assertIndependentDiff = () => {
const diff = screen.getByTestId("file-edit-diff");
expect(diff).toBeVisible();
expect(screen.getByText("Edited")).toBeInTheDocument();
expect(diff.closest('[aria-hidden="true"], [inert], [data-testid="agent-activity-content"]'))
.toBeNull();
expect(screen.getByText("File updated")).toBeVisible();
return diff;
};
assertIndependentDiff();
openAssistantBlockMenu(container);
const disclosure = screen.getByRole("button", { name: "Worked for 5s", exact: true });
expect(disclosure).toHaveAttribute("aria-expanded", "false");
const detailsId = disclosure.getAttribute("aria-controls")!;
expect(container.querySelectorAll(`[id="${detailsId}"]`)).toHaveLength(1);
fireEvent.click(disclosure);
const diff = assertIndependentDiff();
expect(screen.getByText("Before edit").compareDocumentPosition(diff)
& Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
expect(diff.compareDocumentPosition(screen.getByText("After edit"))
& Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
fireEvent.click(screen.getAllByRole("button", { name: /Worked for 5s.*Collapse activity/ })[0]);
assertIndependentDiff();
expect(screen.getByText("Before edit").closest('[aria-hidden="true"][inert]')).not.toBeNull();
expect(screen.getByText("After edit").closest('[aria-hidden="true"][inert]')).not.toBeNull();
rerender(<ThreadMessages messages={[messages[0], messages[2], messages[4]]} isStreaming={false} />);
assertIndependentDiff();
} finally {
localStorage.removeItem("nanobot-webui.settings-preferences");
}
},
);
it.each([0, -13_000, -14_000, -15_000, 15_000])(
"keeps the optimistic timer through acknowledgement and output with %i ms server clock skew",
(clockSkewMs) => {