Skip to contents

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.

Value

An AgentState R6 object.

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

data

Environment. Stores state variables.

reducers

List. Functions applied to merge updates.

schema

List. Expected types for state variables.

Methods


Method new()

Initialize AgentState

Usage

AgentState$new(initial_data = list(), reducers = list(), schema = list())

Arguments

initial_data

List 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".

reducers

Named list of functions or character names. Maps state keys to functions that define how new values are merged with current values (e.g., append or sum).

schema

Named list of character strings. Defines the expected class/type for specific keys (e.g., list(count = "numeric")). Get a state variable


Method get()

Usage

AgentState$get(key, default = NULL)

Arguments

key

String. The name of the variable to retrieve.

default

Value. The value to return if the key is not found in the state.

Returns

The value associated with the key, or the default. Get all state variables as a list


Method get_all()

Usage

AgentState$get_all()

Returns

A named list. Validate a single state variable against the schema


Method validate()

Usage

AgentState$validate(key, value)

Arguments

key

String.

value

Any value.

Returns

TRUE if valid, throws error otherwise. Set a state variable directly (bypassing reducers)


Method set()

Usage

AgentState$set(key, value)

Arguments

key

String.

value

Any value. Update state using reducers


Method update()

Usage

AgentState$update(updates)

Arguments

updates

List of state updates. Update state from a node's output


Method update_from_node()

Usage

AgentState$update_from_node(output, node_id)

Arguments

output

The output from the node.

node_id

The ID of the node. Export state for persistence (logic as names)


Method to_list_serializable()

Usage

AgentState$to_list_serializable()

Returns

List.


Method clone()

The objects of this class are cloneable with this method.

Usage

AgentState$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

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")
} # }