Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(chat): preserve parentheses in plain URL links

yuxino19aeb4dcff4b
diff --git a/apps/desktop/src/lib/chat-links.ts b/apps/desktop/src/lib/chat-links.ts
index 4cee3cccf..fdfb8e2a0 100644
--- a/apps/desktop/src/lib/chat-links.ts
+++ b/apps/desktop/src/lib/chat-links.ts
@@ -245,7 +245,21 @@ export type ChatTextSegment =
// uses `(?![A-Za-z0-9_])` rather than `\b`: in unicode mode `\b` treats CJK
// letters as word characters, which would stop `App.tsx文件` from linking.
const SCAN_RE =
- /@"[^"\n]+"|@[^\s]+|https?:\/\/[^\s<>"'()[\]{}]+|(?:~\/)?\/?\.{1,2}\/(?:[\p{L}\p{N}_@+.-]+\/)*[\p{L}\p{N}_@+.-]+(?::\d+(?::\d+)?)?|(?:~\/)?\/?(?:[\p{L}\p{N}_@+.-]+\/)+[\p{L}\p{N}_@+.-]+(?::\d+(?::\d+)?)?|[\p{L}\p{N}_@+-][\p{L}\p{N}_@+.-]*\.[A-Za-z0-9]{1,8}(?![A-Za-z0-9_])/gu;
+ /@"[^"\n]+"|@[^\s]+|https?:\/\/[^\s<>"'[\]{}]+|(?:~\/)?\/?\.{1,2}\/(?:[\p{L}\p{N}_@+.-]+\/)*[\p{L}\p{N}_@+.-]+(?::\d+(?::\d+)?)?|(?:~\/)?\/?(?:[\p{L}\p{N}_@+.-]+\/)+[\p{L}\p{N}_@+.-]+(?::\d+(?::\d+)?)?|[\p{L}\p{N}_@+-][\p{L}\p{N}_@+.-]*\.[A-Za-z0-9]{1,8}(?![A-Za-z0-9_])/gu;
+
+/** Keep URL parentheses, but stop before a closing prose wrapper. */
+function trimUrlWrapper(token: string): string {
+ if (!/^https?:\/\//i.test(token)) return token;
+ let depth = 0;
+ for (let index = 0; index < token.length; index += 1) {
+ if (token[index] === "(") depth += 1;
+ else if (token[index] === ")") {
+ if (depth === 0) return token.slice(0, index);
+ depth -= 1;
+ }
+ }
+ return token;
+}
/**
* Split plain chat text (user messages) into literal runs and previewable
@@ -259,9 +273,11 @@ export function splitChatText(
): ChatTextSegment[] {
const segments: ChatTextSegment[] = [];
let last = 0;
- for (const match of text.matchAll(SCAN_RE)) {
- const raw = match[0];
- const start = match.index ?? 0;
+ const scanner = new RegExp(SCAN_RE);
+ for (let match = scanner.exec(text); match; match = scanner.exec(text)) {
+ const raw = trimUrlWrapper(match[0]);
+ const start = match.index;
+ scanner.lastIndex = start + raw.length;
const target = resolvePreviewTarget(raw, root, baseDir);
if (!target) continue;
if (start > last) segments.push({ kind: "text", text: text.slice(last, start) });
diff --git a/apps/desktop/test/chat-links.test.mjs b/apps/desktop/test/chat-links.test.mjs
index 7352036a1..05b0c8461 100644
--- a/apps/desktop/test/chat-links.test.mjs
+++ b/apps/desktop/test/chat-links.test.mjs
@@ -274,3 +274,51 @@ test("an ascii filename followed by cjk prose still linkifies", () => {
["App.tsx"],
);
});
+
+
+test("splitChatText preserves parentheses inside HTTP URLs", () => {
+ for (const url of [
+ "https://en.wikipedia.org/wiki/React_(software)",
+ "https://example.com/a_(b_(c))/details?q=(one)&next=two#part(3)",
+ "https://example.com/React_%28software%29",
+ ]) {
+ const segments = splitChatText(url, ROOT);
+ assert.equal(segments.length, 1);
+ assert.deepEqual(segments[0].target, { kind: "url", url });
+ assert.equal(segments[0].text, url);
+ }
+});
+
+test("splitChatText keeps prose closing parentheses outside URL links", () => {
+ for (const url of ["https://example.com", "https://en.wikipedia.org/wiki/React_(software)"]) {
+ for (const suffix of [")", ")).", "), next"]) {
+ const source = `See (${url}${suffix}`;
+ const segments = splitChatText(source, ROOT);
+ assert.deepEqual(segments.filter(s => s.kind === "target").map(s => s.target), [{ kind: "url", url }]);
+ assert.equal(segments.map(s => s.text).join(""), source);
+ assert.equal(segments.at(-1).text, suffix);
+ }
+ }
+});
+
+test("markdown bare-link rewriting preserves parenthesized URL destinations", () => {
+ const url = "https://en.wikipedia.org/wiki/React_(software)";
+ const tree = { type: "root", children: [{ type: "paragraph", children: [{ type: "text", value: `See (${url}).` }] }] };
+ linkifyMdastTree(tree, ROOT);
+ const nodes = tree.children[0].children;
+ assert.equal(nodes.find(node => node.type === "link").url, url);
+ assert.equal(nodes.at(-1).value, ").");
+});
+
+
+test("a prose wrapper does not swallow the next URL or file reference", () => {
+ const source = "(https://example.com)src/a.ts (https://example.org)https://example.net";
+ const segments = splitChatText(source, ROOT);
+ assert.deepEqual(segments.filter(s => s.kind === "target").map(s => s.target), [
+ { kind: "url", url: "https://example.com" },
+ { kind: "file", path: "src/a.ts" },
+ { kind: "url", url: "https://example.org" },
+ { kind: "url", url: "https://example.net" },
+ ]);
+ assert.equal(segments.map(s => s.text).join(""), source);
+});
diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.md
index af36530af..a75fc05d9 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -1665,7 +1665,9 @@ Single message render — either user (plaintext) or assistant (markdown streami
image thumbnail resolves and opens the same way. A chip whose reference
matches nothing opens nothing and reports itself; the OS default application
is no longer what this click does.
- HTTP(S) URLs remain inline text links. Plain clicks — including markdown
+ HTTP(S) URLs remain inline text links. Bare URLs preserve balanced parentheses
+ in paths, queries, and fragments; an unmatched closing parenthesis wrapping
+ the URL in prose stays outside the link. Plain clicks — including markdown
links, autolinked URLs, inline-code URLs, and remote images — follow the
persisted Link open destination setting (Work panel browser by default, or
the system default browser). Right-clicking a link opens a body-level
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index f04b76efc..69bfede70 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -14175,3 +14175,11 @@ the latest destination. These assertions measure work counts, not device FPS.
- **Milestone**: Maintenance.
- **Status**: Covered by the existing HTTP client integration fixture and a
focused component-render validation; no live IDA process required.
+
+
+#### E2E-CHAT-parenthesized-url: Complete URLs in user messages
+
+- **Steps**: Send `https://en.wikipedia.org/wiki/React_(software)` in a user message and click its link. Repeat with the URL wrapped in prose parentheses.
+- **Expected**: The complete URL, including `(software)`, opens the React software article. The outer prose closing parenthesis is not part of the link. Nested parentheses, query/fragment parentheses and percent-encoded parentheses remain intact.
+- **Coverage**: `chat-links.test.mjs`; native desktop click-through with the normal browser destination.
+- **Specs linked**: `04-ux/08-component-spec.md` §8.3.
diff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.md
index 4b25ea117..c42e7ed71 100644
--- a/docs/zh-CN/spec/04-ux/08-component-spec.md
+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md
@@ -1206,7 +1206,7 @@ row 而不是在转录本中添加树镶边。
聊天轮流而不是全角块。气泡按提示的 max-content 收缩,而不是撑到该上限;内联文件芯片使用确定的 240px 名称上限,因此粘贴文件加上短提示仍保持紧凑。用户正文是明文
保留硬换行符(`white-space: pre-wrap`);
仅应用 trailing/leading 输入框修剪,从不应用内部换行符
- 崩溃。序列化的 `@path` 文件引用画成与输入框一致的叶子名芯片(图标 + 省略号文件名;规范路径放在工具提示和无障碍名称里)。未内联成 `@path` 芯片的图片附件渲染为有界缩略图(`fs/readImageDataUrl` 的 data URL);无法解析时保留芯片。消息正文里的裸路径 token 识别 Unicode 字母与数字。在用户消息正文中,它们只是候选:现有的 `fs/resolveRef` 查找确认是真实文件后才显示芯片。等待、未找到或失败的查找保留原文,包括 `使用llama.cpp`。显式 `@path` 引用和结构化附件保留现有芯片,不做猜测查询。每条消息最多检查 32 个唯一候选,可见行之间最多 4 个并发查找;其余候选保持纯文本。确认结果绑定到当前消息文本、工作区路径和会话;任一变化都会丢弃旧结果并取消排队的工作。之后新建的文件在消息重新挂载或作用域变化时再考虑,不轮询。非 ASCII 文件名仍受支持。绝对路径与 `~/` 整体匹配,工作区之外(以及一切家目录路径)保持纯文本,而不是渲染一个永远打不开的芯片——包含边界不变(D322)。点击芯片先经 `pi-desktop/fs/resolveRef` 补全引用——该通道搜索整个打开的项目,按项目组文件夹顺序、主文件夹优先(ADR 0263)——再按解析结果打开:项目文件在随应用打包的 `pi.file-manager` 工作面板视图中打开(该视图不可用时退回宿主 `file:` 选项卡),会话临时目录或附件文件在宿主 `file:` 选项卡中打开,主文件夹中的 `.html`/`.htm` 仍由侧边浏览器打开。交给该视图的地址跟随应答的文件夹:主文件夹中的文件用项目内相对路径传递,同一项目的同级文件夹中的文件用绝对路径传递,与会话临时目录和附件文件一致。已解析的图片缩略图按同样规则打开。什么都没匹配到的芯片不打开任何东西,而是自己报告出来;系统默认应用不再由这次点击触发。HTTP(S) URL 仍是内联文本链接,在侧边浏览器打开;长 URL 在板内换行并保持逻辑开始对齐,而不是继承浏览器的居中按钮文本。
+ 崩溃。序列化的 `@path` 文件引用画成与输入框一致的叶子名芯片(图标 + 省略号文件名;规范路径放在工具提示和无障碍名称里)。未内联成 `@path` 芯片的图片附件渲染为有界缩略图(`fs/readImageDataUrl` 的 data URL);无法解析时保留芯片。消息正文里的裸路径 token 识别 Unicode 字母与数字。在用户消息正文中,它们只是候选:现有的 `fs/resolveRef` 查找确认是真实文件后才显示芯片。等待、未找到或失败的查找保留原文,包括 `使用llama.cpp`。显式 `@path` 引用和结构化附件保留现有芯片,不做猜测查询。每条消息最多检查 32 个唯一候选,可见行之间最多 4 个并发查找;其余候选保持纯文本。确认结果绑定到当前消息文本、工作区路径和会话;任一变化都会丢弃旧结果并取消排队的工作。之后新建的文件在消息重新挂载或作用域变化时再考虑,不轮询。非 ASCII 文件名仍受支持。绝对路径与 `~/` 整体匹配,工作区之外(以及一切家目录路径)保持纯文本,而不是渲染一个永远打不开的芯片——包含边界不变(D322)。点击芯片先经 `pi-desktop/fs/resolveRef` 补全引用——该通道搜索整个打开的项目,按项目组文件夹顺序、主文件夹优先(ADR 0263)——再按解析结果打开:项目文件在随应用打包的 `pi.file-manager` 工作面板视图中打开(该视图不可用时退回宿主 `file:` 选项卡),会话临时目录或附件文件在宿主 `file:` 选项卡中打开,主文件夹中的 `.html`/`.htm` 仍由侧边浏览器打开。交给该视图的地址跟随应答的文件夹:主文件夹中的文件用项目内相对路径传递,同一项目的同级文件夹中的文件用绝对路径传递,与会话临时目录和附件文件一致。已解析的图片缩略图按同样规则打开。什么都没匹配到的芯片不打开任何东西,而是自己报告出来;系统默认应用不再由这次点击触发。HTTP(S) URL 仍是内联文本链接;裸 URL 保留路径、查询和片段中成对的圆括号,正文包裹网址时多出的右括号不属于链接,在侧边浏览器打开;长 URL 在板内换行并保持逻辑开始对齐,而不是继承浏览器的居中按钮文本。
- Assistant:透明表面,左对齐,Markdown 完全渲染
内容宽度。该 Markdown 里的工作区文件路径可预览:行内代码、Markdown 链接与裸路径
token(带已知扩展名)都像芯片一样,在打开项目的每个文件夹里先补全再打开(ADR 0263)——
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index 4e4e4cd1d..47d7b919b 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -8370,3 +8370,11 @@ the latest destination. These assertions measure work counts, not device FPS.
- **自动化:** `apps/desktop/test/plugin-services.test.mjs` 真实 fork 宿主进程、以夹具退出码杀死它,并断言服务状态与审计记录上的
退出码及原始 stderr 缺失;`plugin-isolation.test.mjs` 与关闭用例覆盖"退出不是崩溃"那一半。
- **状态:** 运行时层已自动化;无 UI 驱动读取插件页的错误文本。
+
+
+#### E2E-CHAT-parenthesized-url:用户消息中的完整网址
+
+- **步骤**:在用户消息中发送 `https://en.wikipedia.org/wiki/React_(software)` 并点击链接,再验证正文用圆括号包裹该网址的情况。
+- **预期**:打开包含 `(software)` 的完整网址,进入 React 软件词条;正文外层的右括号不属于链接。嵌套圆括号、查询和片段内的圆括号、百分号编码的圆括号均保持完整。
+- **覆盖**:`chat-links.test.mjs`;桌面端通过正常浏览器目标实际点击验证。
+- **链接规格**:`04-ux/08-component-spec.md` §8.3。