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

modelOptions.ts

220 lines6.6 KB

1import type {
2 ModelCapabilities,
3 ModelSelection,
4 ServerConfig as T3ServerConfig,
5} from "@t3tools/contracts";
6import type { MenuAction } from "@react-native-menu/menu";
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​
88export function buildModelOptions(
89 config: T3ServerConfig | null | undefined,
90 fallbackModelSelection: ModelSelection | null,
91): ReadonlyArray<ModelOption> {
92 const options = new Map<string, ModelOption>();
93​
94 for (const provider of config?.providers ?? []) {
95 if (!provider.enabled || !provider.installed || provider.auth.status === "unauthenticated") {
96 continue;
97 }
98​
99 const providerLabel = providerDisplayLabel(provider);
100 for (const model of provider.models) {
101 const key = `${provider.instanceId}:${model.slug}`;
102 options.set(key, {
103 key,
104 label: model.name,
105 subtitle: providerLabel,
106 providerKey: provider.instanceId,
107 providerLabel,
108 providerDriver: provider.driver,
109 isDefault: model.isDefault === true,
110 isLegacy: model.isLegacy === true,
111 capabilities: model.capabilities,
112 selection: normalizeSelectionOptions(
113 {
114 instanceId: provider.instanceId,
115 model: model.slug,
116 },
117 model.capabilities,
118 ),
119 });
120 }
121 }
122​
123 if (fallbackModelSelection) {
124 const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
125 const existing = options.get(key);
126 if (existing) {
127 options.set(key, {
128 ...existing,
129 selection: normalizeSelectionOptions(fallbackModelSelection, existing.capabilities),
130 });
131 } else {
132 const providerLabel = fallbackModelSelection.instanceId;
133 options.set(key, {
134 key,
135 label: fallbackModelSelection.model,
136 subtitle: providerLabel,
137 providerKey: fallbackModelSelection.instanceId,
138 providerLabel,
139 providerDriver: fallbackModelSelection.instanceId,
140 isDefault: false,
141 isLegacy: false,
142 capabilities: null,
143 selection: fallbackModelSelection,
144 });
145 }
146 }
147​
148 return [...options.values()];
150​
151export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> {
152 const groups = new Map<string, { providerLabel: string; models: ModelOption[] }>();
153 for (const option of options) {
154 const existing = groups.get(option.providerKey);
155 if (existing) {
156 existing.models.push(option);
157 } else {
158 groups.set(option.providerKey, {
159 providerLabel: option.providerLabel,
160 models: [option],
161 });
162 }
163 }
164​
165 return [...groups.entries()].map(([providerKey, group]) => ({
166 providerKey,
167 providerLabel: group.providerLabel,
168 models: group.models,
169 }));
171​
172function modelMenuAction(option: ModelOption, selectedModel: ModelSelection | null): MenuAction {
173 return {
174 id: `model:${option.key}`,
175 title: option.label,
176 state:
177 option.selection.instanceId === selectedModel?.instanceId &&
178 option.selection.model === selectedModel.model
179 ? "on"
180 : undefined,
181 };
183​
184export function buildModelMenuActions(
185 groups: ReadonlyArray<ProviderGroup>,
186 selectedModel: ModelSelection | null,
187): MenuAction[] {
188 return groups.flatMap((group) => {
189 const currentModels = group.models.filter((model) => !model.isLegacy);
190 const legacyModels = group.models.filter((model) => model.isLegacy);
191 const selected = group.models.find(
192 (model) =>
193 model.selection.instanceId === selectedModel?.instanceId &&
194 model.selection.model === selectedModel.model,
195 );
196​
197 return [
198 ...(currentModels.length > 0
199 ? [
200 {
201 id: `provider:${group.providerKey}`,
202 title: group.providerLabel,
203 subtitle: selected && !selected.isLegacy ? selected.label : undefined,
204 subactions: currentModels.map((option) => modelMenuAction(option, selectedModel)),
205 },
206 ]
207 : []),
208 ...(legacyModels.length > 0
209 ? [
210 {
211 id: `legacy-models:${group.providerKey}`,
212 title: `${group.providerLabel} legacy models`,
213 subtitle: selected?.isLegacy ? selected.label : undefined,
214 subactions: legacyModels.map((option) => modelMenuAction(option, selectedModel)),
215 },
216 ]
217 : []),
218 ];
219 });