Skip to content

在footbar增加context上下文使用量展示#408

Closed
carolove wants to merge 1 commit intoclaude-code-best:mainfrom
carolove:main
Closed

在footbar增加context上下文使用量展示#408
carolove wants to merge 1 commit intoclaude-code-best:mainfrom
carolove:main

Conversation

@carolove
Copy link
Copy Markdown

@carolove carolove commented May 3, 2026

Summary by CodeRabbit

  • New Features
    • Added context window usage indicator to the prompt footer, displaying the percentage of available context consumed with severity-level coloring to help manage token allocation.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 3, 2026

📝 Walkthrough

Walkthrough

A single-cohort enhancement adds context window usage display to the prompt footer. The parent component threads the messages array to a new hook that computes context usage percentage and severity, rendering a context pill in the footer indicator.

Changes

Context Window Usage Display

Layer / File(s) Summary
Hook & Imports
src/components/PromptInput/PromptInputFooterLeftSide.tsx (lines 33–43, 81–97)
New imports for context/token utilities (getCurrentUsage, calculateContextPercentages, getContextWindowForModel, useMainLoopModel, getSdkBetas) and Message type. useContextDisplay(messages) hook computes context usage percentage, derives severity level, and returns ctx:${pct}% label or null.
Component Props
src/components/PromptInput/PromptInputFooterLeftSide.tsx (lines 98–113, 219–411)
PromptInputFooterLeftSide Props now include required messages: Message[]. ModeIndicator Props updated to accept messages: Message[] and consume the context display hook.
Prop Threading
src/components/PromptInput/PromptInputFooter.tsx (lines 102–123)
src/components/PromptInput/PromptInputFooterLeftSide.tsx (lines 159–204)
PromptInputFooter passes messages={messages} to PromptInputFooterLeftSide, which destructures and forwards messages to ModeIndicator.
UI Rendering
src/components/PromptInput/PromptInputFooterLeftSide.tsx (lines 219–411)
ModeIndicator calls useContextDisplay(messages), and conditionally appends a context pill into the displayed parts array with coloring driven by computed severity level (normal/warning/error).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A window of context, now on display, 🪟
Tokens are measured in their own way,
From messages flowing to calculus bright,
A percentage pill glows with warning or light!
Footer now knows when we're running tight! 📊

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a context window usage display to the footer component.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@carolove
Copy link
Copy Markdown
Author

carolove commented May 3, 2026

#407

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/components/PromptInput/PromptInputFooterLeftSide.tsx (1)

81-82: 💤 Low value

CtxState is identical to the existing RssState — consider a shared type.

Both types share the same shape: { text: string; level: 'normal' | 'warning' | 'error' }. Deduplicating prevents them from drifting silently.

♻️ Suggested refactor
-type RssState = { text: string; level: 'normal' | 'warning' | 'error' };
+type FooterPillState = { text: string; level: 'normal' | 'warning' | 'error' };
 
-type CtxState = { text: string; level: 'normal' | 'warning' | 'error' };

Then replace all RssState and CtxState references with FooterPillState.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/components/PromptInput/PromptInputFooterLeftSide.tsx` around lines 81 -
82, The two identical types RssState and CtxState should be consolidated into a
single shared type (e.g., FooterPillState) to avoid duplication; add a type
alias or exported type named FooterPillState = { text: string; level: 'normal' |
'warning' | 'error' }, then replace all occurrences of RssState and CtxState
with FooterPillState (update imports/exports where those symbols are used) so
both components consume the single shared type.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/components/PromptInput/PromptInputFooterLeftSide.tsx`:
- Around line 83-96: The useContextDisplay hook (function useContextDisplay)
must be gated behind a feature flag: keep all hook calls (useMainLoopModel,
useMemo/getCurrentUsage, getContextWindowForModel, calculateContextPercentages)
so React rules of hooks are preserved, import feature from 'bun:bundle' and call
feature('FLAG_NAME') and then conditionally return the computed state only when
the flag is true (otherwise return null); do not wrap or remove the hook calls
themselves—gate the return value or extract pure non-hook logic into a helper
and only use that helper when feature('FLAG_NAME') is true.

---

Nitpick comments:
In `@src/components/PromptInput/PromptInputFooterLeftSide.tsx`:
- Around line 81-82: The two identical types RssState and CtxState should be
consolidated into a single shared type (e.g., FooterPillState) to avoid
duplication; add a type alias or exported type named FooterPillState = { text:
string; level: 'normal' | 'warning' | 'error' }, then replace all occurrences of
RssState and CtxState with FooterPillState (update imports/exports where those
symbols are used) so both components consume the single shared type.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1eee86fd-1766-4032-a507-c5b664ea5b22

📥 Commits

Reviewing files that changed from the base of the PR and between 6becb8b and bea6ce1.

📒 Files selected for processing (2)
  • src/components/PromptInput/PromptInputFooter.tsx
  • src/components/PromptInput/PromptInputFooterLeftSide.tsx

Comment on lines +83 to +96
function useContextDisplay(messages: Message[]): CtxState | null {
const model = useMainLoopModel();
const currentUsage = useMemo(() => getCurrentUsage(messages), [messages]);
const contextWindowSize = useMemo(() => getContextWindowForModel(model, getSdkBetas()), [model]);
const percentages = useMemo(
() => calculateContextPercentages(currentUsage, contextWindowSize),
[currentUsage, contextWindowSize],
);

if (!currentUsage || percentages.used === null) return null;
const pct = percentages.used;
const level = pct >= 90 ? 'error' : pct >= 70 ? 'warning' : 'normal';
return { text: `ctx:${pct}%`, level };
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

New context-display feature is not gated by a feature flag.

The useContextDisplay hook and its rendering (lines 303, 400–411) are unconditionally active. Per coding guidelines, new features must use the feature('FLAG_NAME') pattern so they can be controlled at runtime.

♻️ Example of how to gate it
+  const ctxState = feature('CONTEXT_WINDOW_DISPLAY') ? useContextDisplay(messages) : null;

And wrap the hook body:

 function useContextDisplay(messages: Message[]): CtxState | null {
+  if (!feature('CONTEXT_WINDOW_DISPLAY')) return null;
   const model = useMainLoopModel();
   ...

Note: because feature() is compile-time dead-code eliminated, the hook calls inside must still execute (React rules of hooks). Gate the return value, not the hook calls themselves, or extract non-hook logic into a plain helper.

As per coding guidelines: "New features must follow the pattern: keep import { feature } from 'bun:bundle' + feature('FLAG_NAME'), and control via environment variables or configuration at runtime."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/components/PromptInput/PromptInputFooterLeftSide.tsx` around lines 83 -
96, The useContextDisplay hook (function useContextDisplay) must be gated behind
a feature flag: keep all hook calls (useMainLoopModel, useMemo/getCurrentUsage,
getContextWindowForModel, calculateContextPercentages) so React rules of hooks
are preserved, import feature from 'bun:bundle' and call feature('FLAG_NAME')
and then conditionally return the computed state only when the flag is true
(otherwise return null); do not wrap or remove the hook calls themselves—gate
the return value or extract pure non-hook logic into a helper and only use that
helper when feature('FLAG_NAME') is true.

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