Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions apps/web/src/components/chat/ChatMarkdownMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -460,7 +473,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
return (
<ChatMarkdownVideo
src={src}
sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
sourceFailed={unavailable}
unavailableDetail={unavailableDetail}
alt={props.alt}
copyMarkdown={props.copyMarkdown}
Expand All @@ -478,7 +491,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
<ChatMarkdownImage
key={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
src={src}
sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
sourceFailed={unavailable}
unavailableDetail={unavailableDetail}
alt={props.alt}
copyMarkdown={props.copyMarkdown}
Expand Down
51 changes: 47 additions & 4 deletions apps/web/src/components/chat/MessagesTimeline.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ vi.mock(import("~/state/query"), async (original) => ({
})) 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<typeof actual.useAssetUrlState>) =>
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<typeof actual.useAssetUrls>) =>
stubbedAsset.url
? args[1].map(() => stubbedAsset.url)
Expand Down Expand Up @@ -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(
<TimelineRowCtx
value={
{
threadRef: { environmentId: ENVIRONMENT_ID, threadId: ThreadId.make("thread-1") },
workspaceRoot: "/repo",
} as never
}
>
<ViewedImagesTimelineRow
row={{
kind: "viewed-images",
id: "viewed",
createdAt: CREATED_AT,
paths: ["/repo/step-1.jpg", "/repo/step-3.jpg"],
}}
/>
</TimelineRowCtx>,
);
const frame = await vi.waitFor(() => {
const element = screen.container.querySelector<HTMLElement>(
'[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=";
Expand Down
Loading