@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.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +169 -2
- package/docs/capture.json +30 -0
- package/docs/previews.md +22 -0
- package/docs/reference.md +240 -0
- package/docs/subagent-auto-tiers.png +0 -0
- package/docs/subagent-inspector.png +0 -0
- package/docs/subagent-overview.png +0 -0
- package/package.json +82 -5
- package/skills/create-lune-subagent/SKILL.md +186 -0
- package/src/auto-model.ts +592 -0
- package/src/catalog.ts +111 -0
- package/src/definition-file.ts +112 -0
- package/src/definitions.ts +324 -0
- package/src/dock/dock-alert.ts +78 -0
- package/src/dock/dock-contribution.ts +318 -0
- package/src/extension-tools.ts +46 -0
- package/src/index.ts +168 -0
- package/src/inspector/details.ts +113 -0
- package/src/inspector/inspector.ts +653 -0
- package/src/inspector/layout.ts +119 -0
- package/src/inspector/transcript.ts +735 -0
- package/src/inspector/viewport.ts +62 -0
- package/src/inspector/wrap.ts +136 -0
- package/src/models.ts +54 -0
- package/src/notify/notification-box.ts +381 -0
- package/src/notify/notifier.ts +569 -0
- package/src/paths.ts +34 -0
- package/src/render/lune-theme.ts +68 -0
- package/src/render/text.ts +121 -0
- package/src/run/guide.ts +26 -0
- package/src/run/report-tool.ts +90 -0
- package/src/run/run-file.ts +193 -0
- package/src/run/run-manager.ts +838 -0
- package/src/run/run-session.ts +307 -0
- package/src/run/run.ts +681 -0
- package/src/run/session-start.ts +283 -0
- package/src/run/types.ts +167 -0
- package/src/run/worktree.ts +138 -0
- package/src/schema.ts +9 -0
- package/src/settings/agents-menu.ts +363 -0
- package/src/settings/panel.ts +179 -0
- package/src/settings/schema.ts +71 -0
- package/src/tools/format.ts +375 -0
- package/src/tools/subagent-tools.ts +467 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export interface ViewportWindow {
|
|
2
|
+
start: number;
|
|
3
|
+
/** Rows below the window that the user has not seen yet. */
|
|
4
|
+
newestHidden: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Which rows of a transcript the pane shows.
|
|
9
|
+
*
|
|
10
|
+
* The position is an absolute anchor, the first visible row, and `undefined` follows the newest
|
|
11
|
+
* row. That makes scrolling a pause: a tail-relative offset would drag the view along while the
|
|
12
|
+
* run streams and the user reads.
|
|
13
|
+
*/
|
|
14
|
+
export class Viewport {
|
|
15
|
+
private anchor: number | undefined;
|
|
16
|
+
/** Rows the last frame could show; key handling needs it to clamp the anchor. */
|
|
17
|
+
private capacity = 0;
|
|
18
|
+
private total = 0;
|
|
19
|
+
|
|
20
|
+
get paused(): boolean {
|
|
21
|
+
return this.anchor !== undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Fit the view to a frame; landing on the newest rows means following again. */
|
|
25
|
+
window(total: number, capacity: number): ViewportWindow {
|
|
26
|
+
this.capacity = capacity;
|
|
27
|
+
this.total = total;
|
|
28
|
+
|
|
29
|
+
const tailStart = Math.max(0, total - capacity);
|
|
30
|
+
const start = this.anchor === undefined ? tailStart : Math.min(this.anchor, tailStart);
|
|
31
|
+
|
|
32
|
+
this.anchor = start >= tailStart ? undefined : start;
|
|
33
|
+
|
|
34
|
+
return { start, newestHidden: Math.max(0, total - (start + capacity)) };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Returns whether the view moved. */
|
|
38
|
+
scrollBy(step: number): boolean {
|
|
39
|
+
if (this.capacity === 0 || (this.anchor === undefined && step > 0)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const tailStart = Math.max(0, this.total - this.capacity);
|
|
44
|
+
const previous = this.anchor ?? tailStart;
|
|
45
|
+
|
|
46
|
+
this.anchor = Math.min(tailStart, Math.max(0, previous + step));
|
|
47
|
+
|
|
48
|
+
if (this.anchor >= tailStart) {
|
|
49
|
+
this.anchor = undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return (this.anchor ?? tailStart) !== previous;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
jumpToOldest(): void {
|
|
56
|
+
this.anchor = 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
follow(): void {
|
|
60
|
+
this.anchor = undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
// Two generations of this many characters hold every line of a long transcript at one width.
|
|
4
|
+
const GENERATION_LIMIT_CHARS = 1_000_000;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Wrapped rows per display line and width.
|
|
8
|
+
*
|
|
9
|
+
* A streamed answer grows at its end, so keying by text wraps only the lines that are new. Two
|
|
10
|
+
* generations bound the memory: lines the last frames did not need are dropped on the next rotation,
|
|
11
|
+
* which also drops the rows of a width the pane no longer has.
|
|
12
|
+
*/
|
|
13
|
+
export class LineWrapCache {
|
|
14
|
+
private recent = new Map<string, readonly string[]>();
|
|
15
|
+
private previous = new Map<string, readonly string[]>();
|
|
16
|
+
private recentChars = 0;
|
|
17
|
+
|
|
18
|
+
/** `line` holds no newline. */
|
|
19
|
+
rows(line: string, width: number): readonly string[] {
|
|
20
|
+
const key = `${width}\n${line}`;
|
|
21
|
+
const hit = this.recent.get(key);
|
|
22
|
+
|
|
23
|
+
if (hit) {
|
|
24
|
+
return hit;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const rows = this.previous.get(key) ?? wrapTextWithAnsi(line, width);
|
|
28
|
+
|
|
29
|
+
if (this.recentChars >= GENERATION_LIMIT_CHARS) {
|
|
30
|
+
this.previous = this.recent;
|
|
31
|
+
this.recent = new Map();
|
|
32
|
+
this.recentChars = 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.recent.set(key, rows);
|
|
36
|
+
this.recentChars += line.length;
|
|
37
|
+
|
|
38
|
+
return rows;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
clear(): void {
|
|
42
|
+
this.recent.clear();
|
|
43
|
+
this.previous.clear();
|
|
44
|
+
this.recentChars = 0;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface Excerpt {
|
|
49
|
+
rows: string[];
|
|
50
|
+
/** Lines left out, counting a line that was cut. */
|
|
51
|
+
hidden: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The first `limit` rows of `lines` wrapped to `width`.
|
|
56
|
+
*
|
|
57
|
+
* Only the lines that show are wrapped: a tool's output can be hundreds of kilobytes, and wrapping
|
|
58
|
+
* all of it to keep three rows is what made opening a run slow.
|
|
59
|
+
*/
|
|
60
|
+
export function headRows(lines: readonly string[], width: number, limit: number): Excerpt {
|
|
61
|
+
const bound = boundOf(width, limit);
|
|
62
|
+
const rows: string[] = [];
|
|
63
|
+
let shown = 0;
|
|
64
|
+
|
|
65
|
+
while (shown < lines.length && rows.length < limit) {
|
|
66
|
+
const line = lines[shown]!;
|
|
67
|
+
const kept = line.length > bound ? sliceHead(line, bound) : line;
|
|
68
|
+
|
|
69
|
+
rows.push(...wrapTextWithAnsi(kept, width));
|
|
70
|
+
|
|
71
|
+
if (kept !== line || rows.length > limit) {
|
|
72
|
+
rows.length = Math.min(rows.length, limit);
|
|
73
|
+
|
|
74
|
+
return { rows, hidden: lines.length - shown };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
shown += 1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return { rows, hidden: lines.length - shown };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The last `limit` rows of `lines` wrapped to `width`, wrapping from the end. */
|
|
84
|
+
export function tailRows(lines: readonly string[], width: number, limit: number): Excerpt {
|
|
85
|
+
const bound = boundOf(width, limit);
|
|
86
|
+
const chunks: string[][] = [];
|
|
87
|
+
let count = 0;
|
|
88
|
+
// Lines before this index are not shown in full.
|
|
89
|
+
let first = lines.length;
|
|
90
|
+
|
|
91
|
+
while (first > 0 && count < limit) {
|
|
92
|
+
const line = lines[first - 1]!;
|
|
93
|
+
const kept = line.length > bound ? sliceTail(line, bound) : line;
|
|
94
|
+
const wrapped = wrapTextWithAnsi(kept, width);
|
|
95
|
+
|
|
96
|
+
chunks.push(wrapped);
|
|
97
|
+
count += wrapped.length;
|
|
98
|
+
|
|
99
|
+
if (kept !== line || count > limit) {
|
|
100
|
+
return { rows: chunks.reverse().flat().slice(-limit), hidden: first };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
first -= 1;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return { rows: chunks.reverse().flat(), hidden: first };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Characters of one line that fill more than `limit` rows, since a character other than a
|
|
111
|
+
* zero-width one takes at least one cell. A longer line is cut before it is wrapped, so a single
|
|
112
|
+
* huge line costs no more than a short one; the cut line counts as left out.
|
|
113
|
+
*/
|
|
114
|
+
function boundOf(width: number, limit: number): number {
|
|
115
|
+
return (limit + 1) * Math.max(1, width) * 2;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function sliceHead(line: string, length: number): string {
|
|
119
|
+
const end = isHighSurrogate(line.charCodeAt(length - 1)) ? length - 1 : length;
|
|
120
|
+
|
|
121
|
+
return line.slice(0, end);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function sliceTail(line: string, length: number): string {
|
|
125
|
+
const start = line.length - length;
|
|
126
|
+
|
|
127
|
+
return line.slice(isLowSurrogate(line.charCodeAt(start)) ? start + 1 : start);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function isHighSurrogate(code: number): boolean {
|
|
131
|
+
return code >= 0xd800 && code <= 0xdbff;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function isLowSurrogate(code: number): boolean {
|
|
135
|
+
return code >= 0xdc00 && code <= 0xdfff;
|
|
136
|
+
}
|
package/src/models.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
|
|
4
|
+
export interface ModelRef {
|
|
5
|
+
provider: string;
|
|
6
|
+
id: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** The `model` value that lets a classifier choose the run's model; never a `provider/id`. */
|
|
10
|
+
export const AUTO_MODEL = "auto";
|
|
11
|
+
|
|
12
|
+
/** Split `provider/id` at the first slash; model IDs may contain more slashes. */
|
|
13
|
+
export function parseModelRef(ref: string): ModelRef | undefined {
|
|
14
|
+
const slash = ref.indexOf("/");
|
|
15
|
+
|
|
16
|
+
if (slash <= 0 || slash === ref.length - 1) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return { provider: ref.slice(0, slash), id: ref.slice(slash + 1) };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function formatModelRef(model: ModelRef): string {
|
|
24
|
+
return `${model.provider}/${model.id}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The models `subagent_start` may name: the parent's model scope (`--models` or `enabledModels`),
|
|
29
|
+
* or every available model when no scope is configured.
|
|
30
|
+
*/
|
|
31
|
+
export function allowedModelRefs(ctx: ExtensionContext): string[] {
|
|
32
|
+
return allowedModels(ctx).map((model) => formatModelRef(model));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The models of `allowedModelRefs`, once each, in scope order. */
|
|
36
|
+
export function allowedModels(ctx: ExtensionContext): Model<Api>[] {
|
|
37
|
+
const models =
|
|
38
|
+
ctx.scopedModels.length > 0
|
|
39
|
+
? ctx.scopedModels.map((scoped) => scoped.model)
|
|
40
|
+
: ctx.modelRegistry.getAvailable();
|
|
41
|
+
const seen = new Set<string>();
|
|
42
|
+
|
|
43
|
+
return models.filter((model) => {
|
|
44
|
+
const ref = formatModelRef(model);
|
|
45
|
+
|
|
46
|
+
if (seen.has(ref)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
seen.add(ref);
|
|
51
|
+
|
|
52
|
+
return true;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
MessageRenderOptions,
|
|
3
|
+
Theme,
|
|
4
|
+
ThemeBg,
|
|
5
|
+
ThemeColor,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import {
|
|
8
|
+
visibleWidth,
|
|
9
|
+
wrapTextWithAnsi,
|
|
10
|
+
type Component,
|
|
11
|
+
type TuiMouseEvent,
|
|
12
|
+
} from "@earendil-works/pi-tui";
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
activityStatus,
|
|
16
|
+
luneThemeRenderer,
|
|
17
|
+
type LuneActivity,
|
|
18
|
+
type LuneThemeRenderer,
|
|
19
|
+
} from "../render/lune-theme.ts";
|
|
20
|
+
import { bandRow, clipToWidth, displayLines, EXPAND_KEY } from "../render/text.ts";
|
|
21
|
+
import { formatDuration } from "../tools/format.ts";
|
|
22
|
+
import type {
|
|
23
|
+
ContentSlice,
|
|
24
|
+
EarlierReportNote,
|
|
25
|
+
RunNotificationDetails,
|
|
26
|
+
RunNotificationRun,
|
|
27
|
+
} from "./notifier.ts";
|
|
28
|
+
|
|
29
|
+
/** Lines of a report a collapsed box shows; the rest stay behind the expand hint. */
|
|
30
|
+
const PREVIEW_LINES = 5;
|
|
31
|
+
const BODY_INDENT = " ";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Render a batch of ended turns as a band: a header, one row per run and the start of each report.
|
|
35
|
+
* Returns undefined for a message without runs, which leaves it to Pi's own custom-message row.
|
|
36
|
+
*/
|
|
37
|
+
export function renderRunNotificationBox(
|
|
38
|
+
message: { content: unknown; details?: RunNotificationDetails },
|
|
39
|
+
options: MessageRenderOptions,
|
|
40
|
+
theme: Theme,
|
|
41
|
+
): Component | undefined {
|
|
42
|
+
const runs = message.details?.runs;
|
|
43
|
+
|
|
44
|
+
if (!Array.isArray(runs) || runs.length === 0) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return new RunNotificationBox(
|
|
49
|
+
runs,
|
|
50
|
+
typeof message.content === "string" ? message.content : "",
|
|
51
|
+
options,
|
|
52
|
+
theme,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Lines are cached per width, because the transcript renders the box on every frame. While the Lune
|
|
58
|
+
* theme is active the box draws its activity card instead; the renderer is looked up on every frame
|
|
59
|
+
* because the theme can be switched while the message stays on screen.
|
|
60
|
+
*/
|
|
61
|
+
class RunNotificationBox implements Component {
|
|
62
|
+
private readonly runs: readonly RunNotificationRun[];
|
|
63
|
+
private readonly content: string;
|
|
64
|
+
private readonly expanded: boolean;
|
|
65
|
+
private readonly paddingX: number;
|
|
66
|
+
private readonly theme: Theme;
|
|
67
|
+
|
|
68
|
+
private cachedWidth: number | undefined;
|
|
69
|
+
private cachedLines: string[] | undefined;
|
|
70
|
+
private themed: { renderer: LuneThemeRenderer; component: Component } | undefined;
|
|
71
|
+
|
|
72
|
+
constructor(
|
|
73
|
+
runs: readonly RunNotificationRun[],
|
|
74
|
+
content: string,
|
|
75
|
+
options: MessageRenderOptions,
|
|
76
|
+
theme: Theme,
|
|
77
|
+
) {
|
|
78
|
+
this.runs = runs;
|
|
79
|
+
this.content = content;
|
|
80
|
+
this.expanded = options.expanded;
|
|
81
|
+
this.paddingX = Math.max(0, options.outputPad);
|
|
82
|
+
this.theme = theme;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
invalidate(): void {
|
|
86
|
+
this.cachedWidth = undefined;
|
|
87
|
+
this.cachedLines = undefined;
|
|
88
|
+
this.themed?.component.invalidate();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
handleMouse(event: TuiMouseEvent): ReturnType<NonNullable<Component["handleMouse"]>> {
|
|
92
|
+
return this.themed?.component.handleMouse?.(event);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
render(width: number): string[] {
|
|
96
|
+
const renderer = luneThemeRenderer();
|
|
97
|
+
|
|
98
|
+
if (renderer) {
|
|
99
|
+
if (this.themed?.renderer !== renderer) {
|
|
100
|
+
this.themed = {
|
|
101
|
+
renderer,
|
|
102
|
+
component: renderer.renderActivity(
|
|
103
|
+
this.activity(),
|
|
104
|
+
{ expanded: this.expanded, key: this.runs },
|
|
105
|
+
this.theme,
|
|
106
|
+
),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return this.themed.component.render(width);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
this.themed = undefined;
|
|
114
|
+
|
|
115
|
+
if (this.cachedLines !== undefined && this.cachedWidth === width) {
|
|
116
|
+
return this.cachedLines;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
this.cachedWidth = width;
|
|
120
|
+
this.cachedLines = this.buildLines(width);
|
|
121
|
+
|
|
122
|
+
return this.cachedLines;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private buildLines(width: number): string[] {
|
|
126
|
+
const paddingX = Math.min(this.paddingX, Math.max(0, Math.floor((width - 1) / 2)));
|
|
127
|
+
const inner = Math.max(1, width - paddingX * 2);
|
|
128
|
+
const bodyWidth = Math.max(1, inner - BODY_INDENT.length);
|
|
129
|
+
const token = this.bandToken();
|
|
130
|
+
const color: ThemeColor =
|
|
131
|
+
token === "toolErrorBg" ? "error" : token === "toolPendingBg" ? "accent" : "success";
|
|
132
|
+
const rows = [
|
|
133
|
+
`${this.theme.fg(color, "●")} ${this.theme.bold(this.theme.fg(color, this.headerText()))}`,
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
for (const run of this.runs) {
|
|
137
|
+
rows.push(this.runRow(run, inner));
|
|
138
|
+
|
|
139
|
+
for (const note of notes(run)) {
|
|
140
|
+
rows.push(...this.bodyRows(note, bodyWidth, "muted"));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (run.error) {
|
|
144
|
+
rows.push(...this.bodyRows(`Error: ${run.error}`, bodyWidth, "error"));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (run.reason) {
|
|
148
|
+
rows.push(...this.bodyRows(`Reason: ${run.reason}`, bodyWidth, "error"));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (run.question) {
|
|
152
|
+
rows.push(...this.bodyRows(`Question: ${run.question}`, bodyWidth, "accent"));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
rows.push(...this.reportRows(run, bodyWidth));
|
|
156
|
+
|
|
157
|
+
for (const earlier of run.earlierReports ?? []) {
|
|
158
|
+
rows.push(...this.earlierRows(earlier, bodyWidth));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return ["", ...rows, ""].map((row) => bandRow(this.theme, token, row, inner, paddingX));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private activity(): LuneActivity {
|
|
166
|
+
const total = this.runs.length;
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
title: total === 1 ? "subagent" : "subagents",
|
|
170
|
+
summary: total === 1 ? undefined : `${total} runs`,
|
|
171
|
+
entries: this.runs.map((run) => ({
|
|
172
|
+
name: run.name,
|
|
173
|
+
summary: run.question ?? run.reason ?? firstLine(this.body(run)),
|
|
174
|
+
status: activityStatus(run.state),
|
|
175
|
+
detail: [
|
|
176
|
+
run.reportStatus ?? "unreported",
|
|
177
|
+
earlierCount(run),
|
|
178
|
+
run.copied ? "copied" : undefined,
|
|
179
|
+
]
|
|
180
|
+
.filter((part) => part !== undefined)
|
|
181
|
+
.join(" · "),
|
|
182
|
+
durationMs: run.durationMs,
|
|
183
|
+
error: run.error,
|
|
184
|
+
output: displayLines(
|
|
185
|
+
[
|
|
186
|
+
this.body(run),
|
|
187
|
+
...(run.earlierReports ?? []).map((earlier) =>
|
|
188
|
+
[
|
|
189
|
+
earlierTitle(earlier),
|
|
190
|
+
...earlierFields(earlier),
|
|
191
|
+
this.slice(earlier.summary),
|
|
192
|
+
].join("\n"),
|
|
193
|
+
),
|
|
194
|
+
].join("\n\n"),
|
|
195
|
+
).join("\n"),
|
|
196
|
+
})),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private headerText(): string {
|
|
201
|
+
if (this.runs.length === 1) {
|
|
202
|
+
const run = this.runs[0]!;
|
|
203
|
+
|
|
204
|
+
return run.copied
|
|
205
|
+
? `Subagent ${run.name} · copied from another branch`
|
|
206
|
+
: `Subagent ${run.name} ended its turn`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return `${this.runs.length} subagent turns ended`;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** `<name> · <state> · <report status> · <duration>`; the name is clipped first. */
|
|
213
|
+
private runRow(run: RunNotificationRun, width: number): string {
|
|
214
|
+
const state = this.theme.fg(stateColor(run.state), run.state);
|
|
215
|
+
const report = this.theme.fg(
|
|
216
|
+
reportColor(run.reportStatus),
|
|
217
|
+
run.reportStatus ?? "unreported",
|
|
218
|
+
);
|
|
219
|
+
const parts = [state, report];
|
|
220
|
+
|
|
221
|
+
if (run.durationMs !== undefined && Number.isFinite(run.durationMs)) {
|
|
222
|
+
parts.push(this.theme.fg("muted", formatDuration(run.durationMs)));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const separator = this.theme.fg("dim", " · ");
|
|
226
|
+
const used =
|
|
227
|
+
parts.reduce((total, part) => total + visibleWidth(part), 0) +
|
|
228
|
+
parts.length * visibleWidth(separator);
|
|
229
|
+
const name = this.theme.fg("text", clipToWidth(`${run.name} (${run.runId})`, width - used));
|
|
230
|
+
|
|
231
|
+
return [name, ...parts].join(separator);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The start of the report summary, or of an unreported turn's last message. */
|
|
235
|
+
private reportRows(run: RunNotificationRun, width: number): string[] {
|
|
236
|
+
const lines = displayLines(this.body(run)).flatMap((line) => wrapTextWithAnsi(line, width));
|
|
237
|
+
|
|
238
|
+
while (lines.length > 0 && lines[lines.length - 1]!.trim() === "") {
|
|
239
|
+
lines.pop();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const shown = this.expanded ? lines : lines.slice(0, PREVIEW_LINES);
|
|
243
|
+
const hidden = lines.length - shown.length;
|
|
244
|
+
const rows = shown.map((line) => BODY_INDENT + this.theme.fg("toolOutput", line));
|
|
245
|
+
|
|
246
|
+
if (hidden > 0) {
|
|
247
|
+
rows.push(
|
|
248
|
+
BODY_INDENT +
|
|
249
|
+
this.theme.fg("muted", `… ${hidden} more lines · ${EXPAND_KEY} to expand`),
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return rows;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** A report that a later one replaced: one row, and its text once the box is expanded. */
|
|
257
|
+
private earlierRows(earlier: EarlierReportNote, width: number): string[] {
|
|
258
|
+
const rows = this.bodyRows(
|
|
259
|
+
this.expanded
|
|
260
|
+
? earlierTitle(earlier)
|
|
261
|
+
: `${earlierTitle(earlier)} · ${EXPAND_KEY} to expand`,
|
|
262
|
+
width,
|
|
263
|
+
"muted",
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
if (!this.expanded) {
|
|
267
|
+
return rows;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (earlier.reason) {
|
|
271
|
+
rows.push(...this.bodyRows(`Reason: ${earlier.reason}`, width, "error"));
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (earlier.question) {
|
|
275
|
+
rows.push(...this.bodyRows(`Question: ${earlier.question}`, width, "accent"));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
rows.push(...this.bodyRows(this.slice(earlier.summary), width, "toolOutput"));
|
|
279
|
+
|
|
280
|
+
return rows;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private body(run: RunNotificationRun): string {
|
|
284
|
+
return run.body ? this.slice(run.body) : "";
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** A text as the message content holds it; details carry only its offsets. */
|
|
288
|
+
private slice(slice: ContentSlice): string {
|
|
289
|
+
const start = Math.max(0, Math.min(slice.start, this.content.length));
|
|
290
|
+
const end = Math.max(start, Math.min(slice.end, this.content.length));
|
|
291
|
+
|
|
292
|
+
return this.content.slice(start, end);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private bodyRows(text: string, width: number, color: ThemeColor): string[] {
|
|
296
|
+
return displayLines(text)
|
|
297
|
+
.flatMap((line) => wrapTextWithAnsi(line, width))
|
|
298
|
+
.map((line) => BODY_INDENT + this.theme.fg(color, line));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private bandToken(): ThemeBg {
|
|
302
|
+
if (this.runs.some((run) => run.state === "failed" || run.reportStatus === "blocked")) {
|
|
303
|
+
return "toolErrorBg";
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (this.runs.some((run) => run.reportStatus === "needs_input")) {
|
|
307
|
+
return "toolPendingBg";
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return "toolSuccessBg";
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function notes(run: RunNotificationRun): string[] {
|
|
315
|
+
const lines: string[] = [];
|
|
316
|
+
|
|
317
|
+
if (run.stoppedBy === "user") {
|
|
318
|
+
lines.push("Stopped by the user.");
|
|
319
|
+
} else if (run.stoppedBy === "branch") {
|
|
320
|
+
lines.push("Stopped when the user left its branch.");
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (run.userWrote) {
|
|
324
|
+
lines.push("The user wrote to this run during the turn.");
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return lines;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function earlierCount(run: RunNotificationRun): string | undefined {
|
|
331
|
+
const count = run.earlierReports?.length ?? 0;
|
|
332
|
+
|
|
333
|
+
if (count === 0) {
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return `${count} earlier ${count === 1 ? "report" : "reports"}`;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function earlierTitle(earlier: EarlierReportNote): string {
|
|
341
|
+
return `Earlier report: ${earlier.status}, replaced by a later one`;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function earlierFields(earlier: EarlierReportNote): string[] {
|
|
345
|
+
return [
|
|
346
|
+
earlier.reason ? `Reason: ${earlier.reason}` : undefined,
|
|
347
|
+
earlier.question ? `Question: ${earlier.question}` : undefined,
|
|
348
|
+
].filter((field) => field !== undefined);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function firstLine(text: string): string | undefined {
|
|
352
|
+
const line = displayLines(text).find((candidate) => candidate.trim() !== "");
|
|
353
|
+
|
|
354
|
+
return line?.trim();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export function stateColor(state: string): ThemeColor {
|
|
358
|
+
switch (state) {
|
|
359
|
+
case "running":
|
|
360
|
+
return "warning";
|
|
361
|
+
case "idle":
|
|
362
|
+
return "success";
|
|
363
|
+
case "failed":
|
|
364
|
+
return "error";
|
|
365
|
+
default:
|
|
366
|
+
return "muted";
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export function reportColor(status: string | undefined): ThemeColor {
|
|
371
|
+
switch (status) {
|
|
372
|
+
case "done":
|
|
373
|
+
return "success";
|
|
374
|
+
case "blocked":
|
|
375
|
+
return "error";
|
|
376
|
+
case "needs_input":
|
|
377
|
+
return "accent";
|
|
378
|
+
default:
|
|
379
|
+
return "warning";
|
|
380
|
+
}
|
|
381
|
+
}
|