Mutable state for an agent run.
Tracks the conversation history, turn count, token usage, and current status. Passed through each iteration of the agent loop.
Summary
Functions
Append messages to the conversation history.
Append tool execution metadata for the current run.
Clean up resources owned by this state. No-op currently; reserved for future resource management.
Increment the turn counter.
Initialize state from config and optional existing messages.
Extract the text from the last assistant message.
Flush the accumulator into the messages field.
Merge usage from a provider response.
Return messages in chronological order.
Types
@type status() :: :idle | :running | :completed | :error | :max_turns | :halted
@type t() :: %Alloy.Agent.State{ agent_id: String.t(), config: Alloy.Agent.Config.t(), current_task: {reference(), pid(), binary()} | nil, error: term() | nil, messages: [Alloy.Message.t()], messages_new: [Alloy.Message.t()], pending_requests: :queue.queue({String.t(), binary()}), started_at: integer() | nil, status: status(), tool_calls: [map()], tool_defs: [map()], tool_fns: %{required(String.t()) => module()}, turn: non_neg_integer(), usage: Alloy.Usage.t() }
Functions
@spec append_messages(t(), [Alloy.Message.t()] | Alloy.Message.t()) :: t()
Append messages to the conversation history.
Uses an internal accumulator for O(1) append. Call messages/1 to
retrieve the full list in chronological order.
Append tool execution metadata for the current run.
@spec cleanup(t()) :: :ok
Clean up resources owned by this state. No-op currently; reserved for future resource management.
Increment the turn counter.
@spec init(Alloy.Agent.Config.t(), [Alloy.Message.t()]) :: t()
Initialize state from config and optional existing messages.
Extract the text from the last assistant message.
Flush the accumulator into the messages field.
After this call, state.messages contains the full chronological
list and state.messages_new is empty. Call at process boundaries
where code reads state.messages directly.
Merge usage from a provider response.
@spec messages(t()) :: [Alloy.Message.t()]
Return messages in chronological order.
Flushes the internal accumulator and returns the full message list.