use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize)]
pub struct AnthropicRequest {
pub model: String,
pub max_tokens: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub system: Option<String>,
pub messages: Vec<AnthropicMessage>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub tools: Vec<AnthropicToolDef>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_choice: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f32>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub stop_sequences: Vec<String>,
pub stream: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnthropicMessage {
pub role: String,
pub content: Vec<AnthropicContentBlock>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AnthropicContentBlock {
#[serde(rename = "text")]
Text {
text: String,
},
#[serde(rename = "image")]
Image {
source: AnthropicImageSource,
},
#[serde(rename = "tool_use")]
ToolUse {
id: String,
name: String,
input: Value,
},
#[serde(rename = "tool_result")]
ToolResult {
tool_use_id: String,
content: Vec<AnthropicToolResultContent>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AnthropicToolResultContent {
#[serde(rename = "text")]
Text {
text: String,
},
#[serde(rename = "image")]
Image {
source: AnthropicImageSource,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnthropicImageSource {
#[serde(rename = "type")]
pub source_type: String,
pub url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub media_type: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct AnthropicToolDef {
pub name: String,
pub description: String,
pub input_schema: Value,
}
#[derive(Debug, Clone, Deserialize)]
pub struct AnthropicResponse {
pub id: String,
pub model: String,
pub content: Vec<AnthropicContentBlock>,
pub stop_reason: Option<String>,
pub usage: Option<AnthropicUsage>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct AnthropicUsage {
pub input_tokens: u64,
pub output_tokens: u64,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type")]
pub enum AnthropicStreamEvent {
#[serde(rename = "message_start")]
MessageStart {
message: AnthropicStreamMessage,
},
#[serde(rename = "content_block_start")]
ContentBlockStart {
index: usize,
content_block: AnthropicContentBlock,
},
#[serde(rename = "content_block_delta")]
ContentBlockDelta {
index: usize,
delta: AnthropicDelta,
},
#[serde(rename = "content_block_stop")]
ContentBlockStop {
index: usize,
},
#[serde(rename = "message_delta")]
MessageDelta {
delta: AnthropicMessageDelta,
usage: Option<AnthropicUsage>,
},
#[serde(rename = "message_stop")]
MessageStop,
#[serde(other)]
Other,
}
#[derive(Debug, Clone, Deserialize)]
pub struct AnthropicStreamMessage {
pub model: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type")]
pub enum AnthropicDelta {
#[serde(rename = "text_delta")]
TextDelta {
text: String,
},
#[serde(rename = "input_json_delta")]
InputJsonDelta {
partial_json: String,
},
#[serde(other)]
Other,
}
#[derive(Debug, Clone, Deserialize)]
pub struct AnthropicMessageDelta {
pub stop_reason: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct AnthropicErrorBody {
#[serde(rename = "error")]
pub detail: Option<AnthropicErrorDetail>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct AnthropicErrorDetail {
#[serde(rename = "type")]
pub kind: Option<String>,
pub message: Option<String>,
}