agents.ts 3.3 KB

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