@narumitw/pi-subagents 0.12.0 → 0.13.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/README.md +3 -2
- package/package.json +1 -1
- package/src/config-ui.ts +271 -0
- package/src/execution.ts +406 -0
- package/src/params.ts +59 -0
- package/src/render.ts +521 -0
- package/src/runner.ts +368 -0
- package/src/settings.ts +120 -0
- package/src/subagents.ts +23 -1679
package/src/params.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { StringEnum } from "@earendil-works/pi-ai";
|
|
2
|
+
import { Type, type Static } from "typebox";
|
|
3
|
+
import { THINKING_LEVELS } from "./agents.js";
|
|
4
|
+
|
|
5
|
+
const TimeoutMs = Type.Number({
|
|
6
|
+
description:
|
|
7
|
+
"Hard timeout in milliseconds for each subagent subprocess. Defaults to PI_SUBAGENT_TIMEOUT_MS or 600000.",
|
|
8
|
+
minimum: 1,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const ThinkingLevelSchema = StringEnum(THINKING_LEVELS, {
|
|
12
|
+
description: "Pi thinking level for the subagent process: off, minimal, low, medium, high, or xhigh.",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const TaskItem = Type.Object({
|
|
16
|
+
agent: Type.String({ description: "Name of the agent to invoke" }),
|
|
17
|
+
task: Type.String({ description: "Task to delegate to the agent" }),
|
|
18
|
+
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
|
|
19
|
+
timeoutMs: Type.Optional(TimeoutMs),
|
|
20
|
+
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const ChainItem = Type.Object({
|
|
24
|
+
agent: Type.String({ description: "Name of the agent to invoke" }),
|
|
25
|
+
task: Type.String({ description: "Task with optional {previous} placeholder for prior output" }),
|
|
26
|
+
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
|
|
27
|
+
timeoutMs: Type.Optional(TimeoutMs),
|
|
28
|
+
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const AggregatorItem = Type.Object({
|
|
32
|
+
agent: Type.String({ description: "Name of the fan-in agent to invoke after parallel tasks complete" }),
|
|
33
|
+
task: Type.String({ description: "Fan-in task. Use {previous} to include all parallel outputs." }),
|
|
34
|
+
cwd: Type.Optional(Type.String({ description: "Working directory for the aggregator process" })),
|
|
35
|
+
timeoutMs: Type.Optional(TimeoutMs),
|
|
36
|
+
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const AgentScopeSchema = StringEnum(["user", "project", "both"] as const, {
|
|
40
|
+
description: 'Which agent directories to use. Default: "user". Use "both" to include project-local agents.',
|
|
41
|
+
default: "user",
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export const SubagentParams = Type.Object({
|
|
45
|
+
agent: Type.Optional(Type.String({ description: "Name of the agent to invoke (for single mode)" })),
|
|
46
|
+
task: Type.Optional(Type.String({ description: "Task to delegate (for single mode)" })),
|
|
47
|
+
tasks: Type.Optional(Type.Array(TaskItem, { description: "Array of {agent, task} for parallel execution" })),
|
|
48
|
+
chain: Type.Optional(Type.Array(ChainItem, { description: "Array of {agent, task} for sequential execution" })),
|
|
49
|
+
aggregator: Type.Optional(AggregatorItem),
|
|
50
|
+
agentScope: Type.Optional(AgentScopeSchema),
|
|
51
|
+
confirmProjectAgents: Type.Optional(
|
|
52
|
+
Type.Boolean({ description: "Prompt before running project-local agents. Default: true.", default: true }),
|
|
53
|
+
),
|
|
54
|
+
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process (single mode)" })),
|
|
55
|
+
timeoutMs: Type.Optional(TimeoutMs),
|
|
56
|
+
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export type SubagentParams = Static<typeof SubagentParams>;
|
package/src/render.ts
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import * as os from "node:os";
|
|
2
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
3
|
+
import type { Message } from "@earendil-works/pi-ai";
|
|
4
|
+
import {
|
|
5
|
+
getMarkdownTheme,
|
|
6
|
+
type Theme,
|
|
7
|
+
type ThemeColor,
|
|
8
|
+
type ToolRenderResultOptions,
|
|
9
|
+
} from "@earendil-works/pi-coding-agent";
|
|
10
|
+
import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
|
|
11
|
+
import type { AgentScope, SubagentThinkingLevel } from "./agents.js";
|
|
12
|
+
import type { SubagentParams } from "./params.js";
|
|
13
|
+
import {
|
|
14
|
+
getResultFinalOutput,
|
|
15
|
+
type SingleResult,
|
|
16
|
+
type SubagentDetails,
|
|
17
|
+
} from "./runner.js";
|
|
18
|
+
|
|
19
|
+
const COLLAPSED_ITEM_COUNT = 10;
|
|
20
|
+
|
|
21
|
+
export function formatTokens(count: number): string {
|
|
22
|
+
if (count < 1000) return count.toString();
|
|
23
|
+
if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
|
|
24
|
+
if (count < 1000000) return `${Math.round(count / 1000)}k`;
|
|
25
|
+
return `${(count / 1000000).toFixed(1)}M`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function formatUsageStats(
|
|
29
|
+
usage: {
|
|
30
|
+
input: number;
|
|
31
|
+
output: number;
|
|
32
|
+
cacheRead: number;
|
|
33
|
+
cacheWrite: number;
|
|
34
|
+
cost: number;
|
|
35
|
+
contextTokens?: number;
|
|
36
|
+
turns?: number;
|
|
37
|
+
},
|
|
38
|
+
model?: string,
|
|
39
|
+
thinkingLevel?: SubagentThinkingLevel,
|
|
40
|
+
): string {
|
|
41
|
+
const parts: string[] = [];
|
|
42
|
+
if (usage.turns) parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
|
|
43
|
+
if (usage.input) parts.push(`↑${formatTokens(usage.input)}`);
|
|
44
|
+
if (usage.output) parts.push(`↓${formatTokens(usage.output)}`);
|
|
45
|
+
if (usage.cacheRead) parts.push(`R${formatTokens(usage.cacheRead)}`);
|
|
46
|
+
if (usage.cacheWrite) parts.push(`W${formatTokens(usage.cacheWrite)}`);
|
|
47
|
+
if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
|
|
48
|
+
if (usage.contextTokens && usage.contextTokens > 0) {
|
|
49
|
+
parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
|
|
50
|
+
}
|
|
51
|
+
if (model) parts.push(model);
|
|
52
|
+
if (thinkingLevel) parts.push(`thinking:${thinkingLevel}`);
|
|
53
|
+
return parts.join(" ");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function formatToolCall(
|
|
57
|
+
toolName: string,
|
|
58
|
+
args: Record<string, unknown>,
|
|
59
|
+
themeFg: (color: ThemeColor, text: string) => string,
|
|
60
|
+
): string {
|
|
61
|
+
const shortenPath = (p: string) => {
|
|
62
|
+
const home = os.homedir();
|
|
63
|
+
return p.startsWith(home) ? `~${p.slice(home.length)}` : p;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
switch (toolName) {
|
|
67
|
+
case "bash": {
|
|
68
|
+
const command = (args.command as string) || "...";
|
|
69
|
+
const preview = command.length > 60 ? `${command.slice(0, 60)}...` : command;
|
|
70
|
+
return themeFg("muted", "$ ") + themeFg("toolOutput", preview);
|
|
71
|
+
}
|
|
72
|
+
case "read": {
|
|
73
|
+
const rawPath = (args.file_path || args.path || "...") as string;
|
|
74
|
+
const filePath = shortenPath(rawPath);
|
|
75
|
+
const offset = args.offset as number | undefined;
|
|
76
|
+
const limit = args.limit as number | undefined;
|
|
77
|
+
let text = themeFg("accent", filePath);
|
|
78
|
+
if (offset !== undefined || limit !== undefined) {
|
|
79
|
+
const startLine = offset ?? 1;
|
|
80
|
+
const endLine = limit !== undefined ? startLine + limit - 1 : "";
|
|
81
|
+
text += themeFg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
|
|
82
|
+
}
|
|
83
|
+
return themeFg("muted", "read ") + text;
|
|
84
|
+
}
|
|
85
|
+
case "write": {
|
|
86
|
+
const rawPath = (args.file_path || args.path || "...") as string;
|
|
87
|
+
const filePath = shortenPath(rawPath);
|
|
88
|
+
const content = (args.content || "") as string;
|
|
89
|
+
const lines = content.split("\n").length;
|
|
90
|
+
let text = themeFg("muted", "write ") + themeFg("accent", filePath);
|
|
91
|
+
if (lines > 1) text += themeFg("dim", ` (${lines} lines)`);
|
|
92
|
+
return text;
|
|
93
|
+
}
|
|
94
|
+
case "edit": {
|
|
95
|
+
const rawPath = (args.file_path || args.path || "...") as string;
|
|
96
|
+
return themeFg("muted", "edit ") + themeFg("accent", shortenPath(rawPath));
|
|
97
|
+
}
|
|
98
|
+
case "ls": {
|
|
99
|
+
const rawPath = (args.path || ".") as string;
|
|
100
|
+
return themeFg("muted", "ls ") + themeFg("accent", shortenPath(rawPath));
|
|
101
|
+
}
|
|
102
|
+
case "find": {
|
|
103
|
+
const pattern = (args.pattern || "*") as string;
|
|
104
|
+
const rawPath = (args.path || ".") as string;
|
|
105
|
+
return themeFg("muted", "find ") + themeFg("accent", pattern) + themeFg("dim", ` in ${shortenPath(rawPath)}`);
|
|
106
|
+
}
|
|
107
|
+
case "grep": {
|
|
108
|
+
const pattern = (args.pattern || "") as string;
|
|
109
|
+
const rawPath = (args.path || ".") as string;
|
|
110
|
+
return (
|
|
111
|
+
themeFg("muted", "grep ") +
|
|
112
|
+
themeFg("accent", `/${pattern}/`) +
|
|
113
|
+
themeFg("dim", ` in ${shortenPath(rawPath)}`)
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
default: {
|
|
117
|
+
const argsStr = JSON.stringify(args);
|
|
118
|
+
const preview = argsStr.length > 50 ? `${argsStr.slice(0, 50)}...` : argsStr;
|
|
119
|
+
return themeFg("accent", toolName) + themeFg("dim", ` ${preview}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
type DisplayItem =
|
|
125
|
+
| { type: "text"; text: string }
|
|
126
|
+
| { type: "toolCall"; name: string; args: Record<string, unknown> };
|
|
127
|
+
|
|
128
|
+
function getDisplayItems(messages: Message[]): DisplayItem[] {
|
|
129
|
+
const items: DisplayItem[] = [];
|
|
130
|
+
for (const msg of messages) {
|
|
131
|
+
if (msg.role === "assistant") {
|
|
132
|
+
for (const part of msg.content) {
|
|
133
|
+
if (part.type === "text") items.push({ type: "text", text: part.text });
|
|
134
|
+
else if (part.type === "toolCall") items.push({ type: "toolCall", name: part.name, args: part.arguments });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return items;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function renderSubagentCall(args: SubagentParams, theme: Theme) {
|
|
142
|
+
const scope: AgentScope = args.agentScope ?? "user";
|
|
143
|
+
if (args.chain && args.chain.length > 0) {
|
|
144
|
+
let text =
|
|
145
|
+
theme.fg("toolTitle", theme.bold("subagent ")) +
|
|
146
|
+
theme.fg("accent", `chain (${args.chain.length} steps)`) +
|
|
147
|
+
theme.fg("muted", ` [${scope}]`);
|
|
148
|
+
for (let i = 0; i < Math.min(args.chain.length, 3); i++) {
|
|
149
|
+
const step = args.chain[i];
|
|
150
|
+
// Clean up {previous} placeholder for display
|
|
151
|
+
const cleanTask = step.task.replace(/\{previous\}/g, "").trim();
|
|
152
|
+
const preview = cleanTask.length > 40 ? `${cleanTask.slice(0, 40)}...` : cleanTask;
|
|
153
|
+
text +=
|
|
154
|
+
"\n " +
|
|
155
|
+
theme.fg("muted", `${i + 1}.`) +
|
|
156
|
+
" " +
|
|
157
|
+
theme.fg("accent", step.agent) +
|
|
158
|
+
theme.fg("dim", ` ${preview}`);
|
|
159
|
+
}
|
|
160
|
+
if (args.chain.length > 3) text += `\n ${theme.fg("muted", `... +${args.chain.length - 3} more`)}`;
|
|
161
|
+
return new Text(text, 0, 0);
|
|
162
|
+
}
|
|
163
|
+
if (args.tasks && args.tasks.length > 0) {
|
|
164
|
+
let text =
|
|
165
|
+
theme.fg("toolTitle", theme.bold("subagent ")) +
|
|
166
|
+
theme.fg("accent", `parallel (${args.tasks.length} tasks)`) +
|
|
167
|
+
theme.fg("muted", ` [${scope}]`);
|
|
168
|
+
for (const t of args.tasks.slice(0, 3)) {
|
|
169
|
+
const preview = t.task.length > 40 ? `${t.task.slice(0, 40)}...` : t.task;
|
|
170
|
+
text += `\n ${theme.fg("accent", t.agent)}${theme.fg("dim", ` ${preview}`)}`;
|
|
171
|
+
}
|
|
172
|
+
if (args.tasks.length > 3) text += `\n ${theme.fg("muted", `... +${args.tasks.length - 3} more`)}`;
|
|
173
|
+
if (args.aggregator) {
|
|
174
|
+
const preview =
|
|
175
|
+
args.aggregator.task.length > 40 ? `${args.aggregator.task.slice(0, 40)}...` : args.aggregator.task;
|
|
176
|
+
text += `\n ${theme.fg("muted", "fan-in → ")}${theme.fg("accent", args.aggregator.agent)}${theme.fg(
|
|
177
|
+
"dim",
|
|
178
|
+
` ${preview}`,
|
|
179
|
+
)}`;
|
|
180
|
+
}
|
|
181
|
+
return new Text(text, 0, 0);
|
|
182
|
+
}
|
|
183
|
+
const agentName = args.agent || "...";
|
|
184
|
+
const preview = args.task ? (args.task.length > 60 ? `${args.task.slice(0, 60)}...` : args.task) : "...";
|
|
185
|
+
let text =
|
|
186
|
+
theme.fg("toolTitle", theme.bold("subagent ")) +
|
|
187
|
+
theme.fg("accent", agentName) +
|
|
188
|
+
theme.fg("muted", ` [${scope}]`);
|
|
189
|
+
text += `\n ${theme.fg("dim", preview)}`;
|
|
190
|
+
return new Text(text, 0, 0);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function renderSubagentResult(
|
|
194
|
+
result: AgentToolResult<SubagentDetails>,
|
|
195
|
+
{ expanded }: ToolRenderResultOptions,
|
|
196
|
+
theme: Theme,
|
|
197
|
+
) {
|
|
198
|
+
const details = result.details as SubagentDetails | undefined;
|
|
199
|
+
if (!details || details.results.length === 0) {
|
|
200
|
+
const text = result.content[0];
|
|
201
|
+
return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const mdTheme = getMarkdownTheme();
|
|
205
|
+
|
|
206
|
+
const renderDisplayItems = (items: DisplayItem[], limit?: number) => {
|
|
207
|
+
const toShow = limit ? items.slice(-limit) : items;
|
|
208
|
+
const skipped = limit && items.length > limit ? items.length - limit : 0;
|
|
209
|
+
let text = "";
|
|
210
|
+
if (skipped > 0) text += theme.fg("muted", `... ${skipped} earlier items\n`);
|
|
211
|
+
for (const item of toShow) {
|
|
212
|
+
if (item.type === "text") {
|
|
213
|
+
const preview = expanded ? item.text : item.text.split("\n").slice(0, 3).join("\n");
|
|
214
|
+
text += `${theme.fg("toolOutput", preview)}\n`;
|
|
215
|
+
} else {
|
|
216
|
+
text += `${theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme))}\n`;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return text.trimEnd();
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
if (details.mode === "single" && details.results.length === 1) {
|
|
223
|
+
const r = details.results[0];
|
|
224
|
+
const isError = r.exitCode !== 0 || r.stopReason === "error" || r.stopReason === "aborted";
|
|
225
|
+
const icon = isError ? theme.fg("error", "✗") : theme.fg("success", "✓");
|
|
226
|
+
const displayItems = getDisplayItems(r.messages);
|
|
227
|
+
const finalOutput = getResultFinalOutput(r);
|
|
228
|
+
|
|
229
|
+
if (expanded) {
|
|
230
|
+
const container = new Container();
|
|
231
|
+
let header = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
|
|
232
|
+
if (isError && r.stopReason) header += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
|
|
233
|
+
container.addChild(new Text(header, 0, 0));
|
|
234
|
+
if (isError && r.errorMessage)
|
|
235
|
+
container.addChild(new Text(theme.fg("error", `Error: ${r.errorMessage}`), 0, 0));
|
|
236
|
+
container.addChild(new Spacer(1));
|
|
237
|
+
container.addChild(new Text(theme.fg("muted", "─── Task ───"), 0, 0));
|
|
238
|
+
container.addChild(new Text(theme.fg("dim", r.task), 0, 0));
|
|
239
|
+
container.addChild(new Spacer(1));
|
|
240
|
+
container.addChild(new Text(theme.fg("muted", "─── Output ───"), 0, 0));
|
|
241
|
+
if (displayItems.length === 0 && !finalOutput) {
|
|
242
|
+
container.addChild(new Text(theme.fg("muted", "(no output)"), 0, 0));
|
|
243
|
+
} else {
|
|
244
|
+
for (const item of displayItems) {
|
|
245
|
+
if (item.type === "toolCall")
|
|
246
|
+
container.addChild(
|
|
247
|
+
new Text(
|
|
248
|
+
theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
|
|
249
|
+
0,
|
|
250
|
+
0,
|
|
251
|
+
),
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
if (finalOutput) {
|
|
255
|
+
container.addChild(new Spacer(1));
|
|
256
|
+
container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const usageStr = formatUsageStats(r.usage, r.model, r.thinkingLevel);
|
|
260
|
+
if (usageStr) {
|
|
261
|
+
container.addChild(new Spacer(1));
|
|
262
|
+
container.addChild(new Text(theme.fg("dim", usageStr), 0, 0));
|
|
263
|
+
}
|
|
264
|
+
return container;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
let text = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
|
|
268
|
+
if (isError && r.stopReason) text += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
|
|
269
|
+
if (isError && r.errorMessage) text += `\n${theme.fg("error", `Error: ${r.errorMessage}`)}`;
|
|
270
|
+
else if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
|
|
271
|
+
else {
|
|
272
|
+
text += `\n${renderDisplayItems(displayItems, COLLAPSED_ITEM_COUNT)}`;
|
|
273
|
+
if (displayItems.length > COLLAPSED_ITEM_COUNT) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
|
|
274
|
+
}
|
|
275
|
+
const usageStr = formatUsageStats(r.usage, r.model, r.thinkingLevel);
|
|
276
|
+
if (usageStr) text += `\n${theme.fg("dim", usageStr)}`;
|
|
277
|
+
return new Text(text, 0, 0);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const aggregateUsage = (results: SingleResult[]) => {
|
|
281
|
+
const total = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, turns: 0 };
|
|
282
|
+
for (const r of results) {
|
|
283
|
+
total.input += r.usage.input;
|
|
284
|
+
total.output += r.usage.output;
|
|
285
|
+
total.cacheRead += r.usage.cacheRead;
|
|
286
|
+
total.cacheWrite += r.usage.cacheWrite;
|
|
287
|
+
total.cost += r.usage.cost;
|
|
288
|
+
total.turns += r.usage.turns;
|
|
289
|
+
}
|
|
290
|
+
return total;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
if (details.mode === "chain") {
|
|
294
|
+
const successCount = details.results.filter((r) => r.exitCode === 0).length;
|
|
295
|
+
const icon = successCount === details.results.length ? theme.fg("success", "✓") : theme.fg("error", "✗");
|
|
296
|
+
|
|
297
|
+
if (expanded) {
|
|
298
|
+
const container = new Container();
|
|
299
|
+
container.addChild(
|
|
300
|
+
new Text(
|
|
301
|
+
icon +
|
|
302
|
+
" " +
|
|
303
|
+
theme.fg("toolTitle", theme.bold("chain ")) +
|
|
304
|
+
theme.fg("accent", `${successCount}/${details.results.length} steps`),
|
|
305
|
+
0,
|
|
306
|
+
0,
|
|
307
|
+
),
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
for (const r of details.results) {
|
|
311
|
+
const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
|
|
312
|
+
const displayItems = getDisplayItems(r.messages);
|
|
313
|
+
const finalOutput = getResultFinalOutput(r);
|
|
314
|
+
|
|
315
|
+
container.addChild(new Spacer(1));
|
|
316
|
+
container.addChild(
|
|
317
|
+
new Text(
|
|
318
|
+
`${theme.fg("muted", `─── Step ${r.step}: `) + theme.fg("accent", r.agent)} ${rIcon}`,
|
|
319
|
+
0,
|
|
320
|
+
0,
|
|
321
|
+
),
|
|
322
|
+
);
|
|
323
|
+
container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
|
|
324
|
+
|
|
325
|
+
// Show tool calls
|
|
326
|
+
for (const item of displayItems) {
|
|
327
|
+
if (item.type === "toolCall") {
|
|
328
|
+
container.addChild(
|
|
329
|
+
new Text(
|
|
330
|
+
theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
|
|
331
|
+
0,
|
|
332
|
+
0,
|
|
333
|
+
),
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Show final output as markdown
|
|
339
|
+
if (finalOutput) {
|
|
340
|
+
container.addChild(new Spacer(1));
|
|
341
|
+
container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const stepUsage = formatUsageStats(r.usage, r.model, r.thinkingLevel);
|
|
345
|
+
if (stepUsage) container.addChild(new Text(theme.fg("dim", stepUsage), 0, 0));
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const usageStr = formatUsageStats(aggregateUsage(details.results));
|
|
349
|
+
if (usageStr) {
|
|
350
|
+
container.addChild(new Spacer(1));
|
|
351
|
+
container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
|
|
352
|
+
}
|
|
353
|
+
return container;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Collapsed view
|
|
357
|
+
let text =
|
|
358
|
+
icon +
|
|
359
|
+
" " +
|
|
360
|
+
theme.fg("toolTitle", theme.bold("chain ")) +
|
|
361
|
+
theme.fg("accent", `${successCount}/${details.results.length} steps`);
|
|
362
|
+
for (const r of details.results) {
|
|
363
|
+
const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
|
|
364
|
+
const displayItems = getDisplayItems(r.messages);
|
|
365
|
+
text += `\n\n${theme.fg("muted", `─── Step ${r.step}: `)}${theme.fg("accent", r.agent)} ${rIcon}`;
|
|
366
|
+
if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
|
|
367
|
+
else text += `\n${renderDisplayItems(displayItems, 5)}`;
|
|
368
|
+
}
|
|
369
|
+
const usageStr = formatUsageStats(aggregateUsage(details.results));
|
|
370
|
+
if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
|
|
371
|
+
text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
|
|
372
|
+
return new Text(text, 0, 0);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (details.mode === "parallel") {
|
|
376
|
+
const running = details.results.filter((r) => r.exitCode === -1).length;
|
|
377
|
+
const successCount = details.results.filter((r) => r.exitCode === 0).length;
|
|
378
|
+
const failCount = details.results.filter((r) => r.exitCode > 0).length;
|
|
379
|
+
const aggregator = details.aggregator;
|
|
380
|
+
const aggregatorRunning = aggregator?.exitCode === -1;
|
|
381
|
+
const aggregatorFailed = aggregator ? aggregator.exitCode > 0 || aggregator.stopReason === "error" : false;
|
|
382
|
+
const isRunning = running > 0 || aggregatorRunning;
|
|
383
|
+
const icon = isRunning
|
|
384
|
+
? theme.fg("warning", "⏳")
|
|
385
|
+
: failCount > 0 || aggregatorFailed
|
|
386
|
+
? theme.fg("warning", "◐")
|
|
387
|
+
: theme.fg("success", "✓");
|
|
388
|
+
const status = isRunning
|
|
389
|
+
? aggregatorRunning
|
|
390
|
+
? `${successCount + failCount}/${details.results.length} done, fan-in running`
|
|
391
|
+
: `${successCount + failCount}/${details.results.length} done, ${running} running`
|
|
392
|
+
: aggregator
|
|
393
|
+
? `${successCount}/${details.results.length} tasks + fan-in`
|
|
394
|
+
: `${successCount}/${details.results.length} tasks`;
|
|
395
|
+
|
|
396
|
+
if (expanded && !isRunning) {
|
|
397
|
+
const container = new Container();
|
|
398
|
+
container.addChild(
|
|
399
|
+
new Text(
|
|
400
|
+
`${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`,
|
|
401
|
+
0,
|
|
402
|
+
0,
|
|
403
|
+
),
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
for (const r of details.results) {
|
|
407
|
+
const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
|
|
408
|
+
const displayItems = getDisplayItems(r.messages);
|
|
409
|
+
const finalOutput = getResultFinalOutput(r);
|
|
410
|
+
|
|
411
|
+
container.addChild(new Spacer(1));
|
|
412
|
+
container.addChild(
|
|
413
|
+
new Text(`${theme.fg("muted", "─── ") + theme.fg("accent", r.agent)} ${rIcon}`, 0, 0),
|
|
414
|
+
);
|
|
415
|
+
container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
|
|
416
|
+
|
|
417
|
+
// Show tool calls
|
|
418
|
+
for (const item of displayItems) {
|
|
419
|
+
if (item.type === "toolCall") {
|
|
420
|
+
container.addChild(
|
|
421
|
+
new Text(
|
|
422
|
+
theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
|
|
423
|
+
0,
|
|
424
|
+
0,
|
|
425
|
+
),
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Show final output as markdown
|
|
431
|
+
if (finalOutput) {
|
|
432
|
+
container.addChild(new Spacer(1));
|
|
433
|
+
container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const taskUsage = formatUsageStats(r.usage, r.model, r.thinkingLevel);
|
|
437
|
+
if (taskUsage) container.addChild(new Text(theme.fg("dim", taskUsage), 0, 0));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (aggregator) {
|
|
441
|
+
const rIcon = aggregator.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
|
|
442
|
+
const displayItems = getDisplayItems(aggregator.messages);
|
|
443
|
+
const finalOutput = getResultFinalOutput(aggregator);
|
|
444
|
+
|
|
445
|
+
container.addChild(new Spacer(1));
|
|
446
|
+
container.addChild(
|
|
447
|
+
new Text(
|
|
448
|
+
`${theme.fg("muted", "─── fan-in → ") + theme.fg("accent", aggregator.agent)} ${rIcon}`,
|
|
449
|
+
0,
|
|
450
|
+
0,
|
|
451
|
+
),
|
|
452
|
+
);
|
|
453
|
+
container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", aggregator.task), 0, 0));
|
|
454
|
+
for (const item of displayItems) {
|
|
455
|
+
if (item.type === "toolCall") {
|
|
456
|
+
container.addChild(
|
|
457
|
+
new Text(
|
|
458
|
+
theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
|
|
459
|
+
0,
|
|
460
|
+
0,
|
|
461
|
+
),
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (finalOutput) {
|
|
466
|
+
container.addChild(new Spacer(1));
|
|
467
|
+
container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
|
|
468
|
+
}
|
|
469
|
+
const fanInUsage = formatUsageStats(aggregator.usage, aggregator.model, aggregator.thinkingLevel);
|
|
470
|
+
if (fanInUsage) container.addChild(new Text(theme.fg("dim", fanInUsage), 0, 0));
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const usageResults = aggregator ? [...details.results, aggregator] : details.results;
|
|
474
|
+
const usageStr = formatUsageStats(aggregateUsage(usageResults));
|
|
475
|
+
if (usageStr) {
|
|
476
|
+
container.addChild(new Spacer(1));
|
|
477
|
+
container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
|
|
478
|
+
}
|
|
479
|
+
return container;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Collapsed view (or still running)
|
|
483
|
+
let text = `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`;
|
|
484
|
+
for (const r of details.results) {
|
|
485
|
+
const rIcon =
|
|
486
|
+
r.exitCode === -1
|
|
487
|
+
? theme.fg("warning", "⏳")
|
|
488
|
+
: r.exitCode === 0
|
|
489
|
+
? theme.fg("success", "✓")
|
|
490
|
+
: theme.fg("error", "✗");
|
|
491
|
+
const displayItems = getDisplayItems(r.messages);
|
|
492
|
+
text += `\n\n${theme.fg("muted", "─── ")}${theme.fg("accent", r.agent)} ${rIcon}`;
|
|
493
|
+
if (displayItems.length === 0)
|
|
494
|
+
text += `\n${theme.fg("muted", r.exitCode === -1 ? "(running...)" : "(no output)")}`;
|
|
495
|
+
else text += `\n${renderDisplayItems(displayItems, 5)}`;
|
|
496
|
+
}
|
|
497
|
+
if (aggregator) {
|
|
498
|
+
const rIcon =
|
|
499
|
+
aggregator.exitCode === -1
|
|
500
|
+
? theme.fg("warning", "⏳")
|
|
501
|
+
: aggregator.exitCode === 0
|
|
502
|
+
? theme.fg("success", "✓")
|
|
503
|
+
: theme.fg("error", "✗");
|
|
504
|
+
const displayItems = getDisplayItems(aggregator.messages);
|
|
505
|
+
text += `\n\n${theme.fg("muted", "─── fan-in → ")}${theme.fg("accent", aggregator.agent)} ${rIcon}`;
|
|
506
|
+
if (displayItems.length === 0)
|
|
507
|
+
text += `\n${theme.fg("muted", aggregator.exitCode === -1 ? "(running...)" : "(no output)")}`;
|
|
508
|
+
else text += `\n${renderDisplayItems(displayItems, 5)}`;
|
|
509
|
+
}
|
|
510
|
+
if (!isRunning) {
|
|
511
|
+
const usageResults = aggregator ? [...details.results, aggregator] : details.results;
|
|
512
|
+
const usageStr = formatUsageStats(aggregateUsage(usageResults));
|
|
513
|
+
if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
|
|
514
|
+
}
|
|
515
|
+
if (!expanded) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
|
|
516
|
+
return new Text(text, 0, 0);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const text = result.content[0];
|
|
520
|
+
return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
|
|
521
|
+
}
|