@tintinweb/pi-subagents 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -1
- package/README.md +17 -15
- 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 +22 -4
- package/src/agent-runner.ts +11 -45
- package/src/agent-types.ts +4 -15
- package/src/default-agents.ts +2 -36
- package/src/index.ts +30 -24
- package/src/prompts.ts +35 -20
- package/src/ui/agent-widget.ts +100 -24
- package/src/ui/conversation-viewer.ts +4 -2
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-runner.ts — Core execution engine: creates sessions, runs agents, collects results.
|
|
3
|
+
*/
|
|
4
|
+
import { createAgentSession, DefaultResourceLoader, SessionManager, SettingsManager, } from "@mariozechner/pi-coding-agent";
|
|
5
|
+
import { getToolsForType, getConfig, getAgentConfig } from "./agent-types.js";
|
|
6
|
+
import { buildAgentPrompt } from "./prompts.js";
|
|
7
|
+
import { buildParentContext, extractText } from "./context.js";
|
|
8
|
+
import { detectEnv } from "./env.js";
|
|
9
|
+
/** Names of tools registered by this extension that subagents must NOT inherit. */
|
|
10
|
+
const EXCLUDED_TOOL_NAMES = ["Agent", "get_subagent_result", "steer_subagent"];
|
|
11
|
+
/** Default max turns to prevent subagents from looping indefinitely. */
|
|
12
|
+
let defaultMaxTurns = 50;
|
|
13
|
+
/** Get the default max turns value. */
|
|
14
|
+
export function getDefaultMaxTurns() { return defaultMaxTurns; }
|
|
15
|
+
/** Set the default max turns value (minimum 1). */
|
|
16
|
+
export function setDefaultMaxTurns(n) { defaultMaxTurns = Math.max(1, n); }
|
|
17
|
+
/** Additional turns allowed after the soft limit steer message. */
|
|
18
|
+
let graceTurns = 5;
|
|
19
|
+
/** Get the grace turns value. */
|
|
20
|
+
export function getGraceTurns() { return graceTurns; }
|
|
21
|
+
/** Set the grace turns value (minimum 1). */
|
|
22
|
+
export function setGraceTurns(n) { graceTurns = Math.max(1, n); }
|
|
23
|
+
/**
|
|
24
|
+
* Try to find the right model for an agent type.
|
|
25
|
+
* Priority: explicit option > config.model > parent model.
|
|
26
|
+
*/
|
|
27
|
+
function resolveDefaultModel(parentModel, registry, configModel) {
|
|
28
|
+
if (configModel) {
|
|
29
|
+
const slashIdx = configModel.indexOf("/");
|
|
30
|
+
if (slashIdx !== -1) {
|
|
31
|
+
const provider = configModel.slice(0, slashIdx);
|
|
32
|
+
const modelId = configModel.slice(slashIdx + 1);
|
|
33
|
+
// Build a set of available model keys for fast lookup
|
|
34
|
+
const available = registry.getAvailable?.();
|
|
35
|
+
const availableKeys = available
|
|
36
|
+
? new Set(available.map((m) => `${m.provider}/${m.id}`))
|
|
37
|
+
: undefined;
|
|
38
|
+
const isAvailable = (p, id) => !availableKeys || availableKeys.has(`${p}/${id}`);
|
|
39
|
+
const found = registry.find(provider, modelId);
|
|
40
|
+
if (found && isAvailable(provider, modelId))
|
|
41
|
+
return found;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return parentModel;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Subscribe to a session and collect the last assistant message text.
|
|
48
|
+
* Returns an object with a `getText()` getter and an `unsubscribe` function.
|
|
49
|
+
*/
|
|
50
|
+
function collectResponseText(session) {
|
|
51
|
+
let text = "";
|
|
52
|
+
const unsubscribe = session.subscribe((event) => {
|
|
53
|
+
if (event.type === "message_start") {
|
|
54
|
+
text = "";
|
|
55
|
+
}
|
|
56
|
+
if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
|
57
|
+
text += event.assistantMessageEvent.delta;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return { getText: () => text, unsubscribe };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Wire an AbortSignal to abort a session.
|
|
64
|
+
* Returns a cleanup function to remove the listener.
|
|
65
|
+
*/
|
|
66
|
+
function forwardAbortSignal(session, signal) {
|
|
67
|
+
if (!signal)
|
|
68
|
+
return () => { };
|
|
69
|
+
const onAbort = () => session.abort();
|
|
70
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
71
|
+
return () => signal.removeEventListener("abort", onAbort);
|
|
72
|
+
}
|
|
73
|
+
export async function runAgent(ctx, type, prompt, options) {
|
|
74
|
+
const config = getConfig(type);
|
|
75
|
+
const agentConfig = getAgentConfig(type);
|
|
76
|
+
const env = await detectEnv(options.pi, ctx.cwd);
|
|
77
|
+
// Get parent system prompt for append-mode agents
|
|
78
|
+
const parentSystemPrompt = ctx.getSystemPrompt();
|
|
79
|
+
// Build system prompt from agent config
|
|
80
|
+
let systemPrompt;
|
|
81
|
+
if (agentConfig) {
|
|
82
|
+
systemPrompt = buildAgentPrompt(agentConfig, ctx.cwd, env, parentSystemPrompt);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
// Unknown type fallback: general-purpose (defensive — unreachable in practice
|
|
86
|
+
// since index.ts resolves unknown types to "general-purpose" before calling runAgent)
|
|
87
|
+
systemPrompt = buildAgentPrompt({
|
|
88
|
+
name: type,
|
|
89
|
+
description: "General-purpose agent",
|
|
90
|
+
systemPrompt: "",
|
|
91
|
+
promptMode: "append",
|
|
92
|
+
extensions: true,
|
|
93
|
+
skills: true,
|
|
94
|
+
inheritContext: false,
|
|
95
|
+
runInBackground: false,
|
|
96
|
+
isolated: false,
|
|
97
|
+
}, ctx.cwd, env, parentSystemPrompt);
|
|
98
|
+
}
|
|
99
|
+
const tools = getToolsForType(type, ctx.cwd);
|
|
100
|
+
// Resolve extensions/skills: isolated overrides to false
|
|
101
|
+
const extensions = options.isolated ? false : config.extensions;
|
|
102
|
+
const skills = options.isolated ? false : config.skills;
|
|
103
|
+
// Load extensions/skills: true or string[] → load; false → don't
|
|
104
|
+
const loader = new DefaultResourceLoader({
|
|
105
|
+
cwd: ctx.cwd,
|
|
106
|
+
noExtensions: extensions === false,
|
|
107
|
+
noSkills: skills === false,
|
|
108
|
+
noPromptTemplates: true,
|
|
109
|
+
noThemes: true,
|
|
110
|
+
systemPromptOverride: () => systemPrompt,
|
|
111
|
+
});
|
|
112
|
+
await loader.reload();
|
|
113
|
+
// Resolve model: explicit option > config.model > parent model
|
|
114
|
+
const model = options.model ?? resolveDefaultModel(ctx.model, ctx.modelRegistry, agentConfig?.model);
|
|
115
|
+
// Resolve thinking level: explicit option > agent config > undefined (inherit)
|
|
116
|
+
const thinkingLevel = options.thinkingLevel ?? agentConfig?.thinking;
|
|
117
|
+
const sessionOpts = {
|
|
118
|
+
cwd: ctx.cwd,
|
|
119
|
+
sessionManager: SessionManager.inMemory(ctx.cwd),
|
|
120
|
+
settingsManager: SettingsManager.create(),
|
|
121
|
+
modelRegistry: ctx.modelRegistry,
|
|
122
|
+
model,
|
|
123
|
+
tools,
|
|
124
|
+
resourceLoader: loader,
|
|
125
|
+
};
|
|
126
|
+
if (thinkingLevel) {
|
|
127
|
+
sessionOpts.thinkingLevel = thinkingLevel;
|
|
128
|
+
}
|
|
129
|
+
// createAgentSession's type signature may not include thinkingLevel yet
|
|
130
|
+
const { session } = await createAgentSession(sessionOpts);
|
|
131
|
+
// Filter active tools: remove our own tools to prevent nesting,
|
|
132
|
+
// and apply extension allowlist if specified
|
|
133
|
+
if (extensions !== false) {
|
|
134
|
+
const builtinToolNames = new Set(tools.map(t => t.name));
|
|
135
|
+
const activeTools = session.getActiveToolNames().filter((t) => {
|
|
136
|
+
if (EXCLUDED_TOOL_NAMES.includes(t))
|
|
137
|
+
return false;
|
|
138
|
+
if (builtinToolNames.has(t))
|
|
139
|
+
return true;
|
|
140
|
+
if (Array.isArray(extensions)) {
|
|
141
|
+
return extensions.some(ext => t.startsWith(ext) || t.includes(ext));
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
});
|
|
145
|
+
session.setActiveToolsByName(activeTools);
|
|
146
|
+
}
|
|
147
|
+
options.onSessionCreated?.(session);
|
|
148
|
+
// Track turns for graceful max_turns enforcement
|
|
149
|
+
let turnCount = 0;
|
|
150
|
+
const maxTurns = options.maxTurns ?? agentConfig?.maxTurns ?? defaultMaxTurns;
|
|
151
|
+
let softLimitReached = false;
|
|
152
|
+
let aborted = false;
|
|
153
|
+
let currentMessageText = "";
|
|
154
|
+
const unsubTurns = session.subscribe((event) => {
|
|
155
|
+
if (event.type === "turn_end") {
|
|
156
|
+
turnCount++;
|
|
157
|
+
if (!softLimitReached && turnCount >= maxTurns) {
|
|
158
|
+
softLimitReached = true;
|
|
159
|
+
session.steer("You have reached your turn limit. Wrap up immediately — provide your final answer now.");
|
|
160
|
+
}
|
|
161
|
+
else if (softLimitReached && turnCount >= maxTurns + graceTurns) {
|
|
162
|
+
aborted = true;
|
|
163
|
+
session.abort();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (event.type === "message_start") {
|
|
167
|
+
currentMessageText = "";
|
|
168
|
+
}
|
|
169
|
+
if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
|
170
|
+
currentMessageText += event.assistantMessageEvent.delta;
|
|
171
|
+
options.onTextDelta?.(event.assistantMessageEvent.delta, currentMessageText);
|
|
172
|
+
}
|
|
173
|
+
if (event.type === "tool_execution_start") {
|
|
174
|
+
options.onToolActivity?.({ type: "start", toolName: event.toolName });
|
|
175
|
+
}
|
|
176
|
+
if (event.type === "tool_execution_end") {
|
|
177
|
+
options.onToolActivity?.({ type: "end", toolName: event.toolName });
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
const collector = collectResponseText(session);
|
|
181
|
+
const cleanupAbort = forwardAbortSignal(session, options.signal);
|
|
182
|
+
// Build the effective prompt: optionally prepend parent context
|
|
183
|
+
let effectivePrompt = prompt;
|
|
184
|
+
if (options.inheritContext) {
|
|
185
|
+
const parentContext = buildParentContext(ctx);
|
|
186
|
+
if (parentContext) {
|
|
187
|
+
effectivePrompt = parentContext + prompt;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
await session.prompt(effectivePrompt);
|
|
192
|
+
}
|
|
193
|
+
finally {
|
|
194
|
+
unsubTurns();
|
|
195
|
+
collector.unsubscribe();
|
|
196
|
+
cleanupAbort();
|
|
197
|
+
}
|
|
198
|
+
return { responseText: collector.getText(), session, aborted, steered: softLimitReached };
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Send a new prompt to an existing session (resume).
|
|
202
|
+
*/
|
|
203
|
+
export async function resumeAgent(session, prompt, options = {}) {
|
|
204
|
+
const collector = collectResponseText(session);
|
|
205
|
+
const cleanupAbort = forwardAbortSignal(session, options.signal);
|
|
206
|
+
const unsubToolUse = options.onToolActivity
|
|
207
|
+
? session.subscribe((event) => {
|
|
208
|
+
if (event.type === "tool_execution_start")
|
|
209
|
+
options.onToolActivity({ type: "start", toolName: event.toolName });
|
|
210
|
+
if (event.type === "tool_execution_end")
|
|
211
|
+
options.onToolActivity({ type: "end", toolName: event.toolName });
|
|
212
|
+
})
|
|
213
|
+
: () => { };
|
|
214
|
+
try {
|
|
215
|
+
await session.prompt(prompt);
|
|
216
|
+
}
|
|
217
|
+
finally {
|
|
218
|
+
collector.unsubscribe();
|
|
219
|
+
unsubToolUse();
|
|
220
|
+
cleanupAbort();
|
|
221
|
+
}
|
|
222
|
+
return collector.getText();
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Send a steering message to a running subagent.
|
|
226
|
+
* The message will interrupt the agent after its current tool execution.
|
|
227
|
+
*/
|
|
228
|
+
export async function steerAgent(session, message) {
|
|
229
|
+
await session.steer(message);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Get the subagent's conversation messages as formatted text.
|
|
233
|
+
*/
|
|
234
|
+
export function getAgentConversation(session) {
|
|
235
|
+
const parts = [];
|
|
236
|
+
for (const msg of session.messages) {
|
|
237
|
+
if (msg.role === "user") {
|
|
238
|
+
const text = typeof msg.content === "string"
|
|
239
|
+
? msg.content
|
|
240
|
+
: extractText(msg.content);
|
|
241
|
+
if (text.trim())
|
|
242
|
+
parts.push(`[User]: ${text.trim()}`);
|
|
243
|
+
}
|
|
244
|
+
else if (msg.role === "assistant") {
|
|
245
|
+
const textParts = [];
|
|
246
|
+
const toolCalls = [];
|
|
247
|
+
for (const c of msg.content) {
|
|
248
|
+
if (c.type === "text" && c.text)
|
|
249
|
+
textParts.push(c.text);
|
|
250
|
+
else if (c.type === "toolCall")
|
|
251
|
+
toolCalls.push(` Tool: ${c.toolName ?? "unknown"}`);
|
|
252
|
+
}
|
|
253
|
+
if (textParts.length > 0)
|
|
254
|
+
parts.push(`[Assistant]: ${textParts.join("\n")}`);
|
|
255
|
+
if (toolCalls.length > 0)
|
|
256
|
+
parts.push(`[Tool Calls]:\n${toolCalls.join("\n")}`);
|
|
257
|
+
}
|
|
258
|
+
else if (msg.role === "toolResult") {
|
|
259
|
+
const text = extractText(msg.content);
|
|
260
|
+
const truncated = text.length > 200 ? text.slice(0, 200) + "..." : text;
|
|
261
|
+
parts.push(`[Tool Result (${msg.toolName})]: ${truncated}`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return parts.join("\n\n");
|
|
265
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
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 { AgentTool } from "@mariozechner/pi-agent-core";
|
|
8
|
+
import type { AgentConfig } from "./types.js";
|
|
9
|
+
/** All known built-in tool names, derived from the factory registry. */
|
|
10
|
+
export declare const BUILTIN_TOOL_NAMES: string[];
|
|
11
|
+
/**
|
|
12
|
+
* Register agents into the unified registry.
|
|
13
|
+
* Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
|
|
14
|
+
* Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
|
|
15
|
+
*/
|
|
16
|
+
export declare function registerAgents(userAgents: Map<string, AgentConfig>): void;
|
|
17
|
+
/** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
|
|
18
|
+
export declare function resolveType(name: string): string | undefined;
|
|
19
|
+
/** Get the agent config for a type (case-insensitive). */
|
|
20
|
+
export declare function getAgentConfig(name: string): AgentConfig | undefined;
|
|
21
|
+
/** Get all enabled type names (for spawning and tool descriptions). */
|
|
22
|
+
export declare function getAvailableTypes(): string[];
|
|
23
|
+
/** Get all type names including disabled (for UI listing). */
|
|
24
|
+
export declare function getAllTypes(): string[];
|
|
25
|
+
/** Get names of default agents currently in the registry. */
|
|
26
|
+
export declare function getDefaultAgentNames(): string[];
|
|
27
|
+
/** Get names of user-defined agents (non-defaults) currently in the registry. */
|
|
28
|
+
export declare function getUserAgentNames(): string[];
|
|
29
|
+
/** Check if a type is valid and enabled (case-insensitive). */
|
|
30
|
+
export declare function isValidType(type: string): boolean;
|
|
31
|
+
/** Get built-in tools for a type (case-insensitive). */
|
|
32
|
+
export declare function getToolsForType(type: string, cwd: string): AgentTool<any>[];
|
|
33
|
+
/** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
|
|
34
|
+
export declare function getConfig(type: string): {
|
|
35
|
+
displayName: string;
|
|
36
|
+
description: string;
|
|
37
|
+
builtinToolNames: string[];
|
|
38
|
+
extensions: true | string[] | false;
|
|
39
|
+
skills: true | string[] | false;
|
|
40
|
+
promptMode: "replace" | "append";
|
|
41
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
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 { createReadTool, createBashTool, createEditTool, createWriteTool, createGrepTool, createFindTool, createLsTool, } from "@mariozechner/pi-coding-agent";
|
|
8
|
+
import { DEFAULT_AGENTS } from "./default-agents.js";
|
|
9
|
+
const TOOL_FACTORIES = {
|
|
10
|
+
read: (cwd) => createReadTool(cwd),
|
|
11
|
+
bash: (cwd) => createBashTool(cwd),
|
|
12
|
+
edit: (cwd) => createEditTool(cwd),
|
|
13
|
+
write: (cwd) => createWriteTool(cwd),
|
|
14
|
+
grep: (cwd) => createGrepTool(cwd),
|
|
15
|
+
find: (cwd) => createFindTool(cwd),
|
|
16
|
+
ls: (cwd) => createLsTool(cwd),
|
|
17
|
+
};
|
|
18
|
+
/** All known built-in tool names, derived from the factory registry. */
|
|
19
|
+
export const BUILTIN_TOOL_NAMES = Object.keys(TOOL_FACTORIES);
|
|
20
|
+
/** Unified runtime registry of all agents (defaults + user-defined). */
|
|
21
|
+
const agents = new Map();
|
|
22
|
+
/**
|
|
23
|
+
* Register agents into the unified registry.
|
|
24
|
+
* Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
|
|
25
|
+
* Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
|
|
26
|
+
*/
|
|
27
|
+
export function registerAgents(userAgents) {
|
|
28
|
+
agents.clear();
|
|
29
|
+
// Start with defaults
|
|
30
|
+
for (const [name, config] of DEFAULT_AGENTS) {
|
|
31
|
+
agents.set(name, config);
|
|
32
|
+
}
|
|
33
|
+
// Overlay user agents (overrides defaults with same name)
|
|
34
|
+
for (const [name, config] of userAgents) {
|
|
35
|
+
agents.set(name, config);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Case-insensitive key resolution. */
|
|
39
|
+
function resolveKey(name) {
|
|
40
|
+
if (agents.has(name))
|
|
41
|
+
return name;
|
|
42
|
+
const lower = name.toLowerCase();
|
|
43
|
+
for (const key of agents.keys()) {
|
|
44
|
+
if (key.toLowerCase() === lower)
|
|
45
|
+
return key;
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
/** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
|
|
50
|
+
export function resolveType(name) {
|
|
51
|
+
return resolveKey(name);
|
|
52
|
+
}
|
|
53
|
+
/** Get the agent config for a type (case-insensitive). */
|
|
54
|
+
export function getAgentConfig(name) {
|
|
55
|
+
const key = resolveKey(name);
|
|
56
|
+
return key ? agents.get(key) : undefined;
|
|
57
|
+
}
|
|
58
|
+
/** Get all enabled type names (for spawning and tool descriptions). */
|
|
59
|
+
export function getAvailableTypes() {
|
|
60
|
+
return [...agents.entries()]
|
|
61
|
+
.filter(([_, config]) => config.enabled !== false)
|
|
62
|
+
.map(([name]) => name);
|
|
63
|
+
}
|
|
64
|
+
/** Get all type names including disabled (for UI listing). */
|
|
65
|
+
export function getAllTypes() {
|
|
66
|
+
return [...agents.keys()];
|
|
67
|
+
}
|
|
68
|
+
/** Get names of default agents currently in the registry. */
|
|
69
|
+
export function getDefaultAgentNames() {
|
|
70
|
+
return [...agents.entries()]
|
|
71
|
+
.filter(([_, config]) => config.isDefault === true)
|
|
72
|
+
.map(([name]) => name);
|
|
73
|
+
}
|
|
74
|
+
/** Get names of user-defined agents (non-defaults) currently in the registry. */
|
|
75
|
+
export function getUserAgentNames() {
|
|
76
|
+
return [...agents.entries()]
|
|
77
|
+
.filter(([_, config]) => config.isDefault !== true)
|
|
78
|
+
.map(([name]) => name);
|
|
79
|
+
}
|
|
80
|
+
/** Check if a type is valid and enabled (case-insensitive). */
|
|
81
|
+
export function isValidType(type) {
|
|
82
|
+
const key = resolveKey(type);
|
|
83
|
+
if (!key)
|
|
84
|
+
return false;
|
|
85
|
+
return agents.get(key)?.enabled !== false;
|
|
86
|
+
}
|
|
87
|
+
/** Get built-in tools for a type (case-insensitive). */
|
|
88
|
+
export function getToolsForType(type, cwd) {
|
|
89
|
+
const key = resolveKey(type);
|
|
90
|
+
const raw = key ? agents.get(key) : undefined;
|
|
91
|
+
const config = raw?.enabled !== false ? raw : undefined;
|
|
92
|
+
const toolNames = config?.builtinToolNames?.length ? config.builtinToolNames : BUILTIN_TOOL_NAMES;
|
|
93
|
+
return toolNames.filter((n) => n in TOOL_FACTORIES).map((n) => TOOL_FACTORIES[n](cwd));
|
|
94
|
+
}
|
|
95
|
+
/** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
|
|
96
|
+
export function getConfig(type) {
|
|
97
|
+
const key = resolveKey(type);
|
|
98
|
+
const config = key ? agents.get(key) : undefined;
|
|
99
|
+
if (config && config.enabled !== false) {
|
|
100
|
+
return {
|
|
101
|
+
displayName: config.displayName ?? config.name,
|
|
102
|
+
description: config.description,
|
|
103
|
+
builtinToolNames: config.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
104
|
+
extensions: config.extensions,
|
|
105
|
+
skills: config.skills,
|
|
106
|
+
promptMode: config.promptMode,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// Fallback for unknown/disabled types — general-purpose config
|
|
110
|
+
const gp = agents.get("general-purpose");
|
|
111
|
+
if (gp && gp.enabled !== false) {
|
|
112
|
+
return {
|
|
113
|
+
displayName: gp.displayName ?? gp.name,
|
|
114
|
+
description: gp.description,
|
|
115
|
+
builtinToolNames: gp.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
116
|
+
extensions: gp.extensions,
|
|
117
|
+
skills: gp.skills,
|
|
118
|
+
promptMode: gp.promptMode,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
// Absolute fallback (should never happen)
|
|
122
|
+
return {
|
|
123
|
+
displayName: "Agent",
|
|
124
|
+
description: "General-purpose agent for complex, multi-step tasks",
|
|
125
|
+
builtinToolNames: BUILTIN_TOOL_NAMES,
|
|
126
|
+
extensions: true,
|
|
127
|
+
skills: true,
|
|
128
|
+
promptMode: "append",
|
|
129
|
+
};
|
|
130
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* context.ts — Extract parent conversation context for subagent inheritance.
|
|
3
|
+
*/
|
|
4
|
+
import type { ExtensionContext } from "@mariozechner/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
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* custom-agents.ts — Load user-defined agents from project (.pi/agents/) and global (~/.pi/agent/agents/) locations.
|
|
3
|
+
*/
|
|
4
|
+
import type { AgentConfig } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Scan for custom agent .md files from multiple locations.
|
|
7
|
+
* Discovery hierarchy (higher priority wins):
|
|
8
|
+
* 1. Project: <cwd>/.pi/agents/*.md
|
|
9
|
+
* 2. Global: ~/.pi/agent/agents/*.md
|
|
10
|
+
*
|
|
11
|
+
* Project-level agents override global ones with the same name.
|
|
12
|
+
* Any name is allowed — names matching defaults (e.g. "Explore") override them.
|
|
13
|
+
*/
|
|
14
|
+
export declare function loadCustomAgents(cwd: string): Map<string, AgentConfig>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* custom-agents.ts — Load user-defined agents from project (.pi/agents/) and global (~/.pi/agent/agents/) locations.
|
|
3
|
+
*/
|
|
4
|
+
import { parseFrontmatter } from "@mariozechner/pi-coding-agent";
|
|
5
|
+
import { readFileSync, readdirSync, existsSync } from "node:fs";
|
|
6
|
+
import { join, basename } from "node:path";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { BUILTIN_TOOL_NAMES } from "./agent-types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Scan for custom agent .md files from multiple locations.
|
|
11
|
+
* Discovery hierarchy (higher priority wins):
|
|
12
|
+
* 1. Project: <cwd>/.pi/agents/*.md
|
|
13
|
+
* 2. Global: ~/.pi/agent/agents/*.md
|
|
14
|
+
*
|
|
15
|
+
* Project-level agents override global ones with the same name.
|
|
16
|
+
* Any name is allowed — names matching defaults (e.g. "Explore") override them.
|
|
17
|
+
*/
|
|
18
|
+
export function loadCustomAgents(cwd) {
|
|
19
|
+
const globalDir = join(homedir(), ".pi", "agent", "agents");
|
|
20
|
+
const projectDir = join(cwd, ".pi", "agents");
|
|
21
|
+
const agents = new Map();
|
|
22
|
+
loadFromDir(globalDir, agents, "global"); // lower priority
|
|
23
|
+
loadFromDir(projectDir, agents, "project"); // higher priority (overwrites)
|
|
24
|
+
return agents;
|
|
25
|
+
}
|
|
26
|
+
/** Load agent configs from a directory into the map. */
|
|
27
|
+
function loadFromDir(dir, agents, source) {
|
|
28
|
+
if (!existsSync(dir))
|
|
29
|
+
return;
|
|
30
|
+
let files;
|
|
31
|
+
try {
|
|
32
|
+
files = readdirSync(dir).filter(f => f.endsWith(".md"));
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
for (const file of files) {
|
|
38
|
+
const name = basename(file, ".md");
|
|
39
|
+
let content;
|
|
40
|
+
try {
|
|
41
|
+
content = readFileSync(join(dir, file), "utf-8");
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const { frontmatter: fm, body } = parseFrontmatter(content);
|
|
47
|
+
agents.set(name, {
|
|
48
|
+
name,
|
|
49
|
+
displayName: str(fm.display_name),
|
|
50
|
+
description: str(fm.description) ?? name,
|
|
51
|
+
builtinToolNames: csvList(fm.tools, BUILTIN_TOOL_NAMES),
|
|
52
|
+
extensions: inheritField(fm.extensions ?? fm.inherit_extensions),
|
|
53
|
+
skills: inheritField(fm.skills ?? fm.inherit_skills),
|
|
54
|
+
model: str(fm.model),
|
|
55
|
+
thinking: str(fm.thinking),
|
|
56
|
+
maxTurns: positiveInt(fm.max_turns),
|
|
57
|
+
systemPrompt: body.trim(),
|
|
58
|
+
promptMode: fm.prompt_mode === "append" ? "append" : "replace",
|
|
59
|
+
inheritContext: fm.inherit_context === true,
|
|
60
|
+
runInBackground: fm.run_in_background === true,
|
|
61
|
+
isolated: fm.isolated === true,
|
|
62
|
+
enabled: fm.enabled !== false, // default true; explicitly false disables
|
|
63
|
+
source,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// ---- Field parsers ----
|
|
68
|
+
// All follow the same convention: omitted → default, "none"/empty → nothing, value → exact.
|
|
69
|
+
/** Extract a string or undefined. */
|
|
70
|
+
function str(val) {
|
|
71
|
+
return typeof val === "string" ? val : undefined;
|
|
72
|
+
}
|
|
73
|
+
/** Extract a positive integer or undefined. */
|
|
74
|
+
function positiveInt(val) {
|
|
75
|
+
return typeof val === "number" && val >= 1 ? val : undefined;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Parse a comma-separated list field.
|
|
79
|
+
* omitted → defaults; "none"/empty → []; csv → listed items.
|
|
80
|
+
*/
|
|
81
|
+
function csvList(val, defaults) {
|
|
82
|
+
if (val === undefined || val === null)
|
|
83
|
+
return defaults;
|
|
84
|
+
const s = String(val).trim();
|
|
85
|
+
if (!s || s === "none")
|
|
86
|
+
return [];
|
|
87
|
+
return s.split(",").map(t => t.trim()).filter(Boolean);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Parse an inherit field (extensions, skills).
|
|
91
|
+
* omitted/true → true (inherit all); false/"none"/empty → false; csv → listed names.
|
|
92
|
+
*/
|
|
93
|
+
function inheritField(val) {
|
|
94
|
+
if (val === undefined || val === null || val === true)
|
|
95
|
+
return true;
|
|
96
|
+
if (val === false || val === "none")
|
|
97
|
+
return false;
|
|
98
|
+
const items = csvList(val, []);
|
|
99
|
+
return items.length > 0 ? items : false;
|
|
100
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* default-agents.ts — Embedded default agent configurations.
|
|
3
|
+
*
|
|
4
|
+
* These are always available but can be overridden by user .md files with the same name.
|
|
5
|
+
*/
|
|
6
|
+
import type { AgentConfig } from "./types.js";
|
|
7
|
+
export declare const DEFAULT_AGENTS: Map<string, AgentConfig>;
|