@tintinweb/pi-subagents 0.4.0 → 0.4.3
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 +57 -0
- package/README.md +85 -1
- package/dist/agent-manager.d.ts +70 -0
- package/dist/agent-manager.js +236 -0
- package/dist/agent-runner.d.ts +60 -0
- package/dist/agent-runner.js +265 -0
- package/dist/agent-types.d.ts +41 -0
- package/dist/agent-types.js +130 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/custom-agents.d.ts +14 -0
- package/dist/custom-agents.js +100 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +126 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/group-join.d.ts +32 -0
- package/dist/group-join.js +116 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +1270 -0
- package/dist/model-resolver.d.ts +19 -0
- package/dist/model-resolver.js +62 -0
- package/dist/prompts.d.ts +14 -0
- package/dist/prompts.js +48 -0
- package/dist/types.d.ts +62 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-widget.d.ts +101 -0
- package/dist/ui/agent-widget.js +333 -0
- package/dist/ui/conversation-viewer.d.ts +31 -0
- package/dist/ui/conversation-viewer.js +236 -0
- package/package.json +1 -1
- package/src/agent-manager.ts +71 -3
- package/src/agent-runner.ts +71 -15
- package/src/agent-types.ts +26 -0
- package/src/custom-agents.ts +34 -5
- package/src/index.ts +88 -1
- package/src/memory.ts +165 -0
- package/src/prompts.ts +24 -2
- package/src/skill-loader.ts +79 -0
- package/src/types.ts +16 -0
- package/src/worktree.ts +162 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-agents — A pi extension providing Claude Code-style autonomous sub-agents.
|
|
3
|
+
*
|
|
4
|
+
* Tools:
|
|
5
|
+
* Agent — LLM-callable: spawn a sub-agent
|
|
6
|
+
* get_subagent_result — LLM-callable: check background agent status/result
|
|
7
|
+
* steer_subagent — LLM-callable: send a steering message to a running agent
|
|
8
|
+
*
|
|
9
|
+
* Commands:
|
|
10
|
+
* /agents — Interactive agent management menu
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, mkdirSync, unlinkSync, readFileSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { homedir } from "node:os";
|
|
15
|
+
import { Text } from "@mariozechner/pi-tui";
|
|
16
|
+
import { Type } from "@sinclair/typebox";
|
|
17
|
+
import { AgentManager } from "./agent-manager.js";
|
|
18
|
+
import { steerAgent, getAgentConversation, getDefaultMaxTurns, setDefaultMaxTurns, getGraceTurns, setGraceTurns } from "./agent-runner.js";
|
|
19
|
+
import { GroupJoinManager } from "./group-join.js";
|
|
20
|
+
import { getAvailableTypes, getAllTypes, getDefaultAgentNames, getUserAgentNames, getAgentConfig, resolveType, registerAgents, BUILTIN_TOOL_NAMES } from "./agent-types.js";
|
|
21
|
+
import { loadCustomAgents } from "./custom-agents.js";
|
|
22
|
+
import { resolveModel } from "./model-resolver.js";
|
|
23
|
+
import { AgentWidget, SPINNER, formatTokens, formatMs, formatDuration, getDisplayName, getPromptModeLabel, describeActivity, } from "./ui/agent-widget.js";
|
|
24
|
+
// ---- Shared helpers ----
|
|
25
|
+
/** Tool execute return value for a text response. */
|
|
26
|
+
function textResult(msg, details) {
|
|
27
|
+
return { content: [{ type: "text", text: msg }], details: details };
|
|
28
|
+
}
|
|
29
|
+
/** Safe token formatting — wraps session.getSessionStats() in try-catch. */
|
|
30
|
+
function safeFormatTokens(session) {
|
|
31
|
+
if (!session)
|
|
32
|
+
return "";
|
|
33
|
+
try {
|
|
34
|
+
return formatTokens(session.getSessionStats().tokens.total);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return "";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Create an AgentActivity state and spawn callbacks for tracking tool usage.
|
|
42
|
+
* Used by both foreground and background paths to avoid duplication.
|
|
43
|
+
*/
|
|
44
|
+
function createActivityTracker(onStreamUpdate) {
|
|
45
|
+
const state = { activeTools: new Map(), toolUses: 0, tokens: "", responseText: "", session: undefined };
|
|
46
|
+
const callbacks = {
|
|
47
|
+
onToolActivity: (activity) => {
|
|
48
|
+
if (activity.type === "start") {
|
|
49
|
+
state.activeTools.set(activity.toolName + "_" + Date.now(), activity.toolName);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
for (const [key, name] of state.activeTools) {
|
|
53
|
+
if (name === activity.toolName) {
|
|
54
|
+
state.activeTools.delete(key);
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
state.toolUses++;
|
|
59
|
+
}
|
|
60
|
+
state.tokens = safeFormatTokens(state.session);
|
|
61
|
+
onStreamUpdate?.();
|
|
62
|
+
},
|
|
63
|
+
onTextDelta: (_delta, fullText) => {
|
|
64
|
+
state.responseText = fullText;
|
|
65
|
+
onStreamUpdate?.();
|
|
66
|
+
},
|
|
67
|
+
onSessionCreated: (session) => {
|
|
68
|
+
state.session = session;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
return { state, callbacks };
|
|
72
|
+
}
|
|
73
|
+
/** Human-readable status label for agent completion. */
|
|
74
|
+
function getStatusLabel(status, error) {
|
|
75
|
+
switch (status) {
|
|
76
|
+
case "error": return `Error: ${error ?? "unknown"}`;
|
|
77
|
+
case "aborted": return "Aborted (max turns exceeded)";
|
|
78
|
+
case "steered": return "Wrapped up (turn limit)";
|
|
79
|
+
case "stopped": return "Stopped";
|
|
80
|
+
default: return "Done";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Parenthetical status note for completed agent result text. */
|
|
84
|
+
function getStatusNote(status) {
|
|
85
|
+
switch (status) {
|
|
86
|
+
case "aborted": return " (aborted — max turns exceeded, output may be incomplete)";
|
|
87
|
+
case "steered": return " (wrapped up — reached turn limit)";
|
|
88
|
+
case "stopped": return " (stopped by user)";
|
|
89
|
+
default: return "";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Build AgentDetails from a base + record-specific fields. */
|
|
93
|
+
function buildDetails(base, record, overrides) {
|
|
94
|
+
return {
|
|
95
|
+
...base,
|
|
96
|
+
toolUses: record.toolUses,
|
|
97
|
+
tokens: safeFormatTokens(record.session),
|
|
98
|
+
durationMs: (record.completedAt ?? Date.now()) - record.startedAt,
|
|
99
|
+
status: record.status,
|
|
100
|
+
agentId: record.id,
|
|
101
|
+
error: record.error,
|
|
102
|
+
...overrides,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
export default function (pi) {
|
|
106
|
+
/** Reload agents from .pi/agents/*.md and merge with defaults (called on init and each Agent invocation). */
|
|
107
|
+
const reloadCustomAgents = () => {
|
|
108
|
+
const userAgents = loadCustomAgents(process.cwd());
|
|
109
|
+
registerAgents(userAgents);
|
|
110
|
+
};
|
|
111
|
+
// Initial load
|
|
112
|
+
reloadCustomAgents();
|
|
113
|
+
// ---- Agent activity tracking + widget ----
|
|
114
|
+
const agentActivity = new Map();
|
|
115
|
+
// ---- Individual nudge helper (async join mode) ----
|
|
116
|
+
function sendIndividualNudge(record) {
|
|
117
|
+
const displayName = getDisplayName(record.type);
|
|
118
|
+
const duration = formatDuration(record.startedAt, record.completedAt);
|
|
119
|
+
const status = getStatusLabel(record.status, record.error);
|
|
120
|
+
const resultPreview = record.result
|
|
121
|
+
? record.result.length > 500
|
|
122
|
+
? record.result.slice(0, 500) + "\n...(truncated, use get_subagent_result for full output)"
|
|
123
|
+
: record.result
|
|
124
|
+
: "No output.";
|
|
125
|
+
agentActivity.delete(record.id);
|
|
126
|
+
widget.markFinished(record.id);
|
|
127
|
+
const tokens = safeFormatTokens(record.session);
|
|
128
|
+
const toolStats = tokens ? `Tool uses: ${record.toolUses} | ${tokens}` : `Tool uses: ${record.toolUses}`;
|
|
129
|
+
pi.sendUserMessage(`Background agent completed: ${displayName} (${record.description})\n` +
|
|
130
|
+
`Agent ID: ${record.id} | Status: ${status} | ${toolStats} | Duration: ${duration}\n\n` +
|
|
131
|
+
resultPreview, { deliverAs: "followUp" });
|
|
132
|
+
widget.update();
|
|
133
|
+
}
|
|
134
|
+
/** Format a single agent's summary for grouped notification. */
|
|
135
|
+
function formatAgentSummary(record) {
|
|
136
|
+
const displayName = getDisplayName(record.type);
|
|
137
|
+
const duration = formatDuration(record.startedAt, record.completedAt);
|
|
138
|
+
const status = getStatusLabel(record.status, record.error);
|
|
139
|
+
const resultPreview = record.result
|
|
140
|
+
? record.result.length > 300
|
|
141
|
+
? record.result.slice(0, 300) + "\n...(truncated)"
|
|
142
|
+
: record.result
|
|
143
|
+
: "No output.";
|
|
144
|
+
const tokens = safeFormatTokens(record.session);
|
|
145
|
+
const toolStats = tokens ? `Tools: ${record.toolUses} | ${tokens}` : `Tools: ${record.toolUses}`;
|
|
146
|
+
return `- ${displayName} (${record.description})\n ID: ${record.id} | Status: ${status} | ${toolStats} | Duration: ${duration}\n ${resultPreview}`;
|
|
147
|
+
}
|
|
148
|
+
// ---- Group join manager ----
|
|
149
|
+
const groupJoin = new GroupJoinManager((records, partial) => {
|
|
150
|
+
// Filter out agents whose results were already consumed via get_subagent_result
|
|
151
|
+
const unconsumed = records.filter(r => !r.resultConsumed);
|
|
152
|
+
for (const r of records) {
|
|
153
|
+
agentActivity.delete(r.id);
|
|
154
|
+
widget.markFinished(r.id);
|
|
155
|
+
}
|
|
156
|
+
// If all results were already consumed, skip the notification entirely
|
|
157
|
+
if (unconsumed.length === 0) {
|
|
158
|
+
widget.update();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const total = unconsumed.length;
|
|
162
|
+
const label = partial ? `${total} agent(s) finished (partial — others still running)` : `${total} agent(s) finished`;
|
|
163
|
+
const summary = unconsumed.map(r => formatAgentSummary(r)).join("\n\n");
|
|
164
|
+
pi.sendUserMessage(`Background agent group completed: ${label}\n\n${summary}\n\nUse get_subagent_result for full output.`, { deliverAs: "followUp" });
|
|
165
|
+
widget.update();
|
|
166
|
+
}, 30_000);
|
|
167
|
+
// Background completion: route through group join or send individual nudge
|
|
168
|
+
const manager = new AgentManager((record) => {
|
|
169
|
+
// Skip notification if result was already consumed via get_subagent_result
|
|
170
|
+
if (record.resultConsumed) {
|
|
171
|
+
agentActivity.delete(record.id);
|
|
172
|
+
widget.markFinished(record.id);
|
|
173
|
+
widget.update();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
// If this agent is pending batch finalization (debounce window still open),
|
|
177
|
+
// don't send an individual nudge — finalizeBatch will pick it up retroactively.
|
|
178
|
+
if (currentBatchAgents.some(a => a.id === record.id)) {
|
|
179
|
+
widget.update();
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const result = groupJoin.onAgentComplete(record);
|
|
183
|
+
if (result === 'pass') {
|
|
184
|
+
sendIndividualNudge(record);
|
|
185
|
+
}
|
|
186
|
+
// 'held' → do nothing, group will fire later
|
|
187
|
+
// 'delivered' → group callback already fired
|
|
188
|
+
widget.update();
|
|
189
|
+
});
|
|
190
|
+
// Expose manager via Symbol.for() global registry for cross-package access.
|
|
191
|
+
// Standard Node.js pattern for cross-package singletons (used by OpenTelemetry, etc.).
|
|
192
|
+
const MANAGER_KEY = Symbol.for("pi-subagents:manager");
|
|
193
|
+
globalThis[MANAGER_KEY] = {
|
|
194
|
+
waitForAll: () => manager.waitForAll(),
|
|
195
|
+
hasRunning: () => manager.hasRunning(),
|
|
196
|
+
};
|
|
197
|
+
// Wait for all subagents on shutdown, then dispose the manager
|
|
198
|
+
pi.on("session_shutdown", async () => {
|
|
199
|
+
delete globalThis[MANAGER_KEY];
|
|
200
|
+
await manager.waitForAll();
|
|
201
|
+
manager.dispose();
|
|
202
|
+
});
|
|
203
|
+
// Live widget: show running agents above editor
|
|
204
|
+
const widget = new AgentWidget(manager, agentActivity);
|
|
205
|
+
// ---- Join mode configuration ----
|
|
206
|
+
let defaultJoinMode = 'smart';
|
|
207
|
+
function getDefaultJoinMode() { return defaultJoinMode; }
|
|
208
|
+
function setDefaultJoinMode(mode) { defaultJoinMode = mode; }
|
|
209
|
+
// ---- Batch tracking for smart join mode ----
|
|
210
|
+
// Collects background agent IDs spawned in the current turn for smart grouping.
|
|
211
|
+
// Uses a debounced timer: each new agent resets the 100ms window so that all
|
|
212
|
+
// parallel tool calls (which may be dispatched across multiple microtasks by the
|
|
213
|
+
// framework) are captured in the same batch.
|
|
214
|
+
let currentBatchAgents = [];
|
|
215
|
+
let batchFinalizeTimer;
|
|
216
|
+
let batchCounter = 0;
|
|
217
|
+
/** Finalize the current batch: if 2+ smart-mode agents, register as a group. */
|
|
218
|
+
function finalizeBatch() {
|
|
219
|
+
batchFinalizeTimer = undefined;
|
|
220
|
+
const batchAgents = [...currentBatchAgents];
|
|
221
|
+
currentBatchAgents = [];
|
|
222
|
+
const smartAgents = batchAgents.filter(a => a.joinMode === 'smart' || a.joinMode === 'group');
|
|
223
|
+
if (smartAgents.length >= 2) {
|
|
224
|
+
const groupId = `batch-${++batchCounter}`;
|
|
225
|
+
const ids = smartAgents.map(a => a.id);
|
|
226
|
+
groupJoin.registerGroup(groupId, ids);
|
|
227
|
+
// Retroactively process agents that already completed during the debounce window.
|
|
228
|
+
// Their onComplete fired but was deferred (agent was in currentBatchAgents),
|
|
229
|
+
// so we feed them into the group now.
|
|
230
|
+
for (const id of ids) {
|
|
231
|
+
const record = manager.getRecord(id);
|
|
232
|
+
if (!record)
|
|
233
|
+
continue;
|
|
234
|
+
record.groupId = groupId;
|
|
235
|
+
if (record.completedAt != null && !record.resultConsumed) {
|
|
236
|
+
groupJoin.onAgentComplete(record);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
// No group formed — send individual nudges for any agents that completed
|
|
242
|
+
// during the debounce window and had their notification deferred.
|
|
243
|
+
for (const { id } of batchAgents) {
|
|
244
|
+
const record = manager.getRecord(id);
|
|
245
|
+
if (record?.completedAt != null && !record.resultConsumed) {
|
|
246
|
+
sendIndividualNudge(record);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
// Grab UI context from first tool execution + clear lingering widget on new turn
|
|
252
|
+
pi.on("tool_execution_start", async (_event, ctx) => {
|
|
253
|
+
widget.setUICtx(ctx.ui);
|
|
254
|
+
widget.onTurnStart();
|
|
255
|
+
});
|
|
256
|
+
/** Build the full type list text dynamically from the unified registry. */
|
|
257
|
+
const buildTypeListText = () => {
|
|
258
|
+
const defaultNames = getDefaultAgentNames();
|
|
259
|
+
const userNames = getUserAgentNames();
|
|
260
|
+
const defaultDescs = defaultNames.map((name) => {
|
|
261
|
+
const cfg = getAgentConfig(name);
|
|
262
|
+
const modelSuffix = cfg?.model ? ` (${getModelLabelFromConfig(cfg.model)})` : "";
|
|
263
|
+
return `- ${name}: ${cfg?.description ?? name}${modelSuffix}`;
|
|
264
|
+
});
|
|
265
|
+
const customDescs = userNames.map((name) => {
|
|
266
|
+
const cfg = getAgentConfig(name);
|
|
267
|
+
return `- ${name}: ${cfg?.description ?? name}`;
|
|
268
|
+
});
|
|
269
|
+
return [
|
|
270
|
+
"Default agents:",
|
|
271
|
+
...defaultDescs,
|
|
272
|
+
...(customDescs.length > 0 ? ["", "Custom agents:", ...customDescs] : []),
|
|
273
|
+
"",
|
|
274
|
+
"Custom agents can be defined in .pi/agents/<name>.md (project) or ~/.pi/agent/agents/<name>.md (global) — they are picked up automatically. Project-level agents override global ones. Creating a .md file with the same name as a default agent overrides it.",
|
|
275
|
+
].join("\n");
|
|
276
|
+
};
|
|
277
|
+
/** Derive a short model label from a model string. */
|
|
278
|
+
function getModelLabelFromConfig(model) {
|
|
279
|
+
// Strip provider prefix (e.g. "anthropic/claude-sonnet-4-6" → "claude-sonnet-4-6")
|
|
280
|
+
const name = model.includes("/") ? model.split("/").pop() : model;
|
|
281
|
+
// Strip trailing date suffix (e.g. "claude-haiku-4-5-20251001" → "claude-haiku-4-5")
|
|
282
|
+
return name.replace(/-\d{8}$/, "");
|
|
283
|
+
}
|
|
284
|
+
const typeListText = buildTypeListText();
|
|
285
|
+
// ---- Agent tool ----
|
|
286
|
+
pi.registerTool({
|
|
287
|
+
name: "Agent",
|
|
288
|
+
label: "Agent",
|
|
289
|
+
description: `Launch a new agent to handle complex, multi-step tasks autonomously.
|
|
290
|
+
|
|
291
|
+
The Agent tool launches specialized agents that autonomously handle complex tasks. Each agent type has specific capabilities and tools available to it.
|
|
292
|
+
|
|
293
|
+
Available agent types:
|
|
294
|
+
${typeListText}
|
|
295
|
+
|
|
296
|
+
Guidelines:
|
|
297
|
+
- For parallel work, use run_in_background: true on each agent. Foreground calls run sequentially — only one executes at a time.
|
|
298
|
+
- Use Explore for codebase searches and code understanding.
|
|
299
|
+
- Use Plan for architecture and implementation planning.
|
|
300
|
+
- Use general-purpose for complex tasks that need file editing.
|
|
301
|
+
- Provide clear, detailed prompts so the agent can work autonomously.
|
|
302
|
+
- Agent results are returned as text — summarize them for the user.
|
|
303
|
+
- Use run_in_background for work you don't need immediately. You will be notified when it completes.
|
|
304
|
+
- Use resume with an agent ID to continue a previous agent's work.
|
|
305
|
+
- Use steer_subagent to send mid-run messages to a running background agent.
|
|
306
|
+
- Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
|
|
307
|
+
- Use thinking to control extended thinking level.
|
|
308
|
+
- Use inherit_context if the agent needs the parent conversation history.
|
|
309
|
+
- Use join_mode to control how background completion notifications are delivered. By default (smart), 2+ background agents spawned in the same turn are grouped into a single notification. Use "async" for individual notifications or "group" to force grouping.`,
|
|
310
|
+
parameters: Type.Object({
|
|
311
|
+
prompt: Type.String({
|
|
312
|
+
description: "The task for the agent to perform.",
|
|
313
|
+
}),
|
|
314
|
+
description: Type.String({
|
|
315
|
+
description: "A short (3-5 word) description of the task (shown in UI).",
|
|
316
|
+
}),
|
|
317
|
+
subagent_type: Type.String({
|
|
318
|
+
description: `The type of specialized agent to use. Available types: ${getAvailableTypes().join(", ")}. Custom agents from .pi/agents/*.md (project) or ~/.pi/agent/agents/*.md (global) are also available.`,
|
|
319
|
+
}),
|
|
320
|
+
model: Type.Optional(Type.String({
|
|
321
|
+
description: 'Optional model override. Accepts "provider/modelId" or fuzzy name (e.g. "haiku", "sonnet"). Omit to use the agent type\'s default.',
|
|
322
|
+
})),
|
|
323
|
+
thinking: Type.Optional(Type.String({
|
|
324
|
+
description: "Thinking level: off, minimal, low, medium, high, xhigh. Overrides agent default.",
|
|
325
|
+
})),
|
|
326
|
+
max_turns: Type.Optional(Type.Number({
|
|
327
|
+
description: "Maximum number of agentic turns before stopping.",
|
|
328
|
+
minimum: 1,
|
|
329
|
+
})),
|
|
330
|
+
run_in_background: Type.Optional(Type.Boolean({
|
|
331
|
+
description: "Set to true to run in background. Returns agent ID immediately. You will be notified on completion.",
|
|
332
|
+
})),
|
|
333
|
+
resume: Type.Optional(Type.String({
|
|
334
|
+
description: "Optional agent ID to resume from. Continues from previous context.",
|
|
335
|
+
})),
|
|
336
|
+
isolated: Type.Optional(Type.Boolean({
|
|
337
|
+
description: "If true, agent gets no extension/MCP tools — only built-in tools.",
|
|
338
|
+
})),
|
|
339
|
+
inherit_context: Type.Optional(Type.Boolean({
|
|
340
|
+
description: "If true, fork parent conversation into the agent. Default: false (fresh context).",
|
|
341
|
+
})),
|
|
342
|
+
join_mode: Type.Optional(Type.Union([
|
|
343
|
+
Type.Literal("async"),
|
|
344
|
+
Type.Literal("group"),
|
|
345
|
+
], { description: "Override join behavior for background agents. async: individual nudge on completion. group: hold and send one consolidated notification when all agents in the group complete. Default: smart (auto-groups 2+ background agents spawned in the same turn)." })),
|
|
346
|
+
}),
|
|
347
|
+
// ---- Custom rendering: Claude Code style ----
|
|
348
|
+
renderCall(args, theme) {
|
|
349
|
+
const displayName = args.subagent_type ? getDisplayName(args.subagent_type) : "Agent";
|
|
350
|
+
const desc = args.description ?? "";
|
|
351
|
+
return new Text("▸ " + theme.fg("toolTitle", theme.bold(displayName)) + (desc ? " " + theme.fg("muted", desc) : ""), 0, 0);
|
|
352
|
+
},
|
|
353
|
+
renderResult(result, { expanded, isPartial }, theme) {
|
|
354
|
+
const details = result.details;
|
|
355
|
+
if (!details) {
|
|
356
|
+
const text = result.content[0]?.type === "text" ? result.content[0].text : "";
|
|
357
|
+
return new Text(text, 0, 0);
|
|
358
|
+
}
|
|
359
|
+
// Helper: build "haiku · thinking: high · 3 tool uses · 33.8k tokens" stats string
|
|
360
|
+
const stats = (d) => {
|
|
361
|
+
const parts = [];
|
|
362
|
+
if (d.modelName)
|
|
363
|
+
parts.push(d.modelName);
|
|
364
|
+
if (d.tags)
|
|
365
|
+
parts.push(...d.tags);
|
|
366
|
+
if (d.toolUses > 0)
|
|
367
|
+
parts.push(`${d.toolUses} tool use${d.toolUses === 1 ? "" : "s"}`);
|
|
368
|
+
if (d.tokens)
|
|
369
|
+
parts.push(d.tokens);
|
|
370
|
+
return parts.map(p => theme.fg("dim", p)).join(" " + theme.fg("dim", "·") + " ");
|
|
371
|
+
};
|
|
372
|
+
// ---- While running (streaming) ----
|
|
373
|
+
if (isPartial || details.status === "running") {
|
|
374
|
+
const frame = SPINNER[details.spinnerFrame ?? 0];
|
|
375
|
+
const s = stats(details);
|
|
376
|
+
let line = theme.fg("accent", frame) + (s ? " " + s : "");
|
|
377
|
+
line += "\n" + theme.fg("dim", ` ⎿ ${details.activity ?? "thinking…"}`);
|
|
378
|
+
return new Text(line, 0, 0);
|
|
379
|
+
}
|
|
380
|
+
// ---- Background agent launched ----
|
|
381
|
+
if (details.status === "background") {
|
|
382
|
+
return new Text(theme.fg("dim", ` ⎿ Running in background (ID: ${details.agentId})`), 0, 0);
|
|
383
|
+
}
|
|
384
|
+
// ---- Completed / Steered ----
|
|
385
|
+
if (details.status === "completed" || details.status === "steered") {
|
|
386
|
+
const duration = formatMs(details.durationMs);
|
|
387
|
+
const isSteered = details.status === "steered";
|
|
388
|
+
const icon = isSteered ? theme.fg("warning", "✓") : theme.fg("success", "✓");
|
|
389
|
+
const s = stats(details);
|
|
390
|
+
let line = icon + (s ? " " + s : "");
|
|
391
|
+
line += " " + theme.fg("dim", "·") + " " + theme.fg("dim", duration);
|
|
392
|
+
if (expanded) {
|
|
393
|
+
const resultText = result.content[0]?.type === "text" ? result.content[0].text : "";
|
|
394
|
+
if (resultText) {
|
|
395
|
+
const lines = resultText.split("\n").slice(0, 50);
|
|
396
|
+
for (const l of lines) {
|
|
397
|
+
line += "\n" + theme.fg("dim", ` ${l}`);
|
|
398
|
+
}
|
|
399
|
+
if (resultText.split("\n").length > 50) {
|
|
400
|
+
line += "\n" + theme.fg("muted", " ... (use get_subagent_result with verbose for full output)");
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
const doneText = isSteered ? "Wrapped up (turn limit)" : "Done";
|
|
406
|
+
line += "\n" + theme.fg("dim", ` ⎿ ${doneText}`);
|
|
407
|
+
}
|
|
408
|
+
return new Text(line, 0, 0);
|
|
409
|
+
}
|
|
410
|
+
// ---- Stopped (user-initiated abort) ----
|
|
411
|
+
if (details.status === "stopped") {
|
|
412
|
+
const s = stats(details);
|
|
413
|
+
let line = theme.fg("dim", "■") + (s ? " " + s : "");
|
|
414
|
+
line += "\n" + theme.fg("dim", " ⎿ Stopped");
|
|
415
|
+
return new Text(line, 0, 0);
|
|
416
|
+
}
|
|
417
|
+
// ---- Error / Aborted (hard max_turns) ----
|
|
418
|
+
const s = stats(details);
|
|
419
|
+
let line = theme.fg("error", "✗") + (s ? " " + s : "");
|
|
420
|
+
if (details.status === "error") {
|
|
421
|
+
line += "\n" + theme.fg("error", ` ⎿ Error: ${details.error ?? "unknown"}`);
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
line += "\n" + theme.fg("warning", " ⎿ Aborted (max turns exceeded)");
|
|
425
|
+
}
|
|
426
|
+
return new Text(line, 0, 0);
|
|
427
|
+
},
|
|
428
|
+
// ---- Execute ----
|
|
429
|
+
execute: async (_toolCallId, params, signal, onUpdate, ctx) => {
|
|
430
|
+
// Ensure we have UI context for widget rendering
|
|
431
|
+
widget.setUICtx(ctx.ui);
|
|
432
|
+
// Reload custom agents so new .pi/agents/*.md files are picked up without restart
|
|
433
|
+
reloadCustomAgents();
|
|
434
|
+
const rawType = params.subagent_type;
|
|
435
|
+
const resolved = resolveType(rawType);
|
|
436
|
+
const subagentType = resolved ?? "general-purpose";
|
|
437
|
+
const fellBack = resolved === undefined;
|
|
438
|
+
const displayName = getDisplayName(subagentType);
|
|
439
|
+
// Get agent config (if any)
|
|
440
|
+
const customConfig = getAgentConfig(subagentType);
|
|
441
|
+
// Resolve model if specified (supports exact "provider/modelId" or fuzzy match)
|
|
442
|
+
let model = ctx.model;
|
|
443
|
+
const modelInput = params.model ?? customConfig?.model;
|
|
444
|
+
if (modelInput) {
|
|
445
|
+
const resolved = resolveModel(modelInput, ctx.modelRegistry);
|
|
446
|
+
if (typeof resolved === "string") {
|
|
447
|
+
if (params.model)
|
|
448
|
+
return textResult(resolved); // user-specified: error
|
|
449
|
+
// config-specified: silent fallback to parent
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
model = resolved;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
// Resolve thinking: explicit param > custom config > undefined
|
|
456
|
+
const thinking = (params.thinking ?? customConfig?.thinking);
|
|
457
|
+
// Resolve spawn-time defaults from custom config (caller overrides)
|
|
458
|
+
const inheritContext = params.inherit_context ?? customConfig?.inheritContext ?? false;
|
|
459
|
+
const runInBackground = params.run_in_background ?? customConfig?.runInBackground ?? false;
|
|
460
|
+
const isolated = params.isolated ?? customConfig?.isolated ?? false;
|
|
461
|
+
// Build display tags for non-default config
|
|
462
|
+
const parentModelId = ctx.model?.id;
|
|
463
|
+
const effectiveModelId = model?.id;
|
|
464
|
+
const agentModelName = effectiveModelId && effectiveModelId !== parentModelId
|
|
465
|
+
? (model?.name ?? effectiveModelId).replace(/^Claude\s+/i, "").toLowerCase()
|
|
466
|
+
: undefined;
|
|
467
|
+
const agentTags = [];
|
|
468
|
+
const modeLabel = getPromptModeLabel(subagentType);
|
|
469
|
+
if (modeLabel)
|
|
470
|
+
agentTags.push(modeLabel);
|
|
471
|
+
if (thinking)
|
|
472
|
+
agentTags.push(`thinking: ${thinking}`);
|
|
473
|
+
if (isolated)
|
|
474
|
+
agentTags.push("isolated");
|
|
475
|
+
// Shared base fields for all AgentDetails in this call
|
|
476
|
+
const detailBase = {
|
|
477
|
+
displayName,
|
|
478
|
+
description: params.description,
|
|
479
|
+
subagentType,
|
|
480
|
+
modelName: agentModelName,
|
|
481
|
+
tags: agentTags.length > 0 ? agentTags : undefined,
|
|
482
|
+
};
|
|
483
|
+
// Resume existing agent
|
|
484
|
+
if (params.resume) {
|
|
485
|
+
const existing = manager.getRecord(params.resume);
|
|
486
|
+
if (!existing) {
|
|
487
|
+
return textResult(`Agent not found: "${params.resume}". It may have been cleaned up.`);
|
|
488
|
+
}
|
|
489
|
+
if (!existing.session) {
|
|
490
|
+
return textResult(`Agent "${params.resume}" has no active session to resume.`);
|
|
491
|
+
}
|
|
492
|
+
const record = await manager.resume(params.resume, params.prompt, signal);
|
|
493
|
+
if (!record) {
|
|
494
|
+
return textResult(`Failed to resume agent "${params.resume}".`);
|
|
495
|
+
}
|
|
496
|
+
return textResult(record.result ?? record.error ?? "No output.", buildDetails(detailBase, record));
|
|
497
|
+
}
|
|
498
|
+
// Background execution
|
|
499
|
+
if (runInBackground) {
|
|
500
|
+
const { state: bgState, callbacks: bgCallbacks } = createActivityTracker();
|
|
501
|
+
const id = manager.spawn(pi, ctx, subagentType, params.prompt, {
|
|
502
|
+
description: params.description,
|
|
503
|
+
model,
|
|
504
|
+
maxTurns: params.max_turns,
|
|
505
|
+
isolated,
|
|
506
|
+
inheritContext,
|
|
507
|
+
thinkingLevel: thinking,
|
|
508
|
+
isBackground: true,
|
|
509
|
+
...bgCallbacks,
|
|
510
|
+
});
|
|
511
|
+
// Determine join mode and track for batching
|
|
512
|
+
const joinMode = params.join_mode ?? defaultJoinMode;
|
|
513
|
+
const record = manager.getRecord(id);
|
|
514
|
+
if (record)
|
|
515
|
+
record.joinMode = joinMode;
|
|
516
|
+
if (joinMode === 'async') {
|
|
517
|
+
// Explicit async — not part of any batch
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
// smart or group — add to current batch
|
|
521
|
+
currentBatchAgents.push({ id, joinMode });
|
|
522
|
+
// Debounce: reset timer on each new agent so parallel tool calls
|
|
523
|
+
// dispatched across multiple event loop ticks are captured together
|
|
524
|
+
if (batchFinalizeTimer)
|
|
525
|
+
clearTimeout(batchFinalizeTimer);
|
|
526
|
+
batchFinalizeTimer = setTimeout(finalizeBatch, 100);
|
|
527
|
+
}
|
|
528
|
+
agentActivity.set(id, bgState);
|
|
529
|
+
widget.ensureTimer();
|
|
530
|
+
widget.update();
|
|
531
|
+
const isQueued = record?.status === "queued";
|
|
532
|
+
return textResult(`Agent ${isQueued ? "queued" : "started"} in background.\n` +
|
|
533
|
+
`Agent ID: ${id}\n` +
|
|
534
|
+
`Type: ${displayName}\n` +
|
|
535
|
+
`Description: ${params.description}\n` +
|
|
536
|
+
(isQueued ? `Position: queued (max ${manager.getMaxConcurrent()} concurrent)\n` : "") +
|
|
537
|
+
`\nYou will be notified when this agent completes.\n` +
|
|
538
|
+
`Use get_subagent_result to retrieve full results, or steer_subagent to send it messages.\n` +
|
|
539
|
+
`Do not duplicate this agent's work.`, { ...detailBase, toolUses: 0, tokens: "", durationMs: 0, status: "background", agentId: id });
|
|
540
|
+
}
|
|
541
|
+
// Foreground (synchronous) execution — stream progress via onUpdate
|
|
542
|
+
let spinnerFrame = 0;
|
|
543
|
+
const startedAt = Date.now();
|
|
544
|
+
let fgId;
|
|
545
|
+
const streamUpdate = () => {
|
|
546
|
+
const details = {
|
|
547
|
+
...detailBase,
|
|
548
|
+
toolUses: fgState.toolUses,
|
|
549
|
+
tokens: fgState.tokens,
|
|
550
|
+
durationMs: Date.now() - startedAt,
|
|
551
|
+
status: "running",
|
|
552
|
+
activity: describeActivity(fgState.activeTools, fgState.responseText),
|
|
553
|
+
spinnerFrame: spinnerFrame % SPINNER.length,
|
|
554
|
+
};
|
|
555
|
+
onUpdate?.({
|
|
556
|
+
content: [{ type: "text", text: `${fgState.toolUses} tool uses...` }],
|
|
557
|
+
details: details,
|
|
558
|
+
});
|
|
559
|
+
};
|
|
560
|
+
const { state: fgState, callbacks: fgCallbacks } = createActivityTracker(streamUpdate);
|
|
561
|
+
// Wire session creation to register in widget
|
|
562
|
+
const origOnSession = fgCallbacks.onSessionCreated;
|
|
563
|
+
fgCallbacks.onSessionCreated = (session) => {
|
|
564
|
+
origOnSession(session);
|
|
565
|
+
for (const a of manager.listAgents()) {
|
|
566
|
+
if (a.session === session) {
|
|
567
|
+
fgId = a.id;
|
|
568
|
+
agentActivity.set(a.id, fgState);
|
|
569
|
+
widget.ensureTimer();
|
|
570
|
+
break;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
// Animate spinner at ~80ms (smooth rotation through 10 braille frames)
|
|
575
|
+
const spinnerInterval = setInterval(() => {
|
|
576
|
+
spinnerFrame++;
|
|
577
|
+
streamUpdate();
|
|
578
|
+
}, 80);
|
|
579
|
+
streamUpdate();
|
|
580
|
+
const record = await manager.spawnAndWait(pi, ctx, subagentType, params.prompt, {
|
|
581
|
+
description: params.description,
|
|
582
|
+
model,
|
|
583
|
+
maxTurns: params.max_turns,
|
|
584
|
+
isolated,
|
|
585
|
+
inheritContext,
|
|
586
|
+
thinkingLevel: thinking,
|
|
587
|
+
...fgCallbacks,
|
|
588
|
+
});
|
|
589
|
+
clearInterval(spinnerInterval);
|
|
590
|
+
// Clean up foreground agent from widget
|
|
591
|
+
if (fgId) {
|
|
592
|
+
agentActivity.delete(fgId);
|
|
593
|
+
widget.markFinished(fgId);
|
|
594
|
+
}
|
|
595
|
+
// Get final token count
|
|
596
|
+
const tokenText = safeFormatTokens(fgState.session);
|
|
597
|
+
const details = buildDetails(detailBase, record, { tokens: tokenText });
|
|
598
|
+
const fallbackNote = fellBack
|
|
599
|
+
? `Note: Unknown agent type "${rawType}" — using general-purpose.\n\n`
|
|
600
|
+
: "";
|
|
601
|
+
if (record.status === "error") {
|
|
602
|
+
return textResult(`${fallbackNote}Agent failed: ${record.error}`, details);
|
|
603
|
+
}
|
|
604
|
+
const durationMs = (record.completedAt ?? Date.now()) - record.startedAt;
|
|
605
|
+
const statsParts = [`${record.toolUses} tool uses`];
|
|
606
|
+
if (tokenText)
|
|
607
|
+
statsParts.push(tokenText);
|
|
608
|
+
return textResult(`${fallbackNote}Agent completed in ${formatMs(durationMs)} (${statsParts.join(", ")})${getStatusNote(record.status)}.\n\n` +
|
|
609
|
+
(record.result ?? "No output."), details);
|
|
610
|
+
},
|
|
611
|
+
});
|
|
612
|
+
// ---- get_subagent_result tool ----
|
|
613
|
+
pi.registerTool({
|
|
614
|
+
name: "get_subagent_result",
|
|
615
|
+
label: "Get Agent Result",
|
|
616
|
+
description: "Check status and retrieve results from a background agent. Use the agent ID returned by Agent with run_in_background.",
|
|
617
|
+
parameters: Type.Object({
|
|
618
|
+
agent_id: Type.String({
|
|
619
|
+
description: "The agent ID to check.",
|
|
620
|
+
}),
|
|
621
|
+
wait: Type.Optional(Type.Boolean({
|
|
622
|
+
description: "If true, wait for the agent to complete before returning. Default: false.",
|
|
623
|
+
})),
|
|
624
|
+
verbose: Type.Optional(Type.Boolean({
|
|
625
|
+
description: "If true, include the agent's full conversation (messages + tool calls). Default: false.",
|
|
626
|
+
})),
|
|
627
|
+
}),
|
|
628
|
+
execute: async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
|
|
629
|
+
const record = manager.getRecord(params.agent_id);
|
|
630
|
+
if (!record) {
|
|
631
|
+
return textResult(`Agent not found: "${params.agent_id}". It may have been cleaned up.`);
|
|
632
|
+
}
|
|
633
|
+
// Wait for completion if requested
|
|
634
|
+
if (params.wait && record.status === "running" && record.promise) {
|
|
635
|
+
await record.promise;
|
|
636
|
+
}
|
|
637
|
+
const displayName = getDisplayName(record.type);
|
|
638
|
+
const duration = formatDuration(record.startedAt, record.completedAt);
|
|
639
|
+
const tokens = safeFormatTokens(record.session);
|
|
640
|
+
const toolStats = tokens ? `Tool uses: ${record.toolUses} | ${tokens}` : `Tool uses: ${record.toolUses}`;
|
|
641
|
+
let output = `Agent: ${record.id}\n` +
|
|
642
|
+
`Type: ${displayName} | Status: ${record.status} | ${toolStats} | Duration: ${duration}\n` +
|
|
643
|
+
`Description: ${record.description}\n\n`;
|
|
644
|
+
if (record.status === "running") {
|
|
645
|
+
output += "Agent is still running. Use wait: true or check back later.";
|
|
646
|
+
}
|
|
647
|
+
else if (record.status === "error") {
|
|
648
|
+
output += `Error: ${record.error}`;
|
|
649
|
+
}
|
|
650
|
+
else {
|
|
651
|
+
output += record.result ?? "No output.";
|
|
652
|
+
}
|
|
653
|
+
// Mark result as consumed — suppresses the completion notification
|
|
654
|
+
if (record.status !== "running" && record.status !== "queued") {
|
|
655
|
+
record.resultConsumed = true;
|
|
656
|
+
}
|
|
657
|
+
// Verbose: include full conversation
|
|
658
|
+
if (params.verbose && record.session) {
|
|
659
|
+
const conversation = getAgentConversation(record.session);
|
|
660
|
+
if (conversation) {
|
|
661
|
+
output += `\n\n--- Agent Conversation ---\n${conversation}`;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return textResult(output);
|
|
665
|
+
},
|
|
666
|
+
});
|
|
667
|
+
// ---- steer_subagent tool ----
|
|
668
|
+
pi.registerTool({
|
|
669
|
+
name: "steer_subagent",
|
|
670
|
+
label: "Steer Agent",
|
|
671
|
+
description: "Send a steering message to a running agent. The message will interrupt the agent after its current tool execution " +
|
|
672
|
+
"and be injected into its conversation, allowing you to redirect its work mid-run. Only works on running agents.",
|
|
673
|
+
parameters: Type.Object({
|
|
674
|
+
agent_id: Type.String({
|
|
675
|
+
description: "The agent ID to steer (must be currently running).",
|
|
676
|
+
}),
|
|
677
|
+
message: Type.String({
|
|
678
|
+
description: "The steering message to send. This will appear as a user message in the agent's conversation.",
|
|
679
|
+
}),
|
|
680
|
+
}),
|
|
681
|
+
execute: async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
|
|
682
|
+
const record = manager.getRecord(params.agent_id);
|
|
683
|
+
if (!record) {
|
|
684
|
+
return textResult(`Agent not found: "${params.agent_id}". It may have been cleaned up.`);
|
|
685
|
+
}
|
|
686
|
+
if (record.status !== "running") {
|
|
687
|
+
return textResult(`Agent "${params.agent_id}" is not running (status: ${record.status}). Cannot steer a non-running agent.`);
|
|
688
|
+
}
|
|
689
|
+
if (!record.session) {
|
|
690
|
+
return textResult(`Agent "${params.agent_id}" has no active session yet. It may still be initializing.`);
|
|
691
|
+
}
|
|
692
|
+
try {
|
|
693
|
+
await steerAgent(record.session, params.message);
|
|
694
|
+
return textResult(`Steering message sent to agent ${record.id}. The agent will process it after its current tool execution.`);
|
|
695
|
+
}
|
|
696
|
+
catch (err) {
|
|
697
|
+
return textResult(`Failed to steer agent: ${err instanceof Error ? err.message : String(err)}`);
|
|
698
|
+
}
|
|
699
|
+
},
|
|
700
|
+
});
|
|
701
|
+
// ---- /agents interactive menu ----
|
|
702
|
+
const projectAgentsDir = () => join(process.cwd(), ".pi", "agents");
|
|
703
|
+
const personalAgentsDir = () => join(homedir(), ".pi", "agent", "agents");
|
|
704
|
+
/** Find the file path of a custom agent by name (project first, then global). */
|
|
705
|
+
function findAgentFile(name) {
|
|
706
|
+
const projectPath = join(projectAgentsDir(), `${name}.md`);
|
|
707
|
+
if (existsSync(projectPath))
|
|
708
|
+
return { path: projectPath, location: "project" };
|
|
709
|
+
const personalPath = join(personalAgentsDir(), `${name}.md`);
|
|
710
|
+
if (existsSync(personalPath))
|
|
711
|
+
return { path: personalPath, location: "personal" };
|
|
712
|
+
return undefined;
|
|
713
|
+
}
|
|
714
|
+
function getModelLabel(type, registry) {
|
|
715
|
+
const cfg = getAgentConfig(type);
|
|
716
|
+
if (!cfg?.model)
|
|
717
|
+
return "inherit";
|
|
718
|
+
// If registry provided, check if the model actually resolves
|
|
719
|
+
if (registry) {
|
|
720
|
+
const resolved = resolveModel(cfg.model, registry);
|
|
721
|
+
if (typeof resolved === "string")
|
|
722
|
+
return "inherit"; // model not available
|
|
723
|
+
}
|
|
724
|
+
return getModelLabelFromConfig(cfg.model);
|
|
725
|
+
}
|
|
726
|
+
async function showAgentsMenu(ctx) {
|
|
727
|
+
reloadCustomAgents();
|
|
728
|
+
const allNames = getAllTypes();
|
|
729
|
+
// Build select options
|
|
730
|
+
const options = [];
|
|
731
|
+
// Running agents entry (only if there are active agents)
|
|
732
|
+
const agents = manager.listAgents();
|
|
733
|
+
if (agents.length > 0) {
|
|
734
|
+
const running = agents.filter(a => a.status === "running" || a.status === "queued").length;
|
|
735
|
+
const done = agents.filter(a => a.status === "completed" || a.status === "steered").length;
|
|
736
|
+
options.push(`Running agents (${agents.length}) — ${running} running, ${done} done`);
|
|
737
|
+
}
|
|
738
|
+
// Agent types list
|
|
739
|
+
if (allNames.length > 0) {
|
|
740
|
+
options.push(`Agent types (${allNames.length})`);
|
|
741
|
+
}
|
|
742
|
+
// Actions
|
|
743
|
+
options.push("Create new agent");
|
|
744
|
+
options.push("Settings");
|
|
745
|
+
const noAgentsMsg = allNames.length === 0 && agents.length === 0
|
|
746
|
+
? "No agents found. Create specialized subagents that can be delegated to.\n\n" +
|
|
747
|
+
"Each subagent has its own context window, custom system prompt, and specific tools.\n\n" +
|
|
748
|
+
"Try creating: Code Reviewer, Security Auditor, Test Writer, or Documentation Writer.\n\n"
|
|
749
|
+
: "";
|
|
750
|
+
if (noAgentsMsg) {
|
|
751
|
+
ctx.ui.notify(noAgentsMsg, "info");
|
|
752
|
+
}
|
|
753
|
+
const choice = await ctx.ui.select("Agents", options);
|
|
754
|
+
if (!choice)
|
|
755
|
+
return;
|
|
756
|
+
if (choice.startsWith("Running agents (")) {
|
|
757
|
+
await showRunningAgents(ctx);
|
|
758
|
+
await showAgentsMenu(ctx);
|
|
759
|
+
}
|
|
760
|
+
else if (choice.startsWith("Agent types (")) {
|
|
761
|
+
await showAllAgentsList(ctx);
|
|
762
|
+
await showAgentsMenu(ctx);
|
|
763
|
+
}
|
|
764
|
+
else if (choice === "Create new agent") {
|
|
765
|
+
await showCreateWizard(ctx);
|
|
766
|
+
}
|
|
767
|
+
else if (choice === "Settings") {
|
|
768
|
+
await showSettings(ctx);
|
|
769
|
+
await showAgentsMenu(ctx);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
async function showAllAgentsList(ctx) {
|
|
773
|
+
const allNames = getAllTypes();
|
|
774
|
+
if (allNames.length === 0) {
|
|
775
|
+
ctx.ui.notify("No agents.", "info");
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
// Source indicators: defaults unmarked, custom agents get • (project) or ◦ (global)
|
|
779
|
+
// Disabled agents get ✕ prefix
|
|
780
|
+
const sourceIndicator = (cfg) => {
|
|
781
|
+
const disabled = cfg?.enabled === false;
|
|
782
|
+
if (cfg?.source === "project")
|
|
783
|
+
return disabled ? "✕• " : "• ";
|
|
784
|
+
if (cfg?.source === "global")
|
|
785
|
+
return disabled ? "✕◦ " : "◦ ";
|
|
786
|
+
if (disabled)
|
|
787
|
+
return "✕ ";
|
|
788
|
+
return " ";
|
|
789
|
+
};
|
|
790
|
+
const entries = allNames.map(name => {
|
|
791
|
+
const cfg = getAgentConfig(name);
|
|
792
|
+
const disabled = cfg?.enabled === false;
|
|
793
|
+
const model = getModelLabel(name, ctx.modelRegistry);
|
|
794
|
+
const indicator = sourceIndicator(cfg);
|
|
795
|
+
const prefix = `${indicator}${name} · ${model}`;
|
|
796
|
+
const desc = disabled ? "(disabled)" : (cfg?.description ?? name);
|
|
797
|
+
return { name, prefix, desc };
|
|
798
|
+
});
|
|
799
|
+
const maxPrefix = Math.max(...entries.map(e => e.prefix.length));
|
|
800
|
+
const hasCustom = allNames.some(n => { const c = getAgentConfig(n); return c && !c.isDefault && c.enabled !== false; });
|
|
801
|
+
const hasDisabled = allNames.some(n => getAgentConfig(n)?.enabled === false);
|
|
802
|
+
const legendParts = [];
|
|
803
|
+
if (hasCustom)
|
|
804
|
+
legendParts.push("• = project ◦ = global");
|
|
805
|
+
if (hasDisabled)
|
|
806
|
+
legendParts.push("✕ = disabled");
|
|
807
|
+
const legend = legendParts.length ? "\n" + legendParts.join(" ") : "";
|
|
808
|
+
const options = entries.map(({ prefix, desc }) => `${prefix.padEnd(maxPrefix)} — ${desc}`);
|
|
809
|
+
if (legend)
|
|
810
|
+
options.push(legend);
|
|
811
|
+
const choice = await ctx.ui.select("Agent types", options);
|
|
812
|
+
if (!choice)
|
|
813
|
+
return;
|
|
814
|
+
const agentName = choice.split(" · ")[0].replace(/^[•◦✕\s]+/, "").trim();
|
|
815
|
+
if (getAgentConfig(agentName)) {
|
|
816
|
+
await showAgentDetail(ctx, agentName);
|
|
817
|
+
await showAllAgentsList(ctx);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
async function showRunningAgents(ctx) {
|
|
821
|
+
const agents = manager.listAgents();
|
|
822
|
+
if (agents.length === 0) {
|
|
823
|
+
ctx.ui.notify("No agents.", "info");
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
const options = agents.map(a => {
|
|
827
|
+
const dn = getDisplayName(a.type);
|
|
828
|
+
const dur = formatDuration(a.startedAt, a.completedAt);
|
|
829
|
+
return `${dn} (${a.description}) · ${a.toolUses} tools · ${a.status} · ${dur}`;
|
|
830
|
+
});
|
|
831
|
+
const choice = await ctx.ui.select("Running agents", options);
|
|
832
|
+
if (!choice)
|
|
833
|
+
return;
|
|
834
|
+
// Find the selected agent by matching the option index
|
|
835
|
+
const idx = options.indexOf(choice);
|
|
836
|
+
if (idx < 0)
|
|
837
|
+
return;
|
|
838
|
+
const record = agents[idx];
|
|
839
|
+
await viewAgentConversation(ctx, record);
|
|
840
|
+
// Back-navigation: re-show the list
|
|
841
|
+
await showRunningAgents(ctx);
|
|
842
|
+
}
|
|
843
|
+
async function viewAgentConversation(ctx, record) {
|
|
844
|
+
if (!record.session) {
|
|
845
|
+
ctx.ui.notify(`Agent is ${record.status === "queued" ? "queued" : "expired"} — no session available.`, "info");
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
const { ConversationViewer } = await import("./ui/conversation-viewer.js");
|
|
849
|
+
const session = record.session;
|
|
850
|
+
const activity = agentActivity.get(record.id);
|
|
851
|
+
await ctx.ui.custom((tui, theme, _keybindings, done) => {
|
|
852
|
+
return new ConversationViewer(tui, session, record, activity, theme, done);
|
|
853
|
+
}, {
|
|
854
|
+
overlay: true,
|
|
855
|
+
overlayOptions: { anchor: "center", width: "90%" },
|
|
856
|
+
});
|
|
857
|
+
}
|
|
858
|
+
async function showAgentDetail(ctx, name) {
|
|
859
|
+
const cfg = getAgentConfig(name);
|
|
860
|
+
if (!cfg) {
|
|
861
|
+
ctx.ui.notify(`Agent config not found for "${name}".`, "warning");
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
const file = findAgentFile(name);
|
|
865
|
+
const isDefault = cfg.isDefault === true;
|
|
866
|
+
const disabled = cfg.enabled === false;
|
|
867
|
+
let menuOptions;
|
|
868
|
+
if (disabled && file) {
|
|
869
|
+
// Disabled agent with a file — offer Enable
|
|
870
|
+
menuOptions = isDefault
|
|
871
|
+
? ["Enable", "Edit", "Reset to default", "Delete", "Back"]
|
|
872
|
+
: ["Enable", "Edit", "Delete", "Back"];
|
|
873
|
+
}
|
|
874
|
+
else if (isDefault && !file) {
|
|
875
|
+
// Default agent with no .md override
|
|
876
|
+
menuOptions = ["Eject (export as .md)", "Disable", "Back"];
|
|
877
|
+
}
|
|
878
|
+
else if (isDefault && file) {
|
|
879
|
+
// Default agent with .md override (ejected)
|
|
880
|
+
menuOptions = ["Edit", "Disable", "Reset to default", "Delete", "Back"];
|
|
881
|
+
}
|
|
882
|
+
else {
|
|
883
|
+
// User-defined agent
|
|
884
|
+
menuOptions = ["Edit", "Disable", "Delete", "Back"];
|
|
885
|
+
}
|
|
886
|
+
const choice = await ctx.ui.select(name, menuOptions);
|
|
887
|
+
if (!choice || choice === "Back")
|
|
888
|
+
return;
|
|
889
|
+
if (choice === "Edit" && file) {
|
|
890
|
+
const content = readFileSync(file.path, "utf-8");
|
|
891
|
+
const edited = await ctx.ui.editor(`Edit ${name}`, content);
|
|
892
|
+
if (edited !== undefined && edited !== content) {
|
|
893
|
+
const { writeFileSync } = await import("node:fs");
|
|
894
|
+
writeFileSync(file.path, edited, "utf-8");
|
|
895
|
+
reloadCustomAgents();
|
|
896
|
+
ctx.ui.notify(`Updated ${file.path}`, "info");
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
else if (choice === "Delete") {
|
|
900
|
+
if (file) {
|
|
901
|
+
const confirmed = await ctx.ui.confirm("Delete agent", `Delete ${name} from ${file.location} (${file.path})?`);
|
|
902
|
+
if (confirmed) {
|
|
903
|
+
unlinkSync(file.path);
|
|
904
|
+
reloadCustomAgents();
|
|
905
|
+
ctx.ui.notify(`Deleted ${file.path}`, "info");
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
else if (choice === "Reset to default" && file) {
|
|
910
|
+
const confirmed = await ctx.ui.confirm("Reset to default", `Delete override ${file.path} and restore embedded default?`);
|
|
911
|
+
if (confirmed) {
|
|
912
|
+
unlinkSync(file.path);
|
|
913
|
+
reloadCustomAgents();
|
|
914
|
+
ctx.ui.notify(`Restored default ${name}`, "info");
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
else if (choice.startsWith("Eject")) {
|
|
918
|
+
await ejectAgent(ctx, name, cfg);
|
|
919
|
+
}
|
|
920
|
+
else if (choice === "Disable") {
|
|
921
|
+
await disableAgent(ctx, name);
|
|
922
|
+
}
|
|
923
|
+
else if (choice === "Enable") {
|
|
924
|
+
await enableAgent(ctx, name);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
/** Eject a default agent: write its embedded config as a .md file. */
|
|
928
|
+
async function ejectAgent(ctx, name, cfg) {
|
|
929
|
+
const location = await ctx.ui.select("Choose location", [
|
|
930
|
+
"Project (.pi/agents/)",
|
|
931
|
+
"Personal (~/.pi/agent/agents/)",
|
|
932
|
+
]);
|
|
933
|
+
if (!location)
|
|
934
|
+
return;
|
|
935
|
+
const targetDir = location.startsWith("Project") ? projectAgentsDir() : personalAgentsDir();
|
|
936
|
+
mkdirSync(targetDir, { recursive: true });
|
|
937
|
+
const targetPath = join(targetDir, `${name}.md`);
|
|
938
|
+
if (existsSync(targetPath)) {
|
|
939
|
+
const overwrite = await ctx.ui.confirm("Overwrite", `${targetPath} already exists. Overwrite?`);
|
|
940
|
+
if (!overwrite)
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
// Build the .md file content
|
|
944
|
+
const fmFields = [];
|
|
945
|
+
fmFields.push(`description: ${cfg.description}`);
|
|
946
|
+
if (cfg.displayName)
|
|
947
|
+
fmFields.push(`display_name: ${cfg.displayName}`);
|
|
948
|
+
fmFields.push(`tools: ${cfg.builtinToolNames?.join(", ") || "all"}`);
|
|
949
|
+
if (cfg.model)
|
|
950
|
+
fmFields.push(`model: ${cfg.model}`);
|
|
951
|
+
if (cfg.thinking)
|
|
952
|
+
fmFields.push(`thinking: ${cfg.thinking}`);
|
|
953
|
+
if (cfg.maxTurns)
|
|
954
|
+
fmFields.push(`max_turns: ${cfg.maxTurns}`);
|
|
955
|
+
fmFields.push(`prompt_mode: ${cfg.promptMode}`);
|
|
956
|
+
if (cfg.extensions === false)
|
|
957
|
+
fmFields.push("extensions: false");
|
|
958
|
+
else if (Array.isArray(cfg.extensions))
|
|
959
|
+
fmFields.push(`extensions: ${cfg.extensions.join(", ")}`);
|
|
960
|
+
if (cfg.skills === false)
|
|
961
|
+
fmFields.push("skills: false");
|
|
962
|
+
else if (Array.isArray(cfg.skills))
|
|
963
|
+
fmFields.push(`skills: ${cfg.skills.join(", ")}`);
|
|
964
|
+
if (cfg.inheritContext)
|
|
965
|
+
fmFields.push("inherit_context: true");
|
|
966
|
+
if (cfg.runInBackground)
|
|
967
|
+
fmFields.push("run_in_background: true");
|
|
968
|
+
if (cfg.isolated)
|
|
969
|
+
fmFields.push("isolated: true");
|
|
970
|
+
const content = `---\n${fmFields.join("\n")}\n---\n\n${cfg.systemPrompt}\n`;
|
|
971
|
+
const { writeFileSync } = await import("node:fs");
|
|
972
|
+
writeFileSync(targetPath, content, "utf-8");
|
|
973
|
+
reloadCustomAgents();
|
|
974
|
+
ctx.ui.notify(`Ejected ${name} to ${targetPath}`, "info");
|
|
975
|
+
}
|
|
976
|
+
/** Disable an agent: set enabled: false in its .md file, or create a stub for built-in defaults. */
|
|
977
|
+
async function disableAgent(ctx, name) {
|
|
978
|
+
const file = findAgentFile(name);
|
|
979
|
+
if (file) {
|
|
980
|
+
// Existing file — set enabled: false in frontmatter (idempotent)
|
|
981
|
+
const content = readFileSync(file.path, "utf-8");
|
|
982
|
+
if (content.includes("\nenabled: false\n")) {
|
|
983
|
+
ctx.ui.notify(`${name} is already disabled.`, "info");
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
986
|
+
const updated = content.replace(/^---\n/, "---\nenabled: false\n");
|
|
987
|
+
const { writeFileSync } = await import("node:fs");
|
|
988
|
+
writeFileSync(file.path, updated, "utf-8");
|
|
989
|
+
reloadCustomAgents();
|
|
990
|
+
ctx.ui.notify(`Disabled ${name} (${file.path})`, "info");
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
// No file (built-in default) — create a stub
|
|
994
|
+
const location = await ctx.ui.select("Choose location", [
|
|
995
|
+
"Project (.pi/agents/)",
|
|
996
|
+
"Personal (~/.pi/agent/agents/)",
|
|
997
|
+
]);
|
|
998
|
+
if (!location)
|
|
999
|
+
return;
|
|
1000
|
+
const targetDir = location.startsWith("Project") ? projectAgentsDir() : personalAgentsDir();
|
|
1001
|
+
mkdirSync(targetDir, { recursive: true });
|
|
1002
|
+
const targetPath = join(targetDir, `${name}.md`);
|
|
1003
|
+
const { writeFileSync } = await import("node:fs");
|
|
1004
|
+
writeFileSync(targetPath, "---\nenabled: false\n---\n", "utf-8");
|
|
1005
|
+
reloadCustomAgents();
|
|
1006
|
+
ctx.ui.notify(`Disabled ${name} (${targetPath})`, "info");
|
|
1007
|
+
}
|
|
1008
|
+
/** Enable a disabled agent by removing enabled: false from its frontmatter. */
|
|
1009
|
+
async function enableAgent(ctx, name) {
|
|
1010
|
+
const file = findAgentFile(name);
|
|
1011
|
+
if (!file)
|
|
1012
|
+
return;
|
|
1013
|
+
const content = readFileSync(file.path, "utf-8");
|
|
1014
|
+
const updated = content.replace(/^(---\n)enabled: false\n/, "$1");
|
|
1015
|
+
const { writeFileSync } = await import("node:fs");
|
|
1016
|
+
// If the file was just a stub ("---\n---\n"), delete it to restore the built-in default
|
|
1017
|
+
if (updated.trim() === "---\n---" || updated.trim() === "---\n---\n") {
|
|
1018
|
+
unlinkSync(file.path);
|
|
1019
|
+
reloadCustomAgents();
|
|
1020
|
+
ctx.ui.notify(`Enabled ${name} (removed ${file.path})`, "info");
|
|
1021
|
+
}
|
|
1022
|
+
else {
|
|
1023
|
+
writeFileSync(file.path, updated, "utf-8");
|
|
1024
|
+
reloadCustomAgents();
|
|
1025
|
+
ctx.ui.notify(`Enabled ${name} (${file.path})`, "info");
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
async function showCreateWizard(ctx) {
|
|
1029
|
+
const location = await ctx.ui.select("Choose location", [
|
|
1030
|
+
"Project (.pi/agents/)",
|
|
1031
|
+
"Personal (~/.pi/agent/agents/)",
|
|
1032
|
+
]);
|
|
1033
|
+
if (!location)
|
|
1034
|
+
return;
|
|
1035
|
+
const targetDir = location.startsWith("Project") ? projectAgentsDir() : personalAgentsDir();
|
|
1036
|
+
const method = await ctx.ui.select("Creation method", [
|
|
1037
|
+
"Generate with Claude (recommended)",
|
|
1038
|
+
"Manual configuration",
|
|
1039
|
+
]);
|
|
1040
|
+
if (!method)
|
|
1041
|
+
return;
|
|
1042
|
+
if (method.startsWith("Generate")) {
|
|
1043
|
+
await showGenerateWizard(ctx, targetDir);
|
|
1044
|
+
}
|
|
1045
|
+
else {
|
|
1046
|
+
await showManualWizard(ctx, targetDir);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
async function showGenerateWizard(ctx, targetDir) {
|
|
1050
|
+
const description = await ctx.ui.input("Describe what this agent should do");
|
|
1051
|
+
if (!description)
|
|
1052
|
+
return;
|
|
1053
|
+
const name = await ctx.ui.input("Agent name (filename, no spaces)");
|
|
1054
|
+
if (!name)
|
|
1055
|
+
return;
|
|
1056
|
+
mkdirSync(targetDir, { recursive: true });
|
|
1057
|
+
const targetPath = join(targetDir, `${name}.md`);
|
|
1058
|
+
if (existsSync(targetPath)) {
|
|
1059
|
+
const overwrite = await ctx.ui.confirm("Overwrite", `${targetPath} already exists. Overwrite?`);
|
|
1060
|
+
if (!overwrite)
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
ctx.ui.notify("Generating agent definition...", "info");
|
|
1064
|
+
const generatePrompt = `Create a custom pi sub-agent definition file based on this description: "${description}"
|
|
1065
|
+
|
|
1066
|
+
Write a markdown file to: ${targetPath}
|
|
1067
|
+
|
|
1068
|
+
The file format is a markdown file with YAML frontmatter and a system prompt body:
|
|
1069
|
+
|
|
1070
|
+
\`\`\`markdown
|
|
1071
|
+
---
|
|
1072
|
+
description: <one-line description shown in UI>
|
|
1073
|
+
tools: <comma-separated built-in tools: read, bash, edit, write, grep, find, ls. Use "none" for no tools. Omit for all tools>
|
|
1074
|
+
model: <optional model as "provider/modelId", e.g. "anthropic/claude-haiku-4-5-20251001". Omit to inherit parent model>
|
|
1075
|
+
thinking: <optional thinking level: off, minimal, low, medium, high, xhigh. Omit to inherit>
|
|
1076
|
+
max_turns: <optional max agentic turns, default 50. Omit for default>
|
|
1077
|
+
prompt_mode: <"replace" (body IS the full system prompt) or "append" (body is appended to default prompt). Default: replace>
|
|
1078
|
+
extensions: <true (inherit all MCP/extension tools), false (none), or comma-separated names. Default: true>
|
|
1079
|
+
skills: <true (inherit all), false (none). Default: true>
|
|
1080
|
+
inherit_context: <true to fork parent conversation into agent so it sees chat history. Default: false>
|
|
1081
|
+
run_in_background: <true to run in background by default. Default: false>
|
|
1082
|
+
isolated: <true for no extension/MCP tools, only built-in tools. Default: false>
|
|
1083
|
+
---
|
|
1084
|
+
|
|
1085
|
+
<system prompt body — instructions for the agent>
|
|
1086
|
+
\`\`\`
|
|
1087
|
+
|
|
1088
|
+
Guidelines for choosing settings:
|
|
1089
|
+
- For read-only tasks (review, analysis): tools: read, bash, grep, find, ls
|
|
1090
|
+
- For code modification tasks: include edit, write
|
|
1091
|
+
- Use prompt_mode: append if the agent should keep the default system prompt and add specialization on top
|
|
1092
|
+
- Use prompt_mode: replace for fully custom agents with their own personality/instructions
|
|
1093
|
+
- Set inherit_context: true if the agent needs to know what was discussed in the parent conversation
|
|
1094
|
+
- Set isolated: true if the agent should NOT have access to MCP servers or other extensions
|
|
1095
|
+
- Only include frontmatter fields that differ from defaults — omit fields where the default is fine
|
|
1096
|
+
|
|
1097
|
+
Write the file using the write tool. Only write the file, nothing else.`;
|
|
1098
|
+
const record = await manager.spawnAndWait(pi, ctx, "general-purpose", generatePrompt, {
|
|
1099
|
+
description: `Generate ${name} agent`,
|
|
1100
|
+
maxTurns: 5,
|
|
1101
|
+
});
|
|
1102
|
+
if (record.status === "error") {
|
|
1103
|
+
ctx.ui.notify(`Generation failed: ${record.error}`, "warning");
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
reloadCustomAgents();
|
|
1107
|
+
if (existsSync(targetPath)) {
|
|
1108
|
+
ctx.ui.notify(`Created ${targetPath}`, "info");
|
|
1109
|
+
}
|
|
1110
|
+
else {
|
|
1111
|
+
ctx.ui.notify("Agent generation completed but file was not created. Check the agent output.", "warning");
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
async function showManualWizard(ctx, targetDir) {
|
|
1115
|
+
// 1. Name
|
|
1116
|
+
const name = await ctx.ui.input("Agent name (filename, no spaces)");
|
|
1117
|
+
if (!name)
|
|
1118
|
+
return;
|
|
1119
|
+
// 2. Description
|
|
1120
|
+
const description = await ctx.ui.input("Description (one line)");
|
|
1121
|
+
if (!description)
|
|
1122
|
+
return;
|
|
1123
|
+
// 3. Tools
|
|
1124
|
+
const toolChoice = await ctx.ui.select("Tools", ["all", "none", "read-only (read, bash, grep, find, ls)", "custom..."]);
|
|
1125
|
+
if (!toolChoice)
|
|
1126
|
+
return;
|
|
1127
|
+
let tools;
|
|
1128
|
+
if (toolChoice === "all") {
|
|
1129
|
+
tools = BUILTIN_TOOL_NAMES.join(", ");
|
|
1130
|
+
}
|
|
1131
|
+
else if (toolChoice === "none") {
|
|
1132
|
+
tools = "none";
|
|
1133
|
+
}
|
|
1134
|
+
else if (toolChoice.startsWith("read-only")) {
|
|
1135
|
+
tools = "read, bash, grep, find, ls";
|
|
1136
|
+
}
|
|
1137
|
+
else {
|
|
1138
|
+
const customTools = await ctx.ui.input("Tools (comma-separated)", BUILTIN_TOOL_NAMES.join(", "));
|
|
1139
|
+
if (!customTools)
|
|
1140
|
+
return;
|
|
1141
|
+
tools = customTools;
|
|
1142
|
+
}
|
|
1143
|
+
// 4. Model
|
|
1144
|
+
const modelChoice = await ctx.ui.select("Model", [
|
|
1145
|
+
"inherit (parent model)",
|
|
1146
|
+
"haiku",
|
|
1147
|
+
"sonnet",
|
|
1148
|
+
"opus",
|
|
1149
|
+
"custom...",
|
|
1150
|
+
]);
|
|
1151
|
+
if (!modelChoice)
|
|
1152
|
+
return;
|
|
1153
|
+
let modelLine = "";
|
|
1154
|
+
if (modelChoice === "haiku")
|
|
1155
|
+
modelLine = "\nmodel: anthropic/claude-haiku-4-5-20251001";
|
|
1156
|
+
else if (modelChoice === "sonnet")
|
|
1157
|
+
modelLine = "\nmodel: anthropic/claude-sonnet-4-6";
|
|
1158
|
+
else if (modelChoice === "opus")
|
|
1159
|
+
modelLine = "\nmodel: anthropic/claude-opus-4-6";
|
|
1160
|
+
else if (modelChoice === "custom...") {
|
|
1161
|
+
const customModel = await ctx.ui.input("Model (provider/modelId)");
|
|
1162
|
+
if (customModel)
|
|
1163
|
+
modelLine = `\nmodel: ${customModel}`;
|
|
1164
|
+
}
|
|
1165
|
+
// 5. Thinking
|
|
1166
|
+
const thinkingChoice = await ctx.ui.select("Thinking level", [
|
|
1167
|
+
"inherit",
|
|
1168
|
+
"off",
|
|
1169
|
+
"minimal",
|
|
1170
|
+
"low",
|
|
1171
|
+
"medium",
|
|
1172
|
+
"high",
|
|
1173
|
+
"xhigh",
|
|
1174
|
+
]);
|
|
1175
|
+
if (!thinkingChoice)
|
|
1176
|
+
return;
|
|
1177
|
+
let thinkingLine = "";
|
|
1178
|
+
if (thinkingChoice !== "inherit")
|
|
1179
|
+
thinkingLine = `\nthinking: ${thinkingChoice}`;
|
|
1180
|
+
// 6. System prompt
|
|
1181
|
+
const systemPrompt = await ctx.ui.editor("System prompt", "");
|
|
1182
|
+
if (systemPrompt === undefined)
|
|
1183
|
+
return;
|
|
1184
|
+
// Build the file
|
|
1185
|
+
const content = `---
|
|
1186
|
+
description: ${description}
|
|
1187
|
+
tools: ${tools}${modelLine}${thinkingLine}
|
|
1188
|
+
prompt_mode: replace
|
|
1189
|
+
---
|
|
1190
|
+
|
|
1191
|
+
${systemPrompt}
|
|
1192
|
+
`;
|
|
1193
|
+
mkdirSync(targetDir, { recursive: true });
|
|
1194
|
+
const targetPath = join(targetDir, `${name}.md`);
|
|
1195
|
+
if (existsSync(targetPath)) {
|
|
1196
|
+
const overwrite = await ctx.ui.confirm("Overwrite", `${targetPath} already exists. Overwrite?`);
|
|
1197
|
+
if (!overwrite)
|
|
1198
|
+
return;
|
|
1199
|
+
}
|
|
1200
|
+
const { writeFileSync } = await import("node:fs");
|
|
1201
|
+
writeFileSync(targetPath, content, "utf-8");
|
|
1202
|
+
reloadCustomAgents();
|
|
1203
|
+
ctx.ui.notify(`Created ${targetPath}`, "info");
|
|
1204
|
+
}
|
|
1205
|
+
async function showSettings(ctx) {
|
|
1206
|
+
const choice = await ctx.ui.select("Settings", [
|
|
1207
|
+
`Max concurrency (current: ${manager.getMaxConcurrent()})`,
|
|
1208
|
+
`Default max turns (current: ${getDefaultMaxTurns()})`,
|
|
1209
|
+
`Grace turns (current: ${getGraceTurns()})`,
|
|
1210
|
+
`Join mode (current: ${getDefaultJoinMode()})`,
|
|
1211
|
+
]);
|
|
1212
|
+
if (!choice)
|
|
1213
|
+
return;
|
|
1214
|
+
if (choice.startsWith("Max concurrency")) {
|
|
1215
|
+
const val = await ctx.ui.input("Max concurrent background agents", String(manager.getMaxConcurrent()));
|
|
1216
|
+
if (val) {
|
|
1217
|
+
const n = parseInt(val, 10);
|
|
1218
|
+
if (n >= 1) {
|
|
1219
|
+
manager.setMaxConcurrent(n);
|
|
1220
|
+
ctx.ui.notify(`Max concurrency set to ${n}`, "info");
|
|
1221
|
+
}
|
|
1222
|
+
else {
|
|
1223
|
+
ctx.ui.notify("Must be a positive integer.", "warning");
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
else if (choice.startsWith("Default max turns")) {
|
|
1228
|
+
const val = await ctx.ui.input("Default max turns before wrap-up", String(getDefaultMaxTurns()));
|
|
1229
|
+
if (val) {
|
|
1230
|
+
const n = parseInt(val, 10);
|
|
1231
|
+
if (n >= 1) {
|
|
1232
|
+
setDefaultMaxTurns(n);
|
|
1233
|
+
ctx.ui.notify(`Default max turns set to ${n}`, "info");
|
|
1234
|
+
}
|
|
1235
|
+
else {
|
|
1236
|
+
ctx.ui.notify("Must be a positive integer.", "warning");
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
else if (choice.startsWith("Grace turns")) {
|
|
1241
|
+
const val = await ctx.ui.input("Grace turns after wrap-up steer", String(getGraceTurns()));
|
|
1242
|
+
if (val) {
|
|
1243
|
+
const n = parseInt(val, 10);
|
|
1244
|
+
if (n >= 1) {
|
|
1245
|
+
setGraceTurns(n);
|
|
1246
|
+
ctx.ui.notify(`Grace turns set to ${n}`, "info");
|
|
1247
|
+
}
|
|
1248
|
+
else {
|
|
1249
|
+
ctx.ui.notify("Must be a positive integer.", "warning");
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
else if (choice.startsWith("Join mode")) {
|
|
1254
|
+
const val = await ctx.ui.select("Default join mode for background agents", [
|
|
1255
|
+
"smart — auto-group 2+ agents in same turn (default)",
|
|
1256
|
+
"async — always notify individually",
|
|
1257
|
+
"group — always group background agents",
|
|
1258
|
+
]);
|
|
1259
|
+
if (val) {
|
|
1260
|
+
const mode = val.split(" ")[0];
|
|
1261
|
+
setDefaultJoinMode(mode);
|
|
1262
|
+
ctx.ui.notify(`Default join mode set to ${mode}`, "info");
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
pi.registerCommand("agents", {
|
|
1267
|
+
description: "Manage agents",
|
|
1268
|
+
handler: async (_args, ctx) => { await showAgentsMenu(ctx); },
|
|
1269
|
+
});
|
|
1270
|
+
}
|