AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
perf(frontend/copilot): virtualize the copilot transcript (#15740)
Abhimanyu Yadave1095ceae68b
diff --git a/autogpt_platform/frontend/package.json b/autogpt_platform/frontend/package.jsonindex d5360f4668..91a678f76c 100644--- a/autogpt_platform/frontend/package.json+++ b/autogpt_platform/frontend/package.json@@ -72,6 +72,7 @@"@streamdown/math": "1.0.1","@streamdown/mermaid": "1.0.1","@tanstack/react-query": "5.90.6",+ "@tanstack/react-virtual": "3.13.18","@types/jaro-winkler": "0.2.4","@vercel/analytics": "2.0.1","@vercel/speed-insights": "1.2.0",diff --git a/autogpt_platform/frontend/pnpm-lock.yaml b/autogpt_platform/frontend/pnpm-lock.yamlindex 60db4c8314..66c153fec0 100644--- a/autogpt_platform/frontend/pnpm-lock.yaml+++ b/autogpt_platform/frontend/pnpm-lock.yaml@@ -118,6 +118,9 @@ importers:'@tanstack/react-query':specifier: 5.90.6version: 5.90.6([email protected])+ '@tanstack/react-virtual':+ specifier: 3.13.18'@types/jaro-winkler':specifier: 0.2.4version: 0.2.4diff --git a/autogpt_platform/frontend/scripts/perf/lib/copilot-fixture.ts b/autogpt_platform/frontend/scripts/perf/lib/copilot-fixture.tsindex 0fd25e15a2..b8f99ff759 100644--- a/autogpt_platform/frontend/scripts/perf/lib/copilot-fixture.ts+++ b/autogpt_platform/frontend/scripts/perf/lib/copilot-fixture.ts@@ -35,6 +35,7 @@ interface Args {export const SESSION_L = "00000000-0000-4000-8000-0000000000a1";export const SESSION_M = "00000000-0000-4000-8000-0000000000a2";export const SESSION_S = "00000000-0000-4000-8000-0000000000a3";+export const SESSION_XL = "00000000-0000-4000-8000-0000000000a4";export const COPILOT_SESSIONS: SessionSpec[] = [{ id: SESSION_L, title: "Perf: long thread (200)", messageCount: 200 },@@ -42,6 +43,17 @@ export const COPILOT_SESSIONS: SessionSpec[] = [{ id: SESSION_S, title: "Perf: short thread (10)", messageCount: 10 },];+/** The 10-message chat and a 2,000-message one built by repeating the+ * recorded 200 rows, for `copilot-open-long-thread`. */+export const LONG_THREAD_SESSIONS: SessionSpec[] = [+ COPILOT_SESSIONS[2],+ {+ id: SESSION_XL,+ title: "Perf: very long thread (2000)",+ messageCount: 2000,+ },+];+function loadSessionRows(): SessionRow[] {const file = path.join(FIXTURES_DIR, "session-200.json");return (JSON.parse(fs.readFileSync(file, "utf8")) as { messages: [] })@@ -87,7 +99,7 @@ export function createCopilotFixture({function rowsFor(spec: SessionSpec) {const rows = [- ...allRows.slice(0, spec.messageCount),+ ...repeatRows(allRows, spec.messageCount),...(extraRows.get(spec.id) ?? []),];return rows.map((row) => ({ ...row, session_id: spec.id }));@@ -233,6 +245,28 @@ export function createCopilotFixture({};}+/** The first `count` rows, repeating the recorded ones with fresh ids,+ * sequences and tool call ids once past their end. Up to 200 this is the+ * recorded slice unchanged. */+function repeatRows(rows: SessionRow[], count: number): SessionRow[] {+ return Array.from({ length: count }, (_, index) => {+ const row = rows[index % rows.length];+ const round = Math.floor(index / rows.length);+ if (round === 0) return row;+ const toolCalls = row.tool_calls as { id: string }[] | null;+ return {+ ...row,+ id: `${row.id}-r${round}`,+ sequence: index,+ tool_calls: toolCalls?.map((call) => ({+ ...call,+ id: `${call.id}-r${round}`,+ })),+ tool_call_id: row.tool_call_id ? `${row.tool_call_id}-r${round}` : null,+ };+ });+}+async function until(condition: () => boolean,timeoutMs: number,diff --git a/autogpt_platform/frontend/scripts/perf/lib/copilot-page.ts b/autogpt_platform/frontend/scripts/perf/lib/copilot-page.tsindex d28f3311fa..678fffa1ee 100644--- a/autogpt_platform/frontend/scripts/perf/lib/copilot-page.ts+++ b/autogpt_platform/frontend/scripts/perf/lib/copilot-page.ts@@ -3,6 +3,7 @@ import {COPILOT_SESSIONS,createCopilotFixture,SESSION_L,+ type SessionSpec,} from "./copilot-fixture";import { installRoutes } from "./routes";import { settle, type ScenarioContext } from "./scenario";@@ -14,6 +15,7 @@ const READY_TIMEOUT_MS = 60_000;interface OpenArgs {ctx: ScenarioContext;+ sessions?: SessionSpec[];paceMs?: number;sessionID?: string;replyFixture?: string;@@ -26,13 +28,14 @@ interface OpenArgs {*/export async function openLongSession({ctx,+ sessions = COPILOT_SESSIONS,paceMs = LIVE_PACE_MS,sessionID = SESSION_L,replyFixture = REPLY_FIXTURE,}: OpenArgs) {const fixture = createCopilotFixture({userID: ctx.userID,- sessions: COPILOT_SESSIONS,+ sessions,stream: ctx.stream,replyFixture,paceMs,diff --git a/autogpt_platform/frontend/scripts/perf/scenarios/copilot-open-long-thread.ts b/autogpt_platform/frontend/scripts/perf/scenarios/copilot-open-long-thread.tsnew file mode 100644index 0000000000..c469048b9c--- /dev/null+++ b/autogpt_platform/frontend/scripts/perf/scenarios/copilot-open-long-thread.ts@@ -0,0 +1,43 @@+import {+ LONG_THREAD_SESSIONS,+ SESSION_S,+ SESSION_XL,+} from "../lib/copilot-fixture";+import { openLongSession } from "../lib/copilot-page";+import { settle, type Scenario } from "../lib/scenario";++const READY_TIMEOUT_MS = 15_000;+const OPEN_TIMEOUT_MS = 120_000;++export const copilotOpenLongThread: Scenario = {+ name: "copilot-open-long-thread",+ description:+ "Open a 2,000-message chat from the sidebar, coming from the 10-message one.",+ readyWhen:+ "the 10-message chat is open and the 2,000-message chat is listed in the sidebar, then network idle + 1 s",+ windowMs: 2_000,+ async setup(ctx) {+ const { page } = ctx;+ await openLongSession({+ ctx,+ sessionID: SESSION_S,+ sessions: LONG_THREAD_SESSIONS,+ });+ const title = LONG_THREAD_SESSIONS[1].title;+ const row = page+ .getByRole("link", { name: title, exact: true })+ .or(page.getByRole("button", { name: title }))+ .first();+ await row.waitFor({ state: "visible", timeout: READY_TIMEOUT_MS });+ await settle(page);+ return {+ async run() {+ await row.click();+ await page+ .locator(`[data-message-id^="${SESSION_XL}"]`)+ .last()+ .waitFor({ state: "visible", timeout: OPEN_TIMEOUT_MS });+ },+ };+ },+};diff --git a/autogpt_platform/frontend/scripts/perf/scenarios/copilot-switch-threads.ts b/autogpt_platform/frontend/scripts/perf/scenarios/copilot-switch-threads.tsindex 30dfdda912..22a12b1335 100644--- a/autogpt_platform/frontend/scripts/perf/scenarios/copilot-switch-threads.ts+++ b/autogpt_platform/frontend/scripts/perf/scenarios/copilot-switch-threads.ts@@ -20,8 +20,13 @@ export const copilotSwitchThreads: Scenario = {if (await expand.isVisible()) await expand.click();const loadAll = page.getByRole("button", { name: "Load all Otto chats" });if (await loadAll.isVisible()) await loadAll.click();+ // The AppSidebar lists chats as links, the copilot's own ChatSidebar as+ // buttons whose name also carries the date.const links = COPILOT_SESSIONS.map((session) =>- page.getByRole("link", { name: session.title, exact: true }),+ page+ .getByRole("link", { name: session.title, exact: true })+ .or(page.getByRole("button", { name: session.title }))+ .first(),);for (const link of links) {await link.waitFor({ state: "visible", timeout: READY_TIMEOUT_MS });diff --git a/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts b/autogpt_platform/frontend/scripts/perf/scenarios/registry.tsindex ee54394f64..a1ac2116df 100644--- a/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts+++ b/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts@@ -8,6 +8,7 @@ import { builderRunBeads } from "./builder-run-beads";import { builderTypeField } from "./builder-type-field";import { copilotBackgroundStream } from "./copilot-background-stream";import { copilotIdle } from "./copilot-idle";+import { copilotOpenLongThread } from "./copilot-open-long-thread";import { copilotResizeDrag } from "./copilot-resize-drag";import { copilotStreamCode } from "./copilot-stream-code";import { copilotStreamReply } from "./copilot-stream-reply";@@ -37,6 +38,7 @@ export const SCENARIOS: Scenario[] = [builderTypeField,builderBlockMenuSearch,builderLoadGraph,+ copilotOpenLongThread,];export function findScenario(name: string) {diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsxindex c56b9ebd6e..732e958dfb 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx@@ -41,6 +41,9 @@ import type { ExpertIdentity } from "../../useExpertMap";import { ChatMinimap } from "../ChatMinimap/ChatMinimap";import { getWorkRunMetadata } from "../WorkCard/helpers";import { TailSpacer } from "./components/TailSpacer";+import { VirtualTranscript } from "./components/VirtualTranscript/VirtualTranscript";+import type { TranscriptRow } from "./components/VirtualTranscript/helpers";+import type { JumpToMessage } from "./components/VirtualTranscript/useVirtualTranscript";import { MessageRow } from "./components/MessageRow/MessageRow";import { PendingAnswerContexts } from "./components/PendingAnswerContexts";import { ThreadHeader, type FloatingControls } from "./components/ThreadHeader";@@ -55,8 +58,10 @@ import { Clock01Icon } from "@hugeicons/core-free-icons";import { Icon } from "@/components/atoms/Icon/Icon";import { ChatErrorBanner } from "./components/ChatErrorBanner/ChatErrorBanner";+type ChatMessage = UIMessage<unknown, UIDataTypes, UITools>;+interface Props {- messages: UIMessage<unknown, UIDataTypes, UITools>[];+ messages: ChatMessage[];status: string;error: Error | undefined;isLoading: boolean;@@ -315,6 +320,16 @@ export function ChatMessagesContainer({() => buildMessageIndexById(messages),[messages],);+ const transcriptRows = useMemo(+ () => toTranscriptRows(renderRows),+ [renderRows],+ );+ // The minimap scrolls to rows by their DOM node; one the windowed+ // transcript has unmounted is reached through the transcript instead.+ const jumpRef = useRef<JumpToMessage | null>(null);+ const [jumpToUnmountedMessage] = useState(+ () => (messageID: string) => jumpRef.current?.(messageID),+ );const lastMessage = messages[messages.length - 1];const showPendingSend = !readOnly && !!pendingSend;// Read off the rendered rows: a fallback follow-up row the split drops in@@ -441,6 +456,60 @@ export function ChatMessagesContainer({}, [isRestoringActiveSession]);const restoreClock = useElapsedAnchor(showRestoreFallback);+ function renderRow({ message, rowIndex }: TranscriptRow) {+ if (getRowHeldCallKind(message) === "answered") {+ return (+ <Text variant="small" className="py-1 text-center text-zinc-500">+ {answeredLabel(renderRows, rowIndex)}+ </Text>+ );+ }++ const isLastRow = rowIndex === renderRows.length - 1;+ const isAssistant = message.role === "assistant";+ const isLastAssistant = isLastRow && isAssistant;+ const isCurrentlyStreaming = isLastAssistant && isActivelyStreaming;+ const nextRow = renderRows[rowIndex + 1];+ // A segment that only runs up to a mid-turn drain is never the end+ // of its turn — the same backend turn continues under the follow-up+ // bubble, so the stats bar and the assistant actions belong to the+ // last segment alone.+ const isLastInTurn =+ isAssistant &&+ !isMidTurnSegmentRow(message) &&+ (!nextRow || nextRow.role === "user");+ const showsTurnStats = isLastInTurn && !isCurrentlyStreaming;++ return (+ <MessageRow+ message={message}+ isLastAssistant={isLastAssistant}+ isCurrentlyStreaming={isCurrentlyStreaming}+ isLastInTurn={isLastInTurn}+ turnStat={turnStats?.get(message.id)}+ turnMessages={+ showsTurnStats+ ? turnMessagesForRow(messages, message, messageIndexById)+ : undefined+ }+ elapsedSeconds={isLastRow ? streamClock.frozenSeconds : undefined}+ indicator={isLastAssistant && showIndicator ? indicator : null}+ isSessionQueued={+ message.role === "user" && sessionChatStatus === "queued"+ }+ sessionSentFrom={+ isSessionOpeningMessage(message) ? sessionSentFrom : null+ }+ sessionID={sessionID}+ readOnly={readOnly}+ compact={isCompact}+ filePattern={filePattern}+ fileUrlBuilder={fileUrlBuilder}+ onRetry={isLastAssistant ? onRetry : undefined}+ />+ );+ }+const thread = (<>{showThreadHeader && (@@ -450,7 +519,12 @@ export function ChatMessagesContainer({floatingControls={floatingControls}/>)}- {!isCompact && <ChatMinimap messages={messages} />}+ {!isCompact && (+ <ChatMinimap+ messages={messages}+ onJumpToUnmounted={jumpToUnmountedMessage}+ />+ )}<Conversationkey={sessionID ?? "new"}resize="instant"@@ -490,72 +564,15 @@ export function ChatMessagesContainer({<LoadingSpinner className="text-neutral-600" /></div>)}- {renderRows.map((message, rowIndex) => {- // A run-post renders as a WorkCard inside the row, whatever else- // its metadata carries.- const heldCallRow = getWorkRunMetadata(message.metadata)- ? null- : getHeldCallRowKind(message.metadata);- if (heldCallRow === "result") return null;- if (heldCallRow === "answered") {- return (- <Text- key={message.id}- variant="small"- className="py-1 text-center text-zinc-500"- >- {answeredLabel(renderRows, rowIndex)}- </Text>- );- }-- const isLastRow = rowIndex === renderRows.length - 1;- const isAssistant = message.role === "assistant";- const isLastAssistant = isLastRow && isAssistant;- const isCurrentlyStreaming = isLastAssistant && isActivelyStreaming;- const nextRow = renderRows[rowIndex + 1];- // A segment that only runs up to a mid-turn drain is never the end- // of its turn — the same backend turn continues under the follow-up- // bubble, so the stats bar and the assistant actions belong to the- // last segment alone.- const isLastInTurn =- isAssistant &&- !isMidTurnSegmentRow(message) &&- (!nextRow || nextRow.role === "user");- const showsTurnStats = isLastInTurn && !isCurrentlyStreaming;-- return (- <MessageRow- key={message.id}- message={message}- isLastAssistant={isLastAssistant}- isCurrentlyStreaming={isCurrentlyStreaming}- isLastInTurn={isLastInTurn}- turnStat={turnStats?.get(message.id)}- turnMessages={- showsTurnStats- ? turnMessagesForRow(messages, message, messageIndexById)- : undefined- }- elapsedSeconds={- isLastRow ? streamClock.frozenSeconds : undefined- }- indicator={isLastAssistant && showIndicator ? indicator : null}- isSessionQueued={- message.role === "user" && sessionChatStatus === "queued"- }- sessionSentFrom={- isSessionOpeningMessage(message) ? sessionSentFrom : null- }- sessionID={sessionID}- readOnly={readOnly}- compact={isCompact}- filePattern={filePattern}- fileUrlBuilder={fileUrlBuilder}- onRetry={isLastAssistant ? onRetry : undefined}- />- );- })}+ {transcriptRows.length > 0 && (+ <VirtualTranscript+ rows={transcriptRows}+ renderRow={renderRow}+ lastUserMessageID={lastUserMessageID}+ compact={isCompact}+ jumpRef={jumpRef}+ />+ )}{showPendingSend && pendingSend && (<PendingUploadMessagependingSend={pendingSend}@@ -653,3 +670,23 @@ export function ChatMessagesContainer({</PendingAnswerContexts>);}++// A run-post renders as a WorkCard inside the row, whatever else its+// metadata carries.+function getRowHeldCallKind(message: ChatMessage) {+ return getWorkRunMetadata(message.metadata)+ ? null+ : getHeldCallRowKind(message.metadata);+}++// A held call's result row renders nothing, so it stays out of the list: an+// empty row would still take a gap.+function toTranscriptRows(renderRows: ChatMessage[]): TranscriptRow[] {+ const rows: TranscriptRow[] = [];+ renderRows.forEach((message, rowIndex) => {+ if (getRowHeldCallKind(message) !== "result") {+ rows.push({ message, rowIndex });+ }+ });+ return rows;+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/TailSpacer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/TailSpacer.tsxindex a5ab76f5a0..00e0767220 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/TailSpacer.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/TailSpacer.tsx@@ -48,16 +48,16 @@ export const TailSpacer = memo(function TailSpacer({applyHeight(0);return;}- const message = content.querySelector(- `[data-message-id="${CSS.escape(messageID)}"]`,- );- if (!(message instanceof HTMLElement)) {- applyHeight(0);- return;- }+ const selector = `[data-message-id="${CSS.escape(messageID)}"]`;+ if (!content.querySelector(selector)) applyHeight(0);function measure() {- if (!scroller || !spacer || !(message instanceof HTMLElement)) return;+ if (!scroller || !spacer || !content) return;+ // Looked up on every pass: the windowed transcript unmounts a row that+ // scrolls far out of view and mounts a new node when it comes back.+ // While it is away the last height stands.+ const message = content.querySelector(selector);+ if (!(message instanceof HTMLElement)) return;// Measured against the spacer's own top, which sits above its height —// reading the content box instead would feed this back into itself.const turnHeight =diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/VirtualTranscript.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/VirtualTranscript.tsxnew file mode 100644index 0000000000..76f955cfbe--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/VirtualTranscript.tsx@@ -0,0 +1,70 @@+"use client";++import { cn } from "@/lib/utils";+import type { MutableRefObject, ReactNode } from "react";+import type { TranscriptRow } from "./helpers";+import {+ useVirtualTranscript,+ type JumpToMessage,+} from "./useVirtualTranscript";++interface Props {+ rows: TranscriptRow[];+ renderRow: (row: TranscriptRow) => ReactNode;+ lastUserMessageID: string | null;+ compact: boolean;+ /** Set to a function that scrolls a row into view by message ID, for rows+ * the window has unmounted. */+ jumpRef?: MutableRefObject<JumpToMessage | null>;+}++/** The transcript's rows, windowed: only the rows on screen, a few either+ * side and the newest turn are mounted. Rows keep `key={message.id}`, so a+ * row's state survives for as long as it stays in the window. */+export function VirtualTranscript({+ rows,+ renderRow,+ lastUserMessageID,+ compact,+ jumpRef,+}: Props) {+ const { listRef, measureRow, segments, padding, isWindowed, isQuiet } =+ useVirtualTranscript({ rows, lastUserMessageID, compact, jumpRef });++ return (+ <div+ ref={listRef}+ className={cn("flex flex-col", compact ? "gap-4" : "gap-6")}+ style={{+ paddingTop: padding.top || undefined,+ paddingBottom: padding.bottom || undefined,+ overflowAnchor: isWindowed ? "none" : undefined,+ }}+ >+ {segments.map((segment) => {+ if (segment.kind === "gap") {+ return (+ <div+ key={`gap-${segment.afterIndex}`}+ aria-hidden+ style={{ height: segment.height }}+ />+ );+ }+ const row = rows[segment.index];+ const id = row.message.id;+ return (+ <div+ key={id}+ ref={measureRow}+ data-index={segment.index}+ data-key={id}+ className={cn("flex flex-col", isQuiet(id) && "[&>*]:animate-none")}+ >+ {renderRow(row)}+ </div>+ );+ })}+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/__tests__/VirtualTranscript.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/__tests__/VirtualTranscript.test.tsxnew file mode 100644index 0000000000..20684f0011--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/__tests__/VirtualTranscript.test.tsx@@ -0,0 +1,405 @@+import { act, cleanup, render, screen } from "@/tests/integrations/test-utils";+import type { UIDataTypes, UIMessage, UITools } from "ai";+import { useLayoutEffect, useRef, useState, type ReactNode } from "react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { VirtualTranscript } from "../VirtualTranscript";+import type { TranscriptRow } from "../helpers";+import type { JumpToMessage } from "../useVirtualTranscript";++type ChatMessage = UIMessage<unknown, UIDataTypes, UITools>;++// Every row measures what it is estimated at (a short text row), so the+// window's arithmetic is exact: 96 px rows with 24 px gaps.+const ROW_HEIGHT = 96;+const ROW_STRIDE = 120;+const VIEWPORT = 800;++const stick = vi.hoisted(() => ({+ scroller: null as HTMLDivElement | null,+ state: { isAtBottom: true },+}));++// A ref object, as StickToBottom's is: read at the moment it is used.+const scrollRef = {+ get current() {+ return stick.scroller;+ },+};++vi.mock("use-stick-to-bottom", () => ({+ useStickToBottomContext: () => ({ scrollRef, state: stick.state }),+}));++function makeScroller() {+ const scroller = document.createElement("div");+ Object.defineProperty(scroller, "clientHeight", {+ configurable: true,+ value: VIEWPORT,+ });+ Object.defineProperty(scroller, "offsetHeight", { value: VIEWPORT });+ Object.defineProperty(scroller, "offsetWidth", { value: 600 });+ scroller.getBoundingClientRect = () => ({ top: 0 }) as DOMRect;+ Object.defineProperty(scroller, "scrollHeight", { value: 1_000_000 });+ // The browser fires the scroll event later, never inside the call.+ scroller.scrollTo = ((options: ScrollToOptions) => {+ scroller.scrollTop = options.top ?? 0;+ }) as typeof scroller.scrollTo;+ document.body.appendChild(scroller);+ return scroller;+}++function message(index: number, role?: "user" | "assistant"): ChatMessage {+ return {+ id: `m${index}`,+ role: role ?? (index % 2 === 0 ? "user" : "assistant"),+ parts: [{ type: "text", text: `Message ${index}` }],+ } as ChatMessage;+}++function toRows(messages: ChatMessage[]): TranscriptRow[] {+ return messages.map((m, rowIndex) => ({ message: m, rowIndex }));+}++function Counter({ id }: { id: string }) {+ const [count, setCount] = useState(0);+ return (+ <button type="button" onClick={() => setCount((c) => c + 1)}>+ {id} clicked {count}+ </button>+ );+}++let jump: JumpToMessage | null = null;++function Transcript({ messages }: { messages: ChatMessage[] }) {+ const jumpRef = useRef<JumpToMessage | null>(null);+ jump = (id) => jumpRef.current?.(id);+ const lastUser = messages.findLast((m) => m.role === "user");+ return (+ <VirtualTranscript+ rows={toRows(messages)}+ renderRow={({ message: m }) => (+ <div data-message-id={m.id}>+ <span>{(m.parts[0] as { text: string }).text}</span>+ <Counter id={m.id} />+ </div>+ )}+ lastUserMessageID={lastUser?.id ?? null}+ compact={false}+ jumpRef={jumpRef}+ />+ );+}++function mountedIDs() {+ return Array.from(document.querySelectorAll("[data-message-id]")).map(+ (node) => node.getAttribute("data-message-id"),+ );+}++function scrollTo(top: number) {+ act(() => {+ stick.scroller!.scrollTop = top;+ stick.scroller!.dispatchEvent(new Event("scroll"));+ });+}++function bottomOffset(count: number) {+ return count * ROW_STRIDE - 24 - VIEWPORT;+}++function renderTranscript(messages: ChatMessage[]) {+ return render(<Transcript messages={messages} />, {+ container: stick.scroller!,+ });+}++const offsetHeight = Object.getOwnPropertyDescriptor(+ HTMLElement.prototype,+ "offsetHeight",+);+const getBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;++describe("VirtualTranscript", () => {+ beforeEach(() => {+ stick.scroller = makeScroller();+ stick.state.isAtBottom = true;+ Object.defineProperty(HTMLElement.prototype, "offsetHeight", {+ configurable: true,+ get(this: HTMLElement) {+ return this.dataset.index !== undefined ? ROW_HEIGHT : 0;+ },+ });+ // Everything inside the scroller sits at the top of its content.+ HTMLElement.prototype.getBoundingClientRect = function () {+ return { top: -(stick.scroller?.scrollTop ?? 0) } as DOMRect;+ };+ });++ afterEach(() => {+ cleanup();+ stick.scroller?.remove();+ HTMLElement.prototype.getBoundingClientRect = getBoundingClientRect;+ if (offsetHeight) {+ Object.defineProperty(+ HTMLElement.prototype,+ "offsetHeight",+ offsetHeight,+ );+ }+ });++ it("mounts only the rows near the viewport of a 500-message thread", () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ renderTranscript(messages);+ scrollTo(bottomOffset(500));++ const ids = mountedIDs();+ expect(ids).toContain("m499");+ expect(ids).not.toContain("m0");+ // About 7 rows fit the viewport, plus 6 overscan rows either side.+ expect(ids.length).toBeLessThanOrEqual(7 + 2 * 6 + 1);+ });++ it("keeps the list as tall as every row would be", () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ renderTranscript(messages);+ scrollTo(bottomOffset(500));+ const list = document.querySelector("[data-index]")!+ .parentElement as HTMLElement;+ const top = parseFloat(list.style.paddingTop || "0");+ const mounted = mountedIDs().length;+ // Rows above the window collapse into padding: whole rows plus gaps.+ expect(top).toBe((500 - mounted) * ROW_STRIDE);+ });++ it("windows the oldest rows when the user scrolls to the top, keeping the newest turn", () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ renderTranscript(messages);+ stick.state.isAtBottom = false;+ scrollTo(0);++ const ids = mountedIDs();+ expect(ids).toContain("m0");+ // The newest user message and the reply below it stay mounted.+ expect(ids).toContain("m498");+ expect(ids).toContain("m499");+ expect(ids).not.toContain("m250");+ });++ it("keeps the newest user message mounted when its turn runs past eight rows", () => {+ // The newest user message is row 480; nineteen assistant rows follow it.+ const messages = Array.from({ length: 500 }, (_, i) =>+ message(i, i <= 480 ? undefined : "assistant"),+ );+ renderTranscript(messages);+ stick.state.isAtBottom = false;+ scrollTo(0);++ const ids = mountedIDs();+ // TailSpacer measures the turn from this row.+ expect(ids).toContain("m480");+ expect(ids).toContain("m499");+ expect(ids).not.toContain("m485");+ });++ it("mounts a message appended while pinned to the bottom", () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ const { rerender } = renderTranscript(messages);+ scrollTo(bottomOffset(500));++ const next = [...messages, message(500, "user")];+ rerender(<Transcript messages={next} />);+ expect(mountedIDs()).toContain("m500");+ });++ it("does not move the scroll position when a message is appended while scrolled up", () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ const { rerender } = renderTranscript(messages);+ stick.state.isAtBottom = false;+ scrollTo(10 * ROW_STRIDE);++ rerender(<Transcript messages={[...messages, message(500, "user")]} />);+ expect(stick.scroller!.scrollTop).toBe(10 * ROW_STRIDE);+ expect(mountedIDs()).toContain("m10");+ });++ it("keeps the rows on screen mounted when older history is prepended", () => {+ const messages = Array.from({ length: 100 }, (_, i) => message(i + 50));+ const { rerender } = renderTranscript(messages);+ stick.state.isAtBottom = false;+ scrollTo(0);+ act(() => screen.getByRole("button", { name: "m50 clicked 0" }).click());++ const older = Array.from({ length: 50 }, (_, i) => message(i));+ // LoadMoreSentinel moves the scroll position down by what arrived.+ rerender(<Transcript messages={[...older, ...messages]} />);+ scrollTo(50 * ROW_STRIDE);++ expect(screen.getByRole("button", { name: "m50 clicked 1" })).toBeDefined();+ });++ it("scrolls an unmounted message into the window by id", async () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ renderTranscript(messages);+ scrollTo(bottomOffset(500));+ expect(mountedIDs()).not.toContain("m100");++ stick.state.isAtBottom = false;+ act(() => jump?.("m100"));+ act(() => {+ stick.scroller!.dispatchEvent(new Event("scroll"));+ });+ // Let the virtualizer's settle-and-verify frames run out.+ await act(() => new Promise((resolve) => setTimeout(resolve, 100)));++ expect(mountedIDs()).toContain("m100");+ expect(stick.scroller!.scrollTop).toBe(100 * ROW_STRIDE - 352);+ });++ it("skips the entrance animation for rows scrolling back into the window", () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ renderTranscript(messages);+ scrollTo(bottomOffset(500));+ stick.state.isAtBottom = false;+ scrollTo(0);++ const returning = document+ .querySelector('[data-message-id="m0"]')+ ?.closest("[data-index]");+ expect(returning?.className).toContain("[&>*]:animate-none");+ });++ describe("keeps the rows on screen still", () => {+ // Row 470 is taller than its guess: it measures 200 px, 104 px over.+ const TALL_INDEX = 470;+ const TALL_HEIGHT = 200;++ // A small flow layout: each row sits below the list's top padding and+ // the rows before it, 24 px apart.+ function layoutTop(node: HTMLElement) {+ const list = node.parentElement!;+ let top = parseFloat(list.style.paddingTop || "0");+ for (const sibling of Array.from(list.children) as HTMLElement[]) {+ if (sibling === node) break;+ top += (sibling.offsetHeight || parseFloat(sibling.style.height)) + 24;+ }+ return top - stick.scroller!.scrollTop;+ }++ beforeEach(() => {+ Object.defineProperty(HTMLElement.prototype, "offsetHeight", {+ configurable: true,+ get(this: HTMLElement) {+ if (this.dataset.index === undefined) return 0;+ return Number(this.dataset.index) === TALL_INDEX+ ? TALL_HEIGHT+ : ROW_HEIGHT;+ },+ });+ HTMLElement.prototype.getBoundingClientRect = function () {+ if (this.dataset.index === undefined) {+ return { top: -(stick.scroller?.scrollTop ?? 0) } as DOMRect;+ }+ const top = layoutTop(this);+ return { top, bottom: top + this.offsetHeight } as DOMRect;+ };+ });++ it("moves the scroll position by the error of a row that enters above the viewport", () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ renderTranscript(messages);+ stick.state.isAtBottom = false;+ // Row 478 at the top of the viewport: the window starts six rows+ // above, at 472.+ scrollTo(478 * ROW_STRIDE);+ expect(mountedIDs()).not.toContain(`m${TALL_INDEX}`);++ scrollTo(476 * ROW_STRIDE);++ expect(mountedIDs()).toContain(`m${TALL_INDEX}`);+ expect(stick.scroller!.scrollTop).toBe(+ 476 * ROW_STRIDE + (TALL_HEIGHT - ROW_HEIGHT),+ );+ });++ it("leaves the scroll position to StickToBottom while pinned", () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ renderTranscript(messages);+ scrollTo(478 * ROW_STRIDE);++ scrollTo(476 * ROW_STRIDE);++ expect(mountedIDs()).toContain(`m${TALL_INDEX}`);+ expect(stick.scroller!.scrollTop).toBe(476 * ROW_STRIDE);+ });++ it("puts the row being read back in place when older history arrives", () => {+ const messages = Array.from({ length: 100 }, (_, i) => message(i + 50));+ const { rerender } = renderTranscript(messages);+ stick.state.isAtBottom = false;+ scrollTo(0);++ const older = Array.from({ length: 50 }, (_, i) => message(i));+ // The page also merges ten rows further down, so the total height+ // grows by less than the height that arrived above.+ const merged = messages.filter((_, i) => i < 70 || i >= 80);+ rerender(<Transcript messages={[...older, ...merged]} />);++ expect(mountedIDs()).toContain("m50");+ expect(stick.scroller!.scrollTop).toBe(50 * ROW_STRIDE);+ });++ it("does not move for a row that enters below the viewport", () => {+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ renderTranscript(messages);+ stick.state.isAtBottom = false;+ scrollTo(457 * ROW_STRIDE);+ expect(mountedIDs()).not.toContain(`m${TALL_INDEX}`);++ scrollTo(458 * ROW_STRIDE);++ expect(mountedIDs()).toContain(`m${TALL_INDEX}`);+ expect(stick.scroller!.scrollTop).toBe(458 * ROW_STRIDE);+ });+ });++ it("windows rows when it mounts in the same commit as the scroll element", () => {+ const scroller = stick.scroller!;+ stick.scroller = null;+ // React attaches an ancestor's ref after its children's layout effects,+ // as when a session switch remounts StickToBottom and the transcript.+ function AttachScroller({ children }: { children: ReactNode }) {+ useLayoutEffect(() => {+ stick.scroller = scroller;+ }, []);+ return children;+ }+ const renderRow = vi.fn(({ message: m }: TranscriptRow) => (+ <div data-message-id={m.id} />+ ));+ const messages = Array.from({ length: 500 }, (_, i) => message(i));+ render(+ <AttachScroller>+ <VirtualTranscript+ rows={toRows(messages)}+ renderRow={renderRow}+ lastUserMessageID="m498"+ compact={false}+ />+ </AttachScroller>,+ { container: scroller },+ );++ expect(mountedIDs().length).toBeLessThan(30);+ // No pass ever rendered the whole thread.+ expect(renderRow.mock.calls.length).toBeLessThan(150);+ });++ it("renders every row when the scroll element has no height", () => {+ Object.defineProperty(stick.scroller, "clientHeight", { value: 0 });+ const messages = Array.from({ length: 60 }, (_, i) => message(i));+ renderTranscript(messages);+ expect(mountedIDs()).toHaveLength(60);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/helpers.tsnew file mode 100644index 0000000000..c676c7e05e--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/helpers.ts@@ -0,0 +1,176 @@+import type { UIDataTypes, UIMessage, UITools } from "ai";+import type { VirtualItem } from "@tanstack/react-virtual";++type ChatMessage = UIMessage<unknown, UIDataTypes, UITools>;++export interface TranscriptRow {+ message: ChatMessage;+ /** Position in the full list of rendered rows, which also holds the rows+ * the transcript leaves out (a held call's result row). */+ rowIndex: number;+}++type TranscriptSegment =+ | { kind: "row"; index: number }+ | { kind: "gap"; afterIndex: number; height: number };++/** Rows kept mounted above and below the visible window. */+export const OVERSCAN_ROWS = 6;++/** Most rows from the newest user message to the end that stay mounted+ * wherever the user scrolls, so the live turn and `TailSpacer`'s anchor+ * never unmount. */+const MAX_PINNED_TAIL_ROWS = 8;++const TEXT_BUCKETS: { maxChars: number; height: number }[] = [+ { maxChars: 200, height: 96 },+ { maxChars: 1_000, height: 240 },+ { maxChars: 4_000, height: 560 },+];+const HUGE_ROW_HEIGHT = 1_200;+const TOOL_PART_HEIGHT = 44;+const MAX_COUNTED_TOOL_PARTS = 10;++/**+ * Height guess for a row that has not been measured yet. Bucketed by text+ * length so a handful of very tall replies cannot inflate the guess for+ * short ones: when a short row measures far below its guess, the total+ * shrinks and pulls the next row into the window in the same layout pass,+ * and a long chain of those can hit React's update-depth limit.+ */+export function estimateRowHeight(message: ChatMessage): number {+ let chars = 0;+ let toolParts = 0;+ for (const part of message.parts) {+ if (part.type === "text" || part.type === "reasoning") {+ chars += part.text.length;+ } else if (part.type.startsWith("tool-")) {+ toolParts += 1;+ }+ }+ const bucket = TEXT_BUCKETS.find((entry) => chars <= entry.maxChars);+ const textHeight = bucket ? bucket.height : HUGE_ROW_HEIGHT;+ return (+ textHeight + Math.min(toolParts, MAX_COUNTED_TOOL_PARTS) * TOOL_PART_HEIGHT+ );+}++/** Rows that stay mounted however far the user scrolls: the newest user row,+ * which `TailSpacer` measures from, and the rows of its turn up to the last+ * `MAX_PINNED_TAIL_ROWS`. */+function pinnedIndexes(+ rows: TranscriptRow[],+ lastUserMessageID: string | null,+): number[] {+ if (!lastUserMessageID) return [];+ const userIndex = rows.findLastIndex(+ (row) => row.message.id === lastUserMessageID,+ );+ if (userIndex < 0) return [];+ const pinned = [userIndex];+ const tailStart = Math.max(userIndex + 1, rows.length - MAX_PINNED_TAIL_ROWS);+ for (let i = tailStart; i < rows.length; i++) pinned.push(i);+ return pinned;+}++/** The window's row indexes plus the pinned ones, ascending and unique. */+function mergeRenderedIndexes(+ windowIndexes: number[],+ pinned: number[],+): number[] {+ const merged = new Set([...windowIndexes, ...pinned]);+ return Array.from(merged).sort((a, b) => a - b);+}++/**+ * Lays the rendered rows out in normal flow: the list's own padding stands in+ * for the rows above and below, and a spacer fills any hole between two+ * rendered rows (the window and the pinned tail). Each spacer sits between two+ * flex gaps, so it is two gaps shorter than the distance it covers.+ */+function layoutSegments({+ indexes,+ measurements,+ gap,+}: {+ indexes: number[];+ measurements: VirtualItem[];+ gap: number;+}): TranscriptSegment[] {+ const segments: TranscriptSegment[] = [];+ indexes.forEach((index, position) => {+ const previous = indexes[position - 1];+ if (previous !== undefined && index > previous + 1) {+ const height =+ measurements[index].start - measurements[previous].end - 2 * gap;+ segments.push({+ kind: "gap",+ afterIndex: previous,+ height: Math.max(0, height),+ });+ }+ segments.push({ kind: "row", index });+ });+ return segments;+}++/** Padding that stands in for the unmounted rows above and below. */+function listPadding({+ indexes,+ measurements,+ scrollMargin,+ totalSize,+}: {+ indexes: number[];+ measurements: VirtualItem[];+ scrollMargin: number;+ totalSize: number;+}): { top: number; bottom: number } {+ const first = measurements[indexes[0]];+ const last = measurements[indexes[indexes.length - 1]];+ if (!first || !last) return { top: 0, bottom: 0 };+ return {+ top: Math.max(0, first.start - scrollMargin),+ bottom: Math.max(0, totalSize - (last.end - scrollMargin)),+ };+}++/** The rows to mount and how to lay them out. Without a window (no+ * measurable viewport) every row renders with no padding. */+export function windowLayout({+ rows,+ windowIndexes,+ isWindowed,+ lastUserMessageID,+ measurements,+ gap,+ scrollMargin,+ totalSize,+}: {+ rows: TranscriptRow[];+ windowIndexes: number[];+ isWindowed: boolean;+ lastUserMessageID: string | null;+ measurements: VirtualItem[];+ gap: number;+ scrollMargin: number;+ totalSize: number;+}) {+ if (!isWindowed) {+ const indexes = rows.map((_, index) => index);+ return {+ indexes,+ segments: indexes.map((index) => ({ kind: "row" as const, index })),+ padding: { top: 0, bottom: 0 },+ };+ }+ const indexes = mergeRenderedIndexes(+ windowIndexes,+ pinnedIndexes(rows, lastUserMessageID),+ );+ return {+ indexes,+ segments: layoutSegments({ indexes, measurements, gap }),+ padding: listPadding({ indexes, measurements, scrollMargin, totalSize }),+ };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/scrollAnchoring.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/scrollAnchoring.tsnew file mode 100644index 0000000000..e7c3841e90--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/scrollAnchoring.ts@@ -0,0 +1,110 @@+import type { Virtualizer } from "@tanstack/react-virtual";+import type { TranscriptRow } from "./helpers";++/** Where the first row sat before older history landed above it. */+export interface Prepend {+ firstRowID: string;+ scrollTop: number;+ firstRowOffset: number;+}++/**+ * A row above the viewport changed height: a row that scrolled into the+ * window replaced padding sized from a guess, or a mounted row grew or shrank+ * (a code block, an image, a tool row). Moving the scroll position by the+ * same amount keeps what the user is reading in place. A row only partly+ * above the viewport is left alone, so a reply streaming below the fold+ * never drags the view.+ */+export function keepRowsBelowStill({+ instance,+ key,+ delta,+ isEntering,+}: {+ instance: Virtualizer<HTMLElement, Element>;+ key: string | number | bigint;+ delta: number;+ isEntering: boolean;+}) {+ const scroller = instance.scrollElement;+ const node = instance.elementsCache.get(key);+ if (!(scroller instanceof HTMLElement) || !node) return;+ const viewportTop = scroller.getBoundingClientRect().top;+ const rect = node.getBoundingClientRect();+ // An entering row was padding a moment ago, so it counts from its top; a+ // mounted row counts if it sat wholly above before the change.+ const isAbove = isEntering+ ? rect.top < viewportTop+ : rect.bottom - delta <= viewportTop;+ if (isAbove) scroller.scrollTop += delta;+}++/**+ * Older history landed above the rows on screen. Moving the window's offset+ * by the height that arrived keeps the rows the user is reading mounted+ * through the commit, instead of windowing the new rows at the old offset.+ * Returns where the old first row sat, for `keepReadingRowAfterPrepend`.+ */+export function shiftOffsetForPrepend({+ virtualizer,+ rows,+ previousFirstID,+ scrollMargin,+}: {+ virtualizer: Virtualizer<HTMLElement, Element>;+ rows: TranscriptRow[];+ previousFirstID: string | undefined;+ scrollMargin: number;+}): Prepend | null {+ const scrollTop = virtualizer.scrollOffset;+ if (!previousFirstID || scrollTop === null) return null;+ if (scrollTop === Number.MAX_SAFE_INTEGER) return null;+ const previousIndex = rows.findIndex(+ (row) => row.message.id === previousFirstID,+ );+ if (previousIndex <= 0) return null;+ virtualizer.getTotalSize();+ const previousStart = virtualizer.measurementsCache[previousIndex]?.start;+ if (previousStart === undefined) return null;+ virtualizer.scrollOffset = scrollTop + previousStart - scrollMargin;+ return {+ firstRowID: previousFirstID,+ scrollTop,+ firstRowOffset: scrollMargin,+ };+}++/**+ * Puts the row that used to be first back where it was on screen, measured+ * from the DOM after the commit. This runs after `LoadMoreSentinel`'s own+ * restore, which moves by the change in total height: that is only the+ * height that arrived above when nothing below changed, and a page that also+ * merges rows further down would leave the reader somewhere else.+ */+export function keepReadingRowAfterPrepend({+ virtualizer,+ scroller,+ prepend,+}: {+ virtualizer: Virtualizer<HTMLElement, Element>;+ scroller: HTMLElement;+ prepend: Prepend;+}) {+ const node = virtualizer.elementsCache.get(prepend.firstRowID);+ if (!node?.isConnected) return;+ const rowOffset =+ node.getBoundingClientRect().top -+ scroller.getBoundingClientRect().top ++ scroller.scrollTop;+ scroller.scrollTop = prepend.scrollTop + rowOffset - prepend.firstRowOffset;+}++/** Offset of the list's top inside the scroll element's content. */+export function listOffset(scroller: HTMLElement, list: HTMLElement): number {+ return Math.round(+ list.getBoundingClientRect().top -+ scroller.getBoundingClientRect().top ++ scroller.scrollTop,+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useQuietRows.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useQuietRows.tsnew file mode 100644index 0000000000..f4e519b695--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useQuietRows.ts@@ -0,0 +1,59 @@+import { useLayoutEffect, useRef } from "react";+import type { TranscriptRow } from "./helpers";++/**+ * Which mounted rows skip the entrance animation. A row decides once, when it+ * mounts: rows that were already in the thread and only scroll back into the+ * window stay quiet, brand-new messages animate as before.+ */+export function useQuietRows({+ rows,+ indexes,+ isTracking,+}: {+ rows: TranscriptRow[];+ indexes: number[];+ /** False on the first pass, so every row of the opening window animates. */+ isTracking: boolean;+}) {+ const seenIDsRef = useRef(new Set<string>());+ const quietByIDRef = useRef(new Map<string, boolean>());+ const quietByID = quietByIDRef.current;+ for (const index of indexes) {+ const id = rows[index].message.id;+ if (!quietByID.has(id)) quietByID.set(id, seenIDsRef.current.has(id));+ }++ useLayoutEffect(() => {+ if (!isTracking) return;+ rememberMountedRows({+ rows,+ indexes,+ seenIDs: seenIDsRef.current,+ quietByID: quietByIDRef.current,+ });+ });++ return (id: string) => quietByID.get(id) ?? false;+}++/** Rows mounted from here on, other than brand-new messages, skip the+ * entrance animation: they were already in the thread and are only+ * scrolling back into the window. */+function rememberMountedRows({+ rows,+ indexes,+ seenIDs,+ quietByID,+}: {+ rows: TranscriptRow[];+ indexes: number[];+ seenIDs: Set<string>;+ quietByID: Map<string, boolean>;+}) {+ const mounted = new Set(indexes.map((index) => rows[index]?.message.id));+ for (const id of Array.from(quietByID.keys())) {+ if (!mounted.has(id)) quietByID.delete(id);+ }+ for (const row of rows) seenIDs.add(row.message.id);+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useVirtualTranscript.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useVirtualTranscript.tsnew file mode 100644index 0000000000..43656953f7--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/VirtualTranscript/useVirtualTranscript.ts@@ -0,0 +1,216 @@+"use client";++import { useVirtualizer } from "@tanstack/react-virtual";+import {+ useCallback,+ useLayoutEffect,+ useReducer,+ useRef,+ useState,+ type MutableRefObject,+} from "react";+import { useStickToBottomContext } from "use-stick-to-bottom";+import {+ estimateRowHeight,+ OVERSCAN_ROWS,+ windowLayout,+ type TranscriptRow,+} from "./helpers";+import {+ keepReadingRowAfterPrepend,+ keepRowsBelowStill,+ listOffset,+ shiftOffsetForPrepend,+ type Prepend,+} from "./scrollAnchoring";+import { useQuietRows } from "./useQuietRows";++const ROW_GAP_PX = 24;+const COMPACT_ROW_GAP_PX = 16;+const MAX_VIEWPORT_RECHECKS = 3;++export type JumpToMessage = (messageID: string) => void;++interface Args {+ rows: TranscriptRow[];+ lastUserMessageID: string | null;+ compact: boolean;+ jumpRef?: MutableRefObject<JumpToMessage | null>;+}++/**+ * Windows the transcript to the rows on screen plus a few either side, laid+ * out in normal flow inside StickToBottom's scroll element. The list opts out+ * of the browser's scroll anchoring, which switches itself off in every frame+ * the list's padding changes; instead this hook keeps the view still whenever+ * a row above the viewport changes height.+ */+export function useVirtualTranscript({+ rows,+ lastUserMessageID,+ compact,+ jumpRef,+}: Args) {+ const { scrollRef, state: stickState } = useStickToBottomContext();+ const listRef = useRef<HTMLDivElement>(null);+ const [scrollMargin, setScrollMargin] = useState(0);+ // Null until the first commit; false where the scroll element has no+ // height to window against (jsdom, a hidden panel), which renders every row.+ const [hasViewport, setHasViewport] = useState<boolean | null>(null);+ const [, recheckViewport] = useReducer((count: number) => count + 1, 0);+ const viewportRechecksRef = useRef(0);+ const rowsRef = useRef(rows);+ rowsRef.current = rows;+ const enteringRef = useRef(false);+ const prependRef = useRef<Prepend | null>(null);+ const hasViewportRef = useRef(false);+ hasViewportRef.current = hasViewport === true;+ const firstRowIDRef = useRef<string | undefined>(rows[0]?.message.id);+ const gap = compact ? COMPACT_ROW_GAP_PX : ROW_GAP_PX;++ const getItemKey = useCallback(+ (index: number) => rows[index].message.id,+ [rows],+ );++ const virtualizer = useVirtualizer({+ count: rows.length,+ getScrollElement: () => scrollRef.current,+ estimateSize: (index) => estimateRowHeight(rowsRef.current[index].message),+ getItemKey,+ overscan: OVERSCAN_ROWS,+ gap,+ scrollMargin,+ // A thread opens pinned to its end, so the first pass windows the newest+ // rows instead of mounting the oldest ones only to drop them a frame on.+ initialOffset: Number.MAX_SAFE_INTEGER,+ initialRect: {+ width: 0,+ height: typeof window === "undefined" ? 0 : window.innerHeight,+ },+ });++ virtualizer.shouldAdjustScrollPositionOnItemSizeChange = (+ item,+ delta,+ instance,+ ) => {+ // LoadMoreSentinel restores the position after a prepend, and+ // StickToBottom holds a pinned thread at its end.+ if (+ hasViewportRef.current &&+ !prependRef.current &&+ !stickState?.isAtBottom+ ) {+ keepRowsBelowStill({+ instance,+ key: item.key,+ delta,+ isEntering: enteringRef.current,+ });+ }+ return false;+ };++ const [measureRow] = useState(() => (node: HTMLDivElement | null) => {+ if (!node) {+ virtualizer.measureElement(null);+ return;+ }+ enteringRef.current =+ virtualizer.elementsCache.get(node.dataset.key ?? "") !== node;+ virtualizer.measureElement(node);+ enteringRef.current = false;+ });++ const firstRowID = rows[0]?.message.id;+ if (firstRowID !== firstRowIDRef.current) {+ const previousFirstID = firstRowIDRef.current;+ firstRowIDRef.current = firstRowID;+ prependRef.current = shiftOffsetForPrepend({+ virtualizer,+ rows,+ previousFirstID,+ scrollMargin,+ });+ }++ const windowItems = virtualizer.getVirtualItems();+ const measurements = virtualizer.measurementsCache;+ const isWindowed = (hasViewport ?? true) && windowItems.length > 0;+ const { indexes, segments, padding } = windowLayout({+ rows,+ windowIndexes: windowItems.map((item) => item.index),+ isWindowed,+ lastUserMessageID,+ measurements,+ gap,+ scrollMargin,+ totalSize: virtualizer.getTotalSize(),+ });++ const isQuiet = useQuietRows({+ rows,+ indexes,+ isTracking: hasViewport !== null,+ });++ useLayoutEffect(() => {+ const prepend = prependRef.current;+ prependRef.current = null;+ const scroller = scrollRef.current;+ // Mounted together with StickToBottom (a session switch), this runs+ // before React attaches the scroll element's ref (twice under StrictMode).+ // Look again on another layout pass rather than falling back to rendering+ // every row; give up after a few, as with no scroll element at all.+ if (!scroller && viewportRechecksRef.current < MAX_VIEWPORT_RECHECKS) {+ viewportRechecksRef.current += 1;+ recheckViewport();+ return;+ }+ const ready = scroller instanceof HTMLElement && scroller.clientHeight > 0;+ if (prepend && ready) {+ keepReadingRowAfterPrepend({ virtualizer, scroller, prepend });+ }+ if (ready !== hasViewport) setHasViewport(ready);+ const list = listRef.current;+ if (!ready || !scroller || !list) return;+ const margin = listOffset(scroller, list);+ if (margin !== scrollMargin) setScrollMargin(margin);+ });++ useJumpToMessage({ jumpRef, rowsRef, virtualizer });++ return {+ listRef,+ measureRow,+ segments,+ padding,+ isWindowed,+ isQuiet,+ };+}++/** Hands the minimap a way to reach rows the window has unmounted. */+function useJumpToMessage({+ jumpRef,+ rowsRef,+ virtualizer,+}: {+ jumpRef?: MutableRefObject<JumpToMessage | null>;+ rowsRef: MutableRefObject<TranscriptRow[]>;+ virtualizer: ReturnType<typeof useVirtualizer<HTMLElement, Element>>;+}) {+ useLayoutEffect(() => {+ if (!jumpRef) return;+ jumpRef.current = (messageID) => {+ const index = rowsRef.current.findIndex(+ (row) => row.message.id === messageID,+ );+ if (index >= 0) virtualizer.scrollToIndex(index, { align: "center" });+ };+ return () => {+ jumpRef.current = null;+ };+ }, [jumpRef, rowsRef, virtualizer]);+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/TailSpacer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/TailSpacer.test.tsxindex 887fd5b4ef..624f51aef1 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/TailSpacer.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/TailSpacer.test.tsx@@ -31,13 +31,20 @@ function rect(top: number) {}let rerenderParent: () => void = () => {};+let remountRow: () => void = () => {};function Transcript() {const [, setTick] = useState(0);+ const [rowKey, setRowKey] = useState(0);rerenderParent = () => setTick((tick) => tick + 1);+ remountRow = () => setRowKey((key) => key + 1);return (<div>- <div data-message-id="u1" ref={stubTop(() => layout.messageTop)} />+ <div+ key={rowKey}+ data-message-id="u1"+ ref={stubTop(() => layout.messageTop)}+ /><TailSpacer messageID="u1" bottomInset={0} /></div>);@@ -82,6 +89,20 @@ describe("TailSpacer", () => {expect(spacer.style.height).toBe("0px");});+ it("measures the row's new node after the transcript remounts it", () => {+ const spacer = renderTranscript();+ expect(spacer.style.height).toBe("338px");++ // A windowed transcript drops the row when it scrolls far away and+ // mounts a fresh node when it comes back.+ act(() => remountRow());+ layout.messageTop = 40;+ act(() => resize());++ // (900 - 24) * 0.5 - (100 - 40)+ expect(spacer.style.height).toBe("378px");+ });+it("does not render when its parent renders with the same props", () => {renderTranscript();const before = spacerRenders.count;diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/ChatMinimap.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/ChatMinimap.tsxindex 28880189e5..980cfc5a0c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/ChatMinimap.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/ChatMinimap.tsx@@ -17,6 +17,9 @@ const CARD_TRANSITION = { duration: 0.15, delay: 0.0875 } as const;interface Props {messages: UIMessage<unknown, UIDataTypes, UITools>[];+ /** Scrolls to a message whose row is not in the DOM: the transcript only+ * mounts the rows near the viewport. */+ onJumpToUnmounted?: (messageID: string) => void;}/**@@ -25,7 +28,10 @@ interface Props {* one reveals that turn's opening line, and clicking scrolls the transcript to* it, so a long thread stays navigable without dragging the scrollbar.*/-export const ChatMinimap = memo(function ChatMinimap({ messages }: Props) {+export const ChatMinimap = memo(function ChatMinimap({+ messages,+ onJumpToUnmounted,+}: Props) {const [hovered, setHovered] = useState<number | null>(null);const reducedMotion = useReducedMotion();const entries = useMemo(() => toMinimapEntries(messages), [messages]);@@ -47,11 +53,11 @@ export const ChatMinimap = memo(function ChatMinimap({ messages }: Props) {onMouseEnter={() => setHovered(index)}onFocus={() => setHovered(index)}onBlur={() => setHovered(null)}- onClick={() => scrollToMessage(entry.id)}+ onClick={() => scrollToMessage(entry.id, onJumpToUnmounted)}onKeyDown={(e) => {if (isKey(e, "Enter", " ")) {e.preventDefault();- scrollToMessage(entry.id);+ scrollToMessage(entry.id, onJumpToUnmounted);}if (isKey(e, "Escape")) {setHovered(null);@@ -102,8 +108,14 @@ function areSameMinimapProps(prev: Props, next: Props) {return haveSameMinimapRows(prev.messages, next.messages);}-function scrollToMessage(id: string) {- document- .querySelector(`[data-message-id="${CSS.escape(id)}"]`)- ?.scrollIntoView({ behavior: "smooth", block: "center" });+function scrollToMessage(+ id: string,+ onJumpToUnmounted?: (messageID: string) => void,+) {+ const row = document.querySelector(`[data-message-id="${CSS.escape(id)}"]`);+ if (row) {+ row.scrollIntoView({ behavior: "smooth", block: "center" });+ return;+ }+ onJumpToUnmounted?.(id);}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/__tests__/ChatMinimap.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/__tests__/ChatMinimap.test.tsxindex de5f71c87e..bb6d9ea6d7 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/__tests__/ChatMinimap.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/__tests__/ChatMinimap.test.tsx@@ -82,6 +82,33 @@ describe("ChatMinimap", () => {expect(scrollIntoView).toHaveBeenCalledTimes(3);});++ it("hands a message whose row is not mounted to the transcript", () => {+ const onJumpToUnmounted = vi.fn();+ render(+ <div>+ <ChatMinimap+ messages={MESSAGES}+ onJumpToUnmounted={onJumpToUnmounted}+ />+ <div data-message-id="m1">Plan my week</div>+ </div>,+ );+ const scrollIntoView = vi.fn();+ document+ .querySelectorAll("[data-message-id]")+ .forEach(+ (el) => ((el as HTMLElement).scrollIntoView = scrollIntoView as never),+ );++ fireEvent.click(screen.getByLabelText("Jump to: Now the budget"));+ expect(onJumpToUnmounted).toHaveBeenCalledWith("m3");+ expect(scrollIntoView).not.toHaveBeenCalled();++ fireEvent.click(screen.getByLabelText("Jump to: Plan my week"));+ expect(scrollIntoView).toHaveBeenCalledTimes(1);+ expect(onJumpToUnmounted).toHaveBeenCalledTimes(1);+ });});describe("ChatMinimap renders", () => {diff --git a/docs/engineering/frontend-perf-baseline.md b/docs/engineering/frontend-perf-baseline.mdindex bda4e120fe..08e5904f4a 100644--- a/docs/engineering/frontend-perf-baseline.md+++ b/docs/engineering/frontend-perf-baseline.md@@ -870,3 +870,51 @@ Added with SECRT-3012: `dev` app code at `8eb406275`, same machine and settings.| `PopperAnchor props:children` | 1,417 || `Text mount` | 1,260 || `Text props:children` | 1,154 |++## copilot-open-long-thread · baseline — median of 3 runs++Measured on `dev` at `a7fe56d05` (2026-10-20), before the transcript was windowed (SECRT-3011), with `--channel chrome`. Opening the 2,000-message chat mounts 924 rows in one commit; the longest task is about 2.5 s.++| metric | median | range | spread |+| --- | --- | --- | --- |+| commits | 27 | 27 – 28 | 3.7% |+| total renders | 59,027 | 59,025 – 59,042 | 0.0% |+| renders per commit | 2,186.1 | 2,108.6 – 2,186.2 | 3.5% |+| max renders in one commit | 38,844 | 38,844 – 38,844 | 0.0% |+| script time (ms) | 2,546 | 2,537 – 2,708 | 6.7% |+| task time (ms) | 3,337 | 3,240 – 3,387 | 4.4% |+| layout time (ms) | 72 | 71 – 80 | 12.5% |+| style recalc time (ms) | 209 | 194 – 230 | 16.9% |+| long tasks (≥ 50 ms) | 8 | 8 – 8 | 0.0% |+| long task total (ms) | 3,007 | 2,986 – 3,155 | 5.6% |+| heap used after GC (MB) | 119.8 | 119.7 – 119.8 | 0.1% |++**Top components (median renders)**++| name | renders |+| --- | --- |+| `Tooltip` | 3,337 |+| `TooltipContent` | 3,337 |+| `Streamdown` | 2,772 |+| `Presence` | 2,303 |+| `TooltipProvider` | 2,238 |+| `Popper` | 2,235 |+| `PopperAnchor` | 2,235 |+| `PopperProvider` | 2,235 |+| `Primitive.button` | 2,235 |+| `Primitive.button.Slot` | 2,235 |++**Top render causes (median renders)**++| name | renders |+| --- | --- |+| `Tooltip mount` | 2,194 |+| `TooltipContent mount` | 2,194 |+| `Streamdown state#1` | 1,848 |+| `Presence props:children` | 1,196 |+| `Block mount` | 1,177 |+| `Ie mount` | 1,177 |+| `HugeiconsIcon mount` | 1,146 |+| `Icon mount` | 1,146 |+| `Primitive.div props:children` | 1,136 |+| `Primitive.div.Slot props:children` | 1,136 |diff --git a/docs/engineering/frontend-perf-measurement.md b/docs/engineering/frontend-perf-measurement.mdindex 38d63326bc..730e867fbd 100644--- a/docs/engineering/frontend-perf-measurement.md+++ b/docs/engineering/frontend-perf-measurement.md@@ -130,6 +130,7 @@ Names come from the component function. Third-party code bundled already minifie| `builder-type-field` | Open the same 50-node workflow with no run, click the first node's text field and clear it | Type 20 characters, 50 ms apart | 2 s after the last key || `builder-block-menu-search` | Open the same 50-node workflow with no run and open the block menu | Type "text" into its search, 120 ms a key, wait for the first result, then scroll the results 6 times (800 px, 400 ms apart) so more pages load. Results come from the real backend's block search | 2 s after the last scroll || `builder-load-graph` | Open a 1-node workflow | Switch to a saved 100-node workflow with `history.pushState`, as opening another workflow does, until its last node is on the canvas | 2 s after the last node appears |+| `copilot-open-long-thread` | Open the 10-message chat and list the 2,000-message one in the sidebar. The 2,000 rows are the 200 recorded ones repeated, with fresh ids and sequences past 200 | Click the 2,000-message chat until its newest message is on screen | 2 s after that |Each scenario's `readyWhen` field says exactly what setup waits for before the window opens. Most wait for the page's own content, then for network idle (at most 15 s, because some pages poll), then one more second.