@gotgenes/pi-subagents 6.12.1 → 6.13.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.
- package/CHANGELOG.md +27 -0
- package/docs/architecture/architecture.md +300 -161
- package/docs/plans/0136-decompose-agent-menu.md +300 -0
- package/docs/retro/0135-extract-display-helpers.md +38 -0
- package/docs/retro/0136-decompose-agent-menu.md +43 -0
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/ui/agent-config-editor.ts +202 -0
- package/src/ui/agent-creation-wizard.ts +246 -0
- package/src/ui/agent-file-ops.ts +59 -0
- package/src/ui/agent-menu.ts +21 -393
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-creation-wizard.ts — AI-generation and manual-form agent creation flows.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from agent-menu.ts to give each concern a single responsibility.
|
|
5
|
+
* Receives dependencies via injection — no direct `node:fs` imports.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
|
|
10
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import { BUILTIN_TOOL_NAMES } from "../agent-types.js";
|
|
12
|
+
import type { AgentRecord } from "../types.js";
|
|
13
|
+
import type { AgentFileOps } from "./agent-file-ops.js";
|
|
14
|
+
|
|
15
|
+
// ---- Deps interface ----
|
|
16
|
+
|
|
17
|
+
/** Narrow manager interface for agent spawning (generate wizard). */
|
|
18
|
+
export interface WizardManager {
|
|
19
|
+
spawnAndWait: (
|
|
20
|
+
ctx: ExtensionContext,
|
|
21
|
+
type: string,
|
|
22
|
+
prompt: string,
|
|
23
|
+
opts: { description: string; maxTurns: number },
|
|
24
|
+
) => Promise<AgentRecord>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Narrow registry interface for reloading after creation. */
|
|
28
|
+
export interface WizardRegistry {
|
|
29
|
+
reload(): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface AgentCreationWizardDeps {
|
|
33
|
+
fileOps: AgentFileOps;
|
|
34
|
+
manager: WizardManager;
|
|
35
|
+
registry: WizardRegistry;
|
|
36
|
+
personalAgentsDir: string;
|
|
37
|
+
projectAgentsDir: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ---- Factory ----
|
|
41
|
+
|
|
42
|
+
export function createAgentCreationWizard(deps: AgentCreationWizardDeps) {
|
|
43
|
+
async function showCreateWizard(ctx: ExtensionContext) {
|
|
44
|
+
const location = await ctx.ui.select("Choose location", [
|
|
45
|
+
"Project (.pi/agents/)",
|
|
46
|
+
`Personal (${deps.personalAgentsDir})`,
|
|
47
|
+
]);
|
|
48
|
+
if (!location) return;
|
|
49
|
+
|
|
50
|
+
const targetDir = location.startsWith("Project")
|
|
51
|
+
? deps.projectAgentsDir
|
|
52
|
+
: deps.personalAgentsDir;
|
|
53
|
+
|
|
54
|
+
const method = await ctx.ui.select("Creation method", [
|
|
55
|
+
"Generate with Claude (recommended)",
|
|
56
|
+
"Manual configuration",
|
|
57
|
+
]);
|
|
58
|
+
if (!method) return;
|
|
59
|
+
|
|
60
|
+
if (method.startsWith("Generate")) {
|
|
61
|
+
await showGenerateWizard(ctx, targetDir);
|
|
62
|
+
} else {
|
|
63
|
+
await showManualWizard(ctx, targetDir);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function showGenerateWizard(ctx: ExtensionContext, targetDir: string) {
|
|
68
|
+
const description = await ctx.ui.input("Describe what this agent should do");
|
|
69
|
+
if (!description) return;
|
|
70
|
+
|
|
71
|
+
const name = await ctx.ui.input("Agent name (filename, no spaces)");
|
|
72
|
+
if (!name) return;
|
|
73
|
+
|
|
74
|
+
deps.fileOps.ensureDir(targetDir);
|
|
75
|
+
|
|
76
|
+
const targetPath = join(targetDir, `${name}.md`);
|
|
77
|
+
if (deps.fileOps.exists(targetPath)) {
|
|
78
|
+
const overwrite = await ctx.ui.confirm(
|
|
79
|
+
"Overwrite",
|
|
80
|
+
`${targetPath} already exists. Overwrite?`,
|
|
81
|
+
);
|
|
82
|
+
if (!overwrite) return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
ctx.ui.notify("Generating agent definition...", "info");
|
|
86
|
+
|
|
87
|
+
const generatePrompt = `Create a custom pi sub-agent definition file based on this description: "${description}"
|
|
88
|
+
|
|
89
|
+
Write a markdown file to: ${targetPath}
|
|
90
|
+
|
|
91
|
+
The file format is a markdown file with YAML frontmatter and a system prompt body:
|
|
92
|
+
|
|
93
|
+
\`\`\`markdown
|
|
94
|
+
---
|
|
95
|
+
description: <one-line description shown in UI>
|
|
96
|
+
tools: <comma-separated built-in tools: read, bash, edit, write, grep, find, ls. Use "none" for no tools. Omit for all tools>
|
|
97
|
+
model: <optional model as "provider/modelId", e.g. "anthropic/claude-haiku-4-5-20251001". Omit to inherit parent model>
|
|
98
|
+
thinking: <optional thinking level: off, minimal, low, medium, high, xhigh. Omit to inherit>
|
|
99
|
+
max_turns: <optional max agentic turns. 0 or omit for unlimited (default)>
|
|
100
|
+
prompt_mode: <"replace" (body IS the full system prompt) or "append" (body is appended to default prompt). Default: replace>
|
|
101
|
+
extensions: <true (inherit all MCP/extension tools), false (none), or comma-separated names. Default: true>
|
|
102
|
+
skills: <true (inherit all), false (none), or comma-separated skill names to preload into prompt. Default: true>
|
|
103
|
+
disallowed_tools: <comma-separated tool names to block, even if otherwise available. Omit for none>
|
|
104
|
+
inherit_context: <true to fork parent conversation into agent so it sees chat history. Default: false>
|
|
105
|
+
run_in_background: <true to run in background by default. Default: false>
|
|
106
|
+
isolated: <true for no extension/MCP tools, only built-in tools. Default: false>
|
|
107
|
+
memory: <"user" (global), "project" (per-project), or "local" (gitignored per-project) for persistent memory. Omit for none>
|
|
108
|
+
isolation: <"worktree" to run in isolated git worktree. Omit for normal>
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
<system prompt body — instructions for the agent>
|
|
112
|
+
\`\`\`
|
|
113
|
+
|
|
114
|
+
Guidelines for choosing settings:
|
|
115
|
+
- For read-only tasks (review, analysis): tools: read, bash, grep, find, ls
|
|
116
|
+
- For code modification tasks: include edit, write
|
|
117
|
+
- Use prompt_mode: append if the agent should keep the default system prompt and add specialization on top
|
|
118
|
+
- Use prompt_mode: replace for fully custom agents with their own personality/instructions
|
|
119
|
+
- Set inherit_context: true if the agent needs to know what was discussed in the parent conversation
|
|
120
|
+
- Set isolated: true if the agent should NOT have access to MCP servers or other extensions
|
|
121
|
+
- Only include frontmatter fields that differ from defaults — omit fields where the default is fine
|
|
122
|
+
|
|
123
|
+
Write the file using the write tool. Only write the file, nothing else.`;
|
|
124
|
+
|
|
125
|
+
const record = await deps.manager.spawnAndWait(
|
|
126
|
+
ctx,
|
|
127
|
+
"general-purpose",
|
|
128
|
+
generatePrompt,
|
|
129
|
+
{
|
|
130
|
+
description: `Generate ${name} agent`,
|
|
131
|
+
maxTurns: 5,
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
if (record.status === "error") {
|
|
136
|
+
ctx.ui.notify(`Generation failed: ${record.error}`, "warning");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
deps.registry.reload();
|
|
141
|
+
|
|
142
|
+
if (deps.fileOps.exists(targetPath)) {
|
|
143
|
+
ctx.ui.notify(`Created ${targetPath}`, "info");
|
|
144
|
+
} else {
|
|
145
|
+
ctx.ui.notify(
|
|
146
|
+
"Agent generation completed but file was not created. Check the agent output.",
|
|
147
|
+
"warning",
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function showManualWizard(ctx: ExtensionContext, targetDir: string) {
|
|
153
|
+
const name = await ctx.ui.input("Agent name (filename, no spaces)");
|
|
154
|
+
if (!name) return;
|
|
155
|
+
|
|
156
|
+
const description = await ctx.ui.input("Description (one line)");
|
|
157
|
+
if (!description) return;
|
|
158
|
+
|
|
159
|
+
const toolChoice = await ctx.ui.select("Tools", [
|
|
160
|
+
"all",
|
|
161
|
+
"none",
|
|
162
|
+
"read-only (read, bash, grep, find, ls)",
|
|
163
|
+
"custom...",
|
|
164
|
+
]);
|
|
165
|
+
if (!toolChoice) return;
|
|
166
|
+
|
|
167
|
+
let tools: string;
|
|
168
|
+
if (toolChoice === "all") {
|
|
169
|
+
tools = BUILTIN_TOOL_NAMES.join(", ");
|
|
170
|
+
} else if (toolChoice === "none") {
|
|
171
|
+
tools = "none";
|
|
172
|
+
} else if (toolChoice.startsWith("read-only")) {
|
|
173
|
+
tools = "read, bash, grep, find, ls";
|
|
174
|
+
} else {
|
|
175
|
+
const customTools = await ctx.ui.input(
|
|
176
|
+
"Tools (comma-separated)",
|
|
177
|
+
BUILTIN_TOOL_NAMES.join(", "),
|
|
178
|
+
);
|
|
179
|
+
if (!customTools) return;
|
|
180
|
+
tools = customTools;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const modelChoice = await ctx.ui.select("Model", [
|
|
184
|
+
"inherit (parent model)",
|
|
185
|
+
"haiku",
|
|
186
|
+
"sonnet",
|
|
187
|
+
"opus",
|
|
188
|
+
"custom...",
|
|
189
|
+
]);
|
|
190
|
+
if (!modelChoice) return;
|
|
191
|
+
|
|
192
|
+
let modelLine = "";
|
|
193
|
+
if (modelChoice === "haiku")
|
|
194
|
+
modelLine = "\nmodel: anthropic/claude-haiku-4-5-20251001";
|
|
195
|
+
else if (modelChoice === "sonnet")
|
|
196
|
+
modelLine = "\nmodel: anthropic/claude-sonnet-4-6";
|
|
197
|
+
else if (modelChoice === "opus")
|
|
198
|
+
modelLine = "\nmodel: anthropic/claude-opus-4-6";
|
|
199
|
+
else if (modelChoice === "custom...") {
|
|
200
|
+
const customModel = await ctx.ui.input("Model (provider/modelId)");
|
|
201
|
+
if (customModel) modelLine = `\nmodel: ${customModel}`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const thinkingChoice = await ctx.ui.select("Thinking level", [
|
|
205
|
+
"inherit",
|
|
206
|
+
"off",
|
|
207
|
+
"minimal",
|
|
208
|
+
"low",
|
|
209
|
+
"medium",
|
|
210
|
+
"high",
|
|
211
|
+
"xhigh",
|
|
212
|
+
]);
|
|
213
|
+
if (!thinkingChoice) return;
|
|
214
|
+
|
|
215
|
+
let thinkingLine = "";
|
|
216
|
+
if (thinkingChoice !== "inherit") thinkingLine = `\nthinking: ${thinkingChoice}`;
|
|
217
|
+
|
|
218
|
+
const systemPrompt = await ctx.ui.editor("System prompt", "");
|
|
219
|
+
if (systemPrompt === undefined) return;
|
|
220
|
+
|
|
221
|
+
const content = `---
|
|
222
|
+
description: ${description}
|
|
223
|
+
tools: ${tools}${modelLine}${thinkingLine}
|
|
224
|
+
prompt_mode: replace
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
${systemPrompt}
|
|
228
|
+
`;
|
|
229
|
+
|
|
230
|
+
const targetPath = join(targetDir, `${name}.md`);
|
|
231
|
+
|
|
232
|
+
if (deps.fileOps.exists(targetPath)) {
|
|
233
|
+
const overwrite = await ctx.ui.confirm(
|
|
234
|
+
"Overwrite",
|
|
235
|
+
`${targetPath} already exists. Overwrite?`,
|
|
236
|
+
);
|
|
237
|
+
if (!overwrite) return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
deps.fileOps.write(targetPath, content);
|
|
241
|
+
deps.registry.reload();
|
|
242
|
+
ctx.ui.notify(`Created ${targetPath}`, "info");
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return { showCreateWizard };
|
|
246
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-file-ops.ts — Filesystem abstraction for agent .md file operations.
|
|
3
|
+
*
|
|
4
|
+
* Decouples menu sub-modules from direct `node:fs` imports, making them
|
|
5
|
+
* testable via plain stub objects without `vi.mock()`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { dirname, join } from "node:path";
|
|
10
|
+
|
|
11
|
+
// ---- Interface ----
|
|
12
|
+
|
|
13
|
+
/** Filesystem operations for agent `.md` files. */
|
|
14
|
+
export interface AgentFileOps {
|
|
15
|
+
exists(filePath: string): boolean;
|
|
16
|
+
read(filePath: string): string | undefined;
|
|
17
|
+
write(filePath: string, content: string): void;
|
|
18
|
+
remove(filePath: string): void;
|
|
19
|
+
ensureDir(dirPath: string): void;
|
|
20
|
+
findAgentFile(name: string, dirs: string[]): string | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ---- Production implementation ----
|
|
24
|
+
|
|
25
|
+
/** Production implementation wrapping `node:fs` synchronous APIs. */
|
|
26
|
+
export class FsAgentFileOps implements AgentFileOps {
|
|
27
|
+
exists(filePath: string): boolean {
|
|
28
|
+
return existsSync(filePath);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
read(filePath: string): string | undefined {
|
|
32
|
+
try {
|
|
33
|
+
return readFileSync(filePath, "utf-8");
|
|
34
|
+
} catch {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
write(filePath: string, content: string): void {
|
|
40
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
41
|
+
writeFileSync(filePath, content, "utf-8");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
remove(filePath: string): void {
|
|
45
|
+
unlinkSync(filePath);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
ensureDir(dirPath: string): void {
|
|
49
|
+
mkdirSync(dirPath, { recursive: true });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
findAgentFile(name: string, dirs: string[]): string | undefined {
|
|
53
|
+
for (const dir of dirs) {
|
|
54
|
+
const p = join(dir, `${name}.md`);
|
|
55
|
+
if (existsSync(p)) return p;
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
}
|