Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Derive Macro

The #[derive(State)] macro generates channel mappings from struct fields.

Basic Usage

#![allow(unused)]
fn main() {
use eko_core::State;

#[derive(State, Debug, Clone, Serialize, Deserialize)]
struct MyState {
    // No attribute → LastValue (last-write-wins)
    counter: i64,

    // Custom reducer
    #[state(reducer = "append_messages")]
    messages: Vec<String>,

    // Not persisted
    #[state(ephemeral)]
    temp_data: Option<String>,
}

fn append_messages(old: Value, new: Value) -> Result<Value, AgentError> {
    // merge logic
}
}

Field Attributes

AttributeBehavior
(none)LastValue – last write wins
#[state(reducer = "fn_name")]Custom reducer function
#[state(ephemeral)]Not persisted in checkpoints

Reducer Signature

#![allow(unused)]
fn main() {
fn append_messages(old: Value, new: Value) -> Result<Value, AgentError>
}

The reducer receives the current channel value and the incoming write, and returns the merged value.