feat(webui): streamline contextual message controls (#5831)

This commit is contained in:
chengyongru authored and GitHub committed 2026-09-22 16:37:35 +08:00
1 parent 99a7ec256f
commit 920efd00b8
24 files changed
+1776 -955

No files matched your search

+36 -251
View File
@@ -76,49 +76,6 @@ function activityMessages(extraReasoning = "", extraTool?: UIMessage): UIMessage
return rows;
}
function installAnimationFrameQueue() {
const originalRequest = window.requestAnimationFrame;
const originalCancel = window.cancelAnimationFrame;
const callbacks = new Map<number, FrameRequestCallback>();
let nextId = 1;
window.requestAnimationFrame = ((callback: FrameRequestCallback) => {
const id = nextId;
nextId += 1;
callbacks.set(id, callback);
return id;
}) as typeof window.requestAnimationFrame;
window.cancelAnimationFrame = ((id: number) => {
callbacks.delete(id);
}) as typeof window.cancelAnimationFrame;
return {
flush() {
const pending = Array.from(callbacks.entries());
callbacks.clear();
for (const [, callback] of pending) callback(0);
},
restore() {
window.requestAnimationFrame = originalRequest;
window.cancelAnimationFrame = originalCancel;
},
};
}
function setScrollGeometry(
element: HTMLElement,
geometry: { scrollHeight: number; clientHeight: number; scrollTop?: number },
) {
Object.defineProperties(element, {
scrollHeight: { configurable: true, value: geometry.scrollHeight },
clientHeight: { configurable: true, value: geometry.clientHeight },
scrollTop: {
configurable: true,
value: geometry.scrollTop ?? element.scrollTop,
writable: true,
},
});
}
function installReducedMotion() {
const original = window.matchMedia;
@@ -219,186 +176,39 @@ describe("AgentActivityCluster", () => {
expect(screen.queryByTestId("activity-step")).not.toBeInTheDocument();
});
it("jumps to the latest activity when opened", () => {
const raf = installAnimationFrameQueue();
try {
render(
<AgentActivityCluster
messages={activityMessages()}
isTurnStreaming
hasBodyBelow={false}
/>,
);
it.each([false, true])("uses one activity layout without nested scrolling (streaming: %s)", (isTurnStreaming) => {
const onExpandedChange = vi.fn();
const props = {
messages: activityMessages(),
isTurnStreaming,
hasBodyBelow: false,
...(isTurnStreaming ? {} : { expanded: true, hideHeader: true }),
onExpandedChange,
};
const view = render(<AgentActivityCluster {...props} />);
const header = screen.getByRole("button", { name: /^(Working|Worked)/ });
const content = screen.getByTestId("agent-activity-content");
const scrollport = screen.getByTestId("agent-activity-scroll");
setScrollGeometry(scrollport, {
scrollHeight: 1000,
clientHeight: 120,
scrollTop: 0,
});
expect(header).toHaveClass("h-7", "gap-1.5", "px-1.5");
expect(header.querySelector("svg")).toHaveClass("lucide-activity");
expect(header).toHaveAttribute("aria-expanded", "true");
expect(view.container.querySelector("[data-contextual-activity-guide]")).toBeInTheDocument();
expect(content).toHaveClass("ps-6");
expect(content).not.toHaveClass("max-h-[180px]");
expect(content).not.toHaveClass("overflow-y-auto");
expect(screen.queryByTestId("activity-scroll-fade-top")).not.toBeInTheDocument();
expect(screen.queryByTestId("activity-scroll-fade-bottom")).not.toBeInTheDocument();
act(() => {
raf.flush();
});
expect(scrollport.scrollTop).toBe(880);
} finally {
raf.restore();
}
});
it("follows new reasoning and tool activity while the user is at the bottom", () => {
const raf = installAnimationFrameQueue();
try {
const { rerender } = render(
<AgentActivityCluster
messages={activityMessages()}
isTurnStreaming
hasBodyBelow={false}
/>,
);
const scrollport = screen.getByTestId("agent-activity-scroll");
setScrollGeometry(scrollport, {
scrollHeight: 1000,
clientHeight: 120,
scrollTop: 0,
});
act(() => {
raf.flush();
});
rerender(
<AgentActivityCluster
messages={activityMessages(" with more detail", {
id: "t2",
role: "tool",
kind: "trace",
content: "open_browser()",
traces: ["open_browser()"],
createdAt: 3,
})}
isTurnStreaming
hasBodyBelow={false}
/>,
);
setScrollGeometry(scrollport, {
scrollHeight: 1500,
clientHeight: 120,
scrollTop: scrollport.scrollTop,
});
act(() => {
raf.flush();
});
expect(scrollport.scrollTop).toBe(1380);
} finally {
raf.restore();
}
});
it("does not pull the user down after they scroll up inside the activity pane", () => {
const raf = installAnimationFrameQueue();
try {
const { rerender } = render(
<AgentActivityCluster
messages={activityMessages()}
isTurnStreaming
hasBodyBelow={false}
/>,
);
const scrollport = screen.getByTestId("agent-activity-scroll");
setScrollGeometry(scrollport, {
scrollHeight: 1000,
clientHeight: 120,
scrollTop: 0,
});
act(() => {
raf.flush();
});
scrollport.scrollTop = 100;
fireEvent.scroll(scrollport);
rerender(
<AgentActivityCluster
messages={activityMessages(" still streaming")}
isTurnStreaming
hasBodyBelow={false}
/>,
);
setScrollGeometry(scrollport, {
scrollHeight: 1500,
clientHeight: 120,
scrollTop: scrollport.scrollTop,
});
act(() => {
raf.flush();
});
expect(scrollport.scrollTop).toBe(100);
} finally {
raf.restore();
}
});
it("feathers only the activity edges with clipped content", () => {
const raf = installAnimationFrameQueue();
try {
render(
<AgentActivityCluster
messages={activityMessages()}
isTurnStreaming
hasBodyBelow={false}
/>,
);
const scrollport = screen.getByTestId("agent-activity-scroll");
setScrollGeometry(scrollport, {
scrollHeight: 1000,
clientHeight: 120,
scrollTop: 0,
});
act(() => {
raf.flush();
});
expect(scrollport).toHaveAttribute("data-fade-top", "true");
expect(scrollport).toHaveAttribute("data-fade-bottom", "false");
const topFade = screen.getByTestId("activity-scroll-fade-top");
expect(scrollport).not.toContainElement(topFade);
expect(scrollport).not.toHaveClass("activity-scroll-fade");
expect(screen.queryByTestId("activity-scroll-fade-bottom")).not.toBeInTheDocument();
scrollport.scrollTop = 440;
fireEvent.scroll(scrollport);
expect(scrollport).toHaveAttribute("data-fade-top", "true");
expect(scrollport).toHaveAttribute("data-fade-bottom", "true");
expect(screen.getByTestId("activity-scroll-fade-top")).toBeInTheDocument();
expect(screen.getByTestId("activity-scroll-fade-bottom")).toBeInTheDocument();
scrollport.scrollTop = 0;
fireEvent.scroll(scrollport);
expect(scrollport).toHaveAttribute("data-fade-top", "false");
expect(scrollport).toHaveAttribute("data-fade-bottom", "true");
expect(screen.queryByTestId("activity-scroll-fade-top")).not.toBeInTheDocument();
expect(screen.getByTestId("activity-scroll-fade-bottom")).toBeInTheDocument();
setScrollGeometry(scrollport, {
scrollHeight: 100,
clientHeight: 120,
scrollTop: 0,
});
fireEvent.scroll(scrollport);
expect(scrollport).toHaveAttribute("data-fade-top", "false");
expect(scrollport).toHaveAttribute("data-fade-bottom", "false");
expect(screen.queryByTestId("activity-scroll-fade-top")).not.toBeInTheDocument();
expect(screen.queryByTestId("activity-scroll-fade-bottom")).not.toBeInTheDocument();
} finally {
raf.restore();
view.rerender(<AgentActivityCluster {...props} messages={activityMessages(" updated", {
id: "next-tool", role: "tool", kind: "trace", content: "open_browser()", createdAt: 3,
})} />);
expect(screen.getByTestId("agent-activity-content")).toBe(content);
expect(content).toHaveTextContent("thinking updated");
fireEvent.click(header);
if (isTurnStreaming) {
expect(screen.queryByTestId("agent-activity-content")).not.toBeInTheDocument();
} else {
expect(onExpandedChange).toHaveBeenCalledWith(false);
}
});
@@ -459,7 +269,7 @@ describe("AgentActivityCluster", () => {
hasBodyBelow
/>,
);
expect(screen.getByTestId("agent-activity-scroll")).toBeInTheDocument();
expect(screen.getByTestId("agent-activity-content")).toBeInTheDocument();
rerender(
<AgentActivityCluster
@@ -473,11 +283,11 @@ describe("AgentActivityCluster", () => {
/>,
);
expect(screen.getByTestId("agent-activity-scroll")).toBeInTheDocument();
expect(screen.getByTestId("agent-activity-content")).toBeInTheDocument();
act(() => {
vi.advanceTimersByTime(301);
});
expect(screen.queryByTestId("agent-activity-scroll")).not.toBeInTheDocument();
expect(screen.queryByTestId("agent-activity-content")).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Worked" })).toHaveAttribute(
"aria-expanded",
"false",
@@ -487,31 +297,6 @@ describe("AgentActivityCluster", () => {
}
});
it("keeps chevron color feedback faster than the drawer rotation", () => {
render(
<AgentActivityCluster
messages={[{
id: "r-motion",
role: "assistant",
content: "",
reasoning: "checking motion",
createdAt: 1,
}]}
isTurnStreaming={false}
hasBodyBelow
/>,
);
const button = screen.getByRole("button", { name: "Worked" });
expect(button).toHaveAttribute("data-thread-disclosure");
const chevron = button.querySelector("svg");
expect(chevron).toBeInTheDocument();
expect(chevron).toHaveClass("transition-colors", "duration-200");
expect(chevron?.parentElement).toHaveClass(
"transition-transform",
"[transition-duration:220ms]",
);
});
it("uses persisted turn latency for completed history instead of replay timestamps", () => {
render(
@@ -711,7 +496,7 @@ describe("AgentActivityCluster", () => {
const assertIndependentDiff = () => {
const diff = screen.getByTestId("file-edit-diff");
expect(diff).toBeVisible();
expect(diff.closest('[aria-hidden="true"], [inert], [data-testid="agent-activity-scroll"]'))
expect(diff.closest('[aria-hidden="true"], [inert], [data-testid="agent-activity-content"]'))
.toBeNull();
expect(screen.getAllByTestId("activity-file-reference")).toHaveLength(1);
};
@@ -1140,7 +925,7 @@ describe("AgentActivityCluster", () => {
);
expect(screen.queryByRole("button", { name: /edited app\.tsx/i })).not.toBeInTheDocument();
expect(screen.queryByTestId("agent-activity-scroll")).not.toBeInTheDocument();
expect(screen.queryByTestId("agent-activity-content")).not.toBeInTheDocument();
expect(screen.getByText("Edited")).toBeInTheDocument();
expect(screen.queryByTestId("activity-header-file-reference")).not.toBeInTheDocument();
expect(screen.getByTestId("activity-file-reference")).toHaveTextContent("src/app.tsx");