Open History to see the last commit
1import type {
2 EnvironmentId,
3 ProviderInstanceId,
4 ServerProvider,
5 UsageLimitSourceSnapshots,
6} from "@t3tools/contracts";
7import { collectProviderUsageLimits, hasProviderUsageLimits } from "@t3tools/shared/usageLimits";
8import { useState } from "react";
9import { useStableCallback } from "../../hooks/useStableCallback";
10import { toastManager } from "../ui/toast";
11import { usageLimitsBannerItem } from "./ComposerUsageLimits";
12
13/**
14 * The open /usage-limits panel for one thread, model and turn. Only the open
15 * moment is stored: the rows read live provider data, so a redeemed reset
16 * credit or refreshed probe shows through. Anything that spends quota closes
17 * it: a new turn from any source, or the agent resuming after an approval or
18 * answered question.
19 *
20 * `panelKey` names that thread, model and turn. It is null while the provider
21 * list or the thread itself is unavailable, such as during a reconnect; the
22 * panel then stays hidden rather than being dropped.
23 */
24export function useUsageLimitsPanel(input: {
25 readonly environmentId: EnvironmentId;
26 readonly threadKey: string;
27 readonly panelKey: string | null;
28 readonly providerInstanceId: ProviderInstanceId | null;
29 readonly providerDriver: ServerProvider["driver"] | null;
30 readonly providers: readonly ServerProvider[];
31 readonly sources: UsageLimitSourceSnapshots;
32}) {
33 const {
34 environmentId,
35 threadKey,
36 panelKey,
37 providerInstanceId,
38 providerDriver,
39 providers,
40 sources,
41 } = input;
42 const [panel, setPanel] = useState<{
43 readonly key: string;
44 readonly threadKey: string;
45 readonly now: number;
46 } | null>(null);
47 // Drop the snapshot as soon as the thread or model changes so it cannot resurface stale.
48 if (panel !== null && panelKey !== null && panel.key !== panelKey) {
49 setPanel(null);
50 }
51 const report =
52 panel !== null && panelKey !== null && panel.key === panelKey && providerInstanceId !== null
53 ? collectProviderUsageLimits(providerInstanceId, providers, sources, panel.now)
54 : null;
55 const banner =
56 report !== null && panel !== null
57 ? // A fresh id per opening: the stack keeps the last dismissed id as "exiting".
58 usageLimitsBannerItem(`usage-limits:${panel.key}:${panel.now}`, report, environmentId, () =>
59 setPanel(null),
60 )
61 : null;
62 // T3 owns /usage-limits only where Limits has data for the selected provider;
63 // elsewhere the name stays the provider's own and is sent through untouched.
64 const offered =
65 providerDriver !== null && hasProviderUsageLimits(providerDriver, providers, sources);
66
67 // Answered locally from the last Limits snapshot; the agent never sees it.
68 const open = useStableCallback(() => {
69 const now = Date.now();
70 const nextReport =
71 providerInstanceId !== null && panelKey !== null
72 ? collectProviderUsageLimits(providerInstanceId, providers, sources, now)
73 : null;
74 if (nextReport && panelKey !== null) {
75 setPanel({ key: panelKey, threadKey, now });
76 return true;
77 }
78 setPanel(null);
79 toastManager.add({ type: "info", title: "Usage limits are unavailable for this provider" });
80 return false;
81 });
82 // Responses can resolve after navigating away; only the originating thread's panel clears.
83 const clearFor = useStableCallback((originThreadKey: string) =>
84 setPanel((current) =>
85 current !== null && current.threadKey === originThreadKey ? null : current,
86 ),
87 );
88
89 return { banner, offered, open, clearFor };
90}