Skip to main content

Module state

Module state 

Source
Expand description

AG-UI State Management

This module provides state management traits and utilities for AG-UI:

  • AgentState: Marker trait for types that can represent agent state
  • FwdProps: Marker trait for types that can be forwarded as props to UI
  • StateManager: Helper for managing state and generating deltas

These traits enable generic state handling in events while ensuring the necessary bounds for serialization and async operations.

§State Synchronization

AG-UI supports two modes of state synchronization:

  • Snapshots: Send the complete state (simpler but less efficient)
  • Deltas: Send JSON Patch operations (more efficient for large states)

The StateManager helper makes it easy to track state changes and generate appropriate events.

§Example

use ag_ui_core::state::StateManager;
use serde_json::json;

let mut manager = StateManager::new(json!({"count": 0}));

// Update state and get the delta
let delta = manager.update(json!({"count": 1}));
assert!(delta.is_some());

// Get current state
assert_eq!(manager.current()["count"], 1);

Structs§

StateManager
A helper for managing state and generating deltas.
TypedStateManager
A typed state manager for custom state types.

Traits§

AgentState
Marker trait for types that can represent agent state.
FwdProps
Marker trait for types that can be forwarded as props to UI components.

Functions§

diff_states
Computes the difference between two JSON states as a JSON Patch.