The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 3 | import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime"; |
| 4 | import { |
| 5 | isAtomCommandInterrupted, |
| 6 | squashAtomCommandFailure, |
| 7 | } from "@t3tools/client-runtime/state/runtime"; |
| 8 | import { msg } from "gt-react-native"; |
| 9 | |
| 10 | import { m } from "../../i18n/messages"; |
| 11 | |
| 12 | type RefreshProvidersTarget = { |
| 13 | readonly environmentId: EnvironmentId; |
| 14 | readonly input: { readonly refreshModels: true }; |
| 15 | }; |
| 16 | |
| 17 | export function createProviderCatalogRefreshRunner<Result>( |
| 18 | refreshProviders: (target: RefreshProvidersTarget) => Promise<Result>, |
| 19 | ) { |
| 20 | let pending: Promise<Result> | null = null; |
| 21 | |
| 22 | return (environmentId: EnvironmentId): Promise<Result> => { |
| 23 | if (pending) return pending; |
| 24 | pending = refreshProviders({ environmentId, input: { refreshModels: true } }).finally(() => { |
| 25 | pending = null; |
| 26 | }); |
| 27 | return pending; |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | export function providerCatalogRefreshError( |
| 32 | result: AtomCommandResult<unknown, unknown>, |
| 33 | ): string | null { |
| 34 | if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return null; |
| 35 | const error = squashAtomCommandFailure(result); |
| 36 | return errorDisplayMessage(error, m) ?? m(msg("Provider discovery failed.")); |
| 37 | } |