@tanstack/ai-bedrock 1.3.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/adapters/converse-text.js +6 -5
- package/dist/esm/adapters/converse-text.js.map +1 -1
- package/dist/esm/adapters/embedding.js +1 -2
- package/dist/esm/adapters/rerank.js +1 -2
- package/dist/esm/adapters/responses-text.js +1 -2
- package/dist/esm/adapters/text.js +1 -2
- package/dist/esm/api-compatibility.js +1 -2
- package/dist/esm/byok.js +1 -2
- package/dist/esm/converse/message-converter.js +1 -2
- package/dist/esm/converse/prompt-cache.js +1 -2
- package/dist/esm/converse/provider-options.d.ts +8 -3
- package/dist/esm/converse/reasoning.js +1 -2
- package/dist/esm/converse/stream-processor.js +20 -3
- package/dist/esm/converse/stream-processor.js.map +1 -1
- package/dist/esm/converse/structured-output.js +1 -2
- package/dist/esm/converse/tool-converter.js +1 -2
- package/dist/esm/converse/usage.js +1 -2
- package/dist/esm/index.js +3 -4
- package/dist/esm/model-catalog.generated.js +1 -2
- package/dist/esm/model-meta.js +1 -2
- package/dist/esm/utils/auth.js +1 -2
- package/dist/esm/utils/client.js +1 -2
- package/dist/esm/utils/openai-sigv4-fetch.js +1 -2
- package/dist/esm/utils/sigv4.js +1 -2
- package/package.json +5 -5
- package/src/adapters/converse-text.ts +6 -0
- package/src/converse/provider-options.ts +9 -3
- package/src/converse/stream-processor.ts +22 -2
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { importOptionalPeer, resolveBedrockAuth } from "../utils/auth.js";
|
|
2
|
-
import { BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES, BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING } from "../model-meta.js";
|
|
3
2
|
import { toConverseMessages, toolBlocksToText } from "../converse/message-converter.js";
|
|
4
3
|
import { toToolConfig } from "../converse/tool-converter.js";
|
|
5
|
-
import { buildConverseUsage } from "../converse/usage.js";
|
|
6
4
|
import { processConverseStream, throwIfConverseStreamError } from "../converse/stream-processor.js";
|
|
5
|
+
import { buildConverseUsage } from "../converse/usage.js";
|
|
7
6
|
import { converseThinking } from "../converse/reasoning.js";
|
|
7
|
+
import { BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES, BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING } from "../model-meta.js";
|
|
8
8
|
import { addPromptCachePoints } from "../converse/prompt-cache.js";
|
|
9
9
|
import { buildStructuredToolConfig } from "../converse/structured-output.js";
|
|
10
10
|
import { CONTEXT_OVERFLOW_CODE, EventType, convertSchemaToJsonSchema } from "@tanstack/ai";
|
|
11
11
|
import { BaseTextAdapter } from "@tanstack/ai/adapters";
|
|
12
12
|
import { toRunErrorPayload } from "@tanstack/ai/adapter-internals";
|
|
13
|
-
|
|
13
|
+
// src/adapters/converse-text.ts
|
|
14
14
|
var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
15
15
|
kind = "text";
|
|
16
16
|
name = "bedrock-converse";
|
|
@@ -440,7 +440,9 @@ var BedrockConverseTextAdapter = class extends BaseTextAdapter {
|
|
|
440
440
|
...system.length > 0 && { system },
|
|
441
441
|
...toolConfig && { toolConfig },
|
|
442
442
|
...inferenceConfig && { inferenceConfig },
|
|
443
|
-
...additionalModelRequestFields && { additionalModelRequestFields }
|
|
443
|
+
...additionalModelRequestFields && { additionalModelRequestFields },
|
|
444
|
+
...modelOptions?.requestMetadata !== void 0 && { requestMetadata: modelOptions.requestMetadata },
|
|
445
|
+
...modelOptions?.serviceTier !== void 0 && { serviceTier: { type: modelOptions.serviceTier } }
|
|
444
446
|
};
|
|
445
447
|
return addPromptCachePoints(this.model, input, options.promptCache);
|
|
446
448
|
}
|
|
@@ -541,7 +543,6 @@ function createBedrockConverse(model, apiKey, config) {
|
|
|
541
543
|
apiKey
|
|
542
544
|
}, model);
|
|
543
545
|
}
|
|
544
|
-
//#endregion
|
|
545
546
|
export { BedrockConverseTextAdapter, createBedrockConverse };
|
|
546
547
|
|
|
547
548
|
//# sourceMappingURL=converse-text.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"converse-text.js","names":[],"sources":["../../../src/adapters/converse-text.ts"],"sourcesContent":["import {\n CONTEXT_OVERFLOW_CODE,\n EventType,\n convertSchemaToJsonSchema,\n} from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport { importOptionalPeer, resolveBedrockAuth } from '../utils/auth'\nimport {\n toConverseMessages,\n toolBlocksToText,\n} from '../converse/message-converter'\nimport { toToolConfig } from '../converse/tool-converter'\nimport {\n processConverseStream,\n throwIfConverseStreamError,\n} from '../converse/stream-processor'\nimport { buildConverseUsage } from '../converse/usage'\nimport { converseThinking } from '../converse/reasoning'\nimport {\n BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES,\n BEDROCK_MODEL_INPUT_MODALITIES,\n BEDROCK_MODEL_REASONING,\n} from '../model-meta'\nimport { addPromptCachePoints } from '../converse/prompt-cache'\nimport {\n STRUCTURED_TOOL_NAME,\n buildStructuredToolConfig,\n} from '../converse/structured-output'\nimport type { ResolvedBedrockAuth } from '../utils/auth'\nimport type { ConverseToolInput } from '../converse/tool-converter'\nimport type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime'\nimport type {\n BedrockRuntimeClient,\n BedrockRuntimeClientConfig,\n ContentBlock,\n ConverseCommandInput,\n ConverseCommandOutput,\n ConverseStreamCommandInput,\n ConverseStreamOutput,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type { MiddlewareStack } from '@smithy/types'\nimport type {\n JSONSchema,\n Modality,\n AdapterYieldChunk,\n TextOptions,\n TokenUsage,\n Tool,\n} from '@tanstack/ai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type {\n BedrockMessageMetadataByModality,\n BedrockSystemPromptMetadata,\n BedrockToolMetadata,\n} from '../message-types'\nimport type {\n BedrockConverseModels,\n BedrockModelReasoningByName,\n ResolveConverseProviderOptions,\n ResolveInputModalities,\n} from '../model-meta'\n\n/** Config for the Converse adapter — same client config as the chat adapter. */\nexport interface BedrockConverseConfig extends BedrockClientConfig {}\n\n/**\n * Bedrock Converse text adapter. Wires the Converse translation modules (message\n * converter, tool converter, stream processor, structured-output forced-tool\n * builder) onto `@tanstack/ai`'s `BaseTextAdapter` and the\n * `@aws-sdk/client-bedrock-runtime` `BedrockRuntimeClient`.\n *\n * The success-path AG-UI lifecycle (`RUN_STARTED`..`RUN_FINISHED`) is owned by\n * `processConverseStream`; this adapter only owns the catch/`RUN_ERROR` path,\n * mirroring openai-base's `chatStream`.\n *\n * The actual SDK calls live behind two protected seams (`sendStream` / `send`)\n * so tests can subclass and inject canned Converse SDK shapes without a real\n * AWS request.\n */\n/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */\ntype ResolveReasoning<TModel extends string> =\n TModel extends keyof BedrockModelReasoningByName\n ? BedrockModelReasoningByName[TModel]\n : never\n\nexport class BedrockConverseTextAdapter<\n TModel extends BedrockConverseModels,\n // Constraint mirrors the chat adapter (text.ts): the base parameterises\n // `TProviderOptions extends Record<string, any>`, and our default\n // `ResolveConverseProviderOptions<TModel>` resolves to an interface lacking an\n // implicit index signature — which `Record<string, unknown>` would reject but\n // `Record<string, any>` accepts. Confined to the generic constraint (the\n // established adapter pattern) — no value `as` cast is introduced.\n TProviderOptions extends Record<string, any> =\n ResolveConverseProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n BedrockMessageMetadataByModality,\n // TToolCapabilities — Converse has no per-model tool-capability table; the\n // base default.\n ReadonlyArray<string>,\n // TToolCallMetadata — Converse has no tool-call metadata round-tripping.\n unknown,\n // TSystemPromptMetadata — narrows `systemPrompts[i].metadata` at the chat()\n // call site so users get `cachePoint` autocomplete.\n BedrockSystemPromptMetadata,\n ResolveReasoning<TModel>\n> {\n override readonly kind = 'text' as const\n override readonly name = 'bedrock-converse' as const\n override readonly api = 'bedrock-converse-stream'\n override readonly provider = 'amazon-bedrock'\n override readonly inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model]\n\n private sourceMetadata(model: string) {\n return {\n tanstack: { source: { provider: this.provider, api: this.api, model } },\n }\n }\n private clientPromise?: Promise<BedrockRuntimeClient>\n private readonly clientConfig: BedrockConverseConfig\n\n constructor(config: BedrockConverseConfig, model: TModel) {\n super({}, model)\n // Defer client construction and auth resolution: the AWS SDK is Node/\n // server-only, so we must not pull it into the static graph here. The\n // client (and its dynamic import) is built lazily on first SDK call.\n this.clientConfig = config\n }\n\n /**\n * Dynamically import `@aws-sdk/client-bedrock-runtime`. The specifier is held\n * in a variable (not a string literal) so bundler dep scanners (e.g. Vite/\n * esbuild optimizeDeps) cannot statically discover the AWS SDK and try to\n * pre-bundle it for the browser — it would fail on the SDK's Node-only\n * `fromTokenFile` export chain. The SDK is Node/server-only and is only\n * reached on a real request. `typeof import(...)` is a type-only reference\n * (erased at emit) so the imported members keep full typing.\n */\n protected importBedrockRuntime(): Promise<typeof BedrockRuntime> {\n const mod = '@aws-sdk/client-bedrock-runtime'\n return importOptionalPeer<typeof BedrockRuntime>(mod)\n }\n\n /**\n * Lazily construct the `BedrockRuntimeClient`. The dynamic import keeps\n * `@aws-sdk/client-bedrock-runtime` out of the static/browser graph and\n * defers `resolveBedrockAuth` until a real request is made.\n */\n protected async getClient(): Promise<BedrockRuntimeClient> {\n if (!this.clientPromise) {\n this.clientPromise = (async () => {\n const { BedrockRuntimeClient } = await this.importBedrockRuntime()\n const region = this.clientConfig.region ?? 'us-east-1'\n const resolved = resolveBedrockAuth(\n {\n apiKey: this.clientConfig.apiKey,\n region,\n auth: this.clientConfig.auth,\n },\n 'runtime',\n )\n const client = new BedrockRuntimeClient(\n this.buildClientConfig(resolved, region, this.clientConfig.baseURL),\n )\n const defaultHeaders = this.clientConfig.defaultHeaders\n if (defaultHeaders) {\n // `build` runs before SigV4 signing, so gateway headers are signed\n // along with the rest of the request.\n client.middlewareStack.add(\n (next) => (args) => {\n const request = args.request\n if (\n typeof request === 'object' &&\n request !== null &&\n 'headers' in request &&\n typeof request.headers === 'object' &&\n request.headers !== null\n ) {\n Object.assign(request.headers, defaultHeaders)\n }\n return next(args)\n },\n { step: 'build', name: 'tanstackDefaultHeaders' },\n )\n }\n return client\n })().catch((error: unknown) => {\n // Don't cache a rejected promise — clear it so a later call can retry\n // (e.g. after a transient import failure or fixed auth config).\n this.clientPromise = undefined\n throw error\n })\n }\n return this.clientPromise\n }\n\n /**\n * Map resolved auth + endpoint to a `BedrockRuntimeClientConfig`.\n *\n * Recent `@aws-sdk/client-bedrock-runtime` exposes a first-class `token`\n * config field for Bedrock API-key bearer auth. But the client's default\n * auth-scheme order is SigV4 first, then bearer — so passing `token` alone is\n * not enough: the SDK still resolves SigV4 and throws \"Could not load\n * credentials from any providers\". Pinning `authSchemePreference` to the\n * bearer scheme makes the API key actually get used. SigV4 uses the AWS\n * credential provider chain and the default scheme order.\n */\n protected buildClientConfig(\n resolved: ResolvedBedrockAuth,\n region: string,\n endpoint: string | undefined,\n ): BedrockRuntimeClientConfig {\n if (resolved.kind === 'bearer') {\n return {\n region,\n token: { token: resolved.token },\n authSchemePreference: ['httpBearerAuth'],\n ...(endpoint ? { endpoint } : {}),\n }\n }\n return {\n region: resolved.region,\n credentials: resolved.credentials,\n ...(endpoint ? { endpoint } : {}),\n }\n }\n\n // ---------------------------------------------------------------------------\n // SDK seams (overridden in tests so no real AWS call happens)\n // ---------------------------------------------------------------------------\n\n protected async sendStream(\n input: ConverseStreamCommandInput,\n signal?: AbortSignal | null,\n ): Promise<AsyncIterable<ConverseStreamOutput>> {\n const { ConverseStreamCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const command = new ConverseStreamCommand(input)\n addRestoreDocuments(command, input)\n const res = await client.send(command, { abortSignal: signal ?? undefined })\n if (!res.stream) {\n throw new Error('Bedrock Converse: empty stream response')\n }\n return res.stream\n }\n\n protected async send(\n input: ConverseCommandInput,\n signal?: AbortSignal | null,\n ): Promise<ConverseCommandOutput> {\n const { ConverseCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const command = new ConverseCommand(input)\n addRestoreDocuments(command, input)\n return client.send(command, { abortSignal: signal ?? undefined })\n }\n\n // ---------------------------------------------------------------------------\n // Public adapter surface\n // ---------------------------------------------------------------------------\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n try {\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const input = this.buildInput(options)\n // Bedrock rejects toolUse and toolResult blocks without a toolConfig,\n // so a request with no tools sends its tool history as text.\n if (!input.toolConfig && input.messages) {\n input.messages = toolBlocksToText(input.messages)\n }\n const stream = await this.sendStream(input, options.request?.signal)\n for await (const chunk of processConverseStream(\n stream,\n () => this.generateId(),\n {\n threadId: options.threadId,\n parentRunId: options.parentRunId,\n model: options.model,\n },\n )) {\n yield chunk.type === EventType.RUN_STARTED ||\n chunk.type === EventType.RUN_FINISHED\n ? { ...chunk, metadata: this.sourceMetadata(options.model) }\n : chunk\n }\n } catch (error: unknown) {\n const errorPayload = converseRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes` (AG-UI's `RunErrorEvent.code` is optional).\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(options.model),\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Structured output via the forced-tool strategy. Converse has no native\n * json_schema response_format, so we force a single tool whose input schema\n * is the requested output schema and read the model's `toolUse.input` back as\n * the structured result.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n try {\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n // Claude rejects a forced tool while thinking is on, so the forced\n // structured-output tool goes without `chat({ reasoning })`.\n const { reasoning: _reasoning, ...requestOptions } = chatOptions\n const input: ConverseCommandInput = {\n ...this.buildInput(requestOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const res = await this.send(input, chatOptions.request?.signal)\n const usage = res.usage && buildConverseUsage(res.usage)\n // A forced tool call cut off at the output cap carries partial input,\n // which would read like a schema failure (issue #1426).\n if (res.stopReason === 'max_tokens') {\n throw Object.assign(\n new Error(\n `${this.name}.structuredOutput: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`,\n ),\n usage && { usage },\n )\n }\n const structured = extractStructuredToolInput(res)\n if (structured === undefined) {\n throw Object.assign(\n new Error(\n `${this.name}.structuredOutput: response contained no forced-tool output`,\n ),\n usage && { usage },\n )\n }\n return {\n data: structured,\n rawText: JSON.stringify(structured),\n ...(usage && { usage }),\n }\n } catch (error: unknown) {\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Streaming structured output. Same forced-tool strategy as\n * `structuredOutput`, but streamed: the forced tool's `toolUse.input` JSON\n * fragments are accumulated from the Converse stream and a terminal\n * `CUSTOM 'structured-output.complete'` event carries `{ object, raw }`,\n * mirroring openai-base's `structuredOutputStream` contract exactly.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n const { chatOptions, outputSchema } = options\n const runId = this.generateId()\n const threadId = chatOptions.threadId\n const messageId = this.generateId()\n\n let hasEmittedRunStarted = false\n let hasEmittedTextMessageStart = false\n let accumulatedRaw = ''\n let finishReason: 'stop' | 'length' | 'content_filter' = 'stop'\n // Usage arrives on the trailing `metadata` event, after the finish signal,\n // so it is captured during iteration and folded into RUN_FINISHED below.\n let usage: TokenUsage | undefined\n\n try {\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n // Claude rejects a forced tool while thinking is on, so the forced\n // structured-output tool goes without `chat({ reasoning })`.\n const { reasoning: _reasoning, ...requestOptions } = chatOptions\n const input: ConverseStreamCommandInput = {\n ...this.buildInput(requestOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const stream = await this.sendStream(input, chatOptions.request?.signal)\n\n // The forced tool streams its `input` as partial-JSON fragments inside\n // `contentBlockDelta.delta.toolUse.input`. We surface them as\n // TEXT_MESSAGE_CONTENT deltas (raw JSON text), matching openai-base which\n // carries the structured JSON as text deltas.\n for await (const ev of stream) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n // Surface in-band server/throttle/validation errors instead of\n // letting them fall through and masquerade as an empty response.\n throwIfConverseStreamError(ev)\n\n if ('contentBlockDelta' in ev) {\n const delta = ev.contentBlockDelta?.delta\n const fragment =\n delta && 'toolUse' in delta ? delta.toolUse?.input : undefined\n if (fragment !== undefined) {\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n accumulatedRaw += fragment\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: fragment,\n content: accumulatedRaw,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // The forced structured-output tool produces stopReason 'tool_use' on\n // success, but that's an implementation detail — a cleanly-completed\n // structured run reports 'stop', matching openai-base's contract.\n finishReason =\n stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered' ||\n stopReason === 'guardrail_intervened'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n\n // Same truncation check as `structuredOutput()`: report the token limit\n // before the empty-content and parse errors (issue #1426).\n if (finishReason === 'length') {\n const message = `${this.name}.structuredOutputStream: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message,\n code: 'max_tokens',\n error: { message, code: 'max_tokens' },\n ...(usage && { usage }),\n }\n return\n }\n\n if (accumulatedRaw.length === 0) {\n const blocked = finishReason === 'content_filter'\n const message = blocked\n ? `${this.name}.structuredOutputStream: the response was blocked by a content filter`\n : `${this.name}.structuredOutputStream: response contained no content`\n const code = blocked ? 'content_filter' : 'empty-response'\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message,\n code,\n error: { message, code },\n ...(usage && { usage }),\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedRaw)\n } catch {\n const blocked = finishReason === 'content_filter'\n const message = blocked\n ? `${this.name}.structuredOutputStream: the response was blocked by a content filter`\n : `Failed to parse structured output as JSON. Content: ${accumulatedRaw.slice(0, 200)}${accumulatedRaw.length > 200 ? '...' : ''}`\n const code = blocked ? 'content_filter' : 'parse-error'\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message,\n code,\n error: {\n message: blocked\n ? message\n : 'Failed to parse structured output as JSON',\n code,\n },\n ...(usage && { usage }),\n }\n return\n }\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: parsed,\n raw: accumulatedRaw,\n },\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n finishReason,\n ...(usage && { usage }),\n }\n } catch (error: unknown) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n const errorPayload = converseRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n ...(usage && { usage }),\n }\n }\n }\n\n /**\n * Converse sends `tools` and a forced structured-output tool via two separate\n * mechanisms, never together. Declaring `false` makes the engine run the\n * agent loop without `outputSchema` and finalize via `structuredOutput` /\n * `structuredOutputStream`.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return false\n }\n\n // ---------------------------------------------------------------------------\n // Request construction\n // ---------------------------------------------------------------------------\n\n /**\n * Translate `TextOptions` into a `ConverseCommandInput`. Shared by chatStream,\n * structuredOutput, and structuredOutputStream (the latter two override\n * `toolConfig` with the forced structured tool afterwards).\n */\n protected buildInput(\n options: TextOptions<TProviderOptions>,\n ): ConverseCommandInput {\n const { system, messages } = toConverseMessages(\n options.messages,\n options.systemPrompts,\n { model: options.model, provider: this.provider },\n )\n\n // Sampling options live on `modelOptions` (typed as the narrowed\n // `BedrockConverseProviderOptions`, which surfaces the OpenAI Chat\n // Completions field names); translate them into Converse's `inferenceConfig`,\n // which uses AWS-native camelCase keys.\n const modelOptions = options.modelOptions\n const temperature = modelOptions?.temperature\n const topP = modelOptions?.top_p\n // `chat({ reasoning })`: Claude's thinking fields. Budget thinking needs\n // `maxTokens` above the budget, so a smaller or missing value grows.\n const { additionalModelRequestFields, minMaxTokens } = converseThinking(\n this.model,\n options.reasoning,\n BEDROCK_MODEL_REASONING[this.model],\n )\n\n // `chat({ toolChoice })`. Converse has no `none` choice, so `none` sends\n // no tool config, and `chatStream` sends any tool history as text. Claude\n // rejects a forced tool while thinking is on (only Claude gets thinking\n // fields), and some Claude models reject it on every request. Then a\n // forced choice falls back to auto.\n const canForceTool =\n additionalModelRequestFields === undefined &&\n !BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES.some((name) =>\n this.model.includes(name),\n )\n const toolChoice =\n options.toolChoice === 'none' || canForceTool\n ? options.toolChoice\n : 'auto'\n const toolConfig = options.tools\n ? toToolConfig(convertTools(options.tools), toolChoice)\n : undefined\n\n const requestedMaxTokens = modelOptions?.max_completion_tokens\n const maxTokens =\n minMaxTokens !== undefined &&\n (requestedMaxTokens == null || requestedMaxTokens < minMaxTokens)\n ? minMaxTokens\n : requestedMaxTokens\n const stop = modelOptions?.stop\n const stopSequences =\n stop == null ? undefined : Array.isArray(stop) ? stop : [stop]\n\n const inferenceConfig =\n temperature != null ||\n topP != null ||\n maxTokens != null ||\n stopSequences != null\n ? {\n ...(temperature != null && { temperature }),\n ...(topP != null && { topP }),\n ...(maxTokens != null && { maxTokens }),\n ...(stopSequences != null && { stopSequences }),\n }\n : undefined\n\n const input: ConverseCommandInput = {\n modelId: this.model,\n messages,\n ...(system.length > 0 && { system }),\n ...(toolConfig && { toolConfig }),\n ...(inferenceConfig && { inferenceConfig }),\n ...(additionalModelRequestFields && { additionalModelRequestFields }),\n }\n return addPromptCachePoints(this.model, input, options.promptCache)\n }\n}\n\n/**\n * Convert TanStack `Tool[]` to the Converse tool-converter input shape. Reuses\n * the SAME `convertSchemaToJsonSchema` the other adapters use so the Converse\n * tool input schemas match what every other provider sends.\n */\n/**\n * The text Bedrock models use for a context overflow. Converse has no error\n * type for it: it is a `ValidationException` like every bad request, and over\n * a stream the SDK does not keep that name. So the message is the only signal.\n * Each model family has its own text.\n */\nconst CONTEXT_OVERFLOW_MESSAGE =\n /Input Tokens Exceeded|maximum context length|Input is too long for requested model|prompt is too long/\n\n/**\n * `toRunErrorPayload`, with `code` set to `CONTEXT_OVERFLOW_CODE` for a\n * context overflow. A `ThrottlingException` is never an overflow, whatever\n * its text says about tokens.\n */\nfunction converseRunErrorPayload(\n error: unknown,\n fallbackMessage: string,\n): { message: string; code: string | undefined } {\n const payload = toRunErrorPayload(error, fallbackMessage)\n const throttled =\n error instanceof Error && error.name === 'ThrottlingException'\n return !throttled && CONTEXT_OVERFLOW_MESSAGE.test(payload.message)\n ? { ...payload, code: CONTEXT_OVERFLOW_CODE }\n : payload\n}\n\nfunction convertTools(tools: Array<Tool>): Array<ConverseToolInput> {\n return tools.map((tool) => {\n const inputSchema: JSONSchema = convertSchemaToJsonSchema(\n tool.inputSchema,\n ) ?? { type: 'object', properties: {}, required: [] }\n const { cachePoint }: BedrockToolMetadata = tool.metadata ?? {}\n return {\n name: tool.name,\n description: tool.description,\n inputSchema,\n ...(cachePoint && { cachePoint }),\n }\n })\n}\n\n/**\n * Find the forced structured-output tool's `input` in a non-streaming Converse\n * response. SDK-boundary narrowing only — `ConverseOutput` is a tagged union\n * (`{ message }`) and a tool-use block is `{ toolUse: { input } }`.\n */\nfunction extractStructuredToolInput(\n res: ConverseCommandOutput,\n): unknown | undefined {\n const message =\n res.output && 'message' in res.output ? res.output.message : undefined\n const content: Array<ContentBlock> = message?.content ?? []\n for (const block of content) {\n if ('toolUse' in block && block.toolUse) {\n // Only accept the forced structured tool (an unnamed block is allowed,\n // since the forced tool is the only one configured). A differently-named\n // tool-use block is a hallucinated/leftover call whose arbitrary input\n // must not be returned as the validated result — leave it to the caller's\n // `throw` so the failure is accurate instead of silently wrong.\n if (\n block.toolUse.name === STRUCTURED_TOOL_NAME ||\n block.toolUse.name === undefined\n ) {\n return block.toolUse.input\n }\n }\n }\n return undefined\n}\n\n/** The parts of a Converse request that hold documents the adapter fills from user data. */\ntype ConverseDocuments = Pick<ConverseCommandInput, 'messages' | 'toolConfig'>\n\nfunction hasProtoKey(value: unknown): boolean {\n if (typeof value !== 'object' || value === null) return false\n return (\n Object.hasOwn(value, '__proto__') || Object.values(value).some(hasProtoKey)\n )\n}\n\n/**\n * The AWS SDK copies a document (tool input, tool input schema) with\n * `out[key] = value`. For an own key named `__proto__`, the assignment goes\n * to the `__proto__` setter and the key is lost. This puts each such\n * document back in the serialized body. `JSON.stringify` keeps own keys.\n */\nfunction restoreDocuments(request: unknown, input: ConverseDocuments): void {\n const documents = [\n ...(input.messages ?? []).flatMap((message) =>\n (message.content ?? []).map((block) => block.toolUse?.input),\n ),\n ...(input.toolConfig?.tools ?? []).map(\n (tool) => tool.toolSpec?.inputSchema?.json,\n ),\n ]\n // Skip the parse and re-serialize of the body, which can be large (for\n // example with image bytes), unless a document has the key.\n if (!documents.some(hasProtoKey)) return\n if (\n typeof request !== 'object' ||\n request === null ||\n !('body' in request) ||\n typeof request.body !== 'string'\n ) {\n throw new Error(\n 'Bedrock Converse: cannot keep a `__proto__` key because the request body is not a JSON string',\n )\n }\n // The SDK serialized this body from `input` one step earlier, so the two\n // line up by position.\n const body: ConverseDocuments = JSON.parse(request.body)\n body.messages?.forEach((message, i) =>\n message.content?.forEach((block, j) => {\n const source = input.messages?.[i]?.content?.[j]?.toolUse?.input\n if (block.toolUse && hasProtoKey(source)) block.toolUse.input = source\n }),\n )\n body.toolConfig?.tools?.forEach((tool, i) => {\n const source = input.toolConfig?.tools?.[i]?.toolSpec?.inputSchema?.json\n if (tool.toolSpec?.inputSchema && hasProtoKey(source))\n tool.toolSpec.inputSchema.json = source\n })\n request.body = JSON.stringify(body)\n}\n\n/**\n * Adds {@link restoreDocuments} to a command. It runs at `build` with high\n * priority, before the SDK sets Content-Length and signs the body.\n */\nfunction addRestoreDocuments<TInput extends object, TOutput extends object>(\n command: { middlewareStack: MiddlewareStack<TInput, TOutput> },\n input: ConverseDocuments,\n): void {\n command.middlewareStack.add(\n (next) => (args) => {\n restoreDocuments(args.request, input)\n return next(args)\n },\n { step: 'build', name: 'tanstackRestoreDocuments', priority: 'high' },\n )\n}\n\n/** Converse adapter with an explicit API key (low-level; mirrors createBedrockChat). */\nexport function createBedrockConverse<TModel extends BedrockConverseModels>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockConverseConfig, 'apiKey'>,\n): BedrockConverseTextAdapter<TModel> {\n return new BedrockConverseTextAdapter({ ...config, apiKey }, model)\n}\n"],"mappings":";;;;;;;;;;;;;AA0FA,IAAa,6BAAb,cAYU,gBAcR;CACA,OAAyB;CACzB,OAAyB;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SAAS,oBACP,SACA,OACM;CACN,QAAQ,gBAAgB,KACrB,UAAU,SAAS;EAClB,iBAAiB,KAAK,SAAS,KAAK;EACpC,OAAO,KAAK,IAAI;CAClB,GACA;EAAE,MAAM;EAAS,MAAM;EAA4B,UAAU;CAAO,CACtE;AACF;;AAGA,SAAgB,sBACd,OACA,QACA,QACoC;CACpC,OAAO,IAAI,2BAA2B;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AACpE"}
|
|
1
|
+
{"version":3,"file":"converse-text.js","names":[],"sources":["../../../src/adapters/converse-text.ts"],"sourcesContent":["import {\n CONTEXT_OVERFLOW_CODE,\n EventType,\n convertSchemaToJsonSchema,\n} from '@tanstack/ai'\nimport { BaseTextAdapter } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport { importOptionalPeer, resolveBedrockAuth } from '../utils/auth'\nimport {\n toConverseMessages,\n toolBlocksToText,\n} from '../converse/message-converter'\nimport { toToolConfig } from '../converse/tool-converter'\nimport {\n processConverseStream,\n throwIfConverseStreamError,\n} from '../converse/stream-processor'\nimport { buildConverseUsage } from '../converse/usage'\nimport { converseThinking } from '../converse/reasoning'\nimport {\n BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES,\n BEDROCK_MODEL_INPUT_MODALITIES,\n BEDROCK_MODEL_REASONING,\n} from '../model-meta'\nimport { addPromptCachePoints } from '../converse/prompt-cache'\nimport {\n STRUCTURED_TOOL_NAME,\n buildStructuredToolConfig,\n} from '../converse/structured-output'\nimport type { ResolvedBedrockAuth } from '../utils/auth'\nimport type { ConverseToolInput } from '../converse/tool-converter'\nimport type * as BedrockRuntime from '@aws-sdk/client-bedrock-runtime'\nimport type {\n BedrockRuntimeClient,\n BedrockRuntimeClientConfig,\n ContentBlock,\n ConverseCommandInput,\n ConverseCommandOutput,\n ConverseStreamCommandInput,\n ConverseStreamOutput,\n} from '@aws-sdk/client-bedrock-runtime'\nimport type { MiddlewareStack } from '@smithy/types'\nimport type {\n JSONSchema,\n Modality,\n AdapterYieldChunk,\n TextOptions,\n TokenUsage,\n Tool,\n} from '@tanstack/ai'\nimport type {\n StructuredOutputOptions,\n StructuredOutputResult,\n} from '@tanstack/ai/adapters'\nimport type { BedrockClientConfig } from '../utils/client'\nimport type {\n BedrockMessageMetadataByModality,\n BedrockSystemPromptMetadata,\n BedrockToolMetadata,\n} from '../message-types'\nimport type {\n BedrockConverseModels,\n BedrockModelReasoningByName,\n ResolveConverseProviderOptions,\n ResolveInputModalities,\n} from '../model-meta'\n\n/** Config for the Converse adapter — same client config as the chat adapter. */\nexport interface BedrockConverseConfig extends BedrockClientConfig {}\n\n/**\n * Bedrock Converse text adapter. Wires the Converse translation modules (message\n * converter, tool converter, stream processor, structured-output forced-tool\n * builder) onto `@tanstack/ai`'s `BaseTextAdapter` and the\n * `@aws-sdk/client-bedrock-runtime` `BedrockRuntimeClient`.\n *\n * The success-path AG-UI lifecycle (`RUN_STARTED`..`RUN_FINISHED`) is owned by\n * `processConverseStream`; this adapter only owns the catch/`RUN_ERROR` path,\n * mirroring openai-base's `chatStream`.\n *\n * The actual SDK calls live behind two protected seams (`sendStream` / `send`)\n * so tests can subclass and inject canned Converse SDK shapes without a real\n * AWS request.\n */\n/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */\ntype ResolveReasoning<TModel extends string> =\n TModel extends keyof BedrockModelReasoningByName\n ? BedrockModelReasoningByName[TModel]\n : never\n\nexport class BedrockConverseTextAdapter<\n TModel extends BedrockConverseModels,\n // Constraint mirrors the chat adapter (text.ts): the base parameterises\n // `TProviderOptions extends Record<string, any>`, and our default\n // `ResolveConverseProviderOptions<TModel>` resolves to an interface lacking an\n // implicit index signature — which `Record<string, unknown>` would reject but\n // `Record<string, any>` accepts. Confined to the generic constraint (the\n // established adapter pattern) — no value `as` cast is introduced.\n TProviderOptions extends Record<string, any> =\n ResolveConverseProviderOptions<TModel>,\n TInputModalities extends ReadonlyArray<Modality> =\n ResolveInputModalities<TModel>,\n> extends BaseTextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n BedrockMessageMetadataByModality,\n // TToolCapabilities — Converse has no per-model tool-capability table; the\n // base default.\n ReadonlyArray<string>,\n // TToolCallMetadata — Converse has no tool-call metadata round-tripping.\n unknown,\n // TSystemPromptMetadata — narrows `systemPrompts[i].metadata` at the chat()\n // call site so users get `cachePoint` autocomplete.\n BedrockSystemPromptMetadata,\n ResolveReasoning<TModel>\n> {\n override readonly kind = 'text' as const\n override readonly name = 'bedrock-converse' as const\n override readonly api = 'bedrock-converse-stream'\n override readonly provider = 'amazon-bedrock'\n override readonly inputModalities = BEDROCK_MODEL_INPUT_MODALITIES[this.model]\n\n private sourceMetadata(model: string) {\n return {\n tanstack: { source: { provider: this.provider, api: this.api, model } },\n }\n }\n private clientPromise?: Promise<BedrockRuntimeClient>\n private readonly clientConfig: BedrockConverseConfig\n\n constructor(config: BedrockConverseConfig, model: TModel) {\n super({}, model)\n // Defer client construction and auth resolution: the AWS SDK is Node/\n // server-only, so we must not pull it into the static graph here. The\n // client (and its dynamic import) is built lazily on first SDK call.\n this.clientConfig = config\n }\n\n /**\n * Dynamically import `@aws-sdk/client-bedrock-runtime`. The specifier is held\n * in a variable (not a string literal) so bundler dep scanners (e.g. Vite/\n * esbuild optimizeDeps) cannot statically discover the AWS SDK and try to\n * pre-bundle it for the browser — it would fail on the SDK's Node-only\n * `fromTokenFile` export chain. The SDK is Node/server-only and is only\n * reached on a real request. `typeof import(...)` is a type-only reference\n * (erased at emit) so the imported members keep full typing.\n */\n protected importBedrockRuntime(): Promise<typeof BedrockRuntime> {\n const mod = '@aws-sdk/client-bedrock-runtime'\n return importOptionalPeer<typeof BedrockRuntime>(mod)\n }\n\n /**\n * Lazily construct the `BedrockRuntimeClient`. The dynamic import keeps\n * `@aws-sdk/client-bedrock-runtime` out of the static/browser graph and\n * defers `resolveBedrockAuth` until a real request is made.\n */\n protected async getClient(): Promise<BedrockRuntimeClient> {\n if (!this.clientPromise) {\n this.clientPromise = (async () => {\n const { BedrockRuntimeClient } = await this.importBedrockRuntime()\n const region = this.clientConfig.region ?? 'us-east-1'\n const resolved = resolveBedrockAuth(\n {\n apiKey: this.clientConfig.apiKey,\n region,\n auth: this.clientConfig.auth,\n },\n 'runtime',\n )\n const client = new BedrockRuntimeClient(\n this.buildClientConfig(resolved, region, this.clientConfig.baseURL),\n )\n const defaultHeaders = this.clientConfig.defaultHeaders\n if (defaultHeaders) {\n // `build` runs before SigV4 signing, so gateway headers are signed\n // along with the rest of the request.\n client.middlewareStack.add(\n (next) => (args) => {\n const request = args.request\n if (\n typeof request === 'object' &&\n request !== null &&\n 'headers' in request &&\n typeof request.headers === 'object' &&\n request.headers !== null\n ) {\n Object.assign(request.headers, defaultHeaders)\n }\n return next(args)\n },\n { step: 'build', name: 'tanstackDefaultHeaders' },\n )\n }\n return client\n })().catch((error: unknown) => {\n // Don't cache a rejected promise — clear it so a later call can retry\n // (e.g. after a transient import failure or fixed auth config).\n this.clientPromise = undefined\n throw error\n })\n }\n return this.clientPromise\n }\n\n /**\n * Map resolved auth + endpoint to a `BedrockRuntimeClientConfig`.\n *\n * Recent `@aws-sdk/client-bedrock-runtime` exposes a first-class `token`\n * config field for Bedrock API-key bearer auth. But the client's default\n * auth-scheme order is SigV4 first, then bearer — so passing `token` alone is\n * not enough: the SDK still resolves SigV4 and throws \"Could not load\n * credentials from any providers\". Pinning `authSchemePreference` to the\n * bearer scheme makes the API key actually get used. SigV4 uses the AWS\n * credential provider chain and the default scheme order.\n */\n protected buildClientConfig(\n resolved: ResolvedBedrockAuth,\n region: string,\n endpoint: string | undefined,\n ): BedrockRuntimeClientConfig {\n if (resolved.kind === 'bearer') {\n return {\n region,\n token: { token: resolved.token },\n authSchemePreference: ['httpBearerAuth'],\n ...(endpoint ? { endpoint } : {}),\n }\n }\n return {\n region: resolved.region,\n credentials: resolved.credentials,\n ...(endpoint ? { endpoint } : {}),\n }\n }\n\n // ---------------------------------------------------------------------------\n // SDK seams (overridden in tests so no real AWS call happens)\n // ---------------------------------------------------------------------------\n\n protected async sendStream(\n input: ConverseStreamCommandInput,\n signal?: AbortSignal | null,\n ): Promise<AsyncIterable<ConverseStreamOutput>> {\n const { ConverseStreamCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const command = new ConverseStreamCommand(input)\n addRestoreDocuments(command, input)\n const res = await client.send(command, { abortSignal: signal ?? undefined })\n if (!res.stream) {\n throw new Error('Bedrock Converse: empty stream response')\n }\n return res.stream\n }\n\n protected async send(\n input: ConverseCommandInput,\n signal?: AbortSignal | null,\n ): Promise<ConverseCommandOutput> {\n const { ConverseCommand } = await this.importBedrockRuntime()\n const client = await this.getClient()\n const command = new ConverseCommand(input)\n addRestoreDocuments(command, input)\n return client.send(command, { abortSignal: signal ?? undefined })\n }\n\n // ---------------------------------------------------------------------------\n // Public adapter surface\n // ---------------------------------------------------------------------------\n\n async *chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n try {\n options.logger.request(\n `activity=chat provider=${this.name} model=${this.model} messages=${options.messages.length} tools=${options.tools?.length ?? 0} stream=true`,\n { provider: this.name, model: this.model },\n )\n const input = this.buildInput(options)\n // Bedrock rejects toolUse and toolResult blocks without a toolConfig,\n // so a request with no tools sends its tool history as text.\n if (!input.toolConfig && input.messages) {\n input.messages = toolBlocksToText(input.messages)\n }\n const stream = await this.sendStream(input, options.request?.signal)\n for await (const chunk of processConverseStream(\n stream,\n () => this.generateId(),\n {\n threadId: options.threadId,\n parentRunId: options.parentRunId,\n model: options.model,\n },\n )) {\n yield chunk.type === EventType.RUN_STARTED ||\n chunk.type === EventType.RUN_FINISHED\n ? { ...chunk, metadata: this.sourceMetadata(options.model) }\n : chunk\n }\n } catch (error: unknown) {\n const errorPayload = converseRunErrorPayload(\n error,\n `${this.name}.chatStream failed`,\n )\n options.logger.errors(`${this.name}.chatStream fatal`, {\n error: errorPayload,\n source: `${this.name}.chatStream`,\n })\n // Conditional `code` spread keeps the wire shape spec-compliant under\n // `exactOptionalPropertyTypes` (AG-UI's `RunErrorEvent.code` is optional).\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(options.model),\n model: options.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n }\n }\n }\n\n /**\n * Structured output via the forced-tool strategy. Converse has no native\n * json_schema response_format, so we force a single tool whose input schema\n * is the requested output schema and read the model's `toolUse.input` back as\n * the structured result.\n */\n async structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>> {\n const { chatOptions, outputSchema } = options\n try {\n chatOptions.logger.request(\n `activity=structuredOutput provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n // Claude rejects a forced tool while thinking is on, so the forced\n // structured-output tool goes without `chat({ reasoning })`.\n const { reasoning: _reasoning, ...requestOptions } = chatOptions\n const input: ConverseCommandInput = {\n ...this.buildInput(requestOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const res = await this.send(input, chatOptions.request?.signal)\n const usage = res.usage && buildConverseUsage(res.usage)\n // A forced tool call cut off at the output cap carries partial input,\n // which would read like a schema failure (issue #1426).\n if (res.stopReason === 'max_tokens') {\n throw Object.assign(\n new Error(\n `${this.name}.structuredOutput: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`,\n ),\n usage && { usage },\n )\n }\n const structured = extractStructuredToolInput(res)\n if (structured === undefined) {\n throw Object.assign(\n new Error(\n `${this.name}.structuredOutput: response contained no forced-tool output`,\n ),\n usage && { usage },\n )\n }\n return {\n data: structured,\n rawText: JSON.stringify(structured),\n ...(usage && { usage }),\n }\n } catch (error: unknown) {\n chatOptions.logger.errors(`${this.name}.structuredOutput fatal`, {\n error: toRunErrorPayload(error, `${this.name}.structuredOutput failed`),\n source: `${this.name}.structuredOutput`,\n })\n throw error\n }\n }\n\n /**\n * Streaming structured output. Same forced-tool strategy as\n * `structuredOutput`, but streamed: the forced tool's `toolUse.input` JSON\n * fragments are accumulated from the Converse stream and a terminal\n * `CUSTOM 'structured-output.complete'` event carries `{ object, raw }`,\n * mirroring openai-base's `structuredOutputStream` contract exactly.\n */\n async *structuredOutputStream(\n options: StructuredOutputOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk> {\n const { chatOptions, outputSchema } = options\n const runId = this.generateId()\n const threadId = chatOptions.threadId\n const messageId = this.generateId()\n\n let hasEmittedRunStarted = false\n let hasEmittedTextMessageStart = false\n let accumulatedRaw = ''\n let finishReason: 'stop' | 'length' | 'content_filter' = 'stop'\n // Usage arrives on the trailing `metadata` event, after the finish signal,\n // so it is captured during iteration and folded into RUN_FINISHED below.\n let usage: TokenUsage | undefined\n\n try {\n chatOptions.logger.request(\n `activity=structuredOutputStream provider=${this.name} model=${this.model} messages=${chatOptions.messages.length}`,\n { provider: this.name, model: this.model },\n )\n // Claude rejects a forced tool while thinking is on, so the forced\n // structured-output tool goes without `chat({ reasoning })`.\n const { reasoning: _reasoning, ...requestOptions } = chatOptions\n const input: ConverseStreamCommandInput = {\n ...this.buildInput(requestOptions),\n toolConfig: buildStructuredToolConfig(outputSchema),\n }\n const stream = await this.sendStream(input, chatOptions.request?.signal)\n\n // The forced tool streams its `input` as partial-JSON fragments inside\n // `contentBlockDelta.delta.toolUse.input`. We surface them as\n // TEXT_MESSAGE_CONTENT deltas (raw JSON text), matching openai-base which\n // carries the structured JSON as text deltas.\n for await (const ev of stream) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n // Surface in-band server/throttle/validation errors instead of\n // letting them fall through and masquerade as an empty response.\n throwIfConverseStreamError(ev)\n\n if ('contentBlockDelta' in ev) {\n const delta = ev.contentBlockDelta?.delta\n const fragment =\n delta && 'toolUse' in delta ? delta.toolUse?.input : undefined\n if (fragment !== undefined) {\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n accumulatedRaw += fragment\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: fragment,\n content: accumulatedRaw,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // The forced structured-output tool produces stopReason 'tool_use' on\n // success, but that's an implementation detail — a cleanly-completed\n // structured run reports 'stop', matching openai-base's contract.\n finishReason =\n stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered' ||\n stopReason === 'guardrail_intervened'\n ? 'content_filter'\n : 'stop'\n continue\n }\n\n if ('metadata' in ev) {\n const u = ev.metadata?.usage\n if (u) {\n usage = buildConverseUsage(u)\n }\n continue\n }\n }\n\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n }\n\n // Same truncation check as `structuredOutput()`: report the token limit\n // before the empty-content and parse errors (issue #1426).\n if (finishReason === 'length') {\n const message = `${this.name}.structuredOutputStream: the response was cut off because the maximum token limit was reached (stopReason=max_tokens); raise modelOptions.max_completion_tokens`\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message,\n code: 'max_tokens',\n error: { message, code: 'max_tokens' },\n ...(usage && { usage }),\n }\n return\n }\n\n if (accumulatedRaw.length === 0) {\n const blocked = finishReason === 'content_filter'\n const message = blocked\n ? `${this.name}.structuredOutputStream: the response was blocked by a content filter`\n : `${this.name}.structuredOutputStream: response contained no content`\n const code = blocked ? 'content_filter' : 'empty-response'\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message,\n code,\n error: { message, code },\n ...(usage && { usage }),\n }\n return\n }\n\n let parsed: unknown\n try {\n parsed = JSON.parse(accumulatedRaw)\n } catch {\n const blocked = finishReason === 'content_filter'\n const message = blocked\n ? `${this.name}.structuredOutputStream: the response was blocked by a content filter`\n : `Failed to parse structured output as JSON. Content: ${accumulatedRaw.slice(0, 200)}${accumulatedRaw.length > 200 ? '...' : ''}`\n const code = blocked ? 'content_filter' : 'parse-error'\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message,\n code,\n error: {\n message: blocked\n ? message\n : 'Failed to parse structured output as JSON',\n code,\n },\n ...(usage && { usage }),\n }\n return\n }\n\n yield {\n type: EventType.CUSTOM,\n name: 'structured-output.complete',\n value: {\n object: parsed,\n raw: accumulatedRaw,\n },\n model: chatOptions.model,\n timestamp: Date.now(),\n }\n\n yield {\n type: EventType.RUN_FINISHED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n finishReason,\n ...(usage && { usage }),\n }\n } catch (error: unknown) {\n if (!hasEmittedRunStarted) {\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n threadId,\n model: chatOptions.model,\n timestamp: Date.now(),\n parentRunId: chatOptions.parentRunId,\n }\n }\n const errorPayload = converseRunErrorPayload(\n error,\n `${this.name}.structuredOutputStream failed`,\n )\n chatOptions.logger.errors(`${this.name}.structuredOutputStream fatal`, {\n error: errorPayload,\n source: `${this.name}.structuredOutputStream`,\n })\n yield {\n type: EventType.RUN_ERROR,\n metadata: this.sourceMetadata(chatOptions.model),\n runId,\n model: chatOptions.model,\n timestamp: Date.now(),\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n error: {\n message: errorPayload.message,\n ...(errorPayload.code !== undefined && { code: errorPayload.code }),\n },\n ...(usage && { usage }),\n }\n }\n }\n\n /**\n * Converse sends `tools` and a forced structured-output tool via two separate\n * mechanisms, never together. Declaring `false` makes the engine run the\n * agent loop without `outputSchema` and finalize via `structuredOutput` /\n * `structuredOutputStream`.\n */\n supportsCombinedToolsAndSchema(): boolean {\n return false\n }\n\n // ---------------------------------------------------------------------------\n // Request construction\n // ---------------------------------------------------------------------------\n\n /**\n * Translate `TextOptions` into a `ConverseCommandInput`. Shared by chatStream,\n * structuredOutput, and structuredOutputStream (the latter two override\n * `toolConfig` with the forced structured tool afterwards).\n */\n protected buildInput(\n options: TextOptions<TProviderOptions>,\n ): ConverseCommandInput {\n const { system, messages } = toConverseMessages(\n options.messages,\n options.systemPrompts,\n { model: options.model, provider: this.provider },\n )\n\n // Sampling options live on `modelOptions` (typed as the narrowed\n // `BedrockConverseProviderOptions`, which surfaces the OpenAI Chat\n // Completions field names); translate them into Converse's `inferenceConfig`,\n // which uses AWS-native camelCase keys.\n const modelOptions = options.modelOptions\n const temperature = modelOptions?.temperature\n const topP = modelOptions?.top_p\n // `chat({ reasoning })`: Claude's thinking fields. Budget thinking needs\n // `maxTokens` above the budget, so a smaller or missing value grows.\n const { additionalModelRequestFields, minMaxTokens } = converseThinking(\n this.model,\n options.reasoning,\n BEDROCK_MODEL_REASONING[this.model],\n )\n\n // `chat({ toolChoice })`. Converse has no `none` choice, so `none` sends\n // no tool config, and `chatStream` sends any tool history as text. Claude\n // rejects a forced tool while thinking is on (only Claude gets thinking\n // fields), and some Claude models reject it on every request. Then a\n // forced choice falls back to auto.\n const canForceTool =\n additionalModelRequestFields === undefined &&\n !BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES.some((name) =>\n this.model.includes(name),\n )\n const toolChoice =\n options.toolChoice === 'none' || canForceTool\n ? options.toolChoice\n : 'auto'\n const toolConfig = options.tools\n ? toToolConfig(convertTools(options.tools), toolChoice)\n : undefined\n\n const requestedMaxTokens = modelOptions?.max_completion_tokens\n const maxTokens =\n minMaxTokens !== undefined &&\n (requestedMaxTokens == null || requestedMaxTokens < minMaxTokens)\n ? minMaxTokens\n : requestedMaxTokens\n const stop = modelOptions?.stop\n const stopSequences =\n stop == null ? undefined : Array.isArray(stop) ? stop : [stop]\n\n const inferenceConfig =\n temperature != null ||\n topP != null ||\n maxTokens != null ||\n stopSequences != null\n ? {\n ...(temperature != null && { temperature }),\n ...(topP != null && { topP }),\n ...(maxTokens != null && { maxTokens }),\n ...(stopSequences != null && { stopSequences }),\n }\n : undefined\n\n const input: ConverseCommandInput = {\n modelId: this.model,\n messages,\n ...(system.length > 0 && { system }),\n ...(toolConfig && { toolConfig }),\n ...(inferenceConfig && { inferenceConfig }),\n ...(additionalModelRequestFields && { additionalModelRequestFields }),\n ...(modelOptions?.requestMetadata !== undefined && {\n requestMetadata: modelOptions.requestMetadata,\n }),\n ...(modelOptions?.serviceTier !== undefined && {\n serviceTier: { type: modelOptions.serviceTier },\n }),\n }\n return addPromptCachePoints(this.model, input, options.promptCache)\n }\n}\n\n/**\n * Convert TanStack `Tool[]` to the Converse tool-converter input shape. Reuses\n * the SAME `convertSchemaToJsonSchema` the other adapters use so the Converse\n * tool input schemas match what every other provider sends.\n */\n/**\n * The text Bedrock models use for a context overflow. Converse has no error\n * type for it: it is a `ValidationException` like every bad request, and over\n * a stream the SDK does not keep that name. So the message is the only signal.\n * Each model family has its own text.\n */\nconst CONTEXT_OVERFLOW_MESSAGE =\n /Input Tokens Exceeded|maximum context length|Input is too long for requested model|prompt is too long/\n\n/**\n * `toRunErrorPayload`, with `code` set to `CONTEXT_OVERFLOW_CODE` for a\n * context overflow. A `ThrottlingException` is never an overflow, whatever\n * its text says about tokens.\n */\nfunction converseRunErrorPayload(\n error: unknown,\n fallbackMessage: string,\n): { message: string; code: string | undefined } {\n const payload = toRunErrorPayload(error, fallbackMessage)\n const throttled =\n error instanceof Error && error.name === 'ThrottlingException'\n return !throttled && CONTEXT_OVERFLOW_MESSAGE.test(payload.message)\n ? { ...payload, code: CONTEXT_OVERFLOW_CODE }\n : payload\n}\n\nfunction convertTools(tools: Array<Tool>): Array<ConverseToolInput> {\n return tools.map((tool) => {\n const inputSchema: JSONSchema = convertSchemaToJsonSchema(\n tool.inputSchema,\n ) ?? { type: 'object', properties: {}, required: [] }\n const { cachePoint }: BedrockToolMetadata = tool.metadata ?? {}\n return {\n name: tool.name,\n description: tool.description,\n inputSchema,\n ...(cachePoint && { cachePoint }),\n }\n })\n}\n\n/**\n * Find the forced structured-output tool's `input` in a non-streaming Converse\n * response. SDK-boundary narrowing only — `ConverseOutput` is a tagged union\n * (`{ message }`) and a tool-use block is `{ toolUse: { input } }`.\n */\nfunction extractStructuredToolInput(\n res: ConverseCommandOutput,\n): unknown | undefined {\n const message =\n res.output && 'message' in res.output ? res.output.message : undefined\n const content: Array<ContentBlock> = message?.content ?? []\n for (const block of content) {\n if ('toolUse' in block && block.toolUse) {\n // Only accept the forced structured tool (an unnamed block is allowed,\n // since the forced tool is the only one configured). A differently-named\n // tool-use block is a hallucinated/leftover call whose arbitrary input\n // must not be returned as the validated result — leave it to the caller's\n // `throw` so the failure is accurate instead of silently wrong.\n if (\n block.toolUse.name === STRUCTURED_TOOL_NAME ||\n block.toolUse.name === undefined\n ) {\n return block.toolUse.input\n }\n }\n }\n return undefined\n}\n\n/** The parts of a Converse request that hold documents the adapter fills from user data. */\ntype ConverseDocuments = Pick<ConverseCommandInput, 'messages' | 'toolConfig'>\n\nfunction hasProtoKey(value: unknown): boolean {\n if (typeof value !== 'object' || value === null) return false\n return (\n Object.hasOwn(value, '__proto__') || Object.values(value).some(hasProtoKey)\n )\n}\n\n/**\n * The AWS SDK copies a document (tool input, tool input schema) with\n * `out[key] = value`. For an own key named `__proto__`, the assignment goes\n * to the `__proto__` setter and the key is lost. This puts each such\n * document back in the serialized body. `JSON.stringify` keeps own keys.\n */\nfunction restoreDocuments(request: unknown, input: ConverseDocuments): void {\n const documents = [\n ...(input.messages ?? []).flatMap((message) =>\n (message.content ?? []).map((block) => block.toolUse?.input),\n ),\n ...(input.toolConfig?.tools ?? []).map(\n (tool) => tool.toolSpec?.inputSchema?.json,\n ),\n ]\n // Skip the parse and re-serialize of the body, which can be large (for\n // example with image bytes), unless a document has the key.\n if (!documents.some(hasProtoKey)) return\n if (\n typeof request !== 'object' ||\n request === null ||\n !('body' in request) ||\n typeof request.body !== 'string'\n ) {\n throw new Error(\n 'Bedrock Converse: cannot keep a `__proto__` key because the request body is not a JSON string',\n )\n }\n // The SDK serialized this body from `input` one step earlier, so the two\n // line up by position.\n const body: ConverseDocuments = JSON.parse(request.body)\n body.messages?.forEach((message, i) =>\n message.content?.forEach((block, j) => {\n const source = input.messages?.[i]?.content?.[j]?.toolUse?.input\n if (block.toolUse && hasProtoKey(source)) block.toolUse.input = source\n }),\n )\n body.toolConfig?.tools?.forEach((tool, i) => {\n const source = input.toolConfig?.tools?.[i]?.toolSpec?.inputSchema?.json\n if (tool.toolSpec?.inputSchema && hasProtoKey(source))\n tool.toolSpec.inputSchema.json = source\n })\n request.body = JSON.stringify(body)\n}\n\n/**\n * Adds {@link restoreDocuments} to a command. It runs at `build` with high\n * priority, before the SDK sets Content-Length and signs the body.\n */\nfunction addRestoreDocuments<TInput extends object, TOutput extends object>(\n command: { middlewareStack: MiddlewareStack<TInput, TOutput> },\n input: ConverseDocuments,\n): void {\n command.middlewareStack.add(\n (next) => (args) => {\n restoreDocuments(args.request, input)\n return next(args)\n },\n { step: 'build', name: 'tanstackRestoreDocuments', priority: 'high' },\n )\n}\n\n/** Converse adapter with an explicit API key (low-level; mirrors createBedrockChat). */\nexport function createBedrockConverse<TModel extends BedrockConverseModels>(\n model: TModel,\n apiKey: string,\n config?: Omit<BedrockConverseConfig, 'apiKey'>,\n): BedrockConverseTextAdapter<TModel> {\n return new BedrockConverseTextAdapter({ ...config, apiKey }, model)\n}\n"],"mappings":";;;;;;;;;;;;;AA0FA,IAAa,6BAAb,cAYU,gBAcR;CACA,OAAyB;CACzB,OAAyB;CACzB,MAAwB;CACxB,WAA6B;CAC7B,kBAAoC,+BAA+B,KAAK;CAExE,eAAuB,OAAe;EACpC,OAAO,EACL,UAAU,EAAE,QAAQ;GAAE,UAAU,KAAK;GAAU,KAAK,KAAK;GAAK;EAAM,EAAE,EACxE;CACF;CACA;CACA;CAEA,YAAY,QAA+B,OAAe;EACxD,MAAM,CAAC,GAAG,KAAK;EAIf,KAAK,eAAe;CACtB;;;;;;;;;;CAWA,uBAAiE;EAE/D,OAAO,mBAA0C,iCAAG;CACtD;;;;;;CAOA,MAAgB,YAA2C;EACzD,IAAI,CAAC,KAAK,eACR,KAAK,iBAAiB,YAAY;GAChC,MAAM,EAAE,yBAAyB,MAAM,KAAK,qBAAqB;GACjE,MAAM,SAAS,KAAK,aAAa,UAAU;GAC3C,MAAM,WAAW,mBACf;IACE,QAAQ,KAAK,aAAa;IAC1B;IACA,MAAM,KAAK,aAAa;GAC1B,GACA,SACF;GACA,MAAM,SAAS,IAAI,qBACjB,KAAK,kBAAkB,UAAU,QAAQ,KAAK,aAAa,OAAO,CACpE;GACA,MAAM,iBAAiB,KAAK,aAAa;GACzC,IAAI,gBAGF,OAAO,gBAAgB,KACpB,UAAU,SAAS;IAClB,MAAM,UAAU,KAAK;IACrB,IACE,OAAO,YAAY,YACnB,YAAY,QACZ,aAAa,WACb,OAAO,QAAQ,YAAY,YAC3B,QAAQ,YAAY,MAEpB,OAAO,OAAO,QAAQ,SAAS,cAAc;IAE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AAM,QAAQ,IAAI;CACvD,KAAK,UAAU,SAAS,SAAS,MAC/B,QAAQ,SAAS,SAAS,OAAO,MAAM;EACrC,MAAM,SAAS,MAAM,WAAW,EAAE,EAAE,UAAU,EAAE,EAAE,SAAS;EAC3D,IAAI,MAAM,WAAW,YAAY,MAAM,GAAG,MAAM,QAAQ,QAAQ;CAClE,CAAC,CACH;CACA,KAAK,YAAY,OAAO,SAAS,MAAM,MAAM;EAC3C,MAAM,SAAS,MAAM,YAAY,QAAQ,EAAE,EAAE,UAAU,aAAa;EACpE,IAAI,KAAK,UAAU,eAAe,YAAY,MAAM,GAClD,KAAK,SAAS,YAAY,OAAO;CACrC,CAAC;CACD,QAAQ,OAAO,KAAK,UAAU,IAAI;AACpC;;;;;AAMA,SAAS,oBACP,SACA,OACM;CACN,QAAQ,gBAAgB,KACrB,UAAU,SAAS;EAClB,iBAAiB,KAAK,SAAS,KAAK;EACpC,OAAO,KAAK,IAAI;CAClB,GACA;EAAE,MAAM;EAAS,MAAM;EAA4B,UAAU;CAAO,CACtE;AACF;;AAGA,SAAgB,sBACd,OACA,QACA,QACoC;CACpC,OAAO,IAAI,2BAA2B;EAAE,GAAG;EAAQ;CAAO,GAAG,KAAK;AACpE"}
|
|
@@ -3,7 +3,7 @@ import { BEDROCK_EMBEDDING_MODELS } from "../model-meta.js";
|
|
|
3
3
|
import { isFileSource, requireTextOnlyEmbeddingInput, resolveEmbeddingInput, unsupportedFileSourceError } from "@tanstack/ai";
|
|
4
4
|
import { BaseEmbeddingAdapter } from "@tanstack/ai/adapters";
|
|
5
5
|
import { toRunErrorPayload } from "@tanstack/ai/adapter-internals";
|
|
6
|
-
|
|
6
|
+
// src/adapters/embedding.ts
|
|
7
7
|
/** InvokeModel calls issued concurrently during a per-item fan-out. */
|
|
8
8
|
var MAX_CONCURRENT_INVOCATIONS = 5;
|
|
9
9
|
/** Valid `dimensions` for `amazon.titan-embed-text-v2:0`. */
|
|
@@ -329,7 +329,6 @@ function createBedrockEmbedding(model, apiKey, config) {
|
|
|
329
329
|
function bedrockEmbedding(model, config) {
|
|
330
330
|
return new BedrockEmbeddingAdapter(config ?? {}, model);
|
|
331
331
|
}
|
|
332
|
-
//#endregion
|
|
333
332
|
export { BedrockEmbeddingAdapter, bedrockEmbedding, createBedrockEmbedding };
|
|
334
333
|
|
|
335
334
|
//# sourceMappingURL=embedding.js.map
|
|
@@ -2,7 +2,7 @@ import { resolveBedrockAuth } from "../utils/auth.js";
|
|
|
2
2
|
import { createSigV4Fetch } from "../utils/openai-sigv4-fetch.js";
|
|
3
3
|
import { BaseRerankAdapter } from "@tanstack/ai/adapters";
|
|
4
4
|
import { toRunErrorPayload } from "@tanstack/ai/adapter-internals";
|
|
5
|
-
|
|
5
|
+
// src/adapters/rerank.ts
|
|
6
6
|
function isBedrockRerankResponse(value) {
|
|
7
7
|
return typeof value === "object" && value !== null && "results" in value && Array.isArray(value.results) && value.results.every((result) => typeof result === "object" && result !== null && Number.isInteger(result.index) && Number.isFinite(result.relevanceScore)) && (!("nextToken" in value) || typeof value.nextToken === "string" && value.nextToken.length > 0 && value.nextToken.length <= 2048 && !/\s/.test(value.nextToken));
|
|
8
8
|
}
|
|
@@ -117,7 +117,6 @@ var BedrockRerankAdapter = class extends BaseRerankAdapter {
|
|
|
117
117
|
function bedrockRerank(model, config) {
|
|
118
118
|
return new BedrockRerankAdapter(config ?? {}, model);
|
|
119
119
|
}
|
|
120
|
-
//#endregion
|
|
121
120
|
export { BedrockRerankAdapter, bedrockRerank };
|
|
122
121
|
|
|
123
122
|
//# sourceMappingURL=rerank.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isBedrockOpenAIContextOverflow, withBedrockDefaults } from "../utils/client.js";
|
|
2
2
|
import { BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING } from "../model-meta.js";
|
|
3
3
|
import { OpenAIBaseResponsesTextAdapter, createOpenAIClient } from "@tanstack/openai-base";
|
|
4
|
-
|
|
4
|
+
// src/adapters/responses-text.ts
|
|
5
5
|
/**
|
|
6
6
|
* Bedrock Responses adapter. Drives mantle's OpenAI-compatible `/responses`
|
|
7
7
|
* endpoint via the OpenAI-compatible client (`client.responses.create`) — the same base
|
|
@@ -34,7 +34,6 @@ function createBedrockResponsesText(model, apiKey, config) {
|
|
|
34
34
|
apiKey
|
|
35
35
|
}, model);
|
|
36
36
|
}
|
|
37
|
-
//#endregion
|
|
38
37
|
export { BedrockResponsesTextAdapter, createBedrockResponsesText };
|
|
39
38
|
|
|
40
39
|
//# sourceMappingURL=responses-text.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isBedrockOpenAIContextOverflow, withBedrockDefaults } from "../utils/client.js";
|
|
2
2
|
import { BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING } from "../model-meta.js";
|
|
3
3
|
import { OpenAIBaseChatCompletionsTextAdapter, createOpenAIClient } from "@tanstack/openai-base";
|
|
4
|
-
|
|
4
|
+
// src/adapters/text.ts
|
|
5
5
|
/**
|
|
6
6
|
* Bedrock Chat Completions adapter. Drives Bedrock's OpenAI-compatible
|
|
7
7
|
* `/chat/completions` endpoint via the OpenAI-compatible client with a baseURL override
|
|
@@ -53,7 +53,6 @@ function createBedrockChat(model, apiKey, config) {
|
|
|
53
53
|
apiKey
|
|
54
54
|
}, model);
|
|
55
55
|
}
|
|
56
|
-
//#endregion
|
|
57
56
|
export { BedrockTextAdapter, createBedrockChat };
|
|
58
57
|
|
|
59
58
|
//# sourceMappingURL=text.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/api-compatibility.ts
|
|
2
2
|
var BEDROCK_API_COMPATIBILITY = [
|
|
3
3
|
{
|
|
4
4
|
match: "openai.gpt-oss",
|
|
@@ -115,7 +115,6 @@ function mantlePathForModel(model) {
|
|
|
115
115
|
if (typeof model !== "string") return "/v1";
|
|
116
116
|
return lookupBedrockCompatibility(model).mantlePath;
|
|
117
117
|
}
|
|
118
|
-
//#endregion
|
|
119
118
|
export { BEDROCK_API_COMPATIBILITY, lookupBedrockCompatibility, mantlePathForModel };
|
|
120
119
|
|
|
121
120
|
//# sourceMappingURL=api-compatibility.js.map
|
package/dist/esm/byok.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { defineByokProvider } from "@tanstack/ai/byok";
|
|
2
|
-
|
|
2
|
+
// src/byok.ts
|
|
3
3
|
var bedrockByok = defineByokProvider({
|
|
4
4
|
id: "bedrock",
|
|
5
5
|
label: "Amazon Bedrock"
|
|
6
6
|
});
|
|
7
|
-
//#endregion
|
|
8
7
|
export { bedrockByok };
|
|
9
8
|
|
|
10
9
|
//# sourceMappingURL=byok.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isFileSource, normalizeSystemPrompts, unsupportedFileSourceError } from "@tanstack/ai";
|
|
2
2
|
import { hashToolCallId, orderedAssistantBlocks, transformMessagesForReplay } from "@tanstack/ai/adapter-internals";
|
|
3
|
-
|
|
3
|
+
// src/converse/message-converter.ts
|
|
4
4
|
function base64ToBytes(b64) {
|
|
5
5
|
return new Uint8Array(Buffer.from(b64, "base64"));
|
|
6
6
|
}
|
|
@@ -205,7 +205,6 @@ function toolBlocksToText(messages) {
|
|
|
205
205
|
})
|
|
206
206
|
}));
|
|
207
207
|
}
|
|
208
|
-
//#endregion
|
|
209
208
|
export { toConverseMessages, toolBlocksToText };
|
|
210
209
|
|
|
211
210
|
//# sourceMappingURL=message-converter.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/converse/prompt-cache.ts
|
|
2
2
|
/** True when one of the blocks is a cache point. */
|
|
3
3
|
function hasCachePoint(blocks) {
|
|
4
4
|
return blocks?.some((block) => block.cachePoint !== void 0) ?? false;
|
|
@@ -37,7 +37,6 @@ function addPromptCachePoints(model, input, promptCache) {
|
|
|
37
37
|
}] }
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
|
-
//#endregion
|
|
41
40
|
export { addPromptCachePoints };
|
|
42
41
|
|
|
43
42
|
//# sourceMappingURL=prompt-cache.js.map
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { ServiceTierType } from '@aws-sdk/client-bedrock-runtime';
|
|
1
2
|
/**
|
|
2
3
|
* Provider options honored by the Bedrock **Converse** path. Deliberately
|
|
3
4
|
* narrower than the OpenAI-compatible `BedrockTextProviderOptions`: the Converse
|
|
4
|
-
* API
|
|
5
|
-
*
|
|
5
|
+
* API accepts these sampling knobs (mapped into `inferenceConfig`) and request
|
|
6
|
+
* options. Advertising the full Chat Completions option set would promise fields —
|
|
6
7
|
* `frequency_penalty`, `seed`, `logit_bias`, `tool_choice`, `reasoning_effort`,
|
|
7
|
-
* etc. — that Converse silently ignores.
|
|
8
|
+
* etc. — that Converse silently ignores. Sampling field names mirror the Chat
|
|
8
9
|
* Completions shape so callers have one mental model across the three APIs.
|
|
9
10
|
*/
|
|
10
11
|
export interface BedrockConverseProviderOptions {
|
|
@@ -16,4 +17,8 @@ export interface BedrockConverseProviderOptions {
|
|
|
16
17
|
max_completion_tokens?: number | null;
|
|
17
18
|
/** Forwarded to Converse `inferenceConfig.stopSequences`. */
|
|
18
19
|
stop?: string | Array<string> | null;
|
|
20
|
+
/** Request metadata for filtering Bedrock model invocation logs. */
|
|
21
|
+
requestMetadata?: Record<string, string>;
|
|
22
|
+
/** Inference service tier, forwarded to Converse `serviceTier.type`. */
|
|
23
|
+
serviceTier?: ServiceTierType;
|
|
19
24
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { reasoningBudget, resolveReasoning } from "@tanstack/ai/adapter-internals";
|
|
2
|
-
|
|
2
|
+
// src/converse/reasoning.ts
|
|
3
3
|
/** The answer room pi keeps next to a thinking budget. */
|
|
4
4
|
var MIN_ANSWER_TOKENS = 1024;
|
|
5
5
|
/**
|
|
@@ -36,7 +36,6 @@ function converseThinking(model, request, reasoning) {
|
|
|
36
36
|
output_config: { effort: resolved.value }
|
|
37
37
|
} };
|
|
38
38
|
}
|
|
39
|
-
//#endregion
|
|
40
39
|
export { converseThinking };
|
|
41
40
|
|
|
42
41
|
//# sourceMappingURL=reasoning.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { buildConverseUsage } from "./usage.js";
|
|
2
|
-
import { EventType } from "@tanstack/ai";
|
|
3
|
-
|
|
2
|
+
import { CONTEXT_OVERFLOW_CODE, EventType } from "@tanstack/ai";
|
|
3
|
+
// src/converse/stream-processor.ts
|
|
4
4
|
/**
|
|
5
5
|
* Converse delivers server-side failures — throttling, request validation,
|
|
6
6
|
* mid-stream model faults, and service-unavailable — as in-band stream events
|
|
@@ -31,6 +31,7 @@ async function* processConverseStream(stream, newMessageId, lifecycle) {
|
|
|
31
31
|
const toolCallsByIndex = /* @__PURE__ */ new Map();
|
|
32
32
|
let usage;
|
|
33
33
|
let finishReason;
|
|
34
|
+
let contextWindowExceeded = false;
|
|
34
35
|
function* ensureRunStarted() {
|
|
35
36
|
if (hasEmittedRunStarted) return;
|
|
36
37
|
hasEmittedRunStarted = true;
|
|
@@ -196,6 +197,7 @@ async function* processConverseStream(stream, newMessageId, lifecycle) {
|
|
|
196
197
|
}
|
|
197
198
|
if ("messageStop" in ev) {
|
|
198
199
|
const stopReason = ev.messageStop?.stopReason;
|
|
200
|
+
contextWindowExceeded = stopReason === "model_context_window_exceeded";
|
|
199
201
|
finishReason = stopReason === "tool_use" ? "tool_calls" : stopReason === "max_tokens" ? "length" : stopReason === "content_filtered" || stopReason === "guardrail_intervened" ? "content_filter" : "stop";
|
|
200
202
|
continue;
|
|
201
203
|
}
|
|
@@ -223,6 +225,22 @@ async function* processConverseStream(stream, newMessageId, lifecycle) {
|
|
|
223
225
|
timestamp: Date.now()
|
|
224
226
|
};
|
|
225
227
|
yield* closeReasoning();
|
|
228
|
+
if (contextWindowExceeded) {
|
|
229
|
+
const message = "The response was cut off because the model reached the end of its context window.";
|
|
230
|
+
yield {
|
|
231
|
+
type: EventType.RUN_ERROR,
|
|
232
|
+
...model && { model },
|
|
233
|
+
timestamp: Date.now(),
|
|
234
|
+
message,
|
|
235
|
+
code: CONTEXT_OVERFLOW_CODE,
|
|
236
|
+
error: {
|
|
237
|
+
message,
|
|
238
|
+
code: CONTEXT_OVERFLOW_CODE
|
|
239
|
+
},
|
|
240
|
+
...usage && { usage }
|
|
241
|
+
};
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
226
244
|
yield {
|
|
227
245
|
type: EventType.RUN_FINISHED,
|
|
228
246
|
runId,
|
|
@@ -232,7 +250,6 @@ async function* processConverseStream(stream, newMessageId, lifecycle) {
|
|
|
232
250
|
...usage && { usage }
|
|
233
251
|
};
|
|
234
252
|
}
|
|
235
|
-
//#endregion
|
|
236
253
|
export { processConverseStream, throwIfConverseStreamError };
|
|
237
254
|
|
|
238
255
|
//# sourceMappingURL=stream-processor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-processor.js","names":[],"sources":["../../../src/converse/stream-processor.ts"],"sourcesContent":["import { EventType } from '@tanstack/ai'\nimport { buildConverseUsage } from './usage'\nimport type { AdapterYieldChunk, TokenUsage } from '@tanstack/ai'\nimport type { ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime'\n\n/**\n * Converse delivers server-side failures — throttling, request validation,\n * mid-stream model faults, and service-unavailable — as in-band stream events\n * rather than thrown exceptions. If they were ignored the iterator would simply\n * end and the run would look like a clean, truncated success. Throw the\n * underlying exception (these SDK members extend `Error`) so the adapter's\n * `chatStream` / `structuredOutputStream` catch converts it into a `RUN_ERROR`.\n */\nexport function throwIfConverseStreamError(ev: ConverseStreamOutput): void {\n if ('internalServerException' in ev && ev.internalServerException) {\n throw ev.internalServerException\n }\n if ('modelStreamErrorException' in ev && ev.modelStreamErrorException) {\n throw ev.modelStreamErrorException\n }\n if ('validationException' in ev && ev.validationException) {\n throw ev.validationException\n }\n if ('throttlingException' in ev && ev.throttlingException) {\n throw ev.throttlingException\n }\n if ('serviceUnavailableException' in ev && ev.serviceUnavailableException) {\n throw ev.serviceUnavailableException\n }\n}\n\n/** Map Converse blocks to run, text, reasoning, and tool events.\n * Keep signatures and encrypted bytes separate for each contentBlockIndex.\n * Finish after trailing usage events. The adapter handles thrown errors.\n */\nexport async function* processConverseStream(\n stream: AsyncIterable<ConverseStreamOutput>,\n newMessageId: () => string,\n lifecycle: { threadId: string; parentRunId?: string; model?: string },\n): AsyncIterable<AdapterYieldChunk> {\n const runId = newMessageId()\n const { threadId, parentRunId, model } = lifecycle\n const messageId = newMessageId()\n\n let hasEmittedRunStarted = false\n\n // Text lifecycle\n let accumulatedContent = ''\n let hasEmittedTextMessageStart = false\n\n // Reasoning lifecycle\n const reasoningByIndex = new Map<\n number,\n {\n id: string\n signature: string\n bytes: Array<Uint8Array>\n redacted: boolean\n closed: boolean\n encrypted?: string\n }\n >()\n\n // Tool-call lifecycle, keyed by Converse contentBlockIndex. Converse opens a\n // tool-use block with `contentBlockStart`, streams arg fragments via\n // `contentBlockDelta`, and closes it with `contentBlockStop`.\n const toolCallsByIndex = new Map<\n number,\n {\n id: string\n name: string\n started: boolean\n arguments: string\n }\n >()\n\n // Usage + finish-reason are captured during iteration and folded into the\n // single terminal RUN_FINISHED, matching openai-base's deferred-finish\n // contract (usage may arrive after the finish signal).\n let usage: TokenUsage | undefined\n let finishReason: NonNullable<AdapterYieldChunk['finishReason']> | undefined\n\n // Lazily emit RUN_STARTED exactly once, before the first content event.\n function* ensureRunStarted(): Generator<AdapterYieldChunk> {\n if (hasEmittedRunStarted) return\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n parentRunId,\n ...(model && { model }),\n timestamp: Date.now(),\n }\n }\n\n // Close an open reasoning message before text/tool content begins, mirroring\n // openai-base which always emits REASONING_MESSAGE_END before TEXT_MESSAGE_START.\n function* encryptedReasoning(index: number): Generator<AdapterYieldChunk> {\n const reasoning = reasoningByIndex.get(index)\n if (!reasoning) return\n const encrypted = reasoning.redacted\n ? Buffer.concat(reasoning.bytes).toString('base64')\n : reasoning.signature\n if (!encrypted || encrypted === reasoning.encrypted) return\n reasoning.encrypted = encrypted\n yield {\n type: EventType.REASONING_ENCRYPTED_VALUE,\n entityId: reasoning.id,\n subtype: 'message',\n encryptedValue: encrypted,\n timestamp: Date.now(),\n }\n }\n\n function* closeReasoning(index?: number): Generator<AdapterYieldChunk> {\n for (const [blockIndex, reasoning] of reasoningByIndex) {\n if (index !== undefined && index !== blockIndex) continue\n yield* encryptedReasoning(blockIndex)\n if (reasoning.closed) continue\n reasoning.closed = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n }\n }\n\n for await (const ev of stream) {\n yield* ensureRunStarted()\n\n // Surface in-band server/throttle/validation errors instead of dropping them.\n throwIfConverseStreamError(ev)\n\n // messageStart carries only the role; no AG-UI event maps to it.\n if ('messageStart' in ev) continue\n\n if ('contentBlockStart' in ev) {\n const start = ev.contentBlockStart\n const toolUse = start?.start?.toolUse\n if (start && toolUse) {\n yield* closeReasoning()\n const id = toolUse.toolUseId ?? newMessageId()\n const name = toolUse.name ?? ''\n const index = start.contentBlockIndex ?? 0\n toolCallsByIndex.set(index, {\n id,\n name,\n started: true,\n arguments: '',\n })\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: id,\n toolCallName: name,\n timestamp: Date.now(),\n index,\n }\n }\n continue\n }\n\n if ('contentBlockDelta' in ev) {\n const block = ev.contentBlockDelta\n const delta = block?.delta\n const index = block?.contentBlockIndex ?? 0\n\n // Tool-call argument fragments (partial JSON).\n if (delta && 'toolUse' in delta && delta.toolUse?.input !== undefined) {\n const toolCall = toolCallsByIndex.get(index)\n if (toolCall?.started) {\n toolCall.arguments += delta.toolUse.input\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: toolCall.id,\n timestamp: Date.now(),\n delta: delta.toolUse.input,\n }\n }\n continue\n }\n\n if (delta && 'reasoningContent' in delta && delta.reasoningContent) {\n const fragment = delta.reasoningContent\n let reasoning = reasoningByIndex.get(index)\n if (!reasoning) {\n const redacted =\n 'redactedContent' in fragment &&\n (fragment.redactedContent?.length ?? 0) > 0\n reasoning = {\n id: (redacted ? 'redacted_thinking-' : '') + newMessageId(),\n signature: '',\n bytes: [],\n redacted,\n closed: false,\n }\n reasoningByIndex.set(index, reasoning)\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoning.id,\n role: 'reasoning',\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'text' in fragment &&\n fragment.text !== undefined\n ) {\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoning.id,\n delta: fragment.text,\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'signature' in fragment &&\n fragment.signature !== undefined\n )\n reasoning.signature += fragment.signature\n if (\n 'redactedContent' in fragment &&\n fragment.redactedContent &&\n fragment.redactedContent.length > 0\n ) {\n if (!reasoning.redacted) reasoning.encrypted = undefined\n reasoning.redacted = true\n reasoning.signature = ''\n reasoning.bytes.push(fragment.redactedContent)\n yield* encryptedReasoning(index)\n }\n continue\n }\n\n // Text content.\n if (delta && 'text' in delta && delta.text !== undefined) {\n yield* closeReasoning()\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n timestamp: Date.now(),\n }\n }\n accumulatedContent += delta.text\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: delta.text,\n content: accumulatedContent,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('contentBlockStop' in ev) {\n const stopIndex = ev.contentBlockStop?.contentBlockIndex ?? 0\n yield* closeReasoning(stopIndex)\n const toolCall = toolCallsByIndex.get(stopIndex)\n if (toolCall?.started) {\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(stopIndex)\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason = ev.messageStop?.stopReason\n // Map Converse stopReason to AG-UI's narrower finishReason vocabulary.\n finishReason =\n stopReason === 'tool_use'\n ? 'tool_calls'\n : stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered' ||\n stopReason === 'guardrail_intervened'\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 // Stream ended (possibly without any content) — still emit RUN_STARTED so\n // consumers always see a run lifecycle.\n yield* ensureRunStarted()\n\n // Drain any tool call that opened but never received contentBlockStop.\n for (const [index, toolCall] of toolCallsByIndex) {\n if (!toolCall.started) continue\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(index)\n }\n\n // Close the text message lifecycle if it was opened.\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n timestamp: Date.now(),\n }\n }\n\n // Close any reasoning lifecycle that text never closed.\n yield* closeReasoning()\n\n // Single terminal RUN_FINISHED. Conditional `usage` spread keeps the wire\n // shape spec-compliant (AG-UI's `usage` is optional with no `| undefined`).\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n timestamp: Date.now(),\n finishReason: finishReason ?? 'stop',\n ...(usage && { usage }),\n }\n}\n"],"mappings":";;;;;;;;;;;AAaA,SAAgB,2BAA2B,IAAgC;CACzE,IAAI,6BAA6B,MAAM,GAAG,yBACxC,MAAM,GAAG;CAEX,IAAI,+BAA+B,MAAM,GAAG,2BAC1C,MAAM,GAAG;CAEX,IAAI,yBAAyB,MA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EACF;EAEA,IAAI,iBAAiB,IAAI;GACvB,MAAM,aAAa,GAAG,aAAa;GAEnC,eACE,eAAe,aACX,eACA,eAAe,eACb,WACA,eAAe,sBACb,eAAe,yBACf,mBACA;GACV;EACF;EAEA,IAAI,cAAc,IAAI;GACpB,MAAM,IAAI,GAAG,UAAU;GACvB,IAAI,GACF,QAAQ,mBAAmB,CAAC;GAE9B;EACF;CACF;CAIA,OAAO,iBAAiB;CAGxB,KAAK,MAAM,CAAC,OAAO,aAAa,kBAAkB;EAChD,IAAI,CAAC,SAAS,SAAS;EACvB,MAAM;GACJ,MAAM,UAAU;GAChB,YAAY,SAAS;GACrB,MAAM,SAAS;GACf,cAAc,SAAS;GACvB,WAAW,KAAK,IAAI;EACtB;EACA,iBAAiB,OAAO,KAAK;CAC/B;CAGA,IAAI,4BACF,MAAM;EACJ,MAAM,UAAU;EAChB;EACA,WAAW,KAAK,IAAI;CACtB;CAIF,OAAO,eAAe;CAItB,MAAM;EACJ,MAAM,UAAU;EAChB;EACA;EACA,WAAW,KAAK,IAAI;EACpB,cAAc,gBAAgB;EAC9B,GAAI,SAAS,EAAE,MAAM;CACvB;AACF"}
|
|
1
|
+
{"version":3,"file":"stream-processor.js","names":[],"sources":["../../../src/converse/stream-processor.ts"],"sourcesContent":["import { CONTEXT_OVERFLOW_CODE, EventType } from '@tanstack/ai'\nimport { buildConverseUsage } from './usage'\nimport type { AdapterYieldChunk, TokenUsage } from '@tanstack/ai'\nimport type { ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime'\n\n/**\n * Converse delivers server-side failures — throttling, request validation,\n * mid-stream model faults, and service-unavailable — as in-band stream events\n * rather than thrown exceptions. If they were ignored the iterator would simply\n * end and the run would look like a clean, truncated success. Throw the\n * underlying exception (these SDK members extend `Error`) so the adapter's\n * `chatStream` / `structuredOutputStream` catch converts it into a `RUN_ERROR`.\n */\nexport function throwIfConverseStreamError(ev: ConverseStreamOutput): void {\n if ('internalServerException' in ev && ev.internalServerException) {\n throw ev.internalServerException\n }\n if ('modelStreamErrorException' in ev && ev.modelStreamErrorException) {\n throw ev.modelStreamErrorException\n }\n if ('validationException' in ev && ev.validationException) {\n throw ev.validationException\n }\n if ('throttlingException' in ev && ev.throttlingException) {\n throw ev.throttlingException\n }\n if ('serviceUnavailableException' in ev && ev.serviceUnavailableException) {\n throw ev.serviceUnavailableException\n }\n}\n\n/** Map Converse blocks to run, text, reasoning, and tool events.\n * Keep signatures and encrypted bytes separate for each contentBlockIndex.\n * Finish after trailing usage events. The adapter handles thrown errors.\n */\nexport async function* processConverseStream(\n stream: AsyncIterable<ConverseStreamOutput>,\n newMessageId: () => string,\n lifecycle: { threadId: string; parentRunId?: string; model?: string },\n): AsyncIterable<AdapterYieldChunk> {\n const runId = newMessageId()\n const { threadId, parentRunId, model } = lifecycle\n const messageId = newMessageId()\n\n let hasEmittedRunStarted = false\n\n // Text lifecycle\n let accumulatedContent = ''\n let hasEmittedTextMessageStart = false\n\n // Reasoning lifecycle\n const reasoningByIndex = new Map<\n number,\n {\n id: string\n signature: string\n bytes: Array<Uint8Array>\n redacted: boolean\n closed: boolean\n encrypted?: string\n }\n >()\n\n // Tool-call lifecycle, keyed by Converse contentBlockIndex. Converse opens a\n // tool-use block with `contentBlockStart`, streams arg fragments via\n // `contentBlockDelta`, and closes it with `contentBlockStop`.\n const toolCallsByIndex = new Map<\n number,\n {\n id: string\n name: string\n started: boolean\n arguments: string\n }\n >()\n\n // Usage + finish-reason are captured during iteration and folded into the\n // single terminal RUN_FINISHED, matching openai-base's deferred-finish\n // contract (usage may arrive after the finish signal).\n let usage: TokenUsage | undefined\n let finishReason: NonNullable<AdapterYieldChunk['finishReason']> | undefined\n // The model filled its context window before it finished (stopReason\n // `model_context_window_exceeded`). The answer is cut off, so the run ends\n // with a RUN_ERROR instead of a clean RUN_FINISHED.\n let contextWindowExceeded = false\n\n // Lazily emit RUN_STARTED exactly once, before the first content event.\n function* ensureRunStarted(): Generator<AdapterYieldChunk> {\n if (hasEmittedRunStarted) return\n hasEmittedRunStarted = true\n yield {\n type: EventType.RUN_STARTED,\n runId,\n threadId,\n parentRunId,\n ...(model && { model }),\n timestamp: Date.now(),\n }\n }\n\n // Close an open reasoning message before text/tool content begins, mirroring\n // openai-base which always emits REASONING_MESSAGE_END before TEXT_MESSAGE_START.\n function* encryptedReasoning(index: number): Generator<AdapterYieldChunk> {\n const reasoning = reasoningByIndex.get(index)\n if (!reasoning) return\n const encrypted = reasoning.redacted\n ? Buffer.concat(reasoning.bytes).toString('base64')\n : reasoning.signature\n if (!encrypted || encrypted === reasoning.encrypted) return\n reasoning.encrypted = encrypted\n yield {\n type: EventType.REASONING_ENCRYPTED_VALUE,\n entityId: reasoning.id,\n subtype: 'message',\n encryptedValue: encrypted,\n timestamp: Date.now(),\n }\n }\n\n function* closeReasoning(index?: number): Generator<AdapterYieldChunk> {\n for (const [blockIndex, reasoning] of reasoningByIndex) {\n if (index !== undefined && index !== blockIndex) continue\n yield* encryptedReasoning(blockIndex)\n if (reasoning.closed) continue\n reasoning.closed = true\n yield {\n type: EventType.REASONING_MESSAGE_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n yield {\n type: EventType.REASONING_END,\n messageId: reasoning.id,\n timestamp: Date.now(),\n }\n }\n }\n\n for await (const ev of stream) {\n yield* ensureRunStarted()\n\n // Surface in-band server/throttle/validation errors instead of dropping them.\n throwIfConverseStreamError(ev)\n\n // messageStart carries only the role; no AG-UI event maps to it.\n if ('messageStart' in ev) continue\n\n if ('contentBlockStart' in ev) {\n const start = ev.contentBlockStart\n const toolUse = start?.start?.toolUse\n if (start && toolUse) {\n yield* closeReasoning()\n const id = toolUse.toolUseId ?? newMessageId()\n const name = toolUse.name ?? ''\n const index = start.contentBlockIndex ?? 0\n toolCallsByIndex.set(index, {\n id,\n name,\n started: true,\n arguments: '',\n })\n yield {\n type: EventType.TOOL_CALL_START,\n toolCallId: id,\n toolCallName: name,\n timestamp: Date.now(),\n index,\n }\n }\n continue\n }\n\n if ('contentBlockDelta' in ev) {\n const block = ev.contentBlockDelta\n const delta = block?.delta\n const index = block?.contentBlockIndex ?? 0\n\n // Tool-call argument fragments (partial JSON).\n if (delta && 'toolUse' in delta && delta.toolUse?.input !== undefined) {\n const toolCall = toolCallsByIndex.get(index)\n if (toolCall?.started) {\n toolCall.arguments += delta.toolUse.input\n yield {\n type: EventType.TOOL_CALL_ARGS,\n toolCallId: toolCall.id,\n timestamp: Date.now(),\n delta: delta.toolUse.input,\n }\n }\n continue\n }\n\n if (delta && 'reasoningContent' in delta && delta.reasoningContent) {\n const fragment = delta.reasoningContent\n let reasoning = reasoningByIndex.get(index)\n if (!reasoning) {\n const redacted =\n 'redactedContent' in fragment &&\n (fragment.redactedContent?.length ?? 0) > 0\n reasoning = {\n id: (redacted ? 'redacted_thinking-' : '') + newMessageId(),\n signature: '',\n bytes: [],\n redacted,\n closed: false,\n }\n reasoningByIndex.set(index, reasoning)\n yield {\n type: EventType.REASONING_MESSAGE_START,\n messageId: reasoning.id,\n role: 'reasoning',\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'text' in fragment &&\n fragment.text !== undefined\n ) {\n yield {\n type: EventType.REASONING_MESSAGE_CONTENT,\n messageId: reasoning.id,\n delta: fragment.text,\n timestamp: Date.now(),\n }\n }\n if (\n !reasoning.redacted &&\n 'signature' in fragment &&\n fragment.signature !== undefined\n )\n reasoning.signature += fragment.signature\n if (\n 'redactedContent' in fragment &&\n fragment.redactedContent &&\n fragment.redactedContent.length > 0\n ) {\n if (!reasoning.redacted) reasoning.encrypted = undefined\n reasoning.redacted = true\n reasoning.signature = ''\n reasoning.bytes.push(fragment.redactedContent)\n yield* encryptedReasoning(index)\n }\n continue\n }\n\n // Text content.\n if (delta && 'text' in delta && delta.text !== undefined) {\n yield* closeReasoning()\n if (!hasEmittedTextMessageStart) {\n hasEmittedTextMessageStart = true\n yield {\n type: EventType.TEXT_MESSAGE_START,\n messageId,\n role: 'assistant',\n timestamp: Date.now(),\n }\n }\n accumulatedContent += delta.text\n yield {\n type: EventType.TEXT_MESSAGE_CONTENT,\n messageId,\n delta: delta.text,\n content: accumulatedContent,\n timestamp: Date.now(),\n }\n }\n continue\n }\n\n if ('contentBlockStop' in ev) {\n const stopIndex = ev.contentBlockStop?.contentBlockIndex ?? 0\n yield* closeReasoning(stopIndex)\n const toolCall = toolCallsByIndex.get(stopIndex)\n if (toolCall?.started) {\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(stopIndex)\n }\n continue\n }\n\n if ('messageStop' in ev) {\n const stopReason: string | undefined = ev.messageStop?.stopReason\n contextWindowExceeded = stopReason === 'model_context_window_exceeded'\n // Map Converse stopReason to AG-UI's narrower finishReason vocabulary.\n finishReason =\n stopReason === 'tool_use'\n ? 'tool_calls'\n : stopReason === 'max_tokens'\n ? 'length'\n : stopReason === 'content_filtered' ||\n stopReason === 'guardrail_intervened'\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 // Stream ended (possibly without any content) — still emit RUN_STARTED so\n // consumers always see a run lifecycle.\n yield* ensureRunStarted()\n\n // Drain any tool call that opened but never received contentBlockStop.\n for (const [index, toolCall] of toolCallsByIndex) {\n if (!toolCall.started) continue\n yield {\n type: EventType.TOOL_CALL_END,\n toolCallId: toolCall.id,\n args: toolCall.arguments,\n toolCallName: toolCall.name,\n timestamp: Date.now(),\n }\n toolCallsByIndex.delete(index)\n }\n\n // Close the text message lifecycle if it was opened.\n if (hasEmittedTextMessageStart) {\n yield {\n type: EventType.TEXT_MESSAGE_END,\n messageId,\n timestamp: Date.now(),\n }\n }\n\n // Close any reasoning lifecycle that text never closed.\n yield* closeReasoning()\n\n if (contextWindowExceeded) {\n const message =\n 'The response was cut off because the model reached the end of its context window.'\n yield {\n type: EventType.RUN_ERROR,\n ...(model && { model }),\n timestamp: Date.now(),\n message,\n code: CONTEXT_OVERFLOW_CODE,\n error: { message, code: CONTEXT_OVERFLOW_CODE },\n ...(usage && { usage }),\n }\n return\n }\n\n // Single terminal RUN_FINISHED. Conditional `usage` spread keeps the wire\n // shape spec-compliant (AG-UI's `usage` is optional with no `| undefined`).\n yield {\n type: EventType.RUN_FINISHED,\n runId,\n threadId,\n timestamp: Date.now(),\n finishReason: finishReason ?? 'stop',\n ...(usage && { usage }),\n }\n}\n"],"mappings":";;;;;;;;;;;AAaA,SAAgB,2BAA2B,IAAgC;CACzE,IAAI,6BAA6B,MAAM,GAAG,yBACxC,MAAM,GAAG;CAEX,IAAI,+BAA+B,MAAM,GAAG,2BAC1C,MAAM,GAAG;CAEX,IAAI,yBAAyB,MAAM,GAAG,qBACpC,MAAM,GAAG;CAEX,IAAI,yBAAyB,MAAM,GAAG,qBACpC,MAAM,GAAG;CAEX,IAAI,iCAAiC,MAAM,GAAG,6BAC5C,MAAM,GAAG;AAEb;;;;;AAMA,gBAAuB,sBACrB,QACA,cACA,WACkC;CAClC,MAAM,QAAQ,aAAa;CAC3B,MAAM,EAAE,UAAU,aAAa,UAAU;CACzC,MAAM,YAAY,aAAa;CAE/B,IAAI,uBAAuB;CAG3B,IAAI,qBAAqB;CACzB,IAAI,6BAA6B;CAGjC,MAAM,mCAAmB,IAAI,IAU3B;CAKF,MAAM,mCAAmB,IAAI,IAQ3B;CAKF,IAAI;CACJ,IAAI;CAIJ,IAAI,wBAAwB;CAG5B,UAAU,mBAAiD;EACzD,IAAI,sBAAsB;EAC1B,uBAAuB;EACvB,MAAM;GACJ,MAAM,UAAU;GAChB;GACA;GACA;GACA,GAAI,SAAS,EAAE,MAAM;GACrB,WAAW,KAAK,IAAI;EACtB;CACF;CAIA,UAAU,mBAAmB,OAA6C;EACxE,MAAM,YAAY,iBAAiB,IAAI,KAAK;EAC5C,IAAI,CAAC,WAAW;EAChB,MAA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AAS,EAAE,MAAM;GACrB,WAAW,KAAK,IAAI;GACpB;GACA,MAAM;GACN,OAAO;IAAE;IAAS,MAAM;GAAsB;GAC9C,GAAI,SAAS,EAAE,MAAM;EACvB;EACA;CACF;CAIA,MAAM;EACJ,MAAM,UAAU;EAChB;EACA;EACA,WAAW,KAAK,IAAI;EACpB,cAAc,gBAAgB;EAC9B,GAAI,SAAS,EAAE,MAAM;CACvB;AACF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/converse/structured-output.ts
|
|
2
2
|
var STRUCTURED_TOOL_NAME = "structured_output";
|
|
3
3
|
/**
|
|
4
4
|
* Converse has no native json_schema response_format. Structured output is
|
|
@@ -15,7 +15,6 @@ function buildStructuredToolConfig(schema) {
|
|
|
15
15
|
toolChoice: { tool: { name: STRUCTURED_TOOL_NAME } }
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
-
//#endregion
|
|
19
18
|
export { STRUCTURED_TOOL_NAME, buildStructuredToolConfig };
|
|
20
19
|
|
|
21
20
|
//# sourceMappingURL=structured-output.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/converse/tool-converter.ts
|
|
2
2
|
function toToolConfig(tools, choice) {
|
|
3
3
|
if (!tools.length) return void 0;
|
|
4
4
|
if (choice === "none") return void 0;
|
|
@@ -18,7 +18,6 @@ function mapChoice(choice) {
|
|
|
18
18
|
if (choice === "none") return void 0;
|
|
19
19
|
return { tool: { name: choice.name } };
|
|
20
20
|
}
|
|
21
|
-
//#endregion
|
|
22
21
|
export { toToolConfig };
|
|
23
22
|
|
|
24
23
|
//# sourceMappingURL=tool-converter.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { buildBaseUsage } from "@tanstack/ai";
|
|
2
|
-
|
|
2
|
+
// src/converse/usage.ts
|
|
3
3
|
/**
|
|
4
4
|
* Build normalized {@link TokenUsage} from a Converse `usage` object.
|
|
5
5
|
*
|
|
@@ -29,7 +29,6 @@ function buildConverseUsage(usage) {
|
|
|
29
29
|
if (Object.keys(promptTokensDetails).length > 0) result.promptTokensDetails = promptTokensDetails;
|
|
30
30
|
return result;
|
|
31
31
|
}
|
|
32
|
-
//#endregion
|
|
33
32
|
export { buildConverseUsage };
|
|
34
33
|
|
|
35
34
|
//# sourceMappingURL=usage.js.map
|
package/dist/esm/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { resolveBedrockAuth } from "./utils/auth.js";
|
|
2
|
-
import { withBedrockDefaults } from "./utils/client.js";
|
|
3
|
-
import { BEDROCK_CHAT_MODELS, BEDROCK_CONVERSE_MODELS, BEDROCK_EMBEDDING_MODELS, BEDROCK_RERANK_MODELS, BEDROCK_RESPONSES_MODELS } from "./model-meta.js";
|
|
4
2
|
import { BedrockTextAdapter, createBedrockChat } from "./adapters/text.js";
|
|
5
3
|
import { BedrockResponsesTextAdapter, createBedrockResponsesText } from "./adapters/responses-text.js";
|
|
6
4
|
import { BedrockConverseTextAdapter, createBedrockConverse } from "./adapters/converse-text.js";
|
|
5
|
+
import { BEDROCK_CHAT_MODELS, BEDROCK_CONVERSE_MODELS, BEDROCK_EMBEDDING_MODELS, BEDROCK_RERANK_MODELS, BEDROCK_RESPONSES_MODELS } from "./model-meta.js";
|
|
7
6
|
import { BedrockEmbeddingAdapter, bedrockEmbedding, createBedrockEmbedding } from "./adapters/embedding.js";
|
|
8
7
|
import { BedrockRerankAdapter, bedrockRerank } from "./adapters/rerank.js";
|
|
9
|
-
|
|
8
|
+
import { withBedrockDefaults } from "./utils/client.js";
|
|
9
|
+
// src/index.ts
|
|
10
10
|
/**
|
|
11
11
|
* @module @tanstack/ai-bedrock
|
|
12
12
|
*
|
|
@@ -61,7 +61,6 @@ function createBedrockText(model, apiKey, config) {
|
|
|
61
61
|
function bedrockText(model, config) {
|
|
62
62
|
return build(model, config);
|
|
63
63
|
}
|
|
64
|
-
//#endregion
|
|
65
64
|
export { BEDROCK_CHAT_MODELS, BEDROCK_CONVERSE_MODELS, BEDROCK_EMBEDDING_MODELS, BEDROCK_RERANK_MODELS, BEDROCK_RESPONSES_MODELS, BedrockConverseTextAdapter, BedrockEmbeddingAdapter, BedrockRerankAdapter, BedrockResponsesTextAdapter, BedrockTextAdapter, bedrockEmbedding, bedrockRerank, bedrockText, createBedrockChat, createBedrockConverse, createBedrockEmbedding, createBedrockResponsesText, createBedrockText, resolveBedrockAuth, withBedrockDefaults };
|
|
66
65
|
|
|
67
66
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/model-catalog.generated.ts
|
|
2
2
|
var GENERATED_BEDROCK_MODELS = [
|
|
3
3
|
{
|
|
4
4
|
id: "openai.gpt-oss-120b-1:0",
|
|
@@ -195,7 +195,6 @@ var GENERATED_BEDROCK_MODELS = [
|
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
197
|
];
|
|
198
|
-
//#endregion
|
|
199
198
|
export { GENERATED_BEDROCK_MODELS };
|
|
200
199
|
|
|
201
200
|
//# sourceMappingURL=model-catalog.generated.js.map
|
package/dist/esm/model-meta.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GENERATED_BEDROCK_MODELS } from "./model-catalog.generated.js";
|
|
2
|
-
|
|
2
|
+
// src/model-meta.ts
|
|
3
3
|
var BUDGET_OFF_HIGH = {
|
|
4
4
|
levels: {
|
|
5
5
|
off: "off",
|
|
@@ -70,7 +70,6 @@ var BEDROCK_EMBEDDING_MODELS = [
|
|
|
70
70
|
"cohere.embed-english-v3",
|
|
71
71
|
"cohere.embed-multilingual-v3"
|
|
72
72
|
];
|
|
73
|
-
//#endregion
|
|
74
73
|
export { BEDROCK_CHAT_MODELS, BEDROCK_CLAUDE_NO_FORCED_TOOL_FAMILIES, BEDROCK_CONVERSE_MODELS, BEDROCK_EMBEDDING_MODELS, BEDROCK_MODEL_INPUT_MODALITIES, BEDROCK_MODEL_REASONING, BEDROCK_RERANK_MODELS, BEDROCK_RESPONSES_MODELS };
|
|
75
74
|
|
|
76
75
|
//# sourceMappingURL=model-meta.js.map
|
package/dist/esm/utils/auth.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/utils/auth.ts
|
|
2
2
|
/** SigV4 service name differs per endpoint. */
|
|
3
3
|
function sigv4Service(endpoint) {
|
|
4
4
|
return endpoint === "mantle" ? "bedrock-mantle" : "bedrock";
|
|
@@ -44,7 +44,6 @@ function resolveBedrockAuth(config, endpoint) {
|
|
|
44
44
|
}
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
|
-
//#endregion
|
|
48
47
|
export { importOptionalPeer, resolveBedrockAuth, sigv4Service };
|
|
49
48
|
|
|
50
49
|
//# sourceMappingURL=auth.js.map
|
package/dist/esm/utils/client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { resolveBedrockAuth } from "./auth.js";
|
|
2
2
|
import { createSigV4Fetch } from "./openai-sigv4-fetch.js";
|
|
3
3
|
import { mantlePathForModel } from "../api-compatibility.js";
|
|
4
|
-
|
|
4
|
+
// src/utils/client.ts
|
|
5
5
|
var DEFAULT_REGION = "us-east-1";
|
|
6
6
|
/** The OpenAI client requires a non-empty apiKey even when a signed fetch overrides Authorization. */
|
|
7
7
|
var SIGV4_PLACEHOLDER_KEY = "bedrock-sigv4";
|
|
@@ -41,7 +41,6 @@ function withBedrockDefaults(config, forced, model) {
|
|
|
41
41
|
function isBedrockOpenAIContextOverflow(error) {
|
|
42
42
|
return error.code === "validation_error" && error.message.includes("maximum context length");
|
|
43
43
|
}
|
|
44
|
-
//#endregion
|
|
45
44
|
export { isBedrockOpenAIContextOverflow, resolveBedrockAuth, withBedrockDefaults };
|
|
46
45
|
|
|
47
46
|
//# sourceMappingURL=client.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { signSigV4 } from "./sigv4.js";
|
|
2
|
-
|
|
2
|
+
// src/utils/openai-sigv4-fetch.ts
|
|
3
3
|
/**
|
|
4
4
|
* Wraps a fetch so each request is SigV4-signed with the resolved AWS
|
|
5
5
|
* credentials (WebCrypto signer in `./sigv4`).
|
|
@@ -23,7 +23,6 @@ function createSigV4Fetch(auth, baseFetch = fetch) {
|
|
|
23
23
|
});
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
-
//#endregion
|
|
27
26
|
export { createSigV4Fetch };
|
|
28
27
|
|
|
29
28
|
//# sourceMappingURL=openai-sigv4-fetch.js.map
|
package/dist/esm/utils/sigv4.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/utils/sigv4.ts
|
|
2
2
|
var EMPTY_SHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
|
3
3
|
var UNSIGNABLE_HEADERS = /* @__PURE__ */ new Set([
|
|
4
4
|
"authorization",
|
|
@@ -127,7 +127,6 @@ async function signSigV4(request, credentials, options) {
|
|
|
127
127
|
headers.set("authorization", `AWS4-HMAC-SHA256 Credential=${credentials.accessKeyId}/${scope}, SignedHeaders=${signedHeaders}, Signature=${signature}`);
|
|
128
128
|
return headers;
|
|
129
129
|
}
|
|
130
|
-
//#endregion
|
|
131
130
|
export { signSigV4 };
|
|
132
131
|
|
|
133
132
|
//# sourceMappingURL=sigv4.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-bedrock",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Amazon Bedrock adapter for TanStack AI with chat, tools, reasoning, embeddings, and reranking.",
|
|
6
6
|
"author": "",
|
|
@@ -45,14 +45,14 @@
|
|
|
45
45
|
"@aws-sdk/credential-providers": "^3.1057.0",
|
|
46
46
|
"@smithy/signature-v4": "^5.4.5",
|
|
47
47
|
"@smithy/types": "^4.14.2",
|
|
48
|
-
"@vitest/coverage-v8": "4.1.
|
|
49
|
-
"vite": "^8.
|
|
48
|
+
"@vitest/coverage-v8": "4.1.12",
|
|
49
|
+
"vite": "^8.3.8"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"@aws-sdk/client-bedrock-runtime": "^3.1057.0",
|
|
53
53
|
"@aws-sdk/credential-providers": "^3.1057.0",
|
|
54
54
|
"@smithy/types": "^4.14.2",
|
|
55
|
-
"@tanstack/ai": "^1.
|
|
55
|
+
"@tanstack/ai": "^1.7.0"
|
|
56
56
|
},
|
|
57
57
|
"peerDependenciesMeta": {
|
|
58
58
|
"@aws-sdk/client-bedrock-runtime": {
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
69
|
"openai": "^7.30.0",
|
|
70
|
-
"@tanstack/openai-base": "^1.0
|
|
70
|
+
"@tanstack/openai-base": "^1.1.0"
|
|
71
71
|
},
|
|
72
72
|
"scripts": {
|
|
73
73
|
"build": "vite build",
|
|
@@ -730,6 +730,12 @@ export class BedrockConverseTextAdapter<
|
|
|
730
730
|
...(toolConfig && { toolConfig }),
|
|
731
731
|
...(inferenceConfig && { inferenceConfig }),
|
|
732
732
|
...(additionalModelRequestFields && { additionalModelRequestFields }),
|
|
733
|
+
...(modelOptions?.requestMetadata !== undefined && {
|
|
734
|
+
requestMetadata: modelOptions.requestMetadata,
|
|
735
|
+
}),
|
|
736
|
+
...(modelOptions?.serviceTier !== undefined && {
|
|
737
|
+
serviceTier: { type: modelOptions.serviceTier },
|
|
738
|
+
}),
|
|
733
739
|
}
|
|
734
740
|
return addPromptCachePoints(this.model, input, options.promptCache)
|
|
735
741
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import type { ServiceTierType } from '@aws-sdk/client-bedrock-runtime'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Provider options honored by the Bedrock **Converse** path. Deliberately
|
|
3
5
|
* narrower than the OpenAI-compatible `BedrockTextProviderOptions`: the Converse
|
|
4
|
-
* API
|
|
5
|
-
*
|
|
6
|
+
* API accepts these sampling knobs (mapped into `inferenceConfig`) and request
|
|
7
|
+
* options. Advertising the full Chat Completions option set would promise fields —
|
|
6
8
|
* `frequency_penalty`, `seed`, `logit_bias`, `tool_choice`, `reasoning_effort`,
|
|
7
|
-
* etc. — that Converse silently ignores.
|
|
9
|
+
* etc. — that Converse silently ignores. Sampling field names mirror the Chat
|
|
8
10
|
* Completions shape so callers have one mental model across the three APIs.
|
|
9
11
|
*/
|
|
10
12
|
export interface BedrockConverseProviderOptions {
|
|
@@ -16,4 +18,8 @@ export interface BedrockConverseProviderOptions {
|
|
|
16
18
|
max_completion_tokens?: number | null
|
|
17
19
|
/** Forwarded to Converse `inferenceConfig.stopSequences`. */
|
|
18
20
|
stop?: string | Array<string> | null
|
|
21
|
+
/** Request metadata for filtering Bedrock model invocation logs. */
|
|
22
|
+
requestMetadata?: Record<string, string>
|
|
23
|
+
/** Inference service tier, forwarded to Converse `serviceTier.type`. */
|
|
24
|
+
serviceTier?: ServiceTierType
|
|
19
25
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EventType } from '@tanstack/ai'
|
|
1
|
+
import { CONTEXT_OVERFLOW_CODE, EventType } from '@tanstack/ai'
|
|
2
2
|
import { buildConverseUsage } from './usage'
|
|
3
3
|
import type { AdapterYieldChunk, TokenUsage } from '@tanstack/ai'
|
|
4
4
|
import type { ConverseStreamOutput } from '@aws-sdk/client-bedrock-runtime'
|
|
@@ -79,6 +79,10 @@ export async function* processConverseStream(
|
|
|
79
79
|
// contract (usage may arrive after the finish signal).
|
|
80
80
|
let usage: TokenUsage | undefined
|
|
81
81
|
let finishReason: NonNullable<AdapterYieldChunk['finishReason']> | undefined
|
|
82
|
+
// The model filled its context window before it finished (stopReason
|
|
83
|
+
// `model_context_window_exceeded`). The answer is cut off, so the run ends
|
|
84
|
+
// with a RUN_ERROR instead of a clean RUN_FINISHED.
|
|
85
|
+
let contextWindowExceeded = false
|
|
82
86
|
|
|
83
87
|
// Lazily emit RUN_STARTED exactly once, before the first content event.
|
|
84
88
|
function* ensureRunStarted(): Generator<AdapterYieldChunk> {
|
|
@@ -282,7 +286,8 @@ export async function* processConverseStream(
|
|
|
282
286
|
}
|
|
283
287
|
|
|
284
288
|
if ('messageStop' in ev) {
|
|
285
|
-
const stopReason = ev.messageStop?.stopReason
|
|
289
|
+
const stopReason: string | undefined = ev.messageStop?.stopReason
|
|
290
|
+
contextWindowExceeded = stopReason === 'model_context_window_exceeded'
|
|
286
291
|
// Map Converse stopReason to AG-UI's narrower finishReason vocabulary.
|
|
287
292
|
finishReason =
|
|
288
293
|
stopReason === 'tool_use'
|
|
@@ -334,6 +339,21 @@ export async function* processConverseStream(
|
|
|
334
339
|
// Close any reasoning lifecycle that text never closed.
|
|
335
340
|
yield* closeReasoning()
|
|
336
341
|
|
|
342
|
+
if (contextWindowExceeded) {
|
|
343
|
+
const message =
|
|
344
|
+
'The response was cut off because the model reached the end of its context window.'
|
|
345
|
+
yield {
|
|
346
|
+
type: EventType.RUN_ERROR,
|
|
347
|
+
...(model && { model }),
|
|
348
|
+
timestamp: Date.now(),
|
|
349
|
+
message,
|
|
350
|
+
code: CONTEXT_OVERFLOW_CODE,
|
|
351
|
+
error: { message, code: CONTEXT_OVERFLOW_CODE },
|
|
352
|
+
...(usage && { usage }),
|
|
353
|
+
}
|
|
354
|
+
return
|
|
355
|
+
}
|
|
356
|
+
|
|
337
357
|
// Single terminal RUN_FINISHED. Conditional `usage` spread keeps the wire
|
|
338
358
|
// shape spec-compliant (AG-UI's `usage` is optional with no `| undefined`).
|
|
339
359
|
yield {
|