feat(web): render Hermes media natively in the timeline
New MessageAttachments component, shared by user and assistant rows so the two sides cannot drift: images render inline with the existing zoom dialog (UserTimelineRow's hand-rolled grid replaced by the same component), video/* gets the browser's native player, application/pdf gets a card that opens the signed asset URL in a new tab, and everything else — SVG included — gets a download card with name and size. SVG is deliberately never inline: the asset route serves same-origin, and "my agent got confused" is exactly the failure mode a download card contains. Assistant rows place media above the markdown caption; a captionless media message suppresses the "(empty response)" fallback — that text is for a turn that produced nothing, not one that produced a file. Turn-scoped media skips the delivery header: provenance headers are for things that land while you're away, and media inside a live turn reads as part of the reply. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
6cb4f53b0d
commit
a398258ce6
4 files changed
+233
-34
No files matched your search
@@ -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 <img>-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");
|
||||
});
|
||||
});
|
||||
@@ -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 `<img>`-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<ChatAttachment, "type" | "mimeType">,
|
||||
): "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 = (
|
||||
<span className="flex min-w-0 items-center gap-2.5 rounded-lg border border-border/80 bg-background/70 px-3 py-2">
|
||||
{icon}
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block truncate text-sm text-foreground/90">{attachment.name}</span>
|
||||
<span className="block text-xs text-muted-foreground">
|
||||
{formatAttachmentSize(attachment.sizeBytes)}
|
||||
</span>
|
||||
</span>
|
||||
{attachment.previewUrl ? (
|
||||
<DownloadIcon className="size-4 shrink-0 text-muted-foreground/70" aria-hidden />
|
||||
) : null}
|
||||
</span>
|
||||
);
|
||||
if (!attachment.previewUrl) {
|
||||
// The signed URL has not resolved yet (or the asset is gone); an inert
|
||||
// card beats a dead link.
|
||||
return <span className="block max-w-[420px]">{card}</span>;
|
||||
}
|
||||
return (
|
||||
<a
|
||||
href={attachment.previewUrl}
|
||||
// PDFs open in the browser's own viewer; everything else downloads.
|
||||
{...(actionLabel === "Open"
|
||||
? { target: "_blank", rel: "noreferrer" }
|
||||
: { download: attachment.name })}
|
||||
aria-label={`${actionLabel} ${attachment.name}`}
|
||||
className="block max-w-[420px] transition-colors hover:[&>span]:bg-accent/40"
|
||||
>
|
||||
{card}
|
||||
</a>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<ChatAttachment>;
|
||||
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 (
|
||||
<div className={cn("flex min-w-0 flex-col gap-2", className)}>
|
||||
{images.length > 0 ? (
|
||||
<div className="grid max-w-[420px] grid-cols-2 gap-2">
|
||||
{images.map((image) => (
|
||||
<div
|
||||
key={image.id}
|
||||
className="overflow-hidden rounded-lg border border-border/80 bg-background/70"
|
||||
>
|
||||
{image.previewUrl ? (
|
||||
<button
|
||||
type="button"
|
||||
className="h-full w-full cursor-zoom-in"
|
||||
aria-label={`Preview ${image.name}`}
|
||||
onClick={() => {
|
||||
const preview = buildExpandedImagePreview(images, image.id);
|
||||
if (!preview) return;
|
||||
onImageExpand(preview);
|
||||
}}
|
||||
>
|
||||
<img
|
||||
src={image.previewUrl}
|
||||
alt={image.name}
|
||||
className="block h-auto max-h-[220px] w-full object-cover"
|
||||
/>
|
||||
</button>
|
||||
) : (
|
||||
<div className="flex min-h-[72px] items-center justify-center px-2 py-3 text-center text-[11px] text-muted-foreground/70">
|
||||
{image.name}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : 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
|
||||
<video
|
||||
key={attachment.id}
|
||||
controls
|
||||
preload="metadata"
|
||||
src={attachment.previewUrl}
|
||||
className="max-h-[360px] max-w-[560px] rounded-lg border border-border/80 bg-black/40"
|
||||
/>
|
||||
);
|
||||
}
|
||||
if (kind === "pdf") {
|
||||
return (
|
||||
<AttachmentCard
|
||||
key={attachment.id}
|
||||
attachment={attachment}
|
||||
actionLabel="Open"
|
||||
icon={<FileTextIcon className="size-5 shrink-0 text-muted-foreground" aria-hidden />}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<AttachmentCard
|
||||
key={attachment.id}
|
||||
attachment={attachment}
|
||||
actionLabel="Download"
|
||||
icon={<FileIcon className="size-5 shrink-0 text-muted-foreground" aria-hidden />}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -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<TimelineRow, { kind: "message"
|
||||
<div className="group flex flex-col items-end gap-1">
|
||||
<div className="relative max-w-[80%] rounded-2xl bg-accent p-3">
|
||||
{regularImages.length > 0 && (
|
||||
<div className="mb-2 grid max-w-[420px] grid-cols-2 gap-2">
|
||||
{regularImages.map((image: NonNullable<TimelineMessage["attachments"]>[number]) => (
|
||||
<div
|
||||
key={image.id}
|
||||
className="overflow-hidden rounded-lg border border-border/80 bg-background/70"
|
||||
>
|
||||
{image.previewUrl ? (
|
||||
<button
|
||||
type="button"
|
||||
className="h-full w-full cursor-zoom-in"
|
||||
aria-label={`Preview ${image.name}`}
|
||||
onClick={() => {
|
||||
const preview = buildExpandedImagePreview(regularImages, image.id);
|
||||
if (!preview) return;
|
||||
ctx.onImageExpand(preview);
|
||||
}}
|
||||
>
|
||||
<img
|
||||
src={image.previewUrl}
|
||||
alt={image.name}
|
||||
className="block h-auto max-h-[220px] w-full object-cover"
|
||||
/>
|
||||
</button>
|
||||
) : (
|
||||
<div className="flex min-h-[72px] items-center justify-center px-2 py-3 text-center text-[11px] text-muted-foreground/70">
|
||||
{image.name}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<MessageAttachments
|
||||
attachments={regularImages}
|
||||
onImageExpand={ctx.onImageExpand}
|
||||
className="mb-2"
|
||||
/>
|
||||
)}
|
||||
{previewAnnotations.map((annotation, index) => (
|
||||
<UserMessagePreviewAnnotationCard
|
||||
@@ -1093,8 +1068,18 @@ function AssistantTimelineRow({
|
||||
isFirst: boolean;
|
||||
}) {
|
||||
const ctx = use(TimelineRowCtx);
|
||||
const messageText = row.message.text || (row.message.streaming ? "" : "(empty response)");
|
||||
const notificationBadge = resolveNotificationBadge(row.message.notification);
|
||||
const hasAttachments = Boolean(row.message.attachments && row.message.attachments.length > 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 ? (
|
||||
<MessageAttachments
|
||||
attachments={row.message.attachments}
|
||||
onImageExpand={ctx.onImageExpand}
|
||||
// Media above, caption (the message text) below — the text is
|
||||
// subordinate to what it annotates.
|
||||
className="mb-2"
|
||||
/>
|
||||
) : null}
|
||||
<ChatMarkdown
|
||||
text={messageText}
|
||||
cwd={ctx.markdownCwd}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {
|
||||
ChatFileAttachment as ContractChatFileAttachment,
|
||||
ChatImageAttachment as ContractChatImageAttachment,
|
||||
OrchestrationCheckpointFile,
|
||||
OrchestrationCheckpointSummary,
|
||||
@@ -35,7 +36,12 @@ export interface ChatImageAttachment extends ContractChatImageAttachment {
|
||||
readonly previewUrl?: string;
|
||||
}
|
||||
|
||||
export type ChatAttachment = ChatImageAttachment;
|
||||
/** `previewUrl` doubles as the playback/download URL for non-image files. */
|
||||
export interface ChatFileAttachment extends ContractChatFileAttachment {
|
||||
readonly previewUrl?: string;
|
||||
}
|
||||
|
||||
export type ChatAttachment = ChatImageAttachment | ChatFileAttachment;
|
||||
|
||||
export interface ChatMessage extends Omit<OrchestrationMessage, "attachments"> {
|
||||
readonly attachments?: ReadonlyArray<ChatAttachment> | undefined;
|
||||
|
||||
Reference in new issue
Block a user