UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

138 lines (137 loc) • 4.49 kB
import { s as relativePathEscapesRoot } from "./path-guards-TIjxtj6H.mjs"; import { o as normalizeFileReferencePrefix, u as resolveSandboxInputPath } from "./sandbox-paths-CGD5HJYL.mjs"; import { a as resolvePathFromInput, n as preserveAtPrefixedRelativePath } from "./path-policy-CyYRyWre.mjs"; import path from "node:path"; //#region src/agents/apply-patch-paths.ts /** * Input resolution, path extraction, and display for the apply_patch envelope grammar. * Used by pre-execution policy hooks that only need destination paths, not the * full strict patch parser. */ const ADD_FILE_MARKER = "*** Add File: "; const DELETE_FILE_MARKER = "*** Delete File: "; const UPDATE_FILE_MARKER = "*** Update File: "; const MOVE_TO_MARKER = "*** Move to: "; function readPatchText(e) { if (typeof e === "string") return e; if (e && typeof e === "object" && "input" in e) { const t = e.input; return typeof t === "string" ? t : void 0; } } function normalizeMarkerHeaderLine(e) { if (e === void 0) return; const t = e.trimStart(); return t.startsWith("***") ? t.trimEnd() : void 0; } function readMarkerPath(e, t) { const n = normalizeMarkerHeaderLine(e); return n?.startsWith(t) ? n.slice(t.length) : void 0; } /** Walk the executor-compatible envelope grammar without resolving its paths. */ function extractApplyPatchTargets(e) { const t = readPatchText(e); if (!t) return []; const n = t.split(/\r?\n/); const r = []; for (let e = 0; e < n.length; e += 1) { const t = n.at(e); const i = readMarkerPath(t, ADD_FILE_MARKER); if (i !== void 0) { r.push({ kind: "add", path: i }); while (e + 1 < n.length && n.at(e + 1)?.startsWith("+")) e += 1; continue; } const a = readMarkerPath(t, DELETE_FILE_MARKER); if (a !== void 0) { r.push({ kind: "delete", path: a }); continue; } const o = readMarkerPath(t, UPDATE_FILE_MARKER); if (o === void 0) continue; r.push({ kind: "update", path: o }); let s = e + 1; while (s < n.length && n.at(s)?.trim() === "") s += 1; const c = readMarkerPath(n.at(s), MOVE_TO_MARKER); if (c !== void 0) { r.push({ kind: "move", path: c }); s += 1; } while (s < n.length) { const e = n.at(s); if (e === void 0 || e.startsWith("***")) break; s += 1; } e = s - 1; } return r; } function toDisplayPath(e, t) { const n = path.relative(t, e); if (!n) return path.basename(e); if (relativePathEscapesRoot(n)) return e; return n; } /** Resolve a patch input through the same literal-@ policy used by execution. */ async function resolveApplyPatchInputPath(e, t = {}) { const n = t.cwd ?? t.sandbox?.root ?? process.cwd(); const r = await preserveAtPrefixedRelativePath(e, n, t.sandbox?.bridge, t.signal); if (!e.startsWith("@") || r !== e) return r; const i = normalizeFileReferencePrefix(e); return i === "~" || i.startsWith("~/") || i.startsWith("~\\") ? resolvePathFromInput(e, n) : i; } function normalizePatchPath(e, t = {}) { if (e.length === 0) return; const n = t.cwd ?? t.sandbox?.root ?? process.cwd(); try { const r = preserveAtPrefixedRelativePath(e, n); const i = t.sandbox ? t.sandbox.bridge.resolvePath({ filePath: r, cwd: n }) : void 0; const a = path.normalize(i ? i.hostPath ?? i.containerPath : resolveSandboxInputPath(r, n)); return a && a !== "." ? a : void 0; } catch { return; } } /** Resolve distinct target paths without admitting or executing the patch. */ function extractApplyPatchTargetPaths(e, t = {}) { const n = /* @__PURE__ */ new Set(); for (const r of extractApplyPatchTargets(e)) { const e = normalizePatchPath(r.path, t); if (e) n.add(e); } return [...n]; } /** Derive policy-visible paths using the asynchronous resolver used by execution. */ async function extractResolvedApplyPatchTargetPaths(e, t = {}) { const n = /* @__PURE__ */ new Set(); const r = t.cwd ?? t.sandbox?.root ?? process.cwd(); for (const i of extractApplyPatchTargets(e)) try { const e = await resolveApplyPatchInputPath(i.path, t); const a = t.sandbox?.bridge.resolvePath({ filePath: e, cwd: r }); const o = a?.hostPath ? path.normalize(a.hostPath) : a ? path.posix.normalize(a.containerPath) : path.normalize(resolveSandboxInputPath(e, r)); if (o && o !== ".") n.add(o); } catch { t.signal?.throwIfAborted(); } return [...n]; } //#endregion export { toDisplayPath as i, extractResolvedApplyPatchTargetPaths as n, resolveApplyPatchInputPath as r, extractApplyPatchTargetPaths as t };