@quintinshaw/pi-dynamic-workflows 1.8.0 → 1.9.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 +4 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/task-panel.d.ts +24 -0
- package/dist/task-panel.js +82 -0
- package/dist/workflow-commands.js +12 -0
- package/dist/workflow-ui.d.ts +110 -0
- package/dist/workflow-ui.js +426 -0
- package/extensions/workflow.ts +8 -2
- package/package.json +1 -1
- package/src/index.ts +10 -0
- package/src/task-panel.ts +103 -0
- package/src/workflow-commands.ts +12 -0
- package/src/workflow-ui.ts +496 -0
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactive `/workflows` navigator, modeled on Claude Code's view:
|
|
3
|
+
*
|
|
4
|
+
* runs ──enter──▶ phases ──enter──▶ agents ──enter──▶ agent detail
|
|
5
|
+
* ◀──esc─── ◀──esc──── ◀──esc────
|
|
6
|
+
*
|
|
7
|
+
* Keys: ↑/↓ (or j/k) select · enter/→ drill in · esc/← back (esc at top closes)
|
|
8
|
+
* p pause/resume · x stop · r restart · s save · q quit
|
|
9
|
+
*
|
|
10
|
+
* The state machine and line rendering are pure and unit-tested; the pi-tui
|
|
11
|
+
* Component shell (openWorkflowNavigator) wires them to live manager events.
|
|
12
|
+
*/
|
|
13
|
+
import { parseKey } from "@earendil-works/pi-tui";
|
|
14
|
+
import { registerSavedWorkflow } from "./saved-commands.js";
|
|
15
|
+
const STATUS_ICON = {
|
|
16
|
+
pending: "·",
|
|
17
|
+
queued: "·",
|
|
18
|
+
running: "◆",
|
|
19
|
+
paused: "⏸",
|
|
20
|
+
completed: "✓",
|
|
21
|
+
done: "✓",
|
|
22
|
+
failed: "✗",
|
|
23
|
+
error: "✗",
|
|
24
|
+
aborted: "⊘",
|
|
25
|
+
skipped: "⊘",
|
|
26
|
+
};
|
|
27
|
+
const PLAIN = { fg: (_c, t) => t, bold: (t) => t };
|
|
28
|
+
/** Reads run/phase/agent data from the manager, preferring live snapshots. */
|
|
29
|
+
export class NavigatorModel {
|
|
30
|
+
manager;
|
|
31
|
+
constructor(manager) {
|
|
32
|
+
this.manager = manager;
|
|
33
|
+
}
|
|
34
|
+
snapshot(runId) {
|
|
35
|
+
const live = this.manager.getRun(runId);
|
|
36
|
+
if (live)
|
|
37
|
+
return { snapshot: live.snapshot, status: live.status };
|
|
38
|
+
const p = this.manager.listRuns().find((r) => r.runId === runId);
|
|
39
|
+
if (!p)
|
|
40
|
+
return undefined;
|
|
41
|
+
return { snapshot: persistedToSnapshot(p), status: p.status };
|
|
42
|
+
}
|
|
43
|
+
runs() {
|
|
44
|
+
return this.manager.listRuns().map((p) => {
|
|
45
|
+
const live = this.manager.getRun(p.runId);
|
|
46
|
+
const agents = (live?.snapshot.agents ?? p.agents);
|
|
47
|
+
return {
|
|
48
|
+
runId: p.runId,
|
|
49
|
+
name: live?.snapshot.name ?? p.workflowName,
|
|
50
|
+
status: live?.status ?? p.status,
|
|
51
|
+
done: agents.filter((a) => a.status === "done").length,
|
|
52
|
+
total: agents.length,
|
|
53
|
+
tokens: (live?.snapshot.tokenUsage ?? p.tokenUsage)?.total ?? 0,
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
runName(runId) {
|
|
58
|
+
return this.snapshot(runId)?.snapshot.name ?? runId;
|
|
59
|
+
}
|
|
60
|
+
runStatus(runId) {
|
|
61
|
+
return this.snapshot(runId)?.status ?? "unknown";
|
|
62
|
+
}
|
|
63
|
+
phases(runId) {
|
|
64
|
+
const snap = this.snapshot(runId)?.snapshot;
|
|
65
|
+
if (!snap)
|
|
66
|
+
return [];
|
|
67
|
+
const order = snap.phases.length ? [...snap.phases] : [];
|
|
68
|
+
const byPhase = new Map();
|
|
69
|
+
for (const a of snap.agents) {
|
|
70
|
+
const key = a.phase ?? "(no phase)";
|
|
71
|
+
if (!byPhase.has(key))
|
|
72
|
+
byPhase.set(key, []);
|
|
73
|
+
byPhase.get(key)?.push(a);
|
|
74
|
+
if (!order.includes(key))
|
|
75
|
+
order.push(key);
|
|
76
|
+
}
|
|
77
|
+
return order.map((title) => {
|
|
78
|
+
const agents = byPhase.get(title) ?? [];
|
|
79
|
+
return {
|
|
80
|
+
title,
|
|
81
|
+
done: agents.filter((a) => a.status === "done").length,
|
|
82
|
+
total: agents.length,
|
|
83
|
+
tokens: agents.reduce((n, a) => n + (a.tokens ?? 0), 0),
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
agents(runId, phase) {
|
|
88
|
+
const snap = this.snapshot(runId)?.snapshot;
|
|
89
|
+
if (!snap)
|
|
90
|
+
return [];
|
|
91
|
+
return snap.agents
|
|
92
|
+
.filter((a) => (a.phase ?? "(no phase)") === phase)
|
|
93
|
+
.map((a) => ({ id: a.id, label: a.label, status: a.status, phase: a.phase, tokens: a.tokens }));
|
|
94
|
+
}
|
|
95
|
+
agentDetail(runId, agentId) {
|
|
96
|
+
return this.snapshot(runId)?.snapshot.agents.find((a) => a.id === agentId);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function persistedToSnapshot(p) {
|
|
100
|
+
return {
|
|
101
|
+
name: p.workflowName,
|
|
102
|
+
phases: p.phases,
|
|
103
|
+
currentPhase: p.currentPhase,
|
|
104
|
+
logs: p.logs,
|
|
105
|
+
agents: p.agents.map((a) => ({
|
|
106
|
+
id: a.id,
|
|
107
|
+
label: a.label,
|
|
108
|
+
phase: a.phase,
|
|
109
|
+
prompt: a.prompt,
|
|
110
|
+
status: a.status,
|
|
111
|
+
resultPreview: a.result == null ? undefined : String(typeof a.result === "string" ? a.result : JSON.stringify(a.result)),
|
|
112
|
+
error: a.error,
|
|
113
|
+
})),
|
|
114
|
+
agentCount: p.agents.length,
|
|
115
|
+
runningCount: p.agents.filter((a) => a.status === "running").length,
|
|
116
|
+
doneCount: p.agents.filter((a) => a.status === "done").length,
|
|
117
|
+
errorCount: p.agents.filter((a) => a.status === "error").length,
|
|
118
|
+
tokenUsage: p.tokenUsage ? { ...p.tokenUsage } : undefined,
|
|
119
|
+
runId: p.runId,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Navigation state machine: a stack of (view, cursor) frames plus detail scroll. */
|
|
123
|
+
export class NavigatorState {
|
|
124
|
+
stack = [
|
|
125
|
+
{ kind: "runs", cursor: 0 },
|
|
126
|
+
];
|
|
127
|
+
scroll = 0;
|
|
128
|
+
top() {
|
|
129
|
+
return this.stack[this.stack.length - 1];
|
|
130
|
+
}
|
|
131
|
+
get kind() {
|
|
132
|
+
return this.top().kind;
|
|
133
|
+
}
|
|
134
|
+
get cursor() {
|
|
135
|
+
return this.top().cursor;
|
|
136
|
+
}
|
|
137
|
+
get runId() {
|
|
138
|
+
return this.top().runId;
|
|
139
|
+
}
|
|
140
|
+
get phase() {
|
|
141
|
+
return this.top().phase;
|
|
142
|
+
}
|
|
143
|
+
get agentId() {
|
|
144
|
+
return this.top().agentId;
|
|
145
|
+
}
|
|
146
|
+
get depth() {
|
|
147
|
+
return this.stack.length;
|
|
148
|
+
}
|
|
149
|
+
/** Clamp the cursor to [0, count). */
|
|
150
|
+
clamp(count) {
|
|
151
|
+
const t = this.top();
|
|
152
|
+
t.cursor = count <= 0 ? 0 : Math.max(0, Math.min(t.cursor, count - 1));
|
|
153
|
+
}
|
|
154
|
+
move(delta, count) {
|
|
155
|
+
if (this.kind === "detail") {
|
|
156
|
+
this.scroll = Math.max(0, this.scroll + delta);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (count <= 0)
|
|
160
|
+
return;
|
|
161
|
+
const t = this.top();
|
|
162
|
+
t.cursor = (t.cursor + delta + count) % count;
|
|
163
|
+
}
|
|
164
|
+
/** Drill into the selected item. Returns true if the view changed. */
|
|
165
|
+
drill(model) {
|
|
166
|
+
const t = this.top();
|
|
167
|
+
if (t.kind === "runs") {
|
|
168
|
+
const runs = model.runs();
|
|
169
|
+
const run = runs[t.cursor];
|
|
170
|
+
if (!run)
|
|
171
|
+
return false;
|
|
172
|
+
this.stack.push({ kind: "phases", cursor: 0, runId: run.runId });
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
if (t.kind === "phases" && t.runId) {
|
|
176
|
+
const phases = model.phases(t.runId);
|
|
177
|
+
const ph = phases[t.cursor];
|
|
178
|
+
if (!ph)
|
|
179
|
+
return false;
|
|
180
|
+
this.stack.push({ kind: "agents", cursor: 0, runId: t.runId, phase: ph.title });
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
if (t.kind === "agents" && t.runId && t.phase) {
|
|
184
|
+
const agents = model.agents(t.runId, t.phase);
|
|
185
|
+
const ag = agents[t.cursor];
|
|
186
|
+
if (!ag)
|
|
187
|
+
return false;
|
|
188
|
+
this.scroll = 0;
|
|
189
|
+
this.stack.push({ kind: "detail", cursor: 0, runId: t.runId, phase: t.phase, agentId: ag.id });
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
/** Pop one level. Returns false when already at the top (caller should close). */
|
|
195
|
+
back() {
|
|
196
|
+
if (this.stack.length <= 1)
|
|
197
|
+
return false;
|
|
198
|
+
this.stack.pop();
|
|
199
|
+
this.scroll = 0;
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
/** The runId the current view acts on (for pause/stop/save). */
|
|
203
|
+
activeRunId(model) {
|
|
204
|
+
if (this.runId)
|
|
205
|
+
return this.runId;
|
|
206
|
+
if (this.kind === "runs")
|
|
207
|
+
return model.runs()[this.cursor]?.runId;
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function pad(n) {
|
|
212
|
+
return n.toLocaleString();
|
|
213
|
+
}
|
|
214
|
+
function fmtTokens(t) {
|
|
215
|
+
return t > 0 ? `${pad(t)} tok` : "";
|
|
216
|
+
}
|
|
217
|
+
/** Build the lines for the current view. Pure: depends only on state + model + theme. */
|
|
218
|
+
export function renderNavigator(state, model, width, theme = PLAIN) {
|
|
219
|
+
const lines = [];
|
|
220
|
+
const sel = (i, text) => i === state.cursor ? theme.fg("accent", theme.bold(`❯ ${text}`)) : ` ${text}`;
|
|
221
|
+
const dim = (t) => theme.fg("dim", t);
|
|
222
|
+
if (state.kind === "runs") {
|
|
223
|
+
const runs = model.runs();
|
|
224
|
+
state.clamp(runs.length);
|
|
225
|
+
lines.push(theme.bold("Workflows"));
|
|
226
|
+
if (!runs.length)
|
|
227
|
+
lines.push(dim(" No runs yet. Start one with a background workflow."));
|
|
228
|
+
runs.forEach((r, i) => {
|
|
229
|
+
const icon = STATUS_ICON[r.status] ?? "?";
|
|
230
|
+
const meta = [`${r.done}/${r.total}`, fmtTokens(r.tokens)].filter(Boolean).join(" · ");
|
|
231
|
+
lines.push(sel(i, `${icon} ${r.name} ${dim(`${r.runId} · ${r.status} · ${meta}`)}`));
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
else if (state.kind === "phases" && state.runId) {
|
|
235
|
+
const phases = model.phases(state.runId);
|
|
236
|
+
state.clamp(phases.length);
|
|
237
|
+
lines.push(theme.bold(model.runName(state.runId)) + dim(` (${model.runStatus(state.runId)})`));
|
|
238
|
+
phases.forEach((p, i) => {
|
|
239
|
+
const meta = [`${p.done}/${p.total} agents`, fmtTokens(p.tokens)].filter(Boolean).join(" · ");
|
|
240
|
+
lines.push(sel(i, `${p.title} ${dim(meta)}`));
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
else if (state.kind === "agents" && state.runId && state.phase) {
|
|
244
|
+
const agents = model.agents(state.runId, state.phase);
|
|
245
|
+
state.clamp(agents.length);
|
|
246
|
+
lines.push(theme.bold(`${model.runName(state.runId)} › ${state.phase}`));
|
|
247
|
+
agents.forEach((a, i) => {
|
|
248
|
+
const icon = STATUS_ICON[a.status] ?? "?";
|
|
249
|
+
const tok = a.tokens ? dim(` ${fmtTokens(a.tokens)}`) : "";
|
|
250
|
+
lines.push(sel(i, `${icon} ${a.label}${tok}`));
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
else if (state.kind === "detail" && state.runId && state.agentId != null) {
|
|
254
|
+
const a = model.agentDetail(state.runId, state.agentId);
|
|
255
|
+
lines.push(theme.bold(a ? a.label : "agent"));
|
|
256
|
+
if (a) {
|
|
257
|
+
const body = [];
|
|
258
|
+
body.push(dim("Status: ") + (a.status ?? ""));
|
|
259
|
+
if (a.error)
|
|
260
|
+
body.push(dim("Error: ") + a.error);
|
|
261
|
+
body.push("", dim("Prompt:"));
|
|
262
|
+
body.push(...wrap(a.prompt ?? "", width));
|
|
263
|
+
body.push("", dim("Result:"));
|
|
264
|
+
body.push(...wrap(a.resultPreview ?? "(none)", width));
|
|
265
|
+
// Scrollable region.
|
|
266
|
+
const maxScroll = Math.max(0, body.length - 1);
|
|
267
|
+
state.scroll = Math.min(state.scroll, maxScroll);
|
|
268
|
+
lines.push(...body.slice(state.scroll));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
lines.push("");
|
|
272
|
+
lines.push(footerHint(state, theme));
|
|
273
|
+
return lines;
|
|
274
|
+
}
|
|
275
|
+
function footerHint(state, theme) {
|
|
276
|
+
const parts = state.kind === "detail"
|
|
277
|
+
? ["j/k scroll", "esc back"]
|
|
278
|
+
: ["↑/↓ select", "enter open", "esc back", "p pause", "x stop", "r restart", "s save", "q quit"];
|
|
279
|
+
return theme.fg("dim", parts.join(" · "));
|
|
280
|
+
}
|
|
281
|
+
function wrap(text, width) {
|
|
282
|
+
const w = Math.max(20, width - 2);
|
|
283
|
+
const out = [];
|
|
284
|
+
for (const para of String(text).split("\n")) {
|
|
285
|
+
if (para.length <= w) {
|
|
286
|
+
out.push(para);
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
let rest = para;
|
|
290
|
+
while (rest.length > w) {
|
|
291
|
+
out.push(rest.slice(0, w));
|
|
292
|
+
rest = rest.slice(w);
|
|
293
|
+
}
|
|
294
|
+
if (rest)
|
|
295
|
+
out.push(rest);
|
|
296
|
+
}
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
export function keyToAction(keyId, kind) {
|
|
300
|
+
switch (keyId) {
|
|
301
|
+
case "up":
|
|
302
|
+
return { type: "move", delta: -1 };
|
|
303
|
+
case "down":
|
|
304
|
+
return { type: "move", delta: 1 };
|
|
305
|
+
case "k":
|
|
306
|
+
return { type: "move", delta: -1 };
|
|
307
|
+
case "j":
|
|
308
|
+
return { type: "move", delta: 1 };
|
|
309
|
+
case "enter":
|
|
310
|
+
case "return":
|
|
311
|
+
case "right":
|
|
312
|
+
return kind === "detail" ? { type: "none" } : { type: "drill" };
|
|
313
|
+
case "escape":
|
|
314
|
+
case "esc":
|
|
315
|
+
case "left":
|
|
316
|
+
return { type: "back" };
|
|
317
|
+
case "q":
|
|
318
|
+
return { type: "close" };
|
|
319
|
+
case "p":
|
|
320
|
+
return { type: "pause" };
|
|
321
|
+
case "x":
|
|
322
|
+
return { type: "stop" };
|
|
323
|
+
case "r":
|
|
324
|
+
return { type: "restart" };
|
|
325
|
+
case "s":
|
|
326
|
+
return { type: "save" };
|
|
327
|
+
default:
|
|
328
|
+
return { type: "none" };
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
function currentCount(state, model) {
|
|
332
|
+
if (state.kind === "runs")
|
|
333
|
+
return model.runs().length;
|
|
334
|
+
if (state.kind === "phases" && state.runId)
|
|
335
|
+
return model.phases(state.runId).length;
|
|
336
|
+
if (state.kind === "agents" && state.runId && state.phase)
|
|
337
|
+
return model.agents(state.runId, state.phase).length;
|
|
338
|
+
return 0;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Open the interactive `/workflows` navigator as a focused overlay. Resolves when
|
|
342
|
+
* the user closes it (esc at the top level, or `q`).
|
|
343
|
+
*/
|
|
344
|
+
export function openWorkflowNavigator(pi, manager, ui, opts = {}) {
|
|
345
|
+
const model = new NavigatorModel(manager);
|
|
346
|
+
const state = new NavigatorState();
|
|
347
|
+
return ui.custom((tui, theme, _keybindings, done) => {
|
|
348
|
+
const rerender = () => tui.requestRender();
|
|
349
|
+
const events = ["agentStart", "agentEnd", "phase", "log", "complete", "error", "stopped", "paused", "resumed"];
|
|
350
|
+
const onEvent = () => rerender();
|
|
351
|
+
for (const ev of events)
|
|
352
|
+
manager.on(ev, onEvent);
|
|
353
|
+
const cleanup = () => {
|
|
354
|
+
for (const ev of events)
|
|
355
|
+
manager.off(ev, onEvent);
|
|
356
|
+
};
|
|
357
|
+
const act = (data) => {
|
|
358
|
+
const action = keyToAction(parseKey(data), state.kind);
|
|
359
|
+
switch (action.type) {
|
|
360
|
+
case "move":
|
|
361
|
+
state.move(action.delta, currentCount(state, model));
|
|
362
|
+
break;
|
|
363
|
+
case "drill":
|
|
364
|
+
state.drill(model);
|
|
365
|
+
break;
|
|
366
|
+
case "back":
|
|
367
|
+
if (!state.back()) {
|
|
368
|
+
cleanup();
|
|
369
|
+
done();
|
|
370
|
+
}
|
|
371
|
+
break;
|
|
372
|
+
case "close":
|
|
373
|
+
cleanup();
|
|
374
|
+
done();
|
|
375
|
+
return;
|
|
376
|
+
case "pause": {
|
|
377
|
+
const id = state.activeRunId(model);
|
|
378
|
+
if (id)
|
|
379
|
+
ui.notify(manager.pause(id) ? `Paused ${id}` : `Cannot pause ${id}`, "info");
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
case "stop": {
|
|
383
|
+
const id = state.activeRunId(model);
|
|
384
|
+
if (id)
|
|
385
|
+
ui.notify(manager.stop(id) ? `Stopped ${id}` : `Cannot stop ${id}`, "info");
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
case "restart":
|
|
389
|
+
ui.notify("Restarting a single agent isn't supported yet", "warning");
|
|
390
|
+
break;
|
|
391
|
+
case "save": {
|
|
392
|
+
const id = state.activeRunId(model);
|
|
393
|
+
const run = id ? manager.listRuns().find((r) => r.runId === id) : undefined;
|
|
394
|
+
if (!run?.script) {
|
|
395
|
+
ui.notify("No saved run script to save", "warning");
|
|
396
|
+
}
|
|
397
|
+
else if (!opts.storage) {
|
|
398
|
+
ui.notify("Saving is not available (no storage)", "error");
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
const name = run.workflowName || "workflow";
|
|
402
|
+
const saved = opts.storage.save({
|
|
403
|
+
name,
|
|
404
|
+
description: run.workflowName,
|
|
405
|
+
script: run.script,
|
|
406
|
+
location: "project",
|
|
407
|
+
});
|
|
408
|
+
registerSavedWorkflow(pi, opts.cwd ?? process.cwd(), saved);
|
|
409
|
+
ui.notify(`Saved /${name}`, "info");
|
|
410
|
+
}
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
default:
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
rerender();
|
|
417
|
+
};
|
|
418
|
+
const component = {
|
|
419
|
+
render: (width) => renderNavigator(state, model, width, theme),
|
|
420
|
+
handleInput: (data) => act(data),
|
|
421
|
+
invalidate: () => { },
|
|
422
|
+
dispose: () => cleanup(),
|
|
423
|
+
};
|
|
424
|
+
return component;
|
|
425
|
+
}, { overlay: true });
|
|
426
|
+
}
|
package/extensions/workflow.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import {
|
|
3
3
|
createWorkflowStorage,
|
|
4
4
|
createWorkflowTool,
|
|
5
|
+
installResultDelivery,
|
|
6
|
+
installTaskPanel,
|
|
5
7
|
registerAllSavedWorkflows,
|
|
6
8
|
registerBuiltinWorkflows,
|
|
7
9
|
registerWorkflowCommands,
|
|
@@ -20,11 +22,15 @@ export default function extension(pi: ExtensionAPI) {
|
|
|
20
22
|
registerWorkflowCommands(pi, manager, { storage, cwd });
|
|
21
23
|
registerBuiltinWorkflows(pi, { cwd });
|
|
22
24
|
registerAllSavedWorkflows(pi, cwd, storage);
|
|
25
|
+
// Deliver a background run's result into the conversation when it finishes.
|
|
26
|
+
installResultDelivery(pi, manager);
|
|
23
27
|
|
|
24
|
-
pi.on("session_start", () => {
|
|
28
|
+
pi.on("session_start", (_event: unknown, ctx: ExtensionContext) => {
|
|
25
29
|
const active = pi.getActiveTools();
|
|
26
30
|
if (!active.includes(workflowTool.name)) {
|
|
27
31
|
pi.setActiveTools([...active, workflowTool.name]);
|
|
28
32
|
}
|
|
33
|
+
// Live "workflows running" panel below the input (focus + enter to open).
|
|
34
|
+
installTaskPanel(pi, manager, ctx.ui, { storage, cwd });
|
|
29
35
|
});
|
|
30
36
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -45,6 +45,7 @@ export {
|
|
|
45
45
|
} from "./saved-commands.js";
|
|
46
46
|
export type { StructuredOutputCapture, StructuredOutputToolOptions } from "./structured-output.js";
|
|
47
47
|
export { createStructuredOutputTool } from "./structured-output.js";
|
|
48
|
+
export { installResultDelivery, installTaskPanel, type TaskPanelOptions } from "./task-panel.js";
|
|
48
49
|
export { createWebFetchTool, createWebSearchTool, createWebTools } from "./web-tools.js";
|
|
49
50
|
export type {
|
|
50
51
|
AgentOptions,
|
|
@@ -63,5 +64,14 @@ export type { SavedWorkflow, WorkflowStorage } from "./workflow-saved.js";
|
|
|
63
64
|
export { createWorkflowStorage } from "./workflow-saved.js";
|
|
64
65
|
export type { WorkflowToolInput, WorkflowToolOptions } from "./workflow-tool.js";
|
|
65
66
|
export { createWorkflowTool } from "./workflow-tool.js";
|
|
67
|
+
export {
|
|
68
|
+
keyToAction,
|
|
69
|
+
type NavAction,
|
|
70
|
+
NavigatorModel,
|
|
71
|
+
NavigatorState,
|
|
72
|
+
openWorkflowNavigator,
|
|
73
|
+
renderNavigator,
|
|
74
|
+
type ViewKind,
|
|
75
|
+
} from "./workflow-ui.js";
|
|
66
76
|
export type { Worktree } from "./worktree.js";
|
|
67
77
|
export { createWorktree, removeWorktree } from "./worktree.js";
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Background-run UX, mirroring Claude Code:
|
|
3
|
+
* - A live task panel below the input lists in-progress runs while you keep working.
|
|
4
|
+
* Focus it (↓) and press enter to open the full navigator.
|
|
5
|
+
* - When a background run finishes, its result is delivered back into the
|
|
6
|
+
* conversation so the paused task continues with the outcome.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ExtensionAPI, ExtensionUIContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
10
|
+
import type { Component, TUI } from "@earendil-works/pi-tui";
|
|
11
|
+
import { parseKey } from "@earendil-works/pi-tui";
|
|
12
|
+
import type { ManagedRun, WorkflowManager } from "./workflow-manager.js";
|
|
13
|
+
import type { WorkflowStorage } from "./workflow-saved.js";
|
|
14
|
+
import { openWorkflowNavigator } from "./workflow-ui.js";
|
|
15
|
+
|
|
16
|
+
const RUN_EVENTS = ["agentStart", "agentEnd", "phase", "log", "complete", "error", "stopped", "paused", "resumed"];
|
|
17
|
+
|
|
18
|
+
export interface TaskPanelOptions {
|
|
19
|
+
storage?: WorkflowStorage;
|
|
20
|
+
cwd?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function deliverText(run: ManagedRun): string {
|
|
24
|
+
const r = run.result?.result as { report?: unknown } | undefined;
|
|
25
|
+
const body =
|
|
26
|
+
r && typeof r.report === "string" && r.report.trim() ? r.report : JSON.stringify(run.result?.result, null, 2);
|
|
27
|
+
const tokens = run.result?.tokenUsage ? ` · ${run.result.tokenUsage.total.toLocaleString()} tokens` : "";
|
|
28
|
+
const agents = run.result?.agentCount ?? run.snapshot.agentCount;
|
|
29
|
+
return `✓ Workflow "${run.snapshot.name}" finished (${agents} agents${tokens}).\n\n${body}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Deliver a background run's result into the conversation when it completes or
|
|
34
|
+
* fails. Set up once per extension; idempotent via an internal guard.
|
|
35
|
+
*/
|
|
36
|
+
export function installResultDelivery(pi: ExtensionAPI, manager: WorkflowManager): void {
|
|
37
|
+
if ((manager as unknown as { __deliveryInstalled?: boolean }).__deliveryInstalled) return;
|
|
38
|
+
(manager as unknown as { __deliveryInstalled?: boolean }).__deliveryInstalled = true;
|
|
39
|
+
|
|
40
|
+
manager.on("complete", ({ runId }: { runId: string }) => {
|
|
41
|
+
const run = manager.getRun(runId);
|
|
42
|
+
if (run) void pi.sendMessage({ customType: "workflow-result", content: deliverText(run), display: true });
|
|
43
|
+
});
|
|
44
|
+
manager.on("error", ({ runId, error }: { runId: string; error?: { message?: string } }) => {
|
|
45
|
+
void pi.sendMessage({
|
|
46
|
+
customType: "workflow-result",
|
|
47
|
+
content: `✗ Workflow ${runId} failed: ${error?.message ?? "unknown error"}`,
|
|
48
|
+
display: true,
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function renderPanel(manager: WorkflowManager, theme: Theme, focused: boolean): string[] {
|
|
54
|
+
const active = manager.listRuns().filter((r) => r.status === "running" || r.status === "paused");
|
|
55
|
+
if (!active.length) return [];
|
|
56
|
+
const rows = active.map((r) => {
|
|
57
|
+
const live = manager.getRun(r.runId);
|
|
58
|
+
const agents = live?.snapshot.agents ?? r.agents;
|
|
59
|
+
const done = agents.filter((a) => a.status === "done").length;
|
|
60
|
+
const icon = r.status === "paused" ? "⏸" : "◆";
|
|
61
|
+
const phase = live?.snapshot.currentPhase ? ` · ${live.snapshot.currentPhase}` : "";
|
|
62
|
+
return ` ${icon} ${r.workflowName} ${done}/${agents.length} agents${phase}`;
|
|
63
|
+
});
|
|
64
|
+
const hint = focused
|
|
65
|
+
? theme.fg("accent", " enter: open · esc: back")
|
|
66
|
+
: theme.fg("dim", " ↓ then enter, or /workflows, to open");
|
|
67
|
+
return [theme.bold(`Workflows running (${active.length}):`), ...rows, hint];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Install the live "workflows running" panel below the editor. Re-rendered on
|
|
72
|
+
* every manager event; focus + enter opens the navigator.
|
|
73
|
+
*/
|
|
74
|
+
export function installTaskPanel(
|
|
75
|
+
pi: ExtensionAPI,
|
|
76
|
+
manager: WorkflowManager,
|
|
77
|
+
ui: ExtensionUIContext,
|
|
78
|
+
opts: TaskPanelOptions = {},
|
|
79
|
+
): void {
|
|
80
|
+
ui.setWidget(
|
|
81
|
+
"workflow-tasks",
|
|
82
|
+
(tui: TUI, theme: Theme) => {
|
|
83
|
+
const onEvent = () => tui.requestRender();
|
|
84
|
+
for (const ev of RUN_EVENTS) manager.on(ev, onEvent);
|
|
85
|
+
const comp: Component & { focused?: boolean; dispose?(): void } = {
|
|
86
|
+
focused: false,
|
|
87
|
+
render: () => renderPanel(manager, theme, comp.focused ?? false),
|
|
88
|
+
handleInput: (data: string) => {
|
|
89
|
+
const key = parseKey(data);
|
|
90
|
+
if (key === "enter" || key === "return" || key === "right") {
|
|
91
|
+
void openWorkflowNavigator(pi, manager, ui, opts);
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
invalidate: () => {},
|
|
95
|
+
dispose: () => {
|
|
96
|
+
for (const ev of RUN_EVENTS) manager.off(ev, onEvent);
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
return comp;
|
|
100
|
+
},
|
|
101
|
+
{ placement: "belowEditor" },
|
|
102
|
+
);
|
|
103
|
+
}
|
package/src/workflow-commands.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type { PersistedRunState } from "./run-persistence.js";
|
|
|
9
9
|
import { registerSavedWorkflow } from "./saved-commands.js";
|
|
10
10
|
import type { WorkflowManager } from "./workflow-manager.js";
|
|
11
11
|
import type { WorkflowStorage } from "./workflow-saved.js";
|
|
12
|
+
import { openWorkflowNavigator } from "./workflow-ui.js";
|
|
12
13
|
|
|
13
14
|
const STATUS_ICON: Record<string, string> = {
|
|
14
15
|
pending: "·",
|
|
@@ -126,7 +127,18 @@ export function registerWorkflowCommands(
|
|
|
126
127
|
const print = (text: string) => pi.sendMessage({ customType: "workflows", content: text, display: true });
|
|
127
128
|
|
|
128
129
|
switch (sub) {
|
|
130
|
+
case "ui":
|
|
129
131
|
case "list": {
|
|
132
|
+
// Interactive navigator when a UI is available; plain text otherwise
|
|
133
|
+
// (print/RPC mode) or when the user explicitly asks for `list`.
|
|
134
|
+
if (sub !== "list" && ctx.hasUI) {
|
|
135
|
+
await openWorkflowNavigator(pi, manager, ctx.ui, { storage: opts.storage, cwd: opts.cwd });
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (parts.length === 0 && ctx.hasUI) {
|
|
139
|
+
await openWorkflowNavigator(pi, manager, ctx.ui, { storage: opts.storage, cwd: opts.cwd });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
130
142
|
const runs = manager.listRuns();
|
|
131
143
|
if (!runs.length) {
|
|
132
144
|
await print("No workflow runs yet. Start one with a background workflow (background: true).");
|