A pixel-art HUD for Claude Code: context, cost, cache, agents and rate limits at a glance, little 8-bit scenes while Claude thinks, and Telegram notifications.

A pixel-art HUD for Claude Code.
Context, cost, cache, agents and rate limits at a glance,<br> and little 8-bit scenes that play while Claude thinks.
English · 繁體中文
<img src="assets/hud.svg" alt="hudcade inside a Claude Code session: a pixel-scene spinner, the HUD band and the status line" width="100%">
Claude Code shows you the conversation. hudcade shows you everything happening around it.
In a Claude Code terminal session:
/plugin install hudcade --marketplace g761007/hudcade
y to add the marketplace.The HUD shows up right away, in every session from then on, including those the desktop app starts on the same machine.
If you used a statusLine command before, you can remove it from ~/.claude/settings.json. hudcade draws its own status line.
claude plugin marketplace add g761007/hudcade
claude plugin install hudcade@hudcade
Above the prompt, in the terminal and in the desktop app. Each row has a left and a right side. On a narrow terminal the least important parts drop out first.
| Row | What it tells you |
|---|---|
| Identity | Model and effort · project and branch (dirty, ahead/behind) · lines added and removed and files edited this session · session cost, what this turn added, session time |
| Capacity | Context bar, tokens left and compactions · prompt-cache hit ratio · 5-hour and 7-day limits with reset countdowns |
| Activity | Working, waiting for your approval, or idle · turn number, start time (and end time once done) · the tool running now · tokens in and out this turn · output speed · tool calls and failures · task progress |
| Context breakdown | The largest parts of the window. Only appears once context passes the warning threshold. |
| Workers | Subagents, teammates, background tasks and loops, while there are any |
Toasts warn you when context crosses its thresholds, and before a model switch that would throw away a warm prompt cache. The toast includes what rebuilding it will cost.
A compact line under the prompt, for when you only want the essentials:
Opus 5.5 · xhigh │ ctx 41% │ $3.21 │ 5h 13% │ 7d 38%
<img src="assets/demo.gif" alt="The ten spinner scenes, switching between thinking, running a tool and replying" width="828">
While Claude works, the spinner turns into a little 8-bit scene, a different one each turn. Every scene reacts to what Claude is doing:
| Scene | Thinking | Running a tool | Replying |
|---|---|---|---|
| Robot coder | The antenna blinks, a question forms | A progress bar fills, gears turn, sparks fly | It types; bits stream out of the screen |
| Coffee conveyor | Mugs roll under a slow drip | A kettle pours, the drip speeds up | The belt speeds up, every mug steams |
| Little garden | Plants sprout and sway | A watering can flies over | Everything blooms, butterflies arrive |
| Rocket voyage | Cruising among the stars | Collecting energy crystals | Warp speed |
| Cat and yarn | The cat sits, tail swishing | It pounces, the yarn unrolls | A full-speed chase |
| Fishing | The bobber bobs, fish swim below | A bite and a splash | Fish leap out of the water |
| Steam train | Puffing through the trees | The cars load cargo | Full steam ahead |
| Campfire | Small flames under the stars | A log goes in, the fire flares | A marshmallow toasts |
| Weather | Sun and drifting clouds | Storm clouds, rain and lightning | A rainbow and birds |
| Equalizer | A low, gentle wave | Pulses on the beat | Peaks in the red |
Next to the scene are the spinner word, the elapsed time, the effort, the current activity, and what the turn has done so far: tool calls, output tokens, speed and cost (2 tools · ↓900 · 50 tok/s · +$0.42). Pin one scene with the spinnerScene option.
The spinner is terminal only; the desktop app keeps its own.
✅ Claude Code is done
Project: my-app (feat/retry)
Time: 2026-10-08 14:34:19 CST
Took 2m14s · Opus 5.5 · xhigh · $3.21
5h:13%(2h02m) wk:81%(5d16h) ctx:65%(opus)
Summary: Added a retry policy with exponential backoff and covered it with 6 tests.
Session: 11fa1a35
Messages are sent in three cases:
Messages come in English or Traditional Chinese. By default they follow Claude Code's own language setting. The bot token stays in Claude Code's secure storage and never appears in a log.
Everything lives in /config, or run claude plugin configure hudcade:
| Option | Default | Meaning |
|---|---|---|
hud | true | The HUD band |
statusLine | true | The line under the prompt |
spinner | true | The pixel spinner (terminal) |
spinnerScene | random | random, or one of robot, coffee, garden, rocket, cat, fishing, train, campfire, weather, equalizer |
ctxWarnPercent / ctxCriticalPercent | 60 / 80 | Context thresholds for colors, toasts and the breakdown row |
notify | true | Telegram notifications |
notifyMinTurnSeconds | 0 | Skip completion messages for shorter turns; 0 sends every turn |
notifyBlocklist | empty | Project folder names that never notify, separated by spaces |
notifyLanguage | auto | auto follows Claude Code's language setting (Chinese there means Traditional Chinese, anything else English); or en, zh-TW |
telegramBotToken / telegramChatId | — | Your bot and chat. When unset, ~/.claude/hooks/.telegram.env (TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID) is read if it exists |
Per project, .claude/claude-mods.json (shared with the team) and .claude/claude-mods.local.json (just you) override any option except the Telegram credentials:
{ "notify": false, "spinnerScene": "campfire" }
Commands:
/hud: what is switched on, where the settings came from, and what the HUD has read./hud test-notify: send a test Telegram message.hudcade is a mod: a plugin of function hooks that runs inside Claude Code itself.
$.session.usage(). Tool calls, subagents and tasks come from tool.call and the session events. Tokens, cache reads and output speed come from the model's own response stream.Raster repainted about twelve times a second while a turn runs, and only then.git and date on PATH./hud to see what the HUD has read and any reading that failed. For more detail, start claude --debug and look for lines starting with hudcade:./hud tells you whether Telegram is configured. Then try /hud test-notify..claude-plugin/marketplace.json the marketplace
plugin/ the mod
.claude-plugin/plugin.json manifest and options
hooks/register.tsx every hook
src/core/ pure logic: HUD model, band layout, config, messages
src/core/scenes/ one file per spinner scene
types/index.d.ts the session-state contract
tests/ unit and drawing tests
scripts/render-assets.ts renders assets/ from the mod's own code
tsc -p plugin # type-check (TypeScript 5.4+)
claude plugin validate plugin --strict # what the engine would refuse
claude plugin test plugin # tests, terminal and desktop surfaces
claude --plugin-dir ./plugin # run your working copy; saving a file reloads it
bun scripts/render-assets.ts # regenerate the README images (Bun + ImageMagick)
All hooks live in one file because of two engine rules:
$ only into functions declared in the same file.The editor and tsc read the API declarations from plugin/.claude-plugin/types/. The engine writes that folder whenever it loads the mod. Before the first load, copy it from the plugin-authoring skill's types/claude-code.d.ts.
Issues and pull requests are welcome, especially new scenes, new HUD rows and notification channels beyond Telegram.
A scene is one file in plugin/src/core/scenes/: a draw(canvas, mode, t) function that paints an 8-pixel-tall frame for the think, tool and reply moods. Register it in scenes/index.ts, add its name to SceneName and to the spinnerScene options, and the tests check that it draws in every mood and at every width.
Please run the three checks above before opening a PR.
hudcade is an independent community project. It is not affiliated with, endorsed by or sponsored by Anthropic. Claude and Claude Code are trademarks of Anthropic, PBC.
hooks/register.tsx 749 lines1// hudcade: a HUD band with session and agent progress, a status line, a
2// pixel-scene spinner (terminal), and Telegram notifications.
3//
4// The engine admits one hook per event in a module and follows `$` only into
5// functions of the same file, so every hook lives here and dispatches to the
6// enabled features; the rules themselves are pure functions in src/core.
7
8import { atom, read, update } from 'claude-code'
9import type { EngineInterface, Register, Timer } from 'claude-code'
10
11import type { Hud, SceneName } from '../types'
12import { buildBand, statusLine, type Seg } from '../src/core/band'
13import { createRuntime, parseEnvFile, resolveConfig, type Layer, type Runtime } from '../src/core/config'
14import { basename, clock, modelLabel, parseTzOffset, tokens, toolDetail, toolLabel, truncate, usd } from '../src/core/format'
15import { SCENE_ROWS, SCENES, modeLabel, modeOf, pickScene, renderScene, type SpinnerMode } from '../src/core/scenes'
16import { turnFacts } from '../src/core/spinner'
17import {
18 addDiff,
19 addRequestUsage,
20 agentResult,
21 applyResume,
22 applyUsage,
23 completeTurn,
24 emptyHud,
25 endTool,
26 finishAgent,
27 launchAgent,
28 linkAgent,
29 noteCompaction,
30 parseGitStatus,
31 patchCounts,
32 pauseAgent,
33 reconcileAgents,
34 resumeAgent,
35 setCacheTtl,
36 setCtxTop,
37 setAgentModel,
38 setTeammates,
39 setWaiting,
40 setWorkers,
41 startTool,
42 startTurn,
43 taskCreated,
44 taskUpdated,
45 todosWritten,
46 withDefaults,
47 type UsageInput,
48} from '../src/core/hud-model'
49import { buildMessage, resolveLanguage, summarize, type NotifyKind } from '../src/core/telegram'
50
51// The one session value the drawings read: a write redraws the band and the spinner.
52const hudAtom = atom({ plugin: 'hudcade', key: 'hud' } as const, emptyHud(''))
53
54const PROJECT_FILES = ['claude-mods.json', 'claude-mods.local.json'] as const
55const GIT_THROTTLE_MS = 3_000
56const GIT_TOOLS = new Set(['Bash', 'Edit', 'Write', 'NotebookEdit'])
57const AGENT_SYNC_MS = 15_000
58const FRAME_MS = 80
59const SPINNER_TEXT_COLUMNS = 36
60
61type NotifyExtra = { durationMs?: number; summary?: string; detail?: string }
62
63// Every write and read sees a complete state, even one an older version of
64// the mod left in this session.
65function patch($: EngineInterface, change: (h: Hud) => Hud): Promise<Hud> {
66 return update($, hudAtom, h => change(withDefaults(h)))
67}
68
69async function snapshot($: EngineInterface): Promise<Hud> {
70 return withDefaults(await read($, hudAtom))
71}
72
73// Module-local working state (starts over on a hot reload); the values the
74// drawings read live in hudAtom.
75let runtime: Runtime = createRuntime({})
76let values: Record<string, unknown> = {}
77let lastGitAt = 0
78let lastAgentSyncAt = 0
79let gitNote = 'not read yet'
80let lastStatus: string | undefined
81let ticker: Timer | undefined
82let warned: 'none' | 'warn' | 'critical' = 'none'
83let frameTimer: Timer | undefined
84let isTicking = false
85let turnStartedAt = 0
86let toolsAtStart = 0
87let scene: SceneName = 'robot'
88let lastScene: SceneName | undefined
89let spinnerMode: SpinnerMode | undefined
90const spinners = new Map<string, number>() // mounted spinner requestId → scene columns
91
92// ── Configuration ─────────────────────────────────────────────────────────
93
94// Project overrides and the legacy Telegram env file are read through `$`,
95// so the configuration is completed when the session starts.
96async function loadRuntime($: EngineInterface): Promise<void> {
97 const root = await $.session.root()
98 const layers: Layer[] = []
99 const readWarnings: string[] = []
100
101 for (const name of PROJECT_FILES) {
102 const path = `${root}/.claude/${name}`
103 if (!(await $.fs.exists(path))) continue
104 try {
105 const parsed: unknown = JSON.parse(await $.fs.read(path))
106 if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
107 layers.push({ source: `.claude/${name}`, values: parsed as Record<string, unknown> })
108 } else {
109 readWarnings.push(`.claude/${name}: expected a JSON object`)
110 }
111 } catch (error) {
112 readWarnings.push(`.claude/${name}: ${error instanceof Error ? error.message : String(error)}`)
113 }
114 }
115
116 const { config, warnings } = resolveConfig(values, layers)
117 runtime.config = config
118 runtime.warnings = [...readWarnings, ...warnings]
119 runtime.sources = ['userConfig', ...layers.map(layer => layer.source)]
120 runtime.telegramSource = config.telegramBotToken && config.telegramChatId ? 'userConfig' : undefined
121
122 if (!runtime.telegramSource) {
123 const home = await $.env.get('HOME')
124 const path = `${home}/.claude/hooks/.telegram.env`
125 if (home && (await $.fs.exists(path))) {
126 const env = parseEnvFile(await $.fs.read(path))
127 config.telegramBotToken ||= env.TELEGRAM_BOT_TOKEN
128 config.telegramChatId ||= env.TELEGRAM_CHAT_ID
129 if (config.telegramBotToken && config.telegramChatId) runtime.telegramSource = '~/.claude/hooks/.telegram.env'
130 }
131 }
132}
133
134// ── HUD tracking ──────────────────────────────────────────────────────────
135
136async function refreshGit($: EngineInterface, force = false): Promise<void> {
137 const now = await $.clock.now()
138 if (!force && now - lastGitAt < GIT_THROTTLE_MS) return
139 lastGitAt = now
140 try {
141 const { exitCode, stdout } = await $.process.run(
142 ['git', '--no-optional-locks', 'status', '--porcelain=v2', '--branch', '-z'],
143 { timeoutMs: 2_000 },
144 )
145 const git = exitCode === 0 ? parseGitStatus(stdout) : undefined
146 gitNote = git ? 'ok' : `exit ${exitCode}`
147 await patch($, h => ({ ...h, git }))
148 } catch (error) {
149 // Not a repository, or no git: the band shows no branch.
150 gitNote = error instanceof Error ? error.message : String(error)
151 }
152}
153
154async function applyMeasure($: EngineInterface, usage: UsageInput): Promise<void> {
155 await patch($, h => applyUsage(h, usage))
156 const percent = usage.context?.percent
157 if (percent === undefined || percent <= 0 || !runtime.config.hud) return
158 const { ctxWarnPercent, ctxCriticalPercent } = runtime.config
159 if (percent >= ctxCriticalPercent && warned !== 'critical') {
160 warned = 'critical'
161 $.ui.toast(`Context ${Math.round(percent)}% — consider /compact soon`, { timeoutMs: 8_000 })
162 } else if (percent >= ctxWarnPercent && warned === 'none') {
163 warned = 'warn'
164 $.ui.toast(`Context ${Math.round(percent)}% used`)
165 } else if (percent < ctxWarnPercent) {
166 warned = 'none'
167 }
168}
169
170// The engine's roster: teammate rows, and agent rows it has ended though no
171// SubagentStop arrived. The ticker reads it once per AGENT_SYNC_MS at most.
172async function refreshAgents($: EngineInterface, isThrottled = false): Promise<void> {
173 try {
174 const now = await $.clock.now()
175 if (isThrottled && now - lastAgentSyncAt < AGENT_SYNC_MS) return
176 lastAgentSyncAt = now
177 const agents = await $.agent.list()
178 const h = await patch($, h => reconcileAgents(setTeammates(h, agents), agents, now))
179 syncTicker($, h.turn.status !== 'idle' || h.agents.some(a => a.status === 'running'))
180 } catch {
181 // Agent teams off, or the roster unavailable: no teammate rows, and agent
182 // rows wait for SubagentStop.
183 }
184}
185
186// The window's largest parts, worth showing once context passes the warning threshold.
187async function refreshContextBreakdown($: EngineInterface): Promise<void> {
188 try {
189 const usage = await $.session.usage({ breakdown: 'summary' })
190 const categories = usage.context.breakdown?.categories
191 if (categories) await patch($, h => setCtxTop(h, categories))
192 } catch {
193 // The band shows no breakdown row.
194 }
195}
196
197// Pins the compact line under the prompt; only a changed text is sent.
198async function syncStatus($: EngineInterface): Promise<void> {
199 if (!runtime.config.statusLine) {
200 if (lastStatus !== undefined) $.ui.status(undefined)
201 lastStatus = undefined
202 return
203 }
204 const text = statusLine(await snapshot($))
205 if (text === lastStatus) return
206 lastStatus = text
207 $.ui.status(text)
208}
209
210// Redraws once a second while something runs, for the elapsed clocks, and
211// rechecks the agent roster now and then.
212function syncTicker($: EngineInterface, isBusy: boolean): void {
213 const wantsTicks = isBusy && (runtime.config.hud || runtime.config.spinner)
214 if (wantsTicks && !ticker) {
215 ticker = $.clock.every(1_000, () => {
216 $.ui.invalidate('ui.render')
217 void refreshAgents($, true)
218 })
219 } else if (!wantsTicks && ticker) {
220 ticker.cancel()
221 ticker = undefined
222 }
223}
224
225// ── Spinner frames ────────────────────────────────────────────────────────
226
227function stopFrames(): void {
228 frameTimer?.cancel()
229 frameTimer = undefined
230 spinners.clear()
231}
232
233async function paintFrame($: EngineInterface): Promise<void> {
234 if (isTicking || spinners.size === 0) return
235 isTicking = true
236 try {
237 const t = (await $.clock.now()) - turnStartedAt
238 for (const [requestId, columns] of spinners) {
239 const cells = renderScene(scene, modeOf(spinnerMode), t, columns)
240 const painted = await $.ui.blit({ requestId, key: 'scene', cells, columns, rows: SCENE_ROWS })
241 if ('deny' in painted && painted.deny) spinners.delete(requestId)
242 }
243 } catch {
244 // A missed frame is painted on the next tick.
245 } finally {
246 isTicking = false
247 }
248}
249
250// ── Telegram ──────────────────────────────────────────────────────────────
251
252async function notify($: EngineInterface, kind: NotifyKind, extra: NotifyExtra): Promise<string> {
253 const { config } = runtime
254 if (!config.telegramBotToken || !config.telegramChatId) return 'telegram is not configured'
255 const project = basename(await $.session.root())
256 if (kind !== 'test' && config.notifyBlocklist.includes(project)) return `project ${project} is blocklisted`
257
258 const [usage, h, sessionId, now, time] = await Promise.all([
259 $.session.usage(),
260 read($, hudAtom),
261 $.session.id(),
262 $.clock.now(),
263 localTime($),
264 ])
265 let settingsLanguage: unknown
266 if (config.notifyLanguage === 'auto') {
267 try {
268 settingsLanguage = ((await $.settings.read()) as Record<string, unknown>).language
269 } catch {
270 // Unreadable settings: English.
271 }
272 }
273 const text = buildMessage({
274 kind,
275 language: resolveLanguage(config.notifyLanguage, settingsLanguage),
276 project,
277 branch: h.git?.branch,
278 time,
279 now,
280 durationMs: extra.durationMs,
281 model: h.model,
282 effort: h.effort,
283 costUsd: usage.cost?.usd,
284 ctxPercent: usage.context.percent,
285 rates: usage.rateLimits.map(r => ({ kind: r.kind, percent: r.percentUsed, resetsAt: r.resetsAt })),
286 summary: extra.summary,
287 detail: extra.detail,
288 sessionId,
289 sessionTitle: h.sessionTitle,
290 })
291
292 try {
293 const response = await $.http.fetch(`https://api.telegram.org/bot${config.telegramBotToken}/sendMessage`, {
294 method: 'POST',
295 headers: { 'content-type': 'application/json' },
296 body: JSON.stringify({ chat_id: config.telegramChatId, text, parse_mode: 'HTML', disable_web_page_preview: true }),
297 })
298 $.ui.log(`hudcade: telegram ${kind} http=${response.status} project=${project}`, { to: 'debug' })
299 return `http ${response.status}`
300 } catch (error) {
301 // The error's text may carry the request URL, and with it the token.
302 const name = error instanceof Error ? error.name : 'error'
303 $.ui.log(`hudcade: telegram ${kind} failed (${name}) project=${project}`, { to: 'debug' })
304 return `failed (${name})`
305 }
306}
307
308function notifyLater($: EngineInterface, kind: NotifyKind, extra: NotifyExtra): void {
309 if (!runtime.config.notify) return
310 void notify($, kind, extra).catch(() => undefined)
311}
312
313// Local wall-clock time with the zone name, as the old hook's `date` printed it.
314async function localTime($: EngineInterface): Promise<string> {
315 try {
316 const { exitCode, stdout } = await $.process.run(['date', '+%Y-%m-%d %H:%M:%S %Z'], { timeoutMs: 2_000 })
317 if (exitCode === 0 && stdout.trim()) return stdout.trim()
318 } catch {
319 // Fall back to UTC.
320 }
321 return `${new Date().toISOString().replace('T', ' ').slice(0, 19)} UTC`
322}
323
324
325export const register: Register = (on, options) => {
326 values = options as Record<string, unknown>
327 runtime = createRuntime(values)
328
329 // ── Session ───────────────────────────────────────────────────────────────
330
331 on('session.start', async ($, e, next) => {
332 try {
333 await loadRuntime($)
334 } catch (error) {
335 runtime.warnings.push(`config: ${error instanceof Error ? error.message : String(error)}`)
336 }
337 await $.command.register({
338 name: 'hud',
339 description: 'hudcade status; `/hud test-notify` sends a test Telegram message',
340 argumentHint: '[test-notify]',
341 })
342 // Each fact is read on its own: one that fails must not blank the others,
343 // and its reason shows in /hud.
344 const failed: string[] = []
345 const note = (what: string, error: unknown) =>
346 failed.push(`${what}: ${error instanceof Error ? error.message : String(error)}`)
347 let project = ''
348 let model: string | undefined
349 let effort: string | undefined
350 let turns: number | undefined
351 try {
352 project = basename(await $.session.root())
353 } catch (error) {
354 note('session.root', error)
355 }
356 try {
357 model = await $.session.model()
358 } catch (error) {
359 note('session.model', error)
360 }
361 try {
362 const settings = (await $.settings.read()) as Record<string, unknown>
363 if (typeof settings.effortLevel === 'string') effort = settings.effortLevel
364 } catch (error) {
365 note('settings.read', error)
366 }
367 try {
368 turns = await $.session.turns()
369 } catch (error) {
370 note('session.turns', error)
371 }
372 let tzOffsetMinutes: number | undefined
373 try {
374 const { exitCode, stdout } = await $.process.run(['date', '+%z'], { timeoutMs: 2_000 })
375 if (exitCode === 0) tzOffsetMinutes = parseTzOffset(stdout)
376 } catch (error) {
377 note('date +%z', error)
378 }
379 try {
380 // Session state outlives a reload, so merge: what classic.SessionStart
381 // already noted (a resumed context, a cold cache) stays.
382 await patch($, h => ({
383 ...h,
384 project: project || h.project,
385 model: model ?? h.model,
386 effort: effort ?? h.effort,
387 tzOffsetMinutes: tzOffsetMinutes ?? h.tzOffsetMinutes,
388 turn: { ...h.turn, count: Math.max(h.turn.count, turns ?? 0) },
389 }))
390 } catch (error) {
391 note('state', error)
392 }
393 try {
394 const usage = await $.session.usage()
395 await applyMeasure($, { context: usage.context, rateLimits: usage.rateLimits, cost: usage.cost, startedAt: usage.startedAt })
396 } catch (error) {
397 note('session.usage', error)
398 }
399 await refreshGit($, true)
400 await syncStatus($)
401 runtime.warnings.push(...failed)
402 return next(e)
403 })
404
405 on('session.measure', async ($, e, next) => {
406 await applyMeasure($, { context: e.context, rateLimits: e.rateLimits, cost: e.cost })
407 await syncStatus($)
408 return next(e)
409 })
410
411 on('command.run', { command: 'hud' }, async ($, e) => {
412 if (e.args.trim() === 'test-notify') {
413 return { text: `hudcade test notification: ${await notify($, 'test', {})}` }
414 }
415 return { text: statusText(runtime, await snapshot($)) }
416 })
417
418 // ── Turns ─────────────────────────────────────────────────────────────────
419
420 on('turn.start', async ($, e, next) => {
421 const now = await $.clock.now()
422 const h = await patch($, h => startTurn(h, now))
423 syncTicker($, true)
424
425 turnStartedAt = now
426 toolsAtStart = h.tools.total
427 scene = pickScene(runtime.config.spinnerScene, lastScene, Math.random())
428 lastScene = scene
429 spinnerMode = undefined
430 stopFrames()
431 if (runtime.config.spinner) frameTimer = $.clock.every(FRAME_MS, () => void paintFrame($))
432 return next(e)
433 })
434
435 // Every model request of a turn passes here: write only what changed, so
436 // the drawings are not redrawn for nothing.
437 on('turn.step', async function* ($, e, next) {
438 const effort = e.effort === undefined ? undefined : String(e.effort)
439 const hud = await snapshot($)
440 const isChanged = e.agentId
441 ? hud.agents.some(a => a.agentId === e.agentId && a.model !== e.model)
442 : hud.model !== e.model || (effort !== undefined && hud.effort !== effort)
443 if (isChanged) {
444 await patch($, h =>
445 e.agentId ? setAgentModel(h, e.agentId, e.model) : { ...h, model: e.model, effort: effort ?? h.effort },
446 )
447 if (!e.agentId) await syncStatus($)
448 }
449 const agentId = e.agentId
450 if (agentId && hud.agents.some(a => a.agentId === agentId && a.status === 'running' && a.workingSince === undefined)) {
451 const now = await $.clock.now()
452 await patch($, h => resumeAgent(h, agentId, now))
453 }
454
455 // Read the request's stream through unchanged; its stop chunk carries the
456 // tokens, cache reads and writes included.
457 const stream = next(e)
458 let firstChunkAt: number | undefined
459 for await (const chunk of stream) {
460 if (!e.agentId) {
461 firstChunkAt ??= await $.clock.now()
462 if (chunk.kind === 'stop' && chunk.usage) {
463 const usage = chunk.usage
464 const streamMs = (await $.clock.now()) - firstChunkAt
465 try {
466 await patch($, h => addRequestUsage(h, usage, streamMs))
467 } catch {
468 // A missed reading only skews the ratio and the speed.
469 }
470 }
471 }
472 yield chunk
473 }
474 return await stream.result
475 })
476
477 on('turn.complete', async ($, e, next) => {
478 if (!e.agentId) {
479 const now = await $.clock.now()
480 const h = await patch($, h => completeTurn(h, e.durationMs, now))
481 syncTicker($, h.agents.some(a => a.status === 'running'))
482 stopFrames()
483 void refreshGit($)
484 void refreshAgents($)
485 const percent = h.ctx?.percent
486 if (percent !== undefined && percent >= runtime.config.ctxWarnPercent) void refreshContextBreakdown($)
487 await syncStatus($)
488
489 const isLongEnough = e.durationMs >= runtime.config.notifyMinTurnSeconds * 1_000
490 if (!e.isAborted && isLongEnough) {
491 notifyLater($, e.reason === 'error' ? 'failure' : 'done', {
492 durationMs: e.durationMs,
493 summary: summarize(e.answer),
494 })
495 }
496 } else {
497 const now = await $.clock.now()
498 const agentId = e.agentId
499 await patch($, h => pauseAgent(h, agentId, now))
500 }
501 return next(e)
502 })
503
504 // ── Tools, subagents, tasks ───────────────────────────────────────────────
505
506 on('tool.call', async ($, e, next) => {
507 const now = await $.clock.now()
508 const input = e as unknown as Record<string, unknown>
509 const id = e.tool_use_id ?? `${e.tool}:${now}`
510 await patch($, h => {
511 let state = startTool(h, { id, tool: e.tool, detail: toolDetail(e.tool, input), startedAt: now, agentId: e.agentId })
512 if (e.tool === 'Agent') {
513 state = launchAgent(state, {
514 toolUseId: id,
515 type: typeof input.subagent_type === 'string' ? input.subagent_type : 'general-purpose',
516 description: toolDetail('Agent', input),
517 model: typeof input.model === 'string' ? input.model : undefined,
518 isBackground: input.run_in_background === true,
519 now,
520 })
521 }
522 return state
523 })
524
525 const ran = await next(e)
526
527 try {
528 const end = await $.clock.now()
529 const isError = ran.deny !== undefined || ran.isError === true
530 const result = (ran.result ?? {}) as Record<string, unknown>
531 await patch($, h => {
532 let state = endTool(h, id, isError)
533 if (e.tool === 'Agent') {
534 state = agentResult(
535 state,
536 id,
537 {
538 agentId: typeof result.agentId === 'string' ? result.agentId : undefined,
539 model: typeof result.resolvedModel === 'string' ? result.resolvedModel : undefined,
540 isLaunchOnly: result.status === 'async_launched',
541 isError,
542 },
543 end,
544 )
545 } else if (e.tool === 'TaskCreate' && !isError) {
546 const task = result.task as { id?: unknown } | undefined
547 state = taskCreated(state, String(input.subject ?? ''), typeof task?.id === 'string' ? task.id : undefined)
548 } else if (e.tool === 'TaskUpdate' && !isError && typeof input.taskId === 'string') {
549 state = taskUpdated(state, input.taskId, {
550 status: input.status as 'pending' | 'in_progress' | 'completed' | 'deleted' | undefined,
551 subject: typeof input.subject === 'string' ? input.subject : undefined,
552 })
553 } else if ((e.tool === 'Edit' || e.tool === 'Write') && !isError && typeof input.file_path === 'string') {
554 const counts = patchCounts(ran.result)
555 if (counts) state = addDiff(state, input.file_path, counts)
556 } else if (e.tool === 'TodoWrite' && !isError && Array.isArray(input.todos)) {
557 state = todosWritten(state, input.todos as { content: string; status: 'pending' | 'in_progress' | 'completed' }[])
558 }
559 return state
560 })
561 if (GIT_TOOLS.has(e.tool)) void refreshGit($)
562 } catch {
563 // Bookkeeping never changes what the tool returned.
564 }
565 return ran
566 }).catch(($, e, next) => next(e))
567
568 on('classic.SubagentStart', async ($, e, next) => {
569 const now = await $.clock.now()
570 await patch($, h => linkAgent(h, e.agent_id, e.agent_type, now))
571 syncTicker($, true)
572 void refreshAgents($)
573 return next(e)
574 })
575
576 on('classic.SubagentStop', async ($, e, next) => {
577 const now = await $.clock.now()
578 const h = await patch($, h => (e.agent_id ? finishAgent(h, e.agent_id, now) : h))
579 syncTicker($, h.turn.status !== 'idle' || h.agents.some(a => a.status === 'running'))
580 return next(e)
581 })
582
583 on('classic.SessionStart', async ($, e, next) => {
584 if (e.source === 'resume' || e.source === 'fork') {
585 await patch($, h =>
586 applyResume(h, {
587 contextTokens: e.context_tokens,
588 isCacheCold: e.prompt_cache_likely_expired,
589 rebuildUsd: e.estimated_cache_write_usd,
590 }),
591 )
592 } else if (e.source === 'clear') {
593 await patch($, h => ({ ...h, cache: undefined }))
594 }
595 return next(e)
596 })
597
598 on('classic.Stop', async ($, e, next) => {
599 if (!e.agent_id) {
600 const finished = new Set(['completed', 'failed', 'killed', 'stopped'])
601 const background = (e.background_tasks ?? [])
602 .filter(t => !finished.has(t.status))
603 .map(t => ({ id: t.id, type: t.type, status: t.status, description: t.description || t.command || t.name || t.type }))
604 const crons = (e.session_crons ?? []).map(c => ({ id: c.id, schedule: c.schedule, isRecurring: c.recurring, prompt: c.prompt }))
605 await patch($, h => setWorkers(h, background, crons))
606 void refreshAgents($)
607 }
608 return next(e)
609 })
610
611 on('classic.PostCompact', async ($, e, next) => {
612 await patch($, h => noteCompaction(h))
613 return next(e)
614 })
615
616 on('classic.TeammateIdle', async ($, e, next) => {
617 void refreshAgents($)
618 return next(e)
619 })
620
621 // A switch forfeits a warm prompt cache: say what rebuilding it costs.
622 on('classic.PreModelSwitch', async ($, e, next) => {
623 await patch($, h => setCacheTtl(h, e.cache_ttl))
624 if (runtime.config.hud && e.prompt_cache_warm && e.estimated_cache_write_usd >= 0.01) {
625 const target = modelLabel(e.to_model) ?? e.to_model
626 $.ui.toast(
627 `Switching to ${target} rebuilds the prompt cache: ~${usd(e.estimated_cache_write_usd)} for ${tokens(e.context_tokens)} tokens`,
628 { timeoutMs: 8_000 },
629 )
630 }
631 return next(e)
632 })
633
634 on('classic.PostModelSwitch', async ($, e, next) => {
635 await patch($, h => setCacheTtl(h, e.cache_ttl))
636 return next(e)
637 })
638
639 on('classic.UserPromptSubmit', async ($, e, next) => {
640 if (e.session_title) await patch($, h => ({ ...h, sessionTitle: e.session_title }))
641 return next(e)
642 })
643
644 on('classic.Notification', async ($, e, next) => {
645 if (!e.agent_id && e.notification_type === 'permission_prompt') {
646 await patch($, h => setWaiting(h, true))
647 notifyLater($, 'permission', { detail: summarize(e.message, 200) })
648 }
649 return next(e)
650 })
651
652 on('classic.StopFailure', async ($, e, next) => {
653 if (!e.agent_id) {
654 const detail = [String(e.error), e.error_details].filter(Boolean).join(' — ')
655 notifyLater($, 'failure', { detail: summarize(detail, 200) })
656 }
657 return next(e)
658 })
659
660 // ── Drawing ───────────────────────────────────────────────────────────────
661
662 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
663 if (!runtime.config.hud || e.props.hasSurvey) return next(e)
664 const hud = await snapshot($)
665 const now = await $.clock.now()
666 const rows = buildBand(hud, now, Math.max(20, e.props.bodyColumns), {
667 warnPercent: runtime.config.ctxWarnPercent,
668 criticalPercent: runtime.config.ctxCriticalPercent,
669 }).slice(0, Math.max(1, e.props.maxRows))
670 const { Box, Text } = $.ui.resolve(e)
671 const texts = (segs: Seg[]) =>
672 segs.map(seg => (
673 <Text color={seg.color} dimColor={seg.dim} bold={seg.bold} wrap="truncate">
674 {seg.text}
675 </Text>
676 ))
677 return (
678 <Box flexDirection="column">
679 {rows.map(row => (
680 <Box flexDirection="row" justifyContent="space-between">
681 <Box flexDirection="row">{texts(row.left)}</Box>
682 {row.right.length > 0 && <Box flexDirection="row">{texts(row.right)}</Box>}
683 </Box>
684 ))}
685 </Box>
686 )
687 })
688
689 on('ui.render', { component: 'Spinner' }, async ($, e, next) => {
690 if (!runtime.config.spinner || e.surface !== 'terminal') return next(e)
691
692 spinnerMode = e.props.mode
693 const columns = Math.max(16, Math.min(56, (e.viewport?.columns ?? 100) - SPINNER_TEXT_COLUMNS))
694 spinners.set(e.requestId, columns)
695
696 const hud = await snapshot($)
697 const now = await $.clock.now()
698 const t = turnStartedAt ? now - turnStartedAt : 0
699 const elapsed = clock(hud.turn.startedAt !== undefined ? now - hud.turn.startedAt : t)
700 const main = hud.tools.running.filter(run => !run.agentId)
701 const current = main[main.length - 1]
702 const agents = hud.agents.filter(a => a.status === 'running').length
703 const activity = current
704 ? `▸ ${toolLabel(current.tool)} ${truncate(current.detail, 24)}`
705 : agents > 0
706 ? `▸ ${agents} agent${agents > 1 ? 's' : ''} running`
707 : '▸ …'
708
709 const { Box, Text, Raster } = $.ui.resolve(e)
710 return (
711 <Box flexDirection="row">
712 <Raster key="scene" columns={columns} rows={SCENE_ROWS} cells={renderScene(scene, modeOf(spinnerMode), t, columns)} />
713 <Box flexDirection="column" marginLeft={1}>
714 <Text color={SCENES[scene].accent} bold wrap="truncate">
715 {e.props.message ?? e.props.word}
716 {e.props.suffix}
717 </Text>
718 <Text dimColor wrap="truncate">
719 {elapsed} · {hud.effort ?? 'default'} · {modeLabel(spinnerMode)}
720 </Text>
721 <Text color="suggestion" wrap="truncate">
722 {activity}
723 </Text>
724 <Text dimColor wrap="truncate">
725 {turnFacts(hud, toolsAtStart)}
726 </Text>
727 </Box>
728 </Box>
729 )
730 })
731}
732
733function statusText(runtime: Runtime, hud: Hud): string {
734 const { config } = runtime
735 const onOff = (flag: boolean) => (flag ? 'on' : 'off')
736 const lines = [
737 'hudcade status',
738 `modules: hud ${onOff(config.hud)} · status line ${onOff(config.statusLine)} · spinner ${onOff(config.spinner)} (${config.spinnerScene}) · notify ${onOff(config.notify)}`,
739 `context thresholds: warn ${config.ctxWarnPercent}% · critical ${config.ctxCriticalPercent}%`,
740 `config sources: ${runtime.sources.join(', ')}`,
741 `telegram: ${runtime.telegramSource ? `configured (${runtime.telegramSource})` : 'not configured'} · language ${config.notifyLanguage}`,
742 `hud data: project=${hud.project || '—'} · model=${hud.model ?? '—'} · effort=${hud.effort ?? '—'} · turn=${hud.turn.count} (${hud.turn.status})`,
743 ` ctx=${hud.ctx ? `${hud.ctx.percent ?? '?'}% of ${hud.ctx.window}` : '— (no reading yet)'} · rate limits=${hud.rates.length} · cost=${hud.costUsd !== undefined ? usd(hud.costUsd) : '—'}`,
744 ` git=${hud.git ? hud.git.branch : `— (${gitNote})`} · tools=${hud.tools.total} · agents=${hud.agents.length} · tasks=${hud.tasks.length}`,
745 ]
746 if (runtime.warnings.length > 0) lines.push('warnings:', ...runtime.warnings.map(w => ` - ${w}`))
747 return lines.join('\n')
748}
749src/core/band.ts 405 lines1// Lays the HUD snapshot out as rows of styled segments that fit a width.
2// Pure: the mod maps each row to a left and a right group of <Text>.
3//
4// identity ◆ model · effort project ⎇ branch +added −removed · files cost +turn · ◷ time
5// capacity ctx bar % · left · ⟲compactions cache % │ 5h bar % reset │ 7d bar % reset
6// activity ● status clock · turn n since start / start–end ◑ tool ↑in ↓out · tok/s │ tasks d/n ▸ active
7// (context) ↳ the largest parts of the window, once context passes the warning threshold
8// workers agent / mate / bg / loop rows, while there are any
9
10import type { AgentRun, Hud } from '../../types'
11import {
12 bar,
13 clock,
14 wallClock,
15 compactDuration,
16 modelLabel,
17 msUntil,
18 rateLabel,
19 tokens,
20 toolLabel,
21 truncate,
22 usd,
23} from './format'
24import { agentWorkedMs, cacheHitPercent, withDefaults } from './hud-model'
25
26export type Seg = { text: string; color?: string; dim?: boolean; bold?: boolean }
27export type Row = { left: Seg[]; right: Seg[] }
28
29export type BandOptions = { warnPercent: number; criticalPercent: number }
30
31// Lower numbers are dropped last when a row is too wide.
32type Group = { priority: number; side: 'left' | 'right'; segs: Seg[] }
33
34const LEFT_GAP: Seg = { text: ' ' }
35const RIGHT_SEP: Seg = { text: ' │ ', dim: true }
36const MIN_GAP = 2
37const SPIN = ['◐', '◓', '◑', '◒']
38const MAX_WORKER_ROWS = 4
39
40export function buildBand(raw: Hud, now: number, columns: number, options: BandOptions): Row[] {
41 const h = withDefaults(raw)
42 const rows = [identityRow(h, now), capacityRow(h, now, options), activityRow(h, now)].map(groups => fit(groups, columns))
43 const percent = ctxPercent(h)
44 if (percent !== undefined && percent >= options.warnPercent && h.ctxTop?.length) {
45 rows.push(fit([contextGroup(h)], columns))
46 }
47 for (const row of workerRows(h, now)) rows.push(fit([{ priority: 0, side: 'left', segs: row }], columns))
48 return rows.filter(row => row.left.length + row.right.length > 0)
49}
50
51export function rowText(row: Row): string {
52 return [...row.left, ...row.right].map(s => s.text).join('')
53}
54
55// ── Rows ────────────────────────────────────────────────────────────────────
56
57function identityRow(h: Hud, now: number): Group[] {
58 const groups: Group[] = []
59 const model = modelLabel(h.model) ?? 'Claude'
60 groups.push({
61 priority: 0,
62 side: 'left',
63 segs: [{ text: `◆ ${model}`, color: 'claude', bold: true }, ...(h.effort ? [{ text: ` · ${h.effort}`, dim: true }] : [])],
64 })
65
66 if (h.project) {
67 const place: Seg[] = [{ text: h.project, color: 'suggestion' }]
68 if (h.git) {
69 const marks = `${h.git.isDirty ? '*' : ''}${h.git.ahead ? ` ↑${h.git.ahead}` : ''}${h.git.behind ? ` ↓${h.git.behind}` : ''}`
70 place.push({ text: ` ⎇ ${h.git.branch}${marks}`, color: h.git.isDirty ? 'warning' : 'success' })
71 }
72 groups.push({ priority: 2, side: 'left', segs: place })
73 }
74
75 if (h.diff.files.length > 0) {
76 groups.push({
77 priority: 4,
78 side: 'left',
79 segs: [
80 { text: `+${h.diff.added}`, color: 'success' },
81 { text: ` −${h.diff.removed}`, color: 'error' },
82 { text: ` · ${h.diff.files.length} file${h.diff.files.length > 1 ? 's' : ''}`, dim: true },
83 ],
84 })
85 }
86
87 const money: Seg[] = []
88 if (h.costUsd !== undefined) {
89 money.push({ text: usd(h.costUsd), bold: true })
90 const delta = h.turn.status !== 'idle' && h.turn.costAtStart !== undefined ? h.costUsd - h.turn.costAtStart : 0
91 if (delta >= 0.005) money.push({ text: ` +${usd(delta).slice(1)}`, dim: true })
92 }
93 if (h.sessionStartedAt !== undefined) {
94 money.push({ text: `${money.length ? ' · ' : ''}◷ ${compactDuration(now - h.sessionStartedAt)}`, dim: true })
95 }
96 if (money.length) groups.push({ priority: 1, side: 'right', segs: money })
97 return groups
98}
99
100function capacityRow(h: Hud, now: number, options: BandOptions): Group[] {
101 const groups: Group[] = []
102 const percent = ctxPercent(h)
103 const compacted: Seg[] = h.compactions > 0 ? [{ text: ` · ⟲${h.compactions}`, dim: true }] : []
104 if (percent === undefined) {
105 // No response of this session has reported a fill yet (a new or just resumed session).
106 const window = h.ctx?.window ? ` · ${tokens(h.ctx.window)} window` : ''
107 groups.push({
108 priority: 0,
109 side: 'left',
110 segs: [{ text: `ctx ${bar(0, 12)} —${window} · waiting for first reply`, dim: true }, ...compacted],
111 })
112 } else {
113 const color = percent >= options.criticalPercent ? 'error' : percent >= options.warnPercent ? 'warning' : 'success'
114 const left = h.ctx?.tokens !== undefined && h.ctx.window > 0 ? ` · ${tokens(Math.max(0, h.ctx.window - h.ctx.tokens))} left` : ''
115 groups.push({
116 priority: 0,
117 side: 'left',
118 segs: [
119 { text: 'ctx ', dim: true },
120 { text: bar(percent, 12), color },
121 { text: ` ${Math.round(percent)}%`, color, bold: percent >= options.criticalPercent },
122 { text: left, dim: true },
123 ...compacted,
124 ],
125 })
126 }
127
128 const cache = cacheGroup(h)
129 if (cache) groups.push(cache)
130
131 h.rates.forEach((rate, i) => {
132 const color = rate.percent >= 90 ? 'error' : rate.percent >= 75 ? 'warning' : 'success'
133 const wait = msUntil(rate.resetsAt, now)
134 groups.push({
135 priority: 1 + i,
136 side: 'right',
137 segs: [
138 { text: `${rateLabel(rate.kind)} `, dim: true },
139 { text: bar(rate.percent, 4), color },
140 { text: ` ${Math.round(rate.percent)}%`, color: rate.percent >= 75 ? color : undefined },
141 ...(wait !== undefined ? [{ text: ` ${compactDuration(wait)}`, dim: true }] : []),
142 ],
143 })
144 })
145 return groups
146}
147
148function cacheGroup(h: Hud): Group | undefined {
149 const hit = cacheHitPercent(h.cache)
150 if (h.cache?.isCold) {
151 const cost = h.cache.rebuildUsd ? ` ~${usd(h.cache.rebuildUsd)}` : ''
152 return { priority: 4, side: 'right', segs: [{ text: `cache cold${cost}`, color: 'warning' }] }
153 }
154 if (hit === undefined) return undefined
155 const last = h.cache?.lastHitPercent
156 const isMiss = last !== undefined && last < 50 && (h.cache?.requests ?? 0) > 1
157 return {
158 priority: 4,
159 side: 'right',
160 segs: [
161 { text: 'cache ', dim: true },
162 { text: `${Math.round(hit)}%`, color: hit >= 70 ? 'success' : hit < 40 ? 'warning' : undefined },
163 ...(isMiss ? [{ text: ` last ${Math.round(last)}%`, color: 'warning' }] : []),
164 ],
165 }
166}
167
168function activityRow(h: Hud, now: number): Group[] {
169 const groups: Group[] = []
170 const { turn } = h
171 const at = (ms: number) => wallClock(ms, h.tzOffsetMinutes)
172 if (turn.status === 'idle') {
173 groups.push({ priority: 0, side: 'left', segs: [{ text: `○ idle · turn ${turn.count}`, dim: true }] })
174 if (turn.startedAt !== undefined && turn.endedAt !== undefined) {
175 const took = turn.lastDurationMs !== undefined ? ` (${clock(turn.lastDurationMs)})` : ''
176 groups.push({ priority: 2, side: 'left', segs: [{ text: `${at(turn.startedAt)}–${at(turn.endedAt)}${took}`, dim: true }] })
177 }
178 } else {
179 const elapsed = turn.startedAt !== undefined ? ` ${clock(now - turn.startedAt)}` : ''
180 const status: Seg =
181 turn.status === 'waiting'
182 ? { text: `⚠ waiting for you${elapsed}`, color: 'warning', bold: true }
183 : { text: `● working${elapsed}`, color: 'claude', bold: true }
184 groups.push({ priority: 0, side: 'left', segs: [status, { text: ` · turn ${turn.count}`, dim: true }] })
185 if (turn.startedAt !== undefined) {
186 groups.push({ priority: 2, side: 'left', segs: [{ text: `since ${at(turn.startedAt)}`, dim: true }] })
187 }
188 }
189
190 const main = h.tools.running.filter(t => !t.agentId)
191 const current = main[main.length - 1]
192 if (current && turn.status !== 'idle') {
193 groups.push({
194 priority: 1,
195 side: 'left',
196 segs: [
197 { text: `${spin(now)} ${toolLabel(current.tool)}`, color: 'suggestion' },
198 { text: ` ${clock(now - current.startedAt)}`, dim: true },
199 ...(current.detail ? [{ text: ` ${truncate(current.detail, 40)}` }] : []),
200 ],
201 })
202 }
203
204 const io: Seg[] = []
205 if (h.turnTokens && h.turnTokens.input + h.turnTokens.output > 0) {
206 io.push({ text: `↑${tokens(h.turnTokens.input)} ↓${tokens(h.turnTokens.output)}`, dim: true })
207 }
208 if (h.tokensPerSecond !== undefined) io.push({ text: `${io.length ? ' · ' : ''}${Math.round(h.tokensPerSecond)} tok/s`, dim: true })
209 if (h.tools.total > 0) {
210 io.push({ text: `${io.length ? ' · ' : ''}tools ${h.tools.total}`, dim: true })
211 if (h.tools.failed > 0) io.push({ text: ` ✗${h.tools.failed}`, color: 'error' })
212 }
213 if (io.length) groups.push({ priority: 3, side: 'right', segs: io })
214
215 if (h.tasks.length > 0) {
216 const done = h.tasks.filter(t => t.status === 'completed').length
217 const active = h.tasks.find(t => t.status === 'in_progress')
218 groups.push({
219 priority: 2,
220 side: 'right',
221 segs: [
222 { text: `tasks ${done}/${h.tasks.length}`, color: done === h.tasks.length ? 'success' : undefined },
223 ...(active ? [{ text: ` ▸ ${truncate(active.subject, 28)}`, dim: true }] : []),
224 ],
225 })
226 }
227 return groups
228}
229
230function contextGroup(h: Hud): Group {
231 const total = h.ctx?.tokens ?? h.ctxTop!.reduce((sum, c) => sum + c.tokens, 0)
232 const parts = h.ctxTop!.map(c => `${c.name.toLowerCase()} ${Math.round((c.tokens / Math.max(1, total)) * 100)}%`)
233 return { priority: 0, side: 'left', segs: [{ text: ' ↳ ', dim: true }, { text: parts.join(' · '), dim: true }] }
234}
235
236// ── Workers ─────────────────────────────────────────────────────────────────
237
238function workerRows(h: Hud, now: number): Seg[][] {
239 const rows: Seg[][] = []
240 const agents = [...h.agents].sort((a, b) => rankAgent(a) - rankAgent(b) || a.startedAt - b.startedAt)
241 for (const agent of agents) rows.push(agentRow(agent, now))
242 for (const mate of h.teammates) {
243 const isBusy = mate.status === 'running' || mate.status === 'pending'
244 rows.push([
245 tag('mate'),
246 { text: `◇ ${mate.name}`, color: isBusy ? 'suggestion' : undefined, bold: isBusy },
247 { text: ` · ${mate.status}`, dim: !isBusy },
248 ...(mate.description ? [{ text: ` · ${mate.description}`, dim: true }] : []),
249 ])
250 }
251 for (const task of h.background) {
252 rows.push([
253 tag('bg'),
254 { text: `» ${task.description || task.type}`, color: task.status === 'running' ? 'suggestion' : undefined },
255 { text: ` · ${task.status}`, dim: true },
256 ])
257 }
258 for (const cron of h.crons) {
259 rows.push([
260 tag('loop'),
261 { text: `↻ ${cron.schedule}`, color: 'suggestion' },
262 { text: `${cron.isRecurring ? '' : ' · once'} · ${truncate(cron.prompt.replace(/\s+/g, ' '), 60)}`, dim: true },
263 ])
264 }
265 if (rows.length <= MAX_WORKER_ROWS) return rows
266 return [...rows.slice(0, MAX_WORKER_ROWS - 1), [{ text: ` +${rows.length - MAX_WORKER_ROWS + 1} more`, dim: true }]]
267}
268
269function rankAgent(a: AgentRun): number {
270 return a.status === 'running' ? 0 : 1
271}
272
273function agentRow(a: AgentRun, now: number): Seg[] {
274 const isRunning = a.status === 'running'
275 const glyph = isRunning ? spin(now) : a.status === 'done' ? '✓' : '✗'
276 const color = isRunning ? 'suggestion' : a.status === 'done' ? 'success' : 'error'
277 const model = modelLabel(a.model)
278 return [
279 tag('agent'),
280 { text: `${glyph} `, color },
281 { text: a.type, color, bold: isRunning },
282 ...(model ? [{ text: ` · ${model}`, dim: true }] : []),
283 ...(a.isBackground ? [{ text: ' · bg', dim: true }] : []),
284 { text: ` · ${clock(agentWorkedMs(a, now))} · ${a.tools} tools`, dim: true },
285 ...(a.currentTool ? [{ text: ` ▸ ${toolLabel(a.currentTool)}`, color: 'suggestion' }] : []),
286 ...(a.description ? [{ text: ` · ${a.description}`, dim: !isRunning }] : []),
287 ]
288}
289
290function tag(name: string): Seg {
291 return { text: ` ${name.padEnd(6)}`, dim: true }
292}
293
294function spin(now: number): string {
295 return SPIN[Math.floor(now / 1000) % SPIN.length]!
296}
297
298// ── Fitting ─────────────────────────────────────────────────────────────────
299
300// Drops the lowest-priority groups until left, gap and right fit, then cuts
301// the left side's tail.
302function fit(groups: Group[], columns: number): Row {
303 const kept = [...groups]
304 const widthOf = () => {
305 const left = width(joinSide(kept, 'left'))
306 const right = width(joinSide(kept, 'right'))
307 return left + right + (left > 0 && right > 0 ? MIN_GAP : 0)
308 }
309 while (kept.length > 1 && widthOf() > columns) {
310 let drop = 0
311 for (let i = 1; i < kept.length; i++) if (kept[i]!.priority >= kept[drop]!.priority) drop = i
312 kept.splice(drop, 1)
313 }
314 const right = joinSide(kept, 'right')
315 const rightWidth = width(right)
316 const room = columns - (rightWidth > 0 ? rightWidth + MIN_GAP : 0)
317 return { left: fitSegs(joinSide(kept, 'left'), Math.max(0, room)), right: rightWidth > columns ? fitSegs(right, columns) : right }
318}
319
320function joinSide(groups: Group[], side: 'left' | 'right'): Seg[] {
321 const out: Seg[] = []
322 for (const g of groups.filter(g => g.side === side)) {
323 if (out.length > 0) out.push(side === 'left' ? LEFT_GAP : RIGHT_SEP)
324 out.push(...g.segs)
325 }
326 return out
327}
328
329function fitSegs(segs: Seg[], columns: number): Seg[] {
330 const out: Seg[] = []
331 let used = 0
332 for (const seg of segs) {
333 const w = displayWidth(seg.text)
334 if (used + w <= columns) {
335 out.push(seg)
336 used += w
337 continue
338 }
339 const room = columns - used
340 if (room > 1) out.push({ ...seg, text: cutToWidth(seg.text, room - 1) + '…' })
341 break
342 }
343 return out
344}
345
346function width(segs: Seg[]): number {
347 return segs.reduce((sum, seg) => sum + displayWidth(seg.text), 0)
348}
349
350// Context fill: the engine's percent, else tokens over a known window.
351function ctxPercent(h: Hud): number | undefined {
352 if (!h.ctx) return undefined
353 if (h.ctx.percent !== undefined && h.ctx.percent > 0) return h.ctx.percent
354 if (h.ctx.tokens !== undefined && h.ctx.window > 0) return (h.ctx.tokens / h.ctx.window) * 100
355 return h.ctx.percent
356}
357
358// Terminal columns: East Asian wide characters and most emoji take two.
359export function displayWidth(text: string): number {
360 let w = 0
361 for (const ch of text) w += isWide(ch.codePointAt(0)!) ? 2 : 1
362 return w
363}
364
365export function cutToWidth(text: string, max: number): string {
366 let w = 0
367 let out = ''
368 for (const ch of text) {
369 const cw = isWide(ch.codePointAt(0)!) ? 2 : 1
370 if (w + cw > max) break
371 out += ch
372 w += cw
373 }
374 return out
375}
376
377function isWide(cp: number): boolean {
378 return (
379 (cp >= 0x1100 && cp <= 0x115f) ||
380 (cp >= 0x2e80 && cp <= 0xa4cf) ||
381 (cp >= 0xac00 && cp <= 0xd7a3) ||
382 (cp >= 0xf900 && cp <= 0xfaff) ||
383 (cp >= 0xfe30 && cp <= 0xfe4f) ||
384 (cp >= 0xff00 && cp <= 0xff60) ||
385 (cp >= 0xffe0 && cp <= 0xffe6) ||
386 (cp >= 0x1f300 && cp <= 0x1faff) ||
387 (cp >= 0x20000 && cp <= 0x3fffd)
388 )
389}
390
391// The compact line pinned under the prompt: model · effort │ ctx │ cost │ 5h │ 7d.
392export function statusLine(raw: Hud): string {
393 const h = withDefaults(raw)
394 const model = modelLabel(h.model) ?? 'Claude'
395 const parts = [h.effort ? `${model} · ${h.effort}` : model]
396 const percent = ctxPercent(h)
397 parts.push(percent !== undefined ? `ctx ${Math.round(percent)}%` : 'ctx —')
398 if (h.costUsd !== undefined) parts.push(usd(h.costUsd))
399 for (const [kind, label] of [['five_hour', '5h'], ['seven_day', '7d']] as const) {
400 const rate = h.rates.find(r => r.kind === kind)
401 if (rate) parts.push(`${label} ${Math.round(rate.percent)}%`)
402 }
403 return parts.join(' │ ')
404}
405src/core/config.ts 157 lines1// Effective configuration: the plugin's userConfig options, overridden per
2// project by .claude/claude-mods.json and .claude/claude-mods.local.json.
3
4import type { SceneName } from '../../types'
5import { SCENE_NAMES } from './scenes'
6import type { LanguageChoice } from './telegram'
7
8export type SceneChoice = 'random' | SceneName
9
10export type ModsConfig = {
11 hud: boolean
12 statusLine: boolean
13 spinner: boolean
14 spinnerScene: SceneChoice
15 ctxWarnPercent: number
16 ctxCriticalPercent: number
17 notify: boolean
18 notifyMinTurnSeconds: number
19 notifyBlocklist: string[]
20 notifyLanguage: LanguageChoice
21 telegramBotToken?: string
22 telegramChatId?: string
23}
24
25export const DEFAULTS: ModsConfig = {
26 hud: true,
27 statusLine: true,
28 spinner: true,
29 spinnerScene: 'random',
30 ctxWarnPercent: 60,
31 ctxCriticalPercent: 80,
32 notify: true,
33 notifyMinTurnSeconds: 0,
34 notifyBlocklist: [],
35 notifyLanguage: 'auto',
36}
37
38const SCENE_CHOICES: readonly SceneChoice[] = ['random', ...SCENE_NAMES]
39
40// Secrets stay out of project files: only these keys may be overridden there.
41const PROJECT_KEYS = [
42 'hud',
43 'statusLine',
44 'spinner',
45 'spinnerScene',
46 'ctxWarnPercent',
47 'ctxCriticalPercent',
48 'notify',
49 'notifyMinTurnSeconds',
50 'notifyBlocklist',
51 'notifyLanguage',
52] as const
53
54// What every module reads. Created from userConfig when the module loads and
55// completed at session.start, once project files can be read through `$`.
56export type Runtime = {
57 config: ModsConfig
58 warnings: string[]
59 sources: string[]
60 telegramSource?: string
61}
62
63export type Layer = { source: string; values: Record<string, unknown> }
64
65export type ConfigResult = { config: ModsConfig; warnings: string[] }
66
67export function createRuntime(options: Record<string, unknown>): Runtime {
68 const { config, warnings } = resolveConfig(options, [])
69 return { config, warnings, sources: ['userConfig'] }
70}
71
72export function resolveConfig(options: Record<string, unknown>, projectLayers: Layer[]): ConfigResult {
73 const warnings: string[] = []
74 const config: ModsConfig = { ...DEFAULTS, notifyBlocklist: [...DEFAULTS.notifyBlocklist] }
75
76 apply(config, options, 'userConfig', Object.keys(options), warnings)
77 for (const layer of projectLayers) {
78 apply(config, layer.values, layer.source, PROJECT_KEYS, warnings)
79 }
80 return { config, warnings }
81}
82
83function apply(
84 config: ModsConfig,
85 values: Record<string, unknown>,
86 source: string,
87 allowed: readonly string[],
88 warnings: string[],
89): void {
90 for (const [key, value] of Object.entries(values)) {
91 if (value === undefined || value === null || value === '') continue
92 if (!allowed.includes(key)) {
93 warnings.push(`${source}: unknown or not overridable key "${key}"`)
94 continue
95 }
96 const error = set(config, key, value)
97 if (error) warnings.push(`${source}: ${error}`)
98 }
99}
100
101function set(config: ModsConfig, key: string, value: unknown): string | undefined {
102 switch (key) {
103 case 'hud':
104 case 'statusLine':
105 case 'spinner':
106 case 'notify':
107 if (typeof value !== 'boolean') return `"${key}" must be a boolean`
108 config[key] = value
109 return
110 case 'ctxWarnPercent':
111 case 'ctxCriticalPercent':
112 case 'notifyMinTurnSeconds':
113 if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
114 return `"${key}" must be a non-negative number`
115 }
116 config[key] = value
117 return
118 case 'spinnerScene':
119 if (!SCENE_CHOICES.includes(value as SceneChoice)) return `"spinnerScene" must be one of ${SCENE_CHOICES.join(', ')}`
120 config.spinnerScene = value as SceneChoice
121 return
122 case 'notifyLanguage':
123 if (value !== 'auto' && value !== 'en' && value !== 'zh-TW') return '"notifyLanguage" must be one of auto, en, zh-TW'
124 config.notifyLanguage = value
125 return
126 case 'notifyBlocklist':
127 if (typeof value === 'string') config.notifyBlocklist = value.split(/\s+/).filter(Boolean)
128 else if (Array.isArray(value) && value.every(v => typeof v === 'string')) config.notifyBlocklist = value
129 else return '"notifyBlocklist" must be a space-separated string or a string array'
130 return
131 case 'telegramBotToken':
132 case 'telegramChatId':
133 if (typeof value !== 'string') return `"${key}" must be a string`
134 config[key] = value.trim()
135 return
136 default:
137 return `unknown key "${key}"`
138 }
139}
140
141// Parses KEY="value" lines of a shell env file (the old notify hook's format).
142export function parseEnvFile(text: string): Record<string, string> {
143 const out: Record<string, string> = {}
144 for (const raw of text.split('\n')) {
145 const line = raw.trim()
146 if (!line || line.startsWith('#')) continue
147 const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line)
148 if (!match) continue
149 let value = match[2]!.trim()
150 if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
151 value = value.slice(1, -1)
152 }
153 out[match[1]!] = value
154 }
155 return out
156}
157src/core/format.ts 163 lines1// Small formatters shared by the HUD band, the spinner and Telegram messages.
2
3// 0:42, 12:05, 1:02:03
4export function clock(ms: number): string {
5 const total = Math.max(0, Math.floor(ms / 1000))
6 const h = Math.floor(total / 3600)
7 const m = Math.floor((total % 3600) / 60)
8 const s = total % 60
9 const ss = String(s).padStart(2, '0')
10 return h > 0 ? `${h}:${String(m).padStart(2, '0')}:${ss}` : `${m}:${ss}`
11}
12
13// 14:32:05 local time; the offset comes from the machine's `date +%z`, since
14// the mod's own environment may not know the local zone.
15export function wallClock(epochMs: number, offsetMinutes?: number): string {
16 const pad = (n: number) => String(n).padStart(2, '0')
17 if (offsetMinutes === undefined) {
18 const d = new Date(epochMs)
19 return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
20 }
21 const d = new Date(epochMs + offsetMinutes * 60_000)
22 return `${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}:${pad(d.getUTCSeconds())}`
23}
24
25// "+0800" → 480, "-0330" → -210
26export function parseTzOffset(text: string): number | undefined {
27 const match = /^([+-])(\d{2})(\d{2})$/.exec(text.trim())
28 if (!match) return undefined
29 const minutes = Number(match[2]) * 60 + Number(match[3])
30 return match[1] === '-' ? -minutes : minutes
31}
32
33// 42s, 12m, 1h05m, 2d3h
34export function compactDuration(ms: number): string {
35 const total = Math.max(0, Math.floor(ms / 1000))
36 const d = Math.floor(total / 86400)
37 const h = Math.floor((total % 86400) / 3600)
38 const m = Math.floor((total % 3600) / 60)
39 if (d > 0) return `${d}d${h}h`
40 if (h > 0) return `${h}h${String(m).padStart(2, '0')}m`
41 if (m > 0) return `${m}m`
42 return `${total}s`
43}
44
45// 2m14s, 45s, 1h02m
46export function preciseDuration(ms: number): string {
47 const total = Math.max(0, Math.round(ms / 1000))
48 const h = Math.floor(total / 3600)
49 const m = Math.floor((total % 3600) / 60)
50 const s = total % 60
51 if (h > 0) return `${h}h${String(m).padStart(2, '0')}m`
52 if (m > 0) return `${m}m${String(s).padStart(2, '0')}s`
53 return `${s}s`
54}
55
56// 850, 12k, 913k, 1.2M
57export function tokens(n: number): string {
58 if (n >= 1_000_000) return `${trim(n / 1_000_000)}M`
59 if (n >= 1_000) return `${Math.round(n / 1_000)}k`
60 return String(Math.round(n))
61}
62
63function trim(n: number): string {
64 return n >= 10 ? String(Math.round(n)) : n.toFixed(1).replace(/\.0$/, '')
65}
66
67export function usd(n: number): string {
68 return n >= 100 ? `$${Math.round(n)}` : `$${n.toFixed(2)}`
69}
70
71// ██▌░░░ — one cell per 1/width, with a half cell for the remainder.
72export function bar(percent: number, width: number): string {
73 const clamped = Math.min(100, Math.max(0, percent))
74 const halves = Math.round((clamped / 100) * width * 2)
75 const full = Math.floor(halves / 2)
76 const half = halves % 2
77 return '█'.repeat(full) + (half ? '▌' : '') + '░'.repeat(Math.max(0, width - full - half))
78}
79
80// claude-opus-5-5 → Opus 5.5; claude-haiku-4-5-20251001 → Haiku 4.5
81export function modelLabel(id: string | undefined): string | undefined {
82 if (!id) return undefined
83 const base = id.replace(/\[.*\]$/, '').replace(/-\d{8}$/, '')
84 const match = /^(?:claude-)?([a-z]+)-(\d+)(?:-(\d+))?$/.exec(base)
85 if (!match) return id
86 const family = match[1]!.charAt(0).toUpperCase() + match[1]!.slice(1)
87 return match[3] ? `${family} ${match[2]}.${match[3]}` : `${family} ${match[2]}`
88}
89
90export function rateLabel(kind: string): string {
91 if (kind === 'five_hour') return '5h'
92 if (kind === 'seven_day') return '7d'
93 return kind.replace(/_/g, ' ')
94}
95
96// resetsAt arrives as an ISO time or as Unix seconds in a string.
97export function msUntil(resetsAt: string | undefined, now: number): number | undefined {
98 if (!resetsAt) return undefined
99 const at = /^\d+$/.test(resetsAt) ? Number(resetsAt) * 1000 : Date.parse(resetsAt)
100 return Number.isFinite(at) ? Math.max(0, at - now) : undefined
101}
102
103// Drops terminal control sequences and control characters from text that
104// came from outside (commands, descriptions, branch names).
105export function sanitize(text: string): string {
106 return text
107 .replace(/\u001b\[[0-?]*[ -/]*[@-~]/g, '')
108 .replace(/\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)/g, '')
109 .replace(/[\u0000-\u001f\u007f-\u009f--]/g, ' ')
110 .replace(/\s+/g, ' ')
111 .trim()
112}
113
114export function truncate(text: string, max: number): string {
115 if (max <= 0) return ''
116 return text.length <= max ? text : `${text.slice(0, Math.max(0, max - 1))}…`
117}
118
119export function basename(path: string): string {
120 const parts = path.split('/').filter(Boolean)
121 return parts[parts.length - 1] ?? path
122}
123
124// A short description of a tool call for the HUD: the command, the file, the pattern.
125export function toolDetail(tool: string, input: Record<string, unknown>): string {
126 const str = (key: string) => (typeof input[key] === 'string' ? sanitize(input[key] as string) : '')
127 switch (tool) {
128 case 'Bash':
129 return str('description') || str('command')
130 case 'Read':
131 case 'Edit':
132 case 'Write':
133 case 'NotebookEdit':
134 return basename(str('file_path') || str('notebook_path'))
135 case 'Grep':
136 case 'Glob':
137 return str('pattern')
138 case 'WebFetch':
139 return hostOf(str('url'))
140 case 'WebSearch':
141 return str('query')
142 case 'Agent':
143 return str('description')
144 case 'Skill':
145 return str('skill')
146 case 'TaskCreate':
147 return str('subject')
148 default:
149 return ''
150 }
151}
152
153// mcp__server__tool → server:tool
154export function toolLabel(tool: string): string {
155 const match = /^mcp__(.+?)__(.+)$/.exec(tool)
156 return match ? `${match[1]}:${match[2]}` : tool
157}
158
159function hostOf(url: string): string {
160 const match = /^[a-z]+:\/\/([^/]+)/i.exec(url)
161 return match ? match[1]! : url
162}
163src/core/scenes/index.ts 37 lines1import type { SceneName } from '../../../types'
2import { campfire } from './campfire'
3import { Canvas } from './canvas'
4import { cat } from './cat'
5import { coffee } from './coffee'
6import { equalizer } from './equalizer'
7import { fishing } from './fishing'
8import { garden } from './garden'
9import type { Scene } from './kit'
10import type { Mode } from './mode'
11import { robot } from './robot'
12import { rocket } from './rocket'
13import { train } from './train'
14import { weather } from './weather'
15
16export { modeLabel, modeOf, type Mode, type SpinnerMode } from './mode'
17
18export const SCENES: Record<SceneName, Scene> = { robot, coffee, garden, rocket, cat, fishing, train, campfire, weather, equalizer }
19
20export const SCENE_NAMES = Object.keys(SCENES) as SceneName[]
21
22export const SCENE_ROWS = 4 // terminal rows: 8 pixels tall
23
24// One frame of `name` as Raster cells, `columns` wide and SCENE_ROWS tall.
25export function renderScene(name: SceneName, mode: Mode, t: number, columns: number): string {
26 const canvas = new Canvas(columns, SCENE_ROWS * 2)
27 SCENES[name].draw(canvas, mode, t)
28 return canvas.toCells()
29}
30
31// A fixed choice, or a different scene than last turn's for `random`.
32export function pickScene(choice: string, last: SceneName | undefined, roll: number): SceneName {
33 if ((SCENE_NAMES as readonly string[]).includes(choice)) return choice as SceneName
34 const pool = SCENE_NAMES.filter(name => name !== last)
35 return pool[Math.floor(Math.abs(roll) * pool.length) % pool.length]!
36}
37src/core/spinner.ts 15 lines1// The facts line beside the spinner scene: what this turn has done so far.
2
3import type { Hud } from '../../types'
4import { tokens, usd } from './format'
5
6export function turnFacts(h: Hud, toolsAtStart: number): string {
7 const calls = Math.max(0, h.tools.total - toolsAtStart)
8 const parts = [`${calls} tool${calls === 1 ? '' : 's'}`]
9 if (h.turnTokens && h.turnTokens.output > 0) parts.push(`↓${tokens(h.turnTokens.output)}`)
10 if (h.tokensPerSecond !== undefined) parts.push(`${Math.round(h.tokensPerSecond)} tok/s`)
11 const spent = h.costUsd !== undefined && h.turn.costAtStart !== undefined ? h.costUsd - h.turn.costAtStart : 0
12 if (spent >= 0.005) parts.push(`+${usd(spent)}`)
13 return parts.join(' · ')
14}
15src/core/hud-model.ts 434 lines1// Pure state transitions of the HUD snapshot. Hooks feed events in through
2// `update($, hud, fn)`; nothing here touches `$`, so every rule is testable.
3
4import type { AgentRun, BackgroundTask, CacheStats, Cron, CtxSlice, Hud, TaskItem, Teammate, ToolRun } from '../../types'
5
6const DONE_AGENT_TTL_MS = 60_000
7const AGENT_LIST_GRACE_MS = 10_000
8const MAX_RUNNING_TOOLS = 50
9
10export function emptyHud(project: string): Hud {
11 return {
12 project,
13 rates: [],
14 turn: { count: 0, status: 'idle' },
15 tools: { total: 0, failed: 0, running: [] },
16 agents: [],
17 tasks: [],
18 compactions: 0,
19 diff: { added: 0, removed: 0, files: [] },
20 background: [],
21 crons: [],
22 teammates: [],
23 }
24}
25
26// A state written by an older version of the mod (the session outlives a
27// reload) gets the fields it lacks.
28export function withDefaults(h: Partial<Hud> | undefined): Hud {
29 const base = emptyHud(h?.project ?? '')
30 // An agent row from before work time was kept starts with none, its clock
31 // stopped until the agent's next request.
32 const agents = (h?.agents ?? []).map(a => ({ ...a, workedMs: a.workedMs ?? 0 }))
33 return { ...base, ...h, diff: { ...base.diff, ...h?.diff }, agents } as Hud
34}
35
36export type UsageInput = {
37 context?: { tokens?: number; window: number; percent?: number }
38 rateLimits?: readonly { kind: string; percentUsed: number; resetsAt?: string }[]
39 cost?: { usd: number }
40 startedAt?: number
41}
42
43// A context reading of 0 or none (start of session, mid-compaction) keeps the
44// last good one instead of flashing 0%.
45export function applyUsage(h: Hud, usage: UsageInput): Hud {
46 const next = { ...h }
47 if (usage.context) {
48 const hasReading = (usage.context.percent ?? 0) > 0 || (usage.context.tokens ?? 0) > 0
49 if (hasReading || !h.ctx) next.ctx = { ...usage.context }
50 else next.ctx = { ...h.ctx, window: usage.context.window }
51 }
52 if (usage.rateLimits) {
53 next.rates = usage.rateLimits.map(r => ({ kind: r.kind, percent: r.percentUsed, resetsAt: r.resetsAt }))
54 }
55 if (usage.cost) next.costUsd = usage.cost.usd
56 if (usage.startedAt !== undefined) next.sessionStartedAt = usage.startedAt
57 return next
58}
59
60export type RequestUsage = {
61 input_tokens: number
62 output_tokens?: number
63 cache_read_input_tokens: number
64 cache_creation_input_tokens: number
65}
66
67// One main-thread model request: the session's prompt-cache totals, the
68// turn's tokens, and the request's output speed when its duration is known.
69export function addRequestUsage(h: Hud, usage: RequestUsage, streamMs?: number): Hud {
70 const prev = h.cache ?? { read: 0, written: 0, uncached: 0, requests: 0 }
71 const read = usage.cache_read_input_tokens
72 const total = usage.input_tokens + read + usage.cache_creation_input_tokens
73 const output = usage.output_tokens ?? 0
74 const turn = h.turnTokens ?? { input: 0, output: 0 }
75 const isTimed = streamMs !== undefined && streamMs >= 250 && output > 0
76 return {
77 ...h,
78 cache: {
79 ...prev,
80 read: prev.read + read,
81 written: prev.written + usage.cache_creation_input_tokens,
82 uncached: prev.uncached + usage.input_tokens,
83 requests: prev.requests + 1,
84 lastHitPercent: total > 0 ? (read / total) * 100 : prev.lastHitPercent,
85 isCold: false,
86 },
87 turnTokens: { input: turn.input + total, output: turn.output + output },
88 tokensPerSecond: isTimed ? output / (streamMs / 1000) : h.tokensPerSecond,
89 }
90}
91
92export function noteCompaction(h: Hud): Hud {
93 return { ...h, compactions: h.compactions + 1, ctxTop: undefined }
94}
95
96// Lines a successful Edit or Write changed, from its structured patch.
97export function patchCounts(result: unknown): { added: number; removed: number } | undefined {
98 if (!result || typeof result !== 'object') return undefined
99 const r = result as { structuredPatch?: unknown; type?: unknown; content?: unknown }
100 let added = 0
101 let removed = 0
102 if (Array.isArray(r.structuredPatch)) {
103 for (const hunk of r.structuredPatch as { lines?: unknown }[]) {
104 if (!Array.isArray(hunk.lines)) continue
105 for (const line of hunk.lines) {
106 if (typeof line !== 'string') continue
107 if (line.startsWith('+')) added += 1
108 else if (line.startsWith('-')) removed += 1
109 }
110 }
111 }
112 // A new file's patch can be empty: count its content.
113 if (added === 0 && removed === 0 && r.type === 'create' && typeof r.content === 'string') {
114 added = r.content.length === 0 ? 0 : r.content.replace(/\n$/, '').split('\n').length
115 }
116 return { added, removed }
117}
118
119export function addDiff(h: Hud, file: string, counts: { added: number; removed: number }): Hud {
120 const files = h.diff.files.includes(file) ? h.diff.files : [...h.diff.files, file]
121 return { ...h, diff: { added: h.diff.added + counts.added, removed: h.diff.removed + counts.removed, files } }
122}
123
124// What a Stop reports: the session's background tasks and scheduled prompts.
125export function setWorkers(h: Hud, background: readonly BackgroundTask[], crons: readonly Cron[]): Hud {
126 return { ...h, background: [...background], crons: [...crons] }
127}
128
129export function setTeammates(h: Hud, agents: readonly { id: string; teammateId?: string; name?: string; status: string; description: string }[]): Hud {
130 const teammates: Teammate[] = agents
131 .filter(a => a.teammateId)
132 .map(a => ({ id: a.id, name: a.name ?? a.teammateId!.split('@')[0]!, status: a.status, description: a.description }))
133 return { ...h, teammates }
134}
135
136// The largest parts of the window, from /context's breakdown (used rows only).
137export function setCtxTop(h: Hud, categories: readonly { name: string; tokens: number; kind: string }[]): Hud {
138 const ctxTop: CtxSlice[] = categories
139 .filter(c => c.kind === 'used' && c.tokens > 0)
140 .sort((a, b) => b.tokens - a.tokens)
141 .slice(0, 4)
142 .map(c => ({ name: c.name, tokens: c.tokens }))
143 return { ...h, ctxTop }
144}
145
146export function setCacheTtl(h: Hud, ttl: '5m' | '1h'): Hud {
147 return { ...h, cache: { ...(h.cache ?? { read: 0, written: 0, uncached: 0, requests: 0 }), ttl } }
148}
149
150// Share of the session's input tokens served from the prompt cache.
151export function cacheHitPercent(cache: CacheStats | undefined): number | undefined {
152 if (!cache) return undefined
153 const total = cache.read + cache.written + cache.uncached
154 return total > 0 ? (cache.read / total) * 100 : undefined
155}
156
157// What SessionStart says of a resumed transcript: its context size, and
158// whether the prompt cache has outlived its TTL since the last response.
159export function applyResume(h: Hud, facts: { contextTokens?: number; isCacheCold?: boolean; rebuildUsd?: number }): Hud {
160 const hasReading = (h.ctx?.percent ?? 0) > 0 || (h.ctx?.tokens ?? 0) > 0
161 const ctx =
162 !hasReading && facts.contextTokens !== undefined
163 ? { window: h.ctx?.window ?? 0, tokens: facts.contextTokens }
164 : h.ctx
165 const cache = facts.isCacheCold
166 ? { ...(h.cache ?? { read: 0, written: 0, uncached: 0, requests: 0 }), isCold: true, rebuildUsd: facts.rebuildUsd }
167 : h.cache
168 return { ...h, ctx, cache }
169}
170
171export function startTurn(h: Hud, now: number): Hud {
172 return {
173 ...h,
174 turn: {
175 ...h.turn,
176 count: h.turn.count + 1,
177 status: 'working',
178 startedAt: now,
179 endedAt: undefined,
180 costAtStart: h.costUsd,
181 },
182 turnTokens: { input: 0, output: 0 },
183 agents: pruneAgents(h.agents, now),
184 }
185}
186
187export function completeTurn(h: Hud, durationMs: number, now: number): Hud {
188 return {
189 ...h,
190 turn: { ...h.turn, status: 'idle', startedAt: h.turn.startedAt ?? now - durationMs, endedAt: now, lastDurationMs: durationMs },
191 // A main-loop tool still listed when the turn ends was interrupted.
192 tools: { ...h.tools, running: h.tools.running.filter(t => t.agentId && isAgentRunning(h, t.agentId)) },
193 agents: pruneAgents(h.agents, now),
194 }
195}
196
197export function setWaiting(h: Hud, isWaiting: boolean): Hud {
198 if (h.turn.status === 'idle') return h
199 return { ...h, turn: { ...h.turn, status: isWaiting ? 'waiting' : 'working' } }
200}
201
202export function startTool(h: Hud, run: ToolRun): Hud {
203 let agents = h.agents
204 if (run.agentId) {
205 agents = ensureAgent(agents, run.agentId, run.startedAt).map(a =>
206 a.agentId === run.agentId ? { ...a, tools: a.tools + 1, currentTool: run.tool } : a,
207 )
208 }
209 const running = [...h.tools.running, run].slice(-MAX_RUNNING_TOOLS)
210 return {
211 ...setWaiting(h, false),
212 tools: { ...h.tools, total: h.tools.total + 1, running },
213 agents,
214 }
215}
216
217export function endTool(h: Hud, id: string, isError: boolean): Hud {
218 const run = h.tools.running.find(t => t.id === id)
219 const running = h.tools.running.filter(t => t.id !== id)
220 const agents = run?.agentId
221 ? h.agents.map(a => {
222 if (a.agentId !== run.agentId) return a
223 const still = running.filter(t => t.agentId === a.agentId)
224 return { ...a, currentTool: still[still.length - 1]?.tool }
225 })
226 : h.agents
227 return {
228 ...h,
229 tools: { ...h.tools, failed: h.tools.failed + (isError ? 1 : 0), running },
230 agents,
231 }
232}
233
234export type AgentLaunch = {
235 toolUseId: string
236 type: string
237 description: string
238 model?: string
239 isBackground: boolean
240 now: number
241}
242
243export function launchAgent(h: Hud, launch: AgentLaunch): Hud {
244 const run: AgentRun = {
245 key: `tu:${launch.toolUseId}`,
246 toolUseId: launch.toolUseId,
247 type: launch.type,
248 description: launch.description,
249 model: launch.model,
250 isBackground: launch.isBackground,
251 startedAt: launch.now,
252 workedMs: 0,
253 workingSince: launch.now,
254 tools: 0,
255 status: 'running',
256 }
257 return { ...h, agents: [...pruneAgents(h.agents, launch.now), run] }
258}
259
260// SubagentStart names the loop's id: attach it to the oldest launched agent of
261// that type still without one, or list the agent on its own.
262export function linkAgent(h: Hud, agentId: string, agentType: string, now: number): Hud {
263 if (h.agents.some(a => a.agentId === agentId)) return h
264 const index = h.agents.findIndex(a => !a.agentId && a.status === 'running' && a.type === agentType)
265 const fallback = index >= 0 ? index : h.agents.findIndex(a => !a.agentId && a.status === 'running')
266 if (fallback < 0) return { ...h, agents: ensureAgent(h.agents, agentId, now, agentType) }
267 return { ...h, agents: h.agents.map((a, i) => (i === fallback ? { ...a, agentId } : a)) }
268}
269
270// The Agent tool's result: a foreground agent is finished; a background one
271// only reports its id.
272export function agentResult(
273 h: Hud,
274 toolUseId: string,
275 result: { agentId?: string; model?: string; isLaunchOnly: boolean; isError: boolean },
276 now: number,
277): Hud {
278 return {
279 ...h,
280 agents: h.agents.map(a => {
281 if (a.toolUseId !== toolUseId) return a
282 const linked = { ...a, agentId: a.agentId ?? result.agentId, model: result.model ?? a.model }
283 if (result.isLaunchOnly && !result.isError) return linked
284 return { ...stopWork(linked, now), status: result.isError ? 'failed' : 'done', endedAt: now, currentTool: undefined }
285 }),
286 }
287}
288
289export function finishAgent(h: Hud, agentId: string, now: number): Hud {
290 return {
291 ...h,
292 agents: h.agents.map(a =>
293 a.agentId === agentId && a.status === 'running' ? { ...stopWork(a, now), status: 'done', endedAt: now, currentTool: undefined } : a,
294 ),
295 tools: { ...h.tools, running: h.tools.running.filter(t => t.agentId !== agentId) },
296 }
297}
298
299// An agent's clock counts the time it works, not how long its row is listed:
300// it runs from the launch to the end of the run (`turn.complete`), and again
301// from the agent's next request (`turn.step`) when it takes on more work.
302export function agentWorkedMs(a: AgentRun, now: number): number {
303 return a.workedMs + (a.workingSince === undefined ? 0 : Math.max(0, now - a.workingSince))
304}
305
306export function pauseAgent(h: Hud, agentId: string, now: number): Hud {
307 return { ...h, agents: h.agents.map(a => (a.agentId === agentId ? stopWork(a, now) : a)) }
308}
309
310// A subagent's run raises no turn.start: its first request restarts the clock.
311export function resumeAgent(h: Hud, agentId: string, now: number): Hud {
312 return {
313 ...h,
314 agents: h.agents.map(a =>
315 a.agentId === agentId && a.status === 'running' && a.workingSince === undefined ? { ...a, workingSince: now } : a,
316 ),
317 }
318}
319
320function stopWork(a: AgentRun, now: number): AgentRun {
321 return a.workingSince === undefined ? a : { ...a, workedMs: agentWorkedMs(a, now), workingSince: undefined }
322}
323
324// `$.agent.list()` as the backstop for a SubagentStop that never arrived: a
325// running row the roster calls ended is finished. So is one it no longer
326// lists, once past the launch grace, but only a row an Agent call launched:
327// a workflow's agents are never listed.
328export function reconcileAgents(h: Hud, live: readonly { id: string; status: string }[], now: number): Hud {
329 const statuses = new Map(live.map(a => [a.id, a.status]))
330 const ended = new Set<string>()
331 const agents = h.agents.map((a): AgentRun => {
332 if (a.status !== 'running' || !a.agentId) return a
333 const status = statuses.get(a.agentId)
334 const isDropped = status === undefined && a.toolUseId !== undefined && now - a.startedAt >= AGENT_LIST_GRACE_MS
335 const end = status === 'completed' || isDropped ? 'done' : status === 'failed' || status === 'killed' ? 'failed' : undefined
336 if (!end) return a
337 ended.add(a.agentId)
338 return { ...stopWork(a, now), status: end, endedAt: now, currentTool: undefined }
339 })
340 if (ended.size === 0) return h
341 return {
342 ...h,
343 agents,
344 tools: { ...h.tools, running: h.tools.running.filter(t => !t.agentId || !ended.has(t.agentId)) },
345 }
346}
347
348export function setAgentModel(h: Hud, agentId: string, model: string): Hud {
349 if (!h.agents.some(a => a.agentId === agentId && a.model !== model)) return h
350 return { ...h, agents: h.agents.map(a => (a.agentId === agentId ? { ...a, model } : a)) }
351}
352
353export function pruneAgents(agents: AgentRun[], now: number): AgentRun[] {
354 return agents.filter(a => a.status === 'running' || now - (a.endedAt ?? now) < DONE_AGENT_TTL_MS)
355}
356
357function isAgentRunning(h: Hud, agentId: string): boolean {
358 return h.agents.some(a => a.agentId === agentId && a.status === 'running')
359}
360
361function ensureAgent(agents: AgentRun[], agentId: string, now: number, type = 'agent'): AgentRun[] {
362 if (agents.some(a => a.agentId === agentId)) return agents
363 return [
364 ...agents,
365 {
366 key: `ag:${agentId}`,
367 agentId,
368 type,
369 description: '',
370 isBackground: false,
371 startedAt: now,
372 workedMs: 0,
373 workingSince: now,
374 tools: 0,
375 status: 'running',
376 },
377 ]
378}
379
380// TaskCreate adds one; its result carries the id.
381export function taskCreated(h: Hud, subject: string, id: string | undefined): Hud {
382 return { ...h, tasks: [...h.tasks, { id, subject, status: 'pending' }] }
383}
384
385// TaskUpdate names the task by id; tasks created before the id was known are
386// matched by their 1-based position, as Claude Code numbers them.
387export function taskUpdated(
388 h: Hud,
389 taskId: string,
390 change: { status?: 'pending' | 'in_progress' | 'completed' | 'deleted'; subject?: string },
391): Hud {
392 let index = h.tasks.findIndex(t => t.id === taskId)
393 if (index < 0 && /^\d+$/.test(taskId)) {
394 const position = Number(taskId) - 1
395 if (position >= 0 && position < h.tasks.length && !h.tasks[position]!.id) index = position
396 }
397 if (index < 0) return h
398 const status = change.status
399 if (status === 'deleted') return { ...h, tasks: h.tasks.filter((_, i) => i !== index) }
400 const tasks = h.tasks.map((t, i): TaskItem =>
401 i === index ? { ...t, id: taskId, subject: change.subject ?? t.subject, status: status ?? t.status } : t,
402 )
403 return { ...h, tasks }
404}
405
406// TodoWrite replaces the whole list.
407export function todosWritten(h: Hud, todos: readonly { content: string; status: TaskItem['status'] }[]): Hud {
408 return { ...h, tasks: todos.map(t => ({ subject: t.content, status: t.status })) }
409}
410
411// porcelain v2 `# branch.*` headers and change lines.
412export function parseGitStatus(stdout: string): { branch: string; isDirty: boolean; ahead: number; behind: number } | undefined {
413 const records = stdout.split('\0').filter(Boolean)
414 let branch: string | undefined
415 let oid: string | undefined
416 let ahead = 0
417 let behind = 0
418 let isDirty = false
419 for (const record of records) {
420 if (record.startsWith('# branch.head ')) branch = record.slice('# branch.head '.length)
421 else if (record.startsWith('# branch.oid ')) oid = record.slice('# branch.oid '.length)
422 else if (record.startsWith('# branch.ab ')) {
423 const match = /\+(\d+) -(\d+)/.exec(record)
424 if (match) {
425 ahead = Number(match[1])
426 behind = Number(match[2])
427 }
428 } else if (!record.startsWith('#')) isDirty = true
429 }
430 if (!branch) return undefined
431 if (branch === '(detached)') branch = oid && oid !== '(initial)' ? oid.slice(0, 7) : 'detached'
432 return { branch, isDirty, ahead, behind }
433}
434src/core/telegram.ts 141 lines1// Builds the Telegram message (HTML parse mode), in English or Traditional
2// Chinese: project, branch, time, duration, model, effort, cost, limits,
3// context, a summary of the reply and the session id.
4
5import { compactDuration, modelLabel, msUntil, preciseDuration, usd } from './format'
6
7export type NotifyKind = 'done' | 'permission' | 'failure' | 'test'
8
9export type NotifyLanguage = 'en' | 'zh-TW'
10
11export type LanguageChoice = 'auto' | NotifyLanguage
12
13export type NotifyFacts = {
14 kind: NotifyKind
15 language: NotifyLanguage
16 project: string
17 branch?: string
18 time: string
19 now: number
20 durationMs?: number
21 model?: string
22 effort?: string
23 costUsd?: number
24 ctxPercent?: number
25 rates: readonly { kind: string; percent: number; resetsAt?: string }[]
26 summary?: string
27 detail?: string
28 sessionId?: string
29 sessionTitle?: string
30}
31
32type Strings = {
33 titles: Record<NotifyKind, string>
34 project: string
35 topic: string
36 time: string
37 took: string
38 error: string
39 details: string
40 summary: string
41 session: string
42}
43
44const STRINGS: Record<NotifyLanguage, Strings> = {
45 en: {
46 titles: {
47 done: '✅ Claude Code is done',
48 permission: '⚠️ Claude Code needs your approval',
49 failure: '❌ Claude Code turn failed',
50 test: '🧪 hudcade test notification',
51 },
52 project: 'Project: ',
53 topic: 'Topic: ',
54 time: 'Time: ',
55 took: 'Took',
56 error: 'Error: ',
57 details: 'Details: ',
58 summary: 'Summary: ',
59 session: 'Session: ',
60 },
61 'zh-TW': {
62 titles: {
63 done: '✅ Claude Code 完成回覆',
64 permission: '⚠️ Claude Code 需要你的確認',
65 failure: '❌ Claude Code 回覆失敗',
66 test: '🧪 hudcade 測試通知',
67 },
68 project: '專案:',
69 topic: '主題:',
70 time: '時間:',
71 took: '耗時',
72 error: '錯誤:',
73 details: '內容:',
74 summary: '摘要:',
75 session: 'Session:',
76 },
77}
78
79// `auto` follows Claude Code's own `language` setting: Chinese there means
80// Traditional Chinese messages, anything else English.
81export function resolveLanguage(choice: LanguageChoice, settingsLanguage: unknown): NotifyLanguage {
82 if (choice !== 'auto') return choice
83 return typeof settingsLanguage === 'string' && /中文|chinese|^zh/i.test(settingsLanguage.trim()) ? 'zh-TW' : 'en'
84}
85
86export function buildMessage(f: NotifyFacts): string {
87 const s = STRINGS[f.language]
88 const lines = [`<b>${s.titles[f.kind]}</b>`]
89 lines.push(`${s.project}<code>${esc(f.project)}</code>${f.branch ? ` (${esc(f.branch)})` : ''}`)
90 if (f.sessionTitle) lines.push(`${s.topic}${esc(f.sessionTitle)}`)
91 lines.push(`${s.time}${esc(f.time)}`)
92
93 const run = [
94 f.durationMs !== undefined ? `${s.took} ${preciseDuration(f.durationMs)}` : undefined,
95 modelLabel(f.model),
96 f.effort,
97 f.costUsd !== undefined ? usd(f.costUsd) : undefined,
98 ].filter(Boolean)
99 if (run.length > 0) lines.push(esc(run.join(' · ')))
100
101 const stats = statsLine(f)
102 if (stats) lines.push(esc(stats))
103
104 if (f.detail) lines.push(`${f.kind === 'failure' ? s.error : s.details}${esc(f.detail)}`)
105 if (f.summary) lines.push(`${s.summary}${esc(f.summary)}`)
106 if (f.sessionId) lines.push(`${s.session}<code>${esc(f.sessionId.slice(0, 8))}</code>`)
107 return lines.join('\n')
108}
109
110// 5h:13%(2h15m) wk:38%(5d18h) ctx:9%(opus) — the old hook's stats line.
111function statsLine(f: NotifyFacts): string {
112 const parts: string[] = []
113 for (const rate of f.rates) {
114 const label = rate.kind === 'five_hour' ? '5h' : rate.kind === 'seven_day' ? 'wk' : rate.kind
115 const wait = msUntil(rate.resetsAt, f.now)
116 parts.push(`${label}:${Math.round(rate.percent)}%${wait !== undefined ? `(${compactDuration(wait)})` : ''}`)
117 }
118 if (f.ctxPercent !== undefined) {
119 const family = /opus|sonnet|haiku|fable/i.exec(f.model ?? '')?.[0]?.toLowerCase()
120 parts.push(`ctx:${Math.round(f.ctxPercent)}%${family ? `(${family})` : ''}`)
121 }
122 return parts.join(' ')
123}
124
125// The reply's gist: no code blocks or markdown marks, one line, ~max chars.
126export function summarize(text: string, max = 120): string {
127 const plain = text
128 .replace(/```[\s\S]*?```/g, ' ')
129 .replace(/[*_`#>]/g, '')
130 .replace(/\s+/g, ' ')
131 .trim()
132 if (plain.length <= max) return plain
133 const cut = plain.slice(0, max)
134 const space = cut.lastIndexOf(' ')
135 return `${space > max - 24 ? cut.slice(0, space) : cut}…`
136}
137
138export function esc(text: string): string {
139 return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
140}
141src/core/scenes/campfire.ts 46 lines1// Campfire.
2// A campfire under the stars. Thinking: small flames, a few sparks. A tool:
3// a log goes in and the fire flares. Replying: a marshmallow toasts from
4// white to brown.
5
6import type { Canvas } from './canvas'
7import { C, hash, wrap, type Scene } from './kit'
8import type { Mode } from './mode'
9
10function draw(c: Canvas, mode: Mode, t: number) {
11 for (let k = 0; k < 10; k++) if (Math.floor(t / 400 + k * 3) % 5 !== 0) c.set(Math.floor(hash(k) * c.width), Math.floor(hash(k + 9) * 3), C.white)
12 c.sprite(['.YY', 'Y..', 'Y..', '.YY'], 42, 0, { Y: C.cream })
13 c.sprite(['...T...', '..TTT..', '.TTdTT.', 'TTTddTT'], 2, 4, { T: C.orange, d: C.dbrown })
14 const fx = 20
15 c.hline(fx - 3, fx + 3, 7, C.brown)
16 c.set(fx - 2, 6, C.dbrown)
17 c.set(fx + 2, 6, C.dbrown)
18 const big = mode === 'tool' ? 1 + Math.max(0, 2 - wrap(t / 400, 4)) : 0
19 for (let dx = -2; dx <= 2; dx++) {
20 const h = Math.max(1, Math.round(3 + big + hash(Math.floor(t / 90) * 7 + dx) * 2 - Math.abs(dx)))
21 for (let i = 0; i < h; i++) {
22 const y = 6 - i
23 c.set(fx + dx, y, i >= h - 1 ? C.yellow : i >= h - 2 ? C.orange : C.red)
24 }
25 }
26 const sparks = mode === 'tool' ? 6 : 2
27 for (let s = 0; s < sparks; s++) {
28 const age = wrap(t / 120 + s * 2.3, 6)
29 c.set(fx + Math.round(Math.sin(s * 5 + t / 300) * 2), 3 - age, age < 3 ? C.yellow : C.orange)
30 }
31 if (mode === 'tool') {
32 const p = wrap(t / 1600, 1)
33 if (p < 0.3) {
34 const x = 36 - p * 50
35 c.hline(x, x + 2, 4 - Math.sin((p / 0.3) * Math.PI) * 3, C.brown)
36 }
37 }
38 if (mode === 'reply') {
39 for (let x = 25; x < 32; x++) c.set(x, 3 + Math.floor((x - 25) / 4), C.tan)
40 const toast = Math.floor(t / 700) % 3
41 c.sprite(['mm', 'mm'], 23, 3, { m: [C.white, C.tan, C.brown][toast]! })
42 }
43}
44
45export const campfire: Scene = { name: 'campfire', accent: '#ffd23f', draw }
46src/core/scenes/canvas.ts 109 lines1// A tiny pixel canvas packed into Raster cells: each terminal cell shows two
2// vertical pixels with the half-block characters, foreground on top.
3
4export const CLEAR = 0x01000000 // the terminal's default color (transparent)
5
6const SPACE = 0x20
7const UPPER = 0x2580 // ▀
8const LOWER = 0x2584 // ▄
9const FULL = 0x2588 // █
10
11export type Palette = Record<string, number>
12
13export class Canvas {
14 readonly px: Uint32Array
15
16 constructor(
17 readonly width: number,
18 readonly height: number,
19 ) {
20 this.px = new Uint32Array(width * height).fill(CLEAR)
21 }
22
23 set(x: number, y: number, color: number): void {
24 const xi = Math.round(x)
25 const yi = Math.round(y)
26 if (xi < 0 || yi < 0 || xi >= this.width || yi >= this.height) return
27 this.px[yi * this.width + xi] = color
28 }
29
30 get(x: number, y: number): number {
31 if (x < 0 || y < 0 || x >= this.width || y >= this.height) return CLEAR
32 return this.px[y * this.width + x]!
33 }
34
35 // Draws a sprite given as rows of palette keys; '.' is transparent.
36 sprite(rows: readonly string[], x: number, y: number, palette: Palette): void {
37 const left = Math.round(x)
38 const top = Math.round(y)
39 rows.forEach((row, dy) => {
40 for (let dx = 0; dx < row.length; dx++) {
41 const key = row[dx]!
42 if (key === '.') continue
43 const color = palette[key]
44 if (color !== undefined) this.set(left + dx, top + dy, color)
45 }
46 })
47 }
48
49 hline(x0: number, x1: number, y: number, color: number): void {
50 for (let x = x0; x <= x1; x++) this.set(x, y, color)
51 }
52
53 // Raster cells: `width` columns by `height / 2` rows of [codePoint, fg, bg].
54 toCells(): string {
55 const rows = Math.ceil(this.height / 2)
56 const words = new Uint32Array(this.width * rows * 3)
57 let i = 0
58 for (let r = 0; r < rows; r++) {
59 for (let x = 0; x < this.width; x++) {
60 const top = this.get(x, r * 2)
61 const bottom = this.get(x, r * 2 + 1)
62 let code = SPACE
63 let fg = CLEAR
64 let bg = CLEAR
65 if (top !== CLEAR && bottom !== CLEAR) {
66 if (top === bottom) {
67 code = FULL
68 fg = top
69 } else {
70 code = UPPER
71 fg = top
72 bg = bottom
73 }
74 } else if (top !== CLEAR) {
75 code = UPPER
76 fg = top
77 } else if (bottom !== CLEAR) {
78 code = LOWER
79 fg = bottom
80 }
81 words[i++] = code
82 words[i++] = fg
83 words[i++] = bg
84 }
85 }
86 return base64(words)
87 }
88}
89
90const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
91
92// Little-endian u32 words as standard padded base64 (RasterProps.cells).
93export function base64(words: Uint32Array): string {
94 const bytes = new Uint8Array(words.length * 4)
95 const view = new DataView(bytes.buffer)
96 words.forEach((word, i) => view.setUint32(i * 4, word, true))
97 let out = ''
98 for (let i = 0; i < bytes.length; i += 3) {
99 const a = bytes[i]!
100 const b = i + 1 < bytes.length ? bytes[i + 1]! : 0
101 const c = i + 2 < bytes.length ? bytes[i + 2]! : 0
102 const n = (a << 16) | (b << 8) | c
103 out += ALPHABET[(n >> 18) & 63]! + ALPHABET[(n >> 12) & 63]!
104 out += i + 1 < bytes.length ? ALPHABET[(n >> 6) & 63]! : '='
105 out += i + 2 < bytes.length ? ALPHABET[n & 63]! : '='
106 }
107 return out
108}
109src/core/scenes/cat.ts 37 lines1// Cat and yarn.
2// A cat and a ball of yarn. Thinking: the cat sits, tail swishing. A tool:
3// it pounces and the yarn unrolls. Replying: it chases the yarn at full
4// speed.
5
6import type { Canvas } from './canvas'
7import { C, wrap, type Scene } from './kit'
8import type { Mode } from './mode'
9
10const CAT_SIT = ['k.k....', 'kkk....', 'kek....', 'kkkk...', '.kkkk..', '.kkkkT.', '.k.k..T']
11const CAT_RUN = [
12 ['......k.k', '......kkk', 'T.....kek', '.Tkkkkkk.', '..kkkkkk.', '..k....k.'],
13 ['......k.k', '......kkk', '......kek', 'TTkkkkkk.', '..kkkkkk.', '...k..k..'],
14]
15function draw(c: Canvas, mode: Mode, t: number) {
16 c.hline(0, c.width - 1, 7, C.tan)
17 if (mode === 'think') {
18 const tail = Math.floor(t / 300) % 2
19 const sit = CAT_SIT.map((row, i) => (i >= 5 && tail ? row.replace(/T/g, '.') + (i === 5 ? '' : '') : row))
20 c.sprite(sit, 8, 0, { k: C.orange, e: C.dgreen, T: C.orange })
21 if (tail) c.set(14, 4, C.orange)
22 const wobble = Math.round(Math.sin(t / 250))
23 c.sprite(['.pp.', 'pPpp', 'ppPp', '.pp.'], 18 + wobble, 3, { p: C.pink, P: 0xd9467c })
24 return
25 }
26 const speed = mode === 'reply' ? 30 : 14
27 const lap = c.width + 20
28 const yarnX = wrap((t / 1000) * speed, lap) - 4
29 const catX = yarnX - (mode === 'reply' ? 13 : 16)
30 for (let x = Math.max(0, catX + 9); x < yarnX; x++) c.set(x, 6, C.pink)
31 const spin = Math.floor(t / 100) % 2
32 c.sprite(spin ? ['.pp.', 'pPpp', 'ppPp', '.pp.'] : ['.pp.', 'ppPp', 'pPpp', '.pp.'], yarnX, 3, { p: C.pink, P: 0xd9467c })
33 c.sprite(CAT_RUN[Math.floor(t / (mode === 'reply' ? 70 : 120)) % 2]!, catX, 1, { k: C.orange, e: C.dgreen, T: C.orange })
34}
35
36export const cat: Scene = { name: 'cat', accent: '#ff77a8', draw }
37src/core/scenes/coffee.ts 40 lines1// Coffee conveyor.
2// Mugs ride a conveyor under a pour-over. Thinking: slow drips. A tool: a
3// kettle pours and the drip speeds up. Replying: the belt speeds up and
4// every mug steams.
5
6import type { Canvas } from './canvas'
7import { C, wrap, type Scene } from './kit'
8import type { Mode } from './mode'
9
10const MUG = ['WKKKW.', 'WKKKWh', 'WWWWWh', '.WWW..']
11function draw(c: Canvas, mode: Mode, t: number) {
12 const speed = mode === 'reply' ? 14 : mode === 'tool' ? 6 : 4
13 const dripX = 20
14 c.sprite(['DDDDDDD', '.DDDDD.', '..DDD..'], dripX - 3, 0, { D: C.tan })
15 if (mode === 'tool') {
16 c.sprite(['.KKKK', 'KKKKK', '.KKK.'], dripX - 11, 0, { K: C.steel })
17 for (let x = dripX - 6; x <= dripX - 4; x++) c.set(x, 0, C.cyan)
18 }
19 const dripEvery = mode === 'think' ? 260 : 120
20 c.set(dripX, 3 + ((t % dripEvery) / dripEvery > 0.5 ? 1 : 0), C.dbrown)
21 const spacing = 11
22 const offset = (t / 1000) * speed
23 for (let k = -1; k < c.width / spacing + 1; k++) {
24 const x = Math.round(k * spacing + wrap(offset, spacing)) - 2
25 const center = x + 2
26 const isFull = center > dripX + 1
27 const isFilling = Math.abs(center - dripX) <= 1
28 const inside = isFull ? C.dbrown : isFilling ? C.brown : 0x1f2937
29 c.sprite(MUG, x, 3, { W: C.white, h: C.white, K: inside })
30 if (isFull && (mode !== 'think' || k % 2 === 0)) {
31 const s = Math.floor(t / 160 + k) % 3
32 c.set(x + 1 + s, 2 - (s % 2), 0xcbd5e1)
33 if (mode === 'reply') c.set(x + 3 - s, 1 + (s % 2), 0xcbd5e1)
34 }
35 }
36 for (let x = 0; x < c.width; x++) c.set(x, 7, wrap(x - Math.floor(offset), 3) === 0 ? C.dgray : C.gray)
37}
38
39export const coffee: Scene = { name: 'coffee', accent: '#d8a46a', draw }
40