@namzu/bedrock 4.0.0 → 4.0.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 (47) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/package.json +12 -2
  3. package/dist/__tests__/bedrock.test.d.ts +0 -2
  4. package/dist/__tests__/bedrock.test.d.ts.map +0 -1
  5. package/dist/__tests__/bedrock.test.js +0 -94
  6. package/dist/__tests__/bedrock.test.js.map +0 -1
  7. package/dist/__tests__/error-taxonomy.test.d.ts +0 -17
  8. package/dist/__tests__/error-taxonomy.test.d.ts.map +0 -1
  9. package/dist/__tests__/error-taxonomy.test.js +0 -204
  10. package/dist/__tests__/error-taxonomy.test.js.map +0 -1
  11. package/dist/__tests__/model-reachability.test.d.ts +0 -2
  12. package/dist/__tests__/model-reachability.test.d.ts.map +0 -1
  13. package/dist/__tests__/model-reachability.test.js +0 -64
  14. package/dist/__tests__/model-reachability.test.js.map +0 -1
  15. package/dist/__tests__/prompt-cache.test.d.ts +0 -2
  16. package/dist/__tests__/prompt-cache.test.d.ts.map +0 -1
  17. package/dist/__tests__/prompt-cache.test.js +0 -68
  18. package/dist/__tests__/prompt-cache.test.js.map +0 -1
  19. package/dist/__tests__/thinking-refused.test.d.ts +0 -2
  20. package/dist/__tests__/thinking-refused.test.d.ts.map +0 -1
  21. package/dist/__tests__/thinking-refused.test.js +0 -35
  22. package/dist/__tests__/thinking-refused.test.js.map +0 -1
  23. package/dist/__tests__/tool-choice-none.test.d.ts +0 -2
  24. package/dist/__tests__/tool-choice-none.test.d.ts.map +0 -1
  25. package/dist/__tests__/tool-choice-none.test.js +0 -76
  26. package/dist/__tests__/tool-choice-none.test.js.map +0 -1
  27. package/dist/__tests__/tool-content-blocks.test.d.ts +0 -2
  28. package/dist/__tests__/tool-content-blocks.test.d.ts.map +0 -1
  29. package/dist/__tests__/tool-content-blocks.test.js +0 -92
  30. package/dist/__tests__/tool-content-blocks.test.js.map +0 -1
  31. package/dist/__tests__/tool-schema-dialect.test.d.ts +0 -2
  32. package/dist/__tests__/tool-schema-dialect.test.d.ts.map +0 -1
  33. package/dist/__tests__/tool-schema-dialect.test.js +0 -98
  34. package/dist/__tests__/tool-schema-dialect.test.js.map +0 -1
  35. package/dist/__tests__/vision.test.d.ts +0 -2
  36. package/dist/__tests__/vision.test.d.ts.map +0 -1
  37. package/dist/__tests__/vision.test.js +0 -77
  38. package/dist/__tests__/vision.test.js.map +0 -1
  39. package/src/__tests__/bedrock.test.ts +0 -103
  40. package/src/__tests__/error-taxonomy.test.ts +0 -248
  41. package/src/__tests__/model-reachability.test.ts +0 -74
  42. package/src/__tests__/prompt-cache.test.ts +0 -81
  43. package/src/__tests__/thinking-refused.test.ts +0 -44
  44. package/src/__tests__/tool-choice-none.test.ts +0 -87
  45. package/src/__tests__/tool-content-blocks.test.ts +0 -119
  46. package/src/__tests__/tool-schema-dialect.test.ts +0 -108
  47. package/src/__tests__/vision.test.ts +0 -98
@@ -1,248 +0,0 @@
1
- /**
2
- * Provider error taxonomy — Bedrock driver.
3
- *
4
- * `chatStream` awaits `this.client.send(command)` with no catch, so an AWS
5
- * `BedrockRuntimeServiceException` (`ThrottlingException`,
6
- * `ValidationException`, `ServiceUnavailableException`, …) escapes verbatim.
7
- * A caller cannot classify it without importing `@aws-sdk/client-bedrock-runtime`.
8
- *
9
- * Transport seam: `BedrockConfig` exposes no `requestHandler`/`endpoint`, so
10
- * unlike the fetch-based drivers there is nothing to inject at the HTTP
11
- * layer. The tests substitute the `BedrockRuntimeClient`'s `send` and reject
12
- * with the REAL AWS exception classes — which is exactly what the driver
13
- * sees from a live call, and the only surface the driver's (currently
14
- * absent) error mapping would act on.
15
- */
16
-
17
- import {
18
- AccessDeniedException,
19
- ModelErrorException,
20
- ServiceQuotaExceededException,
21
- ServiceUnavailableException,
22
- ThrottlingException,
23
- ValidationException,
24
- } from '@aws-sdk/client-bedrock-runtime'
25
- import type { ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime'
26
- import { describe, expect, it } from 'vitest'
27
- import { BedrockProvider } from '../client.js'
28
-
29
- /** Replace the AWS client's `send` so it rejects with `err`. */
30
- function providerRejectingWith(err: unknown): BedrockProvider {
31
- const provider = new BedrockProvider({ region: 'eu-west-1' })
32
- ;(provider as unknown as { client: { send: () => Promise<never> } }).client = {
33
- send: () => Promise.reject(err),
34
- }
35
- return provider
36
- }
37
-
38
- function providerStreaming(...events: ConverseStreamOutput[]): BedrockProvider {
39
- const provider = new BedrockProvider({ region: 'eu-west-1' })
40
- ;(provider as unknown as { client: { send: () => Promise<unknown> } }).client = {
41
- send: async () => ({
42
- $metadata: { requestId: 'bedrock-test' },
43
- stream: (async function* () {
44
- yield* events
45
- })(),
46
- }),
47
- }
48
- return provider
49
- }
50
-
51
- async function captureChatStreamError(
52
- provider: BedrockProvider,
53
- signal?: AbortSignal,
54
- ): Promise<unknown> {
55
- try {
56
- for await (const _chunk of provider.chatStream({
57
- model: 'anthropic.claude-sonnet-4-5-20250929-v1:0',
58
- messages: [{ role: 'user', content: 'hi' }],
59
- signal,
60
- })) {
61
- // drain
62
- }
63
- } catch (err) {
64
- return err
65
- }
66
- throw new Error('expected chatStream to throw')
67
- }
68
-
69
- describe('@namzu/bedrock — provider error taxonomy', () => {
70
- it('(a) ThrottlingException with `retry-after: 2` becomes a throttle-classified error carrying retryAfterMs', async () => {
71
- const aws = new ThrottlingException({
72
- message: 'Too many requests, please wait before trying again.',
73
- $metadata: { httpStatusCode: 429 },
74
- })
75
- ;(aws as unknown as { $response: unknown }).$response = {
76
- statusCode: 429,
77
- headers: { 'retry-after': '2' },
78
- }
79
-
80
- const err = await captureChatStreamError(providerRejectingWith(aws))
81
-
82
- expect(err).toMatchObject({
83
- name: 'ProviderRequestError',
84
- kind: 'throttle',
85
- status: 429,
86
- retryAfterMs: 2000,
87
- providerId: 'bedrock',
88
- })
89
- })
90
-
91
- it("(c) a ValidationException reporting too many input tokens classifies as 'context_overflow'", async () => {
92
- const aws = new ValidationException({
93
- message:
94
- 'Input is too long for requested model. The input token count exceeds the maximum for this model.',
95
- $metadata: { httpStatusCode: 400 },
96
- })
97
-
98
- const err = await captureChatStreamError(providerRejectingWith(aws))
99
-
100
- expect(err).toMatchObject({
101
- name: 'ProviderRequestError',
102
- kind: 'context_overflow',
103
- status: 400,
104
- providerId: 'bedrock',
105
- })
106
- })
107
-
108
- it("(c) a ValidationException that is not an overflow classifies as 'bad_request'", async () => {
109
- const aws = new ValidationException({
110
- message: 'The value at toolConfig.tools failed to satisfy constraint.',
111
- $metadata: { httpStatusCode: 400 },
112
- })
113
-
114
- const err = await captureChatStreamError(providerRejectingWith(aws))
115
-
116
- expect(err).toMatchObject({
117
- name: 'ProviderRequestError',
118
- kind: 'bad_request',
119
- status: 400,
120
- providerId: 'bedrock',
121
- })
122
- })
123
-
124
- it("an AccessDeniedException classifies as 'auth'", async () => {
125
- const aws = new AccessDeniedException({
126
- message: 'You do not have access to the model with the specified model ID.',
127
- $metadata: { httpStatusCode: 403 },
128
- })
129
-
130
- const err = await captureChatStreamError(providerRejectingWith(aws))
131
-
132
- expect(err).toMatchObject({
133
- name: 'ProviderRequestError',
134
- kind: 'auth',
135
- status: 403,
136
- providerId: 'bedrock',
137
- })
138
- })
139
-
140
- it("a ServiceUnavailableException classifies as 'server'", async () => {
141
- const aws = new ServiceUnavailableException({
142
- message: 'The service is unavailable, try again later.',
143
- $metadata: { httpStatusCode: 503 },
144
- })
145
-
146
- const err = await captureChatStreamError(providerRejectingWith(aws))
147
-
148
- expect(err).toMatchObject({
149
- name: 'ProviderRequestError',
150
- kind: 'server',
151
- status: 503,
152
- providerId: 'bedrock',
153
- })
154
- })
155
-
156
- it("a ModelErrorException with HTTP 424 still classifies as 'server'", async () => {
157
- const aws = new ModelErrorException({
158
- message: 'The model failed while processing the request.',
159
- originalStatusCode: 500,
160
- $metadata: { httpStatusCode: 424 },
161
- })
162
-
163
- const err = await captureChatStreamError(providerRejectingWith(aws))
164
-
165
- expect(err).toMatchObject({
166
- name: 'ProviderRequestError',
167
- kind: 'server',
168
- status: 424,
169
- providerId: 'bedrock',
170
- })
171
- })
172
-
173
- it("a ServiceQuotaExceededException classifies as 'throttle'", async () => {
174
- const aws = new ServiceQuotaExceededException({
175
- message: 'Account inference quota exceeded.',
176
- $metadata: { httpStatusCode: 400 },
177
- })
178
-
179
- const err = await captureChatStreamError(providerRejectingWith(aws))
180
-
181
- expect(err).toMatchObject({
182
- name: 'ProviderRequestError',
183
- kind: 'throttle',
184
- status: 400,
185
- providerId: 'bedrock',
186
- })
187
- })
188
-
189
- it('classifies a throttling exception delivered as a stream union event', async () => {
190
- const aws = new ThrottlingException({
191
- message: 'stream quota exceeded',
192
- $metadata: { httpStatusCode: 429 },
193
- })
194
-
195
- const err = await captureChatStreamError(providerStreaming({ throttlingException: aws }))
196
-
197
- expect(err).toMatchObject({
198
- name: 'ProviderRequestError',
199
- kind: 'throttle',
200
- status: 429,
201
- providerId: 'bedrock',
202
- })
203
- })
204
-
205
- it('keeps a validation stream event’s complaint, scrubbed, and still retains no cause', async () => {
206
- // This test used to assert the opposite half: that the vendor's sentence
207
- // was dropped whole. That was the safe-looking reading of "an error body
208
- // can echo a credential", and it cost a day of production downtime —
209
- // the wire had been naming the exact offending field the entire time and
210
- // the SDK deleted the sentence before anyone could read it.
211
- //
212
- // So the guarantee changed shape rather than weakening: what is scrubbed
213
- // is anything CREDENTIAL-SHAPED, and what survives is the complaint. The
214
- // `cause` half is unchanged and still matters — a `cause` survives every
215
- // logger that serializes an error chain, which is how the raw body would
216
- // get out regardless of what the message says.
217
- const key = 'AKIAIOSFODNN7EXAMPLE'
218
- const aws = new ValidationException({
219
- message: `inputSchema is invalid, echoed from key ${key}`,
220
- $metadata: { httpStatusCode: 400 },
221
- })
222
-
223
- const err = await captureChatStreamError(providerStreaming({ validationException: aws }))
224
-
225
- expect(err).toMatchObject({
226
- name: 'ProviderRequestError',
227
- kind: 'bad_request',
228
- providerId: 'bedrock',
229
- })
230
- expect((err as Error).message).toContain('inputSchema is invalid')
231
- expect((err as Error).message).not.toContain(key)
232
- expect('cause' in (err as object)).toBe(false)
233
- })
234
-
235
- it('preserves the caller abort reason when the SDK replaces the error object', async () => {
236
- const controller = new AbortController()
237
- const reason = new Error('user stopped')
238
- const sdkAbort = Object.assign(new Error('request aborted'), {
239
- name: 'AbortError',
240
- })
241
- controller.abort(reason)
242
-
243
- const err = await captureChatStreamError(providerRejectingWith(sdkAbort), controller.signal)
244
-
245
- expect(sdkAbort).not.toBe(reason)
246
- expect(err).toBe(reason)
247
- })
248
- })
@@ -1,74 +0,0 @@
1
- import type { ChatCompletionParams } from '@namzu/sdk'
2
- import { describe, expect, it } from 'vitest'
3
-
4
- import { BedrockProvider } from '../client.js'
5
- import { assertModelReachable } from '../model-reachability.js'
6
-
7
- /**
8
- * This driver speaks Converse with ARN-versioned model ids. The current Claude
9
- * generation is served by a different Bedrock integration whose ids carry no
10
- * version suffix, and pointing this wire at one produced an opaque AWS
11
- * validation error naming neither the cause nor the remedy.
12
- */
13
-
14
- describe('model ids this wire can serve', () => {
15
- const reachable = [
16
- 'anthropic.claude-sonnet-4-5-20250929-v1:0',
17
- 'us.anthropic.claude-sonnet-4-5-20250929-v1:0',
18
- 'eu.anthropic.claude-opus-4-5-20251101-v1:0',
19
- 'global.anthropic.claude-opus-4-6-v1',
20
- 'apac.anthropic.claude-sonnet-4-20250514-v1:0',
21
- 'anthropic.claude-haiku-4-5-20251001-v1:0',
22
- ]
23
-
24
- for (const model of reachable) {
25
- it(`accepts ${model}`, () => {
26
- expect(() => assertModelReachable(model)).not.toThrow()
27
- })
28
- }
29
-
30
- const unreachable = [
31
- 'anthropic.claude-opus-5',
32
- 'anthropic.claude-sonnet-5',
33
- 'anthropic.claude-fable-5',
34
- 'anthropic.claude-opus-4-8',
35
- 'anthropic.claude-opus-4-7',
36
- 'us.anthropic.claude-opus-5',
37
- ]
38
-
39
- for (const model of unreachable) {
40
- it(`refuses ${model} and says why`, () => {
41
- expect(() => assertModelReachable(model)).toThrow(/cannot reach/)
42
- expect(() => assertModelReachable(model)).toThrow(/Converse API/)
43
- })
44
- }
45
-
46
- it('leaves a non-Claude model alone', () => {
47
- // This driver serves whatever Bedrock serves. The check is about one
48
- // vendor's id scheme, not about policing the catalogue.
49
- expect(() => assertModelReachable('amazon.nova-pro-v1:0')).not.toThrow()
50
- expect(() => assertModelReachable('meta.llama3-70b-instruct-v1:0')).not.toThrow()
51
- })
52
-
53
- it('accepts an unknown versioned Claude id', () => {
54
- // A model this file has never heard of passes as long as its id has
55
- // the shape this wire carries. A list that must be edited for every
56
- // new model is a list that is wrong before anyone reads it.
57
- expect(() => assertModelReachable('anthropic.claude-sonnet-9-20991231-v1:0')).not.toThrow()
58
- })
59
- })
60
-
61
- describe('the driver refuses before it calls AWS', () => {
62
- it('throws on an unreachable model with no credentials in play', async () => {
63
- const provider = new BedrockProvider({ region: 'us-east-1' } as never)
64
-
65
- await expect(async () => {
66
- for await (const _ of provider.chatStream({
67
- model: 'anthropic.claude-opus-5',
68
- messages: [{ role: 'user', content: 'hi' }],
69
- } as ChatCompletionParams)) {
70
- // drain
71
- }
72
- }).rejects.toThrow(/cannot reach "anthropic\.claude-opus-5"/)
73
- })
74
- })
@@ -1,81 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { parseBedrockUsage } from '../client.js'
4
-
5
- /**
6
- * Cache reads and cache writes are priced differently from ordinary input,
7
- * so folding either into the prompt count misreports what a run cost — and
8
- * misreports it silently, because the total still looks plausible.
9
- *
10
- * The counters are also the only evidence that the breakpoints are working
11
- * at all: a run whose cached count stays at zero is paying full price for
12
- * a prefix it believes is cached, and nothing else says so.
13
- */
14
-
15
- describe('cache counters are reported on their own axis', () => {
16
- it('keeps reads out of the prompt count', () => {
17
- const usage = parseBedrockUsage({
18
- inputTokens: 10,
19
- outputTokens: 2,
20
- cacheReadInputTokenCount: 900,
21
- })
22
-
23
- expect(usage.promptTokens).toBe(10)
24
- expect(usage.cachedTokens).toBe(900)
25
- })
26
-
27
- it('keeps writes on their own axis too', () => {
28
- const usage = parseBedrockUsage({
29
- inputTokens: 10,
30
- outputTokens: 2,
31
- cacheWriteInputTokenCount: 40,
32
- })
33
-
34
- expect(usage.cacheWriteTokens).toBe(40)
35
- expect(usage.promptTokens).toBe(10)
36
- })
37
-
38
- it('reports both when both happen in one turn', () => {
39
- const usage = parseBedrockUsage({
40
- inputTokens: 10,
41
- outputTokens: 2,
42
- cacheReadInputTokenCount: 900,
43
- cacheWriteInputTokenCount: 40,
44
- })
45
-
46
- expect(usage).toMatchObject({ cachedTokens: 900, cacheWriteTokens: 40 })
47
- })
48
-
49
- it('reads an absent counter as zero, not undefined', () => {
50
- const usage = parseBedrockUsage({ inputTokens: 5, outputTokens: 1 })
51
-
52
- // A consumer summing these must not have to guard every field; an
53
- // undefined here becomes NaN one addition later.
54
- expect(usage.cachedTokens).toBe(0)
55
- expect(usage.cacheWriteTokens).toBe(0)
56
- })
57
-
58
- it('totals input and output, and nothing else', () => {
59
- const usage = parseBedrockUsage({
60
- inputTokens: 10,
61
- outputTokens: 2,
62
- cacheReadInputTokenCount: 900,
63
- })
64
-
65
- // Cached reads are already counted upstream; adding them here would
66
- // double-count the same tokens in the run's own ledger.
67
- expect(usage.totalTokens).toBe(12)
68
- })
69
-
70
- it('reads a missing usage payload as all zeros', () => {
71
- const usage = parseBedrockUsage(undefined)
72
-
73
- expect(usage).toEqual({
74
- promptTokens: 0,
75
- completionTokens: 0,
76
- totalTokens: 0,
77
- cachedTokens: 0,
78
- cacheWriteTokens: 0,
79
- })
80
- })
81
- })
@@ -1,44 +0,0 @@
1
- import type { ChatCompletionParams } from '@namzu/sdk'
2
- import { describe, expect, it } from 'vitest'
3
-
4
- import { BedrockProvider } from '../client.js'
5
-
6
- /**
7
- * The shared guard's own tests pass whether or not this driver calls it, so
8
- * this drives the real `chatStream`. The defect being fixed was exactly a
9
- * declared parameter that no code path consumed — asserting the helper works
10
- * would have reproduced that shape one level up.
11
- *
12
- * The guard runs before any client work, so no credentials and no network are
13
- * involved: the request never gets that far.
14
- */
15
-
16
- const provider = new BedrockProvider({ region: 'us-east-1' } as never)
17
-
18
- async function run(thinking?: ChatCompletionParams['thinking']): Promise<void> {
19
- for await (const _chunk of provider.chatStream({
20
- model: 'anthropic.claude-sonnet-4-5-20250929-v1:0',
21
- messages: [{ role: 'user', content: 'hi' }],
22
- ...(thinking ? { thinking } : {}),
23
- } as ChatCompletionParams)) {
24
- // drain
25
- }
26
- }
27
-
28
- describe('BedrockProvider refuses a thinking request rather than dropping it', () => {
29
- it('throws on a manual thinking request', async () => {
30
- await expect(run({ type: 'enabled', budgetTokens: 10_000 })).rejects.toThrow(
31
- /BedrockProvider does not implement thinking/,
32
- )
33
- })
34
-
35
- it('throws on an adaptive one', async () => {
36
- await expect(run({ type: 'adaptive' })).rejects.toThrow(/does not implement thinking/)
37
- })
38
-
39
- it('does not throw for an explicit disable', async () => {
40
- // Reaches the client and fails on credentials or network instead,
41
- // which is the point: the guard let it past.
42
- await expect(run({ type: 'disabled' })).rejects.not.toThrow(/does not implement thinking/)
43
- })
44
- })
@@ -1,87 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { toBedrockToolConfig } from '../client.js'
4
-
5
- /**
6
- * `toolChoice: 'none'` means the model must not call a tool. This driver
7
- * mapped it to the wire's "auto", which means the model MAY — the exact
8
- * opposite of what the caller asked for, and silent.
9
- *
10
- * The runtime relies on it: an advisory consultation passes `'none'` so the
11
- * advisor answers in prose. Under this driver the advisor could emit a tool
12
- * call instead, into a turn with no executor waiting for one.
13
- *
14
- * The fix is not a better mapping. Every version of every wire format
15
- * agrees a model cannot call a tool it was never given, so `'none'` sends
16
- * no tools at all — a guarantee rather than a request.
17
- */
18
-
19
- const TOOLS = [
20
- {
21
- type: 'function' as const,
22
- function: { name: 'read', description: 'read a file', parameters: { type: 'object' } },
23
- },
24
- ]
25
-
26
- describe("a caller that said 'none' gets no tools on the wire", () => {
27
- it('omits the tool config entirely', () => {
28
- expect(
29
- toBedrockToolConfig({ model: 'm', messages: [], tools: TOOLS, toolChoice: 'none' }),
30
- ).toBe(undefined)
31
- })
32
-
33
- it('omits it even when the history contains tool blocks', () => {
34
- const config = toBedrockToolConfig({
35
- model: 'm',
36
- messages: [
37
- {
38
- role: 'assistant',
39
- content: null,
40
- toolCalls: [{ id: 'c1', type: 'function', function: { name: 'read', arguments: '{}' } }],
41
- },
42
- { role: 'tool', content: 'ok', toolCallId: 'c1' },
43
- ] as Parameters<typeof toBedrockToolConfig>[0]['messages'],
44
- toolChoice: 'none',
45
- })
46
-
47
- expect(config).toBe(undefined)
48
- })
49
- })
50
-
51
- describe('the other choices still reach the wire unchanged', () => {
52
- it('maps auto', () => {
53
- const config = toBedrockToolConfig({
54
- model: 'm',
55
- messages: [],
56
- tools: TOOLS,
57
- toolChoice: 'auto',
58
- })
59
- expect(config?.toolChoice).toEqual({ auto: {} })
60
- expect(config?.tools).toHaveLength(1)
61
- })
62
-
63
- it('maps required', () => {
64
- const config = toBedrockToolConfig({
65
- model: 'm',
66
- messages: [],
67
- tools: TOOLS,
68
- toolChoice: 'required',
69
- })
70
- expect(config?.toolChoice).toEqual({ any: {} })
71
- })
72
-
73
- it('maps a named tool', () => {
74
- const config = toBedrockToolConfig({
75
- model: 'm',
76
- messages: [],
77
- tools: TOOLS,
78
- toolChoice: { type: 'function', function: { name: 'read' } },
79
- })
80
- expect(config?.toolChoice).toEqual({ tool: { name: 'read' } })
81
- })
82
-
83
- it('defaults to auto when the caller said nothing', () => {
84
- const config = toBedrockToolConfig({ model: 'm', messages: [], tools: TOOLS })
85
- expect(config?.toolChoice).toEqual({ auto: {} })
86
- })
87
- })
@@ -1,119 +0,0 @@
1
- import type { ChatCompletionParams } from '@namzu/sdk'
2
- import { describe, expect, it } from 'vitest'
3
-
4
- import { toBedrockMessages } from '../client.js'
5
-
6
- /**
7
- * A tool result carrying content blocks was `JSON.stringify`d into the
8
- * text field, so a screenshot reached the model as a wall of quoted base64:
9
- * unreadable, and paid for by the character.
10
- *
11
- * This driver does not map image content — `supportsVision` is false and
12
- * says so — which makes the honest form the SDK's named placeholder. It
13
- * reports what was there and how big it was, so the model knows something
14
- * was withheld rather than believing it saw a JSON object.
15
- */
16
-
17
- const PNG = 'iVBORw0KGgo='
18
-
19
- function toolResultFor(content: unknown): Record<string, unknown> {
20
- const messages = toBedrockMessages([
21
- { role: 'user', content: 'take a shot' },
22
- {
23
- role: 'assistant',
24
- content: null,
25
- toolCalls: [
26
- { id: 'call_1', type: 'function', function: { name: 'screenshot', arguments: '{}' } },
27
- ],
28
- },
29
- { role: 'tool', toolCallId: 'call_1', content },
30
- ] as unknown as ChatCompletionParams['messages'])
31
-
32
- const block = messages
33
- .flatMap((m) => m.content ?? [])
34
- .find((b) => (b as { toolResult?: unknown }).toolResult)
35
- return (block as unknown as { toolResult: Record<string, unknown> }).toolResult
36
- }
37
-
38
- function textOf(result: Record<string, unknown>): string {
39
- const content = result.content as Array<{ text?: string }>
40
- return content.map((c) => c.text ?? '').join('\n')
41
- }
42
-
43
- describe('a tool result the wire cannot carry is described, not dumped', () => {
44
- it('names the media type and size instead of the payload', () => {
45
- const text = textOf(toolResultFor([{ type: 'image', mediaType: 'image/png', data: PNG }]))
46
-
47
- expect(text).toContain('image/png')
48
- expect(text).toMatch(/\d+(\.\d+)? (B|KB|MB)/)
49
- })
50
-
51
- it('does not put the base64 payload in the text', () => {
52
- const text = textOf(
53
- toolResultFor([{ type: 'image', mediaType: 'image/png', data: 'A'.repeat(2048) }]),
54
- )
55
-
56
- // The bug in one assertion: the payload used to be JSON-quoted into
57
- // the text field, so the model paid for 2048 unreadable characters.
58
- expect(text).not.toContain('A'.repeat(64))
59
- })
60
-
61
- it('keeps the text blocks that sit alongside', () => {
62
- const text = textOf(
63
- toolResultFor([
64
- { type: 'text', text: 'captured the window' },
65
- { type: 'image', mediaType: 'image/png', data: PNG },
66
- ]),
67
- )
68
-
69
- expect(text).toContain('captured the window')
70
- expect(text).toContain('image/png')
71
- })
72
-
73
- it('names a document by its filename', () => {
74
- const text = textOf(
75
- toolResultFor([
76
- { type: 'document', mediaType: 'application/pdf', data: 'JVBER', name: 'lease.pdf' },
77
- ]),
78
- )
79
-
80
- expect(text).toContain('lease.pdf')
81
- expect(text).toContain('application/pdf')
82
- })
83
- })
84
-
85
- describe('the plain cases are unchanged', () => {
86
- it('passes a string result straight through', () => {
87
- expect(textOf(toolResultFor('just text'))).toBe('just text')
88
- })
89
-
90
- it('passes text-only blocks through as their text', () => {
91
- expect(textOf(toolResultFor([{ type: 'text', text: 'only words' }]))).toBe('only words')
92
- })
93
-
94
- it('answers the tool call it was given', () => {
95
- expect(toolResultFor('x').toolUseId).toBe('call_1')
96
- })
97
-
98
- it('groups results answering one turn into a single message', () => {
99
- const messages = toBedrockMessages([
100
- { role: 'user', content: 'read both' },
101
- {
102
- role: 'assistant',
103
- content: null,
104
- toolCalls: [
105
- { id: 'call_1', type: 'function', function: { name: 'read', arguments: '{}' } },
106
- { id: 'call_2', type: 'function', function: { name: 'read', arguments: '{}' } },
107
- ],
108
- },
109
- { role: 'tool', toolCallId: 'call_1', content: 'one' },
110
- { role: 'tool', toolCallId: 'call_2', content: 'two' },
111
- ] as unknown as ChatCompletionParams['messages'])
112
-
113
- const resultTurns = messages.filter((m) =>
114
- (m.content ?? []).every((b) => (b as { toolResult?: unknown }).toolResult),
115
- )
116
- expect(resultTurns).toHaveLength(1)
117
- expect(resultTurns[0]?.content).toHaveLength(2)
118
- })
119
- })