@clanker-code/pi-subagents 0.10.5
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/.plans/PLAN-next-changes.md +183 -0
- package/.plans/README.md +14 -0
- package/AGENTS.md +31 -0
- package/CHANGELOG.md +583 -0
- package/CLAUDE.md +1 -0
- package/LICENSE +21 -0
- package/README.md +630 -0
- package/RELEASE.md +39 -0
- package/dist/abort-resend.d.ts +35 -0
- package/dist/abort-resend.js +71 -0
- package/dist/agent-details.d.ts +17 -0
- package/dist/agent-details.js +22 -0
- package/dist/agent-manager.d.ts +132 -0
- package/dist/agent-manager.js +493 -0
- package/dist/agent-runner.d.ts +165 -0
- package/dist/agent-runner.js +732 -0
- package/dist/agent-tool-description.d.ts +9 -0
- package/dist/agent-tool-description.js +147 -0
- package/dist/agent-types.d.ts +60 -0
- package/dist/agent-types.js +157 -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 +76 -0
- package/dist/custom-agents.d.ts +14 -0
- package/dist/custom-agents.js +149 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +119 -0
- package/dist/enabled-models.d.ts +49 -0
- package/dist/enabled-models.js +145 -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 +36 -0
- package/dist/index.js +1918 -0
- package/dist/invocation-config.d.ts +25 -0
- package/dist/invocation-config.js +19 -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/notifications.d.ts +6 -0
- package/dist/notifications.js +107 -0
- package/dist/output-file.d.ts +24 -0
- package/dist/output-file.js +86 -0
- package/dist/peek.d.ts +37 -0
- package/dist/peek.js +121 -0
- package/dist/prompts.d.ts +40 -0
- package/dist/prompts.js +95 -0
- package/dist/schedule-store.d.ts +38 -0
- package/dist/schedule-store.js +155 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +338 -0
- package/dist/settings.d.ts +135 -0
- package/dist/settings.js +168 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/status-note.d.ts +13 -0
- package/dist/status-note.js +24 -0
- package/dist/types.d.ts +184 -0
- package/dist/types.js +7 -0
- package/dist/ui/agent-tool-rendering.d.ts +34 -0
- package/dist/ui/agent-tool-rendering.js +154 -0
- package/dist/ui/agent-widget-tree.d.ts +33 -0
- package/dist/ui/agent-widget-tree.js +130 -0
- package/dist/ui/agent-widget.d.ts +156 -0
- package/dist/ui/agent-widget.js +408 -0
- package/dist/ui/conversation-viewer.d.ts +47 -0
- package/dist/ui/conversation-viewer.js +290 -0
- package/dist/ui/menu-select.d.ts +20 -0
- package/dist/ui/menu-select.js +46 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +99 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/usage.d.ts +50 -0
- package/dist/usage.js +49 -0
- package/dist/wait.d.ts +10 -0
- package/dist/wait.js +37 -0
- package/dist/worktree.d.ts +45 -0
- package/dist/worktree.js +160 -0
- package/docs/design/default-extension-tool-exposure.md +56 -0
- package/docs/superpowers/plans/2026-06-19-recursive-subagent-widget.md +600 -0
- package/docs/superpowers/specs/2026-06-19-recursive-subagent-widget-design.md +189 -0
- package/examples/agent-tool-description.md +45 -0
- package/package.json +56 -0
- package/reviews/proposal-structured-output-schema.md +135 -0
- package/reviews/recursive-subagent-widget-preview-rev2.png +0 -0
- package/reviews/recursive-subagent-widget-preview.html +137 -0
- package/reviews/recursive-subagent-widget-preview.png +0 -0
- package/reviews/subagent-features-comparison.md +350 -0
- package/src/abort-resend.ts +75 -0
- package/src/agent-details.ts +31 -0
- package/src/agent-manager.ts +596 -0
- package/src/agent-runner.ts +872 -0
- package/src/agent-tool-description.ts +163 -0
- package/src/agent-types.ts +189 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +122 -0
- package/src/custom-agents.ts +160 -0
- package/src/default-agents.ts +123 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +2115 -0
- package/src/invocation-config.ts +42 -0
- package/src/memory.ts +165 -0
- package/src/model-resolver.ts +81 -0
- package/src/notifications.ts +120 -0
- package/src/output-file.ts +96 -0
- package/src/peek.ts +155 -0
- package/src/prompts.ts +129 -0
- package/src/schedule-store.ts +153 -0
- package/src/schedule.ts +365 -0
- package/src/settings.ts +289 -0
- package/src/skill-loader.ts +102 -0
- package/src/status-note.ts +25 -0
- package/src/types.ts +195 -0
- package/src/ui/agent-tool-rendering.ts +175 -0
- package/src/ui/agent-widget-tree.ts +169 -0
- package/src/ui/agent-widget.ts +497 -0
- package/src/ui/conversation-viewer.ts +297 -0
- package/src/ui/menu-select.ts +68 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/usage.ts +60 -0
- package/src/wait.ts +44 -0
- package/src/worktree.ts +191 -0
- package/vitest.config.ts +25 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { BUILTIN_TOOL_NAMES, getAgentConfig, getAvailableTypes } from "./agent-types.js";
|
|
5
|
+
import type { ToolDescriptionMode } from "./settings.js";
|
|
6
|
+
import type { AgentConfig } from "./types.js";
|
|
7
|
+
import { MAX_RECURSIVE_DEPTH } from "./types.js";
|
|
8
|
+
|
|
9
|
+
const formatToolsSuffix = (cfg: AgentConfig | undefined): string => {
|
|
10
|
+
const tools = cfg?.builtinToolNames;
|
|
11
|
+
if (!tools || tools.length === 0) return "*";
|
|
12
|
+
const isFullSet =
|
|
13
|
+
tools.length === BUILTIN_TOOL_NAMES.length
|
|
14
|
+
&& BUILTIN_TOOL_NAMES.every((t) => tools.includes(t));
|
|
15
|
+
return isFullSet ? "*" : tools.join(", ");
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function getModelLabelFromConfig(model: string): string {
|
|
19
|
+
const name = model.includes("/") ? model.split("/").pop()! : model;
|
|
20
|
+
return name.replace(/-\d{8}$/, "");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const buildTypeListText = () => {
|
|
24
|
+
const available = getAvailableTypes();
|
|
25
|
+
|
|
26
|
+
return available.map((name) => {
|
|
27
|
+
const cfg = getAgentConfig(name);
|
|
28
|
+
const modelSuffix = cfg?.model ? ` (${getModelLabelFromConfig(cfg.model)})` : "";
|
|
29
|
+
const toolsSuffix = ` (Tools: ${formatToolsSuffix(cfg)})`;
|
|
30
|
+
return `- ${name}: ${cfg?.description ?? name}${modelSuffix}${toolsSuffix}`;
|
|
31
|
+
}).join("\n");
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const firstSentence = (text: string): string => {
|
|
35
|
+
const match = text.match(/^.*?[.!?](?=\s|$)/s);
|
|
36
|
+
return (match ? match[0] : text).replace(/\s+/g, " ").trim();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const buildCompactTypeListText = () =>
|
|
40
|
+
getAvailableTypes().map((name) => {
|
|
41
|
+
const cfg = getAgentConfig(name);
|
|
42
|
+
return `- ${name}: ${firstSentence(cfg?.description ?? name)} (Tools: ${formatToolsSuffix(cfg)})`;
|
|
43
|
+
}).join("\n");
|
|
44
|
+
|
|
45
|
+
export interface AgentToolDescriptionOptions {
|
|
46
|
+
mode: ToolDescriptionMode;
|
|
47
|
+
extensionDepth: number;
|
|
48
|
+
schedulingEnabled: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function buildScheduleGuideline(schedulingEnabled: boolean): string {
|
|
52
|
+
return schedulingEnabled
|
|
53
|
+
? `\n- Use \`schedule\` only when the user explicitly asked for scheduled / recurring / delayed execution (e.g. "every Monday", "in an hour"). Don't auto-schedule from vague intent like "monitor X" — run once now or ask.`
|
|
54
|
+
: "";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function buildAgentToolDescription(options: AgentToolDescriptionOptions): string {
|
|
58
|
+
const scheduleGuideline = buildScheduleGuideline(options.schedulingEnabled);
|
|
59
|
+
const recursiveGuideline = `Recursive agents are allowed through depth ${MAX_RECURSIVE_DEPTH}. Current recursive depth: ${options.extensionDepth}/${MAX_RECURSIVE_DEPTH}.`;
|
|
60
|
+
|
|
61
|
+
const compactAgentToolDescription = `Launch an autonomous agent for complex, multi-step tasks. Agent types:
|
|
62
|
+
${buildCompactTypeListText()}
|
|
63
|
+
|
|
64
|
+
Custom agents: .pi/agents/<name>.md (project) or ${getAgentDir()}/agents/<name>.md (global).
|
|
65
|
+
|
|
66
|
+
Notes:
|
|
67
|
+
- description: 3-5 words (shown in UI). Prompts must be self-contained — the agent has not seen this conversation.
|
|
68
|
+
- Parallel work: one message, multiple Agent calls; they all run in the background. You are notified when agents finish — never poll or sleep.
|
|
69
|
+
- Background by default: when you have useful independent work, launch it and continue. Doing nothing while an agent runs is worse than letting background work proceed.
|
|
70
|
+
- Recursive agents: current depth ${options.extensionDepth}/${MAX_RECURSIVE_DEPTH}; you may spawn subagents until depth ${MAX_RECURSIVE_DEPTH}.
|
|
71
|
+
- The result is not shown to the user — summarize it for them. Verify an agent's claimed code changes before reporting work done.
|
|
72
|
+
- resume continues a previous agent by ID; steer_subagent messages a running one.
|
|
73
|
+
- list_models enumerates the model registry the \`model:\` param accepts — call it before picking a model explicitly.
|
|
74
|
+
- isolation: "worktree" runs the agent in an isolated git worktree; changes land on a branch.`;
|
|
75
|
+
|
|
76
|
+
const fullAgentToolDescription = `Launch a new agent to handle complex, multi-step tasks autonomously. Each agent type has specific capabilities and tools available to it.
|
|
77
|
+
|
|
78
|
+
Available agent types and the tools they have access to:
|
|
79
|
+
${buildTypeListText()}
|
|
80
|
+
|
|
81
|
+
Custom agents can be defined in .pi/agents/<name>.md (project) or ${getAgentDir()}/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.
|
|
82
|
+
|
|
83
|
+
When using the Agent tool, specify a subagent_type parameter to select which agent type to use.
|
|
84
|
+
|
|
85
|
+
## When not to use
|
|
86
|
+
|
|
87
|
+
If the target is already known, use a direct tool — \`read\` for a known path, \`grep\`/\`find\` for a specific symbol or string. Reserve this tool for open-ended questions that span the codebase, or tasks that match an available agent type.
|
|
88
|
+
|
|
89
|
+
## Usage notes
|
|
90
|
+
|
|
91
|
+
- Always include a short (3-5 word) description summarizing what the agent will do (shown in UI).
|
|
92
|
+
- When you launch multiple agents for independent work, send them in a single message with multiple tool uses so they run concurrently. If the user specifies that they want agents run "in parallel", you MUST send a single message with multiple tool calls.
|
|
93
|
+
- When the agent is done, it returns a single message back to you. The result is not visible to the user — to show the user, send a text message with a concise summary.
|
|
94
|
+
- Trust but verify: an agent's summary describes what it intended to do, not necessarily what it did. When an agent writes or edits code, check the actual changes before reporting work as done.
|
|
95
|
+
- Agents always run in the background. You will be notified when each completes — do NOT poll or sleep waiting for it. Continue with other work or respond to the user instead.
|
|
96
|
+
- Background by default: when useful independent work exists, launch it and keep going. Doing nothing while an agent runs is worse than using background capacity.
|
|
97
|
+
- ${recursiveGuideline}
|
|
98
|
+
- Use get_subagent_result if you need to retrieve a result before the completion notification arrives, but do not poll or sleep waiting for it.
|
|
99
|
+
- Use resume with an agent ID to continue a previous agent's work. A new (non-resume) Agent call starts a fresh agent with no memory of prior runs, so the prompt must be self-contained.
|
|
100
|
+
- Use list_models to enumerate the model registry the \`model:\` param accepts before passing a model name explicitly.
|
|
101
|
+
- Use steer_subagent to send mid-run messages to a running background agent.
|
|
102
|
+
- Clearly tell the agent whether you expect it to write code or just to do research (search, file reads, etc.), since it is not aware of the user's intent.
|
|
103
|
+
- If an agent's description says it should be used proactively, try to use it without the user having to ask for it first.
|
|
104
|
+
- Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
|
|
105
|
+
- Use thinking to control extended thinking level.
|
|
106
|
+
- Use inherit_context if the agent needs the parent conversation history.
|
|
107
|
+
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications). The worktree is automatically cleaned up if the agent makes no changes; otherwise the path and branch are returned in the result.${scheduleGuideline}
|
|
108
|
+
|
|
109
|
+
## Writing the prompt
|
|
110
|
+
|
|
111
|
+
Provide clear, detailed prompts so the agent can work autonomously. Brief it like a smart colleague who just walked into the room — it hasn't seen this conversation, doesn't know what you've tried, doesn't understand why this task matters.
|
|
112
|
+
- Explain what you're trying to accomplish and why.
|
|
113
|
+
- Describe what you've already learned or ruled out.
|
|
114
|
+
- Give enough context about the surrounding problem that the agent can make judgment calls rather than just following a narrow instruction.
|
|
115
|
+
- If you need a short response, say so ("report in under 200 words").
|
|
116
|
+
- Lookups: hand over the exact command. Investigations: hand over the question — prescribed steps become dead weight when the premise is wrong.
|
|
117
|
+
|
|
118
|
+
Terse command-style prompts produce shallow, generic work.
|
|
119
|
+
|
|
120
|
+
**Never delegate understanding.** Don't write "based on your findings, fix the bug" or "based on the research, implement it." Those phrases push synthesis onto the agent instead of doing it yourself. Write prompts that prove you understood: include file paths, line numbers, what specifically to change.`;
|
|
121
|
+
|
|
122
|
+
const renderToolDescriptionTemplate = (template: string): string => {
|
|
123
|
+
const vars: Record<string, () => string> = {
|
|
124
|
+
typeList: buildTypeListText,
|
|
125
|
+
compactTypeList: buildCompactTypeListText,
|
|
126
|
+
agentDir: getAgentDir,
|
|
127
|
+
scheduleGuideline: () => scheduleGuideline,
|
|
128
|
+
currentDepth: () => String(options.extensionDepth),
|
|
129
|
+
maxDepth: () => String(MAX_RECURSIVE_DEPTH),
|
|
130
|
+
recursiveGuideline: () => recursiveGuideline,
|
|
131
|
+
};
|
|
132
|
+
return template.replace(/\{\{(\w+)\}\}/g, (raw, name: string) => {
|
|
133
|
+
if (vars[name]) return vars[name]();
|
|
134
|
+
console.warn(`[pi-subagents] agent-tool-description.md: unknown placeholder ${raw} left as-is`);
|
|
135
|
+
return raw;
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const loadCustomToolDescription = (): string | undefined => {
|
|
140
|
+
for (const path of [
|
|
141
|
+
join(process.cwd(), ".pi", "agent-tool-description.md"),
|
|
142
|
+
join(getAgentDir(), "agent-tool-description.md"),
|
|
143
|
+
]) {
|
|
144
|
+
try {
|
|
145
|
+
if (!existsSync(path)) continue;
|
|
146
|
+
const text = readFileSync(path, "utf-8").trim();
|
|
147
|
+
if (text) return renderToolDescriptionTemplate(text);
|
|
148
|
+
console.warn(`[pi-subagents] ${path} is empty — ignoring`);
|
|
149
|
+
} catch (err) {
|
|
150
|
+
console.warn(`[pi-subagents] failed to read ${path}: ${err instanceof Error ? err.message : String(err)}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
if (options.mode === "compact") return compactAgentToolDescription;
|
|
157
|
+
if (options.mode === "custom") {
|
|
158
|
+
const custom = loadCustomToolDescription();
|
|
159
|
+
if (custom) return custom;
|
|
160
|
+
console.warn('[pi-subagents] toolDescriptionMode is "custom" but no agent-tool-description.md found — using "full"');
|
|
161
|
+
}
|
|
162
|
+
return fullAgentToolDescription;
|
|
163
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
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
|
+
|
|
8
|
+
import { createCodingTools, createReadOnlyTools } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { DEFAULT_AGENTS } from "./default-agents.js";
|
|
10
|
+
import type { AgentConfig } from "./types.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* All known built-in tool names, derived from pi's own tool factories rather
|
|
14
|
+
* than hardcoded so the set tracks pi-mono if it adds/renames a built-in.
|
|
15
|
+
* `createCodingTools` → read/bash/edit/write; `createReadOnlyTools` →
|
|
16
|
+
* read/grep/find/ls; their de-duplicated union is the 7 built-ins
|
|
17
|
+
* (read, bash, edit, write, grep, find, ls). The `cwd` only binds tool
|
|
18
|
+
* operations we never invoke here — we read each tool's `.name` and discard it.
|
|
19
|
+
*/
|
|
20
|
+
export const BUILTIN_TOOL_NAMES: string[] = [
|
|
21
|
+
...new Set([...createCodingTools("."), ...createReadOnlyTools(".")].map((t) => t.name)),
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
/** Unified runtime registry of all agents (defaults + user-defined). */
|
|
25
|
+
const agents = new Map<string, AgentConfig>();
|
|
26
|
+
|
|
27
|
+
/** When true, DEFAULT_AGENTS are skipped during registration. */
|
|
28
|
+
let disableDefaults = false;
|
|
29
|
+
|
|
30
|
+
/** Check whether default agents are disabled. */
|
|
31
|
+
export function isDefaultsDisabled(): boolean { return disableDefaults; }
|
|
32
|
+
|
|
33
|
+
/** Set whether default agents are disabled. */
|
|
34
|
+
export function setDefaultsDisabled(b: boolean): void { disableDefaults = b; }
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Register agents into the unified registry.
|
|
38
|
+
* Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
|
|
39
|
+
* Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
|
|
40
|
+
*/
|
|
41
|
+
export function registerAgents(userAgents: Map<string, AgentConfig>): void {
|
|
42
|
+
agents.clear();
|
|
43
|
+
|
|
44
|
+
// Start with defaults (unless disabled via settings)
|
|
45
|
+
if (!disableDefaults) {
|
|
46
|
+
for (const [name, config] of DEFAULT_AGENTS) {
|
|
47
|
+
agents.set(name, config);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Overlay user agents (overrides defaults with same name)
|
|
52
|
+
for (const [name, config] of userAgents) {
|
|
53
|
+
agents.set(name, config);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Case-insensitive key resolution. */
|
|
58
|
+
function resolveKey(name: string): string | undefined {
|
|
59
|
+
if (agents.has(name)) return name;
|
|
60
|
+
const lower = name.toLowerCase();
|
|
61
|
+
for (const key of agents.keys()) {
|
|
62
|
+
if (key.toLowerCase() === lower) return key;
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
|
|
68
|
+
export function resolveType(name: string): string | undefined {
|
|
69
|
+
return resolveKey(name);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Get the agent config for a type (case-insensitive). */
|
|
73
|
+
export function getAgentConfig(name: string): AgentConfig | undefined {
|
|
74
|
+
const key = resolveKey(name);
|
|
75
|
+
return key ? agents.get(key) : undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Get all enabled type names (for spawning and tool descriptions). */
|
|
79
|
+
export function getAvailableTypes(): string[] {
|
|
80
|
+
return [...agents.entries()]
|
|
81
|
+
.filter(([_, config]) => config.enabled !== false)
|
|
82
|
+
.map(([name]) => name);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Get all type names including disabled (for UI listing). */
|
|
86
|
+
export function getAllTypes(): string[] {
|
|
87
|
+
return [...agents.keys()];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Get names of default agents currently in the registry. */
|
|
91
|
+
export function getDefaultAgentNames(): string[] {
|
|
92
|
+
return [...agents.entries()]
|
|
93
|
+
.filter(([_, config]) => config.isDefault === true)
|
|
94
|
+
.map(([name]) => name);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Get names of user-defined agents (non-defaults) currently in the registry. */
|
|
98
|
+
export function getUserAgentNames(): string[] {
|
|
99
|
+
return [...agents.entries()]
|
|
100
|
+
.filter(([_, config]) => config.isDefault !== true)
|
|
101
|
+
.map(([name]) => name);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Check if a type is valid and enabled (case-insensitive). */
|
|
105
|
+
export function isValidType(type: string): boolean {
|
|
106
|
+
const key = resolveKey(type);
|
|
107
|
+
if (!key) return false;
|
|
108
|
+
return agents.get(key)?.enabled !== false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Tool names required for memory management. */
|
|
112
|
+
const MEMORY_TOOL_NAMES = ["read", "write", "edit"];
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Get memory tool names (read/write/edit) not already in the provided set.
|
|
116
|
+
*/
|
|
117
|
+
export function getMemoryToolNames(existingToolNames: Set<string>): string[] {
|
|
118
|
+
return MEMORY_TOOL_NAMES.filter(n => !existingToolNames.has(n));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Tool names needed for read-only memory access. */
|
|
122
|
+
const READONLY_MEMORY_TOOL_NAMES = ["read"];
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Get read-only memory tool names not already in the provided set.
|
|
126
|
+
*/
|
|
127
|
+
export function getReadOnlyMemoryToolNames(existingToolNames: Set<string>): string[] {
|
|
128
|
+
return READONLY_MEMORY_TOOL_NAMES.filter(n => !existingToolNames.has(n));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Get built-in tool names for a type (case-insensitive). */
|
|
132
|
+
export function getToolNamesForType(type: string): string[] {
|
|
133
|
+
const key = resolveKey(type);
|
|
134
|
+
const raw = key ? agents.get(key) : undefined;
|
|
135
|
+
const config = raw?.enabled !== false ? raw : undefined;
|
|
136
|
+
// `undefined` (definition omitted the field) → all built-ins; an explicit `[]`
|
|
137
|
+
// (`tools: none` or a `tools:` with only `ext:` entries) → zero built-ins.
|
|
138
|
+
return config?.builtinToolNames ?? [...BUILTIN_TOOL_NAMES];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
|
|
142
|
+
export function getConfig(type: string): {
|
|
143
|
+
displayName: string;
|
|
144
|
+
description: string;
|
|
145
|
+
builtinToolNames: string[];
|
|
146
|
+
extensions: true | string[] | false;
|
|
147
|
+
excludeExtensions?: string[];
|
|
148
|
+
skills: true | string[] | false;
|
|
149
|
+
promptMode: "replace" | "append";
|
|
150
|
+
} {
|
|
151
|
+
const key = resolveKey(type);
|
|
152
|
+
const config = key ? agents.get(key) : undefined;
|
|
153
|
+
if (config && config.enabled !== false) {
|
|
154
|
+
return {
|
|
155
|
+
displayName: config.displayName ?? config.name,
|
|
156
|
+
description: config.description,
|
|
157
|
+
builtinToolNames: config.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
158
|
+
extensions: config.extensions,
|
|
159
|
+
excludeExtensions: config.excludeExtensions,
|
|
160
|
+
skills: config.skills,
|
|
161
|
+
promptMode: config.promptMode,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Fallback for unknown/disabled types — general-purpose config
|
|
166
|
+
const gp = agents.get("general-purpose");
|
|
167
|
+
if (gp && gp.enabled !== false) {
|
|
168
|
+
return {
|
|
169
|
+
displayName: gp.displayName ?? gp.name,
|
|
170
|
+
description: gp.description,
|
|
171
|
+
builtinToolNames: gp.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
172
|
+
extensions: gp.extensions,
|
|
173
|
+
excludeExtensions: gp.excludeExtensions,
|
|
174
|
+
skills: gp.skills,
|
|
175
|
+
promptMode: gp.promptMode,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Absolute fallback (should never happen)
|
|
180
|
+
return {
|
|
181
|
+
displayName: "Agent",
|
|
182
|
+
description: "General-purpose agent for complex, multi-step tasks",
|
|
183
|
+
builtinToolNames: BUILTIN_TOOL_NAMES,
|
|
184
|
+
extensions: true,
|
|
185
|
+
skills: true,
|
|
186
|
+
promptMode: "append",
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* context.ts — Extract parent conversation context for subagent inheritance.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
|
|
7
|
+
/** Extract text from a message content block array. */
|
|
8
|
+
export function extractText(content: unknown[]): string {
|
|
9
|
+
return content
|
|
10
|
+
.filter((c: any) => c.type === "text")
|
|
11
|
+
.map((c: any) => c.text ?? "")
|
|
12
|
+
.join("\n");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Build a text representation of the parent conversation context.
|
|
17
|
+
* Used when inherit_context is true to give the subagent visibility
|
|
18
|
+
* into what has been discussed/done so far.
|
|
19
|
+
*/
|
|
20
|
+
export function buildParentContext(ctx: ExtensionContext): string {
|
|
21
|
+
const entries = ctx.sessionManager.getBranch();
|
|
22
|
+
if (!entries || entries.length === 0) return "";
|
|
23
|
+
|
|
24
|
+
const parts: string[] = [];
|
|
25
|
+
|
|
26
|
+
for (const entry of entries) {
|
|
27
|
+
if (entry.type === "message") {
|
|
28
|
+
const msg = entry.message;
|
|
29
|
+
if (msg.role === "user") {
|
|
30
|
+
const text = typeof msg.content === "string"
|
|
31
|
+
? msg.content
|
|
32
|
+
: extractText(msg.content);
|
|
33
|
+
if (text.trim()) parts.push(`[User]: ${text.trim()}`);
|
|
34
|
+
} else if (msg.role === "assistant") {
|
|
35
|
+
const text = extractText(msg.content);
|
|
36
|
+
if (text.trim()) parts.push(`[Assistant]: ${text.trim()}`);
|
|
37
|
+
}
|
|
38
|
+
// Skip toolResult messages — too verbose for context
|
|
39
|
+
} else if (entry.type === "compaction") {
|
|
40
|
+
// Include compaction summaries — they're already condensed
|
|
41
|
+
if (entry.summary) {
|
|
42
|
+
parts.push(`[Summary]: ${entry.summary}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (parts.length === 0) return "";
|
|
48
|
+
|
|
49
|
+
return `# Parent Conversation Context
|
|
50
|
+
The following is the conversation history from the parent session that spawned you.
|
|
51
|
+
Use this context to understand what has been discussed and decided so far.
|
|
52
|
+
|
|
53
|
+
${parts.join("\n\n")}
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
# Your Task (below)
|
|
57
|
+
`;
|
|
58
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-extension RPC handlers for the subagents extension.
|
|
3
|
+
*
|
|
4
|
+
* Exposes ping, spawn, and stop RPCs over the pi.events event bus,
|
|
5
|
+
* using per-request scoped reply channels.
|
|
6
|
+
*
|
|
7
|
+
* Reply envelope follows pi-mono convention:
|
|
8
|
+
* success → { success: true, data?: T }
|
|
9
|
+
* error → { success: false, error: string }
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { type ModelRegistry, resolveModel } from "./model-resolver.js";
|
|
13
|
+
|
|
14
|
+
/** Minimal event bus interface needed by the RPC handlers. */
|
|
15
|
+
export interface EventBus {
|
|
16
|
+
on(event: string, handler: (data: unknown) => void): () => void;
|
|
17
|
+
emit(event: string, data: unknown): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** RPC reply envelope — matches pi-mono's RpcResponse shape. */
|
|
21
|
+
export type RpcReply<T = void> =
|
|
22
|
+
| { success: true; data?: T }
|
|
23
|
+
| { success: false; error: string };
|
|
24
|
+
|
|
25
|
+
/** RPC protocol version — bumped when the envelope or method contracts change. */
|
|
26
|
+
export const PROTOCOL_VERSION = 2;
|
|
27
|
+
|
|
28
|
+
/** Minimal AgentManager interface needed by the spawn/stop RPCs. */
|
|
29
|
+
export interface SpawnCapable {
|
|
30
|
+
spawn(pi: unknown, ctx: unknown, type: string, prompt: string, options: any): string;
|
|
31
|
+
abort(id: string): boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface RpcDeps {
|
|
35
|
+
events: EventBus;
|
|
36
|
+
pi: unknown; // passed through to manager.spawn
|
|
37
|
+
getCtx: () => unknown | undefined; // returns current ExtensionContext
|
|
38
|
+
manager: SpawnCapable;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface RpcHandle {
|
|
42
|
+
unsubPing: () => void;
|
|
43
|
+
unsubSpawn: () => void;
|
|
44
|
+
unsubStop: () => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Wire a single RPC handler: listen on `channel`, run `fn(params)`,
|
|
49
|
+
* emit the reply envelope on `channel:reply:${requestId}`.
|
|
50
|
+
*/
|
|
51
|
+
function handleRpc<P extends { requestId: string }>(
|
|
52
|
+
events: EventBus,
|
|
53
|
+
channel: string,
|
|
54
|
+
fn: (params: P) => unknown | Promise<unknown>,
|
|
55
|
+
): () => void {
|
|
56
|
+
return events.on(channel, async (raw: unknown) => {
|
|
57
|
+
const params = raw as P;
|
|
58
|
+
try {
|
|
59
|
+
const data = await fn(params);
|
|
60
|
+
const reply: { success: true; data?: unknown } = { success: true };
|
|
61
|
+
if (data !== undefined) reply.data = data;
|
|
62
|
+
events.emit(`${channel}:reply:${params.requestId}`, reply);
|
|
63
|
+
} catch (err: any) {
|
|
64
|
+
events.emit(`${channel}:reply:${params.requestId}`, {
|
|
65
|
+
success: false, error: err?.message ?? String(err),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Register ping, spawn, and stop RPC handlers on the event bus.
|
|
73
|
+
* Returns unsub functions for cleanup.
|
|
74
|
+
*/
|
|
75
|
+
export function registerRpcHandlers(deps: RpcDeps): RpcHandle {
|
|
76
|
+
const { events, pi, getCtx, manager } = deps;
|
|
77
|
+
|
|
78
|
+
const unsubPing = handleRpc(events, "subagents:rpc:ping", () => {
|
|
79
|
+
return { version: PROTOCOL_VERSION };
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const unsubSpawn = handleRpc<{ requestId: string; type: string; prompt: string; options?: any }>(
|
|
83
|
+
events, "subagents:rpc:spawn", ({ type, prompt, options }) => {
|
|
84
|
+
const ctx = getCtx();
|
|
85
|
+
if (!ctx) throw new Error("No active session");
|
|
86
|
+
|
|
87
|
+
// Cross-extension RPC callers (e.g. pi-tasks TaskExecute) naturally
|
|
88
|
+
// forward serializable values, so options.model can be a string like
|
|
89
|
+
// "openai-codex/gpt-5.5". Resolve it to a real Model instance here
|
|
90
|
+
// — same pattern the scheduler path already uses — so the spawned
|
|
91
|
+
// agent's auth lookup doesn't crash with "No API key found for
|
|
92
|
+
// undefined".
|
|
93
|
+
let normalizedOptions = options ?? {};
|
|
94
|
+
if (typeof normalizedOptions.model === "string") {
|
|
95
|
+
const registry = (ctx as { modelRegistry?: ModelRegistry }).modelRegistry;
|
|
96
|
+
if (!registry) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`Model override "${normalizedOptions.model}" provided but ctx.modelRegistry is unavailable`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
const resolved = resolveModel(normalizedOptions.model, registry);
|
|
102
|
+
if (typeof resolved === "string") {
|
|
103
|
+
// resolveModel returns a human-readable error string when the
|
|
104
|
+
// input doesn't match any available model. Surface it instead of
|
|
105
|
+
// silently falling back so the caller sees the auth/typo issue.
|
|
106
|
+
throw new Error(resolved);
|
|
107
|
+
}
|
|
108
|
+
normalizedOptions = { ...normalizedOptions, model: resolved };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { id: manager.spawn(pi, ctx, type, prompt, normalizedOptions) };
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const unsubStop = handleRpc<{ requestId: string; agentId: string }>(
|
|
116
|
+
events, "subagents:rpc:stop", ({ agentId }) => {
|
|
117
|
+
if (!manager.abort(agentId)) throw new Error("Agent not found");
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return { unsubPing, unsubSpawn, unsubStop };
|
|
122
|
+
}
|