@namzu/anthropic 3.1.0 → 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.
- package/CHANGELOG.md +29 -0
- package/package.json +12 -2
- package/dist/__tests__/anthropic.test.d.ts +0 -2
- package/dist/__tests__/anthropic.test.d.ts.map +0 -1
- package/dist/__tests__/anthropic.test.js +0 -425
- package/dist/__tests__/anthropic.test.js.map +0 -1
- package/dist/__tests__/background-delegation.live.test.d.ts +0 -2
- package/dist/__tests__/background-delegation.live.test.d.ts.map +0 -1
- package/dist/__tests__/background-delegation.live.test.js +0 -212
- package/dist/__tests__/background-delegation.live.test.js.map +0 -1
- package/dist/__tests__/block-shape-guard.test.d.ts +0 -2
- package/dist/__tests__/block-shape-guard.test.d.ts.map +0 -1
- package/dist/__tests__/block-shape-guard.test.js +0 -218
- package/dist/__tests__/block-shape-guard.test.js.map +0 -1
- package/dist/__tests__/cache-anchors.test.d.ts +0 -2
- package/dist/__tests__/cache-anchors.test.d.ts.map +0 -1
- package/dist/__tests__/cache-anchors.test.js +0 -220
- package/dist/__tests__/cache-anchors.test.js.map +0 -1
- package/dist/__tests__/citations.test.d.ts +0 -2
- package/dist/__tests__/citations.test.d.ts.map +0 -1
- package/dist/__tests__/citations.test.js +0 -189
- package/dist/__tests__/citations.test.js.map +0 -1
- package/dist/__tests__/content-blocks.test.d.ts +0 -2
- package/dist/__tests__/content-blocks.test.d.ts.map +0 -1
- package/dist/__tests__/content-blocks.test.js +0 -131
- package/dist/__tests__/content-blocks.test.js.map +0 -1
- package/dist/__tests__/document-input.test.d.ts +0 -2
- package/dist/__tests__/document-input.test.d.ts.map +0 -1
- package/dist/__tests__/document-input.test.js +0 -98
- package/dist/__tests__/document-input.test.js.map +0 -1
- package/dist/__tests__/effort-levels-are-askable.test.d.ts +0 -2
- package/dist/__tests__/effort-levels-are-askable.test.d.ts.map +0 -1
- package/dist/__tests__/effort-levels-are-askable.test.js +0 -74
- package/dist/__tests__/effort-levels-are-askable.test.js.map +0 -1
- package/dist/__tests__/error-taxonomy.test.d.ts +0 -16
- package/dist/__tests__/error-taxonomy.test.d.ts.map +0 -1
- package/dist/__tests__/error-taxonomy.test.js +0 -242
- package/dist/__tests__/error-taxonomy.test.js.map +0 -1
- package/dist/__tests__/reasoning-close.test.d.ts +0 -2
- package/dist/__tests__/reasoning-close.test.d.ts.map +0 -1
- package/dist/__tests__/reasoning-close.test.js +0 -219
- package/dist/__tests__/reasoning-close.test.js.map +0 -1
- package/dist/__tests__/thinking-capability.test.d.ts +0 -2
- package/dist/__tests__/thinking-capability.test.d.ts.map +0 -1
- package/dist/__tests__/thinking-capability.test.js +0 -206
- package/dist/__tests__/thinking-capability.test.js.map +0 -1
- package/dist/__tests__/thinking-contract.live.test.d.ts +0 -2
- package/dist/__tests__/thinking-contract.live.test.d.ts.map +0 -1
- package/dist/__tests__/thinking-contract.live.test.js +0 -97
- package/dist/__tests__/thinking-contract.live.test.js.map +0 -1
- package/dist/__tests__/thinking-request-body.test.d.ts +0 -2
- package/dist/__tests__/thinking-request-body.test.d.ts.map +0 -1
- package/dist/__tests__/thinking-request-body.test.js +0 -90
- package/dist/__tests__/thinking-request-body.test.js.map +0 -1
- package/dist/__tests__/wire-contract.live.test.d.ts +0 -2
- package/dist/__tests__/wire-contract.live.test.d.ts.map +0 -1
- package/dist/__tests__/wire-contract.live.test.js +0 -272
- package/dist/__tests__/wire-contract.live.test.js.map +0 -1
- package/src/__tests__/anthropic.test.ts +0 -490
- package/src/__tests__/background-delegation.live.test.ts +0 -243
- package/src/__tests__/block-shape-guard.test.ts +0 -256
- package/src/__tests__/cache-anchors.test.ts +0 -262
- package/src/__tests__/citations.test.ts +0 -218
- package/src/__tests__/content-blocks.test.ts +0 -153
- package/src/__tests__/document-input.test.ts +0 -131
- package/src/__tests__/effort-levels-are-askable.test.ts +0 -86
- package/src/__tests__/error-taxonomy.test.ts +0 -274
- package/src/__tests__/reasoning-close.test.ts +0 -262
- package/src/__tests__/thinking-capability.test.ts +0 -235
- package/src/__tests__/thinking-contract.live.test.ts +0 -125
- package/src/__tests__/thinking-request-body.test.ts +0 -111
- package/src/__tests__/wire-contract.live.test.ts +0 -331
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import type { ChatCompletionParams } from '@namzu/sdk'
|
|
2
|
-
import { describe, expect, it, vi } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import { ANTHROPIC_CAPABILITIES, AnthropicProvider } from '../client.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The driver declared `supportsDocuments: true` and had no document branch.
|
|
8
|
-
*
|
|
9
|
-
* Every attachment was mapped as an image block carrying the attachment's
|
|
10
|
-
* media type, so a PDF went up as `{ type: 'image', source: { media_type:
|
|
11
|
-
* 'application/pdf' } }` — a shape the API rejects. The capability set said
|
|
12
|
-
* documents worked, the SDK carried a `DocumentAttachment` type with a page
|
|
13
|
-
* of documentation about why native handling is worth having, and the one
|
|
14
|
-
* line that would have used it was never written.
|
|
15
|
-
*
|
|
16
|
-
* The native block is not a nicety: it buys page structure, the provider's
|
|
17
|
-
* own extraction, and the citations that make an answer checkable.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
const PDF = 'JVBERi0xLjQKJSVFT0Y='
|
|
21
|
-
|
|
22
|
-
function providerCapturing(seen: { body?: Record<string, unknown> }): AnthropicProvider {
|
|
23
|
-
const provider = new AnthropicProvider({ apiKey: 'test-key' })
|
|
24
|
-
// The vendor client is the only seam; replace its one method.
|
|
25
|
-
;(provider as unknown as { client: { messages: { create: unknown } } }).client = {
|
|
26
|
-
messages: {
|
|
27
|
-
create: vi.fn(async (body: Record<string, unknown>) => {
|
|
28
|
-
seen.body = body
|
|
29
|
-
return (async function* () {
|
|
30
|
-
yield { type: 'message_start', message: { id: 'msg_1' } }
|
|
31
|
-
yield { type: 'message_delta', delta: { stop_reason: 'end_turn' } }
|
|
32
|
-
})()
|
|
33
|
-
}),
|
|
34
|
-
},
|
|
35
|
-
}
|
|
36
|
-
return provider
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async function bodyFor(params: Partial<ChatCompletionParams>): Promise<Record<string, unknown>> {
|
|
40
|
-
const seen: { body?: Record<string, unknown> } = {}
|
|
41
|
-
const provider = providerCapturing(seen)
|
|
42
|
-
for await (const _chunk of provider.chatStream({
|
|
43
|
-
model: 'm',
|
|
44
|
-
messages: [{ role: 'user', content: 'summarize this' }],
|
|
45
|
-
...params,
|
|
46
|
-
} as ChatCompletionParams)) {
|
|
47
|
-
// drain
|
|
48
|
-
}
|
|
49
|
-
return seen.body ?? {}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** The first user message's content blocks, as sent. */
|
|
53
|
-
function blocksOf(body: Record<string, unknown>): Record<string, unknown>[] {
|
|
54
|
-
const messages = body.messages as Array<{ content: unknown }>
|
|
55
|
-
return messages[0]?.content as Record<string, unknown>[]
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function withAttachments(attachments: unknown[]): Partial<ChatCompletionParams> {
|
|
59
|
-
return {
|
|
60
|
-
messages: [{ role: 'user', content: 'summarize this', attachments }],
|
|
61
|
-
} as unknown as Partial<ChatCompletionParams>
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
describe('a document attachment reaches the wire as a document', () => {
|
|
65
|
-
it('sends a document block, not an image block', async () => {
|
|
66
|
-
const body = await bodyFor(
|
|
67
|
-
withAttachments([{ type: 'document', data: PDF, mediaType: 'application/pdf' }]),
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
const doc = blocksOf(body).find((b) => b.type === 'document')
|
|
71
|
-
expect(doc?.source).toEqual({ type: 'base64', media_type: 'application/pdf', data: PDF })
|
|
72
|
-
// The bug in one assertion: it used to be an image block.
|
|
73
|
-
expect(blocksOf(body).some((b) => b.type === 'image')).toBe(false)
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
it('keeps the prose ahead of the document', async () => {
|
|
77
|
-
const body = await bodyFor(
|
|
78
|
-
withAttachments([{ type: 'document', data: PDF, mediaType: 'application/pdf' }]),
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
expect(blocksOf(body)[0]).toMatchObject({ type: 'text', text: 'summarize this' })
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
it('carries the name so the model can refer to the file', async () => {
|
|
85
|
-
const body = await bodyFor(
|
|
86
|
-
withAttachments([
|
|
87
|
-
{ type: 'document', data: PDF, mediaType: 'application/pdf', name: 'lease.pdf' },
|
|
88
|
-
]),
|
|
89
|
-
)
|
|
90
|
-
|
|
91
|
-
expect(blocksOf(body).find((b) => b.type === 'document')?.title).toBe('lease.pdf')
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
it('omits the title when the caller supplied no name', async () => {
|
|
95
|
-
const body = await bodyFor(
|
|
96
|
-
withAttachments([{ type: 'document', data: PDF, mediaType: 'application/pdf' }]),
|
|
97
|
-
)
|
|
98
|
-
|
|
99
|
-
expect(blocksOf(body).find((b) => b.type === 'document')).not.toHaveProperty('title')
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
it('still maps an image attachment as an image', async () => {
|
|
103
|
-
const body = await bodyFor(
|
|
104
|
-
withAttachments([{ type: 'image', data: 'AAAA', mediaType: 'image/png' }]),
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
expect(blocksOf(body).find((b) => b.type === 'image')?.source).toEqual({
|
|
108
|
-
type: 'base64',
|
|
109
|
-
media_type: 'image/png',
|
|
110
|
-
data: 'AAAA',
|
|
111
|
-
})
|
|
112
|
-
expect(blocksOf(body).some((b) => b.type === 'document')).toBe(false)
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
it('carries both kinds in one message, in the order given', async () => {
|
|
116
|
-
const body = await bodyFor(
|
|
117
|
-
withAttachments([
|
|
118
|
-
{ type: 'document', data: PDF, mediaType: 'application/pdf' },
|
|
119
|
-
{ type: 'image', data: 'AAAA', mediaType: 'image/png' },
|
|
120
|
-
]),
|
|
121
|
-
)
|
|
122
|
-
|
|
123
|
-
expect(blocksOf(body).map((b) => b.type)).toEqual(['text', 'document', 'image'])
|
|
124
|
-
})
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
describe('the capability claim matches the mapper', () => {
|
|
128
|
-
it('declares document support, which is now true', () => {
|
|
129
|
-
expect(ANTHROPIC_CAPABILITIES.supportsDocuments).toBe(true)
|
|
130
|
-
})
|
|
131
|
-
})
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { AnthropicProvider } from '../client.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A caller building an effort control has to offer the levels a model actually
|
|
7
|
-
* accepts, and only those, because effort is REFUSED rather than clamped: a
|
|
8
|
-
* level a model does not have makes the vendor reject the request, so a picker
|
|
9
|
-
* offering the wrong one produces a run that fails at the start.
|
|
10
|
-
*
|
|
11
|
-
* The driver already knew the answer and kept it to itself. Every option open
|
|
12
|
-
* to a caller without it was bad: offer all five and break some models, offer
|
|
13
|
-
* the intersection and hide the levels that are most of the reason to build the
|
|
14
|
-
* control, or copy the table — which looks fine and is worst, because the
|
|
15
|
-
* ceiling has moved twice and a copy goes stale SILENTLY, surfacing as a vendor
|
|
16
|
-
* rejection rather than a failing build.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
const provider = (): AnthropicProvider =>
|
|
20
|
-
new AnthropicProvider({ apiKey: 'sk-ant-test', model: 'claude-sonnet-5' })
|
|
21
|
-
|
|
22
|
-
describe('a caller can ask which levels a model takes', () => {
|
|
23
|
-
it('answers per model rather than per provider', () => {
|
|
24
|
-
// Three eras, three different answers. If this ever collapses to one
|
|
25
|
-
// set, the method has been reduced to a constant and the question it
|
|
26
|
-
// exists to answer has stopped being asked.
|
|
27
|
-
const p = provider()
|
|
28
|
-
const modern = p.effortLevelsFor('claude-opus-5')
|
|
29
|
-
const midRange = p.effortLevelsFor('claude-sonnet-4-6')
|
|
30
|
-
const legacy = p.effortLevelsFor('claude-opus-4-5')
|
|
31
|
-
|
|
32
|
-
expect(modern).toContain('xhigh')
|
|
33
|
-
expect(midRange).not.toContain('xhigh')
|
|
34
|
-
expect(midRange).toContain('max')
|
|
35
|
-
expect(legacy).not.toContain('max')
|
|
36
|
-
|
|
37
|
-
const distinct = new Set([modern, midRange, legacy].map((s) => [...s].sort().join(',')))
|
|
38
|
-
expect(distinct.size, 'the three eras must not answer alike').toBe(3)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('reports an empty set rather than nothing for a model with no levels', () => {
|
|
42
|
-
// A real answer, and different from the method being absent. Absent
|
|
43
|
-
// means the driver has no effort concept at all; empty means it does
|
|
44
|
-
// and this model has none.
|
|
45
|
-
expect(provider().effortLevelsFor('claude-haiku-4-5')).toEqual([])
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
describe('the answer depends on the thinking configuration you will send', () => {
|
|
50
|
-
it('narrows for the family that caps effort while thinking is off', () => {
|
|
51
|
-
// The trap this signature exists to remove. A caller reading a single
|
|
52
|
-
// `effort` array, rendering a picker, and then also sending
|
|
53
|
-
// `thinking: disabled` produces a combination the vendor rejects — on
|
|
54
|
-
// exactly one family, which is why it survives casual testing.
|
|
55
|
-
const p = provider()
|
|
56
|
-
const withThinking = p.effortLevelsFor('claude-opus-5', { type: 'adaptive' })
|
|
57
|
-
const withoutThinking = p.effortLevelsFor('claude-opus-5', { type: 'disabled' })
|
|
58
|
-
|
|
59
|
-
expect(withThinking).toContain('max')
|
|
60
|
-
expect(withoutThinking).not.toContain('max')
|
|
61
|
-
expect(withoutThinking).not.toContain('xhigh')
|
|
62
|
-
expect(withoutThinking).toContain('high')
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
it('does not narrow for a sibling that accepts the full set either way', () => {
|
|
66
|
-
// Measured, not inferred: the cap belongs to one family, and a version
|
|
67
|
-
// comparison of "5 and later" would have caught this model too and
|
|
68
|
-
// hidden levels the wire honours.
|
|
69
|
-
const p = provider()
|
|
70
|
-
expect(p.effortLevelsFor('claude-sonnet-5', { type: 'disabled' })).toContain('max')
|
|
71
|
-
})
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
describe('the resolver is reachable for the fuller picture', () => {
|
|
75
|
-
it('is exported from the package entry, not only its own module', async () => {
|
|
76
|
-
// The whole report was that the answer existed and could not be
|
|
77
|
-
// reached. A caller that also needs to know whether thinking can be
|
|
78
|
-
// switched off at all reads the capability directly.
|
|
79
|
-
const entry = await import('../index.js')
|
|
80
|
-
|
|
81
|
-
expect(typeof entry.resolveThinkingCapability).toBe('function')
|
|
82
|
-
const capability = entry.resolveThinkingCapability('claude-opus-5')
|
|
83
|
-
expect(capability.effort.length).toBeGreaterThan(0)
|
|
84
|
-
expect(typeof capability.canDisable).toBe('boolean')
|
|
85
|
-
})
|
|
86
|
-
})
|
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Provider error taxonomy — Anthropic driver.
|
|
3
|
-
*
|
|
4
|
-
* The driver hands the request to the vendor SDK and lets whatever the SDK
|
|
5
|
-
* rejects with escape verbatim (`RateLimitError`, `BadRequestError`, …).
|
|
6
|
-
* Those are vendor types: a caller cannot classify them without importing
|
|
7
|
-
* `@anthropic-ai/sdk`, so nothing downstream can distinguish a throttle
|
|
8
|
-
* from a context overflow from a bad request.
|
|
9
|
-
*
|
|
10
|
-
* Transport seam: `AnthropicConfig.baseURL` — the tests point the SDK at a
|
|
11
|
-
* loopback server that answers with a scripted status/headers/body. This
|
|
12
|
-
* exercises the REAL vendor client (including its own internal retries,
|
|
13
|
-
* which this group must not disturb).
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { type Server, createServer } from 'node:http'
|
|
17
|
-
import type { AddressInfo } from 'node:net'
|
|
18
|
-
import { afterEach, describe, expect, it } from 'vitest'
|
|
19
|
-
import { AnthropicProvider } from '../client.js'
|
|
20
|
-
|
|
21
|
-
/** Obviously fake, non-functional token — present only as a leak probe. */
|
|
22
|
-
const FAKE_CREDENTIAL = 'sk-ant-FAKE-DO-NOT-USE-0000'
|
|
23
|
-
|
|
24
|
-
interface ScriptedReply {
|
|
25
|
-
status: number
|
|
26
|
-
body: string
|
|
27
|
-
headers?: Record<string, string>
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
let server: Server | undefined
|
|
31
|
-
|
|
32
|
-
afterEach(async () => {
|
|
33
|
-
if (server) {
|
|
34
|
-
await new Promise<void>((resolve) => server?.close(() => resolve()))
|
|
35
|
-
server = undefined
|
|
36
|
-
}
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
/** Boot a loopback endpoint that answers every request with `reply`. */
|
|
40
|
-
async function startEndpoint(reply: ScriptedReply): Promise<string> {
|
|
41
|
-
server = createServer((_req, res) => {
|
|
42
|
-
res.writeHead(reply.status, {
|
|
43
|
-
'Content-Type': 'application/json',
|
|
44
|
-
...(reply.headers ?? {}),
|
|
45
|
-
})
|
|
46
|
-
res.end(reply.body)
|
|
47
|
-
})
|
|
48
|
-
await new Promise<void>((resolve) => server?.listen(0, '127.0.0.1', () => resolve()))
|
|
49
|
-
const { port } = server.address() as AddressInfo
|
|
50
|
-
return `http://127.0.0.1:${port}`
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Boot a loopback endpoint that streams `frames` as an SSE 200 response. */
|
|
54
|
-
async function startSseEndpoint(frames: string[]): Promise<string> {
|
|
55
|
-
server = createServer((_req, res) => {
|
|
56
|
-
res.writeHead(200, {
|
|
57
|
-
'Content-Type': 'text/event-stream',
|
|
58
|
-
'Cache-Control': 'no-cache',
|
|
59
|
-
Connection: 'keep-alive',
|
|
60
|
-
})
|
|
61
|
-
res.end(`${frames.join('\n\n')}\n\n`)
|
|
62
|
-
})
|
|
63
|
-
await new Promise<void>((resolve) => server?.listen(0, '127.0.0.1', () => resolve()))
|
|
64
|
-
const { port } = server.address() as AddressInfo
|
|
65
|
-
return `http://127.0.0.1:${port}`
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function captureChatStreamError(reply: ScriptedReply): Promise<unknown> {
|
|
69
|
-
const baseURL = await startEndpoint(reply)
|
|
70
|
-
// maxRetries is deliberately NOT set: this group classifies errors and
|
|
71
|
-
// must leave the vendor SDK's own retry behaviour exactly as it is.
|
|
72
|
-
const provider = new AnthropicProvider({
|
|
73
|
-
apiKey: 'test-key',
|
|
74
|
-
model: 'claude-sonnet-4-5-20250929',
|
|
75
|
-
baseURL,
|
|
76
|
-
})
|
|
77
|
-
try {
|
|
78
|
-
for await (const _chunk of provider.chatStream({
|
|
79
|
-
model: 'claude-sonnet-4-5-20250929',
|
|
80
|
-
messages: [{ role: 'user', content: 'hi' }],
|
|
81
|
-
})) {
|
|
82
|
-
// drain
|
|
83
|
-
}
|
|
84
|
-
} catch (err) {
|
|
85
|
-
return err
|
|
86
|
-
}
|
|
87
|
-
throw new Error('expected chatStream to throw')
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async function captureProviderError(
|
|
91
|
-
provider: AnthropicProvider,
|
|
92
|
-
signal?: AbortSignal,
|
|
93
|
-
): Promise<unknown> {
|
|
94
|
-
try {
|
|
95
|
-
for await (const _chunk of provider.chatStream({
|
|
96
|
-
model: 'claude-sonnet-4-5-20250929',
|
|
97
|
-
messages: [{ role: 'user', content: 'hi' }],
|
|
98
|
-
signal,
|
|
99
|
-
})) {
|
|
100
|
-
// drain
|
|
101
|
-
}
|
|
102
|
-
} catch (err) {
|
|
103
|
-
return err
|
|
104
|
-
}
|
|
105
|
-
throw new Error('expected chatStream to throw')
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
describe('@namzu/anthropic — provider error taxonomy', () => {
|
|
109
|
-
it('(a) 429 with `retry-after: 2` throws a throttle-classified error carrying retryAfterMs', async () => {
|
|
110
|
-
const err = await captureChatStreamError({
|
|
111
|
-
status: 429,
|
|
112
|
-
headers: { 'retry-after': '2' },
|
|
113
|
-
body: JSON.stringify({
|
|
114
|
-
type: 'error',
|
|
115
|
-
error: { type: 'rate_limit_error', message: 'slow' },
|
|
116
|
-
}),
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
expect(err).toMatchObject({
|
|
120
|
-
name: 'ProviderRequestError',
|
|
121
|
-
kind: 'throttle',
|
|
122
|
-
status: 429,
|
|
123
|
-
retryAfterMs: 2000,
|
|
124
|
-
providerId: 'anthropic',
|
|
125
|
-
})
|
|
126
|
-
}, 60_000)
|
|
127
|
-
|
|
128
|
-
it("(c) 400 whose body says the prompt is too long classifies as 'context_overflow'", async () => {
|
|
129
|
-
const err = await captureChatStreamError({
|
|
130
|
-
status: 400,
|
|
131
|
-
body: JSON.stringify({
|
|
132
|
-
type: 'error',
|
|
133
|
-
error: {
|
|
134
|
-
type: 'invalid_request_error',
|
|
135
|
-
message: 'prompt is too long: 305231 tokens > 200000 maximum',
|
|
136
|
-
},
|
|
137
|
-
}),
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
expect(err).toMatchObject({
|
|
141
|
-
name: 'ProviderRequestError',
|
|
142
|
-
kind: 'context_overflow',
|
|
143
|
-
status: 400,
|
|
144
|
-
providerId: 'anthropic',
|
|
145
|
-
})
|
|
146
|
-
}, 30_000)
|
|
147
|
-
|
|
148
|
-
it("(c) 400 that is not an overflow classifies as 'bad_request'", async () => {
|
|
149
|
-
const err = await captureChatStreamError({
|
|
150
|
-
status: 400,
|
|
151
|
-
body: JSON.stringify({
|
|
152
|
-
type: 'error',
|
|
153
|
-
error: {
|
|
154
|
-
type: 'invalid_request_error',
|
|
155
|
-
message: 'messages: at least one message required',
|
|
156
|
-
},
|
|
157
|
-
}),
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
expect(err).toMatchObject({
|
|
161
|
-
name: 'ProviderRequestError',
|
|
162
|
-
kind: 'bad_request',
|
|
163
|
-
status: 400,
|
|
164
|
-
providerId: 'anthropic',
|
|
165
|
-
})
|
|
166
|
-
}, 30_000)
|
|
167
|
-
|
|
168
|
-
it("(auth) 401 classifies as 'auth'", async () => {
|
|
169
|
-
const err = await captureChatStreamError({
|
|
170
|
-
status: 401,
|
|
171
|
-
body: JSON.stringify({
|
|
172
|
-
type: 'error',
|
|
173
|
-
error: { type: 'authentication_error', message: 'invalid x-api-key' },
|
|
174
|
-
}),
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
expect(err).toMatchObject({
|
|
178
|
-
name: 'ProviderRequestError',
|
|
179
|
-
kind: 'auth',
|
|
180
|
-
status: 401,
|
|
181
|
-
providerId: 'anthropic',
|
|
182
|
-
})
|
|
183
|
-
}, 30_000)
|
|
184
|
-
|
|
185
|
-
it("529 (upstream overload) classifies as 'server'", async () => {
|
|
186
|
-
const err = await captureChatStreamError({
|
|
187
|
-
status: 529,
|
|
188
|
-
body: JSON.stringify({
|
|
189
|
-
type: 'error',
|
|
190
|
-
error: { type: 'overloaded_error', message: 'Overloaded' },
|
|
191
|
-
}),
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
expect(err).toMatchObject({
|
|
195
|
-
name: 'ProviderRequestError',
|
|
196
|
-
kind: 'server',
|
|
197
|
-
status: 529,
|
|
198
|
-
providerId: 'anthropic',
|
|
199
|
-
})
|
|
200
|
-
}, 60_000)
|
|
201
|
-
|
|
202
|
-
it("MID-STREAM: an `event: error` frame after a 200 classifies as 'server'", async () => {
|
|
203
|
-
// The vendor SDK's SSE reader throws its own `APIError` for an
|
|
204
|
-
// `event: error` frame (core/streaming: `if (sse.event === 'error')`),
|
|
205
|
-
// so the driver's event switch never sees it — the vendor error escapes
|
|
206
|
-
// the driver unclassified, with the frame body interpolated.
|
|
207
|
-
const baseURL = await startSseEndpoint([
|
|
208
|
-
'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_1","usage":{"input_tokens":5,"output_tokens":0}}}',
|
|
209
|
-
'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"partial"}}',
|
|
210
|
-
'event: error\ndata: {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}',
|
|
211
|
-
])
|
|
212
|
-
const provider = new AnthropicProvider({
|
|
213
|
-
apiKey: 'test-key',
|
|
214
|
-
model: 'claude-sonnet-4-5-20250929',
|
|
215
|
-
baseURL,
|
|
216
|
-
})
|
|
217
|
-
|
|
218
|
-
let caught: unknown
|
|
219
|
-
try {
|
|
220
|
-
for await (const _chunk of provider.chatStream({
|
|
221
|
-
model: 'claude-sonnet-4-5-20250929',
|
|
222
|
-
messages: [{ role: 'user', content: 'hi' }],
|
|
223
|
-
})) {
|
|
224
|
-
// drain
|
|
225
|
-
}
|
|
226
|
-
} catch (err) {
|
|
227
|
-
caught = err
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
expect(caught).toMatchObject({
|
|
231
|
-
name: 'ProviderRequestError',
|
|
232
|
-
kind: 'server',
|
|
233
|
-
providerId: 'anthropic',
|
|
234
|
-
})
|
|
235
|
-
}, 30_000)
|
|
236
|
-
|
|
237
|
-
it('(d) SECURITY: a credential echoed back in a 401 body never reaches the thrown message', async () => {
|
|
238
|
-
const err = await captureChatStreamError({
|
|
239
|
-
status: 401,
|
|
240
|
-
body: JSON.stringify({
|
|
241
|
-
type: 'error',
|
|
242
|
-
error: {
|
|
243
|
-
type: 'authentication_error',
|
|
244
|
-
message: `invalid x-api-key ${FAKE_CREDENTIAL}`,
|
|
245
|
-
},
|
|
246
|
-
}),
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
expect((err as Error).message).not.toContain(FAKE_CREDENTIAL)
|
|
250
|
-
expect(JSON.stringify(err)).not.toContain(FAKE_CREDENTIAL)
|
|
251
|
-
expect('cause' in (err as object)).toBe(false)
|
|
252
|
-
}, 30_000)
|
|
253
|
-
|
|
254
|
-
it('preserves the caller abort reason when the SDK replaces the error object', async () => {
|
|
255
|
-
const controller = new AbortController()
|
|
256
|
-
const reason = new Error('user stopped')
|
|
257
|
-
const sdkAbort = Object.assign(new Error('request aborted'), {
|
|
258
|
-
name: 'APIUserAbortError',
|
|
259
|
-
})
|
|
260
|
-
controller.abort(reason)
|
|
261
|
-
const provider = new AnthropicProvider({
|
|
262
|
-
apiKey: 'test-key',
|
|
263
|
-
model: 'claude-sonnet-4-5-20250929',
|
|
264
|
-
})
|
|
265
|
-
;(provider as unknown as { client: unknown }).client = {
|
|
266
|
-
messages: { create: async () => Promise.reject(sdkAbort) },
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
const err = await captureProviderError(provider, controller.signal)
|
|
270
|
-
|
|
271
|
-
expect(sdkAbort).not.toBe(reason)
|
|
272
|
-
expect(err).toBe(reason)
|
|
273
|
-
})
|
|
274
|
-
})
|