@tanstack/ai-bedrock 0.5.1 → 0.6.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/dist/esm/adapters/converse-text.d.ts +9 -2
- package/dist/esm/adapters/converse-text.js +115 -29
- package/dist/esm/adapters/converse-text.js.map +1 -1
- package/dist/esm/adapters/responses-text.d.ts +11 -2
- package/dist/esm/adapters/responses-text.js +10 -1
- package/dist/esm/adapters/responses-text.js.map +1 -1
- package/dist/esm/adapters/text.d.ts +11 -2
- package/dist/esm/adapters/text.js +10 -1
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/converse/message-converter.d.ts +12 -1
- package/dist/esm/converse/message-converter.js +93 -22
- package/dist/esm/converse/message-converter.js.map +1 -1
- package/dist/esm/converse/prompt-cache.d.ts +16 -0
- package/dist/esm/converse/prompt-cache.js +43 -0
- package/dist/esm/converse/prompt-cache.js.map +1 -0
- package/dist/esm/converse/reasoning.d.ts +28 -0
- package/dist/esm/converse/reasoning.js +42 -0
- package/dist/esm/converse/reasoning.js.map +1 -0
- package/dist/esm/converse/stream-processor.d.ts +3 -29
- package/dist/esm/converse/stream-processor.js +80 -45
- package/dist/esm/converse/stream-processor.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/model-meta.d.ts +80 -0
- package/dist/esm/model-meta.js +54 -1
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/text/responses-provider-options.d.ts +0 -4
- package/dist/esm/text/text-provider-options.d.ts +2 -4
- package/dist/esm/utils/client.d.ts +10 -0
- package/dist/esm/utils/client.js +10 -1
- package/dist/esm/utils/client.js.map +1 -1
- package/package.json +3 -3
- package/src/adapters/converse-text.ts +213 -20
- package/src/adapters/responses-text.ts +32 -2
- package/src/adapters/text.ts +33 -3
- package/src/converse/message-converter.ts +166 -31
- package/src/converse/prompt-cache.ts +53 -0
- package/src/converse/reasoning.ts +62 -0
- package/src/converse/stream-processor.ts +117 -52
- package/src/model-meta.ts +76 -0
- package/src/text/responses-provider-options.ts +0 -2
- package/src/text/text-provider-options.ts +2 -4
- package/src/utils/client.ts +16 -0
|
@@ -1,14 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
CONTEXT_OVERFLOW_CODE,
|
|
3
|
+
EventType,
|
|
4
|
+
convertSchemaToJsonSchema,
|
|
5
|
+
} from '@tanstack/ai'
|
|
2
6
|
import { BaseTextAdapter } from '@tanstack/ai/adapters'
|
|
3
7
|
import { toRunErrorPayload } from '@tanstack/ai/adapter-internals'
|
|
4
8
|
import { resolveBedrockAuth } from '../utils/auth'
|
|
5
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
toConverseMessages,
|
|
11
|
+
toolBlocksToText,
|
|
12
|
+
} from '../converse/message-converter'
|
|
6
13
|
import { toToolConfig } from '../converse/tool-converter'
|
|
7
14
|
import {
|
|
8
15
|
processConverseStream,
|
|
9
16
|
throwIfConverseStreamError,
|
|
10
17
|
} from '../converse/stream-processor'
|
|
11
18
|
import { buildConverseUsage } from '../converse/usage'
|
|
19
|
+
import { converseThinking } from '../converse/reasoning'
|
|
20
|
+
import {
|
|
21
|
+
BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES,
|
|
22
|
+
BEDROCK_MODEL_INPUT_MODALITIES,
|
|
23
|
+
BEDROCK_MODEL_REASONING,
|
|
24
|
+
} from '../model-meta'
|
|
25
|
+
import { addPromptCachePoints } from '../converse/prompt-cache'
|
|
12
26
|
import {
|
|
13
27
|
STRUCTURED_TOOL_NAME,
|
|
14
28
|
buildStructuredToolConfig,
|
|
@@ -25,6 +39,7 @@ import type {
|
|
|
25
39
|
ConverseStreamCommandInput,
|
|
26
40
|
ConverseStreamOutput,
|
|
27
41
|
} from '@aws-sdk/client-bedrock-runtime'
|
|
42
|
+
import type { MiddlewareStack } from '@smithy/types'
|
|
28
43
|
import type {
|
|
29
44
|
JSONSchema,
|
|
30
45
|
Modality,
|
|
@@ -45,6 +60,7 @@ import type {
|
|
|
45
60
|
} from '../message-types'
|
|
46
61
|
import type {
|
|
47
62
|
BedrockConverseModels,
|
|
63
|
+
BedrockModelReasoningByName,
|
|
48
64
|
ResolveConverseProviderOptions,
|
|
49
65
|
ResolveInputModalities,
|
|
50
66
|
} from '../model-meta'
|
|
@@ -66,6 +82,12 @@ export interface BedrockConverseConfig extends BedrockClientConfig {}
|
|
|
66
82
|
* so tests can subclass and inject canned Converse SDK shapes without a real
|
|
67
83
|
* AWS request.
|
|
68
84
|
*/
|
|
85
|
+
/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */
|
|
86
|
+
type ResolveReasoning<TModel extends string> =
|
|
87
|
+
TModel extends keyof BedrockModelReasoningByName
|
|
88
|
+
? BedrockModelReasoningByName[TModel]
|
|
89
|
+
: never
|
|
90
|
+
|
|
69
91
|
export class BedrockConverseTextAdapter<
|
|
70
92
|
TModel extends BedrockConverseModels,
|
|
71
93
|
// Constraint mirrors the chat adapter (text.ts): the base parameterises
|
|
@@ -90,10 +112,20 @@ export class BedrockConverseTextAdapter<
|
|
|
90
112
|
unknown,
|
|
91
113
|
// TSystemPromptMetadata — narrows `systemPrompts[i].metadata` at the chat()
|
|
92
114
|
// call site so users get `cachePoint` autocomplete.
|
|
93
|
-
BedrockSystemPromptMetadata
|
|
115
|
+
BedrockSystemPromptMetadata,
|
|
116
|
+
ResolveReasoning<TModel>
|
|
94
117
|
> {
|
|
95
118
|
override readonly kind = 'text' as const
|
|
96
119
|
override readonly name = 'bedrock-converse' as const
|
|
120
|
+
override readonly api = 'bedrock-converse-stream'
|
|
121
|
+
override readonly provider = 'amazon-bedrock'
|
|
122
|
+
override readonly inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model]
|
|
123
|
+
|
|
124
|
+
private sourceMetadata(model: string) {
|
|
125
|
+
return {
|
|
126
|
+
tanstack: { source: { provider: this.provider, api: this.api, model } },
|
|
127
|
+
}
|
|
128
|
+
}
|
|
97
129
|
private clientPromise?: Promise<BedrockRuntimeClient>
|
|
98
130
|
private readonly clientConfig: BedrockConverseConfig
|
|
99
131
|
|
|
@@ -212,7 +244,9 @@ export class BedrockConverseTextAdapter<
|
|
|
212
244
|
): Promise<AsyncIterable<ConverseStreamOutput>> {
|
|
213
245
|
const { ConverseStreamCommand } = await this.importBedrockRuntime()
|
|
214
246
|
const client = await this.getClient()
|
|
215
|
-
const
|
|
247
|
+
const command = new ConverseStreamCommand(input)
|
|
248
|
+
addRestoreDocuments(command, input)
|
|
249
|
+
const res = await client.send(command)
|
|
216
250
|
if (!res.stream) {
|
|
217
251
|
throw new Error('Bedrock Converse: empty stream response')
|
|
218
252
|
}
|
|
@@ -224,7 +258,9 @@ export class BedrockConverseTextAdapter<
|
|
|
224
258
|
): Promise<ConverseCommandOutput> {
|
|
225
259
|
const { ConverseCommand } = await this.importBedrockRuntime()
|
|
226
260
|
const client = await this.getClient()
|
|
227
|
-
|
|
261
|
+
const command = new ConverseCommand(input)
|
|
262
|
+
addRestoreDocuments(command, input)
|
|
263
|
+
return client.send(command)
|
|
228
264
|
}
|
|
229
265
|
|
|
230
266
|
// ---------------------------------------------------------------------------
|
|
@@ -240,14 +276,28 @@ export class BedrockConverseTextAdapter<
|
|
|
240
276
|
{ provider: this.name, model: this.model },
|
|
241
277
|
)
|
|
242
278
|
const input = this.buildInput(options)
|
|
279
|
+
// Bedrock rejects toolUse and toolResult blocks without a toolConfig,
|
|
280
|
+
// so a request with no tools sends its tool history as text.
|
|
281
|
+
if (!input.toolConfig && input.messages) {
|
|
282
|
+
input.messages = toolBlocksToText(input.messages)
|
|
283
|
+
}
|
|
243
284
|
const stream = await this.sendStream(input)
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
285
|
+
for await (const chunk of processConverseStream(
|
|
286
|
+
stream,
|
|
287
|
+
() => this.generateId(),
|
|
288
|
+
{
|
|
289
|
+
threadId: options.threadId,
|
|
290
|
+
parentRunId: options.parentRunId,
|
|
291
|
+
model: options.model,
|
|
292
|
+
},
|
|
293
|
+
)) {
|
|
294
|
+
yield chunk.type === EventType.RUN_STARTED ||
|
|
295
|
+
chunk.type === EventType.RUN_FINISHED
|
|
296
|
+
? { ...chunk, metadata: this.sourceMetadata(options.model) }
|
|
297
|
+
: chunk
|
|
298
|
+
}
|
|
249
299
|
} catch (error: unknown) {
|
|
250
|
-
const errorPayload =
|
|
300
|
+
const errorPayload = converseRunErrorPayload(
|
|
251
301
|
error,
|
|
252
302
|
`${this.name}.chatStream failed`,
|
|
253
303
|
)
|
|
@@ -259,6 +309,7 @@ export class BedrockConverseTextAdapter<
|
|
|
259
309
|
// `exactOptionalPropertyTypes` (AG-UI's `RunErrorEvent.code` is optional).
|
|
260
310
|
yield {
|
|
261
311
|
type: EventType.RUN_ERROR,
|
|
312
|
+
metadata: this.sourceMetadata(options.model),
|
|
262
313
|
model: options.model,
|
|
263
314
|
timestamp: Date.now(),
|
|
264
315
|
message: errorPayload.message,
|
|
@@ -286,8 +337,11 @@ export class BedrockConverseTextAdapter<
|
|
|
286
337
|
`activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,
|
|
287
338
|
{ provider: this.name, model: this.model },
|
|
288
339
|
)
|
|
340
|
+
// Claude rejects a forced tool while thinking is on, so the forced
|
|
341
|
+
// structured-output tool goes without `chat({ reasoning })`.
|
|
342
|
+
const { reasoning: _reasoning, ...requestOptions } = chatOptions
|
|
289
343
|
const input: ConverseCommandInput = {
|
|
290
|
-
...this.buildInput(
|
|
344
|
+
...this.buildInput(requestOptions),
|
|
291
345
|
toolConfig: buildStructuredToolConfig(outputSchema),
|
|
292
346
|
}
|
|
293
347
|
const res = await this.send(input)
|
|
@@ -347,8 +401,11 @@ export class BedrockConverseTextAdapter<
|
|
|
347
401
|
`activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,
|
|
348
402
|
{ provider: this.name, model: this.model },
|
|
349
403
|
)
|
|
404
|
+
// Claude rejects a forced tool while thinking is on, so the forced
|
|
405
|
+
// structured-output tool goes without `chat({ reasoning })`.
|
|
406
|
+
const { reasoning: _reasoning, ...requestOptions } = chatOptions
|
|
350
407
|
const input: ConverseStreamCommandInput = {
|
|
351
|
-
...this.buildInput(
|
|
408
|
+
...this.buildInput(requestOptions),
|
|
352
409
|
toolConfig: buildStructuredToolConfig(outputSchema),
|
|
353
410
|
}
|
|
354
411
|
const stream = await this.sendStream(input)
|
|
@@ -362,6 +419,7 @@ export class BedrockConverseTextAdapter<
|
|
|
362
419
|
hasEmittedRunStarted = true
|
|
363
420
|
yield {
|
|
364
421
|
type: EventType.RUN_STARTED,
|
|
422
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
365
423
|
runId,
|
|
366
424
|
threadId,
|
|
367
425
|
model: chatOptions.model,
|
|
@@ -429,6 +487,7 @@ export class BedrockConverseTextAdapter<
|
|
|
429
487
|
hasEmittedRunStarted = true
|
|
430
488
|
yield {
|
|
431
489
|
type: EventType.RUN_STARTED,
|
|
490
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
432
491
|
runId,
|
|
433
492
|
threadId,
|
|
434
493
|
model: chatOptions.model,
|
|
@@ -452,6 +511,7 @@ export class BedrockConverseTextAdapter<
|
|
|
452
511
|
const message = `${this.name}.structuredOutputStream: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`
|
|
453
512
|
yield {
|
|
454
513
|
type: EventType.RUN_ERROR,
|
|
514
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
455
515
|
runId,
|
|
456
516
|
model: chatOptions.model,
|
|
457
517
|
timestamp: Date.now(),
|
|
@@ -465,6 +525,7 @@ export class BedrockConverseTextAdapter<
|
|
|
465
525
|
if (accumulatedRaw.length === 0) {
|
|
466
526
|
yield {
|
|
467
527
|
type: EventType.RUN_ERROR,
|
|
528
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
468
529
|
runId,
|
|
469
530
|
model: chatOptions.model,
|
|
470
531
|
timestamp: Date.now(),
|
|
@@ -484,6 +545,7 @@ export class BedrockConverseTextAdapter<
|
|
|
484
545
|
} catch {
|
|
485
546
|
yield {
|
|
486
547
|
type: EventType.RUN_ERROR,
|
|
548
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
487
549
|
runId,
|
|
488
550
|
model: chatOptions.model,
|
|
489
551
|
timestamp: Date.now(),
|
|
@@ -510,6 +572,7 @@ export class BedrockConverseTextAdapter<
|
|
|
510
572
|
|
|
511
573
|
yield {
|
|
512
574
|
type: EventType.RUN_FINISHED,
|
|
575
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
513
576
|
runId,
|
|
514
577
|
threadId,
|
|
515
578
|
model: chatOptions.model,
|
|
@@ -522,6 +585,7 @@ export class BedrockConverseTextAdapter<
|
|
|
522
585
|
hasEmittedRunStarted = true
|
|
523
586
|
yield {
|
|
524
587
|
type: EventType.RUN_STARTED,
|
|
588
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
525
589
|
runId,
|
|
526
590
|
threadId,
|
|
527
591
|
model: chatOptions.model,
|
|
@@ -529,7 +593,7 @@ export class BedrockConverseTextAdapter<
|
|
|
529
593
|
parentRunId: chatOptions.parentRunId,
|
|
530
594
|
}
|
|
531
595
|
}
|
|
532
|
-
const errorPayload =
|
|
596
|
+
const errorPayload = converseRunErrorPayload(
|
|
533
597
|
error,
|
|
534
598
|
`${this.name}.structuredOutputStream failed`,
|
|
535
599
|
)
|
|
@@ -539,6 +603,7 @@ export class BedrockConverseTextAdapter<
|
|
|
539
603
|
})
|
|
540
604
|
yield {
|
|
541
605
|
type: EventType.RUN_ERROR,
|
|
606
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
542
607
|
runId,
|
|
543
608
|
model: chatOptions.model,
|
|
544
609
|
timestamp: Date.now(),
|
|
@@ -577,12 +642,9 @@ export class BedrockConverseTextAdapter<
|
|
|
577
642
|
const { system, messages } = toConverseMessages(
|
|
578
643
|
options.messages,
|
|
579
644
|
options.systemPrompts,
|
|
645
|
+
{ model: options.model, provider: this.provider },
|
|
580
646
|
)
|
|
581
647
|
|
|
582
|
-
const toolConfig = options.tools
|
|
583
|
-
? toToolConfig(convertTools(options.tools), 'auto')
|
|
584
|
-
: undefined
|
|
585
|
-
|
|
586
648
|
// Sampling options live on `modelOptions` (typed as the narrowed
|
|
587
649
|
// `BedrockConverseProviderOptions`, which surfaces the OpenAI Chat
|
|
588
650
|
// Completions field names); translate them into Converse's `inferenceConfig`,
|
|
@@ -590,7 +652,38 @@ export class BedrockConverseTextAdapter<
|
|
|
590
652
|
const modelOptions = options.modelOptions
|
|
591
653
|
const temperature = modelOptions?.temperature
|
|
592
654
|
const topP = modelOptions?.top_p
|
|
593
|
-
|
|
655
|
+
// `chat({ reasoning })`: Claude's thinking fields. Budget thinking needs
|
|
656
|
+
// `maxTokens` above the budget, so a smaller or missing value grows.
|
|
657
|
+
const { additionalModelRequestFields, minMaxTokens } = converseThinking(
|
|
658
|
+
this.model,
|
|
659
|
+
options.reasoning,
|
|
660
|
+
BEDROCK_MODEL_REASONING[this.model],
|
|
661
|
+
)
|
|
662
|
+
|
|
663
|
+
// `chat({ toolChoice })`. Converse has no `none` choice, so `none` sends
|
|
664
|
+
// no tool config, and `chatStream` sends any tool history as text. Claude
|
|
665
|
+
// rejects a forced tool while thinking is on (only Claude gets thinking
|
|
666
|
+
// fields), and some Claude models reject it on every request. Then a
|
|
667
|
+
// forced choice falls back to auto.
|
|
668
|
+
const canForceTool =
|
|
669
|
+
additionalModelRequestFields === undefined &&
|
|
670
|
+
!BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES.some((name) =>
|
|
671
|
+
this.model.includes(name),
|
|
672
|
+
)
|
|
673
|
+
const toolChoice =
|
|
674
|
+
options.toolChoice === 'none' || canForceTool
|
|
675
|
+
? options.toolChoice
|
|
676
|
+
: 'auto'
|
|
677
|
+
const toolConfig = options.tools
|
|
678
|
+
? toToolConfig(convertTools(options.tools), toolChoice)
|
|
679
|
+
: undefined
|
|
680
|
+
|
|
681
|
+
const requestedMaxTokens = modelOptions?.max_completion_tokens
|
|
682
|
+
const maxTokens =
|
|
683
|
+
minMaxTokens !== undefined &&
|
|
684
|
+
(requestedMaxTokens == null || requestedMaxTokens < minMaxTokens)
|
|
685
|
+
? minMaxTokens
|
|
686
|
+
: requestedMaxTokens
|
|
594
687
|
const stop = modelOptions?.stop
|
|
595
688
|
const stopSequences =
|
|
596
689
|
stop == null ? undefined : Array.isArray(stop) ? stop : [stop]
|
|
@@ -608,13 +701,15 @@ export class BedrockConverseTextAdapter<
|
|
|
608
701
|
}
|
|
609
702
|
: undefined
|
|
610
703
|
|
|
611
|
-
|
|
704
|
+
const input: ConverseCommandInput = {
|
|
612
705
|
modelId: this.model,
|
|
613
706
|
messages,
|
|
614
707
|
...(system.length > 0 && { system }),
|
|
615
708
|
...(toolConfig && { toolConfig }),
|
|
616
709
|
...(inferenceConfig && { inferenceConfig }),
|
|
710
|
+
...(additionalModelRequestFields && { additionalModelRequestFields }),
|
|
617
711
|
}
|
|
712
|
+
return addPromptCachePoints(this.model, input, options.promptCache)
|
|
618
713
|
}
|
|
619
714
|
}
|
|
620
715
|
|
|
@@ -623,6 +718,32 @@ export class BedrockConverseTextAdapter<
|
|
|
623
718
|
* the SAME `convertSchemaToJsonSchema` the other adapters use so the Converse
|
|
624
719
|
* tool input schemas match what every other provider sends.
|
|
625
720
|
*/
|
|
721
|
+
/**
|
|
722
|
+
* The text Bedrock models use for a context overflow. Converse has no error
|
|
723
|
+
* type for it: it is a `ValidationException` like every bad request, and over
|
|
724
|
+
* a stream the SDK does not keep that name. So the message is the only signal.
|
|
725
|
+
* Each model family has its own text.
|
|
726
|
+
*/
|
|
727
|
+
const CONTEXT_OVERFLOW_MESSAGE =
|
|
728
|
+
/Input Tokens Exceeded|maximum context length|Input is too long for requested model|prompt is too long/
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* `toRunErrorPayload`, with `code` set to `CONTEXT_OVERFLOW_CODE` for a
|
|
732
|
+
* context overflow. A `ThrottlingException` is never an overflow, whatever
|
|
733
|
+
* its text says about tokens.
|
|
734
|
+
*/
|
|
735
|
+
function converseRunErrorPayload(
|
|
736
|
+
error: unknown,
|
|
737
|
+
fallbackMessage: string,
|
|
738
|
+
): { message: string; code: string | undefined } {
|
|
739
|
+
const payload = toRunErrorPayload(error, fallbackMessage)
|
|
740
|
+
const throttled =
|
|
741
|
+
error instanceof Error && error.name === 'ThrottlingException'
|
|
742
|
+
return !throttled && CONTEXT_OVERFLOW_MESSAGE.test(payload.message)
|
|
743
|
+
? { ...payload, code: CONTEXT_OVERFLOW_CODE }
|
|
744
|
+
: payload
|
|
745
|
+
}
|
|
746
|
+
|
|
626
747
|
function convertTools(tools: Array<Tool>): Array<ConverseToolInput> {
|
|
627
748
|
return tools.map((tool) => {
|
|
628
749
|
const inputSchema: JSONSchema = convertSchemaToJsonSchema(
|
|
@@ -667,6 +788,78 @@ function extractStructuredToolInput(
|
|
|
667
788
|
return undefined
|
|
668
789
|
}
|
|
669
790
|
|
|
791
|
+
/** The parts of a Converse request that hold documents the adapter fills from user data. */
|
|
792
|
+
type ConverseDocuments = Pick<ConverseCommandInput, 'messages' | 'toolConfig'>
|
|
793
|
+
|
|
794
|
+
function hasProtoKey(value: unknown): boolean {
|
|
795
|
+
if (typeof value !== 'object' || value === null) return false
|
|
796
|
+
return (
|
|
797
|
+
Object.hasOwn(value, '__proto__') || Object.values(value).some(hasProtoKey)
|
|
798
|
+
)
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* The AWS SDK copies a document (tool input, tool input schema) with
|
|
803
|
+
* `out[key] = value`. For an own key named `__proto__`, the assignment goes
|
|
804
|
+
* to the `__proto__` setter and the key is lost. This puts each such
|
|
805
|
+
* document back in the serialized body. `JSON.stringify` keeps own keys.
|
|
806
|
+
*/
|
|
807
|
+
function restoreDocuments(request: unknown, input: ConverseDocuments): void {
|
|
808
|
+
const documents = [
|
|
809
|
+
...(input.messages ?? []).flatMap((message) =>
|
|
810
|
+
(message.content ?? []).map((block) => block.toolUse?.input),
|
|
811
|
+
),
|
|
812
|
+
...(input.toolConfig?.tools ?? []).map(
|
|
813
|
+
(tool) => tool.toolSpec?.inputSchema?.json,
|
|
814
|
+
),
|
|
815
|
+
]
|
|
816
|
+
// Skip the parse and re-serialize of the body, which can be large (for
|
|
817
|
+
// example with image bytes), unless a document has the key.
|
|
818
|
+
if (!documents.some(hasProtoKey)) return
|
|
819
|
+
if (
|
|
820
|
+
typeof request !== 'object' ||
|
|
821
|
+
request === null ||
|
|
822
|
+
!('body' in request) ||
|
|
823
|
+
typeof request.body !== 'string'
|
|
824
|
+
) {
|
|
825
|
+
throw new Error(
|
|
826
|
+
'Bedrock Converse: cannot keep a `__proto__` key because the request body is not a JSON string',
|
|
827
|
+
)
|
|
828
|
+
}
|
|
829
|
+
// The SDK serialized this body from `input` one step earlier, so the two
|
|
830
|
+
// line up by position.
|
|
831
|
+
const body: ConverseDocuments = JSON.parse(request.body)
|
|
832
|
+
body.messages?.forEach((message, i) =>
|
|
833
|
+
message.content?.forEach((block, j) => {
|
|
834
|
+
const source = input.messages?.[i]?.content?.[j]?.toolUse?.input
|
|
835
|
+
if (block.toolUse && hasProtoKey(source)) block.toolUse.input = source
|
|
836
|
+
}),
|
|
837
|
+
)
|
|
838
|
+
body.toolConfig?.tools?.forEach((tool, i) => {
|
|
839
|
+
const source = input.toolConfig?.tools?.[i]?.toolSpec?.inputSchema?.json
|
|
840
|
+
if (tool.toolSpec?.inputSchema && hasProtoKey(source))
|
|
841
|
+
tool.toolSpec.inputSchema.json = source
|
|
842
|
+
})
|
|
843
|
+
request.body = JSON.stringify(body)
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* Adds {@link restoreDocuments} to a command. It runs at `build` with high
|
|
848
|
+
* priority, before the SDK sets Content-Length and signs the body.
|
|
849
|
+
*/
|
|
850
|
+
function addRestoreDocuments<TInput extends object, TOutput extends object>(
|
|
851
|
+
command: { middlewareStack: MiddlewareStack<TInput, TOutput> },
|
|
852
|
+
input: ConverseDocuments,
|
|
853
|
+
): void {
|
|
854
|
+
command.middlewareStack.add(
|
|
855
|
+
(next) => (args) => {
|
|
856
|
+
restoreDocuments(args.request, input)
|
|
857
|
+
return next(args)
|
|
858
|
+
},
|
|
859
|
+
{ step: 'build', name: 'tanstackRestoreDocuments', priority: 'high' },
|
|
860
|
+
)
|
|
861
|
+
}
|
|
862
|
+
|
|
670
863
|
/** Converse adapter with an explicit API key (low-level; mirrors createBedrockChat). */
|
|
671
864
|
export function createBedrockConverse<TModel extends BedrockConverseModels>(
|
|
672
865
|
model: TModel,
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import OpenAI from 'openai'
|
|
2
2
|
import { OpenAIBaseResponsesTextAdapter } from '@tanstack/openai-base'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
isBedrockOpenAIContextOverflow,
|
|
5
|
+
withBedrockDefaults,
|
|
6
|
+
} from '../utils/client'
|
|
7
|
+
import {
|
|
8
|
+
BEDROCK_MODEL_INPUT_MODALITIES,
|
|
9
|
+
BEDROCK_MODEL_REASONING,
|
|
10
|
+
} from '../model-meta'
|
|
4
11
|
import type { Modality } from '@tanstack/ai'
|
|
5
12
|
import type { BedrockClientConfig } from '../utils/client'
|
|
6
13
|
import type { BedrockMessageMetadataByModality } from '../message-types'
|
|
7
14
|
import type {
|
|
8
15
|
BedrockChatModelToolCapabilitiesByName,
|
|
16
|
+
BedrockModelReasoningByName,
|
|
9
17
|
BedrockResponsesModels,
|
|
10
18
|
ResolveInputModalities,
|
|
11
19
|
} from '../model-meta'
|
|
@@ -17,6 +25,12 @@ export interface BedrockResponsesConfig
|
|
|
17
25
|
|
|
18
26
|
export type { ExternalResponsesProviderOptions as BedrockResponsesProviderOptions } from '../text/responses-provider-options'
|
|
19
27
|
|
|
28
|
+
/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */
|
|
29
|
+
type ResolveReasoning<TModel extends string> =
|
|
30
|
+
TModel extends keyof BedrockModelReasoningByName
|
|
31
|
+
? BedrockModelReasoningByName[TModel]
|
|
32
|
+
: never
|
|
33
|
+
|
|
20
34
|
type ResolveToolCapabilities<TModel extends string> =
|
|
21
35
|
TModel extends keyof BedrockChatModelToolCapabilitiesByName
|
|
22
36
|
? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]>
|
|
@@ -47,10 +61,13 @@ export class BedrockResponsesTextAdapter<
|
|
|
47
61
|
TProviderOptions,
|
|
48
62
|
TInputModalities,
|
|
49
63
|
BedrockMessageMetadataByModality,
|
|
50
|
-
TToolCapabilities
|
|
64
|
+
TToolCapabilities,
|
|
65
|
+
ResolveReasoning<TModel>
|
|
51
66
|
> {
|
|
52
67
|
override readonly kind = 'text' as const
|
|
53
68
|
override readonly name = 'bedrock-responses' as const
|
|
69
|
+
override readonly provider = 'amazon-bedrock'
|
|
70
|
+
override readonly inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model]
|
|
54
71
|
|
|
55
72
|
constructor(config: BedrockResponsesConfig, model: TModel) {
|
|
56
73
|
// Responses is mantle-only — force the mantle base URL (an explicit
|
|
@@ -61,6 +78,19 @@ export class BedrockResponsesTextAdapter<
|
|
|
61
78
|
fetch: options.fetch,
|
|
62
79
|
})
|
|
63
80
|
}
|
|
81
|
+
|
|
82
|
+
protected override isContextOverflow(error: {
|
|
83
|
+
message: string
|
|
84
|
+
code: string | undefined
|
|
85
|
+
}): boolean {
|
|
86
|
+
return (
|
|
87
|
+
super.isContextOverflow(error) || isBedrockOpenAIContextOverflow(error)
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
protected override modelReasoning(model: string) {
|
|
92
|
+
return BEDROCK_MODEL_REASONING[model]
|
|
93
|
+
}
|
|
64
94
|
}
|
|
65
95
|
|
|
66
96
|
/** Responses adapter with an explicit API key (low-level; the public branching factory delegates here). */
|
package/src/adapters/text.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
import OpenAI from 'openai'
|
|
2
2
|
import { OpenAIBaseChatCompletionsTextAdapter } from '@tanstack/openai-base'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
isBedrockOpenAIContextOverflow,
|
|
5
|
+
withBedrockDefaults,
|
|
6
|
+
} from '../utils/client'
|
|
7
|
+
import {
|
|
8
|
+
BEDROCK_MODEL_INPUT_MODALITIES,
|
|
9
|
+
BEDROCK_MODEL_REASONING,
|
|
10
|
+
} from '../model-meta'
|
|
4
11
|
import type { Modality } from '@tanstack/ai'
|
|
5
12
|
import type { BedrockClientConfig } from '../utils/client'
|
|
6
13
|
import type { BedrockMessageMetadataByModality } from '../message-types'
|
|
7
14
|
import type {
|
|
8
|
-
BedrockChatModelToolCapabilitiesByName,
|
|
9
15
|
BedrockChatModels,
|
|
16
|
+
BedrockChatModelToolCapabilitiesByName,
|
|
17
|
+
BedrockModelReasoningByName,
|
|
10
18
|
ResolveInputModalities,
|
|
11
19
|
ResolveProviderOptions,
|
|
12
20
|
} from '../model-meta'
|
|
@@ -17,6 +25,12 @@ export interface BedrockTextConfig
|
|
|
17
25
|
|
|
18
26
|
export type { ExternalTextProviderOptions as BedrockTextProviderOptions } from '../text/text-provider-options'
|
|
19
27
|
|
|
28
|
+
/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */
|
|
29
|
+
type ResolveReasoning<TModel extends string> =
|
|
30
|
+
TModel extends keyof BedrockModelReasoningByName
|
|
31
|
+
? BedrockModelReasoningByName[TModel]
|
|
32
|
+
: never
|
|
33
|
+
|
|
20
34
|
type ResolveToolCapabilities<TModel extends string> =
|
|
21
35
|
TModel extends keyof BedrockChatModelToolCapabilitiesByName
|
|
22
36
|
? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]>
|
|
@@ -47,10 +61,13 @@ export class BedrockTextAdapter<
|
|
|
47
61
|
TProviderOptions,
|
|
48
62
|
TInputModalities,
|
|
49
63
|
BedrockMessageMetadataByModality,
|
|
50
|
-
TToolCapabilities
|
|
64
|
+
TToolCapabilities,
|
|
65
|
+
ResolveReasoning<TModel>
|
|
51
66
|
> {
|
|
52
67
|
override readonly kind = 'text' as const
|
|
53
68
|
override readonly name = 'bedrock' as const
|
|
69
|
+
override readonly provider = 'amazon-bedrock'
|
|
70
|
+
override readonly inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model]
|
|
54
71
|
|
|
55
72
|
constructor(config: BedrockTextConfig, model: TModel) {
|
|
56
73
|
// No `forced` -> honors config.endpoint ('runtime' default, 'mantle' allowed).
|
|
@@ -61,6 +78,19 @@ export class BedrockTextAdapter<
|
|
|
61
78
|
})
|
|
62
79
|
}
|
|
63
80
|
|
|
81
|
+
protected override isContextOverflow(error: {
|
|
82
|
+
message: string
|
|
83
|
+
code: string | undefined
|
|
84
|
+
}): boolean {
|
|
85
|
+
return (
|
|
86
|
+
super.isContextOverflow(error) || isBedrockOpenAIContextOverflow(error)
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
protected override modelReasoning(model: string) {
|
|
91
|
+
return BEDROCK_MODEL_REASONING[model]
|
|
92
|
+
}
|
|
93
|
+
|
|
64
94
|
/**
|
|
65
95
|
* Surface reasoning deltas (gpt-oss / Claude reasoning) the OpenAI-compatible
|
|
66
96
|
* way. Base types the chunk as `unknown`; narrow with runtime guards — no
|