oc-flight-deck 0.11.0 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -0
- package/LICENSE +1 -1
- package/README.md +63 -173
- package/dist/index.js +10 -0
- package/dist/tui/index.js +3814 -0
- package/flight-deck.example.jsonc +114 -38
- package/flight-deck.schema.json +122 -10
- package/package.json +11 -9
- package/src/tui/clipboard.ts +44 -15
- package/src/tui/config.ts +358 -22
- package/src/tui/file-config.ts +4 -1
- package/src/tui/format.ts +1 -1
- package/src/tui/go-look.ts +118 -0
- package/src/tui/go-panel.ts +637 -0
- package/src/tui/go-usage.ts +8 -1
- package/src/tui/go.ts +663 -27
- package/src/tui/guard.ts +15 -7
- package/src/tui/host-clipboard.ts +21 -4
- package/src/tui/index.tsx +96 -24
- package/src/tui/rows.ts +71 -24
- package/src/tui/session-elapsed.ts +122 -0
- package/src/tui/session-reads.ts +97 -65
- package/src/tui/session-tps.ts +158 -109
- package/src/tui/stat-fields.ts +99 -5
- package/src/tui/stats.ts +9 -9
- package/src/tui/style.ts +24 -5
- package/src/tui/throughput.ts +48 -113
- package/src/tui/active-elapsed.ts +0 -141
|
@@ -0,0 +1,3814 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/tui/index.tsx
|
|
3
|
+
import { Plugin } from "@opencode/plugin/tui";
|
|
4
|
+
|
|
5
|
+
// src/tui/caution.ts
|
|
6
|
+
function asRecord(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : undefined;
|
|
8
|
+
}
|
|
9
|
+
function asCount(value) {
|
|
10
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
11
|
+
}
|
|
12
|
+
function asText(value) {
|
|
13
|
+
if (typeof value !== "string")
|
|
14
|
+
return;
|
|
15
|
+
const text = value.trim();
|
|
16
|
+
return text.length === 0 ? undefined : text;
|
|
17
|
+
}
|
|
18
|
+
var SETTLED_TOOL = new Set(["completed", "error", "aborted", "cancelled"]);
|
|
19
|
+
function readToolCall(value) {
|
|
20
|
+
const part = asRecord(value);
|
|
21
|
+
if (part === undefined || part["type"] !== "tool")
|
|
22
|
+
return;
|
|
23
|
+
const name = asText(part["name"]);
|
|
24
|
+
if (name === undefined)
|
|
25
|
+
return;
|
|
26
|
+
const state = asRecord(part["state"]);
|
|
27
|
+
const time = asRecord(part["time"]);
|
|
28
|
+
return {
|
|
29
|
+
name,
|
|
30
|
+
status: asText(state?.["status"]) ?? "unknown",
|
|
31
|
+
inputKey: stableKey(state?.["input"]),
|
|
32
|
+
startedAt: asCount(time?.["ran"]) ?? asCount(time?.["created"]),
|
|
33
|
+
settledAt: asCount(time?.["completed"])
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function toolCalls(parts) {
|
|
37
|
+
const calls = [];
|
|
38
|
+
for (const part of parts) {
|
|
39
|
+
const call = readToolCall(part);
|
|
40
|
+
if (call !== undefined)
|
|
41
|
+
calls.push(call);
|
|
42
|
+
}
|
|
43
|
+
return calls;
|
|
44
|
+
}
|
|
45
|
+
function stableKey(value) {
|
|
46
|
+
if (value === undefined)
|
|
47
|
+
return "";
|
|
48
|
+
try {
|
|
49
|
+
return JSON.stringify(sortValue(value)).slice(0, 512);
|
|
50
|
+
} catch {
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function sortValue(value) {
|
|
55
|
+
if (Array.isArray(value))
|
|
56
|
+
return value.map(sortValue);
|
|
57
|
+
const record = asRecord(value);
|
|
58
|
+
if (record === undefined)
|
|
59
|
+
return value;
|
|
60
|
+
const sorted = {};
|
|
61
|
+
for (const key of Object.keys(record).sort())
|
|
62
|
+
sorted[key] = sortValue(record[key]);
|
|
63
|
+
return sorted;
|
|
64
|
+
}
|
|
65
|
+
var SEVERITY_ORDER = { caution: 0, watch: 1 };
|
|
66
|
+
function severityFor(elapsed, cautionMs) {
|
|
67
|
+
return elapsed >= cautionMs ? "caution" : "watch";
|
|
68
|
+
}
|
|
69
|
+
function newestActivity(calls) {
|
|
70
|
+
let newest;
|
|
71
|
+
for (const call of calls) {
|
|
72
|
+
const stamp = call.settledAt ?? call.startedAt;
|
|
73
|
+
if (stamp === undefined)
|
|
74
|
+
continue;
|
|
75
|
+
if (newest === undefined || stamp > newest)
|
|
76
|
+
newest = stamp;
|
|
77
|
+
}
|
|
78
|
+
return newest;
|
|
79
|
+
}
|
|
80
|
+
function detectCautions(input) {
|
|
81
|
+
const { parts, now, sessionRunning, thresholds } = input;
|
|
82
|
+
const calls = toolCalls(parts);
|
|
83
|
+
const shells = input.shells ?? [];
|
|
84
|
+
const found = [];
|
|
85
|
+
const exempt = (name) => thresholds.exemptTools.includes(name);
|
|
86
|
+
for (let index = calls.length - 1;index >= 0; index -= 1) {
|
|
87
|
+
const call = calls[index];
|
|
88
|
+
if (call === undefined || SETTLED_TOOL.has(call.status) || exempt(call.name))
|
|
89
|
+
continue;
|
|
90
|
+
if (call.startedAt === undefined)
|
|
91
|
+
continue;
|
|
92
|
+
const elapsedMs = now - call.startedAt;
|
|
93
|
+
if (elapsedMs < thresholds.toolWatchMs)
|
|
94
|
+
continue;
|
|
95
|
+
found.push({
|
|
96
|
+
kind: "hung-tool",
|
|
97
|
+
severity: severityFor(elapsedMs, thresholds.toolCautionMs),
|
|
98
|
+
tool: call.name,
|
|
99
|
+
status: call.status,
|
|
100
|
+
elapsedMs,
|
|
101
|
+
key: `hung:${call.name}`
|
|
102
|
+
});
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
for (const raw of shells) {
|
|
106
|
+
const shell = asRecord(raw);
|
|
107
|
+
if (shell === undefined || shell["status"] !== "running")
|
|
108
|
+
continue;
|
|
109
|
+
const time = asRecord(shell["time"]);
|
|
110
|
+
const startedAt = asCount(time?.["started"]);
|
|
111
|
+
if (startedAt === undefined)
|
|
112
|
+
continue;
|
|
113
|
+
const elapsedMs = now - startedAt;
|
|
114
|
+
if (elapsedMs < thresholds.toolWatchMs)
|
|
115
|
+
continue;
|
|
116
|
+
if (elapsedMs < (found.find((c) => c.kind === "hung-shell")?.elapsedMs ?? 0))
|
|
117
|
+
continue;
|
|
118
|
+
const command = asText(shell["command"]) ?? "shell";
|
|
119
|
+
const label = command.split(/\s+/)[0] ?? "shell";
|
|
120
|
+
const existing = found.findIndex((c) => c.kind === "hung-shell");
|
|
121
|
+
const caution = {
|
|
122
|
+
kind: "hung-shell",
|
|
123
|
+
severity: severityFor(elapsedMs, thresholds.toolCautionMs),
|
|
124
|
+
tool: label,
|
|
125
|
+
status: "running",
|
|
126
|
+
elapsedMs,
|
|
127
|
+
key: `shell:${label}`
|
|
128
|
+
};
|
|
129
|
+
if (existing >= 0)
|
|
130
|
+
found[existing] = caution;
|
|
131
|
+
else
|
|
132
|
+
found.push(caution);
|
|
133
|
+
}
|
|
134
|
+
const threshold = Math.max(2, Math.floor(thresholds.repeatThreshold));
|
|
135
|
+
if (calls.length >= threshold) {
|
|
136
|
+
const tail = calls.slice(-threshold);
|
|
137
|
+
const first = tail[0];
|
|
138
|
+
if (first !== undefined && !exempt(first.name) && first.inputKey.length > 0 && tail.some((call) => call.status !== "error") && tail.every((call) => call.name === first.name && call.inputKey === first.inputKey)) {
|
|
139
|
+
found.push({
|
|
140
|
+
kind: "repeat-loop",
|
|
141
|
+
severity: "caution",
|
|
142
|
+
tool: first.name,
|
|
143
|
+
repeats: threshold,
|
|
144
|
+
key: `repeat:${first.name}:${first.inputKey.slice(0, 64)}`
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (calls.length >= threshold) {
|
|
149
|
+
const tail = calls.slice(-threshold);
|
|
150
|
+
const first = tail[0];
|
|
151
|
+
if (first !== undefined && !exempt(first.name) && first.inputKey.length > 0 && tail.every((call) => call.name === first.name && call.status === "error" && call.inputKey === first.inputKey)) {
|
|
152
|
+
found.push({
|
|
153
|
+
kind: "failure-loop",
|
|
154
|
+
severity: "caution",
|
|
155
|
+
tool: first.name,
|
|
156
|
+
repeats: threshold,
|
|
157
|
+
key: `failing:${first.name}:${first.inputKey.slice(0, 64)}`
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const hasLiveWork = calls.some((call) => !SETTLED_TOOL.has(call.status)) || shells.some((raw) => asRecord(raw)?.["status"] === "running");
|
|
162
|
+
if (sessionRunning && !hasLiveWork) {
|
|
163
|
+
const settledAt = newestActivity(calls) ?? input.lastMessageAt;
|
|
164
|
+
if (settledAt !== undefined) {
|
|
165
|
+
const elapsedMs = now - settledAt;
|
|
166
|
+
if (elapsedMs >= thresholds.turnWatchMs) {
|
|
167
|
+
found.push({
|
|
168
|
+
kind: "silent-turn",
|
|
169
|
+
severity: severityFor(elapsedMs, thresholds.turnCautionMs),
|
|
170
|
+
elapsedMs,
|
|
171
|
+
key: "silent-turn"
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return found.sort((a, b) => SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity] || b.kind.length - a.kind.length || a.key.localeCompare(b.key));
|
|
177
|
+
}
|
|
178
|
+
function worstCaution(cautions) {
|
|
179
|
+
return [...cautions].sort((a, b) => SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity])[0];
|
|
180
|
+
}
|
|
181
|
+
var WATCH_GLYPH = "\u25B2";
|
|
182
|
+
var CAUTION_GLYPH = "\u25CF";
|
|
183
|
+
var CLEAR_GLYPH = "\u25CB";
|
|
184
|
+
function glyphFor(caution, glyphs = {}) {
|
|
185
|
+
if (caution === undefined)
|
|
186
|
+
return glyphs.clear ?? CLEAR_GLYPH;
|
|
187
|
+
return caution.severity === "caution" ? glyphs.caution ?? CAUTION_GLYPH : glyphs.watch ?? WATCH_GLYPH;
|
|
188
|
+
}
|
|
189
|
+
function cautionBody(caution) {
|
|
190
|
+
const elapsed = caution.elapsedMs === undefined ? "" : ` ${formatElapsed(caution.elapsedMs)}`;
|
|
191
|
+
switch (caution.kind) {
|
|
192
|
+
case "hung-tool": {
|
|
193
|
+
const phase = caution.status === "streaming" ? "streaming" : "running";
|
|
194
|
+
return `${caution.tool ?? "tool"} ${phase}${elapsed}`;
|
|
195
|
+
}
|
|
196
|
+
case "hung-shell":
|
|
197
|
+
return `${caution.tool ?? "shell"} running${elapsed}`;
|
|
198
|
+
case "repeat-loop":
|
|
199
|
+
return `${caution.tool ?? "tool"} \xD7${caution.repeats ?? 0} identical`;
|
|
200
|
+
case "failure-loop":
|
|
201
|
+
return `${caution.tool ?? "tool"} failing \xD7${caution.repeats ?? 0}`;
|
|
202
|
+
case "silent-turn":
|
|
203
|
+
return `no progress${elapsed}`;
|
|
204
|
+
default:
|
|
205
|
+
return "unknown";
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function cautionMark(caution, glyphs = {}) {
|
|
209
|
+
const glyph = glyphFor(caution, glyphs);
|
|
210
|
+
return {
|
|
211
|
+
glyph,
|
|
212
|
+
tone: caution.severity === "caution" ? "error" : "warning",
|
|
213
|
+
text: `${glyph} ${cautionBody(caution)}`
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function cautionDetail(caution) {
|
|
217
|
+
const elapsed = caution.elapsedMs === undefined ? "a while" : formatElapsed(caution.elapsedMs);
|
|
218
|
+
const tool = caution.tool ?? "a tool";
|
|
219
|
+
switch (caution.kind) {
|
|
220
|
+
case "hung-tool":
|
|
221
|
+
if (caution.status === "streaming") {
|
|
222
|
+
return `${tool} has been streaming its arguments for ${elapsed}. Either a very large call, or the model has lost the thread.`;
|
|
223
|
+
}
|
|
224
|
+
return `${tool} has been running for ${elapsed} without returning. Slow, or waiting on something that will never finish.`;
|
|
225
|
+
case "hung-shell":
|
|
226
|
+
return `A shell command has been running for ${elapsed}. A build, or a process that will never exit.`;
|
|
227
|
+
case "repeat-loop":
|
|
228
|
+
return `${tool} was called ${caution.repeats ?? 0} times with identical input. That is usually a loop rather than a retry.`;
|
|
229
|
+
case "failure-loop":
|
|
230
|
+
return `${tool} failed ${caution.repeats ?? 0} times in a row. The agent is retrying something that is not working.`;
|
|
231
|
+
case "silent-turn":
|
|
232
|
+
return `The session is running but nothing has settled for ${elapsed}. Either the model is still thinking, or the turn is wedged.`;
|
|
233
|
+
default:
|
|
234
|
+
return "Something looks wrong, but this build cannot say what.";
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function formatElapsed(ms) {
|
|
238
|
+
const total = Math.max(0, Math.floor(ms / 1000));
|
|
239
|
+
const seconds = total % 60;
|
|
240
|
+
const minutes = Math.floor(total / 60) % 60;
|
|
241
|
+
const hours = Math.floor(total / 3600);
|
|
242
|
+
const ss = String(seconds).padStart(2, "0");
|
|
243
|
+
if (hours > 0)
|
|
244
|
+
return `${hours}h${String(minutes).padStart(2, "0")}m${ss}s`;
|
|
245
|
+
return minutes > 0 ? `${minutes}m${ss}s` : `${seconds}s`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// src/tui/host-clipboard.ts
|
|
249
|
+
function createHostClipboard() {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// src/tui/clipboard.ts
|
|
254
|
+
function dispatchOsc52(renderer, text) {
|
|
255
|
+
if (renderer === undefined || renderer === null)
|
|
256
|
+
return false;
|
|
257
|
+
try {
|
|
258
|
+
const boundary = renderer;
|
|
259
|
+
if (boundary.capabilities?.osc52_support === "unsupported")
|
|
260
|
+
return false;
|
|
261
|
+
if (typeof boundary.copyToClipboardOSC52 !== "function")
|
|
262
|
+
return false;
|
|
263
|
+
return boundary.copyToClipboardOSC52.call(renderer, text) === true;
|
|
264
|
+
} catch {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
async function copyToClipboard(renderer, text, createHost = createHostClipboard) {
|
|
269
|
+
let hostReached = true;
|
|
270
|
+
let hostStatus = "failed";
|
|
271
|
+
let host;
|
|
272
|
+
try {
|
|
273
|
+
host = createHost();
|
|
274
|
+
} catch {
|
|
275
|
+
host = undefined;
|
|
276
|
+
}
|
|
277
|
+
if (host === undefined) {
|
|
278
|
+
hostReached = false;
|
|
279
|
+
} else {
|
|
280
|
+
try {
|
|
281
|
+
const result = await host.writeText(text);
|
|
282
|
+
hostStatus = result.status;
|
|
283
|
+
} catch {
|
|
284
|
+
hostStatus = "failed";
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (hostStatus === "written")
|
|
288
|
+
return { verified: true, outcome: "written" };
|
|
289
|
+
if (dispatchOsc52(renderer, text))
|
|
290
|
+
return { verified: false, outcome: "osc52-dispatched" };
|
|
291
|
+
if (!hostReached)
|
|
292
|
+
return { verified: false, outcome: "no-backend" };
|
|
293
|
+
const outcome = hostStatus === "unsupported" || hostStatus === "cancelled" || hostStatus === "timed-out" || hostStatus === "failed" ? hostStatus : "failed";
|
|
294
|
+
return { verified: false, outcome };
|
|
295
|
+
}
|
|
296
|
+
function copyFeedback(result, id) {
|
|
297
|
+
if (result.verified) {
|
|
298
|
+
return { ok: true, variant: "success", message: `Copied the full session id ${id} to the clipboard.` };
|
|
299
|
+
}
|
|
300
|
+
switch (result.outcome) {
|
|
301
|
+
case "osc52-dispatched":
|
|
302
|
+
return {
|
|
303
|
+
ok: false,
|
|
304
|
+
variant: "error",
|
|
305
|
+
message: "Sent the session id to the terminal, but could not confirm it was copied."
|
|
306
|
+
};
|
|
307
|
+
case "unsupported":
|
|
308
|
+
return { ok: false, variant: "error", message: "This terminal cannot copy to the clipboard." };
|
|
309
|
+
case "cancelled":
|
|
310
|
+
return { ok: false, variant: "error", message: "The clipboard copy was cancelled." };
|
|
311
|
+
case "timed-out":
|
|
312
|
+
return { ok: false, variant: "error", message: "The clipboard copy timed out." };
|
|
313
|
+
case "failed":
|
|
314
|
+
return { ok: false, variant: "error", message: "Could not copy the session id to the clipboard." };
|
|
315
|
+
case "no-backend":
|
|
316
|
+
default:
|
|
317
|
+
return { ok: false, variant: "error", message: "No clipboard is available to copy the session id." };
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// src/tui/format.ts
|
|
322
|
+
function formatCount(value) {
|
|
323
|
+
if (value < 1000)
|
|
324
|
+
return String(Math.round(value));
|
|
325
|
+
const thousands = Math.round(value / 1000);
|
|
326
|
+
if (thousands < 1000)
|
|
327
|
+
return `${thousands}k`;
|
|
328
|
+
const millions = (value / 1e6).toFixed(1);
|
|
329
|
+
return `${millions.endsWith(".0") ? millions.slice(0, -2) : millions}M`;
|
|
330
|
+
}
|
|
331
|
+
function formatCost(value) {
|
|
332
|
+
return `$${value.toFixed(value < 1 ? 3 : 2)}`;
|
|
333
|
+
}
|
|
334
|
+
function formatDuration(ms, style = "compact") {
|
|
335
|
+
const total = Math.max(0, Math.floor(ms / 1000));
|
|
336
|
+
const seconds = total % 60;
|
|
337
|
+
const minutes = Math.floor(total / 60) % 60;
|
|
338
|
+
const hours = Math.floor(total / 3600);
|
|
339
|
+
const ss = String(seconds).padStart(2, "0");
|
|
340
|
+
const join = style === "spaced" ? " " : "";
|
|
341
|
+
if (hours > 0)
|
|
342
|
+
return `${hours}h${join}${String(minutes).padStart(2, "0")}m${join}${ss}s`;
|
|
343
|
+
return minutes > 0 ? `${minutes}m${join}${ss}s` : `${seconds}s`;
|
|
344
|
+
}
|
|
345
|
+
var DEFAULT_BAR_WIDTH = 10;
|
|
346
|
+
function fuelBar(ratio, width = DEFAULT_BAR_WIDTH) {
|
|
347
|
+
const cells = Math.max(1, Math.floor(width));
|
|
348
|
+
const filled = Math.max(0, Math.min(cells, Math.round(ratio * cells)));
|
|
349
|
+
return `${"\u2588".repeat(filled)}${"\u2591".repeat(cells - filled)}`;
|
|
350
|
+
}
|
|
351
|
+
var SPARK_LEVELS = ["\u2581", "\u2582", "\u2583", "\u2584", "\u2585", "\u2586", "\u2587", "\u2588"];
|
|
352
|
+
function cellWidthOf(code) {
|
|
353
|
+
if (code >= 4352 && code <= 4447 || code === 9001 || code === 9002 || code >= 11904 && code <= 42191 || code >= 44032 && code <= 55203 || code >= 63744 && code <= 64255 || code >= 65040 && code <= 65049 || code >= 65072 && code <= 65103 || code >= 65280 && code <= 65376 || code >= 65504 && code <= 65510 || code >= 131072 && code <= 262141 || code >= 9728 && code <= 10175 || code >= 11008 && code <= 11263 || code >= 126976 && code <= 129791) {
|
|
354
|
+
return 2;
|
|
355
|
+
}
|
|
356
|
+
return 1;
|
|
357
|
+
}
|
|
358
|
+
function cellWidth(text) {
|
|
359
|
+
let width = 0;
|
|
360
|
+
for (const char of text)
|
|
361
|
+
width += cellWidthOf(char.codePointAt(0) ?? 0);
|
|
362
|
+
return width;
|
|
363
|
+
}
|
|
364
|
+
function clip(text, max) {
|
|
365
|
+
const ellipsis = String.fromCharCode(8230);
|
|
366
|
+
if (cellWidth(text) <= max)
|
|
367
|
+
return text;
|
|
368
|
+
const budget = max - 1;
|
|
369
|
+
let kept = "";
|
|
370
|
+
let width = 0;
|
|
371
|
+
for (const char of text) {
|
|
372
|
+
const next = width + cellWidthOf(char.codePointAt(0) ?? 0);
|
|
373
|
+
if (kept !== "" && next > budget)
|
|
374
|
+
break;
|
|
375
|
+
kept += char;
|
|
376
|
+
width = next;
|
|
377
|
+
}
|
|
378
|
+
return `${kept}${ellipsis}`;
|
|
379
|
+
}
|
|
380
|
+
function sparkline(values) {
|
|
381
|
+
if (values.length === 0)
|
|
382
|
+
return "";
|
|
383
|
+
const max = Math.max(...values);
|
|
384
|
+
if (!Number.isFinite(max) || max <= 0)
|
|
385
|
+
return SPARK_LEVELS[0].repeat(values.length);
|
|
386
|
+
return values.map((value) => {
|
|
387
|
+
const level = Math.floor(Math.max(0, value) / max * (SPARK_LEVELS.length - 1));
|
|
388
|
+
return SPARK_LEVELS[Math.min(SPARK_LEVELS.length - 1, level)];
|
|
389
|
+
}).join("");
|
|
390
|
+
}
|
|
391
|
+
// src/tui/coerce.ts
|
|
392
|
+
function asCount2(value) {
|
|
393
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
|
|
394
|
+
}
|
|
395
|
+
function asText2(value) {
|
|
396
|
+
if (typeof value !== "string")
|
|
397
|
+
return;
|
|
398
|
+
const text = value.trim();
|
|
399
|
+
return text.length === 0 ? undefined : text;
|
|
400
|
+
}
|
|
401
|
+
function asRecord2(value) {
|
|
402
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : undefined;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// src/tui/go-usage.ts
|
|
406
|
+
var WINDOW_ORDER = ["5h", "1w", "1m"];
|
|
407
|
+
function asFiniteNumber(value) {
|
|
408
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
409
|
+
}
|
|
410
|
+
function firstFinite(...candidates) {
|
|
411
|
+
for (const candidate of candidates) {
|
|
412
|
+
const value = asFiniteNumber(candidate);
|
|
413
|
+
if (value !== undefined)
|
|
414
|
+
return value;
|
|
415
|
+
}
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
function asInstantMs(value) {
|
|
419
|
+
const numeric = asFiniteNumber(value);
|
|
420
|
+
if (numeric !== undefined)
|
|
421
|
+
return numeric;
|
|
422
|
+
const text = asText2(value);
|
|
423
|
+
if (text === undefined)
|
|
424
|
+
return;
|
|
425
|
+
const parsed = Date.parse(text);
|
|
426
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
427
|
+
}
|
|
428
|
+
function firstInstant(...candidates) {
|
|
429
|
+
for (const candidate of candidates) {
|
|
430
|
+
const value = asInstantMs(candidate);
|
|
431
|
+
if (value !== undefined)
|
|
432
|
+
return value;
|
|
433
|
+
}
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
function ratioFromPercent(value) {
|
|
437
|
+
const percent = asFiniteNumber(value);
|
|
438
|
+
if (percent === undefined || percent < 0)
|
|
439
|
+
return;
|
|
440
|
+
return Math.min(1, percent / 100);
|
|
441
|
+
}
|
|
442
|
+
function windowIdFrom(value) {
|
|
443
|
+
const text = asText2(value);
|
|
444
|
+
if (text === undefined)
|
|
445
|
+
return;
|
|
446
|
+
const key = text.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
447
|
+
if (key.startsWith("rolling") || key === "5h" || key === "5hour" || key === "session")
|
|
448
|
+
return "5h";
|
|
449
|
+
if (key.startsWith("weekly") || key === "1w" || key === "week" || key === "7d")
|
|
450
|
+
return "1w";
|
|
451
|
+
if (key.startsWith("monthly") || key === "1m" || key === "month" || key === "30d")
|
|
452
|
+
return "1m";
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
function ratioOf(used, limit) {
|
|
456
|
+
if (used === undefined || limit === undefined || limit <= 0)
|
|
457
|
+
return;
|
|
458
|
+
return used / limit;
|
|
459
|
+
}
|
|
460
|
+
function buildWindow(id, used, limit, resetAtMs, explicitRatio, status) {
|
|
461
|
+
if (id === undefined)
|
|
462
|
+
return;
|
|
463
|
+
const ratio = explicitRatio ?? ratioOf(used, limit);
|
|
464
|
+
if (used === undefined && limit === undefined && ratio === undefined)
|
|
465
|
+
return;
|
|
466
|
+
const result = { id };
|
|
467
|
+
if (used !== undefined)
|
|
468
|
+
result.used = used;
|
|
469
|
+
if (limit !== undefined)
|
|
470
|
+
result.limit = limit;
|
|
471
|
+
if (ratio !== undefined)
|
|
472
|
+
result.ratio = ratio;
|
|
473
|
+
if (resetAtMs !== undefined)
|
|
474
|
+
result.resetAtMs = resetAtMs;
|
|
475
|
+
if (status !== undefined)
|
|
476
|
+
result.status = status;
|
|
477
|
+
return result;
|
|
478
|
+
}
|
|
479
|
+
function normalizeWindowValue(value, idHint) {
|
|
480
|
+
if (typeof value === "number") {
|
|
481
|
+
return buildWindow(idHint, asFiniteNumber(value), undefined, undefined);
|
|
482
|
+
}
|
|
483
|
+
const record = asRecord2(value);
|
|
484
|
+
if (record === undefined)
|
|
485
|
+
return;
|
|
486
|
+
const id = idHint ?? windowIdFrom(record.window) ?? windowIdFrom(record.id) ?? windowIdFrom(record.name);
|
|
487
|
+
const used = firstFinite(record.usage, record.used, record.value, record.current, record.count);
|
|
488
|
+
const limit = firstFinite(record.limit, record.max, record.total, record.cap, record.quota);
|
|
489
|
+
const resetAtMs = firstInstant(record.resetAtMs, record.resetsAt, record.resetAt);
|
|
490
|
+
const ratio = ratioFromPercent(record.percent);
|
|
491
|
+
const status = asText2(record.status);
|
|
492
|
+
return buildWindow(id, used, limit, resetAtMs, ratio, status);
|
|
493
|
+
}
|
|
494
|
+
function normalizeFlatWindow(top, id, usedKey, limitKey, prefix) {
|
|
495
|
+
const used = asFiniteNumber(top[usedKey]);
|
|
496
|
+
const limit = asFiniteNumber(top[limitKey]);
|
|
497
|
+
const resetAtMs = firstInstant(top[`${prefix}ResetAtMs`], top[`${prefix}ResetsAt`], top[`${prefix}ResetAt`]);
|
|
498
|
+
return buildWindow(id, used, limit, resetAtMs);
|
|
499
|
+
}
|
|
500
|
+
function collectKeyed(record, push) {
|
|
501
|
+
for (const [key, value] of Object.entries(record)) {
|
|
502
|
+
const id = windowIdFrom(key);
|
|
503
|
+
if (id === undefined)
|
|
504
|
+
continue;
|
|
505
|
+
push(normalizeWindowValue(value, id));
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
function normalizeGoUsage(raw) {
|
|
509
|
+
try {
|
|
510
|
+
const windows = [];
|
|
511
|
+
const seen = new Set;
|
|
512
|
+
const push = (window) => {
|
|
513
|
+
if (window === undefined || seen.has(window.id))
|
|
514
|
+
return;
|
|
515
|
+
seen.add(window.id);
|
|
516
|
+
windows.push(window);
|
|
517
|
+
};
|
|
518
|
+
if (Array.isArray(raw)) {
|
|
519
|
+
for (const entry of raw)
|
|
520
|
+
push(normalizeWindowValue(entry));
|
|
521
|
+
} else {
|
|
522
|
+
const top = asRecord2(raw);
|
|
523
|
+
if (top === undefined)
|
|
524
|
+
return;
|
|
525
|
+
push(normalizeFlatWindow(top, "5h", "rollingUsage", "rollingLimit", "rolling"));
|
|
526
|
+
push(normalizeFlatWindow(top, "1w", "weeklyUsage", "weeklyLimit", "weekly"));
|
|
527
|
+
push(normalizeFlatWindow(top, "1m", "monthlyUsage", "monthlyLimit", "monthly"));
|
|
528
|
+
const usage = asRecord2(top.usage);
|
|
529
|
+
if (usage !== undefined)
|
|
530
|
+
collectKeyed(usage, push);
|
|
531
|
+
const list = top.windows;
|
|
532
|
+
if (Array.isArray(list)) {
|
|
533
|
+
for (const entry of list)
|
|
534
|
+
push(normalizeWindowValue(entry));
|
|
535
|
+
} else {
|
|
536
|
+
const keyed = asRecord2(list);
|
|
537
|
+
if (keyed !== undefined)
|
|
538
|
+
collectKeyed(keyed, push);
|
|
539
|
+
}
|
|
540
|
+
if (windows.length === 0)
|
|
541
|
+
push(normalizeWindowValue(top));
|
|
542
|
+
}
|
|
543
|
+
if (windows.length === 0)
|
|
544
|
+
return;
|
|
545
|
+
windows.sort((a, b) => WINDOW_ORDER.indexOf(a.id) - WINDOW_ORDER.indexOf(b.id));
|
|
546
|
+
return { windows };
|
|
547
|
+
} catch {
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
var GO_ERROR_RATIO = 0.9;
|
|
552
|
+
var GO_OK_STATUSES = ["ok"];
|
|
553
|
+
function goTone(window) {
|
|
554
|
+
if (window.ratio !== undefined && window.ratio >= GO_ERROR_RATIO)
|
|
555
|
+
return "error";
|
|
556
|
+
const status = window.status;
|
|
557
|
+
if (status !== undefined && status !== "" && !GO_OK_STATUSES.includes(status))
|
|
558
|
+
return "error";
|
|
559
|
+
return "normal";
|
|
560
|
+
}
|
|
561
|
+
function formatRemaining(ms) {
|
|
562
|
+
const seconds = Math.ceil(ms / 1000);
|
|
563
|
+
if (seconds < 60)
|
|
564
|
+
return `${seconds}s`;
|
|
565
|
+
const minutes = Math.ceil(ms / 60000);
|
|
566
|
+
if (minutes < 60)
|
|
567
|
+
return `${minutes}m`;
|
|
568
|
+
const hours = Math.ceil(ms / 3600000);
|
|
569
|
+
if (hours < 24)
|
|
570
|
+
return `${hours}h`;
|
|
571
|
+
return `${Math.ceil(ms / 86400000)}d`;
|
|
572
|
+
}
|
|
573
|
+
function goResetSuffix(window, nowMs) {
|
|
574
|
+
if (goTone(window) !== "error")
|
|
575
|
+
return;
|
|
576
|
+
const resetAtMs = window.resetAtMs;
|
|
577
|
+
if (resetAtMs === undefined || !Number.isFinite(resetAtMs) || !Number.isFinite(nowMs))
|
|
578
|
+
return;
|
|
579
|
+
const remaining = resetAtMs - nowMs;
|
|
580
|
+
if (remaining <= 0)
|
|
581
|
+
return;
|
|
582
|
+
return ` \xB7 ${formatRemaining(remaining)}`;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// src/tui/guard-tokens.ts
|
|
586
|
+
function asGuardCount(value) {
|
|
587
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0)
|
|
588
|
+
return Math.floor(value);
|
|
589
|
+
if (Array.isArray(value))
|
|
590
|
+
return value.length;
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
function guardCountOrZero(...candidates) {
|
|
594
|
+
for (const candidate of candidates) {
|
|
595
|
+
const count = asGuardCount(candidate);
|
|
596
|
+
if (count !== undefined)
|
|
597
|
+
return count;
|
|
598
|
+
}
|
|
599
|
+
return 0;
|
|
600
|
+
}
|
|
601
|
+
function guardToken(value) {
|
|
602
|
+
try {
|
|
603
|
+
const top = asRecord2(value);
|
|
604
|
+
if (top === undefined)
|
|
605
|
+
return;
|
|
606
|
+
const air = asRecord2(top.airworthiness);
|
|
607
|
+
const war = asRecord2(top.warden);
|
|
608
|
+
const plan = asRecord2(top.flightPlan);
|
|
609
|
+
const airUsable = air !== undefined && (air.available === true || air.available === false);
|
|
610
|
+
const warUsable = war !== undefined && (war.available === true || war.available === false);
|
|
611
|
+
const planUsable = plan !== undefined && (plan.available === true || plan.available === false);
|
|
612
|
+
if (!airUsable && !warUsable && !planUsable)
|
|
613
|
+
return;
|
|
614
|
+
if (airUsable && air.available === false || warUsable && war.available === false || planUsable && plan.available === false) {
|
|
615
|
+
return "unknown";
|
|
616
|
+
}
|
|
617
|
+
const breaches = warUsable ? guardCountOrZero(war.breaches) : 0;
|
|
618
|
+
const orphans = warUsable ? guardCountOrZero(war.orphans) : 0;
|
|
619
|
+
const findings = airUsable ? guardCountOrZero(air.findings, air.counts) : 0;
|
|
620
|
+
if (breaches > 0)
|
|
621
|
+
return `${formatCount(breaches)} breach`;
|
|
622
|
+
if (orphans > 0)
|
|
623
|
+
return `${formatCount(orphans)} orphan`;
|
|
624
|
+
if (findings > 0)
|
|
625
|
+
return `${formatCount(findings)} finding`;
|
|
626
|
+
return "ok";
|
|
627
|
+
} catch {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// src/tui/stat-fields.ts
|
|
633
|
+
var STAT_FIELDS = [
|
|
634
|
+
"caution",
|
|
635
|
+
"status",
|
|
636
|
+
"agent",
|
|
637
|
+
"model",
|
|
638
|
+
"branch",
|
|
639
|
+
"cost",
|
|
640
|
+
"total",
|
|
641
|
+
"project",
|
|
642
|
+
"tokens",
|
|
643
|
+
"cache",
|
|
644
|
+
"context",
|
|
645
|
+
"perms",
|
|
646
|
+
"elapsed",
|
|
647
|
+
"tps",
|
|
648
|
+
"spark",
|
|
649
|
+
"reasoning",
|
|
650
|
+
"turns",
|
|
651
|
+
"guard",
|
|
652
|
+
"go",
|
|
653
|
+
"ses"
|
|
654
|
+
];
|
|
655
|
+
var ANIMATED_FIELDS = ["caution", "status", "elapsed", "tps"];
|
|
656
|
+
function isStatField(value) {
|
|
657
|
+
return value === "session" || STAT_FIELDS.some((field) => field === value);
|
|
658
|
+
}
|
|
659
|
+
function canonicalField(name) {
|
|
660
|
+
return name === "session" ? "ses" : name;
|
|
661
|
+
}
|
|
662
|
+
function fieldLabel(field, labels) {
|
|
663
|
+
const override = labels?.[canonicalField(field)];
|
|
664
|
+
if (override !== undefined)
|
|
665
|
+
return override;
|
|
666
|
+
return field === "ses" ? "session" : field;
|
|
667
|
+
}
|
|
668
|
+
var DEFAULT_PLACEHOLDER = "\u2014";
|
|
669
|
+
var DEFAULT_LABEL_WIDTH = 10;
|
|
670
|
+
var DEFAULT_SPARK_WIDTH = 12;
|
|
671
|
+
function skeletonValue(field, layout) {
|
|
672
|
+
switch (canonicalField(field)) {
|
|
673
|
+
case "cost":
|
|
674
|
+
case "total":
|
|
675
|
+
case "project":
|
|
676
|
+
return formatCost(0);
|
|
677
|
+
case "tokens":
|
|
678
|
+
return "0 in \xB7 0 out";
|
|
679
|
+
case "cache":
|
|
680
|
+
return "0 read";
|
|
681
|
+
case "context":
|
|
682
|
+
return `${fuelBar(0, layout.barWidth ?? DEFAULT_BAR_WIDTH)} ~0%`;
|
|
683
|
+
case "perms":
|
|
684
|
+
return "0 waiting";
|
|
685
|
+
case "elapsed":
|
|
686
|
+
return formatDuration(0, layout.durationStyle);
|
|
687
|
+
case "tps":
|
|
688
|
+
return "0 tok/s";
|
|
689
|
+
case "spark":
|
|
690
|
+
return "\u2581".repeat(Math.max(0, Math.floor(layout.sparkWidth ?? DEFAULT_SPARK_WIDTH)));
|
|
691
|
+
case "reasoning":
|
|
692
|
+
return formatCount(0);
|
|
693
|
+
case "turns":
|
|
694
|
+
return "0";
|
|
695
|
+
case "go":
|
|
696
|
+
return "\u25CB 0 \u25CB 0 \u25CB 0";
|
|
697
|
+
case "status":
|
|
698
|
+
return "\u25CB idle";
|
|
699
|
+
case "agent":
|
|
700
|
+
case "model":
|
|
701
|
+
case "ses":
|
|
702
|
+
return "none";
|
|
703
|
+
case "branch":
|
|
704
|
+
return "no branch";
|
|
705
|
+
case "guard":
|
|
706
|
+
return "unknown";
|
|
707
|
+
default:
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// src/tui/rows.ts
|
|
713
|
+
var SPINNER = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
|
|
714
|
+
var SES_PREVIEW_WIDTH = 18;
|
|
715
|
+
var CONTROL_CHARS = /[\u0000-\u001F\u007F-\u009F]/g;
|
|
716
|
+
function plain(value) {
|
|
717
|
+
return value.replace(CONTROL_CHARS, " ");
|
|
718
|
+
}
|
|
719
|
+
function formatRow(label, value, labelWidth) {
|
|
720
|
+
return `${labelPrefix(label, labelWidth)}${plain(value)}`;
|
|
721
|
+
}
|
|
722
|
+
function labelPrefix(label, labelWidth) {
|
|
723
|
+
return `${label.padEnd(labelWidth)}${label.length >= labelWidth ? " " : ""}`;
|
|
724
|
+
}
|
|
725
|
+
var GO_ORDER = ["5h", "1w", "1m"];
|
|
726
|
+
var DIAL_STEPS = [
|
|
727
|
+
[13, "\u25CB"],
|
|
728
|
+
[38, "\u25D4"],
|
|
729
|
+
[63, "\u25D1"],
|
|
730
|
+
[88, "\u25D5"],
|
|
731
|
+
[101, "\u25CF"]
|
|
732
|
+
];
|
|
733
|
+
function dialGlyph(percent) {
|
|
734
|
+
for (const [limit, glyph] of DIAL_STEPS) {
|
|
735
|
+
if (percent < limit)
|
|
736
|
+
return glyph;
|
|
737
|
+
}
|
|
738
|
+
return "\u25CF";
|
|
739
|
+
}
|
|
740
|
+
function asGoWindows(value) {
|
|
741
|
+
const list = asRecord2(value)?.windows;
|
|
742
|
+
if (!Array.isArray(list))
|
|
743
|
+
return [];
|
|
744
|
+
const windows = [];
|
|
745
|
+
for (const entry of list) {
|
|
746
|
+
const window = asRecord2(entry);
|
|
747
|
+
if (window === undefined)
|
|
748
|
+
continue;
|
|
749
|
+
const id = asText2(window["id"]);
|
|
750
|
+
if (id !== "5h" && id !== "1w" && id !== "1m")
|
|
751
|
+
continue;
|
|
752
|
+
const ratio = asCount2(window["ratio"]);
|
|
753
|
+
if (ratio === undefined)
|
|
754
|
+
continue;
|
|
755
|
+
const result = { id, ratio: Math.min(1, ratio) };
|
|
756
|
+
const resetAtMs = asCount2(window["resetAtMs"]);
|
|
757
|
+
if (resetAtMs !== undefined)
|
|
758
|
+
result.resetAtMs = resetAtMs;
|
|
759
|
+
const status = asText2(window["status"]);
|
|
760
|
+
if (status !== undefined)
|
|
761
|
+
result.status = status;
|
|
762
|
+
windows.push(result);
|
|
763
|
+
}
|
|
764
|
+
windows.sort((a, b) => GO_ORDER.indexOf(a.id) - GO_ORDER.indexOf(b.id));
|
|
765
|
+
return windows;
|
|
766
|
+
}
|
|
767
|
+
function goValueSegments(source, layout) {
|
|
768
|
+
const windows = asGoWindows(source.go);
|
|
769
|
+
if (windows.length === 0)
|
|
770
|
+
return;
|
|
771
|
+
const nowMs = layout.nowMs ?? Date.now();
|
|
772
|
+
let flagged = -1;
|
|
773
|
+
let flaggedRatio = Number.NEGATIVE_INFINITY;
|
|
774
|
+
for (const [index, window] of windows.entries()) {
|
|
775
|
+
if (goTone(window) !== "error")
|
|
776
|
+
continue;
|
|
777
|
+
const ratio = window.ratio ?? 0;
|
|
778
|
+
if (flagged < 0 || ratio > flaggedRatio) {
|
|
779
|
+
flagged = index;
|
|
780
|
+
flaggedRatio = ratio;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
const chunks = [];
|
|
784
|
+
for (const [index, window] of windows.entries()) {
|
|
785
|
+
const percent = Math.round((window.ratio ?? 0) * 100);
|
|
786
|
+
const tone = goTone(window) === "error" ? "error" : undefined;
|
|
787
|
+
const chunk = [{ text: `${dialGlyph(percent)} ${percent}`, tone }];
|
|
788
|
+
const suffix = index === flagged ? goResetSuffix(window, nowMs) : undefined;
|
|
789
|
+
if (suffix !== undefined)
|
|
790
|
+
chunk.push({ text: suffix, tone });
|
|
791
|
+
chunks.push(chunk);
|
|
792
|
+
}
|
|
793
|
+
const segments = [];
|
|
794
|
+
chunks.forEach((chunk, index) => {
|
|
795
|
+
if (index > 0)
|
|
796
|
+
segments.push({ text: " " });
|
|
797
|
+
segments.push(...chunk);
|
|
798
|
+
});
|
|
799
|
+
return segments;
|
|
800
|
+
}
|
|
801
|
+
function asCautionMark(value) {
|
|
802
|
+
const record = asRecord2(value);
|
|
803
|
+
if (record === undefined)
|
|
804
|
+
return;
|
|
805
|
+
const glyph = asText2(record["glyph"]);
|
|
806
|
+
const text = asText2(record["text"]);
|
|
807
|
+
const tone = record["tone"];
|
|
808
|
+
if (glyph === undefined || text === undefined)
|
|
809
|
+
return;
|
|
810
|
+
if (tone !== "warning" && tone !== "error")
|
|
811
|
+
return;
|
|
812
|
+
return { glyph, tone, text };
|
|
813
|
+
}
|
|
814
|
+
function cautionValueSegments(source) {
|
|
815
|
+
const mark = asCautionMark(source.caution);
|
|
816
|
+
if (mark !== undefined) {
|
|
817
|
+
const text = plain(mark.text);
|
|
818
|
+
return [
|
|
819
|
+
{ text: text.slice(0, mark.glyph.length), tone: mark.tone },
|
|
820
|
+
{ text: text.slice(mark.glyph.length) }
|
|
821
|
+
];
|
|
822
|
+
}
|
|
823
|
+
const text = asText2(source.caution);
|
|
824
|
+
return text === undefined ? undefined : [{ text: plain(text) }];
|
|
825
|
+
}
|
|
826
|
+
function statLine(field, source, layout = {}) {
|
|
827
|
+
const labelWidth = layout.labelWidth ?? DEFAULT_LABEL_WIDTH;
|
|
828
|
+
const barWidth = layout.barWidth ?? DEFAULT_BAR_WIDTH;
|
|
829
|
+
const sparkWidth = layout.sparkWidth ?? DEFAULT_SPARK_WIDTH;
|
|
830
|
+
const row = (label, value) => formatRow(fieldLabel(label, layout.labels), value, labelWidth);
|
|
831
|
+
switch (canonicalField(field)) {
|
|
832
|
+
case "caution": {
|
|
833
|
+
const value = cautionValueSegments(source);
|
|
834
|
+
return value === undefined ? undefined : row("caution", value.map((segment) => segment.text).join(""));
|
|
835
|
+
}
|
|
836
|
+
case "status": {
|
|
837
|
+
const status = asText2(source.status);
|
|
838
|
+
if (status === undefined && source.busy === undefined)
|
|
839
|
+
return;
|
|
840
|
+
if (source.busy !== true && status !== "running")
|
|
841
|
+
return row("status", "\u25CB idle");
|
|
842
|
+
const frame = Math.floor(asCount2(source.frame) ?? 0);
|
|
843
|
+
return row("status", `${SPINNER[frame % SPINNER.length]} running`);
|
|
844
|
+
}
|
|
845
|
+
case "agent": {
|
|
846
|
+
const agent = asText2(source.agent);
|
|
847
|
+
return agent === undefined ? undefined : row("agent", agent);
|
|
848
|
+
}
|
|
849
|
+
case "model": {
|
|
850
|
+
const model = asRecord2(source.model);
|
|
851
|
+
const id = asText2(model?.id);
|
|
852
|
+
if (id === undefined)
|
|
853
|
+
return;
|
|
854
|
+
const variant = asText2(model?.variant);
|
|
855
|
+
return row("model", variant === undefined ? id : `${id} \xB7 ${variant}`);
|
|
856
|
+
}
|
|
857
|
+
case "branch": {
|
|
858
|
+
const branch = asText2(source.branch);
|
|
859
|
+
return branch === undefined ? undefined : row("branch", branch);
|
|
860
|
+
}
|
|
861
|
+
case "cost": {
|
|
862
|
+
const cost = asCount2(source.cost);
|
|
863
|
+
const tree = asRecord2(source.tree);
|
|
864
|
+
const treeCost = asCount2(tree?.cost);
|
|
865
|
+
const count = asCount2(tree?.count);
|
|
866
|
+
const family = count !== undefined && count > 0 ? count : 0;
|
|
867
|
+
const merge = layout.hasTotalRow !== true && cost !== undefined && treeCost !== undefined && family > 0 && treeCost > cost;
|
|
868
|
+
if (!merge) {
|
|
869
|
+
const value = layout.hasTotalRow === true ? cost ?? treeCost : treeCost ?? cost;
|
|
870
|
+
return value === undefined ? undefined : row("cost", formatCost(value));
|
|
871
|
+
}
|
|
872
|
+
const plural = family === 1 ? "subagent" : "subagents";
|
|
873
|
+
return row("cost", `${formatCost(treeCost)} \xB7 ${family} ${plural}`);
|
|
874
|
+
}
|
|
875
|
+
case "total": {
|
|
876
|
+
const tree = asRecord2(source.tree);
|
|
877
|
+
if (tree === undefined)
|
|
878
|
+
return;
|
|
879
|
+
const cost = asCount2(tree.cost);
|
|
880
|
+
if (cost === undefined)
|
|
881
|
+
return;
|
|
882
|
+
const count = asCount2(tree.count) ?? 0;
|
|
883
|
+
const plural = count === 1 ? "subagent" : "subagents";
|
|
884
|
+
return row("total", `${formatCost(cost)} \xB7 ${count} ${plural}`);
|
|
885
|
+
}
|
|
886
|
+
case "project": {
|
|
887
|
+
const project = asRecord2(source.project);
|
|
888
|
+
const cost = asCount2(project?.cost);
|
|
889
|
+
if (cost === undefined)
|
|
890
|
+
return;
|
|
891
|
+
const count = asCount2(project?.count);
|
|
892
|
+
return row("project", `${formatCost(cost)}${count !== undefined && count > 1 ? ` \xB7 ${count} sessions` : ""}`);
|
|
893
|
+
}
|
|
894
|
+
case "tokens": {
|
|
895
|
+
const tokens = asRecord2(source.tokens);
|
|
896
|
+
const input = asCount2(tokens?.input);
|
|
897
|
+
const output = asCount2(tokens?.output);
|
|
898
|
+
if (input === undefined && output === undefined)
|
|
899
|
+
return;
|
|
900
|
+
return row("tokens", `${formatCount(input ?? 0)} in \xB7 ${formatCount(output ?? 0)} out`);
|
|
901
|
+
}
|
|
902
|
+
case "cache": {
|
|
903
|
+
const tokens = asRecord2(source.tokens);
|
|
904
|
+
if (tokens === undefined)
|
|
905
|
+
return;
|
|
906
|
+
const cache = asRecord2(tokens.cache);
|
|
907
|
+
const read = asCount2(cache?.read);
|
|
908
|
+
if (read === undefined)
|
|
909
|
+
return;
|
|
910
|
+
const input = asCount2(tokens?.input);
|
|
911
|
+
const total = input === undefined ? undefined : read + input;
|
|
912
|
+
if (read === 0) {
|
|
913
|
+
return total === undefined || total === 0 ? row("cache", `0 read`) : row("cache", `0% hit \xB7 0 read`);
|
|
914
|
+
}
|
|
915
|
+
const value = total === undefined || total === 0 ? `${formatCount(read)} read` : `${Math.round(read / total * 100)}% hit \xB7 ${formatCount(read)} read`;
|
|
916
|
+
return row("cache", value);
|
|
917
|
+
}
|
|
918
|
+
case "context": {
|
|
919
|
+
const context = asRecord2(source.context);
|
|
920
|
+
const used = asCount2(context?.used);
|
|
921
|
+
if (used === undefined || used === 0)
|
|
922
|
+
return;
|
|
923
|
+
const limit = asCount2(context?.limit);
|
|
924
|
+
if (limit === undefined || limit === 0)
|
|
925
|
+
return row("context", `~${formatCount(used)} used`);
|
|
926
|
+
const ratio = used / limit;
|
|
927
|
+
return row("context", `${fuelBar(ratio, barWidth)} ~${Math.round(Math.min(1, ratio) * 100)}%`);
|
|
928
|
+
}
|
|
929
|
+
case "perms": {
|
|
930
|
+
const perms = asRecord2(source.perms);
|
|
931
|
+
const count = asCount2(perms?.["count"]);
|
|
932
|
+
if (count === undefined || count === 0)
|
|
933
|
+
return;
|
|
934
|
+
const action = asText2(perms?.["action"]);
|
|
935
|
+
const resource = asText2(perms?.["resource"]);
|
|
936
|
+
if (count > 1) {
|
|
937
|
+
return row("perms", action === undefined ? `${count} waiting` : `${count} waiting \xB7 ${action}`);
|
|
938
|
+
}
|
|
939
|
+
if (action === undefined)
|
|
940
|
+
return row("perms", "1 waiting");
|
|
941
|
+
return row("perms", resource === undefined ? action : `${action} \xB7 ${clip(resource, 18)}`);
|
|
942
|
+
}
|
|
943
|
+
case "elapsed": {
|
|
944
|
+
const ms = asCount2(source.elapsedMs);
|
|
945
|
+
if (ms === undefined || ms <= 0)
|
|
946
|
+
return;
|
|
947
|
+
return row("elapsed", formatDuration(ms, layout.durationStyle));
|
|
948
|
+
}
|
|
949
|
+
case "tps": {
|
|
950
|
+
const tps = asCount2(source.tps);
|
|
951
|
+
if (tps === undefined)
|
|
952
|
+
return;
|
|
953
|
+
const rounded = Math.round(tps);
|
|
954
|
+
if (rounded <= 0)
|
|
955
|
+
return;
|
|
956
|
+
return row("tps", `${rounded} tok/s`);
|
|
957
|
+
}
|
|
958
|
+
case "spark": {
|
|
959
|
+
const values = Array.isArray(source.spark) ? source.spark.map((value) => asCount2(value) ?? 0) : [];
|
|
960
|
+
const windowed = values.slice(-Math.max(2, Math.floor(sparkWidth)));
|
|
961
|
+
if (windowed.length < 2)
|
|
962
|
+
return;
|
|
963
|
+
return row("spark", sparkline(windowed));
|
|
964
|
+
}
|
|
965
|
+
case "reasoning": {
|
|
966
|
+
const reasoning = asCount2(asRecord2(source.tokens)?.reasoning);
|
|
967
|
+
if (reasoning === undefined || reasoning === 0)
|
|
968
|
+
return;
|
|
969
|
+
return row("reasoning", formatCount(reasoning));
|
|
970
|
+
}
|
|
971
|
+
case "turns": {
|
|
972
|
+
const turns = asCount2(source.turns);
|
|
973
|
+
if (turns === undefined || turns === 0)
|
|
974
|
+
return;
|
|
975
|
+
return row("turns", String(turns));
|
|
976
|
+
}
|
|
977
|
+
case "guard": {
|
|
978
|
+
const token = guardToken(source.guard);
|
|
979
|
+
return token === undefined ? undefined : row("guard", token);
|
|
980
|
+
}
|
|
981
|
+
case "ses": {
|
|
982
|
+
const id = asText2(source.sessionId);
|
|
983
|
+
const budget = Math.max(8, SES_PREVIEW_WIDTH - Math.max(0, labelPrefix(fieldLabel("ses", layout.labels), labelWidth).length - labelPrefix("ses", labelWidth).length));
|
|
984
|
+
return id === undefined ? undefined : row("ses", clip(id, budget));
|
|
985
|
+
}
|
|
986
|
+
case "go": {
|
|
987
|
+
const value = goValueSegments(source, layout);
|
|
988
|
+
return value === undefined ? undefined : row("go", value.map((segment) => segment.text).join(""));
|
|
989
|
+
}
|
|
990
|
+
default:
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
function statSegments(field, source, layout = {}) {
|
|
995
|
+
const name = canonicalField(field);
|
|
996
|
+
if (name === "caution") {
|
|
997
|
+
const value = cautionValueSegments(source);
|
|
998
|
+
if (value === undefined)
|
|
999
|
+
return;
|
|
1000
|
+
return [
|
|
1001
|
+
{ text: labelPrefix(fieldLabel("caution", layout.labels), layout.labelWidth ?? DEFAULT_LABEL_WIDTH) },
|
|
1002
|
+
...value
|
|
1003
|
+
];
|
|
1004
|
+
}
|
|
1005
|
+
if (name !== "go")
|
|
1006
|
+
return;
|
|
1007
|
+
const value = goValueSegments(source, layout);
|
|
1008
|
+
if (value === undefined)
|
|
1009
|
+
return;
|
|
1010
|
+
return [{ text: labelPrefix(fieldLabel("go", layout.labels), layout.labelWidth ?? DEFAULT_LABEL_WIDTH) }, ...value];
|
|
1011
|
+
}
|
|
1012
|
+
function statRows(fields, source, layout = {}) {
|
|
1013
|
+
const hint = { ...layout, hasTotalRow: fields.includes("total") };
|
|
1014
|
+
const persist = hint.persist === true;
|
|
1015
|
+
const placeholder = hint.placeholder ?? DEFAULT_PLACEHOLDER;
|
|
1016
|
+
const labelWidth = hint.labelWidth ?? DEFAULT_LABEL_WIDTH;
|
|
1017
|
+
const rows = [];
|
|
1018
|
+
for (const field of fields) {
|
|
1019
|
+
const name = canonicalField(field);
|
|
1020
|
+
const line = statLine(name, source, hint);
|
|
1021
|
+
if (line !== undefined) {
|
|
1022
|
+
rows.push(line);
|
|
1023
|
+
continue;
|
|
1024
|
+
}
|
|
1025
|
+
if (!persist || !isStatField(name) || name === "caution")
|
|
1026
|
+
continue;
|
|
1027
|
+
rows.push(formatRow(fieldLabel(name, hint.labels), skeletonValue(name, hint) ?? placeholder, labelWidth));
|
|
1028
|
+
}
|
|
1029
|
+
return rows;
|
|
1030
|
+
}
|
|
1031
|
+
// src/tui/throughput.ts
|
|
1032
|
+
function turnSpan(created, completed, streamed, nowMs) {
|
|
1033
|
+
const start = asCount2(created);
|
|
1034
|
+
if (start === undefined)
|
|
1035
|
+
return;
|
|
1036
|
+
const ended = asCount2(streamed) ?? asCount2(completed) ?? asCount2(nowMs);
|
|
1037
|
+
if (ended === undefined)
|
|
1038
|
+
return;
|
|
1039
|
+
return { start, end: Math.max(start, ended) };
|
|
1040
|
+
}
|
|
1041
|
+
function turnKey(id, created, completed, output, streamed) {
|
|
1042
|
+
const own = asText2(id);
|
|
1043
|
+
if (own !== undefined)
|
|
1044
|
+
return `id:${own}`;
|
|
1045
|
+
const stamp = `t:${asCount2(created) ?? "?"}:${asCount2(completed) ?? "?"}`;
|
|
1046
|
+
if (asCount2(completed) === undefined && asCount2(streamed) === undefined)
|
|
1047
|
+
return stamp;
|
|
1048
|
+
return `${stamp}:${asCount2(output) ?? "?"}`;
|
|
1049
|
+
}
|
|
1050
|
+
function mergedSpanMs(accepted) {
|
|
1051
|
+
accepted.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
1052
|
+
const first = accepted[0];
|
|
1053
|
+
if (first === undefined)
|
|
1054
|
+
return 0;
|
|
1055
|
+
let unionMs = 0;
|
|
1056
|
+
let openStart = first.start;
|
|
1057
|
+
let openEnd = first.end;
|
|
1058
|
+
for (let index = 1;index < accepted.length; index += 1) {
|
|
1059
|
+
const next = accepted[index];
|
|
1060
|
+
if (next === undefined)
|
|
1061
|
+
continue;
|
|
1062
|
+
if (next.start <= openEnd) {
|
|
1063
|
+
if (next.end > openEnd)
|
|
1064
|
+
openEnd = next.end;
|
|
1065
|
+
} else {
|
|
1066
|
+
unionMs += openEnd - openStart;
|
|
1067
|
+
openStart = next.start;
|
|
1068
|
+
openEnd = next.end;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
unionMs += openEnd - openStart;
|
|
1072
|
+
return unionMs;
|
|
1073
|
+
}
|
|
1074
|
+
function unionSpanMs(spans) {
|
|
1075
|
+
const seen = new Set;
|
|
1076
|
+
const accepted = [];
|
|
1077
|
+
for (const span of spans) {
|
|
1078
|
+
const key = asText2(span.key);
|
|
1079
|
+
if (key === undefined || seen.has(key))
|
|
1080
|
+
continue;
|
|
1081
|
+
const start = asCount2(span.start);
|
|
1082
|
+
const end = asCount2(span.end);
|
|
1083
|
+
if (start === undefined || end === undefined)
|
|
1084
|
+
continue;
|
|
1085
|
+
seen.add(key);
|
|
1086
|
+
accepted.push({ start, end: Math.max(start, end) });
|
|
1087
|
+
}
|
|
1088
|
+
return mergedSpanMs(accepted);
|
|
1089
|
+
}
|
|
1090
|
+
var TPS_EWMA_ALPHA = 0.3;
|
|
1091
|
+
var TPS_REPAINT_ABS = 1;
|
|
1092
|
+
var TPS_REPAINT_RATIO = 0.1;
|
|
1093
|
+
function smoothTpsRate(previous, sample, alpha = TPS_EWMA_ALPHA) {
|
|
1094
|
+
if (previous === undefined || !Number.isFinite(previous))
|
|
1095
|
+
return sample;
|
|
1096
|
+
if (!Number.isFinite(sample))
|
|
1097
|
+
return previous;
|
|
1098
|
+
return previous + alpha * (sample - previous);
|
|
1099
|
+
}
|
|
1100
|
+
function tpsNeedsRepaint(displayed, smoothed) {
|
|
1101
|
+
if (displayed === undefined || !Number.isFinite(displayed))
|
|
1102
|
+
return true;
|
|
1103
|
+
if (!Number.isFinite(smoothed))
|
|
1104
|
+
return false;
|
|
1105
|
+
if (Math.abs(smoothed - displayed) >= TPS_REPAINT_ABS)
|
|
1106
|
+
return true;
|
|
1107
|
+
if (displayed === 0)
|
|
1108
|
+
return smoothed !== 0;
|
|
1109
|
+
return Math.abs(smoothed - displayed) / Math.abs(displayed) >= TPS_REPAINT_RATIO;
|
|
1110
|
+
}
|
|
1111
|
+
function unionGenerationTotals(spans) {
|
|
1112
|
+
const seen = new Set;
|
|
1113
|
+
const accepted = [];
|
|
1114
|
+
let tokens = 0;
|
|
1115
|
+
for (const span of spans) {
|
|
1116
|
+
const key = asText2(span.key);
|
|
1117
|
+
if (key === undefined || seen.has(key))
|
|
1118
|
+
continue;
|
|
1119
|
+
const start = asCount2(span.start);
|
|
1120
|
+
const end = asCount2(span.end);
|
|
1121
|
+
if (start === undefined || end === undefined)
|
|
1122
|
+
continue;
|
|
1123
|
+
const output = asCount2(span.tokens);
|
|
1124
|
+
const reasoning = asCount2(span.reasoning);
|
|
1125
|
+
seen.add(key);
|
|
1126
|
+
accepted.push({ start, end: Math.max(start, end) });
|
|
1127
|
+
tokens += (output ?? 0) + (reasoning ?? 0);
|
|
1128
|
+
}
|
|
1129
|
+
return { tokens, unionMs: mergedSpanMs(accepted) };
|
|
1130
|
+
}
|
|
1131
|
+
function createInstantTps(alpha = TPS_EWMA_ALPHA) {
|
|
1132
|
+
let prevTokens;
|
|
1133
|
+
let prevGenMs;
|
|
1134
|
+
let smoothed;
|
|
1135
|
+
let displayed;
|
|
1136
|
+
return function sample(totalTokens, totalGenMs) {
|
|
1137
|
+
const tokens = asCount2(totalTokens);
|
|
1138
|
+
const genMs = asCount2(totalGenMs);
|
|
1139
|
+
if (tokens === undefined || genMs === undefined)
|
|
1140
|
+
return displayed;
|
|
1141
|
+
if (prevTokens === undefined || prevGenMs === undefined) {
|
|
1142
|
+
prevTokens = tokens;
|
|
1143
|
+
prevGenMs = genMs;
|
|
1144
|
+
return 0;
|
|
1145
|
+
}
|
|
1146
|
+
const deltaTokens = tokens - prevTokens;
|
|
1147
|
+
const deltaMs = genMs - prevGenMs;
|
|
1148
|
+
if (deltaTokens < 0 || deltaMs < 0) {
|
|
1149
|
+
prevTokens = tokens;
|
|
1150
|
+
prevGenMs = genMs;
|
|
1151
|
+
smoothed = undefined;
|
|
1152
|
+
return 0;
|
|
1153
|
+
}
|
|
1154
|
+
prevTokens = tokens;
|
|
1155
|
+
prevGenMs = genMs;
|
|
1156
|
+
if (deltaMs <= 0)
|
|
1157
|
+
return 0;
|
|
1158
|
+
const instant = deltaTokens / (deltaMs / 1000);
|
|
1159
|
+
smoothed = smoothTpsRate(smoothed, instant, alpha);
|
|
1160
|
+
if (tpsNeedsRepaint(displayed, smoothed))
|
|
1161
|
+
displayed = smoothed;
|
|
1162
|
+
return displayed;
|
|
1163
|
+
};
|
|
1164
|
+
}
|
|
1165
|
+
// src/tui/style.ts
|
|
1166
|
+
var TextAttributes = {
|
|
1167
|
+
NONE: 0,
|
|
1168
|
+
BOLD: 1,
|
|
1169
|
+
DIM: 2,
|
|
1170
|
+
ITALIC: 4,
|
|
1171
|
+
UNDERLINE: 8,
|
|
1172
|
+
BLINK: 16,
|
|
1173
|
+
INVERSE: 32,
|
|
1174
|
+
HIDDEN: 64,
|
|
1175
|
+
STRIKETHROUGH: 128
|
|
1176
|
+
};
|
|
1177
|
+
var STYLE_COLORS = ["default", "subdued", "warning", "error", "success", "info"];
|
|
1178
|
+
var STYLE_ATTRIBUTES = [
|
|
1179
|
+
"bold",
|
|
1180
|
+
"dim",
|
|
1181
|
+
"italic",
|
|
1182
|
+
"underline",
|
|
1183
|
+
"blink",
|
|
1184
|
+
"inverse",
|
|
1185
|
+
"hidden",
|
|
1186
|
+
"strikethrough"
|
|
1187
|
+
];
|
|
1188
|
+
var DEFAULT_STYLE = {
|
|
1189
|
+
lines: { color: "default", attributes: [] },
|
|
1190
|
+
rows: { wildcard: { color: "subdued", attributes: [] }, overrides: {} }
|
|
1191
|
+
};
|
|
1192
|
+
function rowStyle(style, field) {
|
|
1193
|
+
return style.rows.overrides[field] ?? style.rows.wildcard;
|
|
1194
|
+
}
|
|
1195
|
+
function themeColor(color, theme) {
|
|
1196
|
+
const text = theme.text;
|
|
1197
|
+
if (text === undefined)
|
|
1198
|
+
return;
|
|
1199
|
+
switch (color) {
|
|
1200
|
+
case "default":
|
|
1201
|
+
return text.default ?? text.base;
|
|
1202
|
+
case "subdued":
|
|
1203
|
+
return text.subdued ?? text.muted;
|
|
1204
|
+
default: {
|
|
1205
|
+
const feedback = text.feedback?.[color];
|
|
1206
|
+
return feedback?.default ?? feedback?.base;
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
var ATTRIBUTE_BITS = {
|
|
1211
|
+
bold: TextAttributes.BOLD,
|
|
1212
|
+
dim: TextAttributes.DIM,
|
|
1213
|
+
italic: TextAttributes.ITALIC,
|
|
1214
|
+
underline: TextAttributes.UNDERLINE,
|
|
1215
|
+
blink: TextAttributes.BLINK,
|
|
1216
|
+
inverse: TextAttributes.INVERSE,
|
|
1217
|
+
hidden: TextAttributes.HIDDEN,
|
|
1218
|
+
strikethrough: TextAttributes.STRIKETHROUGH
|
|
1219
|
+
};
|
|
1220
|
+
function attributeMask(attributes) {
|
|
1221
|
+
let mask = 0;
|
|
1222
|
+
for (const attribute of attributes)
|
|
1223
|
+
mask |= ATTRIBUTE_BITS[attribute];
|
|
1224
|
+
return mask === 0 ? undefined : mask;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
// src/tui/config.ts
|
|
1228
|
+
var DEFAULT_GO_PANEL_LOOK = {
|
|
1229
|
+
header: false,
|
|
1230
|
+
barWidth: 10,
|
|
1231
|
+
labelWidth: 6,
|
|
1232
|
+
percent: true,
|
|
1233
|
+
reset: true,
|
|
1234
|
+
align: "right",
|
|
1235
|
+
sweep: false,
|
|
1236
|
+
labels: { rolling: "rolling", weekly: "weekly", monthly: "monthly" },
|
|
1237
|
+
blink: true,
|
|
1238
|
+
blinkMs: 700
|
|
1239
|
+
};
|
|
1240
|
+
var DEFAULT_TPS_SCOPE = "family";
|
|
1241
|
+
function cautionThresholds(config) {
|
|
1242
|
+
return {
|
|
1243
|
+
toolWatchMs: config.toolWatchSeconds * 1000,
|
|
1244
|
+
toolCautionMs: config.toolCautionSeconds * 1000,
|
|
1245
|
+
turnWatchMs: config.turnWatchSeconds * 1000,
|
|
1246
|
+
turnCautionMs: config.turnCautionSeconds * 1000,
|
|
1247
|
+
repeatThreshold: config.repeatThreshold,
|
|
1248
|
+
exemptTools: config.exemptTools
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
var DEFAULT_LAYOUT = {
|
|
1252
|
+
labelWidth: 10,
|
|
1253
|
+
barWidth: 10,
|
|
1254
|
+
sparkWidth: 12
|
|
1255
|
+
};
|
|
1256
|
+
var DEFAULT_FORMAT = { duration: "spaced" };
|
|
1257
|
+
var DEFAULT_GLYPHS = {
|
|
1258
|
+
watch: "\u25B2",
|
|
1259
|
+
caution: "\u25CF",
|
|
1260
|
+
clear: "\u25CB"
|
|
1261
|
+
};
|
|
1262
|
+
var DEFAULT_CAUTION = {
|
|
1263
|
+
enabled: false,
|
|
1264
|
+
toolWatchSeconds: 180,
|
|
1265
|
+
toolCautionSeconds: 420,
|
|
1266
|
+
turnWatchSeconds: 600,
|
|
1267
|
+
turnCautionSeconds: 1200,
|
|
1268
|
+
repeatThreshold: 3,
|
|
1269
|
+
exemptTools: ["question", "task", "subagent", "agent", "delegate", "delegate_many", "delegate_task", "tools.delegate", "tools.delegate_many", "tools.subagent", "tools.task"],
|
|
1270
|
+
toast: false
|
|
1271
|
+
};
|
|
1272
|
+
var DEFAULT_SIDEBAR_LINES = [];
|
|
1273
|
+
var DEFAULT_SIDEBAR_FOOTER_LINES = [];
|
|
1274
|
+
var DEFAULT_SIDEBAR_ROWS = [
|
|
1275
|
+
"status",
|
|
1276
|
+
"agent",
|
|
1277
|
+
"model",
|
|
1278
|
+
"cost",
|
|
1279
|
+
"project",
|
|
1280
|
+
"tokens",
|
|
1281
|
+
"cache",
|
|
1282
|
+
"context",
|
|
1283
|
+
"perms",
|
|
1284
|
+
"elapsed",
|
|
1285
|
+
"tps"
|
|
1286
|
+
];
|
|
1287
|
+
var MAX_LINES = 24;
|
|
1288
|
+
var DEFAULT_REFRESH_MS = 100;
|
|
1289
|
+
var MAX_REFRESH_MS = 60000;
|
|
1290
|
+
var MIN_REFRESH_MS = 16;
|
|
1291
|
+
var DEFAULT_FOOTER_TEXT = "Flight Deck";
|
|
1292
|
+
var DEFAULT_CONFIG = {
|
|
1293
|
+
sidebar: {
|
|
1294
|
+
enabled: true,
|
|
1295
|
+
lines: DEFAULT_SIDEBAR_LINES,
|
|
1296
|
+
footer: { lines: DEFAULT_SIDEBAR_FOOTER_LINES, go: false },
|
|
1297
|
+
rows: DEFAULT_SIDEBAR_ROWS,
|
|
1298
|
+
tps: { scope: DEFAULT_TPS_SCOPE },
|
|
1299
|
+
persist: true,
|
|
1300
|
+
placeholder: DEFAULT_PLACEHOLDER,
|
|
1301
|
+
maxLines: MAX_LINES
|
|
1302
|
+
},
|
|
1303
|
+
footer: { enabled: false, text: DEFAULT_FOOTER_TEXT },
|
|
1304
|
+
caution: DEFAULT_CAUTION,
|
|
1305
|
+
layout: DEFAULT_LAYOUT,
|
|
1306
|
+
format: DEFAULT_FORMAT,
|
|
1307
|
+
glyphs: DEFAULT_GLYPHS,
|
|
1308
|
+
style: DEFAULT_STYLE,
|
|
1309
|
+
refresh: DEFAULT_REFRESH_MS
|
|
1310
|
+
};
|
|
1311
|
+
var MAX_LINE_LENGTH = 120;
|
|
1312
|
+
var CONTROL_CHAR = /[\u0000-\u001F\u007F-\u009F]/;
|
|
1313
|
+
var CONTROL_CHARS2 = /[\u0000-\u001F\u007F-\u009F]/g;
|
|
1314
|
+
function normalizeText(value, path, issues) {
|
|
1315
|
+
if (!CONTROL_CHAR.test(value))
|
|
1316
|
+
return value.trim();
|
|
1317
|
+
issues.push(`${path} contained control characters; they were replaced with spaces`);
|
|
1318
|
+
return value.replace(CONTROL_CHARS2, " ").replace(/ {2,}/g, " ").trim();
|
|
1319
|
+
}
|
|
1320
|
+
function isRecord(value) {
|
|
1321
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1322
|
+
}
|
|
1323
|
+
function readBoolean(value, fallback, path, issues) {
|
|
1324
|
+
if (value === undefined)
|
|
1325
|
+
return fallback;
|
|
1326
|
+
if (typeof value === "boolean")
|
|
1327
|
+
return value;
|
|
1328
|
+
issues.push(`${path} must be true or false; using ${fallback}`);
|
|
1329
|
+
return fallback;
|
|
1330
|
+
}
|
|
1331
|
+
function readText(value, fallback, path, issues) {
|
|
1332
|
+
if (value === undefined)
|
|
1333
|
+
return fallback;
|
|
1334
|
+
if (typeof value !== "string") {
|
|
1335
|
+
issues.push(`${path} must be a string; using the default`);
|
|
1336
|
+
return fallback;
|
|
1337
|
+
}
|
|
1338
|
+
const text = normalizeText(value, path, issues);
|
|
1339
|
+
if (text.length === 0) {
|
|
1340
|
+
issues.push(`${path} must not be empty; using the default`);
|
|
1341
|
+
return fallback;
|
|
1342
|
+
}
|
|
1343
|
+
if (text.length > MAX_LINE_LENGTH) {
|
|
1344
|
+
issues.push(`${path} was longer than ${MAX_LINE_LENGTH} characters; it was shortened`);
|
|
1345
|
+
return text.slice(0, MAX_LINE_LENGTH);
|
|
1346
|
+
}
|
|
1347
|
+
return text;
|
|
1348
|
+
}
|
|
1349
|
+
function readLineList(value, issues, maxLines, fallback, path) {
|
|
1350
|
+
if (value === undefined)
|
|
1351
|
+
return fallback;
|
|
1352
|
+
if (!Array.isArray(value)) {
|
|
1353
|
+
issues.push(`${path} must be an array of strings; using the default lines`);
|
|
1354
|
+
return fallback;
|
|
1355
|
+
}
|
|
1356
|
+
if (value.length === 0)
|
|
1357
|
+
return [];
|
|
1358
|
+
const lines = [];
|
|
1359
|
+
value.forEach((entry, index) => {
|
|
1360
|
+
const entryPath = `${path}[${index}]`;
|
|
1361
|
+
if (typeof entry !== "string") {
|
|
1362
|
+
issues.push(`${entryPath} must be a string; skipping it`);
|
|
1363
|
+
return;
|
|
1364
|
+
}
|
|
1365
|
+
const text = normalizeText(entry, entryPath, issues);
|
|
1366
|
+
if (text.length === 0) {
|
|
1367
|
+
issues.push(`${entryPath} is empty; skipping it`);
|
|
1368
|
+
return;
|
|
1369
|
+
}
|
|
1370
|
+
if (text.length > MAX_LINE_LENGTH) {
|
|
1371
|
+
issues.push(`${entryPath} was longer than ${MAX_LINE_LENGTH} characters; it was shortened`);
|
|
1372
|
+
lines.push(text.slice(0, MAX_LINE_LENGTH));
|
|
1373
|
+
return;
|
|
1374
|
+
}
|
|
1375
|
+
lines.push(text);
|
|
1376
|
+
});
|
|
1377
|
+
if (lines.length === 0) {
|
|
1378
|
+
issues.push(`${path} had no usable entries; using the default lines`);
|
|
1379
|
+
return fallback;
|
|
1380
|
+
}
|
|
1381
|
+
if (lines.length > maxLines) {
|
|
1382
|
+
issues.push(`${path} has ${lines.length} entries; keeping the first ${maxLines}`);
|
|
1383
|
+
return lines.slice(0, maxLines);
|
|
1384
|
+
}
|
|
1385
|
+
return lines;
|
|
1386
|
+
}
|
|
1387
|
+
function readLines(value, issues, maxLines) {
|
|
1388
|
+
return readLineList(value, issues, maxLines, DEFAULT_SIDEBAR_LINES, "sidebar.lines");
|
|
1389
|
+
}
|
|
1390
|
+
function readSidebarFooterLines(value, issues) {
|
|
1391
|
+
return readLineList(value, issues, MAX_LINES, DEFAULT_SIDEBAR_FOOTER_LINES, "sidebar.footer.lines");
|
|
1392
|
+
}
|
|
1393
|
+
var GO_LABEL_MAX_LENGTH = 24;
|
|
1394
|
+
function readGoLabel(value, fallback, path, issues) {
|
|
1395
|
+
if (value === undefined)
|
|
1396
|
+
return fallback;
|
|
1397
|
+
if (typeof value !== "string") {
|
|
1398
|
+
issues.push(`${path} must be a string; using the default`);
|
|
1399
|
+
return fallback;
|
|
1400
|
+
}
|
|
1401
|
+
const text = value.trim();
|
|
1402
|
+
if (text.length === 0) {
|
|
1403
|
+
issues.push(`${path} must not be empty; using the default`);
|
|
1404
|
+
return fallback;
|
|
1405
|
+
}
|
|
1406
|
+
if (text.length > GO_LABEL_MAX_LENGTH) {
|
|
1407
|
+
issues.push(`${path} was longer than ${GO_LABEL_MAX_LENGTH} characters; using the default`);
|
|
1408
|
+
return fallback;
|
|
1409
|
+
}
|
|
1410
|
+
return text;
|
|
1411
|
+
}
|
|
1412
|
+
function readGoLabels(value, issues) {
|
|
1413
|
+
if (value === undefined)
|
|
1414
|
+
return DEFAULT_GO_PANEL_LOOK.labels;
|
|
1415
|
+
if (!isRecord(value)) {
|
|
1416
|
+
issues.push("sidebar.footer.go.labels must be an object; using the defaults");
|
|
1417
|
+
return DEFAULT_GO_PANEL_LOOK.labels;
|
|
1418
|
+
}
|
|
1419
|
+
return {
|
|
1420
|
+
rolling: readGoLabel(value["rolling"], DEFAULT_GO_PANEL_LOOK.labels.rolling, "sidebar.footer.go.labels.rolling", issues),
|
|
1421
|
+
weekly: readGoLabel(value["weekly"], DEFAULT_GO_PANEL_LOOK.labels.weekly, "sidebar.footer.go.labels.weekly", issues),
|
|
1422
|
+
monthly: readGoLabel(value["monthly"], DEFAULT_GO_PANEL_LOOK.labels.monthly, "sidebar.footer.go.labels.monthly", issues)
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
function readGoAlign(value, issues) {
|
|
1426
|
+
if (value === undefined)
|
|
1427
|
+
return DEFAULT_GO_PANEL_LOOK.align;
|
|
1428
|
+
if (value === "left" || value === "right" || value === "center")
|
|
1429
|
+
return value;
|
|
1430
|
+
issues.push('sidebar.footer.go.align must be "left", "right" or "center"; using "right"');
|
|
1431
|
+
return DEFAULT_GO_PANEL_LOOK.align;
|
|
1432
|
+
}
|
|
1433
|
+
function readFooterGo(value, issues) {
|
|
1434
|
+
if (value === undefined)
|
|
1435
|
+
return DEFAULT_CONFIG.sidebar.footer.go;
|
|
1436
|
+
if (typeof value === "boolean")
|
|
1437
|
+
return value;
|
|
1438
|
+
if (!isRecord(value)) {
|
|
1439
|
+
issues.push("sidebar.footer.go must be true, false or an object; using false");
|
|
1440
|
+
return false;
|
|
1441
|
+
}
|
|
1442
|
+
return {
|
|
1443
|
+
header: readBoolean(value["header"], DEFAULT_GO_PANEL_LOOK.header, "sidebar.footer.go.header", issues),
|
|
1444
|
+
barWidth: readNumber(value["barWidth"], DEFAULT_GO_PANEL_LOOK.barWidth, "sidebar.footer.go.barWidth", issues, 1, 40),
|
|
1445
|
+
labelWidth: readNumber(value["labelWidth"], DEFAULT_GO_PANEL_LOOK.labelWidth, "sidebar.footer.go.labelWidth", issues, 5, 24),
|
|
1446
|
+
percent: readBoolean(value["percent"], DEFAULT_GO_PANEL_LOOK.percent, "sidebar.footer.go.percent", issues),
|
|
1447
|
+
reset: readBoolean(value["reset"], DEFAULT_GO_PANEL_LOOK.reset, "sidebar.footer.go.reset", issues),
|
|
1448
|
+
align: readGoAlign(value["align"], issues),
|
|
1449
|
+
sweep: readBoolean(value["sweep"], DEFAULT_GO_PANEL_LOOK.sweep, "sidebar.footer.go.sweep", issues),
|
|
1450
|
+
labels: readGoLabels(value["labels"], issues),
|
|
1451
|
+
blink: readBoolean(value["blink"], DEFAULT_GO_PANEL_LOOK.blink, "sidebar.footer.go.blink", issues),
|
|
1452
|
+
blinkMs: readNumber(value["blinkMs"], DEFAULT_GO_PANEL_LOOK.blinkMs, "sidebar.footer.go.blinkMs", issues, 200, 5000)
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
function readRows(value, issues, maxLines) {
|
|
1456
|
+
if (value === undefined)
|
|
1457
|
+
return DEFAULT_SIDEBAR_ROWS;
|
|
1458
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
1459
|
+
issues.push("sidebar.rows must be a non-empty array of field names; using the default rows");
|
|
1460
|
+
return DEFAULT_SIDEBAR_ROWS;
|
|
1461
|
+
}
|
|
1462
|
+
const rows = [];
|
|
1463
|
+
value.forEach((entry, index) => {
|
|
1464
|
+
const path = `sidebar.rows[${index}]`;
|
|
1465
|
+
if (typeof entry !== "string") {
|
|
1466
|
+
issues.push(`${path} must be a string; skipping it`);
|
|
1467
|
+
return;
|
|
1468
|
+
}
|
|
1469
|
+
const name = normalizeText(entry, path, issues).toLowerCase();
|
|
1470
|
+
if (name.length === 0) {
|
|
1471
|
+
issues.push(`${path} is empty; skipping it`);
|
|
1472
|
+
return;
|
|
1473
|
+
}
|
|
1474
|
+
if (!isStatField(name)) {
|
|
1475
|
+
issues.push(`${path} is not a known field (${STAT_FIELDS.join(", ")}); skipping it`);
|
|
1476
|
+
return;
|
|
1477
|
+
}
|
|
1478
|
+
rows.push(canonicalField(name));
|
|
1479
|
+
});
|
|
1480
|
+
if (rows.length === 0) {
|
|
1481
|
+
issues.push("sidebar.rows had no usable entries; using the default rows");
|
|
1482
|
+
return DEFAULT_SIDEBAR_ROWS;
|
|
1483
|
+
}
|
|
1484
|
+
return rows.slice(0, maxLines);
|
|
1485
|
+
}
|
|
1486
|
+
function readTpsScope(value, issues) {
|
|
1487
|
+
if (value === undefined)
|
|
1488
|
+
return DEFAULT_TPS_SCOPE;
|
|
1489
|
+
if (value === "family" || value === "session")
|
|
1490
|
+
return value;
|
|
1491
|
+
issues.push(`sidebar.tps.scope must be "family" or "session"; using "${DEFAULT_TPS_SCOPE}"`);
|
|
1492
|
+
return DEFAULT_TPS_SCOPE;
|
|
1493
|
+
}
|
|
1494
|
+
function readTps(section, issues) {
|
|
1495
|
+
const raw = section["tps"];
|
|
1496
|
+
if (raw !== undefined && !isRecord(raw)) {
|
|
1497
|
+
issues.push("sidebar.tps must be an object; using defaults");
|
|
1498
|
+
return { scope: DEFAULT_TPS_SCOPE };
|
|
1499
|
+
}
|
|
1500
|
+
const tps = isRecord(raw) ? raw : {};
|
|
1501
|
+
return { scope: readTpsScope(tps["scope"], issues) };
|
|
1502
|
+
}
|
|
1503
|
+
function readMaxLines(value, issues) {
|
|
1504
|
+
return readNumber(value, MAX_LINES, "sidebar.maxLines", issues, 1, MAX_LINES);
|
|
1505
|
+
}
|
|
1506
|
+
function sectionOf(options, key) {
|
|
1507
|
+
const value = options[key];
|
|
1508
|
+
return isRecord(value) ? value : {};
|
|
1509
|
+
}
|
|
1510
|
+
function readRefresh(value, issues) {
|
|
1511
|
+
if (value === undefined)
|
|
1512
|
+
return DEFAULT_REFRESH_MS;
|
|
1513
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
1514
|
+
issues.push("refresh must be a number of milliseconds; using the default");
|
|
1515
|
+
return DEFAULT_REFRESH_MS;
|
|
1516
|
+
}
|
|
1517
|
+
if (value > MAX_REFRESH_MS) {
|
|
1518
|
+
issues.push(`refresh was longer than ${MAX_REFRESH_MS}ms; using ${MAX_REFRESH_MS}`);
|
|
1519
|
+
return MAX_REFRESH_MS;
|
|
1520
|
+
}
|
|
1521
|
+
if (value > 0 && value < MIN_REFRESH_MS) {
|
|
1522
|
+
issues.push(`refresh was faster than ${MIN_REFRESH_MS}ms; using ${MIN_REFRESH_MS}`);
|
|
1523
|
+
return MIN_REFRESH_MS;
|
|
1524
|
+
}
|
|
1525
|
+
return value;
|
|
1526
|
+
}
|
|
1527
|
+
function mergeSection(file, host, key) {
|
|
1528
|
+
const fileValue = file[key];
|
|
1529
|
+
const hostValue = host[key];
|
|
1530
|
+
if (hostValue !== undefined && !isRecord(hostValue))
|
|
1531
|
+
return hostValue;
|
|
1532
|
+
if (fileValue !== undefined && !isRecord(fileValue) && hostValue === undefined)
|
|
1533
|
+
return fileValue;
|
|
1534
|
+
const merged = { ...sectionOf(file, key), ...sectionOf(host, key) };
|
|
1535
|
+
if (key === "sidebar") {
|
|
1536
|
+
const fileSidebar = sectionOf(file, key);
|
|
1537
|
+
const hostSidebar = sectionOf(host, key);
|
|
1538
|
+
const fileFooter = fileSidebar["footer"];
|
|
1539
|
+
const hostFooter = hostSidebar["footer"];
|
|
1540
|
+
if (isRecord(fileFooter) && isRecord(hostFooter)) {
|
|
1541
|
+
merged["footer"] = { ...fileFooter, ...hostFooter };
|
|
1542
|
+
}
|
|
1543
|
+
const fileTps = fileSidebar["tps"];
|
|
1544
|
+
const hostTps = hostSidebar["tps"];
|
|
1545
|
+
if (isRecord(fileTps) && isRecord(hostTps)) {
|
|
1546
|
+
merged["tps"] = { ...fileTps, ...hostTps };
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
return merged;
|
|
1550
|
+
}
|
|
1551
|
+
function mergeOptions(fileOptions, hostOptions) {
|
|
1552
|
+
const file = isRecord(fileOptions) ? fileOptions : {};
|
|
1553
|
+
const host = isRecord(hostOptions) ? hostOptions : {};
|
|
1554
|
+
return {
|
|
1555
|
+
...file,
|
|
1556
|
+
...host,
|
|
1557
|
+
sidebar: mergeSection(file, host, "sidebar"),
|
|
1558
|
+
footer: mergeSection(file, host, "footer"),
|
|
1559
|
+
caution: mergeSection(file, host, "caution"),
|
|
1560
|
+
layout: mergeSection(file, host, "layout"),
|
|
1561
|
+
format: mergeSection(file, host, "format"),
|
|
1562
|
+
glyphs: mergeSection(file, host, "glyphs"),
|
|
1563
|
+
style: mergeSection(file, host, "style")
|
|
1564
|
+
};
|
|
1565
|
+
}
|
|
1566
|
+
function readNumber(value, fallback, path, issues, min, max = Number.POSITIVE_INFINITY) {
|
|
1567
|
+
if (value === undefined)
|
|
1568
|
+
return fallback;
|
|
1569
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
1570
|
+
issues.push(`${path} must be a number; using the default`);
|
|
1571
|
+
return fallback;
|
|
1572
|
+
}
|
|
1573
|
+
if (value < min || value > max) {
|
|
1574
|
+
const limit = max === Number.POSITIVE_INFINITY ? `at least ${min}` : `between ${min} and ${max}`;
|
|
1575
|
+
issues.push(`${path} must be ${limit}; using the default`);
|
|
1576
|
+
return fallback;
|
|
1577
|
+
}
|
|
1578
|
+
return Math.floor(value);
|
|
1579
|
+
}
|
|
1580
|
+
function readLabels(value, issues) {
|
|
1581
|
+
if (value === undefined)
|
|
1582
|
+
return;
|
|
1583
|
+
if (!isRecord(value)) {
|
|
1584
|
+
issues.push("layout.labels must be an object of row names to labels; using the default labels");
|
|
1585
|
+
return;
|
|
1586
|
+
}
|
|
1587
|
+
const labels = {};
|
|
1588
|
+
for (const [rawName, rawLabel] of Object.entries(value)) {
|
|
1589
|
+
const path = `layout.labels.${rawName}`;
|
|
1590
|
+
const key = normalizeText(rawName, path, issues).trim().toLowerCase();
|
|
1591
|
+
if (key.length === 0) {
|
|
1592
|
+
issues.push(`${path} is empty; using the default label`);
|
|
1593
|
+
continue;
|
|
1594
|
+
}
|
|
1595
|
+
if (!isStatField(key)) {
|
|
1596
|
+
issues.push(`${path} is not a known row (${STAT_FIELDS.join(", ")}); using the default label`);
|
|
1597
|
+
continue;
|
|
1598
|
+
}
|
|
1599
|
+
if (typeof rawLabel !== "string") {
|
|
1600
|
+
issues.push(`${path} must be a string; using the default label`);
|
|
1601
|
+
continue;
|
|
1602
|
+
}
|
|
1603
|
+
const label = normalizeText(rawLabel, path, issues).trim();
|
|
1604
|
+
if (label.length === 0) {
|
|
1605
|
+
issues.push(`${path} must not be empty; using the default label`);
|
|
1606
|
+
continue;
|
|
1607
|
+
}
|
|
1608
|
+
labels[canonicalField(key)] = label;
|
|
1609
|
+
}
|
|
1610
|
+
return Object.keys(labels).length === 0 ? undefined : labels;
|
|
1611
|
+
}
|
|
1612
|
+
function readLayout(section, issues) {
|
|
1613
|
+
return {
|
|
1614
|
+
labelWidth: readNumber(section["labelWidth"], DEFAULT_LAYOUT.labelWidth, "layout.labelWidth", issues, 6, 24),
|
|
1615
|
+
barWidth: readNumber(section["barWidth"], DEFAULT_LAYOUT.barWidth, "layout.barWidth", issues, 1, 40),
|
|
1616
|
+
sparkWidth: readNumber(section["sparkWidth"], DEFAULT_LAYOUT.sparkWidth, "layout.sparkWidth", issues, 2, 64),
|
|
1617
|
+
labels: readLabels(section["labels"], issues)
|
|
1618
|
+
};
|
|
1619
|
+
}
|
|
1620
|
+
function readDurationStyle(value, issues) {
|
|
1621
|
+
if (value === undefined)
|
|
1622
|
+
return DEFAULT_FORMAT.duration;
|
|
1623
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
1624
|
+
if (text === "compact" || text === "spaced")
|
|
1625
|
+
return text;
|
|
1626
|
+
issues.push('format.duration must be "compact" or "spaced"; using the default');
|
|
1627
|
+
return DEFAULT_FORMAT.duration;
|
|
1628
|
+
}
|
|
1629
|
+
function readFormat(section, issues) {
|
|
1630
|
+
return { duration: readDurationStyle(section["duration"], issues) };
|
|
1631
|
+
}
|
|
1632
|
+
function readGlyph(value, fallback, path, issues) {
|
|
1633
|
+
if (value === undefined)
|
|
1634
|
+
return fallback;
|
|
1635
|
+
if (typeof value !== "string") {
|
|
1636
|
+
issues.push(`${path} must be a string; using the default`);
|
|
1637
|
+
return fallback;
|
|
1638
|
+
}
|
|
1639
|
+
const text = value.trim();
|
|
1640
|
+
if (text.length === 0 || [...text].length > 2) {
|
|
1641
|
+
issues.push(`${path} must be one or two characters; using the default`);
|
|
1642
|
+
return fallback;
|
|
1643
|
+
}
|
|
1644
|
+
if (CONTROL_CHAR.test(text)) {
|
|
1645
|
+
issues.push(`${path} contained control characters; using the default`);
|
|
1646
|
+
return fallback;
|
|
1647
|
+
}
|
|
1648
|
+
return text;
|
|
1649
|
+
}
|
|
1650
|
+
function readGlyphs(section, issues) {
|
|
1651
|
+
return {
|
|
1652
|
+
watch: readGlyph(section["watch"], DEFAULT_GLYPHS.watch, "glyphs.watch", issues),
|
|
1653
|
+
caution: readGlyph(section["caution"], DEFAULT_GLYPHS.caution, "glyphs.caution", issues),
|
|
1654
|
+
clear: readGlyph(section["clear"], DEFAULT_GLYPHS.clear, "glyphs.clear", issues)
|
|
1655
|
+
};
|
|
1656
|
+
}
|
|
1657
|
+
function readStyleColor(value, fallback, path, issues) {
|
|
1658
|
+
if (value === undefined)
|
|
1659
|
+
return fallback;
|
|
1660
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
1661
|
+
if (STYLE_COLORS.includes(text))
|
|
1662
|
+
return text;
|
|
1663
|
+
issues.push(`${path} must be one of ${STYLE_COLORS.join(", ")}; using ${fallback}`);
|
|
1664
|
+
return fallback;
|
|
1665
|
+
}
|
|
1666
|
+
function readStyleAttributes(value, fallback, path, issues) {
|
|
1667
|
+
if (value === undefined)
|
|
1668
|
+
return fallback;
|
|
1669
|
+
if (!Array.isArray(value)) {
|
|
1670
|
+
issues.push(`${path} must be an array of attribute names; using the default`);
|
|
1671
|
+
return fallback;
|
|
1672
|
+
}
|
|
1673
|
+
const attributes = [];
|
|
1674
|
+
value.forEach((entry, index) => {
|
|
1675
|
+
const at = `${path}[${index}]`;
|
|
1676
|
+
if (typeof entry !== "string") {
|
|
1677
|
+
issues.push(`${at} must be a string; skipping it`);
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
const name = entry.trim();
|
|
1681
|
+
if (!STYLE_ATTRIBUTES.includes(name)) {
|
|
1682
|
+
issues.push(`${at} is not a known attribute (${STYLE_ATTRIBUTES.join(", ")}); skipping it`);
|
|
1683
|
+
return;
|
|
1684
|
+
}
|
|
1685
|
+
attributes.push(name);
|
|
1686
|
+
});
|
|
1687
|
+
return attributes;
|
|
1688
|
+
}
|
|
1689
|
+
function readLineStyle(value, fallback, path, issues) {
|
|
1690
|
+
if (value === undefined)
|
|
1691
|
+
return fallback;
|
|
1692
|
+
if (!isRecord(value)) {
|
|
1693
|
+
issues.push(`${path} must be an object with color and attributes; using the default`);
|
|
1694
|
+
return fallback;
|
|
1695
|
+
}
|
|
1696
|
+
return {
|
|
1697
|
+
color: readStyleColor(value["color"], fallback.color, `${path}.color`, issues),
|
|
1698
|
+
attributes: readStyleAttributes(value["attributes"], fallback.attributes, `${path}.attributes`, issues)
|
|
1699
|
+
};
|
|
1700
|
+
}
|
|
1701
|
+
function readStyle(section, issues) {
|
|
1702
|
+
const rawRows = section["rows"];
|
|
1703
|
+
if (rawRows !== undefined && !isRecord(rawRows)) {
|
|
1704
|
+
issues.push("style.rows must be an object of row names; using the default");
|
|
1705
|
+
}
|
|
1706
|
+
const rows = isRecord(rawRows) ? rawRows : {};
|
|
1707
|
+
const lines = readLineStyle(section["lines"], DEFAULT_STYLE.lines, "style.lines", issues);
|
|
1708
|
+
const wildcard = readLineStyle(rows["*"], DEFAULT_STYLE.rows.wildcard, "style.rows.*", issues);
|
|
1709
|
+
const overrides = {};
|
|
1710
|
+
for (const [key, entry] of Object.entries(rows)) {
|
|
1711
|
+
if (key === "*")
|
|
1712
|
+
continue;
|
|
1713
|
+
const path = `style.rows.${key}`;
|
|
1714
|
+
const name = normalizeText(key, path, issues).toLowerCase();
|
|
1715
|
+
if (name.length === 0) {
|
|
1716
|
+
issues.push(`${path} is empty; skipping it`);
|
|
1717
|
+
continue;
|
|
1718
|
+
}
|
|
1719
|
+
if (!isStatField(name)) {
|
|
1720
|
+
issues.push(`${path} is not a known field (${STAT_FIELDS.join(", ")}); skipping it`);
|
|
1721
|
+
continue;
|
|
1722
|
+
}
|
|
1723
|
+
overrides[canonicalField(name)] = readLineStyle(entry, wildcard, path, issues);
|
|
1724
|
+
}
|
|
1725
|
+
return { lines, rows: { wildcard, overrides } };
|
|
1726
|
+
}
|
|
1727
|
+
function readToolNames(value, issues) {
|
|
1728
|
+
if (value === undefined)
|
|
1729
|
+
return DEFAULT_CAUTION.exemptTools;
|
|
1730
|
+
if (!Array.isArray(value)) {
|
|
1731
|
+
issues.push("caution.exemptTools must be an array of tool names; using the default");
|
|
1732
|
+
return DEFAULT_CAUTION.exemptTools;
|
|
1733
|
+
}
|
|
1734
|
+
const names = [];
|
|
1735
|
+
for (const entry of value) {
|
|
1736
|
+
if (typeof entry !== "string" || entry.trim().length === 0) {
|
|
1737
|
+
issues.push("caution.exemptTools must contain only non-empty tool names; using the default");
|
|
1738
|
+
return DEFAULT_CAUTION.exemptTools;
|
|
1739
|
+
}
|
|
1740
|
+
names.push(entry.trim());
|
|
1741
|
+
}
|
|
1742
|
+
return names;
|
|
1743
|
+
}
|
|
1744
|
+
function readCaution(section, issues) {
|
|
1745
|
+
const toolWatchSeconds = readNumber(section["toolWatchSeconds"], DEFAULT_CAUTION.toolWatchSeconds, "caution.toolWatchSeconds", issues, 0);
|
|
1746
|
+
const turnWatchSeconds = readNumber(section["turnWatchSeconds"], DEFAULT_CAUTION.turnWatchSeconds, "caution.turnWatchSeconds", issues, 0);
|
|
1747
|
+
let toolCautionSeconds = readNumber(section["toolCautionSeconds"], DEFAULT_CAUTION.toolCautionSeconds, "caution.toolCautionSeconds", issues, 0);
|
|
1748
|
+
let turnCautionSeconds = readNumber(section["turnCautionSeconds"], DEFAULT_CAUTION.turnCautionSeconds, "caution.turnCautionSeconds", issues, 0);
|
|
1749
|
+
if (toolCautionSeconds < toolWatchSeconds) {
|
|
1750
|
+
issues.push("caution.toolCautionSeconds is below caution.toolWatchSeconds; raised to match");
|
|
1751
|
+
toolCautionSeconds = toolWatchSeconds;
|
|
1752
|
+
}
|
|
1753
|
+
if (turnCautionSeconds < turnWatchSeconds) {
|
|
1754
|
+
issues.push("caution.turnCautionSeconds is below caution.turnWatchSeconds; raised to match");
|
|
1755
|
+
turnCautionSeconds = turnWatchSeconds;
|
|
1756
|
+
}
|
|
1757
|
+
return {
|
|
1758
|
+
enabled: readBoolean(section["enabled"], DEFAULT_CAUTION.enabled, "caution.enabled", issues),
|
|
1759
|
+
toolWatchSeconds,
|
|
1760
|
+
toolCautionSeconds,
|
|
1761
|
+
turnWatchSeconds,
|
|
1762
|
+
turnCautionSeconds,
|
|
1763
|
+
repeatThreshold: readNumber(section["repeatThreshold"], DEFAULT_CAUTION.repeatThreshold, "caution.repeatThreshold", issues, 2),
|
|
1764
|
+
exemptTools: readToolNames(section["exemptTools"], issues),
|
|
1765
|
+
toast: readBoolean(section["toast"], DEFAULT_CAUTION.toast, "caution.toast", issues)
|
|
1766
|
+
};
|
|
1767
|
+
}
|
|
1768
|
+
function resolveConfig(options) {
|
|
1769
|
+
const issues = [];
|
|
1770
|
+
if (options === undefined || options === null) {
|
|
1771
|
+
return { config: DEFAULT_CONFIG, issues };
|
|
1772
|
+
}
|
|
1773
|
+
if (!isRecord(options)) {
|
|
1774
|
+
issues.push("plugin options must be an object; using defaults");
|
|
1775
|
+
return { config: DEFAULT_CONFIG, issues };
|
|
1776
|
+
}
|
|
1777
|
+
const rawSidebar = options.sidebar;
|
|
1778
|
+
const rawFooter = options.footer;
|
|
1779
|
+
const rawCaution = options.caution;
|
|
1780
|
+
const rawFormat = options.format;
|
|
1781
|
+
const rawStyle = options.style;
|
|
1782
|
+
if (rawSidebar !== undefined && !isRecord(rawSidebar)) {
|
|
1783
|
+
issues.push("sidebar must be an object; using defaults");
|
|
1784
|
+
}
|
|
1785
|
+
if (rawFooter !== undefined && !isRecord(rawFooter)) {
|
|
1786
|
+
issues.push("footer must be an object; using defaults");
|
|
1787
|
+
}
|
|
1788
|
+
if (rawCaution !== undefined && !isRecord(rawCaution)) {
|
|
1789
|
+
issues.push("caution must be an object; using defaults");
|
|
1790
|
+
}
|
|
1791
|
+
if (rawFormat !== undefined && !isRecord(rawFormat)) {
|
|
1792
|
+
issues.push("format must be an object; using defaults");
|
|
1793
|
+
}
|
|
1794
|
+
if (rawStyle !== undefined && !isRecord(rawStyle)) {
|
|
1795
|
+
issues.push("style must be an object; using defaults");
|
|
1796
|
+
}
|
|
1797
|
+
const sidebar = isRecord(rawSidebar) ? rawSidebar : {};
|
|
1798
|
+
const footer = isRecord(rawFooter) ? rawFooter : {};
|
|
1799
|
+
const caution = isRecord(rawCaution) ? rawCaution : {};
|
|
1800
|
+
const layout = isRecord(options.layout) ? options.layout : {};
|
|
1801
|
+
const format = isRecord(rawFormat) ? rawFormat : {};
|
|
1802
|
+
const glyphs = isRecord(options.glyphs) ? options.glyphs : {};
|
|
1803
|
+
const style = isRecord(rawStyle) ? rawStyle : {};
|
|
1804
|
+
const footerConfigured = Object.keys(footer).length > 0;
|
|
1805
|
+
const maxLines = readMaxLines(sidebar.maxLines, issues);
|
|
1806
|
+
const rawSidebarFooter = sidebar.footer;
|
|
1807
|
+
if (rawSidebarFooter !== undefined && !isRecord(rawSidebarFooter)) {
|
|
1808
|
+
issues.push("sidebar.footer must be an object; using defaults");
|
|
1809
|
+
}
|
|
1810
|
+
const sidebarFooter = isRecord(rawSidebarFooter) ? rawSidebarFooter : {};
|
|
1811
|
+
const lines = readLines(sidebar.lines, issues, maxLines);
|
|
1812
|
+
const footerLines = readSidebarFooterLines(sidebarFooter.lines, issues);
|
|
1813
|
+
const rows = readRows(sidebar.rows, issues, maxLines);
|
|
1814
|
+
if (lines.length + rows.length > maxLines) {
|
|
1815
|
+
issues.push(`sidebar.lines and sidebar.rows total ${lines.length + rows.length} lines; the rail draws the first ${maxLines}`);
|
|
1816
|
+
}
|
|
1817
|
+
return {
|
|
1818
|
+
config: {
|
|
1819
|
+
sidebar: {
|
|
1820
|
+
enabled: readBoolean(sidebar.enabled, DEFAULT_CONFIG.sidebar.enabled, "sidebar.enabled", issues),
|
|
1821
|
+
lines,
|
|
1822
|
+
footer: {
|
|
1823
|
+
lines: footerLines,
|
|
1824
|
+
go: readFooterGo(sidebarFooter.go, issues)
|
|
1825
|
+
},
|
|
1826
|
+
rows,
|
|
1827
|
+
tps: readTps(sidebar, issues),
|
|
1828
|
+
persist: readBoolean(sidebar.persist, DEFAULT_CONFIG.sidebar.persist, "sidebar.persist", issues),
|
|
1829
|
+
placeholder: readText(sidebar.placeholder, DEFAULT_CONFIG.sidebar.placeholder, "sidebar.placeholder", issues),
|
|
1830
|
+
maxLines
|
|
1831
|
+
},
|
|
1832
|
+
footer: {
|
|
1833
|
+
enabled: readBoolean(footer.enabled, footerConfigured, "footer.enabled", issues),
|
|
1834
|
+
text: readText(footer.text, DEFAULT_CONFIG.footer.text, "footer.text", issues)
|
|
1835
|
+
},
|
|
1836
|
+
caution: readCaution(caution, issues),
|
|
1837
|
+
layout: readLayout(layout, issues),
|
|
1838
|
+
format: readFormat(format, issues),
|
|
1839
|
+
glyphs: readGlyphs(glyphs, issues),
|
|
1840
|
+
style: readStyle(style, issues),
|
|
1841
|
+
refresh: readRefresh(options.refresh, issues)
|
|
1842
|
+
},
|
|
1843
|
+
issues
|
|
1844
|
+
};
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
// src/tui/file-config.ts
|
|
1848
|
+
import { existsSync, readFileSync, statSync } from "fs";
|
|
1849
|
+
import { homedir } from "os";
|
|
1850
|
+
import { isAbsolute, join } from "path";
|
|
1851
|
+
var CONFIG_FILE_CANDIDATES = ["flight-deck.jsonc", "flight-deck.json"];
|
|
1852
|
+
function globalConfigDirectory() {
|
|
1853
|
+
const xdg = process.env["XDG_CONFIG_HOME"]?.trim();
|
|
1854
|
+
return xdg !== undefined && xdg.length > 0 && isAbsolute(xdg) ? join(xdg, "opencode") : join(homedir(), ".config", "opencode");
|
|
1855
|
+
}
|
|
1856
|
+
var MAX_CONFIG_BYTES = 64 * 1024;
|
|
1857
|
+
function findConfigFile(directory = globalConfigDirectory()) {
|
|
1858
|
+
for (const candidate of CONFIG_FILE_CANDIDATES) {
|
|
1859
|
+
const path = join(directory, candidate);
|
|
1860
|
+
if (existsSync(path))
|
|
1861
|
+
return path;
|
|
1862
|
+
}
|
|
1863
|
+
return;
|
|
1864
|
+
}
|
|
1865
|
+
function parseJsonc(text) {
|
|
1866
|
+
const jsonc = globalThis.Bun?.JSONC;
|
|
1867
|
+
if (typeof jsonc?.parse === "function")
|
|
1868
|
+
return jsonc.parse(text);
|
|
1869
|
+
return JSON.parse(text);
|
|
1870
|
+
}
|
|
1871
|
+
function loadConfigFile(directory = globalConfigDirectory()) {
|
|
1872
|
+
const source = findConfigFile(directory);
|
|
1873
|
+
if (source === undefined)
|
|
1874
|
+
return { options: undefined };
|
|
1875
|
+
try {
|
|
1876
|
+
const stats = statSync(source);
|
|
1877
|
+
if (!stats.isFile()) {
|
|
1878
|
+
return { options: undefined, source, issue: `${source} is not a regular file; using defaults` };
|
|
1879
|
+
}
|
|
1880
|
+
if (stats.size > MAX_CONFIG_BYTES) {
|
|
1881
|
+
const limit = Math.floor(MAX_CONFIG_BYTES / 1024);
|
|
1882
|
+
return { options: undefined, source, issue: `${source} is larger than ${limit} KiB; using defaults` };
|
|
1883
|
+
}
|
|
1884
|
+
const text = readFileSync(source, "utf8");
|
|
1885
|
+
if (text.trim().length === 0) {
|
|
1886
|
+
return { options: undefined, source, issue: `${source} is empty; using defaults` };
|
|
1887
|
+
}
|
|
1888
|
+
const parsed = parseJsonc(text);
|
|
1889
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
1890
|
+
return { options: undefined, source, issue: `${source} must contain a JSON object; using defaults` };
|
|
1891
|
+
}
|
|
1892
|
+
return { options: parsed, source };
|
|
1893
|
+
} catch (error) {
|
|
1894
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1895
|
+
return { options: undefined, source, issue: `${source} could not be parsed (${message}); using defaults` };
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
// src/tui/go-look.ts
|
|
1900
|
+
import { existsSync as existsSync2, statSync as statSync2 } from "fs";
|
|
1901
|
+
import { join as join2 } from "path";
|
|
1902
|
+
function findCandidate(directory) {
|
|
1903
|
+
for (const candidate of CONFIG_FILE_CANDIDATES) {
|
|
1904
|
+
const path = join2(directory, candidate);
|
|
1905
|
+
if (existsSync2(path))
|
|
1906
|
+
return path;
|
|
1907
|
+
}
|
|
1908
|
+
return;
|
|
1909
|
+
}
|
|
1910
|
+
function snapshotOf(path) {
|
|
1911
|
+
try {
|
|
1912
|
+
const stats = statSync2(path);
|
|
1913
|
+
if (!stats.isFile())
|
|
1914
|
+
return;
|
|
1915
|
+
return { mtimeMs: stats.mtimeMs, size: stats.size };
|
|
1916
|
+
} catch {
|
|
1917
|
+
return;
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
function layoutOf(go) {
|
|
1921
|
+
return go === true ? undefined : typeof go === "object" && go !== null ? go : undefined;
|
|
1922
|
+
}
|
|
1923
|
+
function createGoLookReader(input) {
|
|
1924
|
+
const directory = input.directory ?? globalConfigDirectory();
|
|
1925
|
+
const hostOptions = input.hostOptions;
|
|
1926
|
+
let look = input.initial;
|
|
1927
|
+
let reads = 0;
|
|
1928
|
+
let seenPath = findCandidate(directory);
|
|
1929
|
+
let seen = seenPath === undefined ? undefined : snapshotOf(seenPath);
|
|
1930
|
+
const current = () => {
|
|
1931
|
+
try {
|
|
1932
|
+
const path = findCandidate(directory);
|
|
1933
|
+
if (path === undefined) {
|
|
1934
|
+
seenPath = undefined;
|
|
1935
|
+
seen = undefined;
|
|
1936
|
+
return look;
|
|
1937
|
+
}
|
|
1938
|
+
const snap = snapshotOf(path);
|
|
1939
|
+
if (snap === undefined) {
|
|
1940
|
+
seenPath = undefined;
|
|
1941
|
+
seen = undefined;
|
|
1942
|
+
return look;
|
|
1943
|
+
}
|
|
1944
|
+
if (seenPath === path && seen !== undefined && snap.mtimeMs === seen.mtimeMs && snap.size === seen.size) {
|
|
1945
|
+
return look;
|
|
1946
|
+
}
|
|
1947
|
+
const loaded = loadConfigFile(directory);
|
|
1948
|
+
reads += 1;
|
|
1949
|
+
seenPath = path;
|
|
1950
|
+
seen = snapshotOf(path) ?? snap;
|
|
1951
|
+
if (loaded.options === undefined)
|
|
1952
|
+
return look;
|
|
1953
|
+
const { config } = resolveConfig(mergeOptions(loaded.options, hostOptions));
|
|
1954
|
+
look = layoutOf(config.sidebar.footer.go);
|
|
1955
|
+
return look;
|
|
1956
|
+
} catch {
|
|
1957
|
+
return look;
|
|
1958
|
+
}
|
|
1959
|
+
};
|
|
1960
|
+
return { current, reads: () => reads };
|
|
1961
|
+
}
|
|
1962
|
+
|
|
1963
|
+
// src/tui/presentation.ts
|
|
1964
|
+
function railLines(config, source = {}) {
|
|
1965
|
+
if (!config.sidebar.enabled)
|
|
1966
|
+
return [];
|
|
1967
|
+
const layout = {
|
|
1968
|
+
...config.layout,
|
|
1969
|
+
persist: config.sidebar.persist,
|
|
1970
|
+
placeholder: config.sidebar.placeholder,
|
|
1971
|
+
durationStyle: config.format.duration,
|
|
1972
|
+
hasTotalRow: config.sidebar.rows.includes("total")
|
|
1973
|
+
};
|
|
1974
|
+
const lines = config.sidebar.lines.map((text) => ({ text }));
|
|
1975
|
+
for (const field of config.sidebar.rows) {
|
|
1976
|
+
const segments = statSegments(field, source, layout);
|
|
1977
|
+
if (segments !== undefined) {
|
|
1978
|
+
lines.push({ field, text: segments.map((segment) => segment.text).join(""), segments });
|
|
1979
|
+
continue;
|
|
1980
|
+
}
|
|
1981
|
+
const text = statLine(field, source, layout) ?? statRows([field], source, layout)[0];
|
|
1982
|
+
if (text !== undefined)
|
|
1983
|
+
lines.push({ field, text });
|
|
1984
|
+
}
|
|
1985
|
+
return lines.slice(0, config.sidebar.maxLines);
|
|
1986
|
+
}
|
|
1987
|
+
function railLineStyle(style, line) {
|
|
1988
|
+
return line.field === undefined ? style.lines : rowStyle(style, line.field);
|
|
1989
|
+
}
|
|
1990
|
+
function railLineSpans(line, style) {
|
|
1991
|
+
if (line.segments === undefined)
|
|
1992
|
+
return [{ text: line.text, color: style.color }];
|
|
1993
|
+
return line.segments.map((segment) => ({
|
|
1994
|
+
text: segment.text,
|
|
1995
|
+
color: segment.tone ?? style.color
|
|
1996
|
+
}));
|
|
1997
|
+
}
|
|
1998
|
+
function footerLine(config) {
|
|
1999
|
+
return config.footer.enabled ? config.footer.text : undefined;
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
// src/tui/go-panel.ts
|
|
2003
|
+
var PANEL_WINDOWS = ["5h", "1w", "1m"];
|
|
2004
|
+
var PANEL_DEFAULT_LABELS = {
|
|
2005
|
+
rolling: "rolling",
|
|
2006
|
+
weekly: "weekly",
|
|
2007
|
+
monthly: "monthly"
|
|
2008
|
+
};
|
|
2009
|
+
function labelKeyFor(id) {
|
|
2010
|
+
if (id === "5h")
|
|
2011
|
+
return "rolling";
|
|
2012
|
+
if (id === "1w")
|
|
2013
|
+
return "weekly";
|
|
2014
|
+
return "monthly";
|
|
2015
|
+
}
|
|
2016
|
+
var HEADER_GLYPH = "\u25C8";
|
|
2017
|
+
var MARK_REST = String.fromCharCode(9675);
|
|
2018
|
+
var MARK_MID = String.fromCharCode(9678);
|
|
2019
|
+
var MARK_LIVE = String.fromCharCode(9679);
|
|
2020
|
+
var MARK_FRAMES = [MARK_REST, MARK_MID, MARK_LIVE, MARK_MID];
|
|
2021
|
+
var MARK_WIDTH = 2;
|
|
2022
|
+
var CELL = "\u2500";
|
|
2023
|
+
var SWEEP_TONE = "info";
|
|
2024
|
+
var GO_WARNING_RATIO = 0.6;
|
|
2025
|
+
var PANEL_DEFAULT_BAR_WIDTH = 10;
|
|
2026
|
+
var PANEL_DEFAULT_LABEL_WIDTH = 6;
|
|
2027
|
+
var PANEL_DEFAULT_BLINK = true;
|
|
2028
|
+
var PANEL_DEFAULT_BLINK_MS = 700;
|
|
2029
|
+
var PANEL_MIN_BLINK_MS = 200;
|
|
2030
|
+
var PANEL_MAX_BLINK_MS = 5000;
|
|
2031
|
+
var PANEL_MAX_LABEL_LENGTH = 24;
|
|
2032
|
+
var PANEL_DEFAULT_HEADER = false;
|
|
2033
|
+
var PANEL_DEFAULT_PERCENT = true;
|
|
2034
|
+
var PANEL_DEFAULT_RESET = true;
|
|
2035
|
+
var PANEL_DEFAULT_ALIGN = "right";
|
|
2036
|
+
var PANEL_DEFAULT_SWEEP = false;
|
|
2037
|
+
var PANEL_PERCENT_WIDTH = 4;
|
|
2038
|
+
var PANEL_RESET_WIDTH = 6;
|
|
2039
|
+
var PANEL_SIDEBAR_BUDGET = 40;
|
|
2040
|
+
var PANEL_MIN_LABEL_WIDTH = 5;
|
|
2041
|
+
var PANEL_MAX_LABEL_WIDTH = 24;
|
|
2042
|
+
var PANEL_MAX_NAMED_BAR_WIDTH = 40;
|
|
2043
|
+
function readLookFlag(value, fallback) {
|
|
2044
|
+
return typeof value === "boolean" ? value : fallback;
|
|
2045
|
+
}
|
|
2046
|
+
function alignCell(value, width, align) {
|
|
2047
|
+
if (value.length >= width)
|
|
2048
|
+
return value;
|
|
2049
|
+
const padding = width - value.length;
|
|
2050
|
+
if (align === "left")
|
|
2051
|
+
return value + " ".repeat(padding);
|
|
2052
|
+
if (align === "center") {
|
|
2053
|
+
const left = Math.floor(padding / 2);
|
|
2054
|
+
return " ".repeat(left) + value + " ".repeat(padding - left);
|
|
2055
|
+
}
|
|
2056
|
+
return " ".repeat(padding) + value;
|
|
2057
|
+
}
|
|
2058
|
+
function labelField(label, labelWidth) {
|
|
2059
|
+
return label.padEnd(labelWidth);
|
|
2060
|
+
}
|
|
2061
|
+
function readLabel(value, fallback) {
|
|
2062
|
+
if (typeof value !== "string")
|
|
2063
|
+
return fallback;
|
|
2064
|
+
const text = value.trim();
|
|
2065
|
+
if (text.length === 0 || text.length > PANEL_MAX_LABEL_LENGTH)
|
|
2066
|
+
return fallback;
|
|
2067
|
+
return text;
|
|
2068
|
+
}
|
|
2069
|
+
function readLabels2(layout) {
|
|
2070
|
+
const hint = asRecord2(asRecord2(layout)?.["labels"]) ?? {};
|
|
2071
|
+
return {
|
|
2072
|
+
rolling: readLabel(hint["rolling"], PANEL_DEFAULT_LABELS.rolling),
|
|
2073
|
+
weekly: readLabel(hint["weekly"], PANEL_DEFAULT_LABELS.weekly),
|
|
2074
|
+
monthly: readLabel(hint["monthly"], PANEL_DEFAULT_LABELS.monthly)
|
|
2075
|
+
};
|
|
2076
|
+
}
|
|
2077
|
+
function markIndex(nowMs, blinkMs) {
|
|
2078
|
+
if (!Number.isFinite(nowMs) || !Number.isFinite(blinkMs) || blinkMs <= 0)
|
|
2079
|
+
return 0;
|
|
2080
|
+
const step = Math.floor(nowMs / blinkMs) % MARK_FRAMES.length;
|
|
2081
|
+
return (step % MARK_FRAMES.length + MARK_FRAMES.length) % MARK_FRAMES.length;
|
|
2082
|
+
}
|
|
2083
|
+
function markToneAt(index) {
|
|
2084
|
+
return index === 2 ? undefined : "subdued";
|
|
2085
|
+
}
|
|
2086
|
+
function readLook(layout) {
|
|
2087
|
+
const hint = asRecord2(layout) ?? {};
|
|
2088
|
+
const header = readLookFlag(hint["header"], PANEL_DEFAULT_HEADER);
|
|
2089
|
+
const showPercent = readLookFlag(hint["percent"], PANEL_DEFAULT_PERCENT);
|
|
2090
|
+
const showReset = readLookFlag(hint["reset"], PANEL_DEFAULT_RESET);
|
|
2091
|
+
const rawAlign = hint["align"];
|
|
2092
|
+
const align = rawAlign === "left" || rawAlign === "right" || rawAlign === "center" ? rawAlign : PANEL_DEFAULT_ALIGN;
|
|
2093
|
+
const sweep = readLookFlag(hint["sweep"], PANEL_DEFAULT_SWEEP);
|
|
2094
|
+
const rawLabel = hint["labelWidth"];
|
|
2095
|
+
const flooredLabel = rawLabel === undefined ? undefined : Math.floor(asCount2(rawLabel) ?? Number.NaN);
|
|
2096
|
+
const configuredLabelWidth = flooredLabel !== undefined && Number.isFinite(flooredLabel) && flooredLabel >= PANEL_MIN_LABEL_WIDTH && flooredLabel <= PANEL_MAX_LABEL_WIDTH ? flooredLabel : PANEL_DEFAULT_LABEL_WIDTH;
|
|
2097
|
+
const labels = readLabels2(layout);
|
|
2098
|
+
const longestLabel = Math.max(labels.rolling.length, labels.weekly.length, labels.monthly.length);
|
|
2099
|
+
const labelWidth = Math.max(configuredLabelWidth, longestLabel);
|
|
2100
|
+
const blink = readLookFlag(hint["blink"], PANEL_DEFAULT_BLINK);
|
|
2101
|
+
const rawBlinkMs = hint["blinkMs"];
|
|
2102
|
+
const flooredBlinkMs = rawBlinkMs === undefined ? undefined : Math.floor(asCount2(rawBlinkMs) ?? Number.NaN);
|
|
2103
|
+
const blinkMs = flooredBlinkMs !== undefined && Number.isFinite(flooredBlinkMs) && flooredBlinkMs >= PANEL_MIN_BLINK_MS && flooredBlinkMs <= PANEL_MAX_BLINK_MS ? flooredBlinkMs : PANEL_DEFAULT_BLINK_MS;
|
|
2104
|
+
const rawBar = hint["barWidth"];
|
|
2105
|
+
const flooredBar = rawBar === undefined ? undefined : Math.floor(asCount2(rawBar) ?? Number.NaN);
|
|
2106
|
+
const namedBar = flooredBar !== undefined && Number.isFinite(flooredBar) && flooredBar >= 1 ? Math.min(flooredBar, PANEL_MAX_NAMED_BAR_WIDTH) : PANEL_DEFAULT_BAR_WIDTH;
|
|
2107
|
+
const markWidth = blink ? MARK_WIDTH : 0;
|
|
2108
|
+
const fixed = markWidth + labelWidth + 1 + (showPercent ? 1 + PANEL_PERCENT_WIDTH : 0) + (showReset ? 1 + PANEL_RESET_WIDTH : 0);
|
|
2109
|
+
const maxBar = Math.max(1, PANEL_SIDEBAR_BUDGET - fixed);
|
|
2110
|
+
return { header, barWidth: Math.min(namedBar, maxBar), labelWidth, showPercent, showReset, align, sweep, labels, blink, blinkMs };
|
|
2111
|
+
}
|
|
2112
|
+
function windowById(usage) {
|
|
2113
|
+
const byId = new Map;
|
|
2114
|
+
const list = asRecord2(usage)?.["windows"];
|
|
2115
|
+
if (!Array.isArray(list))
|
|
2116
|
+
return byId;
|
|
2117
|
+
for (const entry of list) {
|
|
2118
|
+
const record = asRecord2(entry);
|
|
2119
|
+
if (record === undefined)
|
|
2120
|
+
continue;
|
|
2121
|
+
const id = record["id"];
|
|
2122
|
+
if (id !== "5h" && id !== "1w" && id !== "1m")
|
|
2123
|
+
continue;
|
|
2124
|
+
if (byId.has(id))
|
|
2125
|
+
continue;
|
|
2126
|
+
const window = { id };
|
|
2127
|
+
const ratio = asCount2(record["ratio"]);
|
|
2128
|
+
if (ratio !== undefined)
|
|
2129
|
+
window.ratio = Math.min(1, ratio);
|
|
2130
|
+
const resetAtMs = asCount2(record["resetAtMs"]);
|
|
2131
|
+
if (resetAtMs !== undefined)
|
|
2132
|
+
window.resetAtMs = resetAtMs;
|
|
2133
|
+
const status = record["status"];
|
|
2134
|
+
if (typeof status === "string" && status !== "")
|
|
2135
|
+
window.status = status;
|
|
2136
|
+
byId.set(id, window);
|
|
2137
|
+
}
|
|
2138
|
+
return byId;
|
|
2139
|
+
}
|
|
2140
|
+
function formatCompactRemaining(ms) {
|
|
2141
|
+
const totalSeconds = Math.ceil(ms / 1000);
|
|
2142
|
+
if (totalSeconds < 60)
|
|
2143
|
+
return `${totalSeconds}s`;
|
|
2144
|
+
const totalMinutes = Math.ceil(ms / 60000);
|
|
2145
|
+
if (totalMinutes < 60)
|
|
2146
|
+
return `${totalMinutes}m`;
|
|
2147
|
+
const totalHours = Math.floor(ms / 3600000);
|
|
2148
|
+
if (totalHours < 24) {
|
|
2149
|
+
if (totalHours === 0)
|
|
2150
|
+
return `${totalMinutes}m`;
|
|
2151
|
+
const minutes = Math.floor((ms - totalHours * 3600000) / 60000);
|
|
2152
|
+
return minutes > 0 ? `${totalHours}h${minutes}m` : `${totalHours}h`;
|
|
2153
|
+
}
|
|
2154
|
+
const totalDays = Math.floor(ms / 86400000);
|
|
2155
|
+
const hours = Math.floor((ms - totalDays * 86400000) / 3600000);
|
|
2156
|
+
return hours > 0 ? `${totalDays}d${hours}h` : `${totalDays}d`;
|
|
2157
|
+
}
|
|
2158
|
+
function resetCountdown(window, nowMs) {
|
|
2159
|
+
const resetAtMs = window.resetAtMs;
|
|
2160
|
+
if (resetAtMs === undefined || !Number.isFinite(resetAtMs) || !Number.isFinite(nowMs))
|
|
2161
|
+
return;
|
|
2162
|
+
const remaining = resetAtMs - nowMs;
|
|
2163
|
+
if (remaining <= 0)
|
|
2164
|
+
return;
|
|
2165
|
+
return formatCompactRemaining(remaining);
|
|
2166
|
+
}
|
|
2167
|
+
function panelTone(window) {
|
|
2168
|
+
if (goTone(window) === "error")
|
|
2169
|
+
return "error";
|
|
2170
|
+
const ratio = window.ratio;
|
|
2171
|
+
if (ratio !== undefined && ratio >= GO_WARNING_RATIO)
|
|
2172
|
+
return "warning";
|
|
2173
|
+
return;
|
|
2174
|
+
}
|
|
2175
|
+
function readFrame(frame) {
|
|
2176
|
+
return typeof frame === "number" && Number.isFinite(frame) && frame >= 0 ? Math.floor(frame) : undefined;
|
|
2177
|
+
}
|
|
2178
|
+
function sweepIndex(frame, filled, sweep) {
|
|
2179
|
+
if (!sweep || frame === undefined || filled <= 0)
|
|
2180
|
+
return -1;
|
|
2181
|
+
return frame % filled;
|
|
2182
|
+
}
|
|
2183
|
+
function appendRun(runs, text, tone) {
|
|
2184
|
+
if (text === "")
|
|
2185
|
+
return;
|
|
2186
|
+
const last = runs[runs.length - 1];
|
|
2187
|
+
if (last !== undefined && (last.tone ?? undefined) === tone) {
|
|
2188
|
+
runs[runs.length - 1] = tone === undefined ? { text: last.text + text } : { text: last.text + text, tone };
|
|
2189
|
+
return;
|
|
2190
|
+
}
|
|
2191
|
+
runs.push(tone === undefined ? { text } : { text, tone });
|
|
2192
|
+
}
|
|
2193
|
+
function readReason(reason) {
|
|
2194
|
+
return typeof reason === "string" && /^[a-z][a-z0-9-]{0,15}$/.test(reason) ? reason : undefined;
|
|
2195
|
+
}
|
|
2196
|
+
function headerLine() {
|
|
2197
|
+
const text = ` ${HEADER_GLYPH} OPENCODE GO`;
|
|
2198
|
+
return { field: "go", text, segments: [{ text, tone: "subdued" }] };
|
|
2199
|
+
}
|
|
2200
|
+
function markField(look, nowMs, isFirst) {
|
|
2201
|
+
if (!look.blink)
|
|
2202
|
+
return;
|
|
2203
|
+
if (!isFirst)
|
|
2204
|
+
return { text: " " };
|
|
2205
|
+
const index = markIndex(nowMs, look.blinkMs);
|
|
2206
|
+
const text = `${MARK_FRAMES[index] ?? MARK_REST} `;
|
|
2207
|
+
const tone = markToneAt(index);
|
|
2208
|
+
return tone === undefined ? { text } : { text, tone };
|
|
2209
|
+
}
|
|
2210
|
+
function restingLine(label, look, reason, nowMs, isFirst) {
|
|
2211
|
+
const cells = Math.max(1, Math.floor(look.barWidth));
|
|
2212
|
+
const segments = [];
|
|
2213
|
+
const mark = markField(look, nowMs, isFirst);
|
|
2214
|
+
if (mark !== undefined)
|
|
2215
|
+
segments.push(mark);
|
|
2216
|
+
segments.push({ text: `${labelField(label, look.labelWidth)} ` });
|
|
2217
|
+
let trackCells = cells;
|
|
2218
|
+
if (reason !== undefined) {
|
|
2219
|
+
segments.push({ text: `${reason} `, tone: "subdued" });
|
|
2220
|
+
trackCells = Math.max(0, cells - reason.length - 1);
|
|
2221
|
+
}
|
|
2222
|
+
const tail = CELL.repeat(trackCells) + (look.showPercent ? ` ${alignCell(DEFAULT_PLACEHOLDER, PANEL_PERCENT_WIDTH, look.align)}` : "");
|
|
2223
|
+
segments.push({ text: tail, tone: "subdued" });
|
|
2224
|
+
return { field: "go", text: segments.map((segment) => segment.text).join(""), segments };
|
|
2225
|
+
}
|
|
2226
|
+
function valueLine(label, look, window, nowMs, frame, isFirst) {
|
|
2227
|
+
const tone = panelTone(window);
|
|
2228
|
+
const ratio = window.ratio;
|
|
2229
|
+
const cells = Math.max(1, Math.floor(look.barWidth));
|
|
2230
|
+
const filled = Math.max(0, Math.min(cells, Math.round((ratio ?? 0) * cells)));
|
|
2231
|
+
const sweep = sweepIndex(frame, filled, look.sweep);
|
|
2232
|
+
const fillTone = tone ?? "default";
|
|
2233
|
+
const runs = [];
|
|
2234
|
+
for (let index = 0;index < filled; index += 1) {
|
|
2235
|
+
appendRun(runs, CELL, index === sweep ? SWEEP_TONE : fillTone);
|
|
2236
|
+
}
|
|
2237
|
+
for (let index = filled;index < cells; index += 1) {
|
|
2238
|
+
appendRun(runs, CELL, "subdued");
|
|
2239
|
+
}
|
|
2240
|
+
if (look.showPercent) {
|
|
2241
|
+
const percent = ratio === undefined ? DEFAULT_PLACEHOLDER : `${Math.round(ratio * 100)}%`;
|
|
2242
|
+
appendRun(runs, ` ${alignCell(percent, PANEL_PERCENT_WIDTH, look.align)}`, tone);
|
|
2243
|
+
}
|
|
2244
|
+
const segments = [];
|
|
2245
|
+
const mark = markField(look, nowMs, isFirst);
|
|
2246
|
+
if (mark !== undefined)
|
|
2247
|
+
segments.push(mark);
|
|
2248
|
+
segments.push({ text: `${labelField(label, look.labelWidth)} ` }, ...runs);
|
|
2249
|
+
if (look.showReset) {
|
|
2250
|
+
const countdown = resetCountdown(window, nowMs);
|
|
2251
|
+
if (countdown !== undefined) {
|
|
2252
|
+
segments.push({ text: ` ${alignCell(countdown, PANEL_RESET_WIDTH, look.align)}`, tone: "subdued" });
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
return { field: "go", text: segments.map((segment) => segment.text).join(""), segments };
|
|
2256
|
+
}
|
|
2257
|
+
function goPanelLines(usage, nowMs, layout, frame, reason) {
|
|
2258
|
+
try {
|
|
2259
|
+
const look = readLook(layout);
|
|
2260
|
+
const windows = windowById(usage);
|
|
2261
|
+
const tag = windows.size === 0 ? readReason(reason) : undefined;
|
|
2262
|
+
const sweepFrame = readFrame(frame);
|
|
2263
|
+
const rows = PANEL_WINDOWS.map((id, index) => {
|
|
2264
|
+
const label = look.labels[labelKeyFor(id)];
|
|
2265
|
+
const window = windows.get(id);
|
|
2266
|
+
return window === undefined ? restingLine(label, look, index === 0 ? tag : undefined, nowMs, index === 0) : valueLine(label, look, window, nowMs, sweepFrame, index === 0);
|
|
2267
|
+
});
|
|
2268
|
+
return look.header ? [headerLine(), ...rows] : rows;
|
|
2269
|
+
} catch {
|
|
2270
|
+
const fallback = readLook(undefined);
|
|
2271
|
+
return PANEL_WINDOWS.map((id, index) => restingLine(fallback.labels[labelKeyFor(id)], fallback, undefined, nowMs, index === 0));
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
|
|
2275
|
+
// src/tui/guard.ts
|
|
2276
|
+
var GUARD_KEY = "flight-deck.guard";
|
|
2277
|
+
var GUARD_POLL_MS = 1e4;
|
|
2278
|
+
var GUARD_CONTRACT = {
|
|
2279
|
+
id: "harness-guard",
|
|
2280
|
+
methods: {
|
|
2281
|
+
status: {
|
|
2282
|
+
input: { type: "object", properties: { sessionID: { type: "string" } }, required: ["sessionID"] },
|
|
2283
|
+
output: { type: "object" }
|
|
2284
|
+
}
|
|
2285
|
+
},
|
|
2286
|
+
events: {}
|
|
2287
|
+
};
|
|
2288
|
+
function asRecord3(value) {
|
|
2289
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : undefined;
|
|
2290
|
+
}
|
|
2291
|
+
function asCount3(value) {
|
|
2292
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0)
|
|
2293
|
+
return Math.floor(value);
|
|
2294
|
+
if (Array.isArray(value))
|
|
2295
|
+
return value.length;
|
|
2296
|
+
return;
|
|
2297
|
+
}
|
|
2298
|
+
function countOrZero(...candidates) {
|
|
2299
|
+
for (const candidate of candidates) {
|
|
2300
|
+
const count = asCount3(candidate);
|
|
2301
|
+
if (count !== undefined)
|
|
2302
|
+
return count;
|
|
2303
|
+
}
|
|
2304
|
+
return 0;
|
|
2305
|
+
}
|
|
2306
|
+
function normalizeAirworthiness(value) {
|
|
2307
|
+
const section = asRecord3(value);
|
|
2308
|
+
if (section === undefined)
|
|
2309
|
+
return;
|
|
2310
|
+
if (section.available !== true && section.available !== false)
|
|
2311
|
+
return;
|
|
2312
|
+
return {
|
|
2313
|
+
available: section.available === true,
|
|
2314
|
+
findings: countOrZero(section.findings, section.counts)
|
|
2315
|
+
};
|
|
2316
|
+
}
|
|
2317
|
+
function normalizeWarden(value) {
|
|
2318
|
+
const section = asRecord3(value);
|
|
2319
|
+
if (section === undefined)
|
|
2320
|
+
return;
|
|
2321
|
+
if (section.available !== true && section.available !== false)
|
|
2322
|
+
return;
|
|
2323
|
+
return {
|
|
2324
|
+
available: section.available === true,
|
|
2325
|
+
breaches: countOrZero(section.breaches),
|
|
2326
|
+
orphans: countOrZero(section.orphans)
|
|
2327
|
+
};
|
|
2328
|
+
}
|
|
2329
|
+
function normalizeFlightPlan(value) {
|
|
2330
|
+
const section = asRecord3(value);
|
|
2331
|
+
if (section === undefined)
|
|
2332
|
+
return;
|
|
2333
|
+
if (section.available !== true && section.available !== false)
|
|
2334
|
+
return;
|
|
2335
|
+
return { available: section.available === true };
|
|
2336
|
+
}
|
|
2337
|
+
function normalizeGuardStatus(value) {
|
|
2338
|
+
try {
|
|
2339
|
+
const top = asRecord3(value);
|
|
2340
|
+
if (top === undefined)
|
|
2341
|
+
return;
|
|
2342
|
+
const airworthiness = normalizeAirworthiness(top.airworthiness);
|
|
2343
|
+
const warden = normalizeWarden(top.warden);
|
|
2344
|
+
const flightPlan = normalizeFlightPlan(top.flightPlan);
|
|
2345
|
+
if (airworthiness === undefined && warden === undefined && flightPlan === undefined)
|
|
2346
|
+
return;
|
|
2347
|
+
const status = {};
|
|
2348
|
+
if (airworthiness !== undefined)
|
|
2349
|
+
status.airworthiness = airworthiness;
|
|
2350
|
+
if (warden !== undefined)
|
|
2351
|
+
status.warden = warden;
|
|
2352
|
+
if (flightPlan !== undefined)
|
|
2353
|
+
status.flightPlan = flightPlan;
|
|
2354
|
+
return status;
|
|
2355
|
+
} catch {
|
|
2356
|
+
return;
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
function openStore(storage) {
|
|
2360
|
+
let opened;
|
|
2361
|
+
try {
|
|
2362
|
+
opened = storage.memory(GUARD_KEY, { initial: { value: null } });
|
|
2363
|
+
} catch {
|
|
2364
|
+
return;
|
|
2365
|
+
}
|
|
2366
|
+
if (!Array.isArray(opened) || opened.length < 2)
|
|
2367
|
+
return;
|
|
2368
|
+
const [store, update] = opened;
|
|
2369
|
+
if (store === null || typeof store !== "object")
|
|
2370
|
+
return;
|
|
2371
|
+
if (typeof update !== "function")
|
|
2372
|
+
return;
|
|
2373
|
+
return { store, update };
|
|
2374
|
+
}
|
|
2375
|
+
function readStatus(store) {
|
|
2376
|
+
try {
|
|
2377
|
+
const value = store?.value;
|
|
2378
|
+
if (value === null || value === undefined)
|
|
2379
|
+
return;
|
|
2380
|
+
return normalizeGuardStatus(value);
|
|
2381
|
+
} catch {
|
|
2382
|
+
return;
|
|
2383
|
+
}
|
|
2384
|
+
}
|
|
2385
|
+
function resolveStatusFn(host, deps) {
|
|
2386
|
+
if (deps !== undefined) {
|
|
2387
|
+
try {
|
|
2388
|
+
if (typeof deps.status === "function")
|
|
2389
|
+
return deps.status;
|
|
2390
|
+
} catch {
|
|
2391
|
+
return;
|
|
2392
|
+
}
|
|
2393
|
+
return;
|
|
2394
|
+
}
|
|
2395
|
+
try {
|
|
2396
|
+
const client = asRecord3(host.client);
|
|
2397
|
+
const rpc = client?.rpc;
|
|
2398
|
+
if (typeof rpc !== "function")
|
|
2399
|
+
return;
|
|
2400
|
+
const factory = rpc;
|
|
2401
|
+
const remote = factory(GUARD_CONTRACT);
|
|
2402
|
+
const status = remote.status;
|
|
2403
|
+
if (typeof status !== "function")
|
|
2404
|
+
return;
|
|
2405
|
+
return (input) => status(input);
|
|
2406
|
+
} catch {
|
|
2407
|
+
return;
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
function writeValue(opened, value) {
|
|
2411
|
+
try {
|
|
2412
|
+
opened.update((draft) => {
|
|
2413
|
+
draft.value = value;
|
|
2414
|
+
});
|
|
2415
|
+
return true;
|
|
2416
|
+
} catch {
|
|
2417
|
+
return false;
|
|
2418
|
+
}
|
|
2419
|
+
}
|
|
2420
|
+
function startGuardBridge(host, deps, intervalMs = GUARD_POLL_MS) {
|
|
2421
|
+
const storage = host?.storage;
|
|
2422
|
+
if (host === undefined || storage === undefined || typeof storage.memory !== "function")
|
|
2423
|
+
return;
|
|
2424
|
+
const opened = openStore(storage);
|
|
2425
|
+
if (opened === undefined)
|
|
2426
|
+
return;
|
|
2427
|
+
const cadence = Number.isFinite(intervalMs) && intervalMs > 0 ? intervalMs : GUARD_POLL_MS;
|
|
2428
|
+
let currentSessionID;
|
|
2429
|
+
let timer;
|
|
2430
|
+
let generation = 0;
|
|
2431
|
+
let nextSeq = 0;
|
|
2432
|
+
let latestSeq = 0;
|
|
2433
|
+
let disposed = false;
|
|
2434
|
+
const stopTimer = () => {
|
|
2435
|
+
if (timer !== undefined)
|
|
2436
|
+
clearInterval(timer);
|
|
2437
|
+
timer = undefined;
|
|
2438
|
+
};
|
|
2439
|
+
const poll = async (sessionID) => {
|
|
2440
|
+
if (disposed)
|
|
2441
|
+
return;
|
|
2442
|
+
if (sessionID === undefined || sessionID.length === 0)
|
|
2443
|
+
return;
|
|
2444
|
+
const myGeneration = generation;
|
|
2445
|
+
const mySeq = ++nextSeq;
|
|
2446
|
+
latestSeq = mySeq;
|
|
2447
|
+
const stillCurrent = () => myGeneration === generation && mySeq === latestSeq && !disposed;
|
|
2448
|
+
let raw;
|
|
2449
|
+
try {
|
|
2450
|
+
const statusFn = resolveStatusFn(host, deps);
|
|
2451
|
+
if (statusFn === undefined) {
|
|
2452
|
+
if (!stillCurrent())
|
|
2453
|
+
return;
|
|
2454
|
+
if (currentSessionID !== sessionID)
|
|
2455
|
+
return;
|
|
2456
|
+
if (!writeValue(opened, null))
|
|
2457
|
+
stopTimer();
|
|
2458
|
+
return;
|
|
2459
|
+
}
|
|
2460
|
+
raw = await statusFn({ sessionID });
|
|
2461
|
+
} catch {
|
|
2462
|
+
if (!stillCurrent())
|
|
2463
|
+
return;
|
|
2464
|
+
if (currentSessionID !== sessionID)
|
|
2465
|
+
return;
|
|
2466
|
+
if (!writeValue(opened, null))
|
|
2467
|
+
stopTimer();
|
|
2468
|
+
return;
|
|
2469
|
+
}
|
|
2470
|
+
if (!stillCurrent())
|
|
2471
|
+
return;
|
|
2472
|
+
if (currentSessionID !== sessionID)
|
|
2473
|
+
return;
|
|
2474
|
+
let normalized;
|
|
2475
|
+
try {
|
|
2476
|
+
normalized = normalizeGuardStatus(raw) ?? null;
|
|
2477
|
+
} catch {
|
|
2478
|
+
normalized = null;
|
|
2479
|
+
}
|
|
2480
|
+
if (!writeValue(opened, normalized))
|
|
2481
|
+
stopTimer();
|
|
2482
|
+
};
|
|
2483
|
+
timer = setInterval(() => {
|
|
2484
|
+
const sessionID = currentSessionID;
|
|
2485
|
+
if (sessionID === undefined)
|
|
2486
|
+
return;
|
|
2487
|
+
poll(sessionID);
|
|
2488
|
+
}, cadence);
|
|
2489
|
+
return {
|
|
2490
|
+
get status() {
|
|
2491
|
+
return readStatus(opened.store);
|
|
2492
|
+
},
|
|
2493
|
+
follow(sessionID) {
|
|
2494
|
+
try {
|
|
2495
|
+
if (disposed)
|
|
2496
|
+
return;
|
|
2497
|
+
const next = typeof sessionID === "string" && sessionID.length > 0 ? sessionID : undefined;
|
|
2498
|
+
if (next === currentSessionID)
|
|
2499
|
+
return;
|
|
2500
|
+
generation += 1;
|
|
2501
|
+
currentSessionID = next;
|
|
2502
|
+
if (next === undefined) {
|
|
2503
|
+
if (!writeValue(opened, null))
|
|
2504
|
+
stopTimer();
|
|
2505
|
+
return;
|
|
2506
|
+
}
|
|
2507
|
+
if (!writeValue(opened, null)) {
|
|
2508
|
+
stopTimer();
|
|
2509
|
+
return;
|
|
2510
|
+
}
|
|
2511
|
+
poll(next);
|
|
2512
|
+
} catch {}
|
|
2513
|
+
},
|
|
2514
|
+
dispose() {
|
|
2515
|
+
disposed = true;
|
|
2516
|
+
generation += 1;
|
|
2517
|
+
stopTimer();
|
|
2518
|
+
}
|
|
2519
|
+
};
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
// src/tui/go.ts
|
|
2523
|
+
import { appendFileSync, existsSync as existsSync3, mkdirSync, readFileSync as readFileSync2, writeFileSync } from "fs";
|
|
2524
|
+
import { tmpdir } from "os";
|
|
2525
|
+
import { join as join3 } from "path";
|
|
2526
|
+
var GO_KEY = "flight-deck.go";
|
|
2527
|
+
var GO_POLL_MS = 60000;
|
|
2528
|
+
var GO_USAGE_URL = "https://opencode.ai/zen/go/v1/usage";
|
|
2529
|
+
var GO_INTEGRATION_ID = "opencode-go";
|
|
2530
|
+
var GO_TIMEOUT_MS = 1e4;
|
|
2531
|
+
var GO_CREDENTIAL_TIMEOUT_MS = 6000;
|
|
2532
|
+
var GO_LOG_DIR = "opencode";
|
|
2533
|
+
var GO_LOG_FILE = "flight-deck-go.log";
|
|
2534
|
+
var GO_LOG_ENV = "FLIGHT_DECK_GO_LOG";
|
|
2535
|
+
function goLogEnabled() {
|
|
2536
|
+
try {
|
|
2537
|
+
return process.env[GO_LOG_ENV] === "1";
|
|
2538
|
+
} catch {
|
|
2539
|
+
return false;
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
var GO_LOG_MAX_LINES = 200;
|
|
2543
|
+
async function defaultFetchJson(url, init) {
|
|
2544
|
+
const response = await fetch(url, init);
|
|
2545
|
+
if (!response.ok) {
|
|
2546
|
+
const error = new Error(`go usage: HTTP ${response.status}`);
|
|
2547
|
+
error.status = response.status;
|
|
2548
|
+
throw error;
|
|
2549
|
+
}
|
|
2550
|
+
return response.json();
|
|
2551
|
+
}
|
|
2552
|
+
function openStore2(storage) {
|
|
2553
|
+
let opened;
|
|
2554
|
+
try {
|
|
2555
|
+
opened = storage.memory(GO_KEY, { initial: { value: null, reason: null } });
|
|
2556
|
+
} catch {
|
|
2557
|
+
return;
|
|
2558
|
+
}
|
|
2559
|
+
if (!Array.isArray(opened) || opened.length < 2)
|
|
2560
|
+
return;
|
|
2561
|
+
const [store, update] = opened;
|
|
2562
|
+
if (store === null || typeof store !== "object")
|
|
2563
|
+
return;
|
|
2564
|
+
if (typeof update !== "function")
|
|
2565
|
+
return;
|
|
2566
|
+
return { store, update };
|
|
2567
|
+
}
|
|
2568
|
+
var GO_WINDOW_ORDER = ["5h", "1w", "1m"];
|
|
2569
|
+
function asStoredNumber(value) {
|
|
2570
|
+
try {
|
|
2571
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
2572
|
+
} catch {
|
|
2573
|
+
return;
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2576
|
+
function asStoredWindow(entry, seen) {
|
|
2577
|
+
try {
|
|
2578
|
+
if (entry === null || typeof entry !== "object" || Array.isArray(entry))
|
|
2579
|
+
return;
|
|
2580
|
+
const record = entry;
|
|
2581
|
+
let id;
|
|
2582
|
+
let ratioRaw;
|
|
2583
|
+
let usedRaw;
|
|
2584
|
+
let limitRaw;
|
|
2585
|
+
let resetRaw;
|
|
2586
|
+
let statusRaw;
|
|
2587
|
+
try {
|
|
2588
|
+
id = record["id"];
|
|
2589
|
+
} catch {
|
|
2590
|
+
return;
|
|
2591
|
+
}
|
|
2592
|
+
if (id !== "5h" && id !== "1w" && id !== "1m")
|
|
2593
|
+
return;
|
|
2594
|
+
if (seen.has(id))
|
|
2595
|
+
return;
|
|
2596
|
+
try {
|
|
2597
|
+
ratioRaw = record["ratio"];
|
|
2598
|
+
} catch {
|
|
2599
|
+
return;
|
|
2600
|
+
}
|
|
2601
|
+
let ratio;
|
|
2602
|
+
if (ratioRaw !== undefined) {
|
|
2603
|
+
const finite = asStoredNumber(ratioRaw);
|
|
2604
|
+
if (finite === undefined)
|
|
2605
|
+
return;
|
|
2606
|
+
ratio = Math.min(1, Math.max(0, finite));
|
|
2607
|
+
}
|
|
2608
|
+
let used;
|
|
2609
|
+
let limit;
|
|
2610
|
+
let resetAtMs;
|
|
2611
|
+
let status;
|
|
2612
|
+
try {
|
|
2613
|
+
usedRaw = record["used"];
|
|
2614
|
+
limitRaw = record["limit"];
|
|
2615
|
+
resetRaw = record["resetAtMs"];
|
|
2616
|
+
statusRaw = record["status"];
|
|
2617
|
+
} catch {
|
|
2618
|
+
return;
|
|
2619
|
+
}
|
|
2620
|
+
used = asStoredNumber(usedRaw);
|
|
2621
|
+
limit = asStoredNumber(limitRaw);
|
|
2622
|
+
resetAtMs = asStoredNumber(resetRaw);
|
|
2623
|
+
try {
|
|
2624
|
+
if (typeof statusRaw === "string" && statusRaw.trim().length > 0)
|
|
2625
|
+
status = statusRaw.trim();
|
|
2626
|
+
} catch {
|
|
2627
|
+
status = undefined;
|
|
2628
|
+
}
|
|
2629
|
+
if (used === undefined && limit === undefined && ratio === undefined)
|
|
2630
|
+
return;
|
|
2631
|
+
seen.add(id);
|
|
2632
|
+
const result = { id };
|
|
2633
|
+
if (used !== undefined)
|
|
2634
|
+
result.used = used;
|
|
2635
|
+
if (limit !== undefined)
|
|
2636
|
+
result.limit = limit;
|
|
2637
|
+
if (ratio !== undefined)
|
|
2638
|
+
result.ratio = ratio;
|
|
2639
|
+
if (resetAtMs !== undefined)
|
|
2640
|
+
result.resetAtMs = resetAtMs;
|
|
2641
|
+
if (status !== undefined)
|
|
2642
|
+
result.status = status;
|
|
2643
|
+
return result;
|
|
2644
|
+
} catch {
|
|
2645
|
+
return;
|
|
2646
|
+
}
|
|
2647
|
+
}
|
|
2648
|
+
function readUsage(store) {
|
|
2649
|
+
try {
|
|
2650
|
+
const value = store?.value;
|
|
2651
|
+
if (value === null || value === undefined)
|
|
2652
|
+
return;
|
|
2653
|
+
if (typeof value !== "object" || Array.isArray(value))
|
|
2654
|
+
return;
|
|
2655
|
+
let windowsRaw;
|
|
2656
|
+
try {
|
|
2657
|
+
windowsRaw = value["windows"];
|
|
2658
|
+
} catch {
|
|
2659
|
+
return;
|
|
2660
|
+
}
|
|
2661
|
+
if (!Array.isArray(windowsRaw))
|
|
2662
|
+
return;
|
|
2663
|
+
const windows = [];
|
|
2664
|
+
const seen = new Set;
|
|
2665
|
+
for (const entry of windowsRaw) {
|
|
2666
|
+
const window = asStoredWindow(entry, seen);
|
|
2667
|
+
if (window !== undefined)
|
|
2668
|
+
windows.push(window);
|
|
2669
|
+
}
|
|
2670
|
+
if (windows.length === 0)
|
|
2671
|
+
return;
|
|
2672
|
+
windows.sort((a, b) => GO_WINDOW_ORDER.indexOf(a.id) - GO_WINDOW_ORDER.indexOf(b.id));
|
|
2673
|
+
return { windows };
|
|
2674
|
+
} catch {
|
|
2675
|
+
return;
|
|
2676
|
+
}
|
|
2677
|
+
}
|
|
2678
|
+
var GO_NO_DATA_REASONS = [
|
|
2679
|
+
"no-client",
|
|
2680
|
+
"no-key",
|
|
2681
|
+
"http",
|
|
2682
|
+
"parse",
|
|
2683
|
+
"timeout",
|
|
2684
|
+
"pending"
|
|
2685
|
+
];
|
|
2686
|
+
function readReason2(store) {
|
|
2687
|
+
try {
|
|
2688
|
+
const reason = store?.reason;
|
|
2689
|
+
if (typeof reason !== "string")
|
|
2690
|
+
return;
|
|
2691
|
+
return GO_NO_DATA_REASONS.includes(reason) ? reason : undefined;
|
|
2692
|
+
} catch {
|
|
2693
|
+
return;
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
function writeValue2(opened, value, reason) {
|
|
2697
|
+
try {
|
|
2698
|
+
opened.update((draft) => {
|
|
2699
|
+
draft.value = value;
|
|
2700
|
+
draft.reason = value === null ? reason : null;
|
|
2701
|
+
});
|
|
2702
|
+
return true;
|
|
2703
|
+
} catch {
|
|
2704
|
+
return false;
|
|
2705
|
+
}
|
|
2706
|
+
}
|
|
2707
|
+
function resolveKeyFn(deps) {
|
|
2708
|
+
if (deps !== undefined) {
|
|
2709
|
+
try {
|
|
2710
|
+
if (typeof deps.key === "function")
|
|
2711
|
+
return deps.key;
|
|
2712
|
+
} catch {}
|
|
2713
|
+
}
|
|
2714
|
+
return;
|
|
2715
|
+
}
|
|
2716
|
+
function resolveAsyncKeyFn(deps) {
|
|
2717
|
+
if (deps !== undefined) {
|
|
2718
|
+
try {
|
|
2719
|
+
if (typeof deps.resolveKey === "function")
|
|
2720
|
+
return deps.resolveKey;
|
|
2721
|
+
} catch {}
|
|
2722
|
+
}
|
|
2723
|
+
return;
|
|
2724
|
+
}
|
|
2725
|
+
function keyOfValue(value) {
|
|
2726
|
+
if (value === null || typeof value !== "object")
|
|
2727
|
+
return;
|
|
2728
|
+
const record = value;
|
|
2729
|
+
if (record["type"] !== "key")
|
|
2730
|
+
return;
|
|
2731
|
+
const key = record["key"];
|
|
2732
|
+
return typeof key === "string" && key.length > 0 ? key : undefined;
|
|
2733
|
+
}
|
|
2734
|
+
function safeGet(target, key) {
|
|
2735
|
+
try {
|
|
2736
|
+
if (target === null || typeof target !== "object" && typeof target !== "function")
|
|
2737
|
+
return;
|
|
2738
|
+
return target[key];
|
|
2739
|
+
} catch {
|
|
2740
|
+
return;
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
function resolveCredentialTimeout(deps) {
|
|
2744
|
+
if (deps !== undefined) {
|
|
2745
|
+
try {
|
|
2746
|
+
const value = deps.credentialTimeoutMs;
|
|
2747
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0)
|
|
2748
|
+
return value;
|
|
2749
|
+
} catch {}
|
|
2750
|
+
}
|
|
2751
|
+
return GO_CREDENTIAL_TIMEOUT_MS;
|
|
2752
|
+
}
|
|
2753
|
+
function settleCall(call, ms) {
|
|
2754
|
+
let produced;
|
|
2755
|
+
try {
|
|
2756
|
+
produced = call();
|
|
2757
|
+
} catch {
|
|
2758
|
+
return Promise.resolve({ ok: false, timedOut: false });
|
|
2759
|
+
}
|
|
2760
|
+
const thenable = produced !== null && (typeof produced === "object" || typeof produced === "function") ? produced.then : undefined;
|
|
2761
|
+
if (typeof thenable !== "function")
|
|
2762
|
+
return Promise.resolve({ ok: true, value: produced });
|
|
2763
|
+
return new Promise((resolve) => {
|
|
2764
|
+
const timer = setTimeout(() => resolve({ ok: false, timedOut: true }), ms);
|
|
2765
|
+
produced.then((value) => {
|
|
2766
|
+
clearTimeout(timer);
|
|
2767
|
+
resolve({ ok: true, value });
|
|
2768
|
+
}, () => {
|
|
2769
|
+
clearTimeout(timer);
|
|
2770
|
+
resolve({ ok: false, timedOut: false });
|
|
2771
|
+
});
|
|
2772
|
+
});
|
|
2773
|
+
}
|
|
2774
|
+
function asList(value) {
|
|
2775
|
+
if (Array.isArray(value))
|
|
2776
|
+
return value;
|
|
2777
|
+
if (value === null || typeof value !== "object")
|
|
2778
|
+
return;
|
|
2779
|
+
const record = value;
|
|
2780
|
+
const data = record["data"];
|
|
2781
|
+
if (Array.isArray(data))
|
|
2782
|
+
return data;
|
|
2783
|
+
const output = record["output"];
|
|
2784
|
+
if (Array.isArray(output))
|
|
2785
|
+
return output;
|
|
2786
|
+
return;
|
|
2787
|
+
}
|
|
2788
|
+
function isActive(value) {
|
|
2789
|
+
return Boolean(value);
|
|
2790
|
+
}
|
|
2791
|
+
function goKeyFromEntries(entries) {
|
|
2792
|
+
let fallback;
|
|
2793
|
+
for (const entry of entries) {
|
|
2794
|
+
if (entry === null || typeof entry !== "object")
|
|
2795
|
+
continue;
|
|
2796
|
+
const record = entry;
|
|
2797
|
+
if (record["integrationID"] !== GO_INTEGRATION_ID)
|
|
2798
|
+
continue;
|
|
2799
|
+
const key = keyOfValue(record["value"]);
|
|
2800
|
+
if (key === undefined)
|
|
2801
|
+
continue;
|
|
2802
|
+
if (isActive(record["active"]))
|
|
2803
|
+
return key;
|
|
2804
|
+
if (fallback === undefined)
|
|
2805
|
+
fallback = key;
|
|
2806
|
+
}
|
|
2807
|
+
return fallback;
|
|
2808
|
+
}
|
|
2809
|
+
async function credentialAttempt(store, ms) {
|
|
2810
|
+
const list = safeGet(store, "list");
|
|
2811
|
+
if (typeof list !== "function")
|
|
2812
|
+
return { reason: "no-client", answered: false };
|
|
2813
|
+
const settled = await settleCall(() => list.call(store), ms);
|
|
2814
|
+
if (!settled.ok)
|
|
2815
|
+
return { reason: settled.timedOut ? "timeout" : "no-client", answered: false };
|
|
2816
|
+
const entries = asList(settled.value);
|
|
2817
|
+
if (entries === undefined)
|
|
2818
|
+
return { reason: "no-client", answered: false };
|
|
2819
|
+
const key = goKeyFromEntries(entries);
|
|
2820
|
+
return key === undefined ? { reason: "no-key", answered: true } : { key, answered: true };
|
|
2821
|
+
}
|
|
2822
|
+
function providerKeyOf(info) {
|
|
2823
|
+
if (info === null || typeof info !== "object")
|
|
2824
|
+
return;
|
|
2825
|
+
const record = info;
|
|
2826
|
+
if (record["id"] !== GO_INTEGRATION_ID && record["integrationID"] !== GO_INTEGRATION_ID)
|
|
2827
|
+
return;
|
|
2828
|
+
const settings = record["settings"];
|
|
2829
|
+
if (settings === null || typeof settings !== "object")
|
|
2830
|
+
return;
|
|
2831
|
+
const apiKey = settings["apiKey"];
|
|
2832
|
+
return typeof apiKey === "string" && apiKey.length > 0 ? apiKey : undefined;
|
|
2833
|
+
}
|
|
2834
|
+
function unwrapOne(value) {
|
|
2835
|
+
if (value === null || typeof value !== "object")
|
|
2836
|
+
return value;
|
|
2837
|
+
const data = value["data"];
|
|
2838
|
+
return data !== null && typeof data === "object" && !Array.isArray(data) ? data : value;
|
|
2839
|
+
}
|
|
2840
|
+
async function providerAttempt(provider, ms) {
|
|
2841
|
+
if (provider === null || typeof provider !== "object")
|
|
2842
|
+
return { reason: "no-client", answered: false };
|
|
2843
|
+
const collection = provider;
|
|
2844
|
+
const get = collection["get"];
|
|
2845
|
+
const list = collection["list"];
|
|
2846
|
+
if (typeof get === "function") {
|
|
2847
|
+
const settled = await settleCall(() => get.call(collection, GO_INTEGRATION_ID), ms);
|
|
2848
|
+
if (!settled.ok)
|
|
2849
|
+
return { reason: settled.timedOut ? "timeout" : "no-client", answered: false };
|
|
2850
|
+
if (settled.value === undefined || settled.value === null)
|
|
2851
|
+
return { reason: "no-key", answered: true };
|
|
2852
|
+
const key = providerKeyOf(unwrapOne(settled.value));
|
|
2853
|
+
return key === undefined ? { reason: "no-key", answered: true } : { key, answered: true };
|
|
2854
|
+
}
|
|
2855
|
+
if (typeof list === "function") {
|
|
2856
|
+
const settled = await settleCall(() => list.call(collection), ms);
|
|
2857
|
+
if (!settled.ok)
|
|
2858
|
+
return { reason: settled.timedOut ? "timeout" : "no-client", answered: false };
|
|
2859
|
+
const entries = asList(settled.value);
|
|
2860
|
+
if (entries === undefined)
|
|
2861
|
+
return { reason: "no-client", answered: false };
|
|
2862
|
+
let key;
|
|
2863
|
+
for (const entry of entries) {
|
|
2864
|
+
const candidate = providerKeyOf(entry);
|
|
2865
|
+
if (candidate !== undefined) {
|
|
2866
|
+
key = candidate;
|
|
2867
|
+
break;
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
return key === undefined ? { reason: "no-key", answered: true } : { key, answered: true };
|
|
2871
|
+
}
|
|
2872
|
+
return { reason: "no-client", answered: false };
|
|
2873
|
+
}
|
|
2874
|
+
function resolveFetchJson(deps) {
|
|
2875
|
+
if (deps !== undefined) {
|
|
2876
|
+
try {
|
|
2877
|
+
if (typeof deps.fetchJson === "function")
|
|
2878
|
+
return deps.fetchJson;
|
|
2879
|
+
} catch {}
|
|
2880
|
+
}
|
|
2881
|
+
return defaultFetchJson;
|
|
2882
|
+
}
|
|
2883
|
+
function httpOf(error) {
|
|
2884
|
+
const status = error !== null && typeof error === "object" ? error.status : undefined;
|
|
2885
|
+
return typeof status === "number" && Number.isFinite(status) ? String(status) : "n/a";
|
|
2886
|
+
}
|
|
2887
|
+
function logPoll(entry) {
|
|
2888
|
+
if (!goLogEnabled())
|
|
2889
|
+
return;
|
|
2890
|
+
try {
|
|
2891
|
+
const directory = join3(tmpdir(), GO_LOG_DIR);
|
|
2892
|
+
const path = join3(directory, GO_LOG_FILE);
|
|
2893
|
+
const line = `ts=${new Date().toISOString()} src=${entry.src} key=${entry.key ? "yes" : "no"} http=${entry.http} parse=${entry.parse} reason=${entry.reason}
|
|
2894
|
+
`;
|
|
2895
|
+
let prior = "";
|
|
2896
|
+
try {
|
|
2897
|
+
if (existsSync3(path))
|
|
2898
|
+
prior = readFileSync2(path, "utf8");
|
|
2899
|
+
} catch {
|
|
2900
|
+
prior = "";
|
|
2901
|
+
}
|
|
2902
|
+
const lines = prior.length === 0 ? 0 : prior.split(`
|
|
2903
|
+
`).filter((row) => row.length > 0).length;
|
|
2904
|
+
mkdirSync(directory, { recursive: true });
|
|
2905
|
+
if (lines >= GO_LOG_MAX_LINES)
|
|
2906
|
+
writeFileSync(path, line);
|
|
2907
|
+
else
|
|
2908
|
+
appendFileSync(path, line);
|
|
2909
|
+
} catch {}
|
|
2910
|
+
}
|
|
2911
|
+
function startGoBridge(host, deps, intervalMs = GO_POLL_MS) {
|
|
2912
|
+
const storage = host?.storage;
|
|
2913
|
+
if (host === undefined || storage === undefined || typeof storage.memory !== "function")
|
|
2914
|
+
return;
|
|
2915
|
+
const opened = openStore2(storage);
|
|
2916
|
+
if (opened === undefined)
|
|
2917
|
+
return;
|
|
2918
|
+
const cadence = Number.isFinite(intervalMs) && intervalMs > 0 ? intervalMs : GO_POLL_MS;
|
|
2919
|
+
const keyFn = resolveKeyFn(deps);
|
|
2920
|
+
const asyncKeyFn = resolveAsyncKeyFn(deps);
|
|
2921
|
+
const fetchJson = resolveFetchJson(deps);
|
|
2922
|
+
const credentialTimeoutMs = resolveCredentialTimeout(deps);
|
|
2923
|
+
const client = host.client;
|
|
2924
|
+
const directStore = safeGet(client, "credential");
|
|
2925
|
+
const wrappedStore = safeGet(safeGet(client, "api"), "credential");
|
|
2926
|
+
const providerSource = safeGet(safeGet(safeGet(host, "data"), "location"), "provider");
|
|
2927
|
+
const readKey = async () => {
|
|
2928
|
+
if (keyFn !== undefined) {
|
|
2929
|
+
try {
|
|
2930
|
+
const key = keyFn();
|
|
2931
|
+
return typeof key === "string" && key.length > 0 ? { key, src: "credential" } : { src: "none", reason: "no-key" };
|
|
2932
|
+
} catch {
|
|
2933
|
+
return { src: "none", reason: "no-key" };
|
|
2934
|
+
}
|
|
2935
|
+
}
|
|
2936
|
+
if (asyncKeyFn !== undefined) {
|
|
2937
|
+
try {
|
|
2938
|
+
const key = await asyncKeyFn();
|
|
2939
|
+
return typeof key === "string" && key.length > 0 ? { key, src: "credential" } : { src: "none", reason: "no-key" };
|
|
2940
|
+
} catch {
|
|
2941
|
+
return { src: "none", reason: "no-key" };
|
|
2942
|
+
}
|
|
2943
|
+
}
|
|
2944
|
+
let answered = false;
|
|
2945
|
+
let timedOut = false;
|
|
2946
|
+
const direct = await credentialAttempt(directStore, credentialTimeoutMs);
|
|
2947
|
+
if (direct.key !== undefined)
|
|
2948
|
+
return { key: direct.key, src: "credential" };
|
|
2949
|
+
answered = answered || direct.answered;
|
|
2950
|
+
timedOut = timedOut || direct.reason === "timeout";
|
|
2951
|
+
const wrapped = await credentialAttempt(wrappedStore, credentialTimeoutMs);
|
|
2952
|
+
if (wrapped.key !== undefined)
|
|
2953
|
+
return { key: wrapped.key, src: "credential" };
|
|
2954
|
+
answered = answered || wrapped.answered;
|
|
2955
|
+
timedOut = timedOut || wrapped.reason === "timeout";
|
|
2956
|
+
const provider = await providerAttempt(providerSource, credentialTimeoutMs);
|
|
2957
|
+
if (provider.key !== undefined)
|
|
2958
|
+
return { key: provider.key, src: "provider" };
|
|
2959
|
+
answered = answered || provider.answered;
|
|
2960
|
+
timedOut = timedOut || provider.reason === "timeout";
|
|
2961
|
+
if (answered)
|
|
2962
|
+
return { src: "none", reason: "no-key" };
|
|
2963
|
+
return { src: "none", reason: timedOut ? "timeout" : "no-client" };
|
|
2964
|
+
};
|
|
2965
|
+
let timer;
|
|
2966
|
+
let generation = 0;
|
|
2967
|
+
let nextSeq = 0;
|
|
2968
|
+
let latestSeq = 0;
|
|
2969
|
+
let disposed = false;
|
|
2970
|
+
const stopTimer = () => {
|
|
2971
|
+
if (timer !== undefined)
|
|
2972
|
+
clearInterval(timer);
|
|
2973
|
+
timer = undefined;
|
|
2974
|
+
};
|
|
2975
|
+
const poll = async () => {
|
|
2976
|
+
if (disposed)
|
|
2977
|
+
return;
|
|
2978
|
+
const myGeneration = generation;
|
|
2979
|
+
const mySeq = ++nextSeq;
|
|
2980
|
+
latestSeq = mySeq;
|
|
2981
|
+
const stillCurrent = () => myGeneration === generation && mySeq === latestSeq && !disposed;
|
|
2982
|
+
let key;
|
|
2983
|
+
let keySrc = "none";
|
|
2984
|
+
let keyReason = "no-key";
|
|
2985
|
+
try {
|
|
2986
|
+
const resolved = await readKey();
|
|
2987
|
+
key = resolved.key;
|
|
2988
|
+
keySrc = resolved.src;
|
|
2989
|
+
if (resolved.reason !== undefined)
|
|
2990
|
+
keyReason = resolved.reason;
|
|
2991
|
+
} catch {
|
|
2992
|
+
key = undefined;
|
|
2993
|
+
}
|
|
2994
|
+
if (typeof key !== "string" || key.length === 0) {
|
|
2995
|
+
if (stillCurrent() && !writeValue2(opened, null, keyReason))
|
|
2996
|
+
stopTimer();
|
|
2997
|
+
logPoll({ src: keySrc, key: false, http: "n/a", parse: "n/a", reason: keyReason });
|
|
2998
|
+
return;
|
|
2999
|
+
}
|
|
3000
|
+
let raw;
|
|
3001
|
+
try {
|
|
3002
|
+
raw = await fetchJson(GO_USAGE_URL, {
|
|
3003
|
+
headers: { Authorization: `Bearer ${key}` },
|
|
3004
|
+
signal: AbortSignal.timeout(GO_TIMEOUT_MS)
|
|
3005
|
+
});
|
|
3006
|
+
} catch (error) {
|
|
3007
|
+
if (stillCurrent() && !writeValue2(opened, null, "http"))
|
|
3008
|
+
stopTimer();
|
|
3009
|
+
logPoll({ src: keySrc, key: true, http: httpOf(error), parse: "n/a", reason: "http" });
|
|
3010
|
+
return;
|
|
3011
|
+
}
|
|
3012
|
+
let normalized;
|
|
3013
|
+
try {
|
|
3014
|
+
normalized = normalizeGoUsage(raw) ?? null;
|
|
3015
|
+
} catch {
|
|
3016
|
+
normalized = null;
|
|
3017
|
+
}
|
|
3018
|
+
if (stillCurrent() && !writeValue2(opened, normalized, "parse"))
|
|
3019
|
+
stopTimer();
|
|
3020
|
+
logPoll({
|
|
3021
|
+
src: keySrc,
|
|
3022
|
+
key: true,
|
|
3023
|
+
http: "ok",
|
|
3024
|
+
parse: normalized === null ? "no" : "ok",
|
|
3025
|
+
reason: normalized === null ? "parse" : "none"
|
|
3026
|
+
});
|
|
3027
|
+
};
|
|
3028
|
+
timer = setInterval(() => {
|
|
3029
|
+
poll();
|
|
3030
|
+
}, cadence);
|
|
3031
|
+
poll();
|
|
3032
|
+
return {
|
|
3033
|
+
get usage() {
|
|
3034
|
+
return readUsage(opened.store);
|
|
3035
|
+
},
|
|
3036
|
+
get reason() {
|
|
3037
|
+
return readReason2(opened.store);
|
|
3038
|
+
},
|
|
3039
|
+
dispose() {
|
|
3040
|
+
disposed = true;
|
|
3041
|
+
generation += 1;
|
|
3042
|
+
stopTimer();
|
|
3043
|
+
}
|
|
3044
|
+
};
|
|
3045
|
+
}
|
|
3046
|
+
|
|
3047
|
+
// src/tui/ticker.ts
|
|
3048
|
+
var TICKER_KEY = "flight-deck.frame";
|
|
3049
|
+
var IDLE_REFRESH_MS = 1000;
|
|
3050
|
+
function readFrame2(store) {
|
|
3051
|
+
const value = store?.frame;
|
|
3052
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? Math.floor(value) : 0;
|
|
3053
|
+
}
|
|
3054
|
+
function openStore3(storage) {
|
|
3055
|
+
let opened;
|
|
3056
|
+
try {
|
|
3057
|
+
opened = storage.memory(TICKER_KEY, { initial: { frame: 0 } });
|
|
3058
|
+
} catch {
|
|
3059
|
+
return;
|
|
3060
|
+
}
|
|
3061
|
+
if (!Array.isArray(opened) || opened.length < 2)
|
|
3062
|
+
return;
|
|
3063
|
+
const [store, update] = opened;
|
|
3064
|
+
if (store === null || typeof store !== "object")
|
|
3065
|
+
return;
|
|
3066
|
+
if (typeof update !== "function")
|
|
3067
|
+
return;
|
|
3068
|
+
return { store, update };
|
|
3069
|
+
}
|
|
3070
|
+
function startTicker(host, intervalMs, isBusy = () => true) {
|
|
3071
|
+
if (!Number.isFinite(intervalMs) || intervalMs <= 0)
|
|
3072
|
+
return;
|
|
3073
|
+
const storage = host?.storage;
|
|
3074
|
+
if (storage === undefined || typeof storage.memory !== "function")
|
|
3075
|
+
return;
|
|
3076
|
+
const opened = openStore3(storage);
|
|
3077
|
+
if (opened === undefined)
|
|
3078
|
+
return;
|
|
3079
|
+
const idleEvery = Math.max(1, Math.round(IDLE_REFRESH_MS / intervalMs));
|
|
3080
|
+
let ticks = 0;
|
|
3081
|
+
let timer = setInterval(() => {
|
|
3082
|
+
ticks += 1;
|
|
3083
|
+
let busy = true;
|
|
3084
|
+
try {
|
|
3085
|
+
busy = isBusy();
|
|
3086
|
+
} catch {
|
|
3087
|
+
busy = true;
|
|
3088
|
+
}
|
|
3089
|
+
if (!busy && ticks % idleEvery !== 0)
|
|
3090
|
+
return;
|
|
3091
|
+
try {
|
|
3092
|
+
opened.update((draft) => {
|
|
3093
|
+
draft.frame = readFrame2(draft) + 1;
|
|
3094
|
+
});
|
|
3095
|
+
} catch {
|
|
3096
|
+
if (timer !== undefined)
|
|
3097
|
+
clearInterval(timer);
|
|
3098
|
+
timer = undefined;
|
|
3099
|
+
}
|
|
3100
|
+
}, intervalMs);
|
|
3101
|
+
return {
|
|
3102
|
+
get frame() {
|
|
3103
|
+
return readFrame2(opened.store);
|
|
3104
|
+
},
|
|
3105
|
+
dispose() {
|
|
3106
|
+
if (timer !== undefined)
|
|
3107
|
+
clearInterval(timer);
|
|
3108
|
+
timer = undefined;
|
|
3109
|
+
}
|
|
3110
|
+
};
|
|
3111
|
+
}
|
|
3112
|
+
|
|
3113
|
+
// src/tui/project-totals.ts
|
|
3114
|
+
function createProjectTotals(context) {
|
|
3115
|
+
const PROJECT_CACHE_MS = 5000;
|
|
3116
|
+
let projectCache;
|
|
3117
|
+
const projectTotals = (sessionID) => {
|
|
3118
|
+
try {
|
|
3119
|
+
const projectID = asText2(asRecord2(context.data.session.get(sessionID))?.["projectID"]);
|
|
3120
|
+
const cached = projectCache;
|
|
3121
|
+
if (cached !== undefined && cached.projectID === projectID && Date.now() - cached.at < PROJECT_CACHE_MS) {
|
|
3122
|
+
return cached.value;
|
|
3123
|
+
}
|
|
3124
|
+
const sessions = context.data.session.list() ?? [];
|
|
3125
|
+
const scoped = projectID === undefined ? sessions : sessions.filter((entry) => asText2(asRecord2(entry)?.["projectID"]) === projectID);
|
|
3126
|
+
let cost = 0;
|
|
3127
|
+
for (const entry of scoped)
|
|
3128
|
+
cost += asCount2(asRecord2(entry)?.["cost"]) ?? 0;
|
|
3129
|
+
const value = { cost, count: scoped.length };
|
|
3130
|
+
projectCache = { projectID, at: Date.now(), value };
|
|
3131
|
+
return value;
|
|
3132
|
+
} catch {
|
|
3133
|
+
return;
|
|
3134
|
+
}
|
|
3135
|
+
};
|
|
3136
|
+
return { projectTotals };
|
|
3137
|
+
}
|
|
3138
|
+
|
|
3139
|
+
// src/tui/session-elapsed.ts
|
|
3140
|
+
function createActiveTime(reads, options) {
|
|
3141
|
+
const cacheMs = options?.cacheMs ?? 1000;
|
|
3142
|
+
const maxSessions = options?.maxSessions ?? 256;
|
|
3143
|
+
const entries = new Map;
|
|
3144
|
+
const sweepForgotten = () => {
|
|
3145
|
+
for (const id of entries.keys()) {
|
|
3146
|
+
let known = true;
|
|
3147
|
+
try {
|
|
3148
|
+
known = reads.knows?.(id) ?? true;
|
|
3149
|
+
} catch {
|
|
3150
|
+
known = true;
|
|
3151
|
+
}
|
|
3152
|
+
if (!known)
|
|
3153
|
+
entries.delete(id);
|
|
3154
|
+
}
|
|
3155
|
+
};
|
|
3156
|
+
return function activeTimeMs(sessionID, now) {
|
|
3157
|
+
let entry = entries.get(sessionID);
|
|
3158
|
+
const fresh = entry === undefined || Number.isFinite(now) && now - entry.at >= cacheMs;
|
|
3159
|
+
if (fresh && Number.isFinite(now)) {
|
|
3160
|
+
let union = 0;
|
|
3161
|
+
try {
|
|
3162
|
+
union = unionSpanMs(reads.spansOf(sessionID, now));
|
|
3163
|
+
} catch {
|
|
3164
|
+
union = 0;
|
|
3165
|
+
}
|
|
3166
|
+
if (!Number.isFinite(union) || union < 0)
|
|
3167
|
+
union = 0;
|
|
3168
|
+
const high = Math.max(entry?.high ?? 0, union);
|
|
3169
|
+
if (entry === undefined) {
|
|
3170
|
+
if (entries.size >= maxSessions)
|
|
3171
|
+
sweepForgotten();
|
|
3172
|
+
entry = { union, at: now, high };
|
|
3173
|
+
entries.set(sessionID, entry);
|
|
3174
|
+
} else {
|
|
3175
|
+
entry.union = union;
|
|
3176
|
+
entry.at = now;
|
|
3177
|
+
entry.high = high;
|
|
3178
|
+
}
|
|
3179
|
+
}
|
|
3180
|
+
const high = entry?.high ?? 0;
|
|
3181
|
+
return high <= 0 ? undefined : high;
|
|
3182
|
+
};
|
|
3183
|
+
}
|
|
3184
|
+
|
|
3185
|
+
// src/tui/session-reads.ts
|
|
3186
|
+
function locationOf(context) {
|
|
3187
|
+
try {
|
|
3188
|
+
return context.location ?? context.data.location.default();
|
|
3189
|
+
} catch {
|
|
3190
|
+
return context.location;
|
|
3191
|
+
}
|
|
3192
|
+
}
|
|
3193
|
+
function createSessionReads(context) {
|
|
3194
|
+
const RECENT_MESSAGES = 4;
|
|
3195
|
+
const messagesOf = (sessionID) => {
|
|
3196
|
+
try {
|
|
3197
|
+
return context.data.session.message.list(sessionID) ?? [];
|
|
3198
|
+
} catch {
|
|
3199
|
+
return [];
|
|
3200
|
+
}
|
|
3201
|
+
};
|
|
3202
|
+
const isFamilyRoot = (sessionID) => {
|
|
3203
|
+
try {
|
|
3204
|
+
return context.data.session.root(sessionID) === sessionID;
|
|
3205
|
+
} catch {
|
|
3206
|
+
return true;
|
|
3207
|
+
}
|
|
3208
|
+
};
|
|
3209
|
+
const treeTotals = (sessionID, ownCost) => {
|
|
3210
|
+
if (!isFamilyRoot(sessionID))
|
|
3211
|
+
return;
|
|
3212
|
+
let ids;
|
|
3213
|
+
try {
|
|
3214
|
+
ids = context.data.session.family(sessionID) ?? [sessionID];
|
|
3215
|
+
} catch {
|
|
3216
|
+
return;
|
|
3217
|
+
}
|
|
3218
|
+
let own;
|
|
3219
|
+
try {
|
|
3220
|
+
own = context.data.session.get(sessionID);
|
|
3221
|
+
} catch {
|
|
3222
|
+
own = undefined;
|
|
3223
|
+
}
|
|
3224
|
+
const tokens = { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } };
|
|
3225
|
+
const addTokens = (source) => {
|
|
3226
|
+
const record = asRecord2(source?.tokens);
|
|
3227
|
+
tokens.input += asCount2(record?.["input"]) ?? 0;
|
|
3228
|
+
tokens.output += asCount2(record?.["output"]) ?? 0;
|
|
3229
|
+
tokens.reasoning += asCount2(record?.["reasoning"]) ?? 0;
|
|
3230
|
+
const cache = asRecord2(record?.["cache"]);
|
|
3231
|
+
tokens.cache.read += asCount2(cache?.["read"]) ?? 0;
|
|
3232
|
+
tokens.cache.write += asCount2(cache?.["write"]) ?? 0;
|
|
3233
|
+
};
|
|
3234
|
+
addTokens(own);
|
|
3235
|
+
let cost = asCount2(ownCost) ?? 0;
|
|
3236
|
+
let count = 0;
|
|
3237
|
+
for (const id of ids) {
|
|
3238
|
+
if (id === sessionID)
|
|
3239
|
+
continue;
|
|
3240
|
+
let child;
|
|
3241
|
+
try {
|
|
3242
|
+
child = context.data.session.get(id);
|
|
3243
|
+
} catch {
|
|
3244
|
+
continue;
|
|
3245
|
+
}
|
|
3246
|
+
if (child === undefined)
|
|
3247
|
+
continue;
|
|
3248
|
+
count += 1;
|
|
3249
|
+
cost += asCount2(child.cost) ?? 0;
|
|
3250
|
+
addTokens(child);
|
|
3251
|
+
}
|
|
3252
|
+
if (ownCost === undefined && count === 0)
|
|
3253
|
+
return;
|
|
3254
|
+
return { cost, count, tokens };
|
|
3255
|
+
};
|
|
3256
|
+
const contextUsage = (sessionID, model) => {
|
|
3257
|
+
const messages = messagesOf(sessionID);
|
|
3258
|
+
for (let index = messages.length - 1;index >= 0; index -= 1) {
|
|
3259
|
+
const tokens = asRecord2(asRecord2(messages[index])?.tokens);
|
|
3260
|
+
const input = asCount2(tokens?.input);
|
|
3261
|
+
if (tokens === undefined || input === undefined)
|
|
3262
|
+
continue;
|
|
3263
|
+
const cache = asRecord2(tokens.cache);
|
|
3264
|
+
const used = input + (asCount2(cache?.read) ?? 0) + (asCount2(cache?.write) ?? 0);
|
|
3265
|
+
if (used === 0)
|
|
3266
|
+
return;
|
|
3267
|
+
return { used, limit: contextLimit(model) };
|
|
3268
|
+
}
|
|
3269
|
+
return;
|
|
3270
|
+
};
|
|
3271
|
+
const contextLimit = (model) => {
|
|
3272
|
+
const ref = asRecord2(model);
|
|
3273
|
+
const id = ref?.id;
|
|
3274
|
+
const providerID = ref?.providerID;
|
|
3275
|
+
if (typeof id !== "string")
|
|
3276
|
+
return;
|
|
3277
|
+
try {
|
|
3278
|
+
const location = context.location ?? context.data.location.default();
|
|
3279
|
+
for (const entry of context.data.location.model.list(location) ?? []) {
|
|
3280
|
+
const record = asRecord2(entry);
|
|
3281
|
+
if (record === undefined || record.id !== id)
|
|
3282
|
+
continue;
|
|
3283
|
+
if (typeof providerID === "string" && record.providerID !== providerID)
|
|
3284
|
+
continue;
|
|
3285
|
+
const limit = asRecord2(record.limit);
|
|
3286
|
+
return asCount2(limit?.context) ?? asCount2(record.context) ?? asCount2(record.contextWindow);
|
|
3287
|
+
}
|
|
3288
|
+
} catch {
|
|
3289
|
+
return;
|
|
3290
|
+
}
|
|
3291
|
+
return;
|
|
3292
|
+
};
|
|
3293
|
+
const statusOf = (sessionID) => {
|
|
3294
|
+
try {
|
|
3295
|
+
return context.data.session.status(sessionID);
|
|
3296
|
+
} catch {
|
|
3297
|
+
return;
|
|
3298
|
+
}
|
|
3299
|
+
};
|
|
3300
|
+
const recentParts = (sessionID) => {
|
|
3301
|
+
const parts = [];
|
|
3302
|
+
for (const entry of messagesOf(sessionID).slice(-RECENT_MESSAGES)) {
|
|
3303
|
+
const content = asRecord2(entry)?.["content"];
|
|
3304
|
+
if (Array.isArray(content))
|
|
3305
|
+
parts.push(...content);
|
|
3306
|
+
}
|
|
3307
|
+
return parts;
|
|
3308
|
+
};
|
|
3309
|
+
const shellsOf = (sessionID) => {
|
|
3310
|
+
try {
|
|
3311
|
+
const shells = context.data.shell.list(locationOf(context)) ?? [];
|
|
3312
|
+
return shells.filter((entry) => asText2(asRecord2(asRecord2(entry)?.["metadata"])?.["sessionID"]) === sessionID);
|
|
3313
|
+
} catch {
|
|
3314
|
+
return [];
|
|
3315
|
+
}
|
|
3316
|
+
};
|
|
3317
|
+
const lastActivity = (sessionID) => {
|
|
3318
|
+
const messages = messagesOf(sessionID);
|
|
3319
|
+
for (let index = messages.length - 1;index >= 0; index -= 1) {
|
|
3320
|
+
const time = asRecord2(asRecord2(messages[index])?.["time"]);
|
|
3321
|
+
const stamp = asCount2(time?.["completed"]) ?? asCount2(time?.["streamed"]) ?? asCount2(time?.["created"]);
|
|
3322
|
+
if (stamp !== undefined)
|
|
3323
|
+
return stamp;
|
|
3324
|
+
}
|
|
3325
|
+
return;
|
|
3326
|
+
};
|
|
3327
|
+
const permsOf = (sessionID) => {
|
|
3328
|
+
try {
|
|
3329
|
+
const requests = context.data.session.permission.list(sessionID);
|
|
3330
|
+
if (requests === undefined || requests.length === 0)
|
|
3331
|
+
return;
|
|
3332
|
+
const first = asRecord2(requests[0]);
|
|
3333
|
+
const action = typeof first?.["action"] === "string" ? first["action"] : undefined;
|
|
3334
|
+
const resources = Array.isArray(first?.["resources"]) ? first["resources"] : [];
|
|
3335
|
+
const resource = typeof resources[0] === "string" ? resources[0] : undefined;
|
|
3336
|
+
return { count: requests.length, action, resource };
|
|
3337
|
+
} catch {
|
|
3338
|
+
return;
|
|
3339
|
+
}
|
|
3340
|
+
};
|
|
3341
|
+
const busy = (sessionID) => {
|
|
3342
|
+
const status = statusOf(sessionID);
|
|
3343
|
+
if (status === "running")
|
|
3344
|
+
return true;
|
|
3345
|
+
try {
|
|
3346
|
+
for (const id of context.data.session.family(sessionID) ?? []) {
|
|
3347
|
+
if (id === sessionID)
|
|
3348
|
+
continue;
|
|
3349
|
+
try {
|
|
3350
|
+
if (context.data.session.status(id) === "running")
|
|
3351
|
+
return true;
|
|
3352
|
+
} catch {}
|
|
3353
|
+
}
|
|
3354
|
+
} catch {}
|
|
3355
|
+
try {
|
|
3356
|
+
if (shellsOf(sessionID).some((entry) => asRecord2(entry)?.["status"] === "running"))
|
|
3357
|
+
return true;
|
|
3358
|
+
} catch {}
|
|
3359
|
+
return status === undefined ? undefined : false;
|
|
3360
|
+
};
|
|
3361
|
+
const spansOf = (sessionID, now) => {
|
|
3362
|
+
try {
|
|
3363
|
+
let live = false;
|
|
3364
|
+
try {
|
|
3365
|
+
live = busy(sessionID) === true;
|
|
3366
|
+
} catch {
|
|
3367
|
+
live = false;
|
|
3368
|
+
}
|
|
3369
|
+
const ids = new Set([sessionID]);
|
|
3370
|
+
try {
|
|
3371
|
+
for (const id of context.data.session.family(sessionID) ?? [])
|
|
3372
|
+
ids.add(id);
|
|
3373
|
+
} catch {}
|
|
3374
|
+
const spans = [];
|
|
3375
|
+
for (const id of ids) {
|
|
3376
|
+
for (const entry of messagesOf(id)) {
|
|
3377
|
+
const message = asRecord2(entry);
|
|
3378
|
+
if (message?.["type"] !== "assistant")
|
|
3379
|
+
continue;
|
|
3380
|
+
const stamp = asRecord2(message["time"]);
|
|
3381
|
+
const created = asCount2(stamp?.["created"]);
|
|
3382
|
+
if (created === undefined)
|
|
3383
|
+
continue;
|
|
3384
|
+
const completed = asCount2(stamp?.["completed"]);
|
|
3385
|
+
const streamed = asCount2(stamp?.["streamed"]);
|
|
3386
|
+
const end = completed ?? (live ? now : streamed ?? created);
|
|
3387
|
+
if (!Number.isFinite(end))
|
|
3388
|
+
continue;
|
|
3389
|
+
const output = asRecord2(message["tokens"])?.["output"];
|
|
3390
|
+
spans.push({
|
|
3391
|
+
key: turnKey(message["id"], created, completed, output, streamed),
|
|
3392
|
+
start: created,
|
|
3393
|
+
end: Math.max(created, end)
|
|
3394
|
+
});
|
|
3395
|
+
}
|
|
3396
|
+
}
|
|
3397
|
+
return spans;
|
|
3398
|
+
} catch {
|
|
3399
|
+
return [];
|
|
3400
|
+
}
|
|
3401
|
+
};
|
|
3402
|
+
const knows = (sessionID) => {
|
|
3403
|
+
try {
|
|
3404
|
+
return context.data.session.get(sessionID) !== undefined;
|
|
3405
|
+
} catch {
|
|
3406
|
+
return false;
|
|
3407
|
+
}
|
|
3408
|
+
};
|
|
3409
|
+
const activeTime = createActiveTime({ spansOf, knows });
|
|
3410
|
+
const sessionElapsed = (sessionID, now = Date.now()) => activeTime(sessionID, now);
|
|
3411
|
+
return {
|
|
3412
|
+
messagesOf,
|
|
3413
|
+
treeTotals,
|
|
3414
|
+
contextUsage,
|
|
3415
|
+
sessionElapsed,
|
|
3416
|
+
statusOf,
|
|
3417
|
+
recentParts,
|
|
3418
|
+
shellsOf,
|
|
3419
|
+
lastActivity,
|
|
3420
|
+
permsOf,
|
|
3421
|
+
busy
|
|
3422
|
+
};
|
|
3423
|
+
}
|
|
3424
|
+
|
|
3425
|
+
// src/tui/session-tps.ts
|
|
3426
|
+
function createTpsReader(context, live, scope = DEFAULT_TPS_SCOPE) {
|
|
3427
|
+
const TPS_CACHE_MS = 1000;
|
|
3428
|
+
let hostStamps;
|
|
3429
|
+
let tpsCache;
|
|
3430
|
+
const MAX_TRACKERS = 50;
|
|
3431
|
+
const trackers = new Map;
|
|
3432
|
+
const trackerOf = (sessionID) => {
|
|
3433
|
+
const known = trackers.get(sessionID);
|
|
3434
|
+
if (known !== undefined) {
|
|
3435
|
+
trackers.delete(sessionID);
|
|
3436
|
+
trackers.set(sessionID, known);
|
|
3437
|
+
return known;
|
|
3438
|
+
}
|
|
3439
|
+
const fresh = createInstantTps();
|
|
3440
|
+
if (trackers.size >= MAX_TRACKERS) {
|
|
3441
|
+
for (const oldest of trackers.keys()) {
|
|
3442
|
+
trackers.delete(oldest);
|
|
3443
|
+
break;
|
|
3444
|
+
}
|
|
3445
|
+
}
|
|
3446
|
+
trackers.set(sessionID, fresh);
|
|
3447
|
+
return fresh;
|
|
3448
|
+
};
|
|
3449
|
+
const scanMessages = (id, now) => {
|
|
3450
|
+
let messages;
|
|
3451
|
+
try {
|
|
3452
|
+
messages = context.data.session.message.list(id) ?? [];
|
|
3453
|
+
} catch {
|
|
3454
|
+
return { ok: false, assistant: 0, stamped: 0, spans: [] };
|
|
3455
|
+
}
|
|
3456
|
+
const spans = [];
|
|
3457
|
+
let assistant = 0;
|
|
3458
|
+
let stamped = 0;
|
|
3459
|
+
for (const entry of messages) {
|
|
3460
|
+
const message = asRecord2(entry);
|
|
3461
|
+
if (message?.["type"] !== "assistant")
|
|
3462
|
+
continue;
|
|
3463
|
+
assistant += 1;
|
|
3464
|
+
const stamp = asRecord2(message["time"]);
|
|
3465
|
+
const created = asCount2(stamp?.["created"]);
|
|
3466
|
+
const completed = asCount2(stamp?.["completed"]);
|
|
3467
|
+
const streamed = asCount2(stamp?.["streamed"]);
|
|
3468
|
+
if (created === undefined && completed === undefined && streamed === undefined)
|
|
3469
|
+
continue;
|
|
3470
|
+
stamped += 1;
|
|
3471
|
+
const span = turnSpan(created, completed, streamed, now);
|
|
3472
|
+
if (span === undefined)
|
|
3473
|
+
continue;
|
|
3474
|
+
const tokens = asRecord2(message["tokens"]);
|
|
3475
|
+
const output = tokens?.["output"];
|
|
3476
|
+
const reasoning = tokens?.["reasoning"];
|
|
3477
|
+
const settled = completed !== undefined || streamed !== undefined;
|
|
3478
|
+
const key = turnKey(message["id"], created, completed, output, streamed);
|
|
3479
|
+
spans.push({
|
|
3480
|
+
key: settled ? `${key}:${asCount2(reasoning) ?? "?"}` : key,
|
|
3481
|
+
tokens: output,
|
|
3482
|
+
reasoning,
|
|
3483
|
+
...span
|
|
3484
|
+
});
|
|
3485
|
+
}
|
|
3486
|
+
return { ok: true, assistant, stamped, spans };
|
|
3487
|
+
};
|
|
3488
|
+
const familyIds = (sessionID) => {
|
|
3489
|
+
const ids = [sessionID];
|
|
3490
|
+
try {
|
|
3491
|
+
for (const id of context.data.session.family(sessionID) ?? []) {
|
|
3492
|
+
if (id === sessionID || ids.includes(id))
|
|
3493
|
+
continue;
|
|
3494
|
+
ids.push(id);
|
|
3495
|
+
}
|
|
3496
|
+
} catch {}
|
|
3497
|
+
return ids;
|
|
3498
|
+
};
|
|
3499
|
+
const scanScope = (ids, now) => {
|
|
3500
|
+
let ok = true;
|
|
3501
|
+
let assistant = 0;
|
|
3502
|
+
let stamped = 0;
|
|
3503
|
+
const spans = [];
|
|
3504
|
+
for (const id of ids) {
|
|
3505
|
+
const scan = scanMessages(id, now);
|
|
3506
|
+
ok = ok && scan.ok;
|
|
3507
|
+
assistant += scan.assistant;
|
|
3508
|
+
stamped += scan.stamped;
|
|
3509
|
+
for (const span of scan.spans)
|
|
3510
|
+
spans.push({ ...span, key: `${id} ${String(span.key)}` });
|
|
3511
|
+
}
|
|
3512
|
+
return { ok, assistant, stamped, spans };
|
|
3513
|
+
};
|
|
3514
|
+
const recordGeneration = (session) => {
|
|
3515
|
+
const tokens = asRecord2(session?.tokens);
|
|
3516
|
+
return (asCount2(tokens?.["output"]) ?? 0) + (asCount2(tokens?.["reasoning"]) ?? 0);
|
|
3517
|
+
};
|
|
3518
|
+
const familyGeneration = (sessionID, session) => {
|
|
3519
|
+
let generation = recordGeneration(session);
|
|
3520
|
+
let ok = true;
|
|
3521
|
+
for (const id of familyIds(sessionID)) {
|
|
3522
|
+
if (id === sessionID)
|
|
3523
|
+
continue;
|
|
3524
|
+
try {
|
|
3525
|
+
generation += recordGeneration(context.data.session.get(id));
|
|
3526
|
+
} catch {
|
|
3527
|
+
ok = false;
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3530
|
+
return { ok, generation };
|
|
3531
|
+
};
|
|
3532
|
+
const computeTps = (sessionID, session) => {
|
|
3533
|
+
const time = asRecord2(session?.time);
|
|
3534
|
+
const created = asCount2(time?.["created"]);
|
|
3535
|
+
const now = Date.now();
|
|
3536
|
+
const scan = hostStamps === false ? { ok: true, assistant: 0, stamped: 0, spans: [] } : scope === "family" ? scanScope(familyIds(sessionID), now) : scanMessages(sessionID, now);
|
|
3537
|
+
if (hostStamps === undefined) {
|
|
3538
|
+
if (scan.stamped > 0)
|
|
3539
|
+
hostStamps = true;
|
|
3540
|
+
else if (scan.ok && scan.assistant > 0)
|
|
3541
|
+
hostStamps = false;
|
|
3542
|
+
else
|
|
3543
|
+
return;
|
|
3544
|
+
}
|
|
3545
|
+
const tracker = trackerOf(sessionID);
|
|
3546
|
+
if (hostStamps === true) {
|
|
3547
|
+
if (scope === "family" && scan.ok === false)
|
|
3548
|
+
return 0;
|
|
3549
|
+
const { tokens } = unionGenerationTotals(scan.spans);
|
|
3550
|
+
const genMs = unionSpanMs(scan.spans);
|
|
3551
|
+
return tracker(tokens, genMs);
|
|
3552
|
+
}
|
|
3553
|
+
const family = scope === "family" ? familyGeneration(sessionID, session) : undefined;
|
|
3554
|
+
if (family !== undefined && family.ok === false)
|
|
3555
|
+
return 0;
|
|
3556
|
+
const generation = family?.generation ?? recordGeneration(session);
|
|
3557
|
+
if (created === undefined && scope === "session")
|
|
3558
|
+
return tracker(recordGeneration(session), 0);
|
|
3559
|
+
let genMs = 0;
|
|
3560
|
+
try {
|
|
3561
|
+
genMs = live?.elapsedMsOf?.(sessionID, now) ?? 0;
|
|
3562
|
+
} catch {
|
|
3563
|
+
genMs = 0;
|
|
3564
|
+
}
|
|
3565
|
+
return tracker(generation, genMs);
|
|
3566
|
+
};
|
|
3567
|
+
const sessionTps = (sessionID, session) => {
|
|
3568
|
+
const cached = tpsCache;
|
|
3569
|
+
if (cached !== undefined && cached.sessionID === sessionID && Date.now() - cached.at < TPS_CACHE_MS) {
|
|
3570
|
+
return cached.value;
|
|
3571
|
+
}
|
|
3572
|
+
const value = computeTps(sessionID, session) ?? 0;
|
|
3573
|
+
tpsCache = { sessionID, at: Date.now(), value };
|
|
3574
|
+
return value;
|
|
3575
|
+
};
|
|
3576
|
+
return { sessionTps };
|
|
3577
|
+
}
|
|
3578
|
+
|
|
3579
|
+
// src/tui/index.tsx
|
|
3580
|
+
import { jsx, jsxs } from "@opentui/solid/jsx-runtime";
|
|
3581
|
+
var tui_default = Plugin.define({
|
|
3582
|
+
id: "flight-deck-tui",
|
|
3583
|
+
setup(context) {
|
|
3584
|
+
const file = loadConfigFile();
|
|
3585
|
+
const { config, issues } = resolveConfig(mergeOptions(file.options, context.options));
|
|
3586
|
+
const rows = config.sidebar.enabled ? new Set(config.sidebar.rows) : new Set;
|
|
3587
|
+
const wants = (name) => rows.has(name);
|
|
3588
|
+
if (file.issue !== undefined && file.source !== undefined) {
|
|
3589
|
+
console.warn(`[flight-deck] ${file.issue}`);
|
|
3590
|
+
}
|
|
3591
|
+
const problems = file.issue !== undefined ? [file.issue, ...issues] : issues;
|
|
3592
|
+
if (problems.length > 0) {
|
|
3593
|
+
const extra = problems.length > 1 ? ` (+${problems.length - 1} more)` : "";
|
|
3594
|
+
context.ui.toast.show({
|
|
3595
|
+
title: "Flight Deck",
|
|
3596
|
+
message: `Ignoring invalid config: ${problems[0]}${extra}`,
|
|
3597
|
+
variant: "warning"
|
|
3598
|
+
});
|
|
3599
|
+
}
|
|
3600
|
+
const footer = footerLine(config);
|
|
3601
|
+
const releases = [];
|
|
3602
|
+
const themedLine = (line, copyId) => {
|
|
3603
|
+
const style = railLineStyle(config.style, line);
|
|
3604
|
+
const lineFg = themeColor(style.color, context.theme);
|
|
3605
|
+
const text = /* @__PURE__ */ jsx("text", {
|
|
3606
|
+
fg: lineFg,
|
|
3607
|
+
attributes: attributeMask(style.attributes) ?? 0,
|
|
3608
|
+
wrapMode: line.field === "ses" ? "char" : undefined,
|
|
3609
|
+
children: railLineSpans(line, style).map((span) => /* @__PURE__ */ jsx("span", {
|
|
3610
|
+
style: { fg: themeColor(span.color, context.theme) },
|
|
3611
|
+
children: span.text
|
|
3612
|
+
}))
|
|
3613
|
+
});
|
|
3614
|
+
if (copyId === undefined)
|
|
3615
|
+
return text;
|
|
3616
|
+
return /* @__PURE__ */ jsx("box", {
|
|
3617
|
+
onMouseUp: () => {
|
|
3618
|
+
(async () => {
|
|
3619
|
+
try {
|
|
3620
|
+
const result = await copyToClipboard(context.renderer, copyId);
|
|
3621
|
+
const fb = copyFeedback(result, copyId);
|
|
3622
|
+
if (fb.ok)
|
|
3623
|
+
return;
|
|
3624
|
+
context.ui.toast.show({
|
|
3625
|
+
title: "Flight Deck",
|
|
3626
|
+
message: fb.message,
|
|
3627
|
+
variant: fb.variant,
|
|
3628
|
+
duration: 1500
|
|
3629
|
+
});
|
|
3630
|
+
} catch {}
|
|
3631
|
+
})();
|
|
3632
|
+
},
|
|
3633
|
+
children: text
|
|
3634
|
+
});
|
|
3635
|
+
};
|
|
3636
|
+
const reads = createSessionReads(context);
|
|
3637
|
+
const {
|
|
3638
|
+
messagesOf,
|
|
3639
|
+
treeTotals,
|
|
3640
|
+
contextUsage,
|
|
3641
|
+
sessionElapsed,
|
|
3642
|
+
statusOf,
|
|
3643
|
+
recentParts,
|
|
3644
|
+
shellsOf,
|
|
3645
|
+
lastActivity,
|
|
3646
|
+
permsOf,
|
|
3647
|
+
busy
|
|
3648
|
+
} = reads;
|
|
3649
|
+
const { projectTotals } = createProjectTotals(context);
|
|
3650
|
+
const cautionThreshold = cautionThresholds(config.caution);
|
|
3651
|
+
const cautionsOf = (sessionID, now) => detectCautions({
|
|
3652
|
+
parts: recentParts(sessionID),
|
|
3653
|
+
shells: shellsOf(sessionID),
|
|
3654
|
+
now,
|
|
3655
|
+
sessionRunning: statusOf(sessionID) === "running",
|
|
3656
|
+
lastMessageAt: lastActivity(sessionID),
|
|
3657
|
+
thresholds: cautionThreshold
|
|
3658
|
+
});
|
|
3659
|
+
const { sessionTps } = createTpsReader(context, {
|
|
3660
|
+
elapsedMsOf: (id, now) => sessionElapsed(id, now)
|
|
3661
|
+
}, config.sidebar.tps.scope);
|
|
3662
|
+
const sparkValues = (sessionID) => {
|
|
3663
|
+
const sizes = [];
|
|
3664
|
+
for (const entry of messagesOf(sessionID)) {
|
|
3665
|
+
const message = asRecord2(entry);
|
|
3666
|
+
if (message?.type !== "assistant")
|
|
3667
|
+
continue;
|
|
3668
|
+
const output = asCount2(asRecord2(message.tokens)?.output);
|
|
3669
|
+
if (output === undefined)
|
|
3670
|
+
continue;
|
|
3671
|
+
sizes.push(output);
|
|
3672
|
+
}
|
|
3673
|
+
return sizes.slice(-config.layout.sparkWidth);
|
|
3674
|
+
};
|
|
3675
|
+
const goOption = config.sidebar.footer.go;
|
|
3676
|
+
const goPanelEnabled = goOption === true || typeof goOption === "object" && goOption !== null;
|
|
3677
|
+
const animated = config.sidebar.enabled && (goPanelEnabled || config.sidebar.rows.some((name) => ANIMATED_FIELDS.some((field) => field === name)));
|
|
3678
|
+
let spinnerNeeded = false;
|
|
3679
|
+
const ticker = startTicker(context, animated ? config.refresh : 0, () => spinnerNeeded);
|
|
3680
|
+
const wantGuard = config.sidebar.enabled && config.sidebar.rows.includes("guard");
|
|
3681
|
+
const bridge = wantGuard ? startGuardBridge(context) : undefined;
|
|
3682
|
+
const wantGo = config.sidebar.enabled && (config.sidebar.rows.includes("go") || goPanelEnabled);
|
|
3683
|
+
const goBridge = wantGo ? startGoBridge(context) : undefined;
|
|
3684
|
+
const goPanelLayout = goOption === true ? undefined : typeof goOption === "object" && goOption !== null ? goOption : undefined;
|
|
3685
|
+
const goLook = createGoLookReader({ hostOptions: context.options, initial: goPanelLayout });
|
|
3686
|
+
const announce = (sessionID) => {
|
|
3687
|
+
if (!config.caution.enabled)
|
|
3688
|
+
return;
|
|
3689
|
+
const top = worstCaution(cautionsOf(sessionID, Date.now()));
|
|
3690
|
+
return top === undefined ? undefined : cautionMark(top, config.glyphs);
|
|
3691
|
+
};
|
|
3692
|
+
const branchOf = (location) => {
|
|
3693
|
+
try {
|
|
3694
|
+
return context.data.location.vcs.info(location)?.branch?.current;
|
|
3695
|
+
} catch {
|
|
3696
|
+
return;
|
|
3697
|
+
}
|
|
3698
|
+
};
|
|
3699
|
+
const snapshot = (sessionID) => {
|
|
3700
|
+
const location = locationOf(context);
|
|
3701
|
+
const session = context.data.session.get(sessionID);
|
|
3702
|
+
const isBusy = busy(sessionID);
|
|
3703
|
+
spinnerNeeded = wants("status") && isBusy === true;
|
|
3704
|
+
const turns = wants("turns") ? messagesOf(sessionID).filter((entry) => asRecord2(entry)?.["type"] === "user").length : undefined;
|
|
3705
|
+
const tree = wants("cost") || wants("total") || wants("tokens") || wants("cache") || wants("reasoning") ? treeTotals(sessionID, session?.cost) : undefined;
|
|
3706
|
+
return {
|
|
3707
|
+
...session,
|
|
3708
|
+
tokens: tree?.tokens ?? session?.tokens,
|
|
3709
|
+
caution: wants("caution") ? announce(sessionID) : undefined,
|
|
3710
|
+
branch: wants("branch") ? branchOf(location) : undefined,
|
|
3711
|
+
tree,
|
|
3712
|
+
context: wants("context") ? contextUsage(sessionID, session?.model) : undefined,
|
|
3713
|
+
project: wants("project") ? projectTotals(sessionID) : undefined,
|
|
3714
|
+
status: wants("status") ? statusOf(sessionID) : undefined,
|
|
3715
|
+
busy: isBusy,
|
|
3716
|
+
perms: wants("perms") ? permsOf(sessionID) : undefined,
|
|
3717
|
+
tps: wants("tps") ? sessionTps(sessionID, session) : undefined,
|
|
3718
|
+
spark: wants("spark") ? sparkValues(sessionID) : undefined,
|
|
3719
|
+
elapsedMs: wants("elapsed") ? sessionElapsed(sessionID) : undefined,
|
|
3720
|
+
turns,
|
|
3721
|
+
guard: bridge?.status,
|
|
3722
|
+
go: goBridge?.usage,
|
|
3723
|
+
sessionId: wants("ses") ? sessionID : undefined,
|
|
3724
|
+
frame: ticker?.frame ?? 0
|
|
3725
|
+
};
|
|
3726
|
+
};
|
|
3727
|
+
let watchedSession;
|
|
3728
|
+
let toastedKey;
|
|
3729
|
+
let watchTimer;
|
|
3730
|
+
const toastMs = Math.max(1000, config.refresh > 0 ? config.refresh : 1000);
|
|
3731
|
+
if (config.sidebar.enabled && config.caution.enabled && config.caution.toast) {
|
|
3732
|
+
watchTimer = setInterval(() => {
|
|
3733
|
+
const sessionID = watchedSession;
|
|
3734
|
+
if (sessionID === undefined)
|
|
3735
|
+
return;
|
|
3736
|
+
try {
|
|
3737
|
+
const top = worstCaution(cautionsOf(sessionID, Date.now()));
|
|
3738
|
+
if (top === undefined || top.severity !== "caution") {
|
|
3739
|
+
toastedKey = undefined;
|
|
3740
|
+
return;
|
|
3741
|
+
}
|
|
3742
|
+
if (top.key === toastedKey)
|
|
3743
|
+
return;
|
|
3744
|
+
toastedKey = top.key;
|
|
3745
|
+
context.ui.toast.show({
|
|
3746
|
+
title: "Flight Deck",
|
|
3747
|
+
message: cautionDetail(top),
|
|
3748
|
+
variant: "warning"
|
|
3749
|
+
});
|
|
3750
|
+
} catch {}
|
|
3751
|
+
}, toastMs);
|
|
3752
|
+
}
|
|
3753
|
+
if (config.sidebar.enabled) {
|
|
3754
|
+
releases.push(context.ui.slot({
|
|
3755
|
+
append: "sidebar.content",
|
|
3756
|
+
render: ({ sessionID }) => {
|
|
3757
|
+
watchedSession = sessionID;
|
|
3758
|
+
bridge?.follow(sessionID);
|
|
3759
|
+
ticker?.frame;
|
|
3760
|
+
bridge?.status;
|
|
3761
|
+
goBridge?.usage;
|
|
3762
|
+
const source = snapshot(sessionID);
|
|
3763
|
+
const lines = railLines(config, source);
|
|
3764
|
+
if (lines.length === 0)
|
|
3765
|
+
return null;
|
|
3766
|
+
return /* @__PURE__ */ jsx("box", {
|
|
3767
|
+
flexDirection: "column",
|
|
3768
|
+
children: lines.map((line) => themedLine(line, line.field === "ses" ? sessionID : undefined))
|
|
3769
|
+
});
|
|
3770
|
+
}
|
|
3771
|
+
}));
|
|
3772
|
+
}
|
|
3773
|
+
if (config.sidebar.enabled && (config.sidebar.footer.lines.length > 0 || goPanelEnabled)) {
|
|
3774
|
+
releases.push(context.ui.slot({
|
|
3775
|
+
prepend: "sidebar.footer",
|
|
3776
|
+
render: () => {
|
|
3777
|
+
ticker?.frame;
|
|
3778
|
+
const goUsage = goBridge?.usage;
|
|
3779
|
+
const goReason = goBridge === undefined ? "no-bridge" : goBridge.reason ?? (goUsage === undefined ? "pending" : undefined);
|
|
3780
|
+
const panel = goPanelEnabled ? goPanelLines(goUsage, Date.now(), goLook.current(), ticker?.frame ?? 0, goReason) : [];
|
|
3781
|
+
return /* @__PURE__ */ jsxs("box", {
|
|
3782
|
+
flexDirection: "column",
|
|
3783
|
+
children: [
|
|
3784
|
+
config.sidebar.footer.lines.map((text) => themedLine({ text })),
|
|
3785
|
+
panel.map((line) => themedLine(line))
|
|
3786
|
+
]
|
|
3787
|
+
});
|
|
3788
|
+
}
|
|
3789
|
+
}));
|
|
3790
|
+
}
|
|
3791
|
+
if (footer !== undefined) {
|
|
3792
|
+
releases.push(context.ui.slot({
|
|
3793
|
+
append: "prompt.footer.status",
|
|
3794
|
+
render: () => /* @__PURE__ */ jsx("text", {
|
|
3795
|
+
fg: themeColor("subdued", context.theme),
|
|
3796
|
+
children: footer
|
|
3797
|
+
})
|
|
3798
|
+
}));
|
|
3799
|
+
}
|
|
3800
|
+
return () => {
|
|
3801
|
+
if (watchTimer !== undefined)
|
|
3802
|
+
clearInterval(watchTimer);
|
|
3803
|
+
watchTimer = undefined;
|
|
3804
|
+
ticker?.dispose();
|
|
3805
|
+
bridge?.dispose();
|
|
3806
|
+
goBridge?.dispose();
|
|
3807
|
+
for (const release of releases)
|
|
3808
|
+
release();
|
|
3809
|
+
};
|
|
3810
|
+
}
|
|
3811
|
+
});
|
|
3812
|
+
export {
|
|
3813
|
+
tui_default as default
|
|
3814
|
+
};
|