@pi-archimedes/subagent 1.8.3 → 2.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/LICENSE +21 -0
- package/README.md +2 -1
- package/package.json +2 -2
- package/src/compact.ts +17 -5
- package/src/expanded.ts +22 -6
- package/src/handlers.ts +16 -3
- package/src/stream.test.ts +47 -0
- package/src/stream.ts +8 -1
- package/src/types.ts +12 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Cherubini and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -5,10 +5,11 @@ Subagent dispatch with live TUI streaming and cost tracking for the [Pi coding a
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- **Single & parallel execution** — dispatch one task or fan out multiple tasks across different agents simultaneously
|
|
8
|
-
- **Live TUI streaming** — watch subagent progress in real-time with tool calls, token counts, and cost updates
|
|
8
|
+
- **Live TUI streaming** — watch subagent progress in real-time with color-coded tool calls (grey while running, green/red on completion), readable argument previews, token counts, and cost updates
|
|
9
9
|
- **Agent discovery** — auto-discovers agents from `.pi/agents/*.md` files at project, user, and global scope
|
|
10
10
|
- **Per-agent model override** — each subagent can use its own model, falling back to the parent's selection
|
|
11
11
|
- **Cost tracking** — detailed token usage (input, output, cache read/write) and cost per subagent, emitted through the core bus for the footer to consume
|
|
12
|
+
- **Trace correlation** — results expose the ephemeral child's logical Pi session UUID as optional `childSessionId` when Pi emits a valid session event
|
|
12
13
|
- **`/agents` command** — full CRUD TUI for managing agent definitions with model picker, tool picker, and cross-scope collision warnings (available via the meta package)
|
|
13
14
|
|
|
14
15
|
## Screenshots
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-archimedes/subagent",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package"
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
],
|
|
12
12
|
"main": "./src/index.ts",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@pi-archimedes/core": "
|
|
14
|
+
"@pi-archimedes/core": "2.0.0"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
17
|
"@earendil-works/pi-ai": ">=0.1.0",
|
package/src/compact.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Text } from "@earendil-works/pi-tui";
|
|
2
|
-
import type { SubagentDetails, SubagentProgress, SubagentResult, SubagentToolResult } from "./types.js";
|
|
2
|
+
import type { SubagentDetails, SubagentProgress, SubagentResult, SubagentToolCall, SubagentToolResult } from "./types.js";
|
|
3
3
|
import { formatTokens, formatDuration, formatCost, truncLine, buildStatsLine, buildAgentLabel } from "./format.js";
|
|
4
4
|
|
|
5
5
|
type Theme = { fg: (token: string, text: string) => string; bold: (text: string) => string };
|
|
@@ -14,7 +14,7 @@ interface ActivityData {
|
|
|
14
14
|
finalOutput: string | undefined;
|
|
15
15
|
status: "running" | "completed" | "failed" | undefined;
|
|
16
16
|
error: string | undefined;
|
|
17
|
-
toolCalls?: string[] | undefined;
|
|
17
|
+
toolCalls?: (SubagentToolCall | string)[] | undefined;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export function buildActivityLine(
|
|
@@ -35,7 +35,7 @@ export function buildActivityLine(
|
|
|
35
35
|
return theme.fg("error", "✗ Failed");
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
// Running: show the current tool with live duration
|
|
38
|
+
// Running: show the current tool with live duration (grey while running)
|
|
39
39
|
if (data.currentTool) {
|
|
40
40
|
const arrow = theme.fg("muted", "↳ ");
|
|
41
41
|
const argsPreview = data.currentToolArgs
|
|
@@ -44,7 +44,7 @@ export function buildActivityLine(
|
|
|
44
44
|
const durationPart = data.currentToolStartedAt
|
|
45
45
|
? " | " + formatDuration(Date.now() - data.currentToolStartedAt)
|
|
46
46
|
: "";
|
|
47
|
-
let line = theme.fg("
|
|
47
|
+
let line = theme.fg("muted", data.currentTool);
|
|
48
48
|
if (argsPreview) {
|
|
49
49
|
line += theme.fg("dim", ": " + argsPreview);
|
|
50
50
|
}
|
|
@@ -55,9 +55,21 @@ export function buildActivityLine(
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
// Running, no active tool: show the most recently completed tool call
|
|
58
|
+
// Color only the tool name green (success) or red (error)
|
|
58
59
|
if (data.toolCalls && data.toolCalls.length > 0) {
|
|
59
60
|
const lastCall = data.toolCalls[data.toolCalls.length - 1];
|
|
60
|
-
if (lastCall)
|
|
61
|
+
if (lastCall) {
|
|
62
|
+
if (typeof lastCall === "string") {
|
|
63
|
+
return theme.fg("dim", "↳ " + truncLine(lastCall, 60));
|
|
64
|
+
}
|
|
65
|
+
const color = lastCall.error ? "error" : "success";
|
|
66
|
+
const arrow = theme.fg("muted", "↳ ");
|
|
67
|
+
const name = theme.fg(color, lastCall.name);
|
|
68
|
+
const argsPart = lastCall.argsPreview
|
|
69
|
+
? theme.fg("dim", ": " + truncLine(lastCall.argsPreview, 60))
|
|
70
|
+
: "";
|
|
71
|
+
return arrow + name + argsPart;
|
|
72
|
+
}
|
|
61
73
|
}
|
|
62
74
|
|
|
63
75
|
// Running, no tool history: show first line of streamed output if any
|
package/src/expanded.ts
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
import { Text } from "@earendil-works/pi-tui";
|
|
2
|
-
import type { SubagentDetails, SubagentProgress, SubagentResult } from "./types.js";
|
|
2
|
+
import type { SubagentDetails, SubagentProgress, SubagentResult, SubagentToolCall } from "./types.js";
|
|
3
3
|
import { formatTokens, formatDuration, truncLine, buildStatsLine, buildAgentLabel } from "./format.js";
|
|
4
4
|
|
|
5
5
|
type Theme = { fg: (token: string, text: string) => string; bold: (text: string) => string };
|
|
6
6
|
|
|
7
|
+
// ── Helpers ─────────────────────────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
function formatToolCall(call: SubagentToolCall | string, theme: Theme): string {
|
|
10
|
+
// Backward compat: old persisted sessions may have toolCalls as string[]
|
|
11
|
+
if (typeof call === "string") {
|
|
12
|
+
return theme.fg("dim", "↳ " + call);
|
|
13
|
+
}
|
|
14
|
+
const arrow = theme.fg("muted", "↳ ");
|
|
15
|
+
const color = call.error ? "error" : "success";
|
|
16
|
+
const name = theme.fg(color, call.name);
|
|
17
|
+
const argsPart = call.argsPreview
|
|
18
|
+
? theme.fg("dim", ": " + call.argsPreview)
|
|
19
|
+
: "";
|
|
20
|
+
return arrow + name + argsPart;
|
|
21
|
+
}
|
|
22
|
+
|
|
7
23
|
// ── Expanded completed result ───────────────────────────────────────────────
|
|
8
24
|
|
|
9
25
|
export function buildExpandedText(
|
|
@@ -43,7 +59,7 @@ export function buildExpandedText(
|
|
|
43
59
|
if (toolCalls && toolCalls.length > 0) {
|
|
44
60
|
lines.push("");
|
|
45
61
|
for (const call of toolCalls) {
|
|
46
|
-
lines.push(
|
|
62
|
+
lines.push(formatToolCall(call, theme));
|
|
47
63
|
}
|
|
48
64
|
}
|
|
49
65
|
|
|
@@ -116,7 +132,7 @@ export function renderProgressExpanded(
|
|
|
116
132
|
if (progress.toolCalls && progress.toolCalls.length > 0) {
|
|
117
133
|
lines.push("");
|
|
118
134
|
for (const call of progress.toolCalls) {
|
|
119
|
-
lines.push(
|
|
135
|
+
lines.push(formatToolCall(call, theme));
|
|
120
136
|
}
|
|
121
137
|
}
|
|
122
138
|
|
|
@@ -129,7 +145,7 @@ export function renderProgressExpanded(
|
|
|
129
145
|
const durationPart = progress.currentToolStartedAt
|
|
130
146
|
? " | " + formatDuration(Date.now() - progress.currentToolStartedAt)
|
|
131
147
|
: "";
|
|
132
|
-
let line = theme.fg("
|
|
148
|
+
let line = theme.fg("muted", progress.currentTool);
|
|
133
149
|
if (argsPreview) {
|
|
134
150
|
line += theme.fg("dim", ": " + argsPreview);
|
|
135
151
|
}
|
|
@@ -192,7 +208,7 @@ export function buildProgressExpandedText(
|
|
|
192
208
|
if (progress.toolCalls && progress.toolCalls.length > 0) {
|
|
193
209
|
lines.push("");
|
|
194
210
|
for (const call of progress.toolCalls) {
|
|
195
|
-
lines.push(
|
|
211
|
+
lines.push(formatToolCall(call, theme));
|
|
196
212
|
}
|
|
197
213
|
}
|
|
198
214
|
|
|
@@ -204,7 +220,7 @@ export function buildProgressExpandedText(
|
|
|
204
220
|
const durationPart = progress.currentToolStartedAt
|
|
205
221
|
? " | " + formatDuration(Date.now() - progress.currentToolStartedAt)
|
|
206
222
|
: "";
|
|
207
|
-
let line = theme.fg("
|
|
223
|
+
let line = theme.fg("muted", progress.currentTool);
|
|
208
224
|
if (argsPreview) {
|
|
209
225
|
line += theme.fg("dim", ": " + argsPreview);
|
|
210
226
|
}
|
package/src/handlers.ts
CHANGED
|
@@ -50,11 +50,16 @@ export function extractArgsPreview(args: unknown): string {
|
|
|
50
50
|
export function handleToolStart(state: StreamState, event: JsonEvent): void {
|
|
51
51
|
state.toolCount++;
|
|
52
52
|
state.currentTool = event.toolName as string;
|
|
53
|
-
|
|
53
|
+
// Use extractArgsPreview instead of JSON.stringify for readable display
|
|
54
|
+
const argsPreview = extractArgsPreview(event.args);
|
|
55
|
+
state.currentToolArgs = argsPreview;
|
|
54
56
|
state.currentToolStartedAt = Date.now();
|
|
55
57
|
// Record tool call with args preview
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
state.toolCalls.push({
|
|
59
|
+
name: state.currentTool,
|
|
60
|
+
argsPreview,
|
|
61
|
+
error: false,
|
|
62
|
+
});
|
|
58
63
|
if (state.toolCalls.length > TOOL_CALLS_MAX) {
|
|
59
64
|
state.toolCalls.splice(0, state.toolCalls.length - TOOL_CALLS_MAX);
|
|
60
65
|
}
|
|
@@ -75,6 +80,14 @@ export function handleToolEnd(state: StreamState): void {
|
|
|
75
80
|
*/
|
|
76
81
|
export function handleToolResult(state: StreamState, event: JsonEvent): void {
|
|
77
82
|
const result = event.result as Record<string, unknown> | undefined;
|
|
83
|
+
|
|
84
|
+
// Mark the last tool call as errored if the result indicates an error
|
|
85
|
+
const lastCall = state.toolCalls[state.toolCalls.length - 1];
|
|
86
|
+
const isError = event.isError === true || result?.isError === true;
|
|
87
|
+
if (lastCall && lastCall.name === (event.toolName as string) && isError) {
|
|
88
|
+
lastCall.error = true;
|
|
89
|
+
}
|
|
90
|
+
|
|
78
91
|
if (!result) return;
|
|
79
92
|
|
|
80
93
|
const toolName = (event.toolName as string) ?? "tool";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import { PassThrough } from "node:stream";
|
|
3
|
+
import type { ChildProcess } from "node:child_process";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import { streamEvents } from "./stream.js";
|
|
6
|
+
|
|
7
|
+
type FakeChild = ChildProcess & { stdout: PassThrough; stderr: PassThrough };
|
|
8
|
+
|
|
9
|
+
function fakeChild(): FakeChild {
|
|
10
|
+
const child = new EventEmitter() as FakeChild;
|
|
11
|
+
Object.assign(child, {
|
|
12
|
+
stdout: new PassThrough(),
|
|
13
|
+
stderr: new PassThrough(),
|
|
14
|
+
kill: () => true,
|
|
15
|
+
});
|
|
16
|
+
return child;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function finishWith(events: Array<Record<string, unknown>>) {
|
|
20
|
+
const child = fakeChild();
|
|
21
|
+
const result = streamEvents(child);
|
|
22
|
+
for (const event of events) {
|
|
23
|
+
child.stdout.write(`${JSON.stringify(event)}\n`);
|
|
24
|
+
}
|
|
25
|
+
child.emit("close", 0);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("streamEvents session identity", () => {
|
|
30
|
+
it("returns the logical child Pi session ID", async () => {
|
|
31
|
+
const result = await finishWith([{
|
|
32
|
+
type: "session",
|
|
33
|
+
id: "00000000-0000-7000-8000-000000000003",
|
|
34
|
+
}]);
|
|
35
|
+
|
|
36
|
+
expect(result.childSessionId).toBe("00000000-0000-7000-8000-000000000003");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("omits the child session ID when no valid session event arrives", async () => {
|
|
40
|
+
const result = await finishWith([
|
|
41
|
+
{ type: "session" },
|
|
42
|
+
{ type: "session", id: 42 },
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
expect(result.childSessionId).toBeUndefined();
|
|
46
|
+
});
|
|
47
|
+
});
|
package/src/stream.ts
CHANGED
|
@@ -127,6 +127,12 @@ export function streamEvents(
|
|
|
127
127
|
clearStartupTimer();
|
|
128
128
|
|
|
129
129
|
switch (event.type) {
|
|
130
|
+
case "session": {
|
|
131
|
+
if (typeof event.id === "string" && event.id) {
|
|
132
|
+
state.childSessionId = event.id;
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
130
136
|
case "tool_execution_start": {
|
|
131
137
|
handleToolStart(state, event);
|
|
132
138
|
emitProgress();
|
|
@@ -163,7 +169,7 @@ export function streamEvents(
|
|
|
163
169
|
handleAgentEnd(state, event);
|
|
164
170
|
break;
|
|
165
171
|
}
|
|
166
|
-
// Ignore:
|
|
172
|
+
// Ignore: agent_start, message_start, message_update, turn_end, tool_execution_update
|
|
167
173
|
}
|
|
168
174
|
});
|
|
169
175
|
|
|
@@ -183,6 +189,7 @@ export function streamEvents(
|
|
|
183
189
|
const result: SubagentResult = {
|
|
184
190
|
agent: callbacks.agent ?? "subagent",
|
|
185
191
|
task: callbacks.task ?? "",
|
|
192
|
+
...(state.childSessionId ? { childSessionId: state.childSessionId } : {}),
|
|
186
193
|
exitCode,
|
|
187
194
|
model: state.model,
|
|
188
195
|
usage: {
|
package/src/types.ts
CHANGED
|
@@ -7,6 +7,12 @@ export interface SubagentUsage {
|
|
|
7
7
|
turns: number;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
export interface SubagentToolCall {
|
|
11
|
+
name: string;
|
|
12
|
+
argsPreview: string;
|
|
13
|
+
error: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
10
16
|
export interface SubagentProgress {
|
|
11
17
|
agent: string;
|
|
12
18
|
status: "running" | "completed" | "failed";
|
|
@@ -27,13 +33,15 @@ export interface SubagentProgress {
|
|
|
27
33
|
output: string | undefined;
|
|
28
34
|
/** Last N lines of assistant text for live display */
|
|
29
35
|
recentOutput: string[] | undefined;
|
|
30
|
-
/** History of tool calls
|
|
31
|
-
toolCalls:
|
|
36
|
+
/** History of tool calls with status tracking */
|
|
37
|
+
toolCalls: SubagentToolCall[] | undefined;
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
export interface SubagentResult {
|
|
35
41
|
agent: string;
|
|
36
42
|
task: string;
|
|
43
|
+
/** Logical Pi session UUID for this spawned subagent process. */
|
|
44
|
+
childSessionId?: string;
|
|
37
45
|
exitCode: number;
|
|
38
46
|
usage: SubagentUsage;
|
|
39
47
|
model: string | undefined;
|
|
@@ -57,6 +65,7 @@ export interface SubagentDetails {
|
|
|
57
65
|
|
|
58
66
|
/** Mutable state during streaming — shared between stream.ts and handlers.ts */
|
|
59
67
|
export interface StreamState {
|
|
68
|
+
childSessionId?: string;
|
|
60
69
|
toolCount: number;
|
|
61
70
|
turnCount: number;
|
|
62
71
|
totalInput: number;
|
|
@@ -70,7 +79,7 @@ export interface StreamState {
|
|
|
70
79
|
model: string | undefined;
|
|
71
80
|
accumulatedOutput: string[];
|
|
72
81
|
recentOutput: string[];
|
|
73
|
-
toolCalls:
|
|
82
|
+
toolCalls: SubagentToolCall[];
|
|
74
83
|
finalOutput: string | undefined;
|
|
75
84
|
}
|
|
76
85
|
|