import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { spawn } from "node:child_process"; import { fileURLToPath } from "node:url"; function isFile(fp) { try { return Boolean(fp) && fs.existsSync(fp) && fs.statSync(fp).isFile(); } catch { return false; } } function isDir(fp) { try { return Boolean(fp) && fs.existsSync(fp) && fs.statSync(fp).isDirectory(); } catch { return false; } } function isAiBase(p) { return Boolean(p) && isDir(path.join(p, "dsh-agent-instructions", "lib")); } function uniquePaths(list) { const seen = new Set(); const out = []; for (const item of list) { if (!item) continue; const n = path.normalize(item); const key = n.replace(/\\/g, "/").toLowerCase(); if (seen.has(key)) continue; seen.add(key); out.push(n); } return out; } function slashNorm(p) { return path.normalize(String(p || "")).replace(/\\/g, "/").toLowerCase(); } function isSubPath(child, root) { const c = slashNorm(child).replace(/\/+$/, ""); const r = slashNorm(root).replace(/\/+$/, ""); if (!c || !r || r === "/") return false; if (/^[a-z]:$/.test(r)) return false; return c === r || c.startsWith(`${r}/`); } /** 文件夹名带 DSH Desktop / dsh-desktop 时,任意盘符都算桌面安装树。 */ export function looksLikeDesktopName(p) { const n = slashNorm(p); if (!n) return false; return ( n.includes("/dsh desktop/") || n.includes("/dsh desktop.app/") || n.endsWith("/dsh desktop") || n.endsWith("/dsh desktop.app") || n.includes("dsh-desktop") || /\/dsh desktop\.exe$/i.test(n) ); } export function isDesktopExecutable(fp) { const base = path.basename(String(fp || "")).toLowerCase(); return base === "dsh desktop.exe" || base === "dsh desktop"; } function exeCandidatesBeside(dir) { if (!dir) return []; return [ path.join(dir, "DSH Desktop.exe"), path.join(dir, "DSH Desktop"), path.join(dir, "MacOS", "DSH Desktop"), path.join(dir, "Contents", "MacOS", "DSH Desktop"), ]; } function firstExistingExe(dirs) { for (const dir of dirs) { if (!dir) continue; for (const cand of exeCandidatesBeside(dir)) { if (isFile(cand)) return path.normalize(cand); } } return ""; } const APP_SCRIPT_RE = /(desktop-cli|host-process-entry)\.js$/i; function installDirsFromAppScript(scriptPath) { if (!scriptPath) return []; const abs = path.resolve(scriptPath); const lib = path.dirname(abs); const app = path.dirname(lib); const resources = path.dirname(app); const install = path.dirname(resources); return uniquePaths([lib, app, resources, install]); } /** 从任意 `.../resources/app/...` 树反推安装目录(不要求文件夹叫 DSH Desktop)。 */ export function inferDesktopInstallDir(p) { const raw = path.normalize(String(p || "")).replace(/\\/g, "/"); const n = raw.toLowerCase(); if (!n) return ""; const markers = [ "/resources/app.asar.unpacked/", "/resources/app/", "/contents/resources/", ]; for (const marker of markers) { const i = n.indexOf(marker); if (i <= 0) continue; return path.normalize(raw.slice(0, i)); } return ""; } function extraInstallsFromEnv(env = process.env) { return String(env.DSH_DESKTOP_INSTALL || "") .split(path.delimiter) .map((item) => item.trim()) .filter(Boolean); } function trustedInstallDir(dir) { if (!dir) return false; if (looksLikeDesktopName(dir)) return true; if (firstExistingExe([dir])) return true; return ( isAiBase(path.join(dir, "resources", "app", "node_modules", "@deepseek-ai")) || isAiBase(path.join(dir, "resources", "app.asar.unpacked", "node_modules", "@deepseek-ai")) ); } /** NSIS / 商店默认位置;不是全盘扫描。自定义目录靠进程或 DSH_DESKTOP_INSTALL。 */ export function commonDesktopInstallDirs(env = process.env) { const home = os.homedir(); const local = env.LOCALAPPDATA || path.join(home, "AppData", "Local"); const pf = env.ProgramFiles || "C:\\Program Files"; const pf86 = env["ProgramFiles(x86)"] || "C:\\Program Files (x86)"; return uniquePaths([ ...extraInstallsFromEnv(env).filter(trustedInstallDir), path.join(local, "Programs", "DSH Desktop"), path.join(pf, "DSH Desktop"), path.join(pf86, "DSH Desktop"), "/Applications/DSH Desktop.app", path.join(home, "Applications", "DSH Desktop.app"), ]); } export function listDesktopInstallRoots(input = {}) { const execPath = input.execPath ?? process.execPath; const resourcesPath = input.resourcesPath ?? process.resourcesPath; const argv = input.argv ?? process.argv; const env = input.env ?? process.env; const roots = []; if (isDesktopExecutable(execPath)) { const dir = path.dirname(path.normalize(execPath)); roots.push(dir); if (path.basename(dir) === "MacOS") { const contents = path.dirname(dir); roots.push(contents, path.join(contents, "Resources"), path.dirname(contents)); } } if (resourcesPath) { const res = path.normalize(resourcesPath); roots.push(res, path.dirname(res), path.join(res, "app"), path.join(res, "app.asar.unpacked")); } for (const arg of argv) { if (typeof arg === "string" && APP_SCRIPT_RE.test(arg)) { roots.push(...installDirsFromAppScript(arg)); } } for (const extra of extraInstallsFromEnv(env)) { if (trustedInstallDir(extra)) roots.push(extra); } return uniquePaths(roots); } /** * 是否落在社区 Desktop 安装树。 * 不依赖盘符、也不要求文件夹叫 “DSH Desktop”: * 当前进程的 exe / resources / host-process-entry、旁边真有 DSH Desktop.exe、或名字启发式。 */ export function isInsideDesktopInstall(p, input = {}) { if (!p) return false; if (looksLikeDesktopName(p)) return true; const roots = listDesktopInstallRoots(input); if (roots.some((root) => isSubPath(p, root))) return true; const inferred = inferDesktopInstallDir(p); if (!inferred) return false; if (roots.some((root) => isSubPath(inferred, root) || isSubPath(root, inferred))) return true; return Boolean(firstExistingExe([inferred])); } export function findDesktopAppExecutable(input = {}) { const execPath = input.execPath ?? process.execPath; const resourcesPath = input.resourcesPath ?? process.resourcesPath; const argv = input.argv ?? process.argv; const env = input.env ?? process.env; if (isDesktopExecutable(execPath) && isFile(execPath)) return path.normalize(execPath); if (resourcesPath) { const hit = firstExistingExe([path.dirname(path.normalize(resourcesPath))]); if (hit) return hit; } for (const arg of argv) { if (typeof arg !== "string" || !APP_SCRIPT_RE.test(arg)) continue; const hit = firstExistingExe(installDirsFromAppScript(arg)); if (hit) return hit; } const guessed = firstExistingExe([ ...extraInstallsFromEnv(env), ...commonDesktopInstallDirs(env), ]); return guessed || ""; } function packageRootsFromInstall(installDir) { if (!installDir) return []; const bases = [installDir]; const name = path.basename(installDir); if (name === "MacOS") bases.push(path.join(path.dirname(installDir), "Resources")); if (name === "Contents") bases.push(path.join(installDir, "Resources")); if (name.toLowerCase().endsWith(".app")) bases.push(path.join(installDir, "Contents", "Resources")); const out = []; for (const base of bases) { out.push( path.join(base, "resources", "app", "node_modules", "@deepseek-ai"), path.join(base, "resources", "app.asar.unpacked", "node_modules", "@deepseek-ai"), path.join(base, "app", "node_modules", "@deepseek-ai"), path.join(base, "app.asar.unpacked", "node_modules", "@deepseek-ai"), ); } return out; } export function listDesktopPackageRoots(input = {}) { const execPath = input.execPath ?? process.execPath; const resourcesPath = input.resourcesPath ?? process.resourcesPath; const argv = input.argv ?? process.argv; const env = input.env ?? process.env; const cands = []; if (resourcesPath) { cands.push( path.join(resourcesPath, "app", "node_modules", "@deepseek-ai"), path.join(resourcesPath, "app.asar.unpacked", "node_modules", "@deepseek-ai"), ); } if (isDesktopExecutable(execPath)) { cands.push(...packageRootsFromInstall(path.dirname(execPath))); } const exe = findDesktopAppExecutable(input); if (exe) cands.push(...packageRootsFromInstall(path.dirname(exe))); for (const arg of argv) { if (typeof arg !== "string" || !APP_SCRIPT_RE.test(arg)) continue; const appDir = path.dirname(path.dirname(path.resolve(arg))); cands.push(path.join(appDir, "node_modules", "@deepseek-ai")); cands.push(...packageRootsFromInstall(path.dirname(path.dirname(appDir)))); } for (const install of commonDesktopInstallDirs(env)) { cands.push(...packageRootsFromInstall(install)); } return uniquePaths(cands).filter(isAiBase); } export function resolveDesktopAiBase(input = {}) { return listDesktopPackageRoots(input)[0] || null; } export function resolveDesktopCliScript(input = {}) { const exe = findDesktopAppExecutable(input); if (exe) { const install = path.dirname(exe); const packed = path.join(install, "resources", "app", "lib", "desktop-cli.js"); if (isFile(packed)) return path.normalize(packed); const unpacked = path.join(install, "resources", "app.asar.unpacked", "lib", "desktop-cli.js"); if (isFile(unpacked)) return path.normalize(unpacked); if (path.basename(install) === "MacOS") { const macPacked = path.join(path.dirname(install), "Resources", "app", "lib", "desktop-cli.js"); if (isFile(macPacked)) return path.normalize(macPacked); } } for (const arg of input.argv ?? process.argv) { if (typeof arg === "string" && /desktop-cli\.js$/i.test(arg) && isFile(arg)) { return path.normalize(path.resolve(arg)); } } return ""; } export function cliInvocation(input = {}) { const exe = findDesktopAppExecutable(input); const cli = resolveDesktopCliScript(input); if (!exe || !cli) return null; return { execPath: exe, argvPrefix: ["--expose-internals", cli], env: { ELECTRON_RUN_AS_NODE: "1", DSH_DESKTOP_DEFAULT_PROFILE: "desktop", }, }; } export function isSealedRuntimeDir(dir) { if (!dir) return false; const n = path.normalize(dir).replace(/\\/g, "/").toLowerCase(); if (!/(^|\/)bin\/?$/.test(n)) return false; return ( n.includes("/host-commands/") || n.includes("/runtime-commands/") || n.includes("/dsh desktop/") ); } export function sealedRuntimeRoots(env = process.env) { const home = os.homedir(); const appdata = env.APPDATA || path.join(home, "AppData", "Roaming"); const localapp = env.LOCALAPPDATA || path.join(home, "AppData", "Local"); return uniquePaths([ path.join(appdata, "DSH Desktop", "host-commands"), path.join(appdata, "DSH Desktop", "runtime-commands"), path.join(localapp, "DSH Desktop", "host-commands"), path.join(localapp, "DSH Desktop", "runtime-commands"), path.join(home, "Library", "Application Support", "DSH Desktop", "host-commands"), ]); } function walkBinDirs(dir, out, depth) { if (depth < 0 || !isDir(dir)) return; const n = dir.replace(/\\/g, "/").toLowerCase(); if (/(^|\/)bin$/.test(n)) { out.push(path.normalize(dir)); return; } let ents = []; try { ents = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; } for (const ent of ents) { if (ent.isDirectory()) walkBinDirs(path.join(dir, ent.name), out, depth - 1); } } export function listSealedRuntimeBins(env = process.env) { const out = []; for (const root of sealedRuntimeRoots(env)) walkBinDirs(root, out, 5); return uniquePaths(out).filter(isSealedRuntimeDir); } export function restartNote() { return "正在重启桌面应用…"; } export function restartPlan(input = {}) { const exe = findDesktopAppExecutable(input); if (!exe) { return { kind: "desktop", execPath: "", argv: [], cwd: "", error: "未找到 DSH Desktop.exe,无法重启桌面应用" }; } return { kind: "desktop", execPath: exe, argv: [], cwd: path.dirname(exe), }; } function helperEnv(payload) { const env = { ...process.env }; delete env.DSH_PERMISSION_MODE; env.ELECTRON_RUN_AS_NODE = "1"; env.DSH_PURGE_RESTART = JSON.stringify(payload); return env; } function spawnHelper(helper, payload, opts = {}) { const plan = restartPlan(); if (!plan.execPath) throw new Error(plan.error || "未找到 DSH Desktop.exe,无法重启桌面应用"); const child = spawn(process.execPath, [helper], { detached: true, stdio: "ignore", windowsHide: true, shell: false, env: helperEnv({ kind: "desktop", execPath: plan.execPath, argv: plan.argv, cwd: plan.cwd, pid: process.pid, ...payload, }), }); child.unref(); if (opts.exit !== false) setTimeout(() => process.exit(0), opts.exitAfter ?? 250); } function desktopRuntimeOf(ctx) { if (!ctx || typeof ctx.get !== "function") return null; const runtime = ctx.get("desktopRuntime") || ctx.get("desktopActions"); return runtime && typeof runtime.requestRestart === "function" ? runtime : null; } export function scheduleRestart(request, ctx) { const runtime = desktopRuntimeOf(ctx); if (runtime) { // 只走桌面整应用 relaunch,不要 exit Host(那会先把内嵌 web 单独拉起来)。 Promise.resolve(runtime.requestRestart()).catch((err) => { console.warn("[dsh-purge] desktop requestRestart failed:", err); }); return; } const helper = fileURLToPath(new URL("./restart-desktop.js", import.meta.url)); spawnHelper(helper, { respawn: true, killApp: true }, { exit: false }); } export function scheduleCleanupRestart({ cleanup = [], ctx } = {}) { const src = fileURLToPath(new URL("./restart-desktop.js", import.meta.url)); const dest = path.join(os.tmpdir(), `dsh-purge-uninst-${process.pid}.mjs`); fs.copyFileSync(src, dest); const runtime = desktopRuntimeOf(ctx); if (runtime) { spawnHelper(dest, { cleanup, helper: dest, respawn: false }, { exit: false }); Promise.resolve(runtime.requestRestart()).catch((err) => { console.warn("[dsh-purge] desktop uninstall restart failed:", err); }); return; } spawnHelper(dest, { cleanup, helper: dest, respawn: true, killApp: true }, { exit: false }); }