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): stop waking snapshot subscribers on text tokens (#15717)
Abhimanyu Yadavcdecc4f7bcaa
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamStore.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamStore.test.tsindex abd6648c82..407fb6dfdd 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamStore.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/copilotStreamStore.test.ts@@ -1,4 +1,4 @@-import type { FileUIPart } from "ai";+import type { FileUIPart, UIMessage } from "ai";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";const STORAGE_KEY = "copilot-stream-store";@@ -165,3 +165,183 @@ describe("copilotStreamStore pending first send persistence", () => {).toEqual({ send: null, parts: [] });});});++const SESSION = "snapshot-session";++function userMessage(): UIMessage {+ return { id: "u1", role: "user", parts: [{ type: "text", text: "go" }] };+}++function assistantMessage(parts: UIMessage["parts"]): UIMessage {+ return { id: "a1", role: "assistant", parts };+}++function textOnly(text: string): UIMessage[] {+ return [userMessage(), assistantMessage([{ type: "text", text }])];+}++describe("copilotStreamStore message snapshots", () => {+ beforeEach(() => {+ window.sessionStorage.clear();+ });++ afterEach(() => {+ vi.restoreAllMocks();+ vi.resetModules();+ window.sessionStorage.clear();+ });++ async function freshStoreWithSnapshot(messages: UIMessage[]) {+ const { useCopilotStreamStore } = await importFreshStore();+ useCopilotStreamStore.getState().setMessageSnapshot(SESSION, messages);+ const listener = vi.fn();+ useCopilotStreamStore.subscribe(listener);+ const setItem = vi.spyOn(Storage.prototype, "setItem");+ return { useCopilotStreamStore, listener, setItem };+ }++ it("does not publish a streamed token that only adds text", async () => {+ const first = textOnly("Hel");+ const { useCopilotStreamStore, listener, setItem } =+ await freshStoreWithSnapshot(first);++ const next = textOnly("Hello");+ useCopilotStreamStore.getState().setMessageSnapshot(SESSION, next);+ const withNewMessage = [+ ...next,+ { id: "a2", role: "assistant", parts: [{ type: "text", text: "More" }] },+ ] satisfies UIMessage[];+ useCopilotStreamStore+ .getState()+ .setMessageSnapshot(SESSION, withNewMessage);++ expect(listener).not.toHaveBeenCalled();+ expect(setItem).not.toHaveBeenCalled();+ expect(useCopilotStreamStore.getState().messageSnapshots[SESSION]).toBe(+ first,+ );+ expect(useCopilotStreamStore.getState().getMessageSnapshot(SESSION)).toBe(+ withNewMessage,+ );+ });++ it("ignores a repeated write of the published messages", async () => {+ const first = textOnly("Hel");+ const { useCopilotStreamStore, listener } =+ await freshStoreWithSnapshot(first);+ const store = useCopilotStreamStore.getState();++ store.setMessageSnapshot(SESSION, textOnly("Hello"));+ store.setMessageSnapshot(SESSION, first);++ expect(listener).not.toHaveBeenCalled();+ expect(store.getMessageSnapshot(SESSION)).toBe(first);+ });++ it("publishes a message that gains a file part", async () => {+ const { useCopilotStreamStore, listener } = await freshStoreWithSnapshot(+ textOnly("Here is the file"),+ );++ const next = [+ userMessage(),+ assistantMessage([+ { type: "text", text: "Here is the file" },+ {+ type: "file",+ mediaType: "text/markdown",+ filename: "brief.md",+ url: "/api/proxy/api/workspace/files/bbbbbbbb-0000-0000-0000-000000000002/download",+ },+ ]),+ ];+ useCopilotStreamStore.getState().setMessageSnapshot(SESSION, next);++ expect(listener).toHaveBeenCalledTimes(1);+ expect(useCopilotStreamStore.getState().messageSnapshots[SESSION]).toBe(+ next,+ );+ });++ it("publishes streamed text that links a workspace file", async () => {+ const { useCopilotStreamStore, listener } = await freshStoreWithSnapshot(+ textOnly("Here is [the brief]("),+ );++ const next = textOnly(+ "Here is [the brief](workspace://aaaaaaaa-0000-0000-0000-000000000001)",+ );+ useCopilotStreamStore.getState().setMessageSnapshot(SESSION, next);++ expect(listener).toHaveBeenCalledTimes(1);+ expect(useCopilotStreamStore.getState().messageSnapshots[SESSION]).toBe(+ next,+ );+ });++ it("publishes a tool call that starts a run", async () => {+ const { useCopilotStreamStore, listener } = await freshStoreWithSnapshot(+ textOnly("Starting it"),+ );++ const next = [+ userMessage(),+ assistantMessage([+ { type: "text", text: "Starting it" },+ {+ type: "tool-run_agent",+ toolCallId: "call-1",+ state: "output-available",+ input: {},+ output: { execution_id: "exec-1", status: "QUEUED" },+ },+ ]),+ ];+ useCopilotStreamStore.getState().setMessageSnapshot(SESSION, next);++ expect(listener).toHaveBeenCalledTimes(1);+ expect(useCopilotStreamStore.getState().messageSnapshots[SESSION]).toBe(+ next,+ );+ });++ it("publishes every write of messages it cannot digest", async () => {+ const { useCopilotStreamStore, listener } = await freshStoreWithSnapshot(+ textOnly("Hel"),+ );+ const malformed = {+ type: "text",+ } as unknown as UIMessage["parts"][number];++ const first = [userMessage(), assistantMessage([malformed])];+ const second = [userMessage(), assistantMessage([malformed])];+ useCopilotStreamStore.getState().setMessageSnapshot(SESSION, first);+ useCopilotStreamStore.getState().setMessageSnapshot(SESSION, second);++ expect(listener).toHaveBeenCalledTimes(2);+ expect(useCopilotStreamStore.getState().messageSnapshots[SESSION]).toBe(+ second,+ );+ });++ it("forgets the unpublished latest snapshot when the session is cleared", async () => {+ const { useCopilotStreamStore } = await freshStoreWithSnapshot(+ textOnly("Hel"),+ );+ const store = useCopilotStreamStore.getState();++ store.setMessageSnapshot(SESSION, textOnly("Hello"));+ store.clearSession(SESSION);+ expect(store.getMessageSnapshot(SESSION)).toEqual([]);++ store.setMessageSnapshot(SESSION, textOnly("Hel"));+ store.setMessageSnapshot(SESSION, textOnly("Hello"));+ store.resetAll();+ expect(store.getMessageSnapshot(SESSION)).toEqual([]);++ store.setMessageSnapshot(SESSION, textOnly("Hel"));+ store.setMessageSnapshot(SESSION, textOnly("Hello"));+ useCopilotStreamStore.setState({ messageSnapshots: {} });+ expect(store.getMessageSnapshot(SESSION)).toEqual([]);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/__tests__/ContextPanelToggle.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/__tests__/ContextPanelToggle.test.tsxindex 0328ec4b14..8da38e64b1 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/__tests__/ContextPanelToggle.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/__tests__/ContextPanelToggle.test.tsx@@ -7,6 +7,7 @@ import {screen,} from "@/tests/integrations/test-utils";import { http, HttpResponse } from "msw";+import { Profiler } from "react";import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";import { useCopilotStreamStore } from "../../../copilotStreamStore";import { useCopilotUIStore } from "../../../store";@@ -165,6 +166,43 @@ describe("ContextPanelToggle files button", () => {expect(button.textContent).toBe("1");});+ test("does not re-render for a streamed token that only adds text", async () => {+ const onRender = vi.fn();+ render(+ <Profiler id="toggle" onRender={onRender}>+ <ContextPanelToggle sessionId="s1" />+ </Profiler>,+ );+ expect(await screen.findByLabelText("Open files")).toBeDefined();++ function reply(text: string) {+ return [+ {+ id: "assistant-message",+ role: "assistant" as const,+ parts: [{ type: "text" as const, text }],+ },+ ];+ }+ act(() => {+ useCopilotStreamStore.getState().setMessageSnapshot("s1", reply("Hel"));+ });+ onRender.mockClear();++ act(() => {+ useCopilotStreamStore+ .getState()+ .setMessageSnapshot("s1", reply("Hello, here"));+ });+ act(() => {+ useCopilotStreamStore+ .getState()+ .setMessageSnapshot("s1", reply("Hello, here is the plan"));+ });++ expect(onRender).not.toHaveBeenCalled();+ });+test("opens the files tab of the side panel", () => {render(<ContextPanelToggle sessionId="s1" />);diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/useSessionFiles.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/useSessionFiles.tsindex a58e91081d..e974ebb29a 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/useSessionFiles.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ContextPanel/components/FilesTab/useSessionFiles.ts@@ -50,10 +50,8 @@ export function useSessionFiles(sessionId: string | null) {},});- // The chip and the artifacts button both read this on every render, and- // `messageSnapshots` is rewritten with a fresh array per streamed token —- // so without memoising, scanning every message part (and compiling a- // RegExp per matched workspace URI) would run at token cadence.+ // The chip and the artifacts button both read this on every render, so the+ // file list is memoised and keeps its identity until the files change.const { uploaded, generated, deliverables, files } = useMemo(() => {const files: SessionFile[] = (query.data?.files ?? []).filter((item) => !isHiddenWorkspaceFile(item))diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/WorkspaceFileCards/useSessionActivity.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/WorkspaceFileCards/useSessionActivity.tsindex 70272e8d55..f39ccce58e 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/WorkspaceFileCards/useSessionActivity.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/WorkspaceFileCards/useSessionActivity.ts@@ -8,8 +8,7 @@ export function useSessionActivity(sessionId: string | null) {const messages = useCopilotStreamStore((s) =>sessionId ? s.messageSnapshots[sessionId] : undefined,);- // The thread chip reads this on every render, and `messageSnapshots` is a- // fresh array per streamed token — so the scan over every message part has- // to be memoised, and the identity kept stable for consumers.+ // The thread chip reads this on every render, so the scan over every+ // message part is memoised and its identity kept stable for consumers.return useMemo(() => getSessionActivity(messages ?? []), [messages]);}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.tsindex b1203ff1d3..587e04851c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts@@ -3,6 +3,7 @@ import type { StoredAttachmentPart } from "./helpers/workspaceAttachments";import { create } from "zustand";import { createJSONStorage, persist } from "zustand/middleware";import type { ExpertKickoffMetadata } from "./expertKickoff";+import { getMessageSnapshotDigest } from "./helpers/messageSnapshotDigest";/*** Per-session state. Zustand (not React refs) so it can survive events we@@ -101,6 +102,10 @@ interface PersistedCopilotStreamState {interface CopilotStreamStore {sessions: Record<string, SessionCoord>;+ /** Per-session messages for the subscribers that derive files, runs and+ * schedules from them. Only replaced when one of those changes, so a+ * streamed text token does not wake them; `getMessageSnapshot` returns+ * the newest messages. */messageSnapshots: Record<string, UIMessage[]>;pendingFirstSend: PendingFirstSend | null;pendingFirstSendSessionId: string | null;@@ -136,6 +141,27 @@ interface CopilotStreamStore {resetAll: () => void;}+/**+ * `published` is the session's array in `messageSnapshots` and `digest` what+ * its subscribers derive from it; `latest` is the newest array written, which+ * differs from `published` only in what no subscriber reads. Kept outside the+ * state on purpose: recording a text-only write here notifies no subscriber+ * and writes nothing to sessionStorage.+ */+const snapshotEntries = new Map<+ string,+ { published: UIMessage[]; latest: UIMessage[]; digest: string | null }+>();++/** `null` when the messages cannot be digested, which publishes every write. */+function digestSnapshot(messages: UIMessage[]): string | null {+ try {+ return getMessageSnapshotDigest(messages);+ } catch {+ return null;+ }+}+export const useCopilotStreamStore = create<CopilotStreamStore>()(persist<CopilotStreamStore, [], [], PersistedCopilotStreamState>((set, get) => ({@@ -161,6 +187,7 @@ export const useCopilotStreamStore = create<CopilotStreamStore>()(}));},clearSession(sessionId) {+ snapshotEntries.delete(sessionId);set((state) => {const sessions = { ...state.sessions };delete sessions[sessionId];@@ -175,9 +202,31 @@ export const useCopilotStreamStore = create<CopilotStreamStore>()(});},getMessageSnapshot(sessionId) {- return get().messageSnapshots[sessionId] ?? [];+ const published = get().messageSnapshots[sessionId];+ const entry = snapshotEntries.get(sessionId);+ // A snapshot replaced through `setState` outranks the recorded one.+ if (entry && entry.published === published) return entry.latest;+ return published ?? [];},setMessageSnapshot(sessionId, messages) {+ const published = get().messageSnapshots[sessionId];+ const entry = snapshotEntries.get(sessionId);+ const current =+ published && entry?.published === published ? entry : undefined;+ if (current && messages === published) {+ current.latest = messages;+ return;+ }+ const digest = digestSnapshot(messages);+ if (current && digest !== null && current.digest === digest) {+ current.latest = messages;+ return;+ }+ snapshotEntries.set(sessionId, {+ published: messages,+ latest: messages,+ digest,+ });set((state) => ({messageSnapshots: {...state.messageSnapshots,@@ -238,6 +287,7 @@ export const useCopilotStreamStore = create<CopilotStreamStore>()(},resetAll() {+ snapshotEntries.clear();set({sessions: {},messageSnapshots: {},diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/messageSnapshotDigest.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/messageSnapshotDigest.tsnew file mode 100644index 0000000000..cb322a182a--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/messageSnapshotDigest.ts@@ -0,0 +1,31 @@+import type { UIMessage } from "ai";+import { getSessionActivity } from "../components/WorkspaceFileCards/helpers";++/**+ * What the subscribers of a message snapshot can read from it, as one+ * comparable string: the runs and schedules `useSessionActivity` mines, and+ * the parts `useSessionFiles` finds files in — file parts, and text that holds+ * a `workspace://` URI — with the id and role of their message. Two snapshots+ * with the same digest look the same to every subscriber, so a streamed token+ * that only adds plain text does not have to wake them. It may differ where+ * they would not, which only costs an extra update; it never matches where+ * they would differ.+ */+export function getMessageSnapshotDigest(messages: UIMessage[]): string {+ const fileSources: unknown[] = [];+ for (const message of messages) {+ const sources = message.parts.flatMap((part): unknown[] => {+ if (part.type === "file") {+ return [[part.url, part.filename ?? null, part.mediaType]];+ }+ if (part.type === "text" && part.text.includes("workspace://")) {+ return [part.text];+ }+ return [];+ });+ if (sources.length > 0) {+ fileSources.push([message.id, message.role, sources]);+ }+ }+ return JSON.stringify([fileSources, getSessionActivity(messages)]);+}