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

ThreadDetailsUsage.logic.ts

133 lines4.7 KB

1import {
2 EnvironmentId,
3 type ProviderInstanceId,
4 type ServerProvider,
5 type ServerProviderUsageLimits,
6 type UsageLimitSourceSnapshots,
7} from "@t3tools/contracts";
8import {
9 antigravityWindowLimitsModel,
10 collectLimitAccounts,
11 collectLimitPools,
12 collectProviderUsageLimits,
13 displayLimitWindows,
14 hubRoutes,
15 instanceHubs,
16 type LimitPoolWindow,
17} from "@t3tools/shared/usageLimits";
18import { limitsNotice } from "@t3tools/shared/usageLimitsCopy";
19​
20export type ThreadDetailsLimits =
21 | { readonly kind: "instance"; readonly limits: ServerProviderUsageLimits }
22 | { readonly kind: "pool"; readonly windows: readonly LimitPoolWindow[] };
23​
24// collectLimitAccounts keys its input by environment; the card reads one, so any id serves.
25const CARD_ENVIRONMENT = EnvironmentId.make("thread-details");
26​
27export function resolveThreadDetailsLimits(
28 providerInstanceId: ProviderInstanceId | null,
29 model: string | null,
30 providers: readonly ServerProvider[],
31 sources: UsageLimitSourceSnapshots,
32 now: number,
33): ThreadDetailsLimits | null {
34 const provider = providers.find((entry) => entry.instanceId === providerInstanceId);
35 if (!provider) return null;
36 const report = collectProviderUsageLimits(provider.instanceId, providers, sources, now, model);
37 // Only this instance's data: the report keeps one instance per login, and any other entry may be
38 // a different login or workspace, so fall back to the instance's own snapshot.
39 const own =
40 report?.accounts.find((entry) => entry.instanceId === provider.instanceId)?.limits ??
41 provider.usageLimits;
42 if (!own) return null;
43 const hubs = instanceHubs(provider, sources);
44 if (limitsNotice(own) === null || hubs.length === 0) return { kind: "instance", limits: own };
45 // An instance sent to a hub reads no limits of its own; its hub pools the accounts it spends.
46 return hubPool(provider.driver, model, providers, hubs, now) ?? { kind: "instance", limits: own };
48​
49export function resolveThreadHttpQuotas(
50 providerInstanceId: ProviderInstanceId | null,
51 sources: UsageLimitSourceSnapshots,
52) {
53 if (providerInstanceId === null) return [];
54 return sources.flatMap((source) =>
55 source.kind === "http"
56 ? (source.quotaAccounts ?? []).flatMap((account) =>
57 account.providerInstanceId === providerInstanceId
58 ? account.windows.map((window) => ({
59 key: JSON.stringify([source.id, account.id, window.id]),
60 accountLabel: account.label,
61 window,
62 }))
63 : [],
64 )
65 : [],
66 );
68​
69export function nextPoolReset(window: LimitPoolWindow, now: number) {
70 const pooled = window.members.length > 1;
71 return (
72 window.resets.find((reset) => reset.at > now && (!pooled || reset.restoresPercent > 0)) ?? null
73 );
75​
76function hubPool(
77 driver: ServerProvider["driver"],
78 model: string | null,
79 providers: readonly ServerProvider[],
80 sources: UsageLimitSourceSnapshots,
81 now: number,
82): ThreadDetailsLimits | null {
83 const routes = hubRoutes(sources, driver, model);
84 const routed = sources.map((source) => ({
85 ...source,
86 accounts: source.accounts.filter(routes).map((account) =>
87 account.driver === "antigravity"
88 ? {
89 ...account,
90 usageLimits: {
91 ...account.usageLimits,
92 windows: account.usageLimits.windows.filter((window) =>
93 antigravityWindowLimitsModel(window, model),
94 ),
95 },
96 }
97 : account,
98 ),
99 }));
100 const readable = routed
101 .flatMap((source) => source.accounts)
102 .filter((account) => limitsNotice(account.usageLimits) === null);
103 if (readable.length === 0) return null;
104 // A signed-in login joins the pool only as a fresher reading of an account the hub routes to.
105 const signedIn = providers.filter((provider) =>
106 readable.some(
107 (account) =>
108 account.driver === provider.driver &&
109 account.workspaceId === provider.auth.workspaceId &&
110 account.email !== undefined &&
111 account.email.toLowerCase() === provider.auth.email?.toLowerCase(),
112 ),
113 );
114 const presentations = new Map([
115 [
116 CARD_ENVIRONMENT,
117 {
118 entry: { target: { label: "" } },
119 serverConfig: { providers: signedIn, usageLimitSources: routed },
120 },
121 ],
122 ]);
123 const pools = collectLimitPools(collectLimitAccounts(presentations), now);
124 return pools.length > 0 ? { kind: "pool", windows: pools.flatMap(displayLimitWindows) } : null;
126​
127export function poolSources(window: LimitPoolWindow) {
128 const hubs = window.members.flatMap(({ account }) =>
129 account.environments.length === 0 && account.sourceLabel ? [account.sourceLabel] : [],
130 );
131 const signedIn = window.members.some(({ account }) => account.environments.length > 0);
132 return { hubs: [...new Set(hubs)], signedIn };