mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
* feat(ai): support xhigh and max reasoning effort levels The provider-level reasoning default only offered low/medium/high, so models that document the full graded ladder could not be configured to their top tiers. DeepSeek V4/V4.1 grade `reasoning_effort` continuously up to `max`, and Anthropic exposes `max` on 4.6+ plus `xhigh` from 4.7 onward. Beyond the missing select options, three separate layers silently discarded the higher values: - REASONING_RANK stopped at `high`, so the visibility resolver treated `xhigh`/`max` as stale and fell back to `low`. - The OpenAI capability check only matched `deepseek-r1`, which left the DeepSeek V4 family unrecognised and hid the thinking chip entirely. - The pinned `@ai-sdk/openai@4.0.8` validates provider options against a zod enum capped at `xhigh`, so `max` on Chat Completions threw "invalid openai provider options" before any request was made. Add a shared PROVIDER_REASONING_EFFORT_OPTIONS list used by both the settings select and its save guard, extend the rank ladder, and teach the per-model derivation about DeepSeek V4 grading plus the Anthropic effort generations. Bump `@ai-sdk/openai` to 4.0.11, the first release whose enum accepts `max`; it keeps 4.0.8's data-URL image fix. Models whose vocabulary stops at `high` now clamp these levels onto their own ceiling instead of dropping the setting, so an explicit user choice is never silently ignored and no unsupported value reaches a provider. * fix(ai): keep extended effort for explicit custom relay aliases A custom OpenAI-compatible provider with an explicit reasoning default is the escape hatch for relay model IDs that match no capability pattern. That branch still advertised only low/medium/high, so resolveVisibleCattyThinkingLevel treated a configured `xhigh` or `max` as stale and downgraded it to `high` before the request was built. Advertise the full ladder there: the explicit provider default is already the user's assertion that the alias accepts reasoning_effort, so it should also carry the ceiling they picked. Unrecognized aliases without a default keep advertising nothing. * test(ai): verify max effort with the pinned OpenAI SDK --------- Co-authored-by: Arcadia <57169358+RayYiHang@users.noreply.github.com>
515 lines
21 KiB
TypeScript
515 lines
21 KiB
TypeScript
import type { ProviderStyle } from './types';
|
|
import { resolveOpenAIApi, resolveProviderStyle, type ProviderConfig } from './types';
|
|
import { CATTY_REASONING_LEVELS } from './composerPicker';
|
|
|
|
const ANTHROPIC_THINKING_BUDGET: Record<'low' | 'medium' | 'high', number> = {
|
|
low: 4_000,
|
|
medium: 10_000,
|
|
high: 20_000,
|
|
};
|
|
|
|
const GEMINI_25_THINKING_BUDGET: Record<'low' | 'medium' | 'high', number> = {
|
|
low: 1_024,
|
|
medium: 8_192,
|
|
high: 16_384,
|
|
};
|
|
|
|
const REASONING_RANK = ['off', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'] as const;
|
|
const LEVELS_LOW_MEDIUM_HIGH = ['low', 'medium', 'high'] as const;
|
|
const LEVELS_LOW_HIGH = ['low', 'high'] as const;
|
|
const LEVELS_MINIMAL_LOW_MEDIUM_HIGH = ['minimal', 'low', 'medium', 'high'] as const;
|
|
const LEVELS_MINIMAL_HIGH = ['minimal', 'high'] as const;
|
|
const LEVELS_LOW_MEDIUM_HIGH_MAX = ['low', 'medium', 'high', 'max'] as const;
|
|
const LEVELS_LOW_MEDIUM_HIGH_XHIGH = ['low', 'medium', 'high', 'xhigh'] as const;
|
|
const LEVELS_LOW_MEDIUM_HIGH_XHIGH_MAX = ['low', 'medium', 'high', 'xhigh', 'max'] as const;
|
|
const LEVELS_OFF_LOW_MEDIUM_HIGH_MAX = ['off', 'low', 'medium', 'high', 'max'] as const;
|
|
const LEVELS_OFF_LOW_MEDIUM_HIGH_XHIGH_MAX = ['off', 'low', 'medium', 'high', 'xhigh', 'max'] as const;
|
|
const LEVELS_OFF_LOW_MEDIUM_HIGH_XHIGH = ['off', 'low', 'medium', 'high', 'xhigh'] as const;
|
|
|
|
/**
|
|
* Levels the provider-level default may take. Shared with the settings form so
|
|
* the select and the save guard never drift apart.
|
|
*
|
|
* `xhigh` and `max` are real wire values, not UI sugar: DeepSeek V4 grades
|
|
* `reasoning_effort` continuously up to `max`, Anthropic maps
|
|
* `output_config.effort` (4.6 reaches `max`, 4.7+/5+ also `xhigh`), and OpenAI
|
|
* documents `xhigh` for the Codex Max line. A model whose vocabulary stops
|
|
* earlier clamps these down to its own ceiling instead of emitting a value the
|
|
* provider would reject.
|
|
*/
|
|
export const PROVIDER_REASONING_EFFORT_OPTIONS = ['low', 'medium', 'high', 'xhigh', 'max'] as const;
|
|
|
|
export function isProviderReasoningEffort(value: string | null | undefined): boolean {
|
|
return typeof value === 'string'
|
|
&& (PROVIDER_REASONING_EFFORT_OPTIONS as readonly string[]).includes(value);
|
|
}
|
|
|
|
/** Highest level every reasoning-capable vendor understands. */
|
|
const HIGHEST_UNIVERSAL_EFFORT = 'high';
|
|
|
|
export type CattyReasoningProviderOptions = Record<string, Record<string, unknown>>;
|
|
|
|
/**
|
|
* Effective reasoning effort for a turn: the composer thinking chip wins when
|
|
* the user picked a level; otherwise fall back to the provider-level default
|
|
* configured under advanced parameters. `default` / blank provider values are
|
|
* dropped so the request body stays untouched (avoids 400s on models that do
|
|
* not accept reasoning_effort).
|
|
*/
|
|
export function resolveEffectiveCattyReasoningEffort(
|
|
chipEffort: string | null | undefined,
|
|
providerDefaultEffort: string | null | undefined,
|
|
): string | undefined {
|
|
const chip = typeof chipEffort === 'string' ? chipEffort.trim() : '';
|
|
if (chip) return chip;
|
|
const fallback = typeof providerDefaultEffort === 'string' ? providerDefaultEffort.trim().toLowerCase() : '';
|
|
if (!fallback || fallback === 'default') return undefined;
|
|
return fallback;
|
|
}
|
|
|
|
/** Extra completion tokens the SDK will add on top of maxTokens for thinking. */
|
|
export function estimateReasoningOutputReserve(
|
|
options: CattyReasoningProviderOptions | undefined,
|
|
): number {
|
|
if (!options) return 0;
|
|
const anthropicThinking = options.anthropic?.thinking as { budgetTokens?: unknown } | undefined;
|
|
if (typeof anthropicThinking?.budgetTokens === 'number' && anthropicThinking.budgetTokens > 0) {
|
|
return Math.ceil(anthropicThinking.budgetTokens);
|
|
}
|
|
const googleConfig = options.google?.thinkingConfig as { thinkingBudget?: unknown } | undefined;
|
|
if (typeof googleConfig?.thinkingBudget === 'number' && googleConfig.thinkingBudget > 0) {
|
|
return Math.ceil(googleConfig.thinkingBudget);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* OpenAI Responses turns must run stateless (`store: false`).
|
|
*
|
|
* The Responses API defaults to `store: true`, which makes the SDK replay
|
|
* prior reasoning turns as server-side item references (`rs_…` ids).
|
|
* Relays — the main reason Responses mode is opt-in here — frequently do not
|
|
* persist items, so the next turn fails with "Item with id 'rs_…' not found.
|
|
* Items are not persisted when `store` is set to false." With `store: false`
|
|
* the SDK instead requests `reasoning.encrypted_content` and replays the full
|
|
* reasoning items inline, which works against both relays and OpenAI directly.
|
|
*
|
|
* The installed `@ai-sdk/openai` only auto-adds the encrypted-content include
|
|
* for model IDs its own capability detector recognizes (o-series / gpt-5).
|
|
* Relay reasoners such as `deepseek-r1`, `gpt-oss`, or `grok-4` pass
|
|
* Netcatty's broader classifier but not the SDK's, so the include must be
|
|
* requested explicitly or the provider returns no replayable ciphertext and
|
|
* the reasoning items get dropped from subsequent turns. The SDK dedupes the
|
|
* include when it would have added it anyway.
|
|
*
|
|
* The include is therefore requested for *every* stateless Responses turn,
|
|
* not gated on a reasoning classifier: relay model IDs are arbitrary, and
|
|
* always-thinking models whose IDs match no known pattern (e.g. DeepSeek's
|
|
* default `deepseek-v4-flash`, which streams thinking deltas) would silently
|
|
* lose replayable reasoning. Models that emit no reasoning simply have no
|
|
* reasoning items to encrypt, so the extra include is a no-op for them.
|
|
*/
|
|
const REASONING_ENCRYPTED_CONTENT_INCLUDE = 'reasoning.encrypted_content';
|
|
|
|
export function applyResponsesApiStatelessStoreOption(
|
|
provider: Pick<ProviderConfig, 'openaiApi' | 'providerId' | 'style'> | null | undefined,
|
|
options: CattyReasoningProviderOptions | undefined,
|
|
): CattyReasoningProviderOptions | undefined {
|
|
if (!provider || resolveProviderStyle(provider) !== 'openai') return options;
|
|
if (resolveOpenAIApi(provider) !== 'responses') return options;
|
|
const openaiOptions: Record<string, unknown> = {
|
|
...options?.openai,
|
|
store: false,
|
|
};
|
|
const existingInclude = Array.isArray(openaiOptions.include) ? openaiOptions.include : [];
|
|
if (!existingInclude.includes(REASONING_ENCRYPTED_CONTENT_INCLUDE)) {
|
|
openaiOptions.include = [...existingInclude, REASONING_ENCRYPTED_CONTENT_INCLUDE];
|
|
}
|
|
return {
|
|
...options,
|
|
openai: openaiOptions,
|
|
};
|
|
}
|
|
|
|
export function buildCattyReasoningProviderOptions(
|
|
provider: Pick<ProviderConfig, 'providerId' | 'style' | 'advancedParams' | 'openaiApi'> | null | undefined,
|
|
effort: string | null | undefined,
|
|
modelId?: string,
|
|
): CattyReasoningProviderOptions | undefined {
|
|
if (!provider) return undefined;
|
|
const rawEffort = typeof effort === 'string' ? effort.trim() : '';
|
|
if (!rawEffort) return undefined;
|
|
const style: ProviderStyle = resolveProviderStyle(provider);
|
|
const advertised = cattyReasoningLevelsForSelection(provider, modelId);
|
|
const resolved = advertised.length
|
|
? resolveVisibleCattyThinkingLevel(advertised, rawEffort)
|
|
: rawEffort;
|
|
if (!resolved) return undefined;
|
|
|
|
if (style === 'openai') {
|
|
if (modelId && openaiModelKnownUnsupportedReasoning(modelId)) return undefined;
|
|
const explicitDefault = isProviderReasoningEffort(provider.advancedParams?.reasoningEffort);
|
|
const unrecognizedModel = !!modelId && !openaiModelLikelySupportsReasoning(modelId);
|
|
if (unrecognizedModel && !(provider.providerId === 'custom' && explicitDefault)) return undefined;
|
|
if (resolved === 'off') {
|
|
if (modelId && openaiModelSupportsNoneReasoning(modelId)) {
|
|
return { openai: { reasoningEffort: 'none' } };
|
|
}
|
|
return undefined;
|
|
}
|
|
return {
|
|
openai: {
|
|
reasoningEffort: resolved,
|
|
...(explicitDefault && resolveOpenAIApi(provider) === 'responses' ? { forceReasoning: true } : {}),
|
|
},
|
|
};
|
|
}
|
|
|
|
if (style === 'anthropic') {
|
|
if (modelId && !anthropicModelLikelySupportsThinking(modelId)) return undefined;
|
|
if (resolved === 'off') {
|
|
if (modelId && anthropicUsesAdaptiveThinking(modelId) && anthropicAllowsDisabledThinking(modelId)) {
|
|
return { anthropic: { thinking: { type: 'disabled' } } };
|
|
}
|
|
return undefined;
|
|
}
|
|
// Claude 4.6+ maps `output_config.effort` directly and accepts up to `max`
|
|
// (4.7+/5+ also `xhigh`). Older models only have the fixed token budgets, so
|
|
// a level above `high` must clamp to `high` rather than drop the setting.
|
|
const adaptiveThinking = !!modelId && anthropicUsesAdaptiveThinking(modelId);
|
|
const anthropicEffort = adaptiveThinking
|
|
? resolveAnthropicAdaptiveEffort(modelId!, resolved)
|
|
: clampToUniversalEffort(resolved);
|
|
if (!anthropicEffort) return undefined;
|
|
if (adaptiveThinking) {
|
|
return {
|
|
anthropic: {
|
|
thinking: { type: 'adaptive' },
|
|
effort: anthropicEffort,
|
|
},
|
|
};
|
|
}
|
|
return {
|
|
anthropic: {
|
|
thinking: {
|
|
type: 'enabled',
|
|
budgetTokens: ANTHROPIC_THINKING_BUDGET[anthropicEffort as keyof typeof ANTHROPIC_THINKING_BUDGET],
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
if (style === 'google') {
|
|
if (!modelId || !googleModelLikelySupportsThinking(modelId)) return undefined;
|
|
// Gemini 3 `thinkingLevel` stops at `high`, and Gemini 2.5 only has numeric
|
|
// budgets, so anything above `high` has to clamp instead of being sent raw.
|
|
const googleResolved = clampToUniversalEffort(resolved);
|
|
if (!googleResolved) return undefined;
|
|
if (isGemini3Model(modelId)) {
|
|
if (googleResolved !== 'minimal' && googleResolved !== 'low' && googleResolved !== 'medium' && googleResolved !== 'high') {
|
|
return undefined;
|
|
}
|
|
return {
|
|
google: {
|
|
thinkingConfig: {
|
|
thinkingLevel: googleResolved,
|
|
includeThoughts: googleResolved !== 'minimal',
|
|
},
|
|
},
|
|
};
|
|
}
|
|
if (resolved === 'off') {
|
|
if (!googleModelAllowsDisabledThinking(modelId)) return undefined;
|
|
return {
|
|
google: {
|
|
thinkingConfig: {
|
|
thinkingBudget: 0,
|
|
includeThoughts: false,
|
|
},
|
|
},
|
|
};
|
|
}
|
|
if (googleResolved !== 'low' && googleResolved !== 'medium' && googleResolved !== 'high') return undefined;
|
|
return {
|
|
google: {
|
|
thinkingConfig: {
|
|
thinkingBudget: GEMINI_25_THINKING_BUDGET[googleResolved],
|
|
includeThoughts: true,
|
|
},
|
|
},
|
|
};
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* Collapse `xhigh` / `max` onto `high` for providers whose vocabulary stops
|
|
* there. Dropping the setting entirely would silently ignore an explicit user
|
|
* choice, so the maximum supported depth is used instead.
|
|
*/
|
|
function clampToUniversalEffort(level: string): string | undefined {
|
|
if (level === 'xhigh' || level === 'max') return HIGHEST_UNIVERSAL_EFFORT;
|
|
return level;
|
|
}
|
|
|
|
/**
|
|
* Claude `output_config.effort` vocabulary per adaptive-thinking generation.
|
|
* 4.6 exposes `max`; 4.7 and later (including 5.x) also expose `xhigh`.
|
|
*/
|
|
function resolveAnthropicAdaptiveEffort(modelId: string, level: string): string | undefined {
|
|
if (level !== 'xhigh' && level !== 'max') {
|
|
return level === 'minimal' ? 'low' : level;
|
|
}
|
|
return anthropicSupportsExtraHighEffort(modelId) ? level : 'max';
|
|
}
|
|
|
|
/** Claude 4.7+ / 5+ list `xhigh`; 4.6 stops at `max`. */
|
|
function anthropicSupportsExtraHighEffort(modelId: string): boolean {
|
|
const parsed = parseClaudeModel(modelId);
|
|
if (!parsed) return false;
|
|
if (parsed.major >= 5) return true;
|
|
return parsed.major === 4 && parsed.minor >= 7;
|
|
}
|
|
|
|
/** Extended thinking is Claude 3.7+ / 4+ / 5+; original Claude 3 Haiku/Sonnet reject it. */
|
|
export function anthropicModelLikelySupportsThinking(modelId: string): boolean {
|
|
const parsed = parseClaudeModel(modelId);
|
|
if (!parsed) return false;
|
|
if (parsed.major >= 4) return true;
|
|
return parsed.major === 3 && parsed.minor >= 7;
|
|
}
|
|
|
|
export function googleModelLikelySupportsThinking(modelId: string): boolean {
|
|
const id = modelId.trim().toLowerCase();
|
|
return /gemini-3|gemini-2\.5|gemini-2\.0-flash-thinking|thinking/.test(id);
|
|
}
|
|
|
|
/**
|
|
* `reasoning_effort: "none"` is only valid on GPT-5.1+ (and later minors).
|
|
* Bare gpt-5 / o3 / o4-mini accept low|medium|high (and sometimes minimal),
|
|
* but reject none. Chat snapshots are not reasoners.
|
|
*/
|
|
export function openaiModelSupportsNoneReasoning(modelId: string): boolean {
|
|
const id = modelId.trim().toLowerCase();
|
|
if (!id || openaiModelIsChatSnapshot(id)) return false;
|
|
return /gpt-5\.(?:[1-9]\d*)/.test(id);
|
|
}
|
|
|
|
/** Original GPT-5 (not 5.1+) accepts `minimal` as the floor instead of `none`. */
|
|
export function openaiModelSupportsMinimalReasoning(modelId: string): boolean {
|
|
const id = modelId.trim().toLowerCase();
|
|
if (!id || openaiModelIsChatSnapshot(id)) return false;
|
|
if (openaiModelSupportsNoneReasoning(id)) return false;
|
|
return /gpt-5/.test(id);
|
|
}
|
|
|
|
/**
|
|
* Models whose `reasoning_effort` is a continuous dial rather than an OpenAI
|
|
* tier list. DeepSeek V4 documents `low | high | max` natively and maps
|
|
* everything in between, so the full graded scale is meaningful there.
|
|
*/
|
|
export function openaiModelSupportsGradedReasoningEffort(modelId: string): boolean {
|
|
const id = modelId.trim().toLowerCase();
|
|
if (!id) return false;
|
|
return /deepseek-(?:v?4|v4\.1)/.test(id);
|
|
}
|
|
|
|
/**
|
|
* Explicitly supported OpenAI `xhigh` families. GPT-5.5 accepts xhigh (not
|
|
* max); keep older families and unverified variants at their existing ceiling.
|
|
*/
|
|
export function openaiModelSupportsExtraHighReasoning(modelId: string): boolean {
|
|
const id = modelId.trim().toLowerCase();
|
|
if (!id || openaiModelIsChatSnapshot(id)) return false;
|
|
return /gpt-5(?:\.\d+)?-codex-max/.test(id)
|
|
|| /(?:^|\/)gpt-5\.5(?:-\d{4}-\d{2}-\d{2})?$/.test(id);
|
|
}
|
|
|
|
/** OpenAI-compat models that accept `reasoning_effort` (o-series, GPT-5, reasoners). */
|
|
export function openaiModelLikelySupportsReasoning(modelId: string): boolean {
|
|
const id = modelId.trim().toLowerCase();
|
|
if (!id) return false;
|
|
if (openaiModelIsChatSnapshot(id)) return false;
|
|
return (
|
|
/(^|[^a-z0-9])o[1-4]([^a-z0-9]|$)/.test(id)
|
|
|| /gpt-5/.test(id)
|
|
|| /gpt-oss/.test(id)
|
|
|| /reasoner|reasoning/.test(id)
|
|
|| /deepseek-r1/.test(id)
|
|
|| openaiModelSupportsGradedReasoningEffort(id)
|
|
|| /grok-4/.test(id)
|
|
);
|
|
}
|
|
|
|
/** Levels shown on the Catty thinking chip, or empty when the model cannot take them. */
|
|
export function cattyReasoningLevelsForSelection(
|
|
provider: Pick<ProviderConfig, 'providerId' | 'style' | 'advancedParams' | 'openaiApi'> | null | undefined,
|
|
modelId?: string,
|
|
): readonly string[] {
|
|
if (!provider) return [];
|
|
const style = resolveProviderStyle(provider);
|
|
if (style === 'anthropic') {
|
|
if (!modelId || !anthropicModelLikelySupportsThinking(modelId)) return [];
|
|
return anthropicAdvertisedLevels(modelId);
|
|
}
|
|
if (style === 'google') {
|
|
if (!modelId || !googleModelLikelySupportsThinking(modelId)) return [];
|
|
if (isGemini3Model(modelId)) return gemini3AdvertisedLevels(modelId);
|
|
if (!googleModelAllowsDisabledThinking(modelId)) return LEVELS_LOW_MEDIUM_HIGH;
|
|
return CATTY_REASONING_LEVELS;
|
|
}
|
|
if (style === 'openai') {
|
|
if (!modelId || openaiModelKnownUnsupportedReasoning(modelId)) return [];
|
|
if (!openaiModelLikelySupportsReasoning(modelId)) {
|
|
// A custom provider with an explicit default is the escape hatch for
|
|
// relay aliases we cannot classify. Advertising only the legacy three
|
|
// levels would let resolveVisibleCattyThinkingLevel downgrade a
|
|
// configured `xhigh`/`max` to `high`, so the user's own assertion about
|
|
// the alias becomes the capability signal instead.
|
|
return provider.providerId === 'custom' && isProviderReasoningEffort(provider.advancedParams?.reasoningEffort)
|
|
? LEVELS_LOW_MEDIUM_HIGH_XHIGH_MAX
|
|
: [];
|
|
}
|
|
// DeepSeek grades `reasoning_effort` continuously and `none` is not part of
|
|
// that scale, so the chip starts at `low` and ends at `max`.
|
|
if (openaiModelSupportsGradedReasoningEffort(modelId)) return LEVELS_LOW_MEDIUM_HIGH_XHIGH_MAX;
|
|
if (openaiModelSupportsExtraHighReasoning(modelId)) {
|
|
return openaiModelSupportsNoneReasoning(modelId)
|
|
? LEVELS_OFF_LOW_MEDIUM_HIGH_XHIGH
|
|
: LEVELS_LOW_MEDIUM_HIGH_XHIGH;
|
|
}
|
|
if (openaiModelSupportsNoneReasoning(modelId)) return CATTY_REASONING_LEVELS;
|
|
if (openaiModelSupportsMinimalReasoning(modelId)) return LEVELS_MINIMAL_LOW_MEDIUM_HIGH;
|
|
return LEVELS_LOW_MEDIUM_HIGH;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Anthropic advertises `off` plus the effort tiers the model documents.
|
|
* Adaptive thinking (4.6+) adds `max`; 4.7+/5+ also add `xhigh`. Always-on
|
|
* families (Fable/Mythos 5) cannot switch thinking off at all.
|
|
*/
|
|
function anthropicAdvertisedLevels(modelId: string): readonly string[] {
|
|
const allowsDisabled = anthropicAllowsDisabledThinking(modelId);
|
|
if (!anthropicUsesAdaptiveThinking(modelId)) {
|
|
return allowsDisabled ? CATTY_REASONING_LEVELS : LEVELS_LOW_MEDIUM_HIGH;
|
|
}
|
|
const supportsExtraHigh = anthropicSupportsExtraHighEffort(modelId);
|
|
if (!allowsDisabled) {
|
|
return supportsExtraHigh ? LEVELS_LOW_MEDIUM_HIGH_XHIGH_MAX : LEVELS_LOW_MEDIUM_HIGH_MAX;
|
|
}
|
|
return supportsExtraHigh ? LEVELS_OFF_LOW_MEDIUM_HIGH_XHIGH_MAX : LEVELS_OFF_LOW_MEDIUM_HIGH_MAX;
|
|
}
|
|
|
|
/**
|
|
* Pick a level the current model actually advertises; never keep a stale chip value.
|
|
* Missing mid-levels prefer the next higher advertised value (Gemini 3 Pro medium → high)
|
|
* so the chip, persisted pref, and request payload stay aligned.
|
|
*/
|
|
export function resolveVisibleCattyThinkingLevel(
|
|
levels: readonly string[],
|
|
selected: string | undefined,
|
|
): string | undefined {
|
|
if (!levels.length) return undefined;
|
|
const raw = selected?.trim().toLowerCase();
|
|
if (raw && levels.includes(raw)) return raw;
|
|
if (raw === 'off') {
|
|
if (levels.includes('minimal')) return 'minimal';
|
|
return levels[0];
|
|
}
|
|
const selectedRank = raw ? REASONING_RANK.indexOf(raw as typeof REASONING_RANK[number]) : -1;
|
|
if (selectedRank < 0) return levels[0];
|
|
for (let i = selectedRank + 1; i < REASONING_RANK.length; i += 1) {
|
|
if (levels.includes(REASONING_RANK[i])) return REASONING_RANK[i];
|
|
}
|
|
for (let i = selectedRank - 1; i >= 0; i -= 1) {
|
|
if (levels.includes(REASONING_RANK[i])) return REASONING_RANK[i];
|
|
}
|
|
return levels[0];
|
|
}
|
|
|
|
// Explicit defaults may enable relay aliases, but not known non-reasoning families.
|
|
function openaiModelKnownUnsupportedReasoning(modelId: string): boolean {
|
|
const id = modelId.trim().toLowerCase();
|
|
return openaiModelIsChatSnapshot(id) || /(^|\/)(?:chat)?gpt-(?:4(?:o|[.-]|$)|3\.5(?:[.-]|$))/.test(id);
|
|
}
|
|
|
|
function openaiModelIsChatSnapshot(modelId: string): boolean {
|
|
return /gpt-5(?:\.\d+)?-chat/.test(modelId);
|
|
}
|
|
|
|
function isGemini3Model(modelId: string): boolean {
|
|
return modelId.trim().toLowerCase().includes('gemini-3');
|
|
}
|
|
|
|
function gemini3AdvertisedLevels(modelId: string): readonly string[] {
|
|
const id = modelId.trim().toLowerCase();
|
|
if (/flash-lite-image/.test(id)) return LEVELS_MINIMAL_HIGH;
|
|
if (/gemini-3\.7/.test(id) && /flash/.test(id)) return LEVELS_LOW_MEDIUM_HIGH;
|
|
if (/pro/.test(id) && !/flash/.test(id)) {
|
|
return /gemini-3\.1/.test(id) ? LEVELS_LOW_MEDIUM_HIGH : LEVELS_LOW_HIGH;
|
|
}
|
|
if (/flash/.test(id)) return LEVELS_MINIMAL_LOW_MEDIUM_HIGH;
|
|
return LEVELS_LOW_MEDIUM_HIGH;
|
|
}
|
|
|
|
function googleModelAllowsDisabledThinking(modelId: string): boolean {
|
|
const id = modelId.trim().toLowerCase();
|
|
return /flash-lite|flash/.test(id) && !/pro/.test(id);
|
|
}
|
|
|
|
type ClaudeModel = {
|
|
family: string;
|
|
major: number;
|
|
minor: number;
|
|
};
|
|
|
|
function parseClaudeMinor(raw: string | undefined): number {
|
|
if (!raw) return 0;
|
|
const minor = Number.parseInt(raw, 10);
|
|
// Date suffixes like 20250514 are not minor versions.
|
|
return Number.isFinite(minor) && minor < 100 ? minor : 0;
|
|
}
|
|
|
|
function parseClaudeModel(modelId: string): ClaudeModel | null {
|
|
const id = modelId.trim().toLowerCase();
|
|
if (!id) return null;
|
|
const familyMatch = id.match(
|
|
/claude-(opus|sonnet|haiku|fable|mythos)(?:-preview)?(?:[-.](\d+)(?:[-.](\d+))?)?/,
|
|
);
|
|
if (familyMatch) {
|
|
return {
|
|
family: familyMatch[1],
|
|
major: familyMatch[2] ? Number.parseInt(familyMatch[2], 10) : 5,
|
|
minor: parseClaudeMinor(familyMatch[3]),
|
|
};
|
|
}
|
|
const threeSeven = id.match(/claude-3[-.]7/);
|
|
if (threeSeven) return { family: 'sonnet', major: 3, minor: 7 };
|
|
const bare = id.match(/claude-(\d+)(?:[-.](\d+))?/);
|
|
if (bare) {
|
|
return {
|
|
family: 'claude',
|
|
major: Number.parseInt(bare[1], 10),
|
|
minor: parseClaudeMinor(bare[2]),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** 4.6+ and Claude 5 reject or deprecate manual `type: "enabled"`. */
|
|
function anthropicUsesAdaptiveThinking(modelId: string): boolean {
|
|
const parsed = parseClaudeModel(modelId);
|
|
if (!parsed) return false;
|
|
if (parsed.major >= 5) return true;
|
|
return parsed.major === 4 && parsed.minor >= 6;
|
|
}
|
|
|
|
/** Fable 5 / Mythos 5 are always-on and reject `thinking.type: "disabled"`. */
|
|
function anthropicAllowsDisabledThinking(modelId: string): boolean {
|
|
const parsed = parseClaudeModel(modelId);
|
|
if (!parsed) return true;
|
|
return parsed.family !== 'fable' && parsed.family !== 'mythos';
|
|
}
|