/** * AWS Bedrock model provider implementation. * * This module provides integration with AWS Bedrock's Converse API, * supporting streaming responses, tool use, and prompt caching. * * @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html */ import { type BedrockRuntimeClientConfig } from '@aws-sdk/client-bedrock-runtime'; import { type BaseModelConfig, type CacheConfig, type CountTokensOptions, Model, type StreamOptions } from '../models/model.js'; import type { Message } from '../types/messages.js'; import type { ModelStreamEvent } from '../models/streaming.js'; import type { JSONValue } from '../types/json.js'; /** * TTL durations accepted by Bedrock for prompt-cache checkpoints. * * Bedrock currently accepts `'5m'` (default) and `'1h'`. The `(string & {})` branch keeps * autocomplete on the known values while letting callers pass any string forward — Bedrock * validates the value server-side and rejects unsupported values with `ValidationException`, * so this stays correct as AWS adds new TTL values without an SDK update. * * Bedrock also requires checkpoint TTLs to be **non-increasing** across * `toolConfig` → system → messages — setting a longer TTL on a later checkpoint than an * earlier one will be rejected by the service. * * @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_CachePointBlock.html */ export type BedrockCacheTTL = '5m' | '1h' | (string & {}); /** * Bedrock-specific prompt-caching configuration. Narrows the TTL fields onto the common * {@link CacheConfig} for the Bedrock provider. */ export interface BedrockCacheConfig extends CacheConfig { /** TTL applied to the auto-injected cache point appended after `toolConfig.tools`. */ toolsTTL?: BedrockCacheTTL; /** TTL applied to the auto-injected cache point appended to the last user message. */ messagesTTL?: BedrockCacheTTL; } /** * Redaction configuration for Bedrock guardrails. * Controls whether and how blocked content is replaced. */ export interface BedrockGuardrailRedactionConfig { /** Redact input when blocked. @defaultValue true */ input?: boolean; /** Replacement message for redacted input. @defaultValue '[User input redacted.]' */ inputMessage?: string; /** Redact output when blocked. @defaultValue false */ output?: boolean; /** Replacement message for redacted output. @defaultValue '[Assistant output redacted.]' */ outputMessage?: string; } /** * Configuration for Bedrock guardrails. * * @see https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html */ export interface BedrockGuardrailConfig { /** Guardrail identifier */ guardrailIdentifier: string; /** Guardrail version (e.g., "1", "DRAFT") */ guardrailVersion: string; /** Trace mode for evaluation. @defaultValue 'enabled' */ trace?: 'enabled' | 'disabled' | 'enabled_full'; /** Stream processing mode */ streamProcessingMode?: 'sync' | 'async'; /** Redaction behavior when content is blocked */ redaction?: BedrockGuardrailRedactionConfig; /** * Only evaluate the latest user message with guardrails. * When true, wraps the latest user message's text/image content in guardContent blocks. * This can improve performance and reduce costs in multi-turn conversations. * * @remarks * The implementation finds the last user message containing text or image content * (not just the last message), ensuring correct behavior during tool execution cycles * where toolResult messages may follow the user's actual input. * * @defaultValue false */ guardLatestUserMessage?: boolean; } /** * Configuration interface for AWS Bedrock model provider. * * Extends BaseModelConfig with Bedrock-specific configuration options * for model parameters, caching, and additional request/response fields. * * @example * ```typescript * const config: BedrockModelConfig = { * modelId: 'global.anthropic.claude-sonnet-4-6', * maxTokens: 1024, * temperature: 0.7, * cacheConfig: { strategy: 'auto' } * } * ``` */ export interface BedrockModelConfig extends BaseModelConfig { /** * Maximum number of tokens to generate in the response. * * @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InferenceConfiguration.html */ maxTokens?: number; /** * Controls randomness in generation. * * @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InferenceConfiguration.html */ temperature?: number; /** * Controls diversity via nucleus sampling. * * @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InferenceConfiguration.html */ topP?: number; /** * Array of sequences that will stop generation when encountered. */ stopSequences?: string[]; /** * Configuration for prompt caching. * * @see https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html */ cacheConfig?: BedrockCacheConfig; /** * Additional fields to include in the Bedrock request. */ additionalRequestFields?: JSONValue; /** * Additional response field paths to extract from the Bedrock response. */ additionalResponseFieldPaths?: string[]; /** * Additional arguments to pass through to the Bedrock Converse API. * @see https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/ConverseStreamCommand/ */ additionalArgs?: JSONValue; /** * Whether or not to stream responses from the model. * * This will use the ConverseStream API instead of the Converse API. * * @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html * @see https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html */ stream?: boolean; /** * Flag to include status field in tool results. * - `true`: Always include status field * - `false`: Never include status field * - `'auto'`: Automatically determine based on model ID (default) */ includeToolResultStatus?: 'auto' | boolean; /** * Guardrail configuration for content filtering and safety controls. * @see https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html */ guardrailConfig?: BedrockGuardrailConfig; /** * Whether to use the native Bedrock CountTokens API. * * When `true`, `countTokens()` calls the Bedrock CountTokens API for * accurate counts. When `false` or not set (default), skips the API call and uses * the character-based heuristic estimator. * * @defaultValue false */ useNativeTokenCount?: boolean; } /** * Options for creating a BedrockModel instance. */ export interface BedrockModelOptions extends BedrockModelConfig { /** * AWS region to use for the Bedrock service. */ region?: string; /** * Configuration for the Bedrock Runtime client. */ clientConfig?: BedrockRuntimeClientConfig; /** * Amazon Bedrock API key for bearer token authentication. * When provided, requests use the API key instead of SigV4 signing. * @see https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys.html */ apiKey?: string; } /** * AWS Bedrock model provider implementation. * * Implements the Model interface for AWS Bedrock using the Converse Stream API. * Supports streaming responses, tool use, prompt caching, and comprehensive error handling. * * @example * ```typescript * const provider = new BedrockModel({ * modelConfig: { * modelId: 'global.anthropic.claude-sonnet-4-6', * maxTokens: 1024, * temperature: 0.7 * }, * clientConfig: { * region: 'us-west-2' * } * }) * * const messages: Message[] = [ * { type: 'message', role: 'user', content: [{ type: 'textBlock', text: 'Hello!' }] } * ] * * for await (const event of provider.stream(messages)) { * if (event.type === 'modelContentBlockDeltaEvent' && event.delta.type === 'textDelta') { * process.stdout.write(event.delta.text) * } * } * ``` */ export declare class BedrockModel extends Model { private _config; private _client; /** * Clears the cache of model IDs for which CountTokens is skipped. * After calling this, the next countTokens invocation will attempt the API again. * * @internal */ static clearCountTokensCache(): void; /** * Creates a new BedrockModel instance. * * @param options - Optional configuration for model and client * * @example * ```typescript * // Minimal configuration with defaults * const provider = new BedrockModel({ * region: 'us-west-2' * }) * * // With model configuration * const provider = new BedrockModel({ * region: 'us-west-2', * modelId: 'global.anthropic.claude-sonnet-4-6', * maxTokens: 2048, * temperature: 0.8, * cacheConfig: { strategy: 'auto' } * }) * * // With client configuration * const provider = new BedrockModel({ * region: 'us-east-1', * clientConfig: { * credentials: myCredentials * } * }) * ``` */ constructor(options?: BedrockModelOptions); /** * Returns the cache strategy for this model based on its model ID. * Returns the appropriate cache strategy name, or null if automatic caching is not supported. * * @returns Cache strategy name or null */ private _getCacheStrategy; /** * Determines if caching should be enabled. * Returns true when: * - strategy is 'anthropic' (explicit enable) * - strategy is 'auto' and model supports caching (auto-detect) * * @returns True if caching should be enabled */ private _shouldEnableCaching; /** * Updates the model configuration. * Merges the provided configuration with existing settings. * * @param modelConfig - Configuration object with model-specific settings to update * * @example * ```typescript * // Update temperature and maxTokens * provider.updateConfig({ * temperature: 0.9, * maxTokens: 2048 * }) * ``` */ updateConfig(modelConfig: BedrockModelConfig): void; /** * Retrieves the current model configuration. * * @returns The current configuration object * * @example * ```typescript * const config = provider.getConfig() * console.log(config.modelId) * ``` */ getConfig(): BedrockModelConfig; /** * Count tokens using Bedrock's native CountTokens API. * * Uses the same message format as the Converse API to get accurate token counts * directly from the Bedrock service. Falls back to the base class heuristic on failure. * * @param messages - Array of conversation messages to count tokens for * @param options - Optional options containing system prompt and tool specs * @returns Total input token count */ countTokens(messages: Message[], options?: CountTokensOptions): Promise; /** * Streams a conversation with the Bedrock model. * Returns an async iterable that yields streaming events as they occur. * * @param messages - Array of conversation messages * @param options - Optional streaming configuration * @returns Async iterable of streaming events * * @throws \{ContextWindowOverflowError\} When input exceeds the model's context window * @throws \{ModelThrottledError\} When Bedrock service throttles requests * * @example * ```typescript * const messages: Message[] = [ * { type: 'message', role: $1, content: [{ type: 'textBlock', text: 'What is 2+2?' }] } * ] * * const options: StreamOptions = { * systemPrompt: 'You are a helpful math assistant.', * toolSpecs: [calculatorTool] * } * * for await (const event of provider.stream(messages, options)) { * if (event.type === 'modelContentBlockDeltaEvent') { * console.log(event.delta) * } * } * ``` */ stream(messages: Message[], options?: StreamOptions): AsyncIterable; /** * Formats a request for the Bedrock Converse Stream API. * * @param messages - Conversation messages * @param options - Stream options * @returns Formatted Bedrock request */ private _formatRequest; /** * Get additional request fields, adjusted for compatibility with the current stream options. * * Certain additional request fields are incompatible with specific API options. For example, * Bedrock does not allow thinking mode when tool_choice forces tool use. * * @param options - The stream options for the current request * @returns The additional request fields, or undefined if none */ private _getAdditionalRequestFields; /** * Formats messages for Bedrock API. * * @param messages - SDK messages * @returns Bedrock-formatted messages */ private _formatMessages; /** * Inject a cache point at the end of the last user message. * Strips any existing cache points from all messages first. * * @param messages - List of messages to inject cache point into (modified in place) */ private _injectCachePoint; /** * Wraps a formatted content block in guardContent for guardrail evaluation. * * When guardLatestUserMessage is enabled, this method wraps text and image blocks * in guardContent blocks to signal to Bedrock's guardrails to evaluate only that content. * Other content types (toolUse, toolResult, etc.) pass through unchanged. * * @param formattedBlock - The formatted content block to potentially wrap * @returns The block wrapped in guardContent if applicable, or the original block */ private _applyGuardBlocks; /** * Find the index of the last user message containing text or image content. * * This is used for guardLatestUserMessage guardrail evaluation to ensure that guardContent * wrapping targets the correct message even when toolResult messages (role='user') follow * the actual user text/image input during tool execution cycles. * * @param messages - Array of messages to search * @returns Index of the last user message with text/image content, or undefined if not found */ private _findLastUserTextMessageIndex; /** * Determines whether to include the status field in tool results. * * Uses the includeToolResultStatus config option: * - If explicitly true, always include status * - If explicitly false, never include status * - If 'auto' (default), check if model ID matches known patterns * * @returns True if status field should be included, false otherwise */ private _shouldIncludeToolResultStatus; /** * Formats a content block for Bedrock API. * * @param block - SDK content block * @returns Bedrock-formatted content block */ private _formatContentBlock; /** * Format media source (image/video) for Bedrock API. * Handles bytes, S3 locations, and s3:// URLs. * * @param source - Media source * @returns Formatted source for Bedrock API */ private _formatMediaSource; /** * Format document source for Bedrock API. * Handles bytes, text, content, and S3 locations. * Note: Bedrock API only accepts bytes, content, or s3Location - text is converted to bytes. * * @param source - Document source * @returns Formatted source for Bedrock API */ private _formatDocumentSource; private _mapBedrockEventToSDKEvent; /** * Maps a Bedrock event to SDK streaming events. * * @param chunk - Bedrock event chunk * @param lastStopReason - Stop reason from previous messageStop event * @returns Object containing events array and optional stopReason */ private _mapStreamedBedrockEventToSDKEvent; /** * Transforms a Bedrock stop reason into the SDK's format. * * @param stopReasonRaw - The raw stop reason string from Bedrock. * @param event - The full event output, used to check for tool_use adjustments. * @returns The transformed stop reason. */ private _transformStopReason; /** * Maps a Bedrock object-key citation location to the SDK's type-field format. * * Bedrock uses object-key discrimination (`{ documentChar: { ... } }`) while the SDK uses * type-field discrimination (`{ type: 'documentChar', ... }`). Also normalizes Bedrock's * `searchResultLocation` key to the shorter `searchResult`. * * @param bedrockLocation - Bedrock citation location with object-key discrimination * @returns SDK CitationLocation with type field discrimination */ private _mapBedrockCitationLocation; /** * Maps a Bedrock CitationsContentBlock to SDK CitationsBlockData. * * @param bedrockData - Bedrock CitationsContentBlock * @returns SDK CitationsBlockData with type-field CitationLocations */ private _mapBedrockCitationsData; /** * Maps an SDK Citation to Bedrock's Citation format. * * @param citation - SDK Citation with type-field location * @returns Bedrock Citation with object-key location */ private _mapCitationToBedrock; /** * Maps an SDK CitationLocation to Bedrock's object-key format. * * @param location - SDK CitationLocation with type field * @returns Bedrock CitationLocation with object-key discrimination */ private _mapCitationLocationToBedrock; /** * Generate redaction events based on guardrail configuration. * * @param guardrailData - The guardrail trace assessment data * @returns Array of redaction events to emit */ private _generateRedactionEvents; } //# sourceMappingURL=bedrock.d.ts.map