Skip to main content

WorkflowState

Trait WorkflowState 

Source
pub trait WorkflowState:
    Clone
    + Send
    + Sync
    + Serialize
    + DeserializeOwned {
    type Mutation: StateMutation<Self>;

    // Provided methods
    fn apply_batch(
        &mut self,
        mutations: impl IntoIterator<Item = Self::Mutation>,
    ) { ... }
    fn initial() -> Self
       where Self: Default { ... }
}
Expand description

工作流状态 — 编译期类型安全的状态容器。

替代 HashMap<String, Value> 动态模型。 每个工作流定义自己的 State struct 和 Mutation enum, 实现此 trait 以声明关联类型。

State 只是数据。 状态变更逻辑在 StateMutation trait 中。 Merge 职责已从 WorkflowState 剥离到 MergeStrategy

§示例

// State 只是数据
pub struct AgentState {
    pub messages: Vec<Message>,
    pub iterations: usize,
    pub output_tokens: usize,
}

// Mutation 自己知道怎么改 State
pub enum AgentMutation {
    AppendMessage(Message),
    IncrementIteration,
    RecordOutputTokens(usize),
}

impl StateMutation<AgentState> for AgentMutation {
    fn apply(self, state: &mut AgentState) {
        match self {
            AgentMutation::AppendMessage(msg) => state.messages.push(msg),
            AgentMutation::IncrementIteration => state.iterations += 1,
            AgentMutation::RecordOutputTokens(n) => state.output_tokens += n,
        }
    }
}

// WorkflowState 只声明关联类型 — 没有 apply()
impl WorkflowState for AgentState {
    type Mutation = AgentMutation;
}

Required Associated Types§

Source

type Mutation: StateMutation<Self>

与此状态关联的 Mutation 类型。

Provided Methods§

Source

fn apply_batch(&mut self, mutations: impl IntoIterator<Item = Self::Mutation>)

批量应用 Mutation — 唯一公开入口。

默认实现:逐个调用 StateMutation::apply。 未来可覆盖为 Transaction 语义(begin → validate → apply → commit/rollback)。

Source

fn initial() -> Self
where Self: Default,

创建默认/初始状态。

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§