@lune_99/pi-subagent 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +21 -0
  3. package/README.md +169 -2
  4. package/docs/capture.json +30 -0
  5. package/docs/previews.md +22 -0
  6. package/docs/reference.md +240 -0
  7. package/docs/subagent-auto-tiers.png +0 -0
  8. package/docs/subagent-inspector.png +0 -0
  9. package/docs/subagent-overview.png +0 -0
  10. package/package.json +82 -5
  11. package/skills/create-lune-subagent/SKILL.md +186 -0
  12. package/src/auto-model.ts +592 -0
  13. package/src/catalog.ts +111 -0
  14. package/src/definition-file.ts +112 -0
  15. package/src/definitions.ts +324 -0
  16. package/src/dock/dock-alert.ts +78 -0
  17. package/src/dock/dock-contribution.ts +318 -0
  18. package/src/extension-tools.ts +46 -0
  19. package/src/index.ts +168 -0
  20. package/src/inspector/details.ts +113 -0
  21. package/src/inspector/inspector.ts +653 -0
  22. package/src/inspector/layout.ts +119 -0
  23. package/src/inspector/transcript.ts +735 -0
  24. package/src/inspector/viewport.ts +62 -0
  25. package/src/inspector/wrap.ts +136 -0
  26. package/src/models.ts +54 -0
  27. package/src/notify/notification-box.ts +381 -0
  28. package/src/notify/notifier.ts +569 -0
  29. package/src/paths.ts +34 -0
  30. package/src/render/lune-theme.ts +68 -0
  31. package/src/render/text.ts +121 -0
  32. package/src/run/guide.ts +26 -0
  33. package/src/run/report-tool.ts +90 -0
  34. package/src/run/run-file.ts +193 -0
  35. package/src/run/run-manager.ts +838 -0
  36. package/src/run/run-session.ts +307 -0
  37. package/src/run/run.ts +681 -0
  38. package/src/run/session-start.ts +283 -0
  39. package/src/run/types.ts +167 -0
  40. package/src/run/worktree.ts +138 -0
  41. package/src/schema.ts +9 -0
  42. package/src/settings/agents-menu.ts +363 -0
  43. package/src/settings/panel.ts +179 -0
  44. package/src/settings/schema.ts +71 -0
  45. package/src/tools/format.ts +375 -0
  46. package/src/tools/subagent-tools.ts +467 -0
@@ -0,0 +1,569 @@
1
+ import type {
2
+ ExtensionAPI,
3
+ ExtensionUIContext,
4
+ SessionEntry,
5
+ } from "@earendil-works/pi-coding-agent";
6
+
7
+ import type {
8
+ ReportStatus,
9
+ RunReport,
10
+ SettledState,
11
+ StopSource,
12
+ TurnRecord,
13
+ } from "../run/types.ts";
14
+
15
+ /** Custom message type of a batch of ended turns; the transcript box is registered for it. */
16
+ export const RUN_NOTIFICATION_TYPE = "lune-subagent-notification";
17
+
18
+ const SECTION_SEPARATOR = "\n\n---\n\n";
19
+
20
+ /** One ended turn as the main agent is told about it. */
21
+ export interface RunNotice {
22
+ runId: string;
23
+ name: string;
24
+ record: TurnRecord;
25
+ /** The user copied it from the inspector; the run belongs to another branch. */
26
+ copied?: boolean;
27
+ }
28
+
29
+ /**
30
+ * Where a text sits in the content. The box reads report texts from there rather than from a second
31
+ * copy in details, which are stored too.
32
+ */
33
+ export interface ContentSlice {
34
+ start: number;
35
+ end: number;
36
+ }
37
+
38
+ /** A report that a later report of the same turn replaced. */
39
+ export interface EarlierReportNote {
40
+ status: ReportStatus;
41
+ reason?: string;
42
+ question?: string;
43
+ summary: ContentSlice;
44
+ }
45
+
46
+ export interface RunNotificationRun {
47
+ runId: string;
48
+ name: string;
49
+ copied?: boolean;
50
+ /** The end of the turn; with the run ID it names the turn. Older messages do not have it. */
51
+ endedAt?: string;
52
+ state: SettledState;
53
+ reportStatus?: ReportStatus;
54
+ reason?: string;
55
+ question?: string;
56
+ error?: string;
57
+ userWrote: boolean;
58
+ stoppedBy?: StopSource;
59
+ durationMs?: number;
60
+ /** The turn's earlier reports, oldest first. */
61
+ earlierReports?: EarlierReportNote[];
62
+ /** The report summary, or the last message of an unreported turn. */
63
+ body: ContentSlice;
64
+ }
65
+
66
+ export interface RunNotificationDetails {
67
+ runs: RunNotificationRun[];
68
+ }
69
+
70
+ /** The model-facing text and the box-facing details of one batch; they share the offsets. */
71
+ export function buildRunNotification(batch: readonly RunNotice[]): {
72
+ content: string;
73
+ details: RunNotificationDetails;
74
+ } {
75
+ let content = "";
76
+ const runs: RunNotificationRun[] = [];
77
+
78
+ for (const { runId, name, record, copied } of batch) {
79
+ if (content !== "") {
80
+ content += SECTION_SEPARATOR;
81
+ }
82
+
83
+ const section = new SectionWriter(content.length);
84
+ const report = record.report;
85
+ const earlierReports: EarlierReportNote[] = [];
86
+
87
+ if (copied) {
88
+ section.line(
89
+ `The user copied the last turn of subagent run ${name} (${runId}) from another ` +
90
+ `branch. It ended: ${record.state}.`,
91
+ );
92
+ section.line(
93
+ "The run belongs to that branch, so the subagent tools cannot reach it from here.",
94
+ );
95
+ } else {
96
+ section.line(`Subagent run ${name} (${runId}) ended its turn: ${record.state}.`);
97
+ }
98
+
99
+ if (record.stoppedBy === "user") {
100
+ section.line("The user stopped it from the inspector.");
101
+ } else if (record.stoppedBy === "branch") {
102
+ section.line("It was stopped when the user left its branch.");
103
+ }
104
+
105
+ if (record.userWrote) {
106
+ section.line(
107
+ "The user wrote to this run during the turn, so its report may answer the user " +
108
+ "rather than only your task.",
109
+ );
110
+ }
111
+
112
+ if (record.error) {
113
+ section.line(`Error: ${record.error}`);
114
+ }
115
+
116
+ for (const earlier of record.earlierReports ?? []) {
117
+ section.line(
118
+ `Earlier report in this turn: ${earlier.status}. The run kept working, so a later ` +
119
+ "report replaces it.",
120
+ );
121
+ reportFields(section, earlier);
122
+ section.line("Summary:");
123
+ earlierReports.push({
124
+ status: earlier.status,
125
+ ...(earlier.reason ? { reason: earlier.reason } : {}),
126
+ ...(earlier.question ? { question: earlier.question } : {}),
127
+ summary: section.text(earlier.summary),
128
+ });
129
+ section.line("");
130
+ }
131
+
132
+ let body: ContentSlice;
133
+
134
+ if (report) {
135
+ section.line(`Report: ${report.status}`);
136
+ reportFields(section, report);
137
+ section.line("");
138
+ section.line("Summary:");
139
+ body = section.text(report.summary);
140
+ } else {
141
+ const message = record.unreportedMessage ?? "";
142
+
143
+ section.line("Unreported: the turn ended without a report, so it has no task status.");
144
+ section.line("");
145
+ section.line(message === "" ? "The run left no message." : "The run's last message:");
146
+ body = section.text(message);
147
+ }
148
+
149
+ content += section.content;
150
+ runs.push({
151
+ runId,
152
+ name,
153
+ ...(copied ? { copied } : {}),
154
+ endedAt: record.endedAt,
155
+ state: record.state,
156
+ reportStatus: report?.status,
157
+ reason: report?.reason,
158
+ question: report?.question,
159
+ error: record.error,
160
+ userWrote: record.userWrote,
161
+ stoppedBy: record.stoppedBy,
162
+ durationMs: Date.parse(record.endedAt) - Date.parse(record.startedAt),
163
+ ...(earlierReports.length > 0 ? { earlierReports } : {}),
164
+ body,
165
+ });
166
+ }
167
+
168
+ return { content, details: { runs } };
169
+ }
170
+
171
+ function reportFields(section: SectionWriter, report: RunReport): void {
172
+ if (report.reason) {
173
+ section.line(`Reason: ${report.reason}`);
174
+ }
175
+
176
+ if (report.question) {
177
+ section.line(`Question: ${report.question}`);
178
+ }
179
+ }
180
+
181
+ /** Joins a section's lines with newlines and knows where each text lands in the whole content. */
182
+ class SectionWriter {
183
+ content = "";
184
+ private readonly offset: number;
185
+
186
+ constructor(offset: number) {
187
+ this.offset = offset;
188
+ }
189
+
190
+ line(line: string): void {
191
+ this.text(line);
192
+ }
193
+
194
+ text(text: string): ContentSlice {
195
+ if (this.content !== "") {
196
+ this.content += "\n";
197
+ }
198
+
199
+ const start = this.offset + this.content.length;
200
+
201
+ this.content += text;
202
+
203
+ return { start, end: start + text.length };
204
+ }
205
+ }
206
+
207
+ /** `runId@endedAt`: one turn of one run. */
208
+ export function turnKey(runId: string, endedAt: string): string {
209
+ return `${runId}@${endedAt}`;
210
+ }
211
+
212
+ /** The turns whose notices the session holds. A copied report was never sent as a notice. */
213
+ export function deliveredTurns(entries: readonly SessionEntry[]): Set<string> {
214
+ const keys = new Set<string>();
215
+
216
+ for (const entry of entries) {
217
+ if (entry.type !== "custom_message" || entry.customType !== RUN_NOTIFICATION_TYPE) {
218
+ continue;
219
+ }
220
+
221
+ const runs = (entry.details as Partial<RunNotificationDetails> | undefined)?.runs;
222
+
223
+ if (!Array.isArray(runs)) {
224
+ continue;
225
+ }
226
+
227
+ for (const run of runs) {
228
+ if (!run.copied && typeof run.endedAt === "string") {
229
+ keys.add(turnKey(run.runId, run.endedAt));
230
+ }
231
+ }
232
+ }
233
+
234
+ return keys;
235
+ }
236
+
237
+ /** Where a notice goes when its batch is sent; the active branch can change while it waits. */
238
+ export interface NoticeRouting {
239
+ /** Whether the notice's run is on the active branch, so the main agent can be told now. */
240
+ deliverable(notice: RunNotice): boolean;
241
+ /** Keep a notice for another branch until that branch is active again. */
242
+ hold(notice: RunNotice): void;
243
+ }
244
+
245
+ /** What the notifier reads from the parent session to confirm that its notices arrived. */
246
+ export interface ParentSession {
247
+ /** A run of the main agent is active, so a notice waits in Pi's steering queue. */
248
+ busy(): boolean;
249
+ /** The active run is being aborted; a notice sent now waits in the queue for the next run. */
250
+ aborting(): boolean;
251
+ /** Pi's interactive mode clears its queues before every abort, which deletes queued notices. */
252
+ abortClearsQueues(): boolean;
253
+ /** `turnKey()` values of the notices that the session holds. */
254
+ deliveredTurns(): ReadonlySet<string>;
255
+ }
256
+
257
+ interface Pending {
258
+ item: RunNotice;
259
+ retries: number;
260
+ /** Goes with the main agent's next run instead of starting one. */
261
+ quiet?: boolean;
262
+ }
263
+
264
+ interface Sent extends Pending {
265
+ /** How many runs of the main agent had settled when the notice was sent. */
266
+ settles: number;
267
+ /** It waits in the steering queue of the active run, which an interactive abort clears. */
268
+ clearable: boolean;
269
+ }
270
+
271
+ /**
272
+ * Ended turns that arrive close together go out as one steer message. A sent notice counts as
273
+ * delivered only once the parent session holds it; until then it stays in the outbox.
274
+ */
275
+ export class RunNotifier {
276
+ private buffer: Pending[] = [];
277
+ private outbox: Sent[] = [];
278
+ private settles = 0;
279
+ private delayTimer: ReturnType<typeof setTimeout> | undefined;
280
+ private maxDelayTimer: ReturnType<typeof setTimeout> | undefined;
281
+ private disposed = false;
282
+ /** Set while the session tree changes, when no branch is settled for a notice. */
283
+ private paused = false;
284
+
285
+ private readonly delayMs = 300;
286
+ private readonly maxDelayMs = 1000;
287
+ private readonly maxBatch = 10;
288
+ private readonly maxRetries = 2;
289
+
290
+ private readonly pi: ExtensionAPI;
291
+ private readonly ui: ExtensionUIContext | undefined;
292
+ private readonly routing: NoticeRouting;
293
+ private readonly parent: ParentSession;
294
+
295
+ constructor(
296
+ pi: ExtensionAPI,
297
+ ui: ExtensionUIContext | undefined,
298
+ routing: NoticeRouting,
299
+ parent: ParentSession,
300
+ ) {
301
+ this.pi = pi;
302
+ this.ui = ui;
303
+ this.routing = routing;
304
+ this.parent = parent;
305
+ }
306
+
307
+ push(item: RunNotice): void {
308
+ if (this.disposed) {
309
+ return;
310
+ }
311
+
312
+ this.buffer.push({ item, retries: 0 });
313
+
314
+ if (this.paused) {
315
+ return;
316
+ }
317
+
318
+ if (this.buffer.length >= this.maxBatch) {
319
+ this.flush();
320
+ return;
321
+ }
322
+
323
+ this.schedule();
324
+ }
325
+
326
+ /** Waits for a quiet moment, but never longer than the maximum delay after the oldest arrival. */
327
+ private schedule(): void {
328
+ this.maxDelayTimer ??= setTimeout(() => {
329
+ this.maxDelayTimer = undefined;
330
+ this.flush();
331
+ }, this.maxDelayMs);
332
+
333
+ clearTimeout(this.delayTimer);
334
+ this.delayTimer = setTimeout(() => {
335
+ this.delayTimer = undefined;
336
+ this.flush();
337
+ }, this.delayMs);
338
+ }
339
+
340
+ /** Keep every notice until `resume()`; sent now, one could reach the branch being left. */
341
+ pause(): void {
342
+ this.paused = true;
343
+ this.clearTimers();
344
+ }
345
+
346
+ resume(): void {
347
+ if (!this.paused) {
348
+ return;
349
+ }
350
+
351
+ this.paused = false;
352
+
353
+ if (this.buffer.length > 0) {
354
+ this.schedule();
355
+ }
356
+ }
357
+
358
+ flush(): void {
359
+ if (this.paused || this.buffer.length === 0) {
360
+ return;
361
+ }
362
+
363
+ this.clearTimers();
364
+
365
+ const ready = this.route(this.buffer.splice(0));
366
+
367
+ this.send(ready.filter(({ quiet }) => !quiet));
368
+ this.send(ready.filter(({ quiet }) => quiet));
369
+ }
370
+
371
+ /**
372
+ * Called when a run of the main agent settles; Pi has then saved every message that the run
373
+ * took. A notice sent before the previous settle had a whole run to arrive, because a run takes
374
+ * queued steers and the messages held during a settle when it starts, so a missing one was lost.
375
+ * A notice that waited in the queue of a run the user aborted was deleted with that queue.
376
+ */
377
+ settled(aborted: boolean): void {
378
+ if (this.disposed) {
379
+ return;
380
+ }
381
+
382
+ const settle = this.settles;
383
+
384
+ this.settles += 1;
385
+
386
+ if (this.outbox.length === 0) {
387
+ return;
388
+ }
389
+
390
+ const delivered = this.parent.deliveredTurns();
391
+ const cleared = aborted && this.parent.abortClearsQueues();
392
+ const lost: Pending[] = [];
393
+
394
+ this.outbox = this.outbox.filter((entry) => {
395
+ if (delivered.has(turnKey(entry.item.runId, entry.item.record.endedAt))) {
396
+ return false;
397
+ }
398
+
399
+ if (entry.settles < settle || (cleared && entry.clearable)) {
400
+ lost.push({ item: entry.item, retries: entry.retries });
401
+ return false;
402
+ }
403
+
404
+ return true;
405
+ });
406
+
407
+ if (lost.length > 0) {
408
+ this.resend(lost, aborted);
409
+ }
410
+ }
411
+
412
+ /** Stops parent-facing notifications; queued ones are dropped with the parent session. */
413
+ dispose(): void {
414
+ this.disposed = true;
415
+ this.clearTimers();
416
+ this.buffer = [];
417
+ this.outbox = [];
418
+ }
419
+
420
+ /** The notices whose run is on the active branch; the others wait for their branch. */
421
+ private route(entries: Pending[]): Pending[] {
422
+ return entries.filter(({ item }) => {
423
+ if (this.routing.deliverable(item)) {
424
+ return true;
425
+ }
426
+
427
+ this.routing.hold(item);
428
+
429
+ return false;
430
+ });
431
+ }
432
+
433
+ private send(batch: Pending[]): void {
434
+ if (batch.length === 0) {
435
+ return;
436
+ }
437
+
438
+ const quiet = batch[0]!.quiet === true;
439
+
440
+ try {
441
+ const { content, details } = buildRunNotification(batch.map(({ item }) => item));
442
+
443
+ // A normal return is not a delivery: Pi can still drop a queued message, which
444
+ // settled() finds. Pi reports a failed start as an extension error; only a stale
445
+ // runtime throws here.
446
+ this.pi.sendMessage(
447
+ { customType: RUN_NOTIFICATION_TYPE, content, display: true, details },
448
+ quiet ? { deliverAs: "nextTurn" } : { triggerTurn: true, deliverAs: "steer" },
449
+ );
450
+ } catch (error) {
451
+ const retrying = batch.filter(({ retries }) => retries < this.maxRetries);
452
+ const dropped = batch.filter((entry) => !retrying.includes(entry));
453
+
454
+ for (const entry of retrying) {
455
+ entry.retries += 1;
456
+ }
457
+
458
+ // Requeued notifications go out on the next schedule, never in a tight retry loop.
459
+ this.buffer = retrying.concat(this.buffer);
460
+ this.reportFailure(
461
+ error,
462
+ retrying.map(({ item }) => item),
463
+ dropped.map(({ item }) => item),
464
+ );
465
+
466
+ if (this.buffer.length > 0) {
467
+ this.schedule();
468
+ }
469
+
470
+ return;
471
+ }
472
+
473
+ const clearable = !quiet && this.parent.busy() && !this.parent.aborting();
474
+
475
+ for (const entry of batch) {
476
+ this.outbox.push({ ...entry, settles: this.settles, clearable });
477
+ }
478
+
479
+ if (quiet) {
480
+ this.notify(
481
+ `An interruption removed the queued report of subagent run(s) [${ids(batch)}]. ` +
482
+ "The main agent receives it with your next message.",
483
+ "info",
484
+ );
485
+ }
486
+ }
487
+
488
+ /**
489
+ * Send lost notices again. After an abort they go with the next run: starting one would undo
490
+ * the user's abort.
491
+ */
492
+ private resend(lost: Pending[], aborted: boolean): void {
493
+ const retrying = lost.filter(({ retries }) => retries < this.maxRetries);
494
+ const dropped = lost.filter((entry) => !retrying.includes(entry));
495
+
496
+ if (dropped.length > 0) {
497
+ this.notify(
498
+ `Subagent notification(s) [${ids(dropped)}] did not reach the main agent after ` +
499
+ `${this.maxRetries} retries. subagent_status still returns the results.`,
500
+ "error",
501
+ );
502
+ }
503
+
504
+ if (retrying.length === 0) {
505
+ return;
506
+ }
507
+
508
+ for (const entry of retrying) {
509
+ entry.retries += 1;
510
+ entry.quiet = aborted;
511
+ }
512
+
513
+ if (this.paused) {
514
+ this.buffer = retrying.concat(this.buffer);
515
+ } else if (aborted) {
516
+ // Sent at once, so other waiting notices keep their own schedule.
517
+ this.send(this.route(retrying));
518
+ } else {
519
+ this.buffer = retrying.concat(this.buffer);
520
+ this.schedule();
521
+ }
522
+ }
523
+
524
+ private clearTimers(): void {
525
+ clearTimeout(this.delayTimer);
526
+ clearTimeout(this.maxDelayTimer);
527
+ this.delayTimer = undefined;
528
+ this.maxDelayTimer = undefined;
529
+ }
530
+
531
+ private reportFailure(error: unknown, retrying: RunNotice[], dropped: RunNotice[]): void {
532
+ const reason = error instanceof Error ? error.message : String(error);
533
+
534
+ if (retrying.length > 0) {
535
+ this.notify(
536
+ `Failed to send subagent notification(s) [${noticeIds(retrying)}]: ${reason}. Requeued; at most ${this.maxRetries} retries per notification.`,
537
+ "error",
538
+ );
539
+ }
540
+
541
+ if (dropped.length > 0) {
542
+ this.notify(
543
+ `Permanently failed subagent notification(s) [${noticeIds(dropped)}]: ${reason}. Dropped; subagent_status still returns the results.`,
544
+ "error",
545
+ );
546
+ }
547
+ }
548
+
549
+ private notify(message: string, type: "info" | "error"): void {
550
+ try {
551
+ if (this.ui) {
552
+ this.ui.notify(message, type);
553
+ return;
554
+ }
555
+ } catch {
556
+ // A retired UI cannot show it; the console still can.
557
+ }
558
+
559
+ console.error(message);
560
+ }
561
+ }
562
+
563
+ function ids(entries: readonly Pending[]): string {
564
+ return noticeIds(entries.map(({ item }) => item));
565
+ }
566
+
567
+ function noticeIds(items: readonly RunNotice[]): string {
568
+ return items.map((item) => item.runId).join(", ");
569
+ }
package/src/paths.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { join } from "node:path";
2
+
3
+ import { CONFIG_DIR_NAME, getAgentDir } from "@earendil-works/pi-coding-agent";
4
+
5
+ /** Definitions live directly in this folder; its `sessions/` and `worktrees/` children are never read as definitions. */
6
+ export function definitionsDir(agentDir: string = getAgentDir()): string {
7
+ return join(agentDir, "lune-subagents");
8
+ }
9
+
10
+ /** A project's definitions sit beside Pi's own project resources, under the same folder name. */
11
+ export function projectDefinitionsDir(cwd: string): string {
12
+ return join(cwd, CONFIG_DIR_NAME, "lune-subagents");
13
+ }
14
+
15
+ /** Run sessions sit outside Pi's sessions folder, so `/resume` and Core's `@` mentions never list them. */
16
+ export function runSessionsDir(parentSessionId: string, agentDir: string = getAgentDir()): string {
17
+ return join(definitionsDir(agentDir), "sessions", parentSessionId);
18
+ }
19
+
20
+ export function runSessionFile(
21
+ parentSessionId: string,
22
+ runId: string,
23
+ agentDir: string = getAgentDir(),
24
+ ): string {
25
+ return join(runSessionsDir(parentSessionId, agentDir), `${runId}.jsonl`);
26
+ }
27
+
28
+ export function runWorktreePath(
29
+ parentSessionId: string,
30
+ runId: string,
31
+ agentDir: string = getAgentDir(),
32
+ ): string {
33
+ return join(definitionsDir(agentDir), "worktrees", parentSessionId, runId);
34
+ }
@@ -0,0 +1,68 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import type { Component } from "@earendil-works/pi-tui";
3
+
4
+ import type { SettledState } from "../run/types.ts";
5
+
6
+ /**
7
+ * Core Theme publishes its renderer here only while it draws the transcript, so a missing value
8
+ * means the theme is off or not installed.
9
+ */
10
+ const LUNE_THEME_RENDERER = Symbol.for("@lune_99/pi-core-theme/renderer/v1");
11
+
12
+ export type LuneActivityStatus = "running" | "done" | "failed" | "cancelled";
13
+
14
+ /** The part of Core Theme's renderer contract this package uses; Core Theme is never a dependency. */
15
+ export interface LuneActivity {
16
+ title: string;
17
+ summary?: string;
18
+ entries: readonly {
19
+ name: string;
20
+ summary?: string;
21
+ status: LuneActivityStatus;
22
+ detail?: string;
23
+ durationMs?: number;
24
+ error?: string;
25
+ /** Display text without terminal control sequences. */
26
+ output?: string;
27
+ fullOutputPath?: string;
28
+ }[];
29
+ }
30
+
31
+ export interface LuneThemeRenderer {
32
+ readonly version: 1;
33
+ renderActivity(
34
+ activity: LuneActivity,
35
+ options: {
36
+ expanded: boolean;
37
+ /** Lets the theme remember what a click opened; Pi rebuilds message components often. */
38
+ key?: object;
39
+ },
40
+ theme: Theme,
41
+ ): Component;
42
+ }
43
+
44
+ /** Looked up on every render: the theme can be switched while a surface stays on screen. */
45
+ export function luneThemeRenderer(): LuneThemeRenderer | undefined {
46
+ const renderer = (globalThis as Record<symbol, unknown>)[LUNE_THEME_RENDERER] as
47
+ | LuneThemeRenderer
48
+ | undefined;
49
+
50
+ return renderer?.version === 1 ? renderer : undefined;
51
+ }
52
+
53
+ /**
54
+ * The run state in the theme's vocabulary. A stopped turn was stopped on request, which the theme
55
+ * calls cancelled rather than failed. The report status stays separate, in the entry's detail.
56
+ */
57
+ export function activityStatus(state: SettledState | "running"): LuneActivityStatus {
58
+ switch (state) {
59
+ case "running":
60
+ return "running";
61
+ case "idle":
62
+ return "done";
63
+ case "failed":
64
+ return "failed";
65
+ case "interrupted":
66
+ return "cancelled";
67
+ }
68
+ }