Skip to content

🤖 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.

fix Windows startup ENOENT on dsh.asar materialize (0.1.2)

Marla90f4c979db2f
diff --git a/package-lock.json b/package-lock.json
index 1009336..bac3f51 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,14 +1,15 @@
{
"name": "anymodel-bot",
- "version": "0.1.0",
+ "version": "0.1.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "anymodel-bot",
- "version": "0.1.0",
+ "version": "0.1.1",
"license": "MIT",
"dependencies": {
+ "@electron/asar": "^3.4.1",
"@larksuiteoapi/node-sdk": "1.76.0",
"htm": "^3.1.1",
"preact": "^11.0.2",
@@ -43,7 +44,6 @@
"version": "3.4.1",
"resolved": "https://registry.npmjs.org/@electron/asar/-/asar-3.4.1.tgz",
"integrity": "sha512-i4/rNPRS84t0vSRa2HorerGRXWyF4vThfHesw0dmcWHp+cspK743UanA0suA5Q5y8kzY2y6YKrvbIUn69BCAiA==",
- "dev": true,
"license": "MIT",
"dependencies": {
"commander": "^5.0.0",
@@ -61,14 +61,12 @@
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
- "dev": true,
"license": "MIT"
},
"node_modules/@electron/asar/node_modules/brace-expansion": {
"version": "1.1.21",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.21.tgz",
"integrity": "sha512-9zeA+KLZNNzglF2TPKRQEDyx6Yby7daAkuy8MiPzpXPsYDWi/DRM8jmwUDxokQjYqBpv5DgPiwD4h4ZZSy1Ujw==",
- "dev": true,
"license": "MIT",
"dependencies": {
"balanced-match": "^1.0.0",
@@ -79,7 +77,6 @@
"version": "3.1.5",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.5.tgz",
"integrity": "sha512-VgjWUsnnT6n+NUk6eZq77zeFdpW2LWDzP6zFGrCbHXiYNul5Dzqk2HHQ5uFH2DNW5Xbp8+jVzaeNt94ssEEl4w==",
- "dev": true,
"license": "ISC",
"dependencies": {
"brace-expansion": "^1.1.7"
@@ -2468,7 +2465,6 @@
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz",
"integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">= 6"
@@ -2488,7 +2484,6 @@
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==",
- "dev": true,
"license": "MIT"
},
"node_modules/core-util-is": {
@@ -3386,7 +3381,6 @@
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
- "dev": true,
"license": "ISC"
},
"node_modules/fsevents": {
@@ -3480,7 +3474,6 @@
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
"integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==",
"deprecated": "Old versions of glob are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting [email protected]",
- "dev": true,
"license": "ISC",
"dependencies": {
"fs.realpath": "^1.0.0",
@@ -3501,14 +3494,12 @@
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
- "dev": true,
"license": "MIT"
},
"node_modules/glob/node_modules/brace-expansion": {
"version": "1.1.21",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.21.tgz",
"integrity": "sha512-9zeA+KLZNNzglF2TPKRQEDyx6Yby7daAkuy8MiPzpXPsYDWi/DRM8jmwUDxokQjYqBpv5DgPiwD4h4ZZSy1Ujw==",
- "dev": true,
"license": "MIT",
"dependencies": {
"balanced-match": "^1.0.0",
@@ -3519,7 +3510,6 @@
"version": "3.1.5",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.5.tgz",
"integrity": "sha512-VgjWUsnnT6n+NUk6eZq77zeFdpW2LWDzP6zFGrCbHXiYNul5Dzqk2HHQ5uFH2DNW5Xbp8+jVzaeNt94ssEEl4w==",
- "dev": true,
"license": "ISC",
"dependencies": {
"brace-expansion": "^1.1.7"
@@ -3755,7 +3745,6 @@
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
"integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==",
"deprecated": "This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.",
- "dev": true,
"license": "ISC",
"dependencies": {
"once": "^1.3.0",
@@ -3766,7 +3755,6 @@
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
- "dev": true,
"license": "ISC"
},
"node_modules/is-fullwidth-code-point": {
@@ -4600,7 +4588,6 @@
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
- "dev": true,
"license": "ISC",
"dependencies": {
"wrappy": "1"
@@ -4665,7 +4652,6 @@
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">=0.10.0"
@@ -5999,7 +5985,6 @@
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
- "dev": true,
"license": "ISC"
},
"node_modules/ws": {
diff --git a/package.json b/package.json
index 357170e..e6c8894 100644
--- a/package.json
+++ b/package.json
@@ -1,9 +1,9 @@
{
"name": "anymodel-bot",
"productName": "anymodel bot",
- "version": "0.1.1",
+ "version": "0.1.2",
"private": true,
- "description": "anymodel bot\uff1a\u4efb\u610f\u6a21\u578b\u7684\u4e2a\u4eba AI \u52a9\u624b\uff08\u98de\u4e66 + \u5fae\u4fe1 iLink\uff09\uff0c\u5185\u7f6e magpie \u6a21\u578b\u7f51\u5173",
+ "description": "anymodel bot:任意模型的个人 AI 助手(飞书 + 微信 iLink),内置 magpie 模型网关",
"license": "MIT",
"type": "module",
"main": "dist/electron/main.js",
@@ -31,6 +31,7 @@
"pack:dsh": "node scripts/pack-dsh-asar.mjs"
},
"dependencies": {
+ "@electron/asar": "^3.4.1",
"@larksuiteoapi/node-sdk": "1.76.0",
"htm": "^3.1.1",
"preact": "^11.0.2",
diff --git a/src/agent/host.ts b/src/agent/host.ts
index 0de2743..4220d7c 100644
--- a/src/agent/host.ts
+++ b/src/agent/host.ts
@@ -1,11 +1,13 @@
import fs from "node:fs";
import path from "node:path";
+import { createRequire } from "node:module";
import { execFileSync } from "node:child_process";
import { pathToFileURL } from "node:url";
import { logger } from "../log.js";
import type { Store } from "../store/db.js";
import type { AgentBridge } from "./bridge.js";
import os from "node:os";
+import { findAppRoot } from "../magpie/supervisor.js";
import { adaptSkill, mcpPatchLines, resolveComputerUse, type CuResolved } from "./computer-use.js";
import { cuaConfig, cuaMcpEnv, discoverApps } from "./cua-driver.js";
import { DEFAULT_SETTINGS, permissionMode, type BotSettings } from "./settings.js";
@@ -63,39 +65,137 @@ export function locateDshRuntime(appRoot: string): string | undefined {
}
/**
- * 若运行时在 asar 内,复制到 cacheRoot/dsh-runtime/<version>,供 ELECTRON_RUN_AS_NODE 子进程使用。
- * Electron 主进程的 fs 能读 asar;复制时 asarUnpack 的原生模块也会落到实文件。
+ * Resolve cache key for a materialized DSH runtime.
+ * Prefer the anymodel-bot app package.json version (via findAppRoot), not
+ * `dsh.asar/../package.json` which is wrong under resources/.
*/
-export function materializeDshRuntime(runtimeDir: string, cacheRoot: string): string {
- if (!runtimeDir.split(path.sep).some((p) => p.endsWith(".asar"))) return runtimeDir;
- let version = "unknown";
+function resolveDshCacheVersion(runtimeDir: string): string {
try {
- version = JSON.parse(fs.readFileSync(path.join(runtimeDir, "package.json"), "utf8")).version ?? version;
- } catch {
- try {
- version = JSON.parse(fs.readFileSync(path.join(runtimeDir, "node_modules", "@deepseek-ai", "dsh", "package.json"), "utf8")).version ?? version;
- } catch {
- /* keep unknown */
- }
- }
- // Prefer app version so cache invalidates on each anymodel bump even if dsh pin is unchanged.
- try {
- const appPj = path.join(runtimeDir, "..", "package.json");
+ const appPj = path.join(findAppRoot(), "package.json");
if (fs.existsSync(appPj)) {
const v = JSON.parse(fs.readFileSync(appPj, "utf8")).version;
- if (v) version = v;
+ if (typeof v === "string" && v) return v;
}
} catch {
/* ignore */
}
+ try {
+ const v = JSON.parse(fs.readFileSync(path.join(runtimeDir, "package.json"), "utf8")).version;
+ if (typeof v === "string" && v) return v;
+ } catch {
+ /* ignore */
+ }
+ try {
+ const v = JSON.parse(
+ fs.readFileSync(path.join(runtimeDir, "node_modules", "@deepseek-ai", "dsh", "package.json"), "utf8"),
+ ).version;
+ if (typeof v === "string" && v) return v;
+ } catch {
+ /* ignore */
+ }
+ return "unknown";
+}
+
+/**
+ * Extract an asar archive to dest, then merge sibling `.asar.unpacked` when present.
+ * Prefer `@electron/asar` extractAll (reads archive + unpacked natives). Fall back to a
+ * walk that on ENOENT under the asar reads from the `.asar.unpacked` mirror — never use
+ * plain recursive `fs.cpSync` on an asar with asarUnpack entries (ENOENT in Electron).
+ */
+function extractAsarWithUnpacked(asarPath: string, dest: string): void {
+ fs.mkdirSync(dest, { recursive: true });
+ const unpacked = `${asarPath}.unpacked`;
+ try {
+ const require = createRequire(import.meta.url);
+ const asar = require("@electron/asar") as { extractAll: (a: string, d: string) => void };
+ asar.extractAll(asarPath, dest);
+ } catch (err) {
+ log.warn("asar.extractAll failed; falling back to walk+unpacked merge", {
+ asarPath,
+ err: err instanceof Error ? err.message : String(err),
+ });
+ copyAsarWithUnpackedFallback(asarPath, dest, unpacked);
+ }
+ if (fs.existsSync(unpacked)) {
+ fs.cpSync(unpacked, dest, { recursive: true, force: true });
+ }
+}
+
+/** Recursive copy that treats ENOENT under asarPath as "read from sibling .unpacked". */
+function copyAsarWithUnpackedFallback(asarPath: string, dest: string, unpacked: string): void {
+ const walk = (rel: string): void => {
+ const src = path.join(asarPath, rel);
+ const outDir = path.join(dest, rel);
+ let entries: fs.Dirent[];
+ try {
+ entries = fs.readdirSync(src, { withFileTypes: true });
+ } catch (err) {
+ const code = (err as NodeJS.ErrnoException)?.code;
+ if (code === "ENOENT" && fs.existsSync(path.join(unpacked, rel))) {
+ fs.cpSync(path.join(unpacked, rel), outDir, { recursive: true, force: true });
+ return;
+ }
+ throw err;
+ }
+ fs.mkdirSync(outDir, { recursive: true });
+ for (const e of entries) {
+ const child = rel ? path.join(rel, e.name) : e.name;
+ const childSrc = path.join(asarPath, child);
+ const childOut = path.join(dest, child);
+ const childUnp = path.join(unpacked, child);
+ try {
+ const st = fs.lstatSync(childSrc);
+ if (st.isDirectory()) walk(child);
+ else if (st.isSymbolicLink()) {
+ try {
+ fs.symlinkSync(fs.readlinkSync(childSrc), childOut);
+ } catch {
+ if (fs.existsSync(childUnp)) fs.cpSync(childUnp, childOut, { recursive: true, force: true });
+ }
+ } else {
+ fs.copyFileSync(childSrc, childOut);
+ }
+ } catch (err) {
+ const code = (err as NodeJS.ErrnoException)?.code;
+ if (code === "ENOENT" && fs.existsSync(childUnp)) {
+ fs.cpSync(childUnp, childOut, { recursive: true, force: true });
+ continue;
+ }
+ throw err;
+ }
+ }
+ };
+ walk("");
+}
+
+/**
+ * 若运行时在 asar 内,提取到 cacheRoot/dsh-runtime/<version>,供 ELECTRON_RUN_AS_NODE 子进程使用。
+ * Packaged layout is `resources/dsh.asar` + `dsh.asar.unpacked` (natives); plain cpSync throws ENOENT.
+ */
+export function materializeDshRuntime(runtimeDir: string, cacheRoot: string): string {
+ if (!runtimeDir.split(path.sep).some((p) => p.endsWith(".asar"))) return runtimeDir;
+ const version = resolveDshCacheVersion(runtimeDir);
const dest = path.join(cacheRoot, "dsh-runtime", version);
const marker = path.join(dest, ".amb-dsh-ready");
if (isDshRuntime(dest) && fs.existsSync(marker)) return dest;
const tmp = `${dest}.tmp-${process.pid}`;
fs.rmSync(tmp, { recursive: true, force: true });
fs.mkdirSync(path.dirname(dest), { recursive: true });
- log.info("materializing dsh runtime from asar", { from: runtimeDir, to: dest });
- fs.cpSync(runtimeDir, tmp, { recursive: true });
+ log.info("materializing dsh runtime from asar", { from: runtimeDir, to: dest, version });
+ // runtimeDir may be the asar file itself (…/dsh.asar) or a path inside it.
+ let asarPath = runtimeDir;
+ const parts = runtimeDir.split(path.sep);
+ for (let i = 0; i < parts.length; i++) {
+ if (parts[i].endsWith(".asar")) {
+ asarPath = parts.slice(0, i + 1).join(path.sep);
+ break;
+ }
+ }
+ extractAsarWithUnpacked(asarPath, tmp);
+ if (!isDshRuntime(tmp)) {
+ fs.rmSync(tmp, { recursive: true, force: true });
+ throw new Error(`materializeDshRuntime: extracted tree missing dsh-sdk-client (${asarPath})`);
+ }
fs.writeFileSync(path.join(tmp, ".amb-dsh-ready"), new Date().toISOString());
fs.rmSync(dest, { recursive: true, force: true });
fs.renameSync(tmp, dest);
diff --git a/src/app.ts b/src/app.ts
index d5110d4..1e41004 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -73,7 +73,16 @@ export class App {
config.agent.mode === "dsh"
? (() => {
const located = locateDshRuntime(findAppRoot());
- return located ? materializeDshRuntime(located, config.dataDir) : undefined;
+ if (!located) return undefined;
+ try {
+ return materializeDshRuntime(located, config.dataDir);
+ } catch (err) {
+ log.warn("DSH runtime materialize failed; continuing with plain chat", {
+ located,
+ err: err instanceof Error ? err.message : String(err),
+ });
+ return undefined;
+ }
})()
: undefined,
upstream,
diff --git a/test/dsh-materialize.unit.test.ts b/test/dsh-materialize.unit.test.ts
new file mode 100644
index 0000000..fdc3b61
--- /dev/null
+++ b/test/dsh-materialize.unit.test.ts
@@ -0,0 +1,48 @@
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { afterEach, describe, expect, it } from "vitest";
+import { materializeDshRuntime } from "../src/agent/host.js";
+import { findAppRoot } from "../src/magpie/supervisor.js";
+
+const packagedAsar = path.join(findAppRoot(), "release", "win-unpacked", "resources", "dsh.asar");
+const hasPackaged = fs.existsSync(packagedAsar) && fs.existsSync(`${packagedAsar}.unpacked`);
+
+const dirs: string[] = [];
+afterEach(() => {
+ for (const d of dirs.splice(0)) fs.rmSync(d, { recursive: true, force: true });
+});
+
+describe("materializeDshRuntime", () => {
+ it("returns plain dirs unchanged", () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "amb-plain-"));
+ dirs.push(dir);
+ fs.mkdirSync(path.join(dir, "node_modules", "@deepseek-ai", "dsh-sdk-client"), { recursive: true });
+ fs.writeFileSync(path.join(dir, "node_modules", "@deepseek-ai", "dsh-sdk-client", "package.json"), "{}");
+ const cache = fs.mkdtempSync(path.join(os.tmpdir(), "amb-cache-"));
+ dirs.push(cache);
+ expect(materializeDshRuntime(dir, cache)).toBe(dir);
+ });
+
+ it.skipIf(!hasPackaged)("extracts packaged dsh.asar + unpacked natives into versioned cache", () => {
+ const cache = fs.mkdtempSync(path.join(os.tmpdir(), "amb-dsh-mat-"));
+ dirs.push(cache);
+ const dest = materializeDshRuntime(packagedAsar, cache);
+ expect(dest).toContain(path.join(cache, "dsh-runtime"));
+ expect(fs.existsSync(path.join(dest, ".amb-dsh-ready"))).toBe(true);
+ expect(fs.existsSync(path.join(dest, "node_modules", "@deepseek-ai", "dsh-sdk-client", "package.json"))).toBe(true);
+ // At least one native from dsh.asar.unpacked
+ const walk = (d: string): string[] => {
+ const out: string[] = [];
+ for (const e of fs.readdirSync(d, { withFileTypes: true })) {
+ const p = path.join(d, e.name);
+ if (e.isDirectory()) out.push(...walk(p));
+ else if (e.name.endsWith(".node")) out.push(p);
+ }
+ return out;
+ };
+ expect(walk(dest).length).toBeGreaterThan(0);
+ // Second call hits cache marker
+ expect(materializeDshRuntime(packagedAsar, cache)).toBe(dest);
+ });
+});