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): tighten artifact preview behavior (#12770)

Nicholas Tindle142c5dbe99f4
diff --git a/autogpt_platform/frontend/.storybook/main.ts b/autogpt_platform/frontend/.storybook/main.ts
index 4e3070bfe1..235dbf4749 100644
--- a/autogpt_platform/frontend/.storybook/main.ts
+++ b/autogpt_platform/frontend/.storybook/main.ts
@@ -8,6 +8,7 @@ const config: StorybookConfig = {
"../src/components/molecules/**/*.stories.@(js|jsx|mjs|ts|tsx)",
"../src/components/ai-elements/**/*.stories.@(js|jsx|mjs|ts|tsx)",
"../src/components/renderers/**/*.stories.@(js|jsx|mjs|ts|tsx)",
+ "../src/app/[(]platform[)]/copilot/**/*.stories.@(js|jsx|mjs|ts|tsx)",
],
addons: [
"@storybook/addon-a11y",
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactCard/ArtifactCard.stories.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactCard/ArtifactCard.stories.tsx
new file mode 100644
index 0000000000..d4fc07fb48
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactCard/ArtifactCard.stories.tsx
@@ -0,0 +1,145 @@
+import type { Meta, StoryObj } from "@storybook/nextjs";
+import { ArtifactCard } from "./ArtifactCard";
+import type { ArtifactRef } from "../../store";
+import { useCopilotUIStore } from "../../store";
+
+function makeArtifact(overrides?: Partial<ArtifactRef>): ArtifactRef {
+ return {
+ id: "file-001",
+ title: "report.html",
+ mimeType: "text/html",
+ sourceUrl: "/api/proxy/api/workspace/files/file-001/download",
+ origin: "agent",
+ ...overrides,
+ };
+}
+
+const meta: Meta<typeof ArtifactCard> = {
+ title: "Copilot/ArtifactCard",
+ component: ArtifactCard,
+ tags: ["autodocs"],
+ parameters: {
+ layout: "padded",
+ docs: {
+ description: {
+ component:
+ "Inline artifact card rendered in chat messages. Openable artifacts show a caret and open the ArtifactPanel on click. Download-only artifacts trigger a file download.",
+ },
+ },
+ },
+ decorators: [
+ (Story) => (
+ <div className="w-96">
+ <Story />
+ </div>
+ ),
+ ],
+};
+
+export default meta;
+type Story = StoryObj<typeof meta>;
+
+export const OpenableHTML: Story = {
+ name: "Openable (HTML)",
+ args: {
+ artifact: makeArtifact({
+ title: "dashboard.html",
+ mimeType: "text/html",
+ }),
+ },
+};
+
+export const OpenableImage: Story = {
+ name: "Openable (Image)",
+ args: {
+ artifact: makeArtifact({
+ id: "img-card",
+ title: "chart.png",
+ mimeType: "image/png",
+ }),
+ },
+};
+
+export const OpenableCode: Story = {
+ name: "Openable (Code)",
+ args: {
+ artifact: makeArtifact({
+ title: "script.py",
+ mimeType: "text/x-python",
+ }),
+ },
+};
+
+export const DownloadOnly: Story = {
+ name: "Download Only (ZIP)",
+ args: {
+ artifact: makeArtifact({
+ title: "archive.zip",
+ mimeType: "application/zip",
+ sizeBytes: 2_500_000,
+ }),
+ },
+};
+
+export const PreviewableVideo: Story = {
+ name: "Previewable (Video)",
+ args: {
+ artifact: makeArtifact({
+ title: "demo.mp4",
+ mimeType: "video/mp4",
+ sizeBytes: 15_000_000,
+ }),
+ },
+ parameters: {
+ docs: {
+ description: {
+ story:
+ "Videos with supported formats (MP4, WebM, M4V) are previewable inline in the artifact panel.",
+ },
+ },
+ },
+};
+
+export const WithSize: Story = {
+ name: "With File Size",
+ args: {
+ artifact: makeArtifact({
+ title: "data.csv",
+ mimeType: "text/csv",
+ sizeBytes: 524_288,
+ }),
+ },
+};
+
+export const UserUpload: Story = {
+ name: "User Upload Origin",
+ args: {
+ artifact: makeArtifact({
+ title: "requirements.txt",
+ mimeType: "text/plain",
+ origin: "user-upload",
+ }),
+ },
+};
+
+export const ActiveState: Story = {
+ name: "Active (Panel Open)",
+ args: {
+ artifact: makeArtifact({ id: "active-card" }),
+ },
+ decorators: [
+ (Story) => {
+ useCopilotUIStore.setState({
+ artifactPanel: {
+ isOpen: true,
+ isMinimized: false,
+ isMaximized: false,
+ width: 600,
+ activeArtifact: makeArtifact({ id: "active-card" }),
+ history: [],
+ },
+ });
+ return <Story />;
+ },
+ ],
+};
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/ArtifactPanel.stories.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/ArtifactPanel.stories.tsx
new file mode 100644
index 0000000000..e7b457c6a9
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/ArtifactPanel.stories.tsx
@@ -0,0 +1,223 @@
+import type { Meta, StoryObj } from "@storybook/nextjs";
+import { http, HttpResponse } from "msw";
+import { ArtifactPanel } from "./ArtifactPanel";
+import { useCopilotUIStore } from "../../store";
+import type { ArtifactRef } from "../../store";
+
+const PROXY_BASE = "/api/proxy/api/workspace/files";
+
+function makeArtifact(overrides?: Partial<ArtifactRef>): ArtifactRef {
+ return {
+ id: "file-001",
+ title: "report.html",
+ mimeType: "text/html",
+ sourceUrl: `${PROXY_BASE}/file-001/download`,
+ origin: "agent",
+ ...overrides,
+ };
+}
+
+function openPanelWith(artifact: ArtifactRef) {
+ useCopilotUIStore.setState({
+ artifactPanel: {
+ isOpen: true,
+ isMinimized: false,
+ isMaximized: false,
+ width: 600,
+ activeArtifact: artifact,
+ history: [],
+ },
+ });
+}
+
+const meta: Meta<typeof ArtifactPanel> = {
+ title: "Copilot/ArtifactPanel",
+ component: ArtifactPanel,
+ tags: ["autodocs"],
+ parameters: {
+ layout: "fullscreen",
+ docs: {
+ description: {
+ component:
+ "Side panel for previewing workspace artifacts. Supports resize, minimize, maximize, and navigation history. Bug: panel auto-opens on chat switch instead of staying collapsed.",
+ },
+ },
+ },
+ decorators: [
+ (Story) => (
+ <div className="flex h-[600px] w-full">
+ <div className="flex-1 bg-zinc-50 p-8">
+ <p className="text-sm text-zinc-500">Chat area</p>
+ </div>
+ <Story />
+ </div>
+ ),
+ ],
+};
+
+export default meta;
+type Story = StoryObj<typeof meta>;
+
+export const OpenWithTextArtifact: Story = {
+ name: "Open — Text File",
+ decorators: [
+ (Story) => {
+ openPanelWith(
+ makeArtifact({ title: "notes.txt", mimeType: "text/plain" }),
+ );
+ return <Story />;
+ },
+ ],
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/file-001/download`, () => {
+ return HttpResponse.text(
+ "These are some notes from the agent execution.\n\nKey findings:\n1. Performance improved by 23%\n2. Memory usage reduced\n3. Error rate dropped to 0.1%",
+ );
+ }),
+ ],
+ },
+ },
+};
+
+export const OpenWithHTMLArtifact: Story = {
+ name: "Open — HTML",
+ decorators: [
+ (Story) => {
+ openPanelWith(
+ makeArtifact({
+ id: "html-panel",
+ title: "dashboard.html",
+ mimeType: "text/html",
+ sourceUrl: `${PROXY_BASE}/html-panel/download`,
+ }),
+ );
+ return <Story />;
+ },
+ ],
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/html-panel/download`, () => {
+ return HttpResponse.text(
+ `<!DOCTYPE html><html><body class="p-8 font-sans"><h1 class="text-2xl font-bold text-indigo-600">Dashboard</h1><p class="mt-2 text-gray-600">HTML artifact in the panel.</p></body></html>`,
+ );
+ }),
+ ],
+ },
+ },
+};
+
+export const OpenWithImageArtifact: Story = {
+ name: "Open — Image (Bug: No Loading State)",
+ decorators: [
+ (Story) => {
+ openPanelWith(
+ makeArtifact({
+ id: "img-panel",
+ title: "chart.png",
+ mimeType: "image/png",
+ sourceUrl: `${PROXY_BASE}/img-panel/download`,
+ }),
+ );
+ return <Story />;
+ },
+ ],
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/img-panel/download`, () => {
+ return HttpResponse.text(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="500" height="300"><rect width="500" height="300" fill="#dbeafe"/><text x="250" y="150" text-anchor="middle" fill="#1e40af" font-size="20">Image Preview (no skeleton)</text></svg>',
+ { headers: { "Content-Type": "image/svg+xml" } },
+ );
+ }),
+ ],
+ },
+ docs: {
+ description: {
+ story:
+ "**BUG:** Image artifacts render with a bare `<img>` tag — no loading skeleton or error handling. Compare with text/HTML artifacts which show a proper skeleton while loading.",
+ },
+ },
+ },
+};
+
+export const MinimizedStrip: Story = {
+ name: "Minimized",
+ decorators: [
+ (Story) => {
+ useCopilotUIStore.setState({
+ artifactPanel: {
+ isOpen: true,
+ isMinimized: true,
+ isMaximized: false,
+ width: 600,
+ activeArtifact: makeArtifact(),
+ history: [],
+ },
+ });
+ return <Story />;
+ },
+ ],
+};
+
+export const ErrorState: Story = {
+ name: "Error — Failed to Load (Stale Artifact)",
+ decorators: [
+ (Story) => {
+ openPanelWith(
+ makeArtifact({
+ id: "stale-panel",
+ title: "old-report.html",
+ mimeType: "text/html",
+ sourceUrl: `${PROXY_BASE}/stale-panel/download`,
+ }),
+ );
+ return <Story />;
+ },
+ ],
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/stale-panel/download`, () => {
+ return new HttpResponse(null, { status: 404 });
+ }),
+ ],
+ },
+ docs: {
+ description: {
+ story:
+ "Shows what users see when opening a previously generated artifact that no longer exists on the backend (404). The 'Try again' button retries the fetch.",
+ },
+ },
+ },
+};
+
+export const Closed: Story = {
+ name: "Closed (Default State)",
+ decorators: [
+ (Story) => {
+ useCopilotUIStore.setState({
+ artifactPanel: {
+ isOpen: false,
+ isMinimized: false,
+ isMaximized: false,
+ width: 600,
+ activeArtifact: null,
+ history: [],
+ },
+ });
+ return <Story />;
+ },
+ ],
+ parameters: {
+ docs: {
+ description: {
+ story:
+ "The default state — panel is closed. It should only open when a user clicks on an artifact card in the chat.",
+ },
+ },
+ },
+};
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/__tests__/downloadArtifact.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/__tests__/downloadArtifact.test.ts
new file mode 100644
index 0000000000..4095841e89
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/__tests__/downloadArtifact.test.ts
@@ -0,0 +1,413 @@
+import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
+import { downloadArtifact } from "../downloadArtifact";
+import type { ArtifactRef } from "../../../store";
+
+function makeArtifact(overrides?: Partial<ArtifactRef>): ArtifactRef {
+ return {
+ id: "file-001",
+ title: "report.pdf",
+ mimeType: "application/pdf",
+ sourceUrl: "/api/proxy/api/workspace/files/file-001/download",
+ origin: "agent",
+ ...overrides,
+ };
+}
+
+describe("downloadArtifact", () => {
+ let clickSpy: ReturnType<typeof vi.fn>;
+ let removeSpy: ReturnType<typeof vi.fn>;
+
+ beforeEach(() => {
+ clickSpy = vi.fn();
+ removeSpy = vi.fn();
+
+ vi.stubGlobal(
+ "URL",
+ Object.assign(URL, {
+ createObjectURL: vi.fn().mockReturnValue("blob:fake-url"),
+ revokeObjectURL: vi.fn(),
+ }),
+ );
+
+ vi.spyOn(document, "createElement").mockReturnValue({
+ href: "",
+ download: "",
+ click: clickSpy,
+ remove: removeSpy,
+ } as unknown as HTMLAnchorElement);
+
+ vi.spyOn(document.body, "appendChild").mockImplementation(
+ (node) => node as ChildNode,
+ );
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+ });
+
+ it("downloads file successfully on 200 response", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["pdf content"])),
+ }),
+ );
+
+ await downloadArtifact(makeArtifact());
+
+ expect(fetch).toHaveBeenCalledWith(
+ "/api/proxy/api/workspace/files/file-001/download",
+ );
+ expect(clickSpy).toHaveBeenCalled();
+ expect(removeSpy).toHaveBeenCalled();
+ expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:fake-url");
+ });
+
+ it("rejects on persistent server error after exhausting retries", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: false,
+ status: 500,
+ }),
+ );
+
+ await expect(downloadArtifact(makeArtifact())).rejects.toThrow(
+ "Download failed: 500",
+ );
+ expect(clickSpy).not.toHaveBeenCalled();
+ });
+
+ it("rejects on persistent network error after exhausting retries", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ return Promise.reject(new Error("Network error"));
+ }),
+ );
+
+ await expect(downloadArtifact(makeArtifact())).rejects.toThrow(
+ "Network error",
+ );
+ expect(callCount).toBe(3);
+ expect(clickSpy).not.toHaveBeenCalled();
+ });
+
+ it("retries on transient network error and succeeds", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ if (callCount === 1) {
+ return Promise.reject(new Error("Connection reset"));
+ }
+ return Promise.resolve({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ });
+ }),
+ );
+
+ await downloadArtifact(makeArtifact());
+ expect(callCount).toBe(2);
+ expect(clickSpy).toHaveBeenCalled();
+ });
+
+ it("retries on transient 500 and succeeds", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ if (callCount === 1) {
+ return Promise.resolve({ ok: false, status: 500 });
+ }
+ return Promise.resolve({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ });
+ }),
+ );
+
+ // Should succeed on second attempt
+ await downloadArtifact(makeArtifact());
+ expect(callCount).toBe(2);
+ expect(clickSpy).toHaveBeenCalled();
+ });
+
+ it("sanitizes dangerous filenames", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ }),
+ );
+
+ const anchor = {
+ href: "",
+ download: "",
+ click: clickSpy,
+ remove: removeSpy,
+ };
+ vi.spyOn(document, "createElement").mockReturnValue(
+ anchor as unknown as HTMLAnchorElement,
+ );
+
+ await downloadArtifact(makeArtifact({ title: "../../../etc/passwd" }));
+
+ expect(anchor.download).not.toContain("..");
+ expect(anchor.download).not.toContain("/");
+ });
+
+ // ── Transient retry codes ─────────────────────────────────────────
+
+ it("retries on 408 (Request Timeout) and succeeds", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ if (callCount === 1) {
+ return Promise.resolve({ ok: false, status: 408 });
+ }
+ return Promise.resolve({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ });
+ }),
+ );
+
+ await downloadArtifact(makeArtifact());
+ expect(callCount).toBe(2);
+ expect(clickSpy).toHaveBeenCalled();
+ });
+
+ it("retries on 429 (Too Many Requests) and succeeds", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ if (callCount === 1) {
+ return Promise.resolve({ ok: false, status: 429 });
+ }
+ return Promise.resolve({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ });
+ }),
+ );
+
+ await downloadArtifact(makeArtifact());
+ expect(callCount).toBe(2);
+ expect(clickSpy).toHaveBeenCalled();
+ });
+
+ // ── Non-transient errors ──────────────────────────────────────────
+
+ it("rejects immediately on 403 (non-transient) without retry", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ return Promise.resolve({ ok: false, status: 403 });
+ }),
+ );
+
+ await expect(downloadArtifact(makeArtifact())).rejects.toThrow(
+ "Download failed: 403",
+ );
+ expect(callCount).toBe(1);
+ expect(clickSpy).not.toHaveBeenCalled();
+ });
+
+ it("rejects immediately on 404 without retry", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ return Promise.resolve({ ok: false, status: 404 });
+ }),
+ );
+
+ await expect(downloadArtifact(makeArtifact())).rejects.toThrow(
+ "Download failed: 404",
+ );
+ expect(callCount).toBe(1);
+ });
+
+ // ── Exhausted retries ─────────────────────────────────────────────
+
+ it("rejects after exhausting all retries on persistent 500", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ return Promise.resolve({ ok: false, status: 500 });
+ }),
+ );
+
+ await expect(downloadArtifact(makeArtifact())).rejects.toThrow(
+ "Download failed: 500",
+ );
+ // Initial attempt + 2 retries = 3 total
+ expect(callCount).toBe(3);
+ expect(clickSpy).not.toHaveBeenCalled();
+ });
+
+ // ── Filename edge cases ───────────────────────────────────────────
+
+ it("falls back to 'download' when title is empty", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ }),
+ );
+
+ const anchor = {
+ href: "",
+ download: "",
+ click: clickSpy,
+ remove: removeSpy,
+ };
+ vi.spyOn(document, "createElement").mockReturnValue(
+ anchor as unknown as HTMLAnchorElement,
+ );
+
+ await downloadArtifact(makeArtifact({ title: "" }));
+ expect(anchor.download).toBe("download");
+ });
+
+ it("falls back to 'download' when title is only dots", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ }),
+ );
+
+ const anchor = {
+ href: "",
+ download: "",
+ click: clickSpy,
+ remove: removeSpy,
+ };
+ vi.spyOn(document, "createElement").mockReturnValue(
+ anchor as unknown as HTMLAnchorElement,
+ );
+
+ // Dot-only names should not produce a hidden or empty filename.
+ await downloadArtifact(makeArtifact({ title: "...." }));
+ expect(anchor.download).toBe("download");
+ });
+
+ it("replaces special chars with underscores (not empty)", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ }),
+ );
+
+ const anchor = {
+ href: "",
+ download: "",
+ click: clickSpy,
+ remove: removeSpy,
+ };
+ vi.spyOn(document, "createElement").mockReturnValue(
+ anchor as unknown as HTMLAnchorElement,
+ );
+
+ await downloadArtifact(makeArtifact({ title: '***???"' }));
+ // Special chars become underscores, not removed
+ expect(anchor.download).toBe("_______");
+ });
+
+ it("strips leading dots from filename", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ }),
+ );
+
+ const anchor = {
+ href: "",
+ download: "",
+ click: clickSpy,
+ remove: removeSpy,
+ };
+ vi.spyOn(document, "createElement").mockReturnValue(
+ anchor as unknown as HTMLAnchorElement,
+ );
+
+ await downloadArtifact(makeArtifact({ title: "...hidden.txt" }));
+ expect(anchor.download).not.toMatch(/^\./);
+ expect(anchor.download).toContain("hidden.txt");
+ });
+
+ it("replaces Windows-reserved characters", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ }),
+ );
+
+ const anchor = {
+ href: "",
+ download: "",
+ click: clickSpy,
+ remove: removeSpy,
+ };
+ vi.spyOn(document, "createElement").mockReturnValue(
+ anchor as unknown as HTMLAnchorElement,
+ );
+
+ await downloadArtifact(
+ makeArtifact({ title: "file<name>with:bad*chars?.txt" }),
+ );
+ expect(anchor.download).not.toMatch(/[<>:*?]/);
+ });
+
+ it("replaces control characters in filename", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ blob: () => Promise.resolve(new Blob(["content"])),
+ }),
+ );
+
+ const anchor = {
+ href: "",
+ download: "",
+ click: clickSpy,
+ remove: removeSpy,
+ };
+ vi.spyOn(document, "createElement").mockReturnValue(
+ anchor as unknown as HTMLAnchorElement,
+ );
+
+ await downloadArtifact(
+ makeArtifact({ title: "file\x00with\x1fcontrol.txt" }),
+ );
+ expect(anchor.download).not.toMatch(/[\x00-\x1f]/);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactContent.stories.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactContent.stories.tsx
new file mode 100644
index 0000000000..6b9ef31631
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactContent.stories.tsx
@@ -0,0 +1,460 @@
+import type { Meta, StoryObj } from "@storybook/nextjs";
+import { http, HttpResponse } from "msw";
+import { ArtifactContent } from "./ArtifactContent";
+import type { ArtifactRef } from "../../../store";
+import type { ArtifactClassification } from "../helpers";
+import {
+ Code,
+ File,
+ FileHtml,
+ FileText,
+ Image,
+ Table,
+} from "@phosphor-icons/react";
+
+const PROXY_BASE = "/api/proxy/api/workspace/files";
+
+function makeArtifact(overrides?: Partial<ArtifactRef>): ArtifactRef {
+ return {
+ id: "file-001",
+ title: "test.txt",
+ mimeType: "text/plain",
+ sourceUrl: `${PROXY_BASE}/file-001/download`,
+ origin: "agent",
+ ...overrides,
+ };
+}
+
+function makeClassification(
+ overrides?: Partial<ArtifactClassification>,
+): ArtifactClassification {
+ return {
+ type: "text",
+ icon: FileText,
+ label: "Text",
+ openable: true,
+ hasSourceToggle: false,
+ ...overrides,
+ };
+}
+
+const meta: Meta<typeof ArtifactContent> = {
+ title: "Copilot/ArtifactContent",
+ component: ArtifactContent,
+ tags: ["autodocs"],
+ parameters: {
+ layout: "padded",
+ docs: {
+ description: {
+ component:
+ "Renders artifact content based on file type classification. Supports images, HTML, code, CSV, JSON, markdown, PDF, and plain text. Bug: image artifacts render as bare <img> with no loading/error states.",
+ },
+ },
+ },
+ decorators: [
+ (Story) => (
+ <div
+ className="flex h-[500px] w-[600px] flex-col overflow-hidden border border-zinc-200"
+ style={{ resize: "both" }}
+ >
+ <Story />
+ </div>
+ ),
+ ],
+};
+
+export default meta;
+type Story = StoryObj<typeof meta>;
+
+export const ImageArtifactPNG: Story = {
+ name: "Image (PNG) — No Loading Skeleton (Bug #1)",
+ args: {
+ artifact: makeArtifact({
+ id: "img-png",
+ title: "chart.png",
+ mimeType: "image/png",
+ sourceUrl: `${PROXY_BASE}/img-png/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({ type: "image", icon: Image }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/img-png/download`, () => {
+ return HttpResponse.text(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="400" height="300"><rect width="400" height="300" fill="#e0e7ff"/><text x="200" y="150" text-anchor="middle" fill="#4338ca" font-size="24">PNG Placeholder</text></svg>',
+ { headers: { "Content-Type": "image/svg+xml" } },
+ );
+ }),
+ ],
+ },
+ docs: {
+ description: {
+ story:
+ "**BUG:** This renders a bare `<img>` tag with no loading skeleton or error handling. Compare with WorkspaceFileRenderer which has proper Skeleton + onError states.",
+ },
+ },
+ },
+};
+
+export const ImageArtifactSVG: Story = {
+ name: "Image (SVG)",
+ args: {
+ artifact: makeArtifact({
+ id: "img-svg",
+ title: "diagram.svg",
+ mimeType: "image/svg+xml",
+ sourceUrl: `${PROXY_BASE}/img-svg/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({ type: "image", icon: Image }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/img-svg/download`, () => {
+ return HttpResponse.text(
+ '<svg xmlns="http://www.w3.org/2000/svg" width="400" height="300"><rect width="400" height="300" fill="#fef3c7"/><circle cx="200" cy="150" r="80" fill="#f59e0b"/><text x="200" y="155" text-anchor="middle" fill="white" font-size="20">SVG OK</text></svg>',
+ { headers: { "Content-Type": "image/svg+xml" } },
+ );
+ }),
+ ],
+ },
+ },
+};
+
+export const HTMLArtifact: Story = {
+ name: "HTML",
+ args: {
+ artifact: makeArtifact({
+ id: "html-001",
+ title: "page.html",
+ mimeType: "text/html",
+ sourceUrl: `${PROXY_BASE}/html-001/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({
+ type: "html",
+ icon: FileHtml,
+ label: "HTML",
+ hasSourceToggle: true,
+ }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/html-001/download`, () => {
+ return HttpResponse.text(
+ `<!DOCTYPE html>
+<html>
+<head><title>Artifact Preview</title></head>
+<body class="p-8 font-sans">
+ <h1 class="text-2xl font-bold text-indigo-600 mb-4">HTML Artifact</h1>
+ <p class="text-gray-700">This is an HTML artifact rendered in a sandboxed iframe with Tailwind CSS injected.</p>
+ <div class="mt-4 p-4 bg-blue-50 rounded-lg border border-blue-200">
+ <p class="text-blue-800">Interactive content works via allow-scripts sandbox.</p>
+ </div>
+</body>
+</html>`,
+ { headers: { "Content-Type": "text/html" } },
+ );
+ }),
+ ],
+ },
+ },
+};
+
+export const CodeArtifact: Story = {
+ name: "Code (Python)",
+ args: {
+ artifact: makeArtifact({
+ id: "code-001",
+ title: "analysis.py",
+ mimeType: "text/x-python",
+ sourceUrl: `${PROXY_BASE}/code-001/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({
+ type: "code",
+ icon: Code,
+ label: "Code",
+ }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/code-001/download`, () => {
+ return HttpResponse.text(
+ `import pandas as pd
+import matplotlib.pyplot as plt
+
+def analyze_data(filepath: str) -> pd.DataFrame:
+ """Load and analyze CSV data."""
+ df = pd.read_csv(filepath)
+ summary = df.describe()
+ print(f"Loaded {len(df)} rows")
+ return summary
+
+if __name__ == "__main__":
+ result = analyze_data("data.csv")
+ print(result)`,
+ { headers: { "Content-Type": "text/plain" } },
+ );
+ }),
+ ],
+ },
+ },
+};
+
+export const CSVArtifact: Story = {
+ name: "CSV (Spreadsheet)",
+ args: {
+ artifact: makeArtifact({
+ id: "csv-001",
+ title: "data.csv",
+ mimeType: "text/csv",
+ sourceUrl: `${PROXY_BASE}/csv-001/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({
+ type: "csv",
+ icon: Table,
+ label: "Spreadsheet",
+ hasSourceToggle: true,
+ }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/csv-001/download`, () => {
+ return HttpResponse.text(
+ `Name,Age,City,Score
+Alice,28,New York,92
+Bob,35,San Francisco,87
+Charlie,22,Chicago,95
+Diana,31,Boston,88
+Eve,27,Seattle,91`,
+ { headers: { "Content-Type": "text/csv" } },
+ );
+ }),
+ ],
+ },
+ },
+};
+
+export const JSONArtifact: Story = {
+ name: "JSON (Data)",
+ args: {
+ artifact: makeArtifact({
+ id: "json-001",
+ title: "config.json",
+ mimeType: "application/json",
+ sourceUrl: `${PROXY_BASE}/json-001/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({
+ type: "json",
+ icon: Code,
+ label: "Data",
+ hasSourceToggle: true,
+ }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/json-001/download`, () => {
+ return HttpResponse.text(
+ JSON.stringify(
+ {
+ name: "AutoGPT Agent",
+ version: "2.0",
+ capabilities: ["web_search", "code_execution", "file_io"],
+ settings: { maxTokens: 4096, temperature: 0.7 },
+ },
+ null,
+ 2,
+ ),
+ { headers: { "Content-Type": "application/json" } },
+ );
+ }),
+ ],
+ },
+ },
+};
+
+export const MarkdownArtifact: Story = {
+ name: "Markdown",
+ args: {
+ artifact: makeArtifact({
+ id: "md-001",
+ title: "README.md",
+ mimeType: "text/markdown",
+ sourceUrl: `${PROXY_BASE}/md-001/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({
+ type: "markdown",
+ icon: FileText,
+ label: "Document",
+ hasSourceToggle: true,
+ }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/md-001/download`, () => {
+ return HttpResponse.text(
+ `# Project Summary
+
+## Overview
+This is a **markdown** artifact rendered through the global renderer registry.
+
+## Features
+- Headings and paragraphs
+- **Bold** and *italic* text
+- Lists and code blocks
+
+\`\`\`python
+print("Hello from markdown!")
+\`\`\`
+
+> Blockquotes are also supported.`,
+ { headers: { "Content-Type": "text/plain" } },
+ );
+ }),
+ ],
+ },
+ },
+};
+
+export const PDFArtifact: Story = {
+ name: "PDF",
+ args: {
+ artifact: makeArtifact({
+ id: "pdf-001",
+ title: "report.pdf",
+ mimeType: "application/pdf",
+ sourceUrl: `${PROXY_BASE}/pdf-001/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({
+ type: "pdf",
+ icon: FileText,
+ label: "PDF",
+ }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/pdf-001/download`, () => {
+ return HttpResponse.arrayBuffer(new ArrayBuffer(100), {
+ headers: { "Content-Type": "application/pdf" },
+ });
+ }),
+ ],
+ },
+ docs: {
+ description: {
+ story:
+ "PDF artifacts are rendered in an unsandboxed iframe using a blob URL (Chromium bug #413851 prevents sandboxed PDF rendering).",
+ },
+ },
+ },
+};
+
+export const ErrorState: Story = {
+ name: "Error — Failed to Load Content",
+ args: {
+ artifact: makeArtifact({
+ id: "error-001",
+ title: "old-report.html",
+ mimeType: "text/html",
+ sourceUrl: `${PROXY_BASE}/error-001/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({
+ type: "html",
+ icon: FileHtml,
+ label: "HTML",
+ hasSourceToggle: true,
+ }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/error-001/download`, () => {
+ return new HttpResponse(null, { status: 404 });
+ }),
+ ],
+ },
+ docs: {
+ description: {
+ story:
+ "Shows the error state when an artifact fails to load (e.g., old/expired file returning 404). Includes a 'Try again' retry button.",
+ },
+ },
+ },
+};
+
+export const LoadingSkeleton: Story = {
+ name: "Loading State",
+ args: {
+ artifact: makeArtifact({
+ id: "loading-001",
+ title: "loading.html",
+ mimeType: "text/html",
+ sourceUrl: `${PROXY_BASE}/loading-001/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({
+ type: "html",
+ icon: FileHtml,
+ label: "HTML",
+ }),
+ },
+ parameters: {
+ msw: {
+ handlers: [
+ http.get(`${PROXY_BASE}/loading-001/download`, async () => {
+ // Delay response to show loading state
+ await new Promise((r) => setTimeout(r, 999999));
+ return HttpResponse.text("never resolves");
+ }),
+ ],
+ },
+ docs: {
+ description: {
+ story:
+ "Shows the skeleton loading state while content is being fetched.",
+ },
+ },
+ },
+};
+
+export const DownloadOnly: Story = {
+ name: "Download Only (Binary)",
+ args: {
+ artifact: makeArtifact({
+ id: "bin-001",
+ title: "archive.zip",
+ mimeType: "application/zip",
+ sourceUrl: `${PROXY_BASE}/bin-001/download`,
+ }),
+ isSourceView: false,
+ classification: makeClassification({
+ type: "download-only",
+ icon: File,
+ label: "File",
+ openable: false,
+ }),
+ },
+ parameters: {
+ docs: {
+ description: {
+ story:
+ "Download-only files (binary, video, etc.) are not rendered inline. The ArtifactPanel shows nothing for these — they are handled by ArtifactCard with a download button.",
+ },
+ },
+ },
+};
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactContent.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactContent.tsx
index 6e057293b5..506cbc3b60 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactContent.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactContent.tsx
@@ -2,7 +2,8 @@
import { globalRegistry } from "@/components/contextual/OutputRenderers";
import { codeRenderer } from "@/components/contextual/OutputRenderers/renderers/CodeRenderer";
-import { Suspense } from "react";
+import { Suspense, useState } from "react";
+import { Skeleton } from "@/components/ui/skeleton";
import type { ArtifactRef } from "../../../store";
import type { ArtifactClassification } from "../helpers";
import { ArtifactReactPreview } from "./ArtifactReactPreview";
@@ -63,6 +64,90 @@ function ArtifactContentLoader({
);
}
+function ArtifactImage({ src, alt }: { src: string; alt: string }) {
+ const [loaded, setLoaded] = useState(false);
+ const [error, setError] = useState(false);
+
+ if (error) {
+ return (
+ <div
+ role="alert"
+ className="flex flex-col items-center justify-center gap-3 p-8 text-center"
+ >
+ <p className="text-sm text-zinc-500">Failed to load image</p>
+ <button
+ type="button"
+ onClick={() => {
+ setError(false);
+ setLoaded(false);
+ }}
+ className="rounded-md border border-zinc-200 bg-white px-3 py-1.5 text-xs font-medium text-zinc-700 shadow-sm transition-colors hover:bg-zinc-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-violet-400"
+ >
+ Try again
+ </button>
+ </div>
+ );
+ }
+
+ return (
+ <div className="relative flex items-center justify-center p-4">
+ {!loaded && (
+ <Skeleton className="absolute inset-4 h-[calc(100%-2rem)] w-[calc(100%-2rem)] rounded-md" />
+ )}
+ {/* eslint-disable-next-line @next/next/no-img-element */}
+ <img
+ src={src}
+ alt={alt}
+ className={`max-h-full max-w-full object-contain transition-opacity ${loaded ? "opacity-100" : "opacity-0"}`}
+ onLoad={() => setLoaded(true)}
+ onError={() => setError(true)}
+ />
+ </div>
+ );
+}
+
+function ArtifactVideo({ src }: { src: string }) {
+ const [loaded, setLoaded] = useState(false);
+ const [error, setError] = useState(false);
+
+ if (error) {
+ return (
+ <div
+ role="alert"
+ className="flex flex-col items-center justify-center gap-3 p-8 text-center"
+ >
+ <p className="text-sm text-zinc-500">Failed to load video</p>
+ <button
+ type="button"
+ onClick={() => {
+ setError(false);
+ setLoaded(false);
+ }}
+ className="rounded-md border border-zinc-200 bg-white px-3 py-1.5 text-xs font-medium text-zinc-700 shadow-sm transition-colors hover:bg-zinc-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-violet-400"
+ >
+ Try again
+ </button>
+ </div>
+ );
+ }
+
+ return (
+ <div className="relative flex items-center justify-center p-4">
+ {!loaded && (
+ <Skeleton className="absolute inset-4 h-[calc(100%-2rem)] w-[calc(100%-2rem)] rounded-md" />
+ )}
+ <video
+ src={src}
+ controls
+ preload="metadata"
+ className={`max-h-full max-w-full rounded-md transition-opacity ${loaded ? "opacity-100" : "opacity-0"}`}
+ onLoadedMetadata={() => setLoaded(true)}
+ onError={() => setError(true)}
+ />
+ </div>
+ );
+}
+
function ArtifactRenderer({
artifact,
content,
@@ -79,17 +164,19 @@ function ArtifactRenderer({
// Image: render directly from URL (no content fetch)
if (classification.type === "image") {
return (
- <div className="flex items-center justify-center p-4">
- {/* eslint-disable-next-line @next/next/no-img-element */}
- <img
- src={artifact.sourceUrl}
- alt={artifact.title}
- className="max-h-full max-w-full object-contain"
- />
- </div>
+ <ArtifactImage
+ key={artifact.sourceUrl}
+ src={artifact.sourceUrl}
+ alt={artifact.title}
+ />
);
}
+ // Video: render with <video> controls (no content fetch)
+ if (classification.type === "video") {
+ return <ArtifactVideo key={artifact.sourceUrl} src={artifact.sourceUrl} />;
+ }
+
if (classification.type === "pdf" && pdfUrl) {
// No sandbox — Chrome/Edge block PDF rendering in sandboxed iframes
// (Chromium bug #413851). The blob URL has a null origin so it can't
@@ -164,7 +251,16 @@ function ArtifactRenderer({
// CSV: pass with explicit metadata so CSVRenderer matches
if (classification.type === "csv") {
- const csvMeta = { mimeType: "text/csv", filename: artifact.title };
+ const normalizedMime = artifact.mimeType
+ ?.toLowerCase()
+ .split(";")[0]
+ ?.trim();
+ const csvMimeType =
+ normalizedMime === "text/tab-separated-values" ||
+ artifact.title.toLowerCase().endsWith(".tsv")
+ ? "text/tab-separated-values"
+ : "text/csv";
+ const csvMeta = { mimeType: csvMimeType, filename: artifact.title };
const csvRenderer = globalRegistry.getRenderer(content, csvMeta);
if (csvRenderer) {
return <div className="p-4">{csvRenderer.render(content, csvMeta)}</div>;
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactReactPreview.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactReactPreview.test.tsx
new file mode 100644
index 0000000000..be5b17781d
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/ArtifactReactPreview.test.tsx
@@ -0,0 +1,67 @@
+import { render, screen, waitFor } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { ArtifactReactPreview } from "./ArtifactReactPreview";
+import {
+ buildReactArtifactSrcDoc,
+ collectPreviewStyles,
+ transpileReactArtifactSource,
+} from "./reactArtifactPreview";
+
+vi.mock("./reactArtifactPreview", () => ({
+ buildReactArtifactSrcDoc: vi.fn(),
+ collectPreviewStyles: vi.fn(),
+ transpileReactArtifactSource: vi.fn(),
+}));
+
+describe("ArtifactReactPreview", () => {
+ beforeEach(() => {
+ vi.mocked(collectPreviewStyles).mockReturnValue("<style>preview</style>");
+ vi.mocked(buildReactArtifactSrcDoc).mockReturnValue("<html>preview</html>");
+ });
+
+ it("renders an iframe preview after transpilation succeeds", async () => {
+ vi.mocked(transpileReactArtifactSource).mockResolvedValue(
+ "module.exports.default = function Artifact() { return null; };",
+ );
+
+ const { container } = render(
+ <ArtifactReactPreview
+ source="export default function Artifact() { return null; }"
+ title="Artifact.tsx"
+ />,
+ );
+
+ await waitFor(() => {
+ expect(buildReactArtifactSrcDoc).toHaveBeenCalledWith(
+ "module.exports.default = function Artifact() { return null; };",
+ "Artifact.tsx",
+ "<style>preview</style>",
+ );
+ });
+
+ const iframe = container.querySelector("iframe");
+ expect(iframe).toBeTruthy();
+ expect(iframe?.getAttribute("sandbox")).toBe("allow-scripts");
+ expect(iframe?.getAttribute("title")).toBe("Artifact.tsx preview");
+ expect(iframe?.getAttribute("srcdoc")).toBe("<html>preview</html>");
+ });
+
+ it("shows a readable error when transpilation fails", async () => {
+ vi.mocked(transpileReactArtifactSource).mockRejectedValue(
+ new Error("Transpile exploded"),
+ );
+
+ render(
+ <ArtifactReactPreview
+ source="export default function Artifact() {"
+ title="Broken.tsx"
+ />,
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("Failed to render React preview")).toBeTruthy();
+ });
+
+ expect(screen.getByText("Transpile exploded")).toBeTruthy();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/__tests__/ArtifactContent.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/__tests__/ArtifactContent.test.tsx
new file mode 100644
index 0000000000..e4b287fa9a
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/__tests__/ArtifactContent.test.tsx
@@ -0,0 +1,970 @@
+import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
+import {
+ cleanup,
+ fireEvent,
+ render,
+ screen,
+ waitFor,
+} from "@testing-library/react";
+import { ArtifactContent } from "../ArtifactContent";
+import type { ArtifactRef } from "../../../../store";
+import { classifyArtifact, type ArtifactClassification } from "../../helpers";
+import { globalRegistry } from "@/components/contextual/OutputRenderers";
+import { codeRenderer } from "@/components/contextual/OutputRenderers/renderers/CodeRenderer";
+import { ArtifactReactPreview } from "../ArtifactReactPreview";
+
+// Mock the renderers so we don't pull in the full renderer dependency tree
+vi.mock("@/components/contextual/OutputRenderers", () => ({
+ globalRegistry: {
+ getRenderer: vi.fn().mockReturnValue({
+ render: vi.fn((_val: unknown, meta: Record<string, unknown>) => (
+ <div data-testid="global-renderer">
+ rendered:{String(meta?.mimeType ?? "unknown")}
+ </div>
+ )),
+ }),
+ },
+}));
+
+vi.mock(
+ "@/components/contextual/OutputRenderers/renderers/CodeRenderer",
+ () => ({
+ codeRenderer: {
+ render: vi.fn((content: string) => (
+ <div data-testid="code-renderer">{content}</div>
+ )),
+ },
+ }),
+);
+
+vi.mock("../ArtifactReactPreview", () => ({
+ ArtifactReactPreview: vi.fn(
+ ({ source, title }: { source: string; title: string }) => (
+ <div data-testid="react-preview" data-title={title}>
+ {source}
+ </div>
+ ),
+ ),
+}));
+
+function makeArtifact(overrides?: Partial<ArtifactRef>): ArtifactRef {
+ return {
+ id: "file-001",
+ title: "test.txt",
+ mimeType: "text/plain",
+ sourceUrl: "/api/proxy/api/workspace/files/file-001/download",
+ origin: "agent",
+ ...overrides,
+ };
+}
+
+function makeClassification(
+ overrides?: Partial<ArtifactClassification>,
+): ArtifactClassification {
+ return {
+ type: "text",
+ icon: vi.fn(() => null) as unknown as ArtifactClassification["icon"],
+ label: "Text",
+ openable: true,
+ hasSourceToggle: false,
+ ...overrides,
+ };
+}
+
+describe("ArtifactContent", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve("file content here"),
+ blob: () => Promise.resolve(new Blob(["content"])),
+ }),
+ );
+ });
+
+ afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+ });
+
+ // ── Image ─────────────────────────────────────────────────────────
+
+ it("renders image artifact as img tag with loading skeleton", () => {
+ const artifact = makeArtifact({
+ id: "img-001",
+ title: "photo.png",
+ mimeType: "image/png",
+ sourceUrl: "/api/proxy/api/workspace/files/img-001/download",
+ });
+ const classification = makeClassification({ type: "image" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const img = container.querySelector("img");
+ expect(img).toBeTruthy();
+ expect(img?.getAttribute("src")).toBe(
+ "/api/proxy/api/workspace/files/img-001/download",
+ );
+ expect(fetch).not.toHaveBeenCalled();
+ });
+
+ it("image artifact shows loading skeleton before image loads", () => {
+ const artifact = makeArtifact({
+ id: "img-skeleton",
+ title: "photo.png",
+ mimeType: "image/png",
+ sourceUrl: "/api/proxy/api/workspace/files/img-skeleton/download",
+ });
+ const classification = makeClassification({ type: "image" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ // Skeleton uses animate-pulse class
+ const skeleton = container.querySelector('[class*="animate-pulse"]');
+ expect(skeleton).toBeTruthy();
+ });
+
+ it("image artifact shows error state when image fails to load", () => {
+ const artifact = makeArtifact({
+ id: "img-error",
+ title: "broken.png",
+ mimeType: "image/png",
+ sourceUrl: "/api/proxy/api/workspace/files/img-error/download",
+ });
+ const classification = makeClassification({ type: "image" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const img = container.querySelector("img");
+ expect(img).toBeTruthy();
+ fireEvent.error(img!);
+
+ const errorAlert = screen.queryByRole("alert");
+ expect(errorAlert).toBeTruthy();
+ expect(screen.queryByText("Failed to load image")).toBeTruthy();
+ });
+
+ it("image retry resets error and re-shows img", async () => {
+ const artifact = makeArtifact({
+ id: "img-retry",
+ title: "retry.png",
+ mimeType: "image/png",
+ sourceUrl: "/api/proxy/api/workspace/files/img-retry/download",
+ });
+ const classification = makeClassification({ type: "image" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const img = container.querySelector("img");
+ fireEvent.error(img!);
+
+ // Should show error state
+ await waitFor(() => {
+ expect(screen.queryByText("Failed to load image")).toBeTruthy();
+ });
+
+ // Click "Try again"
+ fireEvent.click(screen.getByRole("button", { name: /try again/i }));
+
+ // Error should be cleared, img should reappear
+ await waitFor(() => {
+ expect(screen.queryByText("Failed to load image")).toBeNull();
+ expect(container.querySelector("img")).toBeTruthy();
+ });
+ });
+
+ // ── Video ─────────────────────────────────────────────────────────
+
+ it("renders video artifact with video tag and controls", () => {
+ const artifact = makeArtifact({
+ id: "vid-001",
+ title: "clip.mp4",
+ mimeType: "video/mp4",
+ sourceUrl: "/api/proxy/api/workspace/files/vid-001/download",
+ });
+ const classification = makeClassification({ type: "video" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const video = container.querySelector("video");
+ expect(video).toBeTruthy();
+ expect(video?.hasAttribute("controls")).toBe(true);
+ expect(video?.getAttribute("src")).toBe(
+ "/api/proxy/api/workspace/files/vid-001/download",
+ );
+ expect(fetch).not.toHaveBeenCalled();
+ });
+
+ it("video shows loading skeleton before metadata loads", () => {
+ const artifact = makeArtifact({
+ id: "vid-skel",
+ title: "clip.mp4",
+ mimeType: "video/mp4",
+ sourceUrl: "/api/proxy/api/workspace/files/vid-skel/download",
+ });
+ const classification = makeClassification({ type: "video" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const skeleton = container.querySelector('[class*="animate-pulse"]');
+ expect(skeleton).toBeTruthy();
+
+ // After metadata loads, skeleton should disappear
+ const video = container.querySelector("video");
+ fireEvent.loadedMetadata(video!);
+
+ expect(container.querySelector('[class*="animate-pulse"]')).toBeNull();
+ });
+
+ it("video shows error state when video fails to load", () => {
+ const artifact = makeArtifact({
+ id: "vid-error",
+ title: "broken.mp4",
+ mimeType: "video/mp4",
+ sourceUrl: "/api/proxy/api/workspace/files/vid-error/download",
+ });
+ const classification = makeClassification({ type: "video" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const video = container.querySelector("video");
+ expect(video).toBeTruthy();
+ fireEvent.error(video!);
+
+ const errorAlert = screen.queryByRole("alert");
+ expect(errorAlert).toBeTruthy();
+ expect(screen.queryByText("Failed to load video")).toBeTruthy();
+ });
+
+ it("video retry resets error and re-shows video", async () => {
+ const artifact = makeArtifact({
+ id: "vid-retry",
+ title: "retry.mp4",
+ mimeType: "video/mp4",
+ sourceUrl: "/api/proxy/api/workspace/files/vid-retry/download",
+ });
+ const classification = makeClassification({ type: "video" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const video = container.querySelector("video");
+ fireEvent.error(video!);
+
+ await waitFor(() => {
+ expect(screen.queryByText("Failed to load video")).toBeTruthy();
+ });
+
+ fireEvent.click(screen.getByRole("button", { name: /try again/i }));
+
+ await waitFor(() => {
+ expect(screen.queryByText("Failed to load video")).toBeNull();
+ expect(container.querySelector("video")).toBeTruthy();
+ });
+ });
+
+ // ── PDF ───────────────────────────────────────────────────────────
+
+ it("renders PDF artifact in unsandboxed iframe with blob URL", async () => {
+ const blobUrl = "blob:http://localhost/fake-pdf-blob";
+ vi.stubGlobal(
+ "URL",
+ Object.assign(URL, {
+ createObjectURL: vi.fn().mockReturnValue(blobUrl),
+ revokeObjectURL: vi.fn(),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "pdf-render",
+ title: "report.pdf",
+ mimeType: "application/pdf",
+ sourceUrl: "/api/proxy/api/workspace/files/pdf-render/download",
+ });
+ const classification = makeClassification({ type: "pdf" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ await waitFor(() => {
+ const iframe = container.querySelector("iframe");
+ expect(iframe).toBeTruthy();
+ expect(iframe?.getAttribute("src")).toBe(blobUrl);
+ // No sandbox attribute — Chrome blocks PDF in sandboxed iframes
+ expect(iframe?.hasAttribute("sandbox")).toBe(false);
+ });
+ });
+
+ // ── Fetch error ───────────────────────────────────────────────────
+
+ it("shows error state with retry button on fetch failure", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: false,
+ status: 404,
+ text: () => Promise.resolve("Not found"),
+ }),
+ );
+
+ const artifact = makeArtifact({ id: "error-content-test" });
+ const classification = makeClassification({ type: "html" });
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const errorText = await screen.findByText("Failed to load content");
+ expect(errorText).toBeTruthy();
+
+ const retryButtons = screen.getAllByRole("button", { name: /try again/i });
+ expect(retryButtons.length).toBeGreaterThan(0);
+ });
+
+ // ── HTML ──────────────────────────────────────────────────────────
+
+ it("renders HTML content in sandboxed iframe", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () =>
+ Promise.resolve("<html><body><h1>Hello World</h1></body></html>"),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "html-001",
+ title: "page.html",
+ mimeType: "text/html",
+ });
+ const classification = makeClassification({ type: "html" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ await screen.findByTitle("page.html");
+ const iframe = container.querySelector("iframe");
+ expect(iframe).toBeTruthy();
+ expect(iframe?.getAttribute("sandbox")).toBe("allow-scripts");
+ });
+
+ // ── Source view ───────────────────────────────────────────────────
+
+ it("renders source view as pre tag", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve("source code here"),
+ }),
+ );
+
+ const artifact = makeArtifact({ id: "source-view-test" });
+ const classification = makeClassification({
+ type: "html",
+ hasSourceToggle: true,
+ });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={true}
+ classification={classification}
+ />,
+ );
+
+ await screen.findByText("source code here");
+ const pre = container.querySelector("pre");
+ expect(pre).toBeTruthy();
+ expect(pre?.textContent).toBe("source code here");
+ });
+
+ // ── React ─────────────────────────────────────────────────────────
+
+ it("renders react artifacts via ArtifactReactPreview", async () => {
+ const jsxSource = "export default function App() { return <div>Hi</div>; }";
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve(jsxSource),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "react-001",
+ title: "App.tsx",
+ mimeType: "text/tsx",
+ });
+ const classification = makeClassification({ type: "react" });
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const preview = await screen.findByTestId("react-preview");
+ expect(preview).toBeTruthy();
+ expect(preview.textContent).toContain(jsxSource);
+ expect(preview.getAttribute("data-title")).toBe("App.tsx");
+ });
+
+ it("routes a concrete props-based TSX artifact into ArtifactReactPreview", async () => {
+ const jsxSource = `
+ import React, { FC, useState } from "react";
+
+ interface ArtifactFile {
+ id: string;
+ name: string;
+ mimeType: string;
+ url: string;
+ sizeBytes: number;
+ }
+
+ interface Props {
+ files: ArtifactFile[];
+ onSelect: (file: ArtifactFile) => void;
+ }
+
+ export const previewProps: Props = {
+ files: [
+ {
+ id: "1",
+ name: "report.png",
+ mimeType: "image/png",
+ url: "/report.png",
+ sizeBytes: 2048,
+ },
+ ],
+ onSelect: () => {},
+ };
+
+ const ArtifactList: FC<Props> = ({ files, onSelect }) => {
+ const [selected, setSelected] = useState<string | null>(null);
+
+ const handleClick = (file: ArtifactFile) => {
+ setSelected(file.id);
+ onSelect(file);
+ };
+
+ return (
+ <ul>
+ {files.map((file) => (
+ <li key={file.id} onClick={() => handleClick(file)}>
+ <span>{selected === file.id ? "selected" : file.name}</span>
+ </li>
+ ))}
+ </ul>
+ );
+ };
+
+ export default ArtifactList;
+ `;
+
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve(jsxSource),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "react-props-001",
+ title: "ArtifactList.tsx",
+ mimeType: "text/tsx",
+ });
+ const classification = classifyArtifact(artifact.mimeType, artifact.title);
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const preview = await screen.findByTestId("react-preview");
+ expect(preview.textContent).toContain("previewProps");
+ expect(preview.getAttribute("data-title")).toBe("ArtifactList.tsx");
+ expect(vi.mocked(ArtifactReactPreview).mock.calls[0]?.[0]).toEqual(
+ expect.objectContaining({
+ source: expect.stringContaining("export const previewProps"),
+ title: "ArtifactList.tsx",
+ }),
+ );
+ });
+
+ // ── Code ──────────────────────────────────────────────────────────
+
+ it("renders code artifacts via codeRenderer", async () => {
+ const code = 'def hello():\n print("hi")';
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve(code),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "code-render-001",
+ title: "script.py",
+ mimeType: "text/x-python",
+ });
+ const classification = makeClassification({ type: "code" });
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const rendered = await screen.findByTestId("code-renderer");
+ expect(rendered).toBeTruthy();
+ expect(rendered.textContent).toContain(code);
+ });
+
+ it.each([
+ {
+ filename: "events.jsonl",
+ mimeType: "application/x-ndjson",
+ content: '{"event":"start"}\n{"event":"finish"}',
+ },
+ {
+ filename: ".env.local",
+ mimeType: "text/plain",
+ content: "OPENAI_API_KEY=test\nDEBUG=true",
+ },
+ {
+ filename: "Dockerfile",
+ mimeType: "text/plain",
+ content: "FROM node:20\nRUN pnpm install",
+ },
+ {
+ filename: "schema.graphql",
+ mimeType: "text/plain",
+ content: "type Query { viewer: User }",
+ },
+ ])(
+ "renders concrete code artifact $filename through codeRenderer",
+ async ({ filename, mimeType, content }) => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve(content),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: `code-${filename}`,
+ title: filename,
+ mimeType,
+ });
+ const classification = classifyArtifact(
+ artifact.mimeType,
+ artifact.title,
+ );
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ await screen.findByTestId("code-renderer");
+
+ expect(classification.type).toBe("code");
+ expect(vi.mocked(codeRenderer.render)).toHaveBeenCalledWith(
+ content,
+ expect.objectContaining({
+ filename,
+ mimeType,
+ type: "code",
+ }),
+ );
+ },
+ );
+
+ // ── JSON ──────────────────────────────────────────────────────────
+
+ it("renders valid JSON via globalRegistry", async () => {
+ const jsonContent = JSON.stringify({ key: "value" }, null, 2);
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve(jsonContent),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "json-render-001",
+ title: "data.json",
+ mimeType: "application/json",
+ });
+ const classification = makeClassification({ type: "json" });
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const rendered = await screen.findByTestId("global-renderer");
+ expect(rendered).toBeTruthy();
+ expect(rendered.textContent).toContain("application/json");
+ });
+
+ it("renders invalid JSON as fallback pre tag", async () => {
+ const { globalRegistry } = await import(
+ "@/components/contextual/OutputRenderers"
+ );
+ const originalImpl = vi
+ .mocked(globalRegistry.getRenderer)
+ .getMockImplementation();
+
+ // For invalid JSON, JSON.parse throws, then the registry fallback
+ // also returns null → falls through to <pre>
+ vi.mocked(globalRegistry.getRenderer).mockReturnValue(null);
+
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve("{invalid json!!!"),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "json-invalid-001",
+ title: "bad.json",
+ mimeType: "application/json",
+ });
+ const classification = makeClassification({ type: "json" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ await waitFor(() => {
+ const pre = container.querySelector("pre");
+ expect(pre).toBeTruthy();
+ expect(pre?.textContent).toBe("{invalid json!!!");
+ });
+
+ // Restore
+ if (originalImpl) {
+ vi.mocked(globalRegistry.getRenderer).mockImplementation(originalImpl);
+ }
+ });
+
+ // ── CSV ───────────────────────────────────────────────────────────
+
+ it("renders CSV via globalRegistry with text/csv metadata", async () => {
+ const csvContent = "Name,Age\nAlice,30\nBob,25";
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve(csvContent),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "csv-render-001",
+ title: "data.csv",
+ mimeType: "text/csv",
+ });
+ const classification = makeClassification({
+ type: "csv",
+ hasSourceToggle: true,
+ });
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const rendered = await screen.findByTestId("global-renderer");
+ expect(rendered).toBeTruthy();
+ expect(rendered.textContent).toContain("text/csv");
+ });
+
+ it("renders TSV via globalRegistry with tab-separated metadata", async () => {
+ const tsvContent = "Name\tAge\nAlice\t30\nBob\t25";
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve(tsvContent),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "tsv-render-001",
+ title: "data.tsv",
+ mimeType: "text/tab-separated-values",
+ });
+ const classification = makeClassification({
+ type: "csv",
+ hasSourceToggle: true,
+ });
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const rendered = await screen.findByTestId("global-renderer");
+ expect(rendered).toBeTruthy();
+ expect(rendered.textContent).toContain("text/tab-separated-values");
+ });
+
+ // ── Markdown ──────────────────────────────────────────────────────
+
+ it("renders markdown via globalRegistry", async () => {
+ const mdContent = "# Hello\n\nThis is **markdown**.";
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve(mdContent),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "md-render-001",
+ title: "README.md",
+ mimeType: "text/markdown",
+ });
+ const classification = makeClassification({
+ type: "markdown",
+ hasSourceToggle: true,
+ });
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const rendered = await screen.findByTestId("global-renderer");
+ expect(rendered).toBeTruthy();
+ expect(rendered.textContent).toContain("text/markdown");
+ });
+
+ // ── Text fallback ─────────────────────────────────────────────────
+
+ it("renders text artifacts via globalRegistry fallback", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve("plain text content"),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "text-render-001",
+ title: "notes.txt",
+ mimeType: "text/plain",
+ });
+ const classification = makeClassification({ type: "text" });
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ const rendered = await screen.findByTestId("global-renderer");
+ expect(rendered).toBeTruthy();
+ });
+
+ it.each([
+ {
+ filename: "calendar.ics",
+ mimeType: "text/calendar",
+ content: "BEGIN:VCALENDAR\nVERSION:2.0\nEND:VCALENDAR",
+ },
+ {
+ filename: "contact.vcf",
+ mimeType: "text/vcard",
+ content: "BEGIN:VCARD\nVERSION:4.0\nFN:Alice Example\nEND:VCARD",
+ },
+ ])(
+ "renders concrete text artifact $filename through the global renderer path",
+ async ({ filename, mimeType, content }) => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve(content),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: `text-${filename}`,
+ title: filename,
+ mimeType,
+ });
+ const classification = classifyArtifact(
+ artifact.mimeType,
+ artifact.title,
+ );
+
+ render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ await screen.findByTestId("global-renderer");
+
+ expect(classification.type).toBe("text");
+ expect(vi.mocked(globalRegistry.getRenderer)).toHaveBeenCalledWith(
+ content,
+ expect.objectContaining({
+ filename,
+ mimeType,
+ }),
+ );
+ },
+ );
+
+ it("falls back to pre tag when no renderer matches", async () => {
+ const { globalRegistry } = await import(
+ "@/components/contextual/OutputRenderers"
+ );
+ const originalImpl = vi
+ .mocked(globalRegistry.getRenderer)
+ .getMockImplementation();
+
+ vi.mocked(globalRegistry.getRenderer).mockReturnValue(null);
+
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: true,
+ text: () => Promise.resolve("raw content fallback"),
+ }),
+ );
+
+ const artifact = makeArtifact({
+ id: "fallback-pre-001",
+ title: "unknown.txt",
+ mimeType: "text/plain",
+ });
+ const classification = makeClassification({ type: "text" });
+
+ const { container } = render(
+ <ArtifactContent
+ artifact={artifact}
+ isSourceView={false}
+ classification={classification}
+ />,
+ );
+
+ await waitFor(() => {
+ const pre = container.querySelector("pre");
+ expect(pre).toBeTruthy();
+ expect(pre?.textContent).toBe("raw content fallback");
+ });
+
+ // Restore
+ if (originalImpl) {
+ vi.mocked(globalRegistry.getRenderer).mockImplementation(originalImpl);
+ }
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/__tests__/useArtifactContent.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/__tests__/useArtifactContent.test.ts
index e9f5a11d3e..0d3c866805 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/__tests__/useArtifactContent.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/__tests__/useArtifactContent.test.ts
@@ -3,6 +3,7 @@ import { renderHook, waitFor, act } from "@testing-library/react";
import {
useArtifactContent,
getCachedArtifactContent,
+ clearContentCache,
} from "../useArtifactContent";
import type { ArtifactRef } from "../../../../store";
import type { ArtifactClassification } from "../../helpers";
@@ -33,6 +34,7 @@ function makeClassification(
describe("useArtifactContent", () => {
beforeEach(() => {
+ clearContentCache();
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({
@@ -44,6 +46,7 @@ describe("useArtifactContent", () => {
});
afterEach(() => {
+ clearContentCache();
vi.restoreAllMocks();
});
@@ -109,9 +112,12 @@ describe("useArtifactContent", () => {
useArtifactContent(artifact, classification),
);
- await waitFor(() => {
- expect(result.current.error).toBeTruthy();
- });
+ await waitFor(
+ () => {
+ expect(result.current.error).toBeTruthy();
+ },
+ { timeout: 2500 },
+ );
expect(result.current.error).toContain("404");
expect(result.current.content).toBeNull();
@@ -132,6 +138,176 @@ describe("useArtifactContent", () => {
expect(getCachedArtifactContent("cache-test")).toBe("file content here");
});
+ it("sets error on fetch failure for HTML artifacts (stale artifact)", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: false,
+ status: 404,
+ text: () => Promise.resolve("Not found"),
+ }),
+ );
+
+ const artifact = makeArtifact({ id: "stale-html-artifact" });
+ const classification = makeClassification({ type: "html" });
+
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+
+ await waitFor(
+ () => {
+ expect(result.current.error).toBeTruthy();
+ },
+ { timeout: 2500 },
+ );
+
+ expect(result.current.error).toContain("404");
+ expect(result.current.content).toBeNull();
+ });
+
+ it("sets error on network failure", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockRejectedValue(new Error("Network error")),
+ );
+
+ const artifact = makeArtifact({ id: "network-error-artifact" });
+ const classification = makeClassification({ type: "html" });
+
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+
+ await waitFor(
+ () => {
+ expect(result.current.error).toBeTruthy();
+ },
+ { timeout: 2500 },
+ );
+
+ expect(result.current.error).toContain("Network error");
+ expect(result.current.content).toBeNull();
+ });
+
+ it("retries transient HTML fetch failures before surfacing an error", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ if (callCount < 3) {
+ return Promise.resolve({
+ ok: false,
+ status: 503,
+ headers: {
+ get: () => "application/json",
+ },
+ json: () => Promise.resolve({ detail: "temporary upstream error" }),
+ });
+ }
+
+ return Promise.resolve({
+ ok: true,
+ text: () => Promise.resolve("<html>ok now</html>"),
+ });
+ }),
+ );
+
+ const artifact = makeArtifact({ id: "transient-html-retry" });
+ const classification = makeClassification({ type: "html" });
+
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+
+ await waitFor(
+ () => {
+ expect(result.current.content).toBe("<html>ok now</html>");
+ },
+ { timeout: 2500 },
+ );
+
+ expect(callCount).toBe(3);
+ expect(result.current.error).toBeNull();
+ });
+
+ it("surfaces backend error detail from JSON responses", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: false,
+ status: 404,
+ headers: {
+ get: () => "application/json",
+ },
+ json: () => Promise.resolve({ detail: "File not found" }),
+ }),
+ );
+
+ const artifact = makeArtifact({ id: "json-error-detail" });
+ const classification = makeClassification({ type: "html" });
+
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+
+ await waitFor(
+ () => {
+ expect(result.current.error).toBeTruthy();
+ },
+ { timeout: 2500 },
+ );
+
+ expect(result.current.error).toContain("404");
+ expect(result.current.error).toContain("File not found");
+ });
+
+ it("retry after 404 on HTML artifact clears cache and re-fetches", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ if (callCount === 1) {
+ return Promise.resolve({
+ ok: false,
+ status: 404,
+ text: () => Promise.resolve("Not found"),
+ });
+ }
+ return Promise.resolve({
+ ok: true,
+ text: () => Promise.resolve("<html>recovered</html>"),
+ });
+ }),
+ );
+
+ const artifact = makeArtifact({ id: "retry-html-artifact" });
+ const classification = makeClassification({ type: "html" });
+
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+
+ await waitFor(() => {
+ expect(result.current.error).toBeTruthy();
+ });
+
+ act(() => {
+ result.current.retry();
+ });
+
+ await waitFor(
+ () => {
+ expect(result.current.content).toBe("<html>recovered</html>");
+ },
+ { timeout: 2500 },
+ );
+
+ expect(result.current.error).toBeNull();
+ });
+
it("retry clears cache and re-fetches", async () => {
let callCount = 0;
vi.stubGlobal(
@@ -164,4 +340,162 @@ describe("useArtifactContent", () => {
expect(result.current.content).toBe("response 2");
});
});
+
+ // ── Non-transient errors ──────────────────────────────────────────
+
+ it("rejects immediately on 403 without retrying", async () => {
+ let callCount = 0;
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation(() => {
+ callCount++;
+ return Promise.resolve({
+ ok: false,
+ status: 403,
+ text: () => Promise.resolve("Forbidden"),
+ });
+ }),
+ );
+
+ const artifact = makeArtifact({ id: "forbidden-no-retry" });
+ const classification = makeClassification({ type: "text" });
+
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+
+ await waitFor(
+ () => {
+ expect(result.current.error).toBeTruthy();
+ },
+ { timeout: 2500 },
+ );
+
+ expect(callCount).toBe(1);
+ expect(result.current.error).toContain("403");
+ });
+
+ // ── Video skip-fetch ──────────────────────────────────────────────
+
+ it("skips fetch for video artifacts (like image)", async () => {
+ const artifact = makeArtifact({
+ id: "video-skip",
+ mimeType: "video/mp4",
+ });
+ const classification = makeClassification({ type: "video" });
+
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+
+ expect(result.current.isLoading).toBe(false);
+ expect(result.current.content).toBeNull();
+ expect(result.current.pdfUrl).toBeNull();
+ expect(fetch).not.toHaveBeenCalled();
+ });
+
+ // ── PDF error paths ───────────────────────────────────────────────
+
+ it("sets error on PDF fetch failure (non-2xx)", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue({
+ ok: false,
+ status: 500,
+ text: () => Promise.resolve("Server Error"),
+ }),
+ );
+
+ const artifact = makeArtifact({ id: "pdf-error" });
+ const classification = makeClassification({ type: "pdf" });
+
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+
+ await waitFor(
+ () => {
+ expect(result.current.error).toBeTruthy();
+ },
+ { timeout: 2500 },
+ );
+
+ expect(result.current.error).toContain("500");
+ expect(result.current.pdfUrl).toBeNull();
+ });
+
+ it("sets error on PDF network failure", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockRejectedValue(new Error("PDF network failure")),
+ );
+
+ const artifact = makeArtifact({ id: "pdf-network-error" });
+ const classification = makeClassification({ type: "pdf" });
+
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+
+ await waitFor(
+ () => {
+ expect(result.current.error).toBeTruthy();
+ },
+ { timeout: 2500 },
+ );
+
+ expect(result.current.error).toContain("PDF network failure");
+ expect(result.current.pdfUrl).toBeNull();
+ });
+
+ // ── LRU cache eviction ────────────────────────────────────────────
+
+ it("evicts oldest entry when cache exceeds 12 items", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockImplementation((url: string) => {
+ const fileId = url.match(/files\/([^/]+)\/download/)?.[1] ?? "unknown";
+ return Promise.resolve({
+ ok: true,
+ text: () => Promise.resolve(`content-${fileId}`),
+ });
+ }),
+ );
+
+ const classification = makeClassification({ type: "text" });
+
+ // Fill the cache with 12 entries (cache max = 12)
+ for (let i = 0; i < 12; i++) {
+ const artifact = makeArtifact({
+ id: `lru-${i}`,
+ sourceUrl: `/api/proxy/api/workspace/files/lru-${i}/download`,
+ });
+ const { result } = renderHook(() =>
+ useArtifactContent(artifact, classification),
+ );
+ await waitFor(() => {
+ expect(result.current.isLoading).toBe(false);
+ });
+ }
+
+ // All 12 should be cached
+ expect(getCachedArtifactContent("lru-0")).toBe("content-lru-0");
+ expect(getCachedArtifactContent("lru-11")).toBe("content-lru-11");
+
+ // Adding a 13th should evict lru-0 (the oldest)
+ const artifact13 = makeArtifact({
+ id: "lru-12",
+ sourceUrl: "/api/proxy/api/workspace/files/lru-12/download",
+ });
+ const { result: result13 } = renderHook(() =>
+ useArtifactContent(artifact13, classification),
+ );
+ await waitFor(() => {
+ expect(result13.current.isLoading).toBe(false);
+ });
+
+ expect(getCachedArtifactContent("lru-0")).toBeUndefined();
+ expect(getCachedArtifactContent("lru-1")).toBe("content-lru-1");
+ expect(getCachedArtifactContent("lru-12")).toBe("content-lru-12");
+ });
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/reactArtifactPreview.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/reactArtifactPreview.test.ts
index 6a6bc806cb..934573fc01 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/reactArtifactPreview.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/reactArtifactPreview.test.ts
@@ -85,4 +85,35 @@ describe("buildReactArtifactSrcDoc", () => {
const doc = buildReactArtifactSrcDoc("module.exports = {};", "A", STYLES);
expect(doc).toContain("box-sizing: border-box");
});
+
+ it("supports a named previewProps export in the runtime", () => {
+ const doc = buildReactArtifactSrcDoc("module.exports = {};", "A", STYLES);
+ expect(doc).toContain("moduleExports.previewProps");
+ expect(doc).toContain("React.createElement(Component, previewProps || {})");
+ });
+
+ it("includes a helpful message for components that expect props", () => {
+ const doc = buildReactArtifactSrcDoc("module.exports = {};", "A", STYLES);
+ expect(doc).toContain("This component appears to expect props.");
+ expect(doc).toContain("previewProps");
+ });
+
+ it("checks componentExpectsProps on the raw component before wrapping", () => {
+ const doc = buildReactArtifactSrcDoc("module.exports = {};", "A", STYLES);
+ expect(doc).toContain("RawComponent.length > 0");
+ expect(doc).toContain("wrapWithProviders(RawComponent");
+ });
+
+ it("wrapWithProviders forwards props to the wrapped component", () => {
+ const doc = buildReactArtifactSrcDoc("module.exports = {};", "A", STYLES);
+ expect(doc).toContain("function WrappedArtifactPreview(props)");
+ expect(doc).toContain("React.createElement(Component, props)");
+ });
+
+ it("supports named exported components and provider wrappers in the runtime", () => {
+ const doc = buildReactArtifactSrcDoc("module.exports = {};", "A", STYLES);
+ expect(doc).toContain('name.endsWith("Provider")');
+ expect(doc).toContain("/^[A-Z]/.test(name)");
+ expect(doc).toContain("wrapWithProviders");
+ });
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/reactArtifactPreview.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/reactArtifactPreview.ts
index 77c031b211..f98fe9f684 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/reactArtifactPreview.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/reactArtifactPreview.ts
@@ -169,8 +169,8 @@ export function buildReactArtifactSrcDoc(
return Component;
}
- return function WrappedArtifactPreview() {
- let tree = React.createElement(Component);
+ return function WrappedArtifactPreview(props) {
+ let tree = React.createElement(Component, props);
for (let i = providers.length - 1; i >= 0; i -= 1) {
tree = React.createElement(providers[i], null, tree);
@@ -180,6 +180,17 @@ export function buildReactArtifactSrcDoc(
};
}
+ function getPreviewProps(moduleExports) {
+ if (
+ moduleExports.previewProps &&
+ typeof moduleExports.previewProps === "object"
+ ) {
+ return moduleExports.previewProps;
+ }
+
+ return null;
+ }
+
function require(name) {
if (name === "react") {
return React;
@@ -235,6 +246,11 @@ export function buildReactArtifactSrcDoc(
render() {
if (this.state.error) {
+ const propsHelp =
+ this.props.componentExpectsProps && !this.props.hasPreviewProps
+ ? "\\n\\nThis component appears to expect props. Export a named previewProps object with sample values to render it in artifact preview."
+ : "";
+
return React.createElement(
"div",
{
@@ -249,7 +265,9 @@ export function buildReactArtifactSrcDoc(
whiteSpace: "pre-wrap",
},
},
- this.state.error.stack || this.state.error.message || String(this.state.error),
+ (this.state.error.stack ||
+ this.state.error.message ||
+ String(this.state.error)) + propsHelp,
);
}
@@ -296,16 +314,19 @@ export function buildReactArtifactSrcDoc(
moduleExports.App = executionResult.app;
}
- const Component = wrapWithProviders(
- getRenderableCandidate(moduleExports),
- moduleExports,
- );
+ const RawComponent = getRenderableCandidate(moduleExports);
+ const componentExpectsProps = RawComponent.length > 0;
+ const Component = wrapWithProviders(RawComponent, moduleExports);
+ const previewProps = getPreviewProps(moduleExports);
ReactDOM.createRoot(rootElement).render(
React.createElement(
PreviewErrorBoundary,
- null,
- React.createElement(Component),
+ {
+ componentExpectsProps: componentExpectsProps,
+ hasPreviewProps: previewProps != null,
+ },
+ React.createElement(Component, previewProps || {}),
),
);
} catch (error) {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/transpileReactArtifact.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/transpileReactArtifact.test.ts
index 5a43b99749..3da1ee84fa 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/transpileReactArtifact.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/transpileReactArtifact.test.ts
@@ -48,4 +48,104 @@ describe("transpileReactArtifactSource", () => {
expect(out).not.toContain(": string");
expect(out).toContain("function greet(name)");
});
+
+ it("transpiles a concrete props-based artifact with previewProps", async () => {
+ const src = `
+ import React, { FC, useState } from "react";
+
+ interface ArtifactFile {
+ id: string;
+ name: string;
+ mimeType: string;
+ url: string;
+ sizeBytes: number;
+ }
+
+ interface Props {
+ files: ArtifactFile[];
+ onSelect: (file: ArtifactFile) => void;
+ }
+
+ export const previewProps: Props = {
+ files: [
+ {
+ id: "1",
+ name: "report.png",
+ mimeType: "image/png",
+ url: "/report.png",
+ sizeBytes: 2048,
+ },
+ ],
+ onSelect: () => {},
+ };
+
+ const ArtifactList: FC<Props> = ({ files, onSelect }) => {
+ const [selected, setSelected] = useState<string | null>(null);
+
+ const handleClick = (file: ArtifactFile) => {
+ setSelected(file.id);
+ onSelect(file);
+ };
+
+ return (
+ <ul>
+ {files.map((file) => (
+ <li key={file.id} onClick={() => handleClick(file)}>
+ <span>{selected === file.id ? "selected" : file.name}</span>
+ </li>
+ ))}
+ </ul>
+ );
+ };
+
+ export default ArtifactList;
+ `;
+
+ const out = await transpileReactArtifactSource(src, "ArtifactList.tsx");
+
+ expect(out).toContain("exports.previewProps");
+ expect(out).toContain("exports.default = ArtifactList");
+ expect(out).toContain("useState");
+ expect(out).not.toContain("interface Props");
+ expect(out).not.toContain("interface ArtifactFile");
+ });
+
+ it("transpiles a named export artifact without a default export", async () => {
+ const src = `
+ export function ResultsGrid() {
+ return (
+ <section>
+ <h1>Results</h1>
+ <p>Named export preview</p>
+ </section>
+ );
+ }
+ `;
+
+ const out = await transpileReactArtifactSource(src, "ResultsGrid.tsx");
+
+ expect(out).toContain("exports.ResultsGrid = ResultsGrid");
+ expect(out).toMatch(/\.createElement\(/);
+ expect(out).not.toContain("<section>");
+ });
+
+ it("transpiles a provider-wrapped artifact with separate provider and component exports", async () => {
+ const src = `
+ import React from "react";
+
+ export function DemoProvider({ children }: { children: React.ReactNode }) {
+ return <div data-theme="demo">{children}</div>;
+ }
+
+ export function DashboardCard() {
+ return <main>Provider-backed preview</main>;
+ }
+ `;
+
+ const out = await transpileReactArtifactSource(src, "DashboardCard.tsx");
+
+ expect(out).toContain("exports.DemoProvider = DemoProvider");
+ expect(out).toContain("exports.DashboardCard = DashboardCard");
+ expect(out).not.toContain("React.ReactNode");
+ });
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/useArtifactContent.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/useArtifactContent.ts
index a800cdcd8f..1479da7a37 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/useArtifactContent.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/components/useArtifactContent.ts
@@ -7,12 +7,116 @@ import type { ArtifactClassification } from "../helpers";
// Cap on cached text artifacts. Long sessions with many large artifacts
// would otherwise hold every opened one in memory.
const CONTENT_CACHE_MAX = 12;
+const CONTENT_FETCH_MAX_RETRIES = 2;
+const CONTENT_FETCH_RETRY_DELAY_MS = 500;
// Module-level LRU keyed by artifact id so a sibling action (e.g. Copy
// in ArtifactPanelHeader) can read what the panel already fetched without
// re-hitting the network.
const contentCache = new Map<string, string>();
+class ArtifactFetchError extends Error {}
+
+function isTransientArtifactFetchStatus(status: number): boolean {
+ return status === 408 || status === 429 || status >= 500;
+}
+
+function sleep(ms: number): Promise<void> {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
+
+function getArtifactErrorMessage(body: unknown): string | null {
+ if (typeof body === "string") {
+ const trimmed = body.replace(/\s+/g, " ").trim();
+ return trimmed || null;
+ }
+
+ if (!body || typeof body !== "object") return null;
+
+ if (
+ "detail" in body &&
+ typeof body.detail === "string" &&
+ body.detail.trim().length > 0
+ ) {
+ return body.detail.trim();
+ }
+
+ if (
+ "error" in body &&
+ typeof body.error === "string" &&
+ body.error.trim().length > 0
+ ) {
+ return body.error.trim();
+ }
+
+ if (
+ "detail" in body &&
+ body.detail &&
+ typeof body.detail === "object" &&
+ "message" in body.detail &&
+ typeof body.detail.message === "string" &&
+ body.detail.message.trim().length > 0
+ ) {
+ return body.detail.message.trim();
+ }
+
+ return null;
+}
+
+async function parseArtifactFetchError(response: Response): Promise<string> {
+ const prefix = `Failed to fetch: ${response.status}`;
+ const contentType =
+ response.headers?.get?.("content-type")?.toLowerCase() ?? "";
+
+ try {
+ if (
+ contentType.includes("application/json") &&
+ typeof response.json === "function"
+ ) {
+ const body = await response.json();
+ const detail = getArtifactErrorMessage(body);
+ return detail ? `${prefix} ${detail}` : prefix;
+ }
+
+ if (typeof response.text === "function") {
+ const text = await response.text();
+ const detail = getArtifactErrorMessage(text);
+ return detail ? `${prefix} ${detail}` : prefix;
+ }
+ } catch {
+ return prefix;
+ }
+
+ return prefix;
+}
+
+async function fetchArtifactResponse(url: string): Promise<Response> {
+ for (let attempt = 0; attempt <= CONTENT_FETCH_MAX_RETRIES; attempt++) {
+ try {
+ const response = await fetch(url);
+ if (response.ok) return response;
+
+ if (
+ !isTransientArtifactFetchStatus(response.status) ||
+ attempt === CONTENT_FETCH_MAX_RETRIES
+ ) {
+ throw new ArtifactFetchError(await parseArtifactFetchError(response));
+ }
+ } catch (error) {
+ if (error instanceof ArtifactFetchError) throw error;
+ if (attempt === CONTENT_FETCH_MAX_RETRIES) {
+ throw error instanceof Error
+ ? error
+ : new Error("Failed to fetch artifact");
+ }
+ }
+
+ await sleep(CONTENT_FETCH_RETRY_DELAY_MS);
+ }
+
+ throw new Error("Failed to fetch artifact");
+}
+
export function getCachedArtifactContent(id: string): string | undefined {
return contentCache.get(id);
}
@@ -64,7 +168,7 @@ export function useArtifactContent(
}, [artifact.id, isLoading]);
useEffect(() => {
- if (classification.type === "image") {
+ if (classification.type === "image" || classification.type === "video") {
setContent(null);
setPdfUrl(null);
setError(null);
@@ -80,11 +184,8 @@ export function useArtifactContent(
let objectUrl: string | null = null;
setContent(null);
setPdfUrl(null);
- fetch(artifact.sourceUrl)
- .then((res) => {
- if (!res.ok) throw new Error(`Failed to fetch: ${res.status}`);
- return res.blob();
- })
+ fetchArtifactResponse(artifact.sourceUrl)
+ .then((res) => res.blob())
.then((blob) => {
objectUrl = URL.createObjectURL(blob);
if (cancelled) {
@@ -121,11 +222,8 @@ export function useArtifactContent(
cancelled = true;
};
}
- fetch(artifact.sourceUrl)
- .then((res) => {
- if (!res.ok) throw new Error(`Failed to fetch: ${res.status}`);
- return res.text();
- })
+ fetchArtifactResponse(artifact.sourceUrl)
+ .then((res) => res.text())
.then((text) => {
if (!cancelled) {
if (cache.size >= CONTENT_CACHE_MAX) {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/downloadArtifact.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/downloadArtifact.ts
index d7d902839a..6ff3e264de 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/downloadArtifact.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/downloadArtifact.ts
@@ -1,5 +1,31 @@
import type { ArtifactRef } from "../../store";
+const MAX_RETRIES = 2;
+const RETRY_DELAY_MS = 500;
+
+function isTransientError(status: number): boolean {
+ return status >= 500 || status === 408 || status === 429;
+}
+
+class DownloadError extends Error {}
+
+async function fetchWithRetry(url: string, retries: number): Promise<Response> {
+ for (let attempt = 0; attempt <= retries; attempt++) {
+ try {
+ const res = await fetch(url);
+ if (res.ok) return res;
+ if (!isTransientError(res.status) || attempt === retries) {
+ throw new DownloadError(`Download failed: ${res.status}`);
+ }
+ } catch (error) {
+ if (error instanceof DownloadError) throw error;
+ if (attempt === retries) throw error;
+ }
+ await new Promise((r) => setTimeout(r, RETRY_DELAY_MS));
+ }
+ throw new Error("Unreachable");
+}
+
/**
* Trigger a file download from an artifact URL.
*
@@ -7,26 +33,28 @@ import type { ArtifactRef } from "../../store";
* ignores the `download` attribute on cross-origin responses (GCS signed
* URLs), and some browsers require the anchor to be attached to the DOM
* before `.click()` fires the download.
+ *
+ * Retries up to {@link MAX_RETRIES} times on transient server errors (5xx,
+ * 408, 429) to handle intermittent proxy/GCS failures.
*/
export function downloadArtifact(artifact: ArtifactRef): Promise<void> {
// Replace path separators, Windows-reserved chars, control chars, and
// parent-dir sequences so the browser-assigned filename is safe to write
// anywhere on the user's filesystem.
- const safeName =
- artifact.title
- .replace(/\.\./g, "_")
- .replace(/[\\/:*?"<>|\x00-\x1f]/g, "_")
- .replace(/^\.+/, "") || "download";
- return fetch(artifact.sourceUrl)
- .then((res) => {
- if (!res.ok) throw new Error(`Download failed: ${res.status}`);
- return res.blob();
- })
+ const collapsedDots = artifact.title.replace(/\.\./g, "");
+ const hasVisibleName = collapsedDots.replace(/^\.+/, "").length > 0;
+ const safeName = artifact.title
+ .replace(/\.\./g, "_")
+ .replace(/[\\/:*?"<>|\x00-\x1f]/g, "_")
+ .replace(/^\.+/, "");
+
+ return fetchWithRetry(artifact.sourceUrl, MAX_RETRIES)
+ .then((res) => res.blob())
.then((blob) => {
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
- a.download = safeName;
+ a.download = safeName && hasVisibleName ? safeName : "download";
document.body.appendChild(a);
a.click();
a.remove();
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/helpers.test.ts
index f45f0695b8..18738768ea 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/helpers.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/helpers.test.ts
@@ -56,7 +56,7 @@ describe("classifyArtifact", () => {
expect(classifyArtifact("application/octet-stream", "x").openable).toBe(
false,
);
- expect(classifyArtifact("video/mp4", "clip.mp4").openable).toBe(false);
+ expect(classifyArtifact("audio/mpeg", "track.mp3").openable).toBe(false);
});
it("defaults unknown extension+MIME to download-only (not text)", () => {
@@ -76,4 +76,398 @@ describe("classifyArtifact", () => {
const c = classifyArtifact("text/plain", "data.csv");
expect(c.type).toBe("csv");
});
+
+ it("classifies video/mp4 as video (previewable)", () => {
+ const c = classifyArtifact("video/mp4", "clip.mp4");
+ expect(c.type).toBe("video");
+ expect(c.openable).toBe(true);
+ });
+
+ it("classifies video/webm as video (previewable)", () => {
+ const c = classifyArtifact("video/webm", "clip.webm");
+ expect(c.type).toBe("video");
+ expect(c.openable).toBe(true);
+ });
+
+ // ── Extension coverage ────────────────────────────────────────────
+
+ it("routes .htm as html (not just .html)", () => {
+ const c = classifyArtifact(null, "page.htm");
+ expect(c.type).toBe("html");
+ expect(c.hasSourceToggle).toBe(true);
+ });
+
+ it("routes .json as json with source toggle", () => {
+ const c = classifyArtifact(null, "config.json");
+ expect(c.type).toBe("json");
+ expect(c.hasSourceToggle).toBe(true);
+ });
+
+ it("routes .txt as text", () => {
+ expect(classifyArtifact(null, "notes.txt").type).toBe("text");
+ });
+
+ it("routes .log as text", () => {
+ expect(classifyArtifact(null, "server.log").type).toBe("text");
+ });
+
+ it("routes .mdx as markdown", () => {
+ expect(classifyArtifact(null, "docs.mdx").type).toBe("markdown");
+ });
+
+ it("routes browser-safe video extensions to video", () => {
+ for (const ext of [".mp4", ".webm", ".m4v"]) {
+ const c = classifyArtifact(null, `clip${ext}`);
+ expect(c.type).toBe("video");
+ expect(c.openable).toBe(true);
+ }
+ });
+
+ it("keeps legacy or unsupported video extensions download-only", () => {
+ for (const ext of [".ogg", ".mov", ".avi", ".mkv", ".flv", ".mpeg"]) {
+ const c = classifyArtifact(null, `clip${ext}`);
+ expect(c.type).toBe("download-only");
+ expect(c.openable).toBe(false);
+ }
+ });
+
+ it("routes all code extensions to code", () => {
+ const codeExts = [
+ "main.js",
+ "app.ts",
+ "theme.scss",
+ "legacy.less",
+ "schema.graphql",
+ "query.gql",
+ "api.proto",
+ "main.dart",
+ "lib.rb",
+ "server.rs",
+ "App.java",
+ "main.c",
+ "util.cpp",
+ "header.h",
+ "Program.cs",
+ "index.php",
+ "main.swift",
+ "App.kt",
+ "run.sh",
+ "start.bash",
+ "prompt.zsh",
+ "config.toml",
+ "settings.ini",
+ "app.cfg",
+ "query.sql",
+ "analysis.r",
+ "game.lua",
+ "script.pl",
+ "Calc.scala",
+ ];
+ for (const file of codeExts) {
+ expect(classifyArtifact(null, file).type).toBe("code");
+ }
+ });
+
+ it("routes config filenames and extensions to code", () => {
+ const configFiles = [
+ ".env",
+ ".env.local",
+ "app.properties",
+ "service.conf",
+ ".gitignore",
+ "Dockerfile",
+ "Makefile",
+ ];
+
+ for (const file of configFiles) {
+ expect(classifyArtifact(null, file).type).toBe("code");
+ }
+ });
+
+ it("routes .jsonl as code for now", () => {
+ const c = classifyArtifact(null, "events.jsonl");
+ expect(c.type).toBe("code");
+ });
+
+ it("routes .tsv as csv/spreadsheet", () => {
+ const c = classifyArtifact(null, "table.tsv");
+ expect(c.type).toBe("csv");
+ expect(c.hasSourceToggle).toBe(true);
+ });
+
+ it("routes .ics and .vcf as text", () => {
+ expect(classifyArtifact(null, "calendar.ics").type).toBe("text");
+ expect(classifyArtifact(null, "contact.vcf").type).toBe("text");
+ });
+
+ it("routes all image extensions to image", () => {
+ for (const ext of [".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".ico"]) {
+ expect(classifyArtifact(null, `file${ext}`).type).toBe("image");
+ }
+ });
+
+ // ── MIME fallback coverage ────────────────────────────────────────
+
+ it("routes application/json MIME to json", () => {
+ const c = classifyArtifact("application/json", "noext");
+ expect(c.type).toBe("json");
+ });
+
+ it("routes text/x-* MIME prefix to code", () => {
+ expect(classifyArtifact("text/x-python", "noext").type).toBe("code");
+ expect(classifyArtifact("text/x-c", "noext").type).toBe("code");
+ expect(classifyArtifact("text/x-java-source", "noext").type).toBe("code");
+ });
+
+ it("routes react MIME types to react", () => {
+ expect(classifyArtifact("text/jsx", "noext").type).toBe("react");
+ expect(classifyArtifact("text/tsx", "noext").type).toBe("react");
+ expect(classifyArtifact("application/jsx", "noext").type).toBe("react");
+ expect(classifyArtifact("application/x-typescript-jsx", "noext").type).toBe(
+ "react",
+ );
+ });
+
+ it("routes JavaScript/TypeScript MIME to code", () => {
+ expect(classifyArtifact("application/javascript", "noext").type).toBe(
+ "code",
+ );
+ expect(classifyArtifact("text/javascript", "noext").type).toBe("code");
+ expect(classifyArtifact("application/typescript", "noext").type).toBe(
+ "code",
+ );
+ expect(classifyArtifact("text/typescript", "noext").type).toBe("code");
+ });
+
+ it("routes XML MIME to code", () => {
+ expect(classifyArtifact("application/xml", "noext").type).toBe("code");
+ expect(classifyArtifact("text/xml", "noext").type).toBe("code");
+ });
+
+ it("routes text/x-markdown MIME to markdown", () => {
+ expect(classifyArtifact("text/x-markdown", "noext").type).toBe("markdown");
+ });
+
+ it("routes text/csv MIME to csv", () => {
+ expect(classifyArtifact("text/csv", "noext").type).toBe("csv");
+ });
+
+ it("routes TSV MIME to csv", () => {
+ expect(classifyArtifact("text/tab-separated-values", "noext").type).toBe(
+ "csv",
+ );
+ });
+
+ it("routes unknown text/* MIME to text (not download-only)", () => {
+ expect(classifyArtifact("text/rtf", "noext").type).toBe("text");
+ });
+
+ it("routes browser-safe image MIME types to image", () => {
+ expect(classifyArtifact("image/avif", "noext").type).toBe("image");
+ });
+
+ it("keeps unsupported image MIME types download-only", () => {
+ for (const mime of [
+ "image/tiff",
+ "image/x-portable-pixmap",
+ "image/x-portable-graymap",
+ ]) {
+ const c = classifyArtifact(mime, "noext");
+ expect(c.type).toBe("download-only");
+ expect(c.openable).toBe(false);
+ }
+ });
+
+ it("routes browser-safe video MIME types to video", () => {
+ expect(classifyArtifact("video/mp4", "noext").type).toBe("video");
+ expect(classifyArtifact("video/webm", "noext").type).toBe("video");
+ });
+
+ it("keeps legacy or unsupported video MIME types download-only", () => {
+ for (const mime of [
+ "video/x-msvideo",
+ "video/x-flv",
+ "video/mpeg",
+ "video/quicktime",
+ "video/x-matroska",
+ "video/ogg",
+ ]) {
+ const c = classifyArtifact(mime, "noext");
+ expect(c.type).toBe("download-only");
+ expect(c.openable).toBe(false);
+ }
+ });
+
+ // ── BINARY_MIMES coverage ────────────────────────────────────────
+
+ it("treats all BINARY_MIMES entries as download-only", () => {
+ const binaryMimes = [
+ "application/zip",
+ "application/x-zip-compressed",
+ "application/gzip",
+ "application/x-tar",
+ "application/x-rar-compressed",
+ "application/x-7z-compressed",
+ "application/octet-stream",
+ "application/x-executable",
+ "application/x-msdos-program",
+ "application/vnd.microsoft.portable-executable",
+ ];
+ for (const mime of binaryMimes) {
+ const c = classifyArtifact(mime, "noext");
+ expect(c.openable).toBe(false);
+ expect(c.type).toBe("download-only");
+ }
+ });
+
+ it("treats audio/* MIME as download-only", () => {
+ expect(classifyArtifact("audio/mpeg", "noext").openable).toBe(false);
+ expect(classifyArtifact("audio/wav", "noext").openable).toBe(false);
+ expect(classifyArtifact("audio/ogg", "noext").openable).toBe(false);
+ });
+
+ // ── Size gate edge cases ──────────────────────────────────────────
+
+ it("does NOT gate files at exactly 10MB (boundary is >10MB)", () => {
+ const tenMB = 10 * 1024 * 1024;
+ const c = classifyArtifact("text/plain", "exact.txt", tenMB);
+ expect(c.type).toBe("text");
+ expect(c.openable).toBe(true);
+ });
+
+ it("gates files at 10MB + 1 byte", () => {
+ const overTenMB = 10 * 1024 * 1024 + 1;
+ const c = classifyArtifact("text/plain", "big.txt", overTenMB);
+ expect(c.type).toBe("download-only");
+ expect(c.openable).toBe(false);
+ });
+
+ it("does not gate when sizeBytes is 0", () => {
+ const c = classifyArtifact("text/plain", "empty.txt", 0);
+ expect(c.type).toBe("text");
+ expect(c.openable).toBe(true);
+ });
+
+ it("does not gate when sizeBytes is undefined", () => {
+ const c = classifyArtifact("text/plain", "file.txt", undefined);
+ expect(c.type).toBe("text");
+ expect(c.openable).toBe(true);
+ });
+
+ // ── Extension over MIME priority ──────────────────────────────────
+
+ it("extension wins over MIME for JSON (MIME says text, ext says json)", () => {
+ const c = classifyArtifact("text/plain", "data.json");
+ expect(c.type).toBe("json");
+ });
+
+ it("extension wins over MIME for markdown", () => {
+ const c = classifyArtifact("text/plain", "README.md");
+ expect(c.type).toBe("markdown");
+ });
+
+ // ── Null/missing inputs ───────────────────────────────────────────
+
+ it("handles null MIME with no filename as download-only", () => {
+ const c = classifyArtifact(null, undefined);
+ expect(c.type).toBe("download-only");
+ });
+
+ it("handles null MIME with empty filename as download-only", () => {
+ const c = classifyArtifact(null, "");
+ expect(c.type).toBe("download-only");
+ });
+
+ it("handles known config files with no extension", () => {
+ const c = classifyArtifact(null, "Makefile");
+ expect(c.type).toBe("code");
+ });
+
+ // ── Exotic/compound extensions must NOT open the side panel ───────
+ // These are real file types agents might produce. Every single one
+ // must be download-only so we never try to render binary garbage.
+
+ it("does not open .tar.gz (compound extension takes last segment)", () => {
+ // getExtension("archive.tar.gz") → ".gz" which is not in EXT_KIND
+ const c = classifyArtifact(null, "archive.tar.gz");
+ expect(c.openable).toBe(false);
+ expect(c.type).toBe("download-only");
+ });
+
+ it("does not open .tar.bz2", () => {
+ const c = classifyArtifact(null, "archive.tar.bz2");
+ expect(c.openable).toBe(false);
+ });
+
+ it("does not open .tar.xz", () => {
+ const c = classifyArtifact(null, "archive.tar.xz");
+ expect(c.openable).toBe(false);
+ });
+
+ it("does not open common binary formats", () => {
+ const binaries = [
+ "setup.exe",
+ "library.dll",
+ "image.iso",
+ "installer.dmg",
+ "package.deb",
+ "package.rpm",
+ "module.wasm",
+ "Main.class",
+ "module.pyc",
+ "app.apk",
+ "game.pak",
+ "model.onnx",
+ "weights.pt",
+ "data.parquet",
+ "archive.rar",
+ "archive.7z",
+ "disk.vhd",
+ "disk.vmdk",
+ "firmware.bin",
+ "core.dump",
+ "database.sqlite",
+ "database.db",
+ "index.idx",
+ ];
+ for (const file of binaries) {
+ const c = classifyArtifact(null, file);
+ expect(c.openable).toBe(false);
+ }
+ });
+
+ it("does not open binary MIME types even with a misleading extension", () => {
+ // Extension is unknown, MIME is binary
+ const c = classifyArtifact("application/x-executable", "run.elf");
+ expect(c.openable).toBe(false);
+ });
+
+ it("does not open files with random/made-up extensions", () => {
+ const weirdExts = [
+ "output.xyz",
+ "data.foo",
+ "file.asdf",
+ "thing.blargh",
+ "result.out",
+ "x.1234",
+ ];
+ for (const file of weirdExts) {
+ const c = classifyArtifact(null, file);
+ expect(c.openable).toBe(false);
+ expect(c.type).toBe("download-only");
+ }
+ });
+
+ it("does not open font files", () => {
+ for (const file of ["sans.ttf", "serif.otf", "icon.woff", "icon.woff2"]) {
+ expect(classifyArtifact(null, file).openable).toBe(false);
+ }
+ });
+
+ it("does not open certificate/key files", () => {
+ // .pem and .key have no extension mapping and null MIME → download-only
+ for (const file of ["cert.pem", "server.key", "ca.crt", "id.p12"]) {
+ expect(classifyArtifact(null, file).openable).toBe(false);
+ }
+ });
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/helpers.ts
index dc9d6cddc6..89a9e023c3 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/helpers.ts
@@ -5,6 +5,7 @@ import {
FileText,
Image,
Table,
+ VideoCamera,
} from "@phosphor-icons/react";
import type { Icon } from "@phosphor-icons/react";
@@ -17,6 +18,7 @@ export interface ArtifactClassification {
| "csv"
| "json"
| "image"
+ | "video"
| "pdf"
| "text"
| "download-only";
@@ -38,6 +40,13 @@ const KIND: Record<string, ArtifactClassification> = {
openable: true,
hasSourceToggle: false,
},
+ video: {
+ type: "video",
+ icon: VideoCamera,
+ label: "Video",
+ openable: true,
+ hasSourceToggle: false,
+ },
pdf: {
type: "pdf",
icon: FileText,
@@ -113,8 +122,13 @@ const EXT_KIND: Record<string, string> = {
".svg": "image",
".bmp": "image",
".ico": "image",
+ ".avif": "image",
+ ".mp4": "video",
+ ".webm": "video",
+ ".m4v": "video",
".pdf": "pdf",
".csv": "csv",
+ ".tsv": "csv",
".html": "html",
".htm": "html",
".jsx": "react",
@@ -122,11 +136,17 @@ const EXT_KIND: Record<string, string> = {
".md": "markdown",
".mdx": "markdown",
".json": "json",
+ ".jsonl": "code",
".txt": "text",
".log": "text",
+ ".ics": "text",
+ ".vcf": "text",
+ ".env": "code",
+ ".gitignore": "code",
// code extensions
".js": "code",
".ts": "code",
+ ".dart": "code",
".py": "code",
".rb": "code",
".go": "code",
@@ -142,11 +162,19 @@ const EXT_KIND: Record<string, string> = {
".sh": "code",
".bash": "code",
".zsh": "code",
+ ".scss": "code",
+ ".sass": "code",
+ ".less": "code",
+ ".graphql": "code",
+ ".gql": "code",
+ ".proto": "code",
".yml": "code",
".yaml": "code",
".toml": "code",
".ini": "code",
".cfg": "code",
+ ".conf": "code",
+ ".properties": "code",
".sql": "code",
".r": "code",
".lua": "code",
@@ -154,10 +182,16 @@ const EXT_KIND: Record<string, string> = {
".scala": "code",
};
+const EXACT_FILENAME_KIND: Record<string, string> = {
+ dockerfile: "code",
+ makefile: "code",
+};
+
// Exact-match MIME → kind (fallback when extension doesn't match).
const MIME_KIND: Record<string, string> = {
"application/pdf": "pdf",
"text/csv": "csv",
+ "text/tab-separated-values": "csv",
"text/html": "html",
"text/jsx": "react",
"text/tsx": "react",
@@ -166,6 +200,9 @@ const MIME_KIND: Record<string, string> = {
"text/markdown": "markdown",
"text/x-markdown": "markdown",
"application/json": "json",
+ "application/x-ndjson": "code",
+ "application/ndjson": "code",
+ "application/jsonl": "code",
"application/javascript": "code",
"text/javascript": "code",
"application/typescript": "code",
@@ -182,11 +219,37 @@ const BINARY_MIMES = new Set([
"application/x-rar-compressed",
"application/x-7z-compressed",
"application/octet-stream",
+ "application/wasm",
"application/x-executable",
"application/x-msdos-program",
"application/vnd.microsoft.portable-executable",
]);
+const PREVIEWABLE_IMAGE_MIMES = new Set([
+ "image/png",
+ "image/jpeg",
+ "image/gif",
+ "image/webp",
+ "image/svg+xml",
+ "image/bmp",
+ "image/x-icon",
+ "image/vnd.microsoft.icon",
+ "image/avif",
+]);
+
+const PREVIEWABLE_VIDEO_MIMES = new Set([
+ "video/mp4",
+ "video/webm",
+ "video/x-m4v",
+]);
+
+function getBasename(filename?: string): string {
+ if (!filename) return "";
+ const normalized = filename.replace(/\\/g, "/");
+ const parts = normalized.split("/");
+ return parts[parts.length - 1]?.toLowerCase() ?? "";
+}
+
function getExtension(filename?: string): string {
if (!filename) return "";
const lastDot = filename.lastIndexOf(".");
@@ -202,24 +265,36 @@ export function classifyArtifact(
// Size gate: >10MB is download-only regardless of type.
if (sizeBytes && sizeBytes > TEN_MB) return KIND["download-only"];
+ const basename = getBasename(filename);
+ const exactKind = EXACT_FILENAME_KIND[basename];
+ if (exactKind) return KIND[exactKind];
+
+ if (basename === ".env" || basename.startsWith(".env.")) {
+ return KIND.code;
+ }
+
// Extension first (more reliable than MIME for AI-generated files).
- const ext = getExtension(filename);
+ const ext = getExtension(basename);
const extKind = EXT_KIND[ext];
if (extKind) return KIND[extKind];
// MIME fallbacks.
const mime = (mimeType ?? "").toLowerCase();
- if (mime.startsWith("image/")) return KIND.image;
+ if (PREVIEWABLE_IMAGE_MIMES.has(mime)) return KIND.image;
+ if (PREVIEWABLE_VIDEO_MIMES.has(mime)) return KIND.video;
const mimeKind = MIME_KIND[mime];
if (mimeKind) return KIND[mimeKind];
if (mime.startsWith("text/x-")) return KIND.code;
if (
- BINARY_MIMES.has(mime) ||
- mime.startsWith("audio/") ||
- mime.startsWith("video/")
+ mime.startsWith("image/") ||
+ mime.startsWith("video/") ||
+ mime.startsWith("font/")
) {
return KIND["download-only"];
}
+ if (BINARY_MIMES.has(mime) || mime.startsWith("audio/")) {
+ return KIND["download-only"];
+ }
if (mime.startsWith("text/")) return KIND.text;
// Unknown extension + unknown MIME: don't open — we can't safely assume
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/useArtifactPanel.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/useArtifactPanel.ts
index 3a512aa709..20443154cd 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/useArtifactPanel.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ArtifactPanel/useArtifactPanel.ts
@@ -83,6 +83,7 @@ export function useArtifactPanel() {
const canCopy =
classification != null &&
classification.type !== "image" &&
+ classification.type !== "video" &&
classification.type !== "download-only" &&
classification.type !== "pdf";
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 f116b053b4..7f3c1d0328 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
@@ -64,10 +64,7 @@ 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({
- messages: isArtifactsEnabled ? messages : [],
- sessionId,
- });
+ useAutoOpenArtifacts({ sessionId });
const isBusy =
status === "streaming" ||
status === "submitted" ||
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/useAutoOpenArtifacts.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/useAutoOpenArtifacts.test.ts
new file mode 100644
index 0000000000..bc0a66709e
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/useAutoOpenArtifacts.test.ts
@@ -0,0 +1,77 @@
+import { describe, expect, it, beforeEach, afterEach } from "vitest";
+import { renderHook } from "@testing-library/react";
+import { useAutoOpenArtifacts } from "../useAutoOpenArtifacts";
+import { useCopilotUIStore } from "../../../store";
+
+// Capture the real store actions before any test can replace them.
+const realOpenArtifact = useCopilotUIStore.getState().openArtifact;
+const realResetArtifactPanel = useCopilotUIStore.getState().resetArtifactPanel;
+
+function resetStore() {
+ useCopilotUIStore.setState({
+ openArtifact: realOpenArtifact,
+ resetArtifactPanel: realResetArtifactPanel,
+ artifactPanel: {
+ isOpen: false,
+ isMinimized: false,
+ isMaximized: false,
+ width: 600,
+ activeArtifact: null,
+ history: [],
+ },
+ });
+}
+
+describe("useAutoOpenArtifacts", () => {
+ beforeEach(resetStore);
+ afterEach(resetStore);
+
+ it("does not auto-open artifacts on initial message load", () => {
+ renderHook(() => useAutoOpenArtifacts({ sessionId: "session-1" }));
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("does not auto-open when rerendering within the same session", () => {
+ const { rerender } = renderHook(
+ ({ sessionId }: { sessionId: string }) =>
+ useAutoOpenArtifacts({ sessionId }),
+ { initialProps: { sessionId: "session-1" } },
+ );
+
+ rerender({ sessionId: "session-1" });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+ });
+
+ it("panel should fully reset when session changes", () => {
+ const artifact = {
+ id: "file1",
+ title: "image.png",
+ mimeType: "image/png",
+ sourceUrl: "/api/proxy/api/workspace/files/file1/download",
+ origin: "agent" as const,
+ };
+ useCopilotUIStore.getState().openArtifact(artifact);
+ useCopilotUIStore.getState().openArtifact({
+ ...artifact,
+ id: "file2",
+ title: "second.png",
+ sourceUrl: "/api/proxy/api/workspace/files/file2/download",
+ });
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+
+ const { rerender } = renderHook(
+ ({ sessionId }: { sessionId: string }) =>
+ useAutoOpenArtifacts({ sessionId }),
+ { initialProps: { sessionId: "session-1" } },
+ );
+
+ expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(true);
+
+ rerender({ sessionId: "session-2" });
+
+ const s = useCopilotUIStore.getState().artifactPanel;
+ expect(s.isOpen).toBe(false);
+ expect(s.activeArtifact).toBeNull();
+ expect(s.history).toEqual([]);
+ });
+});
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
index 140b46b338..8ff3046d55 100644
--- 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
@@ -3,17 +3,19 @@ import { beforeEach, describe, expect, it } from "vitest";
import { useCopilotUIStore } from "../../store";
import { useAutoOpenArtifacts } from "./useAutoOpenArtifacts";
-function assistantMessageWithText(id: string, text: string) {
+const A_ID = "11111111-0000-0000-0000-000000000000";
+const B_ID = "22222222-0000-0000-0000-000000000000";
+
+function makeArtifact(id: string, title = `${id}.txt`) {
return {
id,
- role: "assistant" as const,
- parts: [{ type: "text" as const, text }],
+ title,
+ mimeType: "text/plain",
+ sourceUrl: `/api/proxy/api/workspace/files/${id}/download`,
+ origin: "agent" as const,
};
}
-const A_ID = "11111111-0000-0000-0000-000000000000";
-const B_ID = "22222222-0000-0000-0000-000000000000";
-
function resetStore() {
useCopilotUIStore.setState({
artifactPanel: {
@@ -30,111 +32,60 @@ function resetStore() {
describe("useAutoOpenArtifacts", () => {
beforeEach(resetStore);
- it("does NOT auto-open on the initial hydration of message list (baseline pass)", () => {
- const messages = [
- assistantMessageWithText("m1", `[a](workspace://${A_ID})`),
- ];
- renderHook(() =>
- useAutoOpenArtifacts({ messages: messages as any, sessionId: "s1" }),
- );
- // Initial run just records the baseline fingerprint; nothing opens.
+ it("does not auto-open on initial render", () => {
+ renderHook(() => useAutoOpenArtifacts({ sessionId: "s1" }));
expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
});
- it("auto-opens when an existing assistant message adds a new artifact", () => {
- // 1st render: baseline with no artifact.
- const initial = [assistantMessageWithText("m1", "thinking...")];
+ it("does not auto-open when rerendering within the same session", () => {
const { rerender } = renderHook(
- ({ messages, sessionId }) =>
- useAutoOpenArtifacts({ messages: messages as any, sessionId }),
- { initialProps: { messages: initial, sessionId: "s1" } },
+ ({ sessionId }) => useAutoOpenArtifacts({ sessionId }),
+ { initialProps: { sessionId: "s1" } },
);
- expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
- // 2nd render: same message id now contains an artifact link.
act(() => {
- rerender({
- messages: [
- assistantMessageWithText("m1", `here: [A](workspace://${A_ID})`),
- ],
- sessionId: "s1",
- });
+ rerender({ sessionId: "s1" });
});
- const s = useCopilotUIStore.getState().artifactPanel;
- expect(s.isOpen).toBe(true);
- expect(s.activeArtifact?.id).toBe(A_ID);
- });
- it("does not re-open when the fingerprint hasn't changed", () => {
- const msg = assistantMessageWithText("m1", `[A](workspace://${A_ID})`);
- const { rerender } = renderHook(
- ({ messages, sessionId }) =>
- useAutoOpenArtifacts({ messages: messages as any, sessionId }),
- { initialProps: { messages: [msg], sessionId: "s1" } },
- );
- // Baseline captured; no open.
- expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
-
- // Rerender identical content: no change in fingerprint → no open.
- act(() => {
- rerender({ messages: [msg], sessionId: "s1" });
- });
expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
});
- it("auto-opens when a brand-new assistant message arrives after the baseline is established", () => {
- // First render: one message without artifacts → establishes baseline.
+ 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(
- ({ messages, sessionId }) =>
- useAutoOpenArtifacts({ messages: messages as any, sessionId }),
- {
- initialProps: {
- messages: [assistantMessageWithText("m1", "plain")] as any,
- sessionId: "s1",
- },
- },
+ ({ sessionId }) => useAutoOpenArtifacts({ sessionId }),
+ { initialProps: { sessionId: "s1" } },
);
- expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
- // Second render: a *new* assistant message with an artifact. Baseline
- // is already set, so this should auto-open.
act(() => {
- rerender({
- messages: [
- assistantMessageWithText("m1", "plain"),
- assistantMessageWithText("m2", `[B](workspace://${B_ID})`),
- ] as any,
- sessionId: "s1",
- });
+ rerender({ sessionId: "s2" });
});
+
const s = useCopilotUIStore.getState().artifactPanel;
- expect(s.isOpen).toBe(true);
- expect(s.activeArtifact?.id).toBe(B_ID);
+ expect(s.isOpen).toBe(false);
+ expect(s.activeArtifact).toBeNull();
+ expect(s.history).toEqual([]);
});
- it("resets hydration baseline when sessionId changes", () => {
+ it("does not carry a stale back stack into the next session", () => {
+ useCopilotUIStore.getState().openArtifact(makeArtifact(A_ID, "a.txt"));
+ useCopilotUIStore.getState().openArtifact(makeArtifact(B_ID, "b.txt"));
+
const { rerender } = renderHook(
- ({ messages, sessionId }) =>
- useAutoOpenArtifacts({ messages: messages as any, sessionId }),
- {
- initialProps: {
- messages: [
- assistantMessageWithText("m1", `[A](workspace://${A_ID})`),
- ] as any,
- sessionId: "s1",
- },
- },
+ ({ sessionId }) => useAutoOpenArtifacts({ sessionId }),
+ { initialProps: { sessionId: "s1" } },
);
- // Switch to a new session — the first pass on the new session should
- // NOT auto-open (it's a fresh hydration).
+
act(() => {
- rerender({
- messages: [
- assistantMessageWithText("m2", `[B](workspace://${B_ID})`),
- ] as any,
- sessionId: "s2",
- });
+ rerender({ sessionId: "s2" });
});
- expect(useCopilotUIStore.getState().artifactPanel.isOpen).toBe(false);
+
+ useCopilotUIStore.getState().openArtifact(makeArtifact("c", "c.txt"));
+
+ const s = useCopilotUIStore.getState().artifactPanel;
+ expect(s.activeArtifact?.id).toBe("c");
+ expect(s.history).toEqual([]);
});
});
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 4fc1ca02bb..a8b867009c 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,91 +1,29 @@
"use client";
-import { UIDataTypes, UIMessage, UITools } from "ai";
import { useEffect, useRef } from "react";
-import type { ArtifactRef } from "../../store";
import { useCopilotUIStore } from "../../store";
-import { getMessageArtifacts } from "../ChatMessagesContainer/helpers";
-
-function fingerprintArtifacts(artifacts: ArtifactRef[]): string {
- return artifacts
- .map((a) => `${a.id}:${a.title}:${a.mimeType ?? ""}:${a.sourceUrl}`)
- .join("|");
-}
interface UseAutoOpenArtifactsOptions {
- messages: UIMessage<unknown, UIDataTypes, UITools>[];
sessionId: string | null;
}
export function useAutoOpenArtifacts({
- messages,
sessionId,
}: UseAutoOpenArtifactsOptions) {
- const openArtifact = useCopilotUIStore((state) => state.openArtifact);
- const messageFingerprintsRef = useRef<Map<string, string>>(new Map());
- const hasInitializedRef = useRef(false);
+ const resetArtifactPanel = useCopilotUIStore(
+ (state) => state.resetArtifactPanel,
+ );
+ const prevSessionIdRef = useRef(sessionId);
useEffect(() => {
- messageFingerprintsRef.current = new Map();
- hasInitializedRef.current = false;
- }, [sessionId]);
-
- useEffect(() => {
- if (messages.length === 0) {
- messageFingerprintsRef.current = new Map();
- return;
- }
-
- // Only scan messages whose fingerprint might have changed since the
- // last pass: that's the last assistant message (currently streaming)
- // plus any assistant message whose id isn't in the baseline yet.
- // This keeps the cost O(new+tail), not O(all messages), on every chunk.
- const previous = messageFingerprintsRef.current;
- const nextFingerprints = new Map<string, string>(previous);
- let nextArtifact: ArtifactRef | null = null;
- const lastAssistantIdx = (() => {
- for (let i = messages.length - 1; i >= 0; i--) {
- if (messages[i].role === "assistant") return i;
- }
- return -1;
- })();
-
- for (let i = 0; i < messages.length; i++) {
- const message = messages[i];
- if (message.role !== "assistant") continue;
- const isTailAssistant = i === lastAssistantIdx;
- const isNewMessage = !previous.has(message.id);
- if (!isTailAssistant && !isNewMessage) continue;
-
- const artifacts = getMessageArtifacts(message);
- const fingerprint = fingerprintArtifacts(artifacts);
- nextFingerprints.set(message.id, fingerprint);
-
- if (!hasInitializedRef.current || fingerprint.length === 0) {
- continue;
- }
-
- const previousFingerprint = previous.get(message.id) ?? "";
- if (previousFingerprint === fingerprint) continue;
-
- nextArtifact = artifacts[artifacts.length - 1] ?? nextArtifact;
- }
-
- // Drop entries for messages that no longer exist (e.g. history truncated).
- const liveIds = new Set(messages.map((m) => m.id));
- for (const id of nextFingerprints.keys()) {
- if (!liveIds.has(id)) nextFingerprints.delete(id);
- }
-
- messageFingerprintsRef.current = nextFingerprints;
-
- if (!hasInitializedRef.current) {
- hasInitializedRef.current = true;
- return;
- }
-
- if (nextArtifact) {
- openArtifact(nextArtifact);
+ const isSessionChange = prevSessionIdRef.current !== sessionId;
+ prevSessionIdRef.current = sessionId;
+
+ // 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) {
+ resetArtifactPanel();
}
- }, [messages, openArtifact]);
+ }, [sessionId, resetArtifactPanel]);
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/UsageLimits/__tests__/UsagePanelContent.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/UsageLimits/__tests__/UsagePanelContent.test.ts
index c7804c6dfc..4473a78959 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/UsageLimits/__tests__/UsagePanelContent.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/UsageLimits/__tests__/UsagePanelContent.test.ts
@@ -19,8 +19,16 @@ describe("formatResetTime", () => {
});
it("returns formatted date when over 24 hours away", () => {
- const result = formatResetTime("2025-06-17T00:00:00Z", now);
- expect(result).toMatch(/Tue/);
+ const resetsAt = "2025-06-17T00:00:00Z";
+ const result = formatResetTime(resetsAt, now);
+ const expected = new Date(resetsAt).toLocaleString(undefined, {
+ weekday: "short",
+ hour: "numeric",
+ minute: "2-digit",
+ timeZoneName: "short",
+ });
+
+ expect(result).toBe(expected);
});
it("accepts a Date object for resetsAt", () => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/store.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/store.test.ts
index d31b55ebb7..71683d65cb 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/store.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/store.test.ts
@@ -99,6 +99,50 @@ describe("artifactPanel store actions", () => {
expect(s.history).toEqual([]);
});
+ it("openArtifact does not resurrect a previously closed artifact into history", () => {
+ const a = makeArtifact("a");
+ const b = makeArtifact("b");
+ useCopilotUIStore.getState().openArtifact(a);
+ useCopilotUIStore.getState().closeArtifactPanel();
+ useCopilotUIStore.getState().openArtifact(b);
+
+ const s = useCopilotUIStore.getState().artifactPanel;
+ expect(s.isOpen).toBe(true);
+ expect(s.activeArtifact?.id).toBe("b");
+ expect(s.history).toEqual([]);
+ });
+
+ it("openArtifact ignores non-previewable artifacts", () => {
+ const binary = {
+ ...makeArtifact("bin", "artifact.bin"),
+ mimeType: "application/octet-stream",
+ };
+
+ useCopilotUIStore.getState().openArtifact(binary);
+
+ const s = useCopilotUIStore.getState().artifactPanel;
+ expect(s.isOpen).toBe(false);
+ expect(s.activeArtifact).toBeNull();
+ expect(s.history).toEqual([]);
+ });
+
+ it("resetArtifactPanel clears active artifact and history", () => {
+ const a = makeArtifact("a");
+ const b = makeArtifact("b");
+ useCopilotUIStore.getState().openArtifact(a);
+ useCopilotUIStore.getState().openArtifact(b);
+ useCopilotUIStore.getState().maximizeArtifactPanel();
+
+ useCopilotUIStore.getState().resetArtifactPanel();
+
+ const s = useCopilotUIStore.getState().artifactPanel;
+ expect(s.isOpen).toBe(false);
+ expect(s.isMinimized).toBe(false);
+ expect(s.isMaximized).toBe(false);
+ expect(s.activeArtifact).toBeNull();
+ expect(s.history).toEqual([]);
+ });
+
it("minimize/restore toggles isMinimized without touching activeArtifact", () => {
const a = makeArtifact("a");
useCopilotUIStore.getState().openArtifact(a);
@@ -138,4 +182,35 @@ describe("artifactPanel store actions", () => {
expect(s.width).toBe(720);
expect(s.isMaximized).toBe(false);
});
+
+ it("history is capped at 25 entries (MAX_HISTORY)", () => {
+ // Open 27 artifacts sequentially (A0..A26). History should never exceed 25.
+ for (let i = 0; i < 27; i++) {
+ useCopilotUIStore.getState().openArtifact(makeArtifact(`a${i}`));
+ }
+ const s = useCopilotUIStore.getState().artifactPanel;
+ expect(s.activeArtifact?.id).toBe("a26");
+ expect(s.history.length).toBe(25);
+ // The oldest entry (a0) should have been dropped; a1 is the earliest surviving.
+ expect(s.history[0].id).toBe("a1");
+ expect(s.history[24].id).toBe("a25");
+ });
+
+ it("clearCopilotLocalData resets artifact panel to default", () => {
+ const a = makeArtifact("a");
+ const b = makeArtifact("b");
+ useCopilotUIStore.getState().openArtifact(a);
+ useCopilotUIStore.getState().openArtifact(b);
+ useCopilotUIStore.getState().maximizeArtifactPanel();
+
+ useCopilotUIStore.getState().clearCopilotLocalData();
+
+ const s = useCopilotUIStore.getState().artifactPanel;
+ expect(s.isOpen).toBe(false);
+ expect(s.isMinimized).toBe(false);
+ expect(s.isMaximized).toBe(false);
+ expect(s.activeArtifact).toBeNull();
+ expect(s.history).toEqual([]);
+ expect(s.width).toBe(600); // DEFAULT_PANEL_WIDTH
+ });
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts
index ebd9c3811f..d63c0bd76a 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts
@@ -1,6 +1,7 @@
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 {
@@ -92,6 +93,10 @@ 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;
@@ -121,6 +126,7 @@ interface CopilotUIState {
artifactPanel: ArtifactPanelState;
openArtifact: (ref: ArtifactRef) => void;
closeArtifactPanel: () => void;
+ resetArtifactPanel: () => void;
minimizeArtifactPanel: () => void;
maximizeArtifactPanel: () => void;
restoreArtifactPanel: () => void;
@@ -203,14 +209,20 @@ 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;
+ const shouldPushHistory =
+ state.artifactPanel.isOpen &&
+ activeArtifact != null &&
+ activeArtifact.id !== ref.id;
const MAX_HISTORY = 25;
const history = isReturningToTop
? prevHistory.slice(0, -1)
- : activeArtifact && activeArtifact.id !== ref.id
- ? [...prevHistory, activeArtifact].slice(-MAX_HISTORY)
+ : shouldPushHistory
+ ? [...prevHistory, activeArtifact!].slice(-MAX_HISTORY)
: prevHistory;
return {
artifactPanel: {
@@ -231,6 +243,17 @@ export const useCopilotUIStore = create<CopilotUIState>((set) => ({
history: [],
},
})),
+ resetArtifactPanel: () =>
+ set((state) => ({
+ artifactPanel: {
+ ...state.artifactPanel,
+ isOpen: false,
+ isMinimized: false,
+ isMaximized: false,
+ activeArtifact: null,
+ history: [],
+ },
+ })),
minimizeArtifactPanel: () =>
set((state) => ({
artifactPanel: { ...state.artifactPanel, isMinimized: true },
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/components/BlockOutputCard/BlockOutputCard.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/components/BlockOutputCard/BlockOutputCard.tsx
index 4c4a887939..941e97406d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/components/BlockOutputCard/BlockOutputCard.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/components/BlockOutputCard/BlockOutputCard.tsx
@@ -1,15 +1,13 @@
"use client";
import React, { useState } from "react";
-import { getGetWorkspaceDownloadFileByIdUrl } from "@/app/api/__generated__/endpoints/workspace/workspace";
import { Button } from "@/components/atoms/Button/Button";
import type { BlockOutputResponse } from "@/app/api/__generated__/models/blockOutputResponse";
import {
globalRegistry,
OutputItem,
} from "@/components/contextual/OutputRenderers";
-import type { OutputMetadata } from "@/components/contextual/OutputRenderers";
-import { isWorkspaceURI, parseWorkspaceURI } from "@/lib/workspace-uri";
+import { resolveForRenderer } from "@/app/(platform)/copilot/tools/ViewAgentOutput/ViewAgentOutput";
import {
ContentBadge,
ContentCard,
@@ -24,28 +22,6 @@ interface Props {
const COLLAPSED_LIMIT = 3;
-function resolveForRenderer(value: unknown): {
- value: unknown;
- metadata?: OutputMetadata;
-} {
- if (!isWorkspaceURI(value)) return { value };
-
- const parsed = parseWorkspaceURI(value);
- if (!parsed) return { value };
-
- const apiPath = getGetWorkspaceDownloadFileByIdUrl(parsed.fileID);
- const url = `/api/proxy${apiPath}`;
-
- const metadata: OutputMetadata = {};
- if (parsed.mimeType) {
- metadata.mimeType = parsed.mimeType;
- if (parsed.mimeType.startsWith("image/")) metadata.type = "image";
- else if (parsed.mimeType.startsWith("video/")) metadata.type = "video";
- }
-
- return { value: url, metadata };
-}
-
function RenderOutputValue({ value }: { value: unknown }) {
const resolved = resolveForRenderer(value);
const renderer = globalRegistry.getRenderer(
@@ -63,16 +39,6 @@ function RenderOutputValue({ value }: { value: unknown }) {
);
}
- // Fallback for audio workspace refs
- if (
- isWorkspaceURI(value) &&
- resolved.metadata?.mimeType?.startsWith("audio/")
- ) {
- return (
- <audio controls src={String(resolved.value)} className="mt-2 w-full" />
- );
- }
-
return null;
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/ViewAgentOutput/ViewAgentOutput.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/ViewAgentOutput/ViewAgentOutput.tsx
index b5adb0ef07..b31f76f378 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/ViewAgentOutput/ViewAgentOutput.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/ViewAgentOutput/ViewAgentOutput.tsx
@@ -2,7 +2,6 @@
import type { ToolUIPart } from "ai";
import React from "react";
-import { getGetWorkspaceDownloadFileByIdUrl } from "@/app/api/__generated__/endpoints/workspace/workspace";
import {
globalRegistry,
OutputItem,
@@ -47,7 +46,7 @@ interface Props {
part: ViewAgentOutputToolPart;
}
-function resolveForRenderer(value: unknown): {
+export function resolveForRenderer(value: unknown): {
value: unknown;
metadata?: OutputMetadata;
} {
@@ -56,17 +55,17 @@ function resolveForRenderer(value: unknown): {
const parsed = parseWorkspaceURI(value);
if (!parsed) return { value };
- const apiPath = getGetWorkspaceDownloadFileByIdUrl(parsed.fileID);
- const url = `/api/proxy${apiPath}`;
-
+ // Pass workspace URIs through to the registry unchanged.
+ // WorkspaceFileRenderer (priority 50) matches workspace:// URIs and
+ // handles URL building, loading skeletons, and error states internally.
+ // Previously this converted to a proxy URL which bypassed
+ // WorkspaceFileRenderer, causing ImageRenderer (bare <img>) to match.
const metadata: OutputMetadata = {};
if (parsed.mimeType) {
metadata.mimeType = parsed.mimeType;
- if (parsed.mimeType.startsWith("image/")) metadata.type = "image";
- else if (parsed.mimeType.startsWith("video/")) metadata.type = "video";
}
- return { value: url, metadata };
+ return { value, metadata };
}
function RenderOutputValue({ value }: { value: unknown }) {
@@ -86,16 +85,6 @@ function RenderOutputValue({ value }: { value: unknown }) {
);
}
- // Fallback for audio workspace refs
- if (
- isWorkspaceURI(value) &&
- resolved.metadata?.mimeType?.startsWith("audio/")
- ) {
- return (
- <audio controls src={String(resolved.value)} className="mt-2 w-full" />
- );
- }
-
return null;
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/ViewAgentOutput/__tests__/resolveForRenderer.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/ViewAgentOutput/__tests__/resolveForRenderer.test.ts
new file mode 100644
index 0000000000..d5abf4db4f
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/ViewAgentOutput/__tests__/resolveForRenderer.test.ts
@@ -0,0 +1,52 @@
+import { describe, expect, it } from "vitest";
+import { resolveForRenderer } from "../ViewAgentOutput";
+import { globalRegistry } from "@/components/contextual/OutputRenderers";
+
+describe("resolveForRenderer", () => {
+ it("preserves workspace image URI for the registry to handle", () => {
+ const result = resolveForRenderer("workspace://abc123#image/png");
+ expect(String(result.value)).toMatch(/^workspace:\/\//);
+ expect(result.metadata?.mimeType).toBe("image/png");
+ });
+
+ it("preserves workspace video URI for the registry to handle", () => {
+ const result = resolveForRenderer("workspace://vid456#video/mp4");
+ expect(String(result.value)).toMatch(/^workspace:\/\//);
+ expect(result.metadata?.mimeType).toBe("video/mp4");
+ });
+
+ it("passes non-workspace values through unchanged", () => {
+ const result = resolveForRenderer("just a string");
+ expect(result.value).toBe("just a string");
+ expect(result.metadata).toBeUndefined();
+ });
+
+ it("passes non-string values through unchanged", () => {
+ const obj = { foo: "bar" };
+ const result = resolveForRenderer(obj);
+ expect(result.value).toBe(obj);
+ expect(result.metadata).toBeUndefined();
+ });
+
+ it("workspace image URIs match WorkspaceFileRenderer with loading/error states", () => {
+ // WorkspaceFileRenderer (priority 50) should handle workspace:// URIs
+ // since resolveForRenderer no longer pre-converts them to proxy URLs.
+ const resolved = resolveForRenderer("workspace://abc123#image/png");
+ const renderer = globalRegistry.getRenderer(
+ resolved.value,
+ resolved.metadata,
+ );
+ expect(renderer).toBeDefined();
+ expect(renderer!.name).toBe("WorkspaceFileRenderer");
+ });
+
+ it("workspace video URIs match WorkspaceFileRenderer", () => {
+ const resolved = resolveForRenderer("workspace://vid456#video/mp4");
+ const renderer = globalRegistry.getRenderer(
+ resolved.value,
+ resolved.metadata,
+ );
+ expect(renderer).toBeDefined();
+ expect(renderer!.name).toBe("WorkspaceFileRenderer");
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.test.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.test.ts
new file mode 100644
index 0000000000..c5f8f6d9f9
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.test.ts
@@ -0,0 +1,282 @@
+import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
+import {
+ isWorkspaceDownloadRequest,
+ isRedirectStatus,
+ isTransientWorkspaceDownloadStatus,
+ getWorkspaceDownloadErrorMessage,
+ fetchWorkspaceDownloadOnce,
+ fetchWorkspaceDownloadWithRetry,
+} from "./route.helpers";
+
+describe("isWorkspaceDownloadRequest", () => {
+ it("matches api/workspace/files/{id}/download pattern", () => {
+ expect(
+ isWorkspaceDownloadRequest([
+ "api",
+ "workspace",
+ "files",
+ "abc-123",
+ "download",
+ ]),
+ ).toBe(true);
+ });
+
+ it("rejects paths with wrong segment count", () => {
+ expect(
+ isWorkspaceDownloadRequest(["api", "workspace", "files", "download"]),
+ ).toBe(false);
+ expect(
+ isWorkspaceDownloadRequest([
+ "api",
+ "workspace",
+ "files",
+ "id",
+ "download",
+ "extra",
+ ]),
+ ).toBe(false);
+ });
+
+ it("rejects paths with wrong prefix", () => {
+ expect(
+ isWorkspaceDownloadRequest([
+ "v1",
+ "workspace",
+ "files",
+ "id",
+ "download",
+ ]),
+ ).toBe(false);
+ });
+
+ it("rejects paths not ending with download", () => {
+ expect(
+ isWorkspaceDownloadRequest([
+ "api",
+ "workspace",
+ "files",
+ "id",
+ "metadata",
+ ]),
+ ).toBe(false);
+ });
+});
+
+describe("isRedirectStatus", () => {
+ it.each([301, 302, 303, 307, 308])("returns true for %d", (status) => {
+ expect(isRedirectStatus(status)).toBe(true);
+ });
+
+ it.each([200, 304, 400, 404, 500])("returns false for %d", (status) => {
+ expect(isRedirectStatus(status)).toBe(false);
+ });
+});
+
+describe("isTransientWorkspaceDownloadStatus", () => {
+ it.each([408, 429, 500, 502, 503, 504])(
+ "returns true for transient %d",
+ (status) => {
+ expect(isTransientWorkspaceDownloadStatus(status)).toBe(true);
+ },
+ );
+
+ it.each([400, 401, 403, 404, 405])(
+ "returns false for non-transient %d",
+ (status) => {
+ expect(isTransientWorkspaceDownloadStatus(status)).toBe(false);
+ },
+ );
+});
+
+describe("getWorkspaceDownloadErrorMessage", () => {
+ it("extracts detail string from object", () => {
+ expect(getWorkspaceDownloadErrorMessage({ detail: "Not found" })).toBe(
+ "Not found",
+ );
+ });
+
+ it("extracts error string from object", () => {
+ expect(getWorkspaceDownloadErrorMessage({ error: "Server error" })).toBe(
+ "Server error",
+ );
+ });
+
+ it("extracts nested detail.message", () => {
+ expect(
+ getWorkspaceDownloadErrorMessage({
+ detail: { message: "Nested error" },
+ }),
+ ).toBe("Nested error");
+ });
+
+ it("returns trimmed string body", () => {
+ expect(getWorkspaceDownloadErrorMessage(" error text ")).toBe(
+ "error text",
+ );
+ });
+
+ it("returns null for empty string", () => {
+ expect(getWorkspaceDownloadErrorMessage("")).toBeNull();
+ });
+
+ it("returns null for whitespace-only string", () => {
+ expect(getWorkspaceDownloadErrorMessage(" ")).toBeNull();
+ });
+
+ it("returns null for null/undefined", () => {
+ expect(getWorkspaceDownloadErrorMessage(null)).toBeNull();
+ expect(getWorkspaceDownloadErrorMessage(undefined)).toBeNull();
+ });
+
+ it("returns null for object with empty detail", () => {
+ expect(getWorkspaceDownloadErrorMessage({ detail: "" })).toBeNull();
+ });
+
+ it("returns null for object with no recognized keys", () => {
+ expect(getWorkspaceDownloadErrorMessage({ foo: "bar" })).toBeNull();
+ });
+
+ it("prefers detail over error", () => {
+ expect(
+ getWorkspaceDownloadErrorMessage({
+ detail: "detail msg",
+ error: "error msg",
+ }),
+ ).toBe("detail msg");
+ });
+});
+
+describe("fetchWorkspaceDownloadOnce", () => {
+ beforeEach(() => {
+ vi.stubGlobal("fetch", vi.fn());
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+ });
+
+ it("returns response directly for non-redirect status", async () => {
+ const mockResponse = { ok: true, status: 200, headers: new Headers() };
+ vi.mocked(fetch).mockResolvedValue(mockResponse as unknown as Response);
+
+ const result = await fetchWorkspaceDownloadOnce("https://backend/file", {});
+ expect(result).toBe(mockResponse);
+ expect(fetch).toHaveBeenCalledOnce();
+ });
+
+ it("follows redirect when Location header is present", async () => {
+ const redirectResponse = {
+ ok: false,
+ status: 302,
+ headers: new Headers({ Location: "https://storage.example.com/file" }),
+ };
+ const finalResponse = { ok: true, status: 200, headers: new Headers() };
+ vi.mocked(fetch)
+ .mockResolvedValueOnce(redirectResponse as unknown as Response)
+ .mockResolvedValueOnce(finalResponse as unknown as Response);
+
+ const result = await fetchWorkspaceDownloadOnce("https://backend/file", {
+ Authorization: "Bearer token",
+ });
+ expect(result).toBe(finalResponse);
+ expect(fetch).toHaveBeenCalledTimes(2);
+ expect(fetch).toHaveBeenNthCalledWith(
+ 2,
+ "https://storage.example.com/file",
+ { method: "GET", redirect: "follow" },
+ );
+ });
+
+ it("returns redirect response when Location header is missing", async () => {
+ const redirectResponse = {
+ ok: false,
+ status: 307,
+ headers: new Headers(),
+ };
+ vi.mocked(fetch).mockResolvedValue(redirectResponse as unknown as Response);
+
+ const result = await fetchWorkspaceDownloadOnce("https://backend/file", {});
+ expect(result).toBe(redirectResponse);
+ expect(fetch).toHaveBeenCalledOnce();
+ });
+});
+
+describe("fetchWorkspaceDownloadWithRetry", () => {
+ beforeEach(() => {
+ vi.stubGlobal("fetch", vi.fn());
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+ });
+
+ it("returns immediately on success", async () => {
+ const okResponse = { ok: true, status: 200, headers: new Headers() };
+ vi.mocked(fetch).mockResolvedValue(okResponse as unknown as Response);
+
+ const result = await fetchWorkspaceDownloadWithRetry(
+ "https://backend/file",
+ {},
+ 2,
+ 0,
+ );
+ expect(result).toBe(okResponse);
+ expect(fetch).toHaveBeenCalledOnce();
+ });
+
+ it("returns immediately on non-transient error without retrying", async () => {
+ const notFound = { ok: false, status: 404, headers: new Headers() };
+ vi.mocked(fetch).mockResolvedValue(notFound as unknown as Response);
+
+ const result = await fetchWorkspaceDownloadWithRetry(
+ "https://backend/file",
+ {},
+ 2,
+ 0,
+ );
+ expect(result.status).toBe(404);
+ expect(fetch).toHaveBeenCalledOnce();
+ });
+
+ it("retries on transient 502 and succeeds", async () => {
+ const bad = { ok: false, status: 502, headers: new Headers() };
+ const ok = { ok: true, status: 200, headers: new Headers() };
+ vi.mocked(fetch)
+ .mockResolvedValueOnce(bad as unknown as Response)
+ .mockResolvedValueOnce(ok as unknown as Response);
+
+ const result = await fetchWorkspaceDownloadWithRetry(
+ "https://backend/file",
+ {},
+ 2,
+ 0,
+ );
+ expect(result).toBe(ok);
+ expect(fetch).toHaveBeenCalledTimes(2);
+ });
+
+ it("returns last transient response after exhausting retries", async () => {
+ const bad = { ok: false, status: 503, headers: new Headers() };
+ vi.mocked(fetch).mockResolvedValue(bad as unknown as Response);
+
+ const result = await fetchWorkspaceDownloadWithRetry(
+ "https://backend/file",
+ {},
+ 2,
+ 0,
+ );
+ expect(result.status).toBe(503);
+ expect(fetch).toHaveBeenCalledTimes(3);
+ });
+
+ it("retries on network error and throws after exhausting retries", async () => {
+ vi.mocked(fetch).mockRejectedValue(new Error("Connection reset"));
+
+ await expect(
+ fetchWorkspaceDownloadWithRetry("https://backend/file", {}, 1, 0),
+ ).rejects.toThrow("Connection reset");
+ expect(fetch).toHaveBeenCalledTimes(2);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts
new file mode 100644
index 0000000000..cd83c7274d
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.helpers.ts
@@ -0,0 +1,108 @@
+export function isWorkspaceDownloadRequest(path: string[]): boolean {
+ return (
+ path.length == 5 &&
+ path[0] === "api" &&
+ path[1] === "workspace" &&
+ path[2] === "files" &&
+ path[path.length - 1] === "download"
+ );
+}
+
+export function isRedirectStatus(status: number): boolean {
+ return [301, 302, 303, 307, 308].includes(status);
+}
+
+export function isTransientWorkspaceDownloadStatus(status: number): boolean {
+ return status === 408 || status === 429 || status >= 500;
+}
+
+export function sleep(ms: number): Promise<void> {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
+
+export async function fetchWorkspaceDownloadOnce(
+ backendUrl: string,
+ headers: Record<string, string>,
+): Promise<Response> {
+ const backendResponse = await fetch(backendUrl, {
+ method: "GET",
+ headers,
+ redirect: "manual",
+ });
+
+ if (!isRedirectStatus(backendResponse.status)) {
+ return backendResponse;
+ }
+
+ const location = backendResponse.headers.get("Location");
+ if (!location) return backendResponse;
+
+ return await fetch(location, {
+ method: "GET",
+ redirect: "follow",
+ });
+}
+
+export async function fetchWorkspaceDownloadWithRetry(
+ backendUrl: string,
+ headers: Record<string, string>,
+ maxRetries: number,
+ retryDelayMs: number,
+): Promise<Response> {
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
+ try {
+ const response = await fetchWorkspaceDownloadOnce(backendUrl, headers);
+ if (
+ response.ok ||
+ !isTransientWorkspaceDownloadStatus(response.status) ||
+ attempt === maxRetries
+ ) {
+ return response;
+ }
+ } catch (error) {
+ if (attempt === maxRetries) throw error;
+ }
+
+ await sleep(retryDelayMs);
+ }
+
+ throw new Error("Workspace download failed after retries");
+}
+
+export function getWorkspaceDownloadErrorMessage(body: unknown): string | null {
+ if (typeof body === "string") {
+ const trimmed = body.trim();
+ return trimmed || null;
+ }
+
+ if (!body || typeof body !== "object") return null;
+
+ if (
+ "detail" in body &&
+ typeof body.detail === "string" &&
+ body.detail.trim().length > 0
+ ) {
+ return body.detail.trim();
+ }
+
+ if (
+ "error" in body &&
+ typeof body.error === "string" &&
+ body.error.trim().length > 0
+ ) {
+ return body.error.trim();
+ }
+
+ if (
+ "detail" in body &&
+ body.detail &&
+ typeof body.detail === "object" &&
+ "message" in body.detail &&
+ typeof body.detail.message === "string" &&
+ body.detail.message.trim().length > 0
+ ) {
+ return body.detail.message.trim();
+ }
+
+ return null;
+}
diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
index 74f36d8d0a..605d4cf16c 100644
--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
@@ -11,25 +11,17 @@ import { NextRequest, NextResponse } from "next/server";
export const maxDuration = 300; // 5 minutes timeout for large uploads
export const dynamic = "force-dynamic";
+import {
+ fetchWorkspaceDownloadWithRetry,
+ getWorkspaceDownloadErrorMessage,
+ isWorkspaceDownloadRequest,
+} from "./route.helpers";
+
function buildBackendUrl(path: string[], queryString: string): string {
const backendPath = path.join("/");
return `${environment.getAGPTServerBaseUrl()}/${backendPath}${queryString}`;
}
-/**
- * Check if this is a workspace file download request that needs binary response handling.
- */
-function isWorkspaceDownloadRequest(path: string[]): boolean {
- // Match pattern: api/workspace/files/{id}/download (5 segments)
- return (
- path.length == 5 &&
- path[0] === "api" &&
- path[1] === "workspace" &&
- path[2] === "files" &&
- path[path.length - 1] === "download"
- );
-}
-
/**
* Handle workspace file download requests with proper binary response streaming.
*/
@@ -44,17 +36,15 @@ async function handleWorkspaceDownload(
headers["Authorization"] = `Bearer ${token}`;
}
- const response = await fetch(backendUrl, {
- method: "GET",
+ const response = await fetchWorkspaceDownloadWithRetry(
+ backendUrl,
headers,
- redirect: "follow", // Follow redirects to signed URLs
- });
+ 2,
+ 500,
+ );
if (!response.ok) {
- return NextResponse.json(
- { error: `Failed to download file: ${response.statusText}` },
- { status: response.status },
- );
+ return await createWorkspaceDownloadErrorResponse(response);
}
// Fully buffer the response before forwarding. Passing response.body as a
@@ -81,6 +71,34 @@ async function handleWorkspaceDownload(
});
}
+async function createWorkspaceDownloadErrorResponse(
+ response: Response,
+): Promise<NextResponse> {
+ const contentType = response.headers.get("Content-Type")?.toLowerCase() ?? "";
+
+ try {
+ if (contentType.includes("application/json")) {
+ const body = await response.json();
+ return NextResponse.json(body, { status: response.status });
+ }
+
+ const text = await response.text();
+ const detail =
+ getWorkspaceDownloadErrorMessage(text) ||
+ response.statusText ||
+ "Failed to download file";
+
+ return NextResponse.json({ detail }, { status: response.status });
+ } catch {
+ return NextResponse.json(
+ {
+ detail: response.statusText || "Failed to download file",
+ },
+ { status: response.status },
+ );
+ }
+}
+
async function handleJsonRequest(
req: NextRequest,
method: string,
diff --git a/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/CSVRenderer.test.ts b/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/CSVRenderer.test.ts
index 6fc650cd1a..5576c54064 100644
--- a/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/CSVRenderer.test.ts
+++ b/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/CSVRenderer.test.ts
@@ -1,3 +1,5 @@
+import { render, screen } from "@testing-library/react";
+import type React from "react";
import { describe, expect, it } from "vitest";
import { csvRenderer } from "./CSVRenderer";
@@ -16,6 +18,16 @@ describe("csvRenderer.canRender", () => {
it("matches .csv filename case-insensitively", () => {
expect(csvRenderer.canRender("a,b", { filename: "data.CSV" })).toBe(true);
});
+ it("matches TSV mime type", () => {
+ expect(
+ csvRenderer.canRender("a\tb\n1\t2", {
+ mimeType: "text/tab-separated-values",
+ }),
+ ).toBe(true);
+ });
+ it("matches .tsv filename case-insensitively", () => {
+ expect(csvRenderer.canRender("a\tb", { filename: "data.TSV" })).toBe(true);
+ });
it("rejects non-string values", () => {
expect(csvRenderer.canRender(42, { mimeType: "text/csv" })).toBe(false);
});
@@ -64,4 +76,16 @@ describe("csvRenderer.render (parse via render output smoke)", () => {
const csv = 'name\n"She said ""hi"""';
expect(() => csvRenderer.render(csv)).not.toThrow();
});
+ it("renders TSV columns using tabs as the delimiter", () => {
+ render(
+ csvRenderer.render("name\tage\nAlice\t30", {
+ filename: "data.tsv",
+ }) as React.ReactElement,
+ );
+
+ expect(screen.getByText("name")).toBeDefined();
+ expect(screen.getByText("age")).toBeDefined();
+ expect(screen.getByText("Alice")).toBeDefined();
+ expect(screen.getByText("30")).toBeDefined();
+ });
});
diff --git a/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/CSVRenderer.tsx b/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/CSVRenderer.tsx
index 7f39064eb1..594aecb306 100644
--- a/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/CSVRenderer.tsx
+++ b/autogpt_platform/frontend/src/components/contextual/OutputRenderers/renderers/CSVRenderer.tsx
@@ -6,7 +6,35 @@ import {
CopyContent,
} from "../types";
-function parseCSV(text: string): { headers: string[]; rows: string[][] } {
+function normalizeMime(mime?: string): string | undefined {
+ return mime?.toLowerCase().split(";")[0]?.trim();
+}
+
+function getDelimiter(metadata?: OutputMetadata): "," | "\t" {
+ if (
+ normalizeMime(metadata?.mimeType) === "text/tab-separated-values" ||
+ metadata?.filename?.toLowerCase().endsWith(".tsv")
+ ) {
+ return "\t";
+ }
+
+ return ",";
+}
+
+function getDelimitedMimeType(metadata?: OutputMetadata): string {
+ return getDelimiter(metadata) === "\t"
+ ? "text/tab-separated-values"
+ : "text/csv";
+}
+
+function getDelimitedFallbackFilename(metadata?: OutputMetadata): string {
+ return getDelimiter(metadata) === "\t" ? "data.tsv" : "data.csv";
+}
+
+function parseDelimitedText(
+ text: string,
+ delimiter: "," | "\t",
+): { headers: string[]; rows: string[][] } {
const normalized = text
.replace(/\r\n?/g, "\n")
.replace(/^\ufeff/, "")
@@ -32,7 +60,7 @@ function parseCSV(text: string): { headers: string[]; rows: string[][] } {
}
} else if (ch === '"') {
inQuotes = true;
- } else if (ch === ",") {
+ } else if (ch === delimiter) {
row.push(current);
current = "";
} else if (ch === "\n") {
@@ -51,8 +79,17 @@ function parseCSV(text: string): { headers: string[]; rows: string[][] } {
return { headers, rows: rows.slice(1) };
}
-function CSVTable({ value }: { value: string }) {
- const { headers, rows } = useMemo(() => parseCSV(value), [value]);
+function CSVTable({
+ value,
+ delimiter,
+}: {
+ value: string;
+ delimiter: "," | "\t";
+}) {
+ const { headers, rows } = useMemo(
+ () => parseDelimitedText(value, delimiter),
+ [delimiter, value],
+ );
const [sortCol, setSortCol] = useState<number | null>(null);
const [sortAsc, setSortAsc] = useState(true);
@@ -134,16 +171,17 @@ function CSVTable({ value }: { value: string }) {
function canRenderCSV(value: unknown, metadata?: OutputMetadata): boolean {
if (typeof value !== "string") return false;
- if (metadata?.mimeType === "text/csv") return true;
+ const mime = normalizeMime(metadata?.mimeType);
+ if (mime === "text/csv" || mime === "text/tab-separated-values") {
+ return true;
+ }
if (metadata?.filename?.toLowerCase().endsWith(".csv")) return true;
+ if (metadata?.filename?.toLowerCase().endsWith(".tsv")) return true;
return false;
}
-function renderCSV(
- value: unknown,
- _metadata?: OutputMetadata,
-): React.ReactNode {
- return <CSVTable value={String(value)} />;
+function renderCSV(value: unknown, metadata?: OutputMetadata): React.ReactNode {
+ return <CSVTable value={String(value)} delimiter={getDelimiter(metadata)} />;
}
function getCopyContentCSV(
@@ -159,10 +197,11 @@ function getDownloadContentCSV(
metadata?: OutputMetadata,
): DownloadContent | null {
const text = String(value);
+ const mimeType = getDelimitedMimeType(metadata);
return {
- data: new Blob([text], { type: "text/csv" }),
- filename: metadata?.filename || "data.csv",
- mimeType: "text/csv",
+ data: new Blob([text], { type: mimeType }),
+ filename: metadata?.filename || getDelimitedFallbackFilename(metadata),
+ mimeType,
};
}
diff --git a/codecov.yml b/codecov.yml
index 8a09885275..c59e08e110 100644
--- a/codecov.yml
+++ b/codecov.yml
@@ -4,14 +4,36 @@ codecov:
coverage:
status:
project:
- default:
+ platform-backend:
+ target: auto
+ threshold: 1%
+ flags:
+ - platform-backend
+ platform-frontend:
target: auto
threshold: 1%
informational: true
+ flags:
+ - platform-frontend
patch:
- default:
+ platform-backend:
+ target: 80%
+ flags:
+ - platform-backend
+ platform-frontend:
+ target: 80%
+ flags:
+ - platform-frontend
+ autogpt-libs:
+ target: 80%
+ informational: true
+ flags:
+ - autogpt-libs
+ classic:
target: 80%
informational: true
+ flags:
+ - autogpt-agent
flags:
platform-backend:
@@ -26,6 +48,10 @@ flags:
paths:
- autogpt_platform/frontend/src/
carryforward: true
+ autogpt-libs:
+ paths:
+ - autogpt_platform/autogpt_libs/
+ carryforward: true
autogpt-agent:
paths:
- classic/
@@ -36,8 +62,10 @@ component_management:
statuses:
- type: project
target: auto
+ informational: true
- type: patch
target: 80%
+ informational: true
individual_components:
- component_id: platform-backend
name: "Platform Backend"