@chorus-aidlc/chorus-pi 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,180 @@
1
+ /**
2
+ * Agent discovery and configuration.
3
+ *
4
+ * Copied from pi's official subagent reference example
5
+ * (earendil-works/pi: packages/coding-agent/examples/extensions/subagent/agents.ts)
6
+ * with ONE chorus-pi customization: `discoverAgents` also loads from a
7
+ * package-relative `agents/` directory (BUNDLED_DIR) so the 3 Chorus reviewer
8
+ * agents that ship inside this package are discovered with ZERO manual copy
9
+ * into ~/.pi/agent/agents/. Everything else is verbatim upstream.
10
+ */
11
+
12
+ import * as fs from "node:fs";
13
+ import * as path from "node:path";
14
+ import { fileURLToPath } from "node:url";
15
+ import { CONFIG_DIR_NAME, getAgentDir, parseFrontmatter } from "@earendil-works/pi-coding-agent";
16
+
17
+ export type AgentScope = "user" | "project" | "both";
18
+
19
+ export interface AgentConfig {
20
+ name: string;
21
+ description: string;
22
+ tools?: string[];
23
+ model?: string;
24
+ systemPrompt: string;
25
+ source: "user" | "project";
26
+ filePath: string;
27
+ }
28
+
29
+ export interface AgentDiscoveryResult {
30
+ agents: AgentConfig[];
31
+ projectAgentsDir: string | null;
32
+ }
33
+
34
+ /**
35
+ * chorus-pi customization: the package's own `agents/` directory, resolved
36
+ * relative to this module. This file lives at `<pkg>/extensions/subagent/agents.ts`,
37
+ * so the bundled `agents/` dir is TWO levels up (`../../agents`). The 3 reviewer
38
+ * agents (chorus-{code,task,proposal}-reviewer.md) ship there and load as
39
+ * user-scope agents without any `cp` into ~/.pi/agent/agents/.
40
+ */
41
+ const BUNDLED_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "..", "agents");
42
+
43
+ /**
44
+ * Raw agent frontmatter. Values are `unknown` because `parseFrontmatter` runs a
45
+ * real YAML parser, so any scalar or collection can appear here.
46
+ *
47
+ * A type alias rather than an interface: `parseFrontmatter` constrains its
48
+ * parameter to `Record<string, unknown>`, and only an alias picks up the
49
+ * implicit index signature that satisfies it.
50
+ */
51
+ type AgentFrontmatter = {
52
+ name?: unknown;
53
+ description?: unknown;
54
+ tools?: unknown;
55
+ model?: unknown;
56
+ };
57
+
58
+ /**
59
+ * Normalize a frontmatter `tools` value to a list of tool names.
60
+ *
61
+ * Both spellings are valid YAML and both are in use:
62
+ *
63
+ * tools: read, bash # string
64
+ * tools: [read, bash] # array
65
+ *
66
+ * so accept either. Anything else (a number, a map, a nested list) yields no
67
+ * tools rather than throwing: this runs inside agent discovery, where a single
68
+ * bad file must not take down every other agent in the same directory.
69
+ */
70
+ function parseToolList(value: unknown): string[] | undefined {
71
+ const raw = Array.isArray(value) ? value : typeof value === "string" ? value.split(",") : [];
72
+ const tools = raw
73
+ .filter((t): t is string => typeof t === "string")
74
+ .map((t) => t.trim())
75
+ .filter(Boolean);
76
+ return tools.length > 0 ? tools : undefined;
77
+ }
78
+
79
+ function loadAgentsFromDir(dir: string, source: "user" | "project"): AgentConfig[] {
80
+ const agents: AgentConfig[] = [];
81
+
82
+ if (!fs.existsSync(dir)) {
83
+ return agents;
84
+ }
85
+
86
+ let entries: fs.Dirent[];
87
+ try {
88
+ entries = fs.readdirSync(dir, { withFileTypes: true });
89
+ } catch {
90
+ return agents;
91
+ }
92
+
93
+ for (const entry of entries) {
94
+ if (!entry.name.endsWith(".md")) continue;
95
+ if (!entry.isFile() && !entry.isSymbolicLink()) continue;
96
+
97
+ const filePath = path.join(dir, entry.name);
98
+ let content: string;
99
+ try {
100
+ content = fs.readFileSync(filePath, "utf-8");
101
+ } catch {
102
+ continue;
103
+ }
104
+
105
+ const { frontmatter, body } = parseFrontmatter<AgentFrontmatter>(content);
106
+
107
+ if (typeof frontmatter.name !== "string" || typeof frontmatter.description !== "string") {
108
+ continue;
109
+ }
110
+
111
+ agents.push({
112
+ name: frontmatter.name,
113
+ description: frontmatter.description,
114
+ tools: parseToolList(frontmatter.tools),
115
+ model: typeof frontmatter.model === "string" ? frontmatter.model : undefined,
116
+ systemPrompt: body,
117
+ source,
118
+ filePath,
119
+ });
120
+ }
121
+
122
+ return agents;
123
+ }
124
+
125
+ function isDirectory(p: string): boolean {
126
+ try {
127
+ return fs.statSync(p).isDirectory();
128
+ } catch {
129
+ return false;
130
+ }
131
+ }
132
+
133
+ function findNearestProjectAgentsDir(cwd: string): string | null {
134
+ let currentDir = cwd;
135
+ while (true) {
136
+ const candidate = path.join(currentDir, CONFIG_DIR_NAME, "agents");
137
+ if (isDirectory(candidate)) return candidate;
138
+
139
+ const parentDir = path.dirname(currentDir);
140
+ if (parentDir === currentDir) return null;
141
+ currentDir = parentDir;
142
+ }
143
+ }
144
+
145
+ export function discoverAgents(cwd: string, scope: AgentScope): AgentDiscoveryResult {
146
+ const userDir = path.join(getAgentDir(), "agents");
147
+ const projectAgentsDir = findNearestProjectAgentsDir(cwd);
148
+
149
+ // User scope = the package's bundled reviewer agents PLUS the user's own
150
+ // ~/.pi/agent/agents. The user dir is loaded LAST so a same-named user agent
151
+ // overrides the bundled one (customization wins). chorus-pi customization.
152
+ const userAgents =
153
+ scope === "project"
154
+ ? []
155
+ : [...loadAgentsFromDir(BUNDLED_DIR, "user"), ...loadAgentsFromDir(userDir, "user")];
156
+ const projectAgents = scope === "user" || !projectAgentsDir ? [] : loadAgentsFromDir(projectAgentsDir, "project");
157
+
158
+ const agentMap = new Map<string, AgentConfig>();
159
+
160
+ if (scope === "both") {
161
+ for (const agent of userAgents) agentMap.set(agent.name, agent);
162
+ for (const agent of projectAgents) agentMap.set(agent.name, agent);
163
+ } else if (scope === "user") {
164
+ for (const agent of userAgents) agentMap.set(agent.name, agent);
165
+ } else {
166
+ for (const agent of projectAgents) agentMap.set(agent.name, agent);
167
+ }
168
+
169
+ return { agents: Array.from(agentMap.values()), projectAgentsDir };
170
+ }
171
+
172
+ export function formatAgentList(agents: AgentConfig[], maxItems: number): { text: string; remaining: number } {
173
+ if (agents.length === 0) return { text: "none", remaining: 0 };
174
+ const listed = agents.slice(0, maxItems);
175
+ const remaining = agents.length - listed.length;
176
+ return {
177
+ text: listed.map((a) => `${a.name} (${a.source}): ${a.description}`).join("; "),
178
+ remaining,
179
+ };
180
+ }