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

modelOptions.ts

240 lines7.4 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 buildProviderOptionSelectionsFromDescriptors,
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 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 = buildProviderOptionSelectionsFromDescriptors(
50 getProviderOptionDescriptors({
51 caps: capabilities,
52 selections: selection.options,
53 }),
54 );
55 return options
56 ? { ...selection, options }
57 : {
58 instanceId: selection.instanceId,
59 model: selection.model,
60 };
62​
63/**
64 * A stored model selection is only usable when its provider instance is
65 * currently enabled, installed, and authenticated on the server. Returns the
66 * selection unchanged when usable, otherwise `null` so callers fall through to
67 * the server's default model. A missing config (environment offline) cannot be
68 * validated, so stored selections pass through untouched.
69 */
70export function resolveSelectableModelSelection(
71 config: T3ServerConfig | null | undefined,
72 selection: ModelSelection | null,
73): ModelSelection | null {
74 if (!selection || !config) {
75 return selection;
76 }
77 const provider = config.providers.find(
78 (candidate) => candidate.instanceId === selection.instanceId,
79 );
80 return provider &&
81 provider.enabled &&
82 provider.installed &&
83 provider.auth.status !== "unauthenticated"
84 ? selection
85 : null;
87​
88/**
89 * Like resolveSelectableModelSelection, but additionally rejects legacy
90 * models. Used for implicit defaults (stored draft, project last-used): a
91 * new thread should never quietly start on a legacy model, so those fall
92 * through to the provider's default instead. Explicit picks in the settings
93 * sheet are unaffected.
94 */
95export function resolveDefaultableModelSelection(
96 config: T3ServerConfig | null | undefined,
97 selection: ModelSelection | null,
98): ModelSelection | null {
99 const usable = resolveSelectableModelSelection(config, selection);
100 if (!usable || !config) {
101 return usable;
102 }
103 const provider = config.providers.find((candidate) => candidate.instanceId === usable.instanceId);
104 const model = provider?.models.find((candidate) => candidate.slug === usable.model);
105 return model?.isLegacy === true ? null : usable;
107​
108export function buildModelOptions(
109 config: T3ServerConfig | null | undefined,
110 fallbackModelSelection: ModelSelection | null,
111): ReadonlyArray<ModelOption> {
112 const options = new Map<string, ModelOption>();
113​
114 for (const provider of config?.providers ?? []) {
115 if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") {
116 continue;
117 }
118​
119 const providerLabel = providerDisplayLabel(provider);
120 for (const model of provider.models) {
121 const key = `${provider.instanceId}:${model.slug}`;
122 options.set(key, {
123 key,
124 label: model.name,
125 subtitle: providerLabel,
126 providerKey: provider.instanceId,
127 providerLabel,
128 providerDriver: provider.driver,
129 isDefault: model.isDefault === true,
130 isLegacy: model.isLegacy === true,
131 capabilities: model.capabilities,
132 selection: normalizeSelectionOptions(
133 {
134 instanceId: provider.instanceId,
135 model: model.slug,
136 },
137 model.capabilities,
138 ),
139 });
140 }
141 }
142​
143 if (fallbackModelSelection) {
144 const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
145 const existing = options.get(key);
146 if (existing) {
147 options.set(key, {
148 ...existing,
149 selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
150 });
151 } else {
152 const providerLabel = fallbackModelSelection.instanceId;
153 options.set(key, {
154 key,
155 label: fallbackModelSelection.model,
156 subtitle: providerLabel,
157 providerKey: fallbackModelSelection.instanceId,
158 providerLabel,
159 providerDriver: fallbackModelSelection.instanceId,
160 isDefault: false,
161 isLegacy: false,
162 capabilities: null,
163 selection: fallbackModelSelection,
164 });
165 }
166 }
167​
168 return [...options.values()];
170​
171export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
172 const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
173 for (const option of options) {
174 const existing = groups.get(option.providerKey);
175 if (existing) {
176 existing.models.push(option);
177 } else {
178 groups.set(option.providerKey, {
179 providerLabel: option.providerLabel,
180 models: [option],
181 });
182 }
183 }
184​
185 return [...groups.entries()].map(([providerKey, group]) => ({
186 providerKey,
187 providerLabel: group.providerLabel,
188 models: group.models,
189 }));
191​
192function modelMenuAction(option: ModelOption, selectedModel: ModelSelection | null): MenuAction {
193 return {
194 id: `model:${option.key}`,
195 title: option.label,
196 state:
197 option.selection.instanceId === selectedModel?.instanceId &&
198 option.selection.model === selectedModel.model
199 ? "on"
200 : undefined,
201 };
203​
204export function buildModelMenuActions(
205 groups: ReadonlyArray<ProviderGroup>,
206 selectedModel: ModelSelection | null,
207): MenuAction[] {
208 return groups.flatMap((group) => {
209 const currentModels = group.models.filter((model) => !model.isLegacy);
210 const legacyModels = group.models.filter((model) => model.isLegacy);
211 const selected = group.models.find(
212 (model) =>
213 model.selection.instanceId === selectedModel?.instanceId &&
214 model.selection.model === selectedModel.model,
215 );
216​
217 return [
218 ...(currentModels.length > 0
219 ? [
220 {
221 id: `provider:${group.providerKey}`,
222 title: group.providerLabel,
223 subtitle: selected && !selected.isLegacy ? selected.label : undefined,
224 subactions: currentModels.map((option) => modelMenuAction(option, selectedModel)),
225 },
226 ]
227 : []),
228 ...(legacyModels.length > 0
229 ? [
230 {
231 id: `legacy-models:${group.providerKey}`,
232 title: `${group.providerLabel} legacy models`,
233 subtitle: selected?.isLegacy ? selected.label : undefined,
234 subactions: legacyModels.map((option) => modelMenuAction(option, selectedModel)),
235 },
236 ]
237 : []),
238 ];
239 });