Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(frontend/copilot): show image thumbnails in the chat input and chat (#15882)

Ubbeb5e0b403c4e1
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useSendMessage.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useSendMessage.test.tsx
index 0662adb4b0..0e0321747e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useSendMessage.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useSendMessage.test.tsx
@@ -87,6 +87,7 @@ describe("useSendMessage with local attachments", () => {
mediaType: "application/pdf",
sizeBytes: 2048,
isUploading: true,
+ file: expect.any(File),
},
],
});
@@ -142,12 +143,18 @@ describe("useSendMessage with local attachments", () => {
await waitFor(() => expect(result.current.pendingSend).not.toBeNull());
expect(result.current.pendingSend?.attachments).toEqual([
- { name: "notes.txt", mediaType: "text/plain", isUploading: false },
+ {
+ name: "notes.txt",
+ mediaType: "text/plain",
+ isUploading: false,
+ url: `/api/proxy/api/workspace/files/${FILE_ID}/download`,
+ },
{
name: "icon.png",
mediaType: "image/png",
sizeBytes: 2048,
isUploading: true,
+ file: expect.any(File),
},
]);
});
@@ -240,6 +247,7 @@ describe("useSendMessage with local attachments", () => {
mediaType: "application/pdf",
sizeBytes: 2048,
isUploading: true,
+ file: expect.any(File),
},
],
});
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 20e7139327..23100fde4b 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
@@ -262,6 +262,11 @@ const PREVIEWABLE_IMAGE_MIMES = new Set([
"image/avif",
]);
+/** True for image types a browser can draw in an `<img>`. */
+export function isPreviewableImage(mimeType?: string | null): boolean {
+ return PREVIEWABLE_IMAGE_MIMES.has((mimeType ?? "").toLowerCase());
+}
+
const PREVIEWABLE_VIDEO_MIMES = new Set([
"video/mp4",
"video/webm",
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ChipThumbnail.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ChipThumbnail.tsx
new file mode 100644
index 0000000000..48b5731639
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ChipThumbnail.tsx
@@ -0,0 +1,48 @@
+"use client";
+
+import { cn } from "@/lib/utils";
+import { type ReactNode, useState } from "react";
+import {
+ type Attachment,
+ workspaceFileDownloadUrl,
+} from "../../../helpers/workspaceAttachments";
+import { LocalFileImage } from "../../LocalFileImage/LocalFileImage";
+
+interface Props {
+ attachment: Attachment;
+ stacked: boolean;
+ /** Shown instead when the image fails to load. */
+ fallback: ReactNode;
+}
+
+/** Round preview of an attached image, in place of the chip's file icon. */
+export function ChipThumbnail({ attachment, stacked, fallback }: Props) {
+ const [failed, setFailed] = useState(false);
+ const className = cn(
+ "size-[18px] shrink-0 rounded-full bg-zinc-200 object-cover",
+ stacked ? "-ml-1" : "-ml-1.5",
+ );
+
+ if (failed) return fallback;
+ if (attachment.kind === "local") {
+ return (
+ <LocalFileImage
+ file={attachment.file}
+ className={className}
+ onError={() => setFailed(true)}
+ />
+ );
+ }
+ if (attachment.kind === "workspace") {
+ return (
+ // eslint-disable-next-line @next/next/no-img-element
+ <img
+ src={workspaceFileDownloadUrl(attachment.fileId)}
+ alt=""
+ className={className}
+ onError={() => setFailed(true)}
+ />
+ );
+ }
+ return null;
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/FileChips.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/FileChips.tsx
index 9cd6acae8b..f39deafec1 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/FileChips.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/FileChips.tsx
@@ -1,6 +1,8 @@
"use client";
import { getFileTypeIcon } from "@/app/(platform)/artifacts/components/ArtifactsList/helpers";
+import { isPreviewableImage } from "../../ArtifactPanel/helpers";
+import { ChipThumbnail } from "./ChipThumbnail";
import { AnimatePresence, motion, useReducedMotion } from "framer-motion";
import {
type Attachment,
@@ -63,13 +65,21 @@ export function FileChips({
{attachments.map((attachment, index) => {
const name = attachmentName(attachment);
const isFolder = attachment.kind === "folder";
- const chipIcon = isFolder
- ? Folder01Icon
- : getFileTypeIcon(
- attachment.kind === "local"
- ? attachment.file.type
- : attachment.mimeType,
- );
+ const mimeType =
+ attachment.kind === "local"
+ ? attachment.file.type
+ : attachment.kind === "workspace"
+ ? attachment.mimeType
+ : "";
+ const chipIcon = (
+ <Icon
+ icon={isFolder ? Folder01Icon : getFileTypeIcon(mimeType)}
+ className={cn(
+ "h-3.5 w-3.5 shrink-0 text-zinc-900",
+ stacked && "text-zinc-400",
+ )}
+ />
+ );
const count = isFolder
? folderSummary(
attachment.fileCount,
@@ -114,13 +124,15 @@ export function FileChips({
"gap-1.5 border border-zinc-200 bg-white py-[3px] pl-2 pr-1.5 text-xs text-zinc-900 shadow-[0_1px_2px_rgba(0,0,0,0.02)]",
)}
>
- <Icon
- icon={chipIcon}
- className={cn(
- "h-3.5 w-3.5 shrink-0 text-zinc-900",
- stacked && "text-zinc-400",
- )}
- />
+ {isPreviewableImage(mimeType) ? (
+ <ChipThumbnail
+ attachment={attachment}
+ stacked={stacked}
+ fallback={chipIcon}
+ />
+ ) : (
+ chipIcon
+ )}
<span className="max-w-[160px] truncate">{name}</span>
{count ? (
<span className="shrink-0 text-zinc-500">· {count}</span>
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/FileChips.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/FileChips.test.tsx
index 3e16c0b448..909e58bd47 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/FileChips.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/FileChips.test.tsx
@@ -52,6 +52,53 @@ describe("FileChips", () => {
expect(onRemove).toHaveBeenCalledWith(1);
});
+ it("shows a thumbnail of an attached image in place of its icon", () => {
+ const photo: Attachment = {
+ kind: "local",
+ file: new File(["jpg"], "photo.jpg", { type: "image/jpeg" }),
+ };
+ const stored: Attachment = {
+ kind: "workspace",
+ fileId: "file-2",
+ name: "shot.png",
+ mimeType: "image/png",
+ };
+ const { container } = render(
+ <FileChips
+ attachments={[photo, stored, localAttachment]}
+ onRemove={vi.fn()}
+ />,
+ );
+
+ const sources = Array.from(container.querySelectorAll("img")).map((img) =>
+ img.getAttribute("src"),
+ );
+ expect(sources).toHaveLength(2);
+ expect(sources[0]).toMatch(/^blob:/);
+ expect(sources[1]).toBe("/api/proxy/api/workspace/files/file-2/download");
+ expect(screen.getByText("photo.jpg")).toBeTruthy();
+ });
+
+ it("falls back to the file icon when an image thumbnail fails to load", () => {
+ const stored: Attachment = {
+ kind: "workspace",
+ fileId: "file-2",
+ name: "shot.png",
+ mimeType: "image/png",
+ };
+ const { container } = render(
+ <FileChips attachments={[stored]} onRemove={vi.fn()} />,
+ );
+
+ const image = container.querySelector("img");
+ expect(image).not.toBeNull();
+ fireEvent.error(image!);
+
+ expect(container.querySelector("img")).toBeNull();
+ expect(container.querySelector("svg")).not.toBeNull();
+ expect(screen.getByText("shot.png")).toBeTruthy();
+ });
+
it("shows a spinner instead of a remove button for local files while uploading", () => {
render(
<FileChips
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessageAttachments.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessageAttachments.tsx
index b5e02cf23b..502f8004f6 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessageAttachments.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessageAttachments.tsx
@@ -11,7 +11,10 @@ import {
ContentCardSubtitle,
} from "../../ToolAccordion/AccordionContent";
import { ArtifactCard } from "../../ArtifactCard/ArtifactCard";
+import { isPreviewableImage } from "../../ArtifactPanel/helpers";
+import type { ArtifactRef } from "../../../store";
import { filePartToArtifactRef } from "../helpers";
+import { MessageImageThumbnail } from "./MessageImageThumbnail";
import {
Download04Icon,
File02Icon,
@@ -65,8 +68,21 @@ export function MessageAttachments({
}: Props) {
if (files.length === 0 && folders.length === 0) return null;
+ const { images, otherFiles } = splitUserImages(files, isUser, filePattern);
+
return (
<div className="mt-2 flex flex-col gap-2">
+ {images.length > 0 && (
+ <div className="flex flex-wrap justify-end gap-2">
+ {images.map((image, i) => (
+ <MessageImageThumbnail
+ key={`image-${image.id}-${i}`}
+ artifact={image}
+ readOnly={readOnly}
+ />
+ ))}
+ </div>
+ )}
{folders.map((folder) => (
<FolderAttachmentCard
key={folder.id}
@@ -74,7 +90,7 @@ export function MessageAttachments({
readOnly={readOnly}
/>
))}
- {files.map((file, i) => {
+ {otherFiles.map((file, i) => {
const artifactRef = filePartToArtifactRef(
file,
isUser ? "user-upload" : "agent",
@@ -186,6 +202,25 @@ export function MessageAttachments({
);
}
+/** Images the user sent show as pictures; everything else keeps its card. */
+function splitUserImages(
+ files: FileUIPart[],
+ isUser: boolean | undefined,
+ filePattern: RegExp | undefined,
+) {
+ const images: ArtifactRef[] = [];
+ const otherFiles: FileUIPart[] = [];
+ for (const file of files) {
+ const ref =
+ isUser && isPreviewableImage(file.mediaType)
+ ? filePartToArtifactRef(file, "user-upload", filePattern)
+ : null;
+ if (ref) images.push(ref);
+ else otherFiles.push(file);
+ }
+ return { images, otherFiles };
+}
+
function FolderAttachmentCard({
folder,
readOnly,
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessageImageThumbnail.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessageImageThumbnail.tsx
new file mode 100644
index 0000000000..6677c555b4
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/MessageImageThumbnail.tsx
@@ -0,0 +1,95 @@
+"use client";
+
+import { Icon } from "@/components/atoms/Icon/Icon";
+import { cn } from "@/lib/utils";
+import { Loading03Icon } from "@hugeicons/core-free-icons";
+import { type ReactNode, useState } from "react";
+import type { PendingUploadAttachment } from "../../../copilotStreamStore";
+import type { ArtifactRef } from "../../../store";
+import { useCopilotUIStore } from "../../../store";
+import { ArtifactCard } from "../../ArtifactCard/ArtifactCard";
+import { LocalFileImage } from "../../LocalFileImage/LocalFileImage";
+
+// A fixed size, not the image's own width, so the frame doesn't move while
+// the sent image downloads or when it replaces the pending one.
+const FRAME = "overflow-hidden rounded-2xl border border-zinc-200 bg-zinc-100";
+const IMAGE = "block size-40 object-cover";
+
+interface Props {
+ artifact: ArtifactRef;
+ readOnly?: boolean;
+}
+
+/** An image the user sent, shown as a picture that opens in the side panel. */
+export function MessageImageThumbnail({ artifact, readOnly }: Props) {
+ const openArtifact = useCopilotUIStore((s) => s.openArtifact);
+ const [failed, setFailed] = useState(false);
+
+ if (failed) return <ArtifactCard artifact={artifact} readOnly={readOnly} />;
+
+ return (
+ <button
+ type="button"
+ onClick={() => openArtifact(artifact)}
+ aria-label={`Open ${artifact.title}`}
+ className={cn(
+ FRAME,
+ "transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-zinc-400",
+ )}
+ >
+ {/* eslint-disable-next-line @next/next/no-img-element */}
+ <img
+ src={artifact.sourceUrl}
+ alt={artifact.title}
+ className={IMAGE}
+ loading="lazy"
+ decoding="async"
+ onError={() => setFailed(true)}
+ />
+ </button>
+ );
+}
+
+interface PendingProps {
+ attachment: PendingUploadAttachment;
+ /** Shown instead when the image fails to load. */
+ fallback: ReactNode;
+}
+
+/** The same picture while the message's files upload, so nothing moves when
+ * the sent message replaces the placeholder. */
+export function PendingImageThumbnail({ attachment, fallback }: PendingProps) {
+ const [failed, setFailed] = useState(false);
+
+ if (failed) return fallback;
+
+ return (
+ <div className={cn(FRAME, "relative")}>
+ {attachment.file ? (
+ <LocalFileImage
+ file={attachment.file}
+ alt={attachment.name}
+ className={IMAGE}
+ onError={() => setFailed(true)}
+ />
+ ) : (
+ // eslint-disable-next-line @next/next/no-img-element
+ <img
+ src={attachment.url}
+ alt={attachment.name}
+ className={IMAGE}
+ onError={() => setFailed(true)}
+ />
+ )}
+ {attachment.isUploading && (
+ <span className="absolute inset-0 flex items-center justify-center bg-black/20">
+ <Icon
+ icon={Loading03Icon}
+ size={20}
+ className="animate-spin text-white"
+ />
+ </span>
+ )}
+ </div>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/PendingUploadMessage.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/PendingUploadMessage.tsx
index 74d3fc1279..ac7c65bb1b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/PendingUploadMessage.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/PendingUploadMessage.tsx
@@ -9,8 +9,12 @@ import type {
PendingUploadAttachment,
PendingUploadSend,
} from "../../../copilotStreamStore";
-import { classifyArtifact } from "../../ArtifactPanel/helpers";
+import {
+ classifyArtifact,
+ isPreviewableImage,
+} from "../../ArtifactPanel/helpers";
import { useElapsedTimer } from "../../JobStatsBar/useElapsedTimer";
+import { PendingImageThumbnail } from "./MessageImageThumbnail";
import { ThinkingIndicator } from "./ThinkingIndicator";
interface Props {
@@ -74,6 +78,13 @@ function PendingArtifactCard({ attachment }: CardProps) {
);
}
+function hasImagePreview(attachment: PendingUploadAttachment) {
+ return (
+ isPreviewableImage(attachment.mediaType) &&
+ Boolean(attachment.file || attachment.url)
+ );
+}
+
/**
* Optimistic stand-in for a user message whose local attachments are still
* uploading. Mirrors the real bubble and attachment cards so the swap to the
@@ -83,6 +94,10 @@ function PendingArtifactCard({ attachment }: CardProps) {
export function PendingUploadMessage({ pendingSend, isCompact }: Props) {
const { elapsedSeconds } = useElapsedTimer(true);
const label = uploadStatusLabel(pendingSend.attachments);
+ const images = pendingSend.attachments.filter(hasImagePreview);
+ const otherAttachments = pendingSend.attachments.filter(
+ (a) => !hasImagePreview(a),
+ );
return (
<>
@@ -108,7 +123,18 @@ export function PendingUploadMessage({ pendingSend, isCompact }: Props) {
)}
{pendingSend.attachments.length > 0 && (
<div className="mt-2 flex flex-col gap-2">
- {pendingSend.attachments.map((attachment, i) => (
+ {images.length > 0 && (
+ <div className="flex flex-wrap justify-end gap-2">
+ {images.map((attachment, i) => (
+ <PendingImageThumbnail
+ key={`${attachment.name}-${i}`}
+ attachment={attachment}
+ fallback={<PendingArtifactCard attachment={attachment} />}
+ />
+ ))}
+ </div>
+ )}
+ {otherAttachments.map((attachment, i) => (
<PendingArtifactCard
key={`${attachment.name}-${i}`}
attachment={attachment}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/MessageAttachments.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/MessageAttachments.test.tsx
index 8598a8ec1e..54055b8f48 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/MessageAttachments.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/MessageAttachments.test.tsx
@@ -1,6 +1,7 @@
import { afterEach, describe, expect, it, vi } from "vitest";
-import { cleanup, render, screen } from "@testing-library/react";
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import type { FileUIPart } from "ai";
+import { useCopilotUIStore } from "../../../../store";
import { MessageAttachments } from "../MessageAttachments";
vi.mock("../../../../components/ArtifactCard/ArtifactCard", () => ({
@@ -9,13 +10,14 @@ vi.mock("../../../../components/ArtifactCard/ArtifactCard", () => ({
const FILE_ID = "550e8400-e29b-41d4-a716-446655440000";
-function filePart(url: string, filename = "report.pdf"): FileUIPart {
- return {
- type: "file",
- url,
- filename,
- mediaType: "application/pdf",
- } as FileUIPart;
+const FILE_URL = `/api/proxy/api/workspace/files/${FILE_ID}/download`;
+
+function filePart(
+ url: string,
+ filename = "report.pdf",
+ mediaType = "application/pdf",
+): FileUIPart {
+ return { type: "file", url, filename, mediaType } as FileUIPart;
}
describe("MessageAttachments", () => {
@@ -61,6 +63,47 @@ describe("MessageAttachments", () => {
expect(screen.queryByText(/Empty/)).toBeNull();
});
+ it("shows an image the user sent as a picture that opens in the panel", () => {
+ render(
+ <MessageAttachments
+ files={[filePart(FILE_URL, "photo.jpg", "image/jpeg")]}
+ isUser
+ />,
+ );
+
+ expect(screen.queryByTestId("artifact-card")).toBeNull();
+ const image = screen.getByRole("img", { name: "photo.jpg" });
+ expect(image.getAttribute("src")).toBe(FILE_URL);
+
+ fireEvent.click(screen.getByRole("button", { name: "Open photo.jpg" }));
+ expect(useCopilotUIStore.getState().artifactPanel.activeArtifact?.id).toBe(
+ FILE_ID,
+ );
+ });
+
+ it("falls back to the card when the picture fails to load", () => {
+ render(
+ <MessageAttachments
+ files={[filePart(FILE_URL, "photo.jpg", "image/jpeg")]}
+ isUser
+ />,
+ );
+
+ fireEvent.error(screen.getByRole("img", { name: "photo.jpg" }));
+ expect(screen.getByTestId("artifact-card")).toBeDefined();
+ });
+
+ it("keeps the card for an image Otto sent", () => {
+ render(
+ <MessageAttachments
+ files={[filePart(FILE_URL, "chart.png", "image/png")]}
+ />,
+ );
+
+ expect(screen.getByTestId("artifact-card")).toBeDefined();
+ expect(screen.queryByRole("img")).toBeNull();
+ });
+
it("falls back to the file card for non-workspace files", () => {
render(
<MessageAttachments
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/PendingUploadMessage.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/PendingUploadMessage.test.tsx
index 133e1c5bc4..08babeb94a 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/PendingUploadMessage.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/PendingUploadMessage.test.tsx
@@ -1,4 +1,9 @@
-import { cleanup, render, screen } from "@/tests/integrations/test-utils";
+import {
+ cleanup,
+ fireEvent,
+ render,
+ screen,
+} from "@/tests/integrations/test-utils";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
PENDING_UPLOAD_MESSAGE_ID,
@@ -56,6 +61,51 @@ describe("PendingUploadMessage", () => {
);
});
+ it("shows a picked image as a picture while it uploads", () => {
+ const photo = new File(["jpg"], "photo.jpg", { type: "image/jpeg" });
+ render(
+ <PendingUploadMessage
+ pendingSend={{
+ text: "",
+ attachments: [
+ {
+ name: "photo.jpg",
+ mediaType: "image/jpeg",
+ isUploading: true,
+ file: photo,
+ },
+ ],
+ }}
+ />,
+ );
+
+ const image = screen.getByRole("img", { name: "photo.jpg" });
+ expect(image.getAttribute("src")).toMatch(/^blob:/);
+ });
+
+ it("shows the card when an image fails to load", () => {
+ render(
+ <PendingUploadMessage
+ pendingSend={{
+ text: "",
+ attachments: [
+ {
+ name: "shot.png",
+ mediaType: "image/png",
+ isUploading: false,
+ url: "/api/proxy/api/workspace/files/file-2/download",
+ },
+ ],
+ }}
+ />,
+ );
+
+ fireEvent.error(screen.getByRole("img", { name: "shot.png" }));
+
+ expect(screen.queryByRole("img")).toBeNull();
+ expect(screen.getByText("shot.png")).toBeDefined();
+ });
+
it("carries the message id the tail spacer measures", () => {
render(<PendingUploadMessage pendingSend={{ text: "hi", attachments }} />);
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/LocalFileImage/LocalFileImage.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/LocalFileImage/LocalFileImage.tsx
new file mode 100644
index 0000000000..e2b400f949
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/LocalFileImage/LocalFileImage.tsx
@@ -0,0 +1,27 @@
+"use client";
+
+import { useMountEffect } from "@/hooks/useMountEffect";
+import { useState } from "react";
+
+interface Props {
+ file: File;
+ alt?: string;
+ className?: string;
+ onError?: () => void;
+}
+
+/** Draws a picked file before it uploads. Give it a `key` per file: the blob
+ * URL is made on mount and revoked on unmount. */
+export function LocalFileImage({ file, alt = "", className, onError }: Props) {
+ const [src, setSrc] = useState<string | null>(null);
+
+ useMountEffect(() => {
+ const url = URL.createObjectURL(file);
+ setSrc(url);
+ return () => URL.revokeObjectURL(url);
+ });
+
+ if (!src) return <span aria-hidden className={className} />;
+ // eslint-disable-next-line @next/next/no-img-element
+ return <img src={src} alt={alt} className={className} onError={onError} />;
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts
index 587e04851c..d21f6fb659 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamStore.ts
@@ -65,6 +65,10 @@ export interface PendingUploadAttachment {
sizeBytes?: number;
/** Local files upload before the send; workspace references are already stored. */
isUploading: boolean;
+ /** The picked file, so an image can show before it uploads. */
+ file?: File;
+ /** Where a stored file already lives. */
+ url?: string;
}
/**
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/sendWithAttachments.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/sendWithAttachments.ts
index 3dec7c6894..66171f30ae 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/sendWithAttachments.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/sendWithAttachments.ts
@@ -142,6 +142,7 @@ export function describePendingUpload(
name: part.filename ?? "file",
mediaType: part.mediaType,
isUploading: false,
+ url: part.url,
},
);
const local: PendingUploadAttachment[] = message.files.map((file) => ({
@@ -149,6 +150,7 @@ export function describePendingUpload(
mediaType: file.type || "application/octet-stream",
sizeBytes: file.size,
isUploading: true,
+ file,
}));
return { text: message.text, attachments: [...stored, ...local] };
}
@@ -165,7 +167,7 @@ async function uploadFiles(
return {
file_id: data.file_id,
name: data.name || file.name,
- mime_type: data.mime_type || "application/octet-stream",
+ mime_type: data.mime_type || file.type || "application/octet-stream",
} as UploadedFile;
} catch (err) {
// The send's own failure toast says a plan is needed.
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/__tests__/attachments.test.tsx b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/__tests__/attachments.test.tsx
index 94882bc8f3..6a83fa4f23 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/__tests__/attachments.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/__tests__/attachments.test.tsx
@@ -347,7 +347,9 @@ describe("ExpertChatDrawer attachments", () => {
const bubble = await screen.findByTestId("pending-upload-message");
expect(within(bubble).getByText("what is this")).toBeDefined();
- expect(within(bubble).getByText("photo.png")).toBeDefined();
+ expect(
+ within(bubble).getByRole("img", { name: "photo.png" }),
+ ).toBeDefined();
expect(screen.queryByText("What can I do for you?")).toBeNull();
expect(recorder.uploadSessionIds).toEqual([]);
@@ -410,7 +412,9 @@ describe("ExpertChatDrawer attachments", () => {
const bubble = await screen.findByTestId("pending-upload-message");
expect(within(bubble).getByText("and this")).toBeDefined();
- expect(within(bubble).getByText("photo.png")).toBeDefined();
+ expect(
+ within(bubble).getByRole("img", { name: "photo.png" }),
+ ).toBeDefined();
await waitFor(() =>
expect(recorder.uploadSessionIds).toEqual([SESSION_ID]),
);
@@ -588,7 +592,9 @@ describe("ExpertChatDrawer attachments", () => {
await waitFor(() => expect(recorder.streamBodies.length).toBe(1));
expect(recorder.streamBodies[0].expert_kickoff).toBe(true);
- expect(within(pendingBubble()).getByText("photo.png")).toBeDefined();
+ expect(
+ within(pendingBubble()).getByRole("img", { name: "photo.png" }),
+ ).toBeDefined();
kickoffReply.release();