diff --git a/apps/web/src/components/chat/MessageAttachments.logic.test.ts b/apps/web/src/components/chat/MessageAttachments.logic.test.ts new file mode 100644 index 0000000000..29893b4d68 --- /dev/null +++ b/apps/web/src/components/chat/MessageAttachments.logic.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { resolveAttachmentRenderKind } from "./MessageAttachments"; + +describe("resolveAttachmentRenderKind", () => { + it("renders raster images inline regardless of declared type variant", () => { + expect(resolveAttachmentRenderKind({ type: "image", mimeType: "image/png" })).toBe("image"); + // A media delivery stores non-image/* under type "file", but an image + // mime still renders inline. + expect(resolveAttachmentRenderKind({ type: "file", mimeType: "image/jpeg" })).toBe("image"); + }); + + it("routes SVG to a download card, never inline", () => { + // Same-origin-served SVG can execute script; a confused agent must not + // be able to put one in an -adjacent render path. + expect(resolveAttachmentRenderKind({ type: "file", mimeType: "image/svg+xml" })).toBe("file"); + expect(resolveAttachmentRenderKind({ type: "image", mimeType: "image/svg+xml" })).toBe("file"); + }); + + it("gives video a player and PDF a card", () => { + expect(resolveAttachmentRenderKind({ type: "file", mimeType: "video/mp4" })).toBe("video"); + expect(resolveAttachmentRenderKind({ type: "file", mimeType: "application/pdf" })).toBe("pdf"); + }); + + it("defaults everything else to a download card", () => { + expect(resolveAttachmentRenderKind({ type: "file", mimeType: "application/zip" })).toBe("file"); + expect(resolveAttachmentRenderKind({ type: "file", mimeType: "audio/mpeg" })).toBe("file"); + expect(resolveAttachmentRenderKind({ type: "file", mimeType: "text/plain" })).toBe("file"); + }); +}); diff --git a/apps/web/src/components/chat/MessageAttachments.tsx b/apps/web/src/components/chat/MessageAttachments.tsx new file mode 100644 index 0000000000..7c6a2f4e90 --- /dev/null +++ b/apps/web/src/components/chat/MessageAttachments.tsx @@ -0,0 +1,169 @@ +import { DownloadIcon, FileIcon, FileTextIcon } from "lucide-react"; +import { memo } from "react"; + +import { cn } from "~/lib/utils"; +import type { ChatAttachment } from "../../types"; +import { buildExpandedImagePreview, type ExpandedImagePreview } from "./ExpandedImagePreview"; + +/** + * Which native rendering an attachment gets, derived from its mime alone — + * the contract deliberately does not encode a rendering kind, so adding a + * renderable type is a change here, never a schema migration. + * + * SVG is intentionally NOT inline-image: the asset route serves same-origin, + * and an inline ``-adjacent SVG is one `foreignObject` away from script + * execution. Hermes is the user's own agent, but "my agent got confused" is + * exactly the failure mode a download card contains and inline render does + * not. + */ +export function resolveAttachmentRenderKind( + attachment: Pick, +): "image" | "video" | "pdf" | "file" { + const mime = attachment.mimeType.trim().toLowerCase(); + if (mime === "image/svg+xml") return "file"; + if (attachment.type === "image" || mime.startsWith("image/")) return "image"; + if (mime.startsWith("video/")) return "video"; + if (mime === "application/pdf") return "pdf"; + return "file"; +} + +function formatAttachmentSize(sizeBytes: number): string { + if (sizeBytes < 1024) return `${sizeBytes} B`; + if (sizeBytes < 1024 * 1024) return `${(sizeBytes / 1024).toFixed(0)} KB`; + return `${(sizeBytes / (1024 * 1024)).toFixed(1)} MB`; +} + +function AttachmentCard({ + attachment, + icon, + actionLabel, +}: { + attachment: ChatAttachment; + icon: React.ReactNode; + actionLabel: string; +}) { + const card = ( + + {icon} + + {attachment.name} + + {formatAttachmentSize(attachment.sizeBytes)} + + + {attachment.previewUrl ? ( + + ) : null} + + ); + if (!attachment.previewUrl) { + // The signed URL has not resolved yet (or the asset is gone); an inert + // card beats a dead link. + return {card}; + } + return ( + + {card} + + ); +} + +/** + * Renders a message's attachments natively: images inline with the zoom + * dialog, videos with the browser's player, PDFs as open-in-tab cards, and + * everything else as download cards. Shared by user and assistant rows so + * the two sides cannot drift. + */ +export const MessageAttachments = memo(function MessageAttachments({ + attachments, + onImageExpand, + className, +}: { + attachments: ReadonlyArray; + onImageExpand: (preview: ExpandedImagePreview) => void; + className?: string; +}) { + if (attachments.length === 0) return null; + + const images = attachments.filter((a) => resolveAttachmentRenderKind(a) === "image"); + const rest = attachments.filter((a) => resolveAttachmentRenderKind(a) !== "image"); + + return ( +
+ {images.length > 0 ? ( +
+ {images.map((image) => ( +
+ {image.previewUrl ? ( + + ) : ( +
+ {image.name} +
+ )} +
+ ))} +
+ ) : null} + {rest.map((attachment) => { + const kind = resolveAttachmentRenderKind(attachment); + if (kind === "video" && attachment.previewUrl) { + return ( + // eslint-disable-next-line jsx-a11y/media-has-caption -- agent-produced media carries no track +
+ ); +}); diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 3104ba02da..c9ffd393bb 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -61,6 +61,7 @@ import { } from "lucide-react"; import { Button } from "../ui/button"; import { buildExpandedImagePreview, ExpandedImagePreview } from "./ExpandedImagePreview"; +import { MessageAttachments } from "./MessageAttachments"; import { ProposedPlanCard } from "./ProposedPlanCard"; import { ChangedFilesCard } from "./ChangedFilesTree"; import { shouldAutoExpandChangedFiles } from "./changedFilesPresentation"; @@ -911,37 +912,11 @@ function UserTimelineRow({ row }: { row: Extract
{regularImages.length > 0 && ( -
- {regularImages.map((image: NonNullable[number]) => ( -
- {image.previewUrl ? ( - - ) : ( -
- {image.name} -
- )} -
- ))} -
+ )} {previewAnnotations.map((annotation, index) => ( 0); + // A captionless media message is complete as-is — "(empty response)" is + // for a turn that produced nothing, not for one that produced a file. + const messageText = + row.message.text || (row.message.streaming || hasAttachments ? "" : "(empty response)"); + // Turn-scoped media rides a conversation the user is already in: it reads + // as part of the reply, not as an arrival, so it carries no delivery + // header even though it flows through the notification machinery for + // dedupe. Provenance headers are for things that land while you're away. + const notificationBadge = row.message.turnId + ? null + : resolveNotificationBadge(row.message.notification); return ( <> @@ -1120,6 +1105,15 @@ function AssistantTimelineRow({ )} /> ) : null} + {row.message.attachments && row.message.attachments.length > 0 ? ( + + ) : null} { readonly attachments?: ReadonlyArray | undefined;