Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(attachments): preserve literal source filenames

Afloat842a1f8b16f8
diff --git a/server/bot-attachment.test.ts b/server/bot-attachment.test.ts
index df33c7d152..bb76db128d 100644
--- a/server/bot-attachment.test.ts
+++ b/server/bot-attachment.test.ts
@@ -73,6 +73,16 @@ describe("saveBotAttachment", () => {
expect(saved.attachment).toMatchObject({ source: realpathSync.native(join(WORK, name)) });
});
+ it.each(["report#1.csv", "growth 20%.csv", "report%20one.pdf"])("attaches the literal source path %s", async (name) => {
+ const source = join(WORK, name);
+ writeFileSync(source, `contents of ${name}`);
+ for (const path of [name, source, pathToFileURL(source).href]) {
+ const saved = await saveBotAttachment({ path, roots: [WORK] });
+ expect(saved.attachment).toMatchObject({ name, source: realpathSync.native(source) });
+ expect(readFileSync(saved.attachment.path, "utf8")).toBe(`contents of ${name}`);
+ }
+ });
+
it("reads a file from the VM guest workspace through its host mount", async () => {
writeFileSync(join(VM_HOME, "reports", "nekoneko.pdf"), "%PDF-1.4 fake");
const saved = await saveBotAttachment({
@@ -86,6 +96,17 @@ describe("saveBotAttachment", () => {
expect(readFileSync(saved.attachment.path, "utf8")).toBe("%PDF-1.4 fake");
});
+ it("preserves filename characters after mapping a guest file URL to the host", async () => {
+ const name = "forecast%20#1.csv";
+ const source = join(VM_HOME, "reports", name);
+ writeFileSync(source, "month,total\nJanuary,100\n");
+ for (const path of [`/home/cua/workspace/reports/${name}`, `file:///home/cua/workspace/reports/${encodeURIComponent(name)}`]) {
+ const saved = await saveBotAttachment({ path, roots: [WORK], guest: { root: "/home/cua/workspace", host: VM_HOME } });
+ expect(saved.attachment).toMatchObject({ name, source: realpathSync.native(source) });
+ expect(readFileSync(saved.attachment.path, "utf8")).toBe("month,total\nJanuary,100\n");
+ }
+ });
+
it("attaches a VM video over the preview size, up to the same limit a person's upload has", async () => {
// A bot on a VM has no link to fall back on: attaching is the only way its video reaches the chat.
const guest = { root: "/home/cua/workspace", host: VM_HOME };
@@ -239,6 +260,15 @@ describe("saveBotAttachment for a file a turn saved outside its folders", () =>
expect(readFileSync(source, "utf8")).toBe("jpeg-bytes");
});
+ it("preserves literal filename characters in a file saved outside the workspace", async () => {
+ const since = Date.now();
+ const source = join(DESIGNS, "draft#1.pdf");
+ writeFileSync(source, "pdf-bytes");
+ const saved = await saveBotAttachment({ path: source, roots: [WORK], savedThisTurn: { since, refuse } });
+ expect(saved.attachment).toMatchObject({ name: "draft#1.pdf", source: realpathSync.native(source) });
+ expect(readFileSync(saved.attachment.path, "utf8")).toBe("pdf-bytes");
+ });
+
it("stays roots only without the grant, for a VM turn, and for a relative path", async () => {
const since = Date.now();
writeFileSync(join(DESIGNS, "fresh.pdf"), "%PDF-fresh");
@@ -425,6 +455,14 @@ describe("deliveredCopy", () => {
expect(deliveredCopy([ask, attached, reply], reply, "README.md", "D:\\AI\\openmausbot-skills\\README.md")).toEqual(renamed);
});
+ it.each(["report#1.pdf", "report%20one.pdf"])("matches the literal stored source %s to its file URL", (name) => {
+ const attachment = { ...renamed, source: `/reports/${name}` };
+ const attached = bot("attached", "ask", { attachments: [attachment] });
+ const href = `file:///reports/${encodeURIComponent(name)}`;
+ const reply = bot("reply", "attached", { text: `[Report](${href})` });
+ expect(deliveredCopy([ask, attached, reply], reply, name, href)).toEqual(attachment);
+ });
+
it("finds the same source spelled as a file:// URL or with an encoded space", () => {
const spaced = { ...renamed, source: "D:\\AI\\openmausbot-skills\\my file.pdf", name: "团队技能包.pdf" };
const attached = bot("attached", "ask", { attachments: [spaced] });
diff --git a/server/bot-attachment.ts b/server/bot-attachment.ts
index 98efd6fc8f..e0ed0fe89b 100644
--- a/server/bot-attachment.ts
+++ b/server/bot-attachment.ts
@@ -28,6 +28,8 @@ function statusError(status: number, message: string): Error & { status: number
return Object.assign(new Error(message), { status });
}
+const sourceFileLink = (path: string): string => /^file:/i.test(path) ? path : path.replace(/[%?#]/g, encodeURIComponent);
+
/** Maps a guest workspace path onto its bind-mounted host directory; anything outside it, `..` escapes included, is null. */
export function guestWorkspaceToHost(input: string, guestRoot: string, hostRoot: string): string | null {
let value = input.trim();
@@ -104,14 +106,15 @@ async function sha256Of(file: OpenedMessageFile): Promise<string> {
* Opened at the store's limit, not the preview one: the copy streams, and a person may send the same size. A path the
* roots do not hold is tried in the conversation's inbox before anything else. */
async function openSource(path: string, roots: readonly string[], savedThisTurn: SaveBotAttachmentInput["savedThisTurn"], inbox: InboxRead | undefined): Promise<OpenedMessageFile> {
+ const href = sourceFileLink(path);
try {
- return await openMessageFile(path, roots, FILE_MAX_BYTES);
+ return await openMessageFile(href, roots, FILE_MAX_BYTES);
} catch (error) {
const status = (error as { status?: unknown } | null)?.status, outside = (error as { code?: unknown } | null)?.code === "outside_workspace";
const received = inbox && (outside || status === 404) ? await openInboxFile(path, inbox, FILE_MAX_BYTES) : null;
if (received) return { ...received, mime: mimeFor(received.name) };
if (!savedThisTurn || !outside) throw error;
- return openFileSavedSince(path, savedThisTurn, FILE_MAX_BYTES);
+ return openFileSavedSince(href, savedThisTurn, FILE_MAX_BYTES);
}
}
@@ -203,7 +206,7 @@ export function deliveredCopy(
const bySource = inScope.filter((attachment) => {
if (!attachment.source) return false;
try {
- return referencedPathIdentity(attachment.source) === wanted;
+ return referencedPathIdentity(sourceFileLink(attachment.source)) === wanted;
} catch {
return false;
}
diff --git a/server/message-file.ts b/server/message-file.ts
index 99877e8883..6614e96c67 100644
--- a/server/message-file.ts
+++ b/server/message-file.ts
@@ -475,7 +475,7 @@ export async function openFileSavedSince(
if (hidden || refused.some((root) => containedBy(root, canonical))) {
throw statusError(403, "files in hidden folders or in the app's own data can't be attached", "outside_workspace");
}
- const file = await openMessageFile(canonical, [dirname(canonical)], maxBytes);
+ const file = await openMessageFile(pathToFileURL(canonical).href, [dirname(canonical)], maxBytes);
const opened = await file.handle.stat().catch(async (error: unknown) => {
await file.handle.close().catch(() => undefined);
throw error;