Context window fill and the session's effort as a footer label, and a toast when the fill passes 70%

hooks/register.tsx 112 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4const WARN_AT = 70
5const YELLOW_FROM = 30
6const RED_FROM = 60
7// Terminals have no named orange, so xhigh's is the 256-color palette's 208.
8const EFFORT_COLORS: Record<string, string> = { medium: 'green', high: 'yellow', xhigh: '#ff8700' }
9
10const percent = atom({ plugin: 'context-gauge', key: 'percent' } as const, null)
11const effort = atom({ plugin: 'context-gauge', key: 'effort' } as const, null)
12
13// Stores the fill for the footer label and toasts on crossing WARN_AT; returns
14// whether the context is past it, so the caller toasts once per crossing.
15async function refresh($: EngineInterface, hasWarned: boolean) {
16 const usage = await $.session.usage()
17 const now = usage.context.percent ?? 0
18
19 await update($, percent, () => now)
20
21 if (now >= WARN_AT && !hasWarned) {
22 $.ui.toast(`Context at ${now}% — consider /handover or /compact`)
23 }
24
25 return now >= WARN_AT
26}
27
28export const register: Register = on => {
29 let hasWarned = false
30
31 on('session.start', async ($, e, next) => {
32 const result = await next(e)
33 hasWarned = await refresh($, hasWarned)
34
35 return result
36 })
37
38 on('tool.call', async ($, e, next) => {
39 const result = await next(e)
40 hasWarned = await refresh($, hasWarned)
41
42 return result
43 })
44
45 on('turn.complete', async ($, e, next) => {
46 const result = await next(e)
47 hasWarned = await refresh($, hasWarned)
48
49 return result
50 })
51
52 on('session.compact', async ($, e, next) => {
53 const result = await next(e)
54 hasWarned = await refresh($, hasWarned)
55
56 return result
57 })
58
59 // No `$` call reads the session's effort; each main-loop model request carries
60 // it, so the label shows it from the first request and follows `/effort`.
61 on('turn.step', async function* ($, e, next) {
62 if (e.agentId === undefined) {
63 await update($, effort, () => e.effort ?? null)
64 }
65
66 return yield* next(e)
67 })
68
69 // Tails the hint line: the footer's right side holds the effort indicator,
70 // and a mode label there wraps to a row of its own. The engine's `tail` is
71 // always dim, so from YELLOW_FROM, or with an effort that has a color, the
72 // hook draws the hint text itself; the engine's pills around it stay its own.
73 on('ui.render', { component: 'PromptHint' }, async ($, e, next) => {
74 const now = await read($, percent)
75 const level = await read($, effort)
76
77 if (now === null) {
78 return next(e)
79 }
80
81 const levelColor = typeof level === 'string' ? EFFORT_COLORS[level] : undefined
82
83 if (now < YELLOW_FROM && levelColor === undefined) {
84 const after = level === null ? '' : ` · ${level}`
85
86 return next({ ...e, props: { ...e.props, tail: `◔ ${now}%${after}` } })
87 }
88
89 const { Text } = $.ui.resolve(e)
90 const gauge =
91 now < YELLOW_FROM ? `◔ ${now}%` : <Text color={now >= RED_FROM ? 'red' : 'yellow'}>{`◔ ${now}%`}</Text>
92
93 if (level === null) {
94 return (
95 <Text dimColor>
96 {`${e.props.hint} · `}
97 {gauge}
98 </Text>
99 )
100 }
101
102 return (
103 <Text dimColor>
104 {`${e.props.hint} · `}
105 {gauge}
106 {' · '}
107 {levelColor === undefined ? `${level}` : <Text color={levelColor}>{`${level}`}</Text>}
108 </Text>
109 )
110 })
111}
112types/index.d.ts 9 lines1export type Percent = number | null
2export type Effort = string | number | null
3
4declare module 'claude-code' {
5 interface PluginState {
6 'context-gauge': { percent: Percent; effort: Effort }
7 }
8}
9