pi-subagent-manager 1.10.0 → 1.11.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.
Files changed (186) hide show
  1. package/dist/index.d.ts +3 -0
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +836 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/orch/checklist-integration.d.ts +48 -0
  6. package/dist/orch/checklist-integration.d.ts.map +1 -0
  7. package/dist/orch/checklist-integration.js +204 -0
  8. package/dist/orch/checklist-integration.js.map +1 -0
  9. package/dist/orch/context-entries.d.ts +8 -0
  10. package/dist/orch/context-entries.d.ts.map +1 -0
  11. package/dist/orch/context-entries.js +8 -0
  12. package/dist/orch/context-entries.js.map +1 -0
  13. package/dist/orch/cost-ledger.d.ts +16 -0
  14. package/dist/orch/cost-ledger.d.ts.map +1 -0
  15. package/dist/orch/cost-ledger.js +43 -0
  16. package/dist/orch/cost-ledger.js.map +1 -0
  17. package/dist/orch/diagnostics.d.ts +13 -0
  18. package/dist/orch/diagnostics.d.ts.map +1 -0
  19. package/dist/orch/diagnostics.js +95 -0
  20. package/dist/orch/diagnostics.js.map +1 -0
  21. package/dist/orch/extensions.d.ts +15 -0
  22. package/dist/orch/extensions.d.ts.map +1 -0
  23. package/dist/orch/extensions.js +80 -0
  24. package/dist/orch/extensions.js.map +1 -0
  25. package/dist/orch/housekeeping-usage.d.ts +14 -0
  26. package/dist/orch/housekeeping-usage.d.ts.map +1 -0
  27. package/dist/orch/housekeeping-usage.js +27 -0
  28. package/dist/orch/housekeeping-usage.js.map +1 -0
  29. package/dist/orch/housekeeping.d.ts +89 -0
  30. package/dist/orch/housekeeping.d.ts.map +1 -0
  31. package/dist/orch/housekeeping.js +281 -0
  32. package/dist/orch/housekeeping.js.map +1 -0
  33. package/dist/orch/inherited-tools.d.ts +18 -0
  34. package/dist/orch/inherited-tools.d.ts.map +1 -0
  35. package/dist/orch/inherited-tools.js +71 -0
  36. package/dist/orch/inherited-tools.js.map +1 -0
  37. package/dist/orch/mailbox.d.ts +59 -0
  38. package/dist/orch/mailbox.d.ts.map +1 -0
  39. package/dist/orch/mailbox.js +196 -0
  40. package/dist/orch/mailbox.js.map +1 -0
  41. package/dist/orch/manager.d.ts +82 -0
  42. package/dist/orch/manager.d.ts.map +1 -0
  43. package/dist/orch/manager.js +1010 -0
  44. package/dist/orch/manager.js.map +1 -0
  45. package/dist/orch/paths.d.ts +7 -0
  46. package/dist/orch/paths.d.ts.map +1 -0
  47. package/dist/orch/paths.js +73 -0
  48. package/dist/orch/paths.js.map +1 -0
  49. package/dist/orch/prompt.d.ts +7 -0
  50. package/dist/orch/prompt.d.ts.map +1 -0
  51. package/dist/orch/prompt.js +34 -0
  52. package/dist/orch/prompt.js.map +1 -0
  53. package/dist/orch/registry.d.ts +49 -0
  54. package/dist/orch/registry.d.ts.map +1 -0
  55. package/dist/orch/registry.js +134 -0
  56. package/dist/orch/registry.js.map +1 -0
  57. package/dist/orch/runtime.d.ts +11 -0
  58. package/dist/orch/runtime.d.ts.map +1 -0
  59. package/dist/orch/runtime.js +882 -0
  60. package/dist/orch/runtime.js.map +1 -0
  61. package/dist/orch/tools.d.ts +6 -0
  62. package/dist/orch/tools.d.ts.map +1 -0
  63. package/dist/orch/tools.js +114 -0
  64. package/dist/orch/tools.js.map +1 -0
  65. package/dist/orch/transcript.d.ts +28 -0
  66. package/dist/orch/transcript.d.ts.map +1 -0
  67. package/dist/orch/transcript.js +176 -0
  68. package/dist/orch/transcript.js.map +1 -0
  69. package/dist/prefs/agent-import.d.ts +37 -0
  70. package/dist/prefs/agent-import.d.ts.map +1 -0
  71. package/dist/prefs/agent-import.js +196 -0
  72. package/dist/prefs/agent-import.js.map +1 -0
  73. package/dist/prefs/config.d.ts +108 -0
  74. package/dist/prefs/config.d.ts.map +1 -0
  75. package/dist/prefs/config.js +926 -0
  76. package/dist/prefs/config.js.map +1 -0
  77. package/dist/prefs/import-discovery.d.ts +28 -0
  78. package/dist/prefs/import-discovery.d.ts.map +1 -0
  79. package/dist/prefs/import-discovery.js +562 -0
  80. package/dist/prefs/import-discovery.js.map +1 -0
  81. package/dist/prefs/import-instructions.d.ts +3 -0
  82. package/dist/prefs/import-instructions.d.ts.map +1 -0
  83. package/{src/prefs/import-instructions.ts → dist/prefs/import-instructions.js} +1 -0
  84. package/dist/prefs/import-instructions.js.map +1 -0
  85. package/dist/prefs/models.d.ts +24 -0
  86. package/dist/prefs/models.d.ts.map +1 -0
  87. package/dist/prefs/models.js +57 -0
  88. package/dist/prefs/models.js.map +1 -0
  89. package/dist/prefs/settings.d.ts +89 -0
  90. package/dist/prefs/settings.d.ts.map +1 -0
  91. package/dist/prefs/settings.js +505 -0
  92. package/dist/prefs/settings.js.map +1 -0
  93. package/dist/types.d.ts +293 -0
  94. package/dist/types.d.ts.map +1 -0
  95. package/dist/types.js +2 -0
  96. package/dist/types.js.map +1 -0
  97. package/dist/ui/agent-loader.d.ts +21 -0
  98. package/dist/ui/agent-loader.d.ts.map +1 -0
  99. package/dist/ui/agent-loader.js +73 -0
  100. package/dist/ui/agent-loader.js.map +1 -0
  101. package/dist/ui/agent-navigation-editor.d.ts +46 -0
  102. package/dist/ui/agent-navigation-editor.d.ts.map +1 -0
  103. package/dist/ui/agent-navigation-editor.js +189 -0
  104. package/dist/ui/agent-navigation-editor.js.map +1 -0
  105. package/dist/ui/cost-footer.d.ts +37 -0
  106. package/dist/ui/cost-footer.d.ts.map +1 -0
  107. package/dist/ui/cost-footer.js +215 -0
  108. package/dist/ui/cost-footer.js.map +1 -0
  109. package/dist/ui/dialog.d.ts +135 -0
  110. package/dist/ui/dialog.d.ts.map +1 -0
  111. package/dist/ui/dialog.js +533 -0
  112. package/dist/ui/dialog.js.map +1 -0
  113. package/dist/ui/import-picker.d.ts +38 -0
  114. package/dist/ui/import-picker.d.ts.map +1 -0
  115. package/dist/ui/import-picker.js +211 -0
  116. package/dist/ui/import-picker.js.map +1 -0
  117. package/dist/ui/live-agent-view.d.ts +82 -0
  118. package/dist/ui/live-agent-view.d.ts.map +1 -0
  119. package/dist/ui/live-agent-view.js +546 -0
  120. package/dist/ui/live-agent-view.js.map +1 -0
  121. package/dist/ui/model-picker.d.ts +109 -0
  122. package/dist/ui/model-picker.d.ts.map +1 -0
  123. package/dist/ui/model-picker.js +549 -0
  124. package/dist/ui/model-picker.js.map +1 -0
  125. package/dist/ui/settings-ui.d.ts +17 -0
  126. package/dist/ui/settings-ui.d.ts.map +1 -0
  127. package/dist/ui/settings-ui.js +679 -0
  128. package/dist/ui/settings-ui.js.map +1 -0
  129. package/dist/ui/status-ui.d.ts +67 -0
  130. package/dist/ui/status-ui.d.ts.map +1 -0
  131. package/dist/ui/status-ui.js +487 -0
  132. package/dist/ui/status-ui.js.map +1 -0
  133. package/dist/ui/thread-tree.d.ts +22 -0
  134. package/dist/ui/thread-tree.d.ts.map +1 -0
  135. package/dist/ui/thread-tree.js +153 -0
  136. package/dist/ui/thread-tree.js.map +1 -0
  137. package/dist/ui/tool-picker.d.ts +46 -0
  138. package/dist/ui/tool-picker.d.ts.map +1 -0
  139. package/dist/ui/tool-picker.js +334 -0
  140. package/dist/ui/tool-picker.js.map +1 -0
  141. package/dist/ui/transcript-tool.d.ts +2 -0
  142. package/dist/ui/transcript-tool.d.ts.map +1 -0
  143. package/dist/ui/transcript-tool.js +100 -0
  144. package/dist/ui/transcript-tool.js.map +1 -0
  145. package/dist/ui/ui.d.ts +41 -0
  146. package/dist/ui/ui.d.ts.map +1 -0
  147. package/dist/ui/ui.js +1047 -0
  148. package/dist/ui/ui.js.map +1 -0
  149. package/docs/development.md +2 -1
  150. package/package.json +16 -4
  151. package/src/index.ts +0 -920
  152. package/src/orch/checklist-integration.ts +0 -230
  153. package/src/orch/context-entries.ts +0 -15
  154. package/src/orch/cost-ledger.ts +0 -56
  155. package/src/orch/diagnostics.ts +0 -95
  156. package/src/orch/extensions.ts +0 -83
  157. package/src/orch/housekeeping-usage.ts +0 -47
  158. package/src/orch/housekeeping.ts +0 -348
  159. package/src/orch/inherited-tools.ts +0 -84
  160. package/src/orch/mailbox.ts +0 -261
  161. package/src/orch/manager.ts +0 -1084
  162. package/src/orch/paths.ts +0 -73
  163. package/src/orch/prompt.ts +0 -39
  164. package/src/orch/registry.ts +0 -168
  165. package/src/orch/runtime.ts +0 -1041
  166. package/src/orch/tools.ts +0 -190
  167. package/src/orch/transcript.ts +0 -186
  168. package/src/prefs/agent-import.ts +0 -245
  169. package/src/prefs/config.ts +0 -1017
  170. package/src/prefs/import-discovery.ts +0 -643
  171. package/src/prefs/models.ts +0 -74
  172. package/src/prefs/settings.ts +0 -614
  173. package/src/types.ts +0 -271
  174. package/src/ui/agent-loader.ts +0 -98
  175. package/src/ui/agent-navigation-editor.ts +0 -233
  176. package/src/ui/cost-footer.ts +0 -230
  177. package/src/ui/dialog.ts +0 -678
  178. package/src/ui/import-picker.ts +0 -231
  179. package/src/ui/live-agent-view.ts +0 -600
  180. package/src/ui/model-picker.ts +0 -701
  181. package/src/ui/settings-ui.ts +0 -830
  182. package/src/ui/status-ui.ts +0 -628
  183. package/src/ui/thread-tree.ts +0 -173
  184. package/src/ui/tool-picker.ts +0 -398
  185. package/src/ui/transcript-tool.ts +0 -142
  186. package/src/ui/ui.ts +0 -1407
@@ -1,628 +0,0 @@
1
- import type {
2
- ExtensionCommandContext,
3
- ExtensionContext,
4
- ExtensionUIContext,
5
- Theme,
6
- } from "@earendil-works/pi-coding-agent";
7
- import {
8
- getKeybindings,
9
- setKeybindings,
10
- Key,
11
- matchesKey,
12
- type Keybinding,
13
- } from "@earendil-works/pi-tui";
14
- import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
15
- import {
16
- canOpenDialog,
17
- DIALOG_OPTIONS,
18
- type DialogHost,
19
- DialogSession,
20
- scopeDialogContext,
21
- dialogMenu,
22
- dialogEditor,
23
- dialogHeight,
24
- dialogText,
25
- frameDialog,
26
- } from "./dialog.ts";
27
- import type { ThreadService } from "../types.ts";
28
- import type { LoaderStyle } from "../prefs/settings.ts";
29
- import { buildStatusTree, threadActivity, type StatusRow } from "./thread-tree.ts";
30
- import {
31
- AGENT_PROGRESS_INTERVAL,
32
- agentProgressIcon,
33
- agentTypeBadge,
34
- agentTypeLabel,
35
- showThreads,
36
- threadMetrics,
37
- } from "./ui.ts";
38
- import { LiveAgentView, fillViewport, type AgentViewportState } from "./live-agent-view.ts";
39
-
40
- export { buildStatusTree, type StatusRow };
41
-
42
- const ROOT = "/root";
43
- const INACTIVE_STATES = new Set(["paused", "stopped", "failed", "completed"]);
44
- const TITLE = "Agents status";
45
- const TREE_TITLE = "Agents tree";
46
- const FOOTER =
47
- "Esc close · ↑↓ select · ←→ fold · PgUp/PgDn · Enter inspect · r refresh · Ctrl+C stop all";
48
-
49
- function agentCount(threads: readonly { path?: string }[]): number {
50
- const paths = new Set<string>();
51
- for (const thread of threads) {
52
- if (thread?.path && thread.path !== ROOT) paths.add(thread.path);
53
- }
54
- return paths.size;
55
- }
56
-
57
- function lexicalParent(path: string): string | null {
58
- const index = path.lastIndexOf("/");
59
- return index > 0 ? path.slice(0, index) : null;
60
- }
61
-
62
- function treeParent(rows: StatusRow[], index: number): StatusRow | undefined {
63
- const depth = rows[index]?.prefix.length ?? 0;
64
- for (let cursor = index - 1; cursor >= 0; cursor--) {
65
- if (rows[cursor]!.prefix.length < depth) return rows[cursor];
66
- }
67
- return undefined;
68
- }
69
-
70
- function firstChild(rows: StatusRow[], index: number): StatusRow | undefined {
71
- const next = rows[index + 1];
72
- return next && next.prefix.length > (rows[index]?.prefix.length ?? 0) ? next : undefined;
73
- }
74
-
75
- function label(row: StatusRow): string {
76
- if (!row.thread)
77
- return row.path === ROOT
78
- ? `${dialogText(row.path)} main running main`
79
- : `${dialogText(row.path)} missing parent`;
80
- return [row.path, row.thread.type, row.thread.state, row.thread.status]
81
- .map((part) => dialogText(part))
82
- .join(" ");
83
- }
84
-
85
- export interface AgentTreeState {
86
- selectedPath: string;
87
- collapsed: Set<string>;
88
- }
89
-
90
- export class StatusDialog {
91
- private state: AgentTreeState;
92
- private get selectedPath(): string {
93
- return this.state.selectedPath;
94
- }
95
- private set selectedPath(value: string) {
96
- this.state.selectedPath = value;
97
- }
98
- private get collapsed(): Set<string> {
99
- return this.state.collapsed;
100
- }
101
- private viewport = 1;
102
- private timer: ReturnType<typeof setInterval> | undefined;
103
- private refreshDelay = 0;
104
- private refreshing = false;
105
-
106
- constructor(
107
- private host: DialogHost,
108
- private theme: Theme,
109
- private service: ThreadService,
110
- private done: (path: string | undefined) => void,
111
- selected?: string,
112
- private title = TITLE,
113
- private navigation?: {
114
- state: AgentTreeState;
115
- actions(path: string): void;
116
- fullscreen?: boolean;
117
- /** Right on a leaf steps tree → widget; called just before the tree closes. */
118
- returnToWidget?(): void;
119
- },
120
- private nerdFontIcons = false,
121
- private loaderStyle: LoaderStyle = "circle",
122
- ) {
123
- this.state = navigation?.state ?? { selectedPath: selected || ROOT, collapsed: new Set() };
124
- }
125
-
126
- invalidate(): void {}
127
-
128
- /** Poll retained status, accelerating the existing timer only for animated live agents. */
129
- startRefresh(): void {
130
- this.stopRefresh();
131
- this.refreshing = true;
132
- this.syncRefresh();
133
- }
134
-
135
- /** Stop the refresh. Safe to call more than once, including from dispose(). */
136
- dispose(): void {
137
- this.refreshing = false;
138
- this.stopRefresh();
139
- }
140
-
141
- private syncRefresh(): void {
142
- if (!this.refreshing) return;
143
- const delay = this.service
144
- .list()
145
- .some(
146
- (thread) =>
147
- thread.path !== ROOT && (thread.state === "starting" || thread.state === "running"),
148
- )
149
- ? AGENT_PROGRESS_INTERVAL
150
- : 1000;
151
- if (this.timer !== undefined && this.refreshDelay === delay) return;
152
- this.stopRefresh();
153
- this.refreshDelay = delay;
154
- this.timer = setInterval(() => {
155
- this.syncRefresh();
156
- this.host.requestRender();
157
- }, delay);
158
- if (typeof this.timer.unref === "function") this.timer.unref();
159
- }
160
-
161
- private stopRefresh(): void {
162
- if (this.timer === undefined) return;
163
- clearInterval(this.timer);
164
- this.timer = undefined;
165
- this.refreshDelay = 0;
166
- }
167
-
168
- /** Stop every live agent. Stopping a subtree covers its descendants, so only top-most live paths are sent. */
169
- private stopAll(): void {
170
- const live = this.service
171
- .list()
172
- .filter(
173
- (thread) =>
174
- thread.path !== ROOT && (thread.state === "starting" || thread.state === "running"),
175
- )
176
- .map((thread) => thread.path);
177
- const tops = live.filter((path) => !live.some((other) => path.startsWith(`${other}/`)));
178
- for (const path of tops) {
179
- void this.service
180
- .stop(path)
181
- .catch(() => {})
182
- .finally(() => this.host.requestRender());
183
- }
184
- this.host.requestRender();
185
- }
186
-
187
- private rows(): StatusRow[] {
188
- return buildStatusTree(this.service.list(), this.collapsed);
189
- }
190
-
191
- private locate(rows: StatusRow[]): number {
192
- const exact = rows.findIndex((row) => row.path === this.selectedPath);
193
- if (exact >= 0) return exact;
194
- let parent = lexicalParent(this.selectedPath);
195
- while (parent) {
196
- const index = rows.findIndex((row) => row.path === parent);
197
- if (index >= 0) return index;
198
- parent = lexicalParent(parent);
199
- }
200
- const root = rows.findIndex((row) => row.path === ROOT);
201
- return root >= 0 ? root : 0;
202
- }
203
-
204
- handleInput(data: string): void {
205
- const keys = getKeybindings();
206
- if (matchesKey(data, Key.ctrl("c"))) {
207
- this.stopAll();
208
- return;
209
- }
210
- if (keys.matches(data, "tui.select.cancel")) {
211
- this.done(undefined);
212
- return;
213
- }
214
- const rows = this.rows();
215
- const index = this.locate(rows);
216
- const row = rows[index];
217
- if (row) this.selectedPath = row.path;
218
- if (keys.matches(data, "tui.select.confirm")) {
219
- if (this.navigation && row?.path === ROOT) this.done(undefined);
220
- else if (row?.thread && row.path !== ROOT) this.done(row.path);
221
- return;
222
- }
223
- if (data === "i" && this.navigation) {
224
- if (row?.thread && row.path !== ROOT) this.navigation.actions(row.path);
225
- return;
226
- }
227
- if (matchesKey(data, Key.ctrl("q")) && this.navigation) {
228
- this.done(undefined);
229
- return;
230
- }
231
- if (data === "r") {
232
- this.host.requestRender();
233
- return;
234
- }
235
- if (matchesKey(data, Key.left)) {
236
- if (row?.hasChildren && !this.collapsed.has(row.path)) this.collapsed.add(row.path);
237
- else if (row) {
238
- const parent = treeParent(rows, index);
239
- if (parent) this.selectedPath = parent.path;
240
- }
241
- } else if (matchesKey(data, Key.right)) {
242
- if (row?.hasChildren && this.collapsed.has(row.path)) this.collapsed.delete(row.path);
243
- else {
244
- const child = row?.hasChildren ? firstChild(rows, index) : undefined;
245
- if (child) this.selectedPath = child.path;
246
- else if (this.navigation?.fullscreen) {
247
- // Nothing left to fold or descend into: the next step right is the widget.
248
- this.navigation.returnToWidget?.();
249
- this.done(undefined);
250
- return;
251
- }
252
- }
253
- } else {
254
- let next = index;
255
- if (keys.matches(data, "tui.select.up")) next -= 1;
256
- else if (keys.matches(data, "tui.select.down")) next += 1;
257
- else if (matchesKey(data, Key.pageUp)) next -= this.viewport;
258
- else if (matchesKey(data, Key.pageDown)) next += this.viewport;
259
- else if (matchesKey(data, Key.home)) next = 0;
260
- else if (matchesKey(data, Key.end)) next = Math.max(0, rows.length - 1);
261
- else return;
262
- this.selectedPath =
263
- rows[Math.max(0, Math.min(rows.length - 1, next))]?.path ?? this.selectedPath;
264
- }
265
- this.host.requestRender();
266
- }
267
-
268
- private line(row: StatusRow, selected: boolean, width: number): string[] {
269
- const marker = row.hasChildren ? (this.collapsed.has(row.path) ? "▸ " : "▾ ") : "";
270
- const head = ` ${row.prefix}${selected ? "›" : " "} ${marker}`;
271
- const thread = row.thread;
272
- if (!thread) {
273
- const text = `${head}${label(row)}`;
274
- return [selected ? this.theme.fg("accent", text) : text, ""];
275
- }
276
- const inactive = INACTIVE_STATES.has(thread.state);
277
- const base = thread.state === "completed" ? "dim" : "muted";
278
- const hint =
279
- thread.state === "completed" ? "success" : thread.state === "failed" ? "error" : base;
280
- const progress = agentProgressIcon(
281
- thread,
282
- this.nerdFontIcons,
283
- Date.now(),
284
- true,
285
- this.loaderStyle,
286
- );
287
- const badge = inactive
288
- ? this.theme.fg(base, agentTypeLabel(thread.type, thread.icon, this.nerdFontIcons, progress))
289
- : agentTypeBadge(
290
- thread.type,
291
- thread.color,
292
- this.theme,
293
- thread.icon,
294
- this.nerdFontIcons,
295
- progress,
296
- );
297
- const identity = [
298
- thread.path,
299
- `${thread.model ?? "model pending"}${thread.thinkingLevel ? `:${thread.thinkingLevel}` : ""}`,
300
- ]
301
- .map((part) => dialogText(part))
302
- .join(" ");
303
- const identityLine = [
304
- selected ? this.theme.fg("accent", head) : head,
305
- badge,
306
- this.theme.fg(inactive ? base : selected ? "accent" : "text", ` ${identity} `),
307
- this.theme.fg(inactive ? hint : "muted", dialogText(thread.state)),
308
- ].join("");
309
- // Continue the tree's vertical guides without repeating the branch or selection marker.
310
- const stem = row.prefix.replace("├ ", "│ ").replace("└ ", " ");
311
- const activity = ` ${stem} ${dialogText(threadActivity(thread))}`;
312
- const metrics = this.theme.fg(base, threadMetrics(thread));
313
- const metricWidth = visibleWidth(metrics);
314
- const first =
315
- metricWidth >= width
316
- ? truncateToWidth(metrics, width, "")
317
- : (() => {
318
- const left = truncateToWidth(identityLine, width - metricWidth - 1);
319
- return (
320
- left + " ".repeat(Math.max(1, width - visibleWidth(left) - metricWidth)) + metrics
321
- );
322
- })();
323
- const second = truncateToWidth(
324
- this.theme.fg(inactive ? base : selected ? "accent" : "muted", activity),
325
- width,
326
- );
327
- return [first, second];
328
- }
329
-
330
- private detail(row: StatusRow | undefined): string[] {
331
- if (!row?.thread) {
332
- return [
333
- ` ${row?.path === ROOT ? (agentCount(this.service.list()) ? "Main Pi session" : "Main Pi session — No agents yet") : "Missing parent"}`,
334
- "",
335
- ];
336
- }
337
- return [` task ${dialogText(row.thread.task)}`, ` ${threadMetrics(row.thread)}`];
338
- }
339
-
340
- render(width: number): string[] {
341
- this.syncRefresh();
342
- const rows = this.rows();
343
- const height = this.navigation?.fullscreen
344
- ? Math.max(1, this.host.terminal?.rows ?? 24)
345
- : dialogHeight(this.host);
346
- const index = this.locate(rows);
347
- if (rows[index]) this.selectedPath = rows[index].path;
348
- const budget = Math.max(0, height - 4);
349
- const detail = budget > 2 ? this.detail(rows[index]) : [];
350
- const treeCount = Math.max(0, Math.floor((budget - detail.length) / 2));
351
- this.viewport = Math.max(1, treeCount);
352
- const start = treeCount
353
- ? Math.max(0, Math.min(index - treeCount + 1, Math.max(0, rows.length - treeCount)))
354
- : 0;
355
- const body = [
356
- ...rows
357
- .slice(start, start + treeCount)
358
- .flatMap((row) => this.line(row, row.path === this.selectedPath, Math.max(0, width - 2))),
359
- ...detail,
360
- ];
361
- return frameDialog(
362
- this.theme,
363
- width,
364
- height,
365
- this.heading(rows, start, treeCount),
366
- body,
367
- this.navigation
368
- ? "Esc main · ↑↓ select · ←→ fold · → widget · i Actions · Ctrl+C stop all"
369
- : FOOTER,
370
- );
371
- }
372
-
373
- private heading(rows: StatusRow[], start: number, treeCount: number): string {
374
- if (this.title !== TREE_TITLE) return this.title;
375
- const agents = agentCount(this.service.list());
376
- const noun = agents === 1 ? "agent" : "agents";
377
- const total = rows.length;
378
- const shown = Math.max(0, Math.min(treeCount, total - start));
379
- if (shown <= 0) return `${TREE_TITLE} ${agents} ${noun}`;
380
- return `${TREE_TITLE} ${agents} ${noun} ${start + 1}-${start + shown}/${total}`;
381
- }
382
- }
383
-
384
- async function showAgentDialog(
385
- ctx: ExtensionCommandContext,
386
- service: ThreadService,
387
- options: {
388
- title?: string;
389
- selected?: string;
390
- inspect?: (path: string) => Promise<void>;
391
- nerdFontIcons?: boolean;
392
- loaderStyle?: LoaderStyle;
393
- } = {},
394
- ): Promise<void> {
395
- const open = options.inspect ?? ((path: string) => showThreads(ctx, service, path));
396
- let selected = options.selected;
397
- for (;;) {
398
- // Cleared on close and from dispose(): DialogSession host disposal can
399
- // leave this promise pending, so finally alone may not run immediately.
400
- let dialog: StatusDialog | undefined;
401
- try {
402
- const chosen = await ctx.ui.custom<string | undefined>((host, theme, _keys, done) => {
403
- dialog = new StatusDialog(
404
- host,
405
- theme,
406
- service,
407
- done,
408
- selected,
409
- options.title,
410
- undefined,
411
- options.nerdFontIcons,
412
- options.loaderStyle,
413
- );
414
- dialog.startRefresh();
415
- return dialog;
416
- }, DIALOG_OPTIONS);
417
- dialog?.dispose();
418
- dialog = undefined;
419
- if (!chosen) return;
420
- selected = chosen;
421
- await open(chosen);
422
- } finally {
423
- dialog?.dispose();
424
- }
425
- }
426
- }
427
-
428
- export async function showAgentStatus(
429
- ctx: ExtensionCommandContext,
430
- service: ThreadService,
431
- inspect?: (path: string) => Promise<void>,
432
- nerdFontIcons = false,
433
- loaderStyle: LoaderStyle = "circle",
434
- ): Promise<void> {
435
- if (!canOpenDialog(ctx)) return;
436
- await showAgentDialog(ctx, service, { inspect, nerdFontIcons, loaderStyle });
437
- }
438
-
439
- /** Root-owned navigation state. Opening and closing never changes the executing session. */
440
- export class AgentNavigationController {
441
- private tree: AgentTreeState = { selectedPath: ROOT, collapsed: new Set() };
442
- private viewports = new Map<string, AgentViewportState>();
443
- private session: DialogSession | undefined;
444
- private opening: Promise<void> | undefined;
445
- private generation = 0;
446
-
447
- constructor(private returnToWidget?: () => void) {}
448
-
449
- get isOpen(): boolean {
450
- return this.opening !== undefined;
451
- }
452
-
453
- close(): void {
454
- ++this.generation;
455
- this.session?.close();
456
- this.session = undefined;
457
- }
458
-
459
- open(
460
- ctx: ExtensionContext,
461
- service: ThreadService,
462
- selectedPath?: string,
463
- nerdFontIcons = false,
464
- loaderStyle: LoaderStyle = "circle",
465
- ): Promise<void> {
466
- if (this.opening) return this.opening;
467
- if (!canOpenDialog(ctx)) return Promise.resolve();
468
- if (selectedPath) this.tree.selectedPath = selectedPath;
469
- const generation = ++this.generation;
470
- let failure: unknown;
471
- let failed = false;
472
- let ownedSession: DialogSession | undefined;
473
- const task = ctx.ui.custom<void>(
474
- (host, theme, keys, done) => {
475
- const session = new DialogSession(host, theme, keys, done);
476
- ownedSession = session;
477
- if (generation !== this.generation) {
478
- void Promise.resolve().then(() => session.close());
479
- return session;
480
- }
481
- this.session = session;
482
- // The alt-screen renderer's listener runs before extension listeners. Temporarily
483
- // remove only its search action from the public keybinding lookup, before dispatch.
484
- const previousKeybindings = getKeybindings();
485
- const watchingKeybindings = new Proxy(previousKeybindings, {
486
- get(target, property) {
487
- if (property === "matches")
488
- return (data: string, action: Keybinding) =>
489
- action === "tui.altScreen.search" ? false : target.matches(data, action);
490
- // Preserve all other public manager methods and their original receiver.
491
- const value = Reflect.get(target, property, target);
492
- return typeof value === "function" ? value.bind(target) : value;
493
- },
494
- });
495
- setKeybindings(watchingKeybindings);
496
- const dispose = session.dispose.bind(session);
497
- session.dispose = () => {
498
- // Do not overwrite a newer keybinding owner installed by another extension.
499
- if (getKeybindings() === watchingKeybindings) setKeybindings(previousKeybindings);
500
- dispose();
501
- };
502
- // One fullscreen surface also covers shorter Actions dialogs without exposing main.
503
- const render = session.render.bind(session);
504
- session.render = (width) => fillViewport(render(width), width, host.terminal.rows);
505
- const scoped = scopeDialogContext(ctx, session);
506
- // Built-in UI dialogs otherwise stack independently. Mount these actions in our child slot.
507
- const ui = scoped.ui as ExtensionUIContext;
508
- ui.select = (title, options) =>
509
- dialogMenu(
510
- scoped,
511
- title,
512
- options.map((label) => ({ id: label, label })),
513
- );
514
- ui.confirm = async (title, message) =>
515
- (await dialogMenu(
516
- scoped,
517
- title,
518
- [
519
- { id: "Yes", label: "Yes", help: message },
520
- { id: "No", label: "No", help: message },
521
- ],
522
- { selectedId: "No" },
523
- )) === "Yes";
524
- ui.editor = (title, prefill) => dialogEditor(scoped, title, prefill ?? "");
525
- // Defer until Pi has installed/focused the overlay.
526
- void Promise.resolve()
527
- .then(async () => {
528
- while (generation === this.generation) {
529
- let treeDialog: StatusDialog | undefined;
530
- const chosen = await session.mount<
531
- { kind: "watch" | "actions"; path: string } | undefined
532
- >((tui, activeTheme, _keys, finish) => {
533
- treeDialog = new StatusDialog(
534
- tui,
535
- activeTheme,
536
- service,
537
- (path) => finish(path ? { kind: "watch", path } : undefined),
538
- undefined,
539
- TREE_TITLE,
540
- {
541
- state: this.tree,
542
- fullscreen: true,
543
- returnToWidget: this.returnToWidget,
544
- actions: (path) => finish({ kind: "actions", path }),
545
- },
546
- nerdFontIcons,
547
- loaderStyle,
548
- );
549
- treeDialog.startRefresh();
550
- return treeDialog;
551
- });
552
- treeDialog?.dispose();
553
- if (!chosen || generation !== this.generation) break;
554
- if (chosen.kind === "actions") {
555
- await showThreads(scoped, service, chosen.path);
556
- continue;
557
- }
558
- let viewport = this.viewports.get(chosen.path);
559
- if (!viewport) {
560
- viewport = { scrollTop: 0, follow: true };
561
- this.viewports.set(chosen.path, viewport);
562
- }
563
- const result = await session.mount<"back" | "main">(
564
- (tui, activeTheme, _keys, finish) =>
565
- new LiveAgentView(
566
- tui,
567
- activeTheme,
568
- service,
569
- chosen.path,
570
- viewport!,
571
- finish,
572
- nerdFontIcons,
573
- loaderStyle,
574
- ),
575
- );
576
- if (result !== "back") break;
577
- }
578
- })
579
- .then(
580
- () => session.close(),
581
- (error: unknown) => {
582
- failed = true;
583
- failure = error;
584
- ctx.ui.notify(
585
- dialogText(error instanceof Error ? error.message : String(error)),
586
- "error",
587
- );
588
- session.close();
589
- },
590
- );
591
- return session;
592
- },
593
- {
594
- overlay: true,
595
- overlayOptions: { width: "100%", maxHeight: "100%", row: 0, col: 0, margin: 0 },
596
- },
597
- );
598
- this.opening = task
599
- .then(() => {
600
- if (failed) throw failure;
601
- })
602
- .finally(() => {
603
- // Also clean up if the host rejects mounting after our factory returned.
604
- ownedSession?.dispose();
605
- if (this.opening === result) this.opening = undefined;
606
- if (generation === this.generation) this.session = undefined;
607
- });
608
- const result = this.opening;
609
- return result;
610
- }
611
- }
612
-
613
- const navigationByService = new WeakMap<ThreadService, AgentNavigationController>();
614
-
615
- export async function showAgentTree(
616
- ctx: ExtensionContext,
617
- service: ThreadService,
618
- selectedPath?: string,
619
- nerdFontIcons = false,
620
- loaderStyle: LoaderStyle = "circle",
621
- ): Promise<void> {
622
- let navigation = navigationByService.get(service);
623
- if (!navigation) {
624
- navigation = new AgentNavigationController();
625
- navigationByService.set(service, navigation);
626
- }
627
- await navigation.open(ctx, service, selectedPath, nerdFontIcons, loaderStyle);
628
- }