@tanstack/ai-bedrock 0.3.15 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/adapters/converse-text.d.ts +2 -2
- package/dist/esm/adapters/converse-text.js +3 -1
- package/dist/esm/adapters/converse-text.js.map +1 -1
- package/dist/esm/adapters/responses-text.d.ts +2 -2
- package/dist/esm/adapters/responses-text.js +1 -1
- package/dist/esm/adapters/responses-text.js.map +1 -1
- package/dist/esm/adapters/text.d.ts +2 -2
- package/dist/esm/adapters/text.js +1 -1
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/converse/message-converter.d.ts +2 -1
- package/dist/esm/converse/message-converter.js +7 -2
- package/dist/esm/converse/message-converter.js.map +1 -1
- package/dist/esm/converse/tool-converter.d.ts +3 -0
- package/dist/esm/converse/tool-converter.js +2 -2
- package/dist/esm/converse/tool-converter.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/message-types.d.ts +25 -2
- package/package.json +4 -3
- package/src/adapters/converse-text.ts +16 -2
- package/src/adapters/responses-text.ts +4 -1
- package/src/adapters/text.ts +4 -1
- package/src/converse/message-converter.ts +17 -4
- package/src/converse/tool-converter.ts +12 -6
- package/src/index.ts +3 -0
- package/src/message-types.ts +30 -3
|
@@ -3,7 +3,7 @@ import { ResolvedBedrockAuth } from '../utils/auth.js';
|
|
|
3
3
|
import { BedrockRuntimeClient, BedrockRuntimeClientConfig, ConverseCommandInput, ConverseCommandOutput, ConverseStreamCommandInput, ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime';
|
|
4
4
|
import { Modality, AdapterYieldChunk, TextOptions } from '@tanstack/ai';
|
|
5
5
|
import { BedrockClientConfig } from '../utils/client.js';
|
|
6
|
-
import { BedrockMessageMetadataByModality } from '../message-types.js';
|
|
6
|
+
import { BedrockMessageMetadataByModality, BedrockSystemPromptMetadata } from '../message-types.js';
|
|
7
7
|
import { BedrockConverseModels, ResolveConverseProviderOptions, ResolveInputModalities } from '../model-meta.js';
|
|
8
8
|
import type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime';
|
|
9
9
|
/** Config for the Converse adapter — same client config as the chat adapter. */
|
|
@@ -23,7 +23,7 @@ export interface BedrockConverseConfig extends BedrockClientConfig {
|
|
|
23
23
|
* so tests can subclass and inject canned Converse SDK shapes without a real
|
|
24
24
|
* AWS request.
|
|
25
25
|
*/
|
|
26
|
-
export declare class BedrockConverseTextAdapter<TModel extends BedrockConverseModels, TProviderOptions extends Record<string, any> = ResolveConverseProviderOptions<TModel>, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>> extends BaseTextAdapter<TModel, TProviderOptions, TInputModalities, BedrockMessageMetadataByModality> {
|
|
26
|
+
export declare class BedrockConverseTextAdapter<TModel extends BedrockConverseModels, TProviderOptions extends Record<string, any> = ResolveConverseProviderOptions<TModel>, TInputModalities extends ReadonlyArray<Modality> = ResolveInputModalities<TModel>> extends BaseTextAdapter<TModel, TProviderOptions, TInputModalities, BedrockMessageMetadataByModality, ReadonlyArray<string>, unknown, BedrockSystemPromptMetadata> {
|
|
27
27
|
readonly kind: "text";
|
|
28
28
|
readonly name: "bedrock-converse";
|
|
29
29
|
private clientPromise?;
|
|
@@ -404,10 +404,12 @@ function convertTools(tools) {
|
|
|
404
404
|
properties: {},
|
|
405
405
|
required: []
|
|
406
406
|
};
|
|
407
|
+
const { cachePoint } = tool.metadata ?? {};
|
|
407
408
|
return {
|
|
408
409
|
name: tool.name,
|
|
409
410
|
description: tool.description,
|
|
410
|
-
inputSchema
|
|
411
|
+
inputSchema,
|
|
412
|
+
...cachePoint && { cachePoint }
|
|
411
413
|
};
|
|
412
414
|
});
|
|
413
415
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"converse-text.js","names":[],"sources":["../../../src/adapters/converse-text.ts"],"sourcesContent":["import { EventType, convertSchemaToJsonSchema } from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport { resolveBedrockAuth } from '../utils/auth'\nimport { toConverseMessages } from '../converse/message-converter'\nimport { toToolConfig } from '../converse/tool-converter'\nimport {\n processConverseStream,\n throwIfConverseStreamError,\n} from '../converse/stream-processor'\nimport { buildConverseUsage } from '../converse/usage'\nimport {\n STRUCTURED_TOOL_NAME,\n buildStructuredToolConfig,\n} from '../converse/structured-output'\nimport type { ResolvedBedrockAuth } from '../utils/auth'\nimport type { ConverseToolInput } from '../converse/tool-converter'\nimport type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime'\nimport type {\n BedrockRuntimeClient,\n BedrockRuntimeClientConfig,\n ContentBlock,\n ConverseCommandInput,\n ConverseCommandOutput,\n ConverseStreamCommandInput,\n ConverseStreamOutput,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type {\n JSONSchema,\n Modality,\n AdapterYieldChunk,\n TextOptions,\n TokenUsage,\n Tool,\n} from '@tanstack/ai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type { BedrockMessageMetadataByModality } from '../message-types'\nimport type {\n BedrockConverseModels,\n ResolveConverseProviderOptions,\n ResolveInputModalities,\n} from '../model-meta'\n\n/** Config for the Converse adapter — same client config as the chat adapter. */\nexport interface BedrockConverseConfig extends BedrockClientConfig {}\n\n/**\n * Bedrock Converse text adapter. Wires the Converse translation modules (message\n * converter, tool converter, stream processor, structured-output forced-tool\n * builder) onto `@tanstack/ai`'s `BaseTextAdapter` and the\n * `@aws-sdk/client-bedrock-runtime` `BedrockRuntimeClient`.\n *\n * The success-path AG-UI lifecycle (`RUN_STARTED`..`RUN_FINISHED`) is owned by\n * `processConverseStream`; this adapter only owns the catch/`RUN_ERROR` path,\n * mirroring openai-base's `chatStream`.\n *\n * The actual SDK calls live behind two protected seams (`sendStream` / `send`)\n * so tests can subclass and inject canned Converse SDK shapes without a real\n * AWS request.\n */\nexport class BedrockConverseTextAdapter<\n TModel extends BedrockConverseModels,\n // Constraint mirrors the chat adapter (text.ts): the base parameterises\n // `TProviderOptions extends Record<string, any>`, and our default\n // `ResolveConverseProviderOptions<TModel>` resolves to an interface lacking an\n // implicit index signature — which `Record<string, unknown>` would reject but\n // `Record<string, any>` accepts. Confined to the generic constraint (the\n // established adapter pattern) — no value `as` cast is introduced.\n TProviderOptions extends Record<string, any> =\n ResolveConverseProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n BedrockMessageMetadataByModality\n> {\n override readonly kind = 'text' as const\n override readonly name = 'bedrock-converse' as const\n private clientPromise?: Promise<BedrockRuntimeClient>\n private readonly clientConfig: BedrockConverseConfig\n\n constructor(config: BedrockConverseConfig, model: TModel) {\n super({}, model)\n // Defer client construction and auth resolution: the AWS SDK is Node/\n // server-only, so we must not pull it into the static graph here. The\n // client (and its dynamic import) is built lazily on first SDK call.\n this.clientConfig = config\n }\n\n /**\n * Dynamically import `@aws-sdk/client-bedrock-runtime`. The specifier is held\n * in a variable (not a string literal) so bundler dep scanners (e.g. Vite/\n * esbuild optimizeDeps) cannot statically discover the AWS SDK and try to\n * pre-bundle it for the browser — it would fail on the SDK's Node-only\n * `fromTokenFile` export chain. The SDK is Node/server-only and is only\n * reached on a real request. `typeof import(...)` is a type-only reference\n * (erased at emit) so the imported members keep full typing.\n */\n protected importBedrockRuntime(): Promise<typeof BedrockRuntime> {\n const mod = '@aws-sdk/client-bedrock-runtime'\n return import(/* @vite-ignore */ mod) as Promise<typeof BedrockRuntime>\n }\n\n /**\n * Lazily construct the `BedrockRuntimeClient`. The dynamic import keeps\n * `@aws-sdk/client-bedrock-runtime` out of the static/browser graph and\n * defers `resolveBedrockAuth` until a real request is made.\n */\n protected async getClient(): Promise<BedrockRuntimeClient> {\n if (!this.clientPromise) {\n this.clientPromise = (async () => {\n const { BedrockRuntimeClient } = await this.importBedrockRuntime()\n const region = this.clientConfig.region ?? 'us-east-1'\n const resolved = resolveBedrockAuth(\n {\n apiKey: this.clientConfig.apiKey,\n region,\n auth: this.clientConfig.auth,\n },\n 'runtime',\n )\n const client = new BedrockRuntimeClient(\n this.buildClientConfig(resolved, region, this.clientConfig.baseURL),\n )\n const defaultHeaders = this.clientConfig.defaultHeaders\n if (defaultHeaders) {\n // `build` runs before SigV4 signing, so gateway headers are signed\n // along with the rest of the request.\n client.middlewareStack.add(\n (next) => (args) => {\n const request = args.request\n if (\n typeof request === 'object' &&\n request !== null &&\n 'headers' in request &&\n typeof request.headers === 'object' &&\n request.headers !== null\n ) {\n Object.assign(request.headers, defaultHeaders)\n }\n return next(args)\n },\n { step: 'build', name: 'tanstackDefaultHeaders' },\n )\n }\n return client\n })().catch((error: unknown) => {\n // Don't cache a rejected promise — clear it so a later call can retry\n // (e.g. after a transient import failure or fixed auth config).\n this.clientPromise = undefined\n throw error\n })\n }\n return this.clientPromise\n }\n\n /**\n * Map resolved auth + endpoint to a `BedrockRuntimeClientConfig`.\n *\n * Recent `@aws-sdk/client-bedrock-runtime` exposes a first-class `token`\n * config field for Bedrock API-key bearer auth. But the client's default\n * auth-scheme order is SigV4 first, then bearer — so passing `token` alone is\n * not enough: the SDK still resolves SigV4 and throws \"Could not load\n * credentials from any providers\". Pinning `authSchemePreference` to the\n * bearer scheme makes the API key actually get used. SigV4 uses the AWS\n * credential provider chain and the default scheme order.\n */\n protected buildClientConfig(\n resolved: ResolvedBedrockAuth,\n region: string,\n endpoint: string | undefined,\n ): BedrockRuntimeClientConfig {\n if (resolved.kind === 'bearer') {\n return {\n region,\n token: { token: resolved.token },\n authSchemePreference: ['httpBearerAuth'],\n ...(endpoint ? { endpoint } : {}),\n }\n }\n return {\n region: resolved.region,\n credentials: resolved.credentials,\n ...(endpoint ? { endpoint } : {}),\n }\n }\n\n // ---------------------------------------------------------------------------\n // SDK seams (overridden in tests so no real AWS call happens)\n // ---------------------------------------------------------------------------\n\n protected async sendStream(\n input: ConverseStreamCommandInput,\n ): Promise<AsyncIterable<ConverseStreamOutput>> {\n const { ConverseStreamCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const res = await client.send(new ConverseStreamCommand(input))\n if (!res.stream) {\n throw new Error('Bedrock Converse: empty stream response')\n }\n return res.stream\n }\n\n protected async send(\n input: ConverseCommandInput,\n ): Promise<ConverseCommandOutput> {\n const { ConverseCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n return client.send(new ConverseCommand(input))\n }\n\n // ---------------------------------------------------------------------------\n // Public adapter surface\n // ---------------------------------------------------------------------------\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n try {\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const input = this.buildInput(options)\n const stream = await this.sendStream(input)\n yield* processConverseStream(stream, () => this.generateId(), {\n threadId: options.threadId,\n parentRunId: options.parentRunId,\n model: options.model,\n })\n } catch (error: unknown) {\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes` (AG-UI's `RunErrorEvent.code` is optional).\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Structured output via the forced-tool strategy. Converse has no native\n * json_schema response_format, so we force a single tool whose input schema\n * is the requested output schema and read the model's `toolUse.input` back as\n * the structured result.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n try {\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const input: ConverseCommandInput = {\n ...this.buildInput(chatOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const res = await this.send(input)\n const structured = extractStructuredToolInput(res)\n if (structured === undefined) {\n throw new Error(\n `${this.name}.structuredOutput: response contained no forced-tool output`,\n )\n }\n const usage = res.usage\n return {\n data: structured,\n rawText: JSON.stringify(structured),\n ...(usage && { usage: buildConverseUsage(usage) }),\n }\n } catch (error: unknown) {\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Streaming structured output. Same forced-tool strategy as\n * `structuredOutput`, but streamed: the forced tool's `toolUse.input` JSON\n * fragments are accumulated from the Converse stream and a terminal\n * `CUSTOM 'structured-output.complete'` event carries `{ object, raw }`,\n * mirroring openai-base's `structuredOutputStream` contract exactly.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n const { chatOptions, outputSchema } = options\n const runId = this.generateId()\n const threadId = chatOptions.threadId ?? this.generateId()\n const messageId = this.generateId()\n\n let hasEmittedRunStarted = false\n let hasEmittedTextMessageStart = false\n let accumulatedRaw = ''\n let finishReason: 'stop' | 'length' | 'content_filter' = 'stop'\n // Usage arrives on the trailing `metadata` event, after the finish signal,\n // so it is captured during iteration and folded into RUN_FINISHED below.\n let usage: TokenUsage | undefined\n\n try {\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const input: ConverseStreamCommandInput = {\n ...this.buildInput(chatOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const stream = await this.sendStream(input)\n\n // The forced tool streams its `input` as partial-JSON fragments inside\n // `contentBlockDelta.delta.toolUse.input`. We surface them as\n // TEXT_MESSAGE_CONTENT deltas (raw JSON text), matching openai-base which\n // carries the structured JSON as text deltas.\n for await (const ev of stream) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n // Surface in-band server/throttle/validation errors instead of\n // letting them fall through and masquerade as an empty response.\n throwIfConverseStreamError(ev)\n\n if ('contentBlockDelta' in ev) {\n const delta = ev.contentBlockDelta?.delta\n const fragment =\n delta && 'toolUse' in delta ? delta.toolUse?.input : undefined\n if (fragment !== undefined) {\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n accumulatedRaw += fragment\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: fragment,\n content: accumulatedRaw,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // The forced structured-output tool produces stopReason 'tool_use' on\n // success, but that's an implementation detail — a cleanly-completed\n // structured run reports 'stop', matching openai-base's contract.\n finishReason =\n stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n\n if (accumulatedRaw.length === 0) {\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n error: {\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n },\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedRaw)\n } catch {\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: `Failed to parse structured output as JSON. Content: ${accumulatedRaw.slice(0, 200)}${accumulatedRaw.length > 200 ? '...' : ''}`,\n code: 'parse-error',\n error: {\n message: 'Failed to parse structured output as JSON',\n code: 'parse-error',\n },\n }\n return\n }\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: parsed,\n raw: accumulatedRaw,\n },\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n finishReason,\n ...(usage && { usage }),\n }\n } catch (error: unknown) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Converse sends `tools` and a forced structured-output tool via two separate\n * mechanisms, never together. Declaring `false` makes the engine run the\n * agent loop without `outputSchema` and finalize via `structuredOutput` /\n * `structuredOutputStream`.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return false\n }\n\n // ---------------------------------------------------------------------------\n // Request construction\n // ---------------------------------------------------------------------------\n\n /**\n * Translate `TextOptions` into a `ConverseCommandInput`. Shared by chatStream,\n * structuredOutput, and structuredOutputStream (the latter two override\n * `toolConfig` with the forced structured tool afterwards).\n */\n protected buildInput(\n options: TextOptions<TProviderOptions>,\n ): ConverseCommandInput {\n const { system, messages } = toConverseMessages(\n options.messages,\n options.systemPrompts,\n )\n\n const toolConfig = options.tools\n ? toToolConfig(convertTools(options.tools), 'auto')\n : undefined\n\n // Sampling options live on `modelOptions` (typed as the narrowed\n // `BedrockConverseProviderOptions`, which surfaces the OpenAI Chat\n // Completions field names); translate them into Converse's `inferenceConfig`,\n // which uses AWS-native camelCase keys.\n const modelOptions = options.modelOptions\n const temperature = modelOptions?.temperature\n const topP = modelOptions?.top_p\n const maxTokens = modelOptions?.max_completion_tokens\n const stop = modelOptions?.stop\n const stopSequences =\n stop == null ? undefined : Array.isArray(stop) ? stop : [stop]\n\n const inferenceConfig =\n temperature != null ||\n topP != null ||\n maxTokens != null ||\n stopSequences != null\n ? {\n ...(temperature != null && { temperature }),\n ...(topP != null && { topP }),\n ...(maxTokens != null && { maxTokens }),\n ...(stopSequences != null && { stopSequences }),\n }\n : undefined\n\n return {\n modelId: this.model,\n messages,\n ...(system.length > 0 && { system }),\n ...(toolConfig && { toolConfig }),\n ...(inferenceConfig && { inferenceConfig }),\n }\n }\n}\n\n/**\n * Convert TanStack `Tool[]` to the Converse tool-converter input shape. Reuses\n * the SAME `convertSchemaToJsonSchema` the other adapters use so the Converse\n * tool input schemas match what every other provider sends.\n */\nfunction convertTools(tools: Array<Tool>): Array<ConverseToolInput> {\n return tools.map((tool) => {\n const inputSchema: JSONSchema = convertSchemaToJsonSchema(\n tool.inputSchema,\n ) ?? { type: 'object', properties: {}, required: [] }\n return {\n name: tool.name,\n description: tool.description,\n inputSchema,\n }\n })\n}\n\n/**\n * Find the forced structured-output tool's `input` in a non-streaming Converse\n * response. SDK-boundary narrowing only — `ConverseOutput` is a tagged union\n * (`{ message }`) and a tool-use block is `{ toolUse: { input } }`.\n */\nfunction extractStructuredToolInput(\n res: ConverseCommandOutput,\n): unknown | undefined {\n const message =\n res.output && 'message' in res.output ? res.output.message : undefined\n const content: Array<ContentBlock> = message?.content ?? []\n for (const block of content) {\n if ('toolUse' in block && block.toolUse) {\n // Only accept the forced structured tool (an unnamed block is allowed,\n // since the forced tool is the only one configured). A differently-named\n // tool-use block is a hallucinated/leftover call whose arbitrary input\n // must not be returned as the validated result — leave it to the caller's\n // `throw` so the failure is accurate instead of silently wrong.\n if (\n block.toolUse.name === STRUCTURED_TOOL_NAME ||\n block.toolUse.name === undefined\n ) {\n return block.toolUse.input\n }\n }\n }\n return undefined\n}\n\n/** Converse adapter with an explicit API key (low-level; mirrors createBedrockChat). */\nexport function createBedrockConverse<TModel extends BedrockConverseModels>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockConverseConfig, 'apiKey'>,\n): BedrockConverseTextAdapter<TModel> {\n return new BedrockConverseTextAdapter({ ...config, apiKey }, model)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAgEA,IAAa,6BAAb,cAYU,gBAKR;CACA,OAAyB;CACzB,OAAyB;CACzB;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,q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":3,"file":"converse-text.js","names":[],"sources":["../../../src/adapters/converse-text.ts"],"sourcesContent":["import { EventType, convertSchemaToJsonSchema } from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport { resolveBedrockAuth } from '../utils/auth'\nimport { toConverseMessages } from '../converse/message-converter'\nimport { toToolConfig } from '../converse/tool-converter'\nimport {\n processConverseStream,\n throwIfConverseStreamError,\n} from '../converse/stream-processor'\nimport { buildConverseUsage } from '../converse/usage'\nimport {\n STRUCTURED_TOOL_NAME,\n buildStructuredToolConfig,\n} from '../converse/structured-output'\nimport type { ResolvedBedrockAuth } from '../utils/auth'\nimport type { ConverseToolInput } from '../converse/tool-converter'\nimport type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime'\nimport type {\n BedrockRuntimeClient,\n BedrockRuntimeClientConfig,\n ContentBlock,\n ConverseCommandInput,\n ConverseCommandOutput,\n ConverseStreamCommandInput,\n ConverseStreamOutput,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type {\n JSONSchema,\n Modality,\n AdapterYieldChunk,\n TextOptions,\n TokenUsage,\n Tool,\n} from '@tanstack/ai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type {\n BedrockMessageMetadataByModality,\n BedrockSystemPromptMetadata,\n BedrockToolMetadata,\n} from '../message-types'\nimport type {\n BedrockConverseModels,\n ResolveConverseProviderOptions,\n ResolveInputModalities,\n} from '../model-meta'\n\n/** Config for the Converse adapter — same client config as the chat adapter. */\nexport interface BedrockConverseConfig extends BedrockClientConfig {}\n\n/**\n * Bedrock Converse text adapter. Wires the Converse translation modules (message\n * converter, tool converter, stream processor, structured-output forced-tool\n * builder) onto `@tanstack/ai`'s `BaseTextAdapter` and the\n * `@aws-sdk/client-bedrock-runtime` `BedrockRuntimeClient`.\n *\n * The success-path AG-UI lifecycle (`RUN_STARTED`..`RUN_FINISHED`) is owned by\n * `processConverseStream`; this adapter only owns the catch/`RUN_ERROR` path,\n * mirroring openai-base's `chatStream`.\n *\n * The actual SDK calls live behind two protected seams (`sendStream` / `send`)\n * so tests can subclass and inject canned Converse SDK shapes without a real\n * AWS request.\n */\nexport class BedrockConverseTextAdapter<\n TModel extends BedrockConverseModels,\n // Constraint mirrors the chat adapter (text.ts): the base parameterises\n // `TProviderOptions extends Record<string, any>`, and our default\n // `ResolveConverseProviderOptions<TModel>` resolves to an interface lacking an\n // implicit index signature — which `Record<string, unknown>` would reject but\n // `Record<string, any>` accepts. Confined to the generic constraint (the\n // established adapter pattern) — no value `as` cast is introduced.\n TProviderOptions extends Record<string, any> =\n ResolveConverseProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n BedrockMessageMetadataByModality,\n // TToolCapabilities — Converse has no per-model tool-capability table; the\n // base default.\n ReadonlyArray<string>,\n // TToolCallMetadata — Converse has no tool-call metadata round-tripping.\n unknown,\n // TSystemPromptMetadata — narrows `systemPrompts[i].metadata` at the chat()\n // call site so users get `cachePoint` autocomplete.\n BedrockSystemPromptMetadata\n> {\n override readonly kind = 'text' as const\n override readonly name = 'bedrock-converse' as const\n private clientPromise?: Promise<BedrockRuntimeClient>\n private readonly clientConfig: BedrockConverseConfig\n\n constructor(config: BedrockConverseConfig, model: TModel) {\n super({}, model)\n // Defer client construction and auth resolution: the AWS SDK is Node/\n // server-only, so we must not pull it into the static graph here. The\n // client (and its dynamic import) is built lazily on first SDK call.\n this.clientConfig = config\n }\n\n /**\n * Dynamically import `@aws-sdk/client-bedrock-runtime`. The specifier is held\n * in a variable (not a string literal) so bundler dep scanners (e.g. Vite/\n * esbuild optimizeDeps) cannot statically discover the AWS SDK and try to\n * pre-bundle it for the browser — it would fail on the SDK's Node-only\n * `fromTokenFile` export chain. The SDK is Node/server-only and is only\n * reached on a real request. `typeof import(...)` is a type-only reference\n * (erased at emit) so the imported members keep full typing.\n */\n protected importBedrockRuntime(): Promise<typeof BedrockRuntime> {\n const mod = '@aws-sdk/client-bedrock-runtime'\n return import(/* @vite-ignore */ mod) as Promise<typeof BedrockRuntime>\n }\n\n /**\n * Lazily construct the `BedrockRuntimeClient`. The dynamic import keeps\n * `@aws-sdk/client-bedrock-runtime` out of the static/browser graph and\n * defers `resolveBedrockAuth` until a real request is made.\n */\n protected async getClient(): Promise<BedrockRuntimeClient> {\n if (!this.clientPromise) {\n this.clientPromise = (async () => {\n const { BedrockRuntimeClient } = await this.importBedrockRuntime()\n const region = this.clientConfig.region ?? 'us-east-1'\n const resolved = resolveBedrockAuth(\n {\n apiKey: this.clientConfig.apiKey,\n region,\n auth: this.clientConfig.auth,\n },\n 'runtime',\n )\n const client = new BedrockRuntimeClient(\n this.buildClientConfig(resolved, region, this.clientConfig.baseURL),\n )\n const defaultHeaders = this.clientConfig.defaultHeaders\n if (defaultHeaders) {\n // `build` runs before SigV4 signing, so gateway headers are signed\n // along with the rest of the request.\n client.middlewareStack.add(\n (next) => (args) => {\n const request = args.request\n if (\n typeof request === 'object' &&\n request !== null &&\n 'headers' in request &&\n typeof request.headers === 'object' &&\n request.headers !== null\n ) {\n Object.assign(request.headers, defaultHeaders)\n }\n return next(args)\n },\n { step: 'build', name: 'tanstackDefaultHeaders' },\n )\n }\n return client\n })().catch((error: unknown) => {\n // Don't cache a rejected promise — clear it so a later call can retry\n // (e.g. after a transient import failure or fixed auth config).\n this.clientPromise = undefined\n throw error\n })\n }\n return this.clientPromise\n }\n\n /**\n * Map resolved auth + endpoint to a `BedrockRuntimeClientConfig`.\n *\n * Recent `@aws-sdk/client-bedrock-runtime` exposes a first-class `token`\n * config field for Bedrock API-key bearer auth. But the client's default\n * auth-scheme order is SigV4 first, then bearer — so passing `token` alone is\n * not enough: the SDK still resolves SigV4 and throws \"Could not load\n * credentials from any providers\". Pinning `authSchemePreference` to the\n * bearer scheme makes the API key actually get used. SigV4 uses the AWS\n * credential provider chain and the default scheme order.\n */\n protected buildClientConfig(\n resolved: ResolvedBedrockAuth,\n region: string,\n endpoint: string | undefined,\n ): BedrockRuntimeClientConfig {\n if (resolved.kind === 'bearer') {\n return {\n region,\n token: { token: resolved.token },\n authSchemePreference: ['httpBearerAuth'],\n ...(endpoint ? { endpoint } : {}),\n }\n }\n return {\n region: resolved.region,\n credentials: resolved.credentials,\n ...(endpoint ? { endpoint } : {}),\n }\n }\n\n // ---------------------------------------------------------------------------\n // SDK seams (overridden in tests so no real AWS call happens)\n // ---------------------------------------------------------------------------\n\n protected async sendStream(\n input: ConverseStreamCommandInput,\n ): Promise<AsyncIterable<ConverseStreamOutput>> {\n const { ConverseStreamCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const res = await client.send(new ConverseStreamCommand(input))\n if (!res.stream) {\n throw new Error('Bedrock Converse: empty stream response')\n }\n return res.stream\n }\n\n protected async send(\n input: ConverseCommandInput,\n ): Promise<ConverseCommandOutput> {\n const { ConverseCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n return client.send(new ConverseCommand(input))\n }\n\n // ---------------------------------------------------------------------------\n // Public adapter surface\n // ---------------------------------------------------------------------------\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n try {\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const input = this.buildInput(options)\n const stream = await this.sendStream(input)\n yield* processConverseStream(stream, () => this.generateId(), {\n threadId: options.threadId,\n parentRunId: options.parentRunId,\n model: options.model,\n })\n } catch (error: unknown) {\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes` (AG-UI's `RunErrorEvent.code` is optional).\n yield {\n type: EventType.RUN_ERROR,\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Structured output via the forced-tool strategy. Converse has no native\n * json_schema response_format, so we force a single tool whose input schema\n * is the requested output schema and read the model's `toolUse.input` back as\n * the structured result.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n try {\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const input: ConverseCommandInput = {\n ...this.buildInput(chatOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const res = await this.send(input)\n const structured = extractStructuredToolInput(res)\n if (structured === undefined) {\n throw new Error(\n `${this.name}.structuredOutput: response contained no forced-tool output`,\n )\n }\n const usage = res.usage\n return {\n data: structured,\n rawText: JSON.stringify(structured),\n ...(usage && { usage: buildConverseUsage(usage) }),\n }\n } catch (error: unknown) {\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Streaming structured output. Same forced-tool strategy as\n * `structuredOutput`, but streamed: the forced tool's `toolUse.input` JSON\n * fragments are accumulated from the Converse stream and a terminal\n * `CUSTOM 'structured-output.complete'` event carries `{ object, raw }`,\n * mirroring openai-base's `structuredOutputStream` contract exactly.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n const { chatOptions, outputSchema } = options\n const runId = this.generateId()\n const threadId = chatOptions.threadId ?? this.generateId()\n const messageId = this.generateId()\n\n let hasEmittedRunStarted = false\n let hasEmittedTextMessageStart = false\n let accumulatedRaw = ''\n let finishReason: 'stop' | 'length' | 'content_filter' = 'stop'\n // Usage arrives on the trailing `metadata` event, after the finish signal,\n // so it is captured during iteration and folded into RUN_FINISHED below.\n let usage: TokenUsage | undefined\n\n try {\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n const input: ConverseStreamCommandInput = {\n ...this.buildInput(chatOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const stream = await this.sendStream(input)\n\n // The forced tool streams its `input` as partial-JSON fragments inside\n // `contentBlockDelta.delta.toolUse.input`. We surface them as\n // TEXT_MESSAGE_CONTENT deltas (raw JSON text), matching openai-base which\n // carries the structured JSON as text deltas.\n for await (const ev of stream) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n // Surface in-band server/throttle/validation errors instead of\n // letting them fall through and masquerade as an empty response.\n throwIfConverseStreamError(ev)\n\n if ('contentBlockDelta' in ev) {\n const delta = ev.contentBlockDelta?.delta\n const fragment =\n delta && 'toolUse' in delta ? delta.toolUse?.input : undefined\n if (fragment !== undefined) {\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n accumulatedRaw += fragment\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: fragment,\n content: accumulatedRaw,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // The forced structured-output tool produces stopReason 'tool_use' on\n // success, but that's an implementation detail — a cleanly-completed\n // structured run reports 'stop', matching openai-base's contract.\n finishReason =\n stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n\n if (accumulatedRaw.length === 0) {\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n error: {\n message: `${this.name}.structuredOutputStream: response contained no content`,\n code: 'empty-response',\n },\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedRaw)\n } catch {\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: `Failed to parse structured output as JSON. Content: ${accumulatedRaw.slice(0, 200)}${accumulatedRaw.length > 200 ? '...' : ''}`,\n code: 'parse-error',\n error: {\n message: 'Failed to parse structured output as JSON',\n code: 'parse-error',\n },\n }\n return\n }\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: parsed,\n raw: accumulatedRaw,\n },\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n finishReason,\n ...(usage && { usage }),\n }\n } catch (error: unknown) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n const errorPayload = toRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n yield {\n type: EventType.RUN_ERROR,\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Converse sends `tools` and a forced structured-output tool via two separate\n * mechanisms, never together. Declaring `false` makes the engine run the\n * agent loop without `outputSchema` and finalize via `structuredOutput` /\n * `structuredOutputStream`.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return false\n }\n\n // ---------------------------------------------------------------------------\n // Request construction\n // ---------------------------------------------------------------------------\n\n /**\n * Translate `TextOptions` into a `ConverseCommandInput`. Shared by chatStream,\n * structuredOutput, and structuredOutputStream (the latter two override\n * `toolConfig` with the forced structured tool afterwards).\n */\n protected buildInput(\n options: TextOptions<TProviderOptions>,\n ): ConverseCommandInput {\n const { system, messages } = toConverseMessages(\n options.messages,\n options.systemPrompts,\n )\n\n const toolConfig = options.tools\n ? toToolConfig(convertTools(options.tools), 'auto')\n : undefined\n\n // Sampling options live on `modelOptions` (typed as the narrowed\n // `BedrockConverseProviderOptions`, which surfaces the OpenAI Chat\n // Completions field names); translate them into Converse's `inferenceConfig`,\n // which uses AWS-native camelCase keys.\n const modelOptions = options.modelOptions\n const temperature = modelOptions?.temperature\n const topP = modelOptions?.top_p\n const maxTokens = modelOptions?.max_completion_tokens\n const stop = modelOptions?.stop\n const stopSequences =\n stop == null ? undefined : Array.isArray(stop) ? stop : [stop]\n\n const inferenceConfig =\n temperature != null ||\n topP != null ||\n maxTokens != null ||\n stopSequences != null\n ? {\n ...(temperature != null && { temperature }),\n ...(topP != null && { topP }),\n ...(maxTokens != null && { maxTokens }),\n ...(stopSequences != null && { stopSequences }),\n }\n : undefined\n\n return {\n modelId: this.model,\n messages,\n ...(system.length > 0 && { system }),\n ...(toolConfig && { toolConfig }),\n ...(inferenceConfig && { inferenceConfig }),\n }\n }\n}\n\n/**\n * Convert TanStack `Tool[]` to the Converse tool-converter input shape. Reuses\n * the SAME `convertSchemaToJsonSchema` the other adapters use so the Converse\n * tool input schemas match what every other provider sends.\n */\nfunction convertTools(tools: Array<Tool>): Array<ConverseToolInput> {\n return tools.map((tool) => {\n const inputSchema: JSONSchema = convertSchemaToJsonSchema(\n tool.inputSchema,\n ) ?? { type: 'object', properties: {}, required: [] }\n const { cachePoint }: BedrockToolMetadata = tool.metadata ?? {}\n return {\n name: tool.name,\n description: tool.description,\n inputSchema,\n ...(cachePoint && { cachePoint }),\n }\n })\n}\n\n/**\n * Find the forced structured-output tool's `input` in a non-streaming Converse\n * response. SDK-boundary narrowing only — `ConverseOutput` is a tagged union\n * (`{ message }`) and a tool-use block is `{ toolUse: { input } }`.\n */\nfunction extractStructuredToolInput(\n res: ConverseCommandOutput,\n): unknown | undefined {\n const message =\n res.output && 'message' in res.output ? res.output.message : undefined\n const content: Array<ContentBlock> = message?.content ?? []\n for (const block of content) {\n if ('toolUse' in block && block.toolUse) {\n // Only accept the forced structured tool (an unnamed block is allowed,\n // since the forced tool is the only one configured). A differently-named\n // tool-use block is a hallucinated/leftover call whose arbitrary input\n // must not be returned as the validated result — leave it to the caller's\n // `throw` so the failure is accurate instead of silently wrong.\n if (\n block.toolUse.name === STRUCTURED_TOOL_NAME ||\n block.toolUse.name === undefined\n ) {\n return block.toolUse.input\n }\n }\n }\n return undefined\n}\n\n/** Converse adapter with an explicit API key (low-level; mirrors createBedrockChat). */\nexport function createBedrockConverse<TModel extends BedrockConverseModels>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockConverseConfig, 'apiKey'>,\n): BedrockConverseTextAdapter<TModel> {\n return new BedrockConver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;GACA,GAAI,cAAc,EAAE,WAAW;EACjC;CACF,CAAC;AACH;;;;;;AAOA,SAAS,2BACP,KACqB;CAGrB,MAAM,WADJ,IAAI,UAAU,aAAa,IAAI,SAAS,IAAI,OAAO,UAAU,KAAA,EAAA,EACjB,WAAW,CAAC;CAC1D,KAAK,MAAM,SAAS,SAClB,IAAI,aAAa,SAAS,MAAM,SAO5B;MAAA,MAAM,QAAQ,SAAA,uBACd,MAAM,QAAQ,SAAS,KAAA,GAEvB,OAAO,MAAM,QAAQ;CAAA;AAK7B;;AAGA,SAAgB,sBACd,OACA,QACA,QACoC;CACpC,OAAO,IAAI,2BAA2B;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AACpE"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { OpenAIBaseResponsesTextAdapter } from '@tanstack/openai-base';
|
|
1
|
+
import { OpenAIBaseResponsesTextAdapter, OpenAIBaseTextAdapterOptions } from '@tanstack/openai-base';
|
|
2
2
|
import { Modality } from '@tanstack/ai';
|
|
3
3
|
import { BedrockClientConfig } from '../utils/client.js';
|
|
4
4
|
import { BedrockMessageMetadataByModality } from '../message-types.js';
|
|
5
5
|
import { BedrockChatModelToolCapabilitiesByName, BedrockResponsesModels, ResolveInputModalities } from '../model-meta.js';
|
|
6
6
|
import { ExternalResponsesProviderOptions } from '../text/responses-provider-options.js';
|
|
7
|
-
export interface BedrockResponsesConfig extends BedrockClientConfig {
|
|
7
|
+
export interface BedrockResponsesConfig extends BedrockClientConfig, OpenAIBaseTextAdapterOptions {
|
|
8
8
|
}
|
|
9
9
|
export type { ExternalResponsesProviderOptions as BedrockResponsesProviderOptions } from '../text/responses-provider-options.js';
|
|
10
10
|
type ResolveToolCapabilities<TModel extends string> = TModel extends keyof BedrockChatModelToolCapabilitiesByName ? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]> : readonly [];
|
|
@@ -12,7 +12,7 @@ var BedrockResponsesTextAdapter = class extends OpenAIBaseResponsesTextAdapter {
|
|
|
12
12
|
kind = "text";
|
|
13
13
|
name = "bedrock-responses";
|
|
14
14
|
constructor(config, model) {
|
|
15
|
-
super(model, "bedrock-responses", new OpenAI(withBedrockDefaults(config, "mantle", model)));
|
|
15
|
+
super(model, "bedrock-responses", new OpenAI(withBedrockDefaults(config, "mantle", model)), config);
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
/** Responses adapter with an explicit API key (low-level; the public branching factory delegates here). */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"responses-text.js","names":[],"sources":["../../../src/adapters/responses-text.ts"],"sourcesContent":["import OpenAI from 'openai'\nimport { OpenAIBaseResponsesTextAdapter } from '@tanstack/openai-base'\nimport { withBedrockDefaults } from '../utils/client'\nimport type { Modality } from '@tanstack/ai'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type { BedrockMessageMetadataByModality } from '../message-types'\nimport type {\n BedrockChatModelToolCapabilitiesByName,\n BedrockResponsesModels,\n ResolveInputModalities,\n} from '../model-meta'\nimport type { ExternalResponsesProviderOptions } from '../text/responses-provider-options'\n\nexport interface BedrockResponsesConfig
|
|
1
|
+
{"version":3,"file":"responses-text.js","names":[],"sources":["../../../src/adapters/responses-text.ts"],"sourcesContent":["import OpenAI from 'openai'\nimport { OpenAIBaseResponsesTextAdapter } from '@tanstack/openai-base'\nimport { withBedrockDefaults } from '../utils/client'\nimport type { Modality } from '@tanstack/ai'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type { BedrockMessageMetadataByModality } from '../message-types'\nimport type {\n BedrockChatModelToolCapabilitiesByName,\n BedrockResponsesModels,\n ResolveInputModalities,\n} from '../model-meta'\nimport type { ExternalResponsesProviderOptions } from '../text/responses-provider-options'\nimport type { OpenAIBaseTextAdapterOptions } from '@tanstack/openai-base'\n\nexport interface BedrockResponsesConfig\n extends BedrockClientConfig, OpenAIBaseTextAdapterOptions {}\n\nexport type { ExternalResponsesProviderOptions as BedrockResponsesProviderOptions } from '../text/responses-provider-options'\n\ntype ResolveToolCapabilities<TModel extends string> =\n TModel extends keyof BedrockChatModelToolCapabilitiesByName\n ? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]>\n : readonly []\n\n/**\n * Bedrock Responses adapter. Drives mantle's OpenAI-compatible `/responses`\n * endpoint via the OpenAI SDK (`client.responses.create`) — the same base\n * class ai-openai's `openaiText` uses. Responses is mantle-only, so the\n * constructor forces the mantle baseURL.\n */\nexport class BedrockResponsesTextAdapter<\n TModel extends BedrockResponsesModels,\n // Constraint mirrors the chat adapter (and ai-groq / ai-openai) and the base,\n // which parameterises `TProviderOptions extends Record<string, any>`. Our\n // default `ExternalResponsesProviderOptions` is an interface that (lacking an\n // implicit index signature) `Record<string, unknown>` would reject but\n // `Record<string, any>` accepts. This `any` is confined to the generic\n // constraint — no value/shape `as` cast is introduced.\n TProviderOptions extends Record<string, any> =\n ExternalResponsesProviderOptions,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n TToolCapabilities extends ReadonlyArray<string> =\n ResolveToolCapabilities<TModel>,\n> extends OpenAIBaseResponsesTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n BedrockMessageMetadataByModality,\n TToolCapabilities\n> {\n override readonly kind = 'text' as const\n override readonly name = 'bedrock-responses' as const\n\n constructor(config: BedrockResponsesConfig, model: TModel) {\n // Responses is mantle-only — force the mantle base URL (an explicit\n // config.baseURL still wins, e.g. E2E pointing at aimock).\n super(\n model,\n 'bedrock-responses',\n new OpenAI(withBedrockDefaults(config, 'mantle', model)),\n config,\n )\n }\n}\n\n/** Responses adapter with an explicit API key (low-level; the public branching factory delegates here). */\nexport function createBedrockResponsesText<\n TModel extends BedrockResponsesModels,\n>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockResponsesConfig, 'apiKey'>,\n): BedrockResponsesTextAdapter<TModel> {\n return new BedrockResponsesTextAdapter({ ...config, apiKey }, model)\n}\n"],"mappings":";;;;;;;;;;AA8BA,IAAa,8BAAb,cAcU,+BAMR;CACA,OAAyB;CACzB,OAAyB;CAEzB,YAAY,QAAgC,OAAe;EAGzD,MACE,OACA,qBACA,IAAI,OAAO,oBAAoB,QAAQ,UAAU,KAAK,CAAC,GACvD,MACF;CACF;AACF;;AAGA,SAAgB,2BAGd,OACA,QACA,QACqC;CACrC,OAAO,IAAI,4BAA4B;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AACrE"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { OpenAIBaseChatCompletionsTextAdapter } from '@tanstack/openai-base';
|
|
1
|
+
import { OpenAIBaseChatCompletionsTextAdapter, OpenAIBaseTextAdapterOptions } from '@tanstack/openai-base';
|
|
2
2
|
import { Modality } from '@tanstack/ai';
|
|
3
3
|
import { BedrockClientConfig } from '../utils/client.js';
|
|
4
4
|
import { BedrockMessageMetadataByModality } from '../message-types.js';
|
|
5
5
|
import { BedrockChatModelToolCapabilitiesByName, BedrockChatModels, ResolveInputModalities, ResolveProviderOptions } from '../model-meta.js';
|
|
6
|
-
export interface BedrockTextConfig extends BedrockClientConfig {
|
|
6
|
+
export interface BedrockTextConfig extends BedrockClientConfig, OpenAIBaseTextAdapterOptions {
|
|
7
7
|
}
|
|
8
8
|
export type { ExternalTextProviderOptions as BedrockTextProviderOptions } from '../text/text-provider-options.js';
|
|
9
9
|
type ResolveToolCapabilities<TModel extends string> = TModel extends keyof BedrockChatModelToolCapabilitiesByName ? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]> : readonly [];
|
|
@@ -12,7 +12,7 @@ var BedrockTextAdapter = class extends OpenAIBaseChatCompletionsTextAdapter {
|
|
|
12
12
|
kind = "text";
|
|
13
13
|
name = "bedrock";
|
|
14
14
|
constructor(config, model) {
|
|
15
|
-
super(model, "bedrock", new OpenAI(withBedrockDefaults(config, void 0, model)));
|
|
15
|
+
super(model, "bedrock", new OpenAI(withBedrockDefaults(config, void 0, model)), config);
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Surface reasoning deltas (gpt-oss / Claude reasoning) the OpenAI-compatible
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","names":[],"sources":["../../../src/adapters/text.ts"],"sourcesContent":["import OpenAI from 'openai'\nimport { OpenAIBaseChatCompletionsTextAdapter } from '@tanstack/openai-base'\nimport { withBedrockDefaults } from '../utils/client'\nimport type { Modality } from '@tanstack/ai'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type { BedrockMessageMetadataByModality } from '../message-types'\nimport type {\n BedrockChatModelToolCapabilitiesByName,\n BedrockChatModels,\n ResolveInputModalities,\n ResolveProviderOptions,\n} from '../model-meta'\n\nexport interface BedrockTextConfig
|
|
1
|
+
{"version":3,"file":"text.js","names":[],"sources":["../../../src/adapters/text.ts"],"sourcesContent":["import OpenAI from 'openai'\nimport { OpenAIBaseChatCompletionsTextAdapter } from '@tanstack/openai-base'\nimport { withBedrockDefaults } from '../utils/client'\nimport type { Modality } from '@tanstack/ai'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type { BedrockMessageMetadataByModality } from '../message-types'\nimport type {\n BedrockChatModelToolCapabilitiesByName,\n BedrockChatModels,\n ResolveInputModalities,\n ResolveProviderOptions,\n} from '../model-meta'\nimport type { OpenAIBaseTextAdapterOptions } from '@tanstack/openai-base'\n\nexport interface BedrockTextConfig\n extends BedrockClientConfig, OpenAIBaseTextAdapterOptions {}\n\nexport type { ExternalTextProviderOptions as BedrockTextProviderOptions } from '../text/text-provider-options'\n\ntype ResolveToolCapabilities<TModel extends string> =\n TModel extends keyof BedrockChatModelToolCapabilitiesByName\n ? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]>\n : readonly []\n\n/**\n * Bedrock Chat Completions adapter. Drives Bedrock's OpenAI-compatible\n * `/chat/completions` endpoint via the OpenAI SDK with a baseURL override\n * (same pattern as ai-groq). Tool conversion, streaming, structured output,\n * and the agent loop come from the base.\n */\nexport class BedrockTextAdapter<\n TModel extends BedrockChatModels,\n // Constraint mirrors ai-groq and the base, which parameterises\n // `TProviderOptions extends Record<string, any>`. Our default\n // `ResolveProviderOptions<TModel>` resolves to the `BedrockTextProviderOptions`\n // interface, which (lacking an implicit index signature) `Record<string,\n // unknown>` would reject but `Record<string, any>` accepts. This `any` is\n // confined to the generic constraint (the established ai-groq pattern) — no\n // value/shape `as` cast is introduced.\n TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n TToolCapabilities extends ReadonlyArray<string> =\n ResolveToolCapabilities<TModel>,\n> extends OpenAIBaseChatCompletionsTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n BedrockMessageMetadataByModality,\n TToolCapabilities\n> {\n override readonly kind = 'text' as const\n override readonly name = 'bedrock' as const\n\n constructor(config: BedrockTextConfig, model: TModel) {\n // No `forced` -> honors config.endpoint ('runtime' default, 'mantle' allowed).\n super(\n model,\n 'bedrock',\n new OpenAI(withBedrockDefaults(config, undefined, model)),\n config,\n )\n }\n\n /**\n * Surface reasoning deltas (gpt-oss / Claude reasoning) the OpenAI-compatible\n * way. Base types the chunk as `unknown`; narrow with runtime guards — no\n * `as` casts, no `any`.\n */\n protected override extractReasoning(\n chunk: unknown,\n ): { text: string } | undefined {\n return readDeltaReasoning(chunk)\n }\n}\n\n/** Cast-free narrowing of a Chat Completions chunk's reasoning delta. */\nfunction readDeltaReasoning(chunk: unknown): { text: string } | undefined {\n if (typeof chunk !== 'object' || chunk === null || !('choices' in chunk))\n return undefined\n if (!Array.isArray(chunk.choices)) return undefined\n const choice: unknown = chunk.choices[0]\n if (typeof choice !== 'object' || choice === null || !('delta' in choice))\n return undefined\n const delta = choice.delta\n if (typeof delta !== 'object' || delta === null) return undefined\n const raw =\n 'reasoning' in delta && typeof delta.reasoning === 'string'\n ? delta.reasoning\n : 'reasoning_content' in delta &&\n typeof delta.reasoning_content === 'string'\n ? delta.reasoning_content\n : undefined\n return raw && raw.length > 0 ? { text: raw } : undefined\n}\n\n/** Chat adapter with an explicit API key (low-level; the public branching factory delegates here). */\nexport function createBedrockChat<TModel extends BedrockChatModels>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockTextConfig, 'apiKey'>,\n): BedrockTextAdapter<TModel> {\n return new BedrockTextAdapter({ ...config, apiKey }, model)\n}\n"],"mappings":";;;;;;;;;;AA8BA,IAAa,qBAAb,cAcU,qCAMR;CACA,OAAyB;CACzB,OAAyB;CAEzB,YAAY,QAA2B,OAAe;EAEpD,MACE,OACA,WACA,IAAI,OAAO,oBAAoB,QAAQ,KAAA,GAAW,KAAK,CAAC,GACxD,MACF;CACF;;;;;;CAOA,iBACE,OAC8B;EAC9B,OAAO,mBAAmB,KAAK;CACjC;AACF;;AAGA,SAAS,mBAAmB,OAA8C;CACxE,IAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,EAAE,aAAa,QAChE,OAAO,KAAA;CACT,IAAI,CAAC,MAAM,QAAQ,MAAM,OAAO,GAAG,OAAO,KAAA;CAC1C,MAAM,SAAkB,MAAM,QAAQ;CACtC,IAAI,OAAO,WAAW,YAAY,WAAW,QAAQ,EAAE,WAAW,SAChE,OAAO,KAAA;CACT,MAAM,QAAQ,OAAO;CACrB,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO,KAAA;CACxD,MAAM,MACJ,eAAe,SAAS,OAAO,MAAM,cAAc,WAC/C,MAAM,YACN,uBAAuB,SACrB,OAAO,MAAM,sBAAsB,WACnC,MAAM,oBACN,KAAA;CACR,OAAO,OAAO,IAAI,SAAS,IAAI,EAAE,MAAM,IAAI,IAAI,KAAA;AACjD;;AAGA,SAAgB,kBACd,OACA,QACA,QAC4B;CAC5B,OAAO,IAAI,mBAAmB;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AAC5D"}
|
|
@@ -3,7 +3,8 @@ import { Message, SystemContentBlock } from '@aws-sdk/client-bedrock-runtime';
|
|
|
3
3
|
/**
|
|
4
4
|
* Convert TanStack AI messages + system prompts into the Converse API format.
|
|
5
5
|
*
|
|
6
|
-
* - System prompts are lifted into `SystemContentBlock[]
|
|
6
|
+
* - System prompts are lifted into `SystemContentBlock[]`; a prompt whose
|
|
7
|
+
* `metadata.cachePoint` is set is followed by a `cachePoint` block.
|
|
7
8
|
* - `tool` role messages are remapped to `user` role `toolResult` blocks.
|
|
8
9
|
* - Consecutive messages with the same Converse role are merged (Converse
|
|
9
10
|
* requires strict user/assistant alternation).
|
|
@@ -85,6 +85,10 @@ function messageToBlocks(msg, docCounter) {
|
|
|
85
85
|
} else if (Array.isArray(msg.content)) for (const part of msg.content) {
|
|
86
86
|
const docIndex = isDocumentPart(part) ? ++docCounter.value : 0;
|
|
87
87
|
blocks.push(contentPartToBlock(part, docIndex));
|
|
88
|
+
if (isTextPart(part)) {
|
|
89
|
+
const { cachePoint } = part.metadata ?? {};
|
|
90
|
+
if (cachePoint) blocks.push({ cachePoint });
|
|
91
|
+
}
|
|
88
92
|
}
|
|
89
93
|
if (msg.role === "assistant" && msg.toolCalls) for (const call of msg.toolCalls) {
|
|
90
94
|
const rawArguments = call.function.arguments || "{}";
|
|
@@ -106,13 +110,14 @@ function messageToBlocks(msg, docCounter) {
|
|
|
106
110
|
/**
|
|
107
111
|
* Convert TanStack AI messages + system prompts into the Converse API format.
|
|
108
112
|
*
|
|
109
|
-
* - System prompts are lifted into `SystemContentBlock[]
|
|
113
|
+
* - System prompts are lifted into `SystemContentBlock[]`; a prompt whose
|
|
114
|
+
* `metadata.cachePoint` is set is followed by a `cachePoint` block.
|
|
110
115
|
* - `tool` role messages are remapped to `user` role `toolResult` blocks.
|
|
111
116
|
* - Consecutive messages with the same Converse role are merged (Converse
|
|
112
117
|
* requires strict user/assistant alternation).
|
|
113
118
|
*/
|
|
114
119
|
function toConverseMessages(messages, systemPrompts) {
|
|
115
|
-
const system = normalizeSystemPrompts(systemPrompts).
|
|
120
|
+
const system = normalizeSystemPrompts(systemPrompts).flatMap((p) => p.metadata?.cachePoint ? [{ text: p.content }, { cachePoint: p.metadata.cachePoint }] : [{ text: p.content }]);
|
|
116
121
|
const converseMessages = [];
|
|
117
122
|
const docCounter = { value: 0 };
|
|
118
123
|
for (const msg of messages) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-converter.js","names":[],"sources":["../../../src/converse/message-converter.ts"],"sourcesContent":["import {\n isFileSource,\n normalizeSystemPrompts,\n unsupportedFileSourceError,\n} from '@tanstack/ai'\nimport type {\n ContentPart,\n ContentPartDataSource,\n DocumentPart,\n ImagePart,\n ModelMessage,\n SystemPrompt,\n TextPart,\n} from '@tanstack/ai'\nimport type {\n ContentBlock,\n Message,\n SystemContentBlock,\n ToolResultContentBlock,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type { DocumentType } from '@smithy/types'\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nfunction base64ToBytes(b64: string): Uint8Array {\n return new Uint8Array(Buffer.from(b64, 'base64'))\n}\n\nfunction imageFormat(mime: string): 'png' | 'jpeg' | 'gif' | 'webp' {\n switch (mime) {\n case 'image/png':\n return 'png'\n case 'image/jpeg':\n case 'image/jpg':\n return 'jpeg'\n case 'image/gif':\n return 'gif'\n case 'image/webp':\n return 'webp'\n default:\n throw new Error(\n `Bedrock Converse: unsupported image MIME type \"${mime}\". Supported types: image/png, image/jpeg, image/gif, image/webp.`,\n )\n }\n}\n\nfunction documentFormat(\n mime: string,\n): 'pdf' | 'csv' | 'doc' | 'docx' | 'xls' | 'xlsx' | 'html' | 'txt' | 'md' {\n switch (mime) {\n case 'application/pdf':\n return 'pdf'\n case 'text/csv':\n return 'csv'\n case 'application/msword':\n return 'doc'\n case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':\n return 'docx'\n case 'application/vnd.ms-excel':\n return 'xls'\n case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':\n return 'xlsx'\n case 'text/html':\n return 'html'\n case 'text/plain':\n return 'txt'\n case 'text/markdown':\n case 'text/x-markdown':\n return 'md'\n default:\n throw new Error(\n `Bedrock Converse: unsupported document MIME type \"${mime}\". Supported types: pdf, csv, doc, docx, xls, xlsx, html, txt, md.`,\n )\n }\n}\n\nfunction stringContent(\n content: string | null | undefined | Array<ContentPart>,\n): string {\n if (content === null || content === undefined) return ''\n if (typeof content === 'string') return content\n return content\n .filter((p): p is TextPart => p.type === 'text')\n .map((p) => p.content)\n .join('')\n}\n\nfunction isTextPart(p: ContentPart): p is TextPart {\n return p.type === 'text'\n}\n\nfunction isImagePart(p: ContentPart): p is ImagePart {\n return p.type === 'image'\n}\n\nfunction isDocumentPart(p: ContentPart): p is DocumentPart {\n return p.type === 'document'\n}\n\nfunction isDataSource(\n source: ImagePart['source'] | DocumentPart['source'],\n): source is ContentPartDataSource {\n return source.type === 'data'\n}\n\nfunction contentPartToBlock(part: ContentPart, docIndex: number): ContentBlock {\n if (isTextPart(part)) {\n return { text: part.content }\n }\n\n if (isImagePart(part)) {\n const { source } = part\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (!isDataSource(source)) {\n throw new Error(\n 'Bedrock Converse requires inline image bytes; URL image sources are not supported.',\n )\n }\n return {\n image: {\n format: imageFormat(source.mimeType),\n source: { bytes: base64ToBytes(source.value) },\n },\n }\n }\n\n if (isDocumentPart(part)) {\n const { source } = part\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (!isDataSource(source)) {\n throw new Error(\n 'Bedrock Converse requires inline document bytes; URL document sources are not supported.',\n )\n }\n return {\n document: {\n format: documentFormat(source.mimeType),\n name: `document-${docIndex}`,\n source: { bytes: base64ToBytes(source.value) },\n },\n }\n }\n\n // Fail loud for unsupported part types (audio, video, etc.)\n throw new Error(\n `Bedrock Converse does not support content part type \"${String(part.type)}\".`,\n )\n}\n\nfunction messageToBlocks(\n msg: ModelMessage,\n docCounter: { value: number },\n): Array<ContentBlock> {\n const blocks: Array<ContentBlock> = []\n\n if (msg.role === 'tool') {\n if (!msg.toolCallId) {\n throw new Error(\n 'Bedrock Converse: tool message is missing toolCallId. Every tool result must reference the tool use ID it is responding to.',\n )\n }\n const textContent = stringContent(msg.content)\n const toolResult: ToolResultContentBlock = { text: textContent }\n blocks.push({\n toolResult: {\n toolUseId: msg.toolCallId,\n content: [toolResult],\n status: 'success',\n },\n })\n return blocks\n }\n\n // Map content field to text/image/document blocks\n if (typeof msg.content === 'string') {\n if (msg.content !== '') {\n blocks.push({ text: msg.content })\n }\n } else if (Array.isArray(msg.content)) {\n for (const part of msg.content) {\n const docIndex = isDocumentPart(part) ? ++docCounter.value : 0\n blocks.push(contentPartToBlock(part, docIndex))\n }\n }\n // null → no text blocks\n\n // Append toolUse blocks for assistant tool calls. Malformed or non-object\n // arguments come from a prior assistant turn the engine already accepted, so\n // they signal a real upstream problem — throw rather than silently coercing to\n // `{}` and forwarding a corrupted tool call (the adapter's catch surfaces it\n // as a RUN_ERROR).\n if (msg.role === 'assistant' && msg.toolCalls) {\n for (const call of msg.toolCalls) {\n const rawArguments = call.function.arguments || '{}'\n let parsed: unknown\n try {\n parsed = JSON.parse(rawArguments)\n } catch (error: unknown) {\n throw new Error(\n `Bedrock Converse: tool call \"${call.function.name}\" has malformed JSON arguments (${String(error)}). Raw: ${rawArguments}`,\n )\n }\n if (\n parsed === null ||\n typeof parsed !== 'object' ||\n Array.isArray(parsed)\n ) {\n throw new Error(\n `Bedrock Converse: tool call \"${call.function.name}\" arguments must be a JSON object, got ${Array.isArray(parsed) ? 'array' : typeof parsed}.`,\n )\n }\n blocks.push({\n toolUse: {\n toolUseId: call.id,\n name: call.function.name,\n input: parsed as DocumentType,\n },\n })\n }\n }\n\n return blocks\n}\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\n/**\n * Convert TanStack AI messages + system prompts into the Converse API format.\n *\n * - System prompts are lifted into `SystemContentBlock[]`.\n * - `tool` role messages are remapped to `user` role `toolResult` blocks.\n * - Consecutive messages with the same Converse role are merged (Converse\n * requires strict user/assistant alternation).\n */\nexport function toConverseMessages(\n messages: Array<ModelMessage>,\n systemPrompts?: Array<SystemPrompt>,\n): { system: Array<SystemContentBlock>; messages: Array<Message> } {\n // Build system blocks (uses normalizeSystemPrompts for runtime validation)\n const system: Array<SystemContentBlock> = normalizeSystemPrompts(\n systemPrompts,\n ).map((p) => ({ text: p.content }))\n\n // Convert each ModelMessage to a Converse Message, merging same-role pairs\n const converseMessages: Array<Message> = []\n // Global document counter: ensures every document block across all messages\n // gets a unique name, preventing Bedrock ValidationException for duplicate names.\n const docCounter = { value: 0 }\n\n for (const msg of messages) {\n // Map TanStack roles to Converse roles\n const converseRole: 'user' | 'assistant' =\n msg.role === 'assistant' ? 'assistant' : 'user'\n\n const blocks = messageToBlocks(msg, docCounter)\n\n // Skip messages that produce no content blocks (e.g. assistant with\n // null content and no toolCalls). Pushing an empty-content message to\n // Converse triggers a ValidationException.\n if (blocks.length === 0) continue\n\n const last = converseMessages[converseMessages.length - 1]\n if (last && last.role === converseRole) {\n // Merge into the previous message's content array\n last.content = [...(last.content ?? []), ...blocks]\n } else {\n converseMessages.push({ role: converseRole, content: blocks })\n }\n }\n\n return { system, messages: converseMessages }\n}\n"],"mappings":";;AA0BA,SAAS,cAAc,KAAyB;CAC9C,OAAO,IAAI,WAAW,OAAO,KAAK,KAAK,QAAQ,CAAC;AAClD;AAEA,SAAS,YAAY,MAA+C;CAClE,QAAQ,MAAR;EACE,KAAK,aACH,OAAO;EACT,KAAK;EACL,KAAK,aACH,OAAO;EACT,KAAK,aACH,OAAO;EACT,KAAK,cACH,OAAO;EACT,SACE,MAAM,IAAI,MACR,kDAAkD,KAAK,kEACzD;CACJ;AACF;AAEA,SAAS,eACP,MACyE;CACzE,QAAQ,MAAR;EACE,KAAK,mBACH,OAAO;EACT,KAAK,YACH,OAAO;EACT,KAAK,sBACH,OAAO;EACT,KAAK,2EACH,OAAO;EACT,KAAK,4BACH,OAAO;EACT,KAAK,qEACH,OAAO;EACT,KAAK,aACH,OAA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OAAO;AACT;;;;;;;;;AAcA,SAAgB,mBACd,UACA,eACiE;CAEjE,MAAM,SAAoC,uBACxC,aACF,CAAC,CAAC,KAAK,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE;CAGlC,MAAM,mBAAmC,CAAC;CAG1C,MAAM,aAAa,EAAE,OAAO,EAAE;CAE9B,KAAK,MAAM,OAAO,UAAU;EAE1B,MAAM,eACJ,IAAI,SAAS,cAAc,cAAc;EAE3C,MAAM,SAAS,gBAAgB,KAAK,UAAU;EAK9C,IAAI,OAAO,WAAW,GAAG;EAEzB,MAAM,OAAO,iBAAiB,iBAAiB,SAAS;EACxD,IAAI,QAAQ,KAAK,SAAS,cAExB,KAAK,UAAU,CAAC,GAAI,KAAK,WAAW,CAAC,GAAI,GAAG,MAAM;OAElD,iBAAiB,KAAK;GAAE,MAAM;GAAc,SAAS;EAAO,CAAC;CAEjE;CAEA,OAAO;EAAE;EAAQ,UAAU;CAAiB;AAC9C"}
|
|
1
|
+
{"version":3,"file":"message-converter.js","names":[],"sources":["../../../src/converse/message-converter.ts"],"sourcesContent":["import {\n isFileSource,\n normalizeSystemPrompts,\n unsupportedFileSourceError,\n} from '@tanstack/ai'\nimport type {\n ContentPart,\n ContentPartDataSource,\n DocumentPart,\n ImagePart,\n ModelMessage,\n SystemPrompt,\n TextPart,\n} from '@tanstack/ai'\nimport type {\n ContentBlock,\n Message,\n SystemContentBlock,\n ToolResultContentBlock,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type { DocumentType } from '@smithy/types'\nimport type {\n BedrockSystemPromptMetadata,\n BedrockTextMetadata,\n} from '../message-types'\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nfunction base64ToBytes(b64: string): Uint8Array {\n return new Uint8Array(Buffer.from(b64, 'base64'))\n}\n\nfunction imageFormat(mime: string): 'png' | 'jpeg' | 'gif' | 'webp' {\n switch (mime) {\n case 'image/png':\n return 'png'\n case 'image/jpeg':\n case 'image/jpg':\n return 'jpeg'\n case 'image/gif':\n return 'gif'\n case 'image/webp':\n return 'webp'\n default:\n throw new Error(\n `Bedrock Converse: unsupported image MIME type \"${mime}\". Supported types: image/png, image/jpeg, image/gif, image/webp.`,\n )\n }\n}\n\nfunction documentFormat(\n mime: string,\n): 'pdf' | 'csv' | 'doc' | 'docx' | 'xls' | 'xlsx' | 'html' | 'txt' | 'md' {\n switch (mime) {\n case 'application/pdf':\n return 'pdf'\n case 'text/csv':\n return 'csv'\n case 'application/msword':\n return 'doc'\n case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':\n return 'docx'\n case 'application/vnd.ms-excel':\n return 'xls'\n case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':\n return 'xlsx'\n case 'text/html':\n return 'html'\n case 'text/plain':\n return 'txt'\n case 'text/markdown':\n case 'text/x-markdown':\n return 'md'\n default:\n throw new Error(\n `Bedrock Converse: unsupported document MIME type \"${mime}\". Supported types: pdf, csv, doc, docx, xls, xlsx, html, txt, md.`,\n )\n }\n}\n\nfunction stringContent(\n content: string | null | undefined | Array<ContentPart>,\n): string {\n if (content === null || content === undefined) return ''\n if (typeof content === 'string') return content\n return content\n .filter((p): p is TextPart => p.type === 'text')\n .map((p) => p.content)\n .join('')\n}\n\nfunction isTextPart(p: ContentPart): p is TextPart {\n return p.type === 'text'\n}\n\nfunction isImagePart(p: ContentPart): p is ImagePart {\n return p.type === 'image'\n}\n\nfunction isDocumentPart(p: ContentPart): p is DocumentPart {\n return p.type === 'document'\n}\n\nfunction isDataSource(\n source: ImagePart['source'] | DocumentPart['source'],\n): source is ContentPartDataSource {\n return source.type === 'data'\n}\n\nfunction contentPartToBlock(part: ContentPart, docIndex: number): ContentBlock {\n if (isTextPart(part)) {\n return { text: part.content }\n }\n\n if (isImagePart(part)) {\n const { source } = part\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (!isDataSource(source)) {\n throw new Error(\n 'Bedrock Converse requires inline image bytes; URL image sources are not supported.',\n )\n }\n return {\n image: {\n format: imageFormat(source.mimeType),\n source: { bytes: base64ToBytes(source.value) },\n },\n }\n }\n\n if (isDocumentPart(part)) {\n const { source } = part\n if (isFileSource(source)) throw unsupportedFileSourceError('bedrock')\n if (!isDataSource(source)) {\n throw new Error(\n 'Bedrock Converse requires inline document bytes; URL document sources are not supported.',\n )\n }\n return {\n document: {\n format: documentFormat(source.mimeType),\n name: `document-${docIndex}`,\n source: { bytes: base64ToBytes(source.value) },\n },\n }\n }\n\n // Fail loud for unsupported part types (audio, video, etc.)\n throw new Error(\n `Bedrock Converse does not support content part type \"${String(part.type)}\".`,\n )\n}\n\nfunction messageToBlocks(\n msg: ModelMessage,\n docCounter: { value: number },\n): Array<ContentBlock> {\n const blocks: Array<ContentBlock> = []\n\n if (msg.role === 'tool') {\n if (!msg.toolCallId) {\n throw new Error(\n 'Bedrock Converse: tool message is missing toolCallId. Every tool result must reference the tool use ID it is responding to.',\n )\n }\n const textContent = stringContent(msg.content)\n const toolResult: ToolResultContentBlock = { text: textContent }\n blocks.push({\n toolResult: {\n toolUseId: msg.toolCallId,\n content: [toolResult],\n status: 'success',\n },\n })\n return blocks\n }\n\n // Map content field to text/image/document blocks\n if (typeof msg.content === 'string') {\n if (msg.content !== '') {\n blocks.push({ text: msg.content })\n }\n } else if (Array.isArray(msg.content)) {\n for (const part of msg.content) {\n const docIndex = isDocumentPart(part) ? ++docCounter.value : 0\n blocks.push(contentPartToBlock(part, docIndex))\n if (isTextPart(part)) {\n const { cachePoint } = (part.metadata ?? {}) as BedrockTextMetadata\n if (cachePoint) blocks.push({ cachePoint })\n }\n }\n }\n // null → no text blocks\n\n // Append toolUse blocks for assistant tool calls. Malformed or non-object\n // arguments come from a prior assistant turn the engine already accepted, so\n // they signal a real upstream problem — throw rather than silently coercing to\n // `{}` and forwarding a corrupted tool call (the adapter's catch surfaces it\n // as a RUN_ERROR).\n if (msg.role === 'assistant' && msg.toolCalls) {\n for (const call of msg.toolCalls) {\n const rawArguments = call.function.arguments || '{}'\n let parsed: unknown\n try {\n parsed = JSON.parse(rawArguments)\n } catch (error: unknown) {\n throw new Error(\n `Bedrock Converse: tool call \"${call.function.name}\" has malformed JSON arguments (${String(error)}). Raw: ${rawArguments}`,\n )\n }\n if (\n parsed === null ||\n typeof parsed !== 'object' ||\n Array.isArray(parsed)\n ) {\n throw new Error(\n `Bedrock Converse: tool call \"${call.function.name}\" arguments must be a JSON object, got ${Array.isArray(parsed) ? 'array' : typeof parsed}.`,\n )\n }\n blocks.push({\n toolUse: {\n toolUseId: call.id,\n name: call.function.name,\n input: parsed as DocumentType,\n },\n })\n }\n }\n\n return blocks\n}\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\n/**\n * Convert TanStack AI messages + system prompts into the Converse API format.\n *\n * - System prompts are lifted into `SystemContentBlock[]`; a prompt whose\n * `metadata.cachePoint` is set is followed by a `cachePoint` block.\n * - `tool` role messages are remapped to `user` role `toolResult` blocks.\n * - Consecutive messages with the same Converse role are merged (Converse\n * requires strict user/assistant alternation).\n */\nexport function toConverseMessages(\n messages: Array<ModelMessage>,\n systemPrompts?: Array<SystemPrompt>,\n): { system: Array<SystemContentBlock>; messages: Array<Message> } {\n // Build system blocks (uses normalizeSystemPrompts for runtime validation)\n const system: Array<SystemContentBlock> =\n normalizeSystemPrompts<BedrockSystemPromptMetadata>(systemPrompts).flatMap(\n (p) =>\n p.metadata?.cachePoint\n ? [{ text: p.content }, { cachePoint: p.metadata.cachePoint }]\n : [{ text: p.content }],\n )\n\n // Convert each ModelMessage to a Converse Message, merging same-role pairs\n const converseMessages: Array<Message> = []\n // Global document counter: ensures every document block across all messages\n // gets a unique name, preventing Bedrock ValidationException for duplicate names.\n const docCounter = { value: 0 }\n\n for (const msg of messages) {\n // Map TanStack roles to Converse roles\n const converseRole: 'user' | 'assistant' =\n msg.role === 'assistant' ? 'assistant' : 'user'\n\n const blocks = messageToBlocks(msg, docCounter)\n\n // Skip messages that produce no content blocks (e.g. assistant with\n // null content and no toolCalls). Pushing an empty-content message to\n // Converse triggers a ValidationException.\n if (blocks.length === 0) continue\n\n const last = converseMessages[converseMessages.length - 1]\n if (last && last.role === converseRole) {\n // Merge into the previous message's content array\n last.content = [...(last.content ?? []), ...blocks]\n } else {\n converseMessages.push({ role: converseRole, content: blocks })\n }\n }\n\n return { system, messages: converseMessages }\n}\n"],"mappings":";;AA8BA,SAAS,cAAc,KAAyB;CAC9C,OAAO,IAAI,WAAW,OAAO,KAAK,KAAK,QAAQ,CAAC;AAClD;AAEA,SAAS,YAAY,MAA+C;CAClE,QAAQ,MAAR;EACE,KAAK,aACH,OAAO;EACT,KAAK;EACL,KAAK,aACH,OAAO;EACT,KAAK,aACH,OAAO;EACT,KAAK,cACH,OAAO;EACT,SACE,MAAM,IAAI,MACR,kDAAkD,KAAK,kEACzD;CACJ;AACF;AAEA,SAAS,eACP,MACyE;CACzE,QAAQ,MAAR;EACE,KAAK,mBACH,OAAO;EACT,KAAK,YACH,OAAO;EACT,KAAK,sBACH,OAAO;EACT,KAAK,2EACH,OAAO;EACT,KAAK,4BACH,OAAO;EACT,KAAK,qEACH,OAAO;EACT,KAAK,aACH,OAAO;EACT,KAAK,cACH,OAAO;EACT,KAAK;EACL,KAAK,mBACH,OAAO;EACT,SACE,MAAM,IAAI,MACR,qDAAqD,KAAK,mEAC5D;CACJ;AACF;AAEA,SAAS,cACP,SACQ;CACR,IAAI,YAAY,QAAQ,YAAY,KAAA,GAAW,OAAO;CACtD,IAAI,OAAO,YAAY,UAAU,OAAO;CACxC,OAAO,QACJ,QAAQ,MAAqB,EAAE,SAAS,MAAM,CAAC,CAC/C,KAAK,MAAM,EAAE,OAAO,CAAC,CACrB,KAAK,EAAE;AACZ;AAEA,SAAS,WAAW,GAA+B;CACjD,OAAO,EAAE,SAAS;AACpB;AAEA,SAAS,YAAY,GAAgC;CACnD,OAAO,EAAE,SAAS;AACpB;AAEA,SAAS,eAAe,GAAmC;CACzD,OAAO,EAAE,SAAS;AAC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,cAAc,cAAc;EAE3C,MAAM,SAAS,gBAAgB,KAAK,UAAU;EAK9C,IAAI,OAAO,WAAW,GAAG;EAEzB,MAAM,OAAO,iBAAiB,iBAAiB,SAAS;EACxD,IAAI,QAAQ,KAAK,SAAS,cAExB,KAAK,UAAU,CAAC,GAAI,KAAK,WAAW,CAAC,GAAI,GAAG,MAAM;OAElD,iBAAiB,KAAK;GAAE,MAAM;GAAc,SAAS;EAAO,CAAC;CAEjE;CAEA,OAAO;EAAE;EAAQ,UAAU;CAAiB;AAC9C"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { ToolConfiguration } from '@aws-sdk/client-bedrock-runtime';
|
|
2
|
+
import { BedrockCachePoint } from '../message-types.js';
|
|
2
3
|
export interface ConverseToolInput {
|
|
3
4
|
name: string;
|
|
4
5
|
description?: string;
|
|
5
6
|
inputSchema: unknown;
|
|
7
|
+
/** Emit a `cachePoint` entry right after this tool. */
|
|
8
|
+
cachePoint?: BedrockCachePoint;
|
|
6
9
|
}
|
|
7
10
|
export type ToolChoiceInput = 'auto' | 'required' | 'none' | {
|
|
8
11
|
type: 'tool';
|
|
@@ -4,11 +4,11 @@ function toToolConfig(tools, choice) {
|
|
|
4
4
|
if (choice === "none") return void 0;
|
|
5
5
|
const toolChoice = mapChoice(choice);
|
|
6
6
|
return {
|
|
7
|
-
tools: tools.
|
|
7
|
+
tools: tools.flatMap((t) => [{ toolSpec: {
|
|
8
8
|
name: t.name,
|
|
9
9
|
...t.description ? { description: t.description } : {},
|
|
10
10
|
inputSchema: { json: t.inputSchema }
|
|
11
|
-
} })
|
|
11
|
+
} }, ...t.cachePoint ? [{ cachePoint: t.cachePoint }] : []]),
|
|
12
12
|
...toolChoice ? { toolChoice } : {}
|
|
13
13
|
};
|
|
14
14
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-converter.js","names":[],"sources":["../../../src/converse/tool-converter.ts"],"sourcesContent":["import type {\n ToolChoice,\n ToolConfiguration,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type { DocumentType } from '@smithy/types'\n\nexport interface ConverseToolInput {\n name: string\n description?: string\n inputSchema: unknown\n}\n\nexport type ToolChoiceInput =\n | 'auto'\n | 'required'\n | 'none'\n | { type: 'tool'; name: string }\n\nexport function toToolConfig(\n tools: Array<ConverseToolInput>,\n choice: ToolChoiceInput | undefined,\n): ToolConfiguration | undefined {\n if (!tools.length) return undefined\n // `none` means \"don't call tools\" — omit the tool config entirely, since\n // Bedrock treats a present tool config with no `toolChoice` as auto.\n if (choice === 'none') return undefined\n const toolChoice = mapChoice(choice)\n return {\n tools: tools.
|
|
1
|
+
{"version":3,"file":"tool-converter.js","names":[],"sources":["../../../src/converse/tool-converter.ts"],"sourcesContent":["import type {\n ToolChoice,\n ToolConfiguration,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type { DocumentType } from '@smithy/types'\nimport type { BedrockCachePoint } from '../message-types'\n\nexport interface ConverseToolInput {\n name: string\n description?: string\n inputSchema: unknown\n /** Emit a `cachePoint` entry right after this tool. */\n cachePoint?: BedrockCachePoint\n}\n\nexport type ToolChoiceInput =\n | 'auto'\n | 'required'\n | 'none'\n | { type: 'tool'; name: string }\n\nexport function toToolConfig(\n tools: Array<ConverseToolInput>,\n choice: ToolChoiceInput | undefined,\n): ToolConfiguration | undefined {\n if (!tools.length) return undefined\n // `none` means \"don't call tools\" — omit the tool config entirely, since\n // Bedrock treats a present tool config with no `toolChoice` as auto.\n if (choice === 'none') return undefined\n const toolChoice = mapChoice(choice)\n return {\n tools: tools.flatMap((t) => [\n {\n toolSpec: {\n name: t.name,\n ...(t.description ? { description: t.description } : {}),\n inputSchema: { json: t.inputSchema as DocumentType },\n },\n },\n ...(t.cachePoint ? [{ cachePoint: t.cachePoint }] : []),\n ]),\n ...(toolChoice ? { toolChoice } : {}),\n }\n}\n\nfunction mapChoice(\n choice: ToolChoiceInput | undefined,\n): ToolChoice | undefined {\n if (!choice || choice === 'auto') return { auto: {} }\n if (choice === 'required') return { any: {} }\n // `none` is handled earlier in toToolConfig (omits the tool config); this\n // branch keeps the string union narrowed so `choice.name` type-checks.\n if (choice === 'none') return undefined\n return { tool: { name: choice.name } }\n}\n"],"mappings":";AAqBA,SAAgB,aACd,OACA,QAC+B;CAC/B,IAAI,CAAC,MAAM,QAAQ,OAAO,KAAA;CAG1B,IAAI,WAAW,QAAQ,OAAO,KAAA;CAC9B,MAAM,aAAa,UAAU,MAAM;CACnC,OAAO;EACL,OAAO,MAAM,SAAS,MAAM,CAC1B,EACE,UAAU;GACR,MAAM,EAAE;GACR,GAAI,EAAE,cAAc,EAAE,aAAa,EAAE,YAAY,IAAI,CAAC;GACtD,aAAa,EAAE,MAAM,EAAE,YAA4B;EACrD,EACF,GACA,GAAI,EAAE,aAAa,CAAC,EAAE,YAAY,EAAE,WAAW,CAAC,IAAI,CAAC,CACvD,CAAC;EACD,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;CACrC;AACF;AAEA,SAAS,UACP,QACwB;CACxB,IAAI,CAAC,UAAU,WAAW,QAAQ,OAAO,EAAE,MAAM,CAAC,EAAE;CACpD,IAAI,WAAW,YAAY,OAAO,EAAE,KAAK,CAAC,EAAE;CAG5C,IAAI,WAAW,QAAQ,OAAO,KAAA;CAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,KAAK,EAAE;AACvC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -28,4 +28,4 @@ export type { BedrockCohereEmbeddingInputType, BedrockCohereEmbeddingProviderOpt
|
|
|
28
28
|
export type { BedrockConverseProviderOptions } from './converse/provider-options.js';
|
|
29
29
|
export { resolveBedrockAuth, withBedrockDefaults, type BedrockClientConfig, type BedrockEndpoint, type ResolvedBedrockAuth, } from './utils/client.js';
|
|
30
30
|
export { BEDROCK_CHAT_MODELS, BEDROCK_RESPONSES_MODELS, BEDROCK_CONVERSE_MODELS, BEDROCK_EMBEDDING_MODELS, type BedrockEmbeddingModel, type BedrockEmbeddingModelProviderOptionsByName, type BedrockEmbeddingModelInputModalitiesByName, type BedrockChatModels, type BedrockResponsesModels, type BedrockConverseModels, type BedrockChatModelProviderOptionsByName, type BedrockChatModelToolCapabilitiesByName, type BedrockModelInputModalitiesByName, } from './model-meta.js';
|
|
31
|
-
export type { BedrockMessageMetadataByModality, BedrockTextMetadata, BedrockImageMetadata, BedrockAudioMetadata, BedrockVideoMetadata, BedrockDocumentMetadata, } from './message-types.js';
|
|
31
|
+
export type { BedrockCachePoint, BedrockSystemPromptMetadata, BedrockToolMetadata, BedrockMessageMetadataByModality, BedrockTextMetadata, BedrockImageMetadata, BedrockAudioMetadata, BedrockVideoMetadata, BedrockDocumentMetadata, } from './message-types.js';
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["/**\n * @module @tanstack/ai-bedrock\n *\n * Amazon Bedrock adapter for TanStack AI via Bedrock's OpenAI-compatible APIs\n * and the native Converse API. The public `bedrockText` / `createBedrockText`\n * factory branches between the Converse adapter (DEFAULT), the Chat Completions\n * adapter (`api: 'chat'`), and the Responses adapter (`api: 'responses'`).\n */\n\nimport { BedrockTextAdapter } from './adapters/text'\nimport { BedrockResponsesTextAdapter } from './adapters/responses-text'\nimport { BedrockConverseTextAdapter } from './adapters/converse-text'\nimport {\n BEDROCK_CHAT_MODELS,\n BEDROCK_CONVERSE_MODELS,\n BEDROCK_RESPONSES_MODELS,\n} from './model-meta'\nimport type { BedrockTextConfig } from './adapters/text'\nimport type { BedrockResponsesConfig } from './adapters/responses-text'\nimport type { BedrockConverseConfig } from './adapters/converse-text'\nimport type { BedrockClientConfig } from './utils/client'\nimport type {\n BedrockChatModels,\n BedrockConverseModels,\n BedrockResponsesModels,\n} from './model-meta'\n\n/** Config for the branching factory's converse mode (default, or api: 'converse'). */\nexport type BedrockConverseApiConfig = BedrockConverseConfig & {\n api?: 'converse'\n}\n/** Config for the branching factory's chat mode (api: 'chat' required). */\nexport type BedrockChatApiConfig = BedrockTextConfig & {\n api: 'chat'\n}\n/** Config for the branching factory's responses mode (api: 'responses' required). */\nexport type BedrockResponsesApiConfig = BedrockResponsesConfig & {\n api: 'responses'\n}\n\ntype AnyBedrockAdapter =\n | BedrockConverseTextAdapter<BedrockConverseModels>\n | BedrockTextAdapter<BedrockChatModels>\n | BedrockResponsesTextAdapter<BedrockResponsesModels>\n\n/** Any catalog id the branching factory can receive. */\ntype BedrockFactoryModel =\n | BedrockConverseModels\n | BedrockChatModels\n | BedrockResponsesModels\n\n/** Cast-free runtime guard: is this model in the Responses-capable subset? */\nfunction isResponsesModel(model: string): model is BedrockResponsesModels {\n return BEDROCK_RESPONSES_MODELS.some((m) => m === model)\n}\n\n/** Cast-free runtime guard: is this model in the Chat-capable subset? */\nfunction isChatModel(model: string): model is BedrockChatModels {\n return BEDROCK_CHAT_MODELS.some((m) => m === model)\n}\n\n/** Cast-free runtime guard: is this model in the Converse-capable subset? */\nfunction isConverseModel(model: string): model is BedrockConverseModels {\n return BEDROCK_CONVERSE_MODELS.some((m) => m === model)\n}\n\n/** Strip the `api` discriminator from a config without an unused-var lint error. */\nfunction stripApi<T extends { api?: unknown }>(config: T): Omit<T, 'api'> {\n const { api, ...rest } = config\n void api\n return rest\n}\n\n/**\n * Shared branching used by both public factories. Constructs the adapter\n * classes directly so their constructors run the full auth cascade lazily\n * (config.apiKey → BEDROCK_API_KEY → AWS_BEARER_TOKEN_BEDROCK → SigV4). No\n * eager env-key fetch here, so `auth: 'sigv4'` never throws for a missing key.\n *\n * Default path → Converse adapter; opt-in via `api: 'chat'` or `api: 'responses'`.\n */\nfunction build(\n model: BedrockFactoryModel,\n config?: BedrockClientConfig & { api?: 'converse' | 'chat' | 'responses' },\n): AnyBedrockAdapter {\n if (config?.api === 'responses') {\n const rest = stripApi(config)\n if (!isResponsesModel(model)) {\n throw new Error(\n `Model \"${model}\" is not available on the Bedrock Responses API. ` +\n `Responses-capable models: ${BEDROCK_RESPONSES_MODELS.join(', ')}.`,\n )\n }\n return new BedrockResponsesTextAdapter(rest, model)\n }\n if (config?.api === 'chat') {\n if (!isChatModel(model)) {\n throw new Error(\n `Model \"${model}\" is not available on the Bedrock Chat Completions API. ` +\n `Chat-capable models: ${BEDROCK_CHAT_MODELS.join(', ')}.`,\n )\n }\n return new BedrockTextAdapter(stripApi(config), model)\n }\n if (!isConverseModel(model)) {\n throw new Error(\n `Model \"${model}\" is not available on the Bedrock Converse API. ` +\n `Converse-capable models: ${BEDROCK_CONVERSE_MODELS.join(', ')}.`,\n )\n }\n return new BedrockConverseTextAdapter(config ? stripApi(config) : {}, model)\n}\n\n// --- createBedrockText: explicit key, overloaded on `api` ---\nexport function createBedrockText<TModel extends BedrockConverseModels>(\n model: TModel,\n apiKey: string,\n config?: BedrockConverseApiConfig,\n): BedrockConverseTextAdapter<TModel>\nexport function createBedrockText<TModel extends BedrockChatModels>(\n model: TModel,\n apiKey: string,\n config: BedrockChatApiConfig,\n): BedrockTextAdapter<TModel>\nexport function createBedrockText<TModel extends BedrockResponsesModels>(\n model: TModel,\n apiKey: string,\n config: BedrockResponsesApiConfig,\n): BedrockResponsesTextAdapter<TModel>\nexport function createBedrockText(\n model: BedrockFactoryModel,\n apiKey: string,\n config?:\n | BedrockConverseApiConfig\n | BedrockChatApiConfig\n | BedrockResponsesApiConfig,\n): AnyBedrockAdapter {\n // Explicit apiKey is authoritative — spread config first so it can't override.\n return build(model, { ...config, apiKey })\n}\n\n// --- bedrockText: env-key counterpart, same overloads ---\nexport function bedrockText<TModel extends BedrockConverseModels>(\n model: TModel,\n config?: BedrockConverseApiConfig,\n): BedrockConverseTextAdapter<TModel>\nexport function bedrockText<TModel extends BedrockChatModels>(\n model: TModel,\n config: BedrockChatApiConfig,\n): BedrockTextAdapter<TModel>\nexport function bedrockText<TModel extends BedrockResponsesModels>(\n model: TModel,\n config: BedrockResponsesApiConfig,\n): BedrockResponsesTextAdapter<TModel>\nexport function bedrockText(\n model: BedrockFactoryModel,\n config?:\n | BedrockConverseApiConfig\n | BedrockChatApiConfig\n | BedrockResponsesApiConfig,\n): AnyBedrockAdapter {\n // No eager env-key fetch: the adapter constructor resolves auth lazily so\n // SigV4 (and the env-key fallback) work without a forced API key here.\n return build(model, config)\n}\n\n// --- Re-exports ---\nexport {\n BedrockTextAdapter,\n createBedrockChat,\n type BedrockTextConfig,\n type BedrockTextProviderOptions,\n} from './adapters/text'\nexport {\n BedrockResponsesTextAdapter,\n createBedrockResponsesText,\n type BedrockResponsesConfig,\n type BedrockResponsesProviderOptions,\n} from './adapters/responses-text'\nexport {\n BedrockConverseTextAdapter,\n createBedrockConverse,\n type BedrockConverseConfig,\n} from './adapters/converse-text'\nexport {\n BedrockEmbeddingAdapter,\n bedrockEmbedding,\n createBedrockEmbedding,\n type BedrockEmbeddingConfig,\n} from './adapters/embedding'\nexport type {\n BedrockCohereEmbeddingInputType,\n BedrockCohereEmbeddingProviderOptions,\n BedrockEmbeddingProviderOptions,\n BedrockTitanImageEmbeddingProviderOptions,\n BedrockTitanTextEmbeddingProviderOptions,\n} from './embedding/embedding-provider-options'\nexport type { BedrockConverseProviderOptions } from './converse/provider-options'\nexport {\n resolveBedrockAuth,\n withBedrockDefaults,\n type BedrockClientConfig,\n type BedrockEndpoint,\n type ResolvedBedrockAuth,\n} from './utils/client'\nexport {\n BEDROCK_CHAT_MODELS,\n BEDROCK_RESPONSES_MODELS,\n BEDROCK_CONVERSE_MODELS,\n BEDROCK_EMBEDDING_MODELS,\n type BedrockEmbeddingModel,\n type BedrockEmbeddingModelProviderOptionsByName,\n type BedrockEmbeddingModelInputModalitiesByName,\n type BedrockChatModels,\n type BedrockResponsesModels,\n type BedrockConverseModels,\n type BedrockChatModelProviderOptionsByName,\n type BedrockChatModelToolCapabilitiesByName,\n type BedrockModelInputModalitiesByName,\n} from './model-meta'\nexport type {\n BedrockMessageMetadataByModality,\n BedrockTextMetadata,\n BedrockImageMetadata,\n BedrockAudioMetadata,\n BedrockVideoMetadata,\n BedrockDocumentMetadata,\n} from './message-types'\n"],"mappings":";;;;;;;;;;;;;;;;;AAoDA,SAAS,iBAAiB,OAAgD;CACxE,OAAO,yBAAyB,MAAM,MAAM,MAAM,KAAK;AACzD;;AAGA,SAAS,YAAY,OAA2C;CAC9D,OAAO,oBAAoB,MAAM,MAAM,MAAM,KAAK;AACpD;;AAGA,SAAS,gBAAgB,OAA+C;CACtE,OAAO,wBAAwB,MAAM,MAAM,MAAM,KAAK;AACxD;;AAGA,SAAS,SAAsC,QAA2B;CACxE,MAAM,EAAE,KAAK,GAAG,SAAS;CAEzB,OAAO;AACT;;;;;;;;;AAUA,SAAS,MACP,OACA,QACmB;CACnB,IAAI,QAAQ,QAAQ,aAAa;EAC/B,MAAM,OAAO,SAAS,MAAM;EAC5B,IAAI,CAAC,iBAAiB,KAAK,GACzB,MAAM,IAAI,MACR,UAAU,MAAM,6EACe,yBAAyB,KAAK,IAAI,EAAE,EACrE;EAEF,OAAO,IAAI,4BAA4B,MAAM,KAAK;CACpD;CACA,IAAI,QAAQ,QAAQ,QAAQ;EAC1B,IAAI,CAAC,YAAY,KAAK,GACpB,MAAM,IAAI,MACR,UAAU,MAAM,+EACU,oBAAoB,KAAK,IAAI,EAAE,EAC3D;EAEF,OAAO,IAAI,mBAAmB,SAAS,MAAM,GAAG,KAAK;CACvD;CACA,IAAI,CAAC,gBAAgB,KAAK,GACxB,MAAM,IAAI,MACR,UAAU,MAAM,2EACc,wBAAwB,KAAK,IAAI,EAAE,EACnE;CAEF,OAAO,IAAI,2BAA2B,SAAS,SAAS,MAAM,IAAI,CAAC,GAAG,KAAK;AAC7E;AAkBA,SAAgB,kBACd,OACA,QACA,QAImB;CAEnB,OAAO,MAAM,OAAO;EAAE,GAAG;EAAQ;CAAO,CAAC;AAC3C;AAeA,SAAgB,YACd,OACA,QAImB;CAGnB,OAAO,MAAM,OAAO,MAAM;AAC5B"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["/**\n * @module @tanstack/ai-bedrock\n *\n * Amazon Bedrock adapter for TanStack AI via Bedrock's OpenAI-compatible APIs\n * and the native Converse API. The public `bedrockText` / `createBedrockText`\n * factory branches between the Converse adapter (DEFAULT), the Chat Completions\n * adapter (`api: 'chat'`), and the Responses adapter (`api: 'responses'`).\n */\n\nimport { BedrockTextAdapter } from './adapters/text'\nimport { BedrockResponsesTextAdapter } from './adapters/responses-text'\nimport { BedrockConverseTextAdapter } from './adapters/converse-text'\nimport {\n BEDROCK_CHAT_MODELS,\n BEDROCK_CONVERSE_MODELS,\n BEDROCK_RESPONSES_MODELS,\n} from './model-meta'\nimport type { BedrockTextConfig } from './adapters/text'\nimport type { BedrockResponsesConfig } from './adapters/responses-text'\nimport type { BedrockConverseConfig } from './adapters/converse-text'\nimport type { BedrockClientConfig } from './utils/client'\nimport type {\n BedrockChatModels,\n BedrockConverseModels,\n BedrockResponsesModels,\n} from './model-meta'\n\n/** Config for the branching factory's converse mode (default, or api: 'converse'). */\nexport type BedrockConverseApiConfig = BedrockConverseConfig & {\n api?: 'converse'\n}\n/** Config for the branching factory's chat mode (api: 'chat' required). */\nexport type BedrockChatApiConfig = BedrockTextConfig & {\n api: 'chat'\n}\n/** Config for the branching factory's responses mode (api: 'responses' required). */\nexport type BedrockResponsesApiConfig = BedrockResponsesConfig & {\n api: 'responses'\n}\n\ntype AnyBedrockAdapter =\n | BedrockConverseTextAdapter<BedrockConverseModels>\n | BedrockTextAdapter<BedrockChatModels>\n | BedrockResponsesTextAdapter<BedrockResponsesModels>\n\n/** Any catalog id the branching factory can receive. */\ntype BedrockFactoryModel =\n | BedrockConverseModels\n | BedrockChatModels\n | BedrockResponsesModels\n\n/** Cast-free runtime guard: is this model in the Responses-capable subset? */\nfunction isResponsesModel(model: string): model is BedrockResponsesModels {\n return BEDROCK_RESPONSES_MODELS.some((m) => m === model)\n}\n\n/** Cast-free runtime guard: is this model in the Chat-capable subset? */\nfunction isChatModel(model: string): model is BedrockChatModels {\n return BEDROCK_CHAT_MODELS.some((m) => m === model)\n}\n\n/** Cast-free runtime guard: is this model in the Converse-capable subset? */\nfunction isConverseModel(model: string): model is BedrockConverseModels {\n return BEDROCK_CONVERSE_MODELS.some((m) => m === model)\n}\n\n/** Strip the `api` discriminator from a config without an unused-var lint error. */\nfunction stripApi<T extends { api?: unknown }>(config: T): Omit<T, 'api'> {\n const { api, ...rest } = config\n void api\n return rest\n}\n\n/**\n * Shared branching used by both public factories. Constructs the adapter\n * classes directly so their constructors run the full auth cascade lazily\n * (config.apiKey → BEDROCK_API_KEY → AWS_BEARER_TOKEN_BEDROCK → SigV4). No\n * eager env-key fetch here, so `auth: 'sigv4'` never throws for a missing key.\n *\n * Default path → Converse adapter; opt-in via `api: 'chat'` or `api: 'responses'`.\n */\nfunction build(\n model: BedrockFactoryModel,\n config?: BedrockClientConfig & { api?: 'converse' | 'chat' | 'responses' },\n): AnyBedrockAdapter {\n if (config?.api === 'responses') {\n const rest = stripApi(config)\n if (!isResponsesModel(model)) {\n throw new Error(\n `Model \"${model}\" is not available on the Bedrock Responses API. ` +\n `Responses-capable models: ${BEDROCK_RESPONSES_MODELS.join(', ')}.`,\n )\n }\n return new BedrockResponsesTextAdapter(rest, model)\n }\n if (config?.api === 'chat') {\n if (!isChatModel(model)) {\n throw new Error(\n `Model \"${model}\" is not available on the Bedrock Chat Completions API. ` +\n `Chat-capable models: ${BEDROCK_CHAT_MODELS.join(', ')}.`,\n )\n }\n return new BedrockTextAdapter(stripApi(config), model)\n }\n if (!isConverseModel(model)) {\n throw new Error(\n `Model \"${model}\" is not available on the Bedrock Converse API. ` +\n `Converse-capable models: ${BEDROCK_CONVERSE_MODELS.join(', ')}.`,\n )\n }\n return new BedrockConverseTextAdapter(config ? stripApi(config) : {}, model)\n}\n\n// --- createBedrockText: explicit key, overloaded on `api` ---\nexport function createBedrockText<TModel extends BedrockConverseModels>(\n model: TModel,\n apiKey: string,\n config?: BedrockConverseApiConfig,\n): BedrockConverseTextAdapter<TModel>\nexport function createBedrockText<TModel extends BedrockChatModels>(\n model: TModel,\n apiKey: string,\n config: BedrockChatApiConfig,\n): BedrockTextAdapter<TModel>\nexport function createBedrockText<TModel extends BedrockResponsesModels>(\n model: TModel,\n apiKey: string,\n config: BedrockResponsesApiConfig,\n): BedrockResponsesTextAdapter<TModel>\nexport function createBedrockText(\n model: BedrockFactoryModel,\n apiKey: string,\n config?:\n | BedrockConverseApiConfig\n | BedrockChatApiConfig\n | BedrockResponsesApiConfig,\n): AnyBedrockAdapter {\n // Explicit apiKey is authoritative — spread config first so it can't override.\n return build(model, { ...config, apiKey })\n}\n\n// --- bedrockText: env-key counterpart, same overloads ---\nexport function bedrockText<TModel extends BedrockConverseModels>(\n model: TModel,\n config?: BedrockConverseApiConfig,\n): BedrockConverseTextAdapter<TModel>\nexport function bedrockText<TModel extends BedrockChatModels>(\n model: TModel,\n config: BedrockChatApiConfig,\n): BedrockTextAdapter<TModel>\nexport function bedrockText<TModel extends BedrockResponsesModels>(\n model: TModel,\n config: BedrockResponsesApiConfig,\n): BedrockResponsesTextAdapter<TModel>\nexport function bedrockText(\n model: BedrockFactoryModel,\n config?:\n | BedrockConverseApiConfig\n | BedrockChatApiConfig\n | BedrockResponsesApiConfig,\n): AnyBedrockAdapter {\n // No eager env-key fetch: the adapter constructor resolves auth lazily so\n // SigV4 (and the env-key fallback) work without a forced API key here.\n return build(model, config)\n}\n\n// --- Re-exports ---\nexport {\n BedrockTextAdapter,\n createBedrockChat,\n type BedrockTextConfig,\n type BedrockTextProviderOptions,\n} from './adapters/text'\nexport {\n BedrockResponsesTextAdapter,\n createBedrockResponsesText,\n type BedrockResponsesConfig,\n type BedrockResponsesProviderOptions,\n} from './adapters/responses-text'\nexport {\n BedrockConverseTextAdapter,\n createBedrockConverse,\n type BedrockConverseConfig,\n} from './adapters/converse-text'\nexport {\n BedrockEmbeddingAdapter,\n bedrockEmbedding,\n createBedrockEmbedding,\n type BedrockEmbeddingConfig,\n} from './adapters/embedding'\nexport type {\n BedrockCohereEmbeddingInputType,\n BedrockCohereEmbeddingProviderOptions,\n BedrockEmbeddingProviderOptions,\n BedrockTitanImageEmbeddingProviderOptions,\n BedrockTitanTextEmbeddingProviderOptions,\n} from './embedding/embedding-provider-options'\nexport type { BedrockConverseProviderOptions } from './converse/provider-options'\nexport {\n resolveBedrockAuth,\n withBedrockDefaults,\n type BedrockClientConfig,\n type BedrockEndpoint,\n type ResolvedBedrockAuth,\n} from './utils/client'\nexport {\n BEDROCK_CHAT_MODELS,\n BEDROCK_RESPONSES_MODELS,\n BEDROCK_CONVERSE_MODELS,\n BEDROCK_EMBEDDING_MODELS,\n type BedrockEmbeddingModel,\n type BedrockEmbeddingModelProviderOptionsByName,\n type BedrockEmbeddingModelInputModalitiesByName,\n type BedrockChatModels,\n type BedrockResponsesModels,\n type BedrockConverseModels,\n type BedrockChatModelProviderOptionsByName,\n type BedrockChatModelToolCapabilitiesByName,\n type BedrockModelInputModalitiesByName,\n} from './model-meta'\nexport type {\n BedrockCachePoint,\n BedrockSystemPromptMetadata,\n BedrockToolMetadata,\n BedrockMessageMetadataByModality,\n BedrockTextMetadata,\n BedrockImageMetadata,\n BedrockAudioMetadata,\n BedrockVideoMetadata,\n BedrockDocumentMetadata,\n} from './message-types'\n"],"mappings":";;;;;;;;;;;;;;;;;AAoDA,SAAS,iBAAiB,OAAgD;CACxE,OAAO,yBAAyB,MAAM,MAAM,MAAM,KAAK;AACzD;;AAGA,SAAS,YAAY,OAA2C;CAC9D,OAAO,oBAAoB,MAAM,MAAM,MAAM,KAAK;AACpD;;AAGA,SAAS,gBAAgB,OAA+C;CACtE,OAAO,wBAAwB,MAAM,MAAM,MAAM,KAAK;AACxD;;AAGA,SAAS,SAAsC,QAA2B;CACxE,MAAM,EAAE,KAAK,GAAG,SAAS;CAEzB,OAAO;AACT;;;;;;;;;AAUA,SAAS,MACP,OACA,QACmB;CACnB,IAAI,QAAQ,QAAQ,aAAa;EAC/B,MAAM,OAAO,SAAS,MAAM;EAC5B,IAAI,CAAC,iBAAiB,KAAK,GACzB,MAAM,IAAI,MACR,UAAU,MAAM,6EACe,yBAAyB,KAAK,IAAI,EAAE,EACrE;EAEF,OAAO,IAAI,4BAA4B,MAAM,KAAK;CACpD;CACA,IAAI,QAAQ,QAAQ,QAAQ;EAC1B,IAAI,CAAC,YAAY,KAAK,GACpB,MAAM,IAAI,MACR,UAAU,MAAM,+EACU,oBAAoB,KAAK,IAAI,EAAE,EAC3D;EAEF,OAAO,IAAI,mBAAmB,SAAS,MAAM,GAAG,KAAK;CACvD;CACA,IAAI,CAAC,gBAAgB,KAAK,GACxB,MAAM,IAAI,MACR,UAAU,MAAM,2EACc,wBAAwB,KAAK,IAAI,EAAE,EACnE;CAEF,OAAO,IAAI,2BAA2B,SAAS,SAAS,MAAM,IAAI,CAAC,GAAG,KAAK;AAC7E;AAkBA,SAAgB,kBACd,OACA,QACA,QAImB;CAEnB,OAAO,MAAM,OAAO;EAAE,GAAG;EAAQ;CAAO,CAAC;AAC3C;AAeA,SAAgB,YACd,OACA,QAImB;CAGnB,OAAO,MAAM,OAAO,MAAM;AAC5B"}
|
|
@@ -1,10 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Bedrock prompt-cache checkpoint. Bedrock caches everything in the request
|
|
3
|
+
* before this block and reads it back at the cache rate while the entry lives.
|
|
4
|
+
* A request may carry up to four. Omit `ttl` for the 5-minute default.
|
|
5
|
+
*
|
|
6
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html
|
|
7
|
+
*/
|
|
8
|
+
export interface BedrockCachePoint {
|
|
9
|
+
type: 'default';
|
|
10
|
+
ttl?: '5m' | '1h';
|
|
11
|
+
}
|
|
12
|
+
/** Metadata on a `systemPrompts` entry, read by the Converse adapter. */
|
|
13
|
+
export interface BedrockSystemPromptMetadata {
|
|
14
|
+
/** Place a cache checkpoint right after this system prompt. */
|
|
15
|
+
cachePoint?: BedrockCachePoint;
|
|
16
|
+
}
|
|
17
|
+
/** Metadata on a tool definition, read by the Converse adapter. */
|
|
18
|
+
export interface BedrockToolMetadata {
|
|
19
|
+
/** Place a cache checkpoint right after this tool's definition. */
|
|
20
|
+
cachePoint?: BedrockCachePoint;
|
|
21
|
+
}
|
|
1
22
|
/**
|
|
2
23
|
* Bedrock content-part metadata by modality, used for type inference when
|
|
3
24
|
* constructing multimodal messages. Bedrock's OpenAI-compatible Chat
|
|
4
|
-
* Completions accepts the standard OpenAI image-detail hint;
|
|
5
|
-
*
|
|
25
|
+
* Completions accepts the standard OpenAI image-detail hint; the Converse
|
|
26
|
+
* adapter reads `cachePoint` on text parts.
|
|
6
27
|
*/
|
|
7
28
|
export interface BedrockTextMetadata {
|
|
29
|
+
/** Place a cache checkpoint right after this text block (Converse only). */
|
|
30
|
+
cachePoint?: BedrockCachePoint;
|
|
8
31
|
}
|
|
9
32
|
export interface BedrockImageMetadata {
|
|
10
33
|
/** Image processing detail: 'auto' (default), 'low', or 'high'. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-bedrock",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Amazon Bedrock adapter for TanStack AI — OpenAI-compatible chat, responses, tools, and reasoning.",
|
|
6
6
|
"author": "",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"vite": "^8.2.1"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@tanstack/ai": "^0.
|
|
46
|
+
"@tanstack/ai": "^0.64.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@aws-crypto/sha256-js": "^5.2.0",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"@smithy/signature-v4": "^5.4.5",
|
|
53
53
|
"@smithy/types": "^4.14.2",
|
|
54
54
|
"openai": "^6.41.0",
|
|
55
|
-
"@tanstack/openai-base": "^0.
|
|
55
|
+
"@tanstack/openai-base": "^0.12.2"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "vite build",
|
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
"lint:fix": "oxlint src --type-aware --fix",
|
|
61
61
|
"test:build": "publint --strict",
|
|
62
62
|
"test:oxlint": "oxlint src --type-aware",
|
|
63
|
+
"test:coverage": "vitest run --coverage --coverage.include='src/**' --coverage.reporter=text-summary --coverage.reporter=json-summary",
|
|
63
64
|
"test:lib": "vitest run",
|
|
64
65
|
"test:lib:dev": "pnpm test:lib --watch",
|
|
65
66
|
"test:types": "tsc"
|
|
@@ -38,7 +38,11 @@ import type {
|
|
|
38
38
|
StructuredOutputResult,
|
|
39
39
|
} from '@tanstack/ai/adapters'
|
|
40
40
|
import type { BedrockClientConfig } from '../utils/client'
|
|
41
|
-
import type {
|
|
41
|
+
import type {
|
|
42
|
+
BedrockMessageMetadataByModality,
|
|
43
|
+
BedrockSystemPromptMetadata,
|
|
44
|
+
BedrockToolMetadata,
|
|
45
|
+
} from '../message-types'
|
|
42
46
|
import type {
|
|
43
47
|
BedrockConverseModels,
|
|
44
48
|
ResolveConverseProviderOptions,
|
|
@@ -78,7 +82,15 @@ export class BedrockConverseTextAdapter<
|
|
|
78
82
|
TModel,
|
|
79
83
|
TProviderOptions,
|
|
80
84
|
TInputModalities,
|
|
81
|
-
BedrockMessageMetadataByModality
|
|
85
|
+
BedrockMessageMetadataByModality,
|
|
86
|
+
// TToolCapabilities — Converse has no per-model tool-capability table; the
|
|
87
|
+
// base default.
|
|
88
|
+
ReadonlyArray<string>,
|
|
89
|
+
// TToolCallMetadata — Converse has no tool-call metadata round-tripping.
|
|
90
|
+
unknown,
|
|
91
|
+
// TSystemPromptMetadata — narrows `systemPrompts[i].metadata` at the chat()
|
|
92
|
+
// call site so users get `cachePoint` autocomplete.
|
|
93
|
+
BedrockSystemPromptMetadata
|
|
82
94
|
> {
|
|
83
95
|
override readonly kind = 'text' as const
|
|
84
96
|
override readonly name = 'bedrock-converse' as const
|
|
@@ -593,10 +605,12 @@ function convertTools(tools: Array<Tool>): Array<ConverseToolInput> {
|
|
|
593
605
|
const inputSchema: JSONSchema = convertSchemaToJsonSchema(
|
|
594
606
|
tool.inputSchema,
|
|
595
607
|
) ?? { type: 'object', properties: {}, required: [] }
|
|
608
|
+
const { cachePoint }: BedrockToolMetadata = tool.metadata ?? {}
|
|
596
609
|
return {
|
|
597
610
|
name: tool.name,
|
|
598
611
|
description: tool.description,
|
|
599
612
|
inputSchema,
|
|
613
|
+
...(cachePoint && { cachePoint }),
|
|
600
614
|
}
|
|
601
615
|
})
|
|
602
616
|
}
|
|
@@ -10,8 +10,10 @@ import type {
|
|
|
10
10
|
ResolveInputModalities,
|
|
11
11
|
} from '../model-meta'
|
|
12
12
|
import type { ExternalResponsesProviderOptions } from '../text/responses-provider-options'
|
|
13
|
+
import type { OpenAIBaseTextAdapterOptions } from '@tanstack/openai-base'
|
|
13
14
|
|
|
14
|
-
export interface BedrockResponsesConfig
|
|
15
|
+
export interface BedrockResponsesConfig
|
|
16
|
+
extends BedrockClientConfig, OpenAIBaseTextAdapterOptions {}
|
|
15
17
|
|
|
16
18
|
export type { ExternalResponsesProviderOptions as BedrockResponsesProviderOptions } from '../text/responses-provider-options'
|
|
17
19
|
|
|
@@ -57,6 +59,7 @@ export class BedrockResponsesTextAdapter<
|
|
|
57
59
|
model,
|
|
58
60
|
'bedrock-responses',
|
|
59
61
|
new OpenAI(withBedrockDefaults(config, 'mantle', model)),
|
|
62
|
+
config,
|
|
60
63
|
)
|
|
61
64
|
}
|
|
62
65
|
}
|
package/src/adapters/text.ts
CHANGED
|
@@ -10,8 +10,10 @@ import type {
|
|
|
10
10
|
ResolveInputModalities,
|
|
11
11
|
ResolveProviderOptions,
|
|
12
12
|
} from '../model-meta'
|
|
13
|
+
import type { OpenAIBaseTextAdapterOptions } from '@tanstack/openai-base'
|
|
13
14
|
|
|
14
|
-
export interface BedrockTextConfig
|
|
15
|
+
export interface BedrockTextConfig
|
|
16
|
+
extends BedrockClientConfig, OpenAIBaseTextAdapterOptions {}
|
|
15
17
|
|
|
16
18
|
export type { ExternalTextProviderOptions as BedrockTextProviderOptions } from '../text/text-provider-options'
|
|
17
19
|
|
|
@@ -56,6 +58,7 @@ export class BedrockTextAdapter<
|
|
|
56
58
|
model,
|
|
57
59
|
'bedrock',
|
|
58
60
|
new OpenAI(withBedrockDefaults(config, undefined, model)),
|
|
61
|
+
config,
|
|
59
62
|
)
|
|
60
63
|
}
|
|
61
64
|
|
|
@@ -19,6 +19,10 @@ import type {
|
|
|
19
19
|
ToolResultContentBlock,
|
|
20
20
|
} from '@aws-sdk/client-bedrock-runtime'
|
|
21
21
|
import type { DocumentType } from '@smithy/types'
|
|
22
|
+
import type {
|
|
23
|
+
BedrockSystemPromptMetadata,
|
|
24
|
+
BedrockTextMetadata,
|
|
25
|
+
} from '../message-types'
|
|
22
26
|
|
|
23
27
|
// ---------------------------------------------------------------------------
|
|
24
28
|
// Helpers
|
|
@@ -182,6 +186,10 @@ function messageToBlocks(
|
|
|
182
186
|
for (const part of msg.content) {
|
|
183
187
|
const docIndex = isDocumentPart(part) ? ++docCounter.value : 0
|
|
184
188
|
blocks.push(contentPartToBlock(part, docIndex))
|
|
189
|
+
if (isTextPart(part)) {
|
|
190
|
+
const { cachePoint } = (part.metadata ?? {}) as BedrockTextMetadata
|
|
191
|
+
if (cachePoint) blocks.push({ cachePoint })
|
|
192
|
+
}
|
|
185
193
|
}
|
|
186
194
|
}
|
|
187
195
|
// null → no text blocks
|
|
@@ -231,7 +239,8 @@ function messageToBlocks(
|
|
|
231
239
|
/**
|
|
232
240
|
* Convert TanStack AI messages + system prompts into the Converse API format.
|
|
233
241
|
*
|
|
234
|
-
* - System prompts are lifted into `SystemContentBlock[]
|
|
242
|
+
* - System prompts are lifted into `SystemContentBlock[]`; a prompt whose
|
|
243
|
+
* `metadata.cachePoint` is set is followed by a `cachePoint` block.
|
|
235
244
|
* - `tool` role messages are remapped to `user` role `toolResult` blocks.
|
|
236
245
|
* - Consecutive messages with the same Converse role are merged (Converse
|
|
237
246
|
* requires strict user/assistant alternation).
|
|
@@ -241,9 +250,13 @@ export function toConverseMessages(
|
|
|
241
250
|
systemPrompts?: Array<SystemPrompt>,
|
|
242
251
|
): { system: Array<SystemContentBlock>; messages: Array<Message> } {
|
|
243
252
|
// Build system blocks (uses normalizeSystemPrompts for runtime validation)
|
|
244
|
-
const system: Array<SystemContentBlock> =
|
|
245
|
-
systemPrompts
|
|
246
|
-
|
|
253
|
+
const system: Array<SystemContentBlock> =
|
|
254
|
+
normalizeSystemPrompts<BedrockSystemPromptMetadata>(systemPrompts).flatMap(
|
|
255
|
+
(p) =>
|
|
256
|
+
p.metadata?.cachePoint
|
|
257
|
+
? [{ text: p.content }, { cachePoint: p.metadata.cachePoint }]
|
|
258
|
+
: [{ text: p.content }],
|
|
259
|
+
)
|
|
247
260
|
|
|
248
261
|
// Convert each ModelMessage to a Converse Message, merging same-role pairs
|
|
249
262
|
const converseMessages: Array<Message> = []
|
|
@@ -3,11 +3,14 @@ import type {
|
|
|
3
3
|
ToolConfiguration,
|
|
4
4
|
} from '@aws-sdk/client-bedrock-runtime'
|
|
5
5
|
import type { DocumentType } from '@smithy/types'
|
|
6
|
+
import type { BedrockCachePoint } from '../message-types'
|
|
6
7
|
|
|
7
8
|
export interface ConverseToolInput {
|
|
8
9
|
name: string
|
|
9
10
|
description?: string
|
|
10
11
|
inputSchema: unknown
|
|
12
|
+
/** Emit a `cachePoint` entry right after this tool. */
|
|
13
|
+
cachePoint?: BedrockCachePoint
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
export type ToolChoiceInput =
|
|
@@ -26,13 +29,16 @@ export function toToolConfig(
|
|
|
26
29
|
if (choice === 'none') return undefined
|
|
27
30
|
const toolChoice = mapChoice(choice)
|
|
28
31
|
return {
|
|
29
|
-
tools: tools.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
tools: tools.flatMap((t) => [
|
|
33
|
+
{
|
|
34
|
+
toolSpec: {
|
|
35
|
+
name: t.name,
|
|
36
|
+
...(t.description ? { description: t.description } : {}),
|
|
37
|
+
inputSchema: { json: t.inputSchema as DocumentType },
|
|
38
|
+
},
|
|
34
39
|
},
|
|
35
|
-
|
|
40
|
+
...(t.cachePoint ? [{ cachePoint: t.cachePoint }] : []),
|
|
41
|
+
]),
|
|
36
42
|
...(toolChoice ? { toolChoice } : {}),
|
|
37
43
|
}
|
|
38
44
|
}
|
package/src/index.ts
CHANGED
|
@@ -219,6 +219,9 @@ export {
|
|
|
219
219
|
type BedrockModelInputModalitiesByName,
|
|
220
220
|
} from './model-meta'
|
|
221
221
|
export type {
|
|
222
|
+
BedrockCachePoint,
|
|
223
|
+
BedrockSystemPromptMetadata,
|
|
224
|
+
BedrockToolMetadata,
|
|
222
225
|
BedrockMessageMetadataByModality,
|
|
223
226
|
BedrockTextMetadata,
|
|
224
227
|
BedrockImageMetadata,
|
package/src/message-types.ts
CHANGED
|
@@ -1,10 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Bedrock prompt-cache checkpoint. Bedrock caches everything in the request
|
|
3
|
+
* before this block and reads it back at the cache rate while the entry lives.
|
|
4
|
+
* A request may carry up to four. Omit `ttl` for the 5-minute default.
|
|
5
|
+
*
|
|
6
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html
|
|
7
|
+
*/
|
|
8
|
+
export interface BedrockCachePoint {
|
|
9
|
+
type: 'default'
|
|
10
|
+
ttl?: '5m' | '1h'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Metadata on a `systemPrompts` entry, read by the Converse adapter. */
|
|
14
|
+
export interface BedrockSystemPromptMetadata {
|
|
15
|
+
/** Place a cache checkpoint right after this system prompt. */
|
|
16
|
+
cachePoint?: BedrockCachePoint
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Metadata on a tool definition, read by the Converse adapter. */
|
|
20
|
+
export interface BedrockToolMetadata {
|
|
21
|
+
/** Place a cache checkpoint right after this tool's definition. */
|
|
22
|
+
cachePoint?: BedrockCachePoint
|
|
23
|
+
}
|
|
24
|
+
|
|
1
25
|
/**
|
|
2
26
|
* Bedrock content-part metadata by modality, used for type inference when
|
|
3
27
|
* constructing multimodal messages. Bedrock's OpenAI-compatible Chat
|
|
4
|
-
* Completions accepts the standard OpenAI image-detail hint;
|
|
5
|
-
*
|
|
28
|
+
* Completions accepts the standard OpenAI image-detail hint; the Converse
|
|
29
|
+
* adapter reads `cachePoint` on text parts.
|
|
6
30
|
*/
|
|
7
|
-
export interface BedrockTextMetadata {
|
|
31
|
+
export interface BedrockTextMetadata {
|
|
32
|
+
/** Place a cache checkpoint right after this text block (Converse only). */
|
|
33
|
+
cachePoint?: BedrockCachePoint
|
|
34
|
+
}
|
|
8
35
|
|
|
9
36
|
export interface BedrockImageMetadata {
|
|
10
37
|
/** Image processing detail: 'auto' (default), 'low', or 'high'. */
|