Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

modelSelection.ts

425 lines14.0 KB

1import {
2 ANTIGRAVITY_DEFAULT_MODEL,
3 DEFAULT_TEXT_GENERATION_MODEL,
4 DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER,
5 defaultInstanceIdForDriver,
6 type ModelSelection,
7 type ProjectId,
8 ProviderDriverKind,
9 ProviderInstanceId,
10 type ServerProvider,
11 type ServerSettings,
12} from "@t3tools/contracts";
13import {
14 type CustomModelDefinition,
15 createModelSelection,
16 normalizeCustomModelSlug,
17 readCustomModelEntries,
18 resolveSelectableModel,
19} from "@t3tools/shared/model";
20import { getComposerProviderState } from "./components/chat/composerProviderState";
21import { type ClientSettings, UnifiedSettings } from "@t3tools/contracts/settings";
22import { resolveModelPreferences } from "@t3tools/shared/serverSettings";
23import * as Arr from "effect/Array";
24import * as Equal from "effect/Equal";
25import * as Result from "effect/Result";
26import {
27 getDefaultServerModel,
28 getProviderModels,
29 resolveSelectableProvider,
30} from "./providerModels";
31import { ModelEsque } from "./components/chat/providerIconUtils";
32import {
33 type ProviderInstanceEntry,
34 deriveProviderInstanceEntries,
35 NO_PROVIDER_MODEL_SELECTION,
36 resolveDefaultProviderModelSelection,
37 resolveProjectDefaultProviderModelSelection,
38} from "./providerInstances";
39import { sortModelsForProviderInstance } from "./modelOrdering";
40import { resolveModelShortName } from "@t3tools/client-runtime/provider-clients";
41​
42export const MAX_CUSTOM_MODEL_LENGTH = 256;
43const DEFAULT_TEXT_GENERATION_INSTANCE_ID = ProviderInstanceId.make("codex");
44​
45// Legacy per-driver buckets would leak custom-instance models into stock instances.
46export function readInstanceCustomModels(
47 settings: UnifiedSettings,
48 instanceId: ProviderInstanceId,
49 driverKind: ProviderDriverKind,
50): ReadonlyArray<CustomModelDefinition> {
51 if (driverKind === "antigravity") return [];
52 const instance = settings.providerInstances?.[instanceId];
53 const config = instance?.config;
54 if (config !== null && typeof config === "object") {
55 const value = (config as Record<string, unknown>).customModels;
56 if (Array.isArray(value)) {
57 return readCustomModelEntries(value);
58 }
59 }
60 return [];
62​
63export interface AppModelOption {
64 slug: string;
65 name: string;
66 shortName?: string;
67 subProvider?: string;
68 aliases?: ReadonlyArray<string>;
69 badge?: "new";
70 isCustom: boolean;
71 isDefault?: boolean;
72 isLegacy?: boolean;
73 isUnavailable?: boolean;
75​
76function appendUnavailableDynamicModelSelection(
77 options: AppModelOption[],
78 rawModels: ReadonlyArray<ServerProvider["models"][number]>,
79 provider: ProviderDriverKind,
80 selectedModel: string | null | undefined,
81 hiddenModels: ReadonlyArray<string>,
82): AppModelOption[] {
83 if (provider !== "opencode" && provider !== "antigravity") return options;
84 const slug = normalizeCustomModelSlug(selectedModel);
85 if (!slug) return options;
86 if (provider === "antigravity" && slug === ANTIGRAVITY_DEFAULT_MODEL) return options;
87​
88 if (resolveSelectableModel(provider, slug, rawModels) !== null) return options;
89 if (hiddenModels.includes(slug)) return options;
90 if (options.some((option) => option.slug === slug)) return options;
91​
92 return [...options, { slug, name: slug, isCustom: false, isUnavailable: true }];
94​
95function toAppModelOption(
96 model: ServerProvider["models"][number],
97 driverKind: ProviderDriverKind,
98): AppModelOption {
99 const option: AppModelOption = {
100 slug: model.slug,
101 name: model.name,
102 isCustom: model.isCustom,
103 };
104 const shortName = resolveModelShortName(model, driverKind);
105 if (shortName) option.shortName = shortName;
106 if (model.subProvider) option.subProvider = model.subProvider;
107 if (model.aliases) option.aliases = model.aliases;
108 if (model.badge) option.badge = model.badge;
109 if (model.isDefault) option.isDefault = true;
110 if (model.isLegacy) option.isLegacy = true;
111 return option;
113​
114function readInstanceModelPreferences(
115 settings: UnifiedSettings,
116 instanceId: ProviderInstanceId,
117): { readonly hiddenModels: ReadonlyArray<string>; readonly modelOrder: ReadonlyArray<string> } {
118 return (
119 settings.providerModelPreferences?.[instanceId] ?? {
120 hiddenModels: [],
121 modelOrder: [],
122 }
123 );
125​
126function applyInstanceModelPreferences(
127 options: ReadonlyArray<AppModelOption>,
128 preferences: {
129 readonly hiddenModels: ReadonlyArray<string>;
130 readonly modelOrder: ReadonlyArray<string>;
131 },
132): AppModelOption[] {
133 const hiddenModels = new Set(preferences.hiddenModels);
134 return sortModelsForProviderInstance(
135 options.filter((option) => option.isCustom || !hiddenModels.has(option.slug)),
136 { modelOrder: preferences.modelOrder },
137 );
139​
140function normalizeCustomModelEntries(
141 models: ReadonlyArray<CustomModelDefinition>,
142 builtInModelSlugs: ReadonlySet<string>,
143): CustomModelDefinition[] {
144 const normalizedModels: CustomModelDefinition[] = [];
145 const seen = new Set<string>();
146​
147 for (const candidate of models) {
148 if (
149 candidate.slug.length > MAX_CUSTOM_MODEL_LENGTH ||
150 builtInModelSlugs.has(candidate.slug) ||
151 seen.has(candidate.slug)
152 ) {
153 continue;
154 }
155​
156 seen.add(candidate.slug);
157 normalizedModels.push(candidate);
158 }
159​
160 return normalizedModels;
162​
163function getAppModelOptions(
164 settings: UnifiedSettings,
165 providers: ReadonlyArray<ServerProvider>,
166 provider: ProviderDriverKind,
167 selectedModel?: string | null,
168): AppModelOption[] {
169 const rawModels = getProviderModels(providers, provider);
170 // Server custom-model snapshots can lag removals; rebuild custom rows from settings.
171 const options: AppModelOption[] = rawModels
172 .filter((model) => !model.isCustom)
173 .map((model) => toAppModelOption(model, provider));
174 const seen = new Set(options.map((option) => option.slug));
175 const builtInModelSlugs = new Set(
176 Arr.filterMap(getProviderModels(providers, provider), (model) =>
177 model.isCustom ? Result.failVoid : Result.succeed(model.slug),
178 ),
179 );
180​
181 const defaultInstanceId = defaultInstanceIdForDriver(provider);
182 const customModels = readInstanceCustomModels(settings, defaultInstanceId, provider);
183 for (const entry of normalizeCustomModelEntries(customModels, builtInModelSlugs)) {
184 if (seen.has(entry.slug)) {
185 continue;
186 }
187​
188 seen.add(entry.slug);
189 const shortName = resolveModelShortName(entry, provider);
190 options.push({
191 slug: entry.slug,
192 name: entry.name,
193 isCustom: true,
194 ...(shortName ? { shortName } : {}),
195 });
196 }
197​
198 const preferences = readInstanceModelPreferences(settings, defaultInstanceId);
199 return appendUnavailableDynamicModelSelection(
200 applyInstanceModelPreferences(options, preferences),
201 rawModels,
202 provider,
203 selectedModel,
204 preferences.hiddenModels,
205 );
207​
208export function getAppModelOptionsForInstance(
209 settings: UnifiedSettings,
210 entry: ProviderInstanceEntry,
211 selectedModel?: string | null,
212): AppModelOption[] {
213 const options: AppModelOption[] = entry.models
214 .filter((model) => !model.isCustom)
215 .map((model) => toAppModelOption(model, entry.driverKind));
216 const seen = new Set(options.map((option) => option.slug));
217 const builtInModelSlugs = new Set(
218 Arr.filterMap(entry.models, (model) =>
219 model.isCustom ? Result.failVoid : Result.succeed(model.slug),
220 ),
221 );
222​
223 const customModels = readInstanceCustomModels(settings, entry.instanceId, entry.driverKind);
224 for (const custom of normalizeCustomModelEntries(customModels, builtInModelSlugs)) {
225 if (seen.has(custom.slug)) {
226 continue;
227 }
228​
229 seen.add(custom.slug);
230 const shortName = resolveModelShortName(custom, entry.driverKind);
231 options.push({
232 slug: custom.slug,
233 name: custom.name,
234 isCustom: true,
235 ...(shortName ? { shortName } : {}),
236 });
237 }
238​
239 const preferences = readInstanceModelPreferences(settings, entry.instanceId);
240 return appendUnavailableDynamicModelSelection(
241 applyInstanceModelPreferences(options, preferences),
242 entry.models,
243 entry.driverKind,
244 selectedModel,
245 preferences.hiddenModels,
246 );
248​
249export function resolveAppModelSelection(
250 provider: ProviderDriverKind,
251 settings: UnifiedSettings,
252 providers: ReadonlyArray<ServerProvider>,
253 selectedModel: string | null | undefined,
254): string {
255 const resolvedProvider = resolveSelectableProvider(providers, provider);
256 const options = getAppModelOptions(settings, providers, resolvedProvider, selectedModel);
257 return (
258 resolveSelectableModel(resolvedProvider, selectedModel, options) ??
259 getDefaultServerModel(providers, resolvedProvider)
260 );
262​
263export function resolveAppModelSelectionForInstance(
264 instanceId: ProviderInstanceId,
265 settings: UnifiedSettings,
266 providers: ReadonlyArray<ServerProvider>,
267 selectedModel: string | null | undefined,
268 resolutionOptions?: { readonly preserveUnavailableSelection?: boolean },
269): string | null {
270 const entry = deriveProviderInstanceEntries(providers).find(
271 (candidate) => candidate.instanceId === instanceId,
272 );
273 if (!entry) return null;
274 const options = getAppModelOptionsForInstance(
275 settings,
276 entry,
277 resolutionOptions?.preserveUnavailableSelection ? selectedModel : null,
278 );
279 const resolvedSelection = resolveSelectableModel(entry.driverKind, selectedModel, options);
280 if (resolvedSelection) {
281 return resolvedSelection;
282 }
283 if (
284 resolutionOptions?.preserveUnavailableSelection &&
285 (entry.driverKind === "opencode" || entry.driverKind === "antigravity")
286 ) {
287 const unavailableSelection = normalizeCustomModelSlug(selectedModel);
288 const hiddenModels = readInstanceModelPreferences(settings, entry.instanceId).hiddenModels;
289 if (
290 unavailableSelection &&
291 !hiddenModels.includes(unavailableSelection) &&
292 resolveSelectableModel(entry.driverKind, selectedModel, entry.models) === null &&
293 (entry.driverKind !== "antigravity" || unavailableSelection !== ANTIGRAVITY_DEFAULT_MODEL)
294 ) {
295 return unavailableSelection;
296 }
297 }
298 return options.find((option) => option.isDefault)?.slug ?? options[0]?.slug ?? null;
300​
301// New drafts carry the stored selection; capabilities resolve its raw options later.
302export function resolveEffectiveDefaultModelSelection(
303 settings: UnifiedSettings,
304 providers: ReadonlyArray<ServerProvider>,
305 selection: ModelSelection,
306): ModelSelection {
307 const model = resolveAppModelSelectionForInstance(
308 selection.instanceId,
309 settings,
310 providers,
311 selection.model,
312 { preserveUnavailableSelection: true },
313 );
314 return model === null || model === selection.model ? selection : { ...selection, model };
316​
317export function defaultModelSelectionsAreMixed(
318 clientSettings: ClientSettings,
319 targets: ReadonlyArray<{
320 projectId: ProjectId | null;
321 settings: ServerSettings;
322 providers: ReadonlyArray<ServerProvider>;
323 }>,
324): boolean {
325 const selections = targets.map(({ projectId, settings, providers }) => {
326 const stored = settings.defaultModelSelection;
327 const selection =
328 stored === null
329 ? resolveProjectDefaultProviderModelSelection(providers, settings, projectId)
330 : resolveDefaultProviderModelSelection(providers, stored);
331 return {
332 stored,
333 effective: selection
334 ? resolveEffectiveDefaultModelSelection(
335 {
336 ...clientSettings,
337 ...settings,
338 ...resolveModelPreferences(settings, clientSettings),
339 },
340 providers,
341 selection,
342 )
343 : null,
344 };
345 });
346 return selections.some((selection) => !Equal.equals(selection, selections[0]));
348​
349export function getCustomModelOptionsByInstance(
350 settings: UnifiedSettings,
351 providers: ReadonlyArray<ServerProvider>,
352 selectedInstanceId?: ProviderInstanceId | null,
353 selectedModel?: string | null,
354): ReadonlyMap<ProviderInstanceId, ReadonlyArray<ModelEsque>> {
355 const out = new Map<ProviderInstanceId, ReadonlyArray<ModelEsque>>();
356 for (const entry of deriveProviderInstanceEntries(providers)) {
357 out.set(
358 entry.instanceId,
359 getAppModelOptionsForInstance(
360 settings,
361 entry,
362 entry.instanceId === selectedInstanceId ? selectedModel : null,
363 ),
364 );
365 }
366 return out;
368​
369export function selectsPlanAgent(options: ModelSelection["options"]): boolean {
370 return options?.some((option) => option.id === "agent" && option.value === "plan") ?? false;
372​
373export function resolveAppModelSelectionState(
374 settings: UnifiedSettings,
375 providers: ReadonlyArray<ServerProvider>,
376): ModelSelection {
377 const selection = settings.textGenerationModelSelection ?? {
378 instanceId: DEFAULT_TEXT_GENERATION_INSTANCE_ID,
379 model: DEFAULT_TEXT_GENERATION_MODEL,
380 };
381 const supportedProviders = providers.filter(
382 (provider) => provider.supportsTextGeneration !== false,
383 );
384 const entries = deriveProviderInstanceEntries(supportedProviders);
385 const selectedEntry = entries.find(
386 (entry) => entry.instanceId === selection.instanceId && entry.enabled && entry.isAvailable,
387 );
388 const entry =
389 selectedEntry ?? entries.find((candidate) => candidate.enabled && candidate.isAvailable);
390 if (entry) {
391 const selectedModel = selectedEntry ? selection.model : null;
392 // Use the cheap generation default; the catalog default is the expensive chat flagship.
393 const options = getAppModelOptionsForInstance(settings, entry);
394 const model =
395 resolveSelectableModel(entry.driverKind, selectedModel, options) ??
396 resolveSelectableModel(
397 entry.driverKind,
398 DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER[entry.driverKind],
399 options,
400 ) ??
401 resolveAppModelSelectionForInstance(
402 entry.instanceId,
403 settings,
404 supportedProviders,
405 selectedModel,
406 ) ??
407 entry.models[0]?.slug ??
408 DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER[entry.driverKind];
409 if (!model) {
410 return createModelSelection(entry.instanceId, "", []);
411 }
412 const provider = entry.driverKind;
413 const { modelOptionsForDispatch } = getComposerProviderState({
414 provider,
415 model,
416 models: entry.models,
417 modelOptions: selectedEntry ? selection.options : undefined,
418 includePlanAgent: selectsPlanAgent(selection.options),
419 });
420​
421 return createModelSelection(entry.instanceId, model, modelOptionsForDispatch);
422 }
423​
424 return NO_PROVIDER_MODEL_SELECTION;