mojentic/examples/react/
models.rs1use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
10#[serde(rename_all = "UPPERCASE")]
11pub enum NextAction {
12 Plan,
14 Act,
16 Finish,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct ThoughtActionObservation {
28 pub thought: String,
30 pub action: String,
32 pub observation: String,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema, Default)]
40pub struct Plan {
41 #[serde(default)]
43 pub steps: Vec<String>,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct CurrentContext {
53 pub user_query: String,
55 #[serde(default)]
57 pub plan: Plan,
58 #[serde(default)]
60 pub history: Vec<ThoughtActionObservation>,
61 #[serde(default)]
63 pub iteration: usize,
64}
65
66impl CurrentContext {
67 pub fn new(user_query: impl Into<String>) -> Self {
69 Self {
70 user_query: user_query.into(),
71 plan: Plan::default(),
72 history: Vec::new(),
73 iteration: 0,
74 }
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn test_next_action_serialization() {
84 assert_eq!(serde_json::to_string(&NextAction::Plan).unwrap(), "\"PLAN\"");
85 assert_eq!(serde_json::to_string(&NextAction::Act).unwrap(), "\"ACT\"");
86 assert_eq!(serde_json::to_string(&NextAction::Finish).unwrap(), "\"FINISH\"");
87 }
88
89 #[test]
90 fn test_thought_action_observation() {
91 let tao = ThoughtActionObservation {
92 thought: "I need to get the date".to_string(),
93 action: "Called resolve_date".to_string(),
94 observation: "2025-11-29".to_string(),
95 };
96
97 assert_eq!(tao.thought, "I need to get the date");
98 assert_eq!(tao.action, "Called resolve_date");
99 assert_eq!(tao.observation, "2025-11-29");
100 }
101
102 #[test]
103 fn test_plan_default() {
104 let plan = Plan::default();
105 assert!(plan.steps.is_empty());
106 }
107
108 #[test]
109 fn test_plan_serialization() {
110 let plan = Plan {
111 steps: vec!["Step 1".to_string(), "Step 2".to_string()],
112 };
113
114 let json = serde_json::to_string(&plan).unwrap();
115 assert!(json.contains("Step 1"));
116 assert!(json.contains("Step 2"));
117
118 let deserialized: Plan = serde_json::from_str(&json).unwrap();
119 assert_eq!(deserialized.steps.len(), 2);
120 }
121
122 #[test]
123 fn test_current_context_new() {
124 let ctx = CurrentContext::new("What is the date?");
125 assert_eq!(ctx.user_query, "What is the date?");
126 assert_eq!(ctx.iteration, 0);
127 assert!(ctx.history.is_empty());
128 assert!(ctx.plan.steps.is_empty());
129 }
130
131 #[test]
132 fn test_current_context_with_history() {
133 let mut ctx = CurrentContext::new("Test query");
134 ctx.history.push(ThoughtActionObservation {
135 thought: "Thinking".to_string(),
136 action: "Acting".to_string(),
137 observation: "Observing".to_string(),
138 });
139 ctx.iteration = 1;
140
141 assert_eq!(ctx.history.len(), 1);
142 assert_eq!(ctx.iteration, 1);
143 }
144}