Repository navigation
feat(web-shell): show context usage as a mini progress pill in the status bar #8794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
e226bf9
a751f5a
9934b1f
ed5ea4d
30d7248
ec61627
2e1f321
88276e4
68dd352
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -411,6 +411,7 @@ function getFullscreenSurfaceTabEdges( | |
| } | ||
| const DEFAULT_COMPOSER_TOOLBAR_ACTIONS = [ | ||
| 'approvalMode', | ||
| 'contextUsage', | ||
| 'model', | ||
| 'widthMode', | ||
| 'voice', | ||
|
|
@@ -6255,6 +6256,12 @@ export function App({ | |
| reportError, | ||
| ], | ||
| ); | ||
| // Stable identity: ChatEditor is memoized and an inline closure would | ||
| // re-render it on every app render. | ||
| const handleShowContextUsage = useCallback( | ||
| () => showContextUsage('/context', false), | ||
| [showContextUsage], | ||
| ); | ||
|
|
||
| // Stable reference: this travels through the memoized MessageList → | ||
| // MessageItem chain, so an inline closure would defeat their memo. | ||
|
|
@@ -10628,6 +10635,9 @@ export function App({ | |
| ? DEFAULT_EMPTY_COMPOSER_TOOLBAR_ACTIONS | ||
| : DEFAULT_COMPOSER_TOOLBAR_ACTIONS) | ||
| } | ||
| tokenCount={connection.tokenCount ?? 0} | ||
| contextWindow={connection.contextWindow ?? 0} | ||
| onShowContextUsage={handleShowContextUsage} | ||
|
Comment on lines
+10638
to
+10640
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已处理(30d7248):App.test.tsx 新增接线用例——默认 |
||
| availableModels={availableModels} | ||
| onSelectMode={handleSetMode} | ||
| onSelectModel={handleModelSelect} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1399,6 +1399,34 @@ | |
| flex-shrink: 0; | ||
| } | ||
|
|
||
| .contextUsageBtn { | ||
| flex-shrink: 0; | ||
| padding: 0 4px; | ||
| } | ||
|
|
||
| .contextRingTrack { | ||
| stroke: var(--border); | ||
| } | ||
|
|
||
| .contextRingValue { | ||
| stroke: var(--agent-blue-500); | ||
| transition: stroke-dashoffset 0.25s ease; | ||
|
Comment on lines
+1411
to
+1413
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已处理(30d7248):为 |
||
| } | ||
|
|
||
| .contextRingValueWarning { | ||
| stroke: var(--warning-color); | ||
| } | ||
|
|
||
| .contextRingValueError { | ||
| stroke: var(--error-color); | ||
| } | ||
|
|
||
| @media (prefers-reduced-motion: reduce) { | ||
| .contextRingValue { | ||
| transition: none; | ||
| } | ||
| } | ||
|
|
||
| .widthModeBtn .toolBtnIcon svg { | ||
| width: 18px; | ||
| height: 18px; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import type { | |
| } from '../hooks/useComposerCore'; | ||
| import type { UseDaemonFollowupSuggestionReturn } from '@qwen-code/webui/daemon-react-sdk'; | ||
| import { ChatEditor, type ComposerToolbarAction } from './ChatEditor'; | ||
| import { formatContextUsageDetail } from '../utils/formatTokenCount'; | ||
| import { WebShellPortalRootContext } from '../portalRoot'; | ||
|
|
||
| Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }); | ||
|
|
@@ -265,6 +266,9 @@ function renderChatEditor(props: { | |
| onSelectMode?: (mode: string) => void; | ||
| onSelectModel?: (model: string) => void; | ||
| onAttachmentsChange?: (hasAttachments: boolean) => void; | ||
| tokenCount?: number; | ||
| contextWindow?: number; | ||
| onShowContextUsage?: () => void; | ||
| placeholderText?: string; | ||
| animatePlaceholder?: boolean; | ||
| disabled?: boolean; | ||
|
|
@@ -343,6 +347,145 @@ describe('ChatEditor voice toolbar integration', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('ChatEditor context usage ring', () => { | ||
| const ring = (container: HTMLElement) => | ||
| container.querySelector<HTMLButtonElement>( | ||
| '[data-web-shell-context-usage]', | ||
| ); | ||
|
|
||
| it('renders in toolbarRight before the voice actions', () => { | ||
| const container = renderChatEditor({ | ||
| tokenCount: 34_298, | ||
| contextWindow: 100_000, | ||
| onShowContextUsage: vi.fn(), | ||
| }); | ||
|
|
||
| const button = ring(container)!; | ||
| expect(button).not.toBeNull(); | ||
| expect(button.getAttribute('aria-label')).toBe('34.3% context used'); | ||
| const liveVoice = container.querySelector( | ||
| '[data-testid="live-voice-button"]', | ||
| )!; | ||
| // The ring sits immediately left of the voice cluster. | ||
| expect( | ||
| button.compareDocumentPosition(liveVoice) & | ||
| Node.DOCUMENT_POSITION_FOLLOWING, | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('is controlled by the contextUsage toolbar action', () => { | ||
| const shown = renderChatEditor({ | ||
| tokenCount: 100, | ||
| contextWindow: 1000, | ||
| visibleToolbarActions: ['contextUsage'], | ||
| }); | ||
| const hidden = renderChatEditor({ | ||
| tokenCount: 100, | ||
| contextWindow: 1000, | ||
| visibleToolbarActions: ['voice'], | ||
| }); | ||
|
|
||
| expect(ring(shown)).not.toBeNull(); | ||
| expect(ring(hidden)).toBeNull(); | ||
| }); | ||
|
|
||
| it('stays hidden while usage or the context window is unknown', () => { | ||
| const noUsage = renderChatEditor({ tokenCount: 0, contextWindow: 1000 }); | ||
| const noWindow = renderChatEditor({ tokenCount: 100, contextWindow: 0 }); | ||
|
|
||
| expect(ring(noUsage)).toBeNull(); | ||
| expect(ring(noWindow)).toBeNull(); | ||
| }); | ||
|
|
||
| it('opens the context breakdown when clicked', () => { | ||
| const onShowContextUsage = vi.fn(); | ||
| const container = renderChatEditor({ | ||
| tokenCount: 100, | ||
| contextWindow: 1000, | ||
| onShowContextUsage, | ||
| }); | ||
|
|
||
| act(() => { | ||
| ring(container)!.dispatchEvent( | ||
| new MouseEvent('click', { bubbles: true, cancelable: true }), | ||
| ); | ||
| }); | ||
|
|
||
| expect(onShowContextUsage).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('shows the used/total detail in a tooltip on focus', async () => { | ||
| const container = renderChatEditor({ | ||
| tokenCount: 53_600, | ||
| contextWindow: 1_000_000, | ||
| onShowContextUsage: vi.fn(), | ||
| }); | ||
|
|
||
| await act(async () => { | ||
| ring(container)!.focus(); | ||
| }); | ||
|
|
||
| expect(document.body.textContent).toContain('53.6k / 1.0M tokens (5.4%)'); | ||
| // The arrow must be the Radix-positioned element: a pseudo-element pinned | ||
| // to the content center stops pointing at the trigger once collision | ||
| // avoidance shifts the content near the viewport edge. jsdom has no | ||
| // layout, so pin the positioning classes the rendered arrow depends on — | ||
| // a shadcn regeneration that drops them would detach the arrow visually | ||
| // while an existence check stayed green. | ||
| const arrowClass = document | ||
| .querySelector('[data-slot="tooltip-arrow"]') | ||
| ?.getAttribute('class'); | ||
| expect(arrowClass).toContain('rotate-45'); | ||
| expect(arrowClass).toContain('translate-y-[calc(-50%_-_2px)]'); | ||
| }); | ||
|
|
||
| it('escalates the arc color at the /context panel thresholds', () => { | ||
| const arcClass = (container: HTMLElement) => | ||
| ring(container)!.querySelectorAll('circle')[1].getAttribute('class') ?? | ||
| ''; | ||
|
|
||
| const warn = renderChatEditor({ tokenCount: 61, contextWindow: 100 }); | ||
| const error = renderChatEditor({ tokenCount: 81, contextWindow: 100 }); | ||
| const normal = renderChatEditor({ tokenCount: 60, contextWindow: 100 }); | ||
|
Comment on lines
+446
to
+448
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已处理(30d7248):补了 80 整点用例,断言 exactly 80% 仍是 warning 且不是 error,把严格 |
||
| // Both thresholds are strict: exactly 80% is still warning, matching the | ||
| // /context panel. | ||
| const atError = renderChatEditor({ tokenCount: 80, contextWindow: 100 }); | ||
|
|
||
| expect(arcClass(warn)).toContain('contextRingValueWarning'); | ||
| expect(arcClass(error)).toContain('contextRingValueError'); | ||
| expect(arcClass(normal)).not.toContain('Warning'); | ||
| expect(arcClass(normal)).not.toContain('Error'); | ||
| expect(arcClass(atError)).toContain('contextRingValueWarning'); | ||
| expect(arcClass(atError)).not.toContain('contextRingValueError'); | ||
| }); | ||
|
|
||
| it('caps the arc at 100% while the label keeps the real overflow', () => { | ||
| const container = renderChatEditor({ | ||
| tokenCount: 150, | ||
| contextWindow: 100, | ||
| }); | ||
|
|
||
| const button = ring(container)!; | ||
| expect(button.getAttribute('aria-label')).toBe('150.0% context used'); | ||
| const arc = button.querySelectorAll('circle')[1]; | ||
| expect(arc.getAttribute('stroke-dashoffset')).toBe('0'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('formatContextUsageDetail', () => { | ||
| it('formats used/total with k/M units and one decimal', () => { | ||
| expect(formatContextUsageDetail(53_600, 1_000_000)).toBe( | ||
| '53.6k / 1.0M tokens (5.4%)', | ||
| ); | ||
| expect(formatContextUsageDetail(338_108, 1_000_000)).toBe( | ||
| '338.1k / 1.0M tokens (33.8%)', | ||
| ); | ||
| expect(formatContextUsageDetail(512, 2000)).toBe( | ||
| '512 / 2.0k tokens (25.6%)', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ChatEditor animation layers', () => { | ||
| it('mounts the inert specular layer without replacing composer controls', () => { | ||
| const container = renderChatEditor({}); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已处理(30d7248):App.test.tsx 新增用例——
connection.tokenCount/contextWindow为 undefined(刚连接、尚无 usage)时 ChatEditor 收到 0/0,圆环隐藏,?? 0fallback 不再是测试盲区。