-
Notifications
You must be signed in to change notification settings - Fork 118
feat: force adaptive thinking via KIMI_MODEL_ADAPTIVE_THINKING #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@moonshot-ai/agent-core": minor | ||
| "@moonshot-ai/kimi-code": minor | ||
| "@moonshot-ai/kosong": minor | ||
| --- | ||
|
|
||
| Add `KIMI_MODEL_ADAPTIVE_THINKING` (and a matching `adaptive_thinking` model-alias field) to force adaptive thinking (`thinking: { type: 'adaptive' }`) on or off, overriding the Anthropic model-name version inference. This lets custom-named staff endpoints that back an adaptive-capable model opt in even when the model name does not encode a parseable Claude version. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,13 @@ export interface AnthropicOptions { | |
| metadata?: Record<string, string> | undefined; | ||
| /** Use streaming API. Defaults to true. Set to false for non-streaming (test/fallback). */ | ||
| stream?: boolean | undefined; | ||
| /** | ||
| * Explicitly declare whether the model supports adaptive thinking | ||
| * (`thinking: { type: 'adaptive' }`), overriding the model-name version | ||
| * inference. Useful for custom-named endpoints whose model name does not | ||
| * encode a parseable Claude version. Leave undefined to infer from the name. | ||
| */ | ||
| adaptiveThinking?: boolean | undefined; | ||
| clientFactory?: (auth: ProviderRequestAuth) => Anthropic; | ||
| } | ||
|
|
||
|
|
@@ -285,22 +292,22 @@ function isOpus47(model: string): boolean { | |
| return version.major === 4 && version.minor === 7; | ||
| } | ||
|
|
||
| function supportsEffortParam(model: string): boolean { | ||
| if (supportsAdaptiveThinking(model)) { | ||
| function supportsEffortParam(model: string, adaptive: boolean): boolean { | ||
| if (adaptive) { | ||
| return true; | ||
| } | ||
| const normalized = model.toLowerCase(); | ||
| return normalized.includes('opus-4-5') || normalized.includes('opus-4.5'); | ||
| } | ||
|
|
||
| function clampEffort(effort: ThinkingEffort, model: string): ThinkingEffort { | ||
| function clampEffort(effort: ThinkingEffort, model: string, adaptive: boolean): ThinkingEffort { | ||
| if (effort === 'off') { | ||
| return effort; | ||
| } | ||
| if (effort === 'xhigh' && !isOpus47(model)) { | ||
| return 'high'; | ||
| } | ||
| if (effort === 'max' && !supportsAdaptiveThinking(model)) { | ||
| if (effort === 'max' && !adaptive) { | ||
| return 'high'; | ||
| } | ||
| return effort; | ||
|
|
@@ -807,11 +814,13 @@ export class AnthropicChatProvider implements ChatProvider { | |
| private _baseUrl: string | undefined; | ||
| private _defaultHeaders: Record<string, string> | undefined; | ||
| private _clientFactory: ((auth: ProviderRequestAuth) => Anthropic) | undefined; | ||
| private _adaptiveThinking: boolean | undefined; | ||
|
|
||
| constructor(options: AnthropicOptions) { | ||
| this._model = options.model; | ||
| this._stream = options.stream ?? true; | ||
| this._metadata = options.metadata; | ||
| this._adaptiveThinking = options.adaptiveThinking; | ||
| const apiKey = options.apiKey ?? process.env['ANTHROPIC_API_KEY']; | ||
| this._apiKey = apiKey === undefined || apiKey.length === 0 ? undefined : apiKey; | ||
| this._baseUrl = options.baseUrl; | ||
|
|
@@ -1020,9 +1029,13 @@ export class AnthropicChatProvider implements ChatProvider { | |
| } | ||
|
|
||
| withThinking(effort: ThinkingEffort): AnthropicChatProvider { | ||
| // Resolve once: an explicit `adaptiveThinking` option overrides the | ||
| // model-name version inference, so custom-named endpoints can opt in/out. | ||
| const adaptive = this._adaptiveThinking ?? supportsAdaptiveThinking(this._model); | ||
|
|
||
| if (effort === 'off') { | ||
| let newBetas = [...(this._generationKwargs.betaFeatures ?? [])]; | ||
| if (supportsAdaptiveThinking(this._model)) { | ||
| if (adaptive) { | ||
| newBetas = newBetas.filter((b) => b !== INTERLEAVED_THINKING_BETA); | ||
| } | ||
| const clone = this._withGenerationKwargs({ | ||
|
|
@@ -1033,14 +1046,14 @@ export class AnthropicChatProvider implements ChatProvider { | |
| return clone; | ||
| } | ||
|
|
||
| const effectiveEffort = clampEffort(effort, this._model); | ||
| const effectiveEffort = clampEffort(effort, this._model, adaptive); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| if (effectiveEffort === 'off') { | ||
| throw new Error('Non-off thinking effort unexpectedly clamped to off.'); | ||
| } | ||
|
|
||
| let newBetas = [...(this._generationKwargs.betaFeatures ?? [])]; | ||
|
|
||
| if (supportsAdaptiveThinking(this._model)) { | ||
| if (adaptive) { | ||
| newBetas = newBetas.filter((b) => b !== INTERLEAVED_THINKING_BETA); | ||
| return this._withGenerationKwargs({ | ||
| thinking: { type: 'adaptive', display: 'summarized' }, | ||
|
|
@@ -1053,13 +1066,13 @@ export class AnthropicChatProvider implements ChatProvider { | |
| thinking: { type: 'enabled', budget_tokens: budgetTokensForEffort(effectiveEffort) }, | ||
| betaFeatures: newBetas, | ||
| }; | ||
| if (supportsEffortParam(this._model)) { | ||
| if (supportsEffortParam(this._model, adaptive)) { | ||
| kwargs.output_config = { effort: effectiveEffort }; | ||
| } else { | ||
| kwargs.output_config = undefined; | ||
| } | ||
| const clone = this._withGenerationKwargs(kwargs); | ||
| if (!supportsEffortParam(this._model)) { | ||
| if (!supportsEffortParam(this._model, adaptive)) { | ||
| delete clone._generationKwargs.output_config; | ||
| } | ||
| return clone; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the documented custom-named Anthropic case, setting only
adaptive_thinking = trueleavescapabilitiesempty. The TUI model picker usesModelAlias.capabilitiesdirectly (apps/kimi-code/src/tui/components/dialogs/model-selector.ts:71-75) and treats aliases withoutthinkingas unsupported, so switching to such an alias forcesthinking=falsebefore the request ever reaches the provider; the runtime capability resolver also still relies on declared/detected capabilities (packages/agent-core/src/session/provider-manager.ts:200-205). Please have the new flag imply thethinkingcapability, or requirecapabilities = ["thinking"]alongside it in the config docs, so the advertised one-field opt-in actually enables thinking for custom model names.Useful? React with 👍 / 👎.