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