Skip to content

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 reprocessing the whole chat on each render (#15710)

Abhimanyu Yadav8c376f8fb471
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/helpers.test.ts
index 01820cb798..1d0a90f2dd 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/helpers.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/helpers.test.ts
@@ -1,5 +1,13 @@
import type { UIMessage } from "ai";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ vi,
+ type MockInstance,
+} from "vitest";
import { IMPERSONATION_HEADER_NAME } from "@/lib/constants";
import {
COPILOT_COMPLETION_NOTIFICATION,
@@ -649,6 +657,95 @@ describe("deduplicateMessages", () => {
expect(result).toHaveLength(3);
expect(result.map((m) => m.id)).toEqual(["u1", "a1", "a2"]);
});
+
+ it("returns the input array itself when nothing is a duplicate", () => {
+ const msgs = [
+ makeMsgWithId("u1", "user", "question 1"),
+ makeMsgWithId("a1", "assistant", "answer 1"),
+ makeMsgWithId("u2", "user", "question 2"),
+ makeMsgWithId("a2", "assistant", "answer 2"),
+ ];
+ expect(deduplicateMessages(msgs)).toBe(msgs);
+ expect(deduplicateMessages([])).toEqual([]);
+ });
+
+ it("returns a new array when it removes a duplicate", () => {
+ const msgs = [
+ makeMsgWithId("u1", "user", "hello"),
+ makeMsgWithId("a1", "assistant", "hi there"),
+ makeMsgWithId("a2", "assistant", "hi there"),
+ ];
+ const result = deduplicateMessages(msgs);
+ expect(result).not.toBe(msgs);
+ expect(result.map((m) => m.id)).toEqual(["u1", "a1"]);
+ });
+
+ describe("fingerprint cache", () => {
+ let stringify: MockInstance<typeof JSON.stringify>;
+ beforeEach(() => {
+ stringify = vi.spyOn(JSON, "stringify");
+ });
+ afterEach(() => {
+ stringify.mockRestore();
+ });
+
+ it("fingerprints an unchanged assistant message once, and the newest one on every call", () => {
+ const older = makeMsgWithId("a1", "assistant", "answer 1");
+ const msgs = [
+ makeMsgWithId("u1", "user", "question 1"),
+ older,
+ makeMsgWithId("u2", "user", "question 2"),
+ makeMsgWithId("a2", "assistant", "answer 2"),
+ ];
+
+ deduplicateMessages(msgs);
+ expect(stringify).toHaveBeenCalledTimes(2);
+
+ stringify.mockClear();
+ const streamed = {
+ ...msgs[3],
+ parts: [{ type: "text" as const, text: "answer 2, longer" }],
+ };
+ deduplicateMessages([...msgs.slice(0, 3), streamed]);
+ expect(stringify).toHaveBeenCalledTimes(1);
+ expect(stringify.mock.calls[0][0]).toEqual(["answer 2, longer"]);
+ });
+
+ it("fingerprints a message again once it gets new parts", () => {
+ const msgs: UIMessage[] = [
+ makeMsgWithId("u1", "user", "question"),
+ makeMsgWithId("a1", "assistant", "same"),
+ makeMsgWithId("a2", "assistant", "other"),
+ makeMsgWithId("u2", "user", "next"),
+ ];
+ expect(deduplicateMessages(msgs)).toBe(msgs);
+
+ msgs[2] = { ...msgs[2], parts: [{ type: "text", text: "same" }] };
+ expect(deduplicateMessages(msgs).map((m) => m.id)).toEqual([
+ "u1",
+ "a1",
+ "u2",
+ ]);
+ });
+
+ it("fingerprints a message again when parts are added to it in place", () => {
+ const growing: UIMessage = { id: "a2", role: "assistant", parts: [] };
+ const msgs = [
+ makeMsgWithId("u1", "user", "question"),
+ makeMsgWithId("a1", "assistant", "same"),
+ growing,
+ makeMsgWithId("u2", "user", "next"),
+ ];
+ expect(deduplicateMessages(msgs)).toBe(msgs);
+
+ growing.parts.push({ type: "text", text: "same" });
+ expect(deduplicateMessages(msgs).map((m) => m.id)).toEqual([
+ "u1",
+ "a1",
+ "u2",
+ ]);
+ });
+ });
});
describe("shouldDebounceReconnect", () => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts
index 822b6b09ba..4655f882b6 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts
@@ -389,10 +389,15 @@ export function shouldDebounceReconnect(
*/
export function deduplicateMessages(messages: UIMessage[]): UIMessage[] {
const seenIds = new Set<string>();
- const seenFingerprints = new Set<string>();
+ // Fingerprints seen per user turn. Nested rather than one composite
+ // `${userID}:${fingerprint}` string, which would be rebuilt and re-hashed in
+ // full for every assistant message on every call; the cached fingerprint
+ // string keeps its hash.
+ const seenFingerprintsByTurn = new Map<string, Set<string>>();
let lastUserMsgID = "";
+ const newestIndex = messages.length - 1;
- return messages.filter((msg) => {
+ const kept = messages.filter((msg, index) => {
if (seenIds.has(msg.id)) return false;
seenIds.add(msg.id);
@@ -405,19 +410,9 @@ export function deduplicateMessages(messages: UIMessage[]): UIMessage[] {
}
if (msg.role === "assistant") {
- // JSON.stringify the parts array to avoid separator-collision false
- // positives: a plain join("|") on ["a|b", "c"] and ["a", "b|c"]
- // produces the same string. JSON encoding each element is unambiguous.
- // Fall back to JSON.stringify(p) for parts that carry neither a text nor
- // a toolCallId (e.g. step-start) so structurally different parts never
- // collapse to the same empty-string fingerprint element.
- const contentFingerprint = JSON.stringify(
- msg.parts.map(
- (p) =>
- ("text" in p && p.text) ||
- ("toolCallId" in p && p.toolCallId) ||
- JSON.stringify(p),
- ),
+ const contentFingerprint = getContentFingerprint(
+ msg,
+ index === newestIndex,
);
if (contentFingerprint !== "[]") {
@@ -429,14 +424,60 @@ export function deduplicateMessages(messages: UIMessage[]): UIMessage[] {
// resumeStream() first strips the in-progress assistant message —
// handleReconnect, the wake-resync path, and the hydration-effect path
// all do this. See useCopilotStream.ts.
- const contextKey = `assistant:${lastUserMsgID}:${contentFingerprint}`;
- if (seenFingerprints.has(contextKey)) return false;
- seenFingerprints.add(contextKey);
+ let seenInTurn = seenFingerprintsByTurn.get(lastUserMsgID);
+ if (!seenInTurn) {
+ seenInTurn = new Set<string>();
+ seenFingerprintsByTurn.set(lastUserMsgID, seenInTurn);
+ }
+ if (seenInTurn.has(contentFingerprint)) return false;
+ seenInTurn.add(contentFingerprint);
}
}
return true;
});
+ // Same array back when nothing was dropped, so memos keyed on it hold.
+ return kept.length === messages.length ? messages : kept;
+}
+
+// Keyed by the parts array, which every streamed update replaces, so a
+// message that has not changed is never serialised twice. The length check
+// catches parts pushed in place. The newest message is never cached: the SDK
+// can still be appending to it in place until its next flush.
+const contentFingerprints = new WeakMap<
+ UIMessage["parts"],
+ { partCount: number; fingerprint: string }
+>();
+
+function getContentFingerprint(msg: UIMessage, isNewest: boolean): string {
+ if (isNewest) return computeContentFingerprint(msg.parts);
+ const cached = contentFingerprints.get(msg.parts);
+ if (cached && cached.partCount === msg.parts.length) {
+ return cached.fingerprint;
+ }
+ const fingerprint = computeContentFingerprint(msg.parts);
+ contentFingerprints.set(msg.parts, {
+ partCount: msg.parts.length,
+ fingerprint,
+ });
+ return fingerprint;
+}
+
+function computeContentFingerprint(parts: UIMessage["parts"]): string {
+ // JSON.stringify the parts array to avoid separator-collision false
+ // positives: a plain join("|") on ["a|b", "c"] and ["a", "b|c"]
+ // produces the same string. JSON encoding each element is unambiguous.
+ // Fall back to JSON.stringify(p) for parts that carry neither a text nor
+ // a toolCallId (e.g. step-start) so structurally different parts never
+ // collapse to the same empty-string fingerprint element.
+ return JSON.stringify(
+ parts.map(
+ (p) =>
+ ("text" in p && p.text) ||
+ ("toolCallId" in p && p.toolCallId) ||
+ JSON.stringify(p),
+ ),
+ );
}
/**
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts
index e4790881e0..c2f4f7e1d5 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/convertChatSessionToUiMessages.test.ts
@@ -362,12 +362,12 @@ function uiUser(
describe("concatWithAssistantMerge", () => {
it("returns b unchanged when a is empty", () => {
const b = [uiAssistant(SESSION_ID, 0, "hi")];
- expect(concatWithAssistantMerge([], b)).toEqual(b);
+ expect(concatWithAssistantMerge([], b)).toBe(b);
});
it("returns a unchanged when b is empty", () => {
const a = [uiAssistant(SESSION_ID, 0, "hi")];
- expect(concatWithAssistantMerge(a, [])).toEqual(a);
+ expect(concatWithAssistantMerge(a, [])).toBe(a);
});
it("merges two ASSISTANT bubbles whose DB sequences are strictly adjacent", () => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/stripReplayPrefix.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/stripReplayPrefix.test.ts
index 84765634d6..3c82bbe09a 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/stripReplayPrefix.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/stripReplayPrefix.test.ts
@@ -180,6 +180,112 @@ describe("stripReplayPrefix", () => {
expect(stripReplayPrefix([])).toEqual([]);
});
+ it("returns the input array itself when nothing is a replay", () => {
+ const msgs = [
+ user("u1", "hi"),
+ assistant("a1", "hello"),
+ user("u2", "how are you"),
+ assistant("a2", "good thanks"),
+ ];
+ expect(stripReplayPrefix(msgs)).toBe(msgs);
+ });
+
+ it("returns a new array when it drops or trims a message", () => {
+ const dropped = [
+ user("u1", "hi"),
+ assistant("a1", "hello world"),
+ user("u2", "tell me more"),
+ assistant("a2", "hello world"),
+ ];
+ expect(stripReplayPrefix(dropped)).not.toBe(dropped);
+
+ const trimmed = [
+ user("u1", "hi"),
+ assistant("a1", "hello"),
+ user("u2", "more"),
+ assistant("a2", "hello", " and more"),
+ ];
+ const result = stripReplayPrefix(trimmed);
+ expect(result).not.toBe(trimmed);
+ expect(result).toHaveLength(4);
+ expect(textOf(result[3])).toBe(" and more");
+ });
+
+ it("gives the same result as a fresh call while the newest reply streams", () => {
+ const history = [
+ user("u1", "hi"),
+ assistant("a1", "hello"),
+ user("u2", "again"),
+ assistant("a2", "hello and more"),
+ user("u3", "go"),
+ ];
+ const reply = "hello and more, then something new";
+ for (let n = 1; n <= reply.length; n++) {
+ const msgs = [...history, assistant("a3", reply.slice(0, n))];
+ const result = stripReplayPrefix(msgs);
+ const fresh = reply.slice(0, n);
+ if ("hello and more".startsWith(fresh)) {
+ expect(result.map((m) => m.id)).toEqual(["u1", "a1", "u2", "a2", "u3"]);
+ } else {
+ expect(textOf(result[5])).toBe(fresh.slice("hello and more".length));
+ }
+ }
+ });
+
+ it("does not carry a replay decision over to a conversation whose earlier text differs", () => {
+ const replayed = [
+ user("u1", "hi"),
+ assistant("a1", "hello"),
+ user("u2", "more"),
+ assistant("a2", "hello there"),
+ ];
+ const notReplayed = [
+ user("u1", "hi"),
+ assistant("a1", "goodbye"),
+ user("u2", "more"),
+ assistant("a2", "hello there"),
+ ];
+
+ for (let round = 0; round < 2; round++) {
+ expect(textOf(stripReplayPrefix(replayed)[3])).toBe(" there");
+ expect(stripReplayPrefix(notReplayed)).toBe(notReplayed);
+ }
+ });
+
+ it("re-checks every later message when an earlier one changes", () => {
+ const before = [
+ user("u1", "hi"),
+ assistant("a1", "plan"),
+ user("u2", "next"),
+ assistant("a2", "plan, step two"),
+ ];
+ expect(textOf(stripReplayPrefix(before)[3])).toBe(", step two");
+
+ const edited = [
+ ...before.slice(0, 1),
+ assistant("a1", "other"),
+ ...before.slice(2),
+ ];
+ expect(stripReplayPrefix(edited)).toBe(edited);
+ });
+
+ it("returns a new array when it removes a duplicate or a seeded copy", () => {
+ const duplicated = [user("u1", "hi"), user("u1", "hi")];
+ expect(stripReplayPrefix(duplicated)).toEqual([duplicated[0]]);
+
+ const seeded = assistant("a1", "Saved.");
+ const resumed = {
+ ...assistant("a2", "Saved."),
+ parts: [
+ ...structuredClone(seeded.parts),
+ { type: "step-start" as const },
+ ],
+ };
+ const result = stripReplayPrefix([user("u1", "go"), seeded, resumed]);
+ expect(result.map((m) => m.id)).toEqual(["u1", "a1", "a2"]);
+ expect(result[2].parts).toEqual([{ type: "step-start" }]);
+ });
+
it("drops the copy of the bubble before it that a resumed turn opens with, cards included", () => {
// `useChat` seeds a GET-resumed turn from the assistant message the list
// ends on, so the turn's message starts with all of that bubble's parts.
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/stripReplayPrefix.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/stripReplayPrefix.ts
index 721ee4bb0b..ef65cb4a99 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/stripReplayPrefix.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/stripReplayPrefix.ts
@@ -31,6 +31,7 @@ import { deduplicateMessages } from "../helpers";
export function stripReplayPrefix(raw: UIMessage[]): UIMessage[] {
const deduped = withoutSeededCopies(deduplicateMessages(raw));
const texts = deduped.map(concatText);
+ const matches = reuseReplayMatches(texts);
const out: UIMessage[] = [];
for (let i = 0; i < deduped.length; i++) {
@@ -40,7 +41,7 @@ export function stripReplayPrefix(raw: UIMessage[]): UIMessage[] {
continue;
}
- const match = findLongestReplay(texts, i);
+ const match = (matches[i] ??= findLongestReplay(texts, i));
if (match.drop) continue;
if (match.stripLen === 0) {
out.push(msg);
@@ -49,7 +50,8 @@ export function stripReplayPrefix(raw: UIMessage[]): UIMessage[] {
const trimmed = stripLeadingTextChars(msg, match.stripLen);
if (trimmed !== null) out.push(trimmed);
}
- return out;
+ // Same array back when nothing changed, so memos keyed on it hold.
+ return isSameList(out, raw) ? raw : out;
}
/**
@@ -62,7 +64,7 @@ export function stripReplayPrefix(raw: UIMessage[]): UIMessage[] {
* second time. Only an exact copy of the whole bubble right before it goes.
*/
function withoutSeededCopies(messages: UIMessage[]): UIMessage[] {
- return messages.flatMap((message, i) => {
+ const kept = messages.flatMap((message, i) => {
const previous = messages[i - 1];
if (message.role !== "assistant" || previous?.role !== "assistant") {
return [message];
@@ -78,6 +80,11 @@ function withoutSeededCopies(messages: UIMessage[]): UIMessage[] {
const own = message.parts.slice(seeded.length);
return own.length > 0 ? [{ ...message, parts: own }] : [];
});
+ return isSameList(kept, messages) ? messages : kept;
+}
+
+function isSameList(a: UIMessage[], b: UIMessage[]): boolean {
+ return a.length === b.length && a.every((message, i) => message === b[i]);
}
// Cheap identity for a part: never serialises a tool's output, which can be
@@ -90,6 +97,33 @@ function partKey(part: UIMessage["parts"][number]): string {
return JSON.stringify(part);
}
+interface ReplayMatch {
+ drop: boolean;
+ stripLen: number;
+}
+
+let previousReplayScan: {
+ texts: string[];
+ matches: (ReplayMatch | undefined)[];
+} = { texts: [], matches: [] };
+
+/**
+ * The replay match of message `i` depends only on `texts[0..i]`. While a reply
+ * streams, only the newest message's text changes, so the matches of the
+ * longest text prefix shared with the previous call are carried over instead
+ * of re-running the pairwise scan over the whole conversation. The returned
+ * array is filled in by the caller and kept for the next call.
+ */
+function reuseReplayMatches(texts: string[]): (ReplayMatch | undefined)[] {
+ const previous = previousReplayScan;
+ const limit = Math.min(texts.length, previous.texts.length);
+ let shared = 0;
+ while (shared < limit && texts[shared] === previous.texts[shared]) shared++;
+ const matches = previous.matches.slice(0, shared);
+ previousReplayScan = { texts, matches };
+ return matches;
+}
+
function concatText(msg: UIMessage): string {
return (msg.parts ?? [])
.map((p) => ("text" in p && typeof p.text === "string" ? p.text : ""))
@@ -101,10 +135,7 @@ function concatText(msg: UIMessage): string {
* or `{ stripLen }` with the number of leading text chars to strip (0 =
* keep as-is).
*/
-function findLongestReplay(
- texts: string[],
- i: number,
-): { drop: boolean; stripLen: number } {
+function findLongestReplay(texts: string[], i: number): ReplayMatch {
const myText = texts[i];
let stripLen = 0;
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
index 6845c53c71..ed3bb5ff74 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
@@ -302,7 +302,13 @@ export function useCopilotPage({
// assistant UIMessages at the page boundary so reasoning + response parts
// stay in a single bubble. Paged messages are older history prepended before
// the current page.
- const rawMessages = concatWithAssistantMerge(pagedMessages, currentMessages);
+ // Memoised on their inputs so the stripReplayPrefix memos below only re-run
+ // when the messages actually change, not on every render once older pages
+ // are loaded.
+ const rawMessages = useMemo(
+ () => concatWithAssistantMerge(pagedMessages, currentMessages),
+ [pagedMessages, currentMessages],
+ );
const cachedSessionMessages = useMemo(
() =>
sessionId
@@ -310,9 +316,9 @@ export function useCopilotPage({
: [],
[sessionId],
);
- const cachedRawMessages = concatWithAssistantMerge(
- pagedMessages,
- cachedSessionMessages,
+ const cachedRawMessages = useMemo(
+ () => concatWithAssistantMerge(pagedMessages, cachedSessionMessages),
+ [pagedMessages, cachedSessionMessages],
);
// Drop / trim assistant messages whose leading text is a replay of an
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
index 32a76caa63..5c42b3321a 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
@@ -577,7 +577,12 @@ export function useCopilotStream({
}
// Deduplicate messages continuously to prevent duplicates when resuming streams.
- const messages = deduplicateMessages(rawMessages);
+ // Memoised so `messages` keeps its identity across renders that did not
+ // change the chat, instead of being a new array every render.
+ const messages = useMemo(
+ () => deduplicateMessages(rawMessages),
+ [rawMessages],
+ );
useEffect(() => {
if (!sessionId) return;