UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

41 lines (40 loc) • 1.33 kB
import { realpathSync } from "node:fs"; import { realpath } from "node:fs/promises"; import { basename, dirname, isAbsolute, sep } from "node:path"; import { anchorAtLinkDir } from "../engine/execution-env/symlink-chain.js"; import { pathFamilyOf, rejoinMissingTail } from "./unc-spelling.js"; export function physicalPathOf(p) { return realpathSync.native(p); } export function priorCallerCasedPathOf(p) { return realpathSync(p); } export async function physicalPathOfAsync(p) { return await realpath(p); } export function physicalPathOfExisting(p) { let at = isAbsolute(p) ? p : anchorAtLinkDir(process.cwd(), p, sep); for (;;) { const tail = []; let cur = at; let resolved; while (resolved === undefined) { try { resolved = physicalPathOf(cur); } catch { const parent = dirname(cur); if (parent === cur) resolved = cur; else { tail.push(basename(cur)); cur = parent; } } } const rejoined = rejoinMissingTail(resolved, tail.reverse(), pathFamilyOf({ root: resolved })); if ("key" in rejoined) return rejoined.key; at = rejoined.reprobe; } }