Skip to content

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

fix(desktop): preserve complete chat file references

cauliflower7008c7827eb3
diff --git a/apps/desktop/src/lib/chat-links.ts b/apps/desktop/src/lib/chat-links.ts
index ad355a1ed..47e932076 100644
--- a/apps/desktop/src/lib/chat-links.ts
+++ b/apps/desktop/src/lib/chat-links.ts
@@ -62,13 +62,14 @@ function isLikelyFilePath(path: string): boolean {
const base = normalized.split("/").pop() ?? "";
const dotIndex = base.lastIndexOf(".");
const ext = dotIndex > 0 ? base.slice(dotIndex + 1).toLowerCase() : "";
+ const baseExt = ext.replace(/[+@-][\p{L}\p{N}_@+-]*$/u, "");
if (normalized.includes("/")) {
- if (ext && ext.length <= 8) return true;
+ if (baseExt && baseExt.length <= 8) return true;
if (KNOWN_BARE_NAMES.has(base)) return true;
return false;
}
if (KNOWN_BARE_NAMES.has(base)) return true;
- return KNOWN_EXTS.has(ext);
+ return KNOWN_EXTS.has(baseExt);
}
function isAbsoluteFilePath(path: string): boolean {
@@ -250,14 +251,18 @@ export type ChatTextSegment =
// the extension remains outside the link (Unicode `\b` cannot express that).
const PATH_WORD = String.raw`[\p{L}\p{N}_@+.-]+`;
const PATH_SEGMENT = String.raw`[\p{L}\p{N}_@+. -]+`;
-const FILE_END = String.raw`\.[A-Za-z0-9]{1,8}(?::\d+(?::\d+)?)?(?![A-Za-z0-9_]|\.[A-Za-z0-9])`;
-const FILE_NAME = String.raw`${PATH_SEGMENT}?${FILE_END}`;
+// Only adjacent characters can extend a dotted directory. Whitespace after an
+// extension is ambiguous with prose; quote such paths with @"...".
+const PATH_CONTINUATION = String.raw`[\p{L}\p{N}_@+.-]*[\\/]`;
+const FILE_END = String.raw`\.[A-Za-z0-9]{1,8}(?:[+@-][\p{L}\p{N}_@+-]*)?(?::\d+(?::\d+)?)?(?![A-Za-z0-9_@+-]|\.[A-Za-z0-9]|${PATH_CONTINUATION})`;
+const BARE_FILE_END = String.raw`(?:${[...KNOWN_BARE_NAMES].join("|")})(?![\p{L}\p{N}_@+.-]|${PATH_CONTINUATION})`;
+const FILE_NAME = String.raw`(?:${PATH_SEGMENT}?${FILE_END}|${BARE_FILE_END})`;
const UNC_PREFIX = String.raw`(?:\\\\[^\\/\s]+[\\/]|\/\/[^/\s]+\/)`;
const SPACED_START = String.raw`(?<![\p{L}\p{N}_@+.-])[A-Za-z][\p{L}\p{N}_+-]*(?: [\p{L}\p{N}_+-]+)+`;
// Unmarked first-segment spaces cannot be distinguished from prose. Retry
// after common introducers so ordinary bare file links still work.
const PROSE_INTRODUCERS = new Set([
- "a", "an", "the", "and", "or", "i", "is", "this", "please",
+ "a", "an", "the", "and", "or", "plus", "because", "with", "then", "against", "i", "is", "this", "please",
"see", "open", "read", "view", "check", "show", "find", "edit",
"update", "fix", "inspect", "compare", "review", "use", "add", "remove",
"write", "create", "created", "delete", "rename", "move", "copy",
diff --git a/apps/desktop/test/chat-links.test.mjs b/apps/desktop/test/chat-links.test.mjs
index c48ead3e2..7846e6616 100644
--- a/apps/desktop/test/chat-links.test.mjs
+++ b/apps/desktop/test/chat-links.test.mjs
@@ -36,8 +36,14 @@ test("parseFileRef accepts bare names only with known extensions", () => {
assert.equal(parseFileRef("README.md"), "README.md");
assert.equal(parseFileRef("package.json"), "package.json");
assert.equal(parseFileRef("Makefile"), "Makefile");
+ assert.equal(parseFileRef("report.md+copy"), "report.md+copy");
+ assert.equal(parseFileRef("report.md@draft"), "report.md@draft");
+ assert.equal(parseFileRef("report.md-v2"), "report.md-v2");
+ assert.equal(parseFileRef("docs/report.md+long-version"), "docs/report.md+long-version");
+ assert.equal(parseFileRef("docs/报告.中文"), "docs/报告.中文");
// dotted identifiers in prose stay plain
assert.equal(parseFileRef("store.messages"), null);
+ assert.equal(parseFileRef("store.messages+copy"), null);
assert.equal(parseFileRef("useAppStore.getState"), null);
assert.equal(parseFileRef("i.e."), null);
});
@@ -191,6 +197,123 @@ test("multi-dot absolute paths keep their final extension", () => {
);
});
+test("suffix characters after extensions stay in path links", () => {
+ for (const path of [
+ "report.md+copy",
+ "report.md@draft",
+ "report.md-v2",
+ "/tmp/report.md+copy",
+ "/tmp/report.md+",
+ "/tmp/report.md@draft",
+ "docs/report.md-v2",
+ "C:/repo/report.md+copy",
+ "C:\\repo\\report.md@draft",
+ "\\\\server\\share\\report.md+copy",
+ ]) {
+ const source = `Open ${path} now`;
+ const segments = splitChatText(source, ROOT);
+ assert.deepEqual(
+ segments.filter((segment) => segment.kind === "target").map((segment) => segment.text),
+ [path],
+ path,
+ );
+ assert.equal(segments.map((segment) => segment.text).join(""), source);
+ assert.deepEqual(resolvePreviewTarget(path, ROOT), { kind: "file", path });
+ }
+ assert.deepEqual(
+ splitChatText('@"report.md@draft"', ROOT)
+ .filter((segment) => segment.kind === "target")
+ .map((segment) => segment.target),
+ [{ kind: "file", path: "report.md@draft" }],
+ );
+ assert.deepEqual(splitChatText("Open store.messages+copy", ROOT), [
+ { kind: "text", text: "Open store.messages+copy" },
+ ]);
+});
+
+test("dotted directories do not split absolute file paths", () => {
+ for (const path of [
+ "/tmp/archive.md/report.md",
+ "/tmp/archive.md备份/report.md",
+ "/tmp/archive.md+copy/report.md",
+ "C:/repo.v1/docs/page.md",
+ "C:\\repo.v1\\docs\\page.md",
+ "\\\\server\\share.v1\\page.md",
+ "//server/share.v1/page.md",
+ ]) {
+ const source = `Open ${path} now`;
+ const segments = splitChatText(source, ROOT);
+ assert.deepEqual(
+ segments.filter((segment) => segment.kind === "target").map((segment) => segment.text),
+ [path],
+ path,
+ );
+ assert.equal(segments.map((segment) => segment.text).join(""), source);
+ }
+});
+
+test("quoted refs preserve ambiguous spaced dotted directories", () => {
+ for (const path of [
+ "/tmp/archive.md backup/report.md",
+ "/tmp/archive.md backup files/report.md",
+ "C:/repo.v1 backup/docs/page.md",
+ "C:\\repo.v1 backup\\docs\\page.md",
+ "\\\\server\\share.v1 backup\\page.md",
+ ]) {
+ const targets = splitChatText(`@"${path}"`, ROOT)
+ .filter((segment) => segment.kind === "target")
+ .map((segment) => segment.target);
+ assert.deepEqual(targets, [{ kind: "file", path }], path);
+ }
+ const ambiguous = "Open /tmp/archive.md backup/report.md";
+ const segments = splitChatText(ambiguous, ROOT);
+ assert.deepEqual(
+ segments.filter((segment) => segment.kind === "target").map((segment) => segment.text),
+ ["/tmp/archive.md", "backup/report.md"],
+ );
+ assert.equal(segments.map((segment) => segment.text).join(""), ambiguous);
+});
+
+test("prose between separate paths stays outside both links", () => {
+ for (const [source, expected] of [
+ ["Open /tmp/first.md and /tmp/second.md", ["/tmp/first.md", "/tmp/second.md"]],
+ ["Open /tmp/first.md and docs/second.md", ["/tmp/first.md", "docs/second.md"]],
+ ["Open C:/repo/first.md or C:/repo/second.md", ["C:/repo/first.md", "C:/repo/second.md"]],
+ ["Open /tmp/first.md plus docs/second.md", ["/tmp/first.md", "docs/second.md"]],
+ ["Open /tmp/first.md because docs/second.md", ["/tmp/first.md", "docs/second.md"]],
+ ["Open /tmp/first.md with docs/second.md", ["/tmp/first.md", "docs/second.md"]],
+ ["Open docs/a.md then docs/b.md", ["docs/a.md", "docs/b.md"]],
+ ["Open docs/a.md against docs/b.md", ["docs/a.md", "docs/b.md"]],
+ ["Open /tmp/a.md, then docs/b.md", ["/tmp/a.md", "docs/b.md"]],
+ ["打开 /tmp/first.md 与 docs/second.md", ["/tmp/first.md", "docs/second.md"]],
+ ["打开 /tmp/App.tsx文件 docs/second.md", ["/tmp/App.tsx", "docs/second.md"]],
+ ]) {
+ const targets = splitChatText(source, ROOT)
+ .filter((segment) => segment.kind === "target")
+ .map((segment) => segment.text);
+ assert.deepEqual(targets, expected, source);
+ assert.equal(splitChatText(source, ROOT).map((segment) => segment.text).join(""), source);
+ }
+});
+
+test("spaced absolute paths keep known extensionless filenames", () => {
+ for (const path of [
+ "/tmp/my project/Makefile",
+ "C:/my project/Makefile",
+ "C:\\my project\\Dockerfile",
+ "\\\\server\\my share\\LICENSE",
+ ]) {
+ const source = `Open ${path} now`;
+ const segments = splitChatText(source, ROOT);
+ assert.deepEqual(
+ segments.filter((segment) => segment.kind === "target").map((segment) => segment.text),
+ [path],
+ path,
+ );
+ assert.equal(segments.map((segment) => segment.text).join(""), source);
+ }
+});
+
test("unmarked first-segment spaces stay plain; explicit refs stay whole", () => {
for (const path of ["my project/page.md", "my project/sub/page.md", "My Project/page.md", "my new project/page.md", "test project/page.md"]) {
const source = `Open ${path} now`;
@@ -225,6 +348,18 @@ test("unmarked first-segment spaces stay plain; explicit refs stay whole", () =>
assert.deepEqual(splitChatText("打开 my project/page.md", ROOT), [
{ kind: "text", text: "打开 my project/page.md" },
]);
+ assert.deepEqual(
+ splitChatText("Open docs/a.md and my project/page.md", ROOT)
+ .filter((segment) => segment.kind === "target")
+ .map((segment) => segment.text),
+ ["docs/a.md"],
+ );
+ assert.deepEqual(
+ splitChatText("Open docs/a.md my project/page.md", ROOT)
+ .filter((segment) => segment.kind === "target")
+ .map((segment) => segment.text),
+ ["docs/a.md"],
+ );
for (const source of [
"Open my project/a.md and my project/b.md",
'Open "my project/a.md" and "my project/b.md"',