@@ -20,6 +20,7 @@ import { type ContextWindowSnapshot, formatContextWindowTokens } from "../../lib
2020import { cn } from "../../lib/utils" ;
2121import { formatUpcomingTimestamp } from "../../timestampFormat" ;
2222import { Tooltip , TooltipPopup , TooltipTrigger } from "../ui/tooltip" ;
23+ import { formatContextWindowCompactionMessage } from "./ContextWindowMeter.logic" ;
2324import { ThreadDetailsSection } from "./ThreadDetailsSection" ;
2425
2526const PACE_LABEL : Record < LimitPace , string > = {
@@ -35,16 +36,14 @@ export interface ThreadDetailsUsageInput {
3536 readonly contextWindow : ContextWindowSnapshot | null ;
3637}
3738
38- /**
39- * The selected provider's limit windows and the thread's context window.
40- * Every row reads as room left, so a longer bar always means more headroom.
41- */
39+ type UsageTone = "error" | "warning" | null ;
40+
41+ /** The selected provider's limit windows. Each row reads as room left. */
4242export function ThreadDetailsUsage ( {
4343 providerInstanceId,
4444 providers,
4545 sources,
46- contextWindow,
47- } : ThreadDetailsUsageInput ) {
46+ } : Omit < ThreadDetailsUsageInput , "contextWindow" > ) {
4847 const nowMinute = useNowMinute ( ) ;
4948 const now = Date . parse ( `${ nowMinute } :00Z` ) ;
5049 const report =
@@ -56,28 +55,83 @@ export function ThreadDetailsUsage({
5655 const limits =
5756 report ?. accounts . find ( ( entry ) => entry . instanceId === providerInstanceId ) ?. limits ??
5857 providers . find ( ( provider ) => provider . instanceId === providerInstanceId ) ?. usageLimits ;
59- const notice = limits ? limitsNotice ( limits ) : null ;
60- if ( ! limits && ! contextWindow ) return null ;
58+ if ( ! limits ) return null ;
59+ const notice = limitsNotice ( limits ) ;
6160
6261 return (
6362 < ThreadDetailsSection
6463 headingId = "thread-details-usage-heading"
65- title = "Usage"
64+ title = "Usage limits "
6665 showHeading = { false }
6766 >
6867 < div className = "grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs" >
6968 { notice ? (
7069 < UsageRow
7170 label = "Limits"
72- left = { null }
71+ fill = { null }
72+ tone = { null }
7373 value = "Unavailable"
7474 tooltip = { < span className = "text-foreground" > { notice } </ span > }
7575 />
76- ) : null }
77- { limits && ! notice
78- ? limits . windows . map ( ( window ) => < LimitRow key = { window . id } window = { window } now = { now } /> )
79- : null }
80- { contextWindow ? < ContextRow usage = { contextWindow } spaced = { limits !== undefined } /> : null }
76+ ) : (
77+ limits . windows . map ( ( window ) => < LimitRow key = { window . id } window = { window } now = { now } /> )
78+ ) }
79+ </ div >
80+ </ ThreadDetailsSection >
81+ ) ;
82+ }
83+
84+ /** The thread's context window. The bar fills as context is used; the tick marks auto-compaction. */
85+ export function ThreadDetailsContext ( { usage } : { readonly usage : ContextWindowSnapshot } ) {
86+ const maxTokens = usage . maxTokens != null && usage . maxTokens > 0 ? usage . maxTokens : null ;
87+ const used =
88+ maxTokens !== null && usage . usedPercentage !== null
89+ ? Math . round ( Math . max ( 0 , Math . min ( 100 , usage . usedPercentage ) ) )
90+ : null ;
91+ const threshold = usage . autoCompactThreshold ?? null ;
92+ const tick =
93+ maxTokens !== null && threshold !== null && threshold > 0 && threshold < maxTokens
94+ ? ( threshold / maxTokens ) * 100
95+ : null ;
96+ const usedTokens = formatContextWindowTokens ( usage . usedTokens ) ;
97+ return (
98+ < ThreadDetailsSection
99+ headingId = "thread-details-context-heading"
100+ title = "Context window"
101+ showHeading = { false }
102+ >
103+ < div className = "grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs" >
104+ < UsageRow
105+ label = "Context"
106+ fill = { used }
107+ tone = { used === null ? null : used >= 90 ? "error" : used >= 80 ? "warning" : null }
108+ tick = { tick }
109+ muted
110+ value = {
111+ maxTokens !== null
112+ ? `${ usedTokens } / ${ formatContextWindowTokens ( maxTokens ) } `
113+ : usedTokens
114+ }
115+ ariaValue = { used !== null ? `${ used } % used` : `${ usedTokens } tokens used` }
116+ tooltip = {
117+ < >
118+ < span className = "text-foreground" >
119+ { used !== null ? `Context: ${ used } % used` : `Context: ${ usedTokens } tokens used` }
120+ </ span >
121+ { maxTokens !== null ? (
122+ < span className = "text-muted-foreground" >
123+ { usage . usedTokens . toLocaleString ( "en-US" ) } of { maxTokens . toLocaleString ( "en-US" ) } { " " }
124+ tokens
125+ </ span >
126+ ) : null }
127+ { threshold !== null && threshold > 0 ? (
128+ < span className = "text-muted-foreground" >
129+ { formatContextWindowCompactionMessage ( null , threshold ) }
130+ </ span >
131+ ) : null }
132+ </ >
133+ }
134+ />
81135 </ div >
82136 </ ThreadDetailsSection >
83137 ) ;
@@ -97,7 +151,10 @@ function LimitRow({
97151 return (
98152 < UsageRow
99153 label = { window . label }
100- left = { left }
154+ fill = { left }
155+ tone = { left <= 10 ? "error" : left <= 20 ? "warning" : null }
156+ value = { `${ left } %` }
157+ ariaValue = { `${ left } % left` }
101158 tooltip = {
102159 < >
103160 < span className = "text-foreground" >
@@ -116,76 +173,43 @@ function LimitRow({
116173 ) ;
117174}
118175
119- function ContextRow ( {
120- usage,
121- spaced,
122- } : {
123- readonly usage : ContextWindowSnapshot ;
124- readonly spaced : boolean ;
125- } ) {
126- const maxTokens = usage . maxTokens ?? null ;
127- const left =
128- maxTokens !== null && usage . usedPercentage !== null
129- ? Math . round ( 100 - Math . max ( 0 , Math . min ( 100 , usage . usedPercentage ) ) )
130- : null ;
131- const used = formatContextWindowTokens ( usage . usedTokens ) ;
132- return (
133- < UsageRow
134- label = "Context"
135- left = { left }
136- muted
137- className = { cn ( spaced && "mt-2" ) }
138- tooltip = {
139- < >
140- { left !== null ? < span className = "text-foreground" > Context: { left } % left</ span > : null }
141- < span className = { left !== null ? "text-muted-foreground" : "text-foreground" } >
142- { maxTokens !== null
143- ? `${ used } of ${ formatContextWindowTokens ( maxTokens ) } tokens used`
144- : `${ used } tokens used` }
145- </ span >
146- </ >
147- }
148- value = { left === null ? used : undefined }
149- />
150- ) ;
151- }
152-
153176/** One label, hairline, and value row; the whole row opens the details tooltip. */
154177function UsageRow ( {
155178 label,
156- left,
157- tooltip,
179+ fill,
180+ tone,
181+ tick = null ,
158182 value,
183+ ariaValue = value ,
184+ tooltip,
159185 muted = false ,
160- className,
161186} : {
162187 readonly label : string ;
163- readonly left : number | null ;
188+ /** Bar fill from 0 to 100. Null hides the bar. */
189+ readonly fill : number | null ;
190+ readonly tone : UsageTone ;
191+ /** A marker on the bar, from 0 to 100. */
192+ readonly tick ?: number | null ;
193+ readonly value : string ;
194+ readonly ariaValue ?: string ;
164195 readonly tooltip : ReactNode ;
165- /** Replaces the percentage when there is none. */
166- readonly value ?: string | undefined ;
167196 readonly muted ?: boolean ;
168- readonly className ?: string | undefined ;
169197} ) {
170- const tone = left === null ? null : left <= 10 ? "error" : left <= 20 ? "warning" : null ;
171198 return (
172199 < Tooltip >
173200 < TooltipTrigger
174201 render = {
175202 < div
176203 tabIndex = { 0 }
177- aria-label = { `${ label } : ${ value ?? `${ left } % left` } ` }
178- className = { cn (
179- "col-span-3 grid h-6 cursor-default grid-cols-subgrid items-center rounded-lg px-2.5 outline-none hover:bg-black/[0.035] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring data-popup-open:bg-black/[0.035] dark:hover:bg-white/[0.08] dark:data-popup-open:bg-white/[0.08]" ,
180- className ,
181- ) }
204+ aria-label = { `${ label } : ${ ariaValue } ` }
205+ className = "col-span-3 grid h-6 cursor-default grid-cols-subgrid items-center rounded-lg px-2.5 outline-none hover:bg-black/[0.035] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring data-popup-open:bg-black/[0.035] dark:hover:bg-white/[0.08] dark:data-popup-open:bg-white/[0.08]"
182206 />
183207 }
184208 >
185209 < span className = "max-w-28 truncate text-muted-foreground" > { label } </ span >
186- { left !== null ? (
210+ { fill !== null ? (
187211 < span className = "relative h-[3px] rounded-full bg-muted-foreground/30 dark:bg-muted-foreground/20" >
188- { left > 0 ? (
212+ { fill > 0 ? (
189213 < span
190214 className = { cn (
191215 "absolute inset-y-0 left-0 rounded-full" ,
@@ -197,7 +221,14 @@ function UsageRow({
197221 ? "bg-muted-foreground"
198222 : "bg-foreground/80" ,
199223 ) }
200- style = { { width : `max(${ left } %, 6px)` } }
224+ style = { { width : `max(${ fill } %, 6px)` } }
225+ />
226+ ) : null }
227+ { tick !== null ? (
228+ < span
229+ aria-hidden
230+ className = "absolute -top-[3px] h-[9px] w-px -translate-x-1/2 bg-foreground/60"
231+ style = { { left : `${ tick } %` } }
201232 />
202233 ) : null }
203234 </ span >
@@ -206,17 +237,17 @@ function UsageRow({
206237 className = { cn (
207238 // Text-only rows span the bar column so they never widen the shared value column.
208239 "text-end font-medium tabular-nums" ,
209- left === null && "col-span-2" ,
240+ fill === null && "col-span-2" ,
210241 tone === "error"
211242 ? "text-error-foreground"
212243 : tone === "warning"
213244 ? "text-warning-foreground"
214- : left === null
245+ : fill === null
215246 ? "text-muted-foreground"
216247 : "text-foreground/80" ,
217248 ) }
218249 >
219- { value ?? ` ${ left } %` }
250+ { value }
220251 </ span >
221252 </ TooltipTrigger >
222253 { /* Rows sit 8px inside the card; clear its edge instead of overlapping it. */ }
0 commit comments