import type { JSONValue, Serialized, MaybeSerializedInput, JSONSerializable } from './json.js'; import type { ImageBlockData, VideoBlockData, DocumentBlockData } from './media.js'; import { ImageBlock, VideoBlock, DocumentBlock } from './media.js'; import type { CitationsBlockData } from './citations.js'; import { CitationsBlock } from './citations.js'; import type { Usage, Metrics } from '../models/streaming.js'; /** * Message types and content blocks for conversational AI interactions. * * This module follows a pattern where "Data" interfaces define the structure * for objects, while corresponding classes extend those interfaces with additional * functionality and type discrimination. */ /** * Optional metadata attached to a message. * * Not sent to model providers — model providers construct their own message format * from `role` and `content` only. Persisted alongside the message in session storage. */ export interface MessageMetadata { /** Token usage information from the model response. */ usage?: Usage; /** Performance metrics from the model response. */ metrics?: Metrics; /** Arbitrary user/framework metadata (e.g. compression provenance). */ custom?: Record; } /** * Data for a message. */ export interface MessageData { /** * The role of the message sender. */ role: Role; /** * Array of content blocks that make up this message. */ content: ContentBlockData[]; /** * Optional metadata, not sent to model providers. */ metadata?: MessageMetadata; } /** * A message in a conversation between user and assistant. * Each message has a role (user or assistant) and an array of content blocks. */ export declare class Message implements JSONSerializable { /** * Discriminator for message type. */ readonly type: "message"; /** * The role of the message sender. */ readonly role: Role; /** * Array of content blocks that make up this message. */ readonly content: ContentBlock[]; /** * Optional metadata, not sent to model providers. */ metadata?: MessageMetadata; constructor(data: { role: Role; content: ContentBlock[]; metadata?: MessageMetadata; }); /** * Creates a Message instance from MessageData. */ static fromMessageData(data: MessageData): Message; /** * Serializes the Message to a JSON-compatible MessageData object. * Called automatically by JSON.stringify(). */ toJSON(): MessageData; /** * Creates a Message instance from MessageData. * Alias for fromMessageData for API consistency. * * @param data - MessageData to deserialize * @returns Message instance */ static fromJSON(data: MessageData): Message; /** * Creates a deep copy of this Message (round-trips through serialization). */ clone(): Message; } /** * Role of a message in a conversation. * Can be either 'user' (human input) or 'assistant' (model response). */ export type Role = 'user' | 'assistant'; /** * A block of content within a message. * Content blocks can contain text, tool usage requests, tool results, reasoning content, cache points, guard content, or media (image, video, document). * * This is a discriminated union where the object key determines the content format. * * @example * ```typescript * if ('text' in block) { * console.log(block.text.text) * } * ``` */ export type ContentBlockData = TextBlockData | { toolUse: ToolUseBlockData; } | { toolResult: ToolResultBlockData; } | { reasoning: ReasoningBlockData; } | { cachePoint: CachePointBlockData; } | { guardContent: GuardContentBlockData; } | { image: ImageBlockData; } | { video: VideoBlockData; } | { document: DocumentBlockData; } | { citations: CitationsBlockData; }; export type ContentBlock = TextBlock | ToolUseBlock | ToolResultBlock | ReasoningBlock | CachePointBlock | GuardContentBlock | ImageBlock | VideoBlock | DocumentBlock | CitationsBlock; /** * Data for a text block. */ export interface TextBlockData { /** * Plain text content. */ text: string; } /** * Text content block within a message. */ export declare class TextBlock implements TextBlockData, JSONSerializable { /** * Discriminator for text content. */ readonly type: "textBlock"; /** * Plain text content. */ readonly text: string; constructor(data: string); /** * Serializes the TextBlock to a JSON-compatible TextBlockData object. * Called automatically by JSON.stringify(). */ toJSON(): TextBlockData; /** * Creates a TextBlock instance from TextBlockData. * * @param data - TextBlockData to deserialize * @returns TextBlock instance */ static fromJSON(data: TextBlockData): TextBlock; } /** * Data for a tool use block. */ export interface ToolUseBlockData { /** * The name of the tool to execute. */ name: string; /** * Unique identifier for this tool use instance. */ toolUseId: string; /** * The input parameters for the tool. * This can be any JSON-serializable value. */ input: JSONValue; /** * Reasoning signature from thinking models (e.g., Gemini). * Must be preserved and sent back to the model for multi-turn tool use. */ reasoningSignature?: string; } /** * Tool use content block. */ export declare class ToolUseBlock implements ToolUseBlockData, JSONSerializable<{ toolUse: ToolUseBlockData; }> { /** * Discriminator for tool use content. */ readonly type: "toolUseBlock"; /** * The name of the tool to execute. */ readonly name: string; /** * Unique identifier for this tool use instance. */ readonly toolUseId: string; /** * The input parameters for the tool. * This can be any JSON-serializable value. */ readonly input: JSONValue; /** * Reasoning signature from thinking models (e.g., Gemini). * Must be preserved and sent back to the model for multi-turn tool use. */ readonly reasoningSignature?: string; constructor(data: ToolUseBlockData); /** * Serializes the ToolUseBlock to a JSON-compatible ContentBlockData object. * Called automatically by JSON.stringify(). */ toJSON(): { toolUse: ToolUseBlockData; }; /** * Creates a ToolUseBlock instance from its wrapped data format. * * @param data - Wrapped ToolUseBlockData to deserialize * @returns ToolUseBlock instance */ static fromJSON(data: { toolUse: ToolUseBlockData; }): ToolUseBlock; } /** * Content within a tool result. * Can be text, structured JSON data, or media blocks (image, video, document). * * This is a discriminated union where the object key determines the content format. */ export type ToolResultContentData = TextBlockData | JsonBlockData | { image: ImageBlockData; } | { video: VideoBlockData; } | { document: DocumentBlockData; }; export type ToolResultContent = TextBlock | JsonBlock | ImageBlock | VideoBlock | DocumentBlock; /** * Data for a tool result block. */ export interface ToolResultBlockData { /** * The ID of the tool use that this result corresponds to. */ toolUseId: string; /** * Status of the tool execution. */ status: 'success' | 'error'; /** * The content returned by the tool. */ content: ToolResultContentData[]; /** * The original error object when status is 'error'. * Available for inspection by hooks, error handlers, and agent loop. * Tools must wrap non-Error thrown values into Error objects. */ error?: Error; } /** * Tool result content block. */ export declare class ToolResultBlock implements JSONSerializable<{ toolResult: ToolResultBlockData; }> { /** * Discriminator for tool result content. */ readonly type: "toolResultBlock"; /** * The ID of the tool use that this result corresponds to. */ readonly toolUseId: string; /** * Status of the tool execution. */ readonly status: 'success' | 'error'; /** * The content returned by the tool. */ readonly content: ToolResultContent[]; /** * The original error object when status is 'error'. * Available for inspection by hooks, error handlers, and agent loop. * Tools must wrap non-Error thrown values into Error objects. */ readonly error?: Error; constructor(data: { toolUseId: string; status: 'success' | 'error'; content: ToolResultContent[]; error?: Error; }); /** * Serializes the ToolResultBlock to a JSON-compatible ContentBlockData object. * Called automatically by JSON.stringify(). * Note: The error field is not serialized (deferred for future implementation). */ toJSON(): { toolResult: ToolResultBlockData; }; /** * Creates a ToolResultBlock instance from its wrapped data format. * * @param data - Wrapped ToolResultBlockData to deserialize * @returns ToolResultBlock instance */ static fromJSON(data: { toolResult: ToolResultBlockData; }): ToolResultBlock; } /** * Converts a single ToolResultContentData to a ToolResultContent class instance. * * @param data - The tool result content data to convert * @returns A ToolResultContent instance of the appropriate type * @throws Error if the content data type is unknown */ export declare function toolResultContentFromData(data: ToolResultContentData): ToolResultContent; /** * Data for a reasoning block. */ export interface ReasoningBlockData { /** * The text content of the reasoning process. */ text?: string; /** * A cryptographic signature for verification purposes. */ signature?: string; /** * The redacted content of the reasoning process. */ redactedContent?: Uint8Array; } /** * Reasoning content block within a message. */ export declare class ReasoningBlock implements ReasoningBlockData, JSONSerializable<{ reasoning: Serialized; }> { /** * Discriminator for reasoning content. */ readonly type: "reasoningBlock"; /** * The text content of the reasoning process. */ readonly text?: string; /** * A cryptographic signature for verification purposes. */ readonly signature?: string; /** * The redacted content of the reasoning process. */ readonly redactedContent?: Uint8Array; constructor(data: ReasoningBlockData); /** * Serializes the ReasoningBlock to a JSON-compatible ContentBlockData object. * Called automatically by JSON.stringify(). * Uint8Array redactedContent is encoded as base64 string. */ toJSON(): { reasoning: Serialized; }; /** * Creates a ReasoningBlock instance from its wrapped data format. * Base64-encoded redactedContent is decoded back to Uint8Array. * * @param data - Wrapped ReasoningBlockData to deserialize (accepts both string and Uint8Array for redactedContent) * @returns ReasoningBlock instance */ static fromJSON(data: { reasoning: MaybeSerializedInput; }): ReasoningBlock; } /** * Data for a cache point block. */ export interface CachePointBlockData { /** * The cache type. Currently only 'default' is supported. */ cacheType: 'default'; /** * Optional TTL for the cache entry. When omitted, the provider's default TTL is used. * * The accepted value space is provider-specific. For example, the Bedrock provider only * accepts the values defined by `BedrockCacheTTL` (`'5m'` and `'1h'`). Other providers * may accept different values or ignore this field. */ ttl?: string; } /** * Cache point block for prompt caching. * Marks a position in a message or system prompt where caching should occur. */ export declare class CachePointBlock implements CachePointBlockData, JSONSerializable<{ cachePoint: CachePointBlockData; }> { /** * Discriminator for cache point. */ readonly type: "cachePointBlock"; /** * The cache type. Currently only 'default' is supported. */ readonly cacheType: 'default'; /** * Optional TTL for the cache entry. See {@link CachePointBlockData.ttl} for the * provider-specific value space. */ readonly ttl?: string; constructor(data: CachePointBlockData); /** * Serializes the CachePointBlock to a JSON-compatible ContentBlockData object. * Called automatically by JSON.stringify(). */ toJSON(): { cachePoint: CachePointBlockData; }; /** * Creates a CachePointBlock instance from its wrapped data format. * * @param data - Wrapped CachePointBlockData to deserialize * @returns CachePointBlock instance */ static fromJSON(data: { cachePoint: CachePointBlockData; }): CachePointBlock; } /** * Data for a JSON block. */ export interface JsonBlockData { /** * Structured JSON data. */ json: JSONValue; } /** * JSON content block within a message. * Used for structured data returned from tools or model responses. */ export declare class JsonBlock implements JsonBlockData, JSONSerializable { /** * Discriminator for JSON content. */ readonly type: "jsonBlock"; /** * Structured JSON data. */ readonly json: JSONValue; constructor(data: JsonBlockData); /** * Serializes the JsonBlock to a JSON-compatible JsonBlockData object. * Called automatically by JSON.stringify(). */ toJSON(): JsonBlockData; /** * Creates a JsonBlock instance from JsonBlockData. * * @param data - JsonBlockData to deserialize * @returns JsonBlock instance */ static fromJSON(data: JsonBlockData): JsonBlock; } /** * Reason why the model stopped generating content. * * - `cancelled` - Agent invocation was cancelled via `agent.cancel()` * - `contentFiltered` - Content was filtered by safety mechanisms * - `endTurn` - Natural end of the model's turn * - `guardrailIntervened` - A guardrail policy stopped generation * - `interrupt` - Agent execution was interrupted for human input * - `maxTokens` - The model provider's per-call token cap was reached * - `limitOutputTokens` - Agent loop stopped because `InvokeOptions.limits.outputTokens` was reached * - `limitTotalTokens` - Agent loop stopped because `InvokeOptions.limits.totalTokens` was reached * - `limitTurns` - Agent loop stopped because `InvokeOptions.limits.turns` was reached * - `pauseTurn` - Model paused a long-running turn; the response should be sent back to continue * - `refusal` - A streaming classifier intervened to handle a potential policy violation * - `stopSequence` - A stop sequence was encountered * - `toolUse` - Model wants to use a tool * - `modelContextWindowExceeded` - Input exceeded the model's context window */ export type StopReason = 'cancelled' | 'contentFiltered' | 'endTurn' | 'guardrailIntervened' | 'interrupt' | 'maxTokens' | 'limitOutputTokens' | 'limitTotalTokens' | 'limitTurns' | 'pauseTurn' | 'refusal' | 'stopSequence' | 'toolUse' | 'modelContextWindowExceeded' | (string & {}); /** * System prompt for guiding model behavior. * Can be a simple string or an array of content blocks for advanced caching. * * @example * ```typescript * // Simple string * const prompt: SystemPrompt = 'You are a helpful assistant' * * // Array with cache points for advanced caching * const prompt: SystemPrompt = [ * { textBlock: new TextBlock('You are a helpful assistant') }, * { textBlock: new TextBlock(largeContextDocument) }, * { cachePointBlock: new CachePointBlock({ cacheType: 'default' }) } * ] * ``` */ export type SystemPrompt = string | SystemContentBlock[]; /** * Data representation of a system prompt. * Can be a simple string or an array of system content block data for advanced caching. * * This is the data interface counterpart to SystemPrompt, following the "Data" pattern. */ export type SystemPromptData = string | SystemContentBlockData[]; /** * Converts SystemPromptData to SystemPrompt by converting data blocks to class instances. * If already in SystemPrompt format (class instances), returns as-is. * * @param data - System prompt data to convert * @returns SystemPrompt with class-based content blocks */ export declare function systemPromptFromData(data: SystemPromptData | SystemPrompt): SystemPrompt; /** * Converts a SystemPrompt to its data representation for serialization. * * @param prompt - System prompt to convert (string or content block array) * @returns SystemPromptData suitable for JSON serialization */ export declare function systemPromptToData(prompt: SystemPrompt): SystemPromptData; /** * Creates a deep copy of a SystemPrompt by round-tripping through serialization. */ export declare function cloneSystemPrompt(prompt: SystemPrompt): SystemPrompt; /** * A block of content within a system prompt. * Supports text content, cache points, and guard content for prompt caching and guardrail evaluation. * * This is a discriminated union where the object key determines the block format. */ export type SystemContentBlockData = TextBlockData | { cachePoint: CachePointBlockData; } | { guardContent: GuardContentBlockData; }; export type SystemContentBlock = TextBlock | CachePointBlock | GuardContentBlock; /** * Qualifier for guard content. * Specifies how the content should be evaluated by guardrails. * * - `grounding_source` - Content to check for grounding/factuality * - `query` - User query to evaluate * - `guard_content` - General content for guardrail evaluation */ export type GuardQualifier = 'grounding_source' | 'query' | 'guard_content'; /** * Image format for guard content. * Only formats supported by Bedrock guardrails. */ export type GuardImageFormat = 'png' | 'jpeg'; /** * Source for guard content image. * Only supports raw bytes. */ export type GuardImageSource = { bytes: Uint8Array; }; /** * Text content to be evaluated by guardrails. */ export interface GuardContentText { /** * Qualifiers that specify how this content should be evaluated. */ qualifiers: GuardQualifier[]; /** * The text content to be evaluated. */ text: string; } /** * Image content to be evaluated by guardrails. */ export interface GuardContentImage { /** * Image format. */ format: GuardImageFormat; /** * Image source (bytes only). */ source: GuardImageSource; } /** * Data for a guard content block. * Can contain either text or image content for guardrail evaluation. */ export interface GuardContentBlockData { /** * Text content with evaluation qualifiers. */ text?: GuardContentText; /** * Image content with evaluation qualifiers. */ image?: GuardContentImage; } /** * Guard content block for guardrail evaluation. * Marks content that should be evaluated by guardrails for safety, grounding, or other policies. * Can be used in both message content and system prompts. */ export declare class GuardContentBlock implements GuardContentBlockData, JSONSerializable<{ guardContent: Serialized; }> { /** * Discriminator for guard content. */ readonly type: "guardContentBlock"; /** * Text content with evaluation qualifiers. */ readonly text?: GuardContentText; /** * Image content with evaluation qualifiers. */ readonly image?: GuardContentImage; constructor(data: GuardContentBlockData); /** * Serializes the GuardContentBlock to a JSON-compatible ContentBlockData object. * Called automatically by JSON.stringify(). * Uint8Array image bytes are encoded as base64 string. */ toJSON(): { guardContent: Serialized; }; /** * Creates a GuardContentBlock instance from its wrapped data format. * Base64-encoded image bytes are decoded back to Uint8Array. * * @param data - Wrapped GuardContentBlockData to deserialize (accepts both string and Uint8Array for image bytes) * @returns GuardContentBlock instance */ static fromJSON(data: { guardContent: MaybeSerializedInput; }): GuardContentBlock; } /** * Converts ContentBlockData to a ContentBlock instance. * Handles all content block types including text, tool use/result, reasoning, cache points, guard content, and media blocks. * * @param data - The content block data to convert * @returns A ContentBlock instance of the appropriate type * @throws Error if the content block type is unknown */ export declare function contentBlockFromData(data: ContentBlockData): ContentBlock; //# sourceMappingURL=messages.d.ts.map