Merge commit '0cfbaf738093ab1aa2a3f1b5499acc925baa2b0c' into codex/validate-reviewed-integration

This commit is contained in:
Bingxi Zhao (Frank)
2026-10-31 18:52:54 +08:00
3 changed files with 97 additions and 5 deletions

View File

@@ -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<string, unknown>;
makeFileLinkRemarkPlugin(files)?.()(tree);
const linked = new Set<string>();
const visit = (node: Record<string, unknown>): 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(

View File

@@ -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 (
<div className="mt-3 flex flex-col gap-2">
@@ -2605,6 +2616,7 @@ export const ChatMessageList = memo(function ChatMessageList({
<GeneratedFileCards
attachments={msg.attachments ?? []}
events={msg.events}
content={msg.content}
onOpen={onPreviewAttachment}
/>
{(() => {

View File

@@ -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(
<GeneratedFileCards
attachments={[figure, other]}
content="Here it is.\n\n![figure](outputs/chat/turn-a/figure.png)\n\nThe axis shows time."
/>,
);
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(<GeneratedFileCards attachments={[figure, other]} content="No images here." />);
expect(screen.getAllByRole("img")).toHaveLength(2);
});
it("keeps the card when the reply only mentions the path in code", () => {
render(
<GeneratedFileCards
attachments={[figure]}
content={"Saved to:\n\n```\n![figure](outputs/chat/turn-a/figure.png)\n```"}
/>,
);
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(
<GeneratedFileCards
attachments={[report]}
content="See [the report](outputs/chat/turn-a/report.pdf)."
/>,
);
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(<GeneratedFileCards attachments={[figure]} content={content} />);
expect(screen.getByRole("img", { name: "figure.png" })).toBeTruthy();
});