ExAgent.Message (ExAgent v3.0.0)

View Source

Message and part types exchanged between the agent and a model.

Stability — Stable.

It leans into Elixir's native discriminated unions: every part/message is a struct, and the __struct__ field is the discriminator you pattern-match on (case part do %Part.ToolCall{} -> ...).

Two top-level messages:

  • Request — sent to the model (system/user prompts, tool returns, retry prompts).
  • Response — returned by the model (text, tool calls, thinking).

Message structs implement Jason.Encoder so a whole history can be serialised to JSON for persistence or for talking to providers.

Summary

Functions

Parse a JSON binary back into a message history. Each entry must be a request or response with a list of parts for that role. Tool arguments and result content remain application data. A tool return must include content, even when its value is JSON null or it carries an omission marker. A tool return with an omission marker must include its explicit status. Older histories without a marker can omit status. That legacy form loads as :succeeded, and to_json/1 writes the status explicitly. Responses without a citations field load with an empty citation list. A missing, null or legacy "nil" finish reason loads as nil. Malformed bytes return {:error, :invalid_json}. A decoded value outside the history format or an object with duplicate keys returns {:error, :invalid_history}.

Build a request with optional instructions, run ID, conversation ID and timestamp.

Build a response with optional usage, model name, finish reason, continuation, timestamp and citations. Model adapters must supply an explicit finish reason for a successful new response.

Flatten a message history into its constituent parts (mostly for tests).

Serialize a message history to a JSON binary for persistence.

Types

Functions

from_json(binary)

@spec from_json(term()) :: {:ok, [t()]} | {:error, :invalid_json | :invalid_history}

Parse a JSON binary back into a message history. Each entry must be a request or response with a list of parts for that role. Tool arguments and result content remain application data. A tool return must include content, even when its value is JSON null or it carries an omission marker. A tool return with an omission marker must include its explicit status. Older histories without a marker can omit status. That legacy form loads as :succeeded, and to_json/1 writes the status explicitly. Responses without a citations field load with an empty citation list. A missing, null or legacy "nil" finish reason loads as nil. Malformed bytes return {:error, :invalid_json}. A decoded value outside the history format or an object with duplicate keys returns {:error, :invalid_history}.

new_request(parts, opts \\ [])

Build a request with optional instructions, run ID, conversation ID and timestamp.

new_response(parts, opts \\ [])

Build a response with optional usage, model name, finish reason, continuation, timestamp and citations. Model adapters must supply an explicit finish reason for a successful new response.

parts(messages)

@spec parts([t()]) :: [struct()]

Flatten a message history into its constituent parts (mostly for tests).

to_json(messages)

@spec to_json([t()]) :: String.t()

Serialize a message history to a JSON binary for persistence.