|
| 1 | +import type { |
| 2 | + ProviderInstanceId, |
| 3 | + ServerProvider, |
| 4 | + ServerProviderUsageWindow, |
| 5 | + UsageLimitSourceSnapshots, |
| 6 | +} from "@t3tools/contracts"; |
| 7 | +import { |
| 8 | + collectProviderUsageLimits, |
| 9 | + formatDuration, |
| 10 | + type LimitPace, |
| 11 | + limitsNotice, |
| 12 | + paceOf, |
| 13 | + remainingPercent, |
| 14 | +} from "@t3tools/shared/usageLimits"; |
| 15 | +import type { ReactNode } from "react"; |
| 16 | + |
| 17 | +import { usePrimarySettings } from "../../hooks/useSettings"; |
| 18 | +import { useNowMinute } from "../../hooks/useNowMinute"; |
| 19 | +import { type ContextWindowSnapshot, formatContextWindowTokens } from "../../lib/contextWindow"; |
| 20 | +import { cn } from "../../lib/utils"; |
| 21 | +import { formatUpcomingTimestamp } from "../../timestampFormat"; |
| 22 | +import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 23 | +import { ThreadDetailsSection } from "./ThreadDetailsSection"; |
| 24 | + |
| 25 | +const PACE_LABEL: Record<LimitPace, string> = { |
| 26 | + ahead: "Ahead of pace", |
| 27 | + on: "On pace", |
| 28 | + under: "Under pace", |
| 29 | +}; |
| 30 | + |
| 31 | +export interface ThreadDetailsUsageInput { |
| 32 | + readonly providerInstanceId: ProviderInstanceId | null; |
| 33 | + readonly providers: readonly ServerProvider[]; |
| 34 | + readonly sources: UsageLimitSourceSnapshots; |
| 35 | + readonly contextWindow: ContextWindowSnapshot | null; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Labs readout of 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 | + */ |
| 42 | +export function ThreadDetailsUsage({ |
| 43 | + providerInstanceId, |
| 44 | + providers, |
| 45 | + sources, |
| 46 | + contextWindow, |
| 47 | +}: ThreadDetailsUsageInput) { |
| 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 | + const notice = limits ? limitsNotice(limits) : null; |
| 60 | + if (!limits && !contextWindow) return null; |
| 61 | + |
| 62 | + return ( |
| 63 | + <ThreadDetailsSection |
| 64 | + headingId="thread-details-usage-heading" |
| 65 | + title="Usage" |
| 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 | + left={null} |
| 73 | + value="Unavailable" |
| 74 | + tooltip={<span className="text-foreground">{notice}</span>} |
| 75 | + /> |
| 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} |
| 81 | + </div> |
| 82 | + </ThreadDetailsSection> |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +function LimitRow({ |
| 87 | + window, |
| 88 | + now, |
| 89 | +}: { |
| 90 | + readonly window: ServerProviderUsageWindow; |
| 91 | + readonly now: number; |
| 92 | +}) { |
| 93 | + const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat); |
| 94 | + const left = remainingPercent(window); |
| 95 | + const pace = paceOf(window, now); |
| 96 | + const resetsAt = window.resetsAt ? Date.parse(window.resetsAt) : Number.NaN; |
| 97 | + return ( |
| 98 | + <UsageRow |
| 99 | + label={window.label} |
| 100 | + left={left} |
| 101 | + tooltip={ |
| 102 | + <> |
| 103 | + <span className="text-foreground"> |
| 104 | + {window.label}: {left}% left |
| 105 | + </span> |
| 106 | + {window.resetsAt && resetsAt > now ? ( |
| 107 | + <span className="text-muted-foreground"> |
| 108 | + Resets in {formatDuration(resetsAt - now)} ·{" "} |
| 109 | + {formatUpcomingTimestamp(window.resetsAt, timestampFormat, now)} |
| 110 | + </span> |
| 111 | + ) : null} |
| 112 | + {pace ? <span className="text-muted-foreground">{PACE_LABEL[pace]}</span> : null} |
| 113 | + </> |
| 114 | + } |
| 115 | + /> |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 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 | + |
| 153 | +/** One label, hairline, and value row; the whole row opens the details tooltip. */ |
| 154 | +function UsageRow({ |
| 155 | + label, |
| 156 | + left, |
| 157 | + tooltip, |
| 158 | + value, |
| 159 | + muted = false, |
| 160 | + className, |
| 161 | +}: { |
| 162 | + readonly label: string; |
| 163 | + readonly left: number | null; |
| 164 | + readonly tooltip: ReactNode; |
| 165 | + /** Replaces the percentage when there is none. */ |
| 166 | + readonly value?: string | undefined; |
| 167 | + readonly muted?: boolean; |
| 168 | + readonly className?: string | undefined; |
| 169 | +}) { |
| 170 | + const tone = left === null ? null : left <= 10 ? "error" : left <= 20 ? "warning" : null; |
| 171 | + return ( |
| 172 | + <Tooltip> |
| 173 | + <TooltipTrigger |
| 174 | + render={ |
| 175 | + <div |
| 176 | + 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 | + )} |
| 182 | + /> |
| 183 | + } |
| 184 | + > |
| 185 | + <span className="max-w-28 truncate text-muted-foreground">{label}</span> |
| 186 | + {left !== null ? ( |
| 187 | + <span className="relative h-[3px] rounded-full bg-muted-foreground/30 dark:bg-muted-foreground/20"> |
| 188 | + {left > 0 ? ( |
| 189 | + <span |
| 190 | + className={cn( |
| 191 | + "absolute inset-y-0 left-0 rounded-full", |
| 192 | + tone === "error" |
| 193 | + ? "bg-error" |
| 194 | + : tone === "warning" |
| 195 | + ? "bg-warning" |
| 196 | + : muted |
| 197 | + ? "bg-muted-foreground" |
| 198 | + : "bg-foreground/80", |
| 199 | + )} |
| 200 | + style={{ width: `max(${left}%, 6px)` }} |
| 201 | + /> |
| 202 | + ) : null} |
| 203 | + </span> |
| 204 | + ) : null} |
| 205 | + <span |
| 206 | + className={cn( |
| 207 | + // Text-only rows span the bar column so they never widen the shared value column. |
| 208 | + "text-end font-medium tabular-nums", |
| 209 | + left === null && "col-span-2", |
| 210 | + tone === "error" |
| 211 | + ? "text-error-foreground" |
| 212 | + : tone === "warning" |
| 213 | + ? "text-warning-foreground" |
| 214 | + : left === null |
| 215 | + ? "text-muted-foreground" |
| 216 | + : "text-foreground/80", |
| 217 | + )} |
| 218 | + > |
| 219 | + {value ?? `${left}%`} |
| 220 | + </span> |
| 221 | + </TooltipTrigger> |
| 222 | + {/* Rows sit 8px inside the card; clear its edge instead of overlapping it. */} |
| 223 | + <TooltipPopup side="left" sideOffset={16}> |
| 224 | + <div className="flex flex-col gap-0.5">{tooltip}</div> |
| 225 | + </TooltipPopup> |
| 226 | + </Tooltip> |
| 227 | + ); |
| 228 | +} |
0 commit comments