oc-flight-deck 0.6.2 → 0.6.3
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 +187 -187
- package/flight-deck.example.jsonc +208 -208
- package/package.json +6 -6
- package/src/tui/caution.ts +14 -14
- package/src/tui/coerce.ts +25 -0
- package/src/tui/config.ts +9 -9
- package/src/tui/file-config.ts +1 -1
- package/src/tui/format.ts +82 -0
- package/src/tui/guard-tokens.ts +72 -0
- package/src/tui/guard.ts +5 -5
- package/src/tui/index.tsx +278 -679
- package/src/tui/project-totals.ts +49 -0
- package/src/tui/rows.ts +281 -0
- package/src/tui/session-reads.ts +239 -0
- package/src/tui/session-tps.ts +173 -0
- package/src/tui/stat-fields.ts +122 -0
- package/src/tui/stats.ts +12 -634
- package/src/tui/throughput.ts +85 -0
- package/src/tui/ticker.ts +2 -2
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Project spend: every session in THIS project, not just the one on screen.
|
|
2
|
+
//
|
|
3
|
+
// `session.list()` is not scoped - it returns sessions across every
|
|
4
|
+
// directory the host knows about, so summing it gives "everything you have
|
|
5
|
+
// ever run" while claiming to be a project total. Filtered by `projectID`
|
|
6
|
+
// rather than by directory, because one project legitimately spans several
|
|
7
|
+
// directories (worktrees).
|
|
8
|
+
//
|
|
9
|
+
// If the host does not report a project id, the unfiltered list is used, so
|
|
10
|
+
// this degrades to the old behaviour instead of showing nothing.
|
|
11
|
+
// `project` walks every session the host knows about, and that collection grows
|
|
12
|
+
// with your history rather than staying a handful. It moves slowly, so the walk
|
|
13
|
+
// is held briefly instead of repeated on every tick.
|
|
14
|
+
|
|
15
|
+
import type { Plugin } from "@opencode/plugin/tui";
|
|
16
|
+
import { asCount, asRecord, asText } from "./coerce.js";
|
|
17
|
+
|
|
18
|
+
export function createProjectTotals(context: Plugin.Context) {
|
|
19
|
+
const PROJECT_CACHE_MS = 5_000;
|
|
20
|
+
let projectCache:
|
|
21
|
+
| { readonly projectID: string | undefined; readonly at: number; readonly value: { cost: number; count: number } }
|
|
22
|
+
| undefined;
|
|
23
|
+
|
|
24
|
+
const projectTotals = (sessionID: string) => {
|
|
25
|
+
try {
|
|
26
|
+
const projectID = asText(asRecord(context.data.session.get(sessionID))?.["projectID"]);
|
|
27
|
+
const cached = projectCache;
|
|
28
|
+
if (cached !== undefined && cached.projectID === projectID && Date.now() - cached.at < PROJECT_CACHE_MS) {
|
|
29
|
+
return cached.value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const sessions = context.data.session.list() ?? [];
|
|
33
|
+
const scoped =
|
|
34
|
+
projectID === undefined
|
|
35
|
+
? sessions
|
|
36
|
+
: sessions.filter((entry) => asText(asRecord(entry)?.["projectID"]) === projectID);
|
|
37
|
+
|
|
38
|
+
let cost = 0;
|
|
39
|
+
for (const entry of scoped) cost += asCount(asRecord(entry)?.["cost"]) ?? 0;
|
|
40
|
+
const value = { cost, count: scoped.length };
|
|
41
|
+
projectCache = { projectID, at: Date.now(), value };
|
|
42
|
+
return value;
|
|
43
|
+
} catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return { projectTotals };
|
|
49
|
+
}
|
package/src/tui/rows.ts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// Row rendering for the Flight Deck rail: one padded line per named field.
|
|
2
|
+
//
|
|
3
|
+
// `statLine` renders a single field or `undefined` when the host has not
|
|
4
|
+
// supplied it yet; `statRows` renders a list of fields and folds in the
|
|
5
|
+
// persistence behaviour. Every value passes through `plain`, so a control
|
|
6
|
+
// character in a host-supplied string cannot move the cursor. Pure, so the
|
|
7
|
+
// rendering is trivial to test.
|
|
8
|
+
|
|
9
|
+
import { DEFAULT_BAR_WIDTH, clip, formatCost, formatCount, formatDuration, fuelBar, sparkline } from "./format.js";
|
|
10
|
+
import { asCount, asRecord, asText } from "./coerce.js";
|
|
11
|
+
import { guardToken } from "./guard-tokens.js";
|
|
12
|
+
import {
|
|
13
|
+
DEFAULT_LABEL_WIDTH,
|
|
14
|
+
DEFAULT_PLACEHOLDER,
|
|
15
|
+
DEFAULT_SPARK_WIDTH,
|
|
16
|
+
isStatField,
|
|
17
|
+
type LayoutHint,
|
|
18
|
+
type StatSource,
|
|
19
|
+
} from "./stat-fields.js";
|
|
20
|
+
|
|
21
|
+
/** Braille frames, advanced by the ticker, shown only while the session runs. */
|
|
22
|
+
const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] as const;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Flatten control characters in anything about to be drawn.
|
|
26
|
+
*
|
|
27
|
+
* A rail is one line per row, so a newline or an escape sequence in a
|
|
28
|
+
* host-supplied string does not get seen — it moves the cursor. Config text is
|
|
29
|
+
* already flattened in ./config.ts, but a tool name, a permission resource or a
|
|
30
|
+
* branch name arrives straight from the host, so every row value passes through
|
|
31
|
+
* here on its way out. `replace` is unconditional rather than `test` + `replace`:
|
|
32
|
+
* a global regex used with `test` carries `lastIndex` between calls.
|
|
33
|
+
*/
|
|
34
|
+
const CONTROL_CHARS = /[\u0000-\u001F\u007F-\u009F]/g;
|
|
35
|
+
|
|
36
|
+
function plain(value: string): string {
|
|
37
|
+
return value.replace(CONTROL_CHARS, " ");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* One padded rail row: the label in its column, at least one space, then the
|
|
42
|
+
* flattened value. Shared by live rows and persistent placeholders so both
|
|
43
|
+
* keep the same column.
|
|
44
|
+
*/
|
|
45
|
+
function formatRow(label: string, value: string, labelWidth: number): string {
|
|
46
|
+
return `${label.padEnd(labelWidth)}${label.length >= labelWidth ? " " : ""}${plain(value)}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Render one named field, or `undefined` when the host has not supplied it yet.
|
|
51
|
+
*
|
|
52
|
+
* Returning `undefined` rather than a placeholder is deliberate: a rail that
|
|
53
|
+
* shows `branch unknown` before the repo loads looks broken, while a rail
|
|
54
|
+
* that simply grows a row when the data arrives looks alive.
|
|
55
|
+
*/
|
|
56
|
+
export function statLine(
|
|
57
|
+
field: string,
|
|
58
|
+
source: StatSource,
|
|
59
|
+
layout: LayoutHint = {},
|
|
60
|
+
): string | undefined {
|
|
61
|
+
// Bound here rather than at module scope so a config file can change the
|
|
62
|
+
// column width without every call site having to know about it.
|
|
63
|
+
const labelWidth = layout.labelWidth ?? DEFAULT_LABEL_WIDTH;
|
|
64
|
+
const barWidth = layout.barWidth ?? DEFAULT_BAR_WIDTH;
|
|
65
|
+
const sparkWidth = layout.sparkWidth ?? DEFAULT_SPARK_WIDTH;
|
|
66
|
+
// At least one space, always.
|
|
67
|
+
//
|
|
68
|
+
// `padEnd` returns the label unchanged when it is already wider than the
|
|
69
|
+
// column, so a label longer than `labelWidth` was glued straight onto its
|
|
70
|
+
// value: at labelWidth 8 the `reasoning` row rendered as "reasoning153k".
|
|
71
|
+
// The label is nine characters, which made this reachable from a config file.
|
|
72
|
+
const row = (label: string, value: string): string => formatRow(label, value, labelWidth);
|
|
73
|
+
|
|
74
|
+
switch (field) {
|
|
75
|
+
case "caution": {
|
|
76
|
+
// Rendered only when the caller found something to say, which is what
|
|
77
|
+
// keeps a healthy session visually identical to one without the
|
|
78
|
+
// annunciator at all. The string already carries its glyph; nothing here
|
|
79
|
+
// knows or cares about severity.
|
|
80
|
+
const text = asText(source.caution);
|
|
81
|
+
return text === undefined ? undefined : row("caution", text);
|
|
82
|
+
}
|
|
83
|
+
case "status": {
|
|
84
|
+
const status = asText(source.status);
|
|
85
|
+
if (status === undefined && source.busy === undefined) return undefined;
|
|
86
|
+
// The spinner lives here rather than in a row of its own: an animated
|
|
87
|
+
// glyph beside "running" says the same thing without spending a line.
|
|
88
|
+
//
|
|
89
|
+
// `busy` folds in work the session's own status misses — a subagent
|
|
90
|
+
// running in its own session, or a shell command still going — so the
|
|
91
|
+
// glyph only stops when there is genuinely nothing happening. A host that
|
|
92
|
+
// does not report it falls back to the session's own status.
|
|
93
|
+
if (source.busy !== true && status !== "running") return row("status", "○ idle");
|
|
94
|
+
// Floored at the boundary: a fractional frame would index the frame list
|
|
95
|
+
// with a non-integer and render `undefined`.
|
|
96
|
+
const frame = Math.floor(asCount(source.frame) ?? 0);
|
|
97
|
+
return row("status", `${SPINNER[frame % SPINNER.length]} running`);
|
|
98
|
+
}
|
|
99
|
+
case "agent": {
|
|
100
|
+
const agent = asText(source.agent);
|
|
101
|
+
return agent === undefined ? undefined : row("agent", agent);
|
|
102
|
+
}
|
|
103
|
+
case "model": {
|
|
104
|
+
const model = asRecord(source.model);
|
|
105
|
+
const id = asText(model?.id);
|
|
106
|
+
if (id === undefined) return undefined;
|
|
107
|
+
const variant = asText(model?.variant);
|
|
108
|
+
return row("model", variant === undefined ? id : `${id} · ${variant}`);
|
|
109
|
+
}
|
|
110
|
+
case "branch": {
|
|
111
|
+
const branch = asText(source.branch);
|
|
112
|
+
return branch === undefined ? undefined : row("branch", branch);
|
|
113
|
+
}
|
|
114
|
+
case "cost": {
|
|
115
|
+
const cost = asCount(source.cost);
|
|
116
|
+
const tree = asRecord(source.tree);
|
|
117
|
+
const treeCost = asCount(tree?.cost);
|
|
118
|
+
const count = asCount(tree?.count);
|
|
119
|
+
const family = count !== undefined && count > 0 ? count : 0;
|
|
120
|
+
|
|
121
|
+
// Merge only when subagents actually ran, and only when the rail is not
|
|
122
|
+
// already showing a `total` row. Two rows holding a number and its own
|
|
123
|
+
// superset was the thing worth fixing; a setting to choose between them
|
|
124
|
+
// would just be that duplication with extra steps.
|
|
125
|
+
const merge =
|
|
126
|
+
layout.hasTotalRow !== true &&
|
|
127
|
+
cost !== undefined &&
|
|
128
|
+
treeCost !== undefined &&
|
|
129
|
+
family > 0 &&
|
|
130
|
+
treeCost > cost;
|
|
131
|
+
|
|
132
|
+
if (!merge) {
|
|
133
|
+
const value = layout.hasTotalRow === true ? (cost ?? treeCost) : (treeCost ?? cost);
|
|
134
|
+
return value === undefined ? undefined : row("cost", formatCost(value));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// The delta (`+$0.020`) used to be here and pushed the row to 40 columns,
|
|
138
|
+
// wide enough to wrap in a normal sidebar. It was real information that
|
|
139
|
+
// did not earn its width: the count already explains why the figure is
|
|
140
|
+
// higher, and this form is the same width as the old `total` row.
|
|
141
|
+
const plural = family === 1 ? "subagent" : "subagents";
|
|
142
|
+
return row("cost", `${formatCost(treeCost as number)} · ${family} ${plural}`);
|
|
143
|
+
}
|
|
144
|
+
case "total": {
|
|
145
|
+
// Only worth a row once a subagent has actually run: subagent sessions are
|
|
146
|
+
// separate, so the parent's `cost` alone understates what was spent.
|
|
147
|
+
const tree = asRecord(source.tree);
|
|
148
|
+
const count = asCount(tree?.count);
|
|
149
|
+
const cost = asCount(tree?.cost);
|
|
150
|
+
if (cost === undefined || count === undefined || count === 0) return undefined;
|
|
151
|
+
const plural = count === 1 ? "subagent" : "subagents";
|
|
152
|
+
return row("total", `${formatCost(cost)} · ${count} ${plural}`);
|
|
153
|
+
}
|
|
154
|
+
case "project": {
|
|
155
|
+
const project = asRecord(source.project);
|
|
156
|
+
const cost = asCount(project?.cost);
|
|
157
|
+
if (cost === undefined) return undefined;
|
|
158
|
+
const count = asCount(project?.count);
|
|
159
|
+
return row("project", `${formatCost(cost)}${count !== undefined && count > 1 ? ` · ${count} sessions` : ""}`);
|
|
160
|
+
}
|
|
161
|
+
case "tokens": {
|
|
162
|
+
const tokens = asRecord(source.tokens);
|
|
163
|
+
const input = asCount(tokens?.input);
|
|
164
|
+
const output = asCount(tokens?.output);
|
|
165
|
+
if (input === undefined && output === undefined) return undefined;
|
|
166
|
+
return row("tokens", `${formatCount(input ?? 0)} in · ${formatCount(output ?? 0)} out`);
|
|
167
|
+
}
|
|
168
|
+
case "cache": {
|
|
169
|
+
const tokens = asRecord(source.tokens);
|
|
170
|
+
const cache = asRecord(tokens?.cache);
|
|
171
|
+
const read = asCount(cache?.read);
|
|
172
|
+
if (read === undefined || read === 0) return undefined;
|
|
173
|
+
const input = asCount(tokens?.input);
|
|
174
|
+
// Hit ratio is the whole point: it explains a cheap bill on a huge token
|
|
175
|
+
// count, and it is the first thing to break when caching stops working.
|
|
176
|
+
const total = input === undefined ? undefined : read + input;
|
|
177
|
+
const value =
|
|
178
|
+
total === undefined || total === 0
|
|
179
|
+
? `${formatCount(read)} read`
|
|
180
|
+
: `${Math.round((read / total) * 100)}% hit · ${formatCount(read)} read`;
|
|
181
|
+
return row("cache", value);
|
|
182
|
+
}
|
|
183
|
+
case "context": {
|
|
184
|
+
const context = asRecord(source.context);
|
|
185
|
+
const used = asCount(context?.used);
|
|
186
|
+
if (used === undefined || used === 0) return undefined;
|
|
187
|
+
const limit = asCount(context?.limit);
|
|
188
|
+
if (limit === undefined || limit === 0) return row("context", `${formatCount(used)} used`);
|
|
189
|
+
const ratio = used / limit;
|
|
190
|
+
// Show the percentage whenever the host reports it, but never a bar that
|
|
191
|
+
// reads as more than full.
|
|
192
|
+
return row("context", `${fuelBar(ratio, barWidth)} ${Math.round(Math.min(1, ratio) * 100)}%`);
|
|
193
|
+
}
|
|
194
|
+
case "perms": {
|
|
195
|
+
const perms = asRecord(source.perms);
|
|
196
|
+
const count = asCount(perms?.["count"]);
|
|
197
|
+
if (count === undefined || count === 0) return undefined;
|
|
198
|
+
const action = asText(perms?.["action"]);
|
|
199
|
+
const resource = asText(perms?.["resource"]);
|
|
200
|
+
|
|
201
|
+
// One request: name it, because that is the decision you are being asked
|
|
202
|
+
// to make. Several: the count leads, because the first request in the
|
|
203
|
+
// queue is not necessarily the one you are about to be shown.
|
|
204
|
+
if (count > 1) {
|
|
205
|
+
return row("perms", action === undefined ? `${count} waiting` : `${count} waiting · ${action}`);
|
|
206
|
+
}
|
|
207
|
+
if (action === undefined) return row("perms", "1 waiting");
|
|
208
|
+
// Clipped hard: a row that wraps costs the reader more than a row that
|
|
209
|
+
// shortens a path, and there is no way to ask the host how wide it is.
|
|
210
|
+
return row("perms", resource === undefined ? action : `${action} · ${clip(resource, 18)}`);
|
|
211
|
+
}
|
|
212
|
+
case "elapsed": {
|
|
213
|
+
const ms = asCount(source.elapsedMs);
|
|
214
|
+
if (ms === undefined || ms <= 0) return undefined;
|
|
215
|
+
return row("elapsed", formatDuration(ms));
|
|
216
|
+
}
|
|
217
|
+
case "tps": {
|
|
218
|
+
const tps = asCount(source.tps);
|
|
219
|
+
if (tps === undefined || tps === 0) return undefined;
|
|
220
|
+
return row("tps", `${Math.round(tps)} tok/s`);
|
|
221
|
+
}
|
|
222
|
+
case "spark": {
|
|
223
|
+
const values = Array.isArray(source.spark) ? source.spark.map((value) => asCount(value) ?? 0) : [];
|
|
224
|
+
// The newest samples are the interesting ones, so a narrowed window keeps
|
|
225
|
+
// the recent shape rather than the oldest.
|
|
226
|
+
const windowed = values.slice(-Math.max(2, Math.floor(sparkWidth)));
|
|
227
|
+
if (windowed.length < 2) return undefined;
|
|
228
|
+
return row("spark", sparkline(windowed));
|
|
229
|
+
}
|
|
230
|
+
case "reasoning": {
|
|
231
|
+
const reasoning = asCount(asRecord(source.tokens)?.reasoning);
|
|
232
|
+
if (reasoning === undefined || reasoning === 0) return undefined;
|
|
233
|
+
return row("reasoning", formatCount(reasoning));
|
|
234
|
+
}
|
|
235
|
+
case "turns": {
|
|
236
|
+
const turns = asCount(source.turns);
|
|
237
|
+
if (turns === undefined || turns === 0) return undefined;
|
|
238
|
+
return row("turns", String(turns));
|
|
239
|
+
}
|
|
240
|
+
case "guard": {
|
|
241
|
+
const token = guardToken(source.guard);
|
|
242
|
+
return token === undefined ? undefined : row("guard", token);
|
|
243
|
+
}
|
|
244
|
+
default:
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Render every named field, in the given order.
|
|
250
|
+
*
|
|
251
|
+
* By default only fields with data render; with `layout.persist` every known
|
|
252
|
+
* field renders exactly one row, using `layout.placeholder` (default `"—"`)
|
|
253
|
+
* for the value when the host has nothing to show yet. `statLine` keeps
|
|
254
|
+
* returning `string | undefined` — persistence lives here, not there.
|
|
255
|
+
*/
|
|
256
|
+
export function statRows(
|
|
257
|
+
fields: readonly string[],
|
|
258
|
+
source: StatSource,
|
|
259
|
+
layout: LayoutHint = {},
|
|
260
|
+
): readonly string[] {
|
|
261
|
+
// The rows list decides whether `cost` merges the subagent total into itself.
|
|
262
|
+
// Passing that down means the choice lives in `sidebar.rows`, where someone
|
|
263
|
+
// is already deciding what the rail shows.
|
|
264
|
+
const hint: LayoutHint = { ...layout, hasTotalRow: fields.includes("total") };
|
|
265
|
+
const persist = hint.persist === true;
|
|
266
|
+
const placeholder = hint.placeholder ?? DEFAULT_PLACEHOLDER;
|
|
267
|
+
const labelWidth = hint.labelWidth ?? DEFAULT_LABEL_WIDTH;
|
|
268
|
+
const rows: string[] = [];
|
|
269
|
+
for (const field of fields) {
|
|
270
|
+
const line = statLine(field, source, hint);
|
|
271
|
+
if (line !== undefined) {
|
|
272
|
+
rows.push(line);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
// Unknown names stay skipped even when persistent: a typo must stay a
|
|
276
|
+
// reported-and-skipped row, not a placeholder that looks intentional.
|
|
277
|
+
if (!persist || !isStatField(field)) continue;
|
|
278
|
+
rows.push(formatRow(field, placeholder, labelWidth));
|
|
279
|
+
}
|
|
280
|
+
return rows;
|
|
281
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// Everything the host knows about the session on screen.
|
|
2
|
+
//
|
|
3
|
+
// One factory closes over the plugin `context` and returns the read-only
|
|
4
|
+
// lookups the rail needs: the session's messages, family tree, status,
|
|
5
|
+
// activity, shells, permissions, and derived figures (context occupancy,
|
|
6
|
+
// elapsed time, subagent totals, busy). Every lookup degrades to "no data"
|
|
7
|
+
// instead of throwing — the rail must never break because a read failed.
|
|
8
|
+
// Coercion of untrusted values happens through ./coerce.js.
|
|
9
|
+
|
|
10
|
+
import type { Plugin } from "@opencode/plugin/tui";
|
|
11
|
+
import { asCount, asRecord, asText, type SessionLike } from "./coerce.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The location the rail reads shell records and VCS from: the host's current
|
|
15
|
+
* location, or its default when no location is set. A host that cannot resolve
|
|
16
|
+
* a default degrades to "no location" rather than taking the rail down.
|
|
17
|
+
*/
|
|
18
|
+
export function locationOf(context: Plugin.Context) {
|
|
19
|
+
try {
|
|
20
|
+
return context.location ?? context.data.location.default();
|
|
21
|
+
} catch {
|
|
22
|
+
return context.location;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createSessionReads(context: Plugin.Context) {
|
|
27
|
+
/** How many trailing messages are inspected for tool parts. */
|
|
28
|
+
const RECENT_MESSAGES = 4;
|
|
29
|
+
|
|
30
|
+
// The host owns these caches; a failure to read one must never break the
|
|
31
|
+
// rail, so every lookup degrades to "no data" instead of throwing.
|
|
32
|
+
const messagesOf = (sessionID: string): readonly unknown[] => {
|
|
33
|
+
try {
|
|
34
|
+
return context.data.session.message.list(sessionID) ?? [];
|
|
35
|
+
} catch {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// A subagent session is already inside its parent's family total, and the
|
|
41
|
+
// host keys `family()` by the family ROOT — so asking from a child returns
|
|
42
|
+
// its ancestors and siblings as well. Merging that would report the whole
|
|
43
|
+
// tree as this conversation's own, under a label that promises the
|
|
44
|
+
// conversation plus *its* subagents. Only a root has a family beneath it.
|
|
45
|
+
const isFamilyRoot = (sessionID: string): boolean => {
|
|
46
|
+
try {
|
|
47
|
+
return context.data.session.root(sessionID) === sessionID;
|
|
48
|
+
} catch {
|
|
49
|
+
// A host without `root` keeps the previous behaviour rather than
|
|
50
|
+
// silently dropping everyone's subagent total.
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Subagent sessions are separate sessions, and the parent's `cost` does not
|
|
56
|
+
// include them, so a bare `cost` row understates a swarm. Sum the family.
|
|
57
|
+
const treeTotals = (sessionID: string, ownCost: unknown) => {
|
|
58
|
+
if (!isFamilyRoot(sessionID)) return undefined;
|
|
59
|
+
|
|
60
|
+
let ids: readonly string[];
|
|
61
|
+
try {
|
|
62
|
+
ids = context.data.session.family(sessionID) ?? [sessionID];
|
|
63
|
+
} catch {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
if (ids.length <= 1) return undefined;
|
|
67
|
+
|
|
68
|
+
let cost = asCount(ownCost) ?? 0;
|
|
69
|
+
let count = 0;
|
|
70
|
+
for (const id of ids) {
|
|
71
|
+
if (id === sessionID) continue;
|
|
72
|
+
const child = context.data.session.get(id) as SessionLike | undefined;
|
|
73
|
+
if (child === undefined) continue;
|
|
74
|
+
count += 1;
|
|
75
|
+
cost += asCount(child.cost) ?? 0;
|
|
76
|
+
}
|
|
77
|
+
return count === 0 ? undefined : { cost, count };
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// A message's own totals are per-request, so the last assistant message's
|
|
81
|
+
// prompt size *is* the current context occupancy — not a running total.
|
|
82
|
+
const contextUsage = (sessionID: string, model: unknown) => {
|
|
83
|
+
const messages = messagesOf(sessionID);
|
|
84
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
85
|
+
const tokens = asRecord(asRecord(messages[index])?.tokens);
|
|
86
|
+
const input = asCount(tokens?.input);
|
|
87
|
+
if (tokens === undefined || input === undefined) continue;
|
|
88
|
+
const cache = asRecord(tokens.cache);
|
|
89
|
+
const used = input + (asCount(cache?.read) ?? 0) + (asCount(cache?.write) ?? 0);
|
|
90
|
+
if (used === 0) return undefined;
|
|
91
|
+
return { used, limit: contextLimit(model) };
|
|
92
|
+
}
|
|
93
|
+
return undefined;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// The window lives at `limit.context` on the catalog entry, and two
|
|
97
|
+
// providers can expose the same model id with different windows — so the
|
|
98
|
+
// provider has to match too, or the gauge would read the wrong ceiling.
|
|
99
|
+
const contextLimit = (model: unknown): number | undefined => {
|
|
100
|
+
const ref = asRecord(model);
|
|
101
|
+
const id = ref?.id;
|
|
102
|
+
const providerID = ref?.providerID;
|
|
103
|
+
if (typeof id !== "string") return undefined;
|
|
104
|
+
try {
|
|
105
|
+
const location = context.location ?? context.data.location.default();
|
|
106
|
+
for (const entry of context.data.location.model.list(location) ?? []) {
|
|
107
|
+
const record = asRecord(entry);
|
|
108
|
+
if (record === undefined || record.id !== id) continue;
|
|
109
|
+
if (typeof providerID === "string" && record.providerID !== providerID) continue;
|
|
110
|
+
const limit = asRecord(record.limit);
|
|
111
|
+
return asCount(limit?.context) ?? asCount(record.context) ?? asCount(record.contextWindow);
|
|
112
|
+
}
|
|
113
|
+
} catch {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Measured against the clock rather than the session's last update, so the
|
|
120
|
+
// row keeps moving between events instead of freezing between turns.
|
|
121
|
+
const sessionElapsed = (session: SessionLike | undefined): number | undefined => {
|
|
122
|
+
const created = asCount(asRecord(session?.time)?.created);
|
|
123
|
+
if (created === undefined) return undefined;
|
|
124
|
+
const elapsed = Date.now() - created;
|
|
125
|
+
return elapsed <= 0 ? undefined : elapsed;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const statusOf = (sessionID: string): string | undefined => {
|
|
129
|
+
try {
|
|
130
|
+
return context.data.session.status(sessionID);
|
|
131
|
+
} catch {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// Recent tool parts, oldest first. Only the tail is inspected: a loop that is
|
|
137
|
+
// not currently happening is history, and a hang is by definition now.
|
|
138
|
+
const recentParts = (sessionID: string): readonly unknown[] => {
|
|
139
|
+
const parts: unknown[] = [];
|
|
140
|
+
for (const entry of messagesOf(sessionID).slice(-RECENT_MESSAGES)) {
|
|
141
|
+
const content = asRecord(entry)?.["content"];
|
|
142
|
+
if (Array.isArray(content)) parts.push(...content);
|
|
143
|
+
}
|
|
144
|
+
return parts;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// The host's own shell records, which outlive the tool call: a backgrounded
|
|
148
|
+
// command returns its result immediately, so its part settles while the
|
|
149
|
+
// process keeps running. This is the only signal that survives that.
|
|
150
|
+
//
|
|
151
|
+
// Shells are listed per LOCATION, not per session, so the session is matched
|
|
152
|
+
// on the record's own `metadata.sessionID` — the documented shape, rather
|
|
153
|
+
// than a session-scoped accessor the published API does not carry.
|
|
154
|
+
const shellsOf = (sessionID: string): readonly unknown[] => {
|
|
155
|
+
try {
|
|
156
|
+
const shells = context.data.shell.list(locationOf(context)) ?? [];
|
|
157
|
+
return shells.filter(
|
|
158
|
+
(entry) => asText(asRecord(asRecord(entry)?.["metadata"])?.["sessionID"]) === sessionID,
|
|
159
|
+
);
|
|
160
|
+
} catch {
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// The newest thing the host recorded for this session that is not a tool
|
|
166
|
+
// part, so the quiet-turn rule is not blind to a model that is streaming.
|
|
167
|
+
const lastActivity = (sessionID: string): number | undefined => {
|
|
168
|
+
const messages = messagesOf(sessionID);
|
|
169
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
170
|
+
const time = asRecord(asRecord(messages[index])?.["time"]);
|
|
171
|
+
const stamp =
|
|
172
|
+
asCount(time?.["completed"]) ?? asCount(time?.["streamed"]) ?? asCount(time?.["created"]);
|
|
173
|
+
if (stamp !== undefined) return stamp;
|
|
174
|
+
}
|
|
175
|
+
return undefined;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// What is waiting, not just how many. A bare count tells you to go and look;
|
|
179
|
+
// naming the request tells you whether it is worth looking at.
|
|
180
|
+
const permsOf = (
|
|
181
|
+
sessionID: string,
|
|
182
|
+
): { count: number; action?: string; resource?: string } | undefined => {
|
|
183
|
+
try {
|
|
184
|
+
const requests = context.data.session.permission.list(sessionID);
|
|
185
|
+
if (requests === undefined || requests.length === 0) return undefined;
|
|
186
|
+
const first = asRecord(requests[0]);
|
|
187
|
+
const action = typeof first?.["action"] === "string" ? first["action"] : undefined;
|
|
188
|
+
const resources = Array.isArray(first?.["resources"]) ? first["resources"] : [];
|
|
189
|
+
const resource = typeof resources[0] === "string" ? resources[0] : undefined;
|
|
190
|
+
return { count: requests.length, action, resource };
|
|
191
|
+
} catch {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// Anything genuinely working keeps the status glyph turning: this session
|
|
197
|
+
// (thinking, streaming, running tools), any subagent in its tree, or a shell
|
|
198
|
+
// command still running. Returns `undefined` when the host has not said
|
|
199
|
+
// either way, so the rail can omit the row rather than invent "idle".
|
|
200
|
+
const busy = (sessionID: string): boolean | undefined => {
|
|
201
|
+
const status = statusOf(sessionID);
|
|
202
|
+
if (status === "running") return true;
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
for (const id of context.data.session.family(sessionID) ?? []) {
|
|
206
|
+
if (id === sessionID) continue;
|
|
207
|
+
try {
|
|
208
|
+
if (context.data.session.status(id) === "running") return true;
|
|
209
|
+
} catch {
|
|
210
|
+
// One unreadable child must not hide a running sibling.
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
} catch {
|
|
214
|
+
// No subagent status on this host; the shell check below still applies.
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
try {
|
|
218
|
+
if (shellsOf(sessionID).some((entry) => asRecord(entry)?.["status"] === "running")) return true;
|
|
219
|
+
} catch {
|
|
220
|
+
// Unreadable shells just mean no shell signal.
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return status === undefined ? undefined : false;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
messagesOf,
|
|
228
|
+
isFamilyRoot,
|
|
229
|
+
treeTotals,
|
|
230
|
+
contextUsage,
|
|
231
|
+
sessionElapsed,
|
|
232
|
+
statusOf,
|
|
233
|
+
recentParts,
|
|
234
|
+
shellsOf,
|
|
235
|
+
lastActivity,
|
|
236
|
+
permsOf,
|
|
237
|
+
busy,
|
|
238
|
+
};
|
|
239
|
+
}
|