UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

171 lines (170 loc) • 7.01 kB
import { isDriveAbsoluteSpelling } from "@sema-agent/primitives"; export function isUncSpelling(p) { return /^[\\/]{2}[^\\/]+[\\/]+[^\\/]/.test(p); } export function localDriveSpellingOf(p, family) { if ((family ?? (isWinFormPath(p) ? "win" : "posix")) !== "win") return undefined; const s = stripWin32ExtendedPrefix(win32NamespacePrefixFolded(p)); if (/^\\\\\.\\/.test(s)) { const read = belowTheRoot(s.slice(4), (kept) => (/^UNC$/i.test(kept[0] ?? "") ? 3 : 1), "refuse"); const volume = read?.segs ?? []; const head = volume[0]; const sep = read?.trailing === true ? "\\" : ""; const below = volume.slice(1).join("\\"); if (head !== undefined && isBareDriveSpelling(head)) return read.depth > 1 || sep !== "" ? `${head.toUpperCase()}\\${below}${below !== "" ? sep : ""}` : undefined; return head !== undefined && /^UNC$/i.test(head) ? localDriveSpellingOf(`\\\\${volume.slice(1).join("\\")}${sep}`, "win") : undefined; } const drive = /^([A-Za-z]):[\\/]([\s\S]*)$/.exec(s); const unc = drive !== null ? null : /^[\\/]{2}([^\\/]+)[\\/]+([^\\/]+)(?:[\\/]+([\s\S]*))?$/.exec(s); if (drive === null && (unc === null || !/^[A-Za-z]\$$/.test(unc[2]) || /^[?.]$/.test(unc[1]))) return undefined; const below = belowTheRoot(drive !== null ? drive[2] : (unc[3] ?? ""), () => 0, "clamp"); return `${(drive !== null ? drive[1] : unc[2][0]).toUpperCase()}:\\${below.segs.join("\\")}${below.segs.length > 0 && below.trailing ? "\\" : ""}`; } function belowTheRoot(below, floor, atRoot) { let trailing = /[\\/]$/.test(below); const segs = below.split(/[\\/]+/).filter((seg) => seg !== ""); const kept = []; let depth = 0; for (const [i, seg] of segs.entries()) { if (seg === ".") continue; if (seg === "..") { if (depth > floor(kept)) (kept.push(seg), depth--); else if (atRoot === "refuse") return undefined; continue; } const trimmed = seg.replace(/([^.])\.$/, "$1"); const last = i === segs.length - 1 && !trailing ? trimmed.replace(/[. ]+$/, "") : trimmed; if (last === "") trailing = true; else (kept.push(last), depth++); } return { segs: kept, trailing, depth }; } export function isDeviceSpelling(p) { return /^[\\/]{2}\.[\\/]/.test(p); } export function win32NamespacePrefixFolded(p) { return /^[\\/]{2}[?.][\\/]/.test(p) && !/^\\\\[?.]\\/.test(p) ? p.replace(/\//g, "\\") : p; } export function pathFamilyOf(bases) { const tree = bases.root ?? bases.cwd; return tree === undefined ? undefined : isWinFormPath(tree) ? "win" : "posix"; } export function familyFor(source, p) { return source === "own" ? (isWinFormPath(p) ? "win" : "posix") : source; } export function isAbsoluteForFamily(family, p) { return familyFor(family, p) === "win" ? isDriveAbsoluteSpelling(p) || isUncSpelling(p) : p.startsWith("/"); } export const WIN_RESERVED_RE = /^(CON|PRN|AUX|NUL|COM[0-9¹²³]|LPT[0-9¹²³]|CONIN\$|CONOUT\$)(\.[^\\/]*)?$/i; export function isWinFormPath(p) { return isDriveAbsoluteSpelling(p) || p.startsWith("\\\\") || (!p.startsWith("/") && p.includes("\\")); } export function foldDriveLetter(p) { return /^[A-Za-z]:(\/|$)/.test(p) ? p.charAt(0).toLowerCase() + p.slice(1) : p; } export function isBareDriveSpelling(p) { return /^[A-Za-z]:$/.test(p); } export function nativeSepOf(p) { return isWinFormPath(p) && p.includes("\\") ? "\\" : "/"; } export function normalizeAbsPathLexically(p) { return foldSegmentsLexically(p, true); } export function foldRedundantPathSegments(p) { return foldSegmentsLexically(p, false); } function foldSegmentsLexically(p, foldParents) { p = stripWin32ExtendedPrefix(p); const isUnc = p.startsWith("\\\\"); const drive = /^[A-Za-z]:/.exec(p)?.[0] ?? ""; const winForm = isUnc || drive !== "" || (!p.startsWith("/") && p.includes("\\")); const sep = winForm && p.includes("\\") ? "\\" : "/"; const splitter = winForm ? /[\\/]+/ : /\/+/; let prefix = drive; let rest = p.slice(drive.length + (isUnc ? 2 : 0)); let abs = rest.startsWith("/") || (winForm && rest.startsWith("\\")); if (isUnc) { const segs = rest.split(splitter).filter((seg) => seg !== ""); const host = segs.shift() ?? ""; const share = segs.shift(); prefix = sep + sep + host + (share === undefined ? "" : sep + share); rest = segs.join(sep); abs = true; } const outSegs = []; for (const seg of rest.split(splitter)) { if (seg === "" || seg === ".") continue; if (seg === "..") { if (!foldParents) { outSegs.push(seg); continue; } if (outSegs.length > 0 && outSegs[outSegs.length - 1] !== "..") outSegs.pop(); else if (!abs) outSegs.push(".."); continue; } outSegs.push(seg); } if (outSegs.length === 0) return abs ? (isUnc ? prefix : prefix + sep) : prefix === "" ? "." : prefix; return prefix + (abs ? sep : "") + outSegs.join(sep); } export function stripWin32ExtendedPrefix(p) { if (!p.startsWith("\\\\?\\")) return p; const rest = p.slice(4); const unc = /^UNC\\/i.exec(rest); const body = unc !== null ? rest.slice(unc[0].length) : /^[A-Za-z]:\\/.test(rest) ? rest.slice(3) : undefined; if (body === undefined) return p; if (body !== "") { for (const seg of body.split("\\")) { if (seg === "" || seg === "." || seg === "..") return p; if (seg.endsWith(".") || seg.endsWith(" ") || seg.includes("/")) return p; if (WIN_RESERVED_RE.test(seg)) return p; } } return unc ? "\\\\" + rest.slice(unc[0].length) : rest; } export function rejoinMissingTail(canonical, tail, family) { const trimmed = (at) => at.replace(family === "win" ? /[\\/]+$/ : /\/+$/, ""); let at = canonical; const names = []; for (let i = 0; i < tail.length; i++) { const seg = tail[i]; if (seg === "" || seg === ".") continue; if (seg !== "..") names.push(seg); else if (names.length > 1) names.pop(); else { if (names.length === 1) names.pop(); else { const up = normalizeAbsPathLexically(`${at}/..`); if (up !== normalizeAbsPathLexically(at)) at = up; } const rest = tail.slice(i + 1); if (rest.some((s) => s !== "" && s !== ".")) return { reprobe: [trimmed(at), ...rest].join(nativeSepOf(at)) }; } } return { key: names.length === 0 ? at : [trimmed(at), ...names].join(nativeSepOf(at)) }; }