UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

181 lines (180 loc) • 5.44 kB
import { E as parseStrictNonNegativeInteger, o as asDateTimestampMs } from "./number-coercion-D6u-4YD8.mjs"; import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs"; import { PATH_ALIAS_POLICIES } from "@openclaw/fs-safe/advanced"; import { GUEST_FILESYSTEM_PYTHON } from "@openclaw/fs-safe/guest"; //#region src/infra/directory-entries.ts /** Decode the sandbox directory command's metadata, never file contents. */ function parseDirectoryEntries(e) { const t = JSON.parse(e); if (!Array.isArray(t)) throw new Error("Invalid sandbox directory listing."); return t.map((e) => { const t = asNullableRecord(e); if (!t || typeof t.name !== "string" || typeof t.isDirectory !== "boolean") throw new Error("Invalid sandbox directory entry."); return { name: t.name, isDirectory: t.isDirectory }; }); } //#endregion //#region src/agents/sandbox/fs-bridge-mutation-helper.ts /** * Shell plans for pinned sandbox filesystem operations. * * Selects the local interpreter and supplies quoted Python source to local and remote transports. */ const SANDBOX_PINNED_MUTATION_PYTHON_CANDIDATES = [ "/usr/bin/python3", "/usr/local/bin/python3", "/opt/homebrew/bin/python3", "/bin/python3" ]; const PINNED_MUTATION_ACTION_LABELS = { write: "write files", create: "create files", mkdir: "create directories", remove: "remove files", "copy-destination": "copy files" }; const SANDBOX_PINNED_MUTATION_PYTHON_SHELL_LITERAL = `'${GUEST_FILESYSTEM_PYTHON.replaceAll("'", `'\\''`)}'`; function pinnedEntryArgs(e) { return [ e.mountRootPath, e.relativeParentPath, e.basename ]; } /** Encode only already-admitted pinned facts; transport and path checks stay with each bridge. */ function buildPinnedMutationArgs(e) { switch (e.kind) { case "read": return [ e.kind, ...pinnedEntryArgs(e.pinned), ...e.maxBytes === void 0 ? [] : [String(e.maxBytes)] ]; case "write": case "create": return [ e.kind, ...pinnedEntryArgs(e.pinned), e.mkdir ? "1" : "0" ]; case "mkdirp": case "readdir": return [ e.kind, e.pinned.mountRootPath, e.pinned.relativePath ]; case "remove": return [ e.kind, ...pinnedEntryArgs(e.pinned), e.recursive ? "1" : "0", e.force === false ? "0" : "1" ]; } return [ e.kind, ...pinnedEntryArgs(e.source), ...pinnedEntryArgs(e.destination), e.kind === "rename" || e.mkdir ? "1" : "0" ]; } const PINNED_OPERATION_CHECK_OPTIONS = { write: { action: "write files", requireWritable: true }, create: { action: "create files", requireWritable: true }, readdir: { action: "list directories", allowedType: "directory" }, mkdirp: { action: "create directories", requireWritable: true, allowedType: "directory" }, remove: { action: "remove files", requireWritable: true, allowedType: "file-or-directory" }, rename: { action: "rename files", requireWritable: "subtree", allowedType: "file-or-directory" }, copy: { action: "copy files", requireWritable: true } }; function buildPinnedMutationPlan(e) { const t = { ...PINNED_OPERATION_CHECK_OPTIONS[e.kind] }; if (e.kind === "remove" && e.recursive) t.requireWritable = "subtree"; const n = e.kind === "copy" || e.kind === "rename" ? [{ target: e.sourceTarget, options: e.kind === "copy" ? { action: t.action, allowedType: "file" } : t }, { target: e.destinationTarget, options: t }] : [{ target: e.target, options: t }]; if (e.kind === "remove" || e.kind === "rename") n[0].options = { ...n[0].options, aliasPolicy: PATH_ALIAS_POLICIES.unlinkTarget }; const r = buildPinnedMutationArgs(e); return { checks: n, script: [ "set -eu", "python_cmd=''", ...SANDBOX_PINNED_MUTATION_PYTHON_CANDIDATES.map((e) => `if [ -z "$python_cmd" ] && [ -x '${e}' ]; then python_cmd='${e}'; fi`), "if [ -z \"$python_cmd\" ]; then python_cmd=$(command -v python3 2>/dev/null || command -v python 2>/dev/null || true); fi", "if [ -z \"$python_cmd\" ]; then", " echo >&2 'sandbox pinned mutation helper requires python3 or python'", " exit 127", "fi", `python_script=${SANDBOX_PINNED_MUTATION_PYTHON_SHELL_LITERAL}`, "exec \"$python_cmd\" -c \"$python_script\" \"$@\"" ].join("\n"), args: r }; } //#endregion //#region src/agents/sandbox/fs-bridge-stat-parse.ts /** * Stat output parsers for sandbox filesystem bridges. * * Handles GNU/BSD size and mtime formats returned through backend shell commands. */ function hasMultipleHardlinks(e) { const t = parseStrictNonNegativeInteger(e); return t === void 0 ? /^\d+$/.test(e) : t > 1; } /** Parses file sizes, capping huge integer strings at the largest safe JS integer. */ function parseSandboxStatSize(e) { const t = e ?? "0"; const n = parseStrictNonNegativeInteger(t); if (n !== void 0) return n; return /^\d+$/.test(t) ? Number.MAX_SAFE_INTEGER : 0; } /** Parses stat mtimes from epoch seconds or date strings into millisecond timestamps. */ function parseSandboxStatMtimeMs(e) { const t = e ?? "0"; if (/^\d+(?:\.\d+)?$/.test(t)) { const e = Number(t) * 1e3; return asDateTimestampMs(e) ?? 0; } return asDateTimestampMs(Date.parse(t)) ?? 0; } //#endregion export { SANDBOX_PINNED_MUTATION_PYTHON_SHELL_LITERAL as a, parseDirectoryEntries as c, PINNED_MUTATION_ACTION_LABELS as i, parseSandboxStatMtimeMs as n, buildPinnedMutationArgs as o, parseSandboxStatSize as r, buildPinnedMutationPlan as s, hasMultipleHardlinks as t };