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.

fix(frontend/builder): clear stale chat messages on graph navigation

Zamil Majdy3e35345efb13
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts
index cdb85ad976..f1e0f22cc1 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts
@@ -67,12 +67,14 @@ vi.mock("@/components/molecules/Toast/use-toast", () => ({
}));
const mockSendMessage = vi.fn();
+const mockSetMessages = vi.fn();
const mockStop = vi.fn();
let mockChatMessages: unknown[] = [];
let mockChatStatus = "ready";
vi.mock("@ai-sdk/react", () => ({
useChat: () => ({
messages: mockChatMessages,
+ setMessages: mockSetMessages,
sendMessage: mockSendMessage,
stop: mockStop,
status: mockChatStatus,
@@ -108,6 +110,7 @@ beforeEach(() => {
mockPostV2CreateSession.mockClear();
mockInvalidateQueries.mockClear();
mockSendMessage.mockClear();
+ mockSetMessages.mockClear();
mockToast.mockClear();
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
index 6e8a0163d3..f1385340aa 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
@@ -154,7 +154,7 @@ export function useBuilderChatPanel({
[sessionId],
);
- const { messages, sendMessage, stop, status, error } = useChat({
+ const { messages, setMessages, sendMessage, stop, status, error } = useChat({
id: sessionId ?? undefined,
transport: transport ?? undefined,
});
@@ -163,6 +163,13 @@ export function useBuilderChatPanel({
// without including it in the deps array (avoids re-triggering the effect).
sendMessageRef.current = sendMessage;
+ // Clear messages from useChat when navigating to a different graph so stale
+ // context from the prior session is not briefly visible in the panel UI.
+ useEffect(() => {
+ setMessages([]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [flowID]);
+
// ID of the seed message sent on panel open. Matched by content prefix rather
// than message position so user messages are never accidentally suppressed.
const seedMessageId = useMemo(() => {