flake-update-20260505
1/**
2 * Agent discovery and configuration
3 */
4
5import * as fs from "node:fs";
6import * as os from "node:os";
7import * as path from "node:path";
8import { parseFrontmatter } from "@mariozechner/pi-coding-agent";
9
10export type AgentScope = "user" | "project" | "both";
11
12export interface AgentConfig {
13 name: string;
14 description: string;
15 tools?: string[];
16 model?: string;
17 systemPrompt: string;
18 source: "user" | "project";
19 filePath: string;
20}
21
22export interface AgentDiscoveryResult {
23 agents: AgentConfig[];
24 projectAgentsDir: string | null;
25}
26
27function loadAgentsFromDir(dir: string, source: "user" | "project"): AgentConfig[] {
28 const agents: AgentConfig[] = [];
29
30 if (!fs.existsSync(dir)) {
31 return agents;
32 }
33
34 let entries: fs.Dirent[];
35 try {
36 entries = fs.readdirSync(dir, { withFileTypes: true });
37 } catch {
38 return agents;
39 }
40
41 for (const entry of entries) {
42 if (!entry.name.endsWith(".md")) continue;
43 if (!entry.isFile() && !entry.isSymbolicLink()) continue;
44
45 const filePath = path.join(dir, entry.name);
46 let content: string;
47 try {
48 content = fs.readFileSync(filePath, "utf-8");
49 } catch {
50 continue;
51 }
52
53 const { frontmatter, body } = parseFrontmatter<Record<string, string>>(content);
54
55 if (!frontmatter.name || !frontmatter.description) {
56 continue;
57 }
58
59 const tools = frontmatter.tools
60 ?.split(",")
61 .map((t: string) => t.trim())
62 .filter(Boolean);
63
64 agents.push({
65 name: frontmatter.name,
66 description: frontmatter.description,
67 tools: tools && tools.length > 0 ? tools : undefined,
68 model: frontmatter.model,
69 systemPrompt: body,
70 source,
71 filePath,
72 });
73 }
74
75 return agents;
76}
77
78function isDirectory(p: string): boolean {
79 try {
80 return fs.statSync(p).isDirectory();
81 } catch {
82 return false;
83 }
84}
85
86function findNearestProjectAgentsDir(cwd: string): string | null {
87 let currentDir = cwd;
88 while (true) {
89 const candidate = path.join(currentDir, ".pi", "agents");
90 if (isDirectory(candidate)) return candidate;
91
92 const parentDir = path.dirname(currentDir);
93 if (parentDir === currentDir) return null;
94 currentDir = parentDir;
95 }
96}
97
98export function discoverAgents(cwd: string, scope: AgentScope): AgentDiscoveryResult {
99 const userDir = path.join(os.homedir(), ".pi", "agent", "agents");
100 const projectAgentsDir = findNearestProjectAgentsDir(cwd);
101
102 const userAgents = scope === "project" ? [] : loadAgentsFromDir(userDir, "user");
103 const projectAgents = scope === "user" || !projectAgentsDir ? [] : loadAgentsFromDir(projectAgentsDir, "project");
104
105 const agentMap = new Map<string, AgentConfig>();
106
107 if (scope === "both") {
108 for (const agent of userAgents) agentMap.set(agent.name, agent);
109 for (const agent of projectAgents) agentMap.set(agent.name, agent);
110 } else if (scope === "user") {
111 for (const agent of userAgents) agentMap.set(agent.name, agent);
112 } else {
113 for (const agent of projectAgents) agentMap.set(agent.name, agent);
114 }
115
116 return { agents: Array.from(agentMap.values()), projectAgentsDir };
117}
118
119export function formatAgentList(agents: AgentConfig[], maxItems: number): { text: string; remaining: number } {
120 if (agents.length === 0) return { text: "none", remaining: 0 };
121 const listed = agents.slice(0, maxItems);
122 const remaining = agents.length - listed.length;
123 return {
124 text: listed.map((a) => `${a.name} (${a.source}): ${a.description}`).join("; "),
125 remaining,
126 };
127}