Open History to see the last commit
1import type { EnvironmentId, UsageLimitsReport } from "@t3tools/contracts";
2import type { TimestampFormat } from "@t3tools/contracts/settings";
3import { limitsNotice } from "@t3tools/shared/usageLimits";
4import { GaugeIcon } from "lucide-react";
5
6import { ensureLocalApi } from "../../localApi";
7import { formatUpcomingTimestamp } from "../../timestampFormat";
8import { Button } from "../ui/button";
9import { providerClients } from "../settings/providerDriverMeta";
10import { RedactedSensitiveText } from "../settings/RedactedSensitiveText";
11import { LimitWindows, ResetCredits } from "../usage/UsageLimits";
12import { ComposerBanner } from "./ComposerBanner";
13import type { ComposerBannerStackItem } from "./ComposerBannerStack";
14
15function accountLabel(account: UsageLimitsReport["accounts"][number]): string {
16 if (!account.instanceId) return account.label;
17 const driver = providerClients.get(account.driver)?.label ?? String(account.driver);
18 const instance =
19 account.displayName?.trim() ||
20 (String(account.instanceId) !== String(account.driver) ? account.instanceId : "");
21 return instance && instance.toLowerCase() !== driver.toLowerCase()
22 ? `${driver} · ${instance}`
23 : driver;
24}
25
26function AccountSummary({ account }: { readonly account: UsageLimitsReport["accounts"][number] }) {
27 const label = accountLabel(account);
28 return (
29 <>
30 {label.includes("@") ? (
31 <RedactedSensitiveText
32 key={label}
33 value={label}
34 ariaLabel="Toggle account label visibility"
35 revealTooltip="Click to reveal account"
36 hideTooltip="Click to hide account"
37 className="max-w-full truncate align-bottom font-sans text-xs leading-normal"
38 />
39 ) : (
40 label
41 )}
42 {account.plan ? ` · ${account.plan}` : null}
43 </>
44 );
45}
46
47export function usageLimitsBannerItem(
48 id: string,
49 report: UsageLimitsReport,
50 environmentId: EnvironmentId,
51 onDismiss: () => void,
52): ComposerBannerStackItem {
53 const [first] = report.accounts;
54 const single = report.accounts.length === 1 && first ? first : null;
55 const summary = single ? (
56 <AccountSummary account={single} />
57 ) : (
58 `${report.accounts.length} accounts`
59 );
60 return {
61 id,
62 variant: "info",
63 icon: <GaugeIcon />,
64 title: "Usage limits",
65 description: summary,
66 dismissLabel: "Dismiss usage limits",
67 onDismiss,
68 children: <UsageLimitsBannerBody report={report} environmentId={environmentId} />,
69 };
70}
71
72export function modelFallbackBannerItem({
73 fallbackModelName,
74 defaultProviderName,
75 defaultModelName,
76 resetsAt,
77 timestampFormat,
78 onUseDefault,
79 onDismiss,
80}: {
81 readonly fallbackModelName: string;
82 readonly defaultProviderName: string;
83 readonly defaultModelName: string;
84 readonly resetsAt: string | null;
85 readonly timestampFormat: TimestampFormat;
86 readonly onUseDefault: () => void;
87 readonly onDismiss: () => void;
88}): ComposerBannerStackItem {
89 return {
90 id: "fallback-model",
91 variant: "info",
92 icon: <GaugeIcon />,
93 title: `Using ${fallbackModelName}`,
94 description: `${defaultProviderName} is out of usage${resetsAt ? ` · resets ${formatUpcomingTimestamp(resetsAt, timestampFormat)}` : ""}`,
95 actions: (
96 <Button size="xs" variant="ghost" onClick={onUseDefault}>
97 Use {defaultModelName}
98 </Button>
99 ),
100 dismissLabel: "Dismiss fallback model notice",
101 onDismiss,
102 };
103}
104
105function UsageLimitsBannerBody({
106 report,
107 environmentId,
108}: {
109 readonly report: UsageLimitsReport;
110 readonly environmentId: EnvironmentId;
111}) {
112 const now = Date.parse(report.createdAt);
113 return (
114 <ComposerBanner.Scroll>
115 <ComposerBanner.Body className="flex flex-col gap-2 pt-1 pb-1.5 pe-2">
116 {report.accounts.map((account) => {
117 const resetCreditInput =
118 account.resetCreditInput ??
119 (account.instanceId ? { instanceId: account.instanceId } : undefined);
120 const notice = limitsNotice(account.limits);
121 const externalUsage = account.limits.externalUsage;
122 return (
123 <div key={account.id} className="flex min-w-0 flex-col gap-1">
124 {report.accounts.length > 1 ? (
125 <span className="truncate text-xs text-muted-foreground">
126 <AccountSummary account={account} />
127 </span>
128 ) : null}
129 {notice ? (
130 <span className="text-xs text-muted-foreground">{notice}</span>
131 ) : (
132 <LimitWindows
133 compact
134 driver={account.driver}
135 accentColor={account.accentColor}
136 windows={account.limits.windows}
137 now={now}
138 />
139 )}
140 {externalUsage ? (
141 <Button
142 size="sm"
143 variant="link"
144 className="self-start"
145 onClick={() => void ensureLocalApi().shell.openExternal(externalUsage.url)}
146 >
147 Manage usage
148 </Button>
149 ) : null}
150 {resetCreditInput && account.limits.resetCredits ? (
151 <ResetCredits
152 environmentId={environmentId}
153 input={resetCreditInput}
154 credits={account.limits.resetCredits}
155 now={now}
156 />
157 ) : null}
158 </div>
159 );
160 })}
161 {report.notices.map((notice) => (
162 <span key={notice} className="text-xs text-muted-foreground">
163 {notice}
164 </span>
165 ))}
166 </ComposerBanner.Body>
167 </ComposerBanner.Scroll>
168 );
169}