Skip to main content

StateReducer

Type Alias StateReducer 

Source
pub type StateReducer = Box<dyn Fn(&Value, &Value) -> Result<Value, String> + Send + Sync>;
Expand description

State Reducer 类型别名 — 将已有值与新值合并。

类似于 LangGraph 的 operator.add,但保持显式:

// 追加消息列表
state.reduce("messages", new_msgs, |existing, new| {
    let mut msgs: Vec<Value> = serde_json::from_value(existing.clone())?;
    let additions: Vec<Value> = serde_json::from_value(new.clone())?;
    msgs.extend(additions);
    Ok(serde_json::to_value(msgs)?)
});

Aliased Type§

pub struct StateReducer(/* private fields */);