use serde::{Deserialize, Serialize};
use crate::message::Message;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum StopReason {
EndTurn,
ToolUse,
MaxTokens,
StopSequence,
Other(String),
}
#[derive(Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
pub struct Usage {
pub input_tokens: u32,
pub output_tokens: u32,
pub cached_input_tokens: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cost_usd: Option<f64>,
}
impl std::fmt::Debug for Usage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut debug = f.debug_struct("Usage");
debug
.field("input_tokens", &self.input_tokens)
.field("output_tokens", &self.output_tokens)
.field("cached_input_tokens", &self.cached_input_tokens);
if let Some(cost) = self.cost_usd {
debug.field("cost_usd", &cost);
}
debug.finish()
}
}
impl Usage {
pub fn add_cost(&mut self, cost: Option<f64>) {
if let Some(cost) = cost {
self.cost_usd = Some(self.cost_usd.unwrap_or(0.0) + cost);
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ModelOutput {
pub message: Message,
pub stop_reason: StopReason,
pub usage: Usage,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Step {
pub output: ModelOutput,
}
impl Step {
pub fn ran_tools(&self) -> bool {
self.output
.message
.content
.iter()
.any(|content| matches!(content, crate::message::Content::ToolCall(_)))
}
}