Skip to content

State and channels

A Warpra graph keeps its state in named channels, one per state key. Nodes read the state and return partial updates. Each channel decides how an update is merged into its current value.

You declare each key on the StateGraph builder:

use warpra::*;
use warpra::graph::reducers;
use serde_json::json;
let g = StateGraph::new()
.last_value("topic") // keeps the latest write
.reducer_with_initial("log", reducers::append, json!([])) // merges writes with a reducer
.messages("messages"); // add_messages semantics
Builder method Channel Behaviour
last_value(key) LastValue Holds the latest value written to the key
reducer(key, f) BinaryOperatorAggregate Merges each write into the current value with f
reducer_with_initial(key, f, initial) BinaryOperatorAggregate Same, starting from initial
messages(key) message channel Merges with add_messages
channel(key, channel) any Channel Use a channel kind directly

State is a serde_json::Value built with preserve_order JSON, so reducer order is deterministic.

warpra::graph::reducers has append (list concatenation, like operator.add on lists), extend (alias of append), add_numbers, merge_objects, max_value, min_value and replace. A reducer is any Fn(Value, Value) -> Result<Value, GraphError>.

The channels module has LastValue, LastValueAfterFinish, Topic, BinaryOperatorAggregate, EphemeralValue, NamedBarrierValue (and its after-finish variant), UntrackedValue and AnyValue. The compiler creates the internal ones it needs for edges and joins. You rarely build these yourself.

.messages("messages") declares a key that merges messages with add_messages:

  1. Both sides become lists of message objects.
  2. Messages without an id get a uuid4.
  3. A remove-all marker in the update discards everything before it.
  4. A message whose id already exists is replaced in place, or removed for a remove message. Others are appended. Removing an unknown id is an error.

The function is exported as warpra::add_messages, with remove_message and REMOVE_ALL_MESSAGES for removals.

Messages are JSON objects with role, content, and optionally tool_calls, tool_call_id, name and id:

json!({"messages": [{"role": "user", "content": "What is 2 + 40?"}]})

input_keys([..]) restricts which keys START copies into the state. output_keys([..]) restricts what invoke returns.

If you prefer a Rust struct, use the typed layer. StateGraph::<S>::typed() derives one last-value channel per field of S::default(). add_typed_node deserialises the state into S and serialises the returned partial update. add_typed_conditional_edges gives the router a &S. Override single keys with reducer, messages or channel. S must implement Serialize + DeserializeOwned (and Default for typed()).

set_input_schema, set_output_schema, set_state_schema, set_config_schema_json and set_context_schema_json attach plain JSON Schema values to the graph. Pregel::schemas() reads them back. The server returns them from /assistants/{id}/schemas.

Warpra is open source under Apache-2.0. Made by Liceron.