pig/agent/state
Agent state types and configuration.
AgentConfig holds the immutable configuration for creating an agent.
AgentState holds the runtime state (history, iterations).
State is immutable — every mutation returns a new state.
Note: hooks and dispatcher are runtime concerns, stored on
PigConfig / RuntimeConfig, not here. The pure core (update.gleam)
has no knowledge of hooks or the dispatcher.
Types
Configuration for creating an agent. Immutable once constructed.
Hooks and dispatcher are NOT here — they’re runtime concerns
owned by PigConfig and RuntimeConfig respectively.
pub type AgentConfig {
AgentConfig(
provider: fn(
List(message.Message),
List(tool_definition.ToolDefinition),
) -> Result(provider.InferenceResult, error.AiError),
tools: tool.ToolRegistry,
system_prompt: option.Option(String),
max_iterations: Int,
model: String,
agent_id: option.Option(String),
agent_name: option.Option(String),
agent_description: option.Option(String),
agent_version: option.Option(String),
provider_name: option.Option(String),
session_path: option.Option(String),
)
}
Constructors
-
AgentConfig( provider: fn( List(message.Message), List(tool_definition.ToolDefinition), ) -> Result(provider.InferenceResult, error.AiError), tools: tool.ToolRegistry, system_prompt: option.Option(String), max_iterations: Int, model: String, agent_id: option.Option(String), agent_name: option.Option(String), agent_description: option.Option(String), agent_version: option.Option(String), provider_name: option.Option(String), session_path: option.Option(String), )
Runtime state of an agent. Immutable — mutations return new state.
pub type AgentState {
AgentState(
config: AgentConfig,
history: List(message.Message),
iterations: Int,
)
}
Constructors
-
AgentState( config: AgentConfig, history: List(message.Message), iterations: Int, )
Values
pub fn add_message(
state: AgentState,
msg: message.Message,
) -> AgentState
Add a message to the end of history. Returns new state.
pub fn config(
provider: fn(
List(message.Message),
List(tool_definition.ToolDefinition),
) -> Result(provider.InferenceResult, error.AiError),
) -> AgentConfig
Create an AgentConfig with defaults.
pub fn config_put_provider(
st: AgentState,
provider: fn(
List(message.Message),
List(tool_definition.ToolDefinition),
) -> Result(provider.InferenceResult, error.AiError),
) -> AgentState
Replace the provider on an existing state.
pub fn exceeded_max_iterations(state: AgentState) -> Bool
Check if the agent has exceeded its maximum iterations.
pub fn history(state: AgentState) -> List(message.Message)
Get the conversation history in order.
pub fn increment_iterations(state: AgentState) -> AgentState
Increment the iteration counter. Returns new state.
pub fn max_iterations_error(state: AgentState) -> error.AiError
Build an AiError for exceeding max iterations.
pub fn messages_for_provider(
state: AgentState,
) -> List(message.Message)
Get messages for a provider call.
Prepends the system prompt as a System message if one is configured.
pub fn tool_definitions(
state: AgentState,
) -> List(tool_definition.ToolDefinition)
Get tool definitions from the registry.
pub fn with_agent_description(
config: AgentConfig,
desc: String,
) -> AgentConfig
Set the agent description.
pub fn with_agent_id(
config: AgentConfig,
id: String,
) -> AgentConfig
Set the agent ID.
pub fn with_agent_name(
config: AgentConfig,
name: String,
) -> AgentConfig
Set the agent name.
pub fn with_agent_version(
config: AgentConfig,
version: String,
) -> AgentConfig
Set the agent version.
pub fn with_max_iterations(
config: AgentConfig,
max: Int,
) -> AgentConfig
Set the maximum number of loop iterations before forcing termination.
pub fn with_model(
config: AgentConfig,
model: String,
) -> AgentConfig
Set the model name for telemetry and logging.
pub fn with_provider_name(
config: AgentConfig,
name: String,
) -> AgentConfig
Set the provider name.
pub fn with_session_path(
config: AgentConfig,
path: String,
) -> AgentConfig
Set the session path for persistence and replay.
pub fn with_system_prompt(
config: AgentConfig,
prompt: String,
) -> AgentConfig
Set the system prompt on the config.
pub fn with_tools(
config: AgentConfig,
tools: tool.ToolRegistry,
) -> AgentConfig
Set the tool registry on the config.