ExAgent.Message (ExAgent v0.1.0)

Copy Markdown View Source

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

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).

All structs @derive 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.

Build a request from a list of parts.

Build a response from a list of parts.

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(String.t() | binary()) :: {:ok, [t()]} | {:error, term()}

Parse a JSON binary back into a message history.

new_request(parts, opts \\ [])

Build a request from a list of parts.

new_response(parts, opts \\ [])

Build a response from a list of parts.

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.