UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

158 lines (157 loc) • 6.38 kB
import { existsSync, lstatSync, statSync } from "node:fs"; import { homedir } from "node:os"; import { isAbsolute, join, posix, resolve, sep, win32 } from "node:path"; import { anchorAtLinkDir } from "../engine/execution-env/symlink-chain.js"; import { noticeOncePerSink } from "./notice-once-per-seat.js"; import { receiptRefusal } from "./own-property-receipt.js"; import { physicalPathOf } from "./physical-path.js"; import { describeRejectedValue } from "./tool-search-setting.js"; export function dataRootSpelling(explicit) { return dataRootWrittenFrom(explicit, () => process.env.AGENT_DATA_DIR, homedir).normalize("NFC"); } export function dataRootWrittenFrom(explicit, agentDataDir, home) { return explicit ?? agentDataDir() ?? join(home(), ".ai-agent"); } function refusal(code, message) { return Object.assign(new Error(message), { code }); } export function dataRootLocation(explicit, anchor) { return walkedDataRoot(explicit, anchor).location; } export function walkedDataRoot(explicit, anchor) { if (explicit === undefined && anchor !== undefined) throw receiptRefusal(NO_ROOT_FOR_ANCHOR); const spelled = dataRootSpelling(explicit); if (spelled === "") throw refusal("ENOENT", "the data root is an empty path"); if (isAbsolute(spelled)) return { spelling: spelled, location: walkDataRoot(spelled, spelled) }; const at = readDataRootAnchor(anchor); if (at !== undefined) { let directory; try { directory = statSync(at).isDirectory(); } catch (err) { if (err.code === "ENOENT") throw refusal("ENOENT", `the data root's anchor ${at} does not exist — an anchor is read, never made`); throw err; } if (!directory) throw refusal("ENOTDIR", `the data root's anchor ${at} is not a directory`); } const from = at ?? process.cwd(); const abs = process.platform === "win32" ? resolve(from, spelled) : anchorAtLinkDir(from, spelled, sep); return { spelling: at === undefined ? spelled : abs, location: walkDataRoot(abs, spelled) }; } const NO_ROOT_FOR_ANCHOR = "an anchor was given with no data root: an anchor places the relative root given with it, and a root read from AGENT_DATA_DIR or the home directory is not placed by one"; function readDataRootAnchor(anchor) { if (anchor === undefined) return undefined; if (typeof anchor !== "string" || !isAbsolute(anchor)) { throw receiptRefusal(`the data root's anchor must be a non-empty absolute path, the directory a relative root is placed under (got ${describeRejectedValue(anchor)})`); } return anchor; } const PATH_API = process.platform === "win32" ? win32 : posix; export function walkSplitOf(abs, api) { const root = api.parse(abs).root; return { root, names: abs.slice(root.length).split(api.sep === "\\" ? /[\\/]/ : "/") }; } function walkDataRoot(abs, spelled) { const { root, names } = walkSplitOf(abs, PATH_API); const top = bare(root); let at = top; const toMake = []; const placeOf = (name) => { try { return `${bare(physicalPathOf(at === top ? root : at))}${PATH_API.sep}${name}`; } catch { return `${at}${PATH_API.sep}${name}`; } }; for (const part of names) { if (part === "" || part === ".") continue; if (part === "..") { if (toMake.length > 0) toMake.pop(); else at = `${at}${PATH_API.sep}..`; continue; } if (toMake.length > 0) { toMake.push(part); continue; } const next = `${at}${PATH_API.sep}${part}`; let link; try { link = lstatSync(next).isSymbolicLink(); } catch (err) { if (err.code === "ENOENT") { toMake.push(part); continue; } throw err; } let directory; try { directory = statSync(next).isDirectory(); } catch (err) { if (link && err.code === "ENOENT") throw refusal("ENOENT", `the data root ${spelled} leads through a link to nowhere (${placeOf(part)})`); throw err; } if (!directory) throw refusal("ENOTDIR", `the data root ${spelled} leads through a file, not a directory (${placeOf(part)})`); at = next; } const reached = physicalPathOf(at === top ? root : at); return toMake.length === 0 ? reached : [bare(reached), ...toMake].join(PATH_API.sep); } function bare(p) { return p.endsWith("/") || (PATH_API.sep === "\\" && p.endsWith("\\")) ? p.slice(0, -1) : p; } export function splitSpellingOf(written, seat, anchor) { const split = dataRootSpellingSplitOf(written, anchor); if (split === undefined) return undefined; let fresh = false; noticeOncePerSink(seat, `config.data_root_spelling_split ${resolve(split.opened)}`, () => { fresh = true; }); return fresh ? split : undefined; } export function dataRootSpellingSplitOf(written, anchor) { if (typeof written !== "string") throw receiptRefusal(`dataRootSpellingSplitOf: the root as written must be a string (got ${describeRejectedValue(written)})`); const at = isAbsolute(written) ? undefined : readDataRootAnchor(anchor); const opened = written.normalize("NFC"); if (opened === written) return undefined; const writtenAbs = isAbsolute(written) ? written : at === undefined ? resolve(written) : resolve(at, written); const openedAbs = isAbsolute(opened) ? opened : at === undefined ? resolve(opened) : resolve(at, opened); let writtenIsDir = false; try { writtenIsDir = statSync(writtenAbs).isDirectory(); } catch { return undefined; } if (!writtenIsDir) return undefined; try { const a = statSync(writtenAbs, { bigint: true }); const b = existsSync(openedAbs) ? statSync(openedAbs, { bigint: true }) : undefined; if (b !== undefined && a.dev === b.dev && a.ino === b.ino) return undefined; return { written: writtenAbs, opened: openedAbs, state: b === undefined ? "moved" : "both" }; } catch { return undefined; } }