Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/core/request/thinking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
type RawBody = {
variant?: unknown
providerOptions?: {
variant?: unknown
modelVariant?: unknown
thinkingConfig?: {
thinkingBudget?: unknown
}
}
}

export type ThinkingVariant = 'low' | 'medium' | 'high' | 'max'

export function resolveThinkingConfig(
model: string,
body: unknown,
defaults: { budget: number } = { budget: 20000 }
): { enabled: boolean; budget: number; variant?: string } {
const b = (body || {}) as RawBody

const rawVariant =
(typeof b.variant === 'string' && b.variant) ||
(typeof b.providerOptions?.variant === 'string' && b.providerOptions.variant) ||
(typeof b.providerOptions?.modelVariant === 'string' && b.providerOptions.modelVariant) ||
undefined

const explicitBudget = b.providerOptions?.thinkingConfig?.thinkingBudget
const budgetFromBody =
typeof explicitBudget === 'number' && Number.isFinite(explicitBudget) && explicitBudget > 0
? explicitBudget
: undefined

const budgetFromVariant = variantToBudget(rawVariant)
const enabled =
model.endsWith('-thinking') ||
budgetFromBody !== undefined ||
b.providerOptions?.thinkingConfig !== undefined ||
budgetFromVariant !== undefined

return {
enabled,
budget: budgetFromBody ?? budgetFromVariant ?? defaults.budget,
variant: rawVariant
}
}

function variantToBudget(rawVariant: string | undefined): number | undefined {
if (!rawVariant) return undefined

const v = rawVariant.toLowerCase()
if (v === 'low') return 8192
if (v === 'medium') return 16384
// "high" is the documented name, but "max" is kept as backward compat
if (v === 'high' || v === 'max') return 32768
return undefined
}
30 changes: 30 additions & 0 deletions test/thinking.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import assert from 'node:assert/strict'
import { test } from 'node:test'

import { resolveThinkingConfig } from '../dist/core/request/thinking.js'

test('resolveThinkingConfig: model suffix enables thinking with default budget', () => {
const r = resolveThinkingConfig('claude-sonnet-4-5-thinking', {})
assert.equal(r.enabled, true)
assert.equal(r.budget, 20000)
})

test('resolveThinkingConfig: explicit thinkingConfig takes precedence', () => {
const r = resolveThinkingConfig('claude-sonnet-4-5', {
providerOptions: { thinkingConfig: { thinkingBudget: 12345 } }
})
assert.equal(r.enabled, true)
assert.equal(r.budget, 12345)
})

test('resolveThinkingConfig: variant low/medium/high maps to budgets', () => {
assert.equal(resolveThinkingConfig('claude-sonnet-4-5', { variant: 'low' }).budget, 8192)
assert.equal(resolveThinkingConfig('claude-sonnet-4-5', { variant: 'medium' }).budget, 16384)
assert.equal(resolveThinkingConfig('claude-sonnet-4-5', { variant: 'high' }).budget, 32768)
})

test('resolveThinkingConfig: max is accepted as backward-compatible alias of high', () => {
const r = resolveThinkingConfig('claude-sonnet-4-5', { providerOptions: { variant: 'max' } })
assert.equal(r.enabled, true)
assert.equal(r.budget, 32768)
})