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

modelOptions.ts

307 lines9.8 KB

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