@abelxiaoxing/cadence 2.0.35 → 2.0.37

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 (40) hide show
  1. package/README.de.md +4 -4
  2. package/README.es.md +4 -4
  3. package/README.fr.md +4 -4
  4. package/README.ja.md +3 -4
  5. package/README.ko.md +4 -4
  6. package/README.md +4 -4
  7. package/README.pt-BR.md +4 -4
  8. package/README.ru.md +5 -4
  9. package/README.zh-CN.md +4 -4
  10. package/THIRD_PARTY_NOTICES.md +9 -1
  11. package/docs/conversation-mode.md +21 -2
  12. package/docs/reuse-and-distribution.md +1 -1
  13. package/docs/service-management.md +35 -9
  14. package/package.json +18 -18
  15. package/src/abort-scope.ts +47 -0
  16. package/src/agent-browser/labeled-rule.ts +74 -0
  17. package/src/agent-browser/session-navigator.ts +187 -0
  18. package/src/agent-browser/source.ts +170 -0
  19. package/src/agent-browser/transcript-content.ts +442 -0
  20. package/src/agent-browser/transcript-pane.ts +324 -0
  21. package/src/agent-panel-model.ts +4 -248
  22. package/src/agent-panel.ts +138 -432
  23. package/src/coordinator-tools.mjs +42 -14
  24. package/src/image-content.mjs +7 -28
  25. package/src/native-build.ts +1 -1
  26. package/src/research-scan.mjs +393 -0
  27. package/src/research-tools.mjs +406 -342
  28. package/src/sdk-worker.mjs +40 -55
  29. package/src/service/contracts/core.ts +0 -1
  30. package/src/service/contracts/workbench.ts +10 -16
  31. package/src/service-cli.mjs +328 -106
  32. package/src/service-host.mjs +545 -404
  33. package/src/service-http-client.ts +55 -50
  34. package/src/worker-public.mjs +1 -2
  35. package/src/worker-toolkit.mjs +110 -0
  36. package/src/worker-workspace.d.mts +13 -0
  37. package/src/worker-workspace.mjs +48 -0
  38. package/web/dist/assets/cadence.css +1 -1
  39. package/web/dist/assets/cadence.js +17 -17
  40. package/web/dist/manifest.json +7 -7
@@ -0,0 +1,442 @@
1
+ /** Adapted from gotgenes/pi-packages @ 52854c12d590142e61a4f6a6947be9084035300d.
2
+ * MIT; see licenses/pi-subagents-MIT.txt and provenance/agent-browser-reuse.json. */
3
+ /**
4
+ * transcript-content.ts — the rows the `/subagents:sessions` overlay paints.
5
+ *
6
+ * Owns the transcript's content model: the `SessionMessage` → Pi-component
7
+ * mapping (mirroring Pi's own interactive-mode `renderSessionContext`), and the
8
+ * rendered rows those components produce at a given width. The overlay
9
+ * (`session-navigator.ts`) owns scroll state, chrome, and key handling, and
10
+ * asks this collaborator only for rows — it never reaches into the components.
11
+ *
12
+ * Lives in the SDK/TUI layer rather than the pure `session-navigation.ts` core
13
+ * because Pi's per-entry components require a `TUI`, `cwd`, and markdown theme.
14
+ */
15
+
16
+ import {
17
+ AssistantMessageComponent,
18
+ BashExecutionComponent,
19
+ BranchSummaryMessageComponent,
20
+ CompactionSummaryMessageComponent,
21
+ parseSkillBlock,
22
+ SkillInvocationMessageComponent,
23
+ ToolExecutionComponent,
24
+ UserMessageComponent,
25
+ } from "@earendil-works/pi-coding-agent";
26
+ import {
27
+ Container,
28
+ type MarkdownTheme,
29
+ Spacer,
30
+ type TUI,
31
+ truncateToWidth,
32
+ } from "@earendil-works/pi-tui";
33
+ import type { SessionMessage, TranscriptSource } from "./source.ts";
34
+
35
+ // ─────────────────────────────────────────────────────────────────────────────
36
+
37
+ /** The SDK/TUI environment Pi's per-entry components need, plus the transcript's source. */
38
+ export interface TranscriptContentOptions {
39
+ tui: TUI;
40
+ cwd: string;
41
+ markdownTheme: MarkdownTheme;
42
+ source: TranscriptSource;
43
+ }
44
+
45
+ /**
46
+ * The transcript's renderable rows: settled history rendered through Pi's
47
+ * per-entry components, followed by the running agent's activity row.
48
+ *
49
+ * Settled rows are rendered once per width and cached, so a paint or a scroll
50
+ * costs a slice rather than a walk of the whole component tree. The activity
51
+ * row is recomputed per call — it is two rows, and it tracks live state the
52
+ * source updates without a rebuild.
53
+ *
54
+ * Settled history is held as one block of components per message, so a message
55
+ * that settles appends a block and a tool result touches only its own — neither
56
+ * discards the rest of the transcript's rendering. This is sound because the
57
+ * agent core pushes a finished message into its message array before notifying
58
+ * listeners, so at event time the settled prefix is already visible.
59
+ *
60
+ * The message currently being streamed is held as its own component, updated
61
+ * per delta exactly as Pi's interactive mode does, so a token never touches
62
+ * settled history. The two provably cannot overlap: the agent core keeps the
63
+ * in-flight message outside its message array until it settles.
64
+ */
65
+ export class TranscriptContent {
66
+ private readonly options: TranscriptContentOptions;
67
+ private expanded = false;
68
+ /** Settled history, one block per message that produced components. */
69
+ private blocks: SettledBlock[] = [];
70
+ /** How many source messages have been consumed into `blocks`. */
71
+ private consumedCount = 0;
72
+ /** The last consumed message; a mismatch means history was rewritten. */
73
+ private lastConsumed: SessionMessage | undefined;
74
+ /** Whether any consumed block holds components, driving user-message spacing. */
75
+ private hasVisibleContent = false;
76
+ /** In-flight tool components by tool-call id, pairing a later result to its block. */
77
+ private readonly pendingTools = new Map<string, PendingTool>();
78
+ /** Width `blocks` and `settledRows` were rendered at. */
79
+ private settledWidth: number | undefined;
80
+ /** Concatenation of every block's rows, or undefined when the cache is cold. */
81
+ private settledRows: readonly string[] | undefined;
82
+ /** The message being streamed right now, rendered below settled history. */
83
+ private inFlight: AssistantMessageComponent | undefined;
84
+ /** Width `inFlightRows` was rendered at. */
85
+ private inFlightWidth: number | undefined;
86
+ private inFlightRows: readonly string[] | undefined;
87
+
88
+ constructor(options: TranscriptContentOptions) {
89
+ this.options = options;
90
+ this.apply();
91
+ }
92
+
93
+ /** Total content rows at `width`: settled history plus the live tail. */
94
+ lineCount(width: number): number {
95
+ if (width <= 0) return 0;
96
+ return this.settled(width).length + this.tail(width).length;
97
+ }
98
+
99
+ /** Rows `[start, start + count)` at `width`, clamped to what exists. */
100
+ slice(width: number, start: number, count: number): string[] {
101
+ if (width <= 0) return [];
102
+ const settled = this.settled(width);
103
+ const end = start + count;
104
+ const rows = settled.slice(start, Math.min(end, settled.length));
105
+ if (end > settled.length) {
106
+ const tail = this.tail(width);
107
+ rows.push(
108
+ ...tail.slice(
109
+ Math.max(0, start - settled.length),
110
+ end - settled.length,
111
+ ),
112
+ );
113
+ }
114
+ return rows;
115
+ }
116
+
117
+ /** The source adapts Cadence snapshots to the upstream settled/live split;
118
+ * no synthetic SDK event or running SDK session is required by this view. */
119
+ apply(): void {
120
+ this.consumeSettled();
121
+ const partial = this.options.source.inFlight();
122
+ if (partial) this.updateInFlight(partial);
123
+ else this.clearInFlight();
124
+ }
125
+
126
+ toggleExpanded(): void {
127
+ this.expanded = !this.expanded;
128
+ this.reset();
129
+ this.apply();
130
+ }
131
+
132
+ /** Drop cached rendering held by the mounted components and by this object. */
133
+ invalidate(): void {
134
+ for (const block of this.blocks) {
135
+ block.container.invalidate();
136
+ block.rows = undefined;
137
+ }
138
+ this.settledRows = undefined;
139
+ this.inFlight?.invalidate();
140
+ this.inFlightRows = undefined;
141
+ }
142
+
143
+ // ---- Private ----
144
+
145
+ /** Settled rows at `width`; each block renders once per width and content change. */
146
+ private settled(width: number): readonly string[] {
147
+ if (this.settledWidth !== width) {
148
+ this.settledWidth = width;
149
+ for (const block of this.blocks) block.rows = undefined;
150
+ this.settledRows = undefined;
151
+ }
152
+ if (this.settledRows) return this.settledRows;
153
+ const rows: string[] = [];
154
+ for (const block of this.blocks) {
155
+ block.rows ??= block.container
156
+ .render(width)
157
+ .map((row) => truncateToWidth(stripPromptMarks(row), width));
158
+ rows.push(...block.rows);
159
+ }
160
+ this.settledRows = rows;
161
+ return rows;
162
+ }
163
+
164
+ /** Rows below settled history: the in-flight message, then the activity row. */
165
+ private tail(width: number): readonly string[] {
166
+ const rows = [...this.inFlightRendered(width)];
167
+ const notice = this.options.source.notice();
168
+ if (notice) rows.push(truncateToWidth(notice, width));
169
+ const streaming = this.options.source.streaming();
170
+ if (streaming) {
171
+ const activity = `◍ ${streaming}`;
172
+ rows.push("", truncateToWidth(activity, width));
173
+ }
174
+ return rows;
175
+ }
176
+
177
+ /** The in-flight message's rows, re-rendered independently of settled history. */
178
+ private inFlightRendered(width: number): readonly string[] {
179
+ if (!this.inFlight) return [];
180
+ if (this.inFlightWidth !== width) {
181
+ this.inFlightWidth = width;
182
+ this.inFlightRows = undefined;
183
+ }
184
+ this.inFlightRows ??= this.inFlight
185
+ .render(width)
186
+ .map((row) => truncateToWidth(stripPromptMarks(row), width));
187
+ return this.inFlightRows;
188
+ }
189
+
190
+ private updateInFlight(message: AssistantSessionMessage): void {
191
+ if (this.inFlight) this.inFlight.updateContent(message);
192
+ else
193
+ this.inFlight = new AssistantMessageComponent(
194
+ message,
195
+ true,
196
+ this.options.markdownTheme,
197
+ );
198
+ this.inFlightRows = undefined;
199
+ }
200
+
201
+ private clearInFlight(): void {
202
+ this.inFlight = undefined;
203
+ this.inFlightRows = undefined;
204
+ }
205
+
206
+ /** Discard all settled state, so the next consume rebuilds from scratch. */
207
+ private reset(): void {
208
+ this.blocks = [];
209
+ this.consumedCount = 0;
210
+ this.lastConsumed = undefined;
211
+ this.hasVisibleContent = false;
212
+ this.pendingTools.clear();
213
+ this.settledRows = undefined;
214
+ this.clearInFlight();
215
+ }
216
+
217
+ /**
218
+ * Append blocks for every message that settled since the last check. When the
219
+ * consumed prefix no longer mirrors the source — history replaced wholesale by
220
+ * compaction or branching — start over rather than appending onto stale blocks.
221
+ */
222
+ private consumeSettled(): void {
223
+ const messages = this.options.source.getMessages();
224
+ if (this.hasRewrittenHistory(messages)) this.reset();
225
+ if (messages.length === this.consumedCount) return;
226
+ for (let i = this.consumedCount; i < messages.length; i++)
227
+ this.consumeMessage(messages[i]);
228
+ this.consumedCount = messages.length;
229
+ this.lastConsumed = messages.at(-1);
230
+ this.settledRows = undefined;
231
+ }
232
+
233
+ private hasRewrittenHistory(messages: readonly SessionMessage[]): boolean {
234
+ if (messages.length < this.consumedCount) return true;
235
+ return (
236
+ this.consumedCount > 0 &&
237
+ messages[this.consumedCount - 1] !== this.lastConsumed
238
+ );
239
+ }
240
+
241
+ /**
242
+ * Map one settled message onto its own block of Pi's per-entry components,
243
+ * mirroring Pi's own interactive-mode `renderSessionContext` mapping.
244
+ * `custom`-role messages produce no block — rendering them needs the child
245
+ * session's message-renderer registry, which the navigator does not hold.
246
+ */
247
+ private consumeMessage(message: SessionMessage): void {
248
+ switch (message.role) {
249
+ case "assistant":
250
+ this.consumeAssistant(message);
251
+ break;
252
+ case "toolResult":
253
+ this.consumeToolResult(message);
254
+ break;
255
+ case "user":
256
+ this.consumeUser(message);
257
+ break;
258
+ case "bashExecution":
259
+ this.consumeBashExecution(message);
260
+ break;
261
+ case "compactionSummary":
262
+ this.consumeSummary(
263
+ new CompactionSummaryMessageComponent(
264
+ message,
265
+ this.options.markdownTheme,
266
+ ),
267
+ );
268
+ break;
269
+ case "branchSummary":
270
+ this.consumeSummary(
271
+ new BranchSummaryMessageComponent(
272
+ message,
273
+ this.options.markdownTheme,
274
+ ),
275
+ );
276
+ break;
277
+ }
278
+ }
279
+
280
+ private consumeAssistant(message: AssistantSessionMessage): void {
281
+ const block = this.appendBlock();
282
+ block.container.addChild(
283
+ new AssistantMessageComponent(message, true, this.options.markdownTheme),
284
+ );
285
+ for (const content of message.content) {
286
+ if (content.type !== "toolCall") continue;
287
+ const tool = new ToolExecutionComponent(
288
+ content.name,
289
+ content.id,
290
+ content.arguments,
291
+ { showImages: false },
292
+ this.options.source.getToolDefinition(content.name),
293
+ this.options.tui,
294
+ this.options.cwd,
295
+ );
296
+ tool.setExpanded(this.expanded);
297
+ // Retained edits must never preview a diff against today's checkout.
298
+ if (content.name !== "edit") tool.setArgsComplete();
299
+ block.container.addChild(tool);
300
+ this.pendingTools.set(content.id, { component: tool, block });
301
+ }
302
+ this.hasVisibleContent = true;
303
+ }
304
+
305
+ /** A tool result mutates the block holding its call; no other block changes. */
306
+ private consumeToolResult(
307
+ message: Extract<SessionMessage, { role: "toolResult" }>,
308
+ ): void {
309
+ let pending = this.pendingTools.get(message.toolCallId);
310
+ if (!pending) {
311
+ // The bounded feed can retain a result after its call fell out of view.
312
+ const block = this.appendBlock();
313
+ const component = new ToolExecutionComponent(
314
+ message.toolName,
315
+ message.toolCallId,
316
+ {},
317
+ { showImages: false },
318
+ undefined,
319
+ this.options.tui,
320
+ this.options.cwd,
321
+ );
322
+ component.setExpanded(this.expanded);
323
+ block.container.addChild(component);
324
+ pending = { component, block };
325
+ }
326
+ pending.component.updateResult(message);
327
+ pending.block.rows = undefined;
328
+ this.settledRows = undefined;
329
+ this.pendingTools.delete(message.toolCallId);
330
+ }
331
+
332
+ private consumeUser(
333
+ message: Extract<SessionMessage, { role: "user" }>,
334
+ ): void {
335
+ const block = this.appendBlock();
336
+ addUserComponents(
337
+ block.container,
338
+ message.content,
339
+ this.options.markdownTheme,
340
+ this.hasVisibleContent,
341
+ );
342
+ if (block.container.children.length > 0) this.hasVisibleContent = true;
343
+ }
344
+
345
+ private consumeBashExecution(
346
+ message: Extract<SessionMessage, { role: "bashExecution" }>,
347
+ ): void {
348
+ const block = this.appendBlock();
349
+ const bash = new BashExecutionComponent(
350
+ message.command,
351
+ this.options.tui,
352
+ message.excludeFromContext,
353
+ );
354
+ if (message.output) bash.appendOutput(message.output);
355
+ bash.setComplete(
356
+ message.exitCode,
357
+ message.cancelled,
358
+ undefined,
359
+ message.fullOutputPath,
360
+ );
361
+ block.container.addChild(bash);
362
+ this.hasVisibleContent = true;
363
+ }
364
+
365
+ /** Compaction and branch summaries share the same spacer-plus-expanded shape. */
366
+ private consumeSummary(
367
+ summary: CompactionSummaryMessageComponent | BranchSummaryMessageComponent,
368
+ ): void {
369
+ const block = this.appendBlock();
370
+ block.container.addChild(new Spacer(1));
371
+ summary.setExpanded(true);
372
+ block.container.addChild(summary);
373
+ this.hasVisibleContent = true;
374
+ }
375
+
376
+ private appendBlock(): SettledBlock {
377
+ const block: SettledBlock = { container: new Container(), rows: undefined };
378
+ this.blocks.push(block);
379
+ return block;
380
+ }
381
+ }
382
+
383
+ /** One settled message's components plus its rows at the current width. */
384
+ interface SettledBlock {
385
+ readonly container: Container;
386
+ rows: readonly string[] | undefined;
387
+ }
388
+
389
+ /** A tool call awaiting its result, and the block whose rows it will invalidate. */
390
+ interface PendingTool {
391
+ readonly component: ToolExecutionComponent;
392
+ readonly block: SettledBlock;
393
+ }
394
+
395
+ /** The assistant variant of a session message, as Pi's components consume it. */
396
+ type AssistantSessionMessage = Extract<SessionMessage, { role: "assistant" }>;
397
+
398
+ /**
399
+ * Render a user message (skill block + text) into the block, mirroring Pi.
400
+ * Whether a leading spacer is needed is a whole-transcript property, so the
401
+ * caller — which alone knows what precedes this block — supplies it.
402
+ */
403
+ function addUserComponents(
404
+ container: Container,
405
+ content: string | readonly { type: string; text?: string }[],
406
+ markdownTheme: MarkdownTheme,
407
+ hasPrecedingContent: boolean,
408
+ ): void {
409
+ const text = userMessageText(content);
410
+ if (!text) return;
411
+ if (hasPrecedingContent) container.addChild(new Spacer(1));
412
+
413
+ const skillBlock = parseSkillBlock(text);
414
+ if (!skillBlock) {
415
+ container.addChild(new UserMessageComponent(text, markdownTheme));
416
+ return;
417
+ }
418
+ const skill = new SkillInvocationMessageComponent(skillBlock, markdownTheme);
419
+ skill.setExpanded(true);
420
+ container.addChild(skill);
421
+ if (skillBlock.userMessage) {
422
+ container.addChild(new Spacer(1));
423
+ container.addChild(
424
+ new UserMessageComponent(skillBlock.userMessage, markdownTheme),
425
+ );
426
+ }
427
+ }
428
+
429
+ /** Concatenate the text blocks of a user message's content (mirrors Pi). */
430
+ function userMessageText(
431
+ content: string | readonly { type: string; text?: string }[],
432
+ ): string {
433
+ if (typeof content === "string") return content;
434
+ return content
435
+ .filter((block) => block.type === "text")
436
+ .map((block) => block.text ?? "")
437
+ .join("");
438
+ }
439
+
440
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: remove unmatched shell-integration marks
441
+ const OSC_133 = /\x1b\]133;[^\x07\x1b]*(?:\x07|\x1b\\)/g;
442
+ const stripPromptMarks = (line: string) => line.replace(OSC_133, "");