Open History to see the last commit
1import {
2 DEFAULT_MODEL_BY_PROVIDER,
3 MODEL_SLUG_ALIASES_BY_PROVIDER,
4 type ClaudeCodeEffort,
5 type ClaudeModelOptions,
6 type CodexModelOptions,
7 type ModelCapabilities,
8 type ModelSelection,
9 type ProviderKind,
10} from "@t3tools/contracts";
11
12export interface SelectableModelOption {
13 slug: string;
14 name: string;
15}
16
17// ── Effort helpers ────────────────────────────────────────────────────
18
19/** Check whether a capabilities object includes a given effort value. */
20export function hasEffortLevel(caps: ModelCapabilities, value: string): boolean {
21 return caps.reasoningEffortLevels.some((l) => l.value === value);
22}
23
24/** Return the default effort value for a capabilities object, or null if none. */
25export function getDefaultEffort(caps: ModelCapabilities): string | null {
26 return caps.reasoningEffortLevels.find((l) => l.isDefault)?.value ?? null;
27}
28
29/**
30 * Resolve a raw effort option against capabilities.
31 *
32 * Returns the effective effort value — the explicit value if supported and not
33 * prompt-injected, otherwise the model's default. Returns `undefined` only
34 * when the model has no effort levels at all.
35 *
36 * Prompt-injected efforts (e.g. "ultrathink") are excluded because they are
37 * applied via prompt text, not the effort API parameter.
38 */
39export function resolveEffort(
40 caps: ModelCapabilities,
41 raw: string | null | undefined,
42): string | undefined {
43 const defaultValue = getDefaultEffort(caps);
44 const trimmed = typeof raw === "string" ? raw.trim() : null;
45 if (
46 trimmed &&
47 !caps.promptInjectedEffortLevels.includes(trimmed) &&
48 hasEffortLevel(caps, trimmed)
49 ) {
50 return trimmed;
51 }
52 return defaultValue ?? undefined;
53}
54
55// ── Context window helpers ───────────────────────────────────────────
56
57/** Check whether a capabilities object includes a given context window value. */
58export function hasContextWindowOption(caps: ModelCapabilities, value: string): boolean {
59 return caps.contextWindowOptions.some((o) => o.value === value);
60}
61
62/** Return the default context window value, or `null` if none is defined. */
63export function getDefaultContextWindow(caps: ModelCapabilities): string | null {
64 return caps.contextWindowOptions.find((o) => o.isDefault)?.value ?? null;
65}
66
67/**
68 * Resolve a raw `contextWindow` option against capabilities.
69 *
70 * Returns the effective context window value — the explicit value if supported,
71 * otherwise the model's default. Returns `undefined` only when the model has
72 * no context window options at all.
73 *
74 * Unlike effort levels (where the API has matching defaults), the context
75 * window requires an explicit API suffix (e.g. `[1m]`), so we always preserve
76 * the resolved value to avoid ambiguity between "user chose the default" and
77 * "not specified".
78 */
79export function resolveContextWindow(
80 caps: ModelCapabilities,
81 raw: string | null | undefined,
82): string | undefined {
83 const defaultValue = getDefaultContextWindow(caps);
84 if (!raw) return defaultValue ?? undefined;
85 return hasContextWindowOption(caps, raw) ? raw : (defaultValue ?? undefined);
86}
87
88export function normalizeCodexModelOptionsWithCapabilities(
89 caps: ModelCapabilities,
90 modelOptions: CodexModelOptions | null | undefined,
91): CodexModelOptions | undefined {
92 const reasoningEffort = resolveEffort(caps, modelOptions?.reasoningEffort);
93 const fastMode = caps.supportsFastMode ? modelOptions?.fastMode : undefined;
94 const nextOptions: CodexModelOptions = {
95 ...(reasoningEffort
96 ? { reasoningEffort: reasoningEffort as CodexModelOptions["reasoningEffort"] }
97 : {}),
98 ...(fastMode !== undefined ? { fastMode } : {}),
99 };
100 return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
101}
102
103export function normalizeClaudeModelOptionsWithCapabilities(
104 caps: ModelCapabilities,
105 modelOptions: ClaudeModelOptions | null | undefined,
106): ClaudeModelOptions | undefined {
107 const effort = resolveEffort(caps, modelOptions?.effort);
108 const thinking = caps.supportsThinkingToggle ? modelOptions?.thinking : undefined;
109 const fastMode = caps.supportsFastMode ? modelOptions?.fastMode : undefined;
110 const contextWindow = resolveContextWindow(caps, modelOptions?.contextWindow);
111 const nextOptions: ClaudeModelOptions = {
112 ...(thinking !== undefined ? { thinking } : {}),
113 ...(effort ? { effort: effort as ClaudeModelOptions["effort"] } : {}),
114 ...(fastMode !== undefined ? { fastMode } : {}),
115 ...(contextWindow !== undefined ? { contextWindow } : {}),
116 };
117 return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
118}
119
120export function isClaudeUltrathinkPrompt(text: string | null | undefined): boolean {
121 return typeof text === "string" && /\bultrathink\b/i.test(text);
122}
123
124export function normalizeModelSlug(
125 model: string | null | undefined,
126 provider: ProviderKind = "codex",
127): string | null {
128 if (typeof model !== "string") {
129 return null;
130 }
131
132 const trimmed = model.trim();
133 if (!trimmed) {
134 return null;
135 }
136
137 const aliases = MODEL_SLUG_ALIASES_BY_PROVIDER[provider] as Record<string, string>;
138 const aliased = Object.prototype.hasOwnProperty.call(aliases, trimmed)
139 ? aliases[trimmed]
140 : undefined;
141 return typeof aliased === "string" ? aliased : trimmed;
142}
143
144export function resolveSelectableModel(
145 provider: ProviderKind,
146 value: string | null | undefined,
147 options: ReadonlyArray<SelectableModelOption>,
148): string | null {
149 if (typeof value !== "string") {
150 return null;
151 }
152
153 const trimmed = value.trim();
154 if (!trimmed) {
155 return null;
156 }
157
158 const direct = options.find((option) => option.slug === trimmed);
159 if (direct) {
160 return direct.slug;
161 }
162
163 const byName = options.find((option) => option.name.toLowerCase() === trimmed.toLowerCase());
164 if (byName) {
165 return byName.slug;
166 }
167
168 const normalized = normalizeModelSlug(trimmed, provider);
169 if (!normalized) {
170 return null;
171 }
172
173 const resolved = options.find((option) => option.slug === normalized);
174 return resolved ? resolved.slug : null;
175}
176
177export function resolveModelSlug(model: string | null | undefined, provider: ProviderKind): string {
178 const normalized = normalizeModelSlug(model, provider);
179 if (!normalized) {
180 return DEFAULT_MODEL_BY_PROVIDER[provider];
181 }
182 return normalized;
183}
184
185export function resolveModelSlugForProvider(
186 provider: ProviderKind,
187 model: string | null | undefined,
188): string {
189 return resolveModelSlug(model, provider);
190}
191
192/** Trim a string, returning null for empty/missing values. */
193export function trimOrNull<T extends string>(value: T | null | undefined): T | null {
194 if (typeof value !== "string") return null;
195 const trimmed = value.trim() as T;
196 return trimmed || null;
197}
198
199/**
200 * Resolve the actual API model identifier from a model selection.
201 *
202 * Provider-aware: each provider can map `contextWindow` (or other options)
203 * to whatever the API requires — a model-id suffix, a separate parameter, etc.
204 * The canonical slug stored in the selection stays unchanged so the
205 * capabilities system keeps working.
206 *
207 * Expects `contextWindow` to already be resolved (via `resolveContextWindow`)
208 * to the effective value, not stripped to `undefined` for defaults.
209 */
210export function resolveApiModelId(modelSelection: ModelSelection): string {
211 switch (modelSelection.provider) {
212 case "claudeAgent": {
213 switch (modelSelection.options?.contextWindow) {
214 case "1m":
215 return `${modelSelection.model}[1m]`;
216 default:
217 return modelSelection.model;
218 }
219 }
220 default: {
221 return modelSelection.model;
222 }
223 }
224}
225
226export function applyClaudePromptEffortPrefix(
227 text: string,
228 effort: ClaudeCodeEffort | null | undefined,
229): string {
230 const trimmed = text.trim();
231 if (!trimmed) {
232 return trimmed;
233 }
234 if (effort !== "ultrathink") {
235 return trimmed;
236 }
237 if (trimmed.startsWith("Ultrathink:")) {
238 return trimmed;
239 }
240 return `Ultrathink:\n${trimmed}`;
241}