1use dashmap::DashMap;
11use serde::{Deserialize, Serialize};
12use serde_json::Value;
13use std::sync::atomic::{AtomicUsize, Ordering};
14use std::sync::{Arc, Mutex};
15
16#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
17pub enum TurnDecision {
18 Halt { reason: Option<String> },
19 Repeat { next_prompt: Option<String> },
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
23pub struct TurnMessage {
24 pub role: String,
25 pub content: String,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct ToolTrace {
30 pub tool: String,
31 pub input: Value,
32 pub output: Value,
33 pub timestamp: String,
34}
35
36pub struct IsolatedTurnContext {
37 pub turn_index: usize,
38 pub scratchpad: Arc<DashMap<String, Value>>,
39 pub tool_traces: Arc<Mutex<Vec<ToolTrace>>>,
40}
41
42impl IsolatedTurnContext {
43 pub fn new(turn_index: usize) -> Self {
44 Self {
45 turn_index,
46 scratchpad: Arc::new(DashMap::new()),
47 tool_traces: Arc::new(Mutex::new(Vec::new())),
48 }
49 }
50
51 pub fn record_tool_call(&self, tool: &str, input: Value, output: Value) {
52 let trace = ToolTrace {
53 tool: tool.to_string(),
54 input,
55 output,
56 timestamp: chrono::Utc::now().to_rfc3339(),
57 };
58 self.tool_traces.lock().unwrap().push(trace);
59 }
60
61 pub fn get_traces_count(&self) -> usize {
62 self.tool_traces.lock().unwrap().len()
63 }
64
65 pub fn get_raw_traces_bytes(&self) -> usize {
66 let traces = self.tool_traces.lock().unwrap();
67 serde_json::to_string(&*traces).map(|s| s.len()).unwrap_or(0)
68 }
69}
70
71#[derive(Debug, Clone)]
72pub struct TurnExecutionResult {
73 pub turn_index: usize,
74 pub synthesized_message: TurnMessage,
75 pub decision: TurnDecision,
76 pub intermediate_traces_count: usize,
77 pub pruned_raw_bytes: usize,
78}
79
80#[derive(Debug, Clone)]
81pub struct ConversationalLoopOutcome {
82 pub turns: Vec<TurnExecutionResult>,
83 pub history: Vec<TurnMessage>,
84 pub converged: bool,
85 pub total_pruned_bytes: usize,
86 pub token_efficiency_ratio: f64,
87}
88
89pub struct ConversationalLoopEngine {
90 pub history: Arc<Mutex<Vec<TurnMessage>>>,
91 pub shared_memory: Arc<DashMap<String, Value>>,
92 turn_counter: AtomicUsize,
93}
94
95impl Default for ConversationalLoopEngine {
96 fn default() -> Self {
97 Self::new()
98 }
99}
100
101impl ConversationalLoopEngine {
102 pub fn new() -> Self {
103 Self {
104 history: Arc::new(Mutex::new(Vec::new())),
105 shared_memory: Arc::new(DashMap::new()),
106 turn_counter: AtomicUsize::new(0),
107 }
108 }
109
110 pub fn get_history(&self) -> Vec<TurnMessage> {
111 self.history.lock().unwrap().clone()
112 }
113
114 pub fn execute_turn<F>(
116 &self,
117 prompt: TurnMessage,
118 handler: F,
119 ) -> Result<TurnExecutionResult, String>
120 where
121 F: FnOnce(&IsolatedTurnContext, &TurnMessage, &Arc<DashMap<String, Value>>) -> Result<(String, TurnDecision), String>,
122 {
123 let turn_idx = self.turn_counter.fetch_add(1, Ordering::SeqCst) + 1;
124 self.history.lock().unwrap().push(prompt.clone());
125
126 let turn_ctx = IsolatedTurnContext::new(turn_idx);
128
129 let (response, decision) = handler(&turn_ctx, &prompt, &self.shared_memory)?;
131
132 let intermediate_traces_count = turn_ctx.get_traces_count();
134 let pruned_raw_bytes = turn_ctx.get_raw_traces_bytes();
135
136 drop(turn_ctx);
138
139 let synthesized_message = TurnMessage {
141 role: "assistant".to_string(),
142 content: response,
143 };
144 self.history.lock().unwrap().push(synthesized_message.clone());
145
146 Ok(TurnExecutionResult {
147 turn_index: turn_idx,
148 synthesized_message,
149 decision,
150 intermediate_traces_count,
151 pruned_raw_bytes,
152 })
153 }
154
155 pub fn run_loop<F>(
157 &self,
158 initial_prompt: &str,
159 max_turns: usize,
160 mut handler: F,
161 ) -> Result<ConversationalLoopOutcome, String>
162 where
163 F: FnMut(&IsolatedTurnContext, &TurnMessage, &Arc<DashMap<String, Value>>) -> Result<(String, TurnDecision), String>,
164 {
165 let mut turns = Vec::new();
166 let mut current_prompt = TurnMessage {
167 role: "user".to_string(),
168 content: initial_prompt.to_string(),
169 };
170 let mut total_pruned_bytes = 0;
171 let mut converged = false;
172
173 for _ in 0..max_turns {
174 let res = self.execute_turn(current_prompt.clone(), &mut handler)?;
175 total_pruned_bytes += res.pruned_raw_bytes;
176 let decision = res.decision.clone();
177 turns.push(res);
178
179 match decision {
180 TurnDecision::Halt { .. } => {
181 converged = true;
182 break;
183 }
184 TurnDecision::Repeat { next_prompt } => {
185 current_prompt = TurnMessage {
186 role: "user".to_string(),
187 content: next_prompt.unwrap_or_else(|| "Continue next step".to_string()),
188 };
189 }
190 }
191 }
192
193 let history = self.get_history();
194 let history_bytes = serde_json::to_string(&history).map(|s| s.len()).unwrap_or(0);
195 let unpruned_total = history_bytes + total_pruned_bytes;
196 let token_efficiency_ratio = if unpruned_total > 0 {
197 (total_pruned_bytes as f64 / unpruned_total as f64) * 100.0
198 } else {
199 0.0
200 };
201
202 Ok(ConversationalLoopOutcome {
203 turns,
204 history,
205 converged,
206 total_pruned_bytes,
207 token_efficiency_ratio,
208 })
209 }
210}