Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(webui): check explicit file links before enabling previews (#6483)
chengyongru69d2fd87b0d1
diff --git a/webui/browser/inline-references.spec.ts b/webui/browser/inline-references.spec.tsindex 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.tsxindex 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<typeof FileReferenceChip>) {+ const path = props.previewPath || props.tooltipPath || props.path;const resolve = useFilePreviewAvailabilityResolver();const [result, setResult] = useState<AvailabilityResult | null>(null);@@ -118,7 +117,7 @@ function InferredFileReferenceChip({);return (<FileReferenceChip- path={path}+ {...props}onOpen={onOpen && resolvedAvailable ? onOpen : undefined}/>);@@ -557,7 +556,7 @@ export default function MarkdownTextRenderer({const raw = String(kids).replace(/\n$/, "");if (!documentPath && isLikelyFilePath(raw)) {return (- <InferredFileReferenceChip+ <AvailableFileReferenceChippath={raw}onOpen={onOpenFilePreview}/>@@ -644,7 +643,7 @@ export default function MarkdownTextRenderer({if (filePath) {const label = nodeText(markdownChildren).trim();return (- <FileReferenceChip+ <AvailableFileReferenceChippath={label || filePath}tooltipPath={filePath}previewPath={filePath}diff --git a/webui/src/tests/markdown-text-renderer.test.tsx b/webui/src/tests/markdown-text-renderer.test.tsxindex 89dad6bfe..622690ce8 100644--- a/webui/src/tests/markdown-text-renderer.test.tsx+++ b/webui/src/tests/markdown-text-renderer.test.tsx@@ -136,20 +136,26 @@ describe("MarkdownTextRenderer", () => {);});- 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(<FilePreviewAvailabilityProvider resolve={resolve}><MarkdownTextRenderer onOpenFilePreview={onOpenFilePreview}>- {"Future file: `notes/missing.md`"}+ {markdown}</MarkdownTextRenderer></FilePreviewAvailabilityProvider>,);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<boolean>((_resolve, reject) => {@@ -167,7 +173,7 @@ describe("MarkdownTextRenderer", () => {render(<FilePreviewAvailabilityProvider resolve={resolve}><MarkdownTextRenderer onOpenFilePreview={onOpenFilePreview}>- {"Unreadable file: `notes/locked.md`"}+ {kind === "inferred" ? "Unreadable file: `notes/locked.md`" : "[Unreadable report](notes/locked.md)"}</MarkdownTextRenderer></FilePreviewAvailabilityProvider>,);@@ -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<boolean>((settle) => {+ resolveAvailability = settle;+ }));render(<FilePreviewAvailabilityProvider resolve={resolve}><MarkdownTextRenderer onOpenFilePreview={onOpenFilePreview}>- {"Existing file: `notes/ready.md`"}+ {kind === "inferred" ? "Existing file: `notes/ready.md`" : "[Existing report](notes/ready.md)"}</MarkdownTextRenderer></FilePreviewAvailabilityProvider>,);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.tsxindex 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 () => {