The best way to control Claude Code, Codex and more from any computer or phone

main
ts 82 lines 3.06 KB
Raw
1 import {
2 type ProviderInstanceId,
3 textGeneratorForSelection,
4 type UnifiedSettings,
5 } from "@t3tools/contracts";
6 import { useGT } from "gt-react";
7
8 import { mergeEnvironmentSettings } from "../../hooks/useSettings";
9 import {
10 getCustomModelOptionsByInstance,
11 getTextGenerationModelOptionsByInstance,
12 } from "../../modelSelection";
13 import {
14 applyProviderInstanceSettings,
15 deriveProviderInstanceEntries,
16 type ProviderInstanceEntry,
17 } from "../../providerInstances";
18 import { useEnvironmentProviders, useEnvironments } from "../../state/environments";
19 import { useSettingsScope } from "./SettingsScopeContext";
20
21 export function useScopedModelDisabledReason(
22 settings: UnifiedSettings,
23 entries: readonly ProviderInstanceEntry[],
24 textGeneration = false,
25 ) {
26 const gt = useGT();
27 const { targets } = useSettingsScope();
28 const { environments } = useEnvironments();
29 const providersByEnvironment = useEnvironmentProviders();
30 return (instanceId: ProviderInstanceId, model: string): string | null => {
31 const sourceEntry = entries.find((entry) => entry.instanceId === instanceId);
32 const shownGenerator = textGeneratorForSelection(settings, instanceId);
33 for (const candidate of targets) {
34 const environment = environments.find(
35 (entry) => entry.environmentId === candidate.environmentId,
36 );
37 const unavailable = environment
38 ? gt(
39 "This model is unavailable on {environment}. Select that environment to choose its model separately.",
40 { environment: environment.label },
41 )
42 : gt(
43 "This model is unavailable on a selected environment. Select that environment to choose its model separately.",
44 );
45 const generator = textGeneratorForSelection(candidate.settings, instanceId);
46 if (shownGenerator !== undefined || generator !== undefined) {
47 // Only the shown environment's model list is loaded; elsewhere it must reach the same API.
48 if (
49 !generator?.enabled ||
50 generator.kind !== shownGenerator?.kind ||
51 generator.baseUrl !== shownGenerator.baseUrl
52 ) {
53 return unavailable;
54 }
55 continue;
56 }
57 const providers = providersByEnvironment.get(candidate.environmentId);
58 if (!environment?.serverConfig || !providers) continue;
59 const entry = applyProviderInstanceSettings(
60 deriveProviderInstanceEntries(providers),
61 candidate.settings,
62 candidate.projectId,
63 ).find((option) => option.instanceId === instanceId);
64 const optionsForInstance = textGeneration
65 ? getTextGenerationModelOptionsByInstance
66 : getCustomModelOptionsByInstance;
67 const options = optionsForInstance(
68 mergeEnvironmentSettings(candidate.settings, settings),
69 providers,
70 ).get(instanceId);
71 if (
72 !entry?.enabled ||
73 !entry.isAvailable ||
74 entry.driverKind !== sourceEntry?.driverKind ||
75 !options?.some((option) => option.slug === model && !option.isUnavailable)
76 ) {
77 return unavailable;
78 }
79 }
80 return null;
81 };
82 }