mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(frontend): show context usage on the composer model pill
The model pill shows the context percentage with a tone meter, and the model picker gains a context strip with a Compact action.
This commit is contained in:
1 parent
36d35cc974
commit
52162d5c4a
14 files changed
+438
-13
No files matched your search
@@ -563,3 +563,73 @@ describe('ModelQuickSelect keyboard navigation', () => {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('ModelQuickSelect context strip', () => {
|
||||
const session = { id: 'ses_1' }
|
||||
const usage = { totalTokens: 12000, usagePercentage: 6, contextLimit: 200000 }
|
||||
|
||||
it('shows the used and limit tokens with the percentage and meter', () => {
|
||||
render(<ModelQuickSelect open session={session} contextUsage={usage} onCompact={vi.fn()} />)
|
||||
|
||||
const summary = screen.getByTestId('context-usage-summary')
|
||||
|
||||
expect(summary).toHaveTextContent('12k / 200k · 6%')
|
||||
expect(summary).toHaveClass('text-success')
|
||||
expect(summary).toHaveAttribute('aria-label', 'context 6% used')
|
||||
expect(screen.getByTestId('context-usage-meter-fill')).toHaveStyle({ width: '6%' })
|
||||
})
|
||||
|
||||
it('tints the summary with the warning and destructive tones', () => {
|
||||
const { rerender } = render(
|
||||
<ModelQuickSelect open session={session} contextUsage={{ ...usage, usagePercentage: 60 }} />,
|
||||
)
|
||||
expect(screen.getByTestId('context-usage-summary')).toHaveClass('text-warning')
|
||||
|
||||
rerender(<ModelQuickSelect open session={session} contextUsage={{ ...usage, usagePercentage: 85 }} />)
|
||||
expect(screen.getByTestId('context-usage-summary')).toHaveClass('text-destructive')
|
||||
})
|
||||
|
||||
it('compacts the session from the strip', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onCompact = vi.fn()
|
||||
|
||||
render(<ModelQuickSelect open session={session} contextUsage={usage} onCompact={onCompact} />)
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Compact session' }))
|
||||
|
||||
expect(onCompact).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('disables compaction while compacting or without a session', () => {
|
||||
const onCompact = vi.fn()
|
||||
const { rerender } = render(
|
||||
<ModelQuickSelect open session={session} contextUsage={usage} isCompacting onCompact={onCompact} />,
|
||||
)
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Compact session' })).toBeDisabled()
|
||||
|
||||
rerender(<ModelQuickSelect open contextUsage={usage} onCompact={onCompact} />)
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Compact session' })).toBeDisabled()
|
||||
expect(onCompact).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('falls back to tokens and an empty meter without a known limit', () => {
|
||||
render(
|
||||
<ModelQuickSelect
|
||||
open
|
||||
session={session}
|
||||
contextUsage={{ totalTokens: 12000, usagePercentage: null, contextLimit: null }}
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(screen.getByTestId('context-usage-summary')).toHaveTextContent('12k tokens')
|
||||
expect(screen.getByTestId('context-usage-meter-fill')).toHaveStyle({ width: '0%' })
|
||||
})
|
||||
|
||||
it('omits the strip without context usage', () => {
|
||||
render(<ModelQuickSelect open session={session} />)
|
||||
|
||||
expect(screen.queryByTestId('context-usage-strip')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
@@ -14,8 +14,16 @@ import {
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { ContextUsageMeter } from '@/components/session/ContextUsageMeter'
|
||||
import { buildModelSections, filterModelSections, type ModelOption } from '@/lib/modelSections'
|
||||
import { isTextEntryElement } from '@/lib/domTargets'
|
||||
import {
|
||||
CONTEXT_USAGE_TEXT_CLASS,
|
||||
contextUsageAccessibleLabel,
|
||||
contextUsageTone,
|
||||
formatCompactTokens,
|
||||
type ContextUsage,
|
||||
} from '@/lib/contextUsage'
|
||||
|
||||
interface ModelQuickSelectProps {
|
||||
directory?: string
|
||||
@@ -24,6 +32,9 @@ interface ModelQuickSelectProps {
|
||||
open?: boolean
|
||||
onOpenChange?: (open: boolean) => void
|
||||
session?: ModelSelectionSession
|
||||
contextUsage?: ContextUsage | null
|
||||
isCompacting?: boolean
|
||||
onCompact?: () => void
|
||||
}
|
||||
|
||||
interface ModelListItem {
|
||||
@@ -70,6 +81,50 @@ function formatModelCount(count: number) {
|
||||
return `${count} ${count === 1 ? 'model' : 'models'}`
|
||||
}
|
||||
|
||||
interface ContextUsageStripProps {
|
||||
usage: ContextUsage
|
||||
onCompact?: () => void
|
||||
compactDisabled: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Compact context-window strip pinned above the picker header: the tone meter, then one line with the
|
||||
* used and limit token counts, the percentage, and the session compaction action.
|
||||
*/
|
||||
function ContextUsageStrip({ usage, onCompact, compactDisabled }: ContextUsageStripProps) {
|
||||
const percentage = usage.usagePercentage
|
||||
const toneClass = CONTEXT_USAGE_TEXT_CLASS[contextUsageTone(percentage)]
|
||||
const summary = usage.contextLimit === null || percentage === null
|
||||
? `${formatCompactTokens(usage.totalTokens)} tokens`
|
||||
: `${formatCompactTokens(usage.totalTokens)} / ${formatCompactTokens(usage.contextLimit)} · ${percentage}%`
|
||||
|
||||
return (
|
||||
<div data-testid="context-usage-strip" className="shrink-0 border-b border-border px-4 pb-2">
|
||||
<ContextUsageMeter percentage={percentage} />
|
||||
<div className="mt-1.5 flex items-center gap-3">
|
||||
<span
|
||||
data-testid="context-usage-summary"
|
||||
aria-label={contextUsageAccessibleLabel(usage)}
|
||||
className={`min-w-0 truncate text-xs font-medium ${toneClass}`}
|
||||
>
|
||||
{summary}
|
||||
</span>
|
||||
{onCompact && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onCompact}
|
||||
disabled={compactDisabled}
|
||||
aria-label="Compact session"
|
||||
className="ml-auto shrink-0 rounded text-xs font-medium text-info hover:underline disabled:cursor-not-allowed disabled:opacity-40 disabled:no-underline"
|
||||
>
|
||||
Compact
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function VirtualizedList<T>({
|
||||
items,
|
||||
itemHeight,
|
||||
@@ -176,6 +231,9 @@ export function ModelQuickSelect({
|
||||
open,
|
||||
onOpenChange,
|
||||
session,
|
||||
contextUsage,
|
||||
isCompacting = false,
|
||||
onCompact,
|
||||
}: ModelQuickSelectProps) {
|
||||
const [internalIsOpen, setInternalIsOpen] = useState(false)
|
||||
const isOpen = open ?? internalIsOpen
|
||||
@@ -410,6 +468,11 @@ export function ModelQuickSelect({
|
||||
setSearchQuery('')
|
||||
}
|
||||
|
||||
const handleCompact = () => {
|
||||
onCompact?.()
|
||||
handleOpenChange(false)
|
||||
}
|
||||
|
||||
const getDescription = (item: ModelListItem) => {
|
||||
const context = item.model?.limit?.context
|
||||
if (context) {
|
||||
@@ -576,6 +639,13 @@ export function ModelQuickSelect({
|
||||
className="z-[300] border-border bg-popover text-foreground shadow-2xl md:mx-auto md:max-w-lg"
|
||||
ariaLabel="Select model"
|
||||
>
|
||||
{contextUsage && (
|
||||
<ContextUsageStrip
|
||||
usage={contextUsage}
|
||||
onCompact={onCompact ? handleCompact : undefined}
|
||||
compactDisabled={!session?.id || isCompacting}
|
||||
/>
|
||||
)}
|
||||
<div className={`flex items-center justify-between gap-2 px-4 ${showAllModels ? 'pb-2 pt-2' : 'pb-3 pt-0'}`}>
|
||||
{showAllModels ? (
|
||||
<>
|
||||
|
||||
Reference in new issue
Block a user