Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: windows file paths in messages

ogsamratfff131ad4c1b
diff --git a/apps/app/src/react-app/domains/session/sync/actions-store.ts b/apps/app/src/react-app/domains/session/sync/actions-store.ts
index c9e43aa9..418de79e 100644
--- a/apps/app/src/react-app/domains/session/sync/actions-store.ts
+++ b/apps/app/src/react-app/domains/session/sync/actions-store.ts
@@ -32,7 +32,13 @@ import type {
} from "../../../../app/types";
import { addOpencodeCacheHint, safeStringify } from "../../../../app/utils";
import { clearSessionDraft, LOCAL_SESSION_DRAFT_SCOPE, saveSessionDraft } from "./draft-store";
-import { firstLineLocalFileParts, isReadInlineablePath } from "./prompt-file-parts";
+import {
+ firstLineLocalFileParts,
+ isReadInlineablePath,
+ isWindowsWorkspaceRoot,
+ joinWorkspaceRelativePath,
+ promptFileUrl,
+} from "./prompt-file-parts";
import { composerAttachmentToFilePart } from "./attachment-file-part";
import { mentionPromptParts } from "./mention-parts";
import { personUserMessages } from "../commands/turn-messages";
@@ -148,10 +154,10 @@ export function createSessionActionsStore(options: {
const toAbsolutePath = (path: string) => {
const trimmed = path.trim();
if (!trimmed) return "";
- if (trimmed.startsWith("/")) return trimmed;
- if (/^[a-zA-Z]:\\/.test(trimmed)) return trimmed;
+ if (/^[a-zA-Z]:[\\/]/.test(trimmed) || /^(\\\\|\/\/)[^\\/]/.test(trimmed)) return trimmed;
+ if (trimmed.startsWith("/") && !isWindowsWorkspaceRoot(root)) return trimmed;
if (!root) return "";
- return (root + "/" + trimmed).replace("//", "/");
+ return joinWorkspaceRelativePath(root, trimmed);
};
const filenameFromPath = (path: string) => {
const normalized = path.replace(/\\/g, "/");
@@ -173,14 +179,15 @@ export function createSessionActionsStore(options: {
if (part.type === "file") {
const absolute = toAbsolutePath(part.path);
if (!absolute) continue;
- if (!isReadInlineablePath(absolute)) {
+ const url = promptFileUrl(absolute, root);
+ if (!url || !isReadInlineablePath(absolute)) {
parts.push({ type: "text", text: absolute } as TextPartInput);
continue;
}
parts.push({
type: "file",
mime: "text/plain",
- url: `file://${absolute}`,
+ url,
filename: filenameFromPath(part.path),
} as FilePartInput);
}
@@ -200,10 +207,10 @@ export function createSessionActionsStore(options: {
const toAbsolutePath = (path: string) => {
const trimmed = path.trim();
if (!trimmed) return "";
- if (trimmed.startsWith("/")) return trimmed;
- if (/^[a-zA-Z]:\\/.test(trimmed)) return trimmed;
+ if (/^[a-zA-Z]:[\\/]/.test(trimmed) || /^(\\\\|\/\/)[^\\/]/.test(trimmed)) return trimmed;
+ if (trimmed.startsWith("/") && !isWindowsWorkspaceRoot(root)) return trimmed;
if (!root) return "";
- return (root + "/" + trimmed).replace("//", "/");
+ return joinWorkspaceRelativePath(root, trimmed);
};
const filenameFromPath = (path: string) => {
@@ -215,11 +222,12 @@ export function createSessionActionsStore(options: {
for (const part of draft.parts) {
if (part.type !== "file") continue;
const absolute = toAbsolutePath(part.path);
- if (!absolute || !isReadInlineablePath(absolute)) continue;
+ const url = absolute ? promptFileUrl(absolute, root) : "";
+ if (!url || !isReadInlineablePath(absolute)) continue;
parts.push({
type: "file",
mime: "text/plain",
- url: `file://${absolute}`,
+ url,
filename: filenameFromPath(part.path),
} as FilePartInput);
}
diff --git a/apps/app/src/react-app/domains/session/sync/draft-parts.ts b/apps/app/src/react-app/domains/session/sync/draft-parts.ts
index 72cb3ec4..a1c51bf3 100644
--- a/apps/app/src/react-app/domains/session/sync/draft-parts.ts
+++ b/apps/app/src/react-app/domains/session/sync/draft-parts.ts
@@ -12,8 +12,9 @@ import {
import {
firstLineLocalFileParts,
isReadInlineablePath,
+ isWindowsWorkspaceRoot,
joinWorkspaceRelativePath,
- toFileUrl,
+ promptFileUrl,
} from "./prompt-file-parts";
import { mentionPromptParts } from "./mention-parts";
import { parseConnectSkillToken } from "../surface/composer/connect-skill-token";
@@ -34,8 +35,9 @@ export async function draftToParts(
const toAbsolutePath = (path: string) => {
const trimmed = path.trim();
if (!trimmed) return "";
- if (trimmed.startsWith("/")) return trimmed;
- if (/^[a-zA-Z]:[\\/]/.test(trimmed)) return trimmed;
+ if (/^[a-zA-Z]:[\\/]/.test(trimmed) || /^(\\\\|\/\/)[^\\/]/.test(trimmed)) return trimmed;
+ // On Windows a leading slash is not a full path; treat it as workspace-relative.
+ if (trimmed.startsWith("/") && !isWindowsWorkspaceRoot(root)) return trimmed;
if (!root) return "";
return joinWorkspaceRelativePath(root, trimmed);
};
@@ -122,15 +124,15 @@ export async function draftToParts(
}
if (mentionPart?.type === "file") {
const absolute = toAbsolutePath(mentionPart.path);
- if (!absolute) continue;
- if (!isReadInlineablePath(absolute)) {
- parts.push({ type: "text", text: absolute });
+ const url = absolute ? promptFileUrl(absolute, root) : "";
+ if (!absolute || !url || !isReadInlineablePath(absolute)) {
+ parts.push({ type: "text", text: absolute || mentionPart.path });
continue;
}
parts.push({
type: "file",
mime: "text/plain",
- url: toFileUrl(absolute),
+ url,
filename: filenameFromPath(mentionPart.path),
});
continue;
@@ -162,14 +164,15 @@ export async function draftToParts(
if (part.type === "file") {
const absolute = toAbsolutePath(part.path);
if (!absolute) continue;
- if (!isReadInlineablePath(absolute)) {
+ const url = promptFileUrl(absolute, root);
+ if (!url || !isReadInlineablePath(absolute)) {
parts.push({ type: "text", text: absolute });
continue;
}
parts.push({
type: "file",
mime: "text/plain",
- url: toFileUrl(absolute),
+ url,
filename: filenameFromPath(part.path),
});
}
diff --git a/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts b/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts
index e78dc27f..150f2d98 100644
--- a/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts
+++ b/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts
@@ -29,6 +29,8 @@ function normalizeFileUri(value: string) {
if (parsed.hostname && parsed.hostname.toLowerCase() !== "localhost") {
return `//${parsed.hostname}${pathname}`;
}
+ // `file:///C:/x` has the pathname `/C:/x`; the drive path is `C:/x`.
+ if (/^\/[A-Za-z]:\//.test(pathname)) return pathname.slice(1);
return pathname;
} catch {
return "";
@@ -68,9 +70,40 @@ function encodeFilePath(path: string) {
export function toFileUrl(path: string) {
const normalized = path.replace(/\\/g, "/");
if (/^[A-Za-z]:\//.test(normalized)) return `file:///${encodeFilePath(normalized).replace(/^([A-Za-z])%3A/, "$1:")}`;
+ // UNC share (`\\server\share\...`): the server is the URL host. `file:////server`
+ // has an empty host, which Windows cannot turn back into a path.
+ const unc = normalized.match(/^\/\/([^/]+)(\/.*)$/);
+ if (unc) return `file://${encodeURIComponent(unc[1] ?? "")}${encodeFilePath(unc[2] ?? "")}`;
return `file://${encodeFilePath(normalized)}`;
}
+/** A workspace on Windows: a drive (`C:\\`) or UNC (`\\\\server\\share`) root. */
+export function isWindowsWorkspaceRoot(workspaceRoot: string) {
+ const root = workspaceRoot.trim();
+ if (!root) return typeof navigator !== "undefined" && /^win/i.test(navigator.platform ?? "");
+ return /^[A-Za-z]:([\\/]|$)/.test(root) || /^(\\\\|\/\/)[^\\/]/.test(root);
+}
+
+/**
+ * The `file://` URL the engine reads for `path`, or "" when the engine on that
+ * workspace's system cannot open it. On Windows the engine only accepts drive
+ * and UNC paths: a POSIX-style path such as `/api/users.json` becomes
+ * `file:///api/users.json`, which fails the whole message with "File URL path
+ * must be an absolute path". Relative paths are never sent.
+ */
+export function promptFileUrl(path: string, workspaceRoot: string) {
+ const normalized = path.trim().replace(/\\/g, "/");
+ if (!normalized) return "";
+ const drive = /^[A-Za-z]:\//.test(normalized);
+ const unc = /^\/\/[^/]+\/[^/]/.test(normalized);
+ if (isWindowsWorkspaceRoot(workspaceRoot)) {
+ if (!drive && !unc) return "";
+ } else if (!drive && !normalized.startsWith("/")) {
+ return "";
+ }
+ return toFileUrl(normalized);
+}
+
export function joinWorkspaceRelativePath(workspaceRoot: string, relativePath: string) {
const root = workspaceRoot.trim().replace(/\\/g, "/").replace(/\/+$/, "");
const relative = relativePath.trim().replace(/\\/g, "/").replace(/^\/+/, "");
@@ -119,10 +152,12 @@ export function firstLineLocalFileParts(text: string, workspaceRoot: string): Fi
if (!absolute || seen.has(absolute)) continue;
seen.add(absolute);
if (!isReadInlineablePath(absolute)) continue;
+ const url = promptFileUrl(absolute, workspaceRoot);
+ if (!url) continue;
parts.push({
type: "file",
mime: "text/plain",
- url: toFileUrl(absolute),
+ url,
filename: filenameFromPath(raw),
});
}
diff --git a/apps/app/tests/prompt-file-url.test.ts b/apps/app/tests/prompt-file-url.test.ts
new file mode 100644
index 00000000..f9a1b868
--- /dev/null
+++ b/apps/app/tests/prompt-file-url.test.ts
@@ -0,0 +1,116 @@
+import { describe, expect, test } from "bun:test";
+import { fileURLToPath } from "node:url";
+
+import { draftToParts } from "../src/react-app/domains/session/sync/draft-parts";
+import {
+ firstLineLocalFileParts,
+ isWindowsWorkspaceRoot,
+ promptFileUrl,
+ toFileUrl,
+} from "../src/react-app/domains/session/sync/prompt-file-parts";
+
+// The engine turns every `file://` part back into a path with fileURLToPath;
+// on Windows it throws "File URL path must be an absolute path" for anything
+// without a drive or UNC host, which failed the whole message.
+function windowsPath(url: string) {
+ return fileURLToPath(url, { windows: true });
+}
+
+function fileUrls(parts: Array<{ type: string; url?: string }>) {
+ return parts.filter((part) => part.type === "file").map((part) => part.url ?? "");
+}
+
+describe("Windows workspace roots", () => {
+ test("detects drive and UNC roots", () => {
+ expect(isWindowsWorkspaceRoot("C:\\Users\\omar\\code")).toBe(true);
+ expect(isWindowsWorkspaceRoot("d:/work")).toBe(true);
+ expect(isWindowsWorkspaceRoot("\\\\server\\share\\proj")).toBe(true);
+ expect(isWindowsWorkspaceRoot("//server/share/proj")).toBe(true);
+ expect(isWindowsWorkspaceRoot("/home/omar/code")).toBe(false);
+ expect(isWindowsWorkspaceRoot("/Users/omar/code")).toBe(false);
+ });
+});
+
+describe("prompt file URLs", () => {
+ test("drops POSIX-style paths in a Windows workspace", () => {
+ expect(promptFileUrl("/api/users.json", "C:\\Users\\omar\\code")).toBe("");
+ expect(promptFileUrl("/src/index.ts", "C:/Users/omar/code")).toBe("");
+ });
+
+ test("drops relative paths everywhere", () => {
+ expect(promptFileUrl("src/index.ts", "C:\\Users\\omar\\code")).toBe("");
+ expect(promptFileUrl("src/index.ts", "/home/omar/code")).toBe("");
+ });
+
+ test("keeps drive and UNC paths the engine can open on Windows", () => {
+ const drive = promptFileUrl("C:\\Users\\omar\\my code\\a#1.ts", "C:\\Users\\omar\\code");
+ expect(windowsPath(drive)).toBe("C:\\Users\\omar\\my code\\a#1.ts");
+ const unc = promptFileUrl("\\\\server\\share\\proj\\notes.md", "\\\\server\\share\\proj");
+ expect(unc).toBe("file://server/share/proj/notes.md");
+ expect(windowsPath(unc)).toBe("\\\\server\\share\\proj\\notes.md");
+ });
+
+ test("keeps POSIX paths in a POSIX workspace", () => {
+ expect(promptFileUrl("/home/omar/code/a.ts", "/home/omar/code")).toBe("file:///home/omar/code/a.ts");
+ });
+
+ test("UNC paths get the server as the URL host", () => {
+ expect(toFileUrl("//server/share/x.ts")).toBe("file://server/share/x.ts");
+ });
+});
+
+describe("Windows first-line paths", () => {
+ test("a URL-style path in the first line is not sent as a file", () => {
+ const parts = firstLineLocalFileParts("fix the /api/login route and /src/app.ts", "C:\\Users\\omar\\code");
+ expect(parts).toEqual([]);
+ });
+
+ test("every file part from a Windows first line is a valid Windows path", () => {
+ const parts = firstLineLocalFileParts(
+ "compare C:\\Users\\omar\\a.ts with /etc/hosts and file:///C:/Users/omar/b.ts",
+ "C:\\Users\\omar\\code",
+ );
+ const urls = fileUrls(parts);
+ expect(urls).toHaveLength(2);
+ for (const url of urls) expect(() => windowsPath(url)).not.toThrow();
+ });
+});
+
+describe("Windows draft mentions", () => {
+ test("mentions resolve inside the workspace and never make an unreadable URL", async () => {
+ const parts = await draftToParts(
+ {
+ mode: "prompt",
+ text: "look at @src/app.ts and @/docs/readme.md, also /api/users.json",
+ parts: [
+ { type: "file", path: "src/app.ts" },
+ { type: "file", path: "/docs/readme.md" },
+ ],
+ attachments: [],
+ },
+ "C:\\Users\\omar\\code",
+ "ses_test",
+ null,
+ );
+ const urls = fileUrls(parts);
+ expect(urls.map(windowsPath)).toEqual([
+ "C:\\Users\\omar\\code\\src\\app.ts",
+ "C:\\Users\\omar\\code\\docs\\readme.md",
+ ]);
+ });
+
+ test("POSIX workspaces keep absolute mentions", async () => {
+ const parts = await draftToParts(
+ {
+ mode: "prompt",
+ text: "look at @/etc/hosts",
+ parts: [{ type: "file", path: "/etc/hosts" }],
+ attachments: [],
+ },
+ "/home/omar/code",
+ "ses_test",
+ null,
+ );
+ expect(fileUrls(parts)).toEqual(["file:///etc/hosts"]);
+ });
+});