diff --git a/apps/web/src/components/chat/ChatMarkdownMedia.tsx b/apps/web/src/components/chat/ChatMarkdownMedia.tsx index ea8f5d224dd1..1bf15380c121 100644 --- a/apps/web/src/components/chat/ChatMarkdownMedia.tsx +++ b/apps/web/src/components/chat/ChatMarkdownMedia.tsx @@ -84,6 +84,16 @@ export function authoredImageSizeStyle( return undefined; } +/** + * An unavailable file has no dimensions, so its `aspect-video w-full` frame + * would fill the row and `max-height` would squash it. Bound the width so the + * frame stays 16:9 at the height cap. + */ +function unavailableImageSizeStyle(maxHeightRem: number): CSSProperties | undefined { + if (maxHeightRem === 30) return undefined; + return { maxWidth: `min(100%, 30rem, ${(maxHeightRem * 16) / 9}rem)` }; +} + const CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME = cn( CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME, CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME, @@ -438,13 +448,16 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props : fallbackSrc + (props.srcFragment ?? ""); const knownSize = assetUrl._tag === "Success" ? assetUrl.imageDimensions : undefined; const maxHeightRem = props.maxHeightRem ?? 30; + const unavailable = assetUrl._tag === "Failure" && fallbackSrc === undefined; const style = props.style ?? (knownSize ? authoredImageSizeStyle(knownSize.width, knownSize.height, maxHeightRem) - : maxHeightRem !== 30 - ? { maxHeight: `${maxHeightRem}rem` } - : undefined); + : unavailable + ? unavailableImageSizeStyle(maxHeightRem) + : maxHeightRem !== 30 + ? { maxHeight: `${maxHeightRem}rem` } + : undefined); const actionsSource = assetMediaActionsSource({ environmentId: props.environmentId, resource, @@ -460,7 +473,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props return ( ({ })) as never, })); -const stubbedAsset = vi.hoisted(() => ({ url: "" })); +const stubbedAsset = vi.hoisted(() => ({ url: "", failed: false })); vi.mock(import("~/assets/assetUrls"), async (original) => { const actual = await original(); return { ...actual, useAssetUrlState: ((...args: Parameters) => - stubbedAsset.url - ? { _tag: "Success", url: stubbedAsset.url } - : actual.useAssetUrlState(...args)) as typeof actual.useAssetUrlState, + stubbedAsset.failed + ? { _tag: "Failure" } + : stubbedAsset.url + ? { _tag: "Success", url: stubbedAsset.url } + : actual.useAssetUrlState(...args)) as typeof actual.useAssetUrlState, useAssetUrls: ((...args: Parameters) => stubbedAsset.url ? args[1].map(() => stubbedAsset.url) @@ -1842,6 +1844,47 @@ describe("MessagesTimeline attachments", () => { ); }); + it("keeps an unavailable viewed image inside the 10rem work-log strip", async () => { + stubbedAsset.failed = true; + onTestFinished(() => { + stubbedAsset.failed = false; + }); + await using screen = await renderElement( + + + , + ); + const frame = await vi.waitFor(() => { + const element = screen.container.querySelector( + '[data-image-gallery] [role="alert"]', + ); + expect(element).toBeTruthy(); + return element!; + }); + const size = await vi.waitFor(() => { + const rect = frame.getBoundingClientRect(); + expect(rect.width).toBeGreaterThan(0); + return rect; + }); + expect(size.height).toBeLessThanOrEqual(161); + expect(size.width).toBeLessThanOrEqual(285); + expect(Math.abs(size.width / size.height - 16 / 9)).toBeLessThan(0.02); + }); + it("opens an image from an expanded tool row as a gallery of the turn's viewed images", async () => { stubbedAsset.url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";