Jido.Agent.Strategy.State (Jido v2.0.0-rc.0)

View Source

Helper module for managing strategy-specific state within an Agent.

Strategy state is stored under the reserved key :__strategy__ in agent.state. This keeps all state within the Agent struct for serializability and snapshot/restore.

Structure

The strategy state typically contains:

  • :module - The strategy module managing this state
  • :status - Current execution status (:idle, :running, :waiting, :success, :failure)
  • :data - Strategy-specific data (e.g., BT cursor, LLM conversation history)

Example

# In a Behavior Tree strategy
agent = Strategy.State.put(agent, %{
  module: __MODULE__,
  status: :running,
  tree: bt_definition,
  cursor: root_node
})

# Later, read the state
state = Strategy.State.get(agent)
state.cursor  # => root_node

Summary

Functions

Check if the strategy is actively running (not idle or terminal).

Clear strategy state, resetting to empty map.

Get the strategy state from an agent. Returns the default if no strategy state exists.

Returns the reserved key used for strategy state.

Put new strategy state into an agent. Replaces any existing strategy state.

Set the strategy status.

Get the current strategy status. Returns :idle if no status is set.

Check if the strategy is in a terminal state (success or failure).

Update strategy state using a function. The function receives the current strategy state (or empty map) and returns the new state.

Types

status()

@type status() :: :idle | :running | :waiting | :success | :failure

t()

@type t() :: %{
  optional(:module) => module(),
  optional(:status) => status(),
  optional(:data) => term(),
  optional(atom()) => term()
}

Functions

active?(agent)

@spec active?(Jido.Agent.t()) :: boolean()

Check if the strategy is actively running (not idle or terminal).

clear(agent)

@spec clear(Jido.Agent.t()) :: Jido.Agent.t()

Clear strategy state, resetting to empty map.

get(agent, default \\ %{})

@spec get(Jido.Agent.t(), t()) :: t()

Get the strategy state from an agent. Returns the default if no strategy state exists.

key()

@spec key() :: atom()

Returns the reserved key used for strategy state.

put(agent, new_state)

@spec put(Jido.Agent.t(), t()) :: Jido.Agent.t()

Put new strategy state into an agent. Replaces any existing strategy state.

set_status(agent, status)

@spec set_status(Jido.Agent.t(), status()) :: Jido.Agent.t()

Set the strategy status.

status(agent)

@spec status(Jido.Agent.t()) :: status()

Get the current strategy status. Returns :idle if no status is set.

terminal?(agent)

@spec terminal?(Jido.Agent.t()) :: boolean()

Check if the strategy is in a terminal state (success or failure).

update(agent, fun)

@spec update(Jido.Agent.t(), (t() -> t())) :: Jido.Agent.t()

Update strategy state using a function. The function receives the current strategy state (or empty map) and returns the new state.