-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandcode-hud.mod.ts
More file actions
600 lines (556 loc) · 20.5 KB
/
Copy pathcommandcode-hud.mod.ts
File metadata and controls
600 lines (556 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import {readFileSync, statSync} from 'node:fs';
import type {ModApi} from '@commandcode/harness';
/**
* Command Code 1.14.1 UI surfaces (docs + dist/cli.mjs):
*
* Native footer (inside InputBox):
* `» permission bypass on [shift+tab]` / `? for shortcuts` / `taste on`
* → ModeIndicator + TasteStateBadge. Mods cannot write into that row.
*
* Mod footer line (BELOW the input box):
* `cmd.ui.setStatus(text)` → ModStatusLine
* One segment per mod; multiple mods join with " ".
* Updates replace THAT mod's segment only; they do NOT wipe native indicators.
* Render: segments.map(s => s.text).join(" ") under the input.
* ModStatusLine has hardcoded paddingLeft:2 and sits AFTER a divider line
* (`lt.line.repeat(width)`), so it will always appear one row below the footer.
*
* `cmd.ui.widget` / `refreshWidgets`:
* Public API exists, but runtime is a no-op in 1.14.1 (wire-up pending).
*
* `cmd.addRenderer` + `cmd.showEntry`:
* Feed rows ABOVE the input. Append-only; no update/replace entry API.
*
* Notes on persistence across /reload and /resume:
* - `setStatus(null)` clears the segment; calling it onSessionEnd wipes the
* status before the new session repaints → avoided.
* - State is persisted via cmd.session.appendCustomEntry and re-read on start.
*/
const ANSI_CYAN = '\u001b[36m';
const ANSI_DIM = '\u001b[2m';
const ANSI_RESET = '\u001b[0m';
const ANSI_YELLOW = '\u001b[33m';
const ANSI_GREEN = '\u001b[32m';
const ANSI_RED = '\u001b[31m';
const BRAIN = '\uD83E\uDDE0';
// Context windows per model, mirroring the official list at
// https://commandcode.ai/models (70 models) and Command Code 1.53.0's canonical
// registry (dist/cli.mjs getKnownModelContextWindow). Used to render `ctx used/limit`.
// IDs match `cmd --list-models` / config.json (lowercase).
// GLM-5.1, MiniMax M2.7 (paid) and Qwen 3.6.x are absent from the dist table;
// their values come from the official website.
const CONTEXT_LIMITS: Record<string, number> = {
// GPT-6 / GPT-5.x
'gpt-6-astra': 1_050_000,
'gpt-5.6-luna': 1_050_000,
'gpt-5.6-sol': 1_050_000,
'gpt-5.6-terra': 1_050_000,
'gpt-5.5': 400_000,
'gpt-5.4': 400_000,
'gpt-5.4-mini': 400_000,
'gpt-5.3-codex': 400_000,
// Claude
'claude-opus-5': 1_000_000,
'claude-opus-4-8': 1_000_000,
'claude-opus-4-7': 1_000_000,
'claude-sonnet-5': 1_000_000,
'claude-sonnet-4-6': 1_000_000,
'claude-fable-5-1': 1_000_000,
'claude-fable-5': 1_000_000,
'claude-haiku-4-5-20251001': 200_000,
// DeepSeek
'deepseek/deepseek-v4.1-flash': 1_000_000,
'deepseek/deepseek-v4-flash-vision-exp': 1_000_000,
'deepseek/deepseek-v4-flash-fast': 1_000_000,
'deepseek/deepseek-v4-pro': 1_000_000,
'deepseek/deepseek-v4-flash': 1_000_000,
// Gemini
'google/gemini-3.8-flash': 1_000_000,
'google/gemini-3.7-flash': 1_048_576,
'google/gemini-3.6-flash': 1_000_000,
'google/gemini-3.5-flash': 1_000_000,
'google/gemini-3.5-flash-lite': 1_000_000,
'google/gemini-3.1-flash-lite': 1_000_000,
// Grok
'xai/grok-4.6': 500_000,
'xai/grok-4.5': 500_000,
// Kimi
'moonshotai/kimi-k3': 1_000_000,
'moonshotai/kimi-k2.7-code-highspeed': 262_000,
'moonshotai/kimi-k2.7-code': 256_000,
'moonshotai/kimi-k2.6': 256_000,
'moonshotai/kimi-k2.5': 256_000,
// GLM
'z-ai/glm-5.3-flash': 1_048_576,
'zai-org/glm-5.3': 1_000_000,
'zai-org/glm-5.2': 1_000_000,
'zai-org/glm-5.2-fast': 1_000_000,
'zai-org/glm-5.1': 200_000,
'zai-org/glm-5': 200_000,
// Qwen
'qwen/qwen3.8-max-0902': 1_000_000,
'qwen/qwen3.8-flash': 1_000_000,
'qwen/qwen3.8-27b': 262_144,
'qwen/qwen3.8-max': 1_000_000,
'qwen/qwen3.7-max': 1_000_000,
'qwen/qwen3.7-plus': 1_000_000,
'qwen/qwen3.7-flash': 1_000_000,
'qwen/qwen3.6-max-preview': 200_000,
'qwen/qwen3.6-plus': 200_000,
// Muse
'meta/muse-spark-1.3': 1_048_576,
'meta/muse-spark-1.3-contributor': 1_048_576,
'meta/muse-spark-1.1': 1_048_576,
'meta/muse-spark-1.2': 1_048_576,
'meta/muse-spark-1.2-contributor': 1_048_576,
// MiniMax
'minimaxai/minimax-m3': 1_000_000,
'minimaxai/minimax-m3-free': 1_000_000,
'minimaxai/minimax-m2.7-free': 197_000,
'minimaxai/minimax-m2.7': 200_000,
'minimaxai/minimax-m2.5': 200_000,
// Step
'stepfun/step-3.7-flash': 256_000,
'stepfun/step-3.5-flash': 1_000_000,
// Tencent
'tencent/hy4-preview': 1_048_576,
'tencent/hy3-paid': 262_144,
'tencent/hy3': 262_144,
// Fugu
'sakana/fugu-ultra': 1_000_000,
// Nemotron
'nvidia/nemotron-3-ultra-550b-a55b': 1_000_000,
// MiMo
'xiaomi/mimo-v2.5-pro': 1_000_000,
'xiaomi/mimo-v2.5': 1_000_000,
// Inkling
'thinkingmachines/inkling': 256_000,
'thinkingmachines/inkling-small': 1_000_000,
// Laguna
'poolside/laguna-s-2.1-free': 256_000,
// LongCat
'meituan/longcat-2.0:free': 1_048_576,
// Ling
'inclusionai/ling-3.0-flash-sante:free': 262_144,
'inclusionai/ling-3.0-flash-free': 256_000,
};
const contextLimitFor = (model: string | undefined): number => {
if (!model) return 0;
if (CONTEXT_LIMITS[model]) return CONTEXT_LIMITS[model];
// Also accept short names (after the last "/") and case-insensitive lookups.
const short = model.includes('/') ? model.slice(model.lastIndexOf('/') + 1) : model;
const exact = CONTEXT_LIMITS[short] ?? CONTEXT_LIMITS[model.toLowerCase()];
if (exact) return exact;
// Reverse lookup: find any registered id ending with the given short name.
for (const [id, limit] of Object.entries(CONTEXT_LIMITS)) {
if (id.slice(id.lastIndexOf('/') + 1) === short) return limit;
}
// Unknown model: omit the ctx segment rather than show a wrong value.
return 0;
};
const formatTokens = (value: number): string => {
if (value >= 1_000_000) {
const m = value / 1_000_000;
return `${Number.isInteger(m) ? m : m.toFixed(1)}M`;
}
if (value >= 1_000) {
const k = value / 1_000;
return `${Number.isInteger(k) ? k : k.toFixed(1)}k`;
}
return String(value);
};
const STATE_ENTRY_TYPE = 'commandcode-hud/state-v1';
const CTX_BAR_CELLS = 10;
const renderCtxBar = (pct: number): string => {
const clamped = Math.min(100, Math.max(0, Math.round(pct)));
const filled = Math.min(CTX_BAR_CELLS, Math.floor((clamped / 100) * CTX_BAR_CELLS));
// Threshold colors mirror the reference statusline: green <70, yellow <90, red >=90.
const color = clamped >= 90 ? ANSI_RED : clamped >= 70 ? ANSI_YELLOW : ANSI_GREEN;
const warn = clamped >= 90 ? ` ${ANSI_RED}⚠${ANSI_RESET}` : '';
let bar = '';
for (let i = 0; i < CTX_BAR_CELLS; i++) bar += i < filled ? '█' : '░';
return `${color}${bar} ${clamped}%${ANSI_RESET}${warn}`;
};
const dirtyFromStatus = (stdout: string): boolean =>
stdout.split(/\r?\n/).some(line => line.trim() !== '' && !line.startsWith('## '));
const formatDuration = (elapsedMs: number): string | undefined => {
const sec = Math.floor(elapsedMs / 1000);
if (sec <= 0) return undefined;
const min = Math.floor(sec / 60);
if (min >= 60) {
return `${Math.floor(min / 60)}h${min % 60}m`;
}
return `${min}m${sec % 60}s`;
};
const AUTH_PATH = `${process.env.HOME}/.commandcode/auth.json`;
const BILLING_URL = 'https://api.commandcode.ai/alpha/billing/credits';
const BILLING_TTL_MS = 60_000;
interface RateWindowData {
usedPct: number;
resetAt?: number;
}
interface BillingData {
creditsRemaining?: number;
fiveHour?: RateWindowData;
weekly?: RateWindowData;
}
const readApiKeyFrom = (path: string): string | undefined => {
try {
const parsed = JSON.parse(readFileSync(path, 'utf8')) as Record<string, unknown>;
return text(parsed['apiKey']);
} catch {
return undefined;
}
};
const parseBilling = (stdout: string, code: number): BillingData | undefined => {
if (code !== 0) return undefined;
let root: unknown;
try {
root = JSON.parse(stdout);
} catch {
return undefined;
}
const out: BillingData = {};
const monthly = record(record(root).credits).monthlyCredits;
if (typeof monthly === 'number' && Number.isFinite(monthly) && monthly >= 0) {
out.creditsRemaining = monthly;
}
const windows = record(record(root).windowLimits);
const parseWindow = (key: string): RateWindowData | undefined => {
const w = record(windows[key]);
const cap = w['cap'];
if (typeof cap !== 'number' || !(cap > 0)) return undefined;
const used = typeof w['used'] === 'number' && w['used'] >= 0 ? w['used'] : 0;
const resetAt = typeof w['resetAt'] === 'number' && w['resetAt'] > 0 ? w['resetAt'] : undefined;
return {usedPct: Math.min(100, Math.round((used / cap) * 100)), resetAt};
};
const fiveHour = parseWindow('fiveHour');
if (fiveHour) out.fiveHour = fiveHour;
const weekly = parseWindow('weekly');
if (weekly) out.weekly = weekly;
return Object.keys(out).length > 0 ? out : undefined;
};
const formatCountdown = (resetAtMs: number, nowMs: number): string | undefined => {
const min = Math.floor((resetAtMs - nowMs) / 60_000);
if (min < 1) return undefined;
const h = Math.floor(min / 60);
if (h >= 24) return `${Math.floor(h / 24)}d${h % 24}h`;
if (h >= 1) return `${h}h${min % 60}m`;
return `${min}m`;
};
const formatCredits = (amount: number): string =>
`${amount <= 0 ? ANSI_RED : ''}$${Math.max(0, amount).toFixed(2)}${amount <= 0 ? ANSI_RESET : ''}`;
const renderRateWindow = (label: string, w: RateWindowData, nowMs: number): string => {
const color = w.usedPct >= 90 ? ANSI_RED : w.usedPct >= 70 ? ANSI_YELLOW : ANSI_GREEN;
const countdown = w.resetAt !== undefined ? formatCountdown(w.resetAt, nowMs) : undefined;
const suffix = countdown ? ` ${ANSI_DIM}(${countdown})${ANSI_RESET}` : '';
return `${color}${label} ${w.usedPct}%${ANSI_RESET}${suffix}`;
};
interface BillingFetcher {
get: () => BillingData | undefined;
refresh: () => Promise<void>;
}
const createBillingFetcher = (deps: {
exec: (opts: {command: string; args: string[]}) => Promise<{code: number; stdout: string}>;
readKey: () => string | undefined;
ttlMs?: number;
now?: () => number;
}): BillingFetcher => {
const ttlMs = deps.ttlMs ?? BILLING_TTL_MS;
const now = deps.now ?? Date.now;
let cache: {data: BillingData; at: number} | undefined;
let lastAttempt = -Infinity;
let inFlight = false;
return {
get: () => cache?.data,
refresh: async (): Promise<void> => {
if (inFlight || now() - lastAttempt < ttlMs) return;
const key = deps.readKey();
if (!key) return;
lastAttempt = now();
inFlight = true;
try {
const result = await deps.exec({
command: 'curl',
args: ['-s', '-m', '10', '-H', `Authorization: Bearer ${key}`, '-H', 'Accept: application/json', BILLING_URL],
});
const parsed = parseBilling(String(result?.stdout ?? ''), result?.code ?? 1);
if (parsed) cache = {data: parsed, at: now()};
} catch {
// Silent degradation: segments stay hidden until the next TTL window.
} finally {
inFlight = false;
}
},
};
};
const record = (value: unknown): Record<string, unknown> =>
value !== null && typeof value === 'object' ? value as Record<string, unknown> : {};
const text = (value: unknown): string | undefined =>
typeof value === 'string' && value.trim() ? value.trim() : undefined;
const branchNameFromStatus = (stdout: string): string | undefined => {
const header = stdout.split(/\r?\n/).find(line => line.startsWith('## '));
const headerText = header?.slice(3).trim() ?? '';
if (!headerText) return undefined;
if (headerText.startsWith('No commits yet on ')) {
return headerText.slice('No commits yet on '.length).trim() || undefined;
}
const name = headerText.split('...')[0]?.trim() ?? '';
return name === 'HEAD (no branch)' ? 'detached' : name || undefined;
};
const resolveModelName = (model: unknown, fallback = ''): string => {
if (typeof model === 'string') return model || fallback;
const m = record(model);
return (
text(m.displayName) ??
text(m.display_name) ??
text(m.name) ??
text(m.id) ??
fallback
);
};
const shortModel = (full: string): string => {
const lower = full.toLowerCase();
if (lower.includes('sonnet')) return 'Sonnet';
if (lower.includes('opus')) return 'Opus';
if (lower.includes('haiku')) return 'Haiku';
const slash = full.lastIndexOf('/');
if (slash >= 0) return full.slice(slash + 1);
return full;
};
const modelFromArgs = (): string | undefined => {
const args = process.argv;
const index = args.indexOf('--model');
return index >= 0 ? text(args[index + 1]) : undefined;
};
const effortFromConfig = (model: string): string | undefined => {
try {
const config = JSON.parse(readFileSync(`${process.env.HOME}/.commandcode/config.json`, 'utf8')) as Record<string, unknown>;
const efforts = record(config.reasoningEffort);
return text(efforts[model]) ?? text(config.effort);
} catch {
return undefined;
}
};
const CONFIG_PATH = `${process.env.HOME}/.commandcode/config.json`;
const readModelConfig = (): {model?: string; effort?: string; mtimeMs?: number} => {
try {
const stat = statSync(CONFIG_PATH);
const parsed = JSON.parse(readFileSync(CONFIG_PATH, 'utf8')) as Record<string, unknown>;
const effortMap = record(parsed.reasoningEffort);
const model = text(parsed.model);
return {
model,
effort: model ? text(effortMap[model]) ?? undefined : text(parsed.effort) ?? undefined,
mtimeMs: stat.mtimeMs,
};
} catch {
return {};
}
};
// Export for tests.
export const __test = {
branchNameFromStatus,
dirtyFromStatus,
readModelConfig,
contextLimitFor,
formatTokens,
formatDuration,
renderCtxBar,
readApiKeyFrom,
parseBilling,
formatCountdown,
formatCredits,
renderRateWindow,
createBillingFetcher,
};
export default function (cmd: ModApi): void {
let lastPublished = '';
let currentBranch: string | undefined;
const explicitModel = modelFromArgs();
const initialConfig = readModelConfig();
let currentModel = explicitModel ?? initialConfig.model ?? 'gpt-5.6-luna';
let currentEffort: string | undefined = initialConfig.effort ?? effortFromConfig(currentModel);
let usedTokens = 0;
let currentDirty = false;
let sessionStartedAt: number = Date.now();
const billing = createBillingFetcher({exec: cmd.exec, readKey: () => readApiKeyFrom(AUTH_PATH)});
const refreshBilling = async (): Promise<void> => {
await billing.refresh();
publish();
};
const render = (): string => {
const segments: string[] = [];
const modelParts: string[] = [];
if (currentModel) {
modelParts.push(`${ANSI_CYAN}${shortModel(currentModel)}${ANSI_RESET}`);
if (currentEffort) modelParts.push(`${BRAIN} ${currentEffort}`);
}
if (modelParts.length) segments.push(modelParts.join(' '));
const limit = contextLimitFor(currentModel);
if (limit > 0) {
const percent = Math.min(100, Math.round((usedTokens / limit) * 100));
segments.push(
`${renderCtxBar(percent)} · ${ANSI_YELLOW}${formatTokens(usedTokens)}/${formatTokens(limit)}${ANSI_RESET}`,
);
}
if (currentBranch) {
segments.push(`${ANSI_CYAN}${currentBranch}${currentDirty ? '*' : ''}${ANSI_RESET}`);
} else {
segments.push(`${ANSI_DIM}no-branch${ANSI_RESET}`);
}
const duration = formatDuration(Date.now() - sessionStartedAt);
if (duration) segments.push(`${ANSI_DIM}${duration}${ANSI_RESET}`);
// Billing goes on a second status line (the installer patches the CLI
// sanitizer to allow \n; unpatched runtimes collapse it to a space).
const billingSegments: string[] = [];
const billingData = billing.get();
if (billingData) {
const nowMs = Date.now();
if (billingData.fiveHour) billingSegments.push(renderRateWindow('5h', billingData.fiveHour, nowMs));
if (billingData.weekly) billingSegments.push(renderRateWindow('wk', billingData.weekly, nowMs));
if (billingData.creditsRemaining !== undefined) {
billingSegments.push(formatCredits(billingData.creditsRemaining));
}
}
const line = segments.join(' · ');
return billingSegments.length > 0 ? `${line}\n${billingSegments.join(' · ')}` : line;
};
const publish = (force = false): void => {
const next = render().trim();
if (!force && next === lastPublished) return;
lastPublished = next;
cmd.ui.setStatus(next);
};
const persist = (): void => {
cmd.session?.appendCustomEntry({
customType: STATE_ENTRY_TYPE,
data: {
branch: currentBranch,
dirty: currentDirty,
model: currentModel,
effort: currentEffort,
usedTokens,
sessionStartedAt,
},
});
};
const restore = (): void => {
const session = cmd.session;
if (!session) return;
const last = session
.getCustomEntries({customType: STATE_ENTRY_TYPE})
.map(entry => record(entry).data)
.at(-1);
if (!last) return;
currentBranch = text(last['branch']);
const storedDirty = last['dirty'];
if (typeof storedDirty === 'boolean') currentDirty = storedDirty;
currentModel = explicitModel ?? text(last['model']) ?? currentModel;
currentEffort = text(last['effort']) ?? currentEffort ?? effortFromConfig(currentModel);
const storedTokens = record(last).usedTokens;
if (typeof storedTokens === 'number') usedTokens = storedTokens;
const storedStart = last['sessionStartedAt'];
if (typeof storedStart === 'number' && storedStart > 0) sessionStartedAt = storedStart;
lastPublished = '';
publish(true);
};
const refreshBranch = async (): Promise<void> => {
try {
const result = await cmd.exec({
command: 'git',
args: ['status', '--porcelain=1', '--branch'],
cwd: cmd.cwd,
});
currentBranch = result.code === 0 ? branchNameFromStatus(result.stdout) : undefined;
currentDirty = result.code === 0 ? dirtyFromStatus(result.stdout) : false;
persist();
publish();
} catch {
currentBranch = undefined;
currentDirty = false;
persist();
publish();
}
};
const captureModelAndEffort = (event: unknown, forcePublish = false): void => {
const value = record(event);
const modelRaw = resolveModelName(value.model);
// Effort comes as event.effort (string level) in 1.14.1; check string and object forms
const effortDirect = text(value.effort);
const effortObj = text(record(value.effort).level);
const effortNew = effortDirect ?? effortObj;
let changed = false;
if (modelRaw) {
currentModel = modelRaw;
changed = true;
}
// Only overwrite effort if present; don't clear an earlier effort on empty events
if (effortNew) {
currentEffort = effortNew;
changed = true;
}
// model_request_end usage → used context tokens (input + cache reads), same
// source the TUI context meter uses.
const usage = record(value.usage);
const inputTokens = usage.inputTokens;
if (typeof inputTokens === 'number' && inputTokens > 0) {
usedTokens = inputTokens;
changed = true;
}
if (changed) {
persist();
publish(forcePublish);
}
};
// `/model` writes ~/.commandcode/config.json. Poll its mtime so the HUD
// updates right after the user switches models, without waiting for the
// next model_request_start.
let lastConfigMtime = readModelConfig().mtimeMs ?? 0;
const pollModelConfig = (): void => {
const fresh = readModelConfig();
if (fresh.mtimeMs === undefined || fresh.mtimeMs === lastConfigMtime) return;
lastConfigMtime = fresh.mtimeMs;
if (fresh.model && fresh.model !== currentModel) {
currentModel = fresh.model;
currentEffort = fresh.effort ?? currentEffort;
persist();
publish(true);
}
};
setInterval(pollModelConfig, 2000).unref();
cmd.hooks({
onSessionStart: () => {
restore();
void refreshBranch();
},
});
// No onSessionEnd setStatus(null): it wipes the segment on /reload before the
// new session repaints. Persist instead so /resume and /new recover it.
cmd.on('model_request_start', event => {
captureModelAndEffort(event, true);
});
cmd.on('model_request_end', event => {
captureModelAndEffort(event, true);
});
cmd.on('config_setting_changed', event => {
const value = record(event);
if (value.setting !== 'model') return;
const model = resolveModelName(value.value, currentModel);
if (!model) return;
currentModel = model;
currentEffort = effortFromConfig(model) ?? currentEffort;
persist();
publish(true);
});
cmd.on('run_end', () => {
void refreshBranch();
void refreshBilling();
});
// Immediate first paint for interactive sessions.
restore();
void refreshBranch();
void refreshBilling();
// Also publish model context early if we restored it, before any model event fires
publish(true);
}