From 799714e0f397d31e6d504358c4b352c91e5a9bca Mon Sep 17 00:00:00 2001 From: x-labb <154824493+x-labb@users.noreply.github.com> Date: Fri, 30 Oct 2026 22:21:12 +0100 Subject: [PATCH 1/2] fix(web): don't repeat a generated image as a card when the reply embeds it --- .../chat/messages/ChatMessageList.tsx | 20 ++++-- web/tests/generated-file-cards.spec.tsx | 69 +++++++++++++++++++ 2 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 web/tests/generated-file-cards.spec.tsx diff --git a/web/features/chat/messages/ChatMessageList.tsx b/web/features/chat/messages/ChatMessageList.tsx index bd95ff881..b9a135e71 100644 --- a/web/features/chat/messages/ChatMessageList.tsx +++ b/web/features/chat/messages/ChatMessageList.tsx @@ -45,6 +45,7 @@ import AssistantResponse from "@/components/common/AssistantResponse"; import { InlineFileCardProvider, mergeGeneratedFiles, + unlinkedGeneratedFiles, } from "@/components/common/InlineFileCard"; import Tooltip from "@/shared/ui/Tooltip"; import type { @@ -570,21 +571,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)); + return merged.filter( + (file) => !file.mime_type?.startsWith("image/") || unlinked.has(file), + ); + }, [attachments, events, content]); if (!files.length) return null; return (
@@ -2553,6 +2564,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..d52d6de31 --- /dev/null +++ b/web/tests/generated-file-cards.spec.tsx @@ -0,0 +1,69 @@ +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(); + }); +}); From 0cfbaf738093ab1aa2a3f1b5499acc925baa2b0c Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Sat, 31 Oct 2026 16:42:56 +0800 Subject: [PATCH 2/2] fix(chat): hide generated previews only for embedded images --- web/components/common/InlineFileCard.tsx | 3 ++- web/features/chat/messages/ChatMessageList.tsx | 2 +- web/tests/generated-file-cards.spec.tsx | 10 ++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) 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 b9a135e71..c0135c929 100644 --- a/web/features/chat/messages/ChatMessageList.tsx +++ b/web/features/chat/messages/ChatMessageList.tsx @@ -591,7 +591,7 @@ export function GeneratedFileCards({ const files = useMemo(() => { const merged = mergeGeneratedFiles(attachments, events); if (!content) return merged; - const unlinked = new Set(unlinkedGeneratedFiles(content, merged)); + const unlinked = new Set(unlinkedGeneratedFiles(content, merged, { imagesOnly: true })); return merged.filter( (file) => !file.mime_type?.startsWith("image/") || unlinked.has(file), ); diff --git a/web/tests/generated-file-cards.spec.tsx b/web/tests/generated-file-cards.spec.tsx index d52d6de31..f28965793 100644 --- a/web/tests/generated-file-cards.spec.tsx +++ b/web/tests/generated-file-cards.spec.tsx @@ -67,3 +67,13 @@ describe("generated image cards", () => { 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(); +});