@namzu/anthropic 3.0.1 → 3.1.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.
Files changed (77) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/dist/client.d.ts +15 -1
  3. package/dist/client.d.ts.map +1 -1
  4. package/dist/client.js +18 -0
  5. package/dist/client.js.map +1 -1
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +8 -0
  9. package/dist/index.js.map +1 -1
  10. package/package.json +12 -2
  11. package/src/client.ts +21 -0
  12. package/src/index.ts +18 -0
  13. package/dist/__tests__/anthropic.test.d.ts +0 -2
  14. package/dist/__tests__/anthropic.test.d.ts.map +0 -1
  15. package/dist/__tests__/anthropic.test.js +0 -425
  16. package/dist/__tests__/anthropic.test.js.map +0 -1
  17. package/dist/__tests__/background-delegation.live.test.d.ts +0 -2
  18. package/dist/__tests__/background-delegation.live.test.d.ts.map +0 -1
  19. package/dist/__tests__/background-delegation.live.test.js +0 -212
  20. package/dist/__tests__/background-delegation.live.test.js.map +0 -1
  21. package/dist/__tests__/block-shape-guard.test.d.ts +0 -2
  22. package/dist/__tests__/block-shape-guard.test.d.ts.map +0 -1
  23. package/dist/__tests__/block-shape-guard.test.js +0 -218
  24. package/dist/__tests__/block-shape-guard.test.js.map +0 -1
  25. package/dist/__tests__/cache-anchors.test.d.ts +0 -2
  26. package/dist/__tests__/cache-anchors.test.d.ts.map +0 -1
  27. package/dist/__tests__/cache-anchors.test.js +0 -220
  28. package/dist/__tests__/cache-anchors.test.js.map +0 -1
  29. package/dist/__tests__/citations.test.d.ts +0 -2
  30. package/dist/__tests__/citations.test.d.ts.map +0 -1
  31. package/dist/__tests__/citations.test.js +0 -189
  32. package/dist/__tests__/citations.test.js.map +0 -1
  33. package/dist/__tests__/content-blocks.test.d.ts +0 -2
  34. package/dist/__tests__/content-blocks.test.d.ts.map +0 -1
  35. package/dist/__tests__/content-blocks.test.js +0 -131
  36. package/dist/__tests__/content-blocks.test.js.map +0 -1
  37. package/dist/__tests__/document-input.test.d.ts +0 -2
  38. package/dist/__tests__/document-input.test.d.ts.map +0 -1
  39. package/dist/__tests__/document-input.test.js +0 -98
  40. package/dist/__tests__/document-input.test.js.map +0 -1
  41. package/dist/__tests__/error-taxonomy.test.d.ts +0 -16
  42. package/dist/__tests__/error-taxonomy.test.d.ts.map +0 -1
  43. package/dist/__tests__/error-taxonomy.test.js +0 -242
  44. package/dist/__tests__/error-taxonomy.test.js.map +0 -1
  45. package/dist/__tests__/reasoning-close.test.d.ts +0 -2
  46. package/dist/__tests__/reasoning-close.test.d.ts.map +0 -1
  47. package/dist/__tests__/reasoning-close.test.js +0 -219
  48. package/dist/__tests__/reasoning-close.test.js.map +0 -1
  49. package/dist/__tests__/thinking-capability.test.d.ts +0 -2
  50. package/dist/__tests__/thinking-capability.test.d.ts.map +0 -1
  51. package/dist/__tests__/thinking-capability.test.js +0 -206
  52. package/dist/__tests__/thinking-capability.test.js.map +0 -1
  53. package/dist/__tests__/thinking-contract.live.test.d.ts +0 -2
  54. package/dist/__tests__/thinking-contract.live.test.d.ts.map +0 -1
  55. package/dist/__tests__/thinking-contract.live.test.js +0 -97
  56. package/dist/__tests__/thinking-contract.live.test.js.map +0 -1
  57. package/dist/__tests__/thinking-request-body.test.d.ts +0 -2
  58. package/dist/__tests__/thinking-request-body.test.d.ts.map +0 -1
  59. package/dist/__tests__/thinking-request-body.test.js +0 -90
  60. package/dist/__tests__/thinking-request-body.test.js.map +0 -1
  61. package/dist/__tests__/wire-contract.live.test.d.ts +0 -2
  62. package/dist/__tests__/wire-contract.live.test.d.ts.map +0 -1
  63. package/dist/__tests__/wire-contract.live.test.js +0 -254
  64. package/dist/__tests__/wire-contract.live.test.js.map +0 -1
  65. package/src/__tests__/anthropic.test.ts +0 -490
  66. package/src/__tests__/background-delegation.live.test.ts +0 -243
  67. package/src/__tests__/block-shape-guard.test.ts +0 -256
  68. package/src/__tests__/cache-anchors.test.ts +0 -262
  69. package/src/__tests__/citations.test.ts +0 -218
  70. package/src/__tests__/content-blocks.test.ts +0 -153
  71. package/src/__tests__/document-input.test.ts +0 -131
  72. package/src/__tests__/error-taxonomy.test.ts +0 -274
  73. package/src/__tests__/reasoning-close.test.ts +0 -262
  74. package/src/__tests__/thinking-capability.test.ts +0 -235
  75. package/src/__tests__/thinking-contract.live.test.ts +0 -125
  76. package/src/__tests__/thinking-request-body.test.ts +0 -111
  77. package/src/__tests__/wire-contract.live.test.ts +0 -312
@@ -1,490 +0,0 @@
1
- import { DuplicateProviderError, ProviderRegistry } from '@namzu/sdk'
2
- import type { ChatCompletionParams, LLMToolSchema } from '@namzu/sdk'
3
- import { beforeEach, describe, expect, it } from 'vitest'
4
- import { AnthropicProvider } from '../client.js'
5
- import { ANTHROPIC_CAPABILITIES, registerAnthropic } from '../index.js'
6
-
7
- // Ensure a clean slate between tests. The sdk pre-registers 'mock' on import
8
- // via its sideEffects whitelist; we only need to clear 'anthropic' to make
9
- // re-registration deterministic across tests.
10
- beforeEach(() => {
11
- if (ProviderRegistry.isSupported('anthropic')) {
12
- ProviderRegistry.unregister('anthropic')
13
- }
14
- })
15
-
16
- describe('@namzu/anthropic', () => {
17
- describe('registerAnthropic()', () => {
18
- it("adds 'anthropic' to the ProviderRegistry", () => {
19
- expect(ProviderRegistry.isSupported('anthropic')).toBe(false)
20
- registerAnthropic()
21
- expect(ProviderRegistry.isSupported('anthropic')).toBe(true)
22
- expect(ProviderRegistry.listTypes()).toContain('anthropic')
23
- })
24
-
25
- it('throws DuplicateProviderError when called twice without options', () => {
26
- registerAnthropic()
27
- expect(() => registerAnthropic()).toThrowError(DuplicateProviderError)
28
- })
29
-
30
- it('allows re-registration when { replace: true } is passed', () => {
31
- registerAnthropic()
32
- expect(() => registerAnthropic({ replace: true })).not.toThrow()
33
- expect(ProviderRegistry.isSupported('anthropic')).toBe(true)
34
- })
35
-
36
- it('exposes capabilities through the registry after registration', () => {
37
- registerAnthropic()
38
- const caps = ProviderRegistry.getCapabilities('anthropic')
39
- expect(caps).toEqual(ANTHROPIC_CAPABILITIES)
40
- })
41
- })
42
-
43
- describe('ANTHROPIC_CAPABILITIES', () => {
44
- it('declares the expected capability flags', () => {
45
- expect(ANTHROPIC_CAPABILITIES).toEqual({
46
- supportsTools: true,
47
- supportsStreaming: true,
48
- supportsFunctionCalling: true,
49
- supportsVision: true,
50
- supportsDocuments: true,
51
- })
52
- })
53
-
54
- it('is exposed on the provider instance for runtime negotiation', () => {
55
- const provider = new AnthropicProvider({ apiKey: 'test-key' })
56
- expect(provider.capabilities).toEqual(ANTHROPIC_CAPABILITIES)
57
- })
58
- })
59
-
60
- describe('AnthropicProvider', () => {
61
- it('constructs with an apiKey and exposes provider identity', () => {
62
- const provider = new AnthropicProvider({ apiKey: 'test-key' })
63
- expect(provider.id).toBe('anthropic')
64
- expect(provider.name).toBe('Anthropic')
65
- })
66
-
67
- it('throws when constructed without an apiKey or authToken', () => {
68
- expect(() => new AnthropicProvider({})).toThrow(/apiKey.*or.*authToken.*required/)
69
- })
70
-
71
- it('accepts an authToken instead of an apiKey (OAuth path)', () => {
72
- const provider = new AnthropicProvider({ authToken: 'cc-oauth-token' })
73
- expect(provider.id).toBe('anthropic')
74
- })
75
-
76
- it('creates via ProviderRegistry.create with the registered type', () => {
77
- registerAnthropic()
78
- const { provider, capabilities } = ProviderRegistry.create({
79
- type: 'anthropic',
80
- apiKey: 'test-key',
81
- model: 'claude-sonnet-4-5-20250929',
82
- })
83
- expect(provider.id).toBe('anthropic')
84
- expect(capabilities).toEqual(ANTHROPIC_CAPABILITIES)
85
- })
86
-
87
- it('does not enforce a provider-local stream idle timeout by default', async () => {
88
- const provider = new AnthropicProvider({
89
- apiKey: 'test-key',
90
- model: 'claude-sonnet-4-5-20250929',
91
- })
92
- ;(provider as unknown as { client: unknown }).client = {
93
- messages: {
94
- create: async () => delayedAnthropicStream(10),
95
- },
96
- }
97
-
98
- const chunks = []
99
- for await (const chunk of provider.chatStream({
100
- model: 'claude-sonnet-4-5-20250929',
101
- messages: [{ role: 'user', content: 'hello' }],
102
- maxTokens: 100,
103
- })) {
104
- chunks.push(chunk)
105
- }
106
-
107
- expect(chunks.length).toBeGreaterThan(0)
108
- })
109
-
110
- it('supports an opt-in stream idle timeout for deployments that need it', async () => {
111
- const provider = new AnthropicProvider({
112
- apiKey: 'test-key',
113
- model: 'claude-sonnet-4-5-20250929',
114
- streamIdleTimeoutMs: 1,
115
- })
116
- ;(provider as unknown as { client: unknown }).client = {
117
- messages: {
118
- create: async () => delayedAnthropicStream(20),
119
- },
120
- }
121
-
122
- const drain = async () => {
123
- for await (const _chunk of provider.chatStream({
124
- model: 'claude-sonnet-4-5-20250929',
125
- messages: [{ role: 'user', content: 'hello' }],
126
- maxTokens: 100,
127
- })) {
128
- // Drain the stream.
129
- }
130
- }
131
-
132
- await expect(drain()).rejects.toThrow(/stream idle/)
133
- })
134
- })
135
- })
136
-
137
- describe('AnthropicProvider — buildCreateParams', () => {
138
- type AnthropicRequestBody = Record<string, unknown> & {
139
- tools?: Record<string, unknown>[]
140
- }
141
-
142
- const buildParams = (
143
- provider: AnthropicProvider,
144
- params: ChatCompletionParams,
145
- stream = false,
146
- ): AnthropicRequestBody =>
147
- (
148
- provider as unknown as {
149
- buildCreateParams(input: ChatCompletionParams, useStream: boolean): AnthropicRequestBody
150
- }
151
- ).buildCreateParams(params, stream)
152
-
153
- const makeProvider = () =>
154
- new AnthropicProvider({
155
- apiKey: 'test-key',
156
- model: 'claude-sonnet-4-5-20250929',
157
- })
158
-
159
- const tools: LLMToolSchema[] = [
160
- {
161
- type: 'function',
162
- function: {
163
- name: 'alpha',
164
- description: 'first',
165
- parameters: { type: 'object' },
166
- },
167
- },
168
- {
169
- type: 'function',
170
- function: {
171
- name: 'beta',
172
- description: 'second',
173
- parameters: { type: 'object' },
174
- },
175
- },
176
- ]
177
-
178
- describe('tool_choice mapping', () => {
179
- it("maps 'none' to the first-class {type:'none'} while KEEPING the tools param", () => {
180
- const body = buildParams(makeProvider(), {
181
- model: 'claude-sonnet-4-5-20250929',
182
- messages: [{ role: 'user', content: 'hi' }],
183
- tools,
184
- toolChoice: 'none',
185
- })
186
- expect(body.tool_choice).toEqual({ type: 'none' })
187
- expect(body.tools).toHaveLength(2)
188
- })
189
-
190
- it("maps 'auto' → auto, 'required' → any, and function → tool", () => {
191
- const base = {
192
- model: 'claude-sonnet-4-5-20250929',
193
- messages: [{ role: 'user' as const, content: 'hi' }],
194
- tools,
195
- }
196
- expect(buildParams(makeProvider(), { ...base, toolChoice: 'auto' }).tool_choice).toEqual({
197
- type: 'auto',
198
- })
199
- expect(buildParams(makeProvider(), { ...base, toolChoice: 'required' }).tool_choice).toEqual({
200
- type: 'any',
201
- })
202
- expect(
203
- buildParams(makeProvider(), {
204
- ...base,
205
- toolChoice: { type: 'function', function: { name: 'alpha' } },
206
- }).tool_choice,
207
- ).toEqual({ type: 'tool', name: 'alpha' })
208
- })
209
-
210
- it('omits tool_choice entirely when no tools are present', () => {
211
- const body = buildParams(makeProvider(), {
212
- model: 'claude-sonnet-4-5-20250929',
213
- messages: [{ role: 'user', content: 'hi' }],
214
- toolChoice: 'none',
215
- })
216
- expect(body.tools).toBeUndefined()
217
- expect(body.tool_choice).toBeUndefined()
218
- })
219
-
220
- it('maps parallelToolCalls:false to disable_parallel_tool_use on tool_choice', () => {
221
- const body = buildParams(makeProvider(), {
222
- model: 'claude-sonnet-4-5-20250929',
223
- messages: [{ role: 'user', content: 'hi' }],
224
- tools,
225
- parallelToolCalls: false,
226
- })
227
- expect(body.tool_choice).toEqual({
228
- type: 'auto',
229
- disable_parallel_tool_use: true,
230
- })
231
- })
232
- })
233
-
234
- describe('strict tool input mapping', () => {
235
- const base = (model: string): ChatCompletionParams => ({
236
- model,
237
- messages: [{ role: 'user', content: 'hi' }],
238
- tools,
239
- enforceToolInputSchema: ['alpha'],
240
- })
241
-
242
- it.each([
243
- 'claude-haiku-4-5-20251001',
244
- 'claude-sonnet-4-6',
245
- 'claude-opus-5',
246
- 'anthropic/claude-fable-5',
247
- 'claude-mythos-preview',
248
- ])('enables only named tools for supported model %s', (model) => {
249
- const body = buildParams(makeProvider(), base(model))
250
- expect(body.tools?.[0]).toMatchObject({ name: 'alpha', strict: true })
251
- expect(body.tools?.[1]).toEqual({
252
- name: 'beta',
253
- description: 'second',
254
- input_schema: { type: 'object' },
255
- })
256
- })
257
-
258
- it.each([
259
- 'claude-3-5-sonnet-20241022',
260
- 'proxy-production-alias',
261
- 'proxy-production-claude-opus-5-legacy',
262
- 'not-claude-sonnet-4-5-compatible',
263
- 'claude-mythos-preview-shadow',
264
- ])('falls back to runtime validation for unsupported or unknown model %s', (model) => {
265
- const body = buildParams(makeProvider(), base(model))
266
- expect(JSON.stringify(body.tools)).not.toContain('"strict"')
267
- })
268
-
269
- it('uses the per-call model instead of the configured default in auto mode', () => {
270
- const provider = new AnthropicProvider({
271
- apiKey: 'test-key',
272
- model: 'claude-opus-5',
273
- })
274
- const body = buildParams(provider, base('proxy-production-alias'))
275
- expect(JSON.stringify(body.tools)).not.toContain('"strict"')
276
- })
277
-
278
- it('supports explicit on/off overrides for compatible aliases and proxies', () => {
279
- const on = new AnthropicProvider({
280
- apiKey: 'test-key',
281
- model: 'proxy-production-alias',
282
- strictToolUse: 'on',
283
- })
284
- expect(
285
- buildParams(on, {
286
- ...base(''),
287
- model: '',
288
- }).tools?.[0],
289
- ).toMatchObject({ strict: true })
290
-
291
- const off = new AnthropicProvider({
292
- apiKey: 'test-key',
293
- model: 'claude-opus-5',
294
- strictToolUse: 'off',
295
- })
296
- expect(JSON.stringify(buildParams(off, base('claude-opus-5')).tools)).not.toContain(
297
- '"strict"',
298
- )
299
- })
300
-
301
- it('ignores empty and unknown enforcement names', () => {
302
- for (const names of [[], ['unknown_tool']]) {
303
- const body = buildParams(makeProvider(), {
304
- ...base('claude-opus-5'),
305
- enforceToolInputSchema: names,
306
- })
307
- expect(JSON.stringify(body.tools)).not.toContain('"strict"')
308
- }
309
- })
310
-
311
- it('serializes the reviewed model schema unchanged without unsupported constraints', () => {
312
- const schema = {
313
- type: 'object',
314
- properties: {
315
- path: { type: 'string' },
316
- old_string: { type: 'string' },
317
- new_string: { type: 'string' },
318
- replace_all: { type: 'boolean' },
319
- },
320
- required: ['path', 'old_string', 'new_string'],
321
- additionalProperties: false,
322
- }
323
- const body = buildParams(makeProvider(), {
324
- model: 'claude-opus-5',
325
- messages: [{ role: 'user', content: 'append' }],
326
- tools: [
327
- {
328
- type: 'function',
329
- function: {
330
- name: 'edit',
331
- description: 'Edit',
332
- parameters: schema,
333
- },
334
- },
335
- ],
336
- enforceToolInputSchema: ['edit'],
337
- })
338
-
339
- expect(body.tools?.[0]).toEqual({
340
- name: 'edit',
341
- description: 'Edit',
342
- input_schema: schema,
343
- strict: true,
344
- })
345
- expect(JSON.stringify(body.tools)).not.toContain('"minimum"')
346
- })
347
-
348
- it('coexists with cache_control on the tools-array tail', () => {
349
- const body = buildParams(makeProvider(), {
350
- ...base('claude-opus-5'),
351
- enforceToolInputSchema: ['beta'],
352
- cacheControl: { type: 'auto' },
353
- })
354
- expect(body.tools?.[0]).not.toHaveProperty('strict')
355
- expect(body.tools?.[1]).toMatchObject({
356
- name: 'beta',
357
- strict: true,
358
- cache_control: { type: 'ephemeral' },
359
- })
360
- })
361
- })
362
-
363
- describe('prompt caching', () => {
364
- const cachedParams = (): ChatCompletionParams => ({
365
- model: 'claude-sonnet-4-5-20250929',
366
- messages: [
367
- { role: 'system', content: 'STATIC PREFIX', cacheHint: 'cache' },
368
- { role: 'system', content: 'DYNAMIC SEGMENT', cacheHint: 'ephemeral' },
369
- { role: 'user', content: 'hello' },
370
- ],
371
- tools,
372
- cacheControl: { type: 'auto' },
373
- })
374
-
375
- it('emits zero cache_control blocks when cacheControl is not requested', () => {
376
- const params = cachedParams()
377
- params.cacheControl = undefined
378
- const body = buildParams(makeProvider(), params)
379
- expect(JSON.stringify(body)).not.toContain('cache_control')
380
- // System still arrives as cacheHint-preserving blocks.
381
- expect(body.system).toEqual([
382
- { type: 'text', text: 'STATIC PREFIX' },
383
- { type: 'text', text: 'DYNAMIC SEGMENT' },
384
- ])
385
- })
386
-
387
- it('preserves system segment boundaries and marks only the cache-tagged block', () => {
388
- const body = buildParams(makeProvider(), cachedParams())
389
- expect(body.system).toEqual([
390
- {
391
- type: 'text',
392
- text: 'STATIC PREFIX',
393
- cache_control: { type: 'ephemeral' },
394
- },
395
- { type: 'text', text: 'DYNAMIC SEGMENT' },
396
- ])
397
- })
398
-
399
- it('marks the tools-array tail and the last message block', () => {
400
- const body = buildParams(makeProvider(), cachedParams())
401
- const bodyTools = body.tools as Record<string, unknown>[]
402
- expect(bodyTools[0]?.cache_control).toBeUndefined()
403
- expect(bodyTools[1]?.cache_control).toEqual({ type: 'ephemeral' })
404
-
405
- const messages = body.messages as Array<{ content: unknown }>
406
- expect(messages[messages.length - 1]?.content).toEqual([
407
- { type: 'text', text: 'hello', cache_control: { type: 'ephemeral' } },
408
- ])
409
- })
410
-
411
- it('places the final breakpoint on the last tool_result block of a tool turn', () => {
412
- const params = cachedParams()
413
- params.messages = [
414
- { role: 'system', content: 'STATIC PREFIX', cacheHint: 'cache' },
415
- { role: 'user', content: 'run the tool' },
416
- {
417
- role: 'assistant',
418
- content: null,
419
- toolCalls: [
420
- {
421
- id: 'tu_1',
422
- type: 'function',
423
- function: { name: 'alpha', arguments: '{}' },
424
- },
425
- ],
426
- },
427
- { role: 'tool', content: 'tool says hi', toolCallId: 'tu_1' },
428
- ]
429
- const body = buildParams(makeProvider(), params)
430
- const messages = body.messages as Array<{ content: unknown }>
431
- expect(messages[messages.length - 1]?.content).toEqual([
432
- {
433
- type: 'tool_result',
434
- tool_use_id: 'tu_1',
435
- content: 'tool says hi',
436
- cache_control: { type: 'ephemeral' },
437
- },
438
- ])
439
- })
440
-
441
- it('keeps the Claude Code identity block FIRST on the OAuth path', () => {
442
- const provider = new AnthropicProvider({ authToken: 'cc-oauth-token' })
443
- const body = buildParams(provider, cachedParams())
444
- const system = body.system as Array<{
445
- text: string
446
- cache_control?: unknown
447
- }>
448
- expect(system[0]?.text).toMatch(/^You are Claude Code/)
449
- expect(system[0]?.cache_control).toBeUndefined()
450
- expect(system[1]).toEqual({
451
- type: 'text',
452
- text: 'STATIC PREFIX',
453
- cache_control: { type: 'ephemeral' },
454
- })
455
- })
456
- })
457
- })
458
-
459
- async function* delayedAnthropicStream(delayMs: number) {
460
- yield {
461
- type: 'message_start',
462
- message: {
463
- id: 'msg_test',
464
- usage: { input_tokens: 1, output_tokens: 0 },
465
- },
466
- }
467
- await new Promise((resolve) => setTimeout(resolve, delayMs))
468
- yield {
469
- type: 'content_block_start',
470
- index: 0,
471
- content_block: { type: 'text' },
472
- }
473
- yield {
474
- type: 'content_block_delta',
475
- index: 0,
476
- delta: { type: 'text_delta', text: 'ok' },
477
- }
478
- yield {
479
- type: 'content_block_stop',
480
- index: 0,
481
- }
482
- yield {
483
- type: 'message_delta',
484
- delta: { stop_reason: 'end_turn' },
485
- usage: { output_tokens: 1 },
486
- }
487
- yield {
488
- type: 'message_stop',
489
- }
490
- }