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