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

model.ts

300 lines9.7 KB

1import {
2 DEFAULT_MODEL_BY_PROVIDER,
3 MODEL_SLUG_ALIASES_BY_PROVIDER,
4 type ClaudeAgentEffort,
5 type ClaudeModelOptions,
6 type CodexModelOptions,
7 type CursorModelOptions,
8 type ModelCapabilities,
9 type ModelSelection,
10 type OpenCodeModelOptions,
11 type ProviderKind,
12 type ProviderModelOptions,
13} from "@t3tools/contracts";
14​
15export interface SelectableModelOption {
16 slug: string;
17 name: string;
19​
20/** Check whether a capabilities object includes a given effort value. */
21export function hasEffortLevel(caps: ModelCapabilities, value: string): boolean {
22 return caps.reasoningEffortLevels.some((l) => l.value === value);
24​
25/** Return the default effort value for a capabilities object, or null if none. */
26export function getDefaultEffort(caps: ModelCapabilities): string | null {
27 return caps.reasoningEffortLevels.find((l) => l.isDefault)?.value ?? null;
29​
30/**
31 * Resolve a raw effort option against capabilities.
32 *
33 * Returns the explicit supported value when present and not prompt-injected,
34 * otherwise the model default. Returns `undefined` when the model exposes no
35 * effort levels.
36 */
37export function resolveEffort(
38 caps: ModelCapabilities,
39 raw: string | null | undefined,
40): string | undefined {
41 const defaultValue = getDefaultEffort(caps);
42 const trimmed = typeof raw === "string" ? raw.trim() : null;
43 if (
44 trimmed &&
45 !caps.promptInjectedEffortLevels.includes(trimmed) &&
46 hasEffortLevel(caps, trimmed)
47 ) {
48 return trimmed;
49 }
50 return defaultValue ?? undefined;
52​
53/** Check whether a capabilities object includes a given context window value. */
54export function hasContextWindowOption(caps: ModelCapabilities, value: string): boolean {
55 return caps.contextWindowOptions.some((o) => o.value === value);
57​
58/** Return the default context window value, or `null` if none is defined. */
59export function getDefaultContextWindow(caps: ModelCapabilities): string | null {
60 return caps.contextWindowOptions.find((o) => o.isDefault)?.value ?? null;
62​
63/**
64 * Resolve a raw `contextWindow` option against capabilities.
65 *
66 * Returns the explicit supported value when present, otherwise the model
67 * default. Returns `undefined` when the model exposes no context window options.
68 */
69export function resolveContextWindow(
70 caps: ModelCapabilities,
71 raw: string | null | undefined,
72): string | undefined {
73 const defaultValue = getDefaultContextWindow(caps);
74 if (!raw) return defaultValue ?? undefined;
75 return hasContextWindowOption(caps, raw) ? raw : (defaultValue ?? undefined);
77​
78export function normalizeCodexModelOptionsWithCapabilities(
79 caps: ModelCapabilities,
80 modelOptions: CodexModelOptions | null | undefined,
81): CodexModelOptions | undefined {
82 const reasoningEffort = resolveEffort(caps, modelOptions?.reasoningEffort);
83 const fastMode = caps.supportsFastMode ? modelOptions?.fastMode : undefined;
84 const nextOptions: CodexModelOptions = {
85 ...(reasoningEffort
86 ? { reasoningEffort: reasoningEffort as CodexModelOptions["reasoningEffort"] }
87 : {}),
88 ...(fastMode !== undefined ? { fastMode } : {}),
89 };
90 return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
92​
93export function normalizeClaudeModelOptionsWithCapabilities(
94 caps: ModelCapabilities,
95 modelOptions: ClaudeModelOptions | null | undefined,
96): ClaudeModelOptions | undefined {
97 const effort = resolveEffort(caps, modelOptions?.effort);
98 const thinking = caps.supportsThinkingToggle ? modelOptions?.thinking : undefined;
99 const fastMode = caps.supportsFastMode ? modelOptions?.fastMode : undefined;
100 const contextWindow = resolveContextWindow(caps, modelOptions?.contextWindow);
101 const nextOptions: ClaudeModelOptions = {
102 ...(thinking !== undefined ? { thinking } : {}),
103 ...(effort ? { effort: effort as ClaudeModelOptions["effort"] } : {}),
104 ...(fastMode !== undefined ? { fastMode } : {}),
105 ...(contextWindow !== undefined ? { contextWindow } : {}),
106 };
107 return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
109​
110export function normalizeCursorModelOptionsWithCapabilities(
111 caps: ModelCapabilities,
112 modelOptions: CursorModelOptions | null | undefined,
113): CursorModelOptions | undefined {
114 const reasoning = resolveEffort(caps, modelOptions?.reasoning);
115 const thinking = caps.supportsThinkingToggle ? modelOptions?.thinking : undefined;
116 const fastMode = caps.supportsFastMode ? modelOptions?.fastMode : undefined;
117 const contextWindow = resolveContextWindow(caps, modelOptions?.contextWindow);
118 const nextOptions: CursorModelOptions = {
119 ...(reasoning ? { reasoning: reasoning as CursorModelOptions["reasoning"] } : {}),
120 ...(fastMode !== undefined ? { fastMode } : {}),
121 ...(thinking !== undefined ? { thinking } : {}),
122 ...(contextWindow !== undefined ? { contextWindow } : {}),
123 };
124 return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
126​
127function resolveLabeledOption(
128 options: ReadonlyArray<{ value: string; isDefault?: boolean | undefined }> | undefined,
129 raw: string | null | undefined,
130): string | undefined {
131 if (!options || options.length === 0) {
132 return raw ?? undefined;
133 }
134 if (raw && options.some((option) => option.value === raw)) {
135 return raw;
136 }
137 return options.find((option) => option.isDefault)?.value;
139​
140export function normalizeOpenCodeModelOptionsWithCapabilities(
141 caps: ModelCapabilities,
142 modelOptions: OpenCodeModelOptions | null | undefined,
143): OpenCodeModelOptions | undefined {
144 const variant = resolveLabeledOption(caps.variantOptions, trimOrNull(modelOptions?.variant));
145 const agent = resolveLabeledOption(caps.agentOptions, trimOrNull(modelOptions?.agent));
146 const nextOptions: OpenCodeModelOptions = {
147 ...(variant ? { variant } : {}),
148 ...(agent ? { agent } : {}),
149 };
150 return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
152​
153export function normalizeProviderModelOptionsWithCapabilities(
154 provider: ProviderKind,
155 caps: ModelCapabilities,
156 modelOptions: ProviderModelOptions[ProviderKind] | null | undefined,
157): ProviderModelOptions[ProviderKind] | undefined {
158 switch (provider) {
159 case "codex":
160 return normalizeCodexModelOptionsWithCapabilities(caps, modelOptions as CodexModelOptions);
161 case "claudeAgent":
162 return normalizeClaudeModelOptionsWithCapabilities(caps, modelOptions as ClaudeModelOptions);
163 case "cursor":
164 return normalizeCursorModelOptionsWithCapabilities(caps, modelOptions as CursorModelOptions);
165 case "opencode":
166 return normalizeOpenCodeModelOptionsWithCapabilities(
167 caps,
168 modelOptions as OpenCodeModelOptions,
169 );
170 }
172​
173export function isClaudeUltrathinkPrompt(text: string | null | undefined): boolean {
174 return typeof text === "string" && /\bultrathink\b/i.test(text);
176​
177export function normalizeModelSlug(
178 model: string | null | undefined,
179 provider: ProviderKind = "codex",
180): string | null {
181 if (typeof model !== "string") {
182 return null;
183 }
184​
185 const trimmed = model.trim();
186 if (!trimmed) {
187 return null;
188 }
189​
190 const aliases = MODEL_SLUG_ALIASES_BY_PROVIDER[provider] as Record<string, string>;
191 const aliased = Object.prototype.hasOwnProperty.call(aliases, trimmed)
192 ? aliases[trimmed]
193 : undefined;
194 return typeof aliased === "string" ? aliased : trimmed;
196​
197export function resolveSelectableModel(
198 provider: ProviderKind,
199 value: string | null | undefined,
200 options: ReadonlyArray<SelectableModelOption>,
201): string | null {
202 if (typeof value !== "string") {
203 return null;
204 }
205​
206 const trimmed = value.trim();
207 if (!trimmed) {
208 return null;
209 }
210​
211 const direct = options.find((option) => option.slug === trimmed);
212 if (direct) {
213 return direct.slug;
214 }
215​
216 const byName = options.find((option) => option.name.toLowerCase() === trimmed.toLowerCase());
217 if (byName) {
218 return byName.slug;
219 }
220​
221 const normalized = normalizeModelSlug(trimmed, provider);
222 if (!normalized) {
223 return null;
224 }
225​
226 const resolved = options.find((option) => option.slug === normalized);
227 return resolved ? resolved.slug : null;
229​
230function resolveModelSlug(model: string | null | undefined, provider: ProviderKind): string {
231 const normalized = normalizeModelSlug(model, provider);
232 if (!normalized) {
233 return DEFAULT_MODEL_BY_PROVIDER[provider];
234 }
235 return normalized;
237​
238export function resolveModelSlugForProvider(
239 provider: ProviderKind,
240 model: string | null | undefined,
241): string {
242 return resolveModelSlug(model, provider);
244​
245/** Trim a string, returning null for empty/missing values. */
246export function trimOrNull<T extends string>(value: T | null | undefined): T | null {
247 if (typeof value !== "string") return null;
248 const trimmed = value.trim() as T;
249 return trimmed || null;
251​
252export function createModelSelection(
253 provider: ProviderKind,
254 model: string,
255 options?: ProviderModelOptions[ProviderKind] | undefined,
256): ModelSelection {
257 switch (provider) {
258 case "codex":
259 return {
260 provider,
261 model,
262 ...(options ? { options: options as CodexModelOptions } : {}),
263 };
264 case "claudeAgent":
265 return {
266 provider,
267 model,
268 ...(options ? { options: options as ClaudeModelOptions } : {}),
269 };
270 case "cursor":
271 return {
272 provider,
273 model,
274 ...(options ? { options: options as CursorModelOptions } : {}),
275 };
276 case "opencode":
277 return {
278 provider,
279 model,
280 ...(options ? { options: options as OpenCodeModelOptions } : {}),
281 };
282 }
284​
285export function applyClaudePromptEffortPrefix(
286 text: string,
287 effort: ClaudeAgentEffort | null | undefined,
288): string {
289 const trimmed = text.trim();
290 if (!trimmed) {
291 return trimmed;
292 }
293 if (effort !== "ultrathink") {
294 return trimmed;
295 }
296 if (trimmed.startsWith("Ultrathink:")) {
297 return trimmed;
298 }
299 return `Ultrathink:\n${trimmed}`;