UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

26 lines (25 loc) • 1.05 kB
import { isWinFormPath, pathFamilyOf } from "../../tools/fs/safety.js"; export function rebaseWorkspacePath(p, fromRaw, toRaw) { if (p.includes("\\") || fromRaw.includes("\\") || toRaw.includes("\\")) return p; if (pathFamilyOf({ root: fromRaw }) !== "posix" || pathFamilyOf({ root: toRaw }) !== "posix" || isWinFormPath(p)) return p; const stripTrail = (s) => (s.length > 1 && s.endsWith("/") ? stripTrail(s.slice(0, -1)) : s); const from = stripTrail(fromRaw); const to = stripTrail(toRaw); if (p === from || stripTrail(p) === from) return to; const fromPrefix = from === "/" ? from : `${from}/`; if (!p.startsWith(fromPrefix)) return p; const suffix = p.slice(fromPrefix.length).replace(/^\/+/, ""); return to === "/" ? `${to}${suffix}` : `${to}/${suffix}`; } export function rebaseWorkspacePathAcross(p, froms, to) { for (const from of froms) { const out = rebaseWorkspacePath(p, from, to); if (out !== p) return out; } return p; }