@gotgenes/pi-subagents 1.0.0
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/.markdownlint-cli2.yaml +19 -0
- package/.prettierignore +5 -0
- package/.release-please-manifest.json +3 -0
- package/AGENTS.md +85 -0
- package/CHANGELOG.md +495 -0
- package/LICENSE +21 -0
- package/README.md +528 -0
- package/dist/agent-manager.d.ts +108 -0
- package/dist/agent-manager.js +390 -0
- package/dist/agent-runner.d.ts +93 -0
- package/dist/agent-runner.js +428 -0
- package/dist/agent-types.d.ts +48 -0
- package/dist/agent-types.js +136 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +46 -0
- package/dist/cross-extension-rpc.js +54 -0
- package/dist/custom-agents.d.ts +14 -0
- package/dist/custom-agents.js +127 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +119 -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 +1731 -0
- package/dist/invocation-config.d.ts +22 -0
- package/dist/invocation-config.js +15 -0
- package/dist/memory.d.ts +49 -0
- package/dist/memory.js +151 -0
- package/dist/model-resolver.d.ts +19 -0
- package/dist/model-resolver.js +62 -0
- package/dist/output-file.d.ts +24 -0
- package/dist/output-file.js +86 -0
- package/dist/prompts.d.ts +29 -0
- package/dist/prompts.js +72 -0
- package/dist/schedule-store.d.ts +36 -0
- package/dist/schedule-store.js +144 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +338 -0
- package/dist/settings.d.ts +66 -0
- package/dist/settings.js +130 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/types.d.ts +164 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-widget.d.ts +134 -0
- package/dist/ui/agent-widget.js +451 -0
- package/dist/ui/conversation-viewer.d.ts +35 -0
- package/dist/ui/conversation-viewer.js +252 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +95 -0
- package/dist/usage.d.ts +50 -0
- package/dist/usage.js +49 -0
- package/dist/worktree.d.ts +36 -0
- package/dist/worktree.js +139 -0
- package/docs/decisions/0001-deferred-patches.md +75 -0
- package/package.json +68 -0
- package/prek.toml +24 -0
- package/release-please-config.json +22 -0
- package/src/agent-manager.ts +482 -0
- package/src/agent-runner.ts +625 -0
- package/src/agent-types.ts +164 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +95 -0
- package/src/custom-agents.ts +136 -0
- package/src/default-agents.ts +123 -0
- package/src/env.ts +33 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +1894 -0
- package/src/invocation-config.ts +40 -0
- package/src/memory.ts +165 -0
- package/src/model-resolver.ts +81 -0
- package/src/output-file.ts +96 -0
- package/src/prompts.ts +105 -0
- package/src/schedule-store.ts +143 -0
- package/src/schedule.ts +365 -0
- package/src/settings.ts +186 -0
- package/src/skill-loader.ts +102 -0
- package/src/types.ts +176 -0
- package/src/ui/agent-widget.ts +533 -0
- package/src/ui/conversation-viewer.ts +261 -0
- package/src/ui/schedule-menu.ts +104 -0
- package/src/usage.ts +60 -0
- package/src/worktree.ts +162 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-runner.ts — Core execution engine: creates sessions, runs agents, collects results.
|
|
3
|
+
*/
|
|
4
|
+
import { createAgentSession, DefaultResourceLoader, getAgentDir, SessionManager, SettingsManager, } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { getAgentConfig, getConfig, getMemoryToolNames, getReadOnlyMemoryToolNames, getToolNamesForType, } from "./agent-types.js";
|
|
6
|
+
import { buildParentContext, extractText } from "./context.js";
|
|
7
|
+
import { DEFAULT_AGENTS } from "./default-agents.js";
|
|
8
|
+
import { detectEnv } from "./env.js";
|
|
9
|
+
import { buildMemoryBlock, buildReadOnlyMemoryBlock } from "./memory.js";
|
|
10
|
+
import { buildAgentPrompt } from "./prompts.js";
|
|
11
|
+
import { preloadSkills } from "./skill-loader.js";
|
|
12
|
+
/** Names of tools registered by this extension that subagents must NOT inherit. */
|
|
13
|
+
const EXCLUDED_TOOL_NAMES = ["Agent", "get_subagent_result", "steer_subagent"];
|
|
14
|
+
/**
|
|
15
|
+
* Filter the session's active tool names according to extension/denylist rules.
|
|
16
|
+
*
|
|
17
|
+
* Run twice — once before `bindExtensions` (filters built-in tools) and once after
|
|
18
|
+
* (filters extension-registered tools, which only join the active set during
|
|
19
|
+
* `bindExtensions`). Extracting this keeps the two callsites consistent and makes
|
|
20
|
+
* the post-bind re-filter trivial.
|
|
21
|
+
*
|
|
22
|
+
* @param activeTools Names currently active on the session.
|
|
23
|
+
* @param toolNames The built-in tool name allowlist for this agent type.
|
|
24
|
+
* @param extensions Agent config `extensions` field: false | true | string[] (allowlist).
|
|
25
|
+
* @param disallowedSet Optional denylist from agent config.
|
|
26
|
+
*/
|
|
27
|
+
function filterActiveTools(activeTools, toolNames, extensions, disallowedSet) {
|
|
28
|
+
if (extensions === false) {
|
|
29
|
+
// Extensions disabled: only apply the denylist to built-in tools.
|
|
30
|
+
if (!disallowedSet)
|
|
31
|
+
return activeTools;
|
|
32
|
+
return activeTools.filter((t) => !disallowedSet.has(t));
|
|
33
|
+
}
|
|
34
|
+
const builtinToolNameSet = new Set(toolNames);
|
|
35
|
+
return activeTools.filter((t) => {
|
|
36
|
+
if (EXCLUDED_TOOL_NAMES.includes(t))
|
|
37
|
+
return false;
|
|
38
|
+
if (disallowedSet?.has(t))
|
|
39
|
+
return false;
|
|
40
|
+
if (builtinToolNameSet.has(t))
|
|
41
|
+
return true;
|
|
42
|
+
if (Array.isArray(extensions)) {
|
|
43
|
+
return extensions.some((ext) => t.startsWith(ext) || t.includes(ext));
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/** Default max turns. undefined = unlimited (no turn limit). */
|
|
49
|
+
let defaultMaxTurns;
|
|
50
|
+
/** Normalize max turns. undefined or 0 = unlimited, otherwise minimum 1. */
|
|
51
|
+
export function normalizeMaxTurns(n) {
|
|
52
|
+
if (n == null || n === 0)
|
|
53
|
+
return undefined;
|
|
54
|
+
return Math.max(1, n);
|
|
55
|
+
}
|
|
56
|
+
/** Get the default max turns value. undefined = unlimited. */
|
|
57
|
+
export function getDefaultMaxTurns() {
|
|
58
|
+
return defaultMaxTurns;
|
|
59
|
+
}
|
|
60
|
+
/** Set the default max turns value. undefined or 0 = unlimited, otherwise minimum 1. */
|
|
61
|
+
export function setDefaultMaxTurns(n) {
|
|
62
|
+
defaultMaxTurns = normalizeMaxTurns(n);
|
|
63
|
+
}
|
|
64
|
+
/** Additional turns allowed after the soft limit steer message. */
|
|
65
|
+
let graceTurns = 5;
|
|
66
|
+
/** Get the grace turns value. */
|
|
67
|
+
export function getGraceTurns() {
|
|
68
|
+
return graceTurns;
|
|
69
|
+
}
|
|
70
|
+
/** Set the grace turns value (minimum 1). */
|
|
71
|
+
export function setGraceTurns(n) {
|
|
72
|
+
graceTurns = Math.max(1, n);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Try to find the right model for an agent type.
|
|
76
|
+
* Priority: explicit option > config.model > parent model.
|
|
77
|
+
*/
|
|
78
|
+
function resolveDefaultModel(parentModel, registry, configModel) {
|
|
79
|
+
if (configModel) {
|
|
80
|
+
const slashIdx = configModel.indexOf("/");
|
|
81
|
+
if (slashIdx !== -1) {
|
|
82
|
+
const provider = configModel.slice(0, slashIdx);
|
|
83
|
+
const modelId = configModel.slice(slashIdx + 1);
|
|
84
|
+
// Build a set of available model keys for fast lookup
|
|
85
|
+
const available = registry.getAvailable?.();
|
|
86
|
+
const availableKeys = available
|
|
87
|
+
? new Set(available.map((m) => `${m.provider}/${m.id}`))
|
|
88
|
+
: undefined;
|
|
89
|
+
const isAvailable = (p, id) => !availableKeys || availableKeys.has(`${p}/${id}`);
|
|
90
|
+
const found = registry.find(provider, modelId);
|
|
91
|
+
if (found && isAvailable(provider, modelId))
|
|
92
|
+
return found;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return parentModel;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Subscribe to a session and collect the last assistant message text.
|
|
99
|
+
* Returns an object with a `getText()` getter and an `unsubscribe` function.
|
|
100
|
+
*/
|
|
101
|
+
function collectResponseText(session) {
|
|
102
|
+
let text = "";
|
|
103
|
+
const unsubscribe = session.subscribe((event) => {
|
|
104
|
+
if (event.type === "message_start") {
|
|
105
|
+
text = "";
|
|
106
|
+
}
|
|
107
|
+
if (event.type === "message_update" &&
|
|
108
|
+
event.assistantMessageEvent.type === "text_delta") {
|
|
109
|
+
text += event.assistantMessageEvent.delta;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
return { getText: () => text, unsubscribe };
|
|
113
|
+
}
|
|
114
|
+
/** Get the last assistant text from the completed session history. */
|
|
115
|
+
function getLastAssistantText(session) {
|
|
116
|
+
for (let i = session.messages.length - 1; i >= 0; i--) {
|
|
117
|
+
const msg = session.messages[i];
|
|
118
|
+
if (msg.role !== "assistant")
|
|
119
|
+
continue;
|
|
120
|
+
const text = extractText(msg.content).trim();
|
|
121
|
+
if (text)
|
|
122
|
+
return text;
|
|
123
|
+
}
|
|
124
|
+
return "";
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Wire an AbortSignal to abort a session.
|
|
128
|
+
* Returns a cleanup function to remove the listener.
|
|
129
|
+
*/
|
|
130
|
+
function forwardAbortSignal(session, signal) {
|
|
131
|
+
if (!signal)
|
|
132
|
+
return () => { };
|
|
133
|
+
const onAbort = () => session.abort();
|
|
134
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
135
|
+
return () => signal.removeEventListener("abort", onAbort);
|
|
136
|
+
}
|
|
137
|
+
export async function runAgent(ctx, type, prompt, options) {
|
|
138
|
+
const config = getConfig(type);
|
|
139
|
+
const agentConfig = getAgentConfig(type);
|
|
140
|
+
// Resolve working directory: worktree override > parent cwd
|
|
141
|
+
const effectiveCwd = options.cwd ?? ctx.cwd;
|
|
142
|
+
const env = await detectEnv(options.pi, effectiveCwd);
|
|
143
|
+
// Get parent system prompt for append-mode agents
|
|
144
|
+
const parentSystemPrompt = ctx.getSystemPrompt();
|
|
145
|
+
// Build prompt extras (memory, skill preloading)
|
|
146
|
+
const extras = {};
|
|
147
|
+
// Resolve extensions/skills: isolated overrides to false
|
|
148
|
+
const extensions = options.isolated ? false : config.extensions;
|
|
149
|
+
const skills = options.isolated ? false : config.skills;
|
|
150
|
+
// Skill preloading: when skills is string[], preload their content into prompt
|
|
151
|
+
if (Array.isArray(skills)) {
|
|
152
|
+
const loaded = preloadSkills(skills, effectiveCwd);
|
|
153
|
+
if (loaded.length > 0) {
|
|
154
|
+
extras.skillBlocks = loaded;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
let toolNames = getToolNamesForType(type);
|
|
158
|
+
// Persistent memory: detect write capability and branch accordingly.
|
|
159
|
+
// Account for disallowedTools — a tool in the base set but on the denylist is not truly available.
|
|
160
|
+
if (agentConfig?.memory) {
|
|
161
|
+
const existingNames = new Set(toolNames);
|
|
162
|
+
const denied = agentConfig.disallowedTools
|
|
163
|
+
? new Set(agentConfig.disallowedTools)
|
|
164
|
+
: undefined;
|
|
165
|
+
const effectivelyHas = (name) => existingNames.has(name) && !denied?.has(name);
|
|
166
|
+
const hasWriteTools = effectivelyHas("write") || effectivelyHas("edit");
|
|
167
|
+
if (hasWriteTools) {
|
|
168
|
+
// Read-write memory: add any missing memory tool names (read/write/edit)
|
|
169
|
+
const extraNames = getMemoryToolNames(existingNames);
|
|
170
|
+
if (extraNames.length > 0)
|
|
171
|
+
toolNames = [...toolNames, ...extraNames];
|
|
172
|
+
extras.memoryBlock = buildMemoryBlock(agentConfig.name, agentConfig.memory, effectiveCwd);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
// Read-only memory: only add read tool name, use read-only prompt
|
|
176
|
+
const extraNames = getReadOnlyMemoryToolNames(existingNames);
|
|
177
|
+
if (extraNames.length > 0)
|
|
178
|
+
toolNames = [...toolNames, ...extraNames];
|
|
179
|
+
extras.memoryBlock = buildReadOnlyMemoryBlock(agentConfig.name, agentConfig.memory, effectiveCwd);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// Build system prompt from agent config
|
|
183
|
+
let systemPrompt;
|
|
184
|
+
if (agentConfig) {
|
|
185
|
+
systemPrompt = buildAgentPrompt(agentConfig, effectiveCwd, env, parentSystemPrompt, extras);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
// Unknown type fallback: spread the canonical general-purpose config (defensive —
|
|
189
|
+
// unreachable in practice since index.ts resolves unknown types before calling runAgent).
|
|
190
|
+
const fallback = DEFAULT_AGENTS.get("general-purpose");
|
|
191
|
+
if (!fallback)
|
|
192
|
+
throw new Error(`No fallback config available for unknown type "${type}"`);
|
|
193
|
+
systemPrompt = buildAgentPrompt({ ...fallback, name: type }, effectiveCwd, env, parentSystemPrompt, extras);
|
|
194
|
+
}
|
|
195
|
+
// When skills is string[], we've already preloaded them into the prompt.
|
|
196
|
+
// Still pass noSkills: true since we don't need the skill loader to load them again.
|
|
197
|
+
const noSkills = skills === false || Array.isArray(skills);
|
|
198
|
+
const agentDir = getAgentDir();
|
|
199
|
+
// Load extensions/skills: true or string[] → load; false → don't.
|
|
200
|
+
// Suppress AGENTS.md/CLAUDE.md and APPEND_SYSTEM.md — upstream's
|
|
201
|
+
// buildSystemPrompt() re-appends both AFTER systemPromptOverride, which
|
|
202
|
+
// would defeat prompt_mode: replace and isolated: true. Parent context, if
|
|
203
|
+
// wanted, reaches the subagent via prompt_mode: append (parentSystemPrompt
|
|
204
|
+
// is embedded in systemPromptOverride) or inherit_context (conversation).
|
|
205
|
+
const loader = new DefaultResourceLoader({
|
|
206
|
+
cwd: effectiveCwd,
|
|
207
|
+
agentDir,
|
|
208
|
+
noExtensions: extensions === false,
|
|
209
|
+
noSkills,
|
|
210
|
+
noPromptTemplates: true,
|
|
211
|
+
noThemes: true,
|
|
212
|
+
noContextFiles: true,
|
|
213
|
+
systemPromptOverride: () => systemPrompt,
|
|
214
|
+
appendSystemPromptOverride: () => [],
|
|
215
|
+
});
|
|
216
|
+
await loader.reload();
|
|
217
|
+
// Resolve model: explicit option > config.model > parent model
|
|
218
|
+
const model = options.model ??
|
|
219
|
+
resolveDefaultModel(ctx.model, ctx.modelRegistry, agentConfig?.model);
|
|
220
|
+
// Resolve thinking level: explicit option > agent config > undefined (inherit)
|
|
221
|
+
const thinkingLevel = options.thinkingLevel ?? agentConfig?.thinking;
|
|
222
|
+
const sessionOpts = {
|
|
223
|
+
cwd: effectiveCwd,
|
|
224
|
+
agentDir,
|
|
225
|
+
sessionManager: SessionManager.inMemory(effectiveCwd),
|
|
226
|
+
settingsManager: SettingsManager.create(effectiveCwd, agentDir),
|
|
227
|
+
modelRegistry: ctx.modelRegistry,
|
|
228
|
+
model,
|
|
229
|
+
tools: toolNames,
|
|
230
|
+
resourceLoader: loader,
|
|
231
|
+
};
|
|
232
|
+
if (thinkingLevel) {
|
|
233
|
+
sessionOpts.thinkingLevel = thinkingLevel;
|
|
234
|
+
}
|
|
235
|
+
const { session } = await createAgentSession(sessionOpts);
|
|
236
|
+
// Build disallowed tools set from agent config
|
|
237
|
+
const disallowedSet = agentConfig?.disallowedTools
|
|
238
|
+
? new Set(agentConfig.disallowedTools)
|
|
239
|
+
: undefined;
|
|
240
|
+
// Filter active tools: remove our own tools to prevent nesting,
|
|
241
|
+
// apply extension allowlist if specified, and apply disallowedTools denylist.
|
|
242
|
+
// First pass — over built-in tools, before bindExtensions registers extension tools.
|
|
243
|
+
if (extensions !== false || disallowedSet) {
|
|
244
|
+
const filtered = filterActiveTools(session.getActiveToolNames(), toolNames, extensions, disallowedSet);
|
|
245
|
+
session.setActiveToolsByName(filtered);
|
|
246
|
+
}
|
|
247
|
+
// Bind extensions so that session_start fires and extensions can initialize
|
|
248
|
+
// (e.g. loading credentials, setting up state). Placed after tool filtering
|
|
249
|
+
// so extension-provided skills/prompts from extendResourcesFromExtensions()
|
|
250
|
+
// respect the active tool set. All ExtensionBindings fields are optional.
|
|
251
|
+
await session.bindExtensions({
|
|
252
|
+
onError: (err) => {
|
|
253
|
+
options.onToolActivity?.({
|
|
254
|
+
type: "end",
|
|
255
|
+
toolName: `extension-error:${err.extensionPath}`,
|
|
256
|
+
});
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
// Patch 2 (RepOne #443): re-filter active tools after bindExtensions.
|
|
260
|
+
// Extension-registered tools (added during bindExtensions) are not in the
|
|
261
|
+
// session's active set when the first filter pass runs above. Without this
|
|
262
|
+
// re-filter, the `extensions: string[]` allowlist branch never matches any
|
|
263
|
+
// extension tools and `extensions: true` lets non-allowlisted denylist
|
|
264
|
+
// entries slip in. Run the same filter against the post-bind active set.
|
|
265
|
+
if (extensions !== false || disallowedSet) {
|
|
266
|
+
const refiltered = filterActiveTools(session.getActiveToolNames(), toolNames, extensions, disallowedSet);
|
|
267
|
+
session.setActiveToolsByName(refiltered);
|
|
268
|
+
}
|
|
269
|
+
options.onSessionCreated?.(session);
|
|
270
|
+
// Track turns for graceful max_turns enforcement
|
|
271
|
+
let turnCount = 0;
|
|
272
|
+
const maxTurns = normalizeMaxTurns(options.maxTurns ?? agentConfig?.maxTurns ?? defaultMaxTurns);
|
|
273
|
+
let softLimitReached = false;
|
|
274
|
+
let aborted = false;
|
|
275
|
+
let currentMessageText = "";
|
|
276
|
+
const unsubTurns = session.subscribe((event) => {
|
|
277
|
+
if (event.type === "turn_end") {
|
|
278
|
+
turnCount++;
|
|
279
|
+
options.onTurnEnd?.(turnCount);
|
|
280
|
+
if (maxTurns != null) {
|
|
281
|
+
if (!softLimitReached && turnCount >= maxTurns) {
|
|
282
|
+
softLimitReached = true;
|
|
283
|
+
session.steer("You have reached your turn limit. Wrap up immediately — provide your final answer now.");
|
|
284
|
+
}
|
|
285
|
+
else if (softLimitReached && turnCount >= maxTurns + graceTurns) {
|
|
286
|
+
aborted = true;
|
|
287
|
+
session.abort();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (event.type === "message_start") {
|
|
292
|
+
currentMessageText = "";
|
|
293
|
+
}
|
|
294
|
+
if (event.type === "message_update" &&
|
|
295
|
+
event.assistantMessageEvent.type === "text_delta") {
|
|
296
|
+
currentMessageText += event.assistantMessageEvent.delta;
|
|
297
|
+
options.onTextDelta?.(event.assistantMessageEvent.delta, currentMessageText);
|
|
298
|
+
}
|
|
299
|
+
if (event.type === "tool_execution_start") {
|
|
300
|
+
options.onToolActivity?.({ type: "start", toolName: event.toolName });
|
|
301
|
+
}
|
|
302
|
+
if (event.type === "tool_execution_end") {
|
|
303
|
+
options.onToolActivity?.({ type: "end", toolName: event.toolName });
|
|
304
|
+
}
|
|
305
|
+
if (event.type === "message_end" && event.message.role === "assistant") {
|
|
306
|
+
const u = event.message.usage;
|
|
307
|
+
if (u)
|
|
308
|
+
options.onAssistantUsage?.({
|
|
309
|
+
input: u.input ?? 0,
|
|
310
|
+
output: u.output ?? 0,
|
|
311
|
+
cacheWrite: u.cacheWrite ?? 0,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
if (event.type === "compaction_end" && !event.aborted && event.result) {
|
|
315
|
+
options.onCompaction?.({
|
|
316
|
+
reason: event.reason,
|
|
317
|
+
tokensBefore: event.result.tokensBefore,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
const collector = collectResponseText(session);
|
|
322
|
+
const cleanupAbort = forwardAbortSignal(session, options.signal);
|
|
323
|
+
// Build the effective prompt: optionally prepend parent context
|
|
324
|
+
let effectivePrompt = prompt;
|
|
325
|
+
if (options.inheritContext) {
|
|
326
|
+
const parentContext = buildParentContext(ctx);
|
|
327
|
+
if (parentContext) {
|
|
328
|
+
effectivePrompt = parentContext + prompt;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
try {
|
|
332
|
+
await session.prompt(effectivePrompt);
|
|
333
|
+
}
|
|
334
|
+
finally {
|
|
335
|
+
unsubTurns();
|
|
336
|
+
collector.unsubscribe();
|
|
337
|
+
cleanupAbort();
|
|
338
|
+
}
|
|
339
|
+
const responseText = collector.getText().trim() || getLastAssistantText(session);
|
|
340
|
+
return { responseText, session, aborted, steered: softLimitReached };
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Send a new prompt to an existing session (resume).
|
|
344
|
+
*/
|
|
345
|
+
export async function resumeAgent(session, prompt, options = {}) {
|
|
346
|
+
const collector = collectResponseText(session);
|
|
347
|
+
const cleanupAbort = forwardAbortSignal(session, options.signal);
|
|
348
|
+
const unsubEvents = options.onToolActivity || options.onAssistantUsage || options.onCompaction
|
|
349
|
+
? session.subscribe((event) => {
|
|
350
|
+
if (event.type === "tool_execution_start")
|
|
351
|
+
options.onToolActivity?.({
|
|
352
|
+
type: "start",
|
|
353
|
+
toolName: event.toolName,
|
|
354
|
+
});
|
|
355
|
+
if (event.type === "tool_execution_end")
|
|
356
|
+
options.onToolActivity?.({ type: "end", toolName: event.toolName });
|
|
357
|
+
if (event.type === "message_end" &&
|
|
358
|
+
event.message.role === "assistant") {
|
|
359
|
+
const u = event.message.usage;
|
|
360
|
+
if (u)
|
|
361
|
+
options.onAssistantUsage?.({
|
|
362
|
+
input: u.input ?? 0,
|
|
363
|
+
output: u.output ?? 0,
|
|
364
|
+
cacheWrite: u.cacheWrite ?? 0,
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
if (event.type === "compaction_end" &&
|
|
368
|
+
!event.aborted &&
|
|
369
|
+
event.result) {
|
|
370
|
+
options.onCompaction?.({
|
|
371
|
+
reason: event.reason,
|
|
372
|
+
tokensBefore: event.result.tokensBefore,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
})
|
|
376
|
+
: () => { };
|
|
377
|
+
try {
|
|
378
|
+
await session.prompt(prompt);
|
|
379
|
+
}
|
|
380
|
+
finally {
|
|
381
|
+
collector.unsubscribe();
|
|
382
|
+
unsubEvents();
|
|
383
|
+
cleanupAbort();
|
|
384
|
+
}
|
|
385
|
+
return collector.getText().trim() || getLastAssistantText(session);
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Send a steering message to a running subagent.
|
|
389
|
+
* The message will interrupt the agent after its current tool execution.
|
|
390
|
+
*/
|
|
391
|
+
export async function steerAgent(session, message) {
|
|
392
|
+
await session.steer(message);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Get the subagent's conversation messages as formatted text.
|
|
396
|
+
*/
|
|
397
|
+
export function getAgentConversation(session) {
|
|
398
|
+
const parts = [];
|
|
399
|
+
for (const msg of session.messages) {
|
|
400
|
+
if (msg.role === "user") {
|
|
401
|
+
const text = typeof msg.content === "string"
|
|
402
|
+
? msg.content
|
|
403
|
+
: extractText(msg.content);
|
|
404
|
+
if (text.trim())
|
|
405
|
+
parts.push(`[User]: ${text.trim()}`);
|
|
406
|
+
}
|
|
407
|
+
else if (msg.role === "assistant") {
|
|
408
|
+
const textParts = [];
|
|
409
|
+
const toolCalls = [];
|
|
410
|
+
for (const c of msg.content) {
|
|
411
|
+
if (c.type === "text" && c.text)
|
|
412
|
+
textParts.push(c.text);
|
|
413
|
+
else if (c.type === "toolCall")
|
|
414
|
+
toolCalls.push(` Tool: ${c.name ?? c.toolName ?? "unknown"}`);
|
|
415
|
+
}
|
|
416
|
+
if (textParts.length > 0)
|
|
417
|
+
parts.push(`[Assistant]: ${textParts.join("\n")}`);
|
|
418
|
+
if (toolCalls.length > 0)
|
|
419
|
+
parts.push(`[Tool Calls]:\n${toolCalls.join("\n")}`);
|
|
420
|
+
}
|
|
421
|
+
else if (msg.role === "toolResult") {
|
|
422
|
+
const text = extractText(msg.content);
|
|
423
|
+
const truncated = text.length > 200 ? text.slice(0, 200) + "..." : text;
|
|
424
|
+
parts.push(`[Tool Result (${msg.toolName})]: ${truncated}`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return parts.join("\n\n");
|
|
428
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-types.ts — Unified agent type registry.
|
|
3
|
+
*
|
|
4
|
+
* Merges embedded default agents with user-defined agents from .pi/agents/*.md.
|
|
5
|
+
* User agents override defaults with the same name. Disabled agents are kept but excluded from spawning.
|
|
6
|
+
*/
|
|
7
|
+
import type { AgentConfig } from "./types.js";
|
|
8
|
+
/** All known built-in tool names. */
|
|
9
|
+
export declare const BUILTIN_TOOL_NAMES: string[];
|
|
10
|
+
/**
|
|
11
|
+
* Register agents into the unified registry.
|
|
12
|
+
* Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
|
|
13
|
+
* Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
|
|
14
|
+
*/
|
|
15
|
+
export declare function registerAgents(userAgents: Map<string, AgentConfig>): void;
|
|
16
|
+
/** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
|
|
17
|
+
export declare function resolveType(name: string): string | undefined;
|
|
18
|
+
/** Get the agent config for a type (case-insensitive). */
|
|
19
|
+
export declare function getAgentConfig(name: string): AgentConfig | undefined;
|
|
20
|
+
/** Get all enabled type names (for spawning and tool descriptions). */
|
|
21
|
+
export declare function getAvailableTypes(): string[];
|
|
22
|
+
/** Get all type names including disabled (for UI listing). */
|
|
23
|
+
export declare function getAllTypes(): string[];
|
|
24
|
+
/** Get names of default agents currently in the registry. */
|
|
25
|
+
export declare function getDefaultAgentNames(): string[];
|
|
26
|
+
/** Get names of user-defined agents (non-defaults) currently in the registry. */
|
|
27
|
+
export declare function getUserAgentNames(): string[];
|
|
28
|
+
/** Check if a type is valid and enabled (case-insensitive). */
|
|
29
|
+
export declare function isValidType(type: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Get memory tool names (read/write/edit) not already in the provided set.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getMemoryToolNames(existingToolNames: Set<string>): string[];
|
|
34
|
+
/**
|
|
35
|
+
* Get read-only memory tool names not already in the provided set.
|
|
36
|
+
*/
|
|
37
|
+
export declare function getReadOnlyMemoryToolNames(existingToolNames: Set<string>): string[];
|
|
38
|
+
/** Get built-in tool names for a type (case-insensitive). */
|
|
39
|
+
export declare function getToolNamesForType(type: string): string[];
|
|
40
|
+
/** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
|
|
41
|
+
export declare function getConfig(type: string): {
|
|
42
|
+
displayName: string;
|
|
43
|
+
description: string;
|
|
44
|
+
builtinToolNames: string[];
|
|
45
|
+
extensions: true | string[] | false;
|
|
46
|
+
skills: true | string[] | false;
|
|
47
|
+
promptMode: "replace" | "append";
|
|
48
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-types.ts — Unified agent type registry.
|
|
3
|
+
*
|
|
4
|
+
* Merges embedded default agents with user-defined agents from .pi/agents/*.md.
|
|
5
|
+
* User agents override defaults with the same name. Disabled agents are kept but excluded from spawning.
|
|
6
|
+
*/
|
|
7
|
+
import { DEFAULT_AGENTS } from "./default-agents.js";
|
|
8
|
+
/** All known built-in tool names. */
|
|
9
|
+
export const BUILTIN_TOOL_NAMES = ["read", "bash", "edit", "write", "grep", "find", "ls"];
|
|
10
|
+
/** Unified runtime registry of all agents (defaults + user-defined). */
|
|
11
|
+
const agents = new Map();
|
|
12
|
+
/**
|
|
13
|
+
* Register agents into the unified registry.
|
|
14
|
+
* Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
|
|
15
|
+
* Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
|
|
16
|
+
*/
|
|
17
|
+
export function registerAgents(userAgents) {
|
|
18
|
+
agents.clear();
|
|
19
|
+
// Start with defaults
|
|
20
|
+
for (const [name, config] of DEFAULT_AGENTS) {
|
|
21
|
+
agents.set(name, config);
|
|
22
|
+
}
|
|
23
|
+
// Overlay user agents (overrides defaults with same name)
|
|
24
|
+
for (const [name, config] of userAgents) {
|
|
25
|
+
agents.set(name, config);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Case-insensitive key resolution. */
|
|
29
|
+
function resolveKey(name) {
|
|
30
|
+
if (agents.has(name))
|
|
31
|
+
return name;
|
|
32
|
+
const lower = name.toLowerCase();
|
|
33
|
+
for (const key of agents.keys()) {
|
|
34
|
+
if (key.toLowerCase() === lower)
|
|
35
|
+
return key;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
/** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
|
|
40
|
+
export function resolveType(name) {
|
|
41
|
+
return resolveKey(name);
|
|
42
|
+
}
|
|
43
|
+
/** Get the agent config for a type (case-insensitive). */
|
|
44
|
+
export function getAgentConfig(name) {
|
|
45
|
+
const key = resolveKey(name);
|
|
46
|
+
return key ? agents.get(key) : undefined;
|
|
47
|
+
}
|
|
48
|
+
/** Get all enabled type names (for spawning and tool descriptions). */
|
|
49
|
+
export function getAvailableTypes() {
|
|
50
|
+
return [...agents.entries()]
|
|
51
|
+
.filter(([_, config]) => config.enabled !== false)
|
|
52
|
+
.map(([name]) => name);
|
|
53
|
+
}
|
|
54
|
+
/** Get all type names including disabled (for UI listing). */
|
|
55
|
+
export function getAllTypes() {
|
|
56
|
+
return [...agents.keys()];
|
|
57
|
+
}
|
|
58
|
+
/** Get names of default agents currently in the registry. */
|
|
59
|
+
export function getDefaultAgentNames() {
|
|
60
|
+
return [...agents.entries()]
|
|
61
|
+
.filter(([_, config]) => config.isDefault === true)
|
|
62
|
+
.map(([name]) => name);
|
|
63
|
+
}
|
|
64
|
+
/** Get names of user-defined agents (non-defaults) currently in the registry. */
|
|
65
|
+
export function getUserAgentNames() {
|
|
66
|
+
return [...agents.entries()]
|
|
67
|
+
.filter(([_, config]) => config.isDefault !== true)
|
|
68
|
+
.map(([name]) => name);
|
|
69
|
+
}
|
|
70
|
+
/** Check if a type is valid and enabled (case-insensitive). */
|
|
71
|
+
export function isValidType(type) {
|
|
72
|
+
const key = resolveKey(type);
|
|
73
|
+
if (!key)
|
|
74
|
+
return false;
|
|
75
|
+
return agents.get(key)?.enabled !== false;
|
|
76
|
+
}
|
|
77
|
+
/** Tool names required for memory management. */
|
|
78
|
+
const MEMORY_TOOL_NAMES = ["read", "write", "edit"];
|
|
79
|
+
/**
|
|
80
|
+
* Get memory tool names (read/write/edit) not already in the provided set.
|
|
81
|
+
*/
|
|
82
|
+
export function getMemoryToolNames(existingToolNames) {
|
|
83
|
+
return MEMORY_TOOL_NAMES.filter(n => !existingToolNames.has(n));
|
|
84
|
+
}
|
|
85
|
+
/** Tool names needed for read-only memory access. */
|
|
86
|
+
const READONLY_MEMORY_TOOL_NAMES = ["read"];
|
|
87
|
+
/**
|
|
88
|
+
* Get read-only memory tool names not already in the provided set.
|
|
89
|
+
*/
|
|
90
|
+
export function getReadOnlyMemoryToolNames(existingToolNames) {
|
|
91
|
+
return READONLY_MEMORY_TOOL_NAMES.filter(n => !existingToolNames.has(n));
|
|
92
|
+
}
|
|
93
|
+
/** Get built-in tool names for a type (case-insensitive). */
|
|
94
|
+
export function getToolNamesForType(type) {
|
|
95
|
+
const key = resolveKey(type);
|
|
96
|
+
const raw = key ? agents.get(key) : undefined;
|
|
97
|
+
const config = raw?.enabled !== false ? raw : undefined;
|
|
98
|
+
const names = config?.builtinToolNames?.length ? config.builtinToolNames : [...BUILTIN_TOOL_NAMES];
|
|
99
|
+
return names;
|
|
100
|
+
}
|
|
101
|
+
/** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
|
|
102
|
+
export function getConfig(type) {
|
|
103
|
+
const key = resolveKey(type);
|
|
104
|
+
const config = key ? agents.get(key) : undefined;
|
|
105
|
+
if (config && config.enabled !== false) {
|
|
106
|
+
return {
|
|
107
|
+
displayName: config.displayName ?? config.name,
|
|
108
|
+
description: config.description,
|
|
109
|
+
builtinToolNames: config.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
110
|
+
extensions: config.extensions,
|
|
111
|
+
skills: config.skills,
|
|
112
|
+
promptMode: config.promptMode,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
// Fallback for unknown/disabled types — general-purpose config
|
|
116
|
+
const gp = agents.get("general-purpose");
|
|
117
|
+
if (gp && gp.enabled !== false) {
|
|
118
|
+
return {
|
|
119
|
+
displayName: gp.displayName ?? gp.name,
|
|
120
|
+
description: gp.description,
|
|
121
|
+
builtinToolNames: gp.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
122
|
+
extensions: gp.extensions,
|
|
123
|
+
skills: gp.skills,
|
|
124
|
+
promptMode: gp.promptMode,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
// Absolute fallback (should never happen)
|
|
128
|
+
return {
|
|
129
|
+
displayName: "Agent",
|
|
130
|
+
description: "General-purpose agent for complex, multi-step tasks",
|
|
131
|
+
builtinToolNames: BUILTIN_TOOL_NAMES,
|
|
132
|
+
extensions: true,
|
|
133
|
+
skills: true,
|
|
134
|
+
promptMode: "append",
|
|
135
|
+
};
|
|
136
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* context.ts — Extract parent conversation context for subagent inheritance.
|
|
3
|
+
*/
|
|
4
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
/** Extract text from a message content block array. */
|
|
6
|
+
export declare function extractText(content: unknown[]): string;
|
|
7
|
+
/**
|
|
8
|
+
* Build a text representation of the parent conversation context.
|
|
9
|
+
* Used when inherit_context is true to give the subagent visibility
|
|
10
|
+
* into what has been discussed/done so far.
|
|
11
|
+
*/
|
|
12
|
+
export declare function buildParentContext(ctx: ExtensionContext): string;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* context.ts — Extract parent conversation context for subagent inheritance.
|
|
3
|
+
*/
|
|
4
|
+
/** Extract text from a message content block array. */
|
|
5
|
+
export function extractText(content) {
|
|
6
|
+
return content
|
|
7
|
+
.filter((c) => c.type === "text")
|
|
8
|
+
.map((c) => c.text ?? "")
|
|
9
|
+
.join("\n");
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Build a text representation of the parent conversation context.
|
|
13
|
+
* Used when inherit_context is true to give the subagent visibility
|
|
14
|
+
* into what has been discussed/done so far.
|
|
15
|
+
*/
|
|
16
|
+
export function buildParentContext(ctx) {
|
|
17
|
+
const entries = ctx.sessionManager.getBranch();
|
|
18
|
+
if (!entries || entries.length === 0)
|
|
19
|
+
return "";
|
|
20
|
+
const parts = [];
|
|
21
|
+
for (const entry of entries) {
|
|
22
|
+
if (entry.type === "message") {
|
|
23
|
+
const msg = entry.message;
|
|
24
|
+
if (msg.role === "user") {
|
|
25
|
+
const text = typeof msg.content === "string"
|
|
26
|
+
? msg.content
|
|
27
|
+
: extractText(msg.content);
|
|
28
|
+
if (text.trim())
|
|
29
|
+
parts.push(`[User]: ${text.trim()}`);
|
|
30
|
+
}
|
|
31
|
+
else if (msg.role === "assistant") {
|
|
32
|
+
const text = extractText(msg.content);
|
|
33
|
+
if (text.trim())
|
|
34
|
+
parts.push(`[Assistant]: ${text.trim()}`);
|
|
35
|
+
}
|
|
36
|
+
// Skip toolResult messages — too verbose for context
|
|
37
|
+
}
|
|
38
|
+
else if (entry.type === "compaction") {
|
|
39
|
+
// Include compaction summaries — they're already condensed
|
|
40
|
+
if (entry.summary) {
|
|
41
|
+
parts.push(`[Summary]: ${entry.summary}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (parts.length === 0)
|
|
46
|
+
return "";
|
|
47
|
+
return `# Parent Conversation Context
|
|
48
|
+
The following is the conversation history from the parent session that spawned you.
|
|
49
|
+
Use this context to understand what has been discussed and decided so far.
|
|
50
|
+
|
|
51
|
+
${parts.join("\n\n")}
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
# Your Task (below)
|
|
55
|
+
`;
|
|
56
|
+
}
|