use std::collections::{BTreeMap, HashMap};
use serde::{Deserialize, Serialize};
use crate::algorithms::util::tool_signals::ToolSignals;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StateValue {
String(String),
Count(u32),
Bool(bool),
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct State {
#[serde(skip)]
pub tool_signals: Option<ToolSignals>,
#[serde(skip)]
pub target_prompts: HashMap<switchyard_protocol::ModelId, String>,
#[serde(flatten)]
pub extra: BTreeMap<String, StateValue>,
}
impl PartialEq for State {
fn eq(&self, other: &Self) -> bool {
self.extra == other.extra
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn state_round_trips_through_json_and_compares_extra_only() -> serde_json::Result<()> {
let mut state = State::default();
state
.extra
.insert("capable_hold_turns".to_string(), StateValue::Count(2));
state.extra.insert(
"fall_open".to_string(),
StateValue::String("capable".to_string()),
);
state
.extra
.insert("vgr_handoff_sent".to_string(), StateValue::Bool(true));
let json = serde_json::json!({
"capable_hold_turns": {"count": 2},
"fall_open": {"string": "capable"},
"vgr_handoff_sent": {"bool": true},
});
let with_signals = State {
tool_signals: Some(ToolSignals::default()),
..state.clone()
};
assert_eq!(serde_json::to_value(&with_signals)?, json);
assert_eq!(serde_json::from_value::<State>(json)?, state);
assert_eq!(with_signals, state);
Ok(())
}
}