UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

175 lines (174 loc) • 6.97 kB
import { d as resolveHomeRelativePath, f as resolveRequiredHomeDir, m as resolveUserPath, s as resolveStateDir } from "./account-core-u1qUW4Od.js"; import "./number-coercion-BtnBjAcf.js"; import path from "node:path"; import fs from "node:fs"; import { canUseRootFileOpen, openRootFileSync, readFileDescriptorBoundedSync } from "@openclaw/fs-safe/advanced"; import { FsSafeError } from "@openclaw/fs-safe/errors"; import os from "node:os"; //#region src/config/paths.ts /** * Nix mode detection: When OPENCLAW_NIX_MODE=1, the gateway is running under Nix. * In this mode: * - No auto-install flows should be attempted * - Missing dependencies should produce actionable Nix-specific error messages * - Config is managed externally (read-only from Nix perspective) */ function resolveIsNixMode(e = process.env) { return e.OPENCLAW_NIX_MODE === "1"; } let isNixMode = resolveIsNixMode(); const CONFIG_FILENAME = "openclaw.json"; /** Build a homedir thunk that respects OPENCLAW_HOME for the given env. */ function envHomedir(e) { return () => resolveRequiredHomeDir(e, os.homedir); } function normalizePathForComparison(e) { const t = path.resolve(e); try { return fs.realpathSync.native(t); } catch { return t; } } /** * Optional allowlist of directories that `$include` directives may resolve * outside the config directory. Set via `OPENCLAW_INCLUDE_ROOTS` as a * platform-delimited path list (`:` on POSIX, `;` on Windows). * * Each entry is tilde-expanded and resolved to an absolute path. Entries that * cannot be resolved or that are not absolute after expansion are dropped. * * Returns an empty array when the var is unset or contains no usable entries, * preserving the historical behavior where `$include` is confined to the * directory containing `openclaw.json`. */ function resolveIncludeRoots(e = process.env, t = envHomedir(e)) { const n = e.OPENCLAW_INCLUDE_ROOTS?.trim(); if (!n) return []; const effectiveHomedir = () => resolveRequiredHomeDir(e, t); const r = /* @__PURE__ */ new Set(); const i = []; for (const t of n.split(path.delimiter)) { const n = t.trim(); if (!n) continue; const a = path.resolve(resolveHomeRelativePath(n, { env: e, homedir: effectiveHomedir })); if (r.has(a)) continue; r.add(a); i.push(a); } return i; } let STATE_DIR = resolveStateDir(); /** * Resolve the explicitly selected or canonical config path. */ function resolveConfigPathCandidate(e = process.env, t = envHomedir(e)) { return resolveConfigPath(e, void 0, t); } /** * Active config path. */ function resolveConfigPath(e = process.env, t, n = envHomedir(e)) { const r = e.OPENCLAW_CONFIG_PATH?.trim(); if (r) return resolveUserPath(r, e, n); return path.join(t ?? resolveStateDir(e, n), CONFIG_FILENAME); } let CONFIG_PATH = resolveConfigPathCandidate(); /** * Gateway lock directory inside the selected state tree. * Default: $OPENCLAW_STATE_DIR/tmp/openclaw-<uid> (uid suffix when available). */ function resolveGatewayLockDir(e = resolveStateDir(), t = typeof process.getuid === "function" ? process.getuid() : void 0) { return resolveGatewayLockDirForCanonicalStateDir(normalizePathForComparison(e), t); } /** Append the lock layout when the caller has already resolved the state directory. */ function resolveGatewayLockDirForCanonicalStateDir(e, t = typeof process.getuid === "function" ? process.getuid() : void 0) { const n = t != null ? `openclaw-${t}` : "openclaw"; return path.join(e, "tmp", n); } /** Resolves the legacy credentials directory retained for Doctor and backup ownership. */ function resolveOAuthDir(e = process.env, t = resolveStateDir(e, envHomedir(e))) { const n = e.OPENCLAW_OAUTH_DIR?.trim(); if (n) return resolveUserPath(n, e, envHomedir(e)); return path.join(t, "credentials"); } //#endregion //#region src/infra/sealed-runtime-registry.ts let runtime; function getSealedRuntimeJson5() { return runtime?.json5; } function getSealedRuntimeSecureTempRoot() { return runtime?.resolveSecureTempRoot; } //#endregion //#region src/infra/tmp-openclaw-dir.ts /** Preferred shared OpenClaw temp root on POSIX systems when ownership and permissions are safe. */ const DEFAULT_POSIX_TMP_ROOT = "/tmp/openclaw"; let resolveSecureTempRootRuntime; function loadResolveSecureTempRoot() { if (resolveSecureTempRootRuntime) return resolveSecureTempRootRuntime; const e = getSealedRuntimeSecureTempRoot(); if (e) { resolveSecureTempRootRuntime = e; return e; } if (typeof SEALED_RUNTIME_BUILD === "boolean" && SEALED_RUNTIME_BUILD) throw new Error("sealed temp-root runtime was not registered before use"); if (typeof process.getBuiltinModule !== "function") throw new Error("Node module loading is unavailable for secure temp-root resolution"); resolveSecureTempRootRuntime = process.getBuiltinModule("module").createRequire(import.meta.url)("@openclaw/fs-safe/secure-temp-root").resolveSecureTempRoot; return resolveSecureTempRootRuntime; } function assertSafeTempAncestors(e, t) { const n = t.platform ?? process.platform; if (n === "win32" || n === "android") return; const r = process.getBuiltinModule("fs"); const i = t.lstatSync ?? r.lstatSync; const a = t.lstatSync ? path.resolve(e) : r.realpathSync.native(e); for (let e = path.dirname(a);; e = path.dirname(e)) { const t = i(e); if (t.mode !== void 0 && (t.mode & 18) !== 0 && (t.mode & 512) === 0) { const t = `'${e.replaceAll("'", "'\\''")}'`; throw new Error(`Temp workspace ancestor ${JSON.stringify(e)} is group/world writable without sticky protection; have its owner run chmod +t ${t} for a shared temp directory or chmod go-w ${t} for a private ancestor.`); } if (path.dirname(e) === e) return; } } /** Select a private root with trusted ancestors before consumers create disposable workspaces. */ function resolvePreferredOpenClawTmpDir(e = {}) { const t = loadResolveSecureTempRoot(); const n = { ...e, preferredDir: e.preferredDir ?? "/tmp/openclaw", fallbackPrefix: "openclaw", warningPrefix: "[openclaw]", unsafeFallbackLabel: "OpenClaw temp dir", skipPreferredOnWindows: true }; const r = t(n); if (e.purpose === "coordination") return r; let i; try { assertSafeTempAncestors(r, e); return r; } catch (e) { i = e; } const a = resolveStateDir(); try { const r = t({ ...n, preferredDir: void 0, tmpdir: () => path.join(a, "tmp") }); assertSafeTempAncestors(r, e); (e.warn ?? console.warn)(`[openclaw] ${String(i)} Using private state temp dir: ${r}`); return r; } catch (e) { throw new Error(`No safe OpenClaw temp root is available. Default root: ${String(i)} State fallback ${JSON.stringify(a)}: ${String(e)}`, { cause: e }); } } //#endregion export { getSealedRuntimeJson5 as a, resolveGatewayLockDirForCanonicalStateDir as c, resolvePreferredOpenClawTmpDir as i, resolveIncludeRoots as l, openRootFileSync as n, resolveConfigPath as o, DEFAULT_POSIX_TMP_ROOT as r, resolveGatewayLockDir as s, canUseRootFileOpen as t, resolveOAuthDir as u };