Files
opencode-manager/frontend/src/components/session/ContextUsageIndicator.tsx
T
Chris Scott 10158ac164 fix(sessions): match OpenCode TUI context usage
The header context count differed from the OpenCode TUI and dipped then jumped
back up mid-session:

- cache write tokens were left out of the total, so context stored as a cache
  write was dropped and then reappeared as a cache read on the next step
- the newest assistant message without token data was skipped only one message
  back, so a step without usage showed 0 or a stale count

The total now includes cache write tokens and the newest assistant message that
reports tokens is counted, matching the TUI: messages before the latest completed
compaction are ignored, messages at or after a revert point are ignored, and the
indicator is hidden when no message qualifies or the total is zero.
2026-10-12 01:12:55 +00:00

58 lines
1.4 KiB
TypeScript

import { useContextUsage } from '@/hooks/useContextUsage'
interface ContextUsageIndicatorProps {
directory?: string
sessionID: string | undefined
isConnected: boolean
isReconnecting?: boolean
revertMessageID?: string
}
const getUsageTextColor = (percentage: number) => {
if (percentage < 50) return 'text-success'
if (percentage < 80) return 'text-warning'
return 'text-destructive'
}
export function ContextUsageIndicator({
directory,
sessionID,
isConnected,
isReconnecting,
revertMessageID,
}: ContextUsageIndicatorProps) {
const { usage, isLoading } = useContextUsage(sessionID, directory, revertMessageID)
if (isLoading) {
return (
<div className="flex items-center gap-2">
<span className="text-xs text-muted-foreground">Loading...</span>
</div>
)
}
if (isReconnecting) {
return <span className="text-xs text-warning font-medium">Reconnecting...</span>
}
if (!isConnected) {
return <span className="text-xs text-muted-foreground font-medium">Disconnected</span>
}
if (!usage) {
return null
}
const tokenText = usage.usagePercentage === null
? usage.totalTokens.toLocaleString()
: `${usage.totalTokens.toLocaleString()} (${usage.usagePercentage}%)`
return (
<div className="flex items-center gap-2">
<span className={`text-xs font-medium whitespace-nowrap ${getUsageTextColor(usage.usagePercentage ?? 0)}`}>
{tokenText}
</span>
</div>
)
}