From 69d2fd87b0d1dcbc524ed14351045caf59299d3c Mon Sep 17 00:00:00 2001 From: chengyongru <61816729+chengyongru@users.noreply.github.com> Date: Tue, 27 Oct 2026 09:40:44 +0800 Subject: [PATCH] fix(webui): check explicit file links before enabling previews (#6483) --- webui/browser/inline-references.spec.ts | 1 + webui/src/components/MarkdownTextRenderer.tsx | 17 +++++----- .../src/tests/markdown-text-renderer.test.tsx | 31 +++++++++++++------ webui/src/tests/thread-shell.test.tsx | 9 ++++-- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/webui/browser/inline-references.spec.ts b/webui/browser/inline-references.spec.ts index 794aa9b38..e9ca9c2b0 100644 --- a/webui/browser/inline-references.spec.ts +++ b/webui/browser/inline-references.spec.ts @@ -157,6 +157,7 @@ for (const narrow of [false, true]) { await expect(sentPrefix).toHaveCSS("font-weight", "400"); const sentCommand = page.locator("[data-user-text-bubble]").last().locator("p"); expect(await sentCommand.textContent()).toBe(shellCommand.trim()); + await sentCommand.click(); await sentCommand.evaluate(node => { const range = document.createRange(); range.selectNodeContents(node); diff --git a/webui/src/components/MarkdownTextRenderer.tsx b/webui/src/components/MarkdownTextRenderer.tsx index 5f7012602..e0aa75e07 100644 --- a/webui/src/components/MarkdownTextRenderer.tsx +++ b/webui/src/components/MarkdownTextRenderer.tsx @@ -4,6 +4,7 @@ import { useEffect, useMemo, useState, + type ComponentProps, type ReactNode, } from "react"; import { useTranslation } from "react-i18next"; @@ -86,13 +87,11 @@ type AvailabilityResult = { resolve: FilePreviewAvailabilityResolver; }; -function InferredFileReferenceChip({ - path, +function AvailableFileReferenceChip({ onOpen, -}: { - path: string; - onOpen?: (path: string) => void; -}) { + ...props +}: ComponentProps) { + const path = props.previewPath || props.tooltipPath || props.path; const resolve = useFilePreviewAvailabilityResolver(); const [result, setResult] = useState(null); @@ -118,7 +117,7 @@ function InferredFileReferenceChip({ ); return ( ); @@ -557,7 +556,7 @@ export default function MarkdownTextRenderer({ const raw = String(kids).replace(/\n$/, ""); if (!documentPath && isLikelyFilePath(raw)) { return ( - @@ -644,7 +643,7 @@ export default function MarkdownTextRenderer({ if (filePath) { const label = nodeText(markdownChildren).trim(); return ( - { ); }); - it("keeps unavailable inferred inline file paths non-interactive", async () => { + it.each([ + { kind: "inferred", markdown: "Future file: `notes/missing.md`", path: "notes/missing.md", label: "missing.md" }, + { kind: "explicit", markdown: "[Future report](notes/missing.md)", path: "notes/missing.md", label: "Future report" }, + { kind: "malformed explicit", markdown: "[Python code](/home/example/workspace/model_check.py)", path: "/home/example/workspace/model_check.py", label: "Python code" }, + ])("keeps unavailable $kind file references non-interactive", async ({ markdown, path, label }) => { const onOpenFilePreview = vi.fn(); const resolve = vi.fn().mockResolvedValue(false); render( - {"Future file: `notes/missing.md`"} + {markdown} , ); const reference = screen.getByTestId("inline-file-path"); - expect(reference).toHaveTextContent("missing.md"); - await waitFor(() => expect(resolve).toHaveBeenCalledWith("notes/missing.md")); + expect(reference).toHaveTextContent(label); + expect(reference).not.toHaveAttribute("role"); + expect(reference).toHaveClass("text-muted-foreground"); + await waitFor(() => expect(resolve).toHaveBeenCalledWith(path)); expect(reference).not.toHaveAttribute("role"); expect(reference).not.toHaveAttribute("tabindex"); @@ -158,7 +164,7 @@ describe("MarkdownTextRenderer", () => { expect(onOpenFilePreview).not.toHaveBeenCalled(); }); - it("keeps inferred inline file paths non-interactive when availability lookup fails", async () => { + it.each(["inferred", "explicit"])("keeps %s file references non-interactive when availability lookup fails", async (kind) => { const onOpenFilePreview = vi.fn(); let rejectAvailability!: (reason?: unknown) => void; const resolve = vi.fn(() => new Promise((_resolve, reject) => { @@ -167,7 +173,7 @@ describe("MarkdownTextRenderer", () => { render( - {"Unreadable file: `notes/locked.md`"} + {kind === "inferred" ? "Unreadable file: `notes/locked.md`" : "[Unreadable report](notes/locked.md)"} , ); @@ -185,18 +191,25 @@ describe("MarkdownTextRenderer", () => { expect(onOpenFilePreview).not.toHaveBeenCalled(); }); - it("makes available inferred inline file paths previewable", async () => { + it.each(["inferred", "explicit"])("makes available %s file references previewable after the probe succeeds", async (kind) => { const onOpenFilePreview = vi.fn(); - const resolve = vi.fn().mockResolvedValue(true); + let resolveAvailability!: (available: boolean) => void; + const resolve = vi.fn(() => new Promise((settle) => { + resolveAvailability = settle; + })); render( - {"Existing file: `notes/ready.md`"} + {kind === "inferred" ? "Existing file: `notes/ready.md`" : "[Existing report](notes/ready.md)"} , ); const reference = screen.getByTestId("inline-file-path"); + expect(reference).not.toHaveAttribute("role"); + expect(reference).toHaveClass("text-muted-foreground"); + await waitFor(() => expect(resolve).toHaveBeenCalledWith("notes/ready.md")); + await act(async () => resolveAvailability(true)); await waitFor(() => expect(reference).toHaveAttribute("role", "button")); expect(reference).toHaveAttribute("tabindex", "0"); diff --git a/webui/src/tests/thread-shell.test.tsx b/webui/src/tests/thread-shell.test.tsx index 78945ef39..5478d5a34 100644 --- a/webui/src/tests/thread-shell.test.tsx +++ b/webui/src/tests/thread-shell.test.tsx @@ -1112,7 +1112,10 @@ describe("ThreadShell", () => { portal.remove(); }); - it("keeps inferred file paths non-interactive when the availability probe fails", async () => { + it.each([ + { kind: "inferred", content: "Unreadable file: `prompts/dream.md`", path: "prompts/dream.md" }, + { kind: "explicit", content: "[Python code](/home/example/workspace/model_check.py)", path: "/home/example/workspace/model_check.py" }, + ])("keeps $kind file references non-interactive when the availability probe fails", async ({ content, path }) => { await preloadMarkdownText(); const client = makeClient(); let resolveProbe!: (value: Response) => void; @@ -1123,7 +1126,7 @@ describe("ThreadShell", () => { const url = String(input); if (url.includes("websocket%3Apreview-error/webui-thread")) { return Promise.resolve(httpJson(transcriptFromSimpleMessages([ - { role: "assistant", content: "Unreadable file: `prompts/dream.md`" }, + { role: "assistant", content }, ]))); } if (url.includes("websocket%3Apreview-error/file-preview?")) return probe; @@ -1146,7 +1149,7 @@ describe("ThreadShell", () => { const reference = await screen.findByTestId("inline-file-path"); await waitFor(() => expect(fetchMock).toHaveBeenCalledWith( - expect.stringContaining("file-preview?path=prompts%2Fdream.md&probe=1"), + expect.stringContaining(`file-preview?path=${encodeURIComponent(path)}&probe=1`), expect.anything(), )); await act(async () => {