Eai.Message (eai v0.1.11)

Copy Markdown

Internal message representation (IR) based on AWS Bedrock Converse API format.

Uses Elixir tuples for content blocks, providing a unified format that all provider adapters can convert to/from. This solves the cross-provider compatibility problem and enables multimodal content injection.

Summary

Functions

Check if the message is effectively empty (no content blocks or only empty text).

Convert a Converse-format JSON map back to internal message.

Create a :user message from multimodal_inject blocks returned by read_media_file. Input is a list of JSON-decoded maps (the "blocks" array).

Check if message contains any :tool_use blocks.

Create a new message.

Create a :user message with tool_result block(s), ensuring Anthropic compatibility (tool results must be in their own :user message).

Extract text content from a message as a single string. Joins multiple :text blocks with double newline.

Convert internal message to a JSON-friendly map (atom keys → string keys). Useful for Bedrock Converse direct pass-through.

Extract :tool_use blocks from a message.

Types

content_block()

@type content_block() ::
  {:text, String.t()}
  | {:thinking, String.t()}
  | {:image, format: String.t(), source: {:bytes, String.t()}}
  | {:tool_use, tool_use_id: String.t(), name: String.t(), input: map()}
  | {:tool_result, tool_use_id: String.t(), content: [content_block()]}

role()

@type role() :: :user | :assistant

t()

@type t() :: %{role: role(), content: [content_block()]}

Functions

empty?(arg1)

@spec empty?(t()) :: boolean()

Check if the message is effectively empty (no content blocks or only empty text).

from_converse_map(map)

@spec from_converse_map(map()) :: t()

Convert a Converse-format JSON map back to internal message.

from_inject_blocks(blocks)

@spec from_inject_blocks([map()]) :: t()

Create a :user message from multimodal_inject blocks returned by read_media_file. Input is a list of JSON-decoded maps (the "blocks" array).

has_tool_uses?(map)

@spec has_tool_uses?(t()) :: boolean()

Check if message contains any :tool_use blocks.

new(role, content)

@spec new(role(), String.t() | [content_block()]) :: t()

Create a new message.

When content is a binary, it's wrapped as [{:text, content}]. When content is a list, it's used directly (must be valid content blocks).

new_tool_result(tool_use_id, result_content)

@spec new_tool_result(String.t(), [content_block()]) :: t()

Create a :user message with tool_result block(s), ensuring Anthropic compatibility (tool results must be in their own :user message).

text(map)

@spec text(t()) :: String.t()

Extract text content from a message as a single string. Joins multiple :text blocks with double newline.

to_converse_map(map)

@spec to_converse_map(t()) :: map()

Convert internal message to a JSON-friendly map (atom keys → string keys). Useful for Bedrock Converse direct pass-through.

tool_uses(map)

@spec tool_uses(t()) :: [keyword()]

Extract :tool_use blocks from a message.