Skip to contents

Builder for directed graphs of node functions. Nodes are named R functions; edges are fixed or conditional routing rules. Call $compile() to produce an executable GraphRunner.

Use the pipe-friendly wrappers add_node(), add_edge(), and add_conditional_edge() rather than $-methods directly.

Value

A new StateGraph object.

Invisibly, self (for chaining with |>).

Invisibly, self.

Invisibly, self.

Invisibly, self.

A GraphRunner object ready to execute.

Character string.

Character string.

Invisibly, path.

Methods


Method new()

Create a new StateGraph.

Usage

StateGraph$new(state_schema)

Arguments

state_schema

A WorkflowState object, or a named list that will be passed to workflow_state().


Method add_node()

Register a node function.

Usage

StateGraph$add_node(name, fn)

Arguments

name

Character. Unique node name.

fn

Function with signature function(state, config) returning a named list of state updates.


Method add_edge()

Add a fixed edge between two nodes.

Usage

StateGraph$add_edge(from, to)

Arguments

from

Node name or START.

to

Node name or END.


Method add_conditional_edge()

Add a conditional (routing) edge.

Usage

StateGraph$add_conditional_edge(from, routing_fn, route_map)

Arguments

from

Node name. Must already be registered.

routing_fn

Function function(state) returning a character key present in route_map.

route_map

Named list mapping routing keys to node names or END.


Method set_entry()

Shortcut to set the graph entry node.

Usage

StateGraph$set_entry(name)

Arguments

name

Character. Must be a registered node.


Method compile()

Validate and compile the graph into a GraphRunner.

Usage

StateGraph$compile(
  agents = list(),
  checkpointer = NULL,
  termination = NULL,
  output_channel = NULL
)

Arguments

agents

Named list of Agent objects passed to node functions via config$agents.

checkpointer

A Checkpointer object or NULL.

termination

A termination condition (from max_turns() etc.) or NULL.

output_channel

Character or NULL. The channel whose value is returned by WorkflowState$output(). If NULL, $output() will error unless set by a workflow constructor.


Method as_dot()

Generate a DOT language string for the graph.

Usage

StateGraph$as_dot()


Method as_mermaid()

Generate a Mermaid diagram string.

Usage

StateGraph$as_mermaid()


Method visualize()

Render a visualization.

Usage

StateGraph$visualize(engine = c("dot", "visnetwork", "mermaid"))

Arguments

engine

One of "dot", "visnetwork", or "mermaid".


Method export_diagram()

Export the diagram to a file.

Usage

StateGraph$export_diagram(path, width = 800L, height = 600L)

Arguments

path

File path. Extension determines format (.svg or .png).

width

Integer. Width in pixels (PNG only).

height

Integer. Height in pixels (PNG only).


Method print()

Print graph summary.

Usage

StateGraph$print(...)

Arguments

...

Ignored.


Method clone()

The objects of this class are cloneable with this method.

Usage

StateGraph$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.