Skip to main content

awaken_runtime/extensions/handoff/
state.rs

1use serde::{Deserialize, Serialize};
2
3use crate::state::StateKey;
4
5use super::action::HandoffAction;
6
7/// Persisted handoff state — tracks the active agent variant and any pending request.
8#[derive(Debug, Clone, Default, Serialize, Deserialize)]
9#[serde(default)]
10pub struct HandoffState {
11    /// The currently active agent variant (`None` = base configuration).
12    pub active_agent: Option<String>,
13    /// A handoff requested by the tool, pending activation.
14    pub requested_agent: Option<String>,
15}
16
17impl HandoffState {
18    pub(crate) fn reduce(&mut self, action: HandoffAction) {
19        match action {
20            HandoffAction::Request { agent } => {
21                self.requested_agent = Some(agent);
22            }
23            HandoffAction::Activate { agent } => {
24                self.active_agent = Some(agent);
25                self.requested_agent = None;
26            }
27            HandoffAction::Clear => {
28                self.active_agent = None;
29                self.requested_agent = None;
30            }
31        }
32    }
33}
34
35/// State key for the handoff state.
36pub struct ActiveAgentKey;
37
38impl StateKey for ActiveAgentKey {
39    const KEY: &'static str = "agent_handoff";
40    type Value = HandoffState;
41    type Update = HandoffAction;
42
43    fn apply(value: &mut Self::Value, update: Self::Update) {
44        value.reduce(update);
45    }
46}