A strongly typed, centrally managed state object for passing data between nodes in an AgentDAG. It supports declarative schemas for validation and functional "reducers" for sophisticated state merging during execution.
Details
The AgentState is the single source of truth for a DAG. It is designed
to be serializable, allowing the entire workflow state to be checkpointed
and restored.
Public fields
dataEnvironment. Stores state variables.
reducersList. Functions applied to merge updates.
schemaList. Expected types for state variables.
Methods
Method new()
Initialize AgentState
Usage
AgentState$new(initial_data = list(), reducers = list(), schema = list())Arguments
initial_dataList or String. The starting data for the state. If a list, it is merged into the environment. If a string, it is stored under the key
"input".reducersNamed list of functions or character names. Maps state keys to functions that define how new values are merged with current values (e.g.,
appendorsum).schemaNamed list of character strings. Defines the expected class/type for specific keys (e.g.,
list(count = "numeric")). Get a state variable
Method get()
Method update()
Examples
if (FALSE) { # \dontrun{
# 1. Initialize state with strict schema validation and reducers
state <- AgentState$new(
initial_data = list(count = 0, logs = list()),
schema = list(count = "numeric", logs = "list"),
reducers = list(logs = reducer_append)
)
# 2. Basic update (replaces current value)
state$set("count", 42)
# 3. Reducer update (appends to current list)
state$update(list(logs = "Process started"))
state$update(list(logs = "Processing step 1"))
# 4. Schema validation check
# This will throw an error because 'count' must be numeric
try(state$set("count", "not a number"))
# Verify accumulation
print(state$get("logs")) # list("Process started", "Processing step 1")
} # }