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