@esso0428/pi-subagents 0.17.5 → 0.17.7
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/CHANGELOG.md +14 -0
- package/CONTRIBUTING.md +4 -0
- package/README.md +1 -1
- package/dist/abortable.d.ts +13 -0
- package/dist/abortable.d.ts.map +1 -0
- package/dist/abortable.js +43 -0
- package/dist/abortable.js.map +1 -0
- package/dist/agent-color.d.ts +36 -0
- package/dist/agent-color.d.ts.map +1 -0
- package/dist/agent-color.js +124 -0
- package/dist/agent-color.js.map +1 -0
- package/dist/agent-file-toggle.d.ts +126 -0
- package/dist/agent-file-toggle.d.ts.map +1 -0
- package/dist/agent-file-toggle.js +259 -0
- package/dist/agent-file-toggle.js.map +1 -0
- package/dist/agent-history.d.ts +4 -0
- package/dist/agent-history.d.ts.map +1 -1
- package/dist/agent-history.js +47 -1
- package/dist/agent-history.js.map +1 -1
- package/dist/agent-manager.d.ts +370 -56
- package/dist/agent-manager.d.ts.map +1 -1
- package/dist/agent-manager.js +1123 -409
- package/dist/agent-manager.js.map +1 -1
- package/dist/agent-runner.d.ts +100 -10
- package/dist/agent-runner.d.ts.map +1 -1
- package/dist/agent-runner.js +166 -21
- package/dist/agent-runner.js.map +1 -1
- package/dist/agent-types.d.ts +57 -5
- package/dist/agent-types.d.ts.map +1 -1
- package/dist/agent-types.js +164 -32
- package/dist/agent-types.js.map +1 -1
- package/dist/child-context.d.ts +3 -0
- package/dist/child-context.d.ts.map +1 -0
- package/dist/child-context.js +13 -0
- package/dist/child-context.js.map +1 -0
- package/dist/cross-extension-rpc.d.ts +23 -3
- package/dist/cross-extension-rpc.d.ts.map +1 -1
- package/dist/cross-extension-rpc.js +79 -17
- package/dist/cross-extension-rpc.js.map +1 -1
- package/dist/custom-agents.d.ts +38 -1
- package/dist/custom-agents.d.ts.map +1 -1
- package/dist/custom-agents.js +164 -12
- package/dist/custom-agents.js.map +1 -1
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1908 -495
- package/dist/index.js.map +1 -1
- package/dist/invocation-config.d.ts +87 -2
- package/dist/invocation-config.d.ts.map +1 -1
- package/dist/invocation-config.js +71 -3
- package/dist/invocation-config.js.map +1 -1
- package/dist/mention-clone.d.ts +88 -0
- package/dist/mention-clone.d.ts.map +1 -0
- package/dist/mention-clone.js +154 -0
- package/dist/mention-clone.js.map +1 -0
- package/dist/mention.d.ts +82 -0
- package/dist/mention.d.ts.map +1 -0
- package/dist/mention.js +132 -0
- package/dist/mention.js.map +1 -0
- package/dist/model-resolver.d.ts +17 -0
- package/dist/model-resolver.d.ts.map +1 -1
- package/dist/model-resolver.js +15 -0
- package/dist/model-resolver.js.map +1 -1
- package/dist/model-scope.d.ts +50 -0
- package/dist/model-scope.d.ts.map +1 -0
- package/dist/model-scope.js +49 -0
- package/dist/model-scope.js.map +1 -0
- package/dist/nested-tools.d.ts +57 -0
- package/dist/nested-tools.d.ts.map +1 -0
- package/dist/nested-tools.js +301 -0
- package/dist/nested-tools.js.map +1 -0
- package/dist/output-file.d.ts +22 -3
- package/dist/output-file.d.ts.map +1 -1
- package/dist/output-file.js +58 -7
- package/dist/output-file.js.map +1 -1
- package/dist/prompts.d.ts +23 -0
- package/dist/prompts.d.ts.map +1 -1
- package/dist/prompts.js +20 -2
- package/dist/prompts.js.map +1 -1
- package/dist/schedule.d.ts.map +1 -1
- package/dist/schedule.js +36 -15
- package/dist/schedule.js.map +1 -1
- package/dist/settings.d.ts +228 -2
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +94 -0
- package/dist/settings.js.map +1 -1
- package/dist/status-note.d.ts +49 -1
- package/dist/status-note.d.ts.map +1 -1
- package/dist/status-note.js +62 -1
- package/dist/status-note.js.map +1 -1
- package/dist/structured-output.d.ts +62 -0
- package/dist/structured-output.d.ts.map +1 -0
- package/dist/structured-output.js +113 -0
- package/dist/structured-output.js.map +1 -0
- package/dist/types.d.ts +176 -10
- package/dist/types.d.ts.map +1 -1
- package/dist/ui/agent-mention.d.ts +83 -0
- package/dist/ui/agent-mention.d.ts.map +1 -0
- package/dist/ui/agent-mention.js +188 -0
- package/dist/ui/agent-mention.js.map +1 -0
- package/dist/ui/agent-widget.d.ts +96 -75
- package/dist/ui/agent-widget.d.ts.map +1 -1
- package/dist/ui/agent-widget.js +397 -420
- package/dist/ui/agent-widget.js.map +1 -1
- package/dist/ui/conversation-blocks.d.ts.map +1 -1
- package/dist/ui/conversation-blocks.js +6 -0
- package/dist/ui/conversation-blocks.js.map +1 -1
- package/dist/ui/conversation-timeline.d.ts +10 -2
- package/dist/ui/conversation-timeline.d.ts.map +1 -1
- package/dist/ui/conversation-timeline.js +130 -23
- package/dist/ui/conversation-timeline.js.map +1 -1
- package/dist/ui/conversation-viewer.d.ts +20 -5
- package/dist/ui/conversation-viewer.d.ts.map +1 -1
- package/dist/ui/conversation-viewer.js +274 -73
- package/dist/ui/conversation-viewer.js.map +1 -1
- package/dist/ui/fleet-list.d.ts +198 -0
- package/dist/ui/fleet-list.d.ts.map +1 -0
- package/dist/ui/fleet-list.js +487 -0
- package/dist/ui/fleet-list.js.map +1 -0
- package/dist/ui/schedule-menu.d.ts.map +1 -1
- package/dist/ui/schedule-menu.js +6 -7
- package/dist/ui/schedule-menu.js.map +1 -1
- package/dist/ui/select-item.d.ts +28 -0
- package/dist/ui/select-item.d.ts.map +1 -0
- package/dist/ui/select-item.js +35 -0
- package/dist/ui/select-item.js.map +1 -0
- package/dist/ui/workflow-card.d.ts +176 -0
- package/dist/ui/workflow-card.d.ts.map +1 -0
- package/dist/ui/workflow-card.js +333 -0
- package/dist/ui/workflow-card.js.map +1 -0
- package/dist/ui/workflow-dialog.d.ts +306 -0
- package/dist/ui/workflow-dialog.d.ts.map +1 -0
- package/dist/ui/workflow-dialog.js +844 -0
- package/dist/ui/workflow-dialog.js.map +1 -0
- package/dist/ui/workflow-menu.d.ts +61 -0
- package/dist/ui/workflow-menu.d.ts.map +1 -0
- package/dist/ui/workflow-menu.js +148 -0
- package/dist/ui/workflow-menu.js.map +1 -0
- package/dist/usage.d.ts +86 -1
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +72 -1
- package/dist/usage.js.map +1 -1
- package/dist/workflow/collisions.d.ts +96 -0
- package/dist/workflow/collisions.d.ts.map +1 -0
- package/dist/workflow/collisions.js +89 -0
- package/dist/workflow/collisions.js.map +1 -0
- package/dist/workflow/entry.d.ts +33 -0
- package/dist/workflow/entry.d.ts.map +1 -0
- package/dist/workflow/entry.js +30 -0
- package/dist/workflow/entry.js.map +1 -0
- package/dist/workflow/host.d.ts +63 -0
- package/dist/workflow/host.d.ts.map +1 -0
- package/dist/workflow/host.js +363 -0
- package/dist/workflow/host.js.map +1 -0
- package/dist/workflow/journal.d.ts +98 -0
- package/dist/workflow/journal.d.ts.map +1 -0
- package/dist/workflow/journal.js +121 -0
- package/dist/workflow/journal.js.map +1 -0
- package/dist/workflow/json-schema.d.ts +52 -0
- package/dist/workflow/json-schema.d.ts.map +1 -0
- package/dist/workflow/json-schema.js +112 -0
- package/dist/workflow/json-schema.js.map +1 -0
- package/dist/workflow/meta.d.ts +68 -0
- package/dist/workflow/meta.d.ts.map +1 -0
- package/dist/workflow/meta.js +318 -0
- package/dist/workflow/meta.js.map +1 -0
- package/dist/workflow/progress.d.ts +225 -0
- package/dist/workflow/progress.d.ts.map +1 -0
- package/dist/workflow/progress.js +362 -0
- package/dist/workflow/progress.js.map +1 -0
- package/dist/workflow/runtime.d.ts +335 -0
- package/dist/workflow/runtime.d.ts.map +1 -0
- package/dist/workflow/runtime.js +831 -0
- package/dist/workflow/runtime.js.map +1 -0
- package/dist/workflow/saved.d.ts +91 -0
- package/dist/workflow/saved.d.ts.map +1 -0
- package/dist/workflow/saved.js +204 -0
- package/dist/workflow/saved.js.map +1 -0
- package/dist/workflow/task.d.ts +137 -0
- package/dist/workflow/task.d.ts.map +1 -0
- package/dist/workflow/task.js +208 -0
- package/dist/workflow/task.js.map +1 -0
- package/dist/workflow/tool-description.d.ts +39 -0
- package/dist/workflow/tool-description.d.ts.map +1 -0
- package/dist/workflow/tool-description.js +200 -0
- package/dist/workflow/tool-description.js.map +1 -0
- package/dist/workflow/worker-source.d.ts +48 -0
- package/dist/workflow/worker-source.d.ts.map +1 -0
- package/dist/workflow/worker-source.js +779 -0
- package/dist/workflow/worker-source.js.map +1 -0
- package/dist/worktree.d.ts +10 -3
- package/dist/worktree.d.ts.map +1 -1
- package/dist/worktree.js +58 -54
- package/dist/worktree.js.map +1 -1
- package/dist/xml.d.ts +11 -0
- package/dist/xml.d.ts.map +1 -0
- package/dist/xml.js +13 -0
- package/dist/xml.js.map +1 -0
- package/docs/rpc.md +183 -0
- package/docs/superpowers/plans/2026-09-30-conversation-viewer-scrollbar.md +216 -0
- package/docs/superpowers/plans/2026-09-30-upstream-event-workflow-partial-history.md +195 -0
- package/docs/superpowers/specs/2026-09-30-conversation-viewer-scrollbar-design.md +82 -0
- package/docs/superpowers/specs/2026-09-30-upstream-event-workflow-partial-history-design.md +49 -0
- package/docs/workflows.md +437 -0
- package/examples/agent-tool-description.md +7 -7
- package/examples/workflows/compose.js +51 -0
- package/examples/workflows/fan-out-audit.js +47 -0
- package/examples/workflows/gated-fix.js +60 -0
- package/examples/workflows/lib/count-child.js +27 -0
- package/examples/workflows/review-panel.js +63 -0
- package/examples/workflows/structured-findings.js +78 -0
- package/package.json +1 -1
- package/src/abortable.ts +43 -0
- package/src/agent-color.ts +161 -0
- package/src/agent-file-toggle.ts +269 -0
- package/src/agent-history.ts +54 -2
- package/src/agent-manager.ts +1263 -402
- package/src/agent-runner.ts +251 -27
- package/src/agent-types.ts +188 -32
- package/src/child-context.ts +15 -0
- package/src/cross-extension-rpc.ts +96 -20
- package/src/custom-agents.ts +170 -13
- package/src/index.ts +2024 -537
- package/src/invocation-config.ts +118 -3
- package/src/mention-clone.ts +196 -0
- package/src/mention.ts +141 -0
- package/src/model-resolver.ts +18 -0
- package/src/model-scope.ts +70 -0
- package/src/nested-tools.ts +424 -0
- package/src/output-file.ts +61 -6
- package/src/prompts.ts +45 -2
- package/src/schedule.ts +35 -14
- package/src/settings.ts +312 -2
- package/src/status-note.ts +66 -1
- package/src/structured-output.ts +130 -0
- package/src/types.ts +177 -10
- package/src/ui/agent-mention.ts +216 -0
- package/src/ui/agent-widget.ts +389 -441
- package/src/ui/conversation-blocks.ts +6 -0
- package/src/ui/conversation-timeline.ts +139 -25
- package/src/ui/conversation-viewer.ts +284 -69
- package/src/ui/fleet-list.ts +558 -0
- package/src/ui/schedule-menu.ts +9 -8
- package/src/ui/select-item.ts +45 -0
- package/src/ui/workflow-card.ts +470 -0
- package/src/ui/workflow-dialog.ts +1115 -0
- package/src/ui/workflow-menu.ts +193 -0
- package/src/usage.ts +109 -2
- package/src/workflow/collisions.ts +123 -0
- package/src/workflow/entry.ts +47 -0
- package/src/workflow/host.ts +403 -0
- package/src/workflow/journal.ts +164 -0
- package/src/workflow/json-schema.ts +128 -0
- package/src/workflow/meta.ts +325 -0
- package/src/workflow/progress.ts +550 -0
- package/src/workflow/runtime.ts +1219 -0
- package/src/workflow/saved.ts +217 -0
- package/src/workflow/task.ts +302 -0
- package/src/workflow/tool-description.ts +200 -0
- package/src/workflow/worker-source.ts +781 -0
- package/src/worktree.ts +69 -55
- package/src/xml.ts +13 -0
- package/vitest.config.ts +0 -18
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fleet-list.ts — Claude Code-style "FleetView" list rendered below the editor.
|
|
3
|
+
*
|
|
4
|
+
* Shows `main` + each running/queued subagent as a navigable list. Pressing ↓ (or
|
|
5
|
+
* ←) at an empty prompt activates the list; ↑/↓ move the selection (filled ● marker),
|
|
6
|
+
* Enter opens the selected agent's live conversation overlay, Esc returns to the prompt.
|
|
7
|
+
* A viewer stays open when its agent finishes; finished agents linger briefly in the list.
|
|
8
|
+
*
|
|
9
|
+
* Mechanics (see plan): the list is a `belowEditor` widget (render-only), and ALL key
|
|
10
|
+
* handling goes through `onTerminalInput` — which fires before the focused editor and
|
|
11
|
+
* can `consume` keys — gated on `getEditorText() === ""` so normal typing is untouched.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { Editor, isKeyRelease, Key, matchesKey, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
15
|
+
import { hasAgentBadge, renderAgentName } from "../agent-color.js";
|
|
16
|
+
import { readAgentHistory } from "../agent-history.js";
|
|
17
|
+
import { type AgentManager, isTopLevelAgent } from "../agent-manager.js";
|
|
18
|
+
import type { AgentRecord, ViewerMarkdownMode } from "../types.js";
|
|
19
|
+
import { getLifetimeCost, getLifetimeTotal } from "../usage.js";
|
|
20
|
+
import { type AgentActivity, formatCost, type Theme } from "./agent-widget.js";
|
|
21
|
+
import { ConversationViewer, createStaticConversationSource, VIEWPORT_HEIGHT_PCT } from "./conversation-viewer.js";
|
|
22
|
+
|
|
23
|
+
/** Widget key for the below-editor fleet list. */
|
|
24
|
+
const FLEET_KEY = "fleet";
|
|
25
|
+
/** Max agent rows shown at once; extras collapse into a "↓ N more" indicator. */
|
|
26
|
+
const MAX_AGENT_ROWS = 5;
|
|
27
|
+
/** Re-render cadence so elapsed/token stats tick while agents run. */
|
|
28
|
+
const TICK_MS = 200;
|
|
29
|
+
/** How long a finished agent lingers in the list before it drops out. */
|
|
30
|
+
const FINISHED_LINGER_MS = 4000;
|
|
31
|
+
|
|
32
|
+
/** Minimal UI surface the FleetView needs from `ctx.ui` (structural subset). */
|
|
33
|
+
export type FleetUICtx = {
|
|
34
|
+
setWidget(
|
|
35
|
+
key: string,
|
|
36
|
+
content: undefined | ((tui: any, theme: Theme) => { render(width: number): string[]; invalidate(): void; dispose?(): void }),
|
|
37
|
+
options?: { placement?: "aboveEditor" | "belowEditor" },
|
|
38
|
+
): void;
|
|
39
|
+
onTerminalInput(handler: (data: string) => { consume?: boolean; data?: string } | undefined): () => void;
|
|
40
|
+
getEditorText(): string;
|
|
41
|
+
notify(message: string, type?: "info" | "warning" | "error"): void;
|
|
42
|
+
custom<T>(
|
|
43
|
+
factory: (tui: any, theme: Theme, keybindings: any, done: (result: T) => void) => { render(width: number): string[]; invalidate(): void; dispose?(): void },
|
|
44
|
+
options?: { overlay?: boolean; overlayOptions?: unknown; onHandle?: (handle: unknown) => void },
|
|
45
|
+
): Promise<T>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A workflow run, as the fleet list needs to see it.
|
|
50
|
+
*
|
|
51
|
+
* Narrow on purpose: the list knows nothing about `WorkflowTask`, the runtime
|
|
52
|
+
* or the dialog, so it stays as testable as it was when it only held agents.
|
|
53
|
+
* The extension maps its tasks into this shape and injects an opener.
|
|
54
|
+
*/
|
|
55
|
+
export interface FleetWorkflow {
|
|
56
|
+
id: string;
|
|
57
|
+
/** The `meta.name` of the run, or its id when the script named nothing. */
|
|
58
|
+
name: string;
|
|
59
|
+
status: "running" | "completed" | "failed" | "killed" | "paused";
|
|
60
|
+
doneCount: number;
|
|
61
|
+
totalCount: number;
|
|
62
|
+
startedAt: number;
|
|
63
|
+
/** Set once the run settles, which is what freezes its clock. */
|
|
64
|
+
completedAt?: number;
|
|
65
|
+
tokens: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type MainEntry = { kind: "main" };
|
|
69
|
+
type AgentEntry = { kind: "agent"; record: AgentRecord };
|
|
70
|
+
type WorkflowEntry = { kind: "workflow"; workflow: FleetWorkflow };
|
|
71
|
+
type FleetEntry = MainEntry | WorkflowEntry | AgentEntry;
|
|
72
|
+
|
|
73
|
+
/** `11s` — integer seconds, no decimal/suffix (matches Claude Code, unlike formatMs). */
|
|
74
|
+
export function formatFleetElapsed(ms: number): string {
|
|
75
|
+
return `${Math.max(0, Math.round(ms / 1000))}s`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** `↓ 13.1k tokens` — down-arrow prefix, compact magnitude, plural "tokens". */
|
|
79
|
+
export function formatFleetTokens(count: number): string {
|
|
80
|
+
let compact: string;
|
|
81
|
+
if (count >= 1_000_000) compact = `${(count / 1_000_000).toFixed(1)}M`;
|
|
82
|
+
else if (count >= 1_000) compact = `${(count / 1_000).toFixed(1)}k`;
|
|
83
|
+
else compact = `${count}`;
|
|
84
|
+
return `↓ ${compact} tokens`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Place `right` flush to `width`, truncating `left` first so the stats survive.
|
|
89
|
+
* The final clamp guarantees the line never exceeds `width` (which would wrap and
|
|
90
|
+
* desync pi's line-diff → flicker) even on a terminal too narrow for the stats.
|
|
91
|
+
*/
|
|
92
|
+
function rightAlign(left: string, right: string, width: number): string {
|
|
93
|
+
const rightW = visibleWidth(right);
|
|
94
|
+
const maxLeft = Math.max(0, width - rightW - 1);
|
|
95
|
+
const leftClamped = truncateToWidth(left, maxLeft);
|
|
96
|
+
const gap = Math.max(1, width - visibleWidth(leftClamped) - rightW);
|
|
97
|
+
return truncateToWidth(leftClamped + " ".repeat(gap) + right, width);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export class FleetList {
|
|
101
|
+
private ui: FleetUICtx | undefined;
|
|
102
|
+
private tui: any | undefined;
|
|
103
|
+
private inputUnsub: (() => void) | undefined;
|
|
104
|
+
private widgetRegistered = false;
|
|
105
|
+
private timer: ReturnType<typeof setInterval> | undefined;
|
|
106
|
+
|
|
107
|
+
private enabled = true;
|
|
108
|
+
/** Whether arrow keys currently navigate the list (vs. flow to the editor). */
|
|
109
|
+
private active = false;
|
|
110
|
+
/** 0 = `main`, 1..N = subagents. */
|
|
111
|
+
private selectedIndex = 0;
|
|
112
|
+
/** Set while a conversation overlay is open; calling it closes the overlay. */
|
|
113
|
+
private viewerClose: (() => void) | undefined;
|
|
114
|
+
private viewingAgentId: string | undefined;
|
|
115
|
+
/** Injected by the extension; absent until workflows are wired (or at all). */
|
|
116
|
+
private workflowSource: (() => readonly FleetWorkflow[]) | undefined;
|
|
117
|
+
private openWorkflow: ((id: string) => Promise<void> | void) | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* Set while the workflow inspector is up.
|
|
120
|
+
*
|
|
121
|
+
* It does the two jobs `viewerClose` does for an agent's overlay — keep the
|
|
122
|
+
* list out of the dialog's keys, and remember which row to come back to —
|
|
123
|
+
* minus the close handle, because that overlay belongs to the extension.
|
|
124
|
+
*/
|
|
125
|
+
private viewingWorkflowId: string | undefined;
|
|
126
|
+
|
|
127
|
+
constructor(
|
|
128
|
+
private manager: AgentManager,
|
|
129
|
+
private agentActivity: Map<string, AgentActivity>,
|
|
130
|
+
/**
|
|
131
|
+
* Read live at render time. Whether each row shows an estimated cost after
|
|
132
|
+
* its token count. Defaults to off — the extension supplies the user's
|
|
133
|
+
* `showCost` setting.
|
|
134
|
+
*/
|
|
135
|
+
private showCost: () => boolean = () => false,
|
|
136
|
+
/**
|
|
137
|
+
* The user's `viewerMarkdown` setting, for a conversation overlay opened
|
|
138
|
+
* from here. Read live rather than captured, because the viewer's `m` key
|
|
139
|
+
* changes it while the overlay is up. Omitted → the viewer's own default.
|
|
140
|
+
*/
|
|
141
|
+
private viewerMarkdown?: () => ViewerMarkdownMode,
|
|
142
|
+
/**
|
|
143
|
+
* Persist a mode chosen with `m` in that overlay, so the key means the same
|
|
144
|
+
* thing here as it does from `/agents` — one setting, not one per entry
|
|
145
|
+
* point. Omitted → `m` still cycles, viewer-locally.
|
|
146
|
+
*/
|
|
147
|
+
private onViewerMarkdown?: (mode: ViewerMarkdownMode) => void,
|
|
148
|
+
private cwd?: string,
|
|
149
|
+
) {}
|
|
150
|
+
|
|
151
|
+
// ---- Lifecycle ----
|
|
152
|
+
|
|
153
|
+
setCwd(cwd: string): void {
|
|
154
|
+
this.cwd = cwd;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
setEnabled(enabled: boolean): void {
|
|
158
|
+
if (enabled === this.enabled) return;
|
|
159
|
+
this.enabled = enabled;
|
|
160
|
+
if (!enabled) this.active = false;
|
|
161
|
+
this.update();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Capture the UI context and (re)register the global input handler. */
|
|
165
|
+
setUICtx(ui: FleetUICtx, registerInput = true): void {
|
|
166
|
+
if (ui === this.ui) {
|
|
167
|
+
if (registerInput && !this.inputUnsub) this.inputUnsub = ui.onTerminalInput(data => this.handleKey(data));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
this.inputUnsub?.();
|
|
171
|
+
this.ui = ui;
|
|
172
|
+
this.widgetRegistered = false;
|
|
173
|
+
this.tui = undefined;
|
|
174
|
+
this.inputUnsub = registerInput ? ui.onTerminalInput(data => this.handleKey(data)) : undefined;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Ensure the re-render timer is running (called when an agent spawns). */
|
|
178
|
+
ensureTimer(): void {
|
|
179
|
+
if (!this.timer) this.timer = setInterval(() => this.update(), TICK_MS);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Called when an agent finishes. The viewer (if open on it) stays open so the
|
|
184
|
+
* final output remains readable, and the row lingers in the list — just refresh.
|
|
185
|
+
*/
|
|
186
|
+
onAgentFinished(_id: string): void {
|
|
187
|
+
this.update();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
dispose(): void {
|
|
191
|
+
if (this.timer) { clearInterval(this.timer); this.timer = undefined; }
|
|
192
|
+
this.inputUnsub?.();
|
|
193
|
+
this.inputUnsub = undefined;
|
|
194
|
+
if (this.viewerClose) { this.viewerClose(); this.viewerClose = undefined; }
|
|
195
|
+
this.viewingAgentId = undefined;
|
|
196
|
+
// No handle to close the workflow inspector with, but the list is going
|
|
197
|
+
// away — leaving the id set would keep it swallowing input forever.
|
|
198
|
+
this.viewingWorkflowId = undefined;
|
|
199
|
+
if (this.ui && this.widgetRegistered) this.ui.setWidget(FLEET_KEY, undefined);
|
|
200
|
+
this.widgetRegistered = false;
|
|
201
|
+
this.tui = undefined;
|
|
202
|
+
this.active = false;
|
|
203
|
+
// Null last so a `viewerClose()` microtask above can't re-register the widget.
|
|
204
|
+
this.ui = undefined;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Re-register/refresh the below-editor widget; clears it when nothing remains. */
|
|
208
|
+
update(): void {
|
|
209
|
+
if (!this.ui) return;
|
|
210
|
+
// A run with no agents of its own left in the list is still worth a row —
|
|
211
|
+
// it is the thing the user opens to see what its children did. Read off the
|
|
212
|
+
// roster for the same reason activation does: two counts of "is there
|
|
213
|
+
// anything here" drifted apart once before.
|
|
214
|
+
const hasRows = this.enabled && this.roster().length > 1;
|
|
215
|
+
|
|
216
|
+
if (!hasRows) {
|
|
217
|
+
if (this.widgetRegistered) {
|
|
218
|
+
this.ui.setWidget(FLEET_KEY, undefined);
|
|
219
|
+
this.widgetRegistered = false;
|
|
220
|
+
this.tui = undefined;
|
|
221
|
+
}
|
|
222
|
+
if (this.timer) { clearInterval(this.timer); this.timer = undefined; }
|
|
223
|
+
this.active = false;
|
|
224
|
+
this.selectedIndex = 0;
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
this.clampSelection();
|
|
229
|
+
this.ensureTimer(); // keep stats ticking whenever the list is shown (e.g. after a re-enable)
|
|
230
|
+
|
|
231
|
+
if (!this.widgetRegistered) {
|
|
232
|
+
this.ui.setWidget(FLEET_KEY, (tui, theme) => {
|
|
233
|
+
this.tui = tui;
|
|
234
|
+
return {
|
|
235
|
+
render: (w: number) => this.renderBar(w, theme),
|
|
236
|
+
invalidate: () => { this.widgetRegistered = false; this.tui = undefined; },
|
|
237
|
+
};
|
|
238
|
+
}, { placement: "belowEditor" });
|
|
239
|
+
this.widgetRegistered = true;
|
|
240
|
+
} else {
|
|
241
|
+
this.tui?.requestRender();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ---- Roster ----
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Agents shown in the list, ordered earliest-launched first so the ones you
|
|
249
|
+
* started sooner sit at the top. Every row is openable (has a session), so Enter
|
|
250
|
+
* never dead-ends. Included: running/queued, plus the agent currently being
|
|
251
|
+
* viewed, plus recently-finished ones (they linger briefly before dropping out).
|
|
252
|
+
* Pending agents with no session yet are hidden until they start.
|
|
253
|
+
* (`listAgents()` is newest-first, so we re-sort.)
|
|
254
|
+
*/
|
|
255
|
+
private agentRecords(): AgentRecord[] {
|
|
256
|
+
const now = Date.now();
|
|
257
|
+
return this.manager.listAgents()
|
|
258
|
+
.filter(a => isTopLevelAgent(a) && a.session && (
|
|
259
|
+
a.status === "running" || a.status === "queued"
|
|
260
|
+
|| a.id === this.viewingAgentId
|
|
261
|
+
|| (a.completedAt != null && now - a.completedAt < FINISHED_LINGER_MS)
|
|
262
|
+
))
|
|
263
|
+
.sort((a, b) => a.startedAt - b.startedAt);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Wire workflow runs into the list.
|
|
268
|
+
*
|
|
269
|
+
* Injected rather than constructed here because the fleet list predates
|
|
270
|
+
* workflows and must keep working without them — a session with the feature
|
|
271
|
+
* switched off never calls this, and the roster is agents-only exactly as
|
|
272
|
+
* before.
|
|
273
|
+
*/
|
|
274
|
+
setWorkflowSource(
|
|
275
|
+
source: () => readonly FleetWorkflow[],
|
|
276
|
+
open: (id: string) => Promise<void> | void,
|
|
277
|
+
): void {
|
|
278
|
+
this.workflowSource = source;
|
|
279
|
+
this.openWorkflow = open;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Live runs, plus recently settled ones — the same linger the agents get. */
|
|
283
|
+
private workflows(): FleetWorkflow[] {
|
|
284
|
+
if (!this.workflowSource) return [];
|
|
285
|
+
const now = Date.now();
|
|
286
|
+
return [...this.workflowSource()]
|
|
287
|
+
.filter(run =>
|
|
288
|
+
run.status === "running"
|
|
289
|
+
|| run.status === "paused"
|
|
290
|
+
|| (run.completedAt != null && now - run.completedAt < FINISHED_LINGER_MS)
|
|
291
|
+
)
|
|
292
|
+
.sort((a, b) => a.startedAt - b.startedAt);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Runs sit above the agents rather than interleaved by start time: a run owns
|
|
297
|
+
* most of the agents under it, so listing the container first is what makes
|
|
298
|
+
* the list read as a hierarchy rather than a shuffle.
|
|
299
|
+
*/
|
|
300
|
+
private roster(): FleetEntry[] {
|
|
301
|
+
return [
|
|
302
|
+
{ kind: "main" },
|
|
303
|
+
...this.workflows().map(workflow => ({ kind: "workflow" as const, workflow })),
|
|
304
|
+
...this.agentRecords().map(record => ({ kind: "agent" as const, record })),
|
|
305
|
+
];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private clampSelection(): void {
|
|
309
|
+
const max = this.roster().length - 1;
|
|
310
|
+
if (this.selectedIndex > max) this.selectedIndex = Math.max(0, max);
|
|
311
|
+
if (this.selectedIndex < 0) this.selectedIndex = 0;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// ---- Key handling ----
|
|
315
|
+
|
|
316
|
+
/** Returns `{consume:true}` to swallow a key, or undefined to let it through. */
|
|
317
|
+
handleKey(data: string): { consume?: boolean; data?: string } | undefined {
|
|
318
|
+
if (!this.enabled || !this.ui) return undefined;
|
|
319
|
+
// Input listeners receive BOTH key-press and key-release (the kitty protocol
|
|
320
|
+
// emits both, and matchesKey matches either) — act on press only, or every
|
|
321
|
+
// tap would move/fire twice. Repeats still pass through for held-key nav.
|
|
322
|
+
if (isKeyRelease(data)) return undefined;
|
|
323
|
+
// While an overlay is open, let it own all input. Checked before the focus
|
|
324
|
+
// test below, which would otherwise read the dialog holding the keyboard as
|
|
325
|
+
// "the user left the list" and reset the selection out from under it.
|
|
326
|
+
if (this.viewerClose || this.viewingWorkflowId) return undefined;
|
|
327
|
+
// Input listeners fire BEFORE the focused component, and dialogs
|
|
328
|
+
// (ctx.ui.select/confirm/input, pi's own menus) swap the prompt editor out
|
|
329
|
+
// while getEditorText() still reads the detached — empty — editor. So when
|
|
330
|
+
// anything but the editor owns the keyboard, stay out of its keys (#123).
|
|
331
|
+
if (!this.editorHasFocus()) {
|
|
332
|
+
if (this.active) this.deactivate();
|
|
333
|
+
return undefined;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (!this.active) {
|
|
337
|
+
// Activate: ↓ or ← at an empty prompt moves focus into the list.
|
|
338
|
+
const isActivator = matchesKey(data, "down") || matchesKey(data, "left");
|
|
339
|
+
// Gated on the roster, not the agents: a session whose only row is a
|
|
340
|
+
// workflow run still has somewhere to go, and requiring an agent would
|
|
341
|
+
// render the row but refuse to move into it.
|
|
342
|
+
if (isActivator && this.roster().length > 1 && this.ui.getEditorText() === "") {
|
|
343
|
+
this.active = true;
|
|
344
|
+
this.selectedIndex = 0;
|
|
345
|
+
this.update();
|
|
346
|
+
return { consume: true };
|
|
347
|
+
}
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Active — arrows navigate, Enter opens, Esc / Up-past-top exits.
|
|
352
|
+
if (matchesKey(data, "down")) {
|
|
353
|
+
const max = this.roster().length - 1;
|
|
354
|
+
this.selectedIndex = Math.min(max, this.selectedIndex + 1);
|
|
355
|
+
this.update();
|
|
356
|
+
return { consume: true };
|
|
357
|
+
}
|
|
358
|
+
if (matchesKey(data, "up")) {
|
|
359
|
+
if (this.selectedIndex === 0) { this.deactivate(); return { consume: true }; }
|
|
360
|
+
this.selectedIndex -= 1;
|
|
361
|
+
this.update();
|
|
362
|
+
return { consume: true };
|
|
363
|
+
}
|
|
364
|
+
if (matchesKey(data, "escape")) { this.deactivate(); return { consume: true }; }
|
|
365
|
+
if (matchesKey(data, Key.enter)) { this.openSelected(); return { consume: true }; }
|
|
366
|
+
|
|
367
|
+
// Any other key cancels navigation and flows to the editor.
|
|
368
|
+
this.deactivate();
|
|
369
|
+
return undefined;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* True when pi's prompt editor owns the keyboard. pi's editor is an `Editor`
|
|
374
|
+
* subclass (CustomEditor) while every dialog/selector is not, and the loader
|
|
375
|
+
* aliases pi-tui to pi's own copy, so `instanceof` is a reliable identity
|
|
376
|
+
* check. `focusedComponent` is TUI-private (no public accessor), hence the
|
|
377
|
+
* best-effort peek: unknowable focus (no tui seen yet, nothing focused)
|
|
378
|
+
* counts as the editor so activation keeps working.
|
|
379
|
+
*/
|
|
380
|
+
private editorHasFocus(): boolean {
|
|
381
|
+
const focused = (this.tui as { focusedComponent?: unknown } | undefined)?.focusedComponent;
|
|
382
|
+
return focused == null || focused instanceof Editor;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private deactivate(): void {
|
|
386
|
+
this.active = false;
|
|
387
|
+
this.selectedIndex = 0;
|
|
388
|
+
this.update();
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private openSelected(): void {
|
|
392
|
+
const entry = this.roster()[this.selectedIndex];
|
|
393
|
+
if (!entry || entry.kind === "main") {
|
|
394
|
+
// `main` = return to the prompt; the native transcript is already shown.
|
|
395
|
+
this.deactivate();
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
if (entry.kind === "workflow") {
|
|
399
|
+
// The extension owns this overlay and closes it, so there is no
|
|
400
|
+
// `viewerClose` to hold — but the list still has to know one is up, and
|
|
401
|
+
// still has to put the cursor back on the run when it comes down.
|
|
402
|
+
this.viewingWorkflowId = entry.workflow.id;
|
|
403
|
+
void Promise.resolve(this.openWorkflow?.(entry.workflow.id)).then(
|
|
404
|
+
() => this.clearViewer(),
|
|
405
|
+
() => this.clearViewer(),
|
|
406
|
+
);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
const record = entry.record;
|
|
410
|
+
if (!this.ui) return;
|
|
411
|
+
const session = record.session ?? (record.transcriptPath && this.cwd
|
|
412
|
+
? (() => {
|
|
413
|
+
const messages = readAgentHistory(this.cwd!, record.transcriptPath!);
|
|
414
|
+
return messages ? createStaticConversationSource(messages) : undefined;
|
|
415
|
+
})()
|
|
416
|
+
: undefined);
|
|
417
|
+
if (!session) {
|
|
418
|
+
this.ui.notify(`Agent is ${record.status} — no session available.`, "info");
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const isHistory = record.session === undefined;
|
|
422
|
+
const activity = this.agentActivity.get(record.id);
|
|
423
|
+
this.viewingAgentId = record.id;
|
|
424
|
+
|
|
425
|
+
void this.ui.custom<undefined>(
|
|
426
|
+
(tui, theme, keybindings, done) => {
|
|
427
|
+
this.viewerClose = () => done(undefined);
|
|
428
|
+
return new ConversationViewer(
|
|
429
|
+
tui,
|
|
430
|
+
session,
|
|
431
|
+
record,
|
|
432
|
+
activity,
|
|
433
|
+
theme,
|
|
434
|
+
done,
|
|
435
|
+
isHistory ? undefined : () => {
|
|
436
|
+
if (this.manager.abort(record.id)) this.ui?.notify(`Stopped "${record.description}".`, "info");
|
|
437
|
+
},
|
|
438
|
+
keybindings,
|
|
439
|
+
isHistory ? undefined : (message: string) => this.manager.steer(record.id, message),
|
|
440
|
+
this.showCost(),
|
|
441
|
+
this.viewerMarkdown,
|
|
442
|
+
this.onViewerMarkdown,
|
|
443
|
+
);
|
|
444
|
+
},
|
|
445
|
+
{
|
|
446
|
+
overlay: true,
|
|
447
|
+
overlayOptions: { anchor: "center", width: "90%", maxHeight: `${VIEWPORT_HEIGHT_PCT}%` },
|
|
448
|
+
},
|
|
449
|
+
).then(() => this.clearViewer(), () => this.clearViewer());
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** Reset overlay state and return to the list (on close, auto-close, or error). */
|
|
453
|
+
private clearViewer(): void {
|
|
454
|
+
// Keep the cursor on the agent we were viewing — re-resolve by id so it
|
|
455
|
+
// still feels natural if the list reordered (an earlier agent finished)
|
|
456
|
+
// while the overlay was open. If that agent is gone, leave the index for
|
|
457
|
+
// update()'s clamp to settle.
|
|
458
|
+
const viewed = this.viewingAgentId ?? this.viewingWorkflowId;
|
|
459
|
+
if (viewed !== undefined) {
|
|
460
|
+
const idx = this.roster().findIndex(e =>
|
|
461
|
+
e.kind === "agent" ? e.record.id === viewed
|
|
462
|
+
: e.kind === "workflow" ? e.workflow.id === viewed
|
|
463
|
+
: false,
|
|
464
|
+
);
|
|
465
|
+
if (idx >= 0) this.selectedIndex = idx;
|
|
466
|
+
}
|
|
467
|
+
this.viewerClose = undefined;
|
|
468
|
+
this.viewingAgentId = undefined;
|
|
469
|
+
this.viewingWorkflowId = undefined;
|
|
470
|
+
this.update();
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// ---- Rendering ----
|
|
474
|
+
|
|
475
|
+
private renderBar(width: number, theme: Theme): string[] {
|
|
476
|
+
const rows = this.roster().slice(1) as (WorkflowEntry | AgentEntry)[];
|
|
477
|
+
if (rows.length === 0) return [];
|
|
478
|
+
// Clamp locally so a render between a roster shrink and the next update()
|
|
479
|
+
// (e.g. on terminal resize) never loses the selection marker.
|
|
480
|
+
const sel = Math.min(this.selectedIndex, rows.length);
|
|
481
|
+
|
|
482
|
+
const hint = this.active
|
|
483
|
+
? "↑↓ select · enter view · esc back"
|
|
484
|
+
: "esc to interrupt · ← for agents · ↓ to manage";
|
|
485
|
+
const lines: string[] = [];
|
|
486
|
+
lines.push(truncateToWidth(" " + theme.fg("dim", hint), width));
|
|
487
|
+
lines.push("");
|
|
488
|
+
lines.push(truncateToWidth(` ${this.bullet(0, sel, theme)} main`, width));
|
|
489
|
+
|
|
490
|
+
// Window the rows so the selected one stays visible.
|
|
491
|
+
const visible = Math.min(MAX_AGENT_ROWS, rows.length);
|
|
492
|
+
const selRow = Math.max(0, sel - 1);
|
|
493
|
+
const start = selRow < visible ? 0 : selRow - visible + 1;
|
|
494
|
+
const hiddenBelow = rows.length - (start + visible);
|
|
495
|
+
|
|
496
|
+
if (start > 0) lines.push(rightAlign("", theme.fg("dim", `↑ ${start} more`), width));
|
|
497
|
+
for (let a = start; a < start + visible; a++) {
|
|
498
|
+
const row = rows[a];
|
|
499
|
+
lines.push(
|
|
500
|
+
row.kind === "workflow" ?
|
|
501
|
+
this.renderWorkflowRow(a + 1, sel, row.workflow, width, theme)
|
|
502
|
+
: this.renderAgentRow(a + 1, sel, row.record, width, theme),
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
if (hiddenBelow > 0) lines.push(rightAlign("", theme.fg("dim", `↓ ${hiddenBelow} more`), width));
|
|
506
|
+
|
|
507
|
+
return lines;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
private bullet(rosterIndex: number, sel: number, theme: Theme): string {
|
|
511
|
+
return rosterIndex === sel ? theme.fg("accent", "●") : theme.fg("dim", "○");
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* A run's row. Shaped like an agent's — bullet, kind, name, stats flush right
|
|
516
|
+
* — so the two read as one list, with the agent count where an agent has its
|
|
517
|
+
* description and the same elapsed/token tail.
|
|
518
|
+
*/
|
|
519
|
+
private renderWorkflowRow(
|
|
520
|
+
rosterIndex: number,
|
|
521
|
+
sel: number,
|
|
522
|
+
workflow: FleetWorkflow,
|
|
523
|
+
width: number,
|
|
524
|
+
theme: Theme,
|
|
525
|
+
): string {
|
|
526
|
+
const selected = rosterIndex === sel;
|
|
527
|
+
const kind = theme.fg(selected ? "text" : "muted", "workflow");
|
|
528
|
+
const name = selected ? theme.fg("text", workflow.name) : workflow.name;
|
|
529
|
+
const left = ` ${this.bullet(rosterIndex, sel, theme)} ${kind} ${name}`;
|
|
530
|
+
// Frozen once the run settles, exactly as an agent's clock is.
|
|
531
|
+
const elapsed = (workflow.completedAt ?? Date.now()) - workflow.startedAt;
|
|
532
|
+
const agents = `${workflow.doneCount}/${workflow.totalCount} agent${workflow.totalCount === 1 ? "" : "s"}`;
|
|
533
|
+
const stats = `${agents} · ${formatFleetElapsed(elapsed)} · ${formatFleetTokens(workflow.tokens)}`;
|
|
534
|
+
return rightAlign(left, selected ? theme.fg("text", stats) : theme.fg("dim", stats), width);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
private renderAgentRow(rosterIndex: number, sel: number, record: AgentRecord, width: number, theme: Theme): string {
|
|
538
|
+
// The selected row renders in the theme's primary text color so it reads as
|
|
539
|
+
// one selection (#230). A configured badge survives — Claude Code's FleetView
|
|
540
|
+
// keeps the agent color on the selected row too and only bolds it — which also
|
|
541
|
+
// keeps the row's width fixed as the selection moves.
|
|
542
|
+
const selected = rosterIndex === sel;
|
|
543
|
+
const name = renderAgentName(record.type, theme, selected
|
|
544
|
+
? { fallbackColor: "text", bold: hasAgentBadge(record.type) }
|
|
545
|
+
: { fallbackColor: "muted" });
|
|
546
|
+
const description = selected ? theme.fg("text", record.description) : record.description;
|
|
547
|
+
const left = ` ${this.bullet(rosterIndex, sel, theme)} ${name} ${description}`;
|
|
548
|
+
// The record, not the activity tracker — see the note in AgentWidget's
|
|
549
|
+
// running line: only the record carries a nested child's spend, and only it
|
|
550
|
+
// outlives the agent.
|
|
551
|
+
const tokens = getLifetimeTotal(record.lifetimeUsage);
|
|
552
|
+
const elapsedMs = (record.completedAt ?? Date.now()) - record.startedAt; // freezes once finished
|
|
553
|
+
const cost = this.showCost() ? formatCost(getLifetimeCost(record.lifetimeUsage)) : "";
|
|
554
|
+
const stats = `${formatFleetElapsed(elapsedMs)} · ${formatFleetTokens(tokens)}${cost ? ` · ${cost}` : ""}`;
|
|
555
|
+
const right = selected ? theme.fg("text", stats) : theme.fg("dim", stats);
|
|
556
|
+
return rightAlign(left, right, width);
|
|
557
|
+
}
|
|
558
|
+
}
|
package/src/ui/schedule-menu.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
12
12
|
import type { SubagentScheduler } from "../schedule.js";
|
|
13
13
|
import type { ScheduledSubagent } from "../types.js";
|
|
14
|
+
import { selectItem } from "./select-item.js";
|
|
14
15
|
|
|
15
16
|
/** Format an ISO timestamp as relative time ("in 4h", "2d ago", "—"). */
|
|
16
17
|
function relTime(iso: string | undefined, now = Date.now()): string {
|
|
@@ -85,16 +86,16 @@ export async function showSchedulesMenu(
|
|
|
85
86
|
return;
|
|
86
87
|
}
|
|
87
88
|
|
|
88
|
-
|
|
89
|
-
|
|
89
|
+
// Numbered + item-paired: two jobs whose names agree in the first 18
|
|
90
|
+
// characters format identically, and matching the returned string back
|
|
91
|
+
// against a parallel label array cancelled whichever came first.
|
|
92
|
+
const job = await selectItem(
|
|
93
|
+
ctx.ui,
|
|
90
94
|
`Scheduled jobs (${jobs.length}) — select to cancel`,
|
|
91
|
-
|
|
95
|
+
jobs,
|
|
96
|
+
j => formatJob(j, scheduler),
|
|
92
97
|
);
|
|
93
|
-
if (!
|
|
94
|
-
|
|
95
|
-
const idx = labels.indexOf(choice);
|
|
96
|
-
if (idx < 0) return;
|
|
97
|
-
const job = jobs[idx];
|
|
98
|
+
if (!job) return;
|
|
98
99
|
|
|
99
100
|
const ok = await ctx.ui.confirm(`Cancel "${job.name}"?`, formatDetails(job, scheduler));
|
|
100
101
|
if (!ok) return;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* select-item.ts — pick an item from a list via `ctx.ui.select`, safely.
|
|
3
|
+
*
|
|
4
|
+
* Pi's dialog API is `select(title, options: string[]) => Promise<string | undefined>`:
|
|
5
|
+
* strings in, string out, with no index or value form. Callers therefore have to
|
|
6
|
+
* map the returned string back to the item it came from, and the obvious way —
|
|
7
|
+
* `labels.indexOf(choice)` over a parallel array — silently resolves to the
|
|
8
|
+
* FIRST match whenever two rows format identically. Row formatters here truncate
|
|
9
|
+
* (job names to 18 chars, agent descriptions to whatever fits), and the text they
|
|
10
|
+
* truncate is LLM-authored, so collisions are ordinary rather than exotic.
|
|
11
|
+
*
|
|
12
|
+
* This numbers every row, which makes the labels unique by construction — no
|
|
13
|
+
* data-dependent branch that only executes in the case nobody exercises — and
|
|
14
|
+
* keeps each label paired with its item so a later edit that sorts or filters
|
|
15
|
+
* between building and resolving cannot desync them.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** Minimal shape of the `ctx.ui` surface this needs. */
|
|
19
|
+
export interface SelectUI {
|
|
20
|
+
select(title: string, options: string[]): Promise<string | undefined>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Show a numbered picker and return the chosen item (not its label).
|
|
25
|
+
*
|
|
26
|
+
* Returns undefined when the user escapes, or when the returned string is not
|
|
27
|
+
* one we offered.
|
|
28
|
+
*/
|
|
29
|
+
export async function selectItem<T>(
|
|
30
|
+
ui: SelectUI,
|
|
31
|
+
title: string,
|
|
32
|
+
items: readonly T[],
|
|
33
|
+
format: (item: T, index: number) => string,
|
|
34
|
+
): Promise<T | undefined> {
|
|
35
|
+
// Pad the number so a 10+ item list keeps its columns aligned.
|
|
36
|
+
const width = String(items.length).length;
|
|
37
|
+
const rows = items.map((item, i) => ({
|
|
38
|
+
item,
|
|
39
|
+
label: `${String(i + 1).padStart(width)}. ${format(item, i)}`,
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
const choice = await ui.select(title, rows.map(r => r.label));
|
|
43
|
+
if (!choice) return undefined;
|
|
44
|
+
return rows.find(r => r.label === choice)?.item;
|
|
45
|
+
}
|