@timmyjl12/pi-subagents 0.0.0-stage → 0.20.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 +827 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +21 -0
- package/README.md +1079 -2
- package/SECURITY.md +97 -0
- package/dist/abortable.d.ts +12 -0
- package/dist/abortable.js +42 -0
- package/dist/active-agents-prompt.d.ts +18 -0
- package/dist/active-agents-prompt.js +34 -0
- package/dist/advisor-status.d.ts +16 -0
- package/dist/advisor-status.js +74 -0
- package/dist/agent-color.d.ts +35 -0
- package/dist/agent-color.js +123 -0
- package/dist/agent-file-toggle.d.ts +125 -0
- package/dist/agent-file-toggle.js +264 -0
- package/dist/agent-manager.d.ts +485 -0
- package/dist/agent-manager.js +1380 -0
- package/dist/agent-runner.d.ts +319 -0
- package/dist/agent-runner.js +1128 -0
- package/dist/agent-types.d.ts +119 -0
- package/dist/agent-types.js +286 -0
- package/dist/child-context.d.ts +15 -0
- package/dist/child-context.js +181 -0
- package/dist/child-session-shutdown.d.ts +3 -0
- package/dist/child-session-shutdown.js +48 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +66 -0
- package/dist/cross-extension-rpc.js +138 -0
- package/dist/custom-agents.d.ts +54 -0
- package/dist/custom-agents.js +386 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +125 -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/error-log.d.ts +58 -0
- package/dist/error-log.js +168 -0
- package/dist/group-join.d.ts +32 -0
- package/dist/group-join.js +116 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +3896 -0
- package/dist/invocation-config.d.ts +137 -0
- package/dist/invocation-config.js +129 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/mention-clone.d.ts +87 -0
- package/dist/mention-clone.js +178 -0
- package/dist/mention.d.ts +81 -0
- package/dist/mention.js +131 -0
- package/dist/model-resolver.d.ts +36 -0
- package/dist/model-resolver.js +95 -0
- package/dist/model-scope.d.ts +49 -0
- package/dist/model-scope.js +48 -0
- package/dist/nested-tools.d.ts +55 -0
- package/dist/nested-tools.js +299 -0
- package/dist/output-diagnostics.d.ts +11 -0
- package/dist/output-diagnostics.js +64 -0
- package/dist/output-file.d.ts +53 -0
- package/dist/output-file.js +248 -0
- package/dist/prompts.d.ts +61 -0
- package/dist/prompts.js +94 -0
- package/dist/recovery.d.ts +56 -0
- package/dist/recovery.js +341 -0
- package/dist/schedule-store.d.ts +44 -0
- package/dist/schedule-store.js +210 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +359 -0
- package/dist/settings.d.ts +360 -0
- package/dist/settings.js +251 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/standalone-fallback.d.ts +30 -0
- package/dist/standalone-fallback.js +239 -0
- package/dist/status-note.d.ts +61 -0
- package/dist/status-note.js +85 -0
- package/dist/structured-output.d.ts +61 -0
- package/dist/structured-output.js +112 -0
- package/dist/terminal-progress.d.ts +115 -0
- package/dist/terminal-progress.js +206 -0
- package/dist/types.d.ts +382 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-mention.d.ts +82 -0
- package/dist/ui/agent-mention.js +187 -0
- package/dist/ui/agent-widget.d.ts +235 -0
- package/dist/ui/agent-widget.js +652 -0
- package/dist/ui/conversation-viewer.d.ts +105 -0
- package/dist/ui/conversation-viewer.js +540 -0
- package/dist/ui/fleet-list.d.ts +183 -0
- package/dist/ui/fleet-list.js +494 -0
- package/dist/ui/fleet-overlay.d.ts +172 -0
- package/dist/ui/fleet-overlay.js +691 -0
- package/dist/ui/native-transcript.d.ts +33 -0
- package/dist/ui/native-transcript.js +230 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +94 -0
- package/dist/ui/select-item.d.ts +27 -0
- package/dist/ui/select-item.js +34 -0
- package/dist/ui/tool-progress.d.ts +22 -0
- package/dist/ui/tool-progress.js +56 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/ui/workflow-card.d.ts +175 -0
- package/dist/ui/workflow-card.js +332 -0
- package/dist/ui/workflow-dialog.d.ts +305 -0
- package/dist/ui/workflow-dialog.js +843 -0
- package/dist/ui/workflow-menu.d.ts +60 -0
- package/dist/ui/workflow-menu.js +147 -0
- package/dist/usage.d.ts +135 -0
- package/dist/usage.js +120 -0
- package/dist/workflow/collisions.d.ts +95 -0
- package/dist/workflow/collisions.js +88 -0
- package/dist/workflow/entry.d.ts +32 -0
- package/dist/workflow/entry.js +29 -0
- package/dist/workflow/host.d.ts +62 -0
- package/dist/workflow/host.js +363 -0
- package/dist/workflow/journal.d.ts +97 -0
- package/dist/workflow/journal.js +120 -0
- package/dist/workflow/json-schema.d.ts +51 -0
- package/dist/workflow/json-schema.js +111 -0
- package/dist/workflow/meta.d.ts +67 -0
- package/dist/workflow/meta.js +317 -0
- package/dist/workflow/progress.d.ts +224 -0
- package/dist/workflow/progress.js +361 -0
- package/dist/workflow/runtime.d.ts +334 -0
- package/dist/workflow/runtime.js +830 -0
- package/dist/workflow/saved.d.ts +90 -0
- package/dist/workflow/saved.js +203 -0
- package/dist/workflow/task.d.ts +136 -0
- package/dist/workflow/task.js +207 -0
- package/dist/workflow/tool-description.d.ts +38 -0
- package/dist/workflow/tool-description.js +199 -0
- package/dist/workflow/worker-source.d.ts +47 -0
- package/dist/workflow/worker-source.js +778 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +164 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +12 -0
- package/docs/FORK.md +40 -0
- package/docs/recovery.md +62 -0
- package/docs/rpc.md +189 -0
- package/docs/workflows.md +437 -0
- package/examples/agent-tool-description.md +43 -0
- package/examples/workflows/compose.js +51 -0
- package/examples/workflows/fan-out-audit.js +47 -0
- package/examples/workflows/gated-fix.js +60 -0
- package/examples/workflows/lib/count-child.js +27 -0
- package/examples/workflows/review-panel.js +63 -0
- package/examples/workflows/structured-findings.js +78 -0
- package/package.json +75 -6
- package/src/abortable.ts +43 -0
- package/src/active-agents-prompt.ts +39 -0
- package/src/advisor-status.ts +74 -0
- package/src/agent-color.ts +161 -0
- package/src/agent-file-toggle.ts +273 -0
- package/src/agent-manager.ts +1601 -0
- package/src/agent-runner.ts +1372 -0
- package/src/agent-types.ts +345 -0
- package/src/child-context.ts +173 -0
- package/src/child-session-shutdown.ts +46 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +198 -0
- package/src/custom-agents.ts +417 -0
- package/src/default-agents.ts +129 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/error-log.ts +193 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +4218 -0
- package/src/invocation-config.ts +230 -0
- package/src/memory.ts +179 -0
- package/src/mention-clone.ts +221 -0
- package/src/mention.ts +141 -0
- package/src/model-resolver.ts +118 -0
- package/src/model-scope.ts +70 -0
- package/src/nested-tools.ts +422 -0
- package/src/output-diagnostics.ts +68 -0
- package/src/output-file.ts +258 -0
- package/src/prompts.ts +151 -0
- package/src/recovery.ts +320 -0
- package/src/schedule-store.ts +201 -0
- package/src/schedule.ts +386 -0
- package/src/settings.ts +587 -0
- package/src/skill-loader.ts +102 -0
- package/src/standalone-fallback.ts +195 -0
- package/src/status-note.ts +90 -0
- package/src/structured-output.ts +130 -0
- package/src/terminal-progress.ts +215 -0
- package/src/types.ts +394 -0
- package/src/ui/agent-mention.ts +216 -0
- package/src/ui/agent-widget.ts +732 -0
- package/src/ui/conversation-viewer.ts +523 -0
- package/src/ui/fleet-list.ts +563 -0
- package/src/ui/fleet-overlay.ts +847 -0
- package/src/ui/native-transcript.ts +220 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/select-item.ts +45 -0
- package/src/ui/tool-progress.ts +67 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/ui/workflow-card.ts +470 -0
- package/src/ui/workflow-dialog.ts +1115 -0
- package/src/ui/workflow-menu.ts +193 -0
- package/src/usage.ts +167 -0
- package/src/workflow/collisions.ts +123 -0
- package/src/workflow/entry.ts +47 -0
- package/src/workflow/host.ts +404 -0
- package/src/workflow/journal.ts +164 -0
- package/src/workflow/json-schema.ts +128 -0
- package/src/workflow/meta.ts +325 -0
- package/src/workflow/progress.ts +550 -0
- package/src/workflow/runtime.ts +1219 -0
- package/src/workflow/saved.ts +217 -0
- package/src/workflow/task.ts +302 -0
- package/src/workflow/tool-description.ts +200 -0
- package/src/workflow/worker-source.ts +781 -0
- package/src/worktree.ts +205 -0
- package/src/xml.ts +13 -0
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,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-extension RPC handlers for the subagents extension.
|
|
3
|
+
*
|
|
4
|
+
* Exposes ping, spawn, stop, and consume 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
|
+
* @see docs/rpc.md — the caller-facing integration reference: spawn options
|
|
12
|
+
* (including the fields spawnTopLevel strips), every error string, the
|
|
13
|
+
* completion-notification race, and what protocol version 2 does not promise.
|
|
14
|
+
*/
|
|
15
|
+
import type { AgentRecord } from "./types.js";
|
|
16
|
+
/** Minimal event bus interface needed by the RPC handlers. */
|
|
17
|
+
export interface EventBus {
|
|
18
|
+
on(event: string, handler: (data: unknown) => void): () => void;
|
|
19
|
+
emit(event: string, data: unknown): void;
|
|
20
|
+
}
|
|
21
|
+
/** RPC reply envelope — matches pi-mono's RpcResponse shape. */
|
|
22
|
+
export type RpcReply<T = void> = {
|
|
23
|
+
success: true;
|
|
24
|
+
data?: T;
|
|
25
|
+
} | {
|
|
26
|
+
success: false;
|
|
27
|
+
error: string;
|
|
28
|
+
};
|
|
29
|
+
/** RPC protocol version — bumped when the envelope or method contracts change. */
|
|
30
|
+
export declare const PROTOCOL_VERSION = 2;
|
|
31
|
+
/** Minimal AgentManager interface needed by the spawn/stop/consume RPCs. */
|
|
32
|
+
export interface SpawnCapable {
|
|
33
|
+
spawn(pi: unknown, ctx: unknown, type: string, prompt: string, options: any): string;
|
|
34
|
+
/** Resolves once the spawned agent is running; rejects on a startup failure. */
|
|
35
|
+
awaitStartup(id: string): Promise<void>;
|
|
36
|
+
abort(id: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* The record behind an id, for the stop handler's ownership check. Narrowed
|
|
39
|
+
* to the two fields `isTopLevelAgent` reads, so the RPC layer keeps its
|
|
40
|
+
* deliberately shallow view of the manager.
|
|
41
|
+
*/
|
|
42
|
+
getRecord(id: string): Pick<AgentRecord, "parentAgentId" | "workflowId"> | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Mark a settled agent's result as read by the caller, suppressing the
|
|
45
|
+
* completion notification — what `get_subagent_result` does when it returns
|
|
46
|
+
* one. False when there is no such agent, or it has not settled yet.
|
|
47
|
+
*/
|
|
48
|
+
consumeResult(id: string): boolean;
|
|
49
|
+
}
|
|
50
|
+
export interface RpcDeps {
|
|
51
|
+
events: EventBus;
|
|
52
|
+
pi: unknown;
|
|
53
|
+
getCtx: () => unknown | undefined;
|
|
54
|
+
manager: SpawnCapable;
|
|
55
|
+
}
|
|
56
|
+
export interface RpcHandle {
|
|
57
|
+
unsubPing: () => void;
|
|
58
|
+
unsubSpawn: () => void;
|
|
59
|
+
unsubStop: () => void;
|
|
60
|
+
unsubConsume: () => void;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Register ping, spawn, stop, and consume RPC handlers on the event bus.
|
|
64
|
+
* Returns unsub functions for cleanup.
|
|
65
|
+
*/
|
|
66
|
+
export declare function registerRpcHandlers(deps: RpcDeps): RpcHandle;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-extension RPC handlers for the subagents extension.
|
|
3
|
+
*
|
|
4
|
+
* Exposes ping, spawn, stop, and consume 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
|
+
* @see docs/rpc.md — the caller-facing integration reference: spawn options
|
|
12
|
+
* (including the fields spawnTopLevel strips), every error string, the
|
|
13
|
+
* completion-notification race, and what protocol version 2 does not promise.
|
|
14
|
+
*/
|
|
15
|
+
import { isTopLevelAgent } from "./agent-manager.js";
|
|
16
|
+
import { resolveModel } from "./model-resolver.js";
|
|
17
|
+
import { checkModelScope } from "./model-scope.js";
|
|
18
|
+
/** RPC protocol version — bumped when the envelope or method contracts change. */
|
|
19
|
+
export const PROTOCOL_VERSION = 2;
|
|
20
|
+
/**
|
|
21
|
+
* Wire a single RPC handler: listen on `channel`, run `fn(params)`,
|
|
22
|
+
* emit the reply envelope on `channel:reply:${requestId}`.
|
|
23
|
+
*/
|
|
24
|
+
function handleRpc(events, channel, fn) {
|
|
25
|
+
return events.on(channel, async (raw) => {
|
|
26
|
+
const params = raw;
|
|
27
|
+
try {
|
|
28
|
+
const data = await fn(params);
|
|
29
|
+
const reply = { success: true };
|
|
30
|
+
if (data !== undefined)
|
|
31
|
+
reply.data = data;
|
|
32
|
+
events.emit(`${channel}:reply:${params.requestId}`, reply);
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
events.emit(`${channel}:reply:${params.requestId}`, {
|
|
36
|
+
success: false, error: err?.message ?? String(err),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Register ping, spawn, stop, and consume RPC handlers on the event bus.
|
|
43
|
+
* Returns unsub functions for cleanup.
|
|
44
|
+
*/
|
|
45
|
+
export function registerRpcHandlers(deps) {
|
|
46
|
+
const { events, pi, getCtx, manager } = deps;
|
|
47
|
+
const unsubPing = handleRpc(events, "subagents:rpc:ping", () => {
|
|
48
|
+
return { version: PROTOCOL_VERSION };
|
|
49
|
+
});
|
|
50
|
+
const unsubSpawn = handleRpc(events, "subagents:rpc:spawn", async ({ type, prompt, options }) => {
|
|
51
|
+
const ctx = getCtx();
|
|
52
|
+
if (!ctx)
|
|
53
|
+
throw new Error("No active session");
|
|
54
|
+
// Cross-extension RPC callers (e.g. pi-tasks TaskExecute) naturally
|
|
55
|
+
// forward serializable values, so options.model can be a string like
|
|
56
|
+
// "openai-codex/gpt-5.5". Resolve it to a real Model instance here
|
|
57
|
+
// — same pattern the scheduler path already uses — so the spawned
|
|
58
|
+
// agent's auth lookup doesn't crash with "No API key found for
|
|
59
|
+
// undefined".
|
|
60
|
+
let normalizedOptions = options ?? {};
|
|
61
|
+
// `!= null` on purpose: a JSON-forwarding caller can serialize an unset
|
|
62
|
+
// field as null, and the runner reads `options.model ?? default`, so null
|
|
63
|
+
// means "inherit" — not an override to resolve or scope-check.
|
|
64
|
+
const override = normalizedOptions.model;
|
|
65
|
+
if (override != null) {
|
|
66
|
+
const { modelRegistry, cwd } = ctx;
|
|
67
|
+
// Names the override the same way in both messages below; an object
|
|
68
|
+
// override would otherwise interpolate as "[object Object]".
|
|
69
|
+
const label = typeof override === "string" ? override : `${override.provider}/${override.id}`;
|
|
70
|
+
if (!modelRegistry) {
|
|
71
|
+
throw new Error(`Model override "${label}" provided but ctx.modelRegistry is unavailable`);
|
|
72
|
+
}
|
|
73
|
+
let model = override;
|
|
74
|
+
if (typeof override === "string") {
|
|
75
|
+
const resolved = resolveModel(override, modelRegistry);
|
|
76
|
+
if (typeof resolved === "string") {
|
|
77
|
+
// resolveModel returns a human-readable error string when the
|
|
78
|
+
// input doesn't match any available model. Surface it instead of
|
|
79
|
+
// silently falling back so the caller sees the auth/typo issue.
|
|
80
|
+
throw new Error(resolved);
|
|
81
|
+
}
|
|
82
|
+
model = resolved;
|
|
83
|
+
normalizedOptions = { ...normalizedOptions, model: resolved };
|
|
84
|
+
}
|
|
85
|
+
// A model on the RPC payload is an orchestrator-level choice, exactly
|
|
86
|
+
// like Agent({ model }) — so it gets the Agent tool's hard error, never
|
|
87
|
+
// the frontmatter warn (#240). The check reads the RESOLVED model:
|
|
88
|
+
// resolveModel is fuzzy, so a bare "sonnet" can land on a provider the
|
|
89
|
+
// caller never named. Frontmatter-pinned and parent-inherited models are
|
|
90
|
+
// resolved later, in agent-runner, and keep warn-and-proceed.
|
|
91
|
+
const verdict = checkModelScope({
|
|
92
|
+
model,
|
|
93
|
+
cwd: cwd ?? process.cwd(),
|
|
94
|
+
modelRegistry,
|
|
95
|
+
callerSupplied: true,
|
|
96
|
+
agentLabel: type,
|
|
97
|
+
modelInput: label,
|
|
98
|
+
});
|
|
99
|
+
if (verdict.kind === "error")
|
|
100
|
+
throw new Error(verdict.message);
|
|
101
|
+
}
|
|
102
|
+
const id = manager.spawn(pi, ctx, type, prompt, normalizedOptions);
|
|
103
|
+
// With isolation: "worktree" the agent starts asynchronously — wait for
|
|
104
|
+
// it, so a strict-isolation failure is still an error envelope rather
|
|
105
|
+
// than an id for an agent that never ran.
|
|
106
|
+
await manager.awaitStartup(id);
|
|
107
|
+
return { id };
|
|
108
|
+
});
|
|
109
|
+
const unsubStop = handleRpc(events, "subagents:rpc:stop", ({ agentId }) => {
|
|
110
|
+
const record = manager.getRecord(agentId);
|
|
111
|
+
if (!record)
|
|
112
|
+
throw new Error("Agent not found");
|
|
113
|
+
// Only the session's own agents are this RPC's to stop. A nested child or
|
|
114
|
+
// a workflow's agent is owned by something that is *waiting on it*, and
|
|
115
|
+
// aborting it out from under that owner turns another extension's stop
|
|
116
|
+
// into a failed step here. Defence in depth rather than a live hole: no
|
|
117
|
+
// RPC hands out agent ids, so a caller has no ordinary way to name one it
|
|
118
|
+
// does not own — but the guard is cheap and the id may leak some other
|
|
119
|
+
// way. Same refuse-what-we-should-not-touch stance as `consume` below.
|
|
120
|
+
if (!isTopLevelAgent(record))
|
|
121
|
+
throw new Error("Agent is owned by another agent or workflow");
|
|
122
|
+
// Not "not found" — the lookup above already proved it exists. `abort`
|
|
123
|
+
// returns false only for a record that is neither running nor queued,
|
|
124
|
+
// which is an agent that has already finished.
|
|
125
|
+
if (!manager.abort(agentId))
|
|
126
|
+
throw new Error("Agent is not running");
|
|
127
|
+
});
|
|
128
|
+
// A caller that has already shown the model an agent's result — pi-tasks'
|
|
129
|
+
// TaskOutput is the one in practice — says so here, so the completion
|
|
130
|
+
// notification for that same result is not delivered on top of it and does
|
|
131
|
+
// not cost the parent a turn. Deliberately outside the ping version
|
|
132
|
+
// handshake: an extension built against protocol v2 simply never calls it.
|
|
133
|
+
const unsubConsume = handleRpc(events, "subagents:rpc:consume", ({ agentId }) => {
|
|
134
|
+
if (!manager.consumeResult(agentId))
|
|
135
|
+
throw new Error("Agent not found or still running");
|
|
136
|
+
});
|
|
137
|
+
return { unsubPing, unsubSpawn, unsubStop, unsubConsume };
|
|
138
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
* An agent's type comes from its frontmatter `name:`, falling back to the
|
|
18
|
+
* filename — Claude Code's rule, where "the filename doesn't have to match".
|
|
19
|
+
* Because the type is now declared rather than derived from a unique path, two
|
|
20
|
+
* files can claim the same one; the later load wins, as it always has for a
|
|
21
|
+
* filename clash, and `warnSkippedOverride` reports the substitution.
|
|
22
|
+
*/
|
|
23
|
+
export declare function loadCustomAgents(cwd: string, strict?: boolean): Map<string, AgentConfig>;
|
|
24
|
+
/**
|
|
25
|
+
* Read and parse one agent file, or warn and return undefined for the caller to
|
|
26
|
+
* skip. One bad file must not take the whole extension down with it — an
|
|
27
|
+
* unparseable `.md` used to abort activation, so pi exited before the TUI.
|
|
28
|
+
*
|
|
29
|
+
* The path is as much of the fix as the recovery: a bare YAML error ("line 2,
|
|
30
|
+
* column 14") is unactionable when agents come from three directories at once,
|
|
31
|
+
* and the only other symptom is `Unknown agent type`, which reads like a typo.
|
|
32
|
+
*
|
|
33
|
+
* Under `strict` the same failure rethrows, still naming the path, so callers
|
|
34
|
+
* that opted into failing closed stop rather than run a substituted agent.
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* Parse an agent file's frontmatter, tolerating a leading UTF-8 BOM.
|
|
38
|
+
*
|
|
39
|
+
* Editors across the Windows/CJK world write UTF-8 with a BOM by default, and
|
|
40
|
+
* pi's parser did not look past one before 0.84.3: the fence never matched, so
|
|
41
|
+
* the frontmatter came back empty and the *whole file* — YAML and all — became
|
|
42
|
+
* the body. An agent authored that way silently lost every field. `tools: none`
|
|
43
|
+
* going missing is the sharp edge: the agent registers with the default
|
|
44
|
+
* toolset rather than none, which is a wider grant than its author wrote.
|
|
45
|
+
*
|
|
46
|
+
* Stripped here rather than detected per pi version, because this is the only
|
|
47
|
+
* place agent files are read and the BOM is a file-encoding artifact, not
|
|
48
|
+
* content — normalising it at the boundary keeps one behaviour across the whole
|
|
49
|
+
* supported peer range instead of forking on what happens to be installed.
|
|
50
|
+
*/
|
|
51
|
+
export declare function parseAgentFrontmatter<T extends Record<string, unknown>>(content: string): {
|
|
52
|
+
frontmatter: T;
|
|
53
|
+
body: string;
|
|
54
|
+
};
|