🤖 Open-source, local-first alternative to Grok Bot, OpenAI dots and Claude Duet. Your own AI bots on any model: GPT, Claude, Gemini, Grok, DeepSeek, Kimi, GLM and 70+ more through one Modelflare key. Reach them in the app, WeChat or Feishu; they remember you, work in the background, run commands and use your computer. Pay per use.
Python ai-agentai-assistantchatbotclaudecomputer-usedeepseekdesktop-appdshelectronfeishugeminigptgrokllmlocal-firstmodelflaremulti-modelopenai-compatiblepersonal-ai-agentwechat
fix(weixin): saved inbound file names are valid on Windows
Marlaaa9832f41254
diff --git a/src/core/runtime.ts b/src/core/runtime.tsindex 70f4b47..fb20de2 100644--- a/src/core/runtime.ts+++ b/src/core/runtime.ts@@ -31,10 +31,33 @@ export function inboundImages(ev: InboundEvent): TurnImage[] {return out;}-/** 发送者给的文件名只留最后一段,去掉控制字符和开头的点 */-function safeFileName(name: string | undefined): string {- const base = path.basename((name ?? "").replace(/\\/g, "/")).replace(/[\x00-\x1f\x7f]/g, "").replace(/^\.+/, "").trim();- return (base || "file").slice(-80);+/** Windows 的保留设备名(带不带扩展名都不能用作文件名) */+const RESERVED_NAME = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(\.|$)/i;+/** 文件名上限(UTF-8 字节;前面还要加 23 字节的时间戳前缀,常见文件系统上限 255) */+const MAX_NAME_BYTES = 150;++/**+ * 发送者给的文件名,在三个平台上都能用:只留最后一段,去掉控制字符和 Windows 不允许的 <>:"/\|?*+ * (冒号在 NTFS 上还会写进备用数据流)、开头的点、结尾的点和空格,避开保留设备名,+ * 按字节限长(保留扩展名,不切开字符)。中文照常保留。+ */+export function safeFileName(name: string | undefined): string {+ const base = path+ .basename((name ?? "").replace(/\\/g, "/"))+ .replace(/[\x00-\x1f\x7f<>:"/\\|?*]/g, "")+ .trim()+ .replace(/^\.+/, "");+ const ext = path.extname(base).length <= 16 ? path.extname(base) : "";+ let stem = "";+ let bytes = Buffer.byteLength(ext);+ for (const ch of base.slice(0, base.length - ext.length)) {+ bytes += Buffer.byteLength(ch);+ if (bytes > MAX_NAME_BYTES) break;+ stem += ch;+ }+ const out = (stem + ext).replace(/[. ]+$/, "");+ if (!out) return "file";+ return RESERVED_NAME.test(out) ? `_${out}` : out;}/**diff --git a/test/weixin-media.unit.test.ts b/test/weixin-media.unit.test.tsindex cd59aa6..6d89ee0 100644--- a/test/weixin-media.unit.test.ts+++ b/test/weixin-media.unit.test.ts@@ -7,7 +7,7 @@ import path from "node:path";import { IlinkClient, hasDownloadableMedia, parseAesKey, sniffImageMime, type MessageItem } from "../src/channels/weixin/api.js";import { weixinParts } from "../src/channels/weixin/channel.js";import { eventText, type InboundEvent, type Part } from "../src/core/types.js";-import { inboundImages, storeInbound } from "../src/core/runtime.js";+import { inboundImages, safeFileName, storeInbound } from "../src/core/runtime.js";import { acceptsImages } from "../src/llm/client.js";const PNG = Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), crypto.randomBytes(200)]);@@ -143,6 +143,41 @@ describe("storeInbound / inboundImages", () => {expect(eventText(e)).toBe(`看看\n[图片 ${paths[0]}]\n[图片 ${paths[1]}]\n[文件 ../../etc/evil\u0007.txt → ${paths[2]}]\n[视频 ${paths[3]}]\n[图片(未能下载:CDN 下载失败 HTTP 400)]`);});+ it("file names from the sender are valid on Windows, macOS and Linux", () => {+ // Windows 不允许的字符(冒号在 NTFS 上会写进备用数据流)、结尾的点和空格;中文保留+ expect(safeFileName('季度报告: 草稿?.docx')).toBe("季度报告 草稿.docx");+ expect(safeFileName('a<b>c"d|e*f.txt')).toBe("abcdef.txt");+ expect(safeFileName("C:\\Users\\me\\合同 v2.pdf")).toBe("合同 v2.pdf");+ expect(safeFileName("notes. . ")).toBe("notes");+ expect(safeFileName("..\\..\\.env")).toBe("env");+ expect(safeFileName("tab\there\u0007.md")).toBe("tabhere.md");+ // 保留设备名+ expect(safeFileName("CON")).toBe("_CON");+ expect(safeFileName("nul.txt")).toBe("_nul.txt");+ expect(safeFileName("com1.log")).toBe("_com1.log");+ expect(safeFileName("lpt9")).toBe("_lpt9");+ expect(safeFileName("COM10.txt")).toBe("COM10.txt");+ expect(safeFileName("console.log")).toBe("console.log");+ // 什么都不剩+ for (const n of [undefined, "", " ", "...", "?*:", "/"]) expect(safeFileName(n), String(n)).toBe("file");+ // 按 UTF-8 字节限长,保留扩展名,不切开字符+ const long = safeFileName(`${"报".repeat(100)}.pdf`);+ expect(long).toBe(`${"报".repeat(48)}.pdf`);+ const emoji = safeFileName(`${"😀".repeat(60)}.png`);+ expect(emoji).toBe(`${"😀".repeat(36)}.png`);+ expect(Buffer.byteLength(safeFileName("x".repeat(400)))).toBe(150);+ });++ it("saves a file whose name Windows would reject", () => {+ const ws = fs.mkdtempSync(path.join(os.tmpdir(), "amb-inbox-"));+ const e = ev([{ type: "file", name: "会议纪要 10:30 <终版>?.docx", data: Buffer.from("x") }]);+ storeInbound(ws, e, new Date("2026-10-31T06:43:12Z"));+ const p = (e.parts[0] as any).path as string;+ expect(p).toMatch(/^inbox\/20261031-064312-[0-9a-f]{6}-会议纪要 1030 终版\.docx$/);+ expect(fs.readFileSync(path.join(ws, p), "utf8")).toBe("x");+ fs.rmSync(ws, { recursive: true, force: true });+ });+it("only recognised, reasonably small images go to the model, at most 4", () => {const parts: Part[] = [{ type: "image", data: PNG, mime: "image/png" },