switchyard_libsy/core/
state.rs1use std::collections::{BTreeMap, HashMap};
7
8use serde::{Deserialize, Serialize};
9
10use crate::algorithms::util::tool_signals::ToolSignals;
11
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14#[serde(rename_all = "snake_case")]
15pub enum StateValue {
16 String(String),
18 Count(u32),
20 Bool(bool),
22}
23
24#[derive(Debug, Clone, Default, Serialize, Deserialize)]
26pub struct State {
27 #[serde(skip)]
31 pub tool_signals: Option<ToolSignals>,
32 #[serde(skip)]
34 pub stage_cache: crate::StageCacheHistory,
35 #[serde(skip)]
37 pub target_prompts: HashMap<switchyard_protocol::ModelId, String>,
38 #[serde(flatten)]
40 pub extra: BTreeMap<String, StateValue>,
41}
42
43impl PartialEq for State {
44 fn eq(&self, other: &Self) -> bool {
45 self.extra == other.extra
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn state_round_trips_through_json_and_compares_extra_only() -> serde_json::Result<()> {
55 let mut state = State::default();
56 state
57 .extra
58 .insert("capable_hold_turns".to_string(), StateValue::Count(2));
59 state.extra.insert(
60 "fall_open".to_string(),
61 StateValue::String("capable".to_string()),
62 );
63 state
64 .extra
65 .insert("vgr_handoff_sent".to_string(), StateValue::Bool(true));
66 let json = serde_json::json!({
67 "capable_hold_turns": {"count": 2},
68 "fall_open": {"string": "capable"},
69 "vgr_handoff_sent": {"bool": true},
70 });
71
72 let with_signals = State {
73 tool_signals: Some(ToolSignals::default()),
74 ..state.clone()
75 };
76 assert_eq!(serde_json::to_value(&with_signals)?, json);
77 assert_eq!(serde_json::from_value::<State>(json)?, state);
78 assert_eq!(with_signals, state);
79 Ok(())
80 }
81}