Open History to see the last commit
1import type {
2 ModelCapabilities,
3 ModelSelection,
4 ServerConfig as T3ServerConfig,
5} from "@t3tools/contracts";
6import {
7 buildExplicitProviderOptionSelectionsFromDescriptors,
8 getProviderOptionDescriptors,
9} from "@t3tools/shared/model";
10
11export type ModelOption = {
12 readonly key: string;
13 readonly label: string;
14 readonly subtitle: string;
15 readonly providerKey: string;
16 readonly providerLabel: string;
17 readonly providerDriver: string;
18 readonly isDefault: boolean;
19 readonly isLegacy: boolean;
20 readonly isUnavailable?: boolean;
21 readonly capabilities: ModelCapabilities | null;
22 readonly selection: ModelSelection;
23};
24
25export type ProviderGroup = {
26 readonly providerKey: string;
27 readonly providerLabel: string;
28 readonly models: ReadonlyArray<ModelOption>;
29};
30
31function providerDisplayLabel(provider: {
32 readonly displayName?: string | undefined;
33 readonly driver: string;
34 readonly instanceId: string;
35}): string {
36 if (provider.displayName) return provider.displayName;
37 if (provider.driver === "codex") return "Codex";
38 if (provider.driver === "claudeAgent") return "Claude";
39 return provider.instanceId;
40}
41
42function normalizeSelectionOptions(
43 selection: ModelSelection,
44 capabilities: ModelCapabilities | null,
45): ModelSelection {
46 if (!capabilities) {
47 return selection;
48 }
49 const options = buildExplicitProviderOptionSelectionsFromDescriptors(
50 getProviderOptionDescriptors({
51 caps: capabilities,
52 selections: selection.options,
53 }),
54 selection.options,
55 );
56 return options
57 ? { ...selection, options }
58 : {
59 instanceId: selection.instanceId,
60 model: selection.model,
61 };
62}
63
64/** Whether a known Antigravity selection needs setup or a different model. */
65export function isModelSelectionUnavailable(
66 config: T3ServerConfig | null | undefined,
67 selection: ModelSelection | null | undefined,
68): boolean {
69 if (!config || !selection) {
70 return false;
71 }
72 const provider = config.providers.find(
73 (candidate) => candidate.instanceId === selection.instanceId,
74 );
75 const driver =
76 provider?.driver ?? config.settings?.providerInstances[selection.instanceId]?.driver;
77 return (
78 driver === "antigravity" &&
79 (!provider ||
80 !provider.enabled ||
81 !provider.installed ||
82 provider.auth.status === "unauthenticated" ||
83 provider.availability === "unavailable" ||
84 !provider.models.some((model) => model.slug === selection.model))
85 );
86}
87
88/**
89 * Keep Antigravity selections when setup or catalog changes make them
90 * unavailable. Other providers fall through to the server default when they
91 * are disabled, missing, or signed out. Without config, keep stored selections.
92 */
93export function resolveSelectableModelSelection(
94 config: T3ServerConfig | null | undefined,
95 selection: ModelSelection | null,
96): ModelSelection | null {
97 if (!selection || !config) {
98 return selection;
99 }
100 const provider = config.providers.find(
101 (candidate) => candidate.instanceId === selection.instanceId,
102 );
103 const driver =
104 provider?.driver ?? config.settings?.providerInstances[selection.instanceId]?.driver;
105 if (driver === "antigravity") {
106 return selection;
107 }
108 return provider &&
109 provider.enabled &&
110 provider.installed &&
111 provider.auth.status !== "unauthenticated"
112 ? selection
113 : null;
114}
115
116/**
117 * Reject legacy models for implicit defaults, except Antigravity selections,
118 * which must not silently change after a catalog update. Explicit picks in
119 * the settings sheet are unaffected.
120 */
121export function resolveDefaultableModelSelection(
122 config: T3ServerConfig | null | undefined,
123 selection: ModelSelection | null,
124): ModelSelection | null {
125 const usable = resolveSelectableModelSelection(config, selection);
126 if (!usable || !config) {
127 return usable;
128 }
129 const provider = config.providers.find((candidate) => candidate.instanceId === usable.instanceId);
130 const model = provider?.models.find((candidate) => candidate.slug === usable.model);
131 return provider?.driver !== "antigravity" && model?.isLegacy === true ? null : usable;
132}
133
134export function resolveNewTaskModelSelection(input: {
135 readonly draftSelection: ModelSelection | null;
136 readonly projectDefaultSelection: ModelSelection | null;
137 readonly stickySelection: ModelSelection | null;
138 readonly modelOptions: ReadonlyArray<ModelOption>;
139}): ModelSelection | null {
140 return (
141 input.draftSelection ??
142 input.projectDefaultSelection ??
143 input.stickySelection ??
144 input.modelOptions.find((option) => option.isDefault && !option.isUnavailable)?.selection ??
145 input.modelOptions.find((option) => !option.isUnavailable)?.selection ??
146 null
147 );
148}
149
150export function buildModelOptions(
151 config: T3ServerConfig | null | undefined,
152 fallbackModelSelection: ModelSelection | null,
153): ReadonlyArray<ModelOption> {
154 const options = new Map<string, ModelOption>();
155
156 for (const provider of config?.providers ?? []) {
157 if (
158 !provider.enabled ||
159 !provider.installed ||
160 provider.auth.status === "unauthenticated" ||
161 (provider.driver === "antigravity" && provider.availability === "unavailable")
162 ) {
163 continue;
164 }
165
166 const providerLabel = providerDisplayLabel(provider);
167 for (const model of provider.models) {
168 const key = `${provider.instanceId}:${model.slug}`;
169 options.set(key, {
170 key,
171 label: model.name,
172 subtitle: model.subProvider ?? "",
173 providerKey: provider.instanceId,
174 providerLabel,
175 providerDriver: provider.driver,
176 isDefault: model.isDefault === true,
177 isLegacy: model.isLegacy === true,
178 capabilities: model.capabilities,
179 selection: normalizeSelectionOptions(
180 {
181 instanceId: provider.instanceId,
182 model: model.slug,
183 },
184 model.capabilities,
185 ),
186 });
187 }
188 }
189
190 if (fallbackModelSelection) {
191 const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
192 const existing = options.get(key);
193 if (existing) {
194 options.set(key, {
195 ...existing,
196 selection:
197 existing.providerDriver === "antigravity"
198 ? fallbackModelSelection
199 : normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
200 });
201 } else {
202 const provider = config?.providers.find(
203 (candidate) => candidate.instanceId === fallbackModelSelection.instanceId,
204 );
205 const instanceConfig = config?.settings?.providerInstances[fallbackModelSelection.instanceId];
206 const model = provider?.models.find(
207 (candidate) => candidate.slug === fallbackModelSelection.model,
208 );
209 const providerDriver =
210 provider?.driver ?? instanceConfig?.driver ?? fallbackModelSelection.instanceId;
211 const providerLabel = providerDisplayLabel({
212 driver: providerDriver,
213 displayName: provider?.displayName ?? instanceConfig?.displayName,
214 instanceId: fallbackModelSelection.instanceId,
215 });
216 options.set(key, {
217 key,
218 label: model?.name ?? fallbackModelSelection.model,
219 subtitle: model?.subProvider ?? "",
220 providerKey: fallbackModelSelection.instanceId,
221 providerLabel,
222 providerDriver,
223 isDefault: false,
224 isLegacy: model?.isLegacy === true,
225 ...(isModelSelectionUnavailable(config, fallbackModelSelection)
226 ? { isUnavailable: true }
227 : {}),
228 capabilities: model?.capabilities ?? null,
229 selection: fallbackModelSelection,
230 });
231 }
232 }
233
234 return [...options.values()];
235}
236
237export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
238 const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
239 for (const option of options) {
240 const existing = groups.get(option.providerKey);
241 if (existing) {
242 existing.models.push(option);
243 } else {
244 groups.set(option.providerKey, {
245 providerLabel: option.providerLabel,
246 models: [option],
247 });
248 }
249 }
250
251 return [...groups.entries()].map(([providerKey, group]) => ({
252 providerKey,
253 providerLabel: group.providerLabel,
254 models: group.models,
255 }));
256}