UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

109 lines • 4.47 kB
// What the file tools need from the host they run for, with defaults that need no Node runtime. The local tools // pass their Node implementations (path-utils.ts, the machine-ops mutation queue and realpath canonicalization). const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g; function normalizePosixPath(path) { const parts = []; for (const part of path.split("/")) { if (part === "" || part === ".") continue; if (part === "..") parts.pop(); else parts.push(part); } return `/${parts.join("/")}`; } /** * POSIX resolution as `path.posix.resolve` does it, after the same input clean-up the local resolver applies (Unicode * spaces, a leading `@`, `file://` URLs). `~` is not expanded: the host's home directory is unknown here. */ export const resolvePosixToolPath = (input, cwd) => { let path = input.replace(UNICODE_SPACES, " "); if (path.startsWith("@")) path = path.slice(1); if (path.startsWith("file://")) path = decodeURIComponent(new URL(path).pathname); return normalizePosixPath(path.startsWith("/") ? path : `${cwd}/${path}`); }; export function posixDirname(path) { const trimmed = path.length > 1 ? path.replace(/\/+$/, "") : path; const slash = trimmed.lastIndexOf("/"); if (slash === -1) return "."; return slash === 0 ? "/" : trimmed.slice(0, slash); } const pathTails = new Map(); /** Serializes mutations per resolved path string within this isolate. */ export const inProcessFileMutationQueue = async (absolutePath, fn) => { const previous = pathTails.get(absolutePath) ?? Promise.resolve(); let release = () => { }; const current = new Promise((resolve) => { release = resolve; }); const tail = previous.then(() => current); pathTails.set(absolutePath, tail); await previous; try { return await fn(); } finally { release(); if (pathTails.get(absolutePath) === tail) pathTails.delete(absolutePath); } }; export const lexicalCanonicalPath = async (absolutePath) => absolutePath; const NARROW_NO_BREAK_SPACE = "\u202F"; export function tryMacOSScreenshotPath(filePath) { return filePath.replace(/ (AM|PM)\./gi, `${NARROW_NO_BREAK_SPACE}$1.`); } export function tryNFDVariant(filePath) { // macOS stores filenames in NFD (decomposed) form, try converting user input to NFD return filePath.normalize("NFD"); } export function tryCurlyQuoteVariant(filePath) { // macOS uses U+2019 (right single quotation mark) in screenshot names like "Capture d'écran" // Users typically type U+0027 (straight apostrophe) return filePath.replace(/'/g, "\u2019"); } // Windows Explorer "Copy as path" wraps the path in double quotes (senpi#2170). export function unquotedPath(filePath) { const match = /^@?(["'])(.+)\1$/.exec(filePath.trim()); return match?.[2]; } /** * The read tool's path: the resolved path when it exists, else the first existing spelling variant (quoted, * macOS screenshot AM/PM, NFD, curly quote), else the resolved path. */ export async function resolveReadPathVariants(filePath, cwd, resolve, exists) { const resolved = resolve(filePath, cwd); if (await exists(resolved)) { return resolved; } const unquoted = unquotedPath(filePath); if (unquoted !== undefined) { return resolveReadPathVariants(unquoted, cwd, resolve, exists); } // Try macOS AM/PM variant (narrow no-break space before AM/PM) const amPmVariant = tryMacOSScreenshotPath(resolved); if (amPmVariant !== resolved && (await exists(amPmVariant))) { return amPmVariant; } // Try NFD variant (macOS stores filenames in NFD form) const nfdVariant = tryNFDVariant(resolved); if (nfdVariant !== resolved && (await exists(nfdVariant))) { return nfdVariant; } // Try curly quote variant (macOS uses U+2019 in screenshot names) const curlyVariant = tryCurlyQuoteVariant(resolved); if (curlyVariant !== resolved && (await exists(curlyVariant))) { return curlyVariant; } // Try combined NFD + curly quote (for French macOS screenshots like "Capture d'écran") const nfdCurlyVariant = tryCurlyQuoteVariant(nfdVariant); if (nfdCurlyVariant !== resolved && (await exists(nfdCurlyVariant))) { return nfdCurlyVariant; } return resolved; } //# sourceMappingURL=file-tool-host.js.map