@namzu/anthropic 1.2.0 → 2.0.0
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 +336 -0
- package/dist/__tests__/anthropic.test.js +1 -0
- package/dist/__tests__/anthropic.test.js.map +1 -1
- package/dist/__tests__/block-shape-guard.test.d.ts +2 -0
- package/dist/__tests__/block-shape-guard.test.d.ts.map +1 -0
- package/dist/__tests__/block-shape-guard.test.js +218 -0
- package/dist/__tests__/block-shape-guard.test.js.map +1 -0
- package/dist/__tests__/cache-anchors.test.d.ts +2 -0
- package/dist/__tests__/cache-anchors.test.d.ts.map +1 -0
- package/dist/__tests__/cache-anchors.test.js +220 -0
- package/dist/__tests__/cache-anchors.test.js.map +1 -0
- package/dist/__tests__/citations.test.d.ts +2 -0
- package/dist/__tests__/citations.test.d.ts.map +1 -0
- package/dist/__tests__/citations.test.js +189 -0
- package/dist/__tests__/citations.test.js.map +1 -0
- package/dist/__tests__/content-blocks.test.d.ts +2 -0
- package/dist/__tests__/content-blocks.test.d.ts.map +1 -0
- package/dist/__tests__/content-blocks.test.js +131 -0
- package/dist/__tests__/content-blocks.test.js.map +1 -0
- package/dist/__tests__/document-input.test.d.ts +2 -0
- package/dist/__tests__/document-input.test.d.ts.map +1 -0
- package/dist/__tests__/document-input.test.js +98 -0
- package/dist/__tests__/document-input.test.js.map +1 -0
- package/dist/__tests__/reasoning-close.test.d.ts +2 -0
- package/dist/__tests__/reasoning-close.test.d.ts.map +1 -0
- package/dist/__tests__/reasoning-close.test.js +219 -0
- package/dist/__tests__/reasoning-close.test.js.map +1 -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 +127 -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 +186 -3
- package/dist/client.js.map +1 -1
- package/dist/thinking-capability.d.ts +78 -0
- package/dist/thinking-capability.d.ts.map +1 -0
- package/dist/thinking-capability.js +114 -0
- package/dist/thinking-capability.js.map +1 -0
- package/package.json +4 -4
- package/src/__tests__/anthropic.test.ts +1 -0
- package/src/__tests__/block-shape-guard.test.ts +256 -0
- package/src/__tests__/cache-anchors.test.ts +262 -0
- package/src/__tests__/citations.test.ts +218 -0
- package/src/__tests__/content-blocks.test.ts +153 -0
- package/src/__tests__/document-input.test.ts +131 -0
- package/src/__tests__/reasoning-close.test.ts +262 -0
- package/src/__tests__/thinking-capability.test.ts +150 -0
- package/src/__tests__/thinking-request-body.test.ts +111 -0
- package/src/client.ts +265 -7
- package/src/thinking-capability.ts +177 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import type { ChatCompletionParams, StreamChunk } from '@namzu/sdk'
|
|
2
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { AnthropicProvider } from '../client.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Extended thinking was fully specified in the SDK and entirely absent
|
|
8
|
+
* from this driver.
|
|
9
|
+
*
|
|
10
|
+
* The stream chunk carried a `reasoning` channel whose own comment named
|
|
11
|
+
* the bug — "`thinking_delta` and `signature_delta` fell through the
|
|
12
|
+
* driver's `default: // ignore`" — the run's aggregator bucketed fragments
|
|
13
|
+
* by index and closed them on `done`, and `ReasoningBlock` recorded the
|
|
14
|
+
* signature with a note that replaying it unchanged is mandatory. The
|
|
15
|
+
* driver requested no thinking, parsed none, and replayed none.
|
|
16
|
+
*
|
|
17
|
+
* The close matters as much as the content. A block is only complete once
|
|
18
|
+
* its signature has arrived, and the signature arrives last: without a
|
|
19
|
+
* `done` the aggregator cannot tell a finished block from one still
|
|
20
|
+
* streaming, and a block replayed without its signature invalidates the
|
|
21
|
+
* whole conversation upstream — not just that block.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
function providerOver(events: unknown[], seen: { body?: Record<string, unknown> } = {}) {
|
|
25
|
+
const provider = new AnthropicProvider({ apiKey: 'test-key' })
|
|
26
|
+
;(provider as unknown as { client: { messages: { create: unknown } } }).client = {
|
|
27
|
+
messages: {
|
|
28
|
+
create: vi.fn(async (body: Record<string, unknown>) => {
|
|
29
|
+
seen.body = body
|
|
30
|
+
return (async function* () {
|
|
31
|
+
for (const event of events) yield event
|
|
32
|
+
})()
|
|
33
|
+
}),
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
return provider
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function chunksOf(events: unknown[]): Promise<StreamChunk[]> {
|
|
40
|
+
const out: StreamChunk[] = []
|
|
41
|
+
for await (const chunk of providerOver(events).chatStream({
|
|
42
|
+
model: 'm',
|
|
43
|
+
messages: [{ role: 'user', content: 'q' }],
|
|
44
|
+
} as ChatCompletionParams)) {
|
|
45
|
+
out.push(chunk)
|
|
46
|
+
}
|
|
47
|
+
return out
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const THINKING_STREAM = [
|
|
51
|
+
{ type: 'message_start', message: { id: 'msg_1' } },
|
|
52
|
+
{ type: 'content_block_start', index: 0, content_block: { type: 'thinking' } },
|
|
53
|
+
{
|
|
54
|
+
type: 'content_block_delta',
|
|
55
|
+
index: 0,
|
|
56
|
+
delta: { type: 'thinking_delta', thinking: 'step one' },
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: 'content_block_delta',
|
|
60
|
+
index: 0,
|
|
61
|
+
delta: { type: 'thinking_delta', thinking: ' then two' },
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
type: 'content_block_delta',
|
|
65
|
+
index: 0,
|
|
66
|
+
delta: { type: 'signature_delta', signature: 'sig-abc' },
|
|
67
|
+
},
|
|
68
|
+
{ type: 'content_block_stop', index: 0 },
|
|
69
|
+
{ type: 'content_block_start', index: 1, content_block: { type: 'text' } },
|
|
70
|
+
{ type: 'content_block_delta', index: 1, delta: { type: 'text_delta', text: 'the answer' } },
|
|
71
|
+
{ type: 'message_delta', delta: { stop_reason: 'end_turn' } },
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
function reasoningOf(chunks: StreamChunk[]) {
|
|
75
|
+
return chunks.map((c) => c.delta.reasoning).filter((r): r is NonNullable<typeof r> => Boolean(r))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
describe('a reasoning block streams, then closes', () => {
|
|
79
|
+
it('opens the block with its kind', async () => {
|
|
80
|
+
const reasoning = reasoningOf(await chunksOf(THINKING_STREAM))
|
|
81
|
+
|
|
82
|
+
expect(reasoning[0]).toMatchObject({ index: 0, type: 'thinking' })
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('carries each fragment through in order', async () => {
|
|
86
|
+
const reasoning = reasoningOf(await chunksOf(THINKING_STREAM))
|
|
87
|
+
|
|
88
|
+
expect(reasoning.map((r) => r.text).filter(Boolean)).toEqual(['step one', ' then two'])
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('carries the signature, which arrives once at the end', async () => {
|
|
92
|
+
const reasoning = reasoningOf(await chunksOf(THINKING_STREAM))
|
|
93
|
+
|
|
94
|
+
expect(reasoning.find((r) => r.signature)?.signature).toBe('sig-abc')
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('closes the block so the aggregator knows the signature has landed', async () => {
|
|
98
|
+
const reasoning = reasoningOf(await chunksOf(THINKING_STREAM))
|
|
99
|
+
|
|
100
|
+
expect(reasoning.at(-1)).toEqual({ index: 0, done: true })
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('closes it before the text block starts, not after', async () => {
|
|
104
|
+
const chunks = await chunksOf(THINKING_STREAM)
|
|
105
|
+
const closeAt = chunks.findIndex((c) => c.delta.reasoning?.done)
|
|
106
|
+
const textAt = chunks.findIndex((c) => c.delta.content === 'the answer')
|
|
107
|
+
|
|
108
|
+
expect(closeAt).toBeGreaterThan(-1)
|
|
109
|
+
expect(closeAt).toBeLessThan(textAt)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('does not leak reasoning into the visible text', async () => {
|
|
113
|
+
const chunks = await chunksOf(THINKING_STREAM)
|
|
114
|
+
|
|
115
|
+
expect(chunks.map((c) => c.delta.content).filter(Boolean)).toEqual(['the answer'])
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('groups two blocks by their own index', async () => {
|
|
119
|
+
const reasoning = reasoningOf(
|
|
120
|
+
await chunksOf([
|
|
121
|
+
{ type: 'message_start', message: { id: 'm' } },
|
|
122
|
+
{ type: 'content_block_start', index: 0, content_block: { type: 'thinking' } },
|
|
123
|
+
{ type: 'content_block_delta', index: 0, delta: { type: 'thinking_delta', thinking: 'a' } },
|
|
124
|
+
{ type: 'content_block_stop', index: 0 },
|
|
125
|
+
{ type: 'content_block_start', index: 1, content_block: { type: 'thinking' } },
|
|
126
|
+
{ type: 'content_block_delta', index: 1, delta: { type: 'thinking_delta', thinking: 'b' } },
|
|
127
|
+
{ type: 'content_block_stop', index: 1 },
|
|
128
|
+
]),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
expect(reasoning.filter((r) => r.done).map((r) => r.index)).toEqual([0, 1])
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('carries a redacted block as an opaque payload rather than text', async () => {
|
|
135
|
+
const reasoning = reasoningOf(
|
|
136
|
+
await chunksOf([
|
|
137
|
+
{ type: 'message_start', message: { id: 'm' } },
|
|
138
|
+
{
|
|
139
|
+
type: 'content_block_start',
|
|
140
|
+
index: 0,
|
|
141
|
+
content_block: { type: 'redacted_thinking', data: 'opaque-bytes' },
|
|
142
|
+
},
|
|
143
|
+
{ type: 'content_block_stop', index: 0 },
|
|
144
|
+
]),
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
expect(reasoning[0]).toMatchObject({ type: 'redacted_thinking', encrypted: 'opaque-bytes' })
|
|
148
|
+
expect(reasoning[0]?.text).toBeUndefined()
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('still closes a tool call, which shares the same stop event', async () => {
|
|
152
|
+
const chunks = await chunksOf([
|
|
153
|
+
{ type: 'message_start', message: { id: 'm' } },
|
|
154
|
+
{
|
|
155
|
+
type: 'content_block_start',
|
|
156
|
+
index: 0,
|
|
157
|
+
content_block: { type: 'tool_use', id: 'call_1', name: 'read' },
|
|
158
|
+
},
|
|
159
|
+
{ type: 'content_block_stop', index: 0 },
|
|
160
|
+
])
|
|
161
|
+
|
|
162
|
+
expect(chunks.some((c) => c.delta.toolCallEnd?.id === 'call_1')).toBe(true)
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
describe('thinking can be asked for', () => {
|
|
167
|
+
async function bodyFor(thinking: unknown): Promise<Record<string, unknown>> {
|
|
168
|
+
const seen: { body?: Record<string, unknown> } = {}
|
|
169
|
+
const provider = providerOver([{ type: 'message_start', message: { id: 'm' } }], seen)
|
|
170
|
+
for await (const _chunk of provider.chatStream({
|
|
171
|
+
model: 'm',
|
|
172
|
+
messages: [{ role: 'user', content: 'q' }],
|
|
173
|
+
...(thinking !== undefined ? { thinking } : {}),
|
|
174
|
+
} as ChatCompletionParams)) {
|
|
175
|
+
// drain
|
|
176
|
+
}
|
|
177
|
+
return seen.body ?? {}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
it('sends the budget the caller set', async () => {
|
|
181
|
+
expect(await bodyFor({ type: 'enabled', budgetTokens: 4096 })).toMatchObject({
|
|
182
|
+
thinking: { type: 'enabled', budget_tokens: 4096 },
|
|
183
|
+
})
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it('enables it without a budget when none was named', async () => {
|
|
187
|
+
expect((await bodyFor({ type: 'enabled' })).thinking).toEqual({ type: 'enabled' })
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
it('can turn it off on a model where it is on by default', async () => {
|
|
191
|
+
expect((await bodyFor({ type: 'disabled' })).thinking).toEqual({ type: 'disabled' })
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('says nothing at all when the caller did not ask', async () => {
|
|
195
|
+
expect(await bodyFor(undefined)).not.toHaveProperty('thinking')
|
|
196
|
+
})
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
describe('a reasoned turn is replayed verbatim on the next request', () => {
|
|
200
|
+
async function messagesFor(assistant: Record<string, unknown>) {
|
|
201
|
+
const seen: { body?: Record<string, unknown> } = {}
|
|
202
|
+
const provider = providerOver([{ type: 'message_start', message: { id: 'm' } }], seen)
|
|
203
|
+
for await (const _chunk of provider.chatStream({
|
|
204
|
+
model: 'm',
|
|
205
|
+
messages: [{ role: 'user', content: 'q' }, assistant, { role: 'user', content: 'and then?' }],
|
|
206
|
+
} as unknown as ChatCompletionParams)) {
|
|
207
|
+
// drain
|
|
208
|
+
}
|
|
209
|
+
return seen.body?.messages as Array<{ role: string; content: unknown }>
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
it('replays the block with its signature intact', async () => {
|
|
213
|
+
const messages = await messagesFor({
|
|
214
|
+
role: 'assistant',
|
|
215
|
+
content: 'because of that',
|
|
216
|
+
reasoning: [{ type: 'thinking', text: 'step one', signature: 'sig-abc' }],
|
|
217
|
+
})
|
|
218
|
+
const blocks = messages[1]?.content as Record<string, unknown>[]
|
|
219
|
+
|
|
220
|
+
expect(blocks[0]).toEqual({ type: 'thinking', thinking: 'step one', signature: 'sig-abc' })
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
it('puts the reasoning first, as the wire requires', async () => {
|
|
224
|
+
const messages = await messagesFor({
|
|
225
|
+
role: 'assistant',
|
|
226
|
+
content: 'because of that',
|
|
227
|
+
reasoning: [{ type: 'thinking', text: 'step one', signature: 'sig' }],
|
|
228
|
+
})
|
|
229
|
+
const blocks = messages[1]?.content as Record<string, unknown>[]
|
|
230
|
+
|
|
231
|
+
expect(blocks.map((b) => b.type)).toEqual(['thinking', 'text'])
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('replays it ahead of a tool call too', async () => {
|
|
235
|
+
const messages = await messagesFor({
|
|
236
|
+
role: 'assistant',
|
|
237
|
+
content: null,
|
|
238
|
+
reasoning: [{ type: 'thinking', text: 'i should read it', signature: 'sig' }],
|
|
239
|
+
toolCalls: [{ id: 'call_1', type: 'function', function: { name: 'read', arguments: '{}' } }],
|
|
240
|
+
})
|
|
241
|
+
const blocks = messages[1]?.content as Record<string, unknown>[]
|
|
242
|
+
|
|
243
|
+
expect(blocks.map((b) => b.type)).toEqual(['thinking', 'tool_use'])
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
it('replays a redacted block as its opaque payload', async () => {
|
|
247
|
+
const messages = await messagesFor({
|
|
248
|
+
role: 'assistant',
|
|
249
|
+
content: 'answer',
|
|
250
|
+
reasoning: [{ type: 'redacted_thinking', encrypted: 'opaque-bytes' }],
|
|
251
|
+
})
|
|
252
|
+
const blocks = messages[1]?.content as Record<string, unknown>[]
|
|
253
|
+
|
|
254
|
+
expect(blocks[0]).toEqual({ type: 'redacted_thinking', data: 'opaque-bytes' })
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
it('leaves an assistant turn that did not reason as a plain string', async () => {
|
|
258
|
+
const messages = await messagesFor({ role: 'assistant', content: 'just an answer' })
|
|
259
|
+
|
|
260
|
+
expect(messages[1]?.content).toBe('just an answer')
|
|
261
|
+
})
|
|
262
|
+
})
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
resolveEffort,
|
|
5
|
+
resolveThinkingBody,
|
|
6
|
+
resolveThinkingCapability,
|
|
7
|
+
} from '../thinking-capability.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The driver mapped `enabled` straight through and everything else to
|
|
11
|
+
* `disabled`. On a current model that is not a degraded request, it is a
|
|
12
|
+
* failed one: `thinking.type.enabled` is rejected with a 400 from 4.7 onward,
|
|
13
|
+
* `adaptive` is rejected on 4.5 and earlier, and the always-on models reject
|
|
14
|
+
* `disabled`.
|
|
15
|
+
*
|
|
16
|
+
* The table below is transcribed from the vendor's per-model reference, one
|
|
17
|
+
* case per row, because that table IS the specification — a summary of it in
|
|
18
|
+
* prose would be the thing that drifts.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** [model, adaptive, manual, canDisable, effort] */
|
|
22
|
+
const TABLE: readonly [string, boolean, boolean, boolean, boolean][] = [
|
|
23
|
+
['claude-fable-5', true, false, false, true],
|
|
24
|
+
['claude-mythos-5', true, false, false, true],
|
|
25
|
+
['claude-mythos-preview', true, true, false, true],
|
|
26
|
+
['claude-opus-5', true, false, true, true],
|
|
27
|
+
['claude-opus-4-8', true, false, true, true],
|
|
28
|
+
['claude-opus-4-7', true, false, true, true],
|
|
29
|
+
['claude-sonnet-5', true, false, true, true],
|
|
30
|
+
['claude-opus-4-6', true, true, true, true],
|
|
31
|
+
['claude-sonnet-4-6', true, true, true, true],
|
|
32
|
+
['claude-opus-4-5', false, true, true, true],
|
|
33
|
+
['claude-haiku-4-5', false, true, true, false],
|
|
34
|
+
['claude-sonnet-4-5', false, true, true, false],
|
|
35
|
+
['claude-opus-4-1', false, true, true, false],
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
describe('what each model accepts', () => {
|
|
39
|
+
for (const [model, adaptive, manual, canDisable, effort] of TABLE) {
|
|
40
|
+
it(`resolves ${model}`, () => {
|
|
41
|
+
expect(resolveThinkingCapability(model)).toEqual({ adaptive, manual, canDisable, effort })
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
it('tolerates a vendor prefix and a date suffix', () => {
|
|
46
|
+
expect(resolveThinkingCapability('anthropic/claude-sonnet-5')).toMatchObject({
|
|
47
|
+
adaptive: true,
|
|
48
|
+
manual: false,
|
|
49
|
+
})
|
|
50
|
+
expect(resolveThinkingCapability('claude-sonnet-4-5-20250929')).toMatchObject({
|
|
51
|
+
adaptive: false,
|
|
52
|
+
manual: true,
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('treats an unrecognised model as manual-only', () => {
|
|
57
|
+
// The pre-existing behaviour, and the safe answer for a name this
|
|
58
|
+
// table has not seen: an older model behind a gateway keeps working,
|
|
59
|
+
// and a newer one fails with the vendor's own clear 400 rather than
|
|
60
|
+
// with a request this driver quietly rewrote.
|
|
61
|
+
expect(resolveThinkingCapability('some-gateway/mystery-model')).toEqual({
|
|
62
|
+
adaptive: false,
|
|
63
|
+
manual: true,
|
|
64
|
+
canDisable: true,
|
|
65
|
+
effort: false,
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
describe('turning an intent into a body this model accepts', () => {
|
|
71
|
+
const adaptiveOnly = resolveThinkingCapability('claude-sonnet-5')
|
|
72
|
+
const manualOnly = resolveThinkingCapability('claude-sonnet-4-5')
|
|
73
|
+
const alwaysOn = resolveThinkingCapability('claude-fable-5')
|
|
74
|
+
|
|
75
|
+
it('sends adaptive to an adaptive model', () => {
|
|
76
|
+
expect(resolveThinkingBody({ type: 'adaptive' }, adaptiveOnly)).toEqual({ type: 'adaptive' })
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('rewrites a manual intent to adaptive where manual is rejected', () => {
|
|
80
|
+
// The caller asked to think. This model's way of thinking is adaptive,
|
|
81
|
+
// and the budget has no meaning there, so it goes.
|
|
82
|
+
expect(resolveThinkingBody({ type: 'enabled', budgetTokens: 10_000 }, adaptiveOnly)).toEqual({
|
|
83
|
+
type: 'adaptive',
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('rewrites an adaptive intent to manual where adaptive is rejected', () => {
|
|
88
|
+
expect(resolveThinkingBody({ type: 'adaptive' }, manualOnly)).toEqual({ type: 'enabled' })
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('keeps the budget on a model that has budgets', () => {
|
|
92
|
+
expect(resolveThinkingBody({ type: 'enabled', budgetTokens: 10_000 }, manualOnly)).toEqual({
|
|
93
|
+
type: 'enabled',
|
|
94
|
+
budget_tokens: 10_000,
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('carries display through, which is the whole reason thinking text arrives', () => {
|
|
99
|
+
// It defaults to `omitted` on newer models, so a caller who wants to
|
|
100
|
+
// show reasoning and never serializes this gets empty blocks.
|
|
101
|
+
expect(resolveThinkingBody({ type: 'adaptive', display: 'summarized' }, adaptiveOnly)).toEqual({
|
|
102
|
+
type: 'adaptive',
|
|
103
|
+
display: 'summarized',
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('omits the field entirely when a model cannot be told to stop thinking', () => {
|
|
108
|
+
// Not an error: failing here teaches nothing the vendor 400 would not,
|
|
109
|
+
// and it breaks a caller whose config spans models. No field means the
|
|
110
|
+
// model's documented default, which is the closest honest reading.
|
|
111
|
+
expect(resolveThinkingBody({ type: 'disabled' }, alwaysOn)).toBeUndefined()
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('honours disable where it is accepted', () => {
|
|
115
|
+
expect(resolveThinkingBody({ type: 'disabled' }, adaptiveOnly)).toEqual({ type: 'disabled' })
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('sends nothing when the caller asked for nothing', () => {
|
|
119
|
+
expect(resolveThinkingBody(undefined, adaptiveOnly)).toBeUndefined()
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
describe('when effort rides along', () => {
|
|
124
|
+
const adaptiveOnly = resolveThinkingCapability('claude-sonnet-5')
|
|
125
|
+
const noEffort = resolveThinkingCapability('claude-sonnet-4-5')
|
|
126
|
+
|
|
127
|
+
it('passes effort to a model that takes it', () => {
|
|
128
|
+
expect(resolveEffort('high', { type: 'adaptive' }, adaptiveOnly)).toBe('high')
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('drops effort on a model that does not', () => {
|
|
132
|
+
expect(resolveEffort('high', { type: 'enabled' }, noEffort)).toBeUndefined()
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('keeps effort on the one manual model that accepts it', () => {
|
|
136
|
+
// Opus 4.5: effort shapes the answer, the budget sets thinking depth,
|
|
137
|
+
// and both are meant to be set. So effort is not adaptive-only and
|
|
138
|
+
// must not be gated on the mode.
|
|
139
|
+
const opus45 = resolveThinkingCapability('claude-opus-4-5')
|
|
140
|
+
expect(resolveEffort('high', { type: 'enabled', budget_tokens: 8000 }, opus45)).toBe('high')
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('refuses the combination the vendor rejects', () => {
|
|
144
|
+
// Thinking off at effort xhigh/max is a 400 on Opus 5 and later, and
|
|
145
|
+
// is incoherent anyway: do not think, and think as hard as possible.
|
|
146
|
+
expect(resolveEffort('xhigh', { type: 'disabled' }, adaptiveOnly)).toBeUndefined()
|
|
147
|
+
expect(resolveEffort('max', { type: 'disabled' }, adaptiveOnly)).toBeUndefined()
|
|
148
|
+
expect(resolveEffort('high', { type: 'disabled' }, adaptiveOnly)).toBe('high')
|
|
149
|
+
})
|
|
150
|
+
})
|
|
@@ -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
|
+
})
|