fix(webui): preserve oversized turn history with record pagination (#6586)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-11 17:32:47 +08:00
1 parent 5c1083df7f
commit 3c61bcfdf0
34 files changed
+3466 -380

No files matched your search

+92
View File
@@ -1802,6 +1802,98 @@ describe("ThreadViewport", () => {
}
});
it.each(["sequence", "stream", "shared-stream", "missing"] as const)(
"handles a fragment prepend with %s identity without selecting the wrong answer",
(identity) => {
const turnId = "partial-turn";
const answer = (id: string, content: string, turnSeq: number): UIMessage => ({
id, role: "assistant", content, createdAt: turnSeq, turnId, turnPhase: "answer", turnSeq,
...(identity === "stream" ? { streamId: `stream-${content}` } : {}),
...(identity === "shared-stream" ? { streamId: "shared-stream" } : {}),
});
const activity = (id: string, turnSeq: number): UIMessage => ({
id, role: "tool", kind: "trace", content: "tool", traces: ["tool"],
createdAt: turnSeq, turnId, turnPhase: "activity", turnSeq,
});
const tail = [answer("history-1", "tail one", 3), activity("history-2", 4), answer("history-3", "tail two", 5)];
const { container, rerender } = render(<ThreadViewport messages={tail} isStreaming={false} />);
const scroller = getScroller(container);
let prepended = false;
Object.defineProperties(scroller, {
scrollHeight: { configurable: true, get: () => prepended ? 2_580 : 2_400 },
clientHeight: { configurable: true, value: 600 },
scrollTop: { configurable: true, writable: true, value: 80 },
});
const originalRect = HTMLElement.prototype.getBoundingClientRect;
vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function (this: HTMLElement) {
if (this === scroller) return DOMRect.fromRect({ width: 800, height: 600 });
if (this.hasAttribute("data-thread-display-unit")) {
const top = this.textContent?.includes("tail one") ? (prepended ? 380 : 200) : 150;
return DOMRect.fromRect({ y: top - scroller.scrollTop, width: 800, height: 40 });
}
return originalRect.call(this);
});
const hitTest = stubElementsFromPoint(() => [
screen.getByText("tail one").closest<HTMLElement>("[data-thread-display-unit]")!,
]);
try {
act(() => dispatchUserScroll(scroller));
prepended = true;
const replayedSequence = identity === "stream" || identity === "missing";
rerender(<ThreadViewport isStreaming={false} messages={[
{ id: "history-0", role: "user", content: "earlier prompt", createdAt: 0, turnId, turnPhase: "user" },
answer("history-1", "earlier answer", 1), activity("history-2", 2),
answer("history-3", "tail one", replayedSequence ? 13 : 3),
activity("history-4", replayedSequence ? 14 : 4),
answer("history-5", "tail two", replayedSequence ? 15 : 5),
]} />);
// Without a matching source, do not jump to the earlier answer which reused its old id.
expect(scroller.scrollTop).toBe(identity === "missing" ? 80 : 260);
expect(screen.getByText("tail one").closest<HTMLElement>("[data-thread-display-unit]")!
.getBoundingClientRect().top).toBe(identity === "missing" ? 300 : 120);
} finally {
hitTest.restore();
}
},
);
it("keeps a unique answer anchored when live identity changes on replay", () => {
const live: UIMessage = {
id: "live-answer", role: "assistant", content: "same answer", createdAt: 1,
turnId: "live-turn", turnPhase: "answer", turnSeq: 2,
};
const { container, rerender } = render(<ThreadViewport messages={[live]} isStreaming={false} />);
const scroller = getScroller(container);
let replayed = false;
Object.defineProperties(scroller, {
scrollHeight: { configurable: true, get: () => replayed ? 2_480 : 2_400 },
clientHeight: { configurable: true, value: 600 },
scrollTop: { configurable: true, writable: true, value: 80 },
});
const originalRect = HTMLElement.prototype.getBoundingClientRect;
vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function (this: HTMLElement) {
if (this === scroller) return DOMRect.fromRect({ width: 800, height: 600 });
if (this.hasAttribute("data-thread-display-unit")) {
return DOMRect.fromRect({ y: (replayed ? 280 : 200) - scroller.scrollTop, width: 800, height: 40 });
}
return originalRect.call(this);
});
const hitTest = stubElementsFromPoint(() => [
screen.getByText("same answer").closest<HTMLElement>("[data-thread-display-unit]")!,
]);
try {
act(() => dispatchUserScroll(scroller));
const answerNode = screen.getByText("same answer");
replayed = true;
rerender(<ThreadViewport messages={[{ ...live, id: "replayed-answer", turnSeq: 11 }]} isStreaming={false} />);
expect(screen.getByText("same answer")).toBe(answerNode);
expect(scroller.scrollTop).toBe(160);
expect(answerNode.closest<HTMLElement>("[data-thread-display-unit]")!.getBoundingClientRect().top).toBe(120);
} finally {
hitTest.restore();
}
});
it.each(["wheel", "touch", "keyboard"] as const)(
"requests older history on %s intent when the initial page does not overflow",
(input) => {