use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "UPPERCASE")]
pub enum NextAction {
Plan,
Act,
Finish,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThoughtActionObservation {
pub thought: String,
pub action: String,
pub observation: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema, Default)]
pub struct Plan {
#[serde(default)]
pub steps: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CurrentContext {
pub user_query: String,
#[serde(default)]
pub plan: Plan,
#[serde(default)]
pub history: Vec<ThoughtActionObservation>,
#[serde(default)]
pub iteration: usize,
}
impl CurrentContext {
pub fn new(user_query: impl Into<String>) -> Self {
Self {
user_query: user_query.into(),
plan: Plan::default(),
history: Vec::new(),
iteration: 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_next_action_serialization() {
assert_eq!(serde_json::to_string(&NextAction::Plan).unwrap(), "\"PLAN\"");
assert_eq!(serde_json::to_string(&NextAction::Act).unwrap(), "\"ACT\"");
assert_eq!(serde_json::to_string(&NextAction::Finish).unwrap(), "\"FINISH\"");
}
#[test]
fn test_thought_action_observation() {
let tao = ThoughtActionObservation {
thought: "I need to get the date".to_string(),
action: "Called resolve_date".to_string(),
observation: "2025-11-29".to_string(),
};
assert_eq!(tao.thought, "I need to get the date");
assert_eq!(tao.action, "Called resolve_date");
assert_eq!(tao.observation, "2025-11-29");
}
#[test]
fn test_plan_default() {
let plan = Plan::default();
assert!(plan.steps.is_empty());
}
#[test]
fn test_plan_serialization() {
let plan = Plan {
steps: vec!["Step 1".to_string(), "Step 2".to_string()],
};
let json = serde_json::to_string(&plan).unwrap();
assert!(json.contains("Step 1"));
assert!(json.contains("Step 2"));
let deserialized: Plan = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.steps.len(), 2);
}
#[test]
fn test_current_context_new() {
let ctx = CurrentContext::new("What is the date?");
assert_eq!(ctx.user_query, "What is the date?");
assert_eq!(ctx.iteration, 0);
assert!(ctx.history.is_empty());
assert!(ctx.plan.steps.is_empty());
}
#[test]
fn test_current_context_with_history() {
let mut ctx = CurrentContext::new("Test query");
ctx.history.push(ThoughtActionObservation {
thought: "Thinking".to_string(),
action: "Acting".to_string(),
observation: "Observing".to_string(),
});
ctx.iteration = 1;
assert_eq!(ctx.history.len(), 1);
assert_eq!(ctx.iteration, 1);
}
}