@tanstack/ai-bedrock 0.5.0 → 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
|
@@ -4,7 +4,7 @@ import { BedrockRuntimeClient, BedrockRuntimeClientConfig, ConverseCommandInput,
|
|
|
4
4
|
import { Modality, AdapterYieldChunk, TextOptions } from '@tanstack/ai';
|
|
5
5
|
import { BedrockClientConfig } from '../utils/client.js';
|
|
6
6
|
import { BedrockMessageMetadataByModality, BedrockSystemPromptMetadata } from '../message-types.js';
|
|
7
|
-
import { BedrockConverseModels, ResolveConverseProviderOptions, ResolveInputModalities } from '../model-meta.js';
|
|
7
|
+
import { BedrockConverseModels, BedrockModelReasoningByName, ResolveConverseProviderOptions, ResolveInputModalities } from '../model-meta.js';
|
|
8
8
|
import type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime';
|
|
9
9
|
/** Config for the Converse adapter — same client config as the chat adapter. */
|
|
10
10
|
export interface BedrockConverseConfig extends BedrockClientConfig {
|
|
@@ -23,9 +23,15 @@ export interface BedrockConverseConfig extends BedrockClientConfig {
|
|
|
23
23
|
* so tests can subclass and inject canned Converse SDK shapes without a real
|
|
24
24
|
* AWS request.
|
|
25
25
|
*/
|
|
26
|
-
|
|
26
|
+
/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */
|
|
27
|
+
type ResolveReasoning<TModel extends string> = TModel extends keyof BedrockModelReasoningByName ? BedrockModelReasoningByName[TModel] : never;
|
|
28
|
+
export declare class BedrockConverseTextAdapter<TModel extends BedrockConverseModels, TProviderOptions extends Record<string, any> = ResolveConverseProviderOptions<TModel>, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>> extends BaseTextAdapter<TModel, TProviderOptions, TInputModalities, BedrockMessageMetadataByModality, ReadonlyArray<string>, unknown, BedrockSystemPromptMetadata, ResolveReasoning<TModel>> {
|
|
27
29
|
readonly kind: "text";
|
|
28
30
|
readonly name: "bedrock-converse";
|
|
31
|
+
readonly api = "bedrock-converse-stream";
|
|
32
|
+
readonly provider = "amazon-bedrock";
|
|
33
|
+
readonly inputModalities: readonly ("text" | "image" | "document" | "audio" | "video")[] | undefined;
|
|
34
|
+
private sourceMetadata;
|
|
29
35
|
private clientPromise?;
|
|
30
36
|
private readonly clientConfig;
|
|
31
37
|
constructor(config: BedrockConverseConfig, model: TModel);
|
|
@@ -91,3 +97,4 @@ export declare class BedrockConverseTextAdapter<TModel extends BedrockConverseMo
|
|
|
91
97
|
}
|
|
92
98
|
/** Converse adapter with an explicit API key (low-level; mirrors createBedrockChat). */
|
|
93
99
|
export declare function createBedrockConverse<TModel extends BedrockConverseModels>(model: TModel, apiKey: string, config?: Omit<BedrockConverseConfig, 'apiKey'>): BedrockConverseTextAdapter<TModel>;
|
|
100
|
+
export {};
|
|
@@ -1,30 +1,29 @@
|
|
|
1
1
|
import { resolveBedrockAuth } from "../utils/auth.js";
|
|
2
|
-
import {
|
|
2
|
+
import { BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES, BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING } from "../model-meta.js";
|
|
3
|
+
import { toConverseMessages, toolBlocksToText } from "../converse/message-converter.js";
|
|
3
4
|
import { toToolConfig } from "../converse/tool-converter.js";
|
|
4
5
|
import { buildConverseUsage } from "../converse/usage.js";
|
|
5
6
|
import { processConverseStream, throwIfConverseStreamError } from "../converse/stream-processor.js";
|
|
7
|
+
import { converseThinking } from "../converse/reasoning.js";
|
|
8
|
+
import { addPromptCachePoints } from "../converse/prompt-cache.js";
|
|
6
9
|
import { buildStructuredToolConfig } from "../converse/structured-output.js";
|
|
7
|
-
import { EventType, convertSchemaToJsonSchema } from "@tanstack/ai";
|
|
10
|
+
import { CONTEXT_OVERFLOW_CODE, EventType, convertSchemaToJsonSchema } from "@tanstack/ai";
|
|
8
11
|
import { BaseTextAdapter } from "@tanstack/ai/adapters";
|
|
9
12
|
import { toRunErrorPayload } from "@tanstack/ai/adapter-internals";
|
|
10
13
|
//#region src/adapters/converse-text.ts
|
|
11
|
-
/**
|
|
12
|
-
* Bedrock Converse text adapter. Wires the Converse translation modules (message
|
|
13
|
-
* converter, tool converter, stream processor, structured-output forced-tool
|
|
14
|
-
* builder) onto `@tanstack/ai`'s `BaseTextAdapter` and the
|
|
15
|
-
* `@aws-sdk/client-bedrock-runtime` `BedrockRuntimeClient`.
|
|
16
|
-
*
|
|
17
|
-
* The success-path AG-UI lifecycle (`RUN_STARTED`..`RUN_FINISHED`) is owned by
|
|
18
|
-
* `processConverseStream`; this adapter only owns the catch/`RUN_ERROR` path,
|
|
19
|
-
* mirroring openai-base's `chatStream`.
|
|
20
|
-
*
|
|
21
|
-
* The actual SDK calls live behind two protected seams (`sendStream` / `send`)
|
|
22
|
-
* so tests can subclass and inject canned Converse SDK shapes without a real
|
|
23
|
-
* AWS request.
|
|
24
|
-
*/
|
|
25
14
|
var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
26
15
|
kind = "text";
|
|
27
16
|
name = "bedrock-converse";
|
|
17
|
+
api = "bedrock-converse-stream";
|
|
18
|
+
provider = "amazon-bedrock";
|
|
19
|
+
inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model];
|
|
20
|
+
sourceMetadata(model) {
|
|
21
|
+
return { tanstack: { source: {
|
|
22
|
+
provider: this.provider,
|
|
23
|
+
api: this.api,
|
|
24
|
+
model
|
|
25
|
+
} } };
|
|
26
|
+
}
|
|
28
27
|
clientPromise;
|
|
29
28
|
clientConfig;
|
|
30
29
|
constructor(config, model) {
|
|
@@ -103,13 +102,19 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
103
102
|
}
|
|
104
103
|
async sendStream(input) {
|
|
105
104
|
const { ConverseStreamCommand } = await this.importBedrockRuntime();
|
|
106
|
-
const
|
|
105
|
+
const client = await this.getClient();
|
|
106
|
+
const command = new ConverseStreamCommand(input);
|
|
107
|
+
addRestoreDocuments(command, input);
|
|
108
|
+
const res = await client.send(command);
|
|
107
109
|
if (!res.stream) throw new Error("Bedrock Converse: empty stream response");
|
|
108
110
|
return res.stream;
|
|
109
111
|
}
|
|
110
112
|
async send(input) {
|
|
111
113
|
const { ConverseCommand } = await this.importBedrockRuntime();
|
|
112
|
-
|
|
114
|
+
const client = await this.getClient();
|
|
115
|
+
const command = new ConverseCommand(input);
|
|
116
|
+
addRestoreDocuments(command, input);
|
|
117
|
+
return client.send(command);
|
|
113
118
|
}
|
|
114
119
|
async *chatStream(options) {
|
|
115
120
|
try {
|
|
@@ -118,20 +123,25 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
118
123
|
model: this.model
|
|
119
124
|
});
|
|
120
125
|
const input = this.buildInput(options);
|
|
126
|
+
if (!input.toolConfig && input.messages) input.messages = toolBlocksToText(input.messages);
|
|
121
127
|
const stream = await this.sendStream(input);
|
|
122
|
-
|
|
128
|
+
for await (const chunk of processConverseStream(stream, () => this.generateId(), {
|
|
123
129
|
threadId: options.threadId,
|
|
124
130
|
parentRunId: options.parentRunId,
|
|
125
131
|
model: options.model
|
|
126
|
-
})
|
|
132
|
+
})) yield chunk.type === EventType.RUN_STARTED || chunk.type === EventType.RUN_FINISHED ? {
|
|
133
|
+
...chunk,
|
|
134
|
+
metadata: this.sourceMetadata(options.model)
|
|
135
|
+
} : chunk;
|
|
127
136
|
} catch (error) {
|
|
128
|
-
const errorPayload =
|
|
137
|
+
const errorPayload = converseRunErrorPayload(error, `${this.name}.chatStream failed`);
|
|
129
138
|
options.logger.errors(`${this.name}.chatStream fatal`, {
|
|
130
139
|
error: errorPayload,
|
|
131
140
|
source: `${this.name}.chatStream`
|
|
132
141
|
});
|
|
133
142
|
yield {
|
|
134
143
|
type: EventType.RUN_ERROR,
|
|
144
|
+
metadata: this.sourceMetadata(options.model),
|
|
135
145
|
model: options.model,
|
|
136
146
|
timestamp: Date.now(),
|
|
137
147
|
message: errorPayload.message,
|
|
@@ -156,8 +166,9 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
156
166
|
provider: this.name,
|
|
157
167
|
model: this.model
|
|
158
168
|
});
|
|
169
|
+
const { reasoning: _reasoning, ...requestOptions } = chatOptions;
|
|
159
170
|
const input = {
|
|
160
|
-
...this.buildInput(
|
|
171
|
+
...this.buildInput(requestOptions),
|
|
161
172
|
toolConfig: buildStructuredToolConfig(outputSchema)
|
|
162
173
|
};
|
|
163
174
|
const res = await this.send(input);
|
|
@@ -200,8 +211,9 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
200
211
|
provider: this.name,
|
|
201
212
|
model: this.model
|
|
202
213
|
});
|
|
214
|
+
const { reasoning: _reasoning, ...requestOptions } = chatOptions;
|
|
203
215
|
const input = {
|
|
204
|
-
...this.buildInput(
|
|
216
|
+
...this.buildInput(requestOptions),
|
|
205
217
|
toolConfig: buildStructuredToolConfig(outputSchema)
|
|
206
218
|
};
|
|
207
219
|
const stream = await this.sendStream(input);
|
|
@@ -210,6 +222,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
210
222
|
hasEmittedRunStarted = true;
|
|
211
223
|
yield {
|
|
212
224
|
type: EventType.RUN_STARTED,
|
|
225
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
213
226
|
runId,
|
|
214
227
|
threadId,
|
|
215
228
|
model: chatOptions.model,
|
|
@@ -259,6 +272,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
259
272
|
hasEmittedRunStarted = true;
|
|
260
273
|
yield {
|
|
261
274
|
type: EventType.RUN_STARTED,
|
|
275
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
262
276
|
runId,
|
|
263
277
|
threadId,
|
|
264
278
|
model: chatOptions.model,
|
|
@@ -276,6 +290,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
276
290
|
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`;
|
|
277
291
|
yield {
|
|
278
292
|
type: EventType.RUN_ERROR,
|
|
293
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
279
294
|
runId,
|
|
280
295
|
model: chatOptions.model,
|
|
281
296
|
timestamp: Date.now(),
|
|
@@ -291,6 +306,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
291
306
|
if (accumulatedRaw.length === 0) {
|
|
292
307
|
yield {
|
|
293
308
|
type: EventType.RUN_ERROR,
|
|
309
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
294
310
|
runId,
|
|
295
311
|
model: chatOptions.model,
|
|
296
312
|
timestamp: Date.now(),
|
|
@@ -309,6 +325,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
309
325
|
} catch {
|
|
310
326
|
yield {
|
|
311
327
|
type: EventType.RUN_ERROR,
|
|
328
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
312
329
|
runId,
|
|
313
330
|
model: chatOptions.model,
|
|
314
331
|
timestamp: Date.now(),
|
|
@@ -333,6 +350,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
333
350
|
};
|
|
334
351
|
yield {
|
|
335
352
|
type: EventType.RUN_FINISHED,
|
|
353
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
336
354
|
runId,
|
|
337
355
|
threadId,
|
|
338
356
|
model: chatOptions.model,
|
|
@@ -345,6 +363,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
345
363
|
hasEmittedRunStarted = true;
|
|
346
364
|
yield {
|
|
347
365
|
type: EventType.RUN_STARTED,
|
|
366
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
348
367
|
runId,
|
|
349
368
|
threadId,
|
|
350
369
|
model: chatOptions.model,
|
|
@@ -352,13 +371,14 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
352
371
|
parentRunId: chatOptions.parentRunId
|
|
353
372
|
};
|
|
354
373
|
}
|
|
355
|
-
const errorPayload =
|
|
374
|
+
const errorPayload = converseRunErrorPayload(error, `${this.name}.structuredOutputStream failed`);
|
|
356
375
|
chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {
|
|
357
376
|
error: errorPayload,
|
|
358
377
|
source: `${this.name}.structuredOutputStream`
|
|
359
378
|
});
|
|
360
379
|
yield {
|
|
361
380
|
type: EventType.RUN_ERROR,
|
|
381
|
+
metadata: this.sourceMetadata(chatOptions.model),
|
|
362
382
|
runId,
|
|
363
383
|
model: chatOptions.model,
|
|
364
384
|
timestamp: Date.now(),
|
|
@@ -386,12 +406,19 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
386
406
|
* `toolConfig` with the forced structured tool afterwards).
|
|
387
407
|
*/
|
|
388
408
|
buildInput(options) {
|
|
389
|
-
const { system, messages } = toConverseMessages(options.messages, options.systemPrompts
|
|
390
|
-
|
|
409
|
+
const { system, messages } = toConverseMessages(options.messages, options.systemPrompts, {
|
|
410
|
+
model: options.model,
|
|
411
|
+
provider: this.provider
|
|
412
|
+
});
|
|
391
413
|
const modelOptions = options.modelOptions;
|
|
392
414
|
const temperature = modelOptions?.temperature;
|
|
393
415
|
const topP = modelOptions?.top_p;
|
|
394
|
-
const
|
|
416
|
+
const { additionalModelRequestFields, minMaxTokens } = converseThinking(this.model, options.reasoning, BEDROCK_MODEL_REASONING[this.model]);
|
|
417
|
+
const canForceTool = additionalModelRequestFields === void 0 && !BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES.some((name) => this.model.includes(name));
|
|
418
|
+
const toolChoice = options.toolChoice === "none" || canForceTool ? options.toolChoice : "auto";
|
|
419
|
+
const toolConfig = options.tools ? toToolConfig(convertTools(options.tools), toolChoice) : void 0;
|
|
420
|
+
const requestedMaxTokens = modelOptions?.max_completion_tokens;
|
|
421
|
+
const maxTokens = minMaxTokens !== void 0 && (requestedMaxTokens == null || requestedMaxTokens < minMaxTokens) ? minMaxTokens : requestedMaxTokens;
|
|
395
422
|
const stop = modelOptions?.stop;
|
|
396
423
|
const stopSequences = stop == null ? void 0 : Array.isArray(stop) ? stop : [stop];
|
|
397
424
|
const inferenceConfig = temperature != null || topP != null || maxTokens != null || stopSequences != null ? {
|
|
@@ -400,13 +427,15 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
400
427
|
...maxTokens != null && { maxTokens },
|
|
401
428
|
...stopSequences != null && { stopSequences }
|
|
402
429
|
} : void 0;
|
|
403
|
-
|
|
430
|
+
const input = {
|
|
404
431
|
modelId: this.model,
|
|
405
432
|
messages,
|
|
406
433
|
...system.length > 0 && { system },
|
|
407
434
|
...toolConfig && { toolConfig },
|
|
408
|
-
...inferenceConfig && { inferenceConfig }
|
|
435
|
+
...inferenceConfig && { inferenceConfig },
|
|
436
|
+
...additionalModelRequestFields && { additionalModelRequestFields }
|
|
409
437
|
};
|
|
438
|
+
return addPromptCachePoints(this.model, input, options.promptCache);
|
|
410
439
|
}
|
|
411
440
|
};
|
|
412
441
|
/**
|
|
@@ -414,6 +443,25 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
414
443
|
* the SAME `convertSchemaToJsonSchema` the other adapters use so the Converse
|
|
415
444
|
* tool input schemas match what every other provider sends.
|
|
416
445
|
*/
|
|
446
|
+
/**
|
|
447
|
+
* The text Bedrock models use for a context overflow. Converse has no error
|
|
448
|
+
* type for it: it is a `ValidationException` like every bad request, and over
|
|
449
|
+
* a stream the SDK does not keep that name. So the message is the only signal.
|
|
450
|
+
* Each model family has its own text.
|
|
451
|
+
*/
|
|
452
|
+
var CONTEXT_OVERFLOW_MESSAGE = /Input Tokens Exceeded|maximum context length|Input is too long for requested model|prompt is too long/;
|
|
453
|
+
/**
|
|
454
|
+
* `toRunErrorPayload`, with `code` set to `CONTEXT_OVERFLOW_CODE` for a
|
|
455
|
+
* context overflow. A `ThrottlingException` is never an overflow, whatever
|
|
456
|
+
* its text says about tokens.
|
|
457
|
+
*/
|
|
458
|
+
function converseRunErrorPayload(error, fallbackMessage) {
|
|
459
|
+
const payload = toRunErrorPayload(error, fallbackMessage);
|
|
460
|
+
return !(error instanceof Error && error.name === "ThrottlingException") && CONTEXT_OVERFLOW_MESSAGE.test(payload.message) ? {
|
|
461
|
+
...payload,
|
|
462
|
+
code: CONTEXT_OVERFLOW_CODE
|
|
463
|
+
} : payload;
|
|
464
|
+
}
|
|
417
465
|
function convertTools(tools) {
|
|
418
466
|
return tools.map((tool) => {
|
|
419
467
|
const inputSchema = convertSchemaToJsonSchema(tool.inputSchema) ?? {
|
|
@@ -441,6 +489,44 @@ function extractStructuredToolInput(res) {
|
|
|
441
489
|
if (block.toolUse.name === "structured_output" || block.toolUse.name === void 0) return block.toolUse.input;
|
|
442
490
|
}
|
|
443
491
|
}
|
|
492
|
+
function hasProtoKey(value) {
|
|
493
|
+
if (typeof value !== "object" || value === null) return false;
|
|
494
|
+
return Object.hasOwn(value, "__proto__") || Object.values(value).some(hasProtoKey);
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* The AWS SDK copies a document (tool input, tool input schema) with
|
|
498
|
+
* `out[key] = value`. For an own key named `__proto__`, the assignment goes
|
|
499
|
+
* to the `__proto__` setter and the key is lost. This puts each such
|
|
500
|
+
* document back in the serialized body. `JSON.stringify` keeps own keys.
|
|
501
|
+
*/
|
|
502
|
+
function restoreDocuments(request, input) {
|
|
503
|
+
if (![...(input.messages ?? []).flatMap((message) => (message.content ?? []).map((block) => block.toolUse?.input)), ...(input.toolConfig?.tools ?? []).map((tool) => tool.toolSpec?.inputSchema?.json)].some(hasProtoKey)) return;
|
|
504
|
+
if (typeof request !== "object" || request === null || !("body" in request) || typeof request.body !== "string") throw new Error("Bedrock Converse: cannot keep a `__proto__` key because the request body is not a JSON string");
|
|
505
|
+
const body = JSON.parse(request.body);
|
|
506
|
+
body.messages?.forEach((message, i) => message.content?.forEach((block, j) => {
|
|
507
|
+
const source = input.messages?.[i]?.content?.[j]?.toolUse?.input;
|
|
508
|
+
if (block.toolUse && hasProtoKey(source)) block.toolUse.input = source;
|
|
509
|
+
}));
|
|
510
|
+
body.toolConfig?.tools?.forEach((tool, i) => {
|
|
511
|
+
const source = input.toolConfig?.tools?.[i]?.toolSpec?.inputSchema?.json;
|
|
512
|
+
if (tool.toolSpec?.inputSchema && hasProtoKey(source)) tool.toolSpec.inputSchema.json = source;
|
|
513
|
+
});
|
|
514
|
+
request.body = JSON.stringify(body);
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Adds {@link restoreDocuments} to a command. It runs at `build` with high
|
|
518
|
+
* priority, before the SDK sets Content-Length and signs the body.
|
|
519
|
+
*/
|
|
520
|
+
function addRestoreDocuments(command, input) {
|
|
521
|
+
command.middlewareStack.add((next) => (args) => {
|
|
522
|
+
restoreDocuments(args.request, input);
|
|
523
|
+
return next(args);
|
|
524
|
+
}, {
|
|
525
|
+
step: "build",
|
|
526
|
+
name: "tanstackRestoreDocuments",
|
|
527
|
+
priority: "high"
|
|
528
|
+
});
|
|
529
|
+
}
|
|
444
530
|
/** Converse adapter with an explicit API key (low-level; mirrors createBedrockChat). */
|
|
445
531
|
function createBedrockConverse(model, apiKey, config) {
|
|
446
532
|
return new BedrockConverseTextAdapter({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"converse-text.js","names":[],"sources":["../../../src/adapters/converse-text.ts"],"sourcesContent":["import { EventType, convertSchemaToJsonSchema } from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport { resolveBedrockAuth } from '../utils/auth'\nimport { toConverseMessages } from '../converse/message-converter'\nimport { toToolConfig } from '../converse/tool-converter'\nimport {\n processConverseStream,\n throwIfConverseStreamError,\n} from '../converse/stream-processor'\nimport { buildConverseUsage } from '../converse/usage'\nimport {\n STRUCTURED_TOOL_NAME,\n buildStructuredToolConfig,\n} from '../converse/structured-output'\nimport type { ResolvedBedrockAuth } from '../utils/auth'\nimport type { ConverseToolInput } from '../converse/tool-converter'\nimport type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime'\nimport type {\n BedrockRuntimeClient,\n BedrockRuntimeClientConfig,\n ContentBlock,\n ConverseCommandInput,\n ConverseCommandOutput,\n ConverseStreamCommandInput,\n ConverseStreamOutput,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type {\n JSONSchema,\n Modality,\n AdapterYieldChunk,\n TextOptions,\n TokenUsage,\n Tool,\n} from '@tanstack/ai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type {\n BedrockMessageMetadataByModality,\n BedrockSystemPromptMetadata,\n BedrockToolMetadata,\n} from '../message-types'\nimport type {\n BedrockConverseModels,\n ResolveConverseProviderOptions,\n ResolveInputModalities,\n} from '../model-meta'\n\n/** Config for the Converse adapter — same client config as the chat adapter. */\nexport interface BedrockConverseConfig extends BedrockClientConfig {}\n\n/**\n * Bedrock Converse text adapter. Wires the Converse translation modules (message\n * converter, tool converter, stream processor, structured-output forced-tool\n * builder) onto `@tanstack/ai`'s `BaseTextAdapter` and the\n * `@aws-sdk/client-bedrock-runtime` `BedrockRuntimeClient`.\n *\n * The success-path AG-UI lifecycle (`RUN_STARTED`..`RUN_FINISHED`) is owned by\n * `processConverseStream`; this adapter only owns the catch/`RUN_ERROR` path,\n * mirroring openai-base's `chatStream`.\n *\n * The actual SDK calls live behind two protected seams (`sendStream` / `send`)\n * so tests can subclass and inject canned Converse SDK shapes without a real\n * AWS request.\n */\nexport class BedrockConverseTextAdapter<\n TModel extends BedrockConverseModels,\n // Constraint mirrors the chat adapter (text.ts): the base parameterises\n // `TProviderOptions extends Record<string, any>`, and our default\n // `ResolveConverseProviderOptions<TModel>` resolves to an interface lacking an\n // implicit index signature — which `Record<string, unknown>` would reject but\n // `Record<string, any>` accepts. Confined to the generic constraint (the\n // established adapter pattern) — no value `as` cast is introduced.\n TProviderOptions extends Record<string, any> =\n ResolveConverseProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n BedrockMessageMetadataByModality,\n // TToolCapabilities — Converse has no per-model tool-capability table; the\n // base default.\n ReadonlyArray<string>,\n // TToolCallMetadata — Converse has no tool-call metadata round-tripping.\n unknown,\n // TSystemPromptMetadata — narrows `systemPrompts[i].metadata` at the chat()\n // call site so users get `cachePoint` autocomplete.\n BedrockSystemPromptMetadata\n> {\n override readonly kind = 'text' as const\n override readonly name = 'bedrock-converse' as const\n private clientPromise?: Promise<BedrockRuntimeClient>\n private readonly clientConfig: BedrockConverseConfig\n\n constructor(config: BedrockConverseConfig, model: TModel) {\n super({}, model)\n // Defer client construction and auth resolution: the AWS SDK is Node/\n // server-only, so we must not pull it into the static graph here. The\n // client (and its dynamic import) is built lazily on first SDK call.\n this.clientConfig = config\n }\n\n /**\n * Dynamically import `@aws-sdk/client-bedrock-runtime`. The specifier is held\n * in a variable (not a string literal) so bundler dep scanners (e.g. Vite/\n * esbuild optimizeDeps) cannot statically discover the AWS SDK and try to\n * pre-bundle it for the browser — it would fail on the SDK's Node-only\n * `fromTokenFile` export chain. The SDK is Node/server-only and is only\n * reached on a real request. `typeof import(...)` is a type-only reference\n * (erased at emit) so the imported members keep full typing.\n */\n protected importBedrockRuntime(): Promise<typeof BedrockRuntime> {\n const mod = '@aws-sdk/client-bedrock-runtime'\n return import(/* @vite-ignore */ mod) as Promise<typeof BedrockRuntime>\n }\n\n /**\n * Lazily construct the `BedrockRuntimeClient`. The dynamic import keeps\n * `@aws-sdk/client-bedrock-runtime` out of the static/browser graph and\n * defers `resolveBedrockAuth` until a real request is made.\n */\n protected async getClient(): Promise<BedrockRuntimeClient> {\n if (!this.clientPromise) {\n this.clientPromise = (async () => {\n const { BedrockRuntimeClient } = await this.importBedrockRuntime()\n const region = this.clientConfig.region ?? 'us-east-1'\n const resolved = resolveBedrockAuth(\n {\n apiKey: this.clientConfig.apiKey,\n region,\n auth: this.clientConfig.auth,\n },\n 'runtime',\n )\n const client = new BedrockRuntimeClient(\n this.buildClientConfig(resolved, region, this.clientConfig.baseURL),\n )\n const defaultHeaders = this.clientConfig.defaultHeaders\n if (defaultHeaders) {\n // `build` runs before SigV4 signing, so gateway headers are signed\n // along with the rest of the request.\n client.middlewareStack.add(\n (next) => (args) => {\n const request = args.request\n if (\n typeof request === 'object' &&\n request !== null &&\n 'headers' in request &&\n typeof request.headers === 'object' &&\n request.headers !== null\n ) {\n Object.assign(request.headers, defaultHeaders)\n }\n return next(args)\n },\n { step: 'build', name: 'tanstackDefaultHeaders' },\n )\n }\n return client\n })().catch((error: unknown) => {\n // Don't cache a rejected promise — clear it so a later call can retry\n // (e.g. after a transient import failure or fixed auth config).\n this.clientPromise = undefined\n throw error\n })\n }\n return this.clientPromise\n }\n\n /**\n * Map resolved auth + endpoint to a `BedrockRuntimeClientConfig`.\n *\n * Recent `@aws-sdk/client-bedrock-runtime` exposes a first-class `token`\n * config field for Bedrock API-key bearer auth. But the client's default\n * auth-scheme order is SigV4 first, then bearer — so passing `token` alone is\n * not enough: the SDK still resolves SigV4 and throws \"Could not load\n * credentials from any providers\". Pinning `authSchemePreference` to the\n * bearer scheme makes the API key actually get used. SigV4 uses the AWS\n * credential provider chain and the default scheme order.\n */\n protected buildClientConfig(\n resolved: ResolvedBedrockAuth,\n region: string,\n endpoint: string | undefined,\n ): BedrockRuntimeClientConfig {\n if (resolved.kind === 'bearer') {\n return {\n region,\n token: { token: resolved.token },\n authSchemePreference: ['httpBearerAuth'],\n ...(endpoint ? { endpoint } : {}),\n }\n }\n return {\n region: resolved.region,\n credentials: resolved.credentials,\n ...(endpoint ? { endpoint } : {}),\n }\n }\n\n // ---------------------------------------------------------------------------\n // SDK seams (overridden in tests so no real AWS call happens)\n // ---------------------------------------------------------------------------\n\n protected async sendStream(\n input: ConverseStreamCommandInput,\n ): Promise<AsyncIterable<ConverseStreamOutput>> {\n const { ConverseStreamCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const res = await client.send(new ConverseStreamCommand(input))\n if (!res.stream) {\n throw new Error('Bedrock Converse: empty stream response')\n }\n return res.stream\n }\n\n protected async send(\n input: ConverseCommandInput,\n ): Promise<ConverseCommandOutput> {\n const { ConverseCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n return client.send(new ConverseCommand(input))\n }\n\n // ---------------------------------------------------------------------------\n // Public adapter surface\n // ---------------------------------------------------------------------------\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n try {\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const input = this.buildInput(options)\n const stream = await this.sendStream(input)\n yield* processConverseStream(stream, () => this.generateId(), {\n threadId: options.threadId,\n parentRunId: options.parentRunId,\n model: options.model,\n })\n } catch (error: unknown) {\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes` (AG-UI's `RunErrorEvent.code` is optional).\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Structured output via the forced-tool strategy. Converse has no native\n * json_schema response_format, so we force a single tool whose input schema\n * is the requested output schema and read the model's `toolUse.input` back as\n * the structured result.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n try {\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const input: ConverseCommandInput = {\n ...this.buildInput(chatOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const res = await this.send(input)\n // A forced tool call cut off at the output cap carries partial input,\n // which would read like a schema failure (issue #1426).\n if (res.stopReason === 'max_tokens') {\n throw new Error(\n `${this.name}.structuredOutput: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`,\n )\n }\n const structured = extractStructuredToolInput(res)\n if (structured === undefined) {\n throw new Error(\n `${this.name}.structuredOutput: response contained no forced-tool output`,\n )\n }\n const usage = res.usage\n return {\n data: structured,\n rawText: JSON.stringify(structured),\n ...(usage && { usage: buildConverseUsage(usage) }),\n }\n } catch (error: unknown) {\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Streaming structured output. Same forced-tool strategy as\n * `structuredOutput`, but streamed: the forced tool's `toolUse.input` JSON\n * fragments are accumulated from the Converse stream and a terminal\n * `CUSTOM 'structured-output.complete'` event carries `{ object, raw }`,\n * mirroring openai-base's `structuredOutputStream` contract exactly.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n const { chatOptions, outputSchema } = options\n const runId = this.generateId()\n const threadId = chatOptions.threadId ?? this.generateId()\n const messageId = this.generateId()\n\n let hasEmittedRunStarted = false\n let hasEmittedTextMessageStart = false\n let accumulatedRaw = ''\n let finishReason: 'stop' | 'length' | 'content_filter' = 'stop'\n // Usage arrives on the trailing `metadata` event, after the finish signal,\n // so it is captured during iteration and folded into RUN_FINISHED below.\n let usage: TokenUsage | undefined\n\n try {\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const input: ConverseStreamCommandInput = {\n ...this.buildInput(chatOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const stream = await this.sendStream(input)\n\n // The forced tool streams its `input` as partial-JSON fragments inside\n // `contentBlockDelta.delta.toolUse.input`. We surface them as\n // TEXT_MESSAGE_CONTENT deltas (raw JSON text), matching openai-base which\n // carries the structured JSON as text deltas.\n for await (const ev of stream) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n // Surface in-band server/throttle/validation errors instead of\n // letting them fall through and masquerade as an empty response.\n throwIfConverseStreamError(ev)\n\n if ('contentBlockDelta' in ev) {\n const delta = ev.contentBlockDelta?.delta\n const fragment =\n delta && 'toolUse' in delta ? delta.toolUse?.input : undefined\n if (fragment !== undefined) {\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n accumulatedRaw += fragment\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: fragment,\n content: accumulatedRaw,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // The forced structured-output tool produces stopReason 'tool_use' on\n // success, but that's an implementation detail — a cleanly-completed\n // structured run reports 'stop', matching openai-base's contract.\n finishReason =\n stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n\n // Same truncation check as `structuredOutput()`: report the token limit\n // before the empty-content and parse errors (issue #1426).\n if (finishReason === 'length') {\n 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`\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message,\n code: 'max_tokens',\n error: { message, code: 'max_tokens' },\n }\n return\n }\n\n if (accumulatedRaw.length === 0) {\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n error: {\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n },\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedRaw)\n } catch {\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: `Failed to parse structured output as JSON. Content: ${accumulatedRaw.slice(0, 200)}${accumulatedRaw.length > 200 ? '...' : ''}`,\n code: 'parse-error',\n error: {\n message: 'Failed to parse structured output as JSON',\n code: 'parse-error',\n },\n }\n return\n }\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: parsed,\n raw: accumulatedRaw,\n },\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n finishReason,\n ...(usage && { usage }),\n }\n } catch (error: unknown) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Converse sends `tools` and a forced structured-output tool via two separate\n * mechanisms, never together. Declaring `false` makes the engine run the\n * agent loop without `outputSchema` and finalize via `structuredOutput` /\n * `structuredOutputStream`.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return false\n }\n\n // ---------------------------------------------------------------------------\n // Request construction\n // ---------------------------------------------------------------------------\n\n /**\n * Translate `TextOptions` into a `ConverseCommandInput`. Shared by chatStream,\n * structuredOutput, and structuredOutputStream (the latter two override\n * `toolConfig` with the forced structured tool afterwards).\n */\n protected buildInput(\n options: TextOptions<TProviderOptions>,\n ): ConverseCommandInput {\n const { system, messages } = toConverseMessages(\n options.messages,\n options.systemPrompts,\n )\n\n const toolConfig = options.tools\n ? toToolConfig(convertTools(options.tools), 'auto')\n : undefined\n\n // Sampling options live on `modelOptions` (typed as the narrowed\n // `BedrockConverseProviderOptions`, which surfaces the OpenAI Chat\n // Completions field names); translate them into Converse's `inferenceConfig`,\n // which uses AWS-native camelCase keys.\n const modelOptions = options.modelOptions\n const temperature = modelOptions?.temperature\n const topP = modelOptions?.top_p\n const maxTokens = modelOptions?.max_completion_tokens\n const stop = modelOptions?.stop\n const stopSequences =\n stop == null ? undefined : Array.isArray(stop) ? stop : [stop]\n\n const inferenceConfig =\n temperature != null ||\n topP != null ||\n maxTokens != null ||\n stopSequences != null\n ? {\n ...(temperature != null && { temperature }),\n ...(topP != null && { topP }),\n ...(maxTokens != null && { maxTokens }),\n ...(stopSequences != null && { stopSequences }),\n }\n : undefined\n\n return {\n modelId: this.model,\n messages,\n ...(system.length > 0 && { system }),\n ...(toolConfig && { toolConfig }),\n ...(inferenceConfig && { inferenceConfig }),\n }\n }\n}\n\n/**\n * Convert TanStack `Tool[]` to the Converse tool-converter input shape. Reuses\n * the SAME `convertSchemaToJsonSchema` the other adapters use so the Converse\n * tool input schemas match what every other provider sends.\n */\nfunction convertTools(tools: Array<Tool>): Array<ConverseToolInput> {\n return tools.map((tool) => {\n const inputSchema: JSONSchema = convertSchemaToJsonSchema(\n tool.inputSchema,\n ) ?? { type: 'object', properties: {}, required: [] }\n const { cachePoint }: BedrockToolMetadata = tool.metadata ?? {}\n return {\n name: tool.name,\n description: tool.description,\n inputSchema,\n ...(cachePoint && { cachePoint }),\n }\n })\n}\n\n/**\n * Find the forced structured-output tool's `input` in a non-streaming Converse\n * response. SDK-boundary narrowing only — `ConverseOutput` is a tagged union\n * (`{ message }`) and a tool-use block is `{ toolUse: { input } }`.\n */\nfunction extractStructuredToolInput(\n res: ConverseCommandOutput,\n): unknown | undefined {\n const message =\n res.output && 'message' in res.output ? res.output.message : undefined\n const content: Array<ContentBlock> = message?.content ?? []\n for (const block of content) {\n if ('toolUse' in block && block.toolUse) {\n // Only accept the forced structured tool (an unnamed block is allowed,\n // since the forced tool is the only one configured). A differently-named\n // tool-use block is a hallucinated/leftover call whose arbitrary input\n // must not be returned as the validated result — leave it to the caller's\n // `throw` so the failure is accurate instead of silently wrong.\n if (\n block.toolUse.name === STRUCTURED_TOOL_NAME ||\n block.toolUse.name === undefined\n ) {\n return block.toolUse.input\n }\n }\n }\n return undefined\n}\n\n/** Converse adapter with an explicit API key (low-level; mirrors createBedrockChat). */\nexport function createBedrockConverse<TModel extends BedrockConverseModels>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockConverseConfig, 'apiKey'>,\n): BedrockConverseTextAdapter<TModel> {\n return new BedrockConverseTextA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AgB;EAC3C;CACF;AACF;;;;;;AAOA,SAAS,aAAa,OAA8C;CAClE,OAAO,MAAM,KAAK,SAAS;EACzB,MAAM,cAA0B,0BAC9B,KAAK,WACP,KAAK;GAAE,MAAM;GAAU,YAAY,CAAC;GAAG,UAAU,CAAC;EAAE;EACpD,MAAM,EAAE,eAAoC,KAAK,YAAY,CAAC;EAC9D,OAAO;GACL,MAAM,KAAK;GACX,aAAa,KAAK;GAClB;GACA,GAAI,cAAc,EAAE,WAAW;EACjC;CACF,CAAC;AACH;;;;;;AAOA,SAAS,2BACP,KACqB;CAGrB,MAAM,WADJ,IAAI,UAAU,aAAa,IAAI,SAAS,IAAI,OAAO,UAAU,KAAA,EAAA,EACjB,WAAW,CAAC;CAC1D,KAAK,MAAM,SAAS,SAClB,IAAI,aAAa,SAAS,MAAM,SAO5B;MAAA,MAAM,QAAQ,SAAA,uBACd,MAAM,QAAQ,SAAS,KAAA,GAEvB,OAAO,MAAM,QAAQ;CAAA;AAK7B;;AAGA,SAAgB,sBACd,OACA,QACA,QACoC;CACpC,OAAO,IAAI,2BAA2B;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AACpE"}
|
|
1
|
+
{"version":3,"file":"converse-text.js","names":[],"sources":["../../../src/adapters/converse-text.ts"],"sourcesContent":["import {\n CONTEXT_OVERFLOW_CODE,\n EventType,\n convertSchemaToJsonSchema,\n} from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport { resolveBedrockAuth } from '../utils/auth'\nimport {\n toConverseMessages,\n toolBlocksToText,\n} from '../converse/message-converter'\nimport { toToolConfig } from '../converse/tool-converter'\nimport {\n processConverseStream,\n throwIfConverseStreamError,\n} from '../converse/stream-processor'\nimport { buildConverseUsage } from '../converse/usage'\nimport { converseThinking } from '../converse/reasoning'\nimport {\n BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES,\n BEDROCK_MODEL_INPUT_MODALITIES,\n BEDROCK_MODEL_REASONING,\n} from '../model-meta'\nimport { addPromptCachePoints } from '../converse/prompt-cache'\nimport {\n STRUCTURED_TOOL_NAME,\n buildStructuredToolConfig,\n} from '../converse/structured-output'\nimport type { ResolvedBedrockAuth } from '../utils/auth'\nimport type { ConverseToolInput } from '../converse/tool-converter'\nimport type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime'\nimport type {\n BedrockRuntimeClient,\n BedrockRuntimeClientConfig,\n ContentBlock,\n ConverseCommandInput,\n ConverseCommandOutput,\n ConverseStreamCommandInput,\n ConverseStreamOutput,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type { MiddlewareStack } from '@smithy/types'\nimport type {\n JSONSchema,\n Modality,\n AdapterYieldChunk,\n TextOptions,\n TokenUsage,\n Tool,\n} from '@tanstack/ai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type {\n BedrockMessageMetadataByModality,\n BedrockSystemPromptMetadata,\n BedrockToolMetadata,\n} from '../message-types'\nimport type {\n BedrockConverseModels,\n BedrockModelReasoningByName,\n ResolveConverseProviderOptions,\n ResolveInputModalities,\n} from '../model-meta'\n\n/** Config for the Converse adapter — same client config as the chat adapter. */\nexport interface BedrockConverseConfig extends BedrockClientConfig {}\n\n/**\n * Bedrock Converse text adapter. Wires the Converse translation modules (message\n * converter, tool converter, stream processor, structured-output forced-tool\n * builder) onto `@tanstack/ai`'s `BaseTextAdapter` and the\n * `@aws-sdk/client-bedrock-runtime` `BedrockRuntimeClient`.\n *\n * The success-path AG-UI lifecycle (`RUN_STARTED`..`RUN_FINISHED`) is owned by\n * `processConverseStream`; this adapter only owns the catch/`RUN_ERROR` path,\n * mirroring openai-base's `chatStream`.\n *\n * The actual SDK calls live behind two protected seams (`sendStream` / `send`)\n * so tests can subclass and inject canned Converse SDK shapes without a real\n * AWS request.\n */\n/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */\ntype ResolveReasoning<TModel extends string> =\n TModel extends keyof BedrockModelReasoningByName\n ? BedrockModelReasoningByName[TModel]\n : never\n\nexport class BedrockConverseTextAdapter<\n TModel extends BedrockConverseModels,\n // Constraint mirrors the chat adapter (text.ts): the base parameterises\n // `TProviderOptions extends Record<string, any>`, and our default\n // `ResolveConverseProviderOptions<TModel>` resolves to an interface lacking an\n // implicit index signature — which `Record<string, unknown>` would reject but\n // `Record<string, any>` accepts. Confined to the generic constraint (the\n // established adapter pattern) — no value `as` cast is introduced.\n TProviderOptions extends Record<string, any> =\n ResolveConverseProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n BedrockMessageMetadataByModality,\n // TToolCapabilities — Converse has no per-model tool-capability table; the\n // base default.\n ReadonlyArray<string>,\n // TToolCallMetadata — Converse has no tool-call metadata round-tripping.\n unknown,\n // TSystemPromptMetadata — narrows `systemPrompts[i].metadata` at the chat()\n // call site so users get `cachePoint` autocomplete.\n BedrockSystemPromptMetadata,\n ResolveReasoning<TModel>\n> {\n override readonly kind = 'text' as const\n override readonly name = 'bedrock-converse' as const\n override readonly api = 'bedrock-converse-stream'\n override readonly provider = 'amazon-bedrock'\n override readonly inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model]\n\n private sourceMetadata(model: string) {\n return {\n tanstack: { source: { provider: this.provider, api: this.api, model } },\n }\n }\n private clientPromise?: Promise<BedrockRuntimeClient>\n private readonly clientConfig: BedrockConverseConfig\n\n constructor(config: BedrockConverseConfig, model: TModel) {\n super({}, model)\n // Defer client construction and auth resolution: the AWS SDK is Node/\n // server-only, so we must not pull it into the static graph here. The\n // client (and its dynamic import) is built lazily on first SDK call.\n this.clientConfig = config\n }\n\n /**\n * Dynamically import `@aws-sdk/client-bedrock-runtime`. The specifier is held\n * in a variable (not a string literal) so bundler dep scanners (e.g. Vite/\n * esbuild optimizeDeps) cannot statically discover the AWS SDK and try to\n * pre-bundle it for the browser — it would fail on the SDK's Node-only\n * `fromTokenFile` export chain. The SDK is Node/server-only and is only\n * reached on a real request. `typeof import(...)` is a type-only reference\n * (erased at emit) so the imported members keep full typing.\n */\n protected importBedrockRuntime(): Promise<typeof BedrockRuntime> {\n const mod = '@aws-sdk/client-bedrock-runtime'\n return import(/* @vite-ignore */ mod) as Promise<typeof BedrockRuntime>\n }\n\n /**\n * Lazily construct the `BedrockRuntimeClient`. The dynamic import keeps\n * `@aws-sdk/client-bedrock-runtime` out of the static/browser graph and\n * defers `resolveBedrockAuth` until a real request is made.\n */\n protected async getClient(): Promise<BedrockRuntimeClient> {\n if (!this.clientPromise) {\n this.clientPromise = (async () => {\n const { BedrockRuntimeClient } = await this.importBedrockRuntime()\n const region = this.clientConfig.region ?? 'us-east-1'\n const resolved = resolveBedrockAuth(\n {\n apiKey: this.clientConfig.apiKey,\n region,\n auth: this.clientConfig.auth,\n },\n 'runtime',\n )\n const client = new BedrockRuntimeClient(\n this.buildClientConfig(resolved, region, this.clientConfig.baseURL),\n )\n const defaultHeaders = this.clientConfig.defaultHeaders\n if (defaultHeaders) {\n // `build` runs before SigV4 signing, so gateway headers are signed\n // along with the rest of the request.\n client.middlewareStack.add(\n (next) => (args) => {\n const request = args.request\n if (\n typeof request === 'object' &&\n request !== null &&\n 'headers' in request &&\n typeof request.headers === 'object' &&\n request.headers !== null\n ) {\n Object.assign(request.headers, defaultHeaders)\n }\n return next(args)\n },\n { step: 'build', name: 'tanstackDefaultHeaders' },\n )\n }\n return client\n })().catch((error: unknown) => {\n // Don't cache a rejected promise — clear it so a later call can retry\n // (e.g. after a transient import failure or fixed auth config).\n this.clientPromise = undefined\n throw error\n })\n }\n return this.clientPromise\n }\n\n /**\n * Map resolved auth + endpoint to a `BedrockRuntimeClientConfig`.\n *\n * Recent `@aws-sdk/client-bedrock-runtime` exposes a first-class `token`\n * config field for Bedrock API-key bearer auth. But the client's default\n * auth-scheme order is SigV4 first, then bearer — so passing `token` alone is\n * not enough: the SDK still resolves SigV4 and throws \"Could not load\n * credentials from any providers\". Pinning `authSchemePreference` to the\n * bearer scheme makes the API key actually get used. SigV4 uses the AWS\n * credential provider chain and the default scheme order.\n */\n protected buildClientConfig(\n resolved: ResolvedBedrockAuth,\n region: string,\n endpoint: string | undefined,\n ): BedrockRuntimeClientConfig {\n if (resolved.kind === 'bearer') {\n return {\n region,\n token: { token: resolved.token },\n authSchemePreference: ['httpBearerAuth'],\n ...(endpoint ? { endpoint } : {}),\n }\n }\n return {\n region: resolved.region,\n credentials: resolved.credentials,\n ...(endpoint ? { endpoint } : {}),\n }\n }\n\n // ---------------------------------------------------------------------------\n // SDK seams (overridden in tests so no real AWS call happens)\n // ---------------------------------------------------------------------------\n\n protected async sendStream(\n input: ConverseStreamCommandInput,\n ): Promise<AsyncIterable<ConverseStreamOutput>> {\n const { ConverseStreamCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const command = new ConverseStreamCommand(input)\n addRestoreDocuments(command, input)\n const res = await client.send(command)\n if (!res.stream) {\n throw new Error('Bedrock Converse: empty stream response')\n }\n return res.stream\n }\n\n protected async send(\n input: ConverseCommandInput,\n ): Promise<ConverseCommandOutput> {\n const { ConverseCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const command = new ConverseCommand(input)\n addRestoreDocuments(command, input)\n return client.send(command)\n }\n\n // ---------------------------------------------------------------------------\n // Public adapter surface\n // ---------------------------------------------------------------------------\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n try {\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const input = this.buildInput(options)\n // Bedrock rejects toolUse and toolResult blocks without a toolConfig,\n // so a request with no tools sends its tool history as text.\n if (!input.toolConfig && input.messages) {\n input.messages = toolBlocksToText(input.messages)\n }\n const stream = await this.sendStream(input)\n for await (const chunk of processConverseStream(\n stream,\n () => this.generateId(),\n {\n threadId: options.threadId,\n parentRunId: options.parentRunId,\n model: options.model,\n },\n )) {\n yield chunk.type === EventType.RUN_STARTED ||\n chunk.type === EventType.RUN_FINISHED\n ? { ...chunk, metadata: this.sourceMetadata(options.model) }\n : chunk\n }\n } catch (error: unknown) {\n const errorPayload = converseRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes` (AG-UI's `RunErrorEvent.code` is optional).\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(options.model),\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Structured output via the forced-tool strategy. Converse has no native\n * json_schema response_format, so we force a single tool whose input schema\n * is the requested output schema and read the model's `toolUse.input` back as\n * the structured result.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n try {\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n // Claude rejects a forced tool while thinking is on, so the forced\n // structured-output tool goes without `chat({ reasoning })`.\n const { reasoning: _reasoning, ...requestOptions } = chatOptions\n const input: ConverseCommandInput = {\n ...this.buildInput(requestOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const res = await this.send(input)\n // A forced tool call cut off at the output cap carries partial input,\n // which would read like a schema failure (issue #1426).\n if (res.stopReason === 'max_tokens') {\n throw new Error(\n `${this.name}.structuredOutput: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`,\n )\n }\n const structured = extractStructuredToolInput(res)\n if (structured === undefined) {\n throw new Error(\n `${this.name}.structuredOutput: response contained no forced-tool output`,\n )\n }\n const usage = res.usage\n return {\n data: structured,\n rawText: JSON.stringify(structured),\n ...(usage && { usage: buildConverseUsage(usage) }),\n }\n } catch (error: unknown) {\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Streaming structured output. Same forced-tool strategy as\n * `structuredOutput`, but streamed: the forced tool's `toolUse.input` JSON\n * fragments are accumulated from the Converse stream and a terminal\n * `CUSTOM 'structured-output.complete'` event carries `{ object, raw }`,\n * mirroring openai-base's `structuredOutputStream` contract exactly.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n const { chatOptions, outputSchema } = options\n const runId = this.generateId()\n const threadId = chatOptions.threadId ?? this.generateId()\n const messageId = this.generateId()\n\n let hasEmittedRunStarted = false\n let hasEmittedTextMessageStart = false\n let accumulatedRaw = ''\n let finishReason: 'stop' | 'length' | 'content_filter' = 'stop'\n // Usage arrives on the trailing `metadata` event, after the finish signal,\n // so it is captured during iteration and folded into RUN_FINISHED below.\n let usage: TokenUsage | undefined\n\n try {\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n // Claude rejects a forced tool while thinking is on, so the forced\n // structured-output tool goes without `chat({ reasoning })`.\n const { reasoning: _reasoning, ...requestOptions } = chatOptions\n const input: ConverseStreamCommandInput = {\n ...this.buildInput(requestOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const stream = await this.sendStream(input)\n\n // The forced tool streams its `input` as partial-JSON fragments inside\n // `contentBlockDelta.delta.toolUse.input`. We surface them as\n // TEXT_MESSAGE_CONTENT deltas (raw JSON text), matching openai-base which\n // carries the structured JSON as text deltas.\n for await (const ev of stream) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n // Surface in-band server/throttle/validation errors instead of\n // letting them fall through and masquerade as an empty response.\n throwIfConverseStreamError(ev)\n\n if ('contentBlockDelta' in ev) {\n const delta = ev.contentBlockDelta?.delta\n const fragment =\n delta && 'toolUse' in delta ? delta.toolUse?.input : undefined\n if (fragment !== undefined) {\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n accumulatedRaw += fragment\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: fragment,\n content: accumulatedRaw,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // The forced structured-output tool produces stopReason 'tool_use' on\n // success, but that's an implementation detail — a cleanly-completed\n // structured run reports 'stop', matching openai-base's contract.\n finishReason =\n stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n\n // Same truncation check as `structuredOutput()`: report the token limit\n // before the empty-content and parse errors (issue #1426).\n if (finishReason === 'length') {\n 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`\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message,\n code: 'max_tokens',\n error: { message, code: 'max_tokens' },\n }\n return\n }\n\n if (accumulatedRaw.length === 0) {\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n error: {\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n },\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedRaw)\n } catch {\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: `Failed to parse structured output as JSON. Content: ${accumulatedRaw.slice(0, 200)}${accumulatedRaw.length > 200 ? '...' : ''}`,\n code: 'parse-error',\n error: {\n message: 'Failed to parse structured output as JSON',\n code: 'parse-error',\n },\n }\n return\n }\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: parsed,\n raw: accumulatedRaw,\n },\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n finishReason,\n ...(usage && { usage }),\n }\n } catch (error: unknown) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n const errorPayload = converseRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Converse sends `tools` and a forced structured-output tool via two separate\n * mechanisms, never together. Declaring `false` makes the engine run the\n * agent loop without `outputSchema` and finalize via `structuredOutput` /\n * `structuredOutputStream`.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return false\n }\n\n // ---------------------------------------------------------------------------\n // Request construction\n // ---------------------------------------------------------------------------\n\n /**\n * Translate `TextOptions` into a `ConverseCommandInput`. Shared by chatStream,\n * structuredOutput, and structuredOutputStream (the latter two override\n * `toolConfig` with the forced structured tool afterwards).\n */\n protected buildInput(\n options: TextOptions<TProviderOptions>,\n ): ConverseCommandInput {\n const { system, messages } = toConverseMessages(\n options.messages,\n options.systemPrompts,\n { model: options.model, provider: this.provider },\n )\n\n // Sampling options live on `modelOptions` (typed as the narrowed\n // `BedrockConverseProviderOptions`, which surfaces the OpenAI Chat\n // Completions field names); translate them into Converse's `inferenceConfig`,\n // which uses AWS-native camelCase keys.\n const modelOptions = options.modelOptions\n const temperature = modelOptions?.temperature\n const topP = modelOptions?.top_p\n // `chat({ reasoning })`: Claude's thinking fields. Budget thinking needs\n // `maxTokens` above the budget, so a smaller or missing value grows.\n const { additionalModelRequestFields, minMaxTokens } = converseThinking(\n this.model,\n options.reasoning,\n BEDROCK_MODEL_REASONING[this.model],\n )\n\n // `chat({ toolChoice })`. Converse has no `none` choice, so `none` sends\n // no tool config, and `chatStream` sends any tool history as text. Claude\n // rejects a forced tool while thinking is on (only Claude gets thinking\n // fields), and some Claude models reject it on every request. Then a\n // forced choice falls back to auto.\n const canForceTool =\n additionalModelRequestFields === undefined &&\n !BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES.some((name) =>\n this.model.includes(name),\n )\n const toolChoice =\n options.toolChoice === 'none' || canForceTool\n ? options.toolChoice\n : 'auto'\n const toolConfig = options.tools\n ? toToolConfig(convertTools(options.tools), toolChoice)\n : undefined\n\n const requestedMaxTokens = modelOptions?.max_completion_tokens\n const maxTokens =\n minMaxTokens !== undefined &&\n (requestedMaxTokens == null || requestedMaxTokens < minMaxTokens)\n ? minMaxTokens\n : requestedMaxTokens\n const stop = modelOptions?.stop\n const stopSequences =\n stop == null ? undefined : Array.isArray(stop) ? stop : [stop]\n\n const inferenceConfig =\n temperature != null ||\n topP != null ||\n maxTokens != null ||\n stopSequences != null\n ? {\n ...(temperature != null && { temperature }),\n ...(topP != null && { topP }),\n ...(maxTokens != null && { maxTokens }),\n ...(stopSequences != null && { stopSequences }),\n }\n : undefined\n\n const input: ConverseCommandInput = {\n modelId: this.model,\n messages,\n ...(system.length > 0 && { system }),\n ...(toolConfig && { toolConfig }),\n ...(inferenceConfig && { inferenceConfig }),\n ...(additionalModelRequestFields && { additionalModelRequestFields }),\n }\n return addPromptCachePoints(this.model, input, options.promptCache)\n }\n}\n\n/**\n * Convert TanStack `Tool[]` to the Converse tool-converter input shape. Reuses\n * the SAME `convertSchemaToJsonSchema` the other adapters use so the Converse\n * tool input schemas match what every other provider sends.\n */\n/**\n * The text Bedrock models use for a context overflow. Converse has no error\n * type for it: it is a `ValidationException` like every bad request, and over\n * a stream the SDK does not keep that name. So the message is the only signal.\n * Each model family has its own text.\n */\nconst CONTEXT_OVERFLOW_MESSAGE =\n /Input Tokens Exceeded|maximum context length|Input is too long for requested model|prompt is too long/\n\n/**\n * `toRunErrorPayload`, with `code` set to `CONTEXT_OVERFLOW_CODE` for a\n * context overflow. A `ThrottlingException` is never an overflow, whatever\n * its text says about tokens.\n */\nfunction converseRunErrorPayload(\n error: unknown,\n fallbackMessage: string,\n): { message: string; code: string | undefined } {\n const payload = toRunErrorPayload(error, fallbackMessage)\n const throttled =\n error instanceof Error && error.name === 'ThrottlingException'\n return !throttled && CONTEXT_OVERFLOW_MESSAGE.test(payload.message)\n ? { ...payload, code: CONTEXT_OVERFLOW_CODE }\n : payload\n}\n\nfunction convertTools(tools: Array<Tool>): Array<ConverseToolInput> {\n return tools.map((tool) => {\n const inputSchema: JSONSchema = convertSchemaToJsonSchema(\n tool.inputSchema,\n ) ?? { type: 'object', properties: {}, required: [] }\n const { cachePoint }: BedrockToolMetadata = tool.metadata ?? {}\n return {\n name: tool.name,\n description: tool.description,\n inputSchema,\n ...(cachePoint && { cachePoint }),\n }\n })\n}\n\n/**\n * Find the forced structured-output tool's `input` in a non-streaming Converse\n * response. SDK-boundary narrowing only — `ConverseOutput` is a tagged union\n * (`{ message }`) and a tool-use block is `{ toolUse: { input } }`.\n */\nfunction extractStructuredToolInput(\n res: ConverseCommandOutput,\n): unknown | undefined {\n const message =\n res.output && 'message' in res.output ? res.output.message : undefined\n const content: Array<ContentBlock> = message?.content ?? []\n for (const block of content) {\n if ('toolUse' in block && block.toolUse) {\n // Only accept the forced structured tool (an unnamed block is allowed,\n // since the forced tool is the only one configured). A differently-named\n // tool-use block is a hallucinated/leftover call whose arbitrary input\n // must not be returned as the validated result — leave it to the caller's\n // `throw` so the failure is accurate instead of silently wrong.\n if (\n block.toolUse.name === STRUCTURED_TOOL_NAME ||\n block.toolUse.name === undefined\n ) {\n return block.toolUse.input\n }\n }\n }\n return undefined\n}\n\n/** The parts of a Converse request that hold documents the adapter fills from user data. */\ntype ConverseDocuments = Pick<ConverseCommandInput, 'messages' | 'toolConfig'>\n\nfunction hasProtoKey(value: unknown): boolean {\n if (typeof value !== 'object' || value === null) return false\n return (\n Object.hasOwn(value, '__proto__') || Object.values(value).some(hasProtoKey)\n )\n}\n\n/**\n * The AWS SDK copies a document (tool input, tool input schema) with\n * `out[key] = value`. For an own key named `__proto__`, the assignment goes\n * to the `__proto__` setter and the key is lost. This puts each such\n * document back in the serialized body. `JSON.stringify` keeps own keys.\n */\nfunction restoreDocuments(request: unknown, input: ConverseDocuments): void {\n const documents = [\n ...(input.messages ?? []).flatMap((message) =>\n (message.content ?? []).map((block) => block.toolUse?.input),\n ),\n ...(input.toolConfig?.tools ?? []).map(\n (tool) => tool.toolSpec?.inputSchema?.json,\n ),\n ]\n // Skip the parse and re-serialize of the body, which can be large (for\n // example with image bytes), unless a document has the key.\n if (!documents.some(hasProtoKey)) return\n if (\n typeof request !== 'object' ||\n request === null ||\n !('body' in request) ||\n typeof request.body !== 'string'\n ) {\n throw new Error(\n 'Bedrock Converse: cannot keep a `__proto__` key because the request body is not a JSON string',\n )\n }\n // The SDK serialized this body from `input` one step earlier, so the two\n // line up by position.\n const body: ConverseDocuments = JSON.parse(request.body)\n body.messages?.forEach((message, i) =>\n message.content?.forEach((block, j) => {\n const source = input.messages?.[i]?.content?.[j]?.toolUse?.input\n if (block.toolUse && hasProtoKey(source)) block.toolUse.input = source\n }),\n )\n body.toolConfig?.tools?.forEach((tool, i) => {\n const source = input.toolConfig?.tools?.[i]?.toolSpec?.inputSchema?.json\n if (tool.toolSpec?.inputSchema && hasProtoKey(source))\n tool.toolSpec.inputSchema.json = source\n })\n request.body = JSON.stringify(body)\n}\n\n/**\n * Adds {@link restoreDocuments} to a command. It runs at `build` with high\n * priority, before the SDK sets Content-Length and signs the body.\n */\nfunction addRestoreDocuments<TInput extends object, TOutput extends object>(\n command: { middlewareStack: MiddlewareStack<TInput, TOutput> },\n input: ConverseDocuments,\n): void {\n command.middlewareStack.add(\n (next) => (args) => {\n restoreDocuments(args.request, input)\n return next(args)\n },\n { step: 'build', name: 'tanstackRestoreDocuments', priority: 'high' },\n )\n}\n\n/** Converse adapter with an explicit API key (low-level; mirrors createBedrockChat). */\nexport function createBedrockConverse<TModel extends BedrockConverseModels>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockConverseConfig, 'apiKey'>,\n): BedrockConverseTextAdapter<TModel> {\n return new BedrockConverseTextAdapter({ ...config, apiKey }, model)\n}\n"],"mappings":";;;;;;;;;;;;;AA0FA,IAAa,6BAAb,cAYU,gBAcR;CACA,OAAyB;CACzB,OAAyB;CACzB,MAAwB;CACxB,WAA6B;CAC7B,kBAAoC,+BAA+B,KAAK;CAExE,eAAuB,OAAe;EACpC,OAAO,EACL,UAAU,EAAE,QAAQ;GAAE,UAAU,KAAK;GAAU,KAAK,KAAK;GAAK;EAAM,EAAE,EACxE;CACF;CACA;CACA;CAEA,YAAY,QAA+B,OAAe;EACxD,MAAM,CAAC,GAAG,KAAK;EAIf,KAAK,eAAe;CACtB;;;;;;;;;;CAWA,uBAAiE;EAE/D,OAAO;;GAA0B;;CACnC;;;;;;CAOA,MAAgB,YAA2C;EACzD,IAAI,CAAC,KAAK,eACR,KAAK,iBAAiB,YAAY;GAChC,MAAM,EAAE,yBAAyB,MAAM,KAAK,qBAAqB;GACjE,MAAM,SAAS,KAAK,aAAa,UAAU;GAC3C,MAAM,WAAW,mBACf;IACE,QAAQ,KAAK,aAAa;IAC1B;IACA,MAAM,KAAK,aAAa;GAC1B,GACA,SACF;GACA,MAAM,SAAS,IAAI,qBACjB,KAAK,kBAAkB,UAAU,QAAQ,KAAK,aAAa,OAAO,CACpE;GACA,MAAM,iBAAiB,KAAK,aAAa;GACzC,IAAI,gBAGF,OAAO,gBAAgB,KACpB,UAAU,SAAS;IAClB,MAAM,UAAU,KAAK;IACrB,IACE,OAAO,YAAY,YACnB,YAAY,QACZ,aAAa,WACb,OAAO,QAAQ,YAAY,YAC3B,QAAQ,YAAY,MAEpB,OAAO,OAAO,QAAQ,SAAS,cAAc;IAE/C,OAAO,KAAK,IAAI;GAClB,GACA;IAAE,MAAM;IAAS,MAAM;GAAyB,CAClD;GAEF,OAAO;EACT,EA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GAAG,KAAK;AACpE"}
|
|
@@ -2,11 +2,13 @@ import { OpenAIBaseResponsesTextAdapter, OpenAIBaseTextAdapterOptions } from '@t
|
|
|
2
2
|
import { Modality } from '@tanstack/ai';
|
|
3
3
|
import { BedrockClientConfig } from '../utils/client.js';
|
|
4
4
|
import { BedrockMessageMetadataByModality } from '../message-types.js';
|
|
5
|
-
import { BedrockChatModelToolCapabilitiesByName, BedrockResponsesModels, ResolveInputModalities } from '../model-meta.js';
|
|
5
|
+
import { BedrockChatModelToolCapabilitiesByName, BedrockModelReasoningByName, BedrockResponsesModels, ResolveInputModalities } from '../model-meta.js';
|
|
6
6
|
import { ExternalResponsesProviderOptions } from '../text/responses-provider-options.js';
|
|
7
7
|
export interface BedrockResponsesConfig extends BedrockClientConfig, OpenAIBaseTextAdapterOptions {
|
|
8
8
|
}
|
|
9
9
|
export type { ExternalResponsesProviderOptions as BedrockResponsesProviderOptions } from '../text/responses-provider-options.js';
|
|
10
|
+
/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */
|
|
11
|
+
type ResolveReasoning<TModel extends string> = TModel extends keyof BedrockModelReasoningByName ? BedrockModelReasoningByName[TModel] : never;
|
|
10
12
|
type ResolveToolCapabilities<TModel extends string> = TModel extends keyof BedrockChatModelToolCapabilitiesByName ? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]> : readonly [];
|
|
11
13
|
/**
|
|
12
14
|
* Bedrock Responses adapter. Drives mantle's OpenAI-compatible `/responses`
|
|
@@ -14,10 +16,17 @@ type ResolveToolCapabilities<TModel extends string> = TModel extends keyof Bedro
|
|
|
14
16
|
* class ai-openai's `openaiText` uses. Responses is mantle-only, so the
|
|
15
17
|
* constructor forces the mantle baseURL.
|
|
16
18
|
*/
|
|
17
|
-
export declare class BedrockResponsesTextAdapter<TModel extends BedrockResponsesModels, TProviderOptions extends Record<string, any> = ExternalResponsesProviderOptions, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>, TToolCapabilities extends ReadonlyArray<string> = ResolveToolCapabilities<TModel>> extends OpenAIBaseResponsesTextAdapter<TModel, TProviderOptions, TInputModalities, BedrockMessageMetadataByModality, TToolCapabilities
|
|
19
|
+
export declare class BedrockResponsesTextAdapter<TModel extends BedrockResponsesModels, TProviderOptions extends Record<string, any> = ExternalResponsesProviderOptions, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>, TToolCapabilities extends ReadonlyArray<string> = ResolveToolCapabilities<TModel>> extends OpenAIBaseResponsesTextAdapter<TModel, TProviderOptions, TInputModalities, BedrockMessageMetadataByModality, TToolCapabilities, ResolveReasoning<TModel>> {
|
|
18
20
|
readonly kind: "text";
|
|
19
21
|
readonly name: "bedrock-responses";
|
|
22
|
+
readonly provider = "amazon-bedrock";
|
|
23
|
+
readonly inputModalities: readonly ("text" | "image" | "document" | "audio" | "video")[] | undefined;
|
|
20
24
|
constructor(config: BedrockResponsesConfig, model: TModel);
|
|
25
|
+
protected isContextOverflow(error: {
|
|
26
|
+
message: string;
|
|
27
|
+
code: string | undefined;
|
|
28
|
+
}): boolean;
|
|
29
|
+
protected modelReasoning(model: string): import('@tanstack/ai').ModelReasoning | undefined;
|
|
21
30
|
}
|
|
22
31
|
/** Responses adapter with an explicit API key (low-level; the public branching factory delegates here). */
|
|
23
32
|
export declare function createBedrockResponsesText<TModel extends BedrockResponsesModels>(model: TModel, apiKey: string, config?: Omit<BedrockResponsesConfig, 'apiKey'>): BedrockResponsesTextAdapter<TModel>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { withBedrockDefaults } from "../utils/client.js";
|
|
1
|
+
import { isBedrockOpenAIContextOverflow, withBedrockDefaults } from "../utils/client.js";
|
|
2
|
+
import { BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING } from "../model-meta.js";
|
|
2
3
|
import OpenAI from "openai";
|
|
3
4
|
import { OpenAIBaseResponsesTextAdapter } from "@tanstack/openai-base";
|
|
4
5
|
//#region src/adapters/responses-text.ts
|
|
@@ -11,6 +12,8 @@ import { OpenAIBaseResponsesTextAdapter } from "@tanstack/openai-base";
|
|
|
11
12
|
var BedrockResponsesTextAdapter = class extends OpenAIBaseResponsesTextAdapter {
|
|
12
13
|
kind = "text";
|
|
13
14
|
name = "bedrock-responses";
|
|
15
|
+
provider = "amazon-bedrock";
|
|
16
|
+
inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model];
|
|
14
17
|
constructor(config, model) {
|
|
15
18
|
const options = withBedrockDefaults(config, "mantle", model);
|
|
16
19
|
super(model, "bedrock-responses", new OpenAI(options), {
|
|
@@ -18,6 +21,12 @@ var BedrockResponsesTextAdapter = class extends OpenAIBaseResponsesTextAdapter {
|
|
|
18
21
|
fetch: options.fetch
|
|
19
22
|
});
|
|
20
23
|
}
|
|
24
|
+
isContextOverflow(error) {
|
|
25
|
+
return super.isContextOverflow(error) || isBedrockOpenAIContextOverflow(error);
|
|
26
|
+
}
|
|
27
|
+
modelReasoning(model) {
|
|
28
|
+
return BEDROCK_MODEL_REASONING[model];
|
|
29
|
+
}
|
|
21
30
|
};
|
|
22
31
|
/** Responses adapter with an explicit API key (low-level; the public branching factory delegates here). */
|
|
23
32
|
function createBedrockResponsesText(model, apiKey, config) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"responses-text.js","names":[],"sources":["../../../src/adapters/responses-text.ts"],"sourcesContent":["import OpenAI from 'openai'\nimport { OpenAIBaseResponsesTextAdapter } from '@tanstack/openai-base'\nimport {
|
|
1
|
+
{"version":3,"file":"responses-text.js","names":[],"sources":["../../../src/adapters/responses-text.ts"],"sourcesContent":["import OpenAI from 'openai'\nimport { OpenAIBaseResponsesTextAdapter } from '@tanstack/openai-base'\nimport {\n isBedrockOpenAIContextOverflow,\n withBedrockDefaults,\n} from '../utils/client'\nimport {\n BEDROCK_MODEL_INPUT_MODALITIES,\n BEDROCK_MODEL_REASONING,\n} from '../model-meta'\nimport type { Modality } from '@tanstack/ai'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type { BedrockMessageMetadataByModality } from '../message-types'\nimport type {\n BedrockChatModelToolCapabilitiesByName,\n BedrockModelReasoningByName,\n BedrockResponsesModels,\n ResolveInputModalities,\n} from '../model-meta'\nimport type { ExternalResponsesProviderOptions } from '../text/responses-provider-options'\nimport type { OpenAIBaseTextAdapterOptions } from '@tanstack/openai-base'\n\nexport interface BedrockResponsesConfig\n extends BedrockClientConfig, OpenAIBaseTextAdapterOptions {}\n\nexport type { ExternalResponsesProviderOptions as BedrockResponsesProviderOptions } from '../text/responses-provider-options'\n\n/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */\ntype ResolveReasoning<TModel extends string> =\n TModel extends keyof BedrockModelReasoningByName\n ? BedrockModelReasoningByName[TModel]\n : never\n\ntype ResolveToolCapabilities<TModel extends string> =\n TModel extends keyof BedrockChatModelToolCapabilitiesByName\n ? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]>\n : readonly []\n\n/**\n * Bedrock Responses adapter. Drives mantle's OpenAI-compatible `/responses`\n * endpoint via the OpenAI SDK (`client.responses.create`) — the same base\n * class ai-openai's `openaiText` uses. Responses is mantle-only, so the\n * constructor forces the mantle baseURL.\n */\nexport class BedrockResponsesTextAdapter<\n TModel extends BedrockResponsesModels,\n // Constraint mirrors the chat adapter (and ai-groq / ai-openai) and the base,\n // which parameterises `TProviderOptions extends Record<string, any>`. Our\n // default `ExternalResponsesProviderOptions` is an interface that (lacking an\n // implicit index signature) `Record<string, unknown>` would reject but\n // `Record<string, any>` accepts. This `any` is confined to the generic\n // constraint — no value/shape `as` cast is introduced.\n TProviderOptions extends Record<string, any> =\n ExternalResponsesProviderOptions,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n TToolCapabilities extends ReadonlyArray<string> =\n ResolveToolCapabilities<TModel>,\n> extends OpenAIBaseResponsesTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n BedrockMessageMetadataByModality,\n TToolCapabilities,\n ResolveReasoning<TModel>\n> {\n override readonly kind = 'text' as const\n override readonly name = 'bedrock-responses' as const\n override readonly provider = 'amazon-bedrock'\n override readonly inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model]\n\n constructor(config: BedrockResponsesConfig, model: TModel) {\n // Responses is mantle-only — force the mantle base URL (an explicit\n // config.baseURL still wins, e.g. E2E pointing at aimock).\n const options = withBedrockDefaults(config, 'mantle', model)\n super(model, 'bedrock-responses', new OpenAI(options), {\n ...config,\n fetch: options.fetch,\n })\n }\n\n protected override isContextOverflow(error: {\n message: string\n code: string | undefined\n }): boolean {\n return (\n super.isContextOverflow(error) || isBedrockOpenAIContextOverflow(error)\n )\n }\n\n protected override modelReasoning(model: string) {\n return BEDROCK_MODEL_REASONING[model]\n }\n}\n\n/** Responses adapter with an explicit API key (low-level; the public branching factory delegates here). */\nexport function createBedrockResponsesText<\n TModel extends BedrockResponsesModels,\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockResponsesConfig, 'apiKey'>,\n): BedrockResponsesTextAdapter<TModel> {\n return new BedrockResponsesTextAdapter({ ...config, apiKey }, model)\n}\n"],"mappings":";;;;;;;;;;;AA4CA,IAAa,8BAAb,cAcU,+BAOR;CACA,OAAyB;CACzB,OAAyB;CACzB,WAA6B;CAC7B,kBAAoC,+BAA+B,KAAK;CAExE,YAAY,QAAgC,OAAe;EAGzD,MAAM,UAAU,oBAAoB,QAAQ,UAAU,KAAK;EAC3D,MAAM,OAAO,qBAAqB,IAAI,OAAO,OAAO,GAAG;GACrD,GAAG;GACH,OAAO,QAAQ;EACjB,CAAC;CACH;CAEA,kBAAqC,OAGzB;EACV,OACE,MAAM,kBAAkB,KAAK,KAAK,+BAA+B,KAAK;CAE1E;CAEA,eAAkC,OAAe;EAC/C,OAAO,wBAAwB;CACjC;AACF;;AAGA,SAAgB,2BAGd,OACA,QACA,QACqC;CACrC,OAAO,IAAI,4BAA4B;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AACrE"}
|