use crate::models::Usage;
use std::time::{Duration, Instant};
#[derive(Debug)]
pub struct TurnContext {
pub id: String,
#[allow(dead_code)]
pub started_at: Instant,
pub step: u32,
pub max_steps: u32,
pub tool_calls: Vec<TurnToolCall>,
#[allow(dead_code)]
pub cancelled: bool,
pub usage: Usage,
}
#[derive(Debug, Clone)]
pub struct TurnToolCall {
pub id: String,
pub name: String,
pub input: serde_json::Value,
pub result: Option<String>,
pub error: Option<String>,
pub duration: Option<Duration>,
}
impl TurnContext {
pub fn new(max_steps: u32) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
started_at: Instant::now(),
step: 0,
max_steps,
tool_calls: Vec::new(),
cancelled: false,
usage: Usage {
input_tokens: 0,
output_tokens: 0,
server_tool_use: None,
},
}
}
pub fn next_step(&mut self) -> bool {
self.step += 1;
self.step <= self.max_steps
}
pub fn at_max_steps(&self) -> bool {
self.step >= self.max_steps
}
pub fn record_tool_call(&mut self, call: TurnToolCall) {
self.tool_calls.push(call);
}
#[allow(dead_code)]
pub fn cancel(&mut self) {
self.cancelled = true;
}
#[allow(dead_code)]
pub fn elapsed(&self) -> Duration {
self.started_at.elapsed()
}
pub fn add_usage(&mut self, usage: &Usage) {
self.usage.input_tokens += usage.input_tokens;
self.usage.output_tokens += usage.output_tokens;
}
}
impl TurnToolCall {
pub fn new(id: String, name: String, input: serde_json::Value) -> Self {
Self {
id,
name,
input,
result: None,
error: None,
duration: None,
}
}
pub fn set_result(&mut self, result: String, duration: Duration) {
self.result = Some(result);
self.duration = Some(duration);
}
pub fn set_error(&mut self, error: String, duration: Duration) {
self.error = Some(error);
self.duration = Some(duration);
}
}