Open History to see the last commit
1import {
2 type ProviderInstanceId,
3 textGeneratorForSelection,
4 type UnifiedSettings,
5} from "@t3tools/contracts";
6import { useGT } from "gt-react";
7
8import { mergeEnvironmentSettings } from "../../hooks/useSettings";
9import {
10 getCustomModelOptionsByInstance,
11 getTextGenerationModelOptionsByInstance,
12} from "../../modelSelection";
13import {
14 applyProviderInstanceSettings,
15 deriveProviderInstanceEntries,
16 type ProviderInstanceEntry,
17} from "../../providerInstances";
18import { useEnvironmentProviders, useEnvironments } from "../../state/environments";
19import { useSettingsScope } from "./SettingsScopeContext";
20
21export 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`This model is unavailable on ${{ environment: environment.label }}. Select that environment to choose its model separately.`
39 : gt`This model is unavailable on a selected environment. Select that environment to choose its model separately.`;
40 const generator = textGeneratorForSelection(candidate.settings, instanceId);
41 if (shownGenerator !== undefined || generator !== undefined) {
42 // Only the shown environment's model list is loaded; elsewhere it must reach the same API.
43 if (
44 !generator?.enabled ||
45 generator.kind !== shownGenerator?.kind ||
46 generator.baseUrl !== shownGenerator.baseUrl
47 ) {
48 return unavailable;
49 }
50 continue;
51 }
52 const providers = providersByEnvironment.get(candidate.environmentId);
53 if (!environment?.serverConfig || !providers) continue;
54 const entry = applyProviderInstanceSettings(
55 deriveProviderInstanceEntries(providers),
56 candidate.settings,
57 candidate.projectId,
58 ).find((option) => option.instanceId === instanceId);
59 const optionsForInstance = textGeneration
60 ? getTextGenerationModelOptionsByInstance
61 : getCustomModelOptionsByInstance;
62 const options = optionsForInstance(
63 mergeEnvironmentSettings(candidate.settings, settings),
64 providers,
65 ).get(instanceId);
66 if (
67 !entry?.enabled ||
68 !entry.isAvailable ||
69 entry.driverKind !== sourceEntry?.driverKind ||
70 !options?.some((option) => option.slug === model && !option.isUnavailable)
71 ) {
72 return unavailable;
73 }
74 }
75 return null;
76 };
77}