@fosterg4/pi-subagent 1.0.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/index.ts ADDED
@@ -0,0 +1,1436 @@
1
+ /**
2
+ * @fosterg4/pi-subagent - Delegate tasks to specialized subagents
3
+ *
4
+ * Spawns a separate `pi` process for each subagent invocation,
5
+ * giving it an isolated context window.
6
+ *
7
+ * Supports three modes:
8
+ * - Single: { agent: "name", task: "..." }
9
+ * - Parallel: { tasks: [{ agent: "name", task: "..." }, ...] }
10
+ * - Chain: { chain: [{ agent: "name", task: "... {previous} ..." }, ...] }
11
+ *
12
+ * Extends the reference implementation with:
13
+ * - Bundled agent discovery (agents ship with the package)
14
+ * - Contract schemas (inputSchema/outputSchema with structured JSON handoff)
15
+ * - Live per-subagent TUI tool call streaming
16
+ */
17
+
18
+ import { spawn } from "node:child_process";
19
+ import * as fs from "node:fs";
20
+ import * as os from "node:os";
21
+ import * as path from "node:path";
22
+ import type { Message } from "@earendil-works/pi-ai";
23
+ import { StringEnum } from "@earendil-works/pi-ai";
24
+ import {
25
+ type ExtensionAPI,
26
+ getMarkdownTheme,
27
+ withFileMutationQueue,
28
+ } from "@earendil-works/pi-coding-agent";
29
+ import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
30
+ import { Type } from "typebox";
31
+ import {
32
+ type AgentConfig,
33
+ type AgentScope,
34
+ discoverAgents,
35
+ formatAgentList,
36
+ } from "./agents.ts";
37
+ import { type ValidationResult, validateSchema } from "./validate.ts";
38
+
39
+ const MAX_PARALLEL_TASKS = 8;
40
+ const MAX_CONCURRENCY = 4;
41
+ const COLLAPSED_ITEM_COUNT = 10;
42
+ const PER_TASK_OUTPUT_CAP = 50 * 1024;
43
+
44
+ // ---------------------------------------------------------------------------
45
+ // Formatting utilities
46
+ // ---------------------------------------------------------------------------
47
+
48
+ function formatTokens(count: number): string {
49
+ if (count < 1000) return count.toString();
50
+ if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
51
+ if (count < 1000000) return `${Math.round(count / 1000)}k`;
52
+ return `${(count / 1000000).toFixed(1)}M`;
53
+ }
54
+
55
+ function formatUsageStats(
56
+ usage: {
57
+ input: number;
58
+ output: number;
59
+ cacheRead: number;
60
+ cacheWrite: number;
61
+ cost: number;
62
+ contextTokens?: number;
63
+ turns?: number;
64
+ },
65
+ model?: string,
66
+ ): string {
67
+ const parts: string[] = [];
68
+ if (usage.turns) parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
69
+ if (usage.input) parts.push(`\u2191${formatTokens(usage.input)}`);
70
+ if (usage.output) parts.push(`\u2193${formatTokens(usage.output)}`);
71
+ if (usage.cacheRead) parts.push(`R${formatTokens(usage.cacheRead)}`);
72
+ if (usage.cacheWrite) parts.push(`W${formatTokens(usage.cacheWrite)}`);
73
+ if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
74
+ if (usage.contextTokens && usage.contextTokens > 0) {
75
+ parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
76
+ }
77
+ if (model) parts.push(model);
78
+ return parts.join(" ");
79
+ }
80
+
81
+ function formatToolCall(
82
+ toolName: string,
83
+ args: Record<string, unknown>,
84
+ themeFg: (color: string, text: string) => string,
85
+ ): string {
86
+ const shortenPath = (p: string) => {
87
+ const home = os.homedir();
88
+ return p.startsWith(home) ? `~${p.slice(home.length)}` : p;
89
+ };
90
+
91
+ switch (toolName) {
92
+ case "bash": {
93
+ const command = (args.command as string) || "...";
94
+ const preview = command.length > 60 ? `${command.slice(0, 60)}...` : command;
95
+ return themeFg("muted", "$ ") + themeFg("toolOutput", preview);
96
+ }
97
+ case "read": {
98
+ const rawPath = (args.file_path || args.path || "...") as string;
99
+ const filePath = shortenPath(rawPath);
100
+ const offset = args.offset as number | undefined;
101
+ const limit = args.limit as number | undefined;
102
+ let text = themeFg("accent", filePath);
103
+ if (offset !== undefined || limit !== undefined) {
104
+ const startLine = offset ?? 1;
105
+ const endLine = limit !== undefined ? startLine + limit - 1 : "";
106
+ text += themeFg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
107
+ }
108
+ return themeFg("muted", "read ") + text;
109
+ }
110
+ case "write": {
111
+ const rawPath = (args.file_path || args.path || "...") as string;
112
+ const filePath = shortenPath(rawPath);
113
+ const content = (args.content || "") as string;
114
+ const lines = content.split("\n").length;
115
+ let text = themeFg("muted", "write ") + themeFg("accent", filePath);
116
+ if (lines > 1) text += themeFg("dim", ` (${lines} lines)`);
117
+ return text;
118
+ }
119
+ case "edit": {
120
+ const rawPath = (args.file_path || args.path || "...") as string;
121
+ return themeFg("muted", "edit ") + themeFg("accent", shortenPath(rawPath));
122
+ }
123
+ case "ls":
124
+ return themeFg("muted", "ls ") + themeFg("accent", shortenPath((args.path || ".") as string));
125
+ case "find":
126
+ return (
127
+ themeFg("muted", "find ") +
128
+ themeFg("accent", (args.pattern || "*") as string) +
129
+ themeFg("dim", ` in ${shortenPath((args.path || ".") as string)}`)
130
+ );
131
+ case "grep":
132
+ return (
133
+ themeFg("muted", "grep ") +
134
+ themeFg("accent", `/${(args.pattern || "") as string}/`) +
135
+ themeFg("dim", ` in ${shortenPath((args.path || ".") as string)}`)
136
+ );
137
+ default:
138
+ return (
139
+ themeFg("accent", toolName) +
140
+ themeFg("dim", ` ${JSON.stringify(args).slice(0, 50)}...`)
141
+ );
142
+ }
143
+ }
144
+
145
+ // ---------------------------------------------------------------------------
146
+ // Types
147
+ // ---------------------------------------------------------------------------
148
+
149
+ interface UsageStats {
150
+ input: number;
151
+ output: number;
152
+ cacheRead: number;
153
+ cacheWrite: number;
154
+ cost: number;
155
+ contextTokens: number;
156
+ turns: number;
157
+ }
158
+
159
+ interface SingleResult {
160
+ agent: string;
161
+ agentSource: "user" | "project" | "bundled" | "unknown";
162
+ task: string;
163
+ exitCode: number;
164
+ messages: Message[];
165
+ stderr: string;
166
+ usage: UsageStats;
167
+ model?: string;
168
+ stopReason?: string;
169
+ errorMessage?: string;
170
+ step?: number;
171
+ structuredOutput?: Record<string, unknown>;
172
+ }
173
+
174
+ interface SubagentDetails {
175
+ mode: "single" | "parallel" | "chain";
176
+ agentScope: AgentScope;
177
+ projectAgentsDir: string | null;
178
+ results: SingleResult[];
179
+ }
180
+
181
+ // ---------------------------------------------------------------------------
182
+ // Result helpers
183
+ // ---------------------------------------------------------------------------
184
+
185
+ function getFinalOutput(messages: Message[]): string {
186
+ for (let i = messages.length - 1; i >= 0; i--) {
187
+ const msg = messages[i];
188
+ if (msg.role === "assistant") {
189
+ for (const part of msg.content) {
190
+ if (part.type === "text") return part.text;
191
+ }
192
+ }
193
+ }
194
+ return "";
195
+ }
196
+
197
+ function isFailedResult(result: SingleResult): boolean {
198
+ return (
199
+ result.exitCode !== 0 ||
200
+ result.stopReason === "error" ||
201
+ result.stopReason === "aborted"
202
+ );
203
+ }
204
+
205
+ function getResultOutput(result: SingleResult): string {
206
+ if (isFailedResult(result)) {
207
+ return (
208
+ result.errorMessage ||
209
+ result.stderr ||
210
+ getFinalOutput(result.messages) ||
211
+ "(no output)"
212
+ );
213
+ }
214
+ return getFinalOutput(result.messages) || "(no output)";
215
+ }
216
+
217
+ function truncateParallelOutput(output: string): string {
218
+ const byteLength = Buffer.byteLength(output, "utf8");
219
+ if (byteLength <= PER_TASK_OUTPUT_CAP) return output;
220
+
221
+ let truncated = output.slice(0, PER_TASK_OUTPUT_CAP);
222
+ while (Buffer.byteLength(truncated, "utf8") > PER_TASK_OUTPUT_CAP) {
223
+ truncated = truncated.slice(0, -1);
224
+ }
225
+ return `${truncated}\n\n[Output truncated: ${byteLength - Buffer.byteLength(truncated, "utf8")} bytes omitted. Full output preserved in tool details.]`;
226
+ }
227
+
228
+ type DisplayItem =
229
+ | { type: "text"; text: string }
230
+ | { type: "toolCall"; name: string; args: Record<string, unknown> };
231
+
232
+ function getDisplayItems(messages: Message[]): DisplayItem[] {
233
+ const items: DisplayItem[] = [];
234
+ for (const msg of messages) {
235
+ if (msg.role === "assistant") {
236
+ for (const part of msg.content) {
237
+ if (part.type === "text")
238
+ items.push({ type: "text", text: part.text });
239
+ else if (part.type === "toolCall")
240
+ items.push({ type: "toolCall", name: part.name, args: part.arguments });
241
+ }
242
+ }
243
+ }
244
+ return items;
245
+ }
246
+
247
+ // ---------------------------------------------------------------------------
248
+ // Concurrency
249
+ // ---------------------------------------------------------------------------
250
+
251
+ async function mapWithConcurrencyLimit<TIn, TOut>(
252
+ items: TIn[],
253
+ concurrency: number,
254
+ fn: (item: TIn, index: number) => Promise<TOut>,
255
+ ): Promise<TOut[]> {
256
+ if (items.length === 0) return [];
257
+ const limit = Math.max(1, Math.min(concurrency, items.length));
258
+ const results: TOut[] = new Array(items.length);
259
+ let nextIndex = 0;
260
+ const workers = new Array(limit).fill(null).map(async () => {
261
+ while (true) {
262
+ const current = nextIndex++;
263
+ if (current >= items.length) return;
264
+ results[current] = await fn(items[current], current);
265
+ }
266
+ });
267
+ await Promise.all(workers);
268
+ return results;
269
+ }
270
+
271
+ // ---------------------------------------------------------------------------
272
+ // Temp prompt file
273
+ // ---------------------------------------------------------------------------
274
+
275
+ async function writePromptToTempFile(
276
+ agentName: string,
277
+ prompt: string,
278
+ ): Promise<{ dir: string; filePath: string }> {
279
+ const tmpDir = await fs.promises.mkdtemp(
280
+ path.join(os.tmpdir(), "pi-subagent-"),
281
+ );
282
+ const safeName = agentName.replace(/[^\w.-]+/g, "_");
283
+ const filePath = path.join(tmpDir, `prompt-${safeName}.md`);
284
+ await withFileMutationQueue(filePath, async () => {
285
+ await fs.promises.writeFile(filePath, prompt, {
286
+ encoding: "utf-8",
287
+ mode: 0o600,
288
+ });
289
+ });
290
+ return { dir: tmpDir, filePath };
291
+ }
292
+
293
+ // ---------------------------------------------------------------------------
294
+ // Pi invocation helper
295
+ // ---------------------------------------------------------------------------
296
+
297
+ function getPiInvocation(
298
+ args: string[],
299
+ ): { command: string; args: string[] } {
300
+ const currentScript = process.argv[1];
301
+ const isBunVirtualScript = currentScript?.startsWith("/$bunfs/root/");
302
+ if (currentScript && !isBunVirtualScript && fs.existsSync(currentScript)) {
303
+ return { command: process.execPath, args: [currentScript, ...args] };
304
+ }
305
+
306
+ const execName = path.basename(process.execPath).toLowerCase();
307
+ const isGenericRuntime = /^(node|bun)(\.exe)?$/.test(execName);
308
+ if (!isGenericRuntime) {
309
+ return { command: process.execPath, args };
310
+ }
311
+
312
+ return { command: "pi", args };
313
+ }
314
+
315
+ // ---------------------------------------------------------------------------
316
+ // Extract structured JSON from assistant output
317
+ // ---------------------------------------------------------------------------
318
+
319
+ function extractStructuredOutput(
320
+ messages: Message[],
321
+ ): Record<string, unknown> | undefined {
322
+ const finalOutput = getFinalOutput(messages);
323
+ if (!finalOutput) return undefined;
324
+
325
+ // Try to parse the entire output as JSON
326
+ try {
327
+ return JSON.parse(finalOutput) as Record<string, unknown>;
328
+ } catch {
329
+ // Not valid JSON — look for a JSON code block
330
+ const jsonMatch = finalOutput.match(/```(?:json)?\s*([\s\S]*?)\s*```/);
331
+ if (jsonMatch) {
332
+ try {
333
+ return JSON.parse(jsonMatch[1]) as Record<string, unknown>;
334
+ } catch {
335
+ return undefined;
336
+ }
337
+ }
338
+ return undefined;
339
+ }
340
+ }
341
+
342
+ // ---------------------------------------------------------------------------
343
+ // Run single agent
344
+ // ---------------------------------------------------------------------------
345
+
346
+ type OnUpdateCallback = (partial: {
347
+ content: { type: "text"; text: string }[];
348
+ details: SubagentDetails;
349
+ }) => void;
350
+
351
+ async function runSingleAgent(
352
+ defaultCwd: string,
353
+ agents: AgentConfig[],
354
+ agentName: string,
355
+ task: string,
356
+ cwd: string | undefined,
357
+ step: number | undefined,
358
+ signal: AbortSignal | undefined,
359
+ onUpdate: OnUpdateCallback | undefined,
360
+ makeDetails: (results: SingleResult[]) => SubagentDetails,
361
+ ): Promise<SingleResult> {
362
+ const agent = agents.find((a) => a.name === agentName);
363
+
364
+ if (!agent) {
365
+ const available =
366
+ agents.map((a) => `"${a.name}"`).join(", ") || "none";
367
+ return {
368
+ agent: agentName,
369
+ agentSource: "unknown",
370
+ task,
371
+ exitCode: 1,
372
+ messages: [],
373
+ stderr: `Unknown agent: "${agentName}". Available agents: ${available}.`,
374
+ usage: {
375
+ input: 0,
376
+ output: 0,
377
+ cacheRead: 0,
378
+ cacheWrite: 0,
379
+ cost: 0,
380
+ contextTokens: 0,
381
+ turns: 0,
382
+ },
383
+ step,
384
+ };
385
+ }
386
+
387
+ // Validate task against agent's inputSchema if present
388
+ if (agent.inputSchema) {
389
+ const taskInput = parseStructuredTask(task);
390
+ const validation = validateSchema(taskInput, agent.inputSchema);
391
+ if (!validation.valid) {
392
+ return {
393
+ agent: agentName,
394
+ agentSource: agent.source,
395
+ task,
396
+ exitCode: 1,
397
+ messages: [],
398
+ stderr: `Input validation failed for agent "${agentName}":\n${(validation.errors ?? []).join("\n")}\n\nExpected schema: ${JSON.stringify(agent.inputSchema, null, 2)}`,
399
+ usage: {
400
+ input: 0,
401
+ output: 0,
402
+ cacheRead: 0,
403
+ cacheWrite: 0,
404
+ cost: 0,
405
+ contextTokens: 0,
406
+ turns: 0,
407
+ },
408
+ step,
409
+ };
410
+ }
411
+ }
412
+
413
+ const args: string[] = ["--mode", "json", "-p", "--no-session"];
414
+ if (agent.model) args.push("--model", agent.model);
415
+ if (agent.tools && agent.tools.length > 0)
416
+ args.push("--tools", agent.tools.join(","));
417
+
418
+ let tmpPromptDir: string | null = null;
419
+ let tmpPromptPath: string | null = null;
420
+
421
+ const currentResult: SingleResult = {
422
+ agent: agentName,
423
+ agentSource: agent.source,
424
+ task,
425
+ exitCode: 0,
426
+ messages: [],
427
+ stderr: "",
428
+ usage: {
429
+ input: 0,
430
+ output: 0,
431
+ cacheRead: 0,
432
+ cacheWrite: 0,
433
+ cost: 0,
434
+ contextTokens: 0,
435
+ turns: 0,
436
+ },
437
+ model: agent.model,
438
+ step,
439
+ };
440
+
441
+ const emitUpdate = () => {
442
+ if (onUpdate) {
443
+ onUpdate({
444
+ content: [
445
+ {
446
+ type: "text",
447
+ text: getFinalOutput(currentResult.messages) || "(running...)",
448
+ },
449
+ ],
450
+ details: makeDetails([currentResult]),
451
+ });
452
+ }
453
+ };
454
+
455
+ try {
456
+ if (agent.systemPrompt.trim()) {
457
+ const tmp = await writePromptToTempFile(agent.name, agent.systemPrompt);
458
+ tmpPromptDir = tmp.dir;
459
+ tmpPromptPath = tmp.filePath;
460
+ args.push("--append-system-prompt", tmpPromptPath);
461
+ }
462
+
463
+ args.push(`Task: ${task}`);
464
+
465
+ const exitCode = await new Promise<number>((resolve) => {
466
+ const invocation = getPiInvocation(args);
467
+ const proc = spawn(invocation.command, invocation.args, {
468
+ cwd: cwd ?? defaultCwd,
469
+ shell: false,
470
+ stdio: ["ignore", "pipe", "pipe"],
471
+ });
472
+ let buffer = "";
473
+
474
+ const processLine = (line: string) => {
475
+ if (!line.trim()) return;
476
+ let event: Record<string, unknown>;
477
+ try {
478
+ event = JSON.parse(line);
479
+ } catch {
480
+ return;
481
+ }
482
+
483
+ if (event.type === "message_end" && event.message) {
484
+ const msg = event.message as Message;
485
+ currentResult.messages.push(msg);
486
+
487
+ if (msg.role === "assistant") {
488
+ currentResult.usage.turns++;
489
+ const usage = msg.usage;
490
+ if (usage) {
491
+ currentResult.usage.input += usage.input || 0;
492
+ currentResult.usage.output += usage.output || 0;
493
+ currentResult.usage.cacheRead += usage.cacheRead || 0;
494
+ currentResult.usage.cacheWrite += usage.cacheWrite || 0;
495
+ currentResult.usage.cost += usage.cost?.total || 0;
496
+ currentResult.usage.contextTokens = usage.totalTokens || 0;
497
+ }
498
+ if (!currentResult.model && msg.model)
499
+ currentResult.model = msg.model;
500
+ if (msg.stopReason) currentResult.stopReason = msg.stopReason;
501
+ if (msg.errorMessage) currentResult.errorMessage = msg.errorMessage;
502
+ }
503
+ emitUpdate();
504
+ }
505
+
506
+ if (event.type === "tool_result_end" && event.message) {
507
+ currentResult.messages.push(event.message as Message);
508
+ emitUpdate();
509
+ }
510
+ };
511
+
512
+ proc.stdout.on("data", (data: Buffer) => {
513
+ buffer += data.toString();
514
+ const lines = buffer.split("\n");
515
+ buffer = lines.pop() || "";
516
+ for (const line of lines) processLine(line);
517
+ });
518
+
519
+ proc.stderr.on("data", (data: Buffer) => {
520
+ currentResult.stderr += data.toString();
521
+ });
522
+
523
+ proc.on("close", (code) => {
524
+ if (buffer.trim()) processLine(buffer);
525
+ resolve(code ?? 0);
526
+ });
527
+
528
+ proc.on("error", () => {
529
+ resolve(1);
530
+ });
531
+
532
+ if (signal) {
533
+ let wasAborted = false;
534
+ const killProc = () => {
535
+ if (wasAborted) return;
536
+ wasAborted = true;
537
+ currentResult.stopReason = "aborted";
538
+ proc.kill("SIGTERM");
539
+ setTimeout(() => {
540
+ if (!proc.killed) proc.kill("SIGKILL");
541
+ }, 5000);
542
+ };
543
+ if (signal.aborted) killProc();
544
+ else signal.addEventListener("abort", killProc, { once: true });
545
+ }
546
+ });
547
+
548
+ currentResult.exitCode = exitCode;
549
+
550
+ // Extract structured output from final messages
551
+ if (agent.outputSchema) {
552
+ currentResult.structuredOutput = extractStructuredOutput(
553
+ currentResult.messages,
554
+ );
555
+
556
+ // Validate structured output against outputSchema
557
+ if (currentResult.structuredOutput) {
558
+ const validation = validateSchema(
559
+ currentResult.structuredOutput,
560
+ agent.outputSchema,
561
+ );
562
+ if (!validation.valid) {
563
+ currentResult.stderr += `\n[Output schema validation warning: ${(validation.errors ?? []).join("; ")}]`;
564
+ }
565
+ }
566
+ }
567
+
568
+ return currentResult;
569
+ } finally {
570
+ if (tmpPromptPath)
571
+ try {
572
+ fs.unlinkSync(tmpPromptPath);
573
+ } catch {
574
+ /* ignore */
575
+ }
576
+ if (tmpPromptDir)
577
+ try {
578
+ fs.rmdirSync(tmpPromptDir);
579
+ } catch {
580
+ /* ignore */
581
+ }
582
+ }
583
+ }
584
+
585
+ // ---------------------------------------------------------------------------
586
+ // Parse structured task from text
587
+ // ---------------------------------------------------------------------------
588
+
589
+ function parseStructuredTask(task: string): Record<string, unknown> {
590
+ try {
591
+ return JSON.parse(task) as Record<string, unknown>;
592
+ } catch {
593
+ return { query: task };
594
+ }
595
+ }
596
+
597
+ // ---------------------------------------------------------------------------
598
+ // Tool parameter schemas
599
+ // ---------------------------------------------------------------------------
600
+
601
+ const TaskItem = Type.Object({
602
+ agent: Type.String({ description: "Name of the agent to invoke" }),
603
+ task: Type.String({ description: "Task to delegate to the agent" }),
604
+ cwd: Type.Optional(
605
+ Type.String({ description: "Working directory for the agent process" }),
606
+ ),
607
+ });
608
+
609
+ const ChainItem = Type.Object({
610
+ agent: Type.String({ description: "Name of the agent to invoke" }),
611
+ task: Type.String({
612
+ description:
613
+ "Task with optional {previous} placeholder for prior output",
614
+ }),
615
+ cwd: Type.Optional(
616
+ Type.String({ description: "Working directory for the agent process" }),
617
+ ),
618
+ });
619
+
620
+ const AgentScopeSchema = StringEnum(["user", "project", "both"] as const, {
621
+ description:
622
+ 'Which agent directories to use. Default: "user". Use "both" to include project-local agents.',
623
+ default: "user",
624
+ });
625
+
626
+ const SubagentParams = Type.Object({
627
+ agent: Type.Optional(
628
+ Type.String({ description: "Name of the agent to invoke (for single mode)" }),
629
+ ),
630
+ task: Type.Optional(
631
+ Type.String({ description: "Task to delegate (for single mode)" }),
632
+ ),
633
+ tasks: Type.Optional(
634
+ Type.Array(TaskItem, {
635
+ description: "Array of {agent, task} for parallel execution",
636
+ }),
637
+ ),
638
+ chain: Type.Optional(
639
+ Type.Array(ChainItem, {
640
+ description:
641
+ "Array of {agent, task} for sequential execution",
642
+ }),
643
+ ),
644
+ agentScope: Type.Optional(AgentScopeSchema),
645
+ confirmProjectAgents: Type.Optional(
646
+ Type.Boolean({
647
+ description:
648
+ "Prompt before running project-local agents. Default: true.",
649
+ default: true,
650
+ }),
651
+ ),
652
+ cwd: Type.Optional(
653
+ Type.String({
654
+ description: "Working directory for the agent process (single mode)",
655
+ }),
656
+ ),
657
+ });
658
+
659
+ // ---------------------------------------------------------------------------
660
+ // Extension entry
661
+ // ---------------------------------------------------------------------------
662
+
663
+ export default function (pi: ExtensionAPI) {
664
+ pi.registerTool({
665
+ name: "subagent",
666
+ label: "Subagent",
667
+ description: [
668
+ "Delegate tasks to specialized subagents with isolated context.",
669
+ "Modes: single (agent + task), parallel (tasks array), chain (sequential with {previous} placeholder).",
670
+ 'Default agent scope is "user" (from ~/.pi/agent/agents).',
671
+ 'To enable project-local agents in .pi/agents, set agentScope: "both" (or "project").',
672
+ ].join(" "),
673
+ parameters: SubagentParams,
674
+
675
+ async execute(
676
+ _toolCallId,
677
+ params,
678
+ signal,
679
+ onUpdate,
680
+ ctx,
681
+ ) {
682
+ const agentScope: AgentScope = params.agentScope ?? "user";
683
+ const discovery = discoverAgents(ctx.cwd, agentScope);
684
+ const agents = discovery.agents;
685
+
686
+ const hasChain = (params.chain?.length ?? 0) > 0;
687
+ const hasTasks = (params.tasks?.length ?? 0) > 0;
688
+ const hasSingle = Boolean(params.agent && params.task);
689
+ const modeCount =
690
+ Number(hasChain) + Number(hasTasks) + Number(hasSingle);
691
+
692
+ const makeDetails =
693
+ (mode: "single" | "parallel" | "chain") =>
694
+ (results: SingleResult[]): SubagentDetails => ({
695
+ mode,
696
+ agentScope,
697
+ projectAgentsDir: discovery.projectAgentsDir,
698
+ results,
699
+ });
700
+
701
+ if (modeCount !== 1) {
702
+ const formatted = formatAgentList(agents, 10);
703
+ const available = formatted.text || "none";
704
+ return {
705
+ content: [
706
+ {
707
+ type: "text",
708
+ text: `Invalid parameters. Provide exactly one of: agent+task, tasks[], or chain[].\nAvailable agents: ${available}`,
709
+ },
710
+ ],
711
+ details: makeDetails("single")([]),
712
+ };
713
+ }
714
+
715
+ // Confirm project agents
716
+ if (
717
+ (agentScope === "project" || agentScope === "both") &&
718
+ params.confirmProjectAgents !== false &&
719
+ ctx.hasUI
720
+ ) {
721
+ const requestedAgentNames = new Set<string>();
722
+ if (params.chain)
723
+ for (const step of params.chain)
724
+ requestedAgentNames.add(step.agent);
725
+ if (params.tasks)
726
+ for (const t of params.tasks)
727
+ requestedAgentNames.add(t.agent);
728
+ if (params.agent) requestedAgentNames.add(params.agent);
729
+
730
+ const projectAgentsRequested = Array.from(requestedAgentNames)
731
+ .map((name) => agents.find((a) => a.name === name))
732
+ .filter(
733
+ (a): a is AgentConfig =>
734
+ a?.source === "project",
735
+ );
736
+
737
+ if (projectAgentsRequested.length > 0) {
738
+ const names = projectAgentsRequested
739
+ .map((a) => a.name)
740
+ .join(", ");
741
+ const dir = discovery.projectAgentsDir ?? "(unknown)";
742
+ const ok = await ctx.ui.confirm(
743
+ "Run project-local agents?",
744
+ `Agents: ${names}\nSource: ${dir}\n\nProject agents are repo-controlled. Only continue for trusted repositories.`,
745
+ );
746
+ if (!ok)
747
+ return {
748
+ content: [
749
+ {
750
+ type: "text",
751
+ text: "Canceled: project-local agents not approved.",
752
+ },
753
+ ],
754
+ details: makeDetails(
755
+ hasChain ? "chain" : hasTasks ? "parallel" : "single",
756
+ )([]),
757
+ };
758
+ }
759
+ }
760
+
761
+ // ---- Chain mode ----
762
+ if (params.chain && params.chain.length > 0) {
763
+ const results: SingleResult[] = [];
764
+ let previousStructured: Record<string, unknown> | undefined;
765
+
766
+ for (let i = 0; i < params.chain.length; i++) {
767
+ const step = params.chain[i];
768
+ let taskWithContext = step.task;
769
+
770
+ // Replace {previous} with structured output from prior step
771
+ if (previousStructured) {
772
+ taskWithContext = taskWithContext.replace(
773
+ /\{previous\}/g,
774
+ JSON.stringify(previousStructured, null, 2),
775
+ );
776
+ } else {
777
+ taskWithContext = taskWithContext.replace(
778
+ /\{previous\}/g,
779
+ getFinalOutput(results[i - 1]?.messages ?? ""),
780
+ );
781
+ }
782
+
783
+ const chainUpdate: OnUpdateCallback | undefined = onUpdate
784
+ ? (partial) => {
785
+ const currentResult = partial.details?.results[0];
786
+ if (currentResult) {
787
+ const allResults = [...results, currentResult];
788
+ onUpdate({
789
+ content: partial.content,
790
+ details: makeDetails("chain")(allResults),
791
+ });
792
+ }
793
+ }
794
+ : undefined;
795
+
796
+ const result = await runSingleAgent(
797
+ ctx.cwd,
798
+ agents,
799
+ step.agent,
800
+ taskWithContext,
801
+ step.cwd,
802
+ i + 1,
803
+ signal,
804
+ chainUpdate,
805
+ makeDetails("chain"),
806
+ );
807
+ results.push(result);
808
+
809
+ // Check if next agent in chain expects structured input
810
+ const nextAgent = params.chain[i + 1];
811
+ if (
812
+ nextAgent &&
813
+ result.structuredOutput &&
814
+ !isFailedResult(result)
815
+ ) {
816
+ previousStructured = result.structuredOutput;
817
+ } else {
818
+ previousStructured = undefined;
819
+ }
820
+
821
+ if (isFailedResult(result)) {
822
+ const errorMsg = getResultOutput(result);
823
+ return {
824
+ content: [
825
+ {
826
+ type: "text",
827
+ text: `Chain stopped at step ${i + 1} (${step.agent}): ${errorMsg}`,
828
+ },
829
+ ],
830
+ details: makeDetails("chain")(results),
831
+ isError: true,
832
+ };
833
+ }
834
+ }
835
+
836
+ const lastResult = results[results.length - 1];
837
+ return {
838
+ content: [
839
+ {
840
+ type: "text",
841
+ text:
842
+ getFinalOutput(lastResult.messages) || "(no output)",
843
+ },
844
+ ],
845
+ details: makeDetails("chain")(results),
846
+ };
847
+ }
848
+
849
+ // ---- Parallel mode ----
850
+ if (params.tasks && params.tasks.length > 0) {
851
+ if (params.tasks.length > MAX_PARALLEL_TASKS)
852
+ return {
853
+ content: [
854
+ {
855
+ type: "text",
856
+ text: `Too many parallel tasks (${params.tasks.length}). Max is ${MAX_PARALLEL_TASKS}.`,
857
+ },
858
+ ],
859
+ details: makeDetails("parallel")([]),
860
+ };
861
+
862
+ const allResults: SingleResult[] = new Array(params.tasks.length);
863
+
864
+ for (let i = 0; i < params.tasks.length; i++) {
865
+ allResults[i] = {
866
+ agent: params.tasks[i].agent,
867
+ agentSource: "unknown",
868
+ task: params.tasks[i].task,
869
+ exitCode: -1,
870
+ messages: [],
871
+ stderr: "",
872
+ usage: {
873
+ input: 0,
874
+ output: 0,
875
+ cacheRead: 0,
876
+ cacheWrite: 0,
877
+ cost: 0,
878
+ contextTokens: 0,
879
+ turns: 0,
880
+ },
881
+ };
882
+ }
883
+
884
+ const emitParallelUpdate = () => {
885
+ if (onUpdate) {
886
+ const running = allResults.filter((r) => r.exitCode === -1).length;
887
+ const done = allResults.filter((r) => r.exitCode !== -1).length;
888
+ onUpdate({
889
+ content: [
890
+ {
891
+ type: "text",
892
+ text: `Parallel: ${done}/${allResults.length} done, ${running} running...`,
893
+ },
894
+ ],
895
+ details: makeDetails("parallel")([...allResults]),
896
+ });
897
+ }
898
+ };
899
+
900
+ const results = await mapWithConcurrencyLimit(
901
+ params.tasks,
902
+ MAX_CONCURRENCY,
903
+ async (t, index) => {
904
+ const result = await runSingleAgent(
905
+ ctx.cwd,
906
+ agents,
907
+ t.agent,
908
+ t.task,
909
+ t.cwd,
910
+ undefined,
911
+ signal,
912
+ (partial) => {
913
+ if (partial.details?.results[0]) {
914
+ allResults[index] = partial.details.results[0];
915
+ emitParallelUpdate();
916
+ }
917
+ },
918
+ makeDetails("parallel"),
919
+ );
920
+ allResults[index] = result;
921
+ emitParallelUpdate();
922
+ return result;
923
+ },
924
+ );
925
+
926
+ const successCount = results.filter(
927
+ (r) => !isFailedResult(r),
928
+ ).length;
929
+ const summaries = results.map((r) => {
930
+ const output = truncateParallelOutput(getResultOutput(r));
931
+ const status = isFailedResult(r)
932
+ ? `failed${r.stopReason && r.stopReason !== "end" ? ` (${r.stopReason})` : ""}`
933
+ : "completed";
934
+ return `### [${r.agent}] ${status}\n\n${output}`;
935
+ });
936
+ return {
937
+ content: [
938
+ {
939
+ type: "text",
940
+ text: `Parallel: ${successCount}/${results.length} succeeded\n\n${summaries.join("\n\n---\n\n")}`,
941
+ },
942
+ ],
943
+ details: makeDetails("parallel")(results),
944
+ };
945
+ }
946
+
947
+ // ---- Single mode ----
948
+ if (params.agent && params.task) {
949
+ const result = await runSingleAgent(
950
+ ctx.cwd,
951
+ agents,
952
+ params.agent,
953
+ params.task,
954
+ params.cwd,
955
+ undefined,
956
+ signal,
957
+ onUpdate,
958
+ makeDetails("single"),
959
+ );
960
+ const isError = isFailedResult(result);
961
+ if (isError) {
962
+ const errorMsg = getResultOutput(result);
963
+ return {
964
+ content: [
965
+ {
966
+ type: "text",
967
+ text: `Agent ${result.stopReason || "failed"}: ${errorMsg}`,
968
+ },
969
+ ],
970
+ details: makeDetails("single")([result]),
971
+ isError: true,
972
+ };
973
+ }
974
+ return {
975
+ content: [
976
+ {
977
+ type: "text",
978
+ text: getFinalOutput(result.messages) || "(no output)",
979
+ },
980
+ ],
981
+ details: makeDetails("single")([result]),
982
+ };
983
+ }
984
+
985
+ const formatted = formatAgentList(agents, 10);
986
+ const available = formatted.text || "none";
987
+ return {
988
+ content: [
989
+ {
990
+ type: "text",
991
+ text: `Invalid parameters. Available agents: ${available}`,
992
+ },
993
+ ],
994
+ details: makeDetails("single")([]),
995
+ };
996
+ },
997
+
998
+ // -----------------------------------------------------------------------
999
+ // TUI rendering
1000
+ // -----------------------------------------------------------------------
1001
+
1002
+ renderCall(args, theme, _context) {
1003
+ const scope: AgentScope = args.agentScope ?? "user";
1004
+ if (args.chain && args.chain.length > 0) {
1005
+ let text =
1006
+ theme.fg("toolTitle", theme.bold("subagent ")) +
1007
+ theme.fg("accent", `chain (${args.chain.length} steps)`) +
1008
+ theme.fg("muted", ` [${scope}]`);
1009
+ for (let i = 0; i < Math.min(args.chain.length, 3); i++) {
1010
+ const step = args.chain[i];
1011
+ const cleanTask = step.task.replace(/\{previous\}/g, "").trim();
1012
+ const preview =
1013
+ cleanTask.length > 40
1014
+ ? `${cleanTask.slice(0, 40)}...`
1015
+ : cleanTask;
1016
+ text +=
1017
+ "\n " +
1018
+ theme.fg("muted", `${i + 1}.`) +
1019
+ " " +
1020
+ theme.fg("accent", step.agent) +
1021
+ theme.fg("dim", ` ${preview}`);
1022
+ }
1023
+ if (args.chain.length > 3)
1024
+ text += `\n ${theme.fg("muted", `... +${args.chain.length - 3} more`)}`;
1025
+ return new Text(text, 0, 0);
1026
+ }
1027
+ if (args.tasks && args.tasks.length > 0) {
1028
+ let text =
1029
+ theme.fg("toolTitle", theme.bold("subagent ")) +
1030
+ theme.fg("accent", `parallel (${args.tasks.length} tasks)`) +
1031
+ theme.fg("muted", ` [${scope}]`);
1032
+ for (const t of args.tasks.slice(0, 3)) {
1033
+ const preview =
1034
+ t.task.length > 40 ? `${t.task.slice(0, 40)}...` : t.task;
1035
+ text += `\n ${theme.fg("accent", t.agent)}${theme.fg("dim", ` ${preview}`)}`;
1036
+ }
1037
+ if (args.tasks.length > 3)
1038
+ text += `\n ${theme.fg("muted", `... +${args.tasks.length - 3} more`)}`;
1039
+ return new Text(text, 0, 0);
1040
+ }
1041
+ const agentName = args.agent || "...";
1042
+ const preview = args.task
1043
+ ? args.task.length > 60
1044
+ ? `${args.task.slice(0, 60)}...`
1045
+ : args.task
1046
+ : "...";
1047
+ let text =
1048
+ theme.fg("toolTitle", theme.bold("subagent ")) +
1049
+ theme.fg("accent", agentName) +
1050
+ theme.fg("muted", ` [${scope}]`);
1051
+ text += `\n ${theme.fg("dim", preview)}`;
1052
+ return new Text(text, 0, 0);
1053
+ },
1054
+
1055
+ renderResult(result, { expanded }, theme, _context) {
1056
+ const details = result.details as SubagentDetails | undefined;
1057
+ if (!details || details.results.length === 0) {
1058
+ const text = result.content[0];
1059
+ return new Text(
1060
+ text?.type === "text" ? text.text : "(no output)",
1061
+ 0,
1062
+ 0,
1063
+ );
1064
+ }
1065
+
1066
+ const mdTheme = getMarkdownTheme();
1067
+
1068
+ const renderDisplayItems = (items: DisplayItem[], limit?: number) => {
1069
+ const toShow = limit ? items.slice(-limit) : items;
1070
+ const skipped = limit && items.length > limit ? items.length - limit : 0;
1071
+ let text = "";
1072
+ if (skipped > 0)
1073
+ text += theme.fg("muted", `... ${skipped} earlier items\n`);
1074
+ for (const item of toShow) {
1075
+ if (item.type === "text") {
1076
+ const preview = expanded
1077
+ ? item.text
1078
+ : item.text.split("\n").slice(0, 3).join("\n");
1079
+ text += `${theme.fg("toolOutput", preview)}\n`;
1080
+ } else {
1081
+ text += `${theme.fg("muted", "\u2192 ") + formatToolCall(item.name, item.args, theme.fg.bind(theme))}\n`;
1082
+ }
1083
+ }
1084
+ return text.trimEnd();
1085
+ };
1086
+
1087
+ // --- Single mode ---
1088
+ if (details.mode === "single" && details.results.length === 1) {
1089
+ const r = details.results[0];
1090
+ const isError = isFailedResult(r);
1091
+ const icon = isError
1092
+ ? theme.fg("error", "\u2717")
1093
+ : theme.fg("success", "\u2713");
1094
+ const displayItems = getDisplayItems(r.messages);
1095
+
1096
+ if (expanded) {
1097
+ const container = new Container();
1098
+ let header = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
1099
+ if (isError && r.stopReason)
1100
+ header += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
1101
+ container.addChild(new Text(header, 0, 0));
1102
+ if (isError && r.errorMessage)
1103
+ container.addChild(
1104
+ new Text(theme.fg("error", `Error: ${r.errorMessage}`), 0, 0),
1105
+ );
1106
+ container.addChild(new Spacer(1));
1107
+ container.addChild(new Text(theme.fg("muted", "\u2500\u2500\u2500 Task \u2500\u2500\u2500"), 0, 0));
1108
+ container.addChild(new Text(theme.fg("dim", r.task), 0, 0));
1109
+
1110
+ // Show tool calls
1111
+ const toolCalls = displayItems.filter(
1112
+ (i) => i.type === "toolCall",
1113
+ );
1114
+ if (toolCalls.length > 0) {
1115
+ container.addChild(new Spacer(1));
1116
+ container.addChild(
1117
+ new Text(
1118
+ theme.fg("muted", "\u2500\u2500\u2500 Tool Calls \u2500\u2500\u2500"),
1119
+ 0,
1120
+ 0,
1121
+ ),
1122
+ );
1123
+ for (const item of toolCalls) {
1124
+ container.addChild(
1125
+ new Text(
1126
+ theme.fg("muted", "\u2192 ") +
1127
+ formatToolCall(
1128
+ item.name,
1129
+ item.args,
1130
+ theme.fg.bind(theme),
1131
+ ),
1132
+ 0,
1133
+ 0,
1134
+ ),
1135
+ );
1136
+ }
1137
+ }
1138
+
1139
+ // Show structured output if available
1140
+ if (r.structuredOutput && Object.keys(r.structuredOutput).length > 0) {
1141
+ container.addChild(new Spacer(1));
1142
+ container.addChild(
1143
+ new Text(
1144
+ theme.fg("muted", "\u2500\u2500\u2500 Structured Output \u2500\u2500\u2500"),
1145
+ 0,
1146
+ 0,
1147
+ ),
1148
+ );
1149
+ container.addChild(
1150
+ new Text(
1151
+ theme.fg("toolOutput", JSON.stringify(r.structuredOutput, null, 2)),
1152
+ 0,
1153
+ 0,
1154
+ ),
1155
+ );
1156
+ }
1157
+
1158
+ // Show final output
1159
+ const finalOutput = getFinalOutput(r.messages);
1160
+ if (finalOutput) {
1161
+ container.addChild(new Spacer(1));
1162
+ container.addChild(
1163
+ new Text(theme.fg("muted", "\u2500\u2500\u2500 Output \u2500\u2500\u2500"), 0, 0),
1164
+ );
1165
+ container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
1166
+ }
1167
+
1168
+ const usageStr = formatUsageStats(r.usage, r.model);
1169
+ if (usageStr) {
1170
+ container.addChild(new Spacer(1));
1171
+ container.addChild(new Text(theme.fg("dim", usageStr), 0, 0));
1172
+ }
1173
+ return container;
1174
+ }
1175
+
1176
+ let text = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
1177
+ if (isError && r.stopReason) text += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
1178
+ if (isError && r.errorMessage)
1179
+ text += `\n${theme.fg("error", `Error: ${r.errorMessage}`)}`;
1180
+ else if (displayItems.length === 0)
1181
+ text += `\n${theme.fg("muted", "(no output)")}`;
1182
+ else {
1183
+ text += `\n${renderDisplayItems(displayItems, COLLAPSED_ITEM_COUNT)}`;
1184
+ if (displayItems.length > COLLAPSED_ITEM_COUNT)
1185
+ text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
1186
+ }
1187
+ const usageStr = formatUsageStats(r.usage, r.model);
1188
+ if (usageStr) text += `\n${theme.fg("dim", usageStr)}`;
1189
+ return new Text(text, 0, 0);
1190
+ }
1191
+
1192
+ const aggregateUsage = (results: SingleResult[]) => {
1193
+ const total = {
1194
+ input: 0,
1195
+ output: 0,
1196
+ cacheRead: 0,
1197
+ cacheWrite: 0,
1198
+ cost: 0,
1199
+ turns: 0,
1200
+ };
1201
+ for (const r of results) {
1202
+ total.input += r.usage.input;
1203
+ total.output += r.usage.output;
1204
+ total.cacheRead += r.usage.cacheRead;
1205
+ total.cacheWrite += r.usage.cacheWrite;
1206
+ total.cost += r.usage.cost;
1207
+ total.turns += r.usage.turns;
1208
+ }
1209
+ return total;
1210
+ };
1211
+
1212
+ // --- Chain mode ---
1213
+ if (details.mode === "chain") {
1214
+ const successCount = details.results.filter(
1215
+ (r) => r.exitCode === 0,
1216
+ ).length;
1217
+ const icon =
1218
+ successCount === details.results.length
1219
+ ? theme.fg("success", "\u2713")
1220
+ : theme.fg("error", "\u2717");
1221
+
1222
+ if (expanded) {
1223
+ const container = new Container();
1224
+ container.addChild(
1225
+ new Text(
1226
+ icon +
1227
+ " " +
1228
+ theme.fg("toolTitle", theme.bold("chain ")) +
1229
+ theme.fg("accent", `${successCount}/${details.results.length} steps`),
1230
+ 0,
1231
+ 0,
1232
+ ),
1233
+ );
1234
+
1235
+ for (const r of details.results) {
1236
+ const rIcon =
1237
+ r.exitCode === 0
1238
+ ? theme.fg("success", "\u2713")
1239
+ : theme.fg("error", "\u2717");
1240
+ const displayItems = getDisplayItems(r.messages);
1241
+ const finalOutput = getFinalOutput(r.messages);
1242
+
1243
+ container.addChild(new Spacer(1));
1244
+ container.addChild(
1245
+ new Text(
1246
+ `${theme.fg("muted", `\u2500\u2500\u2500 Step ${r.step}: `) + theme.fg("accent", r.agent)} ${rIcon}`,
1247
+ 0,
1248
+ 0,
1249
+ ),
1250
+ );
1251
+ container.addChild(
1252
+ new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0),
1253
+ );
1254
+
1255
+ for (const item of displayItems) {
1256
+ if (item.type === "toolCall") {
1257
+ container.addChild(
1258
+ new Text(
1259
+ theme.fg("muted", "\u2192 ") +
1260
+ formatToolCall(
1261
+ item.name,
1262
+ item.args,
1263
+ theme.fg.bind(theme),
1264
+ ),
1265
+ 0,
1266
+ 0,
1267
+ ),
1268
+ );
1269
+ }
1270
+ }
1271
+
1272
+ if (finalOutput) {
1273
+ container.addChild(new Spacer(1));
1274
+ container.addChild(
1275
+ new Markdown(finalOutput.trim(), 0, 0, mdTheme),
1276
+ );
1277
+ }
1278
+
1279
+ const stepUsage = formatUsageStats(r.usage, r.model);
1280
+ if (stepUsage)
1281
+ container.addChild(new Text(theme.fg("dim", stepUsage), 0, 0));
1282
+ }
1283
+
1284
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1285
+ if (usageStr) {
1286
+ container.addChild(new Spacer(1));
1287
+ container.addChild(
1288
+ new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0),
1289
+ );
1290
+ }
1291
+ return container;
1292
+ }
1293
+
1294
+ let text =
1295
+ icon +
1296
+ " " +
1297
+ theme.fg("toolTitle", theme.bold("chain ")) +
1298
+ theme.fg("accent", `${successCount}/${details.results.length} steps`);
1299
+ for (const r of details.results) {
1300
+ const rIcon =
1301
+ r.exitCode === 0
1302
+ ? theme.fg("success", "\u2713")
1303
+ : theme.fg("error", "\u2717");
1304
+ const displayItems = getDisplayItems(r.messages);
1305
+ text += `\n\n${theme.fg("muted", `\u2500\u2500\u2500 Step ${r.step}: `)}${theme.fg("accent", r.agent)} ${rIcon}`;
1306
+ if (displayItems.length === 0)
1307
+ text += `\n${theme.fg("muted", "(no output)")}`;
1308
+ else text += `\n${renderDisplayItems(displayItems, 5)}`;
1309
+ }
1310
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1311
+ if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
1312
+ text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
1313
+ return new Text(text, 0, 0);
1314
+ }
1315
+
1316
+ // --- Parallel mode ---
1317
+ if (details.mode === "parallel") {
1318
+ const running = details.results.filter(
1319
+ (r) => r.exitCode === -1,
1320
+ ).length;
1321
+ const successCount = details.results.filter(
1322
+ (r) => r.exitCode !== -1 && !isFailedResult(r),
1323
+ ).length;
1324
+ const failCount = details.results.filter(
1325
+ (r) => r.exitCode !== -1 && isFailedResult(r),
1326
+ ).length;
1327
+ const isRunning = running > 0;
1328
+ const icon = isRunning
1329
+ ? theme.fg("warning", "\u23F3")
1330
+ : failCount > 0
1331
+ ? theme.fg("warning", "\u25D0")
1332
+ : theme.fg("success", "\u2713");
1333
+ const status = isRunning
1334
+ ? `${successCount + failCount}/${details.results.length} done, ${running} running`
1335
+ : `${successCount}/${details.results.length} tasks`;
1336
+
1337
+ if (expanded && !isRunning) {
1338
+ const container = new Container();
1339
+ container.addChild(
1340
+ new Text(
1341
+ `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`,
1342
+ 0,
1343
+ 0,
1344
+ ),
1345
+ );
1346
+
1347
+ for (const r of details.results) {
1348
+ const rIcon = isFailedResult(r)
1349
+ ? theme.fg("error", "\u2717")
1350
+ : theme.fg("success", "\u2713");
1351
+ const displayItems = getDisplayItems(r.messages);
1352
+ const finalOutput = getFinalOutput(r.messages);
1353
+
1354
+ container.addChild(new Spacer(1));
1355
+ container.addChild(
1356
+ new Text(
1357
+ `${theme.fg("muted", "\u2500\u2500\u2500 ") + theme.fg("accent", r.agent)} ${rIcon}`,
1358
+ 0,
1359
+ 0,
1360
+ ),
1361
+ );
1362
+ container.addChild(
1363
+ new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0),
1364
+ );
1365
+
1366
+ for (const item of displayItems) {
1367
+ if (item.type === "toolCall") {
1368
+ container.addChild(
1369
+ new Text(
1370
+ theme.fg("muted", "\u2192 ") +
1371
+ formatToolCall(
1372
+ item.name,
1373
+ item.args,
1374
+ theme.fg.bind(theme),
1375
+ ),
1376
+ 0,
1377
+ 0,
1378
+ ),
1379
+ );
1380
+ }
1381
+ }
1382
+
1383
+ if (finalOutput) {
1384
+ container.addChild(new Spacer(1));
1385
+ container.addChild(
1386
+ new Markdown(finalOutput.trim(), 0, 0, mdTheme),
1387
+ );
1388
+ }
1389
+
1390
+ const taskUsage = formatUsageStats(r.usage, r.model);
1391
+ if (taskUsage)
1392
+ container.addChild(new Text(theme.fg("dim", taskUsage), 0, 0));
1393
+ }
1394
+
1395
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1396
+ if (usageStr) {
1397
+ container.addChild(new Spacer(1));
1398
+ container.addChild(
1399
+ new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0),
1400
+ );
1401
+ }
1402
+ return container;
1403
+ }
1404
+
1405
+ let text = `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`;
1406
+ for (const r of details.results) {
1407
+ const rIcon =
1408
+ r.exitCode === -1
1409
+ ? theme.fg("warning", "\u23F3")
1410
+ : isFailedResult(r)
1411
+ ? theme.fg("error", "\u2717")
1412
+ : theme.fg("success", "\u2713");
1413
+ const displayItems = getDisplayItems(r.messages);
1414
+ text += `\n\n${theme.fg("muted", "\u2500\u2500\u2500 ")}${theme.fg("accent", r.agent)} ${rIcon}`;
1415
+ if (displayItems.length === 0)
1416
+ text += `\n${theme.fg("muted", r.exitCode === -1 ? "(running...)" : "(no output)")}`;
1417
+ else text += `\n${renderDisplayItems(displayItems, 5)}`;
1418
+ }
1419
+ if (!isRunning) {
1420
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1421
+ if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
1422
+ }
1423
+ if (!expanded)
1424
+ text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
1425
+ return new Text(text, 0, 0);
1426
+ }
1427
+
1428
+ const text = result.content[0];
1429
+ return new Text(
1430
+ text?.type === "text" ? text.text : "(no output)",
1431
+ 0,
1432
+ 0,
1433
+ );
1434
+ },
1435
+ });
1436
+ }