@esso0428/pi-subagents 0.15.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/CHANGELOG.md +638 -0
- package/CONTRIBUTING.md +68 -0
- package/LICENSE +21 -0
- package/README.md +745 -0
- package/SECURITY.md +95 -0
- package/dist/agent-manager.d.ts +144 -0
- package/dist/agent-manager.js +542 -0
- package/dist/agent-runner.d.ts +212 -0
- package/dist/agent-runner.js +850 -0
- package/dist/agent-types.d.ts +67 -0
- package/dist/agent-types.js +168 -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 +17 -0
- package/dist/custom-agents.js +156 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +122 -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 +16 -0
- package/dist/index.js +2209 -0
- package/dist/invocation-config.d.ts +22 -0
- package/dist/invocation-config.js +15 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/model-resolver.d.ts +19 -0
- package/dist/model-resolver.js +80 -0
- package/dist/nico-overrides.d.ts +53 -0
- package/dist/nico-overrides.js +169 -0
- package/dist/output-file.d.ts +24 -0
- package/dist/output-file.js +101 -0
- package/dist/prompts.d.ts +32 -0
- package/dist/prompts.js +73 -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 +141 -0
- package/dist/settings.js +162 -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 +197 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-widget.d.ts +160 -0
- package/dist/ui/agent-widget.js +484 -0
- package/dist/ui/conversation-viewer.d.ts +57 -0
- package/dist/ui/conversation-viewer.js +354 -0
- package/dist/ui/fleet-list.d.ts +106 -0
- package/dist/ui/fleet-list.js +345 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +95 -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/worktree.d.ts +45 -0
- package/dist/worktree.js +160 -0
- package/examples/agent-tool-description.md +42 -0
- package/package.json +56 -0
- package/src/agent-manager.ts +631 -0
- package/src/agent-runner.ts +1014 -0
- package/src/agent-types.ts +202 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +122 -0
- package/src/custom-agents.ts +167 -0
- package/src/default-agents.ts +126 -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 +2400 -0
- package/src/invocation-config.ts +40 -0
- package/src/memory.ts +179 -0
- package/src/model-resolver.ts +100 -0
- package/src/nico-overrides.ts +235 -0
- package/src/output-file.ts +110 -0
- package/src/prompts.ts +99 -0
- package/src/schedule-store.ts +153 -0
- package/src/schedule.ts +365 -0
- package/src/settings.ts +288 -0
- package/src/skill-loader.ts +102 -0
- package/src/status-note.ts +25 -0
- package/src/types.ts +208 -0
- package/src/ui/agent-widget.ts +566 -0
- package/src/ui/conversation-viewer.ts +362 -0
- package/src/ui/fleet-list.ts +380 -0
- package/src/ui/schedule-menu.ts +104 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/usage.ts +60 -0
- package/src/worktree.ts +191 -0
- package/vitest.config.ts +18 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-types.ts — Unified agent type registry.
|
|
3
|
+
*
|
|
4
|
+
* Merges embedded default agents with user-defined agents from .pi/agents/*.md, .agents/agents/*.md, and global agents.
|
|
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
|
+
/**
|
|
9
|
+
* All known built-in tool names, derived from pi's own tool factories rather
|
|
10
|
+
* than hardcoded so the set tracks pi-mono if it adds/renames a built-in.
|
|
11
|
+
* `createCodingTools` → read/bash/edit/write; `createReadOnlyTools` →
|
|
12
|
+
* read/grep/find/ls; their de-duplicated union is the 7 built-ins
|
|
13
|
+
* (read, bash, edit, write, grep, find, ls). The `cwd` only binds tool
|
|
14
|
+
* operations we never invoke here — we read each tool's `.name` and discard it.
|
|
15
|
+
*/
|
|
16
|
+
export declare const BUILTIN_TOOL_NAMES: string[];
|
|
17
|
+
/** Check whether default agents are disabled. */
|
|
18
|
+
export declare function isDefaultsDisabled(): boolean;
|
|
19
|
+
/** Set whether default agents are disabled. */
|
|
20
|
+
export declare function setDefaultsDisabled(b: boolean): void;
|
|
21
|
+
/**
|
|
22
|
+
* Register agents into the unified registry.
|
|
23
|
+
* Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
|
|
24
|
+
* Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
|
|
25
|
+
*/
|
|
26
|
+
export declare function registerAgents(userAgents: Map<string, AgentConfig>): void;
|
|
27
|
+
/**
|
|
28
|
+
* Apply npm:pi-subagents-style JSON overrides to the current agent registry.
|
|
29
|
+
* Reads from ~/.pi/agent/settings.json and .pi/settings.json and applies
|
|
30
|
+
* them as the highest-priority layer. Auto-registers agents that don't
|
|
31
|
+
* exist in the registry yet.
|
|
32
|
+
*/
|
|
33
|
+
export declare function applyNicoOverrides(): void;
|
|
34
|
+
/** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
|
|
35
|
+
export declare function resolveType(name: string): string | undefined;
|
|
36
|
+
/** Get the agent config for a type (case-insensitive). */
|
|
37
|
+
export declare function getAgentConfig(name: string): AgentConfig | undefined;
|
|
38
|
+
/** Get all enabled type names (for spawning and tool descriptions). */
|
|
39
|
+
export declare function getAvailableTypes(): string[];
|
|
40
|
+
/** Get all type names including disabled (for UI listing). */
|
|
41
|
+
export declare function getAllTypes(): string[];
|
|
42
|
+
/** Get names of default agents currently in the registry. */
|
|
43
|
+
export declare function getDefaultAgentNames(): string[];
|
|
44
|
+
/** Get names of user-defined agents (non-defaults) currently in the registry. */
|
|
45
|
+
export declare function getUserAgentNames(): string[];
|
|
46
|
+
/** Check if a type is valid and enabled (case-insensitive). */
|
|
47
|
+
export declare function isValidType(type: string): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Get memory tool names (read/write/edit) not already in the provided set.
|
|
50
|
+
*/
|
|
51
|
+
export declare function getMemoryToolNames(existingToolNames: Set<string>): string[];
|
|
52
|
+
/**
|
|
53
|
+
* Get read-only memory tool names not already in the provided set.
|
|
54
|
+
*/
|
|
55
|
+
export declare function getReadOnlyMemoryToolNames(existingToolNames: Set<string>): string[];
|
|
56
|
+
/** Get built-in tool names for a type (case-insensitive). */
|
|
57
|
+
export declare function getToolNamesForType(type: string): string[];
|
|
58
|
+
/** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
|
|
59
|
+
export declare function getConfig(type: string): {
|
|
60
|
+
displayName: string;
|
|
61
|
+
description: string;
|
|
62
|
+
builtinToolNames: string[];
|
|
63
|
+
extensions: true | string[] | false;
|
|
64
|
+
excludeExtensions?: string[];
|
|
65
|
+
skills: true | string[] | false;
|
|
66
|
+
promptMode: "replace" | "append";
|
|
67
|
+
};
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-types.ts — Unified agent type registry.
|
|
3
|
+
*
|
|
4
|
+
* Merges embedded default agents with user-defined agents from .pi/agents/*.md, .agents/agents/*.md, and global agents.
|
|
5
|
+
* User agents override defaults with the same name. Disabled agents are kept but excluded from spawning.
|
|
6
|
+
*/
|
|
7
|
+
import { createCodingTools, createReadOnlyTools } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { DEFAULT_AGENTS } from "./default-agents.js";
|
|
9
|
+
import { applyNicoOverridesToMap, readNicoAgentOverrides } from "./nico-overrides.js";
|
|
10
|
+
/**
|
|
11
|
+
* All known built-in tool names, derived from pi's own tool factories rather
|
|
12
|
+
* than hardcoded so the set tracks pi-mono if it adds/renames a built-in.
|
|
13
|
+
* `createCodingTools` → read/bash/edit/write; `createReadOnlyTools` →
|
|
14
|
+
* read/grep/find/ls; their de-duplicated union is the 7 built-ins
|
|
15
|
+
* (read, bash, edit, write, grep, find, ls). The `cwd` only binds tool
|
|
16
|
+
* operations we never invoke here — we read each tool's `.name` and discard it.
|
|
17
|
+
*/
|
|
18
|
+
export const BUILTIN_TOOL_NAMES = [
|
|
19
|
+
...new Set([...createCodingTools("."), ...createReadOnlyTools(".")].map((t) => t.name)),
|
|
20
|
+
];
|
|
21
|
+
/** Unified runtime registry of all agents (defaults + user-defined). */
|
|
22
|
+
const agents = new Map();
|
|
23
|
+
/** When true, DEFAULT_AGENTS are skipped during registration. */
|
|
24
|
+
let disableDefaults = false;
|
|
25
|
+
/** Check whether default agents are disabled. */
|
|
26
|
+
export function isDefaultsDisabled() { return disableDefaults; }
|
|
27
|
+
/** Set whether default agents are disabled. */
|
|
28
|
+
export function setDefaultsDisabled(b) { disableDefaults = b; }
|
|
29
|
+
/**
|
|
30
|
+
* Register agents into the unified registry.
|
|
31
|
+
* Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
|
|
32
|
+
* Disabled agents (enabled === false) are kept in the registry but excluded from spawning.
|
|
33
|
+
*/
|
|
34
|
+
export function registerAgents(userAgents) {
|
|
35
|
+
agents.clear();
|
|
36
|
+
// Start with defaults (unless disabled via settings)
|
|
37
|
+
if (!disableDefaults) {
|
|
38
|
+
for (const [name, config] of DEFAULT_AGENTS) {
|
|
39
|
+
agents.set(name, config);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Overlay user agents (overrides defaults with same name)
|
|
43
|
+
for (const [name, config] of userAgents) {
|
|
44
|
+
agents.set(name, config);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Apply npm:pi-subagents-style JSON overrides to the current agent registry.
|
|
49
|
+
* Reads from ~/.pi/agent/settings.json and .pi/settings.json and applies
|
|
50
|
+
* them as the highest-priority layer. Auto-registers agents that don't
|
|
51
|
+
* exist in the registry yet.
|
|
52
|
+
*/
|
|
53
|
+
export function applyNicoOverrides() {
|
|
54
|
+
const { overrides, defaultModel } = readNicoAgentOverrides(process.cwd());
|
|
55
|
+
applyNicoOverridesToMap(agents, overrides, defaultModel);
|
|
56
|
+
}
|
|
57
|
+
/** Case-insensitive key resolution. */
|
|
58
|
+
function resolveKey(name) {
|
|
59
|
+
if (agents.has(name))
|
|
60
|
+
return name;
|
|
61
|
+
const lower = name.toLowerCase();
|
|
62
|
+
for (const key of agents.keys()) {
|
|
63
|
+
if (key.toLowerCase() === lower)
|
|
64
|
+
return key;
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
/** Resolve a type name case-insensitively. Returns the canonical key or undefined. */
|
|
69
|
+
export function resolveType(name) {
|
|
70
|
+
return resolveKey(name);
|
|
71
|
+
}
|
|
72
|
+
/** Get the agent config for a type (case-insensitive). */
|
|
73
|
+
export function getAgentConfig(name) {
|
|
74
|
+
const key = resolveKey(name);
|
|
75
|
+
return key ? agents.get(key) : undefined;
|
|
76
|
+
}
|
|
77
|
+
/** Get all enabled type names (for spawning and tool descriptions). */
|
|
78
|
+
export function getAvailableTypes() {
|
|
79
|
+
return [...agents.entries()]
|
|
80
|
+
.filter(([_, config]) => config.enabled !== false)
|
|
81
|
+
.map(([name]) => name);
|
|
82
|
+
}
|
|
83
|
+
/** Get all type names including disabled (for UI listing). */
|
|
84
|
+
export function getAllTypes() {
|
|
85
|
+
return [...agents.keys()];
|
|
86
|
+
}
|
|
87
|
+
/** Get names of default agents currently in the registry. */
|
|
88
|
+
export function getDefaultAgentNames() {
|
|
89
|
+
return [...agents.entries()]
|
|
90
|
+
.filter(([_, config]) => config.isDefault === true)
|
|
91
|
+
.map(([name]) => name);
|
|
92
|
+
}
|
|
93
|
+
/** Get names of user-defined agents (non-defaults) currently in the registry. */
|
|
94
|
+
export function getUserAgentNames() {
|
|
95
|
+
return [...agents.entries()]
|
|
96
|
+
.filter(([_, config]) => config.isDefault !== true)
|
|
97
|
+
.map(([name]) => name);
|
|
98
|
+
}
|
|
99
|
+
/** Check if a type is valid and enabled (case-insensitive). */
|
|
100
|
+
export function isValidType(type) {
|
|
101
|
+
const key = resolveKey(type);
|
|
102
|
+
if (!key)
|
|
103
|
+
return false;
|
|
104
|
+
return agents.get(key)?.enabled !== false;
|
|
105
|
+
}
|
|
106
|
+
/** Tool names required for memory management. */
|
|
107
|
+
const MEMORY_TOOL_NAMES = ["read", "write", "edit"];
|
|
108
|
+
/**
|
|
109
|
+
* Get memory tool names (read/write/edit) not already in the provided set.
|
|
110
|
+
*/
|
|
111
|
+
export function getMemoryToolNames(existingToolNames) {
|
|
112
|
+
return MEMORY_TOOL_NAMES.filter(n => !existingToolNames.has(n));
|
|
113
|
+
}
|
|
114
|
+
/** Tool names needed for read-only memory access. */
|
|
115
|
+
const READONLY_MEMORY_TOOL_NAMES = ["read"];
|
|
116
|
+
/**
|
|
117
|
+
* Get read-only memory tool names not already in the provided set.
|
|
118
|
+
*/
|
|
119
|
+
export function getReadOnlyMemoryToolNames(existingToolNames) {
|
|
120
|
+
return READONLY_MEMORY_TOOL_NAMES.filter(n => !existingToolNames.has(n));
|
|
121
|
+
}
|
|
122
|
+
/** Get built-in tool names for a type (case-insensitive). */
|
|
123
|
+
export function getToolNamesForType(type) {
|
|
124
|
+
const key = resolveKey(type);
|
|
125
|
+
const raw = key ? agents.get(key) : undefined;
|
|
126
|
+
const config = raw?.enabled !== false ? raw : undefined;
|
|
127
|
+
// `undefined` (definition omitted the field) → all built-ins; an explicit `[]`
|
|
128
|
+
// (`tools: none` or a `tools:` with only `ext:` entries) → zero built-ins.
|
|
129
|
+
return config?.builtinToolNames ?? [...BUILTIN_TOOL_NAMES];
|
|
130
|
+
}
|
|
131
|
+
/** Get config for a type (case-insensitive, returns a SubagentTypeConfig-compatible object). Falls back to general-purpose. */
|
|
132
|
+
export function getConfig(type) {
|
|
133
|
+
const key = resolveKey(type);
|
|
134
|
+
const config = key ? agents.get(key) : undefined;
|
|
135
|
+
if (config && config.enabled !== false) {
|
|
136
|
+
return {
|
|
137
|
+
displayName: config.displayName ?? config.name,
|
|
138
|
+
description: config.description,
|
|
139
|
+
builtinToolNames: config.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
140
|
+
extensions: config.extensions,
|
|
141
|
+
excludeExtensions: config.excludeExtensions,
|
|
142
|
+
skills: config.skills,
|
|
143
|
+
promptMode: config.promptMode,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
// Fallback for unknown/disabled types — general-purpose config
|
|
147
|
+
const gp = agents.get("general-purpose");
|
|
148
|
+
if (gp && gp.enabled !== false) {
|
|
149
|
+
return {
|
|
150
|
+
displayName: gp.displayName ?? gp.name,
|
|
151
|
+
description: gp.description,
|
|
152
|
+
builtinToolNames: gp.builtinToolNames ?? BUILTIN_TOOL_NAMES,
|
|
153
|
+
extensions: gp.extensions,
|
|
154
|
+
excludeExtensions: gp.excludeExtensions,
|
|
155
|
+
skills: gp.skills,
|
|
156
|
+
promptMode: gp.promptMode,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
// Absolute fallback (should never happen)
|
|
160
|
+
return {
|
|
161
|
+
displayName: "Agent",
|
|
162
|
+
description: "General-purpose agent for complex, multi-step tasks",
|
|
163
|
+
builtinToolNames: BUILTIN_TOOL_NAMES,
|
|
164
|
+
extensions: true,
|
|
165
|
+
skills: true,
|
|
166
|
+
promptMode: "append",
|
|
167
|
+
};
|
|
168
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
/** Minimal event bus interface needed by the RPC handlers. */
|
|
12
|
+
export interface EventBus {
|
|
13
|
+
on(event: string, handler: (data: unknown) => void): () => void;
|
|
14
|
+
emit(event: string, data: unknown): void;
|
|
15
|
+
}
|
|
16
|
+
/** RPC reply envelope — matches pi-mono's RpcResponse shape. */
|
|
17
|
+
export type RpcReply<T = void> = {
|
|
18
|
+
success: true;
|
|
19
|
+
data?: T;
|
|
20
|
+
} | {
|
|
21
|
+
success: false;
|
|
22
|
+
error: string;
|
|
23
|
+
};
|
|
24
|
+
/** RPC protocol version — bumped when the envelope or method contracts change. */
|
|
25
|
+
export declare const PROTOCOL_VERSION = 2;
|
|
26
|
+
/** Minimal AgentManager interface needed by the spawn/stop RPCs. */
|
|
27
|
+
export interface SpawnCapable {
|
|
28
|
+
spawn(pi: unknown, ctx: unknown, type: string, prompt: string, options: any): string;
|
|
29
|
+
abort(id: string): boolean;
|
|
30
|
+
}
|
|
31
|
+
export interface RpcDeps {
|
|
32
|
+
events: EventBus;
|
|
33
|
+
pi: unknown;
|
|
34
|
+
getCtx: () => unknown | undefined;
|
|
35
|
+
manager: SpawnCapable;
|
|
36
|
+
}
|
|
37
|
+
export interface RpcHandle {
|
|
38
|
+
unsubPing: () => void;
|
|
39
|
+
unsubSpawn: () => void;
|
|
40
|
+
unsubStop: () => void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Register ping, spawn, and stop RPC handlers on the event bus.
|
|
44
|
+
* Returns unsub functions for cleanup.
|
|
45
|
+
*/
|
|
46
|
+
export declare function registerRpcHandlers(deps: RpcDeps): RpcHandle;
|
|
@@ -0,0 +1,76 @@
|
|
|
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
|
+
import { resolveModel } from "./model-resolver.js";
|
|
12
|
+
/** RPC protocol version — bumped when the envelope or method contracts change. */
|
|
13
|
+
export const PROTOCOL_VERSION = 2;
|
|
14
|
+
/**
|
|
15
|
+
* Wire a single RPC handler: listen on `channel`, run `fn(params)`,
|
|
16
|
+
* emit the reply envelope on `channel:reply:${requestId}`.
|
|
17
|
+
*/
|
|
18
|
+
function handleRpc(events, channel, fn) {
|
|
19
|
+
return events.on(channel, async (raw) => {
|
|
20
|
+
const params = raw;
|
|
21
|
+
try {
|
|
22
|
+
const data = await fn(params);
|
|
23
|
+
const reply = { success: true };
|
|
24
|
+
if (data !== undefined)
|
|
25
|
+
reply.data = data;
|
|
26
|
+
events.emit(`${channel}:reply:${params.requestId}`, reply);
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
events.emit(`${channel}:reply:${params.requestId}`, {
|
|
30
|
+
success: false, error: err?.message ?? String(err),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Register ping, spawn, and stop RPC handlers on the event bus.
|
|
37
|
+
* Returns unsub functions for cleanup.
|
|
38
|
+
*/
|
|
39
|
+
export function registerRpcHandlers(deps) {
|
|
40
|
+
const { events, pi, getCtx, manager } = deps;
|
|
41
|
+
const unsubPing = handleRpc(events, "subagents:rpc:ping", () => {
|
|
42
|
+
return { version: PROTOCOL_VERSION };
|
|
43
|
+
});
|
|
44
|
+
const unsubSpawn = handleRpc(events, "subagents:rpc:spawn", ({ type, prompt, options }) => {
|
|
45
|
+
const ctx = getCtx();
|
|
46
|
+
if (!ctx)
|
|
47
|
+
throw new Error("No active session");
|
|
48
|
+
// Cross-extension RPC callers (e.g. pi-tasks TaskExecute) naturally
|
|
49
|
+
// forward serializable values, so options.model can be a string like
|
|
50
|
+
// "openai-codex/gpt-5.5". Resolve it to a real Model instance here
|
|
51
|
+
// — same pattern the scheduler path already uses — so the spawned
|
|
52
|
+
// agent's auth lookup doesn't crash with "No API key found for
|
|
53
|
+
// undefined".
|
|
54
|
+
let normalizedOptions = options ?? {};
|
|
55
|
+
if (typeof normalizedOptions.model === "string") {
|
|
56
|
+
const registry = ctx.modelRegistry;
|
|
57
|
+
if (!registry) {
|
|
58
|
+
throw new Error(`Model override "${normalizedOptions.model}" provided but ctx.modelRegistry is unavailable`);
|
|
59
|
+
}
|
|
60
|
+
const resolved = resolveModel(normalizedOptions.model, registry);
|
|
61
|
+
if (typeof resolved === "string") {
|
|
62
|
+
// resolveModel returns a human-readable error string when the
|
|
63
|
+
// input doesn't match any available model. Surface it instead of
|
|
64
|
+
// silently falling back so the caller sees the auth/typo issue.
|
|
65
|
+
throw new Error(resolved);
|
|
66
|
+
}
|
|
67
|
+
normalizedOptions = { ...normalizedOptions, model: resolved };
|
|
68
|
+
}
|
|
69
|
+
return { id: manager.spawn(pi, ctx, type, prompt, normalizedOptions) };
|
|
70
|
+
});
|
|
71
|
+
const unsubStop = handleRpc(events, "subagents:rpc:stop", ({ agentId }) => {
|
|
72
|
+
if (!manager.abort(agentId))
|
|
73
|
+
throw new Error("Agent not found");
|
|
74
|
+
});
|
|
75
|
+
return { unsubPing, unsubSpawn, unsubStop };
|
|
76
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* custom-agents.ts — Load user-defined agents from project (.pi/agents/, plus the shared .agents/agents/ workspace) and global ($PI_CODING_AGENT_DIR/agents/, default ~/.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 (authoritative — also where /agents writes)
|
|
9
|
+
* 2. Workspace: <cwd>/.agents/agents/*.md (shared cross-tool .agents workspace, read-only)
|
|
10
|
+
* 3. Global: $PI_CODING_AGENT_DIR/agents/*.md (default: ~/.pi/agent/agents/*.md)
|
|
11
|
+
*
|
|
12
|
+
* Project-level agents override global ones with the same name. On a name clash
|
|
13
|
+
* between the two project locations, .pi/agents wins — .pi stays the project
|
|
14
|
+
* authority; .agents/agents is an additional read location.
|
|
15
|
+
* Any name is allowed — names matching defaults (e.g. "Explore") override them.
|
|
16
|
+
*/
|
|
17
|
+
export declare function loadCustomAgents(cwd: string): Map<string, AgentConfig>;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* custom-agents.ts — Load user-defined agents from project (.pi/agents/, plus the shared .agents/agents/ workspace) and global ($PI_CODING_AGENT_DIR/agents/, default ~/.pi/agent/agents/) locations.
|
|
3
|
+
*/
|
|
4
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
5
|
+
import { basename, join } from "node:path";
|
|
6
|
+
import { getAgentDir, parseFrontmatter } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { BUILTIN_TOOL_NAMES } from "./agent-types.js";
|
|
8
|
+
/**
|
|
9
|
+
* Scan for custom agent .md files from multiple locations.
|
|
10
|
+
* Discovery hierarchy (higher priority wins):
|
|
11
|
+
* 1. Project: <cwd>/.pi/agents/*.md (authoritative — also where /agents writes)
|
|
12
|
+
* 2. Workspace: <cwd>/.agents/agents/*.md (shared cross-tool .agents workspace, read-only)
|
|
13
|
+
* 3. Global: $PI_CODING_AGENT_DIR/agents/*.md (default: ~/.pi/agent/agents/*.md)
|
|
14
|
+
*
|
|
15
|
+
* Project-level agents override global ones with the same name. On a name clash
|
|
16
|
+
* between the two project locations, .pi/agents wins — .pi stays the project
|
|
17
|
+
* authority; .agents/agents is an additional read location.
|
|
18
|
+
* Any name is allowed — names matching defaults (e.g. "Explore") override them.
|
|
19
|
+
*/
|
|
20
|
+
export function loadCustomAgents(cwd) {
|
|
21
|
+
const globalDir = join(getAgentDir(), "agents");
|
|
22
|
+
const workspaceProjectDir = join(cwd, ".agents", "agents");
|
|
23
|
+
const projectDir = join(cwd, ".pi", "agents");
|
|
24
|
+
const agents = new Map();
|
|
25
|
+
loadFromDir(globalDir, agents, "global"); // lowest priority
|
|
26
|
+
loadFromDir(workspaceProjectDir, agents, "project"); // shared workspace
|
|
27
|
+
loadFromDir(projectDir, agents, "project"); // highest priority (overwrites)
|
|
28
|
+
return agents;
|
|
29
|
+
}
|
|
30
|
+
/** Load agent configs from a directory into the map. */
|
|
31
|
+
function loadFromDir(dir, agents, source) {
|
|
32
|
+
if (!existsSync(dir))
|
|
33
|
+
return;
|
|
34
|
+
let files;
|
|
35
|
+
try {
|
|
36
|
+
files = readdirSync(dir).filter(f => f.endsWith(".md"));
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
for (const file of files) {
|
|
42
|
+
const name = basename(file, ".md");
|
|
43
|
+
let content;
|
|
44
|
+
try {
|
|
45
|
+
content = readFileSync(join(dir, file), "utf-8");
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const { frontmatter: fm, body } = parseFrontmatter(content);
|
|
51
|
+
const { builtinToolNames, extSelectors } = parseToolsField(fm.tools);
|
|
52
|
+
agents.set(name, {
|
|
53
|
+
name,
|
|
54
|
+
displayName: str(fm.display_name),
|
|
55
|
+
description: str(fm.description) ?? name,
|
|
56
|
+
builtinToolNames,
|
|
57
|
+
extSelectors,
|
|
58
|
+
disallowedTools: csvListOptional(fm.disallowed_tools),
|
|
59
|
+
extensions: inheritField(fm.extensions ?? fm.inherit_extensions),
|
|
60
|
+
excludeExtensions: csvListOptional(fm.exclude_extensions),
|
|
61
|
+
skills: inheritField(fm.skills ?? fm.inherit_skills),
|
|
62
|
+
model: str(fm.model),
|
|
63
|
+
thinking: str(fm.thinking),
|
|
64
|
+
maxTurns: nonNegativeInt(fm.max_turns),
|
|
65
|
+
persistSession: fm.persist_session != null ? fm.persist_session === true : undefined,
|
|
66
|
+
outputTranscript: fm.output_transcript != null ? fm.output_transcript !== false : undefined,
|
|
67
|
+
sessionDir: str(fm.session_dir),
|
|
68
|
+
systemPrompt: body.trim(),
|
|
69
|
+
promptMode: fm.prompt_mode === "append" ? "append" : "replace",
|
|
70
|
+
inheritContext: fm.inherit_context != null ? fm.inherit_context === true : undefined,
|
|
71
|
+
runInBackground: fm.run_in_background != null ? fm.run_in_background === true : undefined,
|
|
72
|
+
isolated: fm.isolated != null ? fm.isolated === true : undefined,
|
|
73
|
+
memory: parseMemory(fm.memory),
|
|
74
|
+
isolation: fm.isolation === "worktree" ? "worktree" : undefined,
|
|
75
|
+
enabled: fm.enabled !== false, // default true; explicitly false disables
|
|
76
|
+
source,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// ---- Field parsers ----
|
|
81
|
+
// All follow the same convention: omitted → default, "none"/empty → nothing, value → exact.
|
|
82
|
+
/** Extract a string or undefined. */
|
|
83
|
+
function str(val) {
|
|
84
|
+
return typeof val === "string" ? val : undefined;
|
|
85
|
+
}
|
|
86
|
+
/** Extract a non-negative integer or undefined. 0 means unlimited for max_turns. */
|
|
87
|
+
function nonNegativeInt(val) {
|
|
88
|
+
return typeof val === "number" && val >= 0 ? val : undefined;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Parse a raw CSV field value into items, or undefined if absent/empty/"none".
|
|
92
|
+
*/
|
|
93
|
+
function parseCsvField(val) {
|
|
94
|
+
if (val === undefined || val === null)
|
|
95
|
+
return undefined;
|
|
96
|
+
const s = String(val).trim();
|
|
97
|
+
if (!s || s === "none")
|
|
98
|
+
return undefined;
|
|
99
|
+
const items = s.split(",").map(t => t.trim()).filter(Boolean);
|
|
100
|
+
return items.length > 0 ? items : undefined;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Parse a comma-separated list field with defaults.
|
|
104
|
+
* omitted → defaults; "none"/empty → []; csv → listed items.
|
|
105
|
+
*/
|
|
106
|
+
function csvList(val, defaults) {
|
|
107
|
+
if (val === undefined || val === null)
|
|
108
|
+
return defaults;
|
|
109
|
+
return parseCsvField(val) ?? [];
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Partition the `tools:` CSV into the built-in tool allowlist and raw `ext:` selectors.
|
|
113
|
+
* `*` (and the case-insensitive alias `all`, for `tools: all`) expands to all
|
|
114
|
+
* built-ins; plain entries are built-in names; `ext:` entries are extension-tool
|
|
115
|
+
* selectors parsed later by the runner. omitted → all built-ins, no selectors.
|
|
116
|
+
* `tools:` present with only `ext:` entries → zero built-ins (use `*`).
|
|
117
|
+
*/
|
|
118
|
+
function parseToolsField(val) {
|
|
119
|
+
const entries = csvList(val, BUILTIN_TOOL_NAMES);
|
|
120
|
+
const isWildcard = (e) => e === "*" || e.toLowerCase() === "all";
|
|
121
|
+
const hasWildcard = entries.some(isWildcard);
|
|
122
|
+
const plain = entries.filter(e => !isWildcard(e) && !e.startsWith("ext:"));
|
|
123
|
+
const extEntries = entries.filter(e => e.startsWith("ext:"));
|
|
124
|
+
return {
|
|
125
|
+
builtinToolNames: hasWildcard ? [...new Set([...BUILTIN_TOOL_NAMES, ...plain])] : plain,
|
|
126
|
+
extSelectors: extEntries.length > 0 ? extEntries : undefined,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Parse an optional comma-separated list field.
|
|
131
|
+
* omitted → undefined; "none"/empty → undefined; csv → listed items.
|
|
132
|
+
*/
|
|
133
|
+
function csvListOptional(val) {
|
|
134
|
+
return parseCsvField(val);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Parse a memory scope field.
|
|
138
|
+
* omitted → undefined; "user"/"project"/"local" → MemoryScope.
|
|
139
|
+
*/
|
|
140
|
+
function parseMemory(val) {
|
|
141
|
+
if (val === "user" || val === "project" || val === "local")
|
|
142
|
+
return val;
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Parse an inherit field (extensions, skills).
|
|
147
|
+
* omitted/true → true (inherit all); false/"none"/empty → false; csv → listed names.
|
|
148
|
+
*/
|
|
149
|
+
function inheritField(val) {
|
|
150
|
+
if (val === undefined || val === null || val === true)
|
|
151
|
+
return true;
|
|
152
|
+
if (val === false || val === "none")
|
|
153
|
+
return false;
|
|
154
|
+
const items = csvList(val, []);
|
|
155
|
+
return items.length > 0 ? items : false;
|
|
156
|
+
}
|
|
@@ -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>;
|