Open History to see the last commit
1import {
2 DEFAULT_MODEL_BY_PROVIDER,
3 MODEL_SLUG_ALIASES_BY_PROVIDER,
4 type ModelCapabilities,
5 type ModelSelection,
6 type ProviderOptionDescriptor,
7 type ProviderOptionSelection,
8 type ProviderKind,
9} from "@t3tools/contracts";
10
11export interface SelectableModelOption {
12 slug: string;
13 name: string;
14}
15
16export function createModelCapabilities(input: {
17 optionDescriptors: ReadonlyArray<ProviderOptionDescriptor>;
18}): ModelCapabilities {
19 return {
20 optionDescriptors: input.optionDescriptors.map(cloneDescriptor),
21 };
22}
23
24function getRawSelectionValueById(
25 selections: ReadonlyArray<ProviderOptionSelection> | null | undefined,
26 id: string,
27): string | boolean | undefined {
28 const selection = selections?.find((candidate) => candidate.id === id);
29 return selection?.value;
30}
31
32export function getProviderOptionSelectionValue(
33 selections: ReadonlyArray<ProviderOptionSelection> | null | undefined,
34 id: string,
35): string | boolean | undefined {
36 return getRawSelectionValueById(selections, id);
37}
38
39export function getProviderOptionStringSelectionValue(
40 selections: ReadonlyArray<ProviderOptionSelection> | null | undefined,
41 id: string,
42): string | undefined {
43 const value = getProviderOptionSelectionValue(selections, id);
44 return typeof value === "string" ? value : undefined;
45}
46
47export function getProviderOptionBooleanSelectionValue(
48 selections: ReadonlyArray<ProviderOptionSelection> | null | undefined,
49 id: string,
50): boolean | undefined {
51 const value = getProviderOptionSelectionValue(selections, id);
52 return typeof value === "boolean" ? value : undefined;
53}
54
55export function getModelSelectionOptionValue(
56 modelSelection: ModelSelection | null | undefined,
57 id: string,
58): string | boolean | undefined {
59 return getProviderOptionSelectionValue(modelSelection?.options, id);
60}
61
62export function getModelSelectionStringOptionValue(
63 modelSelection: ModelSelection | null | undefined,
64 id: string,
65): string | undefined {
66 return getProviderOptionStringSelectionValue(modelSelection?.options, id);
67}
68
69export function getModelSelectionBooleanOptionValue(
70 modelSelection: ModelSelection | null | undefined,
71 id: string,
72): boolean | undefined {
73 return getProviderOptionBooleanSelectionValue(modelSelection?.options, id);
74}
75
76function resolveDescriptorChoiceValue(
77 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>,
78 raw: string | null | undefined,
79): string | undefined {
80 const trimmed = trimOrNull(raw);
81 if (!trimmed) {
82 return descriptor.currentValue ?? descriptor.options.find((option) => option.isDefault)?.id;
83 }
84 if (descriptor.options.length === 0) {
85 return trimmed;
86 }
87 if (
88 descriptor.promptInjectedValues?.includes(trimmed) &&
89 descriptor.options.some((option) => option.id === trimmed)
90 ) {
91 return descriptor.options.find((option) => option.isDefault)?.id;
92 }
93 if (descriptor.options.some((option) => option.id === trimmed)) {
94 return trimmed;
95 }
96 return descriptor.currentValue ?? descriptor.options.find((option) => option.isDefault)?.id;
97}
98
99function cloneDescriptor(descriptor: ProviderOptionDescriptor): ProviderOptionDescriptor {
100 return descriptor.type === "select"
101 ? {
102 ...descriptor,
103 options: [...descriptor.options],
104 ...(descriptor.promptInjectedValues
105 ? { promptInjectedValues: [...descriptor.promptInjectedValues] }
106 : {}),
107 }
108 : { ...descriptor };
109}
110
111function cloneSelection(selection: ProviderOptionSelection): ProviderOptionSelection {
112 return { ...selection };
113}
114
115function withDescriptorCurrentValue(
116 descriptor: ProviderOptionDescriptor,
117 rawCurrentValue: string | boolean | undefined,
118): ProviderOptionDescriptor {
119 if (descriptor.type === "boolean") {
120 if (typeof rawCurrentValue === "boolean") {
121 return {
122 ...descriptor,
123 currentValue: rawCurrentValue,
124 };
125 }
126 return descriptor;
127 }
128 const currentValue =
129 typeof rawCurrentValue === "string"
130 ? resolveDescriptorChoiceValue(descriptor, rawCurrentValue)
131 : resolveDescriptorChoiceValue(descriptor, descriptor.currentValue);
132 if (!currentValue) {
133 const { currentValue: _unusedCurrentValue, ...rest } = descriptor;
134 return rest;
135 }
136 return {
137 ...descriptor,
138 currentValue,
139 };
140}
141
142export function getProviderOptionDescriptors(input: {
143 caps: ModelCapabilities;
144 selections?: ReadonlyArray<ProviderOptionSelection> | null | undefined;
145}): ReadonlyArray<ProviderOptionDescriptor> {
146 const { caps, selections } = input;
147 const baseDescriptors = (caps.optionDescriptors ?? []).map(cloneDescriptor);
148
149 return baseDescriptors.map((descriptor) =>
150 withDescriptorCurrentValue(
151 descriptor,
152 getRawSelectionValueById(selections, descriptor.id) ?? descriptor.currentValue,
153 ),
154 );
155}
156
157export function getProviderOptionCurrentValue(
158 descriptor: ProviderOptionDescriptor | null | undefined,
159): string | boolean | undefined {
160 if (!descriptor) {
161 return undefined;
162 }
163 if (descriptor.type === "boolean") {
164 return descriptor.currentValue;
165 }
166 if (descriptor.currentValue) {
167 return descriptor.currentValue;
168 }
169 return descriptor.options.find((option) => option.isDefault)?.id;
170}
171
172export function getProviderOptionCurrentLabel(
173 descriptor: ProviderOptionDescriptor | null | undefined,
174): string | undefined {
175 if (!descriptor) {
176 return undefined;
177 }
178 if (descriptor.type === "boolean") {
179 return typeof descriptor.currentValue === "boolean"
180 ? descriptor.currentValue
181 ? "On"
182 : "Off"
183 : undefined;
184 }
185 const currentValue = getProviderOptionCurrentValue(descriptor);
186 if (typeof currentValue !== "string") {
187 return undefined;
188 }
189 return descriptor.options.find((option) => option.id === currentValue)?.label;
190}
191
192export function buildProviderOptionSelectionsFromDescriptors(
193 descriptors: ReadonlyArray<ProviderOptionDescriptor> | null | undefined,
194): Array<ProviderOptionSelection> | undefined {
195 if (!descriptors || descriptors.length === 0) {
196 return undefined;
197 }
198
199 const nextSelections: Array<ProviderOptionSelection> = [];
200
201 for (const descriptor of descriptors) {
202 const value = getProviderOptionCurrentValue(descriptor);
203 if (typeof value === "string" || typeof value === "boolean") {
204 nextSelections.push({ id: descriptor.id, value });
205 }
206 }
207
208 return nextSelections.length > 0 ? nextSelections : undefined;
209}
210
211export function getModelSelectionOptionDescriptors(
212 modelSelection: ModelSelection | null | undefined,
213 caps?: ModelCapabilities | null | undefined,
214): ReadonlyArray<ProviderOptionDescriptor> {
215 if (!modelSelection) {
216 return [];
217 }
218 if (!caps) {
219 return [];
220 }
221 return getProviderOptionDescriptors({
222 caps,
223 selections: modelSelection.options,
224 });
225}
226
227export function isClaudeUltrathinkPrompt(text: string | null | undefined): boolean {
228 return typeof text === "string" && /\bultrathink\b/i.test(text);
229}
230
231export function normalizeModelSlug(
232 model: string | null | undefined,
233 provider: ProviderKind = "codex",
234): string | null {
235 if (typeof model !== "string") {
236 return null;
237 }
238
239 const trimmed = model.trim();
240 if (!trimmed) {
241 return null;
242 }
243
244 const aliases = MODEL_SLUG_ALIASES_BY_PROVIDER[provider] as Record<string, string>;
245 const aliased = Object.prototype.hasOwnProperty.call(aliases, trimmed)
246 ? aliases[trimmed]
247 : undefined;
248 return typeof aliased === "string" ? aliased : trimmed;
249}
250
251export function resolveSelectableModel(
252 provider: ProviderKind,
253 value: string | null | undefined,
254 options: ReadonlyArray<SelectableModelOption>,
255): string | null {
256 if (typeof value !== "string") {
257 return null;
258 }
259
260 const trimmed = value.trim();
261 if (!trimmed) {
262 return null;
263 }
264
265 const direct = options.find((option) => option.slug === trimmed);
266 if (direct) {
267 return direct.slug;
268 }
269
270 const byName = options.find((option) => option.name.toLowerCase() === trimmed.toLowerCase());
271 if (byName) {
272 return byName.slug;
273 }
274
275 const normalized = normalizeModelSlug(trimmed, provider);
276 if (!normalized) {
277 return null;
278 }
279
280 const resolved = options.find((option) => option.slug === normalized);
281 return resolved ? resolved.slug : null;
282}
283
284function resolveModelSlug(model: string | null | undefined, provider: ProviderKind): string {
285 const normalized = normalizeModelSlug(model, provider);
286 if (!normalized) {
287 return DEFAULT_MODEL_BY_PROVIDER[provider];
288 }
289 return normalized;
290}
291
292export function resolveModelSlugForProvider(
293 provider: ProviderKind,
294 model: string | null | undefined,
295): string {
296 return resolveModelSlug(model, provider);
297}
298
299/** Trim a string, returning null for empty/missing values. */
300export function trimOrNull<T extends string>(value: T | null | undefined): T | null {
301 if (typeof value !== "string") return null;
302 const trimmed = value.trim() as T;
303 return trimmed || null;
304}
305
306function cloneSelections(
307 selections: ReadonlyArray<ProviderOptionSelection>,
308): Array<ProviderOptionSelection> {
309 return selections.map(cloneSelection);
310}
311
312export function createModelSelection(
313 provider: ProviderKind,
314 model: string,
315 options?: ReadonlyArray<ProviderOptionSelection> | null,
316): ModelSelection {
317 const selections = options ? cloneSelections(options) : [];
318 return {
319 provider,
320 model,
321 ...(selections.length > 0 ? { options: selections } : {}),
322 } as ModelSelection;
323}
324
325/**
326 * Returns the effort value if it is a prompt-injected value according to
327 * any select descriptor in the given capabilities, or null otherwise.
328 *
329 * Unlike a single `find`, this checks every descriptor so that the
330 * correct descriptor's `promptInjectedValues` list is consulted even when
331 * multiple select descriptors exist.
332 */
333export function resolvePromptInjectedEffort(
334 caps: ModelCapabilities,
335 rawEffort: string | null | undefined,
336): string | null {
337 const trimmed = trimOrNull(rawEffort);
338 if (!trimmed) return null;
339 const descriptors = getProviderOptionDescriptors({ caps });
340 for (const descriptor of descriptors) {
341 if (descriptor.type === "select" && descriptor.promptInjectedValues?.includes(trimmed)) {
342 return trimmed;
343 }
344 }
345 return null;
346}
347
348export function applyClaudePromptEffortPrefix(
349 text: string,
350 effort: string | null | undefined,
351): string {
352 const trimmed = text.trim();
353 if (!trimmed) {
354 return trimmed;
355 }
356 if (effort !== "ultrathink") {
357 return trimmed;
358 }
359 if (trimmed.startsWith("Ultrathink:")) {
360 return trimmed;
361 }
362 return `Ultrathink:\n${trimmed}`;
363}