64875
Mc
mini-coder / files
mini-coder / src / prompt.ts
2.4 KB
1import { existsSync, readdirSync, readFileSync } from "node:fs"; 2import { homedir } from "node:os"; 3import { join, resolve } from "node:path"; 4import type { Config } from "./config.ts"; 5 6interface Skill { 7 name: string; 8 description: string; 9 path: string; 10} 11 12function frontmatter(text: string): { name?: string; description?: string } { 13 const match = text.match(/^---\r?\n([\s\S]*?)\r?\n---/); 14 if (!match) return {}; 15 const out: { name?: string; description?: string } = {}; 16 for (const line of match[1].split(/\r?\n/)) { 17 const separator = line.indexOf(":"); 18 if (separator < 0) continue; 19 const key = line.slice(0, separator).trim(); 20 const value = line.slice(separator + 1).trim().replace(/^["']|["']$/g, ""); 21 if (key === "name") out.name = value; 22 if (key === "description") out.description = value; 23 } 24 return out; 25} 26 27function discoverSkills(dirs: string[]): Skill[] { 28 const skills: Skill[] = []; 29 for (const root of dirs) { 30 let entries: string[]; 31 try { 32 entries = readdirSync(root); 33 } catch { 34 continue; 35 } 36 for (const entry of entries) { 37 const path = resolve(root, entry, "SKILL.md"); 38 if (!existsSync(path)) continue; 39 const meta = frontmatter(readFileSync(path, "utf8")); 40 if (!meta.name) continue; 41 skills.push({ name: meta.name, description: meta.description ?? "", path }); 42 } 43 } 44 return skills; 45} 46 47function agentFiles(): { path: string; text: string }[] { 48 const roots = [join(homedir(), ".agents"), process.cwd()]; 49 const found: { path: string; text: string }[] = []; 50 for (const root of roots) { 51 for (const name of ["AGENTS.md", "CLAUDE.md"]) { 52 const path = join(root, name); 53 if (existsSync(path)) found.push({ path, text: readFileSync(path, "utf8").trim() }); 54 } 55 } 56 return found; 57} 58 59export function buildSystemPrompt(config: Config): string { 60 const sections: string[] = []; 61 if (config.systemPrompt.trim()) sections.push(config.systemPrompt.trim()); 62 63 if (config.skillsDirs.length > 0) { 64 const skills = discoverSkills(config.skillsDirs); 65 if (skills.length > 0) { 66 sections.push( 67 "## Skills\n\n" + skills.map((s) => `- ${s.name}: ${s.description} (${s.path})`).join("\n"), 68 ); 69 } 70 } 71 72 if (config.discoverAgentFiles) { 73 for (const file of agentFiles()) { 74 sections.push(`## ${file.path}\n\n${file.text}`); 75 } 76 } 77 78 return sections.join("\n\n"); 79}