oc-flight-deck 0.4.0 → 0.5.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/README.md +18 -3
- package/flight-deck.example.jsonc +15 -5
- package/flight-deck.schema.json +1 -1
- package/package.json +70 -70
- package/src/tui/file-config.ts +35 -13
- package/src/tui/index.tsx +515 -515
- package/src/tui/presentation.ts +34 -34
package/src/tui/index.tsx
CHANGED
|
@@ -1,515 +1,515 @@
|
|
|
1
|
-
/** @jsxImportSource @opentui/solid */
|
|
2
|
-
import { Plugin } from "@opencode/plugin/tui";
|
|
3
|
-
import { cautionDetail, cautionText, detectCautions, worstCaution, type Caution } from "./caution.js";
|
|
4
|
-
import { cautionThresholds, mergeOptions, resolveConfig } from "./config.js";
|
|
5
|
-
import { loadConfigFile } from "./file-config.js";
|
|
6
|
-
import { footerLine, liveRowOffset, sidebarLines } from "./presentation.js";
|
|
7
|
-
import { ANIMATED_FIELDS, type StatSource } from "./stats.js";
|
|
8
|
-
import { startGuardBridge } from "./guard.js";
|
|
9
|
-
import { startTicker } from "./ticker.js";
|
|
10
|
-
|
|
11
|
-
// Flight Deck is a read-only instrument panel for the OpenCode V2 CLI/TUI. It
|
|
12
|
-
// shows the open session's agent, model, branch, cost, tokens, and cache hit
|
|
13
|
-
// rate in the sidebar. It uses only the official CLI plugin boundary —
|
|
14
|
-
// `@opencode/plugin/tui`, `context.ui.slot`, and theme tokens.
|
|
15
|
-
//
|
|
16
|
-
// Read-only but for one thing: the animation tick writes a single counter into
|
|
17
|
-
// the host's ephemeral plugin-memory store. It has to, because a timer owned by
|
|
18
|
-
// this plugin cannot wake the host's renderer — see ./ticker.ts for why. That
|
|
19
|
-
// counter is in-process, scoped to this plugin, and gone when the TUI exits.
|
|
20
|
-
// Everything the rail displays is state the host already holds in memory.
|
|
21
|
-
//
|
|
22
|
-
// Appearance comes from, in order of precedence:
|
|
23
|
-
// 1. `context.options` — the host's plugin options, when the host forwards them
|
|
24
|
-
// 2.
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const file = loadConfigFile(
|
|
54
|
-
const { config, issues } = resolveConfig(mergeOptions(file.options, context.options));
|
|
55
|
-
|
|
56
|
-
if (file.issue !== undefined && file.source !== undefined) {
|
|
57
|
-
console.warn(`[flight-deck] ${file.issue}`);
|
|
58
|
-
}
|
|
59
|
-
const problems = file.issue !== undefined ? [file.issue, ...issues] : issues;
|
|
60
|
-
if (problems.length > 0) {
|
|
61
|
-
const extra = problems.length > 1 ? ` (+${problems.length - 1} more)` : "";
|
|
62
|
-
context.ui.toast.show({
|
|
63
|
-
title: "Flight Deck",
|
|
64
|
-
message: `Ignoring invalid config: ${problems[0]}${extra}`,
|
|
65
|
-
variant: "warning",
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const footer = footerLine(config);
|
|
70
|
-
const releases: Array<() => void> = [];
|
|
71
|
-
|
|
72
|
-
// The host owns these caches; a failure to read one must never break the
|
|
73
|
-
// rail, so every lookup degrades to "no data" instead of throwing.
|
|
74
|
-
const messagesOf = (sessionID: string): readonly unknown[] => {
|
|
75
|
-
try {
|
|
76
|
-
return context.data.session.message.list(sessionID) ?? [];
|
|
77
|
-
} catch {
|
|
78
|
-
return [];
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
// A subagent session is already inside its parent's family total, and the
|
|
83
|
-
// host keys `family()` by the family ROOT — so asking from a child returns
|
|
84
|
-
// its ancestors and siblings as well. Merging that would report the whole
|
|
85
|
-
// tree as this conversation's own, under a label that promises the
|
|
86
|
-
// conversation plus *its* subagents. Only a root has a family beneath it.
|
|
87
|
-
const isFamilyRoot = (sessionID: string): boolean => {
|
|
88
|
-
try {
|
|
89
|
-
return context.data.session.root(sessionID) === sessionID;
|
|
90
|
-
} catch {
|
|
91
|
-
// A host without `root` keeps the previous behaviour rather than
|
|
92
|
-
// silently dropping everyone's subagent total.
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// Subagent sessions are separate sessions, and the parent's `cost` does not
|
|
98
|
-
// include them, so a bare `cost` row understates a swarm. Sum the family.
|
|
99
|
-
const treeTotals = (sessionID: string, ownCost: unknown) => {
|
|
100
|
-
if (!isFamilyRoot(sessionID)) return undefined;
|
|
101
|
-
|
|
102
|
-
let ids: readonly string[];
|
|
103
|
-
try {
|
|
104
|
-
ids = context.data.session.family(sessionID) ?? [sessionID];
|
|
105
|
-
} catch {
|
|
106
|
-
return undefined;
|
|
107
|
-
}
|
|
108
|
-
if (ids.length <= 1) return undefined;
|
|
109
|
-
|
|
110
|
-
let cost = asCount(ownCost) ?? 0;
|
|
111
|
-
let count = 0;
|
|
112
|
-
for (const id of ids) {
|
|
113
|
-
if (id === sessionID) continue;
|
|
114
|
-
const child = context.data.session.get(id) as SessionLike | undefined;
|
|
115
|
-
if (child === undefined) continue;
|
|
116
|
-
count += 1;
|
|
117
|
-
cost += asCount(child.cost) ?? 0;
|
|
118
|
-
}
|
|
119
|
-
return count === 0 ? undefined : { cost, count };
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// A message's own totals are per-request, so the last assistant message's
|
|
123
|
-
// prompt size *is* the current context occupancy — not a running total.
|
|
124
|
-
const contextUsage = (sessionID: string, model: unknown) => {
|
|
125
|
-
const messages = messagesOf(sessionID);
|
|
126
|
-
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
127
|
-
const tokens = asRecord(asRecord(messages[index])?.tokens);
|
|
128
|
-
const input = asCount(tokens?.input);
|
|
129
|
-
if (tokens === undefined || input === undefined) continue;
|
|
130
|
-
const cache = asRecord(tokens.cache);
|
|
131
|
-
const used = input + (asCount(cache?.read) ?? 0) + (asCount(cache?.write) ?? 0);
|
|
132
|
-
if (used === 0) return undefined;
|
|
133
|
-
return { used, limit: contextLimit(model) };
|
|
134
|
-
}
|
|
135
|
-
return undefined;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
// The window lives at `limit.context` on the catalog entry, and two
|
|
139
|
-
// providers can expose the same model id with different windows — so the
|
|
140
|
-
// provider has to match too, or the gauge would read the wrong ceiling.
|
|
141
|
-
const contextLimit = (model: unknown): number | undefined => {
|
|
142
|
-
const ref = asRecord(model);
|
|
143
|
-
const id = ref?.id;
|
|
144
|
-
const providerID = ref?.providerID;
|
|
145
|
-
if (typeof id !== "string") return undefined;
|
|
146
|
-
try {
|
|
147
|
-
const location = context.location ?? context.data.location.default();
|
|
148
|
-
for (const entry of context.data.location.model.list(location) ?? []) {
|
|
149
|
-
const record = asRecord(entry);
|
|
150
|
-
if (record === undefined || record.id !== id) continue;
|
|
151
|
-
if (typeof providerID === "string" && record.providerID !== providerID) continue;
|
|
152
|
-
const limit = asRecord(record.limit);
|
|
153
|
-
return asCount(limit?.context) ?? asCount(record.context) ?? asCount(record.contextWindow);
|
|
154
|
-
}
|
|
155
|
-
} catch {
|
|
156
|
-
return undefined;
|
|
157
|
-
}
|
|
158
|
-
return undefined;
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
// Measured against the clock rather than the session's last update, so the
|
|
162
|
-
// row keeps moving between events instead of freezing between turns.
|
|
163
|
-
const sessionElapsed = (session: SessionLike | undefined): number | undefined => {
|
|
164
|
-
const created = asCount(asRecord(session?.time)?.created);
|
|
165
|
-
if (created === undefined) return undefined;
|
|
166
|
-
const elapsed = Date.now() - created;
|
|
167
|
-
return elapsed <= 0 ? undefined : elapsed;
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
// Project spend: every session in THIS project, not just the one on screen.
|
|
171
|
-
//
|
|
172
|
-
// `session.list()` is not scoped - it returns sessions across every
|
|
173
|
-
// directory the host knows about, so summing it gives "everything you have
|
|
174
|
-
// ever run" while claiming to be a project total. Filtered by `projectID`
|
|
175
|
-
// rather than by directory, because one project legitimately spans several
|
|
176
|
-
// directories (worktrees).
|
|
177
|
-
//
|
|
178
|
-
// If the host does not report a project id, the unfiltered list is used, so
|
|
179
|
-
// this degrades to the old behaviour instead of showing nothing.
|
|
180
|
-
const projectTotals = (sessionID: string) => {
|
|
181
|
-
try {
|
|
182
|
-
const sessions = context.data.session.list() ?? [];
|
|
183
|
-
const projectID = asText(asRecord(context.data.session.get(sessionID))?.["projectID"]);
|
|
184
|
-
const scoped =
|
|
185
|
-
projectID === undefined
|
|
186
|
-
? sessions
|
|
187
|
-
: sessions.filter((entry) => asText(asRecord(entry)?.["projectID"]) === projectID);
|
|
188
|
-
|
|
189
|
-
let cost = 0;
|
|
190
|
-
for (const entry of scoped) cost += asCount(asRecord(entry)?.["cost"]) ?? 0;
|
|
191
|
-
return { cost, count: scoped.length };
|
|
192
|
-
} catch {
|
|
193
|
-
return undefined;
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
const statusOf = (sessionID: string): string | undefined => {
|
|
198
|
-
try {
|
|
199
|
-
return context.data.session.status(sessionID);
|
|
200
|
-
} catch {
|
|
201
|
-
return undefined;
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
// Recent tool parts, oldest first. Only the tail is inspected: a loop that is
|
|
206
|
-
// not currently happening is history, and a hang is by definition now.
|
|
207
|
-
const recentParts = (sessionID: string): readonly unknown[] => {
|
|
208
|
-
const parts: unknown[] = [];
|
|
209
|
-
for (const entry of messagesOf(sessionID).slice(-RECENT_MESSAGES)) {
|
|
210
|
-
const content = asRecord(entry)?.["content"];
|
|
211
|
-
if (Array.isArray(content)) parts.push(...content);
|
|
212
|
-
}
|
|
213
|
-
return parts;
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
// The host's own shell records, which outlive the tool call: a backgrounded
|
|
217
|
-
// command returns its result immediately, so its part settles while the
|
|
218
|
-
// process keeps running. This is the only signal that survives that.
|
|
219
|
-
const shellsOf = (sessionID: string): readonly unknown[] => {
|
|
220
|
-
try {
|
|
221
|
-
const shellApi = context.data.shell as unknown as
|
|
222
|
-
| { listBySession?: (id: string) => readonly unknown[] }
|
|
223
|
-
| undefined;
|
|
224
|
-
return shellApi?.listBySession?.(sessionID) ?? [];
|
|
225
|
-
} catch {
|
|
226
|
-
return [];
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
// The newest thing the host recorded for this session that is not a tool
|
|
231
|
-
// part, so the quiet-turn rule is not blind to a model that is streaming.
|
|
232
|
-
const lastActivity = (sessionID: string): number | undefined => {
|
|
233
|
-
const messages = messagesOf(sessionID);
|
|
234
|
-
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
235
|
-
const time = asRecord(asRecord(messages[index])?.["time"]);
|
|
236
|
-
const stamp =
|
|
237
|
-
asCount(time?.["completed"]) ?? asCount(time?.["streamed"]) ?? asCount(time?.["created"]);
|
|
238
|
-
if (stamp !== undefined) return stamp;
|
|
239
|
-
}
|
|
240
|
-
return undefined;
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
const cautionThreshold = cautionThresholds(config.caution);
|
|
244
|
-
|
|
245
|
-
const cautionsOf = (sessionID: string, now: number): Caution[] =>
|
|
246
|
-
detectCautions({
|
|
247
|
-
parts: recentParts(sessionID),
|
|
248
|
-
shells: shellsOf(sessionID),
|
|
249
|
-
now,
|
|
250
|
-
sessionRunning: statusOf(sessionID) === "running",
|
|
251
|
-
lastMessageAt: lastActivity(sessionID),
|
|
252
|
-
thresholds: cautionThreshold,
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
// What is waiting, not just how many. A bare count tells you to go and look;
|
|
256
|
-
// naming the request tells you whether it is worth looking at.
|
|
257
|
-
const permsOf = (
|
|
258
|
-
sessionID: string,
|
|
259
|
-
): { count: number; action?: string; resource?: string } | undefined => {
|
|
260
|
-
try {
|
|
261
|
-
const requests = context.data.session.permission.list(sessionID);
|
|
262
|
-
if (requests === undefined || requests.length === 0) return undefined;
|
|
263
|
-
const first = asRecord(requests[0]);
|
|
264
|
-
const action = typeof first?.["action"] === "string" ? first["action"] : undefined;
|
|
265
|
-
const resources = Array.isArray(first?.["resources"]) ? first["resources"] : [];
|
|
266
|
-
const resource = typeof resources[0] === "string" ? resources[0] : undefined;
|
|
267
|
-
return { count: requests.length, action, resource };
|
|
268
|
-
} catch {
|
|
269
|
-
return undefined;
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
// Anything genuinely working keeps the status glyph turning: this session
|
|
274
|
-
// (thinking, streaming, running tools), any subagent in its tree, or a shell
|
|
275
|
-
// command still running. Returns `undefined` when the host has not said
|
|
276
|
-
// either way, so the rail can omit the row rather than invent "idle".
|
|
277
|
-
const busy = (sessionID: string): boolean | undefined => {
|
|
278
|
-
const status = statusOf(sessionID);
|
|
279
|
-
if (status === "running") return true;
|
|
280
|
-
|
|
281
|
-
try {
|
|
282
|
-
for (const id of context.data.session.family(sessionID) ?? []) {
|
|
283
|
-
if (id === sessionID) continue;
|
|
284
|
-
try {
|
|
285
|
-
if (context.data.session.status(id) === "running") return true;
|
|
286
|
-
} catch {
|
|
287
|
-
// One unreadable child must not hide a running sibling.
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
} catch {
|
|
291
|
-
// No subagent status on this host; the shell check below still applies.
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
try {
|
|
295
|
-
// `listBySession` is on the reactive client the host hands the plugin,
|
|
296
|
-
// but not on every published type surface, so it is reached defensively:
|
|
297
|
-
// a host without it still falls back to the session's own status.
|
|
298
|
-
const shellApi = context.data.shell as unknown as
|
|
299
|
-
| { listBySession?: (id: string) => readonly unknown[] }
|
|
300
|
-
| undefined;
|
|
301
|
-
const shells = shellApi?.listBySession?.(sessionID);
|
|
302
|
-
if (shells !== undefined && shells.some((entry) => asRecord(entry)?.status === "running")) return true;
|
|
303
|
-
} catch {
|
|
304
|
-
// Same again: unreadable shells just mean no shell signal.
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
return status === undefined ? undefined : false;
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
// Throughput of the last completed turn. The host records when streaming
|
|
311
|
-
// finished, so this is measured rather than estimated from wall-clock.
|
|
312
|
-
const lastTps = (sessionID: string): number | undefined => {
|
|
313
|
-
const messages = messagesOf(sessionID);
|
|
314
|
-
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
315
|
-
const message = asRecord(messages[index]);
|
|
316
|
-
const tokens = asRecord(message?.tokens);
|
|
317
|
-
const output = asCount(tokens?.output);
|
|
318
|
-
const time = asRecord(message?.time);
|
|
319
|
-
const created = asCount(time?.created);
|
|
320
|
-
const streamed = asCount(time?.streamed);
|
|
321
|
-
if (output === undefined || created === undefined || streamed === undefined) continue;
|
|
322
|
-
const duration = streamed - created;
|
|
323
|
-
if (duration <= 0) continue;
|
|
324
|
-
return output / (duration / 1_000);
|
|
325
|
-
}
|
|
326
|
-
return undefined;
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
// Recent turn sizes, oldest first. Drawn from messages the host already
|
|
330
|
-
// holds, so the sparkline needs no history of our own to accumulate.
|
|
331
|
-
const sparkValues = (sessionID: string): readonly number[] => {
|
|
332
|
-
const sizes: number[] = [];
|
|
333
|
-
for (const entry of messagesOf(sessionID)) {
|
|
334
|
-
const message = asRecord(entry);
|
|
335
|
-
if (message?.type !== "assistant") continue;
|
|
336
|
-
const output = asCount(asRecord(message.tokens)?.output);
|
|
337
|
-
if (output === undefined) continue;
|
|
338
|
-
sizes.push(output);
|
|
339
|
-
}
|
|
340
|
-
// Windowed by the configured width, not a hardcoded one: `sparkWidth` is
|
|
341
|
-
// capped here first, so a wider setting used to be silently impossible.
|
|
342
|
-
return sizes.slice(-config.layout.sparkWidth);
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
// A ticker exists only for clock-derived rows; everything else updates from
|
|
346
|
-
// the host's own events. It is skipped entirely when nothing on the rail
|
|
347
|
-
// animates, and `refresh: 0` opts out regardless. The tick has to live in the
|
|
348
|
-
// host's reactive graph rather than ours, or it re-renders nothing at all.
|
|
349
|
-
const animated =
|
|
350
|
-
config.sidebar.enabled &&
|
|
351
|
-
config.sidebar.rows.some((name) => (ANIMATED_FIELDS as readonly string[]).includes(name));
|
|
352
|
-
const ticker = startTicker(context, animated ? config.refresh : 0);
|
|
353
|
-
|
|
354
|
-
// The guard row is opt-in and polls its own RPC: starting the bridge only
|
|
355
|
-
// when the row is on the rail means no timer and no request for a row
|
|
356
|
-
// nobody renders. The bridge resolves `status` from `context.client` on
|
|
357
|
-
// every poll, so installing guard later appears within its interval.
|
|
358
|
-
const wantGuard = config.sidebar.enabled && config.sidebar.rows.includes("guard");
|
|
359
|
-
const bridge = wantGuard ? startGuardBridge(context) : undefined;
|
|
360
|
-
|
|
361
|
-
// Read session state inside the render so the rail stays live: cost and
|
|
362
|
-
// tokens climb as the session runs, and the branch appears once VCS
|
|
363
|
-
// resolves. Nothing here writes, requests, or leaves the process.
|
|
364
|
-
// Only the worst is ever drawn: the rail has one line for this, and two
|
|
365
|
-
// simultaneous observations is one problem with two symptoms.
|
|
366
|
-
//
|
|
367
|
-
// Returns a plain string, themed exactly like every other live row. The
|
|
368
|
-
// annunciator carries its meaning in its glyph, not in a colour of its own:
|
|
369
|
-
// a row that suddenly brightens reads as a different kind of thing rather
|
|
370
|
-
// than as the same panel telling you something.
|
|
371
|
-
const announce = (sessionID: string): string | undefined => {
|
|
372
|
-
if (!config.caution.enabled) return undefined;
|
|
373
|
-
const top = worstCaution(cautionsOf(sessionID, Date.now()));
|
|
374
|
-
return top === undefined ? undefined : cautionText(top, config.glyphs);
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
// Both of these are host lookups like any other, and the slot render must
|
|
378
|
-
// not throw: an unreachable default location, or a VCS call on a directory
|
|
379
|
-
// with no repository, would take the whole rail down with it. Each degrades
|
|
380
|
-
// to "no branch row", the same way every other missing value does.
|
|
381
|
-
const locationOf = () => {
|
|
382
|
-
try {
|
|
383
|
-
return context.location ?? context.data.location.default();
|
|
384
|
-
} catch {
|
|
385
|
-
return context.location;
|
|
386
|
-
}
|
|
387
|
-
};
|
|
388
|
-
|
|
389
|
-
const branchOf = (location: ReturnType<typeof locationOf>): string | undefined => {
|
|
390
|
-
try {
|
|
391
|
-
return context.data.location.vcs.info(location)?.branch?.current;
|
|
392
|
-
} catch {
|
|
393
|
-
return undefined;
|
|
394
|
-
}
|
|
395
|
-
};
|
|
396
|
-
|
|
397
|
-
const snapshot = (sessionID: string): StatSource => {
|
|
398
|
-
const location = locationOf();
|
|
399
|
-
const session = context.data.session.get(sessionID) as SessionLike | undefined;
|
|
400
|
-
// A "turn" is one prompt and the work it caused. The host's message list
|
|
401
|
-
// also carries system, shell and switch records, so counting records
|
|
402
|
-
// reports several times the turns actually taken.
|
|
403
|
-
const turns = messagesOf(sessionID).filter((entry) => asRecord(entry)?.["type"] === "user").length;
|
|
404
|
-
|
|
405
|
-
return {
|
|
406
|
-
...session,
|
|
407
|
-
caution: announce(sessionID),
|
|
408
|
-
branch: branchOf(location),
|
|
409
|
-
tree: treeTotals(sessionID, session?.cost),
|
|
410
|
-
context: contextUsage(sessionID, session?.model),
|
|
411
|
-
project: projectTotals(sessionID),
|
|
412
|
-
status: statusOf(sessionID),
|
|
413
|
-
busy: busy(sessionID),
|
|
414
|
-
perms: permsOf(sessionID),
|
|
415
|
-
tps: lastTps(sessionID),
|
|
416
|
-
spark: sparkValues(sessionID),
|
|
417
|
-
elapsedMs: sessionElapsed(session),
|
|
418
|
-
turns,
|
|
419
|
-
guard: bridge?.status,
|
|
420
|
-
// Read the tick inside the render so the host registers a dependency on
|
|
421
|
-
// it; that read is what makes the rail re-run on the ticker's schedule.
|
|
422
|
-
frame: ticker?.frame ?? 0,
|
|
423
|
-
};
|
|
424
|
-
};
|
|
425
|
-
|
|
426
|
-
// The toast is opt-in and deliberately not the signal — the rail is. It
|
|
427
|
-
// fires on the transition into a caution, once per distinct problem, and
|
|
428
|
-
// re-arms when the session goes quiet, so a persistent hang cannot nag.
|
|
429
|
-
// Reading state on a timer rather than inside the render keeps the render a
|
|
430
|
-
// pure function of what it was given.
|
|
431
|
-
let watchedSession: string | undefined;
|
|
432
|
-
let toastedKey: string | undefined;
|
|
433
|
-
let watchTimer: ReturnType<typeof setInterval> | undefined;
|
|
434
|
-
|
|
435
|
-
if (config.caution.enabled && config.caution.toast) {
|
|
436
|
-
watchTimer = setInterval(() => {
|
|
437
|
-
const sessionID = watchedSession;
|
|
438
|
-
if (sessionID === undefined) return;
|
|
439
|
-
try {
|
|
440
|
-
const top = worstCaution(cautionsOf(sessionID, Date.now()));
|
|
441
|
-
if (top === undefined || top.severity !== "caution") {
|
|
442
|
-
toastedKey = undefined;
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
if (top.key === toastedKey) return;
|
|
446
|
-
toastedKey = top.key;
|
|
447
|
-
context.ui.toast.show({
|
|
448
|
-
title: "Flight Deck",
|
|
449
|
-
message: cautionDetail(top),
|
|
450
|
-
variant: "warning",
|
|
451
|
-
});
|
|
452
|
-
} catch {
|
|
453
|
-
// A warning must never be able to break the panel it warns about.
|
|
454
|
-
}
|
|
455
|
-
}, config.refresh > 0 ? config.refresh : 1_000);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
if (config.sidebar.enabled) {
|
|
459
|
-
releases.push(
|
|
460
|
-
context.ui.slot({
|
|
461
|
-
append: "sidebar.content",
|
|
462
|
-
render: ({ sessionID }) => {
|
|
463
|
-
// The annunciator is drawn from a timer, so the toast watcher needs
|
|
464
|
-
// to know which session is on screen. Reading the frame is what
|
|
465
|
-
// subscribes this render to the host's tick — without it nothing
|
|
466
|
-
// re-runs between host events, and a stall is precisely the failure
|
|
467
|
-
// that produces no host events at all.
|
|
468
|
-
watchedSession = sessionID;
|
|
469
|
-
// Keep the guard bridge on the rendered session: the follow fetches
|
|
470
|
-
// immediately on change, and the status read below subscribes this
|
|
471
|
-
// render to the bridge's host store, so polls re-run the rail.
|
|
472
|
-
bridge?.follow(sessionID);
|
|
473
|
-
void ticker?.frame;
|
|
474
|
-
void bridge?.status;
|
|
475
|
-
|
|
476
|
-
const source = snapshot(sessionID);
|
|
477
|
-
const lines = sidebarLines(config, source);
|
|
478
|
-
if (lines.length === 0) return null;
|
|
479
|
-
const offset = Math.min(liveRowOffset(config), lines.length);
|
|
480
|
-
|
|
481
|
-
return (
|
|
482
|
-
// No padding here: the host already lays out and pads the sidebar,
|
|
483
|
-
// so adding our own would push the rows out of alignment with it.
|
|
484
|
-
//
|
|
485
|
-
// Every live row gets the same token, the annunciator included. It
|
|
486
|
-
// is the same panel; only the glyph changes.
|
|
487
|
-
<box flexDirection="column">
|
|
488
|
-
{lines.map((line, index) => (
|
|
489
|
-
<text fg={index < offset ? context.theme.text.default : context.theme.text.subdued}>{line}</text>
|
|
490
|
-
))}
|
|
491
|
-
</box>
|
|
492
|
-
);
|
|
493
|
-
},
|
|
494
|
-
}),
|
|
495
|
-
);
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
if (footer !== undefined) {
|
|
499
|
-
releases.push(
|
|
500
|
-
context.ui.slot({
|
|
501
|
-
append: "prompt.footer.status",
|
|
502
|
-
render: () => <text fg={context.theme.text.subdued}>{footer}</text>,
|
|
503
|
-
}),
|
|
504
|
-
);
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
return () => {
|
|
508
|
-
if (watchTimer !== undefined) clearInterval(watchTimer);
|
|
509
|
-
watchTimer = undefined;
|
|
510
|
-
ticker?.dispose();
|
|
511
|
-
bridge?.dispose();
|
|
512
|
-
for (const release of releases) release();
|
|
513
|
-
};
|
|
514
|
-
},
|
|
515
|
-
});
|
|
1
|
+
/** @jsxImportSource @opentui/solid */
|
|
2
|
+
import { Plugin } from "@opencode/plugin/tui";
|
|
3
|
+
import { cautionDetail, cautionText, detectCautions, worstCaution, type Caution } from "./caution.js";
|
|
4
|
+
import { cautionThresholds, mergeOptions, resolveConfig } from "./config.js";
|
|
5
|
+
import { loadConfigFile } from "./file-config.js";
|
|
6
|
+
import { footerLine, liveRowOffset, sidebarLines } from "./presentation.js";
|
|
7
|
+
import { ANIMATED_FIELDS, type StatSource } from "./stats.js";
|
|
8
|
+
import { startGuardBridge } from "./guard.js";
|
|
9
|
+
import { startTicker } from "./ticker.js";
|
|
10
|
+
|
|
11
|
+
// Flight Deck is a read-only instrument panel for the OpenCode V2 CLI/TUI. It
|
|
12
|
+
// shows the open session's agent, model, branch, cost, tokens, and cache hit
|
|
13
|
+
// rate in the sidebar. It uses only the official CLI plugin boundary —
|
|
14
|
+
// `@opencode/plugin/tui`, `context.ui.slot`, and theme tokens.
|
|
15
|
+
//
|
|
16
|
+
// Read-only but for one thing: the animation tick writes a single counter into
|
|
17
|
+
// the host's ephemeral plugin-memory store. It has to, because a timer owned by
|
|
18
|
+
// this plugin cannot wake the host's renderer — see ./ticker.ts for why. That
|
|
19
|
+
// counter is in-process, scoped to this plugin, and gone when the TUI exits.
|
|
20
|
+
// Everything the rail displays is state the host already holds in memory.
|
|
21
|
+
//
|
|
22
|
+
// Appearance comes from, in order of precedence:
|
|
23
|
+
// 1. `context.options` — the host's plugin options, when the host forwards them
|
|
24
|
+
// 2. the one global config file, `flight-deck.jsonc` in the opencode
|
|
25
|
+
// configuration directory
|
|
26
|
+
// 3. the sane defaults in ./config.ts
|
|
27
|
+
// See flight-deck.example.jsonc for the commented template.
|
|
28
|
+
|
|
29
|
+
/** How many trailing messages are inspected for tool parts. */
|
|
30
|
+
const RECENT_MESSAGES = 4;
|
|
31
|
+
|
|
32
|
+
type SessionLike = { cost?: unknown; model?: unknown; time?: unknown };
|
|
33
|
+
|
|
34
|
+
function asCount(value: unknown): number | undefined {
|
|
35
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function asText(value: unknown): string | undefined {
|
|
39
|
+
if (typeof value !== "string") return undefined;
|
|
40
|
+
const text = value.trim();
|
|
41
|
+
return text.length === 0 ? undefined : text;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
45
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
46
|
+
? (value as Record<string, unknown>)
|
|
47
|
+
: undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export default Plugin.define({
|
|
51
|
+
id: "flight-deck-tui",
|
|
52
|
+
setup(context) {
|
|
53
|
+
const file = loadConfigFile();
|
|
54
|
+
const { config, issues } = resolveConfig(mergeOptions(file.options, context.options));
|
|
55
|
+
|
|
56
|
+
if (file.issue !== undefined && file.source !== undefined) {
|
|
57
|
+
console.warn(`[flight-deck] ${file.issue}`);
|
|
58
|
+
}
|
|
59
|
+
const problems = file.issue !== undefined ? [file.issue, ...issues] : issues;
|
|
60
|
+
if (problems.length > 0) {
|
|
61
|
+
const extra = problems.length > 1 ? ` (+${problems.length - 1} more)` : "";
|
|
62
|
+
context.ui.toast.show({
|
|
63
|
+
title: "Flight Deck",
|
|
64
|
+
message: `Ignoring invalid config: ${problems[0]}${extra}`,
|
|
65
|
+
variant: "warning",
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const footer = footerLine(config);
|
|
70
|
+
const releases: Array<() => void> = [];
|
|
71
|
+
|
|
72
|
+
// The host owns these caches; a failure to read one must never break the
|
|
73
|
+
// rail, so every lookup degrades to "no data" instead of throwing.
|
|
74
|
+
const messagesOf = (sessionID: string): readonly unknown[] => {
|
|
75
|
+
try {
|
|
76
|
+
return context.data.session.message.list(sessionID) ?? [];
|
|
77
|
+
} catch {
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// A subagent session is already inside its parent's family total, and the
|
|
83
|
+
// host keys `family()` by the family ROOT — so asking from a child returns
|
|
84
|
+
// its ancestors and siblings as well. Merging that would report the whole
|
|
85
|
+
// tree as this conversation's own, under a label that promises the
|
|
86
|
+
// conversation plus *its* subagents. Only a root has a family beneath it.
|
|
87
|
+
const isFamilyRoot = (sessionID: string): boolean => {
|
|
88
|
+
try {
|
|
89
|
+
return context.data.session.root(sessionID) === sessionID;
|
|
90
|
+
} catch {
|
|
91
|
+
// A host without `root` keeps the previous behaviour rather than
|
|
92
|
+
// silently dropping everyone's subagent total.
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// Subagent sessions are separate sessions, and the parent's `cost` does not
|
|
98
|
+
// include them, so a bare `cost` row understates a swarm. Sum the family.
|
|
99
|
+
const treeTotals = (sessionID: string, ownCost: unknown) => {
|
|
100
|
+
if (!isFamilyRoot(sessionID)) return undefined;
|
|
101
|
+
|
|
102
|
+
let ids: readonly string[];
|
|
103
|
+
try {
|
|
104
|
+
ids = context.data.session.family(sessionID) ?? [sessionID];
|
|
105
|
+
} catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
if (ids.length <= 1) return undefined;
|
|
109
|
+
|
|
110
|
+
let cost = asCount(ownCost) ?? 0;
|
|
111
|
+
let count = 0;
|
|
112
|
+
for (const id of ids) {
|
|
113
|
+
if (id === sessionID) continue;
|
|
114
|
+
const child = context.data.session.get(id) as SessionLike | undefined;
|
|
115
|
+
if (child === undefined) continue;
|
|
116
|
+
count += 1;
|
|
117
|
+
cost += asCount(child.cost) ?? 0;
|
|
118
|
+
}
|
|
119
|
+
return count === 0 ? undefined : { cost, count };
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// A message's own totals are per-request, so the last assistant message's
|
|
123
|
+
// prompt size *is* the current context occupancy — not a running total.
|
|
124
|
+
const contextUsage = (sessionID: string, model: unknown) => {
|
|
125
|
+
const messages = messagesOf(sessionID);
|
|
126
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
127
|
+
const tokens = asRecord(asRecord(messages[index])?.tokens);
|
|
128
|
+
const input = asCount(tokens?.input);
|
|
129
|
+
if (tokens === undefined || input === undefined) continue;
|
|
130
|
+
const cache = asRecord(tokens.cache);
|
|
131
|
+
const used = input + (asCount(cache?.read) ?? 0) + (asCount(cache?.write) ?? 0);
|
|
132
|
+
if (used === 0) return undefined;
|
|
133
|
+
return { used, limit: contextLimit(model) };
|
|
134
|
+
}
|
|
135
|
+
return undefined;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// The window lives at `limit.context` on the catalog entry, and two
|
|
139
|
+
// providers can expose the same model id with different windows — so the
|
|
140
|
+
// provider has to match too, or the gauge would read the wrong ceiling.
|
|
141
|
+
const contextLimit = (model: unknown): number | undefined => {
|
|
142
|
+
const ref = asRecord(model);
|
|
143
|
+
const id = ref?.id;
|
|
144
|
+
const providerID = ref?.providerID;
|
|
145
|
+
if (typeof id !== "string") return undefined;
|
|
146
|
+
try {
|
|
147
|
+
const location = context.location ?? context.data.location.default();
|
|
148
|
+
for (const entry of context.data.location.model.list(location) ?? []) {
|
|
149
|
+
const record = asRecord(entry);
|
|
150
|
+
if (record === undefined || record.id !== id) continue;
|
|
151
|
+
if (typeof providerID === "string" && record.providerID !== providerID) continue;
|
|
152
|
+
const limit = asRecord(record.limit);
|
|
153
|
+
return asCount(limit?.context) ?? asCount(record.context) ?? asCount(record.contextWindow);
|
|
154
|
+
}
|
|
155
|
+
} catch {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
return undefined;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// Measured against the clock rather than the session's last update, so the
|
|
162
|
+
// row keeps moving between events instead of freezing between turns.
|
|
163
|
+
const sessionElapsed = (session: SessionLike | undefined): number | undefined => {
|
|
164
|
+
const created = asCount(asRecord(session?.time)?.created);
|
|
165
|
+
if (created === undefined) return undefined;
|
|
166
|
+
const elapsed = Date.now() - created;
|
|
167
|
+
return elapsed <= 0 ? undefined : elapsed;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// Project spend: every session in THIS project, not just the one on screen.
|
|
171
|
+
//
|
|
172
|
+
// `session.list()` is not scoped - it returns sessions across every
|
|
173
|
+
// directory the host knows about, so summing it gives "everything you have
|
|
174
|
+
// ever run" while claiming to be a project total. Filtered by `projectID`
|
|
175
|
+
// rather than by directory, because one project legitimately spans several
|
|
176
|
+
// directories (worktrees).
|
|
177
|
+
//
|
|
178
|
+
// If the host does not report a project id, the unfiltered list is used, so
|
|
179
|
+
// this degrades to the old behaviour instead of showing nothing.
|
|
180
|
+
const projectTotals = (sessionID: string) => {
|
|
181
|
+
try {
|
|
182
|
+
const sessions = context.data.session.list() ?? [];
|
|
183
|
+
const projectID = asText(asRecord(context.data.session.get(sessionID))?.["projectID"]);
|
|
184
|
+
const scoped =
|
|
185
|
+
projectID === undefined
|
|
186
|
+
? sessions
|
|
187
|
+
: sessions.filter((entry) => asText(asRecord(entry)?.["projectID"]) === projectID);
|
|
188
|
+
|
|
189
|
+
let cost = 0;
|
|
190
|
+
for (const entry of scoped) cost += asCount(asRecord(entry)?.["cost"]) ?? 0;
|
|
191
|
+
return { cost, count: scoped.length };
|
|
192
|
+
} catch {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const statusOf = (sessionID: string): string | undefined => {
|
|
198
|
+
try {
|
|
199
|
+
return context.data.session.status(sessionID);
|
|
200
|
+
} catch {
|
|
201
|
+
return undefined;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// Recent tool parts, oldest first. Only the tail is inspected: a loop that is
|
|
206
|
+
// not currently happening is history, and a hang is by definition now.
|
|
207
|
+
const recentParts = (sessionID: string): readonly unknown[] => {
|
|
208
|
+
const parts: unknown[] = [];
|
|
209
|
+
for (const entry of messagesOf(sessionID).slice(-RECENT_MESSAGES)) {
|
|
210
|
+
const content = asRecord(entry)?.["content"];
|
|
211
|
+
if (Array.isArray(content)) parts.push(...content);
|
|
212
|
+
}
|
|
213
|
+
return parts;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
// The host's own shell records, which outlive the tool call: a backgrounded
|
|
217
|
+
// command returns its result immediately, so its part settles while the
|
|
218
|
+
// process keeps running. This is the only signal that survives that.
|
|
219
|
+
const shellsOf = (sessionID: string): readonly unknown[] => {
|
|
220
|
+
try {
|
|
221
|
+
const shellApi = context.data.shell as unknown as
|
|
222
|
+
| { listBySession?: (id: string) => readonly unknown[] }
|
|
223
|
+
| undefined;
|
|
224
|
+
return shellApi?.listBySession?.(sessionID) ?? [];
|
|
225
|
+
} catch {
|
|
226
|
+
return [];
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// The newest thing the host recorded for this session that is not a tool
|
|
231
|
+
// part, so the quiet-turn rule is not blind to a model that is streaming.
|
|
232
|
+
const lastActivity = (sessionID: string): number | undefined => {
|
|
233
|
+
const messages = messagesOf(sessionID);
|
|
234
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
235
|
+
const time = asRecord(asRecord(messages[index])?.["time"]);
|
|
236
|
+
const stamp =
|
|
237
|
+
asCount(time?.["completed"]) ?? asCount(time?.["streamed"]) ?? asCount(time?.["created"]);
|
|
238
|
+
if (stamp !== undefined) return stamp;
|
|
239
|
+
}
|
|
240
|
+
return undefined;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const cautionThreshold = cautionThresholds(config.caution);
|
|
244
|
+
|
|
245
|
+
const cautionsOf = (sessionID: string, now: number): Caution[] =>
|
|
246
|
+
detectCautions({
|
|
247
|
+
parts: recentParts(sessionID),
|
|
248
|
+
shells: shellsOf(sessionID),
|
|
249
|
+
now,
|
|
250
|
+
sessionRunning: statusOf(sessionID) === "running",
|
|
251
|
+
lastMessageAt: lastActivity(sessionID),
|
|
252
|
+
thresholds: cautionThreshold,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// What is waiting, not just how many. A bare count tells you to go and look;
|
|
256
|
+
// naming the request tells you whether it is worth looking at.
|
|
257
|
+
const permsOf = (
|
|
258
|
+
sessionID: string,
|
|
259
|
+
): { count: number; action?: string; resource?: string } | undefined => {
|
|
260
|
+
try {
|
|
261
|
+
const requests = context.data.session.permission.list(sessionID);
|
|
262
|
+
if (requests === undefined || requests.length === 0) return undefined;
|
|
263
|
+
const first = asRecord(requests[0]);
|
|
264
|
+
const action = typeof first?.["action"] === "string" ? first["action"] : undefined;
|
|
265
|
+
const resources = Array.isArray(first?.["resources"]) ? first["resources"] : [];
|
|
266
|
+
const resource = typeof resources[0] === "string" ? resources[0] : undefined;
|
|
267
|
+
return { count: requests.length, action, resource };
|
|
268
|
+
} catch {
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// Anything genuinely working keeps the status glyph turning: this session
|
|
274
|
+
// (thinking, streaming, running tools), any subagent in its tree, or a shell
|
|
275
|
+
// command still running. Returns `undefined` when the host has not said
|
|
276
|
+
// either way, so the rail can omit the row rather than invent "idle".
|
|
277
|
+
const busy = (sessionID: string): boolean | undefined => {
|
|
278
|
+
const status = statusOf(sessionID);
|
|
279
|
+
if (status === "running") return true;
|
|
280
|
+
|
|
281
|
+
try {
|
|
282
|
+
for (const id of context.data.session.family(sessionID) ?? []) {
|
|
283
|
+
if (id === sessionID) continue;
|
|
284
|
+
try {
|
|
285
|
+
if (context.data.session.status(id) === "running") return true;
|
|
286
|
+
} catch {
|
|
287
|
+
// One unreadable child must not hide a running sibling.
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
} catch {
|
|
291
|
+
// No subagent status on this host; the shell check below still applies.
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
try {
|
|
295
|
+
// `listBySession` is on the reactive client the host hands the plugin,
|
|
296
|
+
// but not on every published type surface, so it is reached defensively:
|
|
297
|
+
// a host without it still falls back to the session's own status.
|
|
298
|
+
const shellApi = context.data.shell as unknown as
|
|
299
|
+
| { listBySession?: (id: string) => readonly unknown[] }
|
|
300
|
+
| undefined;
|
|
301
|
+
const shells = shellApi?.listBySession?.(sessionID);
|
|
302
|
+
if (shells !== undefined && shells.some((entry) => asRecord(entry)?.status === "running")) return true;
|
|
303
|
+
} catch {
|
|
304
|
+
// Same again: unreadable shells just mean no shell signal.
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return status === undefined ? undefined : false;
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
// Throughput of the last completed turn. The host records when streaming
|
|
311
|
+
// finished, so this is measured rather than estimated from wall-clock.
|
|
312
|
+
const lastTps = (sessionID: string): number | undefined => {
|
|
313
|
+
const messages = messagesOf(sessionID);
|
|
314
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
315
|
+
const message = asRecord(messages[index]);
|
|
316
|
+
const tokens = asRecord(message?.tokens);
|
|
317
|
+
const output = asCount(tokens?.output);
|
|
318
|
+
const time = asRecord(message?.time);
|
|
319
|
+
const created = asCount(time?.created);
|
|
320
|
+
const streamed = asCount(time?.streamed);
|
|
321
|
+
if (output === undefined || created === undefined || streamed === undefined) continue;
|
|
322
|
+
const duration = streamed - created;
|
|
323
|
+
if (duration <= 0) continue;
|
|
324
|
+
return output / (duration / 1_000);
|
|
325
|
+
}
|
|
326
|
+
return undefined;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
// Recent turn sizes, oldest first. Drawn from messages the host already
|
|
330
|
+
// holds, so the sparkline needs no history of our own to accumulate.
|
|
331
|
+
const sparkValues = (sessionID: string): readonly number[] => {
|
|
332
|
+
const sizes: number[] = [];
|
|
333
|
+
for (const entry of messagesOf(sessionID)) {
|
|
334
|
+
const message = asRecord(entry);
|
|
335
|
+
if (message?.type !== "assistant") continue;
|
|
336
|
+
const output = asCount(asRecord(message.tokens)?.output);
|
|
337
|
+
if (output === undefined) continue;
|
|
338
|
+
sizes.push(output);
|
|
339
|
+
}
|
|
340
|
+
// Windowed by the configured width, not a hardcoded one: `sparkWidth` is
|
|
341
|
+
// capped here first, so a wider setting used to be silently impossible.
|
|
342
|
+
return sizes.slice(-config.layout.sparkWidth);
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
// A ticker exists only for clock-derived rows; everything else updates from
|
|
346
|
+
// the host's own events. It is skipped entirely when nothing on the rail
|
|
347
|
+
// animates, and `refresh: 0` opts out regardless. The tick has to live in the
|
|
348
|
+
// host's reactive graph rather than ours, or it re-renders nothing at all.
|
|
349
|
+
const animated =
|
|
350
|
+
config.sidebar.enabled &&
|
|
351
|
+
config.sidebar.rows.some((name) => (ANIMATED_FIELDS as readonly string[]).includes(name));
|
|
352
|
+
const ticker = startTicker(context, animated ? config.refresh : 0);
|
|
353
|
+
|
|
354
|
+
// The guard row is opt-in and polls its own RPC: starting the bridge only
|
|
355
|
+
// when the row is on the rail means no timer and no request for a row
|
|
356
|
+
// nobody renders. The bridge resolves `status` from `context.client` on
|
|
357
|
+
// every poll, so installing guard later appears within its interval.
|
|
358
|
+
const wantGuard = config.sidebar.enabled && config.sidebar.rows.includes("guard");
|
|
359
|
+
const bridge = wantGuard ? startGuardBridge(context) : undefined;
|
|
360
|
+
|
|
361
|
+
// Read session state inside the render so the rail stays live: cost and
|
|
362
|
+
// tokens climb as the session runs, and the branch appears once VCS
|
|
363
|
+
// resolves. Nothing here writes, requests, or leaves the process.
|
|
364
|
+
// Only the worst is ever drawn: the rail has one line for this, and two
|
|
365
|
+
// simultaneous observations is one problem with two symptoms.
|
|
366
|
+
//
|
|
367
|
+
// Returns a plain string, themed exactly like every other live row. The
|
|
368
|
+
// annunciator carries its meaning in its glyph, not in a colour of its own:
|
|
369
|
+
// a row that suddenly brightens reads as a different kind of thing rather
|
|
370
|
+
// than as the same panel telling you something.
|
|
371
|
+
const announce = (sessionID: string): string | undefined => {
|
|
372
|
+
if (!config.caution.enabled) return undefined;
|
|
373
|
+
const top = worstCaution(cautionsOf(sessionID, Date.now()));
|
|
374
|
+
return top === undefined ? undefined : cautionText(top, config.glyphs);
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
// Both of these are host lookups like any other, and the slot render must
|
|
378
|
+
// not throw: an unreachable default location, or a VCS call on a directory
|
|
379
|
+
// with no repository, would take the whole rail down with it. Each degrades
|
|
380
|
+
// to "no branch row", the same way every other missing value does.
|
|
381
|
+
const locationOf = () => {
|
|
382
|
+
try {
|
|
383
|
+
return context.location ?? context.data.location.default();
|
|
384
|
+
} catch {
|
|
385
|
+
return context.location;
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const branchOf = (location: ReturnType<typeof locationOf>): string | undefined => {
|
|
390
|
+
try {
|
|
391
|
+
return context.data.location.vcs.info(location)?.branch?.current;
|
|
392
|
+
} catch {
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
const snapshot = (sessionID: string): StatSource => {
|
|
398
|
+
const location = locationOf();
|
|
399
|
+
const session = context.data.session.get(sessionID) as SessionLike | undefined;
|
|
400
|
+
// A "turn" is one prompt and the work it caused. The host's message list
|
|
401
|
+
// also carries system, shell and switch records, so counting records
|
|
402
|
+
// reports several times the turns actually taken.
|
|
403
|
+
const turns = messagesOf(sessionID).filter((entry) => asRecord(entry)?.["type"] === "user").length;
|
|
404
|
+
|
|
405
|
+
return {
|
|
406
|
+
...session,
|
|
407
|
+
caution: announce(sessionID),
|
|
408
|
+
branch: branchOf(location),
|
|
409
|
+
tree: treeTotals(sessionID, session?.cost),
|
|
410
|
+
context: contextUsage(sessionID, session?.model),
|
|
411
|
+
project: projectTotals(sessionID),
|
|
412
|
+
status: statusOf(sessionID),
|
|
413
|
+
busy: busy(sessionID),
|
|
414
|
+
perms: permsOf(sessionID),
|
|
415
|
+
tps: lastTps(sessionID),
|
|
416
|
+
spark: sparkValues(sessionID),
|
|
417
|
+
elapsedMs: sessionElapsed(session),
|
|
418
|
+
turns,
|
|
419
|
+
guard: bridge?.status,
|
|
420
|
+
// Read the tick inside the render so the host registers a dependency on
|
|
421
|
+
// it; that read is what makes the rail re-run on the ticker's schedule.
|
|
422
|
+
frame: ticker?.frame ?? 0,
|
|
423
|
+
};
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
// The toast is opt-in and deliberately not the signal — the rail is. It
|
|
427
|
+
// fires on the transition into a caution, once per distinct problem, and
|
|
428
|
+
// re-arms when the session goes quiet, so a persistent hang cannot nag.
|
|
429
|
+
// Reading state on a timer rather than inside the render keeps the render a
|
|
430
|
+
// pure function of what it was given.
|
|
431
|
+
let watchedSession: string | undefined;
|
|
432
|
+
let toastedKey: string | undefined;
|
|
433
|
+
let watchTimer: ReturnType<typeof setInterval> | undefined;
|
|
434
|
+
|
|
435
|
+
if (config.caution.enabled && config.caution.toast) {
|
|
436
|
+
watchTimer = setInterval(() => {
|
|
437
|
+
const sessionID = watchedSession;
|
|
438
|
+
if (sessionID === undefined) return;
|
|
439
|
+
try {
|
|
440
|
+
const top = worstCaution(cautionsOf(sessionID, Date.now()));
|
|
441
|
+
if (top === undefined || top.severity !== "caution") {
|
|
442
|
+
toastedKey = undefined;
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (top.key === toastedKey) return;
|
|
446
|
+
toastedKey = top.key;
|
|
447
|
+
context.ui.toast.show({
|
|
448
|
+
title: "Flight Deck",
|
|
449
|
+
message: cautionDetail(top),
|
|
450
|
+
variant: "warning",
|
|
451
|
+
});
|
|
452
|
+
} catch {
|
|
453
|
+
// A warning must never be able to break the panel it warns about.
|
|
454
|
+
}
|
|
455
|
+
}, config.refresh > 0 ? config.refresh : 1_000);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (config.sidebar.enabled) {
|
|
459
|
+
releases.push(
|
|
460
|
+
context.ui.slot({
|
|
461
|
+
append: "sidebar.content",
|
|
462
|
+
render: ({ sessionID }) => {
|
|
463
|
+
// The annunciator is drawn from a timer, so the toast watcher needs
|
|
464
|
+
// to know which session is on screen. Reading the frame is what
|
|
465
|
+
// subscribes this render to the host's tick — without it nothing
|
|
466
|
+
// re-runs between host events, and a stall is precisely the failure
|
|
467
|
+
// that produces no host events at all.
|
|
468
|
+
watchedSession = sessionID;
|
|
469
|
+
// Keep the guard bridge on the rendered session: the follow fetches
|
|
470
|
+
// immediately on change, and the status read below subscribes this
|
|
471
|
+
// render to the bridge's host store, so polls re-run the rail.
|
|
472
|
+
bridge?.follow(sessionID);
|
|
473
|
+
void ticker?.frame;
|
|
474
|
+
void bridge?.status;
|
|
475
|
+
|
|
476
|
+
const source = snapshot(sessionID);
|
|
477
|
+
const lines = sidebarLines(config, source);
|
|
478
|
+
if (lines.length === 0) return null;
|
|
479
|
+
const offset = Math.min(liveRowOffset(config), lines.length);
|
|
480
|
+
|
|
481
|
+
return (
|
|
482
|
+
// No padding here: the host already lays out and pads the sidebar,
|
|
483
|
+
// so adding our own would push the rows out of alignment with it.
|
|
484
|
+
//
|
|
485
|
+
// Every live row gets the same token, the annunciator included. It
|
|
486
|
+
// is the same panel; only the glyph changes.
|
|
487
|
+
<box flexDirection="column">
|
|
488
|
+
{lines.map((line, index) => (
|
|
489
|
+
<text fg={index < offset ? context.theme.text.default : context.theme.text.subdued}>{line}</text>
|
|
490
|
+
))}
|
|
491
|
+
</box>
|
|
492
|
+
);
|
|
493
|
+
},
|
|
494
|
+
}),
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (footer !== undefined) {
|
|
499
|
+
releases.push(
|
|
500
|
+
context.ui.slot({
|
|
501
|
+
append: "prompt.footer.status",
|
|
502
|
+
render: () => <text fg={context.theme.text.subdued}>{footer}</text>,
|
|
503
|
+
}),
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return () => {
|
|
508
|
+
if (watchTimer !== undefined) clearInterval(watchTimer);
|
|
509
|
+
watchTimer = undefined;
|
|
510
|
+
ticker?.dispose();
|
|
511
|
+
bridge?.dispose();
|
|
512
|
+
for (const release of releases) release();
|
|
513
|
+
};
|
|
514
|
+
},
|
|
515
|
+
});
|