fix(webui): keep edit diffs outside reasoning folds (#5714)
* fix(webui): keep edit diffs outside reasoning folds * fix(webui): retain live status after trailing edit diffs
This commit is contained in:
1 parent
7d845046e0
commit
b5d2a3fb8e
2 files changed
+131
-5
No files matched your search
@@ -156,11 +156,66 @@ interface AgentActivityClusterProps {
|
|||||||
onOpenFilePreview?: (path: string) => void;
|
onOpenFilePreview?: (path: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export function AgentActivityCluster(props: AgentActivityClusterProps) {
|
||||||
* One fold wrapping the complete middle of a turn: reasoning, model segments,
|
const displayMode = useFileEditDisplayMode();
|
||||||
* tool traces, and file edits. The final assistant answer stays outside it.
|
const messages = useMemo(() => coalesceActivityMessages(props.messages), [props.messages]);
|
||||||
*/
|
const editsByMessage = useMemo(
|
||||||
export function AgentActivityCluster({
|
() => summarizeFileEditsByMessage(messages, props.isTurnStreaming),
|
||||||
|
[messages, props.isTurnStreaming],
|
||||||
|
);
|
||||||
|
if (displayMode === "summary" || !editsByMessage.size) {
|
||||||
|
return <FoldedAgentActivity {...props} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Diff rows break the fold so they stay visible in their original timeline position.
|
||||||
|
const items: ReactNode[] = [];
|
||||||
|
let pending: UIMessage[] = [];
|
||||||
|
const flush = (last: boolean) => {
|
||||||
|
// A live turn still needs its status header when the last row is a diff.
|
||||||
|
if (!pending.length && !(last && props.isTurnStreaming)) return;
|
||||||
|
items.push(
|
||||||
|
<FoldedAgentActivity
|
||||||
|
{...props}
|
||||||
|
key={pending[0]?.id ?? "tail-status"}
|
||||||
|
messages={pending}
|
||||||
|
isTurnStreaming={last && props.isTurnStreaming}
|
||||||
|
retryStatus={last ? props.retryStatus : null}
|
||||||
|
hasBodyBelow={false}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
pending = [];
|
||||||
|
};
|
||||||
|
for (const message of messages) {
|
||||||
|
const edits = editsByMessage.get(message.id);
|
||||||
|
if (message.fileEdits?.length) {
|
||||||
|
const traces = traceLines(message).filter((line) => !isFileEditTraceLine(line));
|
||||||
|
if (traces.some((line) => line.trim())) {
|
||||||
|
pending.push({ ...message, traces, content: traces.at(-1) ?? "", fileEdits: undefined });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
pending.push(message);
|
||||||
|
}
|
||||||
|
if (edits?.length) {
|
||||||
|
flush(false);
|
||||||
|
items.push(
|
||||||
|
<FileEditGroup
|
||||||
|
key={`${message.id}:edits`}
|
||||||
|
edits={edits}
|
||||||
|
displayMode={displayMode}
|
||||||
|
onOpenFilePreview={props.onOpenFilePreview}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
flush(true);
|
||||||
|
return (
|
||||||
|
<div className={cn("flex w-full flex-col gap-0.5", props.hasBodyBelow && "mb-2")}>
|
||||||
|
{items}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function FoldedAgentActivity({
|
||||||
messages,
|
messages,
|
||||||
isTurnStreaming,
|
isTurnStreaming,
|
||||||
hasBodyBelow,
|
hasBodyBelow,
|
||||||
|
|||||||
@@ -645,6 +645,77 @@ describe("AgentActivityCluster", () => {
|
|||||||
assertBetween(screen.getByText("Edited"));
|
assertBetween(screen.getByText("Edited"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each(["diff", "collapsed_diff"] as const)(
|
||||||
|
"keeps %s edits outside reasoning folds in live and replayed activity",
|
||||||
|
(fileEditDisplayMode) => {
|
||||||
|
localStorage.setItem(
|
||||||
|
"nanobot-webui.settings-preferences",
|
||||||
|
JSON.stringify({ fileEditDisplayMode }),
|
||||||
|
);
|
||||||
|
const messages: UIMessage[] = [
|
||||||
|
{ id: "before", role: "assistant", content: "", reasoning: "Before edit", createdAt: 1 },
|
||||||
|
{
|
||||||
|
id: "edit", role: "tool", kind: "trace", content: "edit_file()", createdAt: 2,
|
||||||
|
traces: ["edit_file()"],
|
||||||
|
fileEdits: [{
|
||||||
|
call_id: "edit-call", tool: "edit_file", path: "src/app.tsx", phase: "end",
|
||||||
|
added: 1, deleted: 1, approximate: false, status: "done",
|
||||||
|
diff: unifiedFileDiff([
|
||||||
|
"--- a/src/app.tsx", "+++ b/src/app.tsx", "@@ -1 +1 @@", "-old", "+new",
|
||||||
|
]),
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "after", role: "assistant", content: "After edit",
|
||||||
|
activityKind: "model", createdAt: 3,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const { rerender, unmount } = render(
|
||||||
|
<AgentActivityCluster messages={messages} isTurnStreaming hasBodyBelow={false} />,
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
if (fileEditDisplayMode === "collapsed_diff") {
|
||||||
|
fireEvent.click(screen.getByTestId("file-edit-diff-toggle"));
|
||||||
|
}
|
||||||
|
const assertIndependentDiff = () => {
|
||||||
|
const diff = screen.getByTestId("file-edit-diff");
|
||||||
|
expect(diff.closest('[aria-hidden="true"], [inert], [data-testid="agent-activity-scroll"]'))
|
||||||
|
.toBeNull();
|
||||||
|
expect(screen.getByText("Before edit").compareDocumentPosition(diff)
|
||||||
|
& Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
||||||
|
expect(diff.compareDocumentPosition(screen.getByText("After edit"))
|
||||||
|
& Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
||||||
|
expect(screen.getAllByTestId("activity-file-reference")).toHaveLength(1);
|
||||||
|
};
|
||||||
|
assertIndependentDiff();
|
||||||
|
for (const toggle of document.querySelectorAll<HTMLButtonElement>("[data-thread-disclosure]")) {
|
||||||
|
fireEvent.click(toggle);
|
||||||
|
assertIndependentDiff();
|
||||||
|
}
|
||||||
|
rerender(<AgentActivityCluster messages={messages} isTurnStreaming={false} hasBodyBelow />);
|
||||||
|
assertIndependentDiff();
|
||||||
|
rerender(
|
||||||
|
<AgentActivityCluster
|
||||||
|
messages={messages.slice(0, 2)}
|
||||||
|
isTurnStreaming
|
||||||
|
hasBodyBelow={false}
|
||||||
|
retryStatus={{ state: "waiting", attempt: 1, max_attempts: 4, error_kind: "connection" }}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
expect(screen.getByRole("status", { name: "Connection failed · retrying in 0s · attempt 1/4" }))
|
||||||
|
.toBeVisible();
|
||||||
|
unmount();
|
||||||
|
render(<AgentActivityCluster messages={messages} isTurnStreaming={false} hasBodyBelow />);
|
||||||
|
if (fileEditDisplayMode === "collapsed_diff") {
|
||||||
|
fireEvent.click(screen.getByTestId("file-edit-diff-toggle"));
|
||||||
|
}
|
||||||
|
assertIndependentDiff();
|
||||||
|
} finally {
|
||||||
|
localStorage.removeItem("nanobot-webui.settings-preferences");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
it("renders file edit diffs and responds to preference changes", () => {
|
it("renders file edit diffs and responds to preference changes", () => {
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
"nanobot-webui.settings-preferences",
|
"nanobot-webui.settings-preferences",
|
||||||
|
|||||||
Reference in new issue
Block a user