use serde::Deserialize;
use serde_json::Value;
#[derive(Debug, Deserialize)]
pub struct Response {
pub content: Vec<ResponseBlock>,
pub usage: Option<Usage>,
pub stop_reason: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseBlock {
Text { text: String },
ToolUse { id: String, name: String, input: Value },
Thinking { thinking: String },
}
#[derive(Debug, Deserialize)]
pub struct Usage {
pub input_tokens: u32,
pub output_tokens: u32,
#[serde(default)]
pub cache_read_input_tokens: u32,
#[serde(default)]
pub cache_creation_input_tokens: u32,
}
impl From<Usage> for crate::types::UsageStats {
fn from(u: Usage) -> Self {
Self {
prompt_tokens: u.input_tokens as usize,
completion_tokens: u.output_tokens as usize,
total_tokens: (u.input_tokens + u.output_tokens) as usize,
cache_read_tokens: u.cache_read_input_tokens as usize,
cache_creation_tokens: u.cache_creation_input_tokens as usize,
}
}
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StreamEvent {
MessageStart { message: MessageStart },
ContentBlockStart { index: u32, content_block: ContentBlockStart },
ContentBlockDelta { index: u32, delta: ContentBlockDelta },
ContentBlockStop { index: u32 },
MessageDelta { delta: MessageDelta, usage: Option<Usage> },
MessageStop,
Error { error: StreamError },
#[serde(other)]
Unknown,
}
#[derive(Debug, Deserialize)]
pub struct MessageStart {
pub usage: Option<Usage>,
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentBlockStart {
Text { text: String },
ToolUse { id: String, name: String },
Thinking { thinking: String },
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentBlockDelta {
TextDelta { text: String },
InputJsonDelta { partial_json: String },
ThinkingDelta { thinking: String },
}
#[derive(Debug, Deserialize)]
pub struct MessageDelta {
pub stop_reason: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct StreamError {
pub r#type: String,
pub message: String,
}