The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { runAtomCommand } from "@t3tools/client-runtime/state/runtime"; |
| 3 | import { refreshUsageLimits } from "@t3tools/client-runtime/state/usage"; |
| 4 | import { Atom, type AtomRegistry } from "effect/reactivity"; |
| 5 | |
| 6 | import { serverEnvironment } from "../../state/server"; |
| 7 | |
| 8 | /** |
| 9 | * Whether the last finished limits refresh for an environment failed. Not kept |
| 10 | * alive: the limits view mounts it, and leaving the view forgets old failures. |
| 11 | */ |
| 12 | const limitsRefreshFailedAtom = Atom.family((environmentId: EnvironmentId) => |
| 13 | Atom.make(false).pipe(Atom.withLabel(`mobile:limits-refresh-failed:${environmentId}`)), |
| 14 | ); |
| 15 | |
| 16 | export async function refreshEnvironmentLimits( |
| 17 | registry: AtomRegistry.AtomRegistry, |
| 18 | environmentId: EnvironmentId, |
| 19 | options: { readonly automatic?: boolean } = {}, |
| 20 | ): Promise<void> { |
| 21 | // One refresh runs per environment at a time, so results land in the order |
| 22 | // they were started and the newest outcome always wins. |
| 23 | const result = await refreshUsageLimits( |
| 24 | environmentId, |
| 25 | () => |
| 26 | runAtomCommand( |
| 27 | registry, |
| 28 | serverEnvironment.refreshProviders, |
| 29 | { environmentId, input: {} }, |
| 30 | { reportFailure: false }, |
| 31 | ), |
| 32 | options.automatic, |
| 33 | ); |
| 34 | if (result === undefined) return; |
| 35 | registry.set(limitsRefreshFailedAtom(environmentId), result._tag === "Failure"); |
| 36 | } |
| 37 | |
| 38 | // Mounting starts an automatic refresh, which refreshUsageLimits throttles to |
| 39 | // one every few minutes per environment. |
| 40 | const limitsAutoRefreshAtom = Atom.family((environmentId: EnvironmentId) => |
| 41 | Atom.make((get) => { |
| 42 | void refreshEnvironmentLimits(get.registry, environmentId, { automatic: true }); |
| 43 | }).pipe(Atom.withLabel(`mobile:limits-auto-refresh:${environmentId}`)), |
| 44 | ); |
| 45 | |
| 46 | export function environmentIdsKey(environmentIds: Iterable<EnvironmentId>): string { |
| 47 | return [...environmentIds].sort().join(","); |
| 48 | } |
| 49 | |
| 50 | function splitEnvironmentIds(key: string): EnvironmentId[] { |
| 51 | return key === "" ? [] : key.split(",").map((id) => EnvironmentId.make(id)); |
| 52 | } |
| 53 | |
| 54 | /** Keyed by a sorted, comma-joined id list so equal selections share one atom. */ |
| 55 | export const limitsAutoRefreshSetAtom = Atom.family((environmentIds: string) => |
| 56 | Atom.make((get) => { |
| 57 | for (const environmentId of splitEnvironmentIds(environmentIds)) { |
| 58 | get(limitsAutoRefreshAtom(environmentId)); |
| 59 | } |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | export const limitsRefreshFailuresAtom = Atom.family((environmentIds: string) => |
| 64 | Atom.make((get) => |
| 65 | splitEnvironmentIds(environmentIds).filter((environmentId) => |
| 66 | get(limitsRefreshFailedAtom(environmentId)), |
| 67 | ), |
| 68 | ), |
| 69 | ); |