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.

feat(frontend): auto-open artifact panel when agent creates new artifact (SECRT-2263) (#12997)

Nicholas Tindle2cd7fa0a68c9
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/store.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/store.test.ts
index 1f319faf5c..8b6f0cdd14 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/store.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/store.test.ts
@@ -122,7 +122,7 @@ describe("artifactPanel store actions", () => {
expect(s.history).toEqual([]);
});
- it("openArtifact ignores non-previewable artifacts", () => {
+ it("openArtifact opens even non-previewable artifacts", () => {
const binary = {
...makeArtifact("bin", "artifact.bin"),
mimeType: "application/octet-stream",
@@ -131,9 +131,8 @@ describe("artifactPanel store actions", () => {
useCopilotUIStore.getState().openArtifact(binary);
const s = useCopilotUIStore.getState().artifactPanel;
- expect(s.isOpen).toBe(false);
- expect(s.activeArtifact).toBeNull();
- expect(s.history).toEqual([]);
+ expect(s.isOpen).toBe(true);
+ expect(s.activeArtifact?.id).toBe("bin");
});
it("resetArtifactPanel clears active artifact and history", () => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactCard/ArtifactCard.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactCard/ArtifactCard.tsx
index 554d760215..a79eb5ad0a 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactCard/ArtifactCard.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactCard/ArtifactCard.tsx
@@ -3,6 +3,7 @@
import { toast } from "@/components/molecules/Toast/use-toast";
import { cn } from "@/lib/utils";
import { CaretRight, DownloadSimple } from "@phosphor-icons/react";
+import { useEffect } from "react";
import type { ArtifactRef } from "../../store";
import { useCopilotUIStore } from "../../store";
import { downloadArtifact } from "../ArtifactPanel/downloadArtifact";
@@ -20,11 +21,24 @@ function formatSize(bytes?: number): string {
}
export function ArtifactCard({ artifact }: Props) {
- const activeID = useCopilotUIStore((s) => s.artifactPanel.activeArtifact?.id);
- const isOpen = useCopilotUIStore((s) => s.artifactPanel.isOpen);
+ const isActive = useCopilotUIStore(
+ (s) =>
+ s.artifactPanel.isOpen &&
+ s.artifactPanel.activeArtifact?.id === artifact.id,
+ );
const openArtifact = useCopilotUIStore((s) => s.openArtifact);
+ const registerArtifactForAutoOpen = useCopilotUIStore(
+ (s) => s.registerArtifactForAutoOpen,
+ );
+
+ // Register this artifact on mount — the store decides whether to auto-open.
+ // Fires once per artifact ID; subsequent renders with the same ID are no-ops
+ // in the store (knownIds check).
+ useEffect(() => {
+ registerArtifactForAutoOpen(artifact);
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- re-register on ID or MIME change
+ }, [artifact.id, artifact.mimeType, registerArtifactForAutoOpen]);
- const isActive = isOpen && activeID === artifact.id;
const classification = classifyArtifact(
artifact.mimeType,
artifact.title,
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
index ff2b396da1..e3bb7deeec 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
@@ -89,7 +89,12 @@ export const ChatContainer = ({
// open state drive layout width; an artifact generated in a stale session
// state would otherwise shrink the chat column with no panel rendered.
const isArtifactOpen = isArtifactsEnabled && isArtifactPanelOpen;
- useAutoOpenArtifacts({ sessionId });
+ useAutoOpenArtifacts({
+ sessionId,
+ messages,
+ isLoadingSession,
+ isArtifactsEnabled,
+ });
// isStreaming controls the stop-button UI and routes submits to the queue
// endpoint — the input itself must NOT be disabled during streaming so users
// can type and queue their next message. ``isUserStopping`` force-flips
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/useAutoOpenArtifacts.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/useAutoOpenArtifacts.test.ts
new file mode 100644
index 0000000000..49e2711f21
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/useAutoOpenArtifacts.test.ts
@@ -0,0 +1,319 @@
+import { act, cleanup, renderHook } from "@testing-library/react";
+import { UIDataTypes, UIMessage, UITools } from "ai";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import type { ArtifactRef } from "../../store";
+import { useCopilotUIStore } from "../../store";
+import { useAutoOpenArtifacts } from "./useAutoOpenArtifacts";
+
+type Messages = UIMessage<unknown, UIDataTypes, UITools>[];
+
+const A_ID = "11111111-0000-0000-0000-000000000000";
+const B_ID = "22222222-0000-0000-0000-000000000000";
+
+function makeArtifact(
+ id: string,
+ title = `${id}.txt`,
+ origin: ArtifactRef["origin"] = "agent",
+): ArtifactRef {
+ return {
+ id,
+ title,
+ mimeType: "text/plain",
+ sourceUrl: `/api/proxy/api/workspace/files/${id}/download`,
+ origin,
+ };
+}
+
+function makeAgentMessage(
+ id: string,
+ artifactIds: string[],
+): UIMessage<unknown, UIDataTypes, UITools> {
+ return {
+ id,
+ role: "assistant",
+ parts: artifactIds.map((aid) => ({
+ type: "file" as const,
+ url: `/api/proxy/api/workspace/files/${aid}/download`,
+ filename: `${aid}.txt`,
+ mediaType: "text/plain",
+ })),
+ };
+}
+
+function resetStore() {
+ useCopilotUIStore.setState({
+ artifactPanel: {
+ isOpen: false,
+ isMinimized: false,
+ isMaximized: false,
+ width: 600,
+ activeArtifact: null,
+ history: [],
+ },
+ });
+ useCopilotUIStore.getState().resetAutoOpenState();
+}
+
+const defaultProps = {
+ sessionId: "s1",
+ messages: [] as Messages,
+ isLoadingSession: false,
+ isArtifactsEnabled: true,
+};
+
+describe("useAutoOpenArtifacts (card-based)", () => {
+ beforeEach(resetStore);
+ afterEach(cleanup);
+
+ // ── Lifecycle ──────────────────────────────────────────────────────
+
+ it("does not open the panel on initial render", () => {
+ renderHook(() => useAutoOpenArtifacts(defaultProps));
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("resets the panel state when sessionId changes", () => {
+ useCopilotUIStore.getState().openArtifact(makeArtifact(A_ID, "a.txt"));
+ useCopilotUIStore.getState().openArtifact(makeArtifact(B_ID, "b.txt"));
+
+ const { rerender } = renderHook(
+ ({ sessionId }) => useAutoOpenArtifacts({ ...defaultProps, sessionId }),
+ { initialProps: { sessionId: "s1" } },
+ );
+
+ act(() => {
+ rerender({ sessionId: "s2" });
+ });
+
+ const s = useCopilotUIStore.getState().artifactPanel;
+ expect(s.isOpen).toBe(false);
+ expect(s.activeArtifact).toBeNull();
+ expect(s.history).toEqual([]);
+ });
+
+ it("closes the panel on unmount (SECRT-2254)", () => {
+ useCopilotUIStore.getState().openArtifact(makeArtifact(A_ID, "a.txt"));
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+
+ const { unmount } = renderHook(() => useAutoOpenArtifacts(defaultProps));
+ act(() => {
+ unmount();
+ });
+
+ const s = useCopilotUIStore.getState().artifactPanel;
+ expect(s.isOpen).toBe(false);
+ expect(s.activeArtifact).toBeNull();
+ });
+
+ // ── Readiness gating ──────────────────────────────────────────────
+
+ it("sets auto-open ready after loading completes with a session", () => {
+ renderHook(() => useAutoOpenArtifacts(defaultProps));
+ // After the effect fires, registerArtifactForAutoOpen should auto-open
+ const ref = makeArtifact(A_ID);
+ act(() => {
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+ });
+
+ it("does not set ready when sessionId is null", () => {
+ renderHook(() =>
+ useAutoOpenArtifacts({ ...defaultProps, sessionId: null }),
+ );
+ const ref = makeArtifact(A_ID);
+ act(() => {
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("does not set ready when artifacts feature flag is disabled", () => {
+ renderHook(() =>
+ useAutoOpenArtifacts({ ...defaultProps, isArtifactsEnabled: false }),
+ );
+ const ref = makeArtifact(A_ID);
+ act(() => {
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("defers readiness when session was loading and messages haven't hydrated", () => {
+ const { rerender } = renderHook(
+ ({ isLoadingSession, messages }) =>
+ useAutoOpenArtifacts({
+ ...defaultProps,
+ isLoadingSession,
+ messages,
+ }),
+ { initialProps: { isLoadingSession: true, messages: [] as Messages } },
+ );
+
+ // Loading done but messages still empty → should NOT be ready
+ act(() => {
+ rerender({ isLoadingSession: false, messages: [] as Messages });
+ });
+ const ref = makeArtifact(A_ID);
+ act(() => {
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+
+ // Once messages arrive → ready
+ resetStore();
+ act(() => {
+ rerender({
+ isLoadingSession: false,
+ messages: [makeAgentMessage("m1", [A_ID])],
+ });
+ });
+ const ref2 = makeArtifact(B_ID);
+ act(() => {
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref2);
+ });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+ });
+
+ // ── User-close suppression ────────────────────────────────────────
+
+ it("suppresses auto-open after the user explicitly closes the panel", () => {
+ renderHook(() => useAutoOpenArtifacts(defaultProps));
+
+ // Open via store
+ act(() => {
+ useCopilotUIStore.getState().openArtifact(makeArtifact(A_ID));
+ });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+
+ // User closes
+ act(() => {
+ useCopilotUIStore.getState().closeArtifactPanel();
+ });
+
+ // New registration should NOT auto-open
+ const ref = makeArtifact(B_ID);
+ act(() => {
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("resets close-suppression when session changes (remount)", () => {
+ renderHook(() => useAutoOpenArtifacts(defaultProps));
+
+ // Open + close → suppressed
+ act(() => {
+ useCopilotUIStore.getState().openArtifact(makeArtifact(A_ID));
+ });
+ act(() => {
+ useCopilotUIStore.getState().closeArtifactPanel();
+ });
+
+ // Session change resets suppression
+ const { rerender } = renderHook(
+ ({ sessionId }) => useAutoOpenArtifacts({ ...defaultProps, sessionId }),
+ { initialProps: { sessionId: "s1" } },
+ );
+ act(() => {
+ rerender({ sessionId: "s2" });
+ });
+
+ // New registration should auto-open
+ const ref = makeArtifact(B_ID);
+ act(() => {
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+ });
+});
+
+// ── registerArtifactForAutoOpen (store unit tests) ────────────────
+
+describe("registerArtifactForAutoOpen", () => {
+ beforeEach(resetStore);
+
+ it("does not auto-open when not ready", () => {
+ const ref = makeArtifact(A_ID);
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("auto-opens an agent artifact when ready", () => {
+ useCopilotUIStore.getState().setAutoOpenReady();
+ const ref = makeArtifact(A_ID);
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+ expect(useCopilotUIStore.getState().artifactPanel.activeArtifact?.id).toBe(
+ A_ID,
+ );
+ });
+
+ it("does not auto-open user-uploaded artifacts", () => {
+ useCopilotUIStore.getState().setAutoOpenReady();
+ const ref = makeArtifact(A_ID, "upload.txt", "user-upload");
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("does not auto-open the same artifact twice", () => {
+ useCopilotUIStore.getState().setAutoOpenReady();
+ const ref = makeArtifact(A_ID);
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+
+ // Close, then re-register same ID
+ useCopilotUIStore.getState().closeArtifactPanel();
+ // Reset the user-closed flag so it doesn't interfere
+ useCopilotUIStore.getState().resetAutoOpenState();
+ useCopilotUIStore.getState().setAutoOpenReady();
+ // But A_ID is already known from the first registration — wait,
+ // resetAutoOpenState clears known IDs too. Let's re-add it as known.
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ // First call after reset → opens again (this is correct — reset clears known)
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+
+ // NOW close and try again without reset — same ID, should not re-trigger
+ useCopilotUIStore.getState().closeArtifactPanel();
+ // Manually set ready again (close doesn't affect ready)
+ const ref2 = makeArtifact(A_ID);
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref2);
+ // Already known, won't re-open
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("does not auto-open when user-closed flag is set", () => {
+ useCopilotUIStore.getState().setAutoOpenReady();
+ useCopilotUIStore.getState().markUserClosedForAutoOpen();
+ const ref = makeArtifact(A_ID);
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(ref);
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("upgrades activeArtifact metadata when a richer ref arrives for a known ID", () => {
+ useCopilotUIStore.getState().setAutoOpenReady();
+ const weakRef: ArtifactRef = {
+ id: A_ID,
+ title: "File " + A_ID.slice(0, 8),
+ mimeType: null,
+ sourceUrl: `/api/proxy/api/workspace/files/${A_ID}/download`,
+ origin: "agent",
+ };
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(weakRef);
+ expect(
+ useCopilotUIStore.getState().artifactPanel.activeArtifact?.mimeType,
+ ).toBeNull();
+
+ const richRef: ArtifactRef = {
+ id: A_ID,
+ title: "plan.md",
+ mimeType: "text/markdown",
+ sourceUrl: `/api/proxy/api/workspace/files/${A_ID}/download`,
+ origin: "agent",
+ };
+ useCopilotUIStore.getState().registerArtifactForAutoOpen(richRef);
+ expect(
+ useCopilotUIStore.getState().artifactPanel.activeArtifact?.mimeType,
+ ).toBe("text/markdown");
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/useAutoOpenArtifacts.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/useAutoOpenArtifacts.ts
index 04ef7d2631..d382682273 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/useAutoOpenArtifacts.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/useAutoOpenArtifacts.ts
@@ -1,38 +1,102 @@
"use client";
+import { UIDataTypes, UIMessage, UITools } from "ai";
import { useEffect, useRef } from "react";
import { useCopilotUIStore } from "../../store";
interface UseAutoOpenArtifactsOptions {
sessionId: string | null;
+ messages: UIMessage<unknown, UIDataTypes, UITools>[];
+ isLoadingSession: boolean;
+ isArtifactsEnabled: boolean;
}
+/**
+ * Manages the auto-open lifecycle for the artifact panel.
+ *
+ * Auto-open is now **card-based**: each `ArtifactCard` calls
+ * `registerArtifactForAutoOpen` on mount — the store decides whether to open
+ * the panel. This hook only manages lifecycle:
+ * - Resets state on session change
+ * - Gates readiness (don't auto-open stale artifacts on session load)
+ * - Detects user-close to suppress re-opening
+ * - Cleans up on unmount
+ *
+ * This replaces the previous message-scanning approach which iterated all
+ * messages on every streaming render tick.
+ */
export function useAutoOpenArtifacts({
sessionId,
+ messages,
+ isLoadingSession,
+ isArtifactsEnabled,
}: UseAutoOpenArtifactsOptions) {
- const resetArtifactPanel = useCopilotUIStore(
- (state) => state.resetArtifactPanel,
+ const resetArtifactPanel = useCopilotUIStore((s) => s.resetArtifactPanel);
+ const resetAutoOpenState = useCopilotUIStore((s) => s.resetAutoOpenState);
+ const setAutoOpenReady = useCopilotUIStore((s) => s.setAutoOpenReady);
+ const markUserClosedForAutoOpen = useCopilotUIStore(
+ (s) => s.markUserClosedForAutoOpen,
);
+ const isOpen = useCopilotUIStore((s) => s.artifactPanel.isOpen);
+
const prevSessionIdRef = useRef(sessionId);
+ const wasOpenRef = useRef(false);
+ const wasEverLoadingRef = useRef(isLoadingSession);
+ const hasMessages = messages.length > 0;
+ // Detect the user explicitly closing the panel (isOpen: true → false).
+ // Suppress auto-open for the remainder of this session once detected.
useEffect(() => {
- const isSessionChange = prevSessionIdRef.current !== sessionId;
- prevSessionIdRef.current = sessionId;
+ if (wasOpenRef.current && !isOpen) {
+ markUserClosedForAutoOpen();
+ }
+ wasOpenRef.current = isOpen;
+ }, [isOpen, markUserClosedForAutoOpen]);
- // Artifact previews should open only from an explicit user click.
- // When the session changes, fully clear the panel state so stale
- // active artifacts and back-stack entries never bleed into the next chat.
- if (isSessionChange) {
+ // Session change: fully clear the panel and auto-open state so stale
+ // artifacts don't bleed into the next chat.
+ useEffect(() => {
+ if (prevSessionIdRef.current !== sessionId) {
resetArtifactPanel();
+ resetAutoOpenState();
+ wasOpenRef.current = false;
+ }
+ prevSessionIdRef.current = sessionId;
+ }, [sessionId, resetArtifactPanel, resetAutoOpenState]);
+
+ // Mark auto-open as ready once the session is fully loaded.
+ //
+ // React fires child effects (ArtifactCard) before parent effects (this hook),
+ // so all existing cards have already called registerArtifactForAutoOpen by the
+ // time this effect fires. Those registrations populate _autoOpenKnownIds
+ // without triggering auto-open (ready is still false). Once we set ready=true,
+ // only *new* cards mounting in subsequent renders will auto-open.
+ //
+ // The wasEverLoadingRef guard handles a race where isLoadingSession goes false
+ // before messages are hydrated — without it, ready would be set while the
+ // messages array is empty, causing all cards that mount during hydration to
+ // look "new" and auto-open.
+ useEffect(() => {
+ if (isLoadingSession) {
+ wasEverLoadingRef.current = true;
+ return;
}
- }, [sessionId, resetArtifactPanel]);
+ if (!sessionId || !isArtifactsEnabled) return;
+ if (wasEverLoadingRef.current && !hasMessages) return;
+ setAutoOpenReady();
+ }, [
+ sessionId,
+ isLoadingSession,
+ isArtifactsEnabled,
+ hasMessages,
+ setAutoOpenReady,
+ ]);
- // Reset on unmount so navigating away from /copilot (to /profile, /home,
- // etc.) can't leave the panel open in the Zustand store, which would then
- // render the panel re-open when the user returns. See SECRT-2254/2220.
+ // Reset on unmount so navigating away from /copilot can't leave stale state.
useEffect(() => {
return () => {
resetArtifactPanel();
+ resetAutoOpenState();
};
- }, [resetArtifactPanel]);
+ }, [resetArtifactPanel, resetAutoOpenState]);
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/helpers.ts
index 215bf06c4e..f2fb7b2d67 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/helpers.ts
@@ -276,6 +276,9 @@ export function filePartToArtifactRef(
};
}
+const FULL_UUID =
+ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/;
+
export function extractWorkspaceArtifacts(text: string): ArtifactRef[] {
const seen = new Set<string>();
const artifacts: ArtifactRef[] = [];
@@ -286,6 +289,11 @@ export function extractWorkspaceArtifacts(text: string): ArtifactRef[] {
if (!parsed || seen.has(parsed.fileID)) continue;
+ // During streaming, workspace:// URIs arrive character-by-character.
+ // The regex matches progressively longer partial IDs — reject them so
+ // ArtifactCards don't mount/unmount with garbage IDs.
+ if (!FULL_UUID.test(parsed.fileID)) continue;
+
// Skip URIs inside image markdown (`![alt](workspace://...)`). Images are
// rendered inline via resolveWorkspaceUrls — surfacing them as cards too
// would double-render the same asset.
@@ -314,28 +322,32 @@ export function extractWorkspaceArtifacts(text: string): ArtifactRef[] {
export function getMessageArtifacts(
message: UIMessage<unknown, UIDataTypes, UITools>,
): ArtifactRef[] {
- const seen = new Set<string>();
- const artifacts: ArtifactRef[] = [];
+ const byId = new Map<string, ArtifactRef>();
+ // Process file parts first — they carry richer metadata (mediaType from the
+ // server, real filename) compared to workspace:// URIs extracted from text,
+ // which often lack a MIME fragment and fall back to "File {id}".
for (const part of message.parts) {
- if (part.type === "text") {
- for (const artifact of extractWorkspaceArtifacts(part.text)) {
- if (seen.has(artifact.id)) continue;
- seen.add(artifact.id);
- artifacts.push(artifact);
- }
- }
-
if (part.type === "file") {
const origin = message.role === "user" ? "user-upload" : "agent";
const artifact = filePartToArtifactRef(part, origin);
- if (!artifact || seen.has(artifact.id)) continue;
- seen.add(artifact.id);
- artifacts.push(artifact);
+ if (artifact) {
+ byId.set(artifact.id, artifact);
+ }
}
}
- return artifacts;
+ for (const part of message.parts) {
+ if (part.type === "text") {
+ for (const artifact of extractWorkspaceArtifacts(part.text)) {
+ if (!byId.has(artifact.id)) {
+ byId.set(artifact.id, artifact);
+ }
+ }
+ }
+ }
+
+ return Array.from(byId.values());
}
/**
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts
index d8dcbd132c..aff21824de 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts
@@ -1,7 +1,6 @@
import { Key, storage } from "@/services/storage/local-storage";
import { create } from "zustand";
import { clearContentCache } from "./components/ArtifactPanel/components/useArtifactContent";
-import { classifyArtifact } from "./components/ArtifactPanel/helpers";
import { ORIGINAL_TITLE, parseSessionIDs } from "./helpers";
export interface DeleteTarget {
@@ -96,10 +95,6 @@ function persistCompletedSessions(ids: Set<string>) {
}
}
-function isPreviewableArtifact(ref: ArtifactRef): boolean {
- return classifyArtifact(ref.mimeType, ref.title, ref.sizeBytes).openable;
-}
-
interface CopilotUIState {
/** Prompt extracted from URL hash (e.g. /copilot#prompt=...) for input prefill. */
initialPrompt: string | null;
@@ -136,6 +131,13 @@ interface CopilotUIState {
setArtifactPanelWidth: (width: number) => void;
goBackArtifact: () => void;
+ // Card-based auto-open: ArtifactCard registers itself on mount, the store
+ // decides whether to auto-open. Much simpler than message-scanning.
+ registerArtifactForAutoOpen: (ref: ArtifactRef) => void;
+ setAutoOpenReady: () => void;
+ markUserClosedForAutoOpen: () => void;
+ resetAutoOpenState: () => void;
+
/** Autopilot mode: 'extended_thinking' (default) or 'fast'. */
copilotChatMode: CopilotMode;
setCopilotChatMode: (mode: CopilotMode) => void;
@@ -151,7 +153,15 @@ interface CopilotUIState {
clearCopilotLocalData: () => void;
}
-export const useCopilotUIStore = create<CopilotUIState>((set) => ({
+// ── Card-based auto-open tracking ───────────────────────────────────
+// Module-level state — not in Zustand to avoid unnecessary re-renders.
+// ArtifactCard calls registerArtifactForAutoOpen on mount; the store
+// decides whether to auto-open based on these flags.
+const _autoOpenKnownIds = new Set<string>();
+let _autoOpenReady = false;
+let _autoOpenUserClosed = false;
+
+export const useCopilotUIStore = create<CopilotUIState>((set, get) => ({
initialPrompt: null,
setInitialPrompt: (prompt) => set({ initialPrompt: prompt }),
@@ -216,8 +226,6 @@ export const useCopilotUIStore = create<CopilotUIState>((set) => ({
},
openArtifact: (ref) =>
set((state) => {
- if (!isPreviewableArtifact(ref)) return state;
-
const { activeArtifact, history: prevHistory } = state.artifactPanel;
const topOfHistory = prevHistory[prevHistory.length - 1];
const isReturningToTop = topOfHistory?.id === ref.id;
@@ -305,6 +313,36 @@ export const useCopilotUIStore = create<CopilotUIState>((set) => ({
};
}),
+ // ── Card-based auto-open actions ─────────────────────────────────
+ registerArtifactForAutoOpen: (ref) => {
+ if (_autoOpenKnownIds.has(ref.id)) {
+ // Already known — upgrade activeArtifact metadata if this ref is richer
+ // (e.g. file-part ref with real MIME replacing text-extracted null MIME).
+ const active = get().artifactPanel.activeArtifact;
+ if (active?.id === ref.id && !active.mimeType && ref.mimeType) {
+ set((state) => ({
+ artifactPanel: { ...state.artifactPanel, activeArtifact: ref },
+ }));
+ }
+ return;
+ }
+ _autoOpenKnownIds.add(ref.id);
+ if (!_autoOpenReady || _autoOpenUserClosed || ref.origin !== "agent")
+ return;
+ get().openArtifact(ref);
+ },
+ setAutoOpenReady: () => {
+ _autoOpenReady = true;
+ },
+ markUserClosedForAutoOpen: () => {
+ _autoOpenUserClosed = true;
+ },
+ resetAutoOpenState: () => {
+ _autoOpenKnownIds.clear();
+ _autoOpenReady = false;
+ _autoOpenUserClosed = false;
+ },
+
copilotChatMode: (() => {
const saved = isClient ? storage.get(Key.COPILOT_MODE) : null;
return saved === "fast" ? "fast" : "extended_thinking";
@@ -335,6 +373,9 @@ export const useCopilotUIStore = create<CopilotUIState>((set) => ({
clearCopilotLocalData: () => {
clearContentCache();
+ _autoOpenKnownIds.clear();
+ _autoOpenReady = false;
+ _autoOpenUserClosed = false;
storage.clean(Key.COPILOT_NOTIFICATIONS_ENABLED);
storage.clean(Key.COPILOT_SOUND_ENABLED);
storage.clean(Key.COPILOT_NOTIFICATION_BANNER_DISMISSED);