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