The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.
TypeScript code-editorcross-platformdapdatabase-clientdebuggerdesktop-appdeveloper-toolsgithttp-clientideintellijlsprusttauriterminal
fix(agents): open local file links in Markdown messages (#263)
l0ng-ai97144421d24b
diff --git a/src/features/agents/Markdown.test.tsx b/src/features/agents/Markdown.test.tsxnew file mode 100644index 000000000..faed31748--- /dev/null+++ b/src/features/agents/Markdown.test.tsx@@ -0,0 +1,42 @@+import { fireEvent, render, screen } from "@testing-library/react";+import { beforeEach, expect, it, vi } from "vitest";+import { Markdown } from "./Markdown";++const { openFile, openExternalUrl } = vi.hoisted(() => ({+ openFile: vi.fn().mockResolvedValue(undefined),+ openExternalUrl: vi.fn().mockResolvedValue(undefined),+}));+vi.mock("@/workbench/contributions", () => ({ editorService: () => ({ openFile }) }));+vi.mock("@/platform/openUrl", () => ({ openExternalUrl }));++beforeEach(() => vi.clearAllMocks());++it.each([+ ["/Users/thomas/vmos-shots/tiktok-home.jpg", "/Users/thomas/vmos-shots/tiktok-home.jpg", undefined],+ ["images/home.jpg", "/repo/images/home.jpg", undefined],+ ["/tmp/截图%20one.jpg", "/tmp/截图 one.jpg", undefined],+ ["/repo/main.rs:12:5", "/repo/main.rs", { line: 11, col: 4 }],+ ["/repo/main.rs#L7", "/repo/main.rs", { line: 6, col: 0 }],+])("opens a Markdown file link: %s", (href, path, selection) => {+ render(<Markdown text={`[查看文件](${href})`} cwd="/repo" />);+ fireEvent.click(screen.getByRole("link", { name: "查看文件" }));+ expect(openFile).toHaveBeenCalledWith(path, { focus: true, selection });+ expect(openExternalUrl).not.toHaveBeenCalled();+});++it("keeps web links external", () => {+ render(<Markdown text="[Website](https://example.com/image.jpg)" cwd="/repo" />);+ fireEvent.click(screen.getByRole("link"));+ expect(openExternalUrl).toHaveBeenCalledWith("https://example.com/image.jpg");+ expect(openFile).not.toHaveBeenCalled();+});++it.each(["#section", "//example.com/image.jpg", "javascript:alert(1)", "/tmp/%ZZ.jpg"])(+ "does not open unsupported links as files: %s",+ (href) => {+ const { container } = render(<Markdown text={`[Link](${href})`} cwd="/repo" />);+ fireEvent.click(container.querySelector("a")!);+ expect(openFile).not.toHaveBeenCalled();+ expect(openExternalUrl).not.toHaveBeenCalled();+ },+);diff --git a/src/features/agents/Markdown.tsx b/src/features/agents/Markdown.tsxindex edc2d0b17..fe80c6828 100644--- a/src/features/agents/Markdown.tsx+++ b/src/features/agents/Markdown.tsx@@ -12,7 +12,7 @@ import { tx } from "@/platform/i18n";import { openExternalUrl } from "@/platform/openUrl";import { copyText } from "@/ui";import { editorService } from "@/workbench/contributions";-import { pathLink, type PathLink } from "./conversation";+import { fileHref, pathLink, type PathLink } from "./conversation";import styles from "./Session.module.css";const marked = new Marked({ gfm: true, breaks: true });@@ -131,7 +131,12 @@ export function Markdown({ text, cwd }: { text: string; cwd: string }) {const a = target.closest("a");if (!a) return;e.preventDefault();- if (/^https?:/i.test(a.href)) void openExternalUrl(a.href);+ const href = a.getAttribute("href") ?? "";+ if (/^https?:/i.test(href)) void openExternalUrl(href);+ else {+ const link = fileHref(href, cwd);+ if (link) openPathLink(link);+ }}}dangerouslySetInnerHTML={{ __html: html }}/>diff --git a/src/features/agents/conversation.ts b/src/features/agents/conversation.tsindex b6e357d15..253098e63 100644--- a/src/features/agents/conversation.ts+++ b/src/features/agents/conversation.ts@@ -22,6 +22,25 @@ export interface PathLink {const PATH = /^(?:\.\/)?([\w.@-]+(?:\/[\w.@-]+)*\.[A-Za-z][A-Za-z0-9]*)(?::(\d+)(?::(\d+))?|#L(\d+))?$/;+/** Explicit Markdown links can also name files with spaces or non-ASCII names. */+export function fileHref(href: string, cwd: string): PathLink | null {+ if (!href || href.startsWith("#") || href.startsWith("//") || /^[a-z][a-z\d+.-]*:/i.test(href)) return null;+ let decoded: string;+ try {+ decoded = decodeURIComponent(href);+ } catch {+ return null;+ }+ const match = /^(.*?)(?::(\d+)(?::(\d+))?|#L(\d+))?$/.exec(decoded);+ if (!match?.[1] || [...decoded].some((char) => char.charCodeAt(0) < 32)) return null;+ const path = match[1].startsWith("/") ? match[1] : `${cwd.replace(/\/$/, "")}/${match[1]}`;+ const link: PathLink = { path };+ const line = match[2] ?? match[4];+ if (line) link.line = Math.max(0, Number(line) - 1);+ if (match[3]) link.col = Math.max(0, Number(match[3]) - 1);+ return link;+}+/*** The file an inline code span names (`src/lib.rs`, `src/main.rs:12`, `a/b.ts:3:5`,* `/abs/x.py#L7`), resolved against `cwd`; null for text that isn't a file path