use std::{ops::Add, sync::Arc};
use crate::{Message, RunId, StepId, ToolResultContent};
#[derive(Debug)]
#[non_exhaustive]
pub enum StreamChunk {
TextDelta {
delta: String,
},
ToolCallStarted {
id: String,
name: String,
},
ToolCallArgsDelta {
id: String,
delta: String,
},
ToolCallFinished {
id: String,
name: String,
args: serde_json::Value,
},
ReasoningDelta {
delta: String,
},
ReasoningFinished {
signature: Option<String>,
},
RedactedReasoningBlock {
data: String,
},
TurnFinished {
reason: FinishReason,
usage: Usage,
service_tier: Option<String>,
},
RunStarted {
run_id: RunId,
},
StepStarted {
run_id: RunId,
step_id: StepId,
iteration: usize,
},
StepFinished {
run_id: RunId,
step_id: StepId,
iteration: usize,
new_messages_so_far: Arc<Vec<Message>>,
},
ToolResult {
run_id: RunId,
step_id: StepId,
call_id: String,
content: ToolResultContent,
},
RunFinished {
run_id: RunId,
reason: FinishReason,
usage: Usage,
new_messages: Vec<Message>,
},
HistoryCompacted {
run_id: RunId,
before_count: usize,
after_count: usize,
strategy: &'static str,
},
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum FinishReason {
EndTurn,
ToolUse,
MaxTokens,
StopSequence,
Aborted(String),
Other(String),
}
#[derive(Debug, Default, Clone, Copy)]
#[non_exhaustive]
pub struct Usage {
pub input_tokens: u32,
pub output_tokens: u32,
pub cached_input_tokens: u32,
pub cache_creation_input_tokens: u32,
pub cache_creation_5m_tokens: u32,
pub cache_creation_1h_tokens: u32,
}
impl Add for Usage {
type Output = Usage;
fn add(self, other: Usage) -> Usage {
Usage {
input_tokens: self.input_tokens + other.input_tokens,
output_tokens: self.output_tokens + other.output_tokens,
cached_input_tokens: self.cached_input_tokens + other.cached_input_tokens,
cache_creation_input_tokens: self.cache_creation_input_tokens
+ other.cache_creation_input_tokens,
cache_creation_5m_tokens: self.cache_creation_5m_tokens
+ other.cache_creation_5m_tokens,
cache_creation_1h_tokens: self.cache_creation_1h_tokens
+ other.cache_creation_1h_tokens,
}
}
}
impl std::ops::AddAssign for Usage {
fn add_assign(&mut self, other: Usage) {
self.input_tokens += other.input_tokens;
self.output_tokens += other.output_tokens;
self.cached_input_tokens += other.cached_input_tokens;
self.cache_creation_input_tokens += other.cache_creation_input_tokens;
self.cache_creation_5m_tokens += other.cache_creation_5m_tokens;
self.cache_creation_1h_tokens += other.cache_creation_1h_tokens;
}
}