@namzu/anthropic 1.2.0 → 1.3.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 +280 -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/client.d.ts.map +1 -1
- package/dist/client.js +174 -3
- package/dist/client.js.map +1 -1
- 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/client.ts +241 -7
|
@@ -0,0 +1,218 @@
|
|
|
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
|
+
* The whole citation path existed except the two ends that touch a real
|
|
8
|
+
* provider.
|
|
9
|
+
*
|
|
10
|
+
* The SDK had the `Citation` type, the stream chunk carried a slot for it,
|
|
11
|
+
* the run's stream aggregator collected them onto the assistant message,
|
|
12
|
+
* and the iteration attached them to the turn. Only the mock provider ever
|
|
13
|
+
* emitted one. This driver neither asked for citations nor parsed them, so
|
|
14
|
+
* in a real run the slot was always empty: an answer about a contract
|
|
15
|
+
* arrived as prose, and checking it meant reading the contract again.
|
|
16
|
+
*
|
|
17
|
+
* The location stays a union on purpose. A provider that segments by
|
|
18
|
+
* character offset has no page number, and inventing one would make an
|
|
19
|
+
* uncheckable citation look checkable.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const PDF = 'JVBERi0xLjQKJSVFT0Y='
|
|
23
|
+
|
|
24
|
+
function providerOver(
|
|
25
|
+
events: unknown[],
|
|
26
|
+
seen: { body?: Record<string, unknown> } = {},
|
|
27
|
+
): AnthropicProvider {
|
|
28
|
+
const provider = new AnthropicProvider({ apiKey: 'test-key' })
|
|
29
|
+
;(provider as unknown as { client: { messages: { create: unknown } } }).client = {
|
|
30
|
+
messages: {
|
|
31
|
+
create: vi.fn(async (body: Record<string, unknown>) => {
|
|
32
|
+
seen.body = body
|
|
33
|
+
return (async function* () {
|
|
34
|
+
for (const event of events) yield event
|
|
35
|
+
})()
|
|
36
|
+
}),
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
return provider
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function chunksOf(events: unknown[]): Promise<StreamChunk[]> {
|
|
43
|
+
const provider = providerOver(events)
|
|
44
|
+
const out: StreamChunk[] = []
|
|
45
|
+
for await (const chunk of provider.chatStream({
|
|
46
|
+
model: 'm',
|
|
47
|
+
messages: [{ role: 'user', content: 'q' }],
|
|
48
|
+
} as ChatCompletionParams)) {
|
|
49
|
+
out.push(chunk)
|
|
50
|
+
}
|
|
51
|
+
return out
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function citationEvent(citation: Record<string, unknown>): unknown {
|
|
55
|
+
return {
|
|
56
|
+
type: 'content_block_delta',
|
|
57
|
+
index: 0,
|
|
58
|
+
delta: { type: 'citations_delta', citation },
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const PAGE_CITATION = {
|
|
63
|
+
type: 'page_location',
|
|
64
|
+
cited_text: 'the term is five years',
|
|
65
|
+
document_index: 0,
|
|
66
|
+
document_title: 'lease.pdf',
|
|
67
|
+
start_page_number: 3,
|
|
68
|
+
end_page_number: 3,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe('a cited passage reaches the caller', () => {
|
|
72
|
+
it('emits the citation on the chunk delta', async () => {
|
|
73
|
+
const chunks = await chunksOf([
|
|
74
|
+
{ type: 'message_start', message: { id: 'msg_1' } },
|
|
75
|
+
citationEvent(PAGE_CITATION),
|
|
76
|
+
{ type: 'message_delta', delta: { stop_reason: 'end_turn' } },
|
|
77
|
+
])
|
|
78
|
+
|
|
79
|
+
const cited = chunks.find((c) => c.delta.citation)?.delta.citation
|
|
80
|
+
expect(cited?.citedText).toBe('the term is five years')
|
|
81
|
+
expect(cited?.documentIndex).toBe(0)
|
|
82
|
+
expect(cited?.documentTitle).toBe('lease.pdf')
|
|
83
|
+
expect(cited?.location).toEqual({ kind: 'page', start: 3, end: 3 })
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('does not emit it as text — a citation is not prose', async () => {
|
|
87
|
+
const chunks = await chunksOf([
|
|
88
|
+
{ type: 'message_start', message: { id: 'msg_1' } },
|
|
89
|
+
citationEvent(PAGE_CITATION),
|
|
90
|
+
])
|
|
91
|
+
|
|
92
|
+
expect(chunks.some((c) => c.delta.content)).toBe(false)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('reads a character-offset citation as char, not as a page', async () => {
|
|
96
|
+
const chunks = await chunksOf([
|
|
97
|
+
{ type: 'message_start', message: { id: 'msg_1' } },
|
|
98
|
+
citationEvent({
|
|
99
|
+
cited_text: 'clause 4',
|
|
100
|
+
document_index: 1,
|
|
101
|
+
start_char_index: 120,
|
|
102
|
+
end_char_index: 148,
|
|
103
|
+
}),
|
|
104
|
+
])
|
|
105
|
+
|
|
106
|
+
expect(chunks.find((c) => c.delta.citation)?.delta.citation?.location).toEqual({
|
|
107
|
+
kind: 'char',
|
|
108
|
+
start: 120,
|
|
109
|
+
end: 148,
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('reads a block citation as block', async () => {
|
|
114
|
+
const chunks = await chunksOf([
|
|
115
|
+
{ type: 'message_start', message: { id: 'msg_1' } },
|
|
116
|
+
citationEvent({
|
|
117
|
+
cited_text: 'row 7',
|
|
118
|
+
document_index: 0,
|
|
119
|
+
start_block_index: 7,
|
|
120
|
+
end_block_index: 8,
|
|
121
|
+
}),
|
|
122
|
+
])
|
|
123
|
+
|
|
124
|
+
expect(chunks.find((c) => c.delta.citation)?.delta.citation?.location).toEqual({
|
|
125
|
+
kind: 'block',
|
|
126
|
+
start: 7,
|
|
127
|
+
end: 8,
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('carries several citations through in order', async () => {
|
|
132
|
+
const chunks = await chunksOf([
|
|
133
|
+
{ type: 'message_start', message: { id: 'msg_1' } },
|
|
134
|
+
citationEvent({ ...PAGE_CITATION, cited_text: 'first' }),
|
|
135
|
+
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: ' and ' } },
|
|
136
|
+
citationEvent({ ...PAGE_CITATION, cited_text: 'second' }),
|
|
137
|
+
])
|
|
138
|
+
|
|
139
|
+
expect(chunks.filter((c) => c.delta.citation).map((c) => c.delta.citation?.citedText)).toEqual([
|
|
140
|
+
'first',
|
|
141
|
+
'second',
|
|
142
|
+
])
|
|
143
|
+
})
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
describe('a citation that cannot be checked is dropped, not guessed', () => {
|
|
147
|
+
it('drops one with no location, rather than inventing a page', async () => {
|
|
148
|
+
const chunks = await chunksOf([
|
|
149
|
+
{ type: 'message_start', message: { id: 'msg_1' } },
|
|
150
|
+
citationEvent({ cited_text: 'somewhere', document_index: 0 }),
|
|
151
|
+
])
|
|
152
|
+
|
|
153
|
+
// A citation that looks checkable and is not is worse than none:
|
|
154
|
+
// the reader follows it to a page number nobody measured.
|
|
155
|
+
expect(chunks.some((c) => c.delta.citation)).toBe(false)
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it('drops one with no text', async () => {
|
|
159
|
+
const chunks = await chunksOf([
|
|
160
|
+
{ type: 'message_start', message: { id: 'msg_1' } },
|
|
161
|
+
citationEvent({ document_index: 0, start_page_number: 1, end_page_number: 1 }),
|
|
162
|
+
])
|
|
163
|
+
|
|
164
|
+
expect(chunks.some((c) => c.delta.citation)).toBe(false)
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('leaves the rest of the stream intact around a dropped one', async () => {
|
|
168
|
+
const chunks = await chunksOf([
|
|
169
|
+
{ type: 'message_start', message: { id: 'msg_1' } },
|
|
170
|
+
citationEvent({ cited_text: 'no location' }),
|
|
171
|
+
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'answer' } },
|
|
172
|
+
{ type: 'message_delta', delta: { stop_reason: 'end_turn' } },
|
|
173
|
+
])
|
|
174
|
+
|
|
175
|
+
expect(chunks.map((c) => c.delta.content).filter(Boolean)).toEqual(['answer'])
|
|
176
|
+
expect(chunks.at(-1)?.finishReason).toBe('stop')
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
describe('citations are requested only when the caller asked to check the answer', () => {
|
|
181
|
+
async function documentBlockFor(citations?: boolean): Promise<Record<string, unknown>> {
|
|
182
|
+
const seen: { body?: Record<string, unknown> } = {}
|
|
183
|
+
const provider = providerOver([{ type: 'message_start', message: { id: 'm' } }], seen)
|
|
184
|
+
for await (const _chunk of provider.chatStream({
|
|
185
|
+
model: 'm',
|
|
186
|
+
messages: [
|
|
187
|
+
{
|
|
188
|
+
role: 'user',
|
|
189
|
+
content: 'summarize',
|
|
190
|
+
attachments: [
|
|
191
|
+
{
|
|
192
|
+
type: 'document',
|
|
193
|
+
data: PDF,
|
|
194
|
+
mediaType: 'application/pdf',
|
|
195
|
+
...(citations !== undefined ? { citations } : {}),
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
} as unknown as ChatCompletionParams)) {
|
|
201
|
+
// drain
|
|
202
|
+
}
|
|
203
|
+
const messages = seen.body?.messages as Array<{ content: Record<string, unknown>[] }>
|
|
204
|
+
return messages[0]?.content.find((b) => b.type === 'document') ?? {}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
it('enables them when the attachment asked for them', async () => {
|
|
208
|
+
expect(await documentBlockFor(true)).toMatchObject({ citations: { enabled: true } })
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
it('leaves them off by default — they are not free', async () => {
|
|
212
|
+
expect(await documentBlockFor()).not.toHaveProperty('citations')
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('leaves them off when explicitly declined', async () => {
|
|
216
|
+
expect(await documentBlockFor(false)).not.toHaveProperty('citations')
|
|
217
|
+
})
|
|
218
|
+
})
|
|
@@ -0,0 +1,153 @@
|
|
|
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
|
+
* A tool result carrying an image used to be `JSON.stringify`d.
|
|
8
|
+
*
|
|
9
|
+
* So a screenshot reached the model as a wall of quoted base64 — the exact
|
|
10
|
+
* thing the SDK's degrade helper exists to prevent, and pure waste besides:
|
|
11
|
+
* the model paid for every character and could read none of them. This wire
|
|
12
|
+
* carries image blocks inside a tool result natively; the mapper simply
|
|
13
|
+
* never used the shape.
|
|
14
|
+
*
|
|
15
|
+
* The degrade path still matters for what this wire genuinely cannot take.
|
|
16
|
+
* A document inside a tool result becomes the named placeholder rather than
|
|
17
|
+
* an invented image block, because the wrong block fails the whole request
|
|
18
|
+
* instead of just that part of it.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const PNG = 'iVBORw0KGgo='
|
|
22
|
+
|
|
23
|
+
function bodyCapturer() {
|
|
24
|
+
const 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
|
+
yield { type: 'message_start', message: { id: 'msg_1' } }
|
|
32
|
+
})()
|
|
33
|
+
}),
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
return { provider, seen }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function toolResultFor(content: unknown): Promise<Record<string, unknown>> {
|
|
40
|
+
const { provider, seen } = bodyCapturer()
|
|
41
|
+
for await (const _chunk of provider.chatStream({
|
|
42
|
+
model: 'm',
|
|
43
|
+
messages: [
|
|
44
|
+
{ role: 'user', content: 'take a shot' },
|
|
45
|
+
{
|
|
46
|
+
role: 'assistant',
|
|
47
|
+
content: null,
|
|
48
|
+
toolCalls: [
|
|
49
|
+
{ id: 'call_1', type: 'function', function: { name: 'screenshot', arguments: '{}' } },
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
{ role: 'tool', toolCallId: 'call_1', content },
|
|
53
|
+
],
|
|
54
|
+
} as unknown as ChatCompletionParams)) {
|
|
55
|
+
// drain
|
|
56
|
+
}
|
|
57
|
+
const messages = seen.body?.messages as Array<{ content: Record<string, unknown>[] }>
|
|
58
|
+
const block = messages
|
|
59
|
+
.flatMap((m) => (Array.isArray(m.content) ? m.content : []))
|
|
60
|
+
.find((b) => b.type === 'tool_result')
|
|
61
|
+
return block ?? {}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe('an image in a tool result is sent as an image', () => {
|
|
65
|
+
it('carries the payload as a native image block', async () => {
|
|
66
|
+
const result = await toolResultFor([
|
|
67
|
+
{ type: 'text', text: 'captured' },
|
|
68
|
+
{ type: 'image', mediaType: 'image/png', data: PNG },
|
|
69
|
+
])
|
|
70
|
+
const blocks = result.content as Record<string, unknown>[]
|
|
71
|
+
|
|
72
|
+
expect(blocks.find((b) => b.type === 'image')?.source).toEqual({
|
|
73
|
+
type: 'base64',
|
|
74
|
+
media_type: 'image/png',
|
|
75
|
+
data: PNG,
|
|
76
|
+
})
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('does not stringify the payload into text', async () => {
|
|
80
|
+
const result = await toolResultFor([{ type: 'image', mediaType: 'image/png', data: PNG }])
|
|
81
|
+
|
|
82
|
+
// The bug in one assertion: the content used to be a JSON string
|
|
83
|
+
// with the base64 quoted inside it.
|
|
84
|
+
expect(typeof result.content).not.toBe('string')
|
|
85
|
+
expect(JSON.stringify(result.content)).not.toContain('\\"data\\"')
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('keeps the text alongside the image, in order', async () => {
|
|
89
|
+
const result = await toolResultFor([
|
|
90
|
+
{ type: 'text', text: 'captured' },
|
|
91
|
+
{ type: 'image', mediaType: 'image/png', data: PNG },
|
|
92
|
+
])
|
|
93
|
+
const blocks = result.content as Record<string, unknown>[]
|
|
94
|
+
|
|
95
|
+
expect(blocks.map((b) => b.type)).toEqual(['text', 'image'])
|
|
96
|
+
expect(blocks[0]?.text).toBe('captured')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('carries several images', async () => {
|
|
100
|
+
const result = await toolResultFor([
|
|
101
|
+
{ type: 'image', mediaType: 'image/png', data: PNG },
|
|
102
|
+
{ type: 'image', mediaType: 'image/jpeg', data: 'other' },
|
|
103
|
+
])
|
|
104
|
+
const blocks = result.content as Record<string, unknown>[]
|
|
105
|
+
|
|
106
|
+
expect(blocks.filter((b) => b.type === 'image')).toHaveLength(2)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe('what this wire cannot carry degrades to a named placeholder', () => {
|
|
111
|
+
it('describes a document rather than inventing an image block', async () => {
|
|
112
|
+
const result = await toolResultFor([
|
|
113
|
+
{ type: 'document', mediaType: 'application/pdf', data: 'JVBER', name: 'lease.pdf' },
|
|
114
|
+
])
|
|
115
|
+
const blocks = result.content as Record<string, unknown>[]
|
|
116
|
+
|
|
117
|
+
expect(blocks[0]?.type).toBe('text')
|
|
118
|
+
expect(blocks[0]?.text).toContain('lease.pdf')
|
|
119
|
+
expect(blocks[0]?.text).toContain('application/pdf')
|
|
120
|
+
// A block the wire rejects fails the request, not just the block.
|
|
121
|
+
expect(blocks.some((b) => b.type === 'image')).toBe(false)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('says how big it was, so the model knows what it is missing', async () => {
|
|
125
|
+
const result = await toolResultFor([
|
|
126
|
+
{ type: 'document', mediaType: 'application/pdf', data: 'A'.repeat(4096) },
|
|
127
|
+
])
|
|
128
|
+
const blocks = result.content as Record<string, unknown>[]
|
|
129
|
+
|
|
130
|
+
expect(blocks[0]?.text).toMatch(/\d+(\.\d+)? (B|KB|MB)/)
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
describe('the plain cases are unchanged', () => {
|
|
135
|
+
it('sends a string result as a string', async () => {
|
|
136
|
+
expect(await toolResultFor('just text')).toMatchObject({ content: 'just text' })
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('sends text-only blocks as a text block', async () => {
|
|
140
|
+
const result = await toolResultFor([{ type: 'text', text: 'only words' }])
|
|
141
|
+
const blocks = result.content as Record<string, unknown>[]
|
|
142
|
+
|
|
143
|
+
expect(blocks).toEqual([{ type: 'text', text: 'only words' }])
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('sends an empty string rather than an empty array, which is rejected', async () => {
|
|
147
|
+
expect(await toolResultFor([])).toMatchObject({ content: '' })
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('keeps the tool call id it answers', async () => {
|
|
151
|
+
expect(await toolResultFor('x')).toMatchObject({ tool_use_id: 'call_1' })
|
|
152
|
+
})
|
|
153
|
+
})
|
|
@@ -0,0 +1,131 @@
|
|
|
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
|
+
})
|