i want to add a small graph to the chatbox that shows the current context of a chat. the…
- User
i want to add a small graph to the chatbox that shows the current context of a chat. the total length of the bar is the 100% of the context window for the model i'm using in the chat, and the green will fill the bar based on the current size of the context in the chat.
Assistant
I'll explore the codebase to understand the current chat input component, how models/context sizes are tracked, and where to add this indicator.
Assistant
Now let me check the StatusView for the existing context display and the styles for the composer area:
Assistant
Now I have a clear picture. The
contextTokensandcontextWindoware already computed inApp.tsx, and there's an existing.ctx-barCSS class in the StatusView. I'll:- Pass
contextTokensandcontextWindowintoChatView - Add a small context bar in the composer row
- Add CSS for the compact inline variant
- Pass
Assistant
Now pass the props from
App.tsx:Assistant
Now add the CSS for the inline context bar:
Assistant
Let me fix the CSS — the
calc()with/in parentheses won't parse correctly. Let me use a simpler approach:Assistant
Now let me verify everything compiles:
Assistant
Both typecheck and build pass. Let me verify the final result looks right:
Assistant
Done. Here's what I added:
ChatView.tsx— NewContextBarcomponent + two new props:ContextBar: A small inline component showing the token count (e.g.,2.4K) with a 3px progress bar underneath. At 80%+ usage, the bar turns from green (--accent) to amber (--warn). Hovering showstitle="2.4K / 8.00M tokens".- Props:
contextTokens(current usage) andcontextWindow(max tokens) passed through fromApp.tsx. - Placement: In the composer row, between the model name hint and the token count — only visible when a context window is configured and the chat has messages.
styles.css—.ctx-bar-inlineclass:- Uses CSS
maskwith alinear-gradientto create the fill effect driven by the--ctx-pctCSS variable. - Compact 3px bar, 22px wide, sitting below the text.
App.tsx— Passes the already-computedcontextTokensandcontextWindowdown toChatView.- User
it's not showing in the local build...
Assistant
Let me check what's happening. The most likely issue is the CSS mask not rendering the fill bar, or the condition not matching.
Assistant
The issue is CSS stacking order —
::beforepaints behind::after, so the masked fill is hidden under the track. Let me swap them.Assistant
There's also a second issue —
contextWindowisnullunless you've explicitly set it on your endpoint inconfig.toml. Let me check if that's the blocker:Assistant
The
contextWindowis only available if you've setcontext_windowon your endpoint inconfig.toml. Let me also make it render in the dev build even without that config so you can see it — I'll add a sensible default:Assistant
Now let me verify it builds:
Assistant
Actually let me remove the unused
knownprop — not needed for now:Assistant
Fixed two issues:
-
CSS stacking order —
::afternow paints on top of::before, so the masked fill bar is visible above the track background. -
Removed the
contextWindowrequirement — The bar now shows whenever there are messages, defaulting to 200k tokens if your endpoint doesn't havecontext_windowconfigured. Setcontext_window = 8192(or whatever your model supports) inconfig.tomlfor accurate sizing.
Both typecheck and build pass. The mini progress bar should now appear in the composer row next to the model name, showing something like
2.4Kwith a green fill bar below it.-
- User
ok it's there. kinda. there's no label (e.g. 24k/128k) and there's no color
Assistant
Let me check the rendered output and fix both issues. The CSS mask approach likely isn't working in the WKWebView. I'll switch to a simple nested-element approach.
Assistant
Now replace the pseudo-element CSS with real nested elements:
Assistant
Build passes. The changes:
- Label: Now renders
fmtTokens(used) / fmtTokens(total)text directly (e.g.2.4K / 8.00M) - Color: Swapped the CSS
maskpseudo-element approach (doesn't work reliably in WKWebView) for real nested<span>s — a track withvar(--panel-3)background and a fill child sized by inlinewidthstyle usingvar(--accent)/var(--warn)
- Label: Now renders
- User
ok so i think it's there now -- but it's still not working correctly i don't think. can we verify things?
Assistant
Several issues to verify. Let me trace the data:
Assistant
Found several problems. Here's what's wrong:
1.
contextTokensnever updates during streaming. TheuseMemodepends on[activeConvId, messages.length, streaming]. During streaming,messages.lengthstays the same (the last message is mutated in place), andstreamingis only true/false — so it fires once at the start and once at the end. The fill bar is stale mid-conversation.2. It counts only message text — no system prompt, no tool call overhead, no image tokens. The real count comes from the server's
usageevent (prompt_tokens).3.
contextWindowis likelynullif you haven't set it inconfig.toml, so it defaults to 200k and the bar fills barely at all.The fix: use the server's
prompt_tokensfrom theusageevent (the authoritative count). Let me wire that through:Assistant
Now I'll make the fix. The server's
prompt_tokensfrom theusageevent is the authoritative count. I'll track it per-conversation and use it for the bar.Assistant
Both clean. Here's what was wrong and what I fixed:
Problems
- Stale count —
contextTokensusedcountTokens()over message text and only recomputed whenmessages.lengthchanged. During streaming, the last message is mutated in place so the length never changes — the bar was frozen after the first turn. - Missed tokens — The frontend estimate skipped system prompts, tool call overhead, and image tokens. The server's
prompt_tokensin theusageevent is the real number.
Changes
ConvRuntimenow trackspromptTokens(server-reported from the lastusageevent).contextTokensderivation prefers the server value when available, falling back to the frontend estimate only on the very first load before any turn completes.usageevent handler now updates the per-conversationpromptTokensviapatchConv, so the bar updates as soon as each turn finishes.
- Stale count —
- User
/exit