diff --git a/web/components/common/InlineFileCard.tsx b/web/components/common/InlineFileCard.tsx index fff2d748f..cfdcebf44 100644 --- a/web/components/common/InlineFileCard.tsx +++ b/web/components/common/InlineFileCard.tsx @@ -385,13 +385,14 @@ export function makeFileLinkRemarkPlugin( export function unlinkedGeneratedFiles( content: string, files: MessageAttachment[], + { imagesOnly = false }: { imagesOnly?: boolean } = {}, ): MessageAttachment[] { if (!files.length) return []; const tree = fromMarkdown(content) as unknown as Record; makeFileLinkRemarkPlugin(files)?.()(tree); const linked = new Set(); const visit = (node: Record): void => { - if (node.type === "link" || node.type === "image") { + if (node.type === "image" || (!imagesOnly && node.type === "link")) { const name = parseAttachmentHref(node.url as string | undefined); if (name) { const matching = files.filter( diff --git a/web/features/chat/messages/ChatMessageList.tsx b/web/features/chat/messages/ChatMessageList.tsx index ea664e149..6495ad299 100644 --- a/web/features/chat/messages/ChatMessageList.tsx +++ b/web/features/chat/messages/ChatMessageList.tsx @@ -46,6 +46,7 @@ import AssistantResponse from "@/components/common/AssistantResponse"; import { InlineFileCardProvider, mergeGeneratedFiles, + unlinkedGeneratedFiles, } from "@/components/common/InlineFileCard"; import Tooltip from "@/shared/ui/Tooltip"; import type { @@ -580,21 +581,31 @@ function humanizeFilename(filename: string): string { * side panel, same path as user uploads. Sources: persisted ``generated`` * attachments on the message (durable) merged with artifacts from streamed * tool_result events (live, while the turn is still running), deduped by URL. + * + * An image the reply already embeds with ``![](...)`` is drawn there, so its + * card is left out; showing both reads as the figure appearing twice. */ export function GeneratedFileCards({ attachments, events, + content, onOpen, }: { attachments: MessageAttachment[]; events?: StreamEvent[]; + /** The reply text, used to tell which images are already shown inline. */ + content?: string; onOpen?: (attachment: MessageAttachment) => void; }) { const { t } = useTranslation(); - const files = useMemo( - () => mergeGeneratedFiles(attachments, events), - [attachments, events], - ); + const files = useMemo(() => { + const merged = mergeGeneratedFiles(attachments, events); + if (!content) return merged; + const unlinked = new Set(unlinkedGeneratedFiles(content, merged, { imagesOnly: true })); + return merged.filter( + (file) => !file.mime_type?.startsWith("image/") || unlinked.has(file), + ); + }, [attachments, events, content]); if (!files.length) return null; return (
@@ -2605,6 +2616,7 @@ export const ChatMessageList = memo(function ChatMessageList({ {(() => { diff --git a/web/tests/generated-file-cards.spec.tsx b/web/tests/generated-file-cards.spec.tsx new file mode 100644 index 000000000..f28965793 --- /dev/null +++ b/web/tests/generated-file-cards.spec.tsx @@ -0,0 +1,79 @@ +import { render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import { GeneratedFileCards } from "@/features/chat/messages/ChatMessageList"; +import type { MessageAttachment } from "@/features/chat/ChatStateAdapter"; + +vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key }) })); + +function image(path: string, id: string): MessageAttachment { + return { + type: "image", + filename: path.split("/").pop(), + url: `/files/workspace-items/ws_test/${id}`, + mime_type: "image/png", + generated: true, + origin: "workspace", + workspace_id: "ws_test", + workspace_item_id: id, + relative_path: path, + }; +} + +const figure = image("outputs/chat/turn-a/figure.png", "wsi_a"); +const other = image("outputs/chat/turn-a/other.png", "wsi_b"); + +describe("generated image cards", () => { + it("leaves out an image the reply already embeds inline", () => { + render( + , + ); + expect(screen.queryByRole("img", { name: "figure.png" })).toBeNull(); + expect(screen.getByRole("img", { name: "other.png" })).toBeTruthy(); + }); + + it("keeps every card when the reply does not embed the image", () => { + render(); + expect(screen.getAllByRole("img")).toHaveLength(2); + }); + + it("keeps the card when the reply only mentions the path in code", () => { + render( + , + ); + expect(screen.getByRole("img", { name: "figure.png" })).toBeTruthy(); + }); + + it("does not hide a non-image file the reply links", () => { + const report: MessageAttachment = { + ...figure, + type: "document", + filename: "report.pdf", + mime_type: "application/pdf", + relative_path: "outputs/chat/turn-a/report.pdf", + url: "/files/workspace-items/ws_test/wsi_c", + }; + render( + , + ); + expect(screen.getByText(/report/i)).toBeTruthy(); + }); +}); + + +it.each([ + "[Download](outputs/chat/turn-a/figure.png)", + "Saved to outputs/chat/turn-a/figure.png", + "`![figure](outputs/chat/turn-a/figure.png)`", +])("keeps the image preview when content only provides a link or example: %s", (content) => { + render(); + expect(screen.getByRole("img", { name: "figure.png" })).toBeTruthy(); +});