UNPKG

@phren/cli

Version:

Knowledge layer for AI agents - CLI, MCP server, and data layer

86 lines (85 loc) • 3.94 kB
import path from "node:path"; import { recentChatCheckouts } from "./chat-files.js"; import { normalizeFileReference, resolveFilePath, resolveRepositoryFile } from "./file-resolve.js"; import { readFileRange } from "./file-range.js"; import { git } from "./projects.js"; import { BridgeError } from "./protocol.js"; const MAX_MATCHES = 12; const wireRoot = (root) => root.split(path.sep).join("/"); const isStatus = (error, ...statuses) => error instanceof BridgeError && statuses.includes(error.status); export async function recentFileRepositories(folders) { const checkouts = await recentChatCheckouts(folders); return new Map(checkouts.map(({ root, folders }) => [root, folders])); } export async function chatFileMatches(root, cwd, requested, getRecent) { const name = normalizeFileReference(requested); try { return { matches: [await resolveFilePath(root, cwd, requested)] }; } catch (error) { // Unsafe relative paths must not fall through to another checkout. if (!isStatus(error, 404, 409) && !(path.isAbsolute(name) && isStatus(error, 403) && error instanceof BridgeError && error.message === "This file is outside the repository.")) throw error; } if (name.split("/").includes("..")) return { matches: [] }; const roots = await getRecent(); if (root && !roots.has(root)) roots.set(root, [cwd]); const matches = []; const bare = !name.includes("/"); for (const [checkout, folders] of roots) { const seen = new Set(); const recentRoot = checkout === root ? undefined : wireRoot(checkout); const add = (match) => { const key = process.platform === "win32" ? match.path.toLowerCase() : match.path; if (seen.has(key) || matches.length >= MAX_MATCHES) return; seen.add(key); matches.push({ ...match, recentRoot }); }; for (const folder of folders) { try { const match = await resolveRepositoryFile(checkout, folder, name); if (match) add(match); } catch (error) { if (!isStatus(error, 400, 403, 404)) throw error; } } if (bare && matches.length < MAX_MATCHES) { // Exact basenames in tracked and unignored files. Escape Git pathspec // metacharacters even though most chat-generated links have none. const escaped = name.replace(/[\[\]*?]/g, "\\$&"); const magic = process.platform === "win32" ? "glob,icase" : "glob"; const entries = await git(checkout, "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", `:(${magic})**/${escaped}`); for (const entry of new Set(entries.split("\0").filter(Boolean))) { try { const match = await resolveRepositoryFile(checkout, checkout, normalizeFileReference(entry)); if (match) add(match); } catch (error) { if (!isStatus(error, 400, 403, 404)) throw error; } if (matches.length >= MAX_MATCHES) break; } } if (matches.length >= MAX_MATCHES) break; } return { matches }; } export async function readRecentChatFile(roots, root, file, offset, length, version) { const checkout = [...roots.keys()].find(candidate => wireRoot(candidate) === root); if (!checkout) throw new BridgeError(404, "This checkout is no longer in the session's recent folders."); if (file.includes(":") || file.split("/").some(part => !part || part === ".")) throw new BridgeError(400, "Invalid file path."); return readFileRange(checkout, file, offset, length, version); }