@tanstack/ai-bedrock 1.0.0 → 1.2.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/README.md +2 -0
- package/dist/esm/adapters/converse-text.d.ts +2 -2
- package/dist/esm/adapters/converse-text.js +21 -20
- package/dist/esm/adapters/converse-text.js.map +1 -1
- package/dist/esm/adapters/embedding.d.ts +1 -1
- package/dist/esm/adapters/embedding.js +7 -10
- package/dist/esm/adapters/embedding.js.map +1 -1
- package/dist/esm/adapters/responses-text.d.ts +1 -1
- package/dist/esm/adapters/responses-text.js +3 -4
- package/dist/esm/adapters/responses-text.js.map +1 -1
- package/dist/esm/adapters/text.d.ts +1 -1
- package/dist/esm/adapters/text.js +3 -4
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/converse/usage.js +3 -1
- package/dist/esm/converse/usage.js.map +1 -1
- package/dist/esm/utils/auth.d.ts +6 -0
- package/dist/esm/utils/auth.js +17 -5
- package/dist/esm/utils/auth.js.map +1 -1
- package/dist/esm/utils/client.js +1 -1
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/openai-sigv4-fetch.d.ts +2 -2
- package/dist/esm/utils/openai-sigv4-fetch.js +12 -23
- package/dist/esm/utils/openai-sigv4-fetch.js.map +1 -1
- package/dist/esm/utils/sigv4.d.ts +28 -0
- package/dist/esm/utils/sigv4.js +133 -0
- package/dist/esm/utils/sigv4.js.map +1 -0
- package/package.json +22 -7
- package/src/adapters/converse-text.ts +25 -13
- package/src/adapters/embedding.ts +7 -5
- package/src/adapters/responses-text.ts +6 -4
- package/src/adapters/text.ts +6 -4
- package/src/converse/usage.ts +5 -0
- package/src/utils/auth.ts +19 -3
- package/src/utils/client.ts +1 -1
- package/src/utils/openai-sigv4-fetch.ts +14 -34
- package/src/utils/sigv4.ts +202 -0
package/README.md
CHANGED
|
@@ -101,6 +101,8 @@ itself; the OpenAI-compatible chat/responses paths sign via the bundled
|
|
|
101
101
|
the standard credential chain (env vars, shared config, instance role):
|
|
102
102
|
|
|
103
103
|
```typescript
|
|
104
|
+
import { bedrockText } from '@tanstack/ai-bedrock'
|
|
105
|
+
|
|
104
106
|
const adapter = bedrockText('us.anthropic.claude-haiku-4-5-20251001-v1:0', {
|
|
105
107
|
auth: 'sigv4',
|
|
106
108
|
region: 'us-east-1',
|
|
@@ -63,8 +63,8 @@ export declare class BedrockConverseTextAdapter<TModel extends BedrockConverseMo
|
|
|
63
63
|
* credential provider chain and the default scheme order.
|
|
64
64
|
*/
|
|
65
65
|
protected buildClientConfig(resolved: ResolvedBedrockAuth, region: string, endpoint: string | undefined): BedrockRuntimeClientConfig;
|
|
66
|
-
protected sendStream(input: ConverseStreamCommandInput): Promise<AsyncIterable<ConverseStreamOutput>>;
|
|
67
|
-
protected send(input: ConverseCommandInput): Promise<ConverseCommandOutput>;
|
|
66
|
+
protected sendStream(input: ConverseStreamCommandInput, signal?: AbortSignal | null): Promise<AsyncIterable<ConverseStreamOutput>>;
|
|
67
|
+
protected send(input: ConverseCommandInput, signal?: AbortSignal | null): Promise<ConverseCommandOutput>;
|
|
68
68
|
chatStream(options: TextOptions<TProviderOptions>): AsyncIterable<AdapterYieldChunk>;
|
|
69
69
|
/**
|
|
70
70
|
* Structured output via the forced-tool strategy. Converse has no native
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { resolveBedrockAuth } from "../utils/auth.js";
|
|
1
|
+
import { importOptionalPeer, resolveBedrockAuth } from "../utils/auth.js";
|
|
2
2
|
import { BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES, BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING } from "../model-meta.js";
|
|
3
3
|
import { toConverseMessages, toolBlocksToText } from "../converse/message-converter.js";
|
|
4
4
|
import { toToolConfig } from "../converse/tool-converter.js";
|
|
@@ -40,10 +40,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
40
40
|
* (erased at emit) so the imported members keep full typing.
|
|
41
41
|
*/
|
|
42
42
|
importBedrockRuntime() {
|
|
43
|
-
return
|
|
44
|
-
/* @vite-ignore */
|
|
45
|
-
"@aws-sdk/client-bedrock-runtime"
|
|
46
|
-
);
|
|
43
|
+
return importOptionalPeer("@aws-sdk/client-bedrock-runtime");
|
|
47
44
|
}
|
|
48
45
|
/**
|
|
49
46
|
* Lazily construct the `BedrockRuntimeClient`. The dynamic import keeps
|
|
@@ -100,21 +97,21 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
100
97
|
...endpoint ? { endpoint } : {}
|
|
101
98
|
};
|
|
102
99
|
}
|
|
103
|
-
async sendStream(input) {
|
|
100
|
+
async sendStream(input, signal) {
|
|
104
101
|
const { ConverseStreamCommand } = await this.importBedrockRuntime();
|
|
105
102
|
const client = await this.getClient();
|
|
106
103
|
const command = new ConverseStreamCommand(input);
|
|
107
104
|
addRestoreDocuments(command, input);
|
|
108
|
-
const res = await client.send(command);
|
|
105
|
+
const res = await client.send(command, { abortSignal: signal ?? void 0 });
|
|
109
106
|
if (!res.stream) throw new Error("Bedrock Converse: empty stream response");
|
|
110
107
|
return res.stream;
|
|
111
108
|
}
|
|
112
|
-
async send(input) {
|
|
109
|
+
async send(input, signal) {
|
|
113
110
|
const { ConverseCommand } = await this.importBedrockRuntime();
|
|
114
111
|
const client = await this.getClient();
|
|
115
112
|
const command = new ConverseCommand(input);
|
|
116
113
|
addRestoreDocuments(command, input);
|
|
117
|
-
return client.send(command);
|
|
114
|
+
return client.send(command, { abortSignal: signal ?? void 0 });
|
|
118
115
|
}
|
|
119
116
|
async *chatStream(options) {
|
|
120
117
|
try {
|
|
@@ -124,7 +121,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
124
121
|
});
|
|
125
122
|
const input = this.buildInput(options);
|
|
126
123
|
if (!input.toolConfig && input.messages) input.messages = toolBlocksToText(input.messages);
|
|
127
|
-
const stream = await this.sendStream(input);
|
|
124
|
+
const stream = await this.sendStream(input, options.request?.signal);
|
|
128
125
|
for await (const chunk of processConverseStream(stream, () => this.generateId(), {
|
|
129
126
|
threadId: options.threadId,
|
|
130
127
|
parentRunId: options.parentRunId,
|
|
@@ -171,15 +168,15 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
171
168
|
...this.buildInput(requestOptions),
|
|
172
169
|
toolConfig: buildStructuredToolConfig(outputSchema)
|
|
173
170
|
};
|
|
174
|
-
const res = await this.send(input);
|
|
175
|
-
|
|
171
|
+
const res = await this.send(input, chatOptions.request?.signal);
|
|
172
|
+
const usage = res.usage && buildConverseUsage(res.usage);
|
|
173
|
+
if (res.stopReason === "max_tokens") throw Object.assign(/* @__PURE__ */ new Error(`${this.name}.structuredOutput: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`), usage && { usage });
|
|
176
174
|
const structured = extractStructuredToolInput(res);
|
|
177
|
-
if (structured === void 0) throw new Error(`${this.name}.structuredOutput: response contained no forced-tool output`);
|
|
178
|
-
const usage = res.usage;
|
|
175
|
+
if (structured === void 0) throw Object.assign(/* @__PURE__ */ new Error(`${this.name}.structuredOutput: response contained no forced-tool output`), usage && { usage });
|
|
179
176
|
return {
|
|
180
177
|
data: structured,
|
|
181
178
|
rawText: JSON.stringify(structured),
|
|
182
|
-
...usage && { usage
|
|
179
|
+
...usage && { usage }
|
|
183
180
|
};
|
|
184
181
|
} catch (error) {
|
|
185
182
|
chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {
|
|
@@ -216,7 +213,7 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
216
213
|
...this.buildInput(requestOptions),
|
|
217
214
|
toolConfig: buildStructuredToolConfig(outputSchema)
|
|
218
215
|
};
|
|
219
|
-
const stream = await this.sendStream(input);
|
|
216
|
+
const stream = await this.sendStream(input, chatOptions.request?.signal);
|
|
220
217
|
for await (const ev of stream) {
|
|
221
218
|
if (!hasEmittedRunStarted) {
|
|
222
219
|
hasEmittedRunStarted = true;
|
|
@@ -299,7 +296,8 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
299
296
|
error: {
|
|
300
297
|
message,
|
|
301
298
|
code: "max_tokens"
|
|
302
|
-
}
|
|
299
|
+
},
|
|
300
|
+
...usage && { usage }
|
|
303
301
|
};
|
|
304
302
|
return;
|
|
305
303
|
}
|
|
@@ -315,7 +313,8 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
315
313
|
error: {
|
|
316
314
|
message: `${this.name}.structuredOutputStream: response contained no content`,
|
|
317
315
|
code: "empty-response"
|
|
318
|
-
}
|
|
316
|
+
},
|
|
317
|
+
...usage && { usage }
|
|
319
318
|
};
|
|
320
319
|
return;
|
|
321
320
|
}
|
|
@@ -334,7 +333,8 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
334
333
|
error: {
|
|
335
334
|
message: "Failed to parse structured output as JSON",
|
|
336
335
|
code: "parse-error"
|
|
337
|
-
}
|
|
336
|
+
},
|
|
337
|
+
...usage && { usage }
|
|
338
338
|
};
|
|
339
339
|
return;
|
|
340
340
|
}
|
|
@@ -387,7 +387,8 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
387
387
|
error: {
|
|
388
388
|
message: errorPayload.message,
|
|
389
389
|
...errorPayload.code !== void 0 && { code: errorPayload.code }
|
|
390
|
-
}
|
|
390
|
+
},
|
|
391
|
+
...usage && { usage }
|
|
391
392
|
};
|
|
392
393
|
}
|
|
393
394
|
}
|
|
@@ -1 +1 @@
|
|
|
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\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,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U,SAAS;EAClB,iBAAiB,KAAK,SAAS,KAAK;EACpC,OAAO,KAAK,IAAI;CAClB,GACA;EAAE,MAAM;EAAS,MAAM;EAA4B,UAAU;CAAO,CACtE;AACF;;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 { importOptionalPeer, 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 importOptionalPeer<typeof BedrockRuntime>(mod)\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 signal?: AbortSignal | null,\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, { abortSignal: signal ?? undefined })\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 signal?: AbortSignal | null,\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, { abortSignal: signal ?? undefined })\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, options.request?.signal)\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, chatOptions.request?.signal)\n const usage = res.usage && buildConverseUsage(res.usage)\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 Object.assign(\n 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 usage && { usage },\n )\n }\n const structured = extractStructuredToolInput(res)\n if (structured === undefined) {\n throw Object.assign(\n new Error(\n `${this.name}.structuredOutput: response contained no forced-tool output`,\n ),\n usage && { usage },\n )\n }\n return {\n data: structured,\n rawText: JSON.stringify(structured),\n ...(usage && { 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\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, chatOptions.request?.signal)\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 ...(usage && { usage }),\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 ...(usage && { usage }),\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 ...(usage && { usage }),\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 ...(usage && { usage }),\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,W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,OACA,QACA,QACoC;CACpC,OAAO,IAAI,2BAA2B;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AACpE"}
|
|
@@ -55,7 +55,7 @@ export declare class BedrockEmbeddingAdapter<TModel extends BedrockEmbeddingMode
|
|
|
55
55
|
*/
|
|
56
56
|
protected buildClientConfig(resolved: ResolvedBedrockAuth, region: string, endpoint: string | undefined): BedrockRuntimeClientConfig;
|
|
57
57
|
/** Send one InvokeModel call and parse its JSON response body. */
|
|
58
|
-
protected invokeModel(modelId: string, body: Record<string, unknown
|
|
58
|
+
protected invokeModel(modelId: string, body: Record<string, unknown>, abortSignal?: AbortSignal): Promise<unknown>;
|
|
59
59
|
createEmbeddings(options: EmbeddingOptions<TProviderOptions>): Promise<EmbeddingResult>;
|
|
60
60
|
/**
|
|
61
61
|
* `amazon.titan-embed-text-v2:0` — one text per InvokeModel call, fanned
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { resolveBedrockAuth } from "../utils/auth.js";
|
|
1
|
+
import { importOptionalPeer, resolveBedrockAuth } from "../utils/auth.js";
|
|
2
2
|
import { BEDROCK_EMBEDDING_MODELS } from "../model-meta.js";
|
|
3
3
|
import { isFileSource, requireTextOnlyEmbeddingInput, resolveEmbeddingInput, unsupportedFileSourceError } from "@tanstack/ai";
|
|
4
4
|
import { BaseEmbeddingAdapter } from "@tanstack/ai/adapters";
|
|
@@ -55,10 +55,7 @@ var BedrockEmbeddingAdapter = class extends BaseEmbeddingAdapter {
|
|
|
55
55
|
* Same pattern as the Converse text adapter.
|
|
56
56
|
*/
|
|
57
57
|
importBedrockRuntime() {
|
|
58
|
-
return
|
|
59
|
-
/* @vite-ignore */
|
|
60
|
-
"@aws-sdk/client-bedrock-runtime"
|
|
61
|
-
);
|
|
58
|
+
return importOptionalPeer("@aws-sdk/client-bedrock-runtime");
|
|
62
59
|
}
|
|
63
60
|
/**
|
|
64
61
|
* Lazily construct the `BedrockRuntimeClient`, deferring
|
|
@@ -99,14 +96,14 @@ var BedrockEmbeddingAdapter = class extends BaseEmbeddingAdapter {
|
|
|
99
96
|
};
|
|
100
97
|
}
|
|
101
98
|
/** Send one InvokeModel call and parse its JSON response body. */
|
|
102
|
-
async invokeModel(modelId, body) {
|
|
99
|
+
async invokeModel(modelId, body, abortSignal) {
|
|
103
100
|
const { InvokeModelCommand } = await this.importBedrockRuntime();
|
|
104
101
|
const response = await (await this.getClient()).send(new InvokeModelCommand({
|
|
105
102
|
modelId,
|
|
106
103
|
contentType: "application/json",
|
|
107
104
|
accept: "application/json",
|
|
108
105
|
body: JSON.stringify(body)
|
|
109
|
-
}));
|
|
106
|
+
}), abortSignal ? { abortSignal } : {});
|
|
110
107
|
return JSON.parse(new TextDecoder().decode(response.body));
|
|
111
108
|
}
|
|
112
109
|
async createEmbeddings(options) {
|
|
@@ -144,7 +141,7 @@ var BedrockEmbeddingAdapter = class extends BaseEmbeddingAdapter {
|
|
|
144
141
|
const body = { inputText: text };
|
|
145
142
|
if (dimensions !== void 0) body.dimensions = dimensions;
|
|
146
143
|
if (normalize !== void 0) body.normalize = normalize;
|
|
147
|
-
return readTitanEmbeddingBody(await this.invokeModel(model, body), `${this.name} ${model}`);
|
|
144
|
+
return readTitanEmbeddingBody(await this.invokeModel(model, body, options.abortSignal), `${this.name} ${model}`);
|
|
148
145
|
});
|
|
149
146
|
return this.toTitanResult(model, responses);
|
|
150
147
|
}
|
|
@@ -164,7 +161,7 @@ var BedrockEmbeddingAdapter = class extends BaseEmbeddingAdapter {
|
|
|
164
161
|
if (item.texts.length > 0) body.inputText = item.texts.join("\n");
|
|
165
162
|
const image = item.images[0];
|
|
166
163
|
if (image) body.inputImage = toTitanInputImage(image, model);
|
|
167
|
-
return readTitanEmbeddingBody(await this.invokeModel(model, body), `${this.name} ${model}`);
|
|
164
|
+
return readTitanEmbeddingBody(await this.invokeModel(model, body, options.abortSignal), `${this.name} ${model}`);
|
|
168
165
|
});
|
|
169
166
|
return this.toTitanResult(model, responses);
|
|
170
167
|
}
|
|
@@ -185,7 +182,7 @@ var BedrockEmbeddingAdapter = class extends BaseEmbeddingAdapter {
|
|
|
185
182
|
input_type: inputType
|
|
186
183
|
};
|
|
187
184
|
if (truncate !== void 0) body.truncate = truncate;
|
|
188
|
-
return readCohereEmbeddingBody(await this.invokeModel(model, body), `${this.name} ${model}`);
|
|
185
|
+
return readCohereEmbeddingBody(await this.invokeModel(model, body, options.abortSignal), `${this.name} ${model}`);
|
|
189
186
|
});
|
|
190
187
|
return {
|
|
191
188
|
id: this.generateId(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"embedding.js","names":[],"sources":["../../../src/adapters/embedding.ts"],"sourcesContent":["import { BaseEmbeddingAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport {\n isFileSource,\n requireTextOnlyEmbeddingInput,\n resolveEmbeddingInput,\n unsupportedFileSourceError,\n} from '@tanstack/ai'\nimport { resolveBedrockAuth } from '../utils/auth'\nimport { BEDROCK_EMBEDDING_MODELS } from '../model-meta'\nimport type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime'\nimport type {\n BedrockRuntimeClient,\n BedrockRuntimeClientConfig,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type {\n EmbeddingOptions,\n EmbeddingResult,\n ImagePart,\n TokenUsage,\n} from '@tanstack/ai'\nimport type { ResolvedBedrockAuth } from '../utils/auth'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type {\n BedrockEmbeddingModel,\n BedrockEmbeddingModelInputModalitiesByName,\n BedrockEmbeddingModelProviderOptionsByName,\n ResolveEmbeddingProviderOptions,\n} from '../model-meta'\n\n/**\n * Config for the Bedrock embedding adapter — the same auth surface as the\n * other Bedrock adapters (apiKey → env → SigV4 via `resolveBedrockAuth`),\n * minus the OpenAI-compat client options that don't apply to `InvokeModel`.\n */\nexport interface BedrockEmbeddingConfig extends Pick<\n BedrockClientConfig,\n 'apiKey' | 'region' | 'auth' | 'baseURL'\n> {}\n\n/** InvokeModel calls issued concurrently during a per-item fan-out. */\nconst MAX_CONCURRENT_INVOCATIONS = 5\n\n/** Valid `dimensions` for `amazon.titan-embed-text-v2:0`. */\nconst TITAN_TEXT_DIMENSIONS: ReadonlyArray<number> = [256, 512, 1024]\n\n/** Valid `dimensions` (outputEmbeddingLength) for `amazon.titan-embed-image-v1`. */\nconst TITAN_IMAGE_DIMENSIONS: ReadonlyArray<number> = [256, 384, 1024]\nconst TITAN_IMAGE_DEFAULT_DIMENSIONS = 1024\n\n/** Cohere embed accepts at most 96 texts per InvokeModel call. */\nconst COHERE_MAX_BATCH_SIZE = 96\n\n/**\n * Bedrock Embedding Adapter\n *\n * Tree-shakeable adapter for embeddings served through Bedrock's native\n * `InvokeModel` API (embedding models have no Converse surface). Each model\n * family has its own JSON body dialect:\n *\n * - `amazon.titan-embed-text-v2:0` — text-only, ONE text per call; the batch\n * is fanned out with a small concurrency cap and per-call\n * `inputTextTokenCount`s are summed into usage.\n * - `amazon.titan-embed-image-v1` — MULTIMODAL: text, image, or a fused\n * text+image item embedded into a single vector; one item per call.\n * - `cohere.embed-english-v3` / `cohere.embed-multilingual-v3` — text-only,\n * batched natively (chunked at 96 texts per call). `inputType` is required.\n *\n * The SDK call lives behind a protected `invokeModel` seam so tests can\n * subclass and inject canned response bodies without a real AWS request, and\n * the AWS SDK itself is imported lazily (it's Node/server-only).\n */\nexport class BedrockEmbeddingAdapter<\n TModel extends BedrockEmbeddingModel,\n // Same rationale as the text adapters: the base parameterises\n // `TProviderOptions extends object`, and the per-model options interfaces\n // lack implicit index signatures — `Record<string, any>` (not `unknown`)\n // accepts them. Confined to the generic constraint; no value cast.\n TProviderOptions extends Record<string, any> =\n ResolveEmbeddingProviderOptions<TModel>,\n> extends BaseEmbeddingAdapter<\n TModel,\n TProviderOptions,\n BedrockEmbeddingModelProviderOptionsByName,\n BedrockEmbeddingModelInputModalitiesByName\n> {\n readonly name = 'bedrock' as const\n private clientPromise?: Promise<BedrockRuntimeClient>\n private readonly clientConfig: BedrockEmbeddingConfig\n\n constructor(config: BedrockEmbeddingConfig, 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\n * held in a variable (not a string literal) so bundler dep scanners cannot\n * statically discover the AWS SDK and try to pre-bundle it for the browser.\n * Same pattern as the Converse text adapter.\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`, deferring\n * `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 return new BedrockRuntimeClient(\n this.buildClientConfig(resolved, region, this.clientConfig.baseURL),\n )\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`. Bearer\n * auth needs `authSchemePreference` pinned or the SDK still tries SigV4\n * first — same reasoning as the Converse text adapter.\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 seam (overridden in tests so no real AWS call happens)\n // ---------------------------------------------------------------------------\n\n /** Send one InvokeModel call and parse its JSON response body. */\n protected async invokeModel(\n modelId: string,\n body: Record<string, unknown>,\n ): Promise<unknown> {\n const { InvokeModelCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const response = await client.send(\n new InvokeModelCommand({\n modelId,\n contentType: 'application/json',\n accept: 'application/json',\n body: JSON.stringify(body),\n }),\n )\n return JSON.parse(new TextDecoder().decode(response.body))\n }\n\n // ---------------------------------------------------------------------------\n // Public adapter surface\n // ---------------------------------------------------------------------------\n\n async createEmbeddings(\n options: EmbeddingOptions<TProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, logger } = options\n try {\n logger.request(\n `activity=embed provider=${this.name} model=${model} inputs=${options.input.length}`,\n { provider: this.name, model },\n )\n switch (model) {\n case 'amazon.titan-embed-text-v2:0':\n return await this.embedTitanText(options)\n case 'amazon.titan-embed-image-v1':\n return await this.embedTitanImage(options)\n case 'cohere.embed-english-v3':\n case 'cohere.embed-multilingual-v3':\n return await this.embedCohere(options)\n default:\n throw new Error(\n `Unknown Bedrock embedding model \"${model}\". Supported models: ` +\n `${BEDROCK_EMBEDDING_MODELS.join(', ')}.`,\n )\n }\n } catch (error: unknown) {\n logger.errors(`${this.name}.createEmbeddings fatal`, {\n error: toRunErrorPayload(error, `${this.name}.createEmbeddings failed`),\n source: `${this.name}.createEmbeddings`,\n })\n throw error\n }\n }\n\n // ---------------------------------------------------------------------------\n // Per-model request mapping\n // ---------------------------------------------------------------------------\n\n /**\n * `amazon.titan-embed-text-v2:0` — one text per InvokeModel call, fanned\n * out with a concurrency cap; result order matches input order and per-call\n * `inputTextTokenCount`s are summed into usage.\n */\n private async embedTitanText(\n options: EmbeddingOptions<TProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, dimensions } = options\n if (\n dimensions !== undefined &&\n !TITAN_TEXT_DIMENSIONS.includes(dimensions)\n ) {\n throw new Error(\n `${model} supports dimensions 256, 512, or 1024; got ${dimensions}`,\n )\n }\n const normalize: boolean | undefined = options.modelOptions?.normalize\n const texts = requireTextOnlyEmbeddingInput(options.input, this.name, model)\n\n const responses = await mapWithConcurrency(\n texts,\n MAX_CONCURRENT_INVOCATIONS,\n async (text) => {\n // Built incrementally: exactOptionalPropertyTypes is on, and Titan\n // rejects explicit nulls/undefined for absent optional fields.\n const body: Record<string, unknown> = { inputText: text }\n if (dimensions !== undefined) body.dimensions = dimensions\n if (normalize !== undefined) body.normalize = normalize\n return readTitanEmbeddingBody(\n await this.invokeModel(model, body),\n `${this.name} ${model}`,\n )\n },\n )\n\n return this.toTitanResult(model, responses)\n }\n\n /**\n * `amazon.titan-embed-image-v1` (Titan Multimodal) — one item per\n * InvokeModel call. An item may carry text, an image, or both (a fused\n * item embedded into a single vector). Titan accepts at most one image per\n * request and never fetches remote URLs.\n */\n private async embedTitanImage(\n options: EmbeddingOptions<TProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, dimensions } = options\n const outputEmbeddingLength = dimensions ?? TITAN_IMAGE_DEFAULT_DIMENSIONS\n if (!TITAN_IMAGE_DIMENSIONS.includes(outputEmbeddingLength)) {\n throw new Error(\n `${model} supports dimensions 256, 384, or 1024; got ${outputEmbeddingLength}`,\n )\n }\n const items = resolveEmbeddingInput(options.input)\n\n const responses = await mapWithConcurrency(\n items,\n MAX_CONCURRENT_INVOCATIONS,\n async (item, index) => {\n if (item.images.length > 1) {\n throw new Error(\n `${model} accepts at most one image per input item; input item ` +\n `at index ${index} contains ${item.images.length} images. ` +\n `Pass them as separate input items (one vector each).`,\n )\n }\n const body: Record<string, unknown> = {\n embeddingConfig: { outputEmbeddingLength },\n }\n if (item.texts.length > 0) body.inputText = item.texts.join('\\n')\n const image = item.images[0]\n if (image) body.inputImage = toTitanInputImage(image, model)\n return readTitanEmbeddingBody(\n await this.invokeModel(model, body),\n `${this.name} ${model}`,\n )\n },\n )\n\n return this.toTitanResult(model, responses)\n }\n\n /**\n * `cohere.embed-*-v3` — natively batched (chunked at 96 texts per call,\n * order preserved across chunks). `inputType` is required by the Cohere\n * API; output dimensionality is fixed, so `dimensions` is rejected.\n */\n private async embedCohere(\n options: EmbeddingOptions<TProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, dimensions } = options\n if (dimensions !== undefined) {\n throw new Error(\n `${model} does not support the dimensions option; its output size is fixed`,\n )\n }\n const inputType: string | undefined = options.modelOptions?.inputType\n if (inputType === undefined) {\n throw new Error(\n `${model} requires modelOptions.inputType ('search_document' | ` +\n `'search_query' | 'classification' | 'clustering')`,\n )\n }\n const truncate: string | undefined = options.modelOptions?.truncate\n const texts = requireTextOnlyEmbeddingInput(options.input, this.name, model)\n const batches = chunk(texts, COHERE_MAX_BATCH_SIZE)\n\n const responses = await mapWithConcurrency(\n batches,\n MAX_CONCURRENT_INVOCATIONS,\n async (batch) => {\n const body: Record<string, unknown> = {\n texts: batch,\n input_type: inputType,\n }\n if (truncate !== undefined) body.truncate = truncate\n return readCohereEmbeddingBody(\n await this.invokeModel(model, body),\n `${this.name} ${model}`,\n )\n },\n )\n\n return {\n id: this.generateId(),\n model,\n embeddings: responses.flat().map((vector, index) => ({ vector, index })),\n }\n }\n\n /** Assemble an EmbeddingResult from per-item Titan responses. */\n private toTitanResult(\n model: string,\n responses: Array<TitanEmbeddingBody>,\n ): EmbeddingResult {\n let promptTokens = 0\n const embeddings = responses.map((response, index) => {\n promptTokens += response.inputTextTokenCount\n return { vector: response.embedding, index }\n })\n const usage: TokenUsage = {\n promptTokens,\n completionTokens: 0,\n totalTokens: promptTokens,\n }\n return { id: this.generateId(), model, embeddings, usage }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Response-body narrowing (SDK JSON boundary)\n// ---------------------------------------------------------------------------\n\ninterface TitanEmbeddingBody {\n embedding: Array<number>\n /** 0 when the response omits it (e.g. image-only Titan Multimodal calls). */\n inputTextTokenCount: number\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null\n}\n\n/** Narrow a Titan InvokeModel JSON body: `{ embedding, inputTextTokenCount? }`. */\nfunction readTitanEmbeddingBody(\n raw: unknown,\n context: string,\n): TitanEmbeddingBody {\n const embedding =\n isRecord(raw) && Array.isArray(raw.embedding) ? raw.embedding : undefined\n if (!embedding) {\n throw new Error(\n `${context}: response body is missing the \"embedding\" array`,\n )\n }\n const inputTextTokenCount =\n isRecord(raw) && typeof raw.inputTextTokenCount === 'number'\n ? raw.inputTextTokenCount\n : 0\n return { embedding, inputTextTokenCount }\n}\n\n/** Narrow a Cohere InvokeModel JSON body: `{ embeddings: number[][] }` (float). */\nfunction readCohereEmbeddingBody(\n raw: unknown,\n context: string,\n): Array<Array<number>> {\n const embeddings =\n isRecord(raw) && Array.isArray(raw.embeddings) ? raw.embeddings : undefined\n if (!embeddings) {\n throw new Error(\n `${context}: response body is missing the \"embeddings\" array`,\n )\n }\n return embeddings\n}\n\n// ---------------------------------------------------------------------------\n// Input mapping helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Map an ImagePart to Titan's `inputImage` (RAW base64, no data: prefix).\n * Accepts `data` sources as-is and `url` sources ONLY when the value is a\n * `data:` URI; Titan cannot fetch remote http(s) URLs.\n */\nfunction toTitanInputImage(image: ImagePart, model: string): string {\n const source = image.source\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (source.type === 'data') {\n return source.value\n }\n if (source.value.startsWith('data:')) {\n const comma = source.value.indexOf(',')\n if (comma !== -1) {\n return source.value.slice(comma + 1)\n }\n }\n throw new Error(\n `Bedrock Titan does not fetch remote image URLs; pass base64 data ` +\n `(a { type: 'data' } source or a data: URI) for ${model}.`,\n )\n}\n\n/** Split into runs of at most `size`, preserving order. */\nfunction chunk<T>(items: Array<T>, size: number): Array<Array<T>> {\n const chunks: Array<Array<T>> = []\n for (let i = 0; i < items.length; i += size) {\n chunks.push(items.slice(i, i + size))\n }\n return chunks\n}\n\n/**\n * Map `fn` over `items` with at most `limit` calls in flight, returning\n * results in input order (result[i] corresponds to items[i] regardless of\n * completion order). Rejects with the first error.\n */\nasync function mapWithConcurrency<T, TResult>(\n items: ReadonlyArray<T>,\n limit: number,\n fn: (item: T, index: number) => Promise<TResult>,\n): Promise<Array<TResult>> {\n const results = new Array<TResult>(items.length)\n let next = 0\n const worker = async (): Promise<void> => {\n while (next < items.length) {\n const index = next++\n const item = items[index]\n if (item === undefined) continue // unreachable: index < length\n results[index] = await fn(item, index)\n }\n }\n const workerCount = Math.max(1, Math.min(limit, items.length))\n await Promise.all(Array.from({ length: workerCount }, () => worker()))\n return results\n}\n\n// ---------------------------------------------------------------------------\n// Factories\n// ---------------------------------------------------------------------------\n\n/**\n * Creates a Bedrock embedding adapter with an explicit API key (bearer).\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'amazon.titan-embed-text-v2:0')\n * @param apiKey - Your Bedrock API key\n * @param config - Optional additional configuration (region, baseURL, ...)\n * @returns Configured Bedrock embedding adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = createBedrockEmbedding(\n * 'amazon.titan-embed-text-v2:0',\n * 'bedrock-api-key',\n * );\n *\n * const result = await embed({\n * adapter,\n * input: 'a red guitar',\n * });\n * ```\n */\nexport function createBedrockEmbedding<TModel extends BedrockEmbeddingModel>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockEmbeddingConfig, 'apiKey'>,\n): BedrockEmbeddingAdapter<TModel> {\n // Explicit apiKey is authoritative — spread config first so it can't override.\n return new BedrockEmbeddingAdapter({ ...config, apiKey }, model)\n}\n\n/**\n * Creates a Bedrock embedding adapter using the ambient auth cascade:\n * `config.apiKey` → `BEDROCK_API_KEY` → `AWS_BEARER_TOKEN_BEDROCK` → SigV4\n * (AWS credential provider chain). Auth resolves lazily on the first\n * request, so `auth: 'sigv4'` never requires an API key.\n *\n * @param model - The model name (e.g., 'cohere.embed-english-v3')\n * @param config - Optional configuration (region, auth, baseURL, ...)\n * @returns Configured Bedrock embedding adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = bedrockEmbedding('cohere.embed-english-v3');\n *\n * const result = await embed({\n * adapter,\n * input: ['a red guitar', 'a blue drum kit'],\n * modelOptions: { inputType: 'search_document' },\n * });\n *\n * console.log(result.embeddings[0].vector)\n * ```\n */\nexport function bedrockEmbedding<TModel extends BedrockEmbeddingModel>(\n model: TModel,\n config?: BedrockEmbeddingConfig,\n): BedrockEmbeddingAdapter<TModel> {\n return new BedrockEmbeddingAdapter(config ?? {}, model)\n}\n"],"mappings":";;;;;;;AAyCA,IAAM,6BAA6B;;AAGnC,IAAM,wBAA+C;CAAC;CAAK;CAAK;AAAI;;AAGpE,IAAM,yBAAgD;CAAC;CAAK;CAAK;AAAI;AACrE,IAAM,iCAAiC;;AAGvC,IAAM,wBAAwB;;;;;;;;;;;;;;;;;;;;AAqB9B,IAAa,0BAAb,cAQU,qBAKR;CACA,OAAgB;CAChB;CACA;CAEA,YAAY,QAAgC,OAAe;EACzD,MAAM,OAAO,CAAC,CAAC;EAIf,KAAK,eAAe;CACtB;;;;;;;CAQA,uBAAiE;EAE/D,OAAO;;GAA0B;;CACnC;;;;;CAMA,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,OAAO,IAAI,qBACT,KAAK,kBAAkB,UAAU,QAAQ,KAAK,aAAa,OAAO,CACpE;EACF,EAAA,CAAG,CAAC,CAAC,OAAO,UAAmB;GAG7B,KAAK,gBAAgB,KAAA;GACrB,MAAM;EACR,CAAC;EAEH,OAAO,KAAK;CACd;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AM,QAAQ,IAAI,MAAM,KAAK,EAAE,QAAQ,YAAY,SAAS,OAAO,CAAC,CAAC;CACrE,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,uBACd,OACA,QACA,QACiC;CAEjC,OAAO,IAAI,wBAAwB;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AACjE;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,iBACd,OACA,QACiC;CACjC,OAAO,IAAI,wBAAwB,UAAU,CAAC,GAAG,KAAK;AACxD"}
|
|
1
|
+
{"version":3,"file":"embedding.js","names":[],"sources":["../../../src/adapters/embedding.ts"],"sourcesContent":["import { BaseEmbeddingAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport {\n isFileSource,\n requireTextOnlyEmbeddingInput,\n resolveEmbeddingInput,\n unsupportedFileSourceError,\n} from '@tanstack/ai'\nimport { importOptionalPeer, resolveBedrockAuth } from '../utils/auth'\nimport { BEDROCK_EMBEDDING_MODELS } from '../model-meta'\nimport type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime'\nimport type {\n BedrockRuntimeClient,\n BedrockRuntimeClientConfig,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type {\n EmbeddingOptions,\n EmbeddingResult,\n ImagePart,\n TokenUsage,\n} from '@tanstack/ai'\nimport type { ResolvedBedrockAuth } from '../utils/auth'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type {\n BedrockEmbeddingModel,\n BedrockEmbeddingModelInputModalitiesByName,\n BedrockEmbeddingModelProviderOptionsByName,\n ResolveEmbeddingProviderOptions,\n} from '../model-meta'\n\n/**\n * Config for the Bedrock embedding adapter — the same auth surface as the\n * other Bedrock adapters (apiKey → env → SigV4 via `resolveBedrockAuth`),\n * minus the OpenAI-compat client options that don't apply to `InvokeModel`.\n */\nexport interface BedrockEmbeddingConfig extends Pick<\n BedrockClientConfig,\n 'apiKey' | 'region' | 'auth' | 'baseURL'\n> {}\n\n/** InvokeModel calls issued concurrently during a per-item fan-out. */\nconst MAX_CONCURRENT_INVOCATIONS = 5\n\n/** Valid `dimensions` for `amazon.titan-embed-text-v2:0`. */\nconst TITAN_TEXT_DIMENSIONS: ReadonlyArray<number> = [256, 512, 1024]\n\n/** Valid `dimensions` (outputEmbeddingLength) for `amazon.titan-embed-image-v1`. */\nconst TITAN_IMAGE_DIMENSIONS: ReadonlyArray<number> = [256, 384, 1024]\nconst TITAN_IMAGE_DEFAULT_DIMENSIONS = 1024\n\n/** Cohere embed accepts at most 96 texts per InvokeModel call. */\nconst COHERE_MAX_BATCH_SIZE = 96\n\n/**\n * Bedrock Embedding Adapter\n *\n * Tree-shakeable adapter for embeddings served through Bedrock's native\n * `InvokeModel` API (embedding models have no Converse surface). Each model\n * family has its own JSON body dialect:\n *\n * - `amazon.titan-embed-text-v2:0` — text-only, ONE text per call; the batch\n * is fanned out with a small concurrency cap and per-call\n * `inputTextTokenCount`s are summed into usage.\n * - `amazon.titan-embed-image-v1` — MULTIMODAL: text, image, or a fused\n * text+image item embedded into a single vector; one item per call.\n * - `cohere.embed-english-v3` / `cohere.embed-multilingual-v3` — text-only,\n * batched natively (chunked at 96 texts per call). `inputType` is required.\n *\n * The SDK call lives behind a protected `invokeModel` seam so tests can\n * subclass and inject canned response bodies without a real AWS request, and\n * the AWS SDK itself is imported lazily (it's Node/server-only).\n */\nexport class BedrockEmbeddingAdapter<\n TModel extends BedrockEmbeddingModel,\n // Same rationale as the text adapters: the base parameterises\n // `TProviderOptions extends object`, and the per-model options interfaces\n // lack implicit index signatures — `Record<string, any>` (not `unknown`)\n // accepts them. Confined to the generic constraint; no value cast.\n TProviderOptions extends Record<string, any> =\n ResolveEmbeddingProviderOptions<TModel>,\n> extends BaseEmbeddingAdapter<\n TModel,\n TProviderOptions,\n BedrockEmbeddingModelProviderOptionsByName,\n BedrockEmbeddingModelInputModalitiesByName\n> {\n readonly name = 'bedrock' as const\n private clientPromise?: Promise<BedrockRuntimeClient>\n private readonly clientConfig: BedrockEmbeddingConfig\n\n constructor(config: BedrockEmbeddingConfig, 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\n * held in a variable (not a string literal) so bundler dep scanners cannot\n * statically discover the AWS SDK and try to pre-bundle it for the browser.\n * Same pattern as the Converse text adapter.\n */\n protected importBedrockRuntime(): Promise<typeof BedrockRuntime> {\n const mod = '@aws-sdk/client-bedrock-runtime'\n return importOptionalPeer<typeof BedrockRuntime>(mod)\n }\n\n /**\n * Lazily construct the `BedrockRuntimeClient`, deferring\n * `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 return new BedrockRuntimeClient(\n this.buildClientConfig(resolved, region, this.clientConfig.baseURL),\n )\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`. Bearer\n * auth needs `authSchemePreference` pinned or the SDK still tries SigV4\n * first — same reasoning as the Converse text adapter.\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 seam (overridden in tests so no real AWS call happens)\n // ---------------------------------------------------------------------------\n\n /** Send one InvokeModel call and parse its JSON response body. */\n protected async invokeModel(\n modelId: string,\n body: Record<string, unknown>,\n abortSignal?: AbortSignal,\n ): Promise<unknown> {\n const { InvokeModelCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const response = await client.send(\n new InvokeModelCommand({\n modelId,\n contentType: 'application/json',\n accept: 'application/json',\n body: JSON.stringify(body),\n }),\n abortSignal ? { abortSignal } : {},\n )\n return JSON.parse(new TextDecoder().decode(response.body))\n }\n\n // ---------------------------------------------------------------------------\n // Public adapter surface\n // ---------------------------------------------------------------------------\n\n async createEmbeddings(\n options: EmbeddingOptions<TProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, logger } = options\n try {\n logger.request(\n `activity=embed provider=${this.name} model=${model} inputs=${options.input.length}`,\n { provider: this.name, model },\n )\n switch (model) {\n case 'amazon.titan-embed-text-v2:0':\n return await this.embedTitanText(options)\n case 'amazon.titan-embed-image-v1':\n return await this.embedTitanImage(options)\n case 'cohere.embed-english-v3':\n case 'cohere.embed-multilingual-v3':\n return await this.embedCohere(options)\n default:\n throw new Error(\n `Unknown Bedrock embedding model \"${model}\". Supported models: ` +\n `${BEDROCK_EMBEDDING_MODELS.join(', ')}.`,\n )\n }\n } catch (error: unknown) {\n logger.errors(`${this.name}.createEmbeddings fatal`, {\n error: toRunErrorPayload(error, `${this.name}.createEmbeddings failed`),\n source: `${this.name}.createEmbeddings`,\n })\n throw error\n }\n }\n\n // ---------------------------------------------------------------------------\n // Per-model request mapping\n // ---------------------------------------------------------------------------\n\n /**\n * `amazon.titan-embed-text-v2:0` — one text per InvokeModel call, fanned\n * out with a concurrency cap; result order matches input order and per-call\n * `inputTextTokenCount`s are summed into usage.\n */\n private async embedTitanText(\n options: EmbeddingOptions<TProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, dimensions } = options\n if (\n dimensions !== undefined &&\n !TITAN_TEXT_DIMENSIONS.includes(dimensions)\n ) {\n throw new Error(\n `${model} supports dimensions 256, 512, or 1024; got ${dimensions}`,\n )\n }\n const normalize: boolean | undefined = options.modelOptions?.normalize\n const texts = requireTextOnlyEmbeddingInput(options.input, this.name, model)\n\n const responses = await mapWithConcurrency(\n texts,\n MAX_CONCURRENT_INVOCATIONS,\n async (text) => {\n // Built incrementally: exactOptionalPropertyTypes is on, and Titan\n // rejects explicit nulls/undefined for absent optional fields.\n const body: Record<string, unknown> = { inputText: text }\n if (dimensions !== undefined) body.dimensions = dimensions\n if (normalize !== undefined) body.normalize = normalize\n return readTitanEmbeddingBody(\n await this.invokeModel(model, body, options.abortSignal),\n `${this.name} ${model}`,\n )\n },\n )\n\n return this.toTitanResult(model, responses)\n }\n\n /**\n * `amazon.titan-embed-image-v1` (Titan Multimodal) — one item per\n * InvokeModel call. An item may carry text, an image, or both (a fused\n * item embedded into a single vector). Titan accepts at most one image per\n * request and never fetches remote URLs.\n */\n private async embedTitanImage(\n options: EmbeddingOptions<TProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, dimensions } = options\n const outputEmbeddingLength = dimensions ?? TITAN_IMAGE_DEFAULT_DIMENSIONS\n if (!TITAN_IMAGE_DIMENSIONS.includes(outputEmbeddingLength)) {\n throw new Error(\n `${model} supports dimensions 256, 384, or 1024; got ${outputEmbeddingLength}`,\n )\n }\n const items = resolveEmbeddingInput(options.input)\n\n const responses = await mapWithConcurrency(\n items,\n MAX_CONCURRENT_INVOCATIONS,\n async (item, index) => {\n if (item.images.length > 1) {\n throw new Error(\n `${model} accepts at most one image per input item; input item ` +\n `at index ${index} contains ${item.images.length} images. ` +\n `Pass them as separate input items (one vector each).`,\n )\n }\n const body: Record<string, unknown> = {\n embeddingConfig: { outputEmbeddingLength },\n }\n if (item.texts.length > 0) body.inputText = item.texts.join('\\n')\n const image = item.images[0]\n if (image) body.inputImage = toTitanInputImage(image, model)\n return readTitanEmbeddingBody(\n await this.invokeModel(model, body, options.abortSignal),\n `${this.name} ${model}`,\n )\n },\n )\n\n return this.toTitanResult(model, responses)\n }\n\n /**\n * `cohere.embed-*-v3` — natively batched (chunked at 96 texts per call,\n * order preserved across chunks). `inputType` is required by the Cohere\n * API; output dimensionality is fixed, so `dimensions` is rejected.\n */\n private async embedCohere(\n options: EmbeddingOptions<TProviderOptions>,\n ): Promise<EmbeddingResult> {\n const { model, dimensions } = options\n if (dimensions !== undefined) {\n throw new Error(\n `${model} does not support the dimensions option; its output size is fixed`,\n )\n }\n const inputType: string | undefined = options.modelOptions?.inputType\n if (inputType === undefined) {\n throw new Error(\n `${model} requires modelOptions.inputType ('search_document' | ` +\n `'search_query' | 'classification' | 'clustering')`,\n )\n }\n const truncate: string | undefined = options.modelOptions?.truncate\n const texts = requireTextOnlyEmbeddingInput(options.input, this.name, model)\n const batches = chunk(texts, COHERE_MAX_BATCH_SIZE)\n\n const responses = await mapWithConcurrency(\n batches,\n MAX_CONCURRENT_INVOCATIONS,\n async (batch) => {\n const body: Record<string, unknown> = {\n texts: batch,\n input_type: inputType,\n }\n if (truncate !== undefined) body.truncate = truncate\n return readCohereEmbeddingBody(\n await this.invokeModel(model, body, options.abortSignal),\n `${this.name} ${model}`,\n )\n },\n )\n\n return {\n id: this.generateId(),\n model,\n embeddings: responses.flat().map((vector, index) => ({ vector, index })),\n }\n }\n\n /** Assemble an EmbeddingResult from per-item Titan responses. */\n private toTitanResult(\n model: string,\n responses: Array<TitanEmbeddingBody>,\n ): EmbeddingResult {\n let promptTokens = 0\n const embeddings = responses.map((response, index) => {\n promptTokens += response.inputTextTokenCount\n return { vector: response.embedding, index }\n })\n const usage: TokenUsage = {\n promptTokens,\n completionTokens: 0,\n totalTokens: promptTokens,\n }\n return { id: this.generateId(), model, embeddings, usage }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Response-body narrowing (SDK JSON boundary)\n// ---------------------------------------------------------------------------\n\ninterface TitanEmbeddingBody {\n embedding: Array<number>\n /** 0 when the response omits it (e.g. image-only Titan Multimodal calls). */\n inputTextTokenCount: number\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null\n}\n\n/** Narrow a Titan InvokeModel JSON body: `{ embedding, inputTextTokenCount? }`. */\nfunction readTitanEmbeddingBody(\n raw: unknown,\n context: string,\n): TitanEmbeddingBody {\n const embedding =\n isRecord(raw) && Array.isArray(raw.embedding) ? raw.embedding : undefined\n if (!embedding) {\n throw new Error(\n `${context}: response body is missing the \"embedding\" array`,\n )\n }\n const inputTextTokenCount =\n isRecord(raw) && typeof raw.inputTextTokenCount === 'number'\n ? raw.inputTextTokenCount\n : 0\n return { embedding, inputTextTokenCount }\n}\n\n/** Narrow a Cohere InvokeModel JSON body: `{ embeddings: number[][] }` (float). */\nfunction readCohereEmbeddingBody(\n raw: unknown,\n context: string,\n): Array<Array<number>> {\n const embeddings =\n isRecord(raw) && Array.isArray(raw.embeddings) ? raw.embeddings : undefined\n if (!embeddings) {\n throw new Error(\n `${context}: response body is missing the \"embeddings\" array`,\n )\n }\n return embeddings\n}\n\n// ---------------------------------------------------------------------------\n// Input mapping helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Map an ImagePart to Titan's `inputImage` (RAW base64, no data: prefix).\n * Accepts `data` sources as-is and `url` sources ONLY when the value is a\n * `data:` URI; Titan cannot fetch remote http(s) URLs.\n */\nfunction toTitanInputImage(image: ImagePart, model: string): string {\n const source = image.source\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (source.type === 'data') {\n return source.value\n }\n if (source.value.startsWith('data:')) {\n const comma = source.value.indexOf(',')\n if (comma !== -1) {\n return source.value.slice(comma + 1)\n }\n }\n throw new Error(\n `Bedrock Titan does not fetch remote image URLs; pass base64 data ` +\n `(a { type: 'data' } source or a data: URI) for ${model}.`,\n )\n}\n\n/** Split into runs of at most `size`, preserving order. */\nfunction chunk<T>(items: Array<T>, size: number): Array<Array<T>> {\n const chunks: Array<Array<T>> = []\n for (let i = 0; i < items.length; i += size) {\n chunks.push(items.slice(i, i + size))\n }\n return chunks\n}\n\n/**\n * Map `fn` over `items` with at most `limit` calls in flight, returning\n * results in input order (result[i] corresponds to items[i] regardless of\n * completion order). Rejects with the first error.\n */\nasync function mapWithConcurrency<T, TResult>(\n items: ReadonlyArray<T>,\n limit: number,\n fn: (item: T, index: number) => Promise<TResult>,\n): Promise<Array<TResult>> {\n const results = new Array<TResult>(items.length)\n let next = 0\n const worker = async (): Promise<void> => {\n while (next < items.length) {\n const index = next++\n const item = items[index]\n if (item === undefined) continue // unreachable: index < length\n results[index] = await fn(item, index)\n }\n }\n const workerCount = Math.max(1, Math.min(limit, items.length))\n await Promise.all(Array.from({ length: workerCount }, () => worker()))\n return results\n}\n\n// ---------------------------------------------------------------------------\n// Factories\n// ---------------------------------------------------------------------------\n\n/**\n * Creates a Bedrock embedding adapter with an explicit API key (bearer).\n * Type resolution happens here at the call site.\n *\n * @param model - The model name (e.g., 'amazon.titan-embed-text-v2:0')\n * @param apiKey - Your Bedrock API key\n * @param config - Optional additional configuration (region, baseURL, ...)\n * @returns Configured Bedrock embedding adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = createBedrockEmbedding(\n * 'amazon.titan-embed-text-v2:0',\n * 'bedrock-api-key',\n * );\n *\n * const result = await embed({\n * adapter,\n * input: 'a red guitar',\n * });\n * ```\n */\nexport function createBedrockEmbedding<TModel extends BedrockEmbeddingModel>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockEmbeddingConfig, 'apiKey'>,\n): BedrockEmbeddingAdapter<TModel> {\n // Explicit apiKey is authoritative — spread config first so it can't override.\n return new BedrockEmbeddingAdapter({ ...config, apiKey }, model)\n}\n\n/**\n * Creates a Bedrock embedding adapter using the ambient auth cascade:\n * `config.apiKey` → `BEDROCK_API_KEY` → `AWS_BEARER_TOKEN_BEDROCK` → SigV4\n * (AWS credential provider chain). Auth resolves lazily on the first\n * request, so `auth: 'sigv4'` never requires an API key.\n *\n * @param model - The model name (e.g., 'cohere.embed-english-v3')\n * @param config - Optional configuration (region, auth, baseURL, ...)\n * @returns Configured Bedrock embedding adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = bedrockEmbedding('cohere.embed-english-v3');\n *\n * const result = await embed({\n * adapter,\n * input: ['a red guitar', 'a blue drum kit'],\n * modelOptions: { inputType: 'search_document' },\n * });\n *\n * console.log(result.embeddings[0].vector)\n * ```\n */\nexport function bedrockEmbedding<TModel extends BedrockEmbeddingModel>(\n model: TModel,\n config?: BedrockEmbeddingConfig,\n): BedrockEmbeddingAdapter<TModel> {\n return new BedrockEmbeddingAdapter(config ?? {}, model)\n}\n"],"mappings":";;;;;;;AAyCA,IAAM,6BAA6B;;AAGnC,IAAM,wBAA+C;CAAC;CAAK;CAAK;AAAI;;AAGpE,IAAM,yBAAgD;CAAC;CAAK;CAAK;AAAI;AACrE,IAAM,iCAAiC;;AAGvC,IAAM,wBAAwB;;;;;;;;;;;;;;;;;;;;AAqB9B,IAAa,0BAAb,cAQU,qBAKR;CACA,OAAgB;CAChB;CACA;CAEA,YAAY,QAAgC,OAAe;EACzD,MAAM,OAAO,CAAC,CAAC;EAIf,KAAK,eAAe;CACtB;;;;;;;CAQA,uBAAiE;EAE/D,OAAO,mBAA0C,iCAAG;CACtD;;;;;CAMA,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,OAAO,IAAI,qBACT,KAAK,kBAAkB,UAAU,QAAQ,KAAK,aAAa,OAAO,CACpE;EACF,EAAA,CAAG,CAAC,CAAC,OAAO,UAAmB;GAG7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S,KAAA,GAAW;GACxB,QAAQ,SAAS,MAAM,GAAG,MAAM,KAAK;EACvC;CACF;CACA,MAAM,cAAc,KAAK,IAAI,GAAG,KAAK,IAAI,OAAO,MAAM,MAAM,CAAC;CAC7D,MAAM,QAAQ,IAAI,MAAM,KAAK,EAAE,QAAQ,YAAY,SAAS,OAAO,CAAC,CAAC;CACrE,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,uBACd,OACA,QACA,QACiC;CAEjC,OAAO,IAAI,wBAAwB;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AACjE;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,iBACd,OACA,QACiC;CACjC,OAAO,IAAI,wBAAwB,UAAU,CAAC,GAAG,KAAK;AACxD"}
|
|
@@ -12,7 +12,7 @@ type ResolveReasoning<TModel extends string> = TModel extends keyof BedrockModel
|
|
|
12
12
|
type ResolveToolCapabilities<TModel extends string> = TModel extends keyof BedrockChatModelToolCapabilitiesByName ? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]> : readonly [];
|
|
13
13
|
/**
|
|
14
14
|
* Bedrock Responses adapter. Drives mantle's OpenAI-compatible `/responses`
|
|
15
|
-
* endpoint via the OpenAI
|
|
15
|
+
* endpoint via the OpenAI-compatible client (`client.responses.create`) — the same base
|
|
16
16
|
* class ai-openai's `openaiText` uses. Responses is mantle-only, so the
|
|
17
17
|
* constructor forces the mantle baseURL.
|
|
18
18
|
*/
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { isBedrockOpenAIContextOverflow, withBedrockDefaults } from "../utils/client.js";
|
|
2
2
|
import { BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING } from "../model-meta.js";
|
|
3
|
-
import
|
|
4
|
-
import { OpenAIBaseResponsesTextAdapter } from "@tanstack/openai-base";
|
|
3
|
+
import { OpenAIBaseResponsesTextAdapter, createOpenAIClient } from "@tanstack/openai-base";
|
|
5
4
|
//#region src/adapters/responses-text.ts
|
|
6
5
|
/**
|
|
7
6
|
* Bedrock Responses adapter. Drives mantle's OpenAI-compatible `/responses`
|
|
8
|
-
* endpoint via the OpenAI
|
|
7
|
+
* endpoint via the OpenAI-compatible client (`client.responses.create`) — the same base
|
|
9
8
|
* class ai-openai's `openaiText` uses. Responses is mantle-only, so the
|
|
10
9
|
* constructor forces the mantle baseURL.
|
|
11
10
|
*/
|
|
@@ -16,7 +15,7 @@ var BedrockResponsesTextAdapter = class extends OpenAIBaseResponsesTextAdapter {
|
|
|
16
15
|
inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model];
|
|
17
16
|
constructor(config, model) {
|
|
18
17
|
const options = withBedrockDefaults(config, "mantle", model);
|
|
19
|
-
super(model, "bedrock-responses",
|
|
18
|
+
super(model, "bedrock-responses", createOpenAIClient(options), {
|
|
20
19
|
...config,
|
|
21
20
|
fetch: options.fetch
|
|
22
21
|
});
|