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

modelOptions.ts

313 lines10.0 KB

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