Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadDetailsUsage.tsx

260 lines8.8 KB

1import type {
2 ProviderInstanceId,
3 ServerProvider,
4 ServerProviderUsageWindow,
5 UsageLimitSourceSnapshots,
6} from "@t3tools/contracts";
7import {
8 collectProviderUsageLimits,
9 formatDuration,
10 type LimitPace,
11 limitsNotice,
12 paceOf,
13 remainingPercent,
14} from "@t3tools/shared/usageLimits";
15import type { ReactNode } from "react";
16​
17import { usePrimarySettings } from "../../hooks/useSettings";
18import { useNowMinute } from "../../hooks/useNowMinute";
19import type { ContextWindowSnapshot } from "@t3tools/client-runtime/state/context-window";
20import { formatContextWindowTokens } from "../../lib/contextWindow";
21import { cn } from "../../lib/utils";
22import { formatUpcomingTimestamp } from "../../timestampFormat";
23import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
24import { formatContextWindowCompactionMessage } from "./compaction.logic";
25import { ThreadDetailsSection } from "./ThreadDetailsSection";
26​
27const PACE_LABEL: Record<LimitPace, string> = {
28 ahead: "Ahead of pace",
29 on: "On pace",
30 under: "Under pace",
31};
32​
33export interface ThreadDetailsUsageInput {
34 readonly providerInstanceId: ProviderInstanceId | null;
35 readonly providers: readonly ServerProvider[];
36 readonly sources: UsageLimitSourceSnapshots;
37 readonly contextWindow: ContextWindowSnapshot | null;
39​
40type UsageTone = "error" | "warning" | null;
41​
42/** The selected provider's limit windows. Each row reads as room left. */
43export function ThreadDetailsUsage({
44 providerInstanceId,
45 providers,
46 sources,
47}: Omit<ThreadDetailsUsageInput, "contextWindow">) {
48 const nowMinute = useNowMinute();
49 const now = Date.parse(`${nowMinute}:00Z`);
50 const report =
51 providerInstanceId !== null
52 ? collectProviderUsageLimits(providerInstanceId, providers, sources, now)
53 : null;
54 // Only this instance's data: the report keeps one instance per login, and any other entry may be
55 // a different login or workspace, so fall back to the instance's own snapshot.
56 const limits =
57 report?.accounts.find((entry) => entry.instanceId === providerInstanceId)?.limits ??
58 providers.find((provider) => provider.instanceId === providerInstanceId)?.usageLimits;
59 if (!limits) return null;
60 const notice = limitsNotice(limits);
61​
62 return (
63 <ThreadDetailsSection
64 headingId="thread-details-usage-heading"
65 title="Usage limits"
66 showHeading={false}
67 >
68 <div className="grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs">
69 {notice ? (
70 <UsageRow
71 label="Limits"
72 fill={null}
73 tone={null}
74 value="Unavailable"
75 tooltip={<span className="text-foreground">{notice}</span>}
76 />
77 ) : (
78 limits.windows.map((window) => <LimitRow key={window.id} window={window} now={now} />)
79 )}
80 </div>
81 </ThreadDetailsSection>
82 );
84​
85/** The thread's context window. The bar fills as context is used; the tick marks auto-compaction. */
86export function ThreadDetailsContext({ usage }: { readonly usage: ContextWindowSnapshot }) {
87 const maxTokens = usage.maxTokens != null && usage.maxTokens > 0 ? usage.maxTokens : null;
88 const used =
89 maxTokens !== null && usage.usedPercentage !== null
90 ? Math.round(Math.max(0, Math.min(100, usage.usedPercentage)))
91 : null;
92 const threshold = usage.autoCompactThreshold ?? null;
93 const tick =
94 maxTokens !== null && threshold !== null && threshold > 0 && threshold < maxTokens
95 ? (threshold / maxTokens) * 100
96 : null;
97 const usedTokens = formatContextWindowTokens(usage.usedTokens);
98 return (
99 <ThreadDetailsSection
100 headingId="thread-details-context-heading"
101 title="Context window"
102 showHeading={false}
103 >
104 <div className="grid grid-cols-[auto_minmax(3rem,1fr)_auto] gap-x-3 text-xs">
105 <UsageRow
106 label="Context"
107 fill={used}
108 tone={used === null ? null : used >= 90 ? "error" : used >= 80 ? "warning" : null}
109 tick={tick}
110 muted
111 value={
112 maxTokens !== null
113 ? `${usedTokens} / ${formatContextWindowTokens(maxTokens)}`
114 : usedTokens
115 }
116 ariaValue={used !== null ? `${used}% used` : `${usedTokens} tokens used`}
117 tooltip={
118 <>
119 <span className="text-foreground">
120 {used !== null ? `Context: ${used}% used` : `Context: ${usedTokens} tokens used`}
121 </span>
122 {maxTokens !== null ? (
123 <span className="text-muted-foreground">
124 {usage.usedTokens.toLocaleString("en-US")} of {maxTokens.toLocaleString("en-US")}{" "}
125 tokens
126 </span>
127 ) : null}
128 {threshold !== null && threshold > 0 ? (
129 <span className="text-muted-foreground">
130 {formatContextWindowCompactionMessage(threshold)}
131 </span>
132 ) : null}
133 </>
134 }
135 />
136 </div>
137 </ThreadDetailsSection>
138 );
140​
141function LimitRow({
142 window,
143 now,
144}: {
145 readonly window: ServerProviderUsageWindow;
146 readonly now: number;
147}) {
148 const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat);
149 const left = remainingPercent(window);
150 const pace = paceOf(window, now);
151 const resetsAt = window.resetsAt ? Date.parse(window.resetsAt) : Number.NaN;
152 return (
153 <UsageRow
154 label={window.label}
155 fill={left}
156 tone={left <= 10 ? "error" : left <= 20 ? "warning" : null}
157 value={`${left}%`}
158 ariaValue={`${left}% left`}
159 tooltip={
160 <>
161 <span className="text-foreground">
162 {window.label}: {left}% left
163 </span>
164 {window.resetsAt && resetsAt > now ? (
165 <span className="text-muted-foreground">
166 Resets in {formatDuration(resetsAt - now)} ·{" "}
167 {formatUpcomingTimestamp(window.resetsAt, timestampFormat, now)}
168 </span>
169 ) : null}
170 {pace ? <span className="text-muted-foreground">{PACE_LABEL[pace]}</span> : null}
171 </>
172 }
173 />
174 );
176​
177/** One label, hairline, and value row; the whole row opens the details tooltip. */
178function UsageRow({
179 label,
180 fill,
181 tone,
182 tick = null,
183 value,
184 ariaValue = value,
185 tooltip,
186 muted = false,
187}: {
188 readonly label: string;
189 /** Bar fill from 0 to 100. Null hides the bar. */
190 readonly fill: number | null;
191 readonly tone: UsageTone;
192 /** A marker on the bar, from 0 to 100. */
193 readonly tick?: number | null;
194 readonly value: string;
195 readonly ariaValue?: string;
196 readonly tooltip: ReactNode;
197 readonly muted?: boolean;
198}) {
199 return (
200 <Tooltip>
201 <TooltipTrigger
202 render={
203 <div
204 tabIndex={0}
205 aria-label={`${label}: ${ariaValue}`}
206 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]"
207 />
208 }
209 >
210 <span className="max-w-28 truncate text-muted-foreground">{label}</span>
211 {fill !== null ? (
212 <span className="relative h-[3px] rounded-full bg-muted-foreground/30 dark:bg-muted-foreground/20">
213 {fill > 0 ? (
214 <span
215 className={cn(
216 "absolute inset-y-0 left-0 rounded-full",
217 tone === "error"
218 ? "bg-error"
219 : tone === "warning"
220 ? "bg-warning"
221 : muted
222 ? "bg-muted-foreground"
223 : "bg-foreground/80",
224 )}
225 style={{ width: `max(${fill}%, 6px)` }}
226 />
227 ) : null}
228 {tick !== null ? (
229 <span
230 aria-hidden
231 className="absolute -top-[3px] h-[9px] w-px -translate-x-1/2 bg-foreground/60"
232 style={{ left: `${tick}%` }}
233 />
234 ) : null}
235 </span>
236 ) : null}
237 <span
238 className={cn(
239 // Text-only rows span the bar column so they never widen the shared value column.
240 "text-end font-medium tabular-nums",
241 fill === null && "col-span-2",
242 tone === "error"
243 ? "text-error-foreground"
244 : tone === "warning"
245 ? "text-warning-foreground"
246 : fill === null
247 ? "text-muted-foreground"
248 : "text-foreground/80",
249 )}
250 >
251 {value}
252 </span>
253 </TooltipTrigger>
254 {/* Rows sit 8px inside the card; clear its edge instead of overlapping it. */}
255 <TooltipPopup side="left" sideOffset={16}>
256 <div className="flex flex-col gap-0.5">{tooltip}</div>
257 </TooltipPopup>
258 </Tooltip>
259 );