@namzu/anthropic 1.3.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +170 -0
- package/dist/__tests__/thinking-capability.test.d.ts +2 -0
- package/dist/__tests__/thinking-capability.test.d.ts.map +1 -0
- package/dist/__tests__/thinking-capability.test.js +178 -0
- package/dist/__tests__/thinking-capability.test.js.map +1 -0
- package/dist/__tests__/thinking-request-body.test.d.ts +2 -0
- package/dist/__tests__/thinking-request-body.test.d.ts.map +1 -0
- package/dist/__tests__/thinking-request-body.test.js +90 -0
- package/dist/__tests__/thinking-request-body.test.js.map +1 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +39 -21
- package/dist/client.js.map +1 -1
- package/dist/thinking-capability.d.ts +114 -0
- package/dist/thinking-capability.d.ts.map +1 -0
- package/dist/thinking-capability.js +132 -0
- package/dist/thinking-capability.js.map +1 -0
- package/package.json +2 -2
- package/src/__tests__/thinking-capability.test.ts +206 -0
- package/src/__tests__/thinking-request-body.test.ts +111 -0
- package/src/client.ts +52 -21
- package/src/thinking-capability.ts +209 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { ChatCompletionParams } from '@namzu/sdk'
|
|
2
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { AnthropicProvider } from '../client.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The resolver's own tests pass whether or not the client calls it. These
|
|
8
|
+
* drive the real `chatStream` and read the body that would have gone on the
|
|
9
|
+
* wire, because "declared, typed, exported, and never sent" is precisely the
|
|
10
|
+
* defect being fixed here — `display` had been on `ThinkingConfig` all along
|
|
11
|
+
* and the request builder dropped it.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
async function bodyFor(params: Partial<ChatCompletionParams>): Promise<Record<string, unknown>> {
|
|
15
|
+
const seen: { body?: Record<string, unknown> } = {}
|
|
16
|
+
const provider = new AnthropicProvider({ apiKey: 'test-key' })
|
|
17
|
+
;(provider as unknown as { client: { messages: { create: unknown } } }).client = {
|
|
18
|
+
messages: {
|
|
19
|
+
create: vi.fn(async (body: Record<string, unknown>) => {
|
|
20
|
+
seen.body = body
|
|
21
|
+
return (async function* () {
|
|
22
|
+
yield { type: 'message_start', message: { id: 'msg_1' } }
|
|
23
|
+
})()
|
|
24
|
+
}),
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
for await (const _chunk of provider.chatStream({
|
|
28
|
+
model: 'claude-sonnet-5',
|
|
29
|
+
messages: [{ role: 'user', content: 'hi' }],
|
|
30
|
+
...params,
|
|
31
|
+
} as ChatCompletionParams)) {
|
|
32
|
+
// drain
|
|
33
|
+
}
|
|
34
|
+
return seen.body ?? {}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe('the thinking configuration that reaches the wire', () => {
|
|
38
|
+
it('sends adaptive to a model that only accepts adaptive', async () => {
|
|
39
|
+
const body = await bodyFor({ model: 'claude-sonnet-5', thinking: { type: 'adaptive' } })
|
|
40
|
+
|
|
41
|
+
expect(body.thinking).toEqual({ type: 'adaptive' })
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('does not send a manual budget to a model that rejects manual mode', async () => {
|
|
45
|
+
// This is the failure being fixed: the old builder passed `enabled`
|
|
46
|
+
// straight through, and the vendor answers that with a 400.
|
|
47
|
+
const body = await bodyFor({
|
|
48
|
+
model: 'claude-sonnet-5',
|
|
49
|
+
thinking: { type: 'enabled', budgetTokens: 10_000 },
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
expect(body.thinking).toEqual({ type: 'adaptive' })
|
|
53
|
+
expect(JSON.stringify(body)).not.toContain('budget_tokens')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('still sends a manual budget to a manual model', async () => {
|
|
57
|
+
const body = await bodyFor({
|
|
58
|
+
model: 'claude-sonnet-4-5',
|
|
59
|
+
thinking: { type: 'enabled', budgetTokens: 10_000 },
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
expect(body.thinking).toEqual({ type: 'enabled', budget_tokens: 10_000 })
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('serializes display, without which the thinking text comes back empty', async () => {
|
|
66
|
+
const body = await bodyFor({
|
|
67
|
+
model: 'claude-sonnet-5',
|
|
68
|
+
thinking: { type: 'adaptive', display: 'summarized' },
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
expect(body.thinking).toEqual({ type: 'adaptive', display: 'summarized' })
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('sends effort beside thinking, not inside it', async () => {
|
|
75
|
+
const body = await bodyFor({
|
|
76
|
+
model: 'claude-sonnet-5',
|
|
77
|
+
thinking: { type: 'adaptive' },
|
|
78
|
+
effort: 'xhigh',
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
expect(body.output_config).toEqual({ effort: 'xhigh' })
|
|
82
|
+
expect(body.thinking).toEqual({ type: 'adaptive' })
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('sends effort even with no thinking configuration at all', async () => {
|
|
86
|
+
// Effort shapes the whole response, so it is not conditional on a
|
|
87
|
+
// thinking block being present.
|
|
88
|
+
const body = await bodyFor({ model: 'claude-sonnet-5', effort: 'low' })
|
|
89
|
+
|
|
90
|
+
expect(body.output_config).toEqual({ effort: 'low' })
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('drops effort on a model that does not accept it', async () => {
|
|
94
|
+
const body = await bodyFor({ model: 'claude-sonnet-4-5', effort: 'high' })
|
|
95
|
+
|
|
96
|
+
expect(body.output_config).toBeUndefined()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('omits thinking entirely on a model that cannot be told to stop', async () => {
|
|
100
|
+
const body = await bodyFor({ model: 'claude-fable-5', thinking: { type: 'disabled' } })
|
|
101
|
+
|
|
102
|
+
expect(body.thinking).toBeUndefined()
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('sends no thinking field when the caller configured none', async () => {
|
|
106
|
+
const body = await bodyFor({ model: 'claude-sonnet-5' })
|
|
107
|
+
|
|
108
|
+
expect(body.thinking).toBeUndefined()
|
|
109
|
+
expect(body.output_config).toBeUndefined()
|
|
110
|
+
})
|
|
111
|
+
})
|
package/src/client.ts
CHANGED
|
@@ -15,11 +15,19 @@ import type {
|
|
|
15
15
|
} from '@namzu/sdk'
|
|
16
16
|
import {
|
|
17
17
|
ProviderRequestError,
|
|
18
|
+
assertStrictSchema,
|
|
18
19
|
isCallerAbortError,
|
|
19
20
|
isProviderRequestError,
|
|
21
|
+
modelVersionAtLeast,
|
|
20
22
|
providerVendorError,
|
|
21
23
|
toolResultToText,
|
|
22
24
|
} from '@namzu/sdk'
|
|
25
|
+
import {
|
|
26
|
+
MODEL_ID_GRAMMAR,
|
|
27
|
+
resolveEffort,
|
|
28
|
+
resolveThinkingBody,
|
|
29
|
+
resolveThinkingCapability,
|
|
30
|
+
} from './thinking-capability.js'
|
|
23
31
|
import type { AnthropicConfig } from './types.js'
|
|
24
32
|
|
|
25
33
|
// Floor for `max_tokens` when neither the request nor the provider
|
|
@@ -358,6 +366,12 @@ function toAnthropicTools(
|
|
|
358
366
|
const enforcedNames = new Set(params.enforceToolInputSchema ?? [])
|
|
359
367
|
const tools: AnthropicToolParam[] = params.tools.map((t) => {
|
|
360
368
|
const strict = strictToolUseEnabled && enforcedNames.has(t.function.name)
|
|
369
|
+
// Checked exactly where the pairing is made, because this is the only
|
|
370
|
+
// place both facts are in hand: that this schema is going out, and that
|
|
371
|
+
// this driver is about to vouch for it. A schema outside the strict
|
|
372
|
+
// subset is not degraded by the vendor — the whole request is rejected,
|
|
373
|
+
// so one unexpressible field takes down every other tool with it.
|
|
374
|
+
if (strict) assertStrictSchema(t.function.name, t.function.parameters)
|
|
361
375
|
return {
|
|
362
376
|
name: t.function.name,
|
|
363
377
|
description: t.function.description ?? '',
|
|
@@ -384,13 +398,11 @@ function shouldUseStrictToolInputs(
|
|
|
384
398
|
|
|
385
399
|
const normalized = model.toLowerCase()
|
|
386
400
|
if (/^(?:anthropic\/)?claude-mythos-preview$/.test(normalized)) return true
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const minor = Number(version[2] ?? 0)
|
|
393
|
-
return major > 4 || (major === 4 && minor >= 5)
|
|
401
|
+
// Shared matcher. The copy that stood here read the 8-digit date suffix as
|
|
402
|
+
// the minor version, so `claude-sonnet-4-20250514` compared as 4.20250514
|
|
403
|
+
// and cleared this 4.5 gate — strict tool inputs were enabled for a model
|
|
404
|
+
// below the threshold.
|
|
405
|
+
return modelVersionAtLeast(normalized, MODEL_ID_GRAMMAR, 4, 5)
|
|
394
406
|
}
|
|
395
407
|
|
|
396
408
|
function toAnthropicToolChoice(tc?: ToolChoice, parallelToolCalls?: boolean): unknown {
|
|
@@ -451,6 +463,15 @@ interface RawAnthropicUsage {
|
|
|
451
463
|
output_tokens?: number
|
|
452
464
|
cache_read_input_tokens?: number | null
|
|
453
465
|
cache_creation_input_tokens?: number | null
|
|
466
|
+
/**
|
|
467
|
+
* Reasoning share of the output, when the vendor reports it.
|
|
468
|
+
*
|
|
469
|
+
* Counted INSIDE `output_tokens`, so it is carried as a breakdown rather
|
|
470
|
+
* than added to the total. When streaming, this arrives only on the final
|
|
471
|
+
* `message_delta` — earlier events carry no breakdown at all, which is why
|
|
472
|
+
* an absent value has to mean "not reported" and not "zero".
|
|
473
|
+
*/
|
|
474
|
+
output_tokens_details?: { thinking_tokens?: number } | null
|
|
454
475
|
}
|
|
455
476
|
|
|
456
477
|
function emptyUsage(): TokenUsage {
|
|
@@ -467,12 +488,17 @@ function parseUsage(raw?: RawAnthropicUsage): TokenUsage {
|
|
|
467
488
|
if (!raw) return emptyUsage()
|
|
468
489
|
const input = raw.input_tokens ?? 0
|
|
469
490
|
const output = raw.output_tokens ?? 0
|
|
491
|
+
const thinking = raw.output_tokens_details?.thinking_tokens
|
|
470
492
|
return {
|
|
471
493
|
promptTokens: input,
|
|
472
494
|
completionTokens: output,
|
|
473
495
|
totalTokens: input + output,
|
|
474
496
|
cachedTokens: raw.cache_read_input_tokens ?? 0,
|
|
475
497
|
cacheWriteTokens: raw.cache_creation_input_tokens ?? 0,
|
|
498
|
+
// Only when reported. Defaulting to 0 would say "this turn did no
|
|
499
|
+
// thinking" about every turn on every driver that stays silent, and
|
|
500
|
+
// about every streamed event before the last one.
|
|
501
|
+
...(thinking !== undefined ? { reasoningTokens: thinking } : {}),
|
|
476
502
|
}
|
|
477
503
|
}
|
|
478
504
|
|
|
@@ -686,20 +712,25 @@ export class AnthropicProvider implements LLMProvider {
|
|
|
686
712
|
// otherwise) — this also drops a parallelToolCalls-derived choice on
|
|
687
713
|
// tool-less requests.
|
|
688
714
|
if (tools && toolChoice) body.tool_choice = toolChoice
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
715
|
+
// Resolved against the model rather than sent verbatim. This used to
|
|
716
|
+
// map `enabled` straight through and everything else to `disabled`,
|
|
717
|
+
// which fails outright on current models: `thinking.type.enabled` is
|
|
718
|
+
// rejected with a 400 from 4.7 onward, `adaptive` is rejected on 4.5
|
|
719
|
+
// and earlier, and the always-on models reject `disabled`. One body
|
|
720
|
+
// for every model is not a compromise here, it is a failed request.
|
|
721
|
+
//
|
|
722
|
+
// `display` is carried through now too. It defaults to `omitted` on
|
|
723
|
+
// newer models, so a caller who wanted to show reasoning and never
|
|
724
|
+
// serialized the field received thinking blocks with empty text and
|
|
725
|
+
// nothing to explain why.
|
|
726
|
+
const capability = resolveThinkingCapability(params.model)
|
|
727
|
+
const thinkingBody = resolveThinkingBody(params.thinking, capability)
|
|
728
|
+
if (thinkingBody) body.thinking = thinkingBody
|
|
729
|
+
|
|
730
|
+
// A sibling of `thinking`, not a field inside it — and gated on the
|
|
731
|
+
// model, since only some accept it at all.
|
|
732
|
+
const effort = resolveEffort(params.effort, thinkingBody, capability)
|
|
733
|
+
if (effort) body.output_config = { effort }
|
|
703
734
|
if (params.temperature !== undefined) body.temperature = params.temperature
|
|
704
735
|
if (params.topP !== undefined) body.top_p = params.topP
|
|
705
736
|
if (params.topK !== undefined) body.top_k = params.topK
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { parseVersionedModelId } from '@namzu/sdk'
|
|
2
|
+
import type { ModelIdGrammar, ReasoningEffort, ThinkingConfig } from '@namzu/sdk'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a given model will actually accept for thinking.
|
|
6
|
+
*
|
|
7
|
+
* This exists because the vendor **rejects** a mismatched mode rather than
|
|
8
|
+
* degrading it. A request carrying `thinking: {type: "enabled"}` to a current
|
|
9
|
+
* model comes back 400 with `"thinking.type.enabled" is not supported for this
|
|
10
|
+
* model`; the same request with `"adaptive"` to an older one comes back
|
|
11
|
+
* `adaptive thinking is not supported on this model`. So sending one body to
|
|
12
|
+
* every model is not a compatibility compromise, it is a failed request — and
|
|
13
|
+
* the SDK's `ThinkingConfig` is deliberately a statement of *intent* that each
|
|
14
|
+
* driver resolves against the model it is about to call.
|
|
15
|
+
*
|
|
16
|
+
* The split, from the vendor's per-model table:
|
|
17
|
+
*
|
|
18
|
+
* - **Adaptive only** — Fable 5, Mythos 5, Opus 5, Sonnet 5, Opus 4.8, Opus
|
|
19
|
+
* 4.7. These reject `enabled`. Fable 5 and Mythos 5 additionally reject
|
|
20
|
+
* `disabled` because they cannot stop thinking at all.
|
|
21
|
+
* - **Both** — Opus 4.6, Sonnet 4.6 (manual mode deprecated but working), and
|
|
22
|
+
* Mythos Preview (which rejects `disabled`).
|
|
23
|
+
* - **Manual only** — Opus 4.5, Sonnet 4.5, Haiku 4.5, Opus 4.1. These reject
|
|
24
|
+
* `adaptive`.
|
|
25
|
+
*
|
|
26
|
+
* Unknown models are treated as manual-only, which is the pre-existing
|
|
27
|
+
* behaviour and therefore the safe answer for a name this table has not seen:
|
|
28
|
+
* a third-party gateway serving an older model keeps working, and the failure
|
|
29
|
+
* mode for a newer one is a clear vendor 400 rather than a silently altered
|
|
30
|
+
* request.
|
|
31
|
+
*/
|
|
32
|
+
/**
|
|
33
|
+
* The effort levels a model accepts — a SET, not a flag.
|
|
34
|
+
*
|
|
35
|
+
* Modelling this as a boolean was wrong in the same way `oneOf` under strict
|
|
36
|
+
* was wrong: the vendor rejects a level a model does not have rather than
|
|
37
|
+
* clamping it, so `xhigh` on a 4.6 and `max` on a 4.5 were 400s this driver
|
|
38
|
+
* forwarded without noticing. The ceiling moved twice — `xhigh` arrived with
|
|
39
|
+
* 4.7, and `max` is absent below 4.6 — so there is no single answer to encode.
|
|
40
|
+
*
|
|
41
|
+
* Empty means the model takes no `effort` field at all.
|
|
42
|
+
*/
|
|
43
|
+
export type EffortLevels = readonly ReasoningEffort[]
|
|
44
|
+
|
|
45
|
+
const NO_EFFORT: EffortLevels = []
|
|
46
|
+
/** Everything from 4.7 onward, plus the always-on families. */
|
|
47
|
+
const FULL_EFFORT: EffortLevels = ['low', 'medium', 'high', 'xhigh', 'max']
|
|
48
|
+
/** 4.6: `max` exists, `xhigh` does not — it arrived with 4.7. */
|
|
49
|
+
const EFFORT_WITHOUT_XHIGH: EffortLevels = ['low', 'medium', 'high', 'max']
|
|
50
|
+
/** 4.5: neither `xhigh` nor `max`. */
|
|
51
|
+
const EFFORT_BASE: EffortLevels = ['low', 'medium', 'high']
|
|
52
|
+
|
|
53
|
+
export interface ThinkingCapability {
|
|
54
|
+
readonly adaptive: boolean
|
|
55
|
+
readonly manual: boolean
|
|
56
|
+
/** False on models that cannot turn thinking off. */
|
|
57
|
+
readonly canDisable: boolean
|
|
58
|
+
/** Which `effort` levels this model accepts; empty means none. */
|
|
59
|
+
readonly effort: EffortLevels
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const MANUAL_ONLY: ThinkingCapability = {
|
|
63
|
+
adaptive: false,
|
|
64
|
+
manual: true,
|
|
65
|
+
canDisable: true,
|
|
66
|
+
effort: NO_EFFORT,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The vocabulary this driver's ids are spelled in.
|
|
71
|
+
*
|
|
72
|
+
* The matcher itself is `parseVersionedModelId` in the kernel; only these
|
|
73
|
+
* words are local, because only this package knows them. The comment that
|
|
74
|
+
* stood here warned that "a second, subtly different model matcher in the same
|
|
75
|
+
* file is how two capability decisions drift apart on the same model name" —
|
|
76
|
+
* and there were three copies of it, this file and two drivers, all with the
|
|
77
|
+
* same defect: the minor group was `\d+`, so it swallowed the 8-digit date
|
|
78
|
+
* suffix and a dated id naming no minor parsed as minor 20250514. The warning
|
|
79
|
+
* was right, and the answer to it was not another careful copy.
|
|
80
|
+
*/
|
|
81
|
+
export const MODEL_ID_GRAMMAR: ModelIdGrammar = {
|
|
82
|
+
product: 'claude',
|
|
83
|
+
families: ['haiku', 'sonnet', 'opus', 'fable', 'mythos'],
|
|
84
|
+
routingPrefix: 'anthropic/',
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function resolveThinkingCapability(model: string): ThinkingCapability {
|
|
88
|
+
const normalized = model.toLowerCase()
|
|
89
|
+
|
|
90
|
+
// Named rather than versioned: the preview carries no version number the
|
|
91
|
+
// parser can compare, and it is the one model supporting both modes while
|
|
92
|
+
// refusing to be switched off.
|
|
93
|
+
if (/^(?:anthropic\/)?claude-mythos-preview$/.test(normalized)) {
|
|
94
|
+
return { adaptive: true, manual: true, canDisable: false, effort: FULL_EFFORT }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const parsed = parseVersionedModelId(model, MODEL_ID_GRAMMAR)
|
|
98
|
+
if (!parsed) return MANUAL_ONLY
|
|
99
|
+
|
|
100
|
+
const { family, major, minor } = parsed
|
|
101
|
+
|
|
102
|
+
// Always-on families: thinking cannot be disabled at any version.
|
|
103
|
+
if (family === 'fable' || family === 'mythos') {
|
|
104
|
+
return { adaptive: true, manual: false, canDisable: false, effort: FULL_EFFORT }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 4.7 and later dropped manual mode outright.
|
|
108
|
+
if (major > 4 || (major === 4 && minor >= 7)) {
|
|
109
|
+
return { adaptive: true, manual: false, canDisable: true, effort: FULL_EFFORT }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 4.6 accepts both; manual is deprecated there but still functional.
|
|
113
|
+
if (major === 4 && minor === 6) {
|
|
114
|
+
return { adaptive: true, manual: true, canDisable: true, effort: EFFORT_WITHOUT_XHIGH }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 4.5 and earlier are manual-only. Opus 4.5 is the one that also takes
|
|
118
|
+
// effort, where effort shapes the answer and the budget sets depth — and it
|
|
119
|
+
// takes only the first three levels: `xhigh` did not exist yet and `max`
|
|
120
|
+
// is rejected there.
|
|
121
|
+
const isOpus45 = family === 'opus' && major === 4 && minor === 5
|
|
122
|
+
return { ...MANUAL_ONLY, effort: isOpus45 ? EFFORT_BASE : NO_EFFORT }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The `thinking` body value, or `undefined` to send no thinking field. */
|
|
126
|
+
export type ResolvedThinkingBody =
|
|
127
|
+
| { type: 'adaptive'; display?: 'summarized' | 'omitted' }
|
|
128
|
+
| { type: 'enabled'; budget_tokens?: number; display?: 'summarized' | 'omitted' }
|
|
129
|
+
| { type: 'disabled' }
|
|
130
|
+
| undefined
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Turn a declared intent into a body this model accepts, or into nothing.
|
|
134
|
+
*
|
|
135
|
+
* Omission is a real answer and the reason this returns `undefined` rather
|
|
136
|
+
* than throwing. A caller asking to disable thinking on a model that cannot
|
|
137
|
+
* disable it has asked for something impossible — but failing the request
|
|
138
|
+
* teaches nothing the vendor's own 400 would not, and it breaks a caller whose
|
|
139
|
+
* config is shared across models. Sending no `thinking` field leaves the model
|
|
140
|
+
* on its documented default, which is the closest honest reading of "I did not
|
|
141
|
+
* want to spend on this".
|
|
142
|
+
*
|
|
143
|
+
* The same holds in reverse: an `enabled` intent on an adaptive-only model
|
|
144
|
+
* becomes `adaptive`, because the caller asked for thinking and this model's
|
|
145
|
+
* way of thinking is adaptive. The budget is dropped with it — it has no
|
|
146
|
+
* meaning there, and `effort` is the depth control instead.
|
|
147
|
+
*/
|
|
148
|
+
export function resolveThinkingBody(
|
|
149
|
+
thinking: ThinkingConfig | undefined,
|
|
150
|
+
capability: ThinkingCapability,
|
|
151
|
+
): ResolvedThinkingBody {
|
|
152
|
+
if (!thinking) return undefined
|
|
153
|
+
|
|
154
|
+
const display = thinking.display ? { display: thinking.display } : {}
|
|
155
|
+
|
|
156
|
+
if (thinking.type === 'disabled') {
|
|
157
|
+
return capability.canDisable ? { type: 'disabled' } : undefined
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (thinking.type === 'adaptive') {
|
|
161
|
+
if (capability.adaptive) return { type: 'adaptive', ...display }
|
|
162
|
+
// Manual-only model: honour the intent to think, with the one mode it
|
|
163
|
+
// has. No budget was given, so the vendor default applies.
|
|
164
|
+
return capability.manual ? { type: 'enabled', ...display } : undefined
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// 'enabled'
|
|
168
|
+
if (capability.manual) {
|
|
169
|
+
return {
|
|
170
|
+
type: 'enabled',
|
|
171
|
+
...(thinking.budgetTokens !== undefined ? { budget_tokens: thinking.budgetTokens } : {}),
|
|
172
|
+
...display,
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return capability.adaptive ? { type: 'adaptive', ...display } : undefined
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Whether `effort` may ride on this request.
|
|
180
|
+
*
|
|
181
|
+
* Two things get checked, and the second used to be missed entirely.
|
|
182
|
+
*
|
|
183
|
+
* The LEVEL has to be one this model has. The ceiling moved twice — `xhigh`
|
|
184
|
+
* arrived with 4.7, and `max` does not exist below 4.6 — so `capability.effort`
|
|
185
|
+
* is a set rather than a flag. It was a flag, which meant `xhigh` on a 4.6 and
|
|
186
|
+
* `max` on a 4.5 were forwarded to a vendor that rejects an unknown level
|
|
187
|
+
* rather than clamping it. Dropping the field is right where the level does
|
|
188
|
+
* not exist: `effort` shapes an answer the model will still produce, so a
|
|
189
|
+
* request without it is the same request at the model's own default, whereas
|
|
190
|
+
* refusing would fail a call that has a correct answer.
|
|
191
|
+
*
|
|
192
|
+
* The COMBINATION has to be legal. On Opus 5 and later,
|
|
193
|
+
* `thinking: {type: "disabled"}` is accepted at effort `high` or below and
|
|
194
|
+
* **rejected at `xhigh`/`max`**, enforced per request. Rather than encode
|
|
195
|
+
* "Opus 5 and later" as a second version comparison, this refuses the pairing
|
|
196
|
+
* on every model that can disable thinking — it is incoherent anyway, since it
|
|
197
|
+
* asks a model not to think and then to think as hard as possible.
|
|
198
|
+
*/
|
|
199
|
+
export function resolveEffort(
|
|
200
|
+
effort: ReasoningEffort | undefined,
|
|
201
|
+
thinkingBody: ResolvedThinkingBody,
|
|
202
|
+
capability: ThinkingCapability,
|
|
203
|
+
): ReasoningEffort | undefined {
|
|
204
|
+
if (effort === undefined || !capability.effort.includes(effort)) return undefined
|
|
205
|
+
if (thinkingBody?.type === 'disabled' && (effort === 'xhigh' || effort === 'max')) {
|
|
206
|
+
return undefined
|
|
207
|
+
}
|
|
208
|
+
return effort
|
|
209
|
+
}
|