@abelxiaoxing/cadence 2.0.23 → 2.0.25
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/docs/conversation-mode.md +19 -0
- package/package.json +2 -1
- package/src/agent-panel-model.ts +362 -122
- package/src/agent-panel.ts +142 -49
- package/src/cadence-mode.ts +283 -165
- package/src/coordinator-tools.mjs +9 -0
- package/src/native-build.ts +1 -1
- package/src/native-workflow-history.ts +3 -1
- package/src/research-tools.mjs +365 -0
- package/src/sdk-worker.mjs +59 -9
- package/src/service/contracts/workflow.ts +132 -1
- package/src/service-cli.mjs +15 -2
- package/src/service-host.mjs +217 -29
- package/src/worker-public.mjs +16 -1
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +15 -14
- package/web/dist/manifest.json +7 -7
package/src/agent-panel.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
/** Background-agent panel for Pi's TUI. Observation only: it subscribes to
|
|
2
2
|
* the `subagent:*` events on Pi's shared extension bus and renders them; it
|
|
3
|
-
* never starts, cancels or keeps an agent or the Pi process alive.
|
|
3
|
+
* never starts, cancels or keeps an agent or the Pi process alive.
|
|
4
|
+
*
|
|
5
|
+
* Nothing shows while no agent runs. A batch of agents is live above the
|
|
6
|
+
* input; once all of them ended it collapses into a card in the transcript
|
|
7
|
+
* (a display-only session entry) and the live view goes away. The list below
|
|
8
|
+
* the input appears only while it is being used. */
|
|
4
9
|
import {
|
|
5
10
|
AssistantMessageComponent,
|
|
6
11
|
type ExtensionAPI,
|
|
@@ -24,7 +29,14 @@ import {
|
|
|
24
29
|
truncateToWidth,
|
|
25
30
|
} from "@earendil-works/pi-tui";
|
|
26
31
|
import {
|
|
32
|
+
type AgentCard,
|
|
33
|
+
CARD_ENTRY,
|
|
34
|
+
cardLines,
|
|
35
|
+
cardOf,
|
|
36
|
+
decodeCard,
|
|
27
37
|
factsText,
|
|
38
|
+
liveLines,
|
|
39
|
+
MAX_ENDED,
|
|
28
40
|
type Paint,
|
|
29
41
|
type PanelAction,
|
|
30
42
|
PanelFocus,
|
|
@@ -35,19 +47,23 @@ import {
|
|
|
35
47
|
type SubagentState,
|
|
36
48
|
SubagentStore,
|
|
37
49
|
statusSymbol,
|
|
38
|
-
summaryLines,
|
|
39
50
|
} from "./agent-panel-model.ts";
|
|
40
51
|
|
|
41
52
|
export interface AgentPanelOptions {
|
|
42
53
|
/** Agent rows shown at once; more collapse into `+K more`. */
|
|
43
54
|
maxRows?: number;
|
|
44
|
-
/** Keep
|
|
45
|
-
|
|
55
|
+
/** Keep a finished batch live instead of collapsing it into a card
|
|
56
|
+
* (CADENCE_AGENT_PANEL=always). */
|
|
57
|
+
keep?: boolean;
|
|
46
58
|
now?: () => number;
|
|
47
59
|
}
|
|
48
|
-
const
|
|
49
|
-
|
|
60
|
+
const LIVE = "cadence-agents",
|
|
61
|
+
LIST = "cadence-agents-list",
|
|
50
62
|
SPIN_MS = 100;
|
|
63
|
+
/** A batch whose agents all ended stays live this long before it collapses
|
|
64
|
+
* into its card, so the outcome is seen and a start that follows at once
|
|
65
|
+
* (the next workflow step) joins the same batch. */
|
|
66
|
+
export const SETTLE_MS = 1500;
|
|
51
67
|
|
|
52
68
|
/** Rows kept for Pi's footer/status lines while an agent view is shown. */
|
|
53
69
|
const RESERVED = 5;
|
|
@@ -86,13 +102,31 @@ export function registerAgentPanel(
|
|
|
86
102
|
pi.events.on(channel, (data) => {
|
|
87
103
|
store.apply(channel, data);
|
|
88
104
|
});
|
|
105
|
+
// Pi renders the card where it was appended and again on resume; Ctrl+O
|
|
106
|
+
// (Pi's tool-output expansion) lists every member.
|
|
107
|
+
pi.registerEntryRenderer(CARD_ENTRY, (entry, { expanded }, theme) => {
|
|
108
|
+
const card = decodeCard(entry.data);
|
|
109
|
+
if (!card) return undefined;
|
|
110
|
+
const paint = themePaint(theme);
|
|
111
|
+
return {
|
|
112
|
+
render: (width) =>
|
|
113
|
+
cardLines(card, expanded, Math.max(1, width - 1), paint).map(
|
|
114
|
+
(line) => ` ${line}`,
|
|
115
|
+
),
|
|
116
|
+
invalidate() {},
|
|
117
|
+
};
|
|
118
|
+
});
|
|
89
119
|
let detach: (() => void) | undefined;
|
|
90
120
|
pi.on("session_start", (_event, ctx) => {
|
|
91
121
|
detach?.();
|
|
92
122
|
detach =
|
|
93
123
|
ctx.mode === "tui" && ctx.hasUI
|
|
94
|
-
? attachPanel(
|
|
95
|
-
|
|
124
|
+
? attachPanel(
|
|
125
|
+
ctx,
|
|
126
|
+
store,
|
|
127
|
+
options,
|
|
128
|
+
(id) => pi.events.emit(SUBAGENT_VIEW, { id }),
|
|
129
|
+
(card) => pi.appendEntry(CARD_ENTRY, card),
|
|
96
130
|
)
|
|
97
131
|
: undefined;
|
|
98
132
|
});
|
|
@@ -107,19 +141,28 @@ function attachPanel(
|
|
|
107
141
|
store: SubagentStore,
|
|
108
142
|
options: AgentPanelOptions,
|
|
109
143
|
announce: (id: string | null) => void,
|
|
144
|
+
commit: (card: AgentCard) => void,
|
|
110
145
|
): () => void {
|
|
111
146
|
const now = options.now ?? Date.now;
|
|
112
147
|
const maxRows = Math.max(1, options.maxRows ?? 6);
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
148
|
+
const keep = options.keep ?? process.env.CADENCE_AGENT_PANEL === "always";
|
|
149
|
+
const focus = new PanelFocus(store, maxRows);
|
|
150
|
+
// A batch that ended while no session showed it has no card to give, and a
|
|
151
|
+
// new session lists only its own agents; ones still running carry over.
|
|
152
|
+
if (store.settled()) store.closeBatch();
|
|
153
|
+
store.prune(0);
|
|
116
154
|
let tui: TUI | undefined,
|
|
117
155
|
paint: Paint = (_role, text) => text,
|
|
118
156
|
frame = 0,
|
|
119
157
|
spinner: ReturnType<typeof setInterval> | undefined,
|
|
158
|
+
settling: ReturnType<typeof setTimeout> | undefined,
|
|
120
159
|
view: { component: Component; done: () => void } | undefined,
|
|
121
|
-
//
|
|
122
|
-
|
|
160
|
+
// Mounted widgets: the batch above the input while one is live, and the
|
|
161
|
+
// list's slot below it while any agent is kept for review.
|
|
162
|
+
live = false,
|
|
163
|
+
list = false,
|
|
164
|
+
// The last batch, kept live after it ended (CADENCE_AGENT_PANEL=always).
|
|
165
|
+
kept: SubagentState[] = [],
|
|
123
166
|
// The agent last announced on `subagent:view`, so producers load only it.
|
|
124
167
|
announced: string | undefined;
|
|
125
168
|
const announceView = () => {
|
|
@@ -132,10 +175,84 @@ function attachPanel(
|
|
|
132
175
|
clearInterval(spinner);
|
|
133
176
|
spinner = undefined;
|
|
134
177
|
};
|
|
178
|
+
const liveAgents = () => {
|
|
179
|
+
const open = store.batch();
|
|
180
|
+
return open.length ? open : kept;
|
|
181
|
+
};
|
|
182
|
+
const widget =
|
|
183
|
+
(lines: (width: number) => string[]) => (t: TUI, theme: Theme) => {
|
|
184
|
+
tui = t;
|
|
185
|
+
paint = themePaint(theme);
|
|
186
|
+
return { render: lines, invalidate() {} };
|
|
187
|
+
};
|
|
188
|
+
const liveWidget = widget((width) =>
|
|
189
|
+
// The agent view takes this place; a summary above it would be noise.
|
|
190
|
+
view
|
|
191
|
+
? []
|
|
192
|
+
: liveLines(
|
|
193
|
+
{
|
|
194
|
+
agents: liveAgents(),
|
|
195
|
+
now: now(),
|
|
196
|
+
frame,
|
|
197
|
+
maxRows,
|
|
198
|
+
hint: focus.focus === "input",
|
|
199
|
+
},
|
|
200
|
+
width,
|
|
201
|
+
paint,
|
|
202
|
+
),
|
|
203
|
+
);
|
|
204
|
+
const listWidget = widget((width) => panelLines(panelFrame(), width, paint));
|
|
205
|
+
const mount = () => {
|
|
206
|
+
const showLive = liveAgents().length > 0;
|
|
207
|
+
// The list renders nothing until used, and Pi gives the slot below the
|
|
208
|
+
// input no spacing, so it costs no row; mounting it while agents exist
|
|
209
|
+
// is what lets ↓ see whether Pi's input box has focus.
|
|
210
|
+
const showList = store.size > 0 || focus.view !== undefined;
|
|
211
|
+
if (showLive !== live) {
|
|
212
|
+
live = showLive;
|
|
213
|
+
ctx.ui.setWidget(LIVE, showLive ? liveWidget : undefined, {
|
|
214
|
+
placement: "aboveEditor",
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
if (showList !== list) {
|
|
218
|
+
list = showList;
|
|
219
|
+
ctx.ui.setWidget(LIST, showList ? listWidget : undefined, {
|
|
220
|
+
placement: "belowEditor",
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
/** Close the settled batch: its card goes into the session, the live view
|
|
225
|
+
* goes away and older ended agents beyond the review bound are dropped. */
|
|
226
|
+
const close = () => {
|
|
227
|
+
clearTimeout(settling);
|
|
228
|
+
settling = undefined;
|
|
229
|
+
if (!store.settled()) return;
|
|
230
|
+
const agents = store.closeBatch();
|
|
231
|
+
if (keep) kept = agents;
|
|
232
|
+
else
|
|
233
|
+
try {
|
|
234
|
+
commit(cardOf(agents));
|
|
235
|
+
} catch {
|
|
236
|
+
// A replaced runtime cannot append; the agents stay reviewable.
|
|
237
|
+
}
|
|
238
|
+
const dropped = store.prune(MAX_ENDED, [focus.selected, focus.view]);
|
|
239
|
+
for (const id of dropped) transcripts.delete(id);
|
|
240
|
+
focus.forget(dropped);
|
|
241
|
+
};
|
|
135
242
|
// One redraw per spinner frame while anything runs; Pi's differential
|
|
136
243
|
// renderer rewrites only the changed rows.
|
|
137
244
|
const changed = () => {
|
|
138
|
-
if (
|
|
245
|
+
if (kept.length && store.batch().length) kept = [];
|
|
246
|
+
if (!store.settled()) {
|
|
247
|
+
clearTimeout(settling);
|
|
248
|
+
settling = undefined;
|
|
249
|
+
} else if (!settling) {
|
|
250
|
+
settling = setTimeout(() => {
|
|
251
|
+
close();
|
|
252
|
+
changed();
|
|
253
|
+
}, SETTLE_MS);
|
|
254
|
+
settling.unref?.();
|
|
255
|
+
}
|
|
139
256
|
if (store.running() === 0) stopSpinner();
|
|
140
257
|
else if (!spinner) {
|
|
141
258
|
spinner = setInterval(() => {
|
|
@@ -144,6 +261,7 @@ function attachPanel(
|
|
|
144
261
|
}, SPIN_MS);
|
|
145
262
|
spinner.unref?.();
|
|
146
263
|
}
|
|
264
|
+
mount();
|
|
147
265
|
tui?.requestRender();
|
|
148
266
|
};
|
|
149
267
|
const unsubscribe = store.subscribe(changed);
|
|
@@ -157,6 +275,7 @@ function attachPanel(
|
|
|
157
275
|
if (action === "open") openView();
|
|
158
276
|
else if (action === "close") view?.done();
|
|
159
277
|
announceView();
|
|
278
|
+
mount();
|
|
160
279
|
tui?.requestRender();
|
|
161
280
|
return action === "pass" ? undefined : { consume: true };
|
|
162
281
|
};
|
|
@@ -168,10 +287,12 @@ function attachPanel(
|
|
|
168
287
|
focus.abandon();
|
|
169
288
|
view = undefined;
|
|
170
289
|
announceView();
|
|
290
|
+
mount();
|
|
171
291
|
}
|
|
172
292
|
if (focus.focus === "panel" && !editorFocused()) {
|
|
173
293
|
// A dialog took focus: its keys are not the panel's.
|
|
174
|
-
focus.
|
|
294
|
+
focus.abandon();
|
|
295
|
+
mount();
|
|
175
296
|
tui?.requestRender();
|
|
176
297
|
return undefined;
|
|
177
298
|
}
|
|
@@ -208,7 +329,6 @@ function attachPanel(
|
|
|
208
329
|
now: now(),
|
|
209
330
|
frame,
|
|
210
331
|
maxRows,
|
|
211
|
-
showEmpty,
|
|
212
332
|
});
|
|
213
333
|
const viewLines = (width: number): string[] => {
|
|
214
334
|
const agent = focus.view === undefined ? undefined : store.get(focus.view);
|
|
@@ -257,47 +377,20 @@ function attachPanel(
|
|
|
257
377
|
)}`;
|
|
258
378
|
};
|
|
259
379
|
|
|
260
|
-
const mount = () => {
|
|
261
|
-
mounted = true;
|
|
262
|
-
ctx.ui.setWidget(
|
|
263
|
-
SUMMARY,
|
|
264
|
-
(t, theme) => {
|
|
265
|
-
tui = t;
|
|
266
|
-
paint = themePaint(theme);
|
|
267
|
-
return {
|
|
268
|
-
// The agent view takes Z1's place; its summary would sit above it.
|
|
269
|
-
render: (width) =>
|
|
270
|
-
view ? [] : summaryLines(store.ordered(), width, paint, maxRows),
|
|
271
|
-
invalidate() {},
|
|
272
|
-
};
|
|
273
|
-
},
|
|
274
|
-
{ placement: "aboveEditor" },
|
|
275
|
-
);
|
|
276
|
-
ctx.ui.setWidget(
|
|
277
|
-
PANEL,
|
|
278
|
-
(t, theme) => {
|
|
279
|
-
tui = t;
|
|
280
|
-
paint = themePaint(theme);
|
|
281
|
-
return {
|
|
282
|
-
render: (width) => panelLines(panelFrame(), width, paint),
|
|
283
|
-
invalidate() {},
|
|
284
|
-
};
|
|
285
|
-
},
|
|
286
|
-
{ placement: "belowEditor" },
|
|
287
|
-
);
|
|
288
|
-
};
|
|
289
380
|
changed();
|
|
290
381
|
return () => {
|
|
291
382
|
unsubscribe();
|
|
292
383
|
unlisten();
|
|
293
384
|
stopSpinner();
|
|
385
|
+
// A batch that already ended still gets its card in the session it ran
|
|
386
|
+
// in; one still running carries over to the next session.
|
|
387
|
+
if (settling) close();
|
|
388
|
+
clearTimeout(settling);
|
|
294
389
|
if (announced !== undefined) announce(null);
|
|
295
390
|
try {
|
|
296
391
|
view?.done();
|
|
297
|
-
if (
|
|
298
|
-
|
|
299
|
-
ctx.ui.setWidget(PANEL, undefined);
|
|
300
|
-
}
|
|
392
|
+
if (live) ctx.ui.setWidget(LIVE, undefined);
|
|
393
|
+
if (list) ctx.ui.setWidget(LIST, undefined);
|
|
301
394
|
} catch {
|
|
302
395
|
// A replaced runtime already cleared its UI.
|
|
303
396
|
}
|