Skip to content

feat(ui): show context-usage pie chart on the clear-context button - #3079

Open
sufan721 wants to merge 1 commit into
zhayujie:masterfrom
sufan721:feat/context-usage-pie
Open

feat(ui): show context-usage pie chart on the clear-context button#3079
sufan721 wants to merge 1 commit into
zhayujie:masterfrom
sufan721:feat/context-usage-pie

Conversation

@sufan721

Copy link
Copy Markdown
Contributor

Add a hover pie chart next to the clear-context button in both the Electron desktop and the web console, breaking the live context window down into system prompt / tool definitions / conversation / free.

What does this PR do?

Clearing context used to be a blind operation — users couldn't see how much of the context window was occupied, or by what. This PR adds a hover pie chart to the clear-context button in both the Electron desktop app and the web console, breaking the live context down into four slices:

  • System prompt / Tool definitions / Conversation / Free

The donut center shows the used percentage, with per-slice token counts and a used X / Y footer.

The backend had no context-usage endpoint at all (token estimation existed but was never surfaced — Agent.last_usage is dead code), so this also adds a read-only endpoint:

  • GET /api/sessions/{id}/context_usage (SessionContextUsageHandler)
  • Agent.get_context_usage() — reuses the existing _estimate_* estimators; the input budget mirrors _trim_messages
  • AgentBridge.peek_agent() — a side-effect-free lookup of the live instance, so hovering never spins up MCP/skills; returns available: false when the session has no live context
  • build_tools_schema() extracted from the request path for reuse by the estimator (no behavior change)

Frontend (no chart library added — hand-rolled SVG donut):

  • Desktop: new ContextUsageDonut + ContextUsagePopover (body portal), wired into ChatInput; getContextUsage added to the API client
  • Web console: installContextUsagePopover(), old data-tooltip removed from the button so the two don't overlap, i18n keys for zh / zh-Hant / en

Type of change

  • Bug fix
  • New feature
  • Docs
  • Refactor / chore

Checklist

  • I have read the Contributing Guide
  • I tested this change locally
  • Code comments and docs are in English
  • Linked related issue (if any): closes #
微信图片_20260826203015_14_1

Add a hover pie chart next to the clear-context button in both the
Electron desktop and the web console, breaking the live context window
down into system prompt / tool definitions / conversation / free.
@zhayujie

zhayujie commented Sep 4, 2026

Copy link
Copy Markdown
Owner

Really like this — surfacing context usage on the clear button is exactly the right place for it, and the implementation is careful: reusing the existing _estimate_* estimators, peek_agent() being side-effect-free so hovering never spins up MCP/skills, marking the payload estimated, and documenting why used can exceed limit. The hand-rolled SVG donut with no extra dependency is a nice touch too.

A few notes before merge:

  1. Rebase needed — it's currently CONFLICTING. Conflicts are small and desktop-only: desktop/src/renderer/src/api/client.ts and desktop/src/renderer/src/components/ChatInput.tsx. The Python backend and web console merge cleanly. Just re-apply your getContextUsage client method and the ChatInput wiring on top of current master.

  2. Please drop the accidentally committed editor file — the diff adds .obsidian/plugins/obsidian-ai-translate (a gitlink/submodule). It should be removed from the PR (and consider adding .obsidian/ to .gitignore).

  3. The tools slice counts all available tools' schema, but at run time _select_tools_for_injection() may inject a subset (e.g. on-demand MCP tool retrieval). Worth a one-line comment that the tools slice is an upper bound when retrieval is on, so the chart reading isn't confusing.

Overall solid work — happy to see it land once rebased.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants