oc-flight-deck 0.6.1 → 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 +107 -214
- package/flight-deck.example.jsonc +208 -208
- package/flight-deck.schema.json +1 -1
- package/package.json +70 -70
- package/src/tui/caution.ts +14 -14
- package/src/tui/coerce.ts +25 -0
- package/src/tui/config.ts +12 -12
- 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
package/src/tui/stats.ts
CHANGED
|
@@ -4,637 +4,15 @@
|
|
|
4
4
|
// hands to the plugin. There is no network call, no server, and no storage
|
|
5
5
|
// write: this module turns a session snapshot into strings.
|
|
6
6
|
//
|
|
7
|
-
//
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export
|
|
19
|
-
/**
|
|
20
|
-
* Prompt tokens sent on the last request: input, cache reads, and cache
|
|
21
|
-
* writes. All three occupy the window, so all three count.
|
|
22
|
-
*/
|
|
23
|
-
readonly used?: unknown;
|
|
24
|
-
/** The model's context window, when the catalog knows it. */
|
|
25
|
-
readonly limit?: unknown;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Whole-project spend, across every session in this project. */
|
|
29
|
-
export interface StatProject {
|
|
30
|
-
readonly cost?: unknown;
|
|
31
|
-
readonly count?: unknown;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** The subset of a session snapshot the rail reads. All fields are untrusted. */
|
|
35
|
-
export interface StatSource {
|
|
36
|
-
/**
|
|
37
|
-
* The annunciator line, precomputed by the caller.
|
|
38
|
-
*
|
|
39
|
-
* A plain string on purpose: the rules that decide whether something is wrong
|
|
40
|
-
* live in ./caution.ts and need the clock and the session snapshot, not a
|
|
41
|
-
* formatting function. Severity is already encoded in the glyph.
|
|
42
|
-
*/
|
|
43
|
-
readonly caution?: unknown;
|
|
44
|
-
readonly agent?: unknown;
|
|
45
|
-
readonly model?: unknown;
|
|
46
|
-
readonly cost?: unknown;
|
|
47
|
-
readonly tokens?: unknown;
|
|
48
|
-
readonly branch?: unknown;
|
|
49
|
-
readonly tree?: unknown;
|
|
50
|
-
readonly context?: unknown;
|
|
51
|
-
readonly project?: unknown;
|
|
52
|
-
readonly status?: unknown;
|
|
53
|
-
/** True when anything is working: this session, a subagent, or a shell. */
|
|
54
|
-
readonly busy?: unknown;
|
|
55
|
-
readonly perms?: unknown;
|
|
56
|
-
/**
|
|
57
|
-
* Current session throughput. On a host that stamps its messages this is a
|
|
58
|
-
* trailing-window rate over the last minute, subagents summed in; on a host
|
|
59
|
-
* without message timestamps it falls back to the lifetime average. Either
|
|
60
|
-
* way it is derived from state the host already holds.
|
|
61
|
-
*/
|
|
62
|
-
readonly tps?: unknown;
|
|
63
|
-
readonly elapsedMs?: unknown;
|
|
64
|
-
readonly turns?: unknown;
|
|
65
|
-
/** Recent per-turn output sizes, oldest first, for the sparkline. */
|
|
66
|
-
readonly spark?: unknown;
|
|
67
|
-
/** Animation frame counter, advanced by the ticker. */
|
|
68
|
-
readonly frame?: unknown;
|
|
69
|
-
/** Harness status from oc-harness-guard's RPC, via the guard bridge. Untrusted. */
|
|
70
|
-
readonly guard?: unknown;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** Fields a user may name in `sidebar.rows`, in the order they are documented. */
|
|
74
|
-
export const STAT_FIELDS = [
|
|
75
|
-
"caution",
|
|
76
|
-
"status",
|
|
77
|
-
"agent",
|
|
78
|
-
"model",
|
|
79
|
-
"branch",
|
|
80
|
-
"cost",
|
|
81
|
-
"total",
|
|
82
|
-
"project",
|
|
83
|
-
"tokens",
|
|
84
|
-
"cache",
|
|
85
|
-
"context",
|
|
86
|
-
"perms",
|
|
87
|
-
"elapsed",
|
|
88
|
-
"tps",
|
|
89
|
-
"spark",
|
|
90
|
-
"reasoning",
|
|
91
|
-
"turns",
|
|
92
|
-
"guard",
|
|
93
|
-
] as const;
|
|
94
|
-
|
|
95
|
-
export type StatField = (typeof STAT_FIELDS)[number];
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Fields whose value only ever changes on a clock tick.
|
|
99
|
-
*
|
|
100
|
-
* The ticker exists to animate these. When none of them is on screen there is
|
|
101
|
-
* nothing to animate, so the plugin does not start a timer at all.
|
|
102
|
-
*
|
|
103
|
-
* `caution` belongs here for a stronger reason than the others: its whole job is
|
|
104
|
-
* noticing that time has passed without anything happening, so without a tick
|
|
105
|
-
* its thresholds could never be crossed on screen.
|
|
106
|
-
*/
|
|
107
|
-
export const ANIMATED_FIELDS = ["caution", "status", "elapsed"] as const;
|
|
108
|
-
|
|
109
|
-
export function isStatField(value: string): value is StatField {
|
|
110
|
-
return (STAT_FIELDS as readonly string[]).includes(value);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Row geometry, overridable per call.
|
|
115
|
-
*
|
|
116
|
-
* A structural type rather than an import of `FlightDeckConfig`: ./config.ts
|
|
117
|
-
* imports this module, so reaching back into it would create a cycle. The
|
|
118
|
-
* shapes are compatible, so the config passes straight through.
|
|
119
|
-
*/
|
|
120
|
-
export interface LayoutHint {
|
|
121
|
-
readonly labelWidth?: number;
|
|
122
|
-
readonly barWidth?: number;
|
|
123
|
-
readonly sparkWidth?: number;
|
|
124
|
-
/**
|
|
125
|
-
* True when the rail already draws a `total` row.
|
|
126
|
-
*
|
|
127
|
-
* The `cost` row merges the subagent total into itself only when nothing else
|
|
128
|
-
* on the rail already shows it — which makes "do I want one money row or two"
|
|
129
|
-
* a property of `sidebar.rows`, not a separate setting to find.
|
|
130
|
-
*/
|
|
131
|
-
readonly hasTotalRow?: boolean;
|
|
132
|
-
/**
|
|
133
|
-
* When true, `statRows` renders one row per named field even when the host
|
|
134
|
-
* has no data for it, using `placeholder` as the value. Default off when
|
|
135
|
-
* calling `statRows` directly; `sidebarLines` turns it on from
|
|
136
|
-
* `config.sidebar.persist`.
|
|
137
|
-
*/
|
|
138
|
-
readonly persist?: boolean;
|
|
139
|
-
/** Value shown for a row with no data when `persist` is on. Default `"—"`. */
|
|
140
|
-
readonly placeholder?: string;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** Placeholder value for a persistent row with no data yet. */
|
|
144
|
-
export const DEFAULT_PLACEHOLDER = "—";
|
|
145
|
-
|
|
146
|
-
const DEFAULT_LABEL_WIDTH = 10;
|
|
147
|
-
const DEFAULT_BAR_WIDTH = 10;
|
|
148
|
-
const DEFAULT_SPARK_WIDTH = 12;
|
|
149
|
-
|
|
150
|
-
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
151
|
-
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
152
|
-
? (value as Record<string, unknown>)
|
|
153
|
-
: undefined;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function asText(value: unknown): string | undefined {
|
|
157
|
-
if (typeof value !== "string") return undefined;
|
|
158
|
-
const text = value.trim();
|
|
159
|
-
return text.length === 0 ? undefined : text;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function asCount(value: unknown): number | undefined {
|
|
163
|
-
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* A guard count: a non-negative whole number, or an array counted by length.
|
|
168
|
-
*
|
|
169
|
-
* The RPC aggregates are numbers, but an array (findings, breaches) reads the
|
|
170
|
-
* same way — how many — so both count. Anything else is dropped to `undefined`
|
|
171
|
-
* so the caller can fall back to zero rather than print garbage.
|
|
172
|
-
*/
|
|
173
|
-
function asGuardCount(value: unknown): number | undefined {
|
|
174
|
-
if (typeof value === "number" && Number.isFinite(value) && value >= 0) return Math.floor(value);
|
|
175
|
-
if (Array.isArray(value)) return value.length;
|
|
176
|
-
return undefined;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function guardCountOrZero(...candidates: readonly unknown[]): number {
|
|
180
|
-
for (const candidate of candidates) {
|
|
181
|
-
const count = asGuardCount(candidate);
|
|
182
|
-
if (count !== undefined) return count;
|
|
183
|
-
}
|
|
184
|
-
return 0;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Short harness token for the `guard` row, or `undefined` when there is no
|
|
189
|
-
* usable data (the persist layer then renders the placeholder).
|
|
190
|
-
*
|
|
191
|
-
* A section counts as usable only with an explicit boolean `available`: a
|
|
192
|
-
* present section with a missing or garbage flag is dropped, not read as
|
|
193
|
-
* either `ok` or `unknown`. Any usable section reporting `available: false`
|
|
194
|
-
* reads as `unknown` — a failed or disabled source never renders a false `ok`.
|
|
195
|
-
* Otherwise the worst signal wins: breaches, then orphans, then findings.
|
|
196
|
-
* ASCII, short, no padding.
|
|
197
|
-
*/
|
|
198
|
-
function guardToken(value: unknown): string | undefined {
|
|
199
|
-
try {
|
|
200
|
-
const top = asRecord(value);
|
|
201
|
-
if (top === undefined) return undefined;
|
|
202
|
-
|
|
203
|
-
const air = asRecord(top.airworthiness);
|
|
204
|
-
const war = asRecord(top.warden);
|
|
205
|
-
const plan = asRecord(top.flightPlan);
|
|
206
|
-
|
|
207
|
-
const airUsable = air !== undefined && (air.available === true || air.available === false);
|
|
208
|
-
const warUsable = war !== undefined && (war.available === true || war.available === false);
|
|
209
|
-
const planUsable = plan !== undefined && (plan.available === true || plan.available === false);
|
|
210
|
-
if (!airUsable && !warUsable && !planUsable) return undefined;
|
|
211
|
-
|
|
212
|
-
if ((airUsable && air.available === false) || (warUsable && war.available === false) || (planUsable && plan.available === false)) {
|
|
213
|
-
return "unknown";
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const breaches = warUsable ? guardCountOrZero(war.breaches) : 0;
|
|
217
|
-
const orphans = warUsable ? guardCountOrZero(war.orphans) : 0;
|
|
218
|
-
const findings = airUsable ? guardCountOrZero(air.findings, air.counts) : 0;
|
|
219
|
-
|
|
220
|
-
// Abbreviated so a huge count cannot exceed the rail width; stays ASCII.
|
|
221
|
-
if (breaches > 0) return `${formatCount(breaches)} breach`;
|
|
222
|
-
if (orphans > 0) return `${formatCount(orphans)} orphan`;
|
|
223
|
-
if (findings > 0) return `${formatCount(findings)} finding`;
|
|
224
|
-
return "ok";
|
|
225
|
-
} catch {
|
|
226
|
-
return undefined;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/** `518k`, `29.2M`, `32M`, `940` — short enough for a narrow rail, precise enough to read. */
|
|
231
|
-
export function formatCount(value: number): string {
|
|
232
|
-
if (value < 1_000) return String(Math.round(value));
|
|
233
|
-
// Round *before* choosing the unit. Rounding only inside the `k` branch made
|
|
234
|
-
// anything from 999,500 to 999,999 print as `1000k` — a number with two
|
|
235
|
-
// magnitudes in it, and one an agent reads as a different size entirely.
|
|
236
|
-
const thousands = Math.round(value / 1_000);
|
|
237
|
-
if (thousands < 1_000) return `${thousands}k`;
|
|
238
|
-
const millions = (value / 1_000_000).toFixed(1);
|
|
239
|
-
return `${millions.endsWith(".0") ? millions.slice(0, -2) : millions}M`;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/** Sub-dollar sessions keep a third decimal so a cheap run never reads `$0.00`. */
|
|
243
|
-
export function formatCost(value: number): string {
|
|
244
|
-
return `$${value.toFixed(value < 1 ? 3 : 2)}`;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Overall session throughput: output tokens over the session's lifetime.
|
|
249
|
-
*
|
|
250
|
-
* Unlike a per-turn rate, this is the whole conversation's average, so it
|
|
251
|
-
* includes every subagent and every pause. A just-started session is floored
|
|
252
|
-
* at one second so it cannot divide by a sliver of time and flash an absurd
|
|
253
|
-
* rate. Returns `undefined` when there is nothing to divide.
|
|
254
|
-
*/
|
|
255
|
-
export function sessionThroughput(outputTokens: unknown, elapsedMs: unknown): number | undefined {
|
|
256
|
-
const output = asCount(outputTokens);
|
|
257
|
-
const elapsed = asCount(elapsedMs);
|
|
258
|
-
if (output === undefined || output <= 0) return undefined;
|
|
259
|
-
if (elapsed === undefined || elapsed <= 0) return undefined;
|
|
260
|
-
return output / (Math.max(elapsed, 1_000) / 1_000);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/** Length of the trailing window, in milliseconds, that `windowedThroughput` measures. */
|
|
264
|
-
export const TPS_WINDOW_MS = 60_000;
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* One assistant turn's contribution to a throughput window.
|
|
268
|
-
*
|
|
269
|
-
* Both fields are untrusted: `tokens` is the turn's output count and `at` is the
|
|
270
|
-
* timestamp it finished at. Anything unusable is skipped rather than guessed at.
|
|
271
|
-
*/
|
|
272
|
-
export interface ThroughputSample {
|
|
273
|
-
readonly tokens?: unknown;
|
|
274
|
-
readonly at?: unknown;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* A trailing-window throughput rate: output tokens per second over the last
|
|
279
|
-
* `windowMs`, measured from `nowMs`.
|
|
280
|
-
*
|
|
281
|
-
* The denominator is how much of the window the session has actually lived, so
|
|
282
|
-
* a just-started session ramps up against its real age instead of being divided
|
|
283
|
-
* by a full minute it has not run — floored at one second, the same floor the
|
|
284
|
-
* lifetime average uses, so a sliver of time cannot flash an absurd rate. Once
|
|
285
|
-
* the session is older than the window, it settles into a clean rolling minute.
|
|
286
|
-
*
|
|
287
|
-
* Returns `undefined` — the caller hides the row — unless at least one sample
|
|
288
|
-
* carries a usable timestamp inside the window and a positive output count. An
|
|
289
|
-
* idle session therefore reports no rate rather than a decaying one. The window
|
|
290
|
-
* is inclusive at both ends (`start <= at <= now`) so a boundary sample counts.
|
|
291
|
-
*
|
|
292
|
-
* Pure and dependency-free: no clock, no I/O, no rendering.
|
|
293
|
-
*/
|
|
294
|
-
export function windowedThroughput(
|
|
295
|
-
samples: readonly ThroughputSample[],
|
|
296
|
-
nowMs: number,
|
|
297
|
-
windowMs: number,
|
|
298
|
-
sessionCreatedMs: number,
|
|
299
|
-
): number | undefined {
|
|
300
|
-
if (!Number.isFinite(nowMs)) return undefined;
|
|
301
|
-
if (!Number.isFinite(windowMs) || windowMs <= 0) return undefined;
|
|
302
|
-
|
|
303
|
-
// An unusable creation time cannot tell us how long the session has lived, so
|
|
304
|
-
// the whole window is assumed available rather than dropping the rate.
|
|
305
|
-
const created = Number.isFinite(sessionCreatedMs) ? sessionCreatedMs : undefined;
|
|
306
|
-
const alive = created === undefined ? windowMs : nowMs - created;
|
|
307
|
-
const denominatorMs = Math.max(1_000, Math.min(windowMs, alive));
|
|
308
|
-
|
|
309
|
-
const start = nowMs - windowMs;
|
|
310
|
-
let output = 0;
|
|
311
|
-
let sawOne = false;
|
|
312
|
-
for (const sample of samples) {
|
|
313
|
-
const at = asCount(sample.at);
|
|
314
|
-
if (at === undefined || at < start || at > nowMs) continue;
|
|
315
|
-
const tokens = asCount(sample.tokens);
|
|
316
|
-
if (tokens === undefined || tokens <= 0) continue;
|
|
317
|
-
output += tokens;
|
|
318
|
-
sawOne = true;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (!sawOne) return undefined;
|
|
322
|
-
return output / (denominatorMs / 1_000);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/** `45s`, `14m`, `2h 14m`. */
|
|
326
|
-
export function formatDuration(ms: number): string {
|
|
327
|
-
const seconds = Math.floor(ms / 1_000);
|
|
328
|
-
if (seconds < 60) return `${seconds}s`;
|
|
329
|
-
const minutes = Math.floor(seconds / 60);
|
|
330
|
-
if (minutes < 60) return `${minutes}m`;
|
|
331
|
-
const hours = Math.floor(minutes / 60);
|
|
332
|
-
const rest = minutes % 60;
|
|
333
|
-
return rest === 0 ? `${hours}h` : `${hours}h ${rest}m`;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/** A ten-cell gauge, so a percentage is readable at a glance rather than parsed. */
|
|
337
|
-
export function fuelBar(ratio: number, width: number = DEFAULT_BAR_WIDTH): string {
|
|
338
|
-
const cells = Math.max(1, Math.floor(width));
|
|
339
|
-
const filled = Math.max(0, Math.min(cells, Math.round(ratio * cells)));
|
|
340
|
-
return `${"█".repeat(filled)}${"░".repeat(cells - filled)}`;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
const SPARK_LEVELS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] as const;
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Shorten a value for a narrow rail.
|
|
347
|
-
*
|
|
348
|
-
* The sidebar is roughly thirty-odd columns wide and the label already costs
|
|
349
|
-
* ten, so a resource path has to be cut. The ellipsis is deliberate: a silently
|
|
350
|
-
* truncated path reads as a complete one.
|
|
351
|
-
*/
|
|
352
|
-
export function clip(text: string, max: number): string {
|
|
353
|
-
if (text.length <= max) return text;
|
|
354
|
-
const cut = text.slice(0, Math.max(1, max - 1));
|
|
355
|
-
// Never cut a surrogate pair in half: the orphaned half renders as a
|
|
356
|
-
// replacement character, which reads as corruption rather than as a shortened
|
|
357
|
-
// path. Losing one more column is the cheaper mistake.
|
|
358
|
-
const safe = /[\uD800-\uDBFF]$/.test(cut) ? cut.slice(0, -1) : cut;
|
|
359
|
-
return `${safe}…`;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/** Braille frames, advanced by the ticker, shown only while the session runs. */
|
|
363
|
-
const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] as const;
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* Flatten control characters in anything about to be drawn.
|
|
367
|
-
*
|
|
368
|
-
* A rail is one line per row, so a newline or an escape sequence in a
|
|
369
|
-
* host-supplied string does not get seen — it moves the cursor. Config text is
|
|
370
|
-
* already flattened in ./config.ts, but a tool name, a permission resource or a
|
|
371
|
-
* branch name arrives straight from the host, so every row value passes through
|
|
372
|
-
* here on its way out. `replace` is unconditional rather than `test` + `replace`:
|
|
373
|
-
* a global regex used with `test` carries `lastIndex` between calls.
|
|
374
|
-
*/
|
|
375
|
-
const CONTROL_CHARS = /[\u0000-\u001F\u007F-\u009F]/g;
|
|
376
|
-
|
|
377
|
-
function plain(value: string): string {
|
|
378
|
-
return value.replace(CONTROL_CHARS, " ");
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* One padded rail row: the label in its column, at least one space, then the
|
|
383
|
-
* flattened value. Shared by live rows and persistent placeholders so both
|
|
384
|
-
* keep the same column.
|
|
385
|
-
*/
|
|
386
|
-
function formatRow(label: string, value: string, labelWidth: number): string {
|
|
387
|
-
return `${label.padEnd(labelWidth)}${label.length >= labelWidth ? " " : ""}${plain(value)}`;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* Recent turn sizes as a shape.
|
|
392
|
-
*
|
|
393
|
-
* Scaled against the largest value in the window rather than an absolute scale,
|
|
394
|
-
* so the sparkline stays readable whether turns are tiny or enormous.
|
|
395
|
-
*/
|
|
396
|
-
export function sparkline(values: readonly number[]): string {
|
|
397
|
-
if (values.length === 0) return "";
|
|
398
|
-
const max = Math.max(...values);
|
|
399
|
-
if (!Number.isFinite(max) || max <= 0) return SPARK_LEVELS[0].repeat(values.length);
|
|
400
|
-
return values
|
|
401
|
-
.map((value) => {
|
|
402
|
-
const level = Math.floor((Math.max(0, value) / max) * (SPARK_LEVELS.length - 1));
|
|
403
|
-
return SPARK_LEVELS[Math.min(SPARK_LEVELS.length - 1, level)];
|
|
404
|
-
})
|
|
405
|
-
.join("");
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
/**
|
|
409
|
-
* Render one named field, or `undefined` when the host has not supplied it yet.
|
|
410
|
-
*
|
|
411
|
-
* Returning `undefined` rather than a placeholder is deliberate: a rail that
|
|
412
|
-
* shows `branch unknown` before the repo loads looks broken, while a rail
|
|
413
|
-
* that simply grows a row when the data arrives looks alive.
|
|
414
|
-
*/
|
|
415
|
-
export function statLine(
|
|
416
|
-
field: string,
|
|
417
|
-
source: StatSource,
|
|
418
|
-
layout: LayoutHint = {},
|
|
419
|
-
): string | undefined {
|
|
420
|
-
// Bound here rather than at module scope so a config file can change the
|
|
421
|
-
// column width without every call site having to know about it.
|
|
422
|
-
const labelWidth = layout.labelWidth ?? DEFAULT_LABEL_WIDTH;
|
|
423
|
-
const barWidth = layout.barWidth ?? DEFAULT_BAR_WIDTH;
|
|
424
|
-
const sparkWidth = layout.sparkWidth ?? DEFAULT_SPARK_WIDTH;
|
|
425
|
-
// At least one space, always.
|
|
426
|
-
//
|
|
427
|
-
// `padEnd` returns the label unchanged when it is already wider than the
|
|
428
|
-
// column, so a label longer than `labelWidth` was glued straight onto its
|
|
429
|
-
// value: at labelWidth 8 the `reasoning` row rendered as "reasoning153k".
|
|
430
|
-
// The label is nine characters, which made this reachable from a config file.
|
|
431
|
-
const row = (label: string, value: string): string => formatRow(label, value, labelWidth);
|
|
432
|
-
|
|
433
|
-
switch (field) {
|
|
434
|
-
case "caution": {
|
|
435
|
-
// Rendered only when the caller found something to say, which is what
|
|
436
|
-
// keeps a healthy session visually identical to one without the
|
|
437
|
-
// annunciator at all. The string already carries its glyph; nothing here
|
|
438
|
-
// knows or cares about severity.
|
|
439
|
-
const text = asText(source.caution);
|
|
440
|
-
return text === undefined ? undefined : row("caution", text);
|
|
441
|
-
}
|
|
442
|
-
case "status": {
|
|
443
|
-
const status = asText(source.status);
|
|
444
|
-
if (status === undefined && source.busy === undefined) return undefined;
|
|
445
|
-
// The spinner lives here rather than in a row of its own: an animated
|
|
446
|
-
// glyph beside "running" says the same thing without spending a line.
|
|
447
|
-
//
|
|
448
|
-
// `busy` folds in work the session's own status misses — a subagent
|
|
449
|
-
// running in its own session, or a shell command still going — so the
|
|
450
|
-
// glyph only stops when there is genuinely nothing happening. A host that
|
|
451
|
-
// does not report it falls back to the session's own status.
|
|
452
|
-
if (source.busy !== true && status !== "running") return row("status", "○ idle");
|
|
453
|
-
// Floored at the boundary: a fractional frame would index the frame list
|
|
454
|
-
// with a non-integer and render `undefined`.
|
|
455
|
-
const frame = Math.floor(asCount(source.frame) ?? 0);
|
|
456
|
-
return row("status", `${SPINNER[frame % SPINNER.length]} running`);
|
|
457
|
-
}
|
|
458
|
-
case "agent": {
|
|
459
|
-
const agent = asText(source.agent);
|
|
460
|
-
return agent === undefined ? undefined : row("agent", agent);
|
|
461
|
-
}
|
|
462
|
-
case "model": {
|
|
463
|
-
const model = asRecord(source.model);
|
|
464
|
-
const id = asText(model?.id);
|
|
465
|
-
if (id === undefined) return undefined;
|
|
466
|
-
const variant = asText(model?.variant);
|
|
467
|
-
return row("model", variant === undefined ? id : `${id} · ${variant}`);
|
|
468
|
-
}
|
|
469
|
-
case "branch": {
|
|
470
|
-
const branch = asText(source.branch);
|
|
471
|
-
return branch === undefined ? undefined : row("branch", branch);
|
|
472
|
-
}
|
|
473
|
-
case "cost": {
|
|
474
|
-
const cost = asCount(source.cost);
|
|
475
|
-
const tree = asRecord(source.tree);
|
|
476
|
-
const treeCost = asCount(tree?.cost);
|
|
477
|
-
const count = asCount(tree?.count);
|
|
478
|
-
const family = count !== undefined && count > 0 ? count : 0;
|
|
479
|
-
|
|
480
|
-
// Merge only when subagents actually ran, and only when the rail is not
|
|
481
|
-
// already showing a `total` row. Two rows holding a number and its own
|
|
482
|
-
// superset was the thing worth fixing; a setting to choose between them
|
|
483
|
-
// would just be that duplication with extra steps.
|
|
484
|
-
const merge =
|
|
485
|
-
layout.hasTotalRow !== true &&
|
|
486
|
-
cost !== undefined &&
|
|
487
|
-
treeCost !== undefined &&
|
|
488
|
-
family > 0 &&
|
|
489
|
-
treeCost > cost;
|
|
490
|
-
|
|
491
|
-
if (!merge) {
|
|
492
|
-
const value = layout.hasTotalRow === true ? (cost ?? treeCost) : (treeCost ?? cost);
|
|
493
|
-
return value === undefined ? undefined : row("cost", formatCost(value));
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
// The delta (`+$0.020`) used to be here and pushed the row to 40 columns,
|
|
497
|
-
// wide enough to wrap in a normal sidebar. It was real information that
|
|
498
|
-
// did not earn its width: the count already explains why the figure is
|
|
499
|
-
// higher, and this form is the same width as the old `total` row.
|
|
500
|
-
const plural = family === 1 ? "subagent" : "subagents";
|
|
501
|
-
return row("cost", `${formatCost(treeCost as number)} · ${family} ${plural}`);
|
|
502
|
-
}
|
|
503
|
-
case "total": {
|
|
504
|
-
// Only worth a row once a subagent has actually run: subagent sessions are
|
|
505
|
-
// separate, so the parent's `cost` alone understates what was spent.
|
|
506
|
-
const tree = asRecord(source.tree);
|
|
507
|
-
const count = asCount(tree?.count);
|
|
508
|
-
const cost = asCount(tree?.cost);
|
|
509
|
-
if (cost === undefined || count === undefined || count === 0) return undefined;
|
|
510
|
-
const plural = count === 1 ? "subagent" : "subagents";
|
|
511
|
-
return row("total", `${formatCost(cost)} · ${count} ${plural}`);
|
|
512
|
-
}
|
|
513
|
-
case "project": {
|
|
514
|
-
const project = asRecord(source.project);
|
|
515
|
-
const cost = asCount(project?.cost);
|
|
516
|
-
if (cost === undefined) return undefined;
|
|
517
|
-
const count = asCount(project?.count);
|
|
518
|
-
return row("project", `${formatCost(cost)}${count !== undefined && count > 1 ? ` · ${count} sessions` : ""}`);
|
|
519
|
-
}
|
|
520
|
-
case "tokens": {
|
|
521
|
-
const tokens = asRecord(source.tokens);
|
|
522
|
-
const input = asCount(tokens?.input);
|
|
523
|
-
const output = asCount(tokens?.output);
|
|
524
|
-
if (input === undefined && output === undefined) return undefined;
|
|
525
|
-
return row("tokens", `${formatCount(input ?? 0)} in · ${formatCount(output ?? 0)} out`);
|
|
526
|
-
}
|
|
527
|
-
case "cache": {
|
|
528
|
-
const tokens = asRecord(source.tokens);
|
|
529
|
-
const cache = asRecord(tokens?.cache);
|
|
530
|
-
const read = asCount(cache?.read);
|
|
531
|
-
if (read === undefined || read === 0) return undefined;
|
|
532
|
-
const input = asCount(tokens?.input);
|
|
533
|
-
// Hit ratio is the whole point: it explains a cheap bill on a huge token
|
|
534
|
-
// count, and it is the first thing to break when caching stops working.
|
|
535
|
-
const total = input === undefined ? undefined : read + input;
|
|
536
|
-
const value =
|
|
537
|
-
total === undefined || total === 0
|
|
538
|
-
? `${formatCount(read)} read`
|
|
539
|
-
: `${Math.round((read / total) * 100)}% hit · ${formatCount(read)} read`;
|
|
540
|
-
return row("cache", value);
|
|
541
|
-
}
|
|
542
|
-
case "context": {
|
|
543
|
-
const context = asRecord(source.context);
|
|
544
|
-
const used = asCount(context?.used);
|
|
545
|
-
if (used === undefined || used === 0) return undefined;
|
|
546
|
-
const limit = asCount(context?.limit);
|
|
547
|
-
if (limit === undefined || limit === 0) return row("context", `${formatCount(used)} used`);
|
|
548
|
-
const ratio = used / limit;
|
|
549
|
-
// Show the percentage whenever the host reports it, but never a bar that
|
|
550
|
-
// reads as more than full.
|
|
551
|
-
return row("context", `${fuelBar(ratio, barWidth)} ${Math.round(Math.min(1, ratio) * 100)}%`);
|
|
552
|
-
}
|
|
553
|
-
case "perms": {
|
|
554
|
-
const perms = asRecord(source.perms);
|
|
555
|
-
const count = asCount(perms?.["count"]);
|
|
556
|
-
if (count === undefined || count === 0) return undefined;
|
|
557
|
-
const action = asText(perms?.["action"]);
|
|
558
|
-
const resource = asText(perms?.["resource"]);
|
|
559
|
-
|
|
560
|
-
// One request: name it, because that is the decision you are being asked
|
|
561
|
-
// to make. Several: the count leads, because the first request in the
|
|
562
|
-
// queue is not necessarily the one you are about to be shown.
|
|
563
|
-
if (count > 1) {
|
|
564
|
-
return row("perms", action === undefined ? `${count} waiting` : `${count} waiting · ${action}`);
|
|
565
|
-
}
|
|
566
|
-
if (action === undefined) return row("perms", "1 waiting");
|
|
567
|
-
// Clipped hard: a row that wraps costs the reader more than a row that
|
|
568
|
-
// shortens a path, and there is no way to ask the host how wide it is.
|
|
569
|
-
return row("perms", resource === undefined ? action : `${action} · ${clip(resource, 18)}`);
|
|
570
|
-
}
|
|
571
|
-
case "elapsed": {
|
|
572
|
-
const ms = asCount(source.elapsedMs);
|
|
573
|
-
if (ms === undefined || ms <= 0) return undefined;
|
|
574
|
-
return row("elapsed", formatDuration(ms));
|
|
575
|
-
}
|
|
576
|
-
case "tps": {
|
|
577
|
-
const tps = asCount(source.tps);
|
|
578
|
-
if (tps === undefined || tps === 0) return undefined;
|
|
579
|
-
return row("tps", `${Math.round(tps)} tok/s`);
|
|
580
|
-
}
|
|
581
|
-
case "spark": {
|
|
582
|
-
const values = Array.isArray(source.spark) ? source.spark.map((value) => asCount(value) ?? 0) : [];
|
|
583
|
-
// The newest samples are the interesting ones, so a narrowed window keeps
|
|
584
|
-
// the recent shape rather than the oldest.
|
|
585
|
-
const windowed = values.slice(-Math.max(2, Math.floor(sparkWidth)));
|
|
586
|
-
if (windowed.length < 2) return undefined;
|
|
587
|
-
return row("spark", sparkline(windowed));
|
|
588
|
-
}
|
|
589
|
-
case "reasoning": {
|
|
590
|
-
const reasoning = asCount(asRecord(source.tokens)?.reasoning);
|
|
591
|
-
if (reasoning === undefined || reasoning === 0) return undefined;
|
|
592
|
-
return row("reasoning", formatCount(reasoning));
|
|
593
|
-
}
|
|
594
|
-
case "turns": {
|
|
595
|
-
const turns = asCount(source.turns);
|
|
596
|
-
if (turns === undefined || turns === 0) return undefined;
|
|
597
|
-
return row("turns", String(turns));
|
|
598
|
-
}
|
|
599
|
-
case "guard": {
|
|
600
|
-
const token = guardToken(source.guard);
|
|
601
|
-
return token === undefined ? undefined : row("guard", token);
|
|
602
|
-
}
|
|
603
|
-
default:
|
|
604
|
-
return undefined;
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
/** Render every named field, in the given order.
|
|
609
|
-
*
|
|
610
|
-
* By default only fields with data render; with `layout.persist` every known
|
|
611
|
-
* field renders exactly one row, using `layout.placeholder` (default `"—"`)
|
|
612
|
-
* for the value when the host has nothing to show yet. `statLine` keeps
|
|
613
|
-
* returning `string | undefined` — persistence lives here, not there.
|
|
614
|
-
*/
|
|
615
|
-
export function statRows(
|
|
616
|
-
fields: readonly string[],
|
|
617
|
-
source: StatSource,
|
|
618
|
-
layout: LayoutHint = {},
|
|
619
|
-
): readonly string[] {
|
|
620
|
-
// The rows list decides whether `cost` merges the subagent total into itself.
|
|
621
|
-
// Passing that down means the choice lives in `sidebar.rows`, where someone
|
|
622
|
-
// is already deciding what the rail shows.
|
|
623
|
-
const hint: LayoutHint = { ...layout, hasTotalRow: fields.includes("total") };
|
|
624
|
-
const persist = hint.persist === true;
|
|
625
|
-
const placeholder = hint.placeholder ?? DEFAULT_PLACEHOLDER;
|
|
626
|
-
const labelWidth = hint.labelWidth ?? DEFAULT_LABEL_WIDTH;
|
|
627
|
-
const rows: string[] = [];
|
|
628
|
-
for (const field of fields) {
|
|
629
|
-
const line = statLine(field, source, hint);
|
|
630
|
-
if (line !== undefined) {
|
|
631
|
-
rows.push(line);
|
|
632
|
-
continue;
|
|
633
|
-
}
|
|
634
|
-
// Unknown names stay skipped even when persistent: a typo must stay a
|
|
635
|
-
// reported-and-skipped row, not a placeholder that looks intentional.
|
|
636
|
-
if (!persist || !isStatField(field)) continue;
|
|
637
|
-
rows.push(formatRow(field, placeholder, labelWidth));
|
|
638
|
-
}
|
|
639
|
-
return rows;
|
|
640
|
-
}
|
|
7
|
+
// The implementation is split by responsibility: ./stat-fields.js holds the
|
|
8
|
+
// vocabulary, ./coerce.js the coercion guards, ./format.js the short strings
|
|
9
|
+
// and glyph strips, ./guard-tokens.js the guard token, ./throughput.js the
|
|
10
|
+
// rates, and ./rows.js the row rendering. This module re-exports the same
|
|
11
|
+
// public surface it has always had.
|
|
12
|
+
|
|
13
|
+
export { clip, formatCost, formatCount, formatDuration, fuelBar, sparkline } from "./format.js";
|
|
14
|
+
export { statLine, statRows } from "./rows.js";
|
|
15
|
+
export { ANIMATED_FIELDS, DEFAULT_PLACEHOLDER, STAT_FIELDS, isStatField } from "./stat-fields.js";
|
|
16
|
+
export type { StatSource } from "./stat-fields.js";
|
|
17
|
+
export { sessionThroughput, TPS_WINDOW_MS, windowedThroughput } from "./throughput.js";
|
|
18
|
+
export type { ThroughputSample } from "./throughput.js";
|