KOZMOS Questline: task progress above the prompt. One segmented bar for the session's todos and tasks (done green, running violet and pulsing, waiting gray)…

Task progress above the prompt, from the main loop's TodoWrite lists and TaskCreate / TaskUpdate tasks.
3/7 · ▶ Running the tests · 1:24: the count, the running task's activeForm, and how long it has run.✓ Quest complete with a shine rolling across the bar).Terminal: a Client bar whose running segment breathes with a highlight running through it and whose clock ticks locally. Desktop: one SVG with rounded segments, a glowing, shimmering active segment and check marks on the done ones.
/questline shows or hides it; ✕ hides it. The choice is kept across sessions.
İstemin üstünde görev ilerlemesi; ana döngünün TodoWrite listelerinden ve TaskCreate / TaskUpdate görevlerinden.
3/7 · ▶ Running the tests · 1:24: sayı, süren görevin activeForm'u ve ne kadar süredir sürdüğü.✓ Quest complete).Terminal: süren bölmesi nefes alan ve içinden bir ışık geçen, saati yerelde işleyen bir Client çubuğu. Masaüstü: yuvarlak bölmeli, aktif bölmesi parlayıp ışıldayan, bitenlerde onay işareti olan tek SVG.
/questline gösterir veya gizler; ✕ gizler. Seçim oturumlar arasında saklanır.
hooks/register.tsx 148 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4import type { Quest, QuestSnap } from '../types'
5import type { BarProps } from './bar.tsx'
6import { coarseSpan, createTask, fromTodos, isShown, progress, updateTask } from './quests.ts'
7import type { Todo } from './quests.ts'
8import { questSvg } from './svg.ts'
9
10const snapAtom = atom({ plugin: 'questline', key: 'snap' } as const, null)
11const hiddenAtom = atom({ plugin: 'questline', key: 'isHidden' } as const, false)
12
13// The quest list: TodoWrite rows and TaskCreate/TaskUpdate tasks of the main
14// loop. Module variables start over on a reload; session.start restores them
15// from the last snapshot, which the session keeps.
16let todos: Quest[] = []
17let tasks: Quest[] = []
18let allDoneAt: number | null = null
19let publishedKey = ''
20
21const all = (): Quest[] => [...tasks, ...todos]
22
23async function publish($: EngineInterface): Promise<void> {
24 const now = await $.clock.now()
25 const items = all()
26 const { done, total, active } = progress(items)
27 if (total > 0 && done === total) allDoneAt ??= now
28 else allDoneAt = null
29 const isVisible = isShown(items, allDoneAt, now)
30 // The desktop caption shows a coarse elapsed time: publish when it changes.
31 const key = JSON.stringify({ items, isVisible, since: active?.since !== undefined ? coarseSpan(now - active.since) : '' })
32 if (key === publishedKey) return
33 publishedKey = key
34 const snap: QuestSnap = { items, done, total, allDoneAt, isVisible, now }
35 await update($, snapAtom, () => snap)
36}
37
38async function restore($: EngineInterface): Promise<void> {
39 const snap = await read($, snapAtom)
40 if (!snap) return
41 tasks = snap.items.filter(q => q.id.startsWith('task:'))
42 todos = snap.items.filter(q => q.id.startsWith('todo:'))
43 allDoneAt = snap.allDoneAt
44}
45
46async function setHidden($: EngineInterface, isHidden: boolean): Promise<void> {
47 await update($, hiddenAtom, () => isHidden)
48 try {
49 await $.store.set('isHidden', isHidden)
50 } catch {
51 // Hidden for this session only.
52 }
53}
54
55async function loadHidden($: EngineInterface): Promise<void> {
56 try {
57 const v = await $.store.get('isHidden')
58 if (typeof v === 'boolean') await update($, hiddenAtom, () => v)
59 } catch {
60 // Nothing stored.
61 }
62}
63
64const str = (v: unknown): string | undefined => (typeof v === 'string' ? v : undefined)
65
66export const register: Register = on => {
67 on('session.start', async ($, e, next) => {
68 const started = await next(e)
69 todos = []
70 tasks = []
71 allDoneAt = null
72 publishedKey = ''
73 await restore($)
74 await $.command.register({ name: 'questline', description: 'KOZMOS: show or hide the Questline task bar above the prompt', immediate: true })
75 await loadHidden($)
76 $.clock.every(1000, () => void publish($).catch(() => undefined))
77 return started
78 })
79
80 on('command.run', { command: 'questline' }, async $ => {
81 const isHidden = !(await read($, hiddenAtom))
82 await setHidden($, isHidden)
83 return { text: isHidden ? 'Questline hidden. /questline brings it back.' : 'Questline shown: it appears while the session has tasks.' }
84 })
85
86 on('tool.call', async ($, e, next) => {
87 const ran = await next(e)
88 if (e.agentId !== undefined || ran.isError === true || ran.deny !== undefined) return ran
89 const now = await $.clock.now()
90 const input = e as unknown as Record<string, unknown>
91 if (e.tool === 'TodoWrite' && Array.isArray(input.todos)) {
92 todos = fromTodos(todos, input.todos as Todo[], now)
93 } else if (e.tool === 'TaskCreate') {
94 const created = (ran.result as { task?: { id?: unknown; subject?: unknown } } | undefined)?.task
95 const id = created?.id
96 if (typeof id === 'string' || typeof id === 'number') tasks = createTask(tasks, String(id), str(created?.subject) ?? str(input.subject) ?? `Task #${id}`, str(input.activeForm))
97 } else if (e.tool === 'TaskUpdate' && (typeof input.taskId === 'string' || typeof input.taskId === 'number')) {
98 tasks = updateTask(tasks, { taskId: String(input.taskId), status: str(input.status), subject: str(input.subject), activeForm: str(input.activeForm) }, now)
99 } else return ran
100 await publish($)
101 return ran
102 }).catch(($, e, next) => next(e))
103
104 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
105 const drawn = await next(e)
106 if (e.props.hasSurvey || (await read($, hiddenAtom))) return drawn
107 const snap = await read($, snapAtom)
108 if (!snap || !snap.isVisible) return drawn
109 const cols = Math.max(20, (e.props.bodyColumns || 80) - 3)
110
111 if (e.surface === 'terminal') {
112 const { Box, Button, Client } = $.ui.resolve(e)
113 const { active } = progress(snap.items)
114 const now = await $.clock.now()
115 const props: BarProps = {
116 marks: snap.items.map(q => (q.status === 'completed' ? 'd' : q.status === 'in_progress' ? 'a' : 'p')).join(''),
117 done: snap.done,
118 total: snap.total,
119 activeText: active?.active ?? '',
120 activeMs: active?.since !== undefined ? Math.max(0, now - active.since) : -1,
121 allDone: snap.total > 0 && snap.done === snap.total,
122 }
123 return (
124 <Box flexDirection="column">
125 {drawn}
126 <Box key="questline" flexDirection="row">
127 <Client key="questline-bar" module="./bar.tsx" width={cols} height={1} props={props} />
128 <Button key="questline-hide" label="✕" plain dimColor role="dismiss" onPress={() => void setHidden($, true)} />
129 </Box>
130 </Box>
131 )
132 }
133
134 // Every surface but the terminal draws an Svg.
135 const { Box, Button, Svg } = $.ui.resolve(e)
136 const pic = questSvg(snap, cols * 8 - 8)
137 return (
138 <Box flexDirection="column">
139 {drawn}
140 <Box key="questline" flexDirection="row">
141 <Svg source={pic.source} alt={pic.alt} width={pic.width} height={pic.height} />
142 <Button key="questline-hide" label="✕" plain dimColor role="dismiss" onPress={() => void setHidden($, true)} />
143 </Box>
144 </Box>
145 )
146 })
147}
148hooks/bar.tsx 103 lines1// Questline's terminal bar: a Client surface module. The running segment
2// breathes and a highlight runs through it; the elapsed clock ticks locally
3// from the last published figure, so the hooks module publishes only changes.
4import type { ClientModule, RenderElement } from 'claude-code'
5
6import { mix } from './lib/kz.ts'
7import { segments } from './quests.ts'
8
9export type BarProps = {
10 /** One letter per quest: d done, a active, p pending. */
11 marks: string
12 done: number
13 total: number
14 activeText: string
15 /** How long the active quest had run when published; -1 for none. */
16 activeMs: number
17 allDone: boolean
18}
19
20type BarState = { t: number; base: number; at: number }
21
22const GREEN = '#4ade80'
23const VIOLET = '#a78bfa'
24const PALE = '#ede9fe'
25const GRAY = '#6b7280'
26
27function clock(ms: number): string {
28 const s = Math.max(0, Math.floor(ms / 1000))
29 const h = Math.floor(s / 3600)
30 const m = Math.floor((s % 3600) / 60)
31 const ss = String(s % 60).padStart(2, '0')
32 return h ? `${h}:${String(m).padStart(2, '0')}:${ss}` : `${m}:${ss}`
33}
34
35let latest: BarProps = { marks: '', done: 0, total: 0, activeText: '', activeMs: -1, allDone: false }
36
37const Bar: ClientModule<BarProps, BarState> = (props, surface) => {
38 if (surface.state === undefined) {
39 surface.setState({ t: 0, base: props.activeMs, at: 0 })
40 surface.every(40, () => {
41 const s = surface.state
42 if (!s) return
43 // A new figure from the hooks restarts the local count from it.
44 const isNew = latest.activeMs !== s.base
45 surface.setState({ t: s.t + 1, base: latest.activeMs, at: isNew ? s.t + 1 : s.at })
46 })
47 }
48 latest = props
49 const st = surface.state ?? { t: 0, base: props.activeMs, at: 0 }
50 const { Box, Text } = surface.elements
51 const cols = surface.columns || 80
52 const t = st.t
53 const budget = Math.max(6, Math.min(48, Math.floor(cols * 0.4)))
54 const { width, gap } = segments(props.marks.length, budget)
55
56 type Run = { s: string; c: string }
57 const runs: Run[] = []
58 const push = (s: string, c: string) => {
59 const last = runs[runs.length - 1]
60 if (last && last.c === c) last.s += s
61 else runs.push({ s, c })
62 }
63 const breathe = 0.5 + 0.5 * Math.sin(t * 0.16)
64 const wave = (t * 0.6) % (props.marks.length * (width + gap) + 24)
65 let x = 0
66 for (const m of props.marks) {
67 for (let i = 0; i < width; i++, x++) {
68 if (m === 'd') {
69 // Once all is done a bright wave rolls across the bar now and then.
70 const glow = props.allDone ? Math.max(0, 1 - Math.abs(x - wave) / 3) : 0
71 push('█', glow > 0 ? mix(GREEN, '#f0fdf4', glow * 0.8) : GREEN)
72 } else if (m === 'a') {
73 const sweep = Math.floor(t / 3) % Math.max(1, width + 2) - 1
74 push('█', i === sweep ? PALE : mix('#7c3aed', VIOLET, breathe))
75 } else push('░', GRAY)
76 }
77 if (gap) {
78 push(' ', GRAY)
79 x++
80 }
81 }
82 const elapsed = props.activeMs >= 0 ? props.activeMs + Math.max(0, t - st.at) * 40 : -1
83 const line: RenderElement[] = runs.map((r, i) => <Text key={`s${i}`} color={r.c}>{r.s}</Text>)
84 if (props.allDone) {
85 line.push(<Text key="done" color={GREEN} bold> ✓ {props.total}/{props.total} quest complete</Text>)
86 } else {
87 line.push(<Text key="n" bold> {props.done}/{props.total}</Text>)
88 if (props.activeText) {
89 line.push(<Text key="sep" dimColor> · </Text>)
90 line.push(<Text key="go" color={mix('#7c3aed', VIOLET, breathe)}>▶ </Text>)
91 line.push(<Text key="what">{props.activeText}</Text>)
92 if (elapsed >= 0) line.push(<Text key="el" dimColor> · {clock(elapsed)}</Text>)
93 } else line.push(<Text key="idle" dimColor> · waiting for the next task</Text>)
94 }
95 return (
96 <Box flexDirection="row">
97 <Text wrap="truncate-end">{line}</Text>
98 </Box>
99 )
100}
101
102export default Bar
103hooks/quests.ts 79 lines1// Questline's bookkeeping: pure functions over the quest list, no `$`.
2import type { Quest, QuestStatus } from '../types'
3
4/** After everything is done the band lingers this long, then hides. */
5export const LINGER_MS = 120_000
6
7export type Todo = { content?: unknown; status?: unknown; activeForm?: unknown }
8
9const statusOf = (v: unknown): QuestStatus => (v === 'completed' || v === 'in_progress' ? v : 'pending')
10const text = (v: unknown, fallback: string): string => (typeof v === 'string' && v.trim() ? v.trim() : fallback)
11
12/** A TodoWrite list replaces the todo quests; a row keeps its start time when it stays in progress. */
13export function fromTodos(prev: readonly Quest[], todos: readonly Todo[], now: number): Quest[] {
14 return todos.map((t, i) => {
15 const title = text(t.content, `Step ${i + 1}`)
16 const status = statusOf(t.status)
17 const before = prev.find(q => q.title === title) ?? prev[i]
18 const since = status === 'in_progress' ? (before?.status === 'in_progress' && before.title === title ? before.since ?? now : now) : undefined
19 return { id: `todo:${i}`, title, active: text(t.activeForm, title), status, ...(since !== undefined ? { since } : {}) }
20 })
21}
22
23export function createTask(list: readonly Quest[], id: string, subject: string, activeForm: string | undefined): Quest[] {
24 const key = `task:${id}`
25 if (list.some(q => q.id === key)) return [...list]
26 return [...list, { id: key, title: subject, active: activeForm?.trim() || subject, status: 'pending' }]
27}
28
29export type TaskPatch = { taskId: string; status?: string; subject?: string; activeForm?: string }
30
31export function updateTask(list: readonly Quest[], patch: TaskPatch, now: number): Quest[] {
32 const key = `task:${patch.taskId}`
33 if (patch.status === 'deleted') return list.filter(q => q.id !== key)
34 const found = list.find(q => q.id === key)
35 const base: Quest = found ?? { id: key, title: patch.subject || `Task #${patch.taskId}`, active: patch.activeForm || patch.subject || `Task #${patch.taskId}`, status: 'pending' }
36 const status = patch.status === undefined ? base.status : statusOf(patch.status)
37 const next: Quest = {
38 id: key,
39 title: patch.subject || base.title,
40 active: patch.activeForm || (patch.subject && base.active === base.title ? patch.subject : base.active),
41 status,
42 }
43 if (status === 'in_progress') next.since = base.status === 'in_progress' ? base.since ?? now : now
44 return found ? list.map(q => (q.id === key ? next : q)) : [...list, next]
45}
46
47export type Progress = { done: number; total: number; active: Quest | undefined }
48
49export function progress(list: readonly Quest[]): Progress {
50 const active = list.find(q => q.status === 'in_progress')
51 return { done: list.filter(q => q.status === 'completed').length, total: list.length, active }
52}
53
54/** Shown while there is a task, until two minutes after everything completed. */
55export function isShown(list: readonly Quest[], allDoneAt: number | null, now: number): boolean {
56 if (!list.length) return false
57 const { done, total } = progress(list)
58 if (done < total) return true
59 return allDoneAt === null || now - allDoneAt < LINGER_MS
60}
61
62/** A coarse elapsed label that changes rarely: <10s, 20s, ..., 4m, 1h12m. */
63export function coarseSpan(ms: number): string {
64 const s = Math.max(0, Math.floor(ms / 1000))
65 if (s < 10) return '<10s'
66 if (s < 60) return `${Math.floor(s / 10) * 10}s`
67 const m = Math.floor(s / 60)
68 if (m < 60) return `${m}m`
69 return `${Math.floor(m / 60)}h${String(m % 60).padStart(2, '0')}m`
70}
71
72/** Segment widths for `n` quests in `budget` cells: equal, at most 6, with a gap when there is room. */
73export function segments(n: number, budget: number): { width: number; gap: number } {
74 if (n <= 0) return { width: 0, gap: 0 }
75 const withGap = Math.floor((budget + 1) / n) - 1
76 if (withGap >= 2) return { width: Math.min(6, withGap), gap: 1 }
77 return { width: Math.max(1, Math.floor(budget / n)), gap: 0 }
78}
79hooks/svg.ts 75 lines1// Questline's desktop drawing: one SVG row of segments and a caption.
2import type { QuestSnap } from '../types'
3import { KZ, fitText, svg, svgText, textWidth } from './lib/kz.ts'
4import { coarseSpan, progress } from './quests.ts'
5
6const CSS = `
7.qa{animation:qa 1.6s ease-in-out infinite}@keyframes qa{50%{opacity:.62}}
8.qs{animation:qs 1.8s cubic-bezier(.4,0,.2,1) infinite}@keyframes qs{from{transform:translateX(-30px)}to{transform:translateX(var(--w))}}
9.qw{animation:qw 3.2s ease-in-out infinite}@keyframes qw{from{transform:translateX(-60px)}60%,to{transform:translateX(var(--w))}}
10`
11
12export function questSvg(s: QuestSnap, width: number): { source: string; width: number; height: number; alt: string } {
13 const W = Math.max(260, width)
14 const H = 30
15 const { done, total, active } = progress(s.items)
16 const allDone = total > 0 && done === total
17 const n = s.items.length
18 const gap = n > 24 ? 2 : 4
19 const area = Math.min(W * 0.46, n * 38)
20 const sw = Math.max(3, (area - gap * (n - 1)) / n)
21 const y = 10
22 const h = 10
23 const p: string[] = []
24 p.push(`<defs><linearGradient id="qsh" x1="0" x2="1"><stop offset="0" stop-color="#fff" stop-opacity="0"/><stop offset=".5" stop-color="#fff" stop-opacity=".75"/><stop offset="1" stop-color="#fff" stop-opacity="0"/></linearGradient>`)
25 p.push(`<filter id="qg" x="-40%" y="-80%" width="180%" height="260%"><feGaussianBlur stdDeviation="2.2"/></filter>`)
26 p.push(`<clipPath id="qall">`)
27 s.items.forEach((_, i) => p.push(`<rect x="${(4 + i * (sw + gap)).toFixed(1)}" y="${y}" width="${sw.toFixed(1)}" height="${h}" rx="${Math.min(5, sw / 2)}"/>`))
28 p.push(`</clipPath></defs>`)
29 s.items.forEach((q, i) => {
30 const x = 4 + i * (sw + gap)
31 const rx = Math.min(5, sw / 2)
32 if (q.status === 'completed') {
33 p.push(`<rect x="${x.toFixed(1)}" y="${y}" width="${sw.toFixed(1)}" height="${h}" rx="${rx}" fill="${KZ.green}"/>`)
34 if (sw >= 16) p.push(`<path d="M${(x + sw / 2 - 3).toFixed(1)} ${y + 5}l2 2 4-4" stroke="#fff" stroke-width="1.6" fill="none" stroke-linecap="round" stroke-linejoin="round"/>`)
35 } else if (q.status === 'in_progress') {
36 p.push(`<rect x="${x.toFixed(1)}" y="${y}" width="${sw.toFixed(1)}" height="${h}" rx="${rx}" fill="${KZ.violet}" filter="url(#qg)" class="qa"/>`)
37 p.push(`<rect x="${x.toFixed(1)}" y="${y}" width="${sw.toFixed(1)}" height="${h}" rx="${rx}" fill="${KZ.violet}" class="qa"/>`)
38 p.push(`<clipPath id="qc${i}"><rect x="${x.toFixed(1)}" y="${y}" width="${sw.toFixed(1)}" height="${h}" rx="${rx}"/></clipPath>`)
39 p.push(`<g clip-path="url(#qc${i})"><rect class="qs" style="--w:${(sw + 30).toFixed(0)}px" x="${x.toFixed(1)}" y="${y}" width="24" height="${h}" fill="url(#qsh)"/></g>`)
40 } else {
41 p.push(`<rect x="${x.toFixed(1)}" y="${y}" width="${sw.toFixed(1)}" height="${h}" rx="${rx}" class="k"/>`)
42 }
43 })
44 const barEnd = 4 + n * (sw + gap) - gap
45 if (allDone) p.push(`<g clip-path="url(#qall)"><rect class="qw" style="--w:${(barEnd + 60).toFixed(0)}px" x="0" y="${y}" width="50" height="${h}" fill="url(#qsh)"/></g>`)
46
47 let tx = barEnd + 12
48 const base = y + h - 1
49 const put = (s: string, o: Parameters<typeof svgText>[3] & { size: number }) => {
50 p.push(svgText(tx, base, s, o))
51 tx += textWidth(s, o.size) + 6
52 }
53 if (allDone) {
54 put('✓ Quest complete', { size: 12, weight: 700, fill: KZ.green })
55 put(`${total}/${total}`, { cls: 's', size: 11.5, weight: 600 })
56 } else {
57 put(`${done}/${total}`, { size: 12.5, weight: 750 })
58 if (active) {
59 p.push(`<path d="M${tx} ${base - 9}l7 4.5-7 4.5z" fill="${KZ.violet}" class="qa"/>`)
60 tx += 12
61 const since = active.since !== undefined ? coarseSpan(s.now - active.since) : ''
62 const room = W - tx - (since ? textWidth(since, 11) + 16 : 0) - 6
63 put(fitText(active.active, 12, room), { size: 12, weight: 550 })
64 if (since) put(`· ${since}`, { cls: 'm', size: 11 })
65 } else put('waiting for the next task', { cls: 'm', size: 11.5 })
66 }
67 return { source: svg(W, H, p.join(''), CSS), width: W, height: H, alt: altOf(s) }
68}
69
70export function altOf(s: QuestSnap): string {
71 const { done, total, active } = progress(s.items)
72 if (total > 0 && done === total) return `Questline: all ${total} tasks done`
73 return `Questline: ${done} of ${total} tasks done${active ? `; now: ${active.active}` : ''}`
74}
75hooks/lib/kz.ts 521 lines1// GENERATED by scripts/sync-shared.mjs from shared/kz.ts. Do not edit here.
2// KOZMOS shared kit. Source of truth: shared/kz.ts at the bundle root.
3// `node scripts/sync-shared.mjs` copies it into every mod as hooks/lib/kz.ts,
4// because a hooks module may only import files inside its own plugin.
5// Edit the root copy, never a mod's copy.
6
7// ---------------------------------------------------------------------------
8// Palette: one neon family across every KOZMOS mod.
9
10export const KZ = {
11 violet: '#a78bfa',
12 magenta: '#f472b6',
13 cyan: '#22d3ee',
14 teal: '#2dd4bf',
15 green: '#4ade80',
16 lime: '#a3e635',
17 yellow: '#facc15',
18 amber: '#fb923c',
19 red: '#f87171',
20 blue: '#60a5fa',
21 clay: '#d97757',
22 ink: '#1f1e1d',
23 mist: '#9ca3af',
24} as const
25
26/** Tool families, colored the same in every mod. */
27export function toolColor(tool: string): string {
28 const t = String(tool)
29 if (t === 'Bash' || t === 'PowerShell') return KZ.green
30 if (t === 'Edit' || t === 'Write' || t === 'NotebookEdit' || t === 'MultiEdit') return KZ.yellow
31 if (t === 'Read' || t === 'Glob' || t === 'Grep' || t === 'LSP') return KZ.blue
32 if (t === 'Agent' || t === 'Task' || t === 'Workflow') return KZ.violet
33 if (t.startsWith('Web')) return KZ.cyan
34 if (t.startsWith('Todo') || t.startsWith('Task')) return KZ.teal
35 if (t.startsWith('mcp__')) return KZ.magenta
36 return KZ.mist
37}
38
39/** A short glyph per tool family. */
40export function toolGlyph(tool: string): string {
41 const t = String(tool)
42 if (t === 'Bash' || t === 'PowerShell') return '$'
43 if (t === 'Edit' || t === 'Write' || t === 'NotebookEdit') return '✎'
44 if (t === 'Read') return '◉'
45 if (t === 'Glob' || t === 'Grep') return '⌕'
46 if (t === 'Agent') return '◈'
47 if (t.startsWith('Web')) return '◍'
48 if (t.startsWith('Todo') || t.startsWith('Task')) return '☑'
49 if (t.startsWith('mcp__')) return '⬡'
50 return '•'
51}
52
53// ---------------------------------------------------------------------------
54// Colors.
55
56export function hexToRgb(hex: string): [number, number, number] {
57 const h = hex.replace('#', '')
58 const n = parseInt(h.length === 3 ? h.split('').map(c => c + c).join('') : h, 16)
59 return [(n >> 16) & 255, (n >> 8) & 255, n & 255]
60}
61
62export function rgbToHex(r: number, g: number, b: number): string {
63 const c = (v: number) => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, '0')
64 return `#${c(r)}${c(g)}${c(b)}`
65}
66
67export function mix(a: string, b: string, t: number): string {
68 const [r1, g1, b1] = hexToRgb(a)
69 const [r2, g2, b2] = hexToRgb(b)
70 const k = clamp01(t)
71 return rgbToHex(r1 + (r2 - r1) * k, g1 + (g2 - g1) * k, b1 + (b2 - b1) * k)
72}
73
74/** Green at 0, yellow at 0.6, red at 1: the heat of a gauge. */
75export function heat(t: number): string {
76 const k = clamp01(t)
77 return k < 0.6 ? mix(KZ.green, KZ.yellow, k / 0.6) : mix(KZ.yellow, KZ.red, (k - 0.6) / 0.4)
78}
79
80/** A smooth rainbow for hues 0..1 (for auroras, plasma and the like). */
81export function hue(h: number, s = 0.75, l = 0.6): string {
82 const k = ((h % 1) + 1) % 1
83 const a = s * Math.min(l, 1 - l)
84 const f = (n: number) => {
85 const x = (n + k * 12) % 12
86 return l - a * Math.max(-1, Math.min(x - 3, 9 - x, 1))
87 }
88 return rgbToHex(f(0) * 255, f(8) * 255, f(4) * 255)
89}
90
91export function hexToInt(hex: string): number {
92 const [r, g, b] = hexToRgb(hex)
93 return (r << 16) | (g << 8) | b
94}
95
96// ---------------------------------------------------------------------------
97// Numbers and text.
98
99export const clamp01 = (v: number): number => (Number.isFinite(v) ? Math.max(0, Math.min(1, v)) : 0)
100
101export function fmtTokens(n: number): string {
102 if (!Number.isFinite(n)) return '—'
103 // Each unit starts where the one below would round up to 1000 of itself.
104 if (n >= 999.5e6) return `${(n / 1e9).toFixed(1)}B`
105 if (n >= 999_500) return `${(n / 1e6).toFixed(n >= 1e7 ? 0 : 1)}M`
106 if (n >= 999.5) return `${(n / 1e3).toFixed(n >= 1e4 ? 0 : 1)}k`
107 return `${Math.round(n)}`
108}
109
110export function fmtUsd(usd: number): string {
111 if (!Number.isFinite(usd)) return '$—'
112 if (usd < 0.01 && usd > 0) return '<$0.01'
113 return usd < 100 ? `$${usd.toFixed(2)}` : `$${Math.round(usd)}`
114}
115
116/** 0:42, 3:07, 1:02:33. */
117export function fmtClock(ms: number): string {
118 const s = Math.max(0, Math.round(ms / 1000))
119 const h = Math.floor(s / 3600)
120 const m = Math.floor((s % 3600) / 60)
121 const ss = String(s % 60).padStart(2, '0')
122 return h ? `${h}:${String(m).padStart(2, '0')}:${ss}` : `${m}:${ss}`
123}
124
125/** 42s, 7m, 2h41m, 3d4h: compact durations for countdowns. */
126export function fmtSpan(ms: number): string {
127 const s = Math.max(0, Math.round(ms / 1000))
128 if (s < 60) return `${s}s`
129 const m = Math.floor(s / 60)
130 if (m < 60) return `${m}m`
131 const h = Math.floor(m / 60)
132 if (h < 48) return `${h}h${String(m % 60).padStart(2, '0')}m`
133 return `${Math.floor(h / 24)}d${h % 24}h`
134}
135
136export function fmtPct(p: number | undefined): string {
137 return p === undefined || !Number.isFinite(p) ? '—' : `${Math.round(p)}%`
138}
139
140// The cuts below count UTF-16 units; they must not land between the two halves of an emoji.
141const isHigh = (u: number): boolean => u >= 0xd800 && u <= 0xdbff
142const isLow = (u: number): boolean => u >= 0xdc00 && u <= 0xdfff
143
144export function clip(s: string, max: number): string {
145 const one = s.replace(/\s+/g, ' ').trim()
146 if (one.length <= max) return one
147 let cut = Math.max(1, max - 1)
148 if (isHigh(one.charCodeAt(cut - 1))) cut--
149 return one.slice(0, cut) + '…'
150}
151
152export function padEnd(s: string, n: number): string {
153 if (s.length < n) return s + ' '.repeat(n - s.length)
154 // A pair cut in half gives way to a space, so the result keeps its width.
155 return n > 0 && isHigh(s.charCodeAt(n - 1)) ? s.slice(0, n - 1) + ' ' : s.slice(0, n)
156}
157
158export function padStart(s: string, n: number): string {
159 if (s.length < n) return ' '.repeat(n - s.length) + s
160 const k = s.length - n
161 return k > 0 && isLow(s.charCodeAt(k)) ? ' ' + s.slice(k + 1) : s.slice(k)
162}
163
164/** `claude-opus-5-5[1m]` → `Opus 5.5`. */
165export function modelName(id: string | undefined): string {
166 if (!id) return '—'
167 const m = /(fable|mythos|opus|sonnet|haiku)-(\d+)(?:-(\d{1,2})(?!\d))?/i.exec(id)
168 if (!m) return id.replace(/^claude-/, '').replace(/\[.*\]$/, '')
169 const fam = m[1]!
170 return `${fam.charAt(0).toUpperCase()}${fam.slice(1).toLowerCase()} ${m[2]}${m[3] ? '.' + m[3] : ''}`
171}
172
173/** Last path segment, either slash. */
174export function baseName(p: string): string {
175 const parts = p.split(/[\\/]/).filter(Boolean)
176 return parts[parts.length - 1] ?? p
177}
178
179/** A stable small hash for seeding. */
180export function hash(s: string): number {
181 let h = 2166136261
182 for (let i = 0; i < s.length; i++) {
183 h ^= s.charCodeAt(i)
184 h = Math.imul(h, 16777619)
185 }
186 return h >>> 0
187}
188
189/** Deterministic 0..1 noise from two ints. */
190export function noise(x: number, y: number): number {
191 const s = Math.sin(x * 12.9898 + y * 78.233) * 43758.5453
192 return s - Math.floor(s)
193}
194
195/** A seeded PRNG (mulberry32). */
196export function rng(seed: number): () => number {
197 let a = seed >>> 0
198 return () => {
199 a = (a + 0x6d2b79f5) >>> 0
200 let t = a
201 t = Math.imul(t ^ (t >>> 15), t | 1)
202 t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
203 return ((t ^ (t >>> 14)) >>> 0) / 4294967296
204 }
205}
206
207// ---------------------------------------------------------------------------
208// Text gauges for the terminal.
209
210const EIGHTHS = ['', '▏', '▎', '▍', '▌', '▋', '▊', '▉']
211
212/** A smooth bar with eighth blocks: `█████▍ `. */
213export function bar(ratio: number, width: number, empty = '░'): string {
214 const w = Math.max(1, Math.floor(width))
215 const exact = clamp01(ratio) * w
216 const full = Math.floor(exact)
217 const part = EIGHTHS[Math.floor((exact - full) * 8)] ?? ''
218 const used = full + (part ? 1 : 0)
219 return '█'.repeat(full) + part + empty.repeat(Math.max(0, w - used))
220}
221
222/** A segmented gauge: `▰▰▰▱▱▱`. */
223export function pips(ratio: number, width: number, on = '▰', off = '▱'): string {
224 const w = Math.max(1, Math.floor(width))
225 const n = Math.round(clamp01(ratio) * w)
226 return on.repeat(n) + off.repeat(w - n)
227}
228
229const SPARK = '▁▂▃▄▅▆▇█'
230
231/** A sparkline of the last `width` values, scaled to their own max (or `max`). */
232export function sparkline(values: readonly number[], width: number, max?: number): string {
233 const tail = values.slice(-Math.max(1, width))
234 const top = max ?? Math.max(1e-9, ...tail)
235 const line = tail.map(v => SPARK[Math.min(7, Math.max(0, Math.round((v / top) * 7)))] ?? '▁').join('')
236 return line.padStart(width, ' ')
237}
238
239/** Braille line graph, 2 samples per cell, `rows` cells tall. */
240export function brailleGraph(values: readonly number[], width: number, rows: number, max?: number): string[] {
241 const samples = values.slice(-(width * 2))
242 const top = max ?? Math.max(1e-9, ...samples)
243 const dotsTall = rows * 4
244 const grid: number[][] = Array.from({ length: rows }, () => Array.from({ length: width }, () => 0))
245 const offset = width * 2 - samples.length
246 // Dot bits per column, from the bottom row of a cell up.
247 const LEFT = [0x40, 0x04, 0x02, 0x01]
248 const RIGHT = [0x80, 0x20, 0x10, 0x08]
249 samples.forEach((v, i) => {
250 const x = offset + i
251 const col = Math.floor(x / 2)
252 const isRight = x % 2 === 1
253 const h = Math.round(clamp01(v / top) * (dotsTall - 1))
254 for (let d = 0; d <= h; d++) {
255 const row = rows - 1 - Math.floor(d / 4)
256 const bits = isRight ? RIGHT : LEFT
257 const cellRow = grid[row]
258 if (cellRow && col >= 0) cellRow[col] = (cellRow[col] ?? 0) | bits[d % 4]!
259 }
260 })
261 return grid.map(r => r.map(b => String.fromCharCode(0x2800 + b)).join(''))
262}
263
264// ---------------------------------------------------------------------------
265// Raster: a grid of colored cells, packed as the engine wants it.
266
267const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
268
269export function toBase64(bytes: Uint8Array): string {
270 let out = ''
271 let i = 0
272 for (; i + 2 < bytes.length; i += 3) {
273 const n = (bytes[i]! << 16) | (bytes[i + 1]! << 8) | bytes[i + 2]!
274 out += B64[(n >> 18) & 63]! + B64[(n >> 12) & 63]! + B64[(n >> 6) & 63]! + B64[n & 63]!
275 }
276 const rest = bytes.length - i
277 if (rest === 1) {
278 const n = bytes[i]! << 16
279 out += B64[(n >> 18) & 63]! + B64[(n >> 12) & 63]! + '=='
280 } else if (rest === 2) {
281 const n = (bytes[i]! << 16) | (bytes[i + 1]! << 8)
282 out += B64[(n >> 18) & 63]! + B64[(n >> 12) & 63]! + B64[(n >> 6) & 63]! + '='
283 }
284 return out
285}
286
287/** The terminal's own default color, for a cell's foreground or background. */
288export const DEFAULT_COLOR = 0x01000000
289
290export class Canvas {
291 readonly cols: number
292 readonly rows: number
293 private readonly words: Uint32Array
294
295 constructor(cols: number, rows: number) {
296 this.cols = Math.max(1, Math.min(512, Math.floor(cols)))
297 this.rows = Math.max(1, Math.min(256, Math.floor(rows)))
298 this.words = new Uint32Array(this.cols * this.rows * 3)
299 this.clear()
300 }
301
302 clear(bg: number = DEFAULT_COLOR): void {
303 for (let i = 0; i < this.cols * this.rows; i++) {
304 this.words[i * 3] = 0x20
305 this.words[i * 3 + 1] = DEFAULT_COLOR
306 this.words[i * 3 + 2] = bg
307 }
308 }
309
310 /** Puts one width-1 character; colors are '#rrggbb' or a packed int. */
311 set(x: number, y: number, ch: string, fg?: string | number, bg?: string | number): void {
312 const cx = Math.floor(x)
313 const cy = Math.floor(y)
314 if (cx < 0 || cy < 0 || cx >= this.cols || cy >= this.rows) return
315 const i = (cy * this.cols + cx) * 3
316 const code = ch.codePointAt(0) ?? 0x20
317 this.words[i] = code > 0xffff || code < 0x20 ? 0x20 : code
318 if (fg !== undefined) this.words[i + 1] = typeof fg === 'number' ? fg : hexToInt(fg)
319 if (bg !== undefined) this.words[i + 2] = typeof bg === 'number' ? bg : hexToInt(bg)
320 }
321
322 /** Writes a string left to right, clipped to the canvas. */
323 text(x: number, y: number, s: string, fg?: string | number, bg?: string | number): void {
324 let cx = x
325 for (const ch of s) {
326 this.set(cx, y, ch, fg, bg)
327 cx++
328 }
329 }
330
331 /** Paints a cell's background only, keeping its character. */
332 paint(x: number, y: number, bg: string | number): void {
333 const cx = Math.floor(x)
334 const cy = Math.floor(y)
335 if (cx < 0 || cy < 0 || cx >= this.cols || cy >= this.rows) return
336 this.words[(cy * this.cols + cx) * 3 + 2] = typeof bg === 'number' ? bg : hexToInt(bg)
337 }
338
339 /** Two vertical pixels per cell with the upper-half block: `py` is in half-cells. */
340 pixel(x: number, py: number, color: string): void {
341 const cx = Math.floor(x)
342 const cy = Math.floor(py / 2)
343 if (cx < 0 || cy < 0 || cx >= this.cols || cy >= this.rows) return
344 const i = (cy * this.cols + cx) * 3
345 const c = hexToInt(color)
346 const isTop = Math.floor(py) % 2 === 0
347 if (this.words[i] !== 0x2580) {
348 this.words[i] = 0x2580
349 this.words[i + 1] = DEFAULT_COLOR
350 this.words[i + 2] = DEFAULT_COLOR
351 }
352 if (isTop) this.words[i + 1] = c
353 else this.words[i + 2] = c
354 }
355
356 /** The `cells` prop of a Raster. */
357 encode(): string {
358 return toBase64(new Uint8Array(this.words.buffer))
359 }
360}
361
362// ---------------------------------------------------------------------------
363// SVG for the desktop: one drawing per row (the desktop wraps siblings).
364
365export const FONT = "-apple-system,BlinkMacSystemFont,'Segoe UI',Inter,sans-serif"
366export const MONO = "ui-monospace,'Cascadia Code','SF Mono',Consolas,monospace"
367
368const XML_ESC: Record<string, string> = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }
369
370export function xml(s: string): string {
371 return s.replace(/[&<>"']/g, c => XML_ESC[c]!)
372}
373
374/** Theme-aware classes every KOZMOS drawing shares: t text, s secondary, m muted, k track, p panel. */
375export const SVG_BASE_CSS = `
376.t{fill:#1f1f1f}.s{fill:#5f5f5c}.m{fill:#8e8e8a}.k{fill:#e7e5e0}.p{fill:#f5f4f1}.ln{stroke:#e1dfda}
377@media (prefers-color-scheme: dark){.t{fill:#ededed}.s{fill:#b4b4b0}.m{fill:#7c7c78}.k{fill:#2d2d2b}.p{fill:#232322}.ln{stroke:#363634}}
378.pulse{animation:kzp 1.6s ease-in-out infinite}@keyframes kzp{50%{opacity:.35}}
379.spin{transform-box:fill-box;transform-origin:center;animation:kzs 2s linear infinite}@keyframes kzs{to{transform:rotate(360deg)}}
380@media (prefers-reduced-motion: reduce){*{animation:none!important}}
381`
382
383export function svg(width: number, height: number, body: string, css = ''): string {
384 return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}"><style>${SVG_BASE_CSS}${css}</style>${body}</svg>`
385}
386
387/** Rough width of UI text in px, for fitting labels. */
388export function textWidth(s: string, size: number): number {
389 let w = 0
390 for (const ch of s) w += (/[\s.,:;'|!il1()[\]]/.test(ch) ? 0.3 : /[A-Z@%MWmw]/.test(ch) ? 0.72 : 0.56) * size
391 return w
392}
393
394export function fitText(s: string, size: number, maxW: number): string {
395 if (textWidth(s, size) <= maxW) return s
396 let out = ''
397 for (const ch of s) {
398 if (textWidth(out + ch + '…', size) > maxW) break
399 out += ch
400 }
401 return out + '…'
402}
403
404/** A rounded progress bar as SVG markup. */
405export function svgBar(x: number, y: number, w: number, h: number, ratio: number, color: string): string {
406 const fw = Math.max(0, Math.min(w, w * clamp01(ratio)))
407 return `<rect class="k" x="${x}" y="${y}" width="${w}" height="${h}" rx="${h / 2}"/>` +
408 (fw > 0 ? `<rect x="${x}" y="${y}" width="${Math.max(h, fw)}" height="${h}" rx="${h / 2}" fill="${color}"/>` : '')
409}
410
411/** SVG text helper. */
412export function svgText(x: number, y: number, s: string, opts: { cls?: string; size?: number; weight?: number; anchor?: 'start' | 'middle' | 'end'; fill?: string; mono?: boolean } = {}): string {
413 const { cls = 't', size = 12, weight = 400, anchor = 'start', fill, mono = false } = opts
414 return `<text ${fill ? `fill="${fill}"` : `class="${cls}"`} x="${x}" y="${y}" font-family="${mono ? MONO : FONT}" font-size="${size}" font-weight="${weight}" text-anchor="${anchor}" font-variant-numeric="tabular-nums">${xml(s)}</text>`
415}
416
417/** Pixel width a desktop pane or band gives a drawing for `columns` reported columns. */
418export function pxOf(columns: number | undefined, fallback = 60, slack = 8): number {
419 return Math.max(200, Math.min(1600, (columns || fallback) * 8 - slack))
420}
421
422// ---------------------------------------------------------------------------
423// Prices, USD per million tokens: input, output, cache read, cache write (5 min).
424// Anthropic first-party rates as of 2026-10. Used only where the engine reports
425// tokens and not money (a subagent's own spend); the session total comes from
426// $.session.usage().cost, which is the engine's.
427
428const PRICES: [RegExp, [number, number, number, number]][] = [
429 [/fable|mythos/i, [10, 50, 0.25, 12.5]],
430 [/opus-5-5/i, [4, 20, 0.2, 5]],
431 [/opus/i, [5, 25, 0.5, 6.25]],
432 [/sonnet-5/i, [2, 10, 0.2, 2.5]],
433 [/sonnet/i, [3, 15, 0.3, 3.75]],
434 [/haiku-5/i, [0.1, 0.5, 0.01, 0.125]],
435 [/haiku/i, [1, 5, 0.1, 1.25]],
436]
437
438export type Usage = {
439 input_tokens?: number
440 output_tokens?: number
441 cache_read_input_tokens?: number
442 cache_creation_input_tokens?: number
443}
444
445export function priceOf(model: string): [number, number, number, number] {
446 return PRICES.find(([re]) => re.test(model))?.[1] ?? [4, 20, 0.2, 5]
447}
448
449export function costOf(model: string, u: Usage | null | undefined): number {
450 if (!u) return 0
451 const [i, o, r, w] = priceOf(model)
452 return ((u.input_tokens ?? 0) * i + (u.output_tokens ?? 0) * o + (u.cache_read_input_tokens ?? 0) * r + (u.cache_creation_input_tokens ?? 0) * w) / 1e6
453}
454
455export function tokensOf(u: Usage | null | undefined): number {
456 if (!u) return 0
457 return (u.input_tokens ?? 0) + (u.output_tokens ?? 0) + (u.cache_read_input_tokens ?? 0) + (u.cache_creation_input_tokens ?? 0)
458}
459
460/** The context a request filled: everything it read plus what it wrote. */
461export function contextOf(u: Usage | null | undefined): number {
462 return tokensOf(u)
463}
464
465export function windowOf(model: string): number {
466 return /haiku-4/i.test(model) ? 200_000 : 1_000_000
467}
468
469// ---------------------------------------------------------------------------
470// Rate limits.
471
472export type Limit = { kind: string; percentUsed: number; resetsAt?: string }
473
474export function limitLabel(kind: string): string {
475 if (kind === 'five_hour') return '5h'
476 if (kind === 'seven_day') return '7d'
477 if (kind === 'spend_limit') return '$'
478 return kind.replace(/_/g, ' ')
479}
480
481/** Milliseconds until a window resets, or undefined. */
482export function untilReset(l: Limit, now: number): number | undefined {
483 if (!l.resetsAt) return undefined
484 const t = Date.parse(l.resetsAt)
485 return Number.isFinite(t) ? Math.max(0, t - now) : undefined
486}
487
488/** The length of a window in ms, for burn-rate math. */
489export function windowMs(kind: string): number | undefined {
490 if (kind === 'five_hour') return 5 * 3600_000
491 if (kind === 'seven_day') return 7 * 24 * 3600_000
492 return undefined
493}
494
495// ---------------------------------------------------------------------------
496// Tool calls, described in a few words.
497
498/** What a tool call is about: the command, the file, the pattern, the agent's task. */
499export function toolDetail(tool: string, input: unknown): string {
500 const e = (input ?? {}) as Record<string, unknown>
501 const s = (k: string) => (typeof e[k] === 'string' ? (e[k] as string) : '')
502 const t = String(tool)
503 if (t === 'Bash' || t === 'PowerShell') return clip(s('description') || s('command'), 60)
504 if (s('file_path')) return baseName(s('file_path'))
505 if (s('notebook_path')) return baseName(s('notebook_path'))
506 if (t === 'Agent') return clip(s('description') || s('subagent_type'), 60)
507 if (s('pattern')) return clip(s('pattern'), 60)
508 if (s('url')) return clip(s('url').replace(/^https?:\/\//, ''), 60)
509 if (s('query')) return clip(s('query'), 60)
510 if (s('subject')) return clip(s('subject'), 60)
511 if (Array.isArray(e.todos)) return `${(e.todos as unknown[]).length} todos`
512 if (t.startsWith('mcp__')) return t.split('__').slice(1).join(' · ')
513 return ''
514}
515
516/** `mcp__server__tool` → `server·tool`; built-ins unchanged. */
517export function toolName(tool: string): string {
518 const t = String(tool)
519 return t.startsWith('mcp__') ? t.split('__').slice(1).join('·') : t
520}
521types/index.d.ts 30 lines1export type QuestStatus = 'pending' | 'in_progress' | 'completed'
2
3/** One task, from TodoWrite or TaskCreate/TaskUpdate. */
4export type Quest = {
5 /** `todo:<n>` for a TodoWrite row, `task:<id>` for a task. */
6 id: string
7 title: string
8 /** The present-continuous form shown while it runs ("Running the tests"). */
9 active: string
10 status: QuestStatus
11 /** When it went in progress (ms), while it is. */
12 since?: number
13}
14
15export type QuestSnap = {
16 items: Quest[]
17 done: number
18 total: number
19 /** When the last task completed with nothing left, else null. */
20 allDoneAt: number | null
21 isVisible: boolean
22 now: number
23}
24
25declare module 'claude-code' {
26 interface PluginState {
27 questline: { snap: QuestSnap | null; isHidden: boolean }
28 }
29}
30