@namzu/anthropic 0.2.0 → 1.1.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.
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,WAAW,eAAe;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,iDAAiD;IACjD,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,2HAA2H;IAC3H,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,6BAA6B;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,gDAAgD;IAChD,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACvC,yEAAyE;IACzE,SAAS,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,uBAAwB,SAAQ,eAAe;IAC/D,IAAI,EAAE,WAAW,CAAA;CACjB"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,WAAW,eAAe;IAC/B;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,iDAAiD;IACjD,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,2HAA2H;IAC3H,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,6BAA6B;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,gDAAgD;IAChD,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACvC,0EAA0E;IAC1E,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,uBAAwB,SAAQ,eAAe;IAC/D,IAAI,EAAE,WAAW,CAAA;CACjB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@namzu/anthropic",
3
- "version": "0.2.0",
3
+ "version": "1.1.0",
4
4
  "description": "Anthropic LLM provider for @namzu/sdk via the official @anthropic-ai/sdk. Messages API with streaming, tool-use, and extended thinking support.",
5
5
  "license": "FSL-1.1-MIT",
6
6
  "type": "module",
@@ -37,14 +37,14 @@
37
37
  "@anthropic-ai/sdk": "^0.89.0"
38
38
  },
39
39
  "peerDependencies": {
40
- "@namzu/sdk": ">=0.1.6 <1.0.0"
40
+ "@namzu/sdk": ">=1.0.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@biomejs/biome": "^1.9.4",
44
44
  "@types/node": "^22.19.17",
45
45
  "typescript": "^5.5.0",
46
46
  "vitest": "^2.0.0",
47
- "@namzu/sdk": "^0.5.0"
47
+ "@namzu/sdk": "^1.1.0"
48
48
  },
49
49
  "scripts": {
50
50
  "build": "tsc --build",
@@ -1,4 +1,5 @@
1
1
  import { DuplicateProviderError, ProviderRegistry } from '@namzu/sdk'
2
+ import type { ChatCompletionParams, LLMToolSchema } from '@namzu/sdk'
2
3
  import { beforeEach, describe, expect, it } from 'vitest'
3
4
  import { AnthropicProvider } from '../client.js'
4
5
  import { ANTHROPIC_CAPABILITIES, registerAnthropic } from '../index.js'
@@ -56,8 +57,13 @@ describe('@namzu/anthropic', () => {
56
57
  expect(provider.name).toBe('Anthropic')
57
58
  })
58
59
 
59
- it('throws when constructed without an apiKey', () => {
60
- expect(() => new AnthropicProvider({} as any)).toThrow(/apiKey is required/)
60
+ it('throws when constructed without an apiKey or authToken', () => {
61
+ expect(() => new AnthropicProvider({} as any)).toThrow(/apiKey.*or.*authToken.*required/)
62
+ })
63
+
64
+ it('accepts an authToken instead of an apiKey (OAuth path)', () => {
65
+ const provider = new AnthropicProvider({ authToken: 'cc-oauth-token' })
66
+ expect(provider.id).toBe('anthropic')
61
67
  })
62
68
 
63
69
  it('creates via ProviderRegistry.create with the registered type', () => {
@@ -70,5 +76,246 @@ describe('@namzu/anthropic', () => {
70
76
  expect(provider.id).toBe('anthropic')
71
77
  expect(capabilities).toEqual(ANTHROPIC_CAPABILITIES)
72
78
  })
79
+
80
+ it('does not enforce a provider-local stream idle timeout by default', async () => {
81
+ const provider = new AnthropicProvider({
82
+ apiKey: 'test-key',
83
+ model: 'claude-sonnet-4-5-20250929',
84
+ })
85
+ ;(provider as any).client = {
86
+ messages: {
87
+ create: async () => delayedAnthropicStream(10),
88
+ },
89
+ }
90
+
91
+ const chunks = []
92
+ for await (const chunk of provider.chatStream({
93
+ model: 'claude-sonnet-4-5-20250929',
94
+ messages: [{ role: 'user', content: 'hello' }],
95
+ maxTokens: 100,
96
+ })) {
97
+ chunks.push(chunk)
98
+ }
99
+
100
+ expect(chunks.length).toBeGreaterThan(0)
101
+ })
102
+
103
+ it('supports an opt-in stream idle timeout for deployments that need it', async () => {
104
+ const provider = new AnthropicProvider({
105
+ apiKey: 'test-key',
106
+ model: 'claude-sonnet-4-5-20250929',
107
+ streamIdleTimeoutMs: 1,
108
+ })
109
+ ;(provider as any).client = {
110
+ messages: {
111
+ create: async () => delayedAnthropicStream(20),
112
+ },
113
+ }
114
+
115
+ const drain = async () => {
116
+ for await (const _chunk of provider.chatStream({
117
+ model: 'claude-sonnet-4-5-20250929',
118
+ messages: [{ role: 'user', content: 'hello' }],
119
+ maxTokens: 100,
120
+ })) {
121
+ // Drain the stream.
122
+ }
123
+ }
124
+
125
+ await expect(drain()).rejects.toThrow(/stream idle/)
126
+ })
127
+ })
128
+ })
129
+
130
+ describe('AnthropicProvider — buildCreateParams', () => {
131
+ const buildParams = (
132
+ provider: AnthropicProvider,
133
+ params: ChatCompletionParams,
134
+ stream = false,
135
+ ): Record<string, any> =>
136
+ (provider as any).buildCreateParams(params, stream) as Record<string, any>
137
+
138
+ const makeProvider = () =>
139
+ new AnthropicProvider({ apiKey: 'test-key', model: 'claude-sonnet-4-5-20250929' })
140
+
141
+ const tools: LLMToolSchema[] = [
142
+ {
143
+ type: 'function',
144
+ function: { name: 'alpha', description: 'first', parameters: { type: 'object' } },
145
+ },
146
+ {
147
+ type: 'function',
148
+ function: { name: 'beta', description: 'second', parameters: { type: 'object' } },
149
+ },
150
+ ]
151
+
152
+ describe('tool_choice mapping', () => {
153
+ it("maps 'none' to the first-class {type:'none'} while KEEPING the tools param", () => {
154
+ const body = buildParams(makeProvider(), {
155
+ model: 'claude-sonnet-4-5-20250929',
156
+ messages: [{ role: 'user', content: 'hi' }],
157
+ tools,
158
+ toolChoice: 'none',
159
+ })
160
+ expect(body.tool_choice).toEqual({ type: 'none' })
161
+ expect(body.tools).toHaveLength(2)
162
+ })
163
+
164
+ it("maps 'auto' → auto, 'required' → any, and function → tool", () => {
165
+ const base = {
166
+ model: 'claude-sonnet-4-5-20250929',
167
+ messages: [{ role: 'user' as const, content: 'hi' }],
168
+ tools,
169
+ }
170
+ expect(buildParams(makeProvider(), { ...base, toolChoice: 'auto' }).tool_choice).toEqual({
171
+ type: 'auto',
172
+ })
173
+ expect(buildParams(makeProvider(), { ...base, toolChoice: 'required' }).tool_choice).toEqual({
174
+ type: 'any',
175
+ })
176
+ expect(
177
+ buildParams(makeProvider(), {
178
+ ...base,
179
+ toolChoice: { type: 'function', function: { name: 'alpha' } },
180
+ }).tool_choice,
181
+ ).toEqual({ type: 'tool', name: 'alpha' })
182
+ })
183
+
184
+ it('omits tool_choice entirely when no tools are present', () => {
185
+ const body = buildParams(makeProvider(), {
186
+ model: 'claude-sonnet-4-5-20250929',
187
+ messages: [{ role: 'user', content: 'hi' }],
188
+ toolChoice: 'none',
189
+ })
190
+ expect(body.tools).toBeUndefined()
191
+ expect(body.tool_choice).toBeUndefined()
192
+ })
193
+
194
+ it('maps parallelToolCalls:false to disable_parallel_tool_use on tool_choice', () => {
195
+ const body = buildParams(makeProvider(), {
196
+ model: 'claude-sonnet-4-5-20250929',
197
+ messages: [{ role: 'user', content: 'hi' }],
198
+ tools,
199
+ parallelToolCalls: false,
200
+ })
201
+ expect(body.tool_choice).toEqual({ type: 'auto', disable_parallel_tool_use: true })
202
+ })
203
+ })
204
+
205
+ describe('prompt caching', () => {
206
+ const cachedParams = (): ChatCompletionParams => ({
207
+ model: 'claude-sonnet-4-5-20250929',
208
+ messages: [
209
+ { role: 'system', content: 'STATIC PREFIX', cacheHint: 'cache' },
210
+ { role: 'system', content: 'DYNAMIC SEGMENT', cacheHint: 'ephemeral' },
211
+ { role: 'user', content: 'hello' },
212
+ ],
213
+ tools,
214
+ cacheControl: { type: 'auto' },
215
+ })
216
+
217
+ it('emits zero cache_control blocks when cacheControl is not requested', () => {
218
+ const params = cachedParams()
219
+ params.cacheControl = undefined
220
+ const body = buildParams(makeProvider(), params)
221
+ expect(JSON.stringify(body)).not.toContain('cache_control')
222
+ // System still arrives as cacheHint-preserving blocks.
223
+ expect(body.system).toEqual([
224
+ { type: 'text', text: 'STATIC PREFIX' },
225
+ { type: 'text', text: 'DYNAMIC SEGMENT' },
226
+ ])
227
+ })
228
+
229
+ it('preserves system segment boundaries and marks only the cache-tagged block', () => {
230
+ const body = buildParams(makeProvider(), cachedParams())
231
+ expect(body.system).toEqual([
232
+ { type: 'text', text: 'STATIC PREFIX', cache_control: { type: 'ephemeral' } },
233
+ { type: 'text', text: 'DYNAMIC SEGMENT' },
234
+ ])
235
+ })
236
+
237
+ it('marks the tools-array tail and the last message block', () => {
238
+ const body = buildParams(makeProvider(), cachedParams())
239
+ const bodyTools = body.tools as Record<string, unknown>[]
240
+ expect(bodyTools[0]?.cache_control).toBeUndefined()
241
+ expect(bodyTools[1]?.cache_control).toEqual({ type: 'ephemeral' })
242
+
243
+ const messages = body.messages as Array<{ content: unknown }>
244
+ expect(messages[messages.length - 1]?.content).toEqual([
245
+ { type: 'text', text: 'hello', cache_control: { type: 'ephemeral' } },
246
+ ])
247
+ })
248
+
249
+ it('places the final breakpoint on the last tool_result block of a tool turn', () => {
250
+ const params = cachedParams()
251
+ params.messages = [
252
+ { role: 'system', content: 'STATIC PREFIX', cacheHint: 'cache' },
253
+ { role: 'user', content: 'run the tool' },
254
+ {
255
+ role: 'assistant',
256
+ content: null,
257
+ toolCalls: [
258
+ { id: 'tu_1', type: 'function', function: { name: 'alpha', arguments: '{}' } },
259
+ ],
260
+ },
261
+ { role: 'tool', content: 'tool says hi', toolCallId: 'tu_1' },
262
+ ]
263
+ const body = buildParams(makeProvider(), params)
264
+ const messages = body.messages as Array<{ content: unknown }>
265
+ expect(messages[messages.length - 1]?.content).toEqual([
266
+ {
267
+ type: 'tool_result',
268
+ tool_use_id: 'tu_1',
269
+ content: 'tool says hi',
270
+ cache_control: { type: 'ephemeral' },
271
+ },
272
+ ])
273
+ })
274
+
275
+ it('keeps the Claude Code identity block FIRST on the OAuth path', () => {
276
+ const provider = new AnthropicProvider({ authToken: 'cc-oauth-token' })
277
+ const body = buildParams(provider, cachedParams())
278
+ const system = body.system as Array<{ text: string; cache_control?: unknown }>
279
+ expect(system[0]?.text).toMatch(/^You are Claude Code/)
280
+ expect(system[0]?.cache_control).toBeUndefined()
281
+ expect(system[1]).toEqual({
282
+ type: 'text',
283
+ text: 'STATIC PREFIX',
284
+ cache_control: { type: 'ephemeral' },
285
+ })
286
+ })
73
287
  })
74
288
  })
289
+
290
+ async function* delayedAnthropicStream(delayMs: number) {
291
+ yield {
292
+ type: 'message_start',
293
+ message: {
294
+ id: 'msg_test',
295
+ usage: { input_tokens: 1, output_tokens: 0 },
296
+ },
297
+ }
298
+ await new Promise((resolve) => setTimeout(resolve, delayMs))
299
+ yield {
300
+ type: 'content_block_start',
301
+ index: 0,
302
+ content_block: { type: 'text' },
303
+ }
304
+ yield {
305
+ type: 'content_block_delta',
306
+ index: 0,
307
+ delta: { type: 'text_delta', text: 'ok' },
308
+ }
309
+ yield {
310
+ type: 'content_block_stop',
311
+ index: 0,
312
+ }
313
+ yield {
314
+ type: 'message_delta',
315
+ delta: { stop_reason: 'end_turn' },
316
+ usage: { output_tokens: 1 },
317
+ }
318
+ yield {
319
+ type: 'message_stop',
320
+ }
321
+ }