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

modelOptions.ts

260 lines8.3 KB

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;
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 };
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 );
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;
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;
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 );
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 if (provider.driver === "cursor" && model.isCustom) {
169 continue;
170 }
171 const key = `${provider.instanceId}:${model.slug}`;
172 options.set(key, {
173 key,
174 label: model.name,
175 subtitle: model.subProvider ?? "",
176 providerKey: provider.instanceId,
177 providerLabel,
178 providerDriver: provider.driver,
179 isDefault: model.isDefault === true,
180 isLegacy: model.isLegacy === true,
181 capabilities: model.capabilities,
182 selection: normalizeSelectionOptions(
183 {
184 instanceId: provider.instanceId,
185 model: model.slug,
186 },
187 model.capabilities,
188 ),
189 });
190 }
191 }
192​
193 if (fallbackModelSelection) {
194 const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
195 const existing = options.get(key);
196 if (existing) {
197 options.set(key, {
198 ...existing,
199 selection:
200 existing.providerDriver === "antigravity"
201 ? fallbackModelSelection
202 : normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
203 });
204 } else {
205 const provider = config?.providers.find(
206 (candidate) => candidate.instanceId === fallbackModelSelection.instanceId,
207 );
208 const instanceConfig = config?.settings?.providerInstances[fallbackModelSelection.instanceId];
209 const model = provider?.models.find(
210 (candidate) => candidate.slug === fallbackModelSelection.model,
211 );
212 const providerDriver =
213 provider?.driver ?? instanceConfig?.driver ?? fallbackModelSelection.instanceId;
214 const providerLabel = providerDisplayLabel({
215 driver: providerDriver,
216 displayName: provider?.displayName ?? instanceConfig?.displayName,
217 instanceId: fallbackModelSelection.instanceId,
218 });
219 const unavailable =
220 (provider?.driver === "cursor" && model?.isCustom === true) ||
221 isModelSelectionUnavailable(config, fallbackModelSelection);
222 options.set(key, {
223 key,
224 label: model?.name ?? fallbackModelSelection.model,
225 subtitle: model?.subProvider ?? "",
226 providerKey: fallbackModelSelection.instanceId,
227 providerLabel,
228 providerDriver,
229 isDefault: false,
230 isLegacy: model?.isLegacy === true,
231 ...(unavailable ? { isUnavailable: true } : {}),
232 capabilities: model?.capabilities ?? null,
233 selection: fallbackModelSelection,
234 });
235 }
236 }
237​
238 return [...options.values()];
240​
241export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
242 const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
243 for (const option of options) {
244 const existing = groups.get(option.providerKey);
245 if (existing) {
246 existing.models.push(option);
247 } else {
248 groups.set(option.providerKey, {
249 providerLabel: option.providerLabel,
250 models: [option],
251 });
252 }
253 }
254​
255 return [...groups.entries()].map(([providerKey, group]) => ({
256 providerKey,
257 providerLabel: group.providerLabel,
258 models: group.models,
259 }));