@yandy0725/pi-subagents 0.1.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/README.md +155 -0
- package/README.zh.md +155 -0
- package/index.ts +1 -0
- package/package.json +49 -0
- package/src/config/agent-types.ts +127 -0
- package/src/config/custom-agents.ts +109 -0
- package/src/config/default-agents.ts +117 -0
- package/src/config/invocation-config.ts +30 -0
- package/src/debug.ts +14 -0
- package/src/handlers/index.ts +3 -0
- package/src/handlers/interrupt.ts +49 -0
- package/src/handlers/lifecycle.ts +63 -0
- package/src/handlers/tool-start.ts +32 -0
- package/src/index.ts +186 -0
- package/src/layered-settings.ts +105 -0
- package/src/lifecycle/child-lifecycle.ts +88 -0
- package/src/lifecycle/concurrency-limiter.ts +55 -0
- package/src/lifecycle/create-subagent-session.ts +240 -0
- package/src/lifecycle/parent-snapshot.ts +45 -0
- package/src/lifecycle/run-listeners.ts +37 -0
- package/src/lifecycle/subagent-manager.ts +353 -0
- package/src/lifecycle/subagent-session.ts +232 -0
- package/src/lifecycle/subagent-state.ts +216 -0
- package/src/lifecycle/subagent.ts +498 -0
- package/src/lifecycle/turn-limits.ts +13 -0
- package/src/lifecycle/usage.ts +65 -0
- package/src/lifecycle/workspace-bracket.ts +59 -0
- package/src/lifecycle/workspace.ts +47 -0
- package/src/observation/composite-subagent-observer.ts +49 -0
- package/src/observation/notification-state.ts +27 -0
- package/src/observation/notification.ts +186 -0
- package/src/observation/record-observer.ts +75 -0
- package/src/observation/renderer.ts +63 -0
- package/src/observation/subagent-events-observer.ts +94 -0
- package/src/runtime.ts +77 -0
- package/src/service/service-adapter.ts +131 -0
- package/src/service/service.ts +123 -0
- package/src/session/content-items.ts +51 -0
- package/src/session/context.ts +78 -0
- package/src/session/conversation.ts +44 -0
- package/src/session/env.ts +40 -0
- package/src/session/model-resolver.ts +121 -0
- package/src/session/prompts.ts +83 -0
- package/src/session/session-config.ts +172 -0
- package/src/session/session-dir.ts +38 -0
- package/src/settings.ts +227 -0
- package/src/tools/agent-tool.ts +220 -0
- package/src/tools/background-spawner.ts +66 -0
- package/src/tools/foreground-runner.ts +114 -0
- package/src/tools/get-result-tool.ts +120 -0
- package/src/tools/helpers.ts +105 -0
- package/src/tools/result-renderer.ts +109 -0
- package/src/tools/spawn-config.ts +150 -0
- package/src/tools/steer-tool.ts +90 -0
- package/src/types.ts +115 -0
- package/src/ui/agent-widget.ts +311 -0
- package/src/ui/display.ts +174 -0
- package/src/ui/session-navigation.ts +147 -0
- package/src/ui/session-navigator.ts +406 -0
- package/src/ui/subagents-settings.ts +77 -0
- package/src/ui/widget-renderer.ts +296 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-redundant-type-constituents -- Pi SDK types are not fully exported; see upstream Pi SDK for type improvements */
|
|
2
|
+
/**
|
|
3
|
+
* agent-widget.ts — Persistent widget showing running/completed agents above the editor.
|
|
4
|
+
*
|
|
5
|
+
* Displays a tree of agents with animated spinners, live stats, and activity descriptions.
|
|
6
|
+
* Uses the callback form of setWidget for themed rendering.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { AgentTypeRegistry } from "../config/agent-types";
|
|
10
|
+
import type { Subagent } from "../lifecycle/subagent";
|
|
11
|
+
import type { SubagentManager, SubagentManagerObserver } from "../lifecycle/subagent-manager";
|
|
12
|
+
import type { CompactionInfo } from "../types";
|
|
13
|
+
import { ERROR_STATUSES, type Theme } from "../ui/display";
|
|
14
|
+
import { renderWidgetLines, type WidgetAgent } from "../ui/widget-renderer";
|
|
15
|
+
|
|
16
|
+
// ---- Types ----
|
|
17
|
+
|
|
18
|
+
/** Minimal agent shape needed for widget lifecycle decisions. */
|
|
19
|
+
interface AgentSummary {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly status: string;
|
|
22
|
+
readonly completedAt?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Lightweight state snapshot used by AgentWidget.update() to decide what to show. */
|
|
26
|
+
export interface WidgetState {
|
|
27
|
+
readonly runningCount: number;
|
|
28
|
+
readonly queuedCount: number;
|
|
29
|
+
readonly hasFinished: boolean;
|
|
30
|
+
/** True when runningCount > 0 || queuedCount > 0. Included for call-site readability. */
|
|
31
|
+
readonly hasActive: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Count agents by status and return a lightweight state snapshot.
|
|
36
|
+
* Pure function — no IO, no side effects. Exported for direct unit testing.
|
|
37
|
+
*/
|
|
38
|
+
export function assembleWidgetState(
|
|
39
|
+
agents: readonly AgentSummary[],
|
|
40
|
+
shouldShowFinished: (agentId: string, status: string) => boolean,
|
|
41
|
+
): WidgetState {
|
|
42
|
+
let runningCount = 0;
|
|
43
|
+
let queuedCount = 0;
|
|
44
|
+
let hasFinished = false;
|
|
45
|
+
for (const a of agents) {
|
|
46
|
+
if (a.status === "running") {
|
|
47
|
+
runningCount++;
|
|
48
|
+
} else if (a.status === "queued") {
|
|
49
|
+
queuedCount++;
|
|
50
|
+
} else if (a.completedAt && shouldShowFinished(a.id, a.status)) {
|
|
51
|
+
hasFinished = true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const hasActive = runningCount > 0 || queuedCount > 0;
|
|
55
|
+
return { runningCount, queuedCount, hasFinished, hasActive };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type UICtx = {
|
|
59
|
+
setStatus(key: string, text: string | undefined): void;
|
|
60
|
+
setWidget(
|
|
61
|
+
key: string,
|
|
62
|
+
content: undefined | ((tui: any, theme: Theme) => { render(): string[]; invalidate(): void }),
|
|
63
|
+
options?: { placement?: "aboveEditor" | "belowEditor" },
|
|
64
|
+
): void;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// ---- Widget manager ----
|
|
68
|
+
|
|
69
|
+
export class AgentWidget implements SubagentManagerObserver {
|
|
70
|
+
private uiCtx: UICtx | undefined;
|
|
71
|
+
private widgetFrame = 0;
|
|
72
|
+
private widgetInterval: ReturnType<typeof setInterval> | undefined;
|
|
73
|
+
/** Tracks how many turns each finished agent has survived. Key: agent ID, Value: turns since finished. */
|
|
74
|
+
private finishedTurnAge = new Map<string, number>();
|
|
75
|
+
/** How many extra turns errors/aborted agents linger (completed agents clear after 1 turn). */
|
|
76
|
+
private static readonly ERROR_LINGER_TURNS = 2;
|
|
77
|
+
|
|
78
|
+
/** Whether the widget callback is currently registered with the TUI. */
|
|
79
|
+
private widgetRegistered = false;
|
|
80
|
+
/** Cached TUI reference from widget factory callback, used for requestRender(). */
|
|
81
|
+
private tui: any | undefined;
|
|
82
|
+
/** Last status bar text, used to avoid redundant setStatus calls. */
|
|
83
|
+
private lastStatusText: string | undefined;
|
|
84
|
+
|
|
85
|
+
constructor(
|
|
86
|
+
private manager: SubagentManager,
|
|
87
|
+
private registry: AgentTypeRegistry,
|
|
88
|
+
) {}
|
|
89
|
+
|
|
90
|
+
/** Set the UI context (grabbed from first tool execution). */
|
|
91
|
+
setUICtx(ctx: UICtx) {
|
|
92
|
+
if (ctx !== this.uiCtx) {
|
|
93
|
+
// UICtx changed — the widget registered on the old context is gone.
|
|
94
|
+
// Force re-registration on next update().
|
|
95
|
+
this.uiCtx = ctx;
|
|
96
|
+
this.widgetRegistered = false;
|
|
97
|
+
this.tui = undefined;
|
|
98
|
+
this.lastStatusText = undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Called on each new turn (tool_execution_start).
|
|
104
|
+
* Ages finished agents and clears those that have lingered long enough.
|
|
105
|
+
*/
|
|
106
|
+
onTurnStart() {
|
|
107
|
+
// Age all finished agents
|
|
108
|
+
for (const [id, age] of this.finishedTurnAge) {
|
|
109
|
+
this.finishedTurnAge.set(id, age + 1);
|
|
110
|
+
}
|
|
111
|
+
// Trigger a widget refresh (will filter out expired agents)
|
|
112
|
+
this.update();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ---- SubagentManagerObserver: react to lifecycle, self-drive the timer ----
|
|
116
|
+
|
|
117
|
+
/** A subagent started running — ensure the update loop is live and render. */
|
|
118
|
+
onSubagentStarted(_record: Subagent) {
|
|
119
|
+
this.startLoop();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A background subagent was created (queued) — ensure the loop is live and render. */
|
|
123
|
+
onSubagentCreated(_record: Subagent) {
|
|
124
|
+
this.startLoop();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A subagent completed — render so the finished state is seeded and shown. */
|
|
128
|
+
onSubagentCompleted(_record: Subagent) {
|
|
129
|
+
this.update();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A subagent's session compacted — render to refresh the compaction count. */
|
|
133
|
+
onSubagentCompacted(_record: Subagent, _info: CompactionInfo) {
|
|
134
|
+
this.update();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Start the update timer (if not already running) and render immediately. */
|
|
138
|
+
private startLoop() {
|
|
139
|
+
this.ensureTimer();
|
|
140
|
+
this.update();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Ensure the widget update timer is running. */
|
|
144
|
+
private ensureTimer() {
|
|
145
|
+
this.widgetInterval ??= setInterval(() => this.update(), 80);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Check if a finished agent should still be shown in the widget. */
|
|
149
|
+
private shouldShowFinished(agentId: string, status: string): boolean {
|
|
150
|
+
const age = this.finishedTurnAge.get(agentId) ?? 0;
|
|
151
|
+
const maxAge = ERROR_STATUSES.has(status) ? AgentWidget.ERROR_LINGER_TURNS : 1;
|
|
152
|
+
return age < maxAge;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Background agents only — the widget's sole audience (ADR-0004 Decision A).
|
|
157
|
+
* Foreground runs are rendered by the `subagent` tool's inline `onUpdate` stream,
|
|
158
|
+
* so funneling both `listAgents()` call sites through this accessor applies the
|
|
159
|
+
* background predicate exactly once at the source.
|
|
160
|
+
*/
|
|
161
|
+
private listBackgroundAgents(): Subagent[] {
|
|
162
|
+
return this.manager.listAgents().filter((record) => record.invocation?.runInBackground === true);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Project a live Subagent record onto a pure-data WidgetAgent snapshot. */
|
|
166
|
+
private toWidgetAgent(record: Subagent): WidgetAgent {
|
|
167
|
+
return {
|
|
168
|
+
id: record.id,
|
|
169
|
+
type: record.type,
|
|
170
|
+
status: record.status,
|
|
171
|
+
description: record.description,
|
|
172
|
+
toolUses: record.toolUses,
|
|
173
|
+
startedAt: record.startedAt,
|
|
174
|
+
completedAt: record.completedAt,
|
|
175
|
+
error: record.error,
|
|
176
|
+
lifetimeUsage: record.lifetimeUsage,
|
|
177
|
+
compactionCount: record.compactionCount,
|
|
178
|
+
turnCount: record.turnCount,
|
|
179
|
+
maxTurns: record.maxTurns,
|
|
180
|
+
activeTools: record.activeTools,
|
|
181
|
+
responseText: record.responseText,
|
|
182
|
+
contextPercent: record.getContextPercent(),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Delegate rendering to the pure widget-renderer module. */
|
|
187
|
+
private renderWidget(tui: any, theme: Theme): string[] {
|
|
188
|
+
return renderWidgetLines({
|
|
189
|
+
agents: this.listBackgroundAgents().map((r) => this.toWidgetAgent(r)),
|
|
190
|
+
registry: this.registry,
|
|
191
|
+
spinnerFrame: this.widgetFrame,
|
|
192
|
+
terminalWidth: tui.terminal.columns,
|
|
193
|
+
theme,
|
|
194
|
+
shouldShowFinished: (id, status) => this.shouldShowFinished(id, status),
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Unregister the widget, clear the status bar, stop the interval timer, and
|
|
200
|
+
* purge stale `finishedTurnAge` entries for agents no longer in `backgroundAgents`.
|
|
201
|
+
* Called only from `update`'s idle path — not from `dispose`.
|
|
202
|
+
*/
|
|
203
|
+
private clearWidget(backgroundAgents: readonly AgentSummary[]): void {
|
|
204
|
+
if (this.widgetRegistered) {
|
|
205
|
+
this.uiCtx!.setWidget("agents", undefined);
|
|
206
|
+
this.widgetRegistered = false;
|
|
207
|
+
this.tui = undefined;
|
|
208
|
+
}
|
|
209
|
+
if (this.lastStatusText !== undefined) {
|
|
210
|
+
this.uiCtx!.setStatus("subagents", undefined);
|
|
211
|
+
this.lastStatusText = undefined;
|
|
212
|
+
}
|
|
213
|
+
if (this.widgetInterval) {
|
|
214
|
+
clearInterval(this.widgetInterval);
|
|
215
|
+
this.widgetInterval = undefined;
|
|
216
|
+
}
|
|
217
|
+
for (const [id] of this.finishedTurnAge) {
|
|
218
|
+
if (!backgroundAgents.some((a) => a.id === id)) this.finishedTurnAge.delete(id);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Compute the status bar text from the current widget state and call
|
|
224
|
+
* `setStatus` only when it differs from the last cached value.
|
|
225
|
+
*/
|
|
226
|
+
private updateStatusBar(state: WidgetState): void {
|
|
227
|
+
let newStatusText: string | undefined;
|
|
228
|
+
if (state.hasActive) {
|
|
229
|
+
const statusParts: string[] = [];
|
|
230
|
+
if (state.runningCount > 0) statusParts.push(`${state.runningCount} running`);
|
|
231
|
+
if (state.queuedCount > 0) statusParts.push(`${state.queuedCount} queued`);
|
|
232
|
+
const total = state.runningCount + state.queuedCount;
|
|
233
|
+
newStatusText = `${statusParts.join(", ")} agent${total === 1 ? "" : "s"}`;
|
|
234
|
+
}
|
|
235
|
+
if (newStatusText !== this.lastStatusText) {
|
|
236
|
+
this.uiCtx!.setStatus("subagents", newStatusText);
|
|
237
|
+
this.lastStatusText = newStatusText;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Seed linger tracking for any newly-observed finished agent.
|
|
243
|
+
* The widget owns detection of completions it observes via `listAgents()`,
|
|
244
|
+
* so no external bookkeeping call is needed.
|
|
245
|
+
* Idempotent — only seeds when an entry is absent, so repeated updates within
|
|
246
|
+
* a turn neither reset nor advance the age.
|
|
247
|
+
*/
|
|
248
|
+
private seedFinishedAgents(agents: readonly AgentSummary[]): void {
|
|
249
|
+
for (const a of agents) {
|
|
250
|
+
if (a.completedAt && !this.finishedTurnAge.has(a.id)) {
|
|
251
|
+
this.finishedTurnAge.set(a.id, 0);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Force an immediate widget update. */
|
|
257
|
+
update() {
|
|
258
|
+
if (!this.uiCtx) return;
|
|
259
|
+
|
|
260
|
+
const backgroundAgents = this.listBackgroundAgents();
|
|
261
|
+
this.seedFinishedAgents(backgroundAgents);
|
|
262
|
+
const state = assembleWidgetState(backgroundAgents, (id, status) => this.shouldShowFinished(id, status));
|
|
263
|
+
|
|
264
|
+
if (!state.hasActive && !state.hasFinished) {
|
|
265
|
+
this.clearWidget(backgroundAgents);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
this.updateStatusBar(state);
|
|
270
|
+
this.widgetFrame++;
|
|
271
|
+
|
|
272
|
+
// Register widget callback once; subsequent updates use requestRender()
|
|
273
|
+
// which re-invokes render() without replacing the component (avoids layout thrashing).
|
|
274
|
+
if (!this.widgetRegistered) {
|
|
275
|
+
this.uiCtx.setWidget(
|
|
276
|
+
"agents",
|
|
277
|
+
(tui, theme) => {
|
|
278
|
+
this.tui = tui;
|
|
279
|
+
return {
|
|
280
|
+
render: () => this.renderWidget(tui, theme),
|
|
281
|
+
invalidate: () => {
|
|
282
|
+
// Theme changed — force re-registration so factory captures fresh theme.
|
|
283
|
+
this.widgetRegistered = false;
|
|
284
|
+
this.tui = undefined;
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
},
|
|
288
|
+
{ placement: "aboveEditor" },
|
|
289
|
+
);
|
|
290
|
+
this.widgetRegistered = true;
|
|
291
|
+
} else {
|
|
292
|
+
// Widget already registered — just request a re-render of existing components.
|
|
293
|
+
this.tui?.requestRender();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// fallow-ignore-next-line unused-class-member
|
|
298
|
+
dispose() {
|
|
299
|
+
if (this.widgetInterval) {
|
|
300
|
+
clearInterval(this.widgetInterval);
|
|
301
|
+
this.widgetInterval = undefined;
|
|
302
|
+
}
|
|
303
|
+
if (this.uiCtx) {
|
|
304
|
+
this.uiCtx.setWidget("agents", undefined);
|
|
305
|
+
this.uiCtx.setStatus("subagents", undefined);
|
|
306
|
+
}
|
|
307
|
+
this.widgetRegistered = false;
|
|
308
|
+
this.tui = undefined;
|
|
309
|
+
this.lastStatusText = undefined;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* display.ts — Pure formatting helpers and display utilities for agent UI.
|
|
3
|
+
*
|
|
4
|
+
* All functions are stateless and dependency-free (no SDK, no widget lifecycle).
|
|
5
|
+
* Consumed by the widget, the menu, tool modules, and the notification renderer.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { AgentConfigLookup } from "../config/agent-types";
|
|
9
|
+
import type { AgentInvocation, SubagentType } from "../types";
|
|
10
|
+
|
|
11
|
+
// ---- Types ----
|
|
12
|
+
|
|
13
|
+
export type Theme = {
|
|
14
|
+
fg(color: string, text: string): string;
|
|
15
|
+
bold(text: string): string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** Metadata attached to Agent tool results for custom rendering. */
|
|
19
|
+
export interface AgentDetails {
|
|
20
|
+
displayName: string;
|
|
21
|
+
description: string;
|
|
22
|
+
subagentType: string;
|
|
23
|
+
toolUses: number;
|
|
24
|
+
tokens: string;
|
|
25
|
+
durationMs: number;
|
|
26
|
+
status: "queued" | "running" | "completed" | "steered" | "aborted" | "stopped" | "error" | "background";
|
|
27
|
+
/** Human-readable description of what the agent is currently doing. */
|
|
28
|
+
activity?: string;
|
|
29
|
+
/** Current spinner frame index (for animated running indicator). */
|
|
30
|
+
spinnerFrame?: number;
|
|
31
|
+
/** Short model name if different from parent (e.g. "haiku", "sonnet"). */
|
|
32
|
+
modelName?: string;
|
|
33
|
+
/** Notable config tags (e.g. ["thinking: high", "inherit context"]). */
|
|
34
|
+
tags?: string[];
|
|
35
|
+
/** Current turn count. */
|
|
36
|
+
turnCount?: number;
|
|
37
|
+
/** Effective max turns (undefined = unlimited). */
|
|
38
|
+
maxTurns?: number;
|
|
39
|
+
agentId?: string;
|
|
40
|
+
error?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ---- Constants ----
|
|
44
|
+
|
|
45
|
+
/** Braille spinner frames for animated running indicator. */
|
|
46
|
+
export const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
47
|
+
|
|
48
|
+
/** Statuses that indicate an error/non-success outcome (used for linger behavior and icon rendering). */
|
|
49
|
+
export const ERROR_STATUSES = new Set(["error", "aborted", "steered", "stopped"]);
|
|
50
|
+
|
|
51
|
+
/** Tool name → human-readable action for activity descriptions. */
|
|
52
|
+
const TOOL_DISPLAY: Record<string, string> = {
|
|
53
|
+
read: "reading",
|
|
54
|
+
bash: "running command",
|
|
55
|
+
edit: "editing",
|
|
56
|
+
write: "writing",
|
|
57
|
+
grep: "searching",
|
|
58
|
+
find: "finding files",
|
|
59
|
+
ls: "listing",
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// ---- Pure formatters ----
|
|
63
|
+
|
|
64
|
+
/** Format a token count compactly: "33.8k token", "1.2M token". */
|
|
65
|
+
export function formatTokens(count: number): string {
|
|
66
|
+
if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}M token`;
|
|
67
|
+
if (count >= 1_000) return `${(count / 1_000).toFixed(1)}k token`;
|
|
68
|
+
return `${count} token`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Token count with optional context-fill % and compaction-count annotations.
|
|
73
|
+
* Thresholds for percent: <70% dim, 70–85% warning, ≥85% error.
|
|
74
|
+
* Compaction count rendered as `↻N` in dim.
|
|
75
|
+
*
|
|
76
|
+
* "12.3k token" — no annotations
|
|
77
|
+
* "12.3k token (45%)" — percent only
|
|
78
|
+
* "12.3k token (↻2)" — compactions only (e.g. right after compact)
|
|
79
|
+
* "12.3k token (45% · ↻2)" — both
|
|
80
|
+
*/
|
|
81
|
+
export function formatSessionTokens(tokens: number, percent: number | null, theme: Theme, compactions = 0): string {
|
|
82
|
+
const tokenStr = formatTokens(tokens);
|
|
83
|
+
const annot: string[] = [];
|
|
84
|
+
if (percent !== null) {
|
|
85
|
+
const color = percent >= 85 ? "error" : percent >= 70 ? "warning" : "dim";
|
|
86
|
+
annot.push(theme.fg(color, `${Math.round(percent)}%`));
|
|
87
|
+
}
|
|
88
|
+
if (compactions > 0) {
|
|
89
|
+
annot.push(theme.fg("dim", `↻${compactions}`));
|
|
90
|
+
}
|
|
91
|
+
if (annot.length === 0) return tokenStr;
|
|
92
|
+
const sep = theme.fg("dim", " · ");
|
|
93
|
+
return `${tokenStr} ${theme.fg("dim", "(")}${annot.join(sep)}${theme.fg("dim", ")")}`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Format turn count with optional max limit: "⟳5≤30" or "⟳5". */
|
|
97
|
+
export function formatTurns(turnCount: number, maxTurns?: number | null): string {
|
|
98
|
+
return maxTurns != null ? `⟳${turnCount}≤${maxTurns}` : `⟳${turnCount}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Format milliseconds as human-readable duration. */
|
|
102
|
+
export function formatMs(ms: number): string {
|
|
103
|
+
return `${(ms / 1000).toFixed(1)}s`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Format duration from start/completed timestamps. */
|
|
107
|
+
export function formatDuration(startedAt: number, completedAt?: number): string {
|
|
108
|
+
if (completedAt) return formatMs(completedAt - startedAt);
|
|
109
|
+
return `${formatMs(Date.now() - startedAt)} (running)`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ---- Display helpers ----
|
|
113
|
+
|
|
114
|
+
/** Get display name for any agent type (built-in or custom). */
|
|
115
|
+
export function getDisplayName(type: SubagentType, registry: AgentConfigLookup): string {
|
|
116
|
+
const config = registry.resolveAgentConfig(type);
|
|
117
|
+
return config.displayName ?? config.name;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Short label for prompt mode: "twin" for append, nothing for replace (the default). */
|
|
121
|
+
export function getPromptModeLabel(type: SubagentType, registry: AgentConfigLookup): string | undefined {
|
|
122
|
+
const config = registry.resolveAgentConfig(type);
|
|
123
|
+
return config.promptMode === "append" ? "twin" : undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Mode label is not included — callers add it where they want it. */
|
|
127
|
+
export function buildInvocationTags(invocation: AgentInvocation | undefined): { modelName?: string; tags: string[] } {
|
|
128
|
+
const tags: string[] = [];
|
|
129
|
+
if (!invocation) return { tags };
|
|
130
|
+
if (invocation.thinking) tags.push(`thinking: ${invocation.thinking}`);
|
|
131
|
+
if (invocation.inheritContext) tags.push("inherit context");
|
|
132
|
+
if (invocation.runInBackground) tags.push("background");
|
|
133
|
+
if (invocation.maxTurns != null) tags.push(`max turns: ${invocation.maxTurns}`);
|
|
134
|
+
return { modelName: invocation.modelName, tags };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Truncate text to a single line, max `len` chars. */
|
|
138
|
+
function truncateLine(text: string, len = 60): string {
|
|
139
|
+
const line =
|
|
140
|
+
text
|
|
141
|
+
.split("\n")
|
|
142
|
+
.find((l) => l.trim())
|
|
143
|
+
?.trim() ?? "";
|
|
144
|
+
if (line.length <= len) return line;
|
|
145
|
+
return line.slice(0, len) + "…";
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Build a human-readable activity string from currently-running tools or response text. */
|
|
149
|
+
export function describeActivity(activeTools: ReadonlyMap<string, string>, responseText?: string): string {
|
|
150
|
+
if (activeTools.size > 0) {
|
|
151
|
+
const groups = new Map<string, number>();
|
|
152
|
+
for (const toolName of activeTools.values()) {
|
|
153
|
+
const action = TOOL_DISPLAY[toolName] ?? toolName;
|
|
154
|
+
groups.set(action, (groups.get(action) ?? 0) + 1);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const parts: string[] = [];
|
|
158
|
+
for (const [action, count] of groups) {
|
|
159
|
+
if (count > 1) {
|
|
160
|
+
parts.push(`${action} ${count} ${action === "searching" ? "patterns" : "files"}`);
|
|
161
|
+
} else {
|
|
162
|
+
parts.push(action);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return parts.join(", ") + "…";
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// No tools active — show truncated response text if available
|
|
169
|
+
if (responseText && responseText.trim().length > 0) {
|
|
170
|
+
return truncateLine(responseText);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return "thinking…";
|
|
174
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* session-navigation.ts — Pure selection and transcript-sourcing for native session navigation.
|
|
3
|
+
*
|
|
4
|
+
* Splits the unit-testable core of the `/subagents:sessions` command from its TUI
|
|
5
|
+
* wiring (`session-navigator.ts`): which subagents are navigable and how a picked
|
|
6
|
+
* agent's transcript is sourced (live, in this slice).
|
|
7
|
+
*
|
|
8
|
+
* The `TranscriptSource` seam decouples *how messages are sourced* (live record
|
|
9
|
+
* here; a file snapshot in a follow-up) from *how they render* — the renderer
|
|
10
|
+
* (`session-navigator.ts`, which mounts Pi's per-entry components) talks only to
|
|
11
|
+
* this seam. Rendering lives in the SDK/TUI module because the per-entry
|
|
12
|
+
* components require a `TUI`, `cwd`, and markdown theme.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
buildSessionContext,
|
|
17
|
+
parseSessionEntries,
|
|
18
|
+
type SessionEntry,
|
|
19
|
+
type ToolDefinition,
|
|
20
|
+
} from "@earendil-works/pi-coding-agent";
|
|
21
|
+
import type { AgentConfigLookup } from "../config/agent-types";
|
|
22
|
+
import type { EvictedSubagent } from "../lifecycle/subagent-manager";
|
|
23
|
+
import type { SubagentStatus } from "../lifecycle/subagent-state";
|
|
24
|
+
import type { AgentSessionEvent, SessionMessage, SubagentType } from "../types";
|
|
25
|
+
import { formatDuration, getDisplayName } from "../ui/display";
|
|
26
|
+
|
|
27
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
/** The record fields the navigator reads to label and live-source a transcript. */
|
|
30
|
+
export interface NavigableSubagent {
|
|
31
|
+
readonly id: string;
|
|
32
|
+
readonly type: SubagentType;
|
|
33
|
+
readonly description: string;
|
|
34
|
+
readonly status: SubagentStatus;
|
|
35
|
+
readonly startedAt: number;
|
|
36
|
+
readonly completedAt: number | undefined;
|
|
37
|
+
readonly toolUses: number;
|
|
38
|
+
readonly activeTools: ReadonlyMap<string, string>;
|
|
39
|
+
readonly responseText: string;
|
|
40
|
+
readonly agentMessages: readonly SessionMessage[];
|
|
41
|
+
isSessionReady(): boolean;
|
|
42
|
+
subscribeToUpdates(fn: (event: AgentSessionEvent) => void): (() => void) | undefined;
|
|
43
|
+
getToolDefinition(name: string): ToolDefinition | undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A navigable entry plus the label shown in the picker.
|
|
48
|
+
*
|
|
49
|
+
* A `live` entry sources its transcript from the in-memory record; an `evicted`
|
|
50
|
+
* entry sources it from the persisted session file (the record is gone).
|
|
51
|
+
*/
|
|
52
|
+
export type NavigationEntry =
|
|
53
|
+
| { readonly kind: "live"; readonly label: string; readonly record: NavigableSubagent }
|
|
54
|
+
| { readonly kind: "evicted"; readonly label: string; readonly outputFile: string };
|
|
55
|
+
|
|
56
|
+
/** The fields `buildLabel` reads — shared by a live record and an evicted descriptor. */
|
|
57
|
+
interface LabelFields {
|
|
58
|
+
readonly type: SubagentType;
|
|
59
|
+
readonly description: string;
|
|
60
|
+
readonly status: SubagentStatus;
|
|
61
|
+
readonly startedAt: number;
|
|
62
|
+
readonly completedAt: number | undefined;
|
|
63
|
+
readonly toolUses: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Running-agent streaming state, surfaced by a live source. */
|
|
67
|
+
export interface StreamingState {
|
|
68
|
+
readonly activeTools: ReadonlyMap<string, string>;
|
|
69
|
+
readonly responseText: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Liveness-agnostic transcript source consumed by the renderer. */
|
|
73
|
+
export interface TranscriptSource {
|
|
74
|
+
/** Current message history. */
|
|
75
|
+
getMessages(): readonly SessionMessage[];
|
|
76
|
+
/** Subscribe to changes; returns an unsubscribe, or undefined for a static snapshot. */
|
|
77
|
+
subscribe(onChange: () => void): (() => void) | undefined;
|
|
78
|
+
/** Running-agent streaming state, or undefined when not streaming. */
|
|
79
|
+
streaming(): StreamingState | undefined;
|
|
80
|
+
/** Resolve a registered tool definition by name, for Pi's tool-execution components. */
|
|
81
|
+
getToolDefinition(name: string): ToolDefinition | undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Label every navigable subagent for the picker: live records with a viewable
|
|
86
|
+
* session, then agents evicted by the cleanup sweep (deduped against live ids).
|
|
87
|
+
*/
|
|
88
|
+
export function listNavigableAgents(
|
|
89
|
+
agents: readonly NavigableSubagent[],
|
|
90
|
+
evicted: readonly EvictedSubagent[],
|
|
91
|
+
registry: AgentConfigLookup,
|
|
92
|
+
): NavigationEntry[] {
|
|
93
|
+
const live = agents
|
|
94
|
+
.filter((record) => record.isSessionReady())
|
|
95
|
+
.map((record): NavigationEntry => ({ kind: "live", record, label: buildLabel(record, registry) }));
|
|
96
|
+
const liveIds = new Set(agents.map((record) => record.id));
|
|
97
|
+
const evictedEntries = evicted
|
|
98
|
+
.filter((descriptor) => !liveIds.has(descriptor.id))
|
|
99
|
+
.map(
|
|
100
|
+
(descriptor): NavigationEntry => ({
|
|
101
|
+
kind: "evicted",
|
|
102
|
+
outputFile: descriptor.outputFile,
|
|
103
|
+
label: buildLabel(descriptor, registry, true),
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
return [...live, ...evictedEntries];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Source a transcript from a persisted child-session JSONL snapshot.
|
|
111
|
+
*
|
|
112
|
+
* For an agent evicted from the manager's map by the 10-minute cleanup sweep:
|
|
113
|
+
* the in-memory record (and its message history) is gone, but the session file
|
|
114
|
+
* survives on disk. Reads the file, drops the `SessionHeader`, and resolves the
|
|
115
|
+
* message list via Pi's own parser. A static snapshot — no subscription, no
|
|
116
|
+
* streaming, no live tool registry. `readFile` is injected so this module makes
|
|
117
|
+
* no `fs` calls.
|
|
118
|
+
*/
|
|
119
|
+
export function fileSnapshotSource(outputFile: string, readFile: (path: string) => string): TranscriptSource {
|
|
120
|
+
const entries = parseSessionEntries(readFile(outputFile));
|
|
121
|
+
const sessionEntries = entries.filter((entry): entry is SessionEntry => entry.type !== "session");
|
|
122
|
+
const { messages } = buildSessionContext(sessionEntries);
|
|
123
|
+
return {
|
|
124
|
+
getMessages: () => messages,
|
|
125
|
+
subscribe: () => undefined,
|
|
126
|
+
streaming: () => undefined,
|
|
127
|
+
getToolDefinition: () => undefined,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Source a transcript live from an in-memory record (this slice's only source). */
|
|
132
|
+
export function liveSource(record: NavigableSubagent): TranscriptSource {
|
|
133
|
+
return {
|
|
134
|
+
getMessages: () => record.agentMessages,
|
|
135
|
+
subscribe: (onChange) => record.subscribeToUpdates(() => onChange()),
|
|
136
|
+
streaming: () =>
|
|
137
|
+
record.status === "running" ? { activeTools: record.activeTools, responseText: record.responseText } : undefined,
|
|
138
|
+
getToolDefinition: (name) => record.getToolDefinition(name),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function buildLabel(fields: LabelFields, registry: AgentConfigLookup, evicted = false): string {
|
|
143
|
+
const name = getDisplayName(fields.type, registry);
|
|
144
|
+
const duration = formatDuration(fields.startedAt, fields.completedAt);
|
|
145
|
+
const marker = evicted ? " · evicted (snapshot)" : "";
|
|
146
|
+
return `${name} (${fields.description}) · ${fields.toolUses} tools · ${fields.status} · ${duration}${marker}`;
|
|
147
|
+
}
|