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
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// Session throughput, measured from two sources chosen by host capability and
|
|
2
|
+
// never by the moment. A host that stamps its messages gets a trailing-window
|
|
3
|
+
// rate — output tokens over the last minute, subagents summed in — so the
|
|
4
|
+
// figure reflects current speed rather than a whole-conversation average. A
|
|
5
|
+
// host proven to expose no message timestamps uses the lifetime average.
|
|
6
|
+
//
|
|
7
|
+
// The capability is learned once and remembered for the process. `undefined`
|
|
8
|
+
// means "not yet known": a read that failed, or carried no assistant turns,
|
|
9
|
+
// tells us nothing and must not choose a metric. Once known it is sticky in
|
|
10
|
+
// BOTH directions, so a transient `message.list` failure can never flip a
|
|
11
|
+
// stamped host back to the lifetime average (nor an unstamped host off it) —
|
|
12
|
+
// a failure just hides the row. One value for the whole host, deliberately
|
|
13
|
+
// not keyed by session.
|
|
14
|
+
//
|
|
15
|
+
// The rail re-runs on the fast tick, and walking every family message for
|
|
16
|
+
// every session would repeat the same work fifty times a second, so the
|
|
17
|
+
// result is held briefly. `undefined` is cached too: an idle window must not
|
|
18
|
+
// be re-derived on each tick either.
|
|
19
|
+
//
|
|
20
|
+
// One factory closes over the plugin `context`; `isFamilyRoot` comes from
|
|
21
|
+
// ./session-reads.js so a child session keeps its own figure while a root
|
|
22
|
+
// sums its tree. The math itself lives in ./throughput.js (pure) and
|
|
23
|
+
// ./stats.js.
|
|
24
|
+
|
|
25
|
+
import type { Plugin } from "@opencode/plugin/tui";
|
|
26
|
+
import { asCount, asRecord, type SessionLike } from "./coerce.js";
|
|
27
|
+
import { sessionThroughput, TPS_WINDOW_MS, windowedThroughput, type ThroughputSample } from "./stats.js";
|
|
28
|
+
|
|
29
|
+
/** What one session's message list contributes to a throughput window. */
|
|
30
|
+
interface TpsScan {
|
|
31
|
+
/** False only when the host refused to list the session's messages. */
|
|
32
|
+
readonly ok: boolean;
|
|
33
|
+
/** Assistant messages seen, whether or not they carried a timestamp. */
|
|
34
|
+
readonly assistant: number;
|
|
35
|
+
/** Samples that carried a usable timestamp. */
|
|
36
|
+
readonly samples: readonly ThroughputSample[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createTpsReader(
|
|
40
|
+
context: Plugin.Context,
|
|
41
|
+
isFamilyRoot: (sessionID: string) => boolean,
|
|
42
|
+
) {
|
|
43
|
+
const TPS_CACHE_MS = 1_000;
|
|
44
|
+
let hostStamps: boolean | undefined;
|
|
45
|
+
let tpsCache:
|
|
46
|
+
| { readonly sessionID: string; readonly at: number; readonly value: number | undefined }
|
|
47
|
+
| undefined;
|
|
48
|
+
|
|
49
|
+
const scanMessages = (id: string): TpsScan => {
|
|
50
|
+
let messages: readonly unknown[];
|
|
51
|
+
try {
|
|
52
|
+
messages = context.data.session.message.list(id) ?? [];
|
|
53
|
+
} catch {
|
|
54
|
+
// A refused read is not "this host has no messages".
|
|
55
|
+
return { ok: false, assistant: 0, samples: [] };
|
|
56
|
+
}
|
|
57
|
+
const samples: ThroughputSample[] = [];
|
|
58
|
+
let assistant = 0;
|
|
59
|
+
for (const entry of messages) {
|
|
60
|
+
const message = asRecord(entry);
|
|
61
|
+
if (message?.["type"] !== "assistant") continue;
|
|
62
|
+
assistant += 1;
|
|
63
|
+
const stamp = asRecord(message["time"]);
|
|
64
|
+
const at =
|
|
65
|
+
asCount(stamp?.["completed"]) ?? asCount(stamp?.["streamed"]) ?? asCount(stamp?.["created"]);
|
|
66
|
+
// "Stamped" depends only on the timestamp, never on the output count: a
|
|
67
|
+
// batch of zero-output assistant turns still proves the host stamps.
|
|
68
|
+
if (at === undefined) continue;
|
|
69
|
+
samples.push({ tokens: asRecord(message["tokens"])?.["output"], at });
|
|
70
|
+
}
|
|
71
|
+
return { ok: true, assistant, samples };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// The lifetime average, the metric of last resort for a host that exposes no
|
|
75
|
+
// per-message timestamps. Measured from the session's own clock
|
|
76
|
+
// (`time.updated`, falling back to now) so a finished session settles on a
|
|
77
|
+
// stable figure instead of decaying as it sits on screen.
|
|
78
|
+
const lifetimeTps = (
|
|
79
|
+
sessionID: string,
|
|
80
|
+
session: SessionLike | undefined,
|
|
81
|
+
created: number,
|
|
82
|
+
time: Record<string, unknown> | undefined,
|
|
83
|
+
): number | undefined => {
|
|
84
|
+
const updated = asCount(time?.["updated"]);
|
|
85
|
+
const end = updated === undefined ? Date.now() : Math.min(Date.now(), updated);
|
|
86
|
+
const elapsed = end - created;
|
|
87
|
+
if (elapsed <= 0) return undefined;
|
|
88
|
+
|
|
89
|
+
const own = asCount(asRecord(session?.tokens)?.["output"]);
|
|
90
|
+
let output = own;
|
|
91
|
+
// Only a family root owns the tree beneath it; a child asking `family()`
|
|
92
|
+
// gets its ancestors and siblings too, so it keeps its own figure.
|
|
93
|
+
if (isFamilyRoot(sessionID)) {
|
|
94
|
+
try {
|
|
95
|
+
const ids = context.data.session.family(sessionID) ?? [];
|
|
96
|
+
if (ids.length > 1) {
|
|
97
|
+
let total = 0;
|
|
98
|
+
let sawOne = false;
|
|
99
|
+
for (const id of ids) {
|
|
100
|
+
const member = context.data.session.get(id) as SessionLike | undefined;
|
|
101
|
+
const out = asCount(asRecord(member?.tokens)?.["output"]);
|
|
102
|
+
if (out === undefined) continue;
|
|
103
|
+
total += out;
|
|
104
|
+
sawOne = true;
|
|
105
|
+
}
|
|
106
|
+
if (sawOne) output = total;
|
|
107
|
+
}
|
|
108
|
+
} catch {
|
|
109
|
+
// No family on this host: the session's own tokens still stand.
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return sessionThroughput(output, elapsed);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const computeTps = (sessionID: string, session: SessionLike | undefined): number | undefined => {
|
|
116
|
+
const time = asRecord(session?.time);
|
|
117
|
+
const created = asCount(time?.["created"]);
|
|
118
|
+
if (created === undefined) return undefined;
|
|
119
|
+
|
|
120
|
+
// A host proven unstamped needs no message read at all: its metric is the
|
|
121
|
+
// lifetime average whether or not the read would have succeeded.
|
|
122
|
+
if (hostStamps === false) return lifetimeTps(sessionID, session, created, time);
|
|
123
|
+
|
|
124
|
+
// Timestamped assistant output this session has produced, plus — only from
|
|
125
|
+
// a family root — every subagent session's. A child asking `family()` gets
|
|
126
|
+
// its ancestors and siblings back, so it keeps its own samples.
|
|
127
|
+
const scans: TpsScan[] = [scanMessages(sessionID)];
|
|
128
|
+
if (isFamilyRoot(sessionID)) {
|
|
129
|
+
try {
|
|
130
|
+
for (const id of context.data.session.family(sessionID) ?? []) {
|
|
131
|
+
if (id !== sessionID) scans.push(scanMessages(id));
|
|
132
|
+
}
|
|
133
|
+
} catch {
|
|
134
|
+
// No family on this host: the session's own samples still stand.
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const samples: ThroughputSample[] = [];
|
|
139
|
+
let assistant = 0;
|
|
140
|
+
let readOk = false;
|
|
141
|
+
for (const scan of scans) {
|
|
142
|
+
samples.push(...scan.samples);
|
|
143
|
+
assistant += scan.assistant;
|
|
144
|
+
readOk = readOk || scan.ok;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (hostStamps === undefined) {
|
|
148
|
+
// A stamped assistant turn proves the host stamps. Assistant turns with
|
|
149
|
+
// no timestamp prove it does not. Neither, and the capability stays
|
|
150
|
+
// unknown so the row hides rather than guessing a metric.
|
|
151
|
+
if (samples.length > 0) hostStamps = true;
|
|
152
|
+
else if (readOk && assistant > 0) hostStamps = false;
|
|
153
|
+
else return undefined;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (hostStamps === true) {
|
|
157
|
+
return windowedThroughput(samples, Date.now(), TPS_WINDOW_MS, created);
|
|
158
|
+
}
|
|
159
|
+
return lifetimeTps(sessionID, session, created, time);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const sessionTps = (sessionID: string, session: SessionLike | undefined): number | undefined => {
|
|
163
|
+
const cached = tpsCache;
|
|
164
|
+
if (cached !== undefined && cached.sessionID === sessionID && Date.now() - cached.at < TPS_CACHE_MS) {
|
|
165
|
+
return cached.value;
|
|
166
|
+
}
|
|
167
|
+
const value = computeTps(sessionID, session);
|
|
168
|
+
tpsCache = { sessionID, at: Date.now(), value };
|
|
169
|
+
return value;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
return { sessionTps };
|
|
173
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// The rail's stat vocabulary.
|
|
2
|
+
//
|
|
3
|
+
// `StatSource` is the subset of a session snapshot the rail reads; `STAT_FIELDS`
|
|
4
|
+
// is the set a user may name in `sidebar.rows`; `ANIMATED_FIELDS` drives the
|
|
5
|
+
// ticker; `LayoutHint` carries per-call row geometry. Untrusted values are read
|
|
6
|
+
// through ./coerce.js rather than re-checked here.
|
|
7
|
+
|
|
8
|
+
/** The subset of a session snapshot the rail reads. All fields are untrusted. */
|
|
9
|
+
export interface StatSource {
|
|
10
|
+
/**
|
|
11
|
+
* The annunciator line, precomputed by the caller.
|
|
12
|
+
*
|
|
13
|
+
* A plain string on purpose: the rules that decide whether something is wrong
|
|
14
|
+
* live in ./caution.ts and need the clock and the session snapshot, not a
|
|
15
|
+
* formatting function. Severity is already encoded in the glyph.
|
|
16
|
+
*/
|
|
17
|
+
readonly caution?: unknown;
|
|
18
|
+
readonly agent?: unknown;
|
|
19
|
+
readonly model?: unknown;
|
|
20
|
+
readonly cost?: unknown;
|
|
21
|
+
readonly tokens?: unknown;
|
|
22
|
+
readonly branch?: unknown;
|
|
23
|
+
readonly tree?: unknown;
|
|
24
|
+
readonly context?: unknown;
|
|
25
|
+
readonly project?: unknown;
|
|
26
|
+
readonly status?: unknown;
|
|
27
|
+
/** True when anything is working: this session, a subagent, or a shell. */
|
|
28
|
+
readonly busy?: unknown;
|
|
29
|
+
readonly perms?: unknown;
|
|
30
|
+
/**
|
|
31
|
+
* Current session throughput. On a host that stamps its messages this is a
|
|
32
|
+
* trailing-window rate over the last minute, subagents summed in; on a host
|
|
33
|
+
* without message timestamps it falls back to the lifetime average. Either
|
|
34
|
+
* way it is derived from state the host already holds.
|
|
35
|
+
*/
|
|
36
|
+
readonly tps?: unknown;
|
|
37
|
+
readonly elapsedMs?: unknown;
|
|
38
|
+
readonly turns?: unknown;
|
|
39
|
+
/** Recent per-turn output sizes, oldest first, for the sparkline. */
|
|
40
|
+
readonly spark?: unknown;
|
|
41
|
+
/** Animation frame counter, advanced by the ticker. */
|
|
42
|
+
readonly frame?: unknown;
|
|
43
|
+
/** Harness status from oc-harness-guard's RPC, via the guard bridge. Untrusted. */
|
|
44
|
+
readonly guard?: unknown;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Fields a user may name in `sidebar.rows`, in the order they are documented. */
|
|
48
|
+
export const STAT_FIELDS = [
|
|
49
|
+
"caution",
|
|
50
|
+
"status",
|
|
51
|
+
"agent",
|
|
52
|
+
"model",
|
|
53
|
+
"branch",
|
|
54
|
+
"cost",
|
|
55
|
+
"total",
|
|
56
|
+
"project",
|
|
57
|
+
"tokens",
|
|
58
|
+
"cache",
|
|
59
|
+
"context",
|
|
60
|
+
"perms",
|
|
61
|
+
"elapsed",
|
|
62
|
+
"tps",
|
|
63
|
+
"spark",
|
|
64
|
+
"reasoning",
|
|
65
|
+
"turns",
|
|
66
|
+
"guard",
|
|
67
|
+
] as const;
|
|
68
|
+
|
|
69
|
+
type StatField = (typeof STAT_FIELDS)[number];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Fields whose value only ever changes on a clock tick.
|
|
73
|
+
*
|
|
74
|
+
* The ticker exists to animate these. When none of them is on screen there is
|
|
75
|
+
* nothing to animate, so the plugin does not start a timer at all.
|
|
76
|
+
*
|
|
77
|
+
* `caution` belongs here for a stronger reason than the others: its whole job is
|
|
78
|
+
* noticing that time has passed without anything happening, so without a tick
|
|
79
|
+
* its thresholds could never be crossed on screen.
|
|
80
|
+
*/
|
|
81
|
+
export const ANIMATED_FIELDS = ["caution", "status", "elapsed"] as const;
|
|
82
|
+
|
|
83
|
+
export function isStatField(value: string): value is StatField {
|
|
84
|
+
return STAT_FIELDS.some((field) => field === value);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Row geometry, overridable per call.
|
|
89
|
+
*
|
|
90
|
+
* A structural type rather than an import of `FlightDeckConfig`: ./config.ts
|
|
91
|
+
* imports this module, so reaching back into it would create a cycle. The
|
|
92
|
+
* shapes are compatible, so the config passes straight through.
|
|
93
|
+
*/
|
|
94
|
+
export interface LayoutHint {
|
|
95
|
+
readonly labelWidth?: number;
|
|
96
|
+
readonly barWidth?: number;
|
|
97
|
+
readonly sparkWidth?: number;
|
|
98
|
+
/**
|
|
99
|
+
* True when the rail already draws a `total` row.
|
|
100
|
+
*
|
|
101
|
+
* The `cost` row merges the subagent total into itself only when nothing else
|
|
102
|
+
* on the rail already shows it — which makes "do I want one money row or two"
|
|
103
|
+
* a property of `sidebar.rows`, not a separate setting to find.
|
|
104
|
+
*/
|
|
105
|
+
readonly hasTotalRow?: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* When true, `statRows` renders one row per named field even when the host
|
|
108
|
+
* has no data for it, using `placeholder` as the value. Default off when
|
|
109
|
+
* calling `statRows` directly; `sidebarLines` turns it on from
|
|
110
|
+
* `config.sidebar.persist`.
|
|
111
|
+
*/
|
|
112
|
+
readonly persist?: boolean;
|
|
113
|
+
/** Value shown for a row with no data when `persist` is on. Default `"—"`. */
|
|
114
|
+
readonly placeholder?: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Placeholder value for a persistent row with no data yet. */
|
|
118
|
+
export const DEFAULT_PLACEHOLDER = "—";
|
|
119
|
+
|
|
120
|
+
export const DEFAULT_LABEL_WIDTH = 10;
|
|
121
|
+
export const DEFAULT_SPARK_WIDTH = 12;
|
|
122
|
+
|