@mediadatafusion/pi-workflow-suite 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/CONTRIBUTING.md +9 -0
- package/LICENSE.md +201 -0
- package/NOTICE +6 -0
- package/README.md +1208 -0
- package/SECURITY.md +7 -0
- package/SUPPORT.md +9 -0
- package/TRADEMARKS.md +14 -0
- package/VERSION +1 -0
- package/agents/codebase-research.md +42 -0
- package/agents/general-worker.md +26 -0
- package/agents/implementation-planning.md +46 -0
- package/agents/quality-validation.md +43 -0
- package/agents/workflow-orchestrator.md +44 -0
- package/config/prompts/execute-approved-plan.md +43 -0
- package/config/prompts/mission-checkpoint.md +26 -0
- package/config/prompts/mission-final-validation.md +21 -0
- package/config/prompts/mission-plan.md +129 -0
- package/config/prompts/mission-repair.md +33 -0
- package/config/prompts/mission-run.md +37 -0
- package/config/prompts/validate-approved-plan.md +42 -0
- package/config/prompts/workflow-plan-prompt.md +93 -0
- package/config/prompts/workflow-repair.md +20 -0
- package/config/prompts/workflow-summary.md +23 -0
- package/config/workflow-settings.example.json +335 -0
- package/docs/assets/mediadatafusion-logo.png +0 -0
- package/docs/assets/pi-workflow-suite-card.png +0 -0
- package/docs/assets/pi-workflow-suite-header.png +0 -0
- package/docs/assets/pi-workflow-suite-video-thumb.png +0 -0
- package/docs/assets/readme-link-commands.svg +10 -0
- package/docs/assets/readme-link-install.svg +10 -0
- package/docs/assets/readme-link-quick-start.svg +10 -0
- package/docs/assets/readme-link-settings.svg +10 -0
- package/extensions/subagent/agents.ts +149 -0
- package/extensions/subagent/index.ts +1136 -0
- package/extensions/subagent/runner.ts +291 -0
- package/extensions/workflow-model-router.ts +1485 -0
- package/extensions/workflow-modes.ts +14778 -0
- package/extensions/workflow-parsers.ts +212 -0
- package/extensions/workflow-settings-capabilities.ts +282 -0
- package/extensions/workflow-state.ts +978 -0
- package/extensions/workflow-subagent-policy.ts +180 -0
- package/extensions/workflow-summary.ts +381 -0
- package/extensions/workflow-tool-guard.ts +302 -0
- package/extensions/workflow-validation-classifier.ts +102 -0
- package/extensions/workflow-web-tools.ts +356 -0
- package/package.json +1 -0
- package/scripts/audit-live.sh +69 -0
- package/scripts/audit-settings.sh +136 -0
- package/scripts/backup-live.sh +63 -0
- package/scripts/bootstrap-project.sh +220 -0
- package/scripts/install-to-live.sh +87 -0
- package/scripts/quarantine-live-junk.sh +69 -0
- package/scripts/verify-live.sh +128 -0
- package/skills/codebase-discovery/SKILL.md +20 -0
- package/skills/find-skills/SKILL.md +155 -0
- package/skills/git-safe-summary/SKILL.md +20 -0
- package/skills/implementation-planning/SKILL.md +20 -0
- package/skills/project-rules-audit/SKILL.md +20 -0
- package/skills/safe-execution/SKILL.md +20 -0
- package/skills/validation-review/SKILL.md +20 -0
|
@@ -0,0 +1,1136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subagent Tool - Delegate tasks to specialized agents.
|
|
3
|
+
*
|
|
4
|
+
* Vendored from @earendil-works/pi-coding-agent examples/extensions/subagent
|
|
5
|
+
* and adapted for Pi Workflow Suite package-local agents.
|
|
6
|
+
*
|
|
7
|
+
* Spawns a separate `pi` process for each subagent invocation,
|
|
8
|
+
* giving it an isolated context window.
|
|
9
|
+
*
|
|
10
|
+
* Supports three modes:
|
|
11
|
+
* - Single: { agent: "name", task: "..." }
|
|
12
|
+
* - Parallel: { tasks: [{ agent: "name", task: "..." }, ...] }
|
|
13
|
+
* - Chain: { chain: [{ agent: "name", task: "... {previous} ..." }, ...] }
|
|
14
|
+
*
|
|
15
|
+
* Uses JSON mode to capture structured output from subagents.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { spawn } from "node:child_process";
|
|
19
|
+
import { createRequire } from "node:module";
|
|
20
|
+
import * as fs from "node:fs";
|
|
21
|
+
import * as os from "node:os";
|
|
22
|
+
import * as path from "node:path";
|
|
23
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
24
|
+
import type { Message } from "@earendil-works/pi-ai";
|
|
25
|
+
import { StringEnum } from "@earendil-works/pi-ai";
|
|
26
|
+
import { type ExtensionAPI, getAgentDir, getMarkdownTheme, withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
27
|
+
import { Type } from "typebox";
|
|
28
|
+
import { loadWorkflowSettings } from "../workflow-model-router.js";
|
|
29
|
+
import { type AgentConfig, type AgentScope, type AgentSource, discoverAgents } from "./agents.js";
|
|
30
|
+
|
|
31
|
+
const requireFromExtension = createRequire(import.meta.url);
|
|
32
|
+
const USER_AGENTS_DIR = path.join(getAgentDir(), "agents");
|
|
33
|
+
|
|
34
|
+
class FallbackText {
|
|
35
|
+
constructor(private readonly text: string) {}
|
|
36
|
+
render(): string[] { return String(this.text ?? "").split("\n"); }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class FallbackSpacer {
|
|
40
|
+
constructor(private readonly lines = 1) {}
|
|
41
|
+
render(): string[] { return Array(Math.max(0, this.lines)).fill(""); }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class FallbackContainer {
|
|
45
|
+
private readonly children: any[] = [];
|
|
46
|
+
addChild(child: any): void { this.children.push(child); }
|
|
47
|
+
render(width: number): string[] {
|
|
48
|
+
return this.children.flatMap((child) => typeof child?.render === "function" ? child.render(width) : String(child ?? "").split("\n"));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class FallbackMarkdown extends FallbackText {}
|
|
53
|
+
|
|
54
|
+
function loadPiTuiComponents(): { Container: any; Markdown: any; Spacer: any; Text: any; truncateToWidth?: (line: string, width: number, ellipsis?: string) => string } {
|
|
55
|
+
try {
|
|
56
|
+
const tui = requireFromExtension("@earendil-works/pi-tui");
|
|
57
|
+
if (tui.Container && tui.Markdown && tui.Spacer && tui.Text) return tui;
|
|
58
|
+
} catch { /* fall back to local components */ }
|
|
59
|
+
return { Container: FallbackContainer, Markdown: FallbackMarkdown, Spacer: FallbackSpacer, Text: FallbackText };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const { Markdown, Spacer, Text, truncateToWidth: piTruncateToWidth } = loadPiTuiComponents();
|
|
63
|
+
|
|
64
|
+
function fallbackTruncateToWidth(line: string, width: number, ellipsis = "..."): string {
|
|
65
|
+
const maxWidth = Math.max(0, Math.floor(width));
|
|
66
|
+
if (maxWidth <= 0) return "";
|
|
67
|
+
const plain = String(line ?? "");
|
|
68
|
+
return plain.length > maxWidth ? `${plain.slice(0, Math.max(0, maxWidth - ellipsis.length))}${ellipsis}` : plain;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function safeTruncateToWidth(line: string, width: number, ellipsis = "..."): string {
|
|
72
|
+
const maxWidth = Number.isFinite(width) ? Math.max(0, Math.floor(width)) : 80;
|
|
73
|
+
return piTruncateToWidth ? piTruncateToWidth(String(line ?? ""), maxWidth, ellipsis) : fallbackTruncateToWidth(line, maxWidth, ellipsis);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function safeRenderedLines(lines: string[], width: number): string[] {
|
|
77
|
+
return lines.flatMap((line) => String(line ?? "").split("\n")).map((line) => safeTruncateToWidth(line, width, "..."));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function safeComponent(component: any): any {
|
|
81
|
+
return {
|
|
82
|
+
render(width: number) {
|
|
83
|
+
const lines = typeof component?.render === "function" ? component.render(width) : String(component ?? "").split("\n");
|
|
84
|
+
return safeRenderedLines(lines, width);
|
|
85
|
+
},
|
|
86
|
+
invalidate() { component?.invalidate?.(); },
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function safeText(text: string): any {
|
|
91
|
+
return safeComponent(new Text(String(text ?? ""), 0, 0));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function safeMarkdown(text: string, mdTheme: any): any {
|
|
95
|
+
return safeComponent(new Markdown(String(text ?? ""), 0, 0, mdTheme));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
class SafeContainer {
|
|
99
|
+
private readonly children: any[] = [];
|
|
100
|
+
addChild(child: any): void { this.children.push(child); }
|
|
101
|
+
render(width: number): string[] {
|
|
102
|
+
return safeRenderedLines(this.children.flatMap((child) => typeof child?.render === "function" ? child.render(width) : String(child ?? "").split("\n")), width);
|
|
103
|
+
}
|
|
104
|
+
invalidate(): void {
|
|
105
|
+
for (const child of this.children) child?.invalidate?.();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const MAX_PARALLEL_TASKS = 8;
|
|
110
|
+
const MAX_CONCURRENCY = 4;
|
|
111
|
+
const COLLAPSED_ITEM_COUNT = 10;
|
|
112
|
+
|
|
113
|
+
function formatTokens(count: number): string {
|
|
114
|
+
if (count < 1000) return count.toString();
|
|
115
|
+
if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
|
|
116
|
+
if (count < 1000000) return `${Math.round(count / 1000)}k`;
|
|
117
|
+
return `${(count / 1000000).toFixed(1)}M`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function formatUsageStats(
|
|
121
|
+
usage: {
|
|
122
|
+
input: number;
|
|
123
|
+
output: number;
|
|
124
|
+
cacheRead: number;
|
|
125
|
+
cacheWrite: number;
|
|
126
|
+
cost: number;
|
|
127
|
+
contextTokens?: number;
|
|
128
|
+
turns?: number;
|
|
129
|
+
},
|
|
130
|
+
model?: string,
|
|
131
|
+
): string {
|
|
132
|
+
const parts: string[] = [];
|
|
133
|
+
if (usage.turns) parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
|
|
134
|
+
if (usage.input) parts.push(`↑${formatTokens(usage.input)}`);
|
|
135
|
+
if (usage.output) parts.push(`↓${formatTokens(usage.output)}`);
|
|
136
|
+
if (usage.cacheRead) parts.push(`R${formatTokens(usage.cacheRead)}`);
|
|
137
|
+
if (usage.cacheWrite) parts.push(`W${formatTokens(usage.cacheWrite)}`);
|
|
138
|
+
if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
|
|
139
|
+
if (usage.contextTokens && usage.contextTokens > 0) {
|
|
140
|
+
parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
|
|
141
|
+
}
|
|
142
|
+
if (model) parts.push(model);
|
|
143
|
+
return parts.join(" ");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function formatToolCall(
|
|
147
|
+
toolName: string,
|
|
148
|
+
args: Record<string, unknown>,
|
|
149
|
+
themeFg: (color: any, text: string) => string,
|
|
150
|
+
): string {
|
|
151
|
+
const shortenPath = (p: string) => {
|
|
152
|
+
const home = os.homedir();
|
|
153
|
+
return p.startsWith(home) ? `~${p.slice(home.length)}` : p;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
switch (toolName) {
|
|
157
|
+
case "bash": {
|
|
158
|
+
const command = (args.command as string) || "...";
|
|
159
|
+
const preview = command.length > 60 ? `${command.slice(0, 60)}...` : command;
|
|
160
|
+
return themeFg("muted", "$ ") + themeFg("toolOutput", preview);
|
|
161
|
+
}
|
|
162
|
+
case "read": {
|
|
163
|
+
const rawPath = (args.file_path || args.path || "...") as string;
|
|
164
|
+
const filePath = shortenPath(rawPath);
|
|
165
|
+
const offset = args.offset as number | undefined;
|
|
166
|
+
const limit = args.limit as number | undefined;
|
|
167
|
+
let text = themeFg("accent", filePath);
|
|
168
|
+
if (offset !== undefined || limit !== undefined) {
|
|
169
|
+
const startLine = offset ?? 1;
|
|
170
|
+
const endLine = limit !== undefined ? startLine + limit - 1 : "";
|
|
171
|
+
text += themeFg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
|
|
172
|
+
}
|
|
173
|
+
return themeFg("muted", "read ") + text;
|
|
174
|
+
}
|
|
175
|
+
case "write": {
|
|
176
|
+
const rawPath = (args.file_path || args.path || "...") as string;
|
|
177
|
+
const filePath = shortenPath(rawPath);
|
|
178
|
+
const content = (args.content || "") as string;
|
|
179
|
+
const lines = content.split("\n").length;
|
|
180
|
+
let text = themeFg("muted", "write ") + themeFg("accent", filePath);
|
|
181
|
+
if (lines > 1) text += themeFg("dim", ` (${lines} lines)`);
|
|
182
|
+
return text;
|
|
183
|
+
}
|
|
184
|
+
case "edit": {
|
|
185
|
+
const rawPath = (args.file_path || args.path || "...") as string;
|
|
186
|
+
return themeFg("muted", "edit ") + themeFg("accent", shortenPath(rawPath));
|
|
187
|
+
}
|
|
188
|
+
case "ls": {
|
|
189
|
+
const rawPath = (args.path || ".") as string;
|
|
190
|
+
return themeFg("muted", "ls ") + themeFg("accent", shortenPath(rawPath));
|
|
191
|
+
}
|
|
192
|
+
case "find": {
|
|
193
|
+
const pattern = (args.pattern || "*") as string;
|
|
194
|
+
const rawPath = (args.path || ".") as string;
|
|
195
|
+
return themeFg("muted", "find ") + themeFg("accent", pattern) + themeFg("dim", ` in ${shortenPath(rawPath)}`);
|
|
196
|
+
}
|
|
197
|
+
case "grep": {
|
|
198
|
+
const pattern = (args.pattern || "") as string;
|
|
199
|
+
const rawPath = (args.path || ".") as string;
|
|
200
|
+
return (
|
|
201
|
+
themeFg("muted", "grep ") +
|
|
202
|
+
themeFg("accent", `/${pattern}/`) +
|
|
203
|
+
themeFg("dim", ` in ${shortenPath(rawPath)}`)
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
default: {
|
|
207
|
+
const argsStr = JSON.stringify(args);
|
|
208
|
+
const preview = argsStr.length > 50 ? `${argsStr.slice(0, 50)}...` : argsStr;
|
|
209
|
+
return themeFg("accent", toolName) + themeFg("dim", ` ${preview}`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
interface UsageStats {
|
|
215
|
+
input: number;
|
|
216
|
+
output: number;
|
|
217
|
+
cacheRead: number;
|
|
218
|
+
cacheWrite: number;
|
|
219
|
+
cost: number;
|
|
220
|
+
contextTokens: number;
|
|
221
|
+
turns: number;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
interface SingleResult {
|
|
225
|
+
agent: string;
|
|
226
|
+
agentSource: AgentSource | "unknown";
|
|
227
|
+
task: string;
|
|
228
|
+
exitCode: number;
|
|
229
|
+
messages: Message[];
|
|
230
|
+
stderr: string;
|
|
231
|
+
usage: UsageStats;
|
|
232
|
+
model?: string;
|
|
233
|
+
stopReason?: string;
|
|
234
|
+
errorMessage?: string;
|
|
235
|
+
step?: number;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
interface SubagentDetails {
|
|
239
|
+
mode: "single" | "parallel" | "chain";
|
|
240
|
+
agentScope: AgentScope;
|
|
241
|
+
projectAgentsDir: string | null;
|
|
242
|
+
results: SingleResult[];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function getFinalOutput(messages: Message[]): string {
|
|
246
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
247
|
+
const msg = messages[i];
|
|
248
|
+
if (msg.role === "assistant") {
|
|
249
|
+
for (const part of msg.content) {
|
|
250
|
+
if (part.type === "text") return part.text;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return "";
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
type DisplayItem = { type: "text"; text: string } | { type: "toolCall"; name: string; args: Record<string, any> };
|
|
258
|
+
|
|
259
|
+
function getDisplayItems(messages: Message[]): DisplayItem[] {
|
|
260
|
+
const items: DisplayItem[] = [];
|
|
261
|
+
for (const msg of messages) {
|
|
262
|
+
if (msg.role === "assistant") {
|
|
263
|
+
for (const part of msg.content) {
|
|
264
|
+
if (part.type === "text") items.push({ type: "text", text: part.text });
|
|
265
|
+
else if (part.type === "toolCall") items.push({ type: "toolCall", name: part.name, args: part.arguments });
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return items;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async function mapWithConcurrencyLimit<TIn, TOut>(
|
|
273
|
+
items: TIn[],
|
|
274
|
+
concurrency: number,
|
|
275
|
+
fn: (item: TIn, index: number) => Promise<TOut>,
|
|
276
|
+
): Promise<TOut[]> {
|
|
277
|
+
if (items.length === 0) return [];
|
|
278
|
+
const limit = Math.max(1, Math.min(concurrency, items.length));
|
|
279
|
+
const results: TOut[] = new Array(items.length);
|
|
280
|
+
let nextIndex = 0;
|
|
281
|
+
const workers = new Array(limit).fill(null).map(async () => {
|
|
282
|
+
while (true) {
|
|
283
|
+
const current = nextIndex++;
|
|
284
|
+
if (current >= items.length) return;
|
|
285
|
+
results[current] = await fn(items[current], current);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
await Promise.all(workers);
|
|
289
|
+
return results;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async function writePromptToTempFile(agentName: string, prompt: string): Promise<{ dir: string; filePath: string }> {
|
|
293
|
+
const tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "pi-subagent-"));
|
|
294
|
+
const safeName = agentName.replace(/[^\w.-]+/g, "_");
|
|
295
|
+
const filePath = path.join(tmpDir, `prompt-${safeName}.md`);
|
|
296
|
+
await withFileMutationQueue(filePath, async () => {
|
|
297
|
+
await fs.promises.writeFile(filePath, prompt, { encoding: "utf-8", mode: 0o600 });
|
|
298
|
+
});
|
|
299
|
+
return { dir: tmpDir, filePath };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function getPiInvocation(args: string[]): { command: string; args: string[] } {
|
|
303
|
+
const currentScript = process.argv[1];
|
|
304
|
+
const isBunVirtualScript = currentScript?.startsWith("/$bunfs/root/");
|
|
305
|
+
if (currentScript && !isBunVirtualScript && fs.existsSync(currentScript)) {
|
|
306
|
+
return { command: process.execPath, args: [currentScript, ...args] };
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const execName = path.basename(process.execPath).toLowerCase();
|
|
310
|
+
const isGenericRuntime = /^(node|bun)(\.exe)?$/.test(execName);
|
|
311
|
+
if (!isGenericRuntime) {
|
|
312
|
+
return { command: process.execPath, args };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return { command: "pi", args };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
type OnUpdateCallback = (partial: AgentToolResult<SubagentDetails>) => void;
|
|
319
|
+
|
|
320
|
+
async function runSingleAgent(
|
|
321
|
+
defaultCwd: string,
|
|
322
|
+
agents: AgentConfig[],
|
|
323
|
+
agentName: string,
|
|
324
|
+
task: string,
|
|
325
|
+
cwd: string | undefined,
|
|
326
|
+
step: number | undefined,
|
|
327
|
+
signal: AbortSignal | undefined,
|
|
328
|
+
limits: { timeoutMinutes?: number; staleMinutes?: number } | undefined,
|
|
329
|
+
onUpdate: OnUpdateCallback | undefined,
|
|
330
|
+
makeDetails: (results: SingleResult[]) => SubagentDetails,
|
|
331
|
+
): Promise<SingleResult> {
|
|
332
|
+
const agent = agents.find((a) => a.name === agentName);
|
|
333
|
+
|
|
334
|
+
if (!agent) {
|
|
335
|
+
const available = agents.map((a) => `"${a.name}"`).join(", ") || "none";
|
|
336
|
+
return {
|
|
337
|
+
agent: agentName,
|
|
338
|
+
agentSource: "unknown",
|
|
339
|
+
task,
|
|
340
|
+
exitCode: 1,
|
|
341
|
+
messages: [],
|
|
342
|
+
stderr: `Unknown agent: "${agentName}". Available agents: ${available}.`,
|
|
343
|
+
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
|
|
344
|
+
step,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const args: string[] = ["--no-extensions", "--mode", "json", "-p", "--no-session"];
|
|
349
|
+
if (agent.model) args.push("--model", agent.model);
|
|
350
|
+
if (agent.tools && agent.tools.length > 0) args.push("--tools", agent.tools.join(","));
|
|
351
|
+
|
|
352
|
+
let tmpPromptDir: string | null = null;
|
|
353
|
+
let tmpPromptPath: string | null = null;
|
|
354
|
+
|
|
355
|
+
const currentResult: SingleResult = {
|
|
356
|
+
agent: agentName,
|
|
357
|
+
agentSource: agent.source,
|
|
358
|
+
task,
|
|
359
|
+
exitCode: 0,
|
|
360
|
+
messages: [],
|
|
361
|
+
stderr: "",
|
|
362
|
+
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
|
|
363
|
+
model: agent.model,
|
|
364
|
+
step,
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const emitUpdate = () => {
|
|
368
|
+
if (onUpdate) {
|
|
369
|
+
onUpdate({
|
|
370
|
+
content: [{ type: "text", text: getFinalOutput(currentResult.messages) || "(running...)" }],
|
|
371
|
+
details: makeDetails([currentResult]),
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
try {
|
|
377
|
+
if (agent.systemPrompt.trim()) {
|
|
378
|
+
const tmp = await writePromptToTempFile(agent.name, agent.systemPrompt);
|
|
379
|
+
tmpPromptDir = tmp.dir;
|
|
380
|
+
tmpPromptPath = tmp.filePath;
|
|
381
|
+
args.push("--append-system-prompt", tmpPromptPath);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
args.push(`Task: ${task}`);
|
|
385
|
+
let wasAborted = false;
|
|
386
|
+
let timeoutReason = "";
|
|
387
|
+
const timeoutMs = Math.max(1, Math.min(240, Number(limits?.timeoutMinutes ?? 20))) * 60_000;
|
|
388
|
+
const staleMs = Math.max(1, Math.min(240, Number(limits?.staleMinutes ?? 8))) * 60_000;
|
|
389
|
+
|
|
390
|
+
const exitCode = await new Promise<number>((resolve) => {
|
|
391
|
+
const invocation = getPiInvocation(args);
|
|
392
|
+
const proc = spawn(invocation.command, invocation.args, {
|
|
393
|
+
cwd: cwd ?? defaultCwd,
|
|
394
|
+
shell: false,
|
|
395
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
396
|
+
env: {
|
|
397
|
+
...process.env,
|
|
398
|
+
PI_SUBAGENT_WORKER: "1",
|
|
399
|
+
PI_SUBAGENT_NAME: agent.name,
|
|
400
|
+
},
|
|
401
|
+
});
|
|
402
|
+
let buffer = "";
|
|
403
|
+
let lastOutputAt = Date.now();
|
|
404
|
+
let settled = false;
|
|
405
|
+
const stopProcess = (reason: string) => {
|
|
406
|
+
if (settled) return;
|
|
407
|
+
timeoutReason = reason;
|
|
408
|
+
wasAborted = true;
|
|
409
|
+
currentResult.errorMessage = reason;
|
|
410
|
+
proc.kill("SIGTERM");
|
|
411
|
+
setTimeout(() => {
|
|
412
|
+
if (!proc.killed) proc.kill("SIGKILL");
|
|
413
|
+
}, 5000);
|
|
414
|
+
};
|
|
415
|
+
const timeoutTimer = setTimeout(() => stopProcess(`Sub-agent timed out after ${Math.round(timeoutMs / 60000)} minute(s).`), timeoutMs);
|
|
416
|
+
const staleTimer = setInterval(() => {
|
|
417
|
+
if (Date.now() - lastOutputAt >= staleMs) stopProcess(`Sub-agent stale watchdog stopped worker after ${Math.round(staleMs / 60000)} minute(s) without parsed progress.`);
|
|
418
|
+
}, Math.min(staleMs, 60_000));
|
|
419
|
+
|
|
420
|
+
const processLine = (line: string) => {
|
|
421
|
+
if (!line.trim()) return;
|
|
422
|
+
let event: any;
|
|
423
|
+
try {
|
|
424
|
+
event = JSON.parse(line);
|
|
425
|
+
} catch {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (event.type === "message_end" && event.message) {
|
|
430
|
+
lastOutputAt = Date.now();
|
|
431
|
+
const msg = event.message as Message;
|
|
432
|
+
currentResult.messages.push(msg);
|
|
433
|
+
|
|
434
|
+
if (msg.role === "assistant") {
|
|
435
|
+
currentResult.usage.turns++;
|
|
436
|
+
const usage = msg.usage;
|
|
437
|
+
if (usage) {
|
|
438
|
+
currentResult.usage.input += usage.input || 0;
|
|
439
|
+
currentResult.usage.output += usage.output || 0;
|
|
440
|
+
currentResult.usage.cacheRead += usage.cacheRead || 0;
|
|
441
|
+
currentResult.usage.cacheWrite += usage.cacheWrite || 0;
|
|
442
|
+
currentResult.usage.cost += usage.cost?.total || 0;
|
|
443
|
+
currentResult.usage.contextTokens = usage.totalTokens || 0;
|
|
444
|
+
}
|
|
445
|
+
if (!currentResult.model && msg.model) currentResult.model = msg.model;
|
|
446
|
+
if (msg.stopReason) currentResult.stopReason = msg.stopReason;
|
|
447
|
+
if (msg.errorMessage) currentResult.errorMessage = msg.errorMessage;
|
|
448
|
+
}
|
|
449
|
+
emitUpdate();
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (event.type === "tool_result_end" && event.message) {
|
|
453
|
+
lastOutputAt = Date.now();
|
|
454
|
+
currentResult.messages.push(event.message as Message);
|
|
455
|
+
emitUpdate();
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
proc.stdout.on("data", (data) => {
|
|
460
|
+
buffer += data.toString();
|
|
461
|
+
const lines = buffer.split("\n");
|
|
462
|
+
buffer = lines.pop() || "";
|
|
463
|
+
for (const line of lines) processLine(line);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
proc.stderr.on("data", (data) => {
|
|
467
|
+
currentResult.stderr += data.toString();
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
proc.on("close", (code) => {
|
|
471
|
+
settled = true;
|
|
472
|
+
clearTimeout(timeoutTimer);
|
|
473
|
+
clearInterval(staleTimer);
|
|
474
|
+
if (buffer.trim()) processLine(buffer);
|
|
475
|
+
resolve(code ?? 0);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
proc.on("error", () => {
|
|
479
|
+
settled = true;
|
|
480
|
+
clearTimeout(timeoutTimer);
|
|
481
|
+
clearInterval(staleTimer);
|
|
482
|
+
resolve(1);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
if (signal) {
|
|
486
|
+
const killProc = () => {
|
|
487
|
+
stopProcess("Subagent was aborted");
|
|
488
|
+
};
|
|
489
|
+
if (signal.aborted) killProc();
|
|
490
|
+
else signal.addEventListener("abort", killProc, { once: true });
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
currentResult.exitCode = wasAborted ? 1 : exitCode;
|
|
495
|
+
if (wasAborted) {
|
|
496
|
+
currentResult.stopReason = "aborted";
|
|
497
|
+
currentResult.errorMessage = timeoutReason || "Subagent was aborted";
|
|
498
|
+
}
|
|
499
|
+
return currentResult;
|
|
500
|
+
} finally {
|
|
501
|
+
if (tmpPromptPath)
|
|
502
|
+
try {
|
|
503
|
+
fs.unlinkSync(tmpPromptPath);
|
|
504
|
+
} catch {
|
|
505
|
+
/* ignore */
|
|
506
|
+
}
|
|
507
|
+
if (tmpPromptDir)
|
|
508
|
+
try {
|
|
509
|
+
fs.rmdirSync(tmpPromptDir);
|
|
510
|
+
} catch {
|
|
511
|
+
/* ignore */
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const TaskItem = Type.Object({
|
|
517
|
+
agent: Type.String({ description: "Name of the agent to invoke" }),
|
|
518
|
+
task: Type.String({ description: "Task to delegate to the agent" }),
|
|
519
|
+
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
const ChainItem = Type.Object({
|
|
523
|
+
agent: Type.String({ description: "Name of the agent to invoke" }),
|
|
524
|
+
task: Type.String({ description: "Task with optional {previous} placeholder for prior output" }),
|
|
525
|
+
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
const AgentScopeSchema = StringEnum(["user", "project", "both"] as const, {
|
|
529
|
+
description: 'Which agent directories to use. Default: "user". Use "both" to include project-local agents.',
|
|
530
|
+
default: "user",
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
const WorkflowPhaseSchema = StringEnum(["planning", "execution", "repair", "review", "validation"] as const, {
|
|
534
|
+
description: "Workflow phase for Workflow Suite policy tracking.",
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
const SubagentParams = Type.Object({
|
|
538
|
+
agent: Type.Optional(Type.String({ description: "Name of the agent to invoke (for single mode)" })),
|
|
539
|
+
task: Type.Optional(Type.String({ description: "Task to delegate (for single mode)" })),
|
|
540
|
+
tasks: Type.Optional(Type.Array(TaskItem, { description: "Array of {agent, task} for parallel execution" })),
|
|
541
|
+
chain: Type.Optional(Type.Array(ChainItem, { description: "Array of {agent, task} for sequential execution" })),
|
|
542
|
+
workflowPhase: Type.Optional(WorkflowPhaseSchema),
|
|
543
|
+
agentScope: Type.Optional(AgentScopeSchema),
|
|
544
|
+
confirmProjectAgents: Type.Optional(
|
|
545
|
+
Type.Boolean({ description: "Prompt before running project-local agents. Default: true.", default: true }),
|
|
546
|
+
),
|
|
547
|
+
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process (single mode)" })),
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
export default function (pi: ExtensionAPI) {
|
|
551
|
+
pi.registerTool({
|
|
552
|
+
name: "subagent",
|
|
553
|
+
label: "Subagent",
|
|
554
|
+
description: [
|
|
555
|
+
"Delegate tasks to specialized subagents with isolated context.",
|
|
556
|
+
"Modes: single (agent + task), parallel (tasks array), chain (sequential with {previous} placeholder).",
|
|
557
|
+
"Workflow Suite modes can set workflowPhase to planning, execution, repair, review, or validation for policy tracking.",
|
|
558
|
+
`Default agent scope is "user" (bundled package agents plus ${USER_AGENTS_DIR}).`,
|
|
559
|
+
'To enable project-local agents in .pi/agents, set agentScope: "both" (or "project").',
|
|
560
|
+
].join(" "),
|
|
561
|
+
parameters: SubagentParams,
|
|
562
|
+
|
|
563
|
+
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
|
564
|
+
const settings = loadWorkflowSettings(ctx.cwd);
|
|
565
|
+
const subagentLimits = { timeoutMinutes: settings.subagents.subagentTimeoutMinutes, staleMinutes: settings.subagents.subagentStaleMinutes };
|
|
566
|
+
if (settings.subagents.requireApprovalBeforeRun === true) {
|
|
567
|
+
const requested = params.tasks?.length
|
|
568
|
+
? params.tasks.map((task) => task.agent).join(", ")
|
|
569
|
+
: params.chain?.length
|
|
570
|
+
? params.chain.map((step) => step.agent).join(" -> ")
|
|
571
|
+
: params.agent ?? "unknown";
|
|
572
|
+
if (!ctx.hasUI) {
|
|
573
|
+
return {
|
|
574
|
+
content: [{ type: "text", text: `Sub-agent run blocked: subagents.requireApprovalBeforeRun=true but no UI is available to approve requested agents: ${requested}.` }],
|
|
575
|
+
details: { mode: "single", agentScope: params.agentScope ?? "user", projectAgentsDir: null, results: [] },
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
const approved = await ctx.ui.confirm(
|
|
579
|
+
"Run sub-agent worker(s)?",
|
|
580
|
+
`Requested agents: ${requested}\n\nThis gate is active because subagents.requireApprovalBeforeRun=true.`,
|
|
581
|
+
);
|
|
582
|
+
if (!approved) {
|
|
583
|
+
return {
|
|
584
|
+
content: [{ type: "text", text: `Sub-agent run cancelled by approval gate. Requested agents: ${requested}.` }],
|
|
585
|
+
details: { mode: "single", agentScope: params.agentScope ?? "user", projectAgentsDir: null, results: [] },
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
const agentScope: AgentScope = params.agentScope ?? "user";
|
|
591
|
+
const discovery = discoverAgents(ctx.cwd, agentScope);
|
|
592
|
+
const agents = discovery.agents;
|
|
593
|
+
const confirmProjectAgents = params.confirmProjectAgents ?? true;
|
|
594
|
+
|
|
595
|
+
const hasChain = (params.chain?.length ?? 0) > 0;
|
|
596
|
+
const hasTasks = (params.tasks?.length ?? 0) > 0;
|
|
597
|
+
const hasSingle = Boolean(params.agent && params.task);
|
|
598
|
+
const modeCount = Number(hasChain) + Number(hasTasks) + Number(hasSingle);
|
|
599
|
+
|
|
600
|
+
const makeDetails =
|
|
601
|
+
(mode: "single" | "parallel" | "chain") =>
|
|
602
|
+
(results: SingleResult[]): SubagentDetails => ({
|
|
603
|
+
mode,
|
|
604
|
+
agentScope,
|
|
605
|
+
projectAgentsDir: discovery.projectAgentsDir,
|
|
606
|
+
results,
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
if (modeCount !== 1) {
|
|
610
|
+
const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
|
|
611
|
+
return {
|
|
612
|
+
content: [
|
|
613
|
+
{
|
|
614
|
+
type: "text",
|
|
615
|
+
text: `Invalid parameters. Provide exactly one mode.\nAvailable agents: ${available}`,
|
|
616
|
+
},
|
|
617
|
+
],
|
|
618
|
+
details: makeDetails("single")([]),
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
if ((agentScope === "project" || agentScope === "both") && confirmProjectAgents && ctx.hasUI) {
|
|
623
|
+
const requestedAgentNames = new Set<string>();
|
|
624
|
+
if (params.chain) for (const step of params.chain) requestedAgentNames.add(step.agent);
|
|
625
|
+
if (params.tasks) for (const t of params.tasks) requestedAgentNames.add(t.agent);
|
|
626
|
+
if (params.agent) requestedAgentNames.add(params.agent);
|
|
627
|
+
|
|
628
|
+
const projectAgentsRequested = Array.from(requestedAgentNames)
|
|
629
|
+
.map((name) => agents.find((a) => a.name === name))
|
|
630
|
+
.filter((a): a is AgentConfig => a?.source === "project");
|
|
631
|
+
|
|
632
|
+
if (projectAgentsRequested.length > 0) {
|
|
633
|
+
const names = projectAgentsRequested.map((a) => a.name).join(", ");
|
|
634
|
+
const dir = discovery.projectAgentsDir ?? "(unknown)";
|
|
635
|
+
const ok = await ctx.ui.confirm(
|
|
636
|
+
"Run project-local agents?",
|
|
637
|
+
`Agents: ${names}\nSource: ${dir}\n\nProject agents are repo-controlled. Only continue for trusted repositories.`,
|
|
638
|
+
);
|
|
639
|
+
if (!ok)
|
|
640
|
+
return {
|
|
641
|
+
content: [{ type: "text", text: "Canceled: project-local agents not approved." }],
|
|
642
|
+
details: makeDetails(hasChain ? "chain" : hasTasks ? "parallel" : "single")([]),
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
if (params.chain && params.chain.length > 0) {
|
|
648
|
+
const results: SingleResult[] = [];
|
|
649
|
+
let previousOutput = "";
|
|
650
|
+
|
|
651
|
+
for (let i = 0; i < params.chain.length; i++) {
|
|
652
|
+
const step = params.chain[i];
|
|
653
|
+
const taskWithContext = step.task.replace(/\{previous\}/g, previousOutput);
|
|
654
|
+
|
|
655
|
+
// Create update callback that includes all previous results
|
|
656
|
+
const chainUpdate: OnUpdateCallback | undefined = onUpdate
|
|
657
|
+
? (partial) => {
|
|
658
|
+
// Combine completed results with current streaming result
|
|
659
|
+
const currentResult = partial.details?.results[0];
|
|
660
|
+
if (currentResult) {
|
|
661
|
+
const allResults = [...results, currentResult];
|
|
662
|
+
onUpdate({
|
|
663
|
+
content: partial.content,
|
|
664
|
+
details: makeDetails("chain")(allResults),
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
: undefined;
|
|
669
|
+
|
|
670
|
+
const result = await runSingleAgent(
|
|
671
|
+
ctx.cwd,
|
|
672
|
+
agents,
|
|
673
|
+
step.agent,
|
|
674
|
+
taskWithContext,
|
|
675
|
+
step.cwd,
|
|
676
|
+
i + 1,
|
|
677
|
+
signal,
|
|
678
|
+
subagentLimits,
|
|
679
|
+
chainUpdate,
|
|
680
|
+
makeDetails("chain"),
|
|
681
|
+
);
|
|
682
|
+
results.push(result);
|
|
683
|
+
|
|
684
|
+
const isError =
|
|
685
|
+
result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
|
|
686
|
+
if (isError) {
|
|
687
|
+
const errorMsg =
|
|
688
|
+
result.errorMessage || result.stderr || getFinalOutput(result.messages) || "(no output)";
|
|
689
|
+
return {
|
|
690
|
+
content: [{ type: "text", text: `Chain stopped at step ${i + 1} (${step.agent}): ${errorMsg}` }],
|
|
691
|
+
details: makeDetails("chain")(results),
|
|
692
|
+
isError: true,
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
previousOutput = getFinalOutput(result.messages);
|
|
696
|
+
}
|
|
697
|
+
return {
|
|
698
|
+
content: [{ type: "text", text: getFinalOutput(results[results.length - 1].messages) || "(no output)" }],
|
|
699
|
+
details: makeDetails("chain")(results),
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
if (params.tasks && params.tasks.length > 0) {
|
|
704
|
+
if (params.tasks.length > MAX_PARALLEL_TASKS)
|
|
705
|
+
return {
|
|
706
|
+
content: [
|
|
707
|
+
{
|
|
708
|
+
type: "text",
|
|
709
|
+
text: `Too many parallel tasks (${params.tasks.length}). Max is ${MAX_PARALLEL_TASKS}.`,
|
|
710
|
+
},
|
|
711
|
+
],
|
|
712
|
+
details: makeDetails("parallel")([]),
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
// Track all results for streaming updates
|
|
716
|
+
const allResults: SingleResult[] = new Array(params.tasks.length);
|
|
717
|
+
|
|
718
|
+
// Initialize placeholder results
|
|
719
|
+
for (let i = 0; i < params.tasks.length; i++) {
|
|
720
|
+
allResults[i] = {
|
|
721
|
+
agent: params.tasks[i].agent,
|
|
722
|
+
agentSource: "unknown",
|
|
723
|
+
task: params.tasks[i].task,
|
|
724
|
+
exitCode: -1, // -1 = still running
|
|
725
|
+
messages: [],
|
|
726
|
+
stderr: "",
|
|
727
|
+
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const emitParallelUpdate = () => {
|
|
732
|
+
if (onUpdate) {
|
|
733
|
+
const running = allResults.filter((r) => r.exitCode === -1).length;
|
|
734
|
+
const done = allResults.filter((r) => r.exitCode !== -1).length;
|
|
735
|
+
onUpdate({
|
|
736
|
+
content: [
|
|
737
|
+
{ type: "text", text: `Parallel: ${done}/${allResults.length} done, ${running} running...` },
|
|
738
|
+
],
|
|
739
|
+
details: makeDetails("parallel")([...allResults]),
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
const results = await mapWithConcurrencyLimit(params.tasks, MAX_CONCURRENCY, async (t, index) => {
|
|
745
|
+
const result = await runSingleAgent(
|
|
746
|
+
ctx.cwd,
|
|
747
|
+
agents,
|
|
748
|
+
t.agent,
|
|
749
|
+
t.task,
|
|
750
|
+
t.cwd,
|
|
751
|
+
undefined,
|
|
752
|
+
signal,
|
|
753
|
+
subagentLimits,
|
|
754
|
+
// Per-task update callback
|
|
755
|
+
(partial) => {
|
|
756
|
+
if (partial.details?.results[0]) {
|
|
757
|
+
allResults[index] = partial.details.results[0];
|
|
758
|
+
emitParallelUpdate();
|
|
759
|
+
}
|
|
760
|
+
},
|
|
761
|
+
makeDetails("parallel"),
|
|
762
|
+
);
|
|
763
|
+
allResults[index] = result;
|
|
764
|
+
emitParallelUpdate();
|
|
765
|
+
return result;
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
const successCount = results.filter((r) => r.exitCode === 0).length;
|
|
769
|
+
const summaries = results.map((r) => {
|
|
770
|
+
const output = getFinalOutput(r.messages);
|
|
771
|
+
const preview = output.slice(0, 100) + (output.length > 100 ? "..." : "");
|
|
772
|
+
return `[${r.agent}] ${r.exitCode === 0 ? "completed" : "failed"}: ${preview || "(no output)"}`;
|
|
773
|
+
});
|
|
774
|
+
return {
|
|
775
|
+
content: [
|
|
776
|
+
{
|
|
777
|
+
type: "text",
|
|
778
|
+
text: `Parallel: ${successCount}/${results.length} succeeded\n\n${summaries.join("\n\n")}`,
|
|
779
|
+
},
|
|
780
|
+
],
|
|
781
|
+
details: makeDetails("parallel")(results),
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
if (params.agent && params.task) {
|
|
786
|
+
const result = await runSingleAgent(
|
|
787
|
+
ctx.cwd,
|
|
788
|
+
agents,
|
|
789
|
+
params.agent,
|
|
790
|
+
params.task,
|
|
791
|
+
params.cwd,
|
|
792
|
+
undefined,
|
|
793
|
+
signal,
|
|
794
|
+
subagentLimits,
|
|
795
|
+
onUpdate,
|
|
796
|
+
makeDetails("single"),
|
|
797
|
+
);
|
|
798
|
+
const isError = result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
|
|
799
|
+
if (isError) {
|
|
800
|
+
const errorMsg =
|
|
801
|
+
result.errorMessage || result.stderr || getFinalOutput(result.messages) || "(no output)";
|
|
802
|
+
return {
|
|
803
|
+
content: [{ type: "text", text: `Agent ${result.stopReason || "failed"}: ${errorMsg}` }],
|
|
804
|
+
details: makeDetails("single")([result]),
|
|
805
|
+
isError: true,
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
return {
|
|
809
|
+
content: [{ type: "text", text: getFinalOutput(result.messages) || "(no output)" }],
|
|
810
|
+
details: makeDetails("single")([result]),
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
|
|
815
|
+
return {
|
|
816
|
+
content: [{ type: "text", text: `Invalid parameters. Available agents: ${available}` }],
|
|
817
|
+
details: makeDetails("single")([]),
|
|
818
|
+
};
|
|
819
|
+
},
|
|
820
|
+
|
|
821
|
+
renderCall(args, theme, _context) {
|
|
822
|
+
const scope: AgentScope = args.agentScope ?? "user";
|
|
823
|
+
if (args.chain && args.chain.length > 0) {
|
|
824
|
+
let text =
|
|
825
|
+
theme.fg("toolTitle", theme.bold("subagent ")) +
|
|
826
|
+
theme.fg("accent", `chain (${args.chain.length} steps)`) +
|
|
827
|
+
theme.fg("muted", ` [${scope}]`);
|
|
828
|
+
for (let i = 0; i < Math.min(args.chain.length, 3); i++) {
|
|
829
|
+
const step = args.chain[i];
|
|
830
|
+
// Clean up {previous} placeholder for display
|
|
831
|
+
const cleanTask = step.task.replace(/\{previous\}/g, "").trim();
|
|
832
|
+
const preview = cleanTask.length > 40 ? `${cleanTask.slice(0, 40)}...` : cleanTask;
|
|
833
|
+
text +=
|
|
834
|
+
"\n " +
|
|
835
|
+
theme.fg("muted", `${i + 1}.`) +
|
|
836
|
+
" " +
|
|
837
|
+
theme.fg("accent", step.agent) +
|
|
838
|
+
theme.fg("dim", ` ${preview}`);
|
|
839
|
+
}
|
|
840
|
+
if (args.chain.length > 3) text += `\n ${theme.fg("muted", `... +${args.chain.length - 3} more`)}`;
|
|
841
|
+
return safeText(text);
|
|
842
|
+
}
|
|
843
|
+
if (args.tasks && args.tasks.length > 0) {
|
|
844
|
+
let text =
|
|
845
|
+
theme.fg("toolTitle", theme.bold("subagent ")) +
|
|
846
|
+
theme.fg("accent", `parallel (${args.tasks.length} tasks)`) +
|
|
847
|
+
theme.fg("muted", ` [${scope}]`);
|
|
848
|
+
for (const t of args.tasks.slice(0, 3)) {
|
|
849
|
+
const preview = t.task.length > 40 ? `${t.task.slice(0, 40)}...` : t.task;
|
|
850
|
+
text += `\n ${theme.fg("accent", t.agent)}${theme.fg("dim", ` ${preview}`)}`;
|
|
851
|
+
}
|
|
852
|
+
if (args.tasks.length > 3) text += `\n ${theme.fg("muted", `... +${args.tasks.length - 3} more`)}`;
|
|
853
|
+
return safeText(text);
|
|
854
|
+
}
|
|
855
|
+
const agentName = args.agent || "...";
|
|
856
|
+
const preview = args.task ? (args.task.length > 60 ? `${args.task.slice(0, 60)}...` : args.task) : "...";
|
|
857
|
+
let text =
|
|
858
|
+
theme.fg("toolTitle", theme.bold("subagent ")) +
|
|
859
|
+
theme.fg("accent", agentName) +
|
|
860
|
+
theme.fg("muted", ` [${scope}]`);
|
|
861
|
+
text += `\n ${theme.fg("dim", preview)}`;
|
|
862
|
+
return safeText(text);
|
|
863
|
+
},
|
|
864
|
+
|
|
865
|
+
renderResult(result, { expanded }, theme, _context) {
|
|
866
|
+
const details = result.details as SubagentDetails | undefined;
|
|
867
|
+
if (!details || details.results.length === 0) {
|
|
868
|
+
const text = result.content[0];
|
|
869
|
+
return safeText(text?.type === "text" ? text.text : "(no output)");
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
const mdTheme = getMarkdownTheme();
|
|
873
|
+
|
|
874
|
+
const renderDisplayItems = (items: DisplayItem[], limit?: number) => {
|
|
875
|
+
const toShow = limit ? items.slice(-limit) : items;
|
|
876
|
+
const skipped = limit && items.length > limit ? items.length - limit : 0;
|
|
877
|
+
let text = "";
|
|
878
|
+
if (skipped > 0) text += theme.fg("muted", `... ${skipped} earlier items\n`);
|
|
879
|
+
for (const item of toShow) {
|
|
880
|
+
if (item.type === "text") {
|
|
881
|
+
const preview = expanded ? item.text : item.text.split("\n").slice(0, 3).join("\n");
|
|
882
|
+
text += `${theme.fg("toolOutput", preview)}\n`;
|
|
883
|
+
} else {
|
|
884
|
+
text += `${theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme))}\n`;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
return text.trimEnd();
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
if (details.mode === "single" && details.results.length === 1) {
|
|
891
|
+
const r = details.results[0];
|
|
892
|
+
const isError = r.exitCode !== 0 || r.stopReason === "error" || r.stopReason === "aborted";
|
|
893
|
+
const icon = isError ? theme.fg("error", "✗") : theme.fg("success", "✓");
|
|
894
|
+
const displayItems = getDisplayItems(r.messages);
|
|
895
|
+
const finalOutput = getFinalOutput(r.messages);
|
|
896
|
+
|
|
897
|
+
if (expanded) {
|
|
898
|
+
const container = new SafeContainer();
|
|
899
|
+
let header = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
|
|
900
|
+
if (isError && r.stopReason) header += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
|
|
901
|
+
container.addChild(safeText(header));
|
|
902
|
+
if (isError && r.errorMessage)
|
|
903
|
+
container.addChild(new Text(theme.fg("error", `Error: ${r.errorMessage}`), 0, 0));
|
|
904
|
+
container.addChild(new Spacer(1));
|
|
905
|
+
container.addChild(new Text(theme.fg("muted", "─── Task ───"), 0, 0));
|
|
906
|
+
container.addChild(new Text(theme.fg("dim", r.task), 0, 0));
|
|
907
|
+
container.addChild(new Spacer(1));
|
|
908
|
+
container.addChild(new Text(theme.fg("muted", "─── Output ───"), 0, 0));
|
|
909
|
+
if (displayItems.length === 0 && !finalOutput) {
|
|
910
|
+
container.addChild(new Text(theme.fg("muted", "(no output)"), 0, 0));
|
|
911
|
+
} else {
|
|
912
|
+
for (const item of displayItems) {
|
|
913
|
+
if (item.type === "toolCall")
|
|
914
|
+
container.addChild(
|
|
915
|
+
new Text(
|
|
916
|
+
theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
|
|
917
|
+
0,
|
|
918
|
+
0,
|
|
919
|
+
),
|
|
920
|
+
);
|
|
921
|
+
}
|
|
922
|
+
if (finalOutput) {
|
|
923
|
+
container.addChild(new Spacer(1));
|
|
924
|
+
container.addChild(safeMarkdown(finalOutput.trim(), mdTheme));
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
const usageStr = formatUsageStats(r.usage, r.model);
|
|
928
|
+
if (usageStr) {
|
|
929
|
+
container.addChild(new Spacer(1));
|
|
930
|
+
container.addChild(new Text(theme.fg("dim", usageStr), 0, 0));
|
|
931
|
+
}
|
|
932
|
+
return container;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
let text = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
|
|
936
|
+
if (isError && r.stopReason) text += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
|
|
937
|
+
if (isError && r.errorMessage) text += `\n${theme.fg("error", `Error: ${r.errorMessage}`)}`;
|
|
938
|
+
else if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
|
|
939
|
+
else {
|
|
940
|
+
text += `\n${renderDisplayItems(displayItems, COLLAPSED_ITEM_COUNT)}`;
|
|
941
|
+
if (displayItems.length > COLLAPSED_ITEM_COUNT) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
|
|
942
|
+
}
|
|
943
|
+
const usageStr = formatUsageStats(r.usage, r.model);
|
|
944
|
+
if (usageStr) text += `\n${theme.fg("dim", usageStr)}`;
|
|
945
|
+
return safeText(text);
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
const aggregateUsage = (results: SingleResult[]) => {
|
|
949
|
+
const total = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, turns: 0 };
|
|
950
|
+
for (const r of results) {
|
|
951
|
+
total.input += r.usage.input;
|
|
952
|
+
total.output += r.usage.output;
|
|
953
|
+
total.cacheRead += r.usage.cacheRead;
|
|
954
|
+
total.cacheWrite += r.usage.cacheWrite;
|
|
955
|
+
total.cost += r.usage.cost;
|
|
956
|
+
total.turns += r.usage.turns;
|
|
957
|
+
}
|
|
958
|
+
return total;
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
if (details.mode === "chain") {
|
|
962
|
+
const successCount = details.results.filter((r) => r.exitCode === 0).length;
|
|
963
|
+
const icon = successCount === details.results.length ? theme.fg("success", "✓") : theme.fg("error", "✗");
|
|
964
|
+
|
|
965
|
+
if (expanded) {
|
|
966
|
+
const container = new SafeContainer();
|
|
967
|
+
container.addChild(
|
|
968
|
+
new Text(
|
|
969
|
+
icon +
|
|
970
|
+
" " +
|
|
971
|
+
theme.fg("toolTitle", theme.bold("chain ")) +
|
|
972
|
+
theme.fg("accent", `${successCount}/${details.results.length} steps`),
|
|
973
|
+
0,
|
|
974
|
+
0,
|
|
975
|
+
),
|
|
976
|
+
);
|
|
977
|
+
|
|
978
|
+
for (const r of details.results) {
|
|
979
|
+
const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
|
|
980
|
+
const displayItems = getDisplayItems(r.messages);
|
|
981
|
+
const finalOutput = getFinalOutput(r.messages);
|
|
982
|
+
|
|
983
|
+
container.addChild(new Spacer(1));
|
|
984
|
+
container.addChild(
|
|
985
|
+
new Text(
|
|
986
|
+
`${theme.fg("muted", `─── Step ${r.step}: `) + theme.fg("accent", r.agent)} ${rIcon}`,
|
|
987
|
+
0,
|
|
988
|
+
0,
|
|
989
|
+
),
|
|
990
|
+
);
|
|
991
|
+
container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
|
|
992
|
+
|
|
993
|
+
// Show tool calls
|
|
994
|
+
for (const item of displayItems) {
|
|
995
|
+
if (item.type === "toolCall") {
|
|
996
|
+
container.addChild(
|
|
997
|
+
new Text(
|
|
998
|
+
theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
|
|
999
|
+
0,
|
|
1000
|
+
0,
|
|
1001
|
+
),
|
|
1002
|
+
);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// Show final output as markdown
|
|
1007
|
+
if (finalOutput) {
|
|
1008
|
+
container.addChild(new Spacer(1));
|
|
1009
|
+
container.addChild(safeMarkdown(finalOutput.trim(), mdTheme));
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
const stepUsage = formatUsageStats(r.usage, r.model);
|
|
1013
|
+
if (stepUsage) container.addChild(new Text(theme.fg("dim", stepUsage), 0, 0));
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
const usageStr = formatUsageStats(aggregateUsage(details.results));
|
|
1017
|
+
if (usageStr) {
|
|
1018
|
+
container.addChild(new Spacer(1));
|
|
1019
|
+
container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
|
|
1020
|
+
}
|
|
1021
|
+
return container;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// Collapsed view
|
|
1025
|
+
let text =
|
|
1026
|
+
icon +
|
|
1027
|
+
" " +
|
|
1028
|
+
theme.fg("toolTitle", theme.bold("chain ")) +
|
|
1029
|
+
theme.fg("accent", `${successCount}/${details.results.length} steps`);
|
|
1030
|
+
for (const r of details.results) {
|
|
1031
|
+
const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
|
|
1032
|
+
const displayItems = getDisplayItems(r.messages);
|
|
1033
|
+
text += `\n\n${theme.fg("muted", `─── Step ${r.step}: `)}${theme.fg("accent", r.agent)} ${rIcon}`;
|
|
1034
|
+
if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
|
|
1035
|
+
else text += `\n${renderDisplayItems(displayItems, 5)}`;
|
|
1036
|
+
}
|
|
1037
|
+
const usageStr = formatUsageStats(aggregateUsage(details.results));
|
|
1038
|
+
if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
|
|
1039
|
+
text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
|
|
1040
|
+
return safeText(text);
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
if (details.mode === "parallel") {
|
|
1044
|
+
const running = details.results.filter((r) => r.exitCode === -1).length;
|
|
1045
|
+
const successCount = details.results.filter((r) => r.exitCode === 0).length;
|
|
1046
|
+
const failCount = details.results.filter((r) => r.exitCode > 0).length;
|
|
1047
|
+
const isRunning = running > 0;
|
|
1048
|
+
const icon = isRunning
|
|
1049
|
+
? theme.fg("warning", "⏳")
|
|
1050
|
+
: failCount > 0
|
|
1051
|
+
? theme.fg("warning", "◐")
|
|
1052
|
+
: theme.fg("success", "✓");
|
|
1053
|
+
const status = isRunning
|
|
1054
|
+
? `${successCount + failCount}/${details.results.length} done, ${running} running`
|
|
1055
|
+
: `${successCount}/${details.results.length} tasks`;
|
|
1056
|
+
|
|
1057
|
+
if (expanded && !isRunning) {
|
|
1058
|
+
const container = new SafeContainer();
|
|
1059
|
+
container.addChild(
|
|
1060
|
+
new Text(
|
|
1061
|
+
`${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`,
|
|
1062
|
+
0,
|
|
1063
|
+
0,
|
|
1064
|
+
),
|
|
1065
|
+
);
|
|
1066
|
+
|
|
1067
|
+
for (const r of details.results) {
|
|
1068
|
+
const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
|
|
1069
|
+
const displayItems = getDisplayItems(r.messages);
|
|
1070
|
+
const finalOutput = getFinalOutput(r.messages);
|
|
1071
|
+
|
|
1072
|
+
container.addChild(new Spacer(1));
|
|
1073
|
+
container.addChild(
|
|
1074
|
+
new Text(`${theme.fg("muted", "─── ") + theme.fg("accent", r.agent)} ${rIcon}`, 0, 0),
|
|
1075
|
+
);
|
|
1076
|
+
container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
|
|
1077
|
+
|
|
1078
|
+
// Show tool calls
|
|
1079
|
+
for (const item of displayItems) {
|
|
1080
|
+
if (item.type === "toolCall") {
|
|
1081
|
+
container.addChild(
|
|
1082
|
+
new Text(
|
|
1083
|
+
theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
|
|
1084
|
+
0,
|
|
1085
|
+
0,
|
|
1086
|
+
),
|
|
1087
|
+
);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
// Show final output as markdown
|
|
1092
|
+
if (finalOutput) {
|
|
1093
|
+
container.addChild(new Spacer(1));
|
|
1094
|
+
container.addChild(safeMarkdown(finalOutput.trim(), mdTheme));
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
const taskUsage = formatUsageStats(r.usage, r.model);
|
|
1098
|
+
if (taskUsage) container.addChild(new Text(theme.fg("dim", taskUsage), 0, 0));
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
const usageStr = formatUsageStats(aggregateUsage(details.results));
|
|
1102
|
+
if (usageStr) {
|
|
1103
|
+
container.addChild(new Spacer(1));
|
|
1104
|
+
container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
|
|
1105
|
+
}
|
|
1106
|
+
return container;
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
// Collapsed view (or still running)
|
|
1110
|
+
let text = `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`;
|
|
1111
|
+
for (const r of details.results) {
|
|
1112
|
+
const rIcon =
|
|
1113
|
+
r.exitCode === -1
|
|
1114
|
+
? theme.fg("warning", "⏳")
|
|
1115
|
+
: r.exitCode === 0
|
|
1116
|
+
? theme.fg("success", "✓")
|
|
1117
|
+
: theme.fg("error", "✗");
|
|
1118
|
+
const displayItems = getDisplayItems(r.messages);
|
|
1119
|
+
text += `\n\n${theme.fg("muted", "─── ")}${theme.fg("accent", r.agent)} ${rIcon}`;
|
|
1120
|
+
if (displayItems.length === 0)
|
|
1121
|
+
text += `\n${theme.fg("muted", r.exitCode === -1 ? "(running...)" : "(no output)")}`;
|
|
1122
|
+
else text += `\n${renderDisplayItems(displayItems, 5)}`;
|
|
1123
|
+
}
|
|
1124
|
+
if (!isRunning) {
|
|
1125
|
+
const usageStr = formatUsageStats(aggregateUsage(details.results));
|
|
1126
|
+
if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
|
|
1127
|
+
}
|
|
1128
|
+
if (!expanded) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
|
|
1129
|
+
return safeText(text);
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
const text = result.content[0];
|
|
1133
|
+
return safeText(text?.type === "text" ? text.text : "(no output)");
|
|
1134
|
+
},
|
|
1135
|
+
});
|
|
1136
|
+
}
|