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

ThreadDetailsUsage.logic.ts

120 lines4.4 KB

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