Skip to content

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

fix(chat): keep URL suffix punctuation outside links

yuxino322cbb01a884
diff --git a/apps/desktop/src/lib/chat-links.ts b/apps/desktop/src/lib/chat-links.ts
index fdfb8e2a0..fc8f10904 100644
--- a/apps/desktop/src/lib/chat-links.ts
+++ b/apps/desktop/src/lib/chat-links.ts
@@ -245,20 +245,24 @@ 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;
+/** Scan once, keeping URL parentheses but stopping at a closing prose wrapper. */
+function scanUrl(text: string, start: number): string {
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);
+ let end = start;
+ for (; end < text.length; end += 1) {
+ const character = text[end];
+ if (/[\s<>"'[\]{}]/u.test(character)) break;
+ if (character === "(") depth += 1;
+ else if (character === ")") {
+ if (depth === 0) break;
depth -= 1;
}
}
- return token;
+ // Punctuation after a parenthesized destination belongs to the sentence.
+ // Preserve extensions such as "(draft).html" and existing non-parenthesized URLs.
+ return text.slice(start, end).replace(/(?<=\))[.,!?;:,。!?;:]+$/u, "");
}
/**
@@ -275,8 +279,10 @@ export function splitChatText(
let last = 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;
+ const raw = /^https?:\/\//i.test(match[0])
+ ? scanUrl(text, start)
+ : match[0];
scanner.lastIndex = start + raw.length;
const target = resolvePreviewTarget(raw, root, baseDir);
if (!target) continue;
diff --git a/apps/desktop/test/chat-links.test.mjs b/apps/desktop/test/chat-links.test.mjs
index 05b0c8461..3a079dc10 100644
--- a/apps/desktop/test/chat-links.test.mjs
+++ b/apps/desktop/test/chat-links.test.mjs
@@ -322,3 +322,24 @@ test("a prose wrapper does not swallow the next URL or file reference", () => {
]);
assert.equal(segments.map(s => s.text).join(""), source);
});
+
+
+test("sentence punctuation after a parenthesized URL stays outside the link", () => {
+ const url = "https://en.wikipedia.org/wiki/React_(software)";
+ for (const suffix of [".", ",", "!", "?", ";", ":", "。", ",", "!", "?", "..."]) {
+ const source = `See ${url}${suffix}`;
+ const segments = splitChatText(source, ROOT);
+ assert.equal(segments.find(s => s.kind === "target").target.url, url);
+ assert.equal(segments.at(-1).text, suffix);
+ assert.equal(segments.map(s => s.text).join(""), source);
+ }
+ const urlWithExtension = "https://example.com/report_(draft).html?q=(one)#part(2)";
+ assert.equal(splitChatText(urlWithExtension, ROOT)[0].target.url, urlWithExtension);
+});
+
+test("adjacent parenthesis-wrapped URLs all remain independently linkable", () => {
+ const source = "(https://example.com)".repeat(1000);
+ const segments = splitChatText(source, ROOT);
+ assert.equal(segments.filter(s => s.kind === "target").length, 1000);
+ 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 a75fc05d9..9a50d428c 100644
--- a/docs/spec/04-ux/08-component-spec.md
+++ b/docs/spec/04-ux/08-component-spec.md
@@ -1667,7 +1667,9 @@ Single message render — either user (plaintext) or assistant (markdown streami
is no longer what this click does.
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
+ the URL in prose stays outside the link. Sentence punctuation immediately
+ after a closing URL parenthesis stays outside as well; suffixes such as
+ `(draft).html` remain part of the URL. 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 69bfede70..350f9a23b 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -14179,7 +14179,7 @@ the latest destination. These assertions measure work counts, not device FPS.
#### 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.
+- **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, followed by a sentence-ending period, and followed immediately by another link or file reference.
+- **Expected**: The complete URL, including `(software)`, opens the React software article. The outer prose closing parenthesis and sentence-ending punctuation after the URL parenthesis are not part of the link. Adjacent references remain independently clickable. 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 c42e7ed71..bf720f1be 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 保留路径、查询和片段中成对的圆括号,正文包裹网址时多出的右括号不属于链接,在侧边浏览器打开;长 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 右括号的句末标点也不属于链接,但 `(draft).html` 这样的后缀仍保留,在侧边浏览器打开;长 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 47d7b919b..c5741b122 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
@@ -8374,7 +8374,7 @@ the latest destination. These assertions measure work counts, not device FPS.
#### E2E-CHAT-parenthesized-url:用户消息中的完整网址
-- **步骤**:在用户消息中发送 `https://en.wikipedia.org/wiki/React_(software)` 并点击链接,再验证正文用圆括号包裹该网址的情况。
-- **预期**:打开包含 `(software)` 的完整网址,进入 React 软件词条;正文外层的右括号不属于链接。嵌套圆括号、查询和片段内的圆括号、百分号编码的圆括号均保持完整。
+- **步骤**:在用户消息中发送 `https://en.wikipedia.org/wiki/React_(software)` 并点击链接,再验证正文用圆括号包裹该网址、网址后跟句号及紧接另一链接或文件引用的情况。
+- **预期**:打开包含 `(software)` 的完整网址,进入 React 软件词条;正文外层的右括号和紧跟 URL 右括号的句末标点不属于链接,相邻引用仍能独立点击。嵌套圆括号、查询和片段内的圆括号、百分号编码的圆括号均保持完整。
- **覆盖**:`chat-links.test.mjs`;桌面端通过正常浏览器目标实际点击验证。
- **链接规格**:`04-ux/08-component-spec.md` §8.3。