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