UNPKG

@phren/cli

Version:

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

104 lines (103 loc) • 4.63 kB
import * as fs from "fs"; import * as path from "path"; import { withFileLock } from "../governance/locks.js"; import { validateSkillFrontmatter } from "../link/skills.js"; import { atomicWriteText } from "../phren-paths.js"; import { isValidProjectName, safeProjectPath } from "../utils.js"; import { findLocalSkill } from "./registry.js"; function proposalDirectory(phrenPath, scope) { let dir = phrenPath; for (const segment of scope ? [".runtime", "skill-proposals", scope] : [".runtime", "skill-proposals"]) { dir = path.join(dir, segment); const stat = fs.lstatSync(dir, { throwIfNoEntry: false }); if (stat && !stat.isDirectory()) throw new Error("Invalid skill proposal directory."); } return dir; } function proposalTarget(phrenPath, scope, name) { scope = scope.toLowerCase() === "global" ? "global" : scope; if (!isValidProjectName(scope) || scope !== scope.trim()) { throw new Error(`Invalid scope: "${scope}". Use 'global' or a project name.`); } const skillName = name.replace(/\.md$/i, ""); if (!/^[a-z0-9][a-z0-9_-]{0,99}$/i.test(skillName) || /^(?:con|prn|aux|nul|com[0-9]|lpt[0-9])$/i.test(skillName)) { throw new Error(`Invalid skill name: "${name}". Use letters, numbers, hyphens, or underscores.`); } const file = path.join(proposalDirectory(phrenPath, scope), `${skillName.toLowerCase()}.md`); return { file, scope, name: skillName }; } function liveSkillPath(phrenPath, scope, name) { const projectDir = safeProjectPath(phrenPath, scope); if (!projectDir || (scope !== "global" && !fs.statSync(projectDir, { throwIfNoEntry: false })?.isDirectory())) { throw new Error(`Project "${scope}" not found.`); } const destDir = safeProjectPath(phrenPath, scope, "skills"); if (!destDir) throw new Error("Skill path escapes phren store."); const existing = findLocalSkill(phrenPath, scope, name); const dest = safeProjectPath(destDir, existing ? path.relative(destDir, existing.path) : `${name}.md`); if (!dest) throw new Error("Skill path escapes phren store."); if (fs.lstatSync(dest, { throwIfNoEntry: false })?.isSymbolicLink()) { throw new Error("Refusing to write through a symlinked skill path."); } return dest; } function validateContent(content) { const { valid, errors } = validateSkillFrontmatter(content); if (!valid) throw new Error(`Invalid frontmatter: ${errors.join("; ")}`); } function readProposal(file) { const stat = fs.lstatSync(file, { throwIfNoEntry: false }); if (!stat) return undefined; if (!stat.isFile()) throw new Error("Invalid skill proposal file."); return fs.readFileSync(file, "utf8"); } export function proposeSkill(phrenPath, scope, name, content) { const target = proposalTarget(phrenPath, scope, name); validateContent(content); liveSkillPath(phrenPath, target.scope, target.name); withFileLock(target.file, () => { const existing = readProposal(target.file); if (existing === content) return; if (existing !== undefined) { throw new Error("A different proposal is already pending for this skill. Review or reject it before submitting another."); } atomicWriteText(target.file, content, { mode: 0o600 }); }); return target; } export function listSkillProposals(phrenPath) { const root = proposalDirectory(phrenPath); if (!fs.existsSync(root)) return []; const proposals = []; for (const scope of fs.readdirSync(root, { withFileTypes: true })) { if (!scope.isDirectory() || !isValidProjectName(scope.name)) continue; for (const entry of fs.readdirSync(path.join(root, scope.name), { withFileTypes: true })) { if (!entry.isFile() || !entry.name.endsWith(".md")) continue; proposals.push({ scope: scope.name, name: entry.name.slice(0, -3), file: path.join(root, scope.name, entry.name) }); } } return proposals; } export function resolveSkillProposal(phrenPath, scope, name, approve) { const target = proposalTarget(phrenPath, scope, name); withFileLock(target.file, () => { const content = readProposal(target.file); if (content === undefined) throw new Error(`No pending skill "${name}" in ${target.scope}.`); if (approve) { validateContent(content); atomicWriteText(liveSkillPath(phrenPath, target.scope, target.name), content); } fs.unlinkSync(target.file); }); }