multi_llm/providers/anthropic/
types.rs

1//! Anthropic API request and response type definitions
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6/// Cache control for prompt caching
7#[derive(Debug, Serialize, Deserialize, Clone)]
8pub struct CacheControl {
9    #[serde(rename = "type")]
10    pub cache_type: String, // "ephemeral"
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub ttl: Option<String>, // "5m" or "1h"
13}
14
15/// System message with optional cache control
16#[derive(Debug, Serialize, Deserialize, Clone)]
17pub struct SystemMessage {
18    #[serde(rename = "type")]
19    pub message_type: String, // "text"
20    pub text: String,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub cache_control: Option<CacheControl>,
23}
24
25/// System field can be either a string (legacy) or array of system messages (with caching)
26#[derive(Debug, Serialize, Deserialize, Clone)]
27#[serde(untagged)]
28pub enum SystemField {
29    String(String),
30    Messages(Vec<SystemMessage>),
31}
32
33/// Anthropic Messages API request structure
34/// Fields are ordered to match Anthropic's caching hierarchy: tools->system->messages->params
35#[derive(Debug, Serialize, Clone)]
36pub(super) struct AnthropicRequest {
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub tools: Option<Vec<Value>>,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub system: Option<SystemField>,
41    pub messages: Vec<AnthropicMessage>,
42    pub model: String,
43    pub max_tokens: u32,
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub temperature: Option<f32>,
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub top_p: Option<f32>,
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub top_k: Option<u32>,
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub stop_sequences: Option<Vec<String>>,
52}
53
54/// Anthropic message structure
55#[derive(Debug, Serialize, Deserialize, Clone)]
56pub(super) struct AnthropicMessage {
57    pub role: String,
58    pub content: AnthropicContent,
59}
60
61/// Anthropic content structure (can be string or array of content blocks)
62#[derive(Debug, Serialize, Deserialize, Clone)]
63#[serde(untagged)]
64pub(super) enum AnthropicContent {
65    Text(String),
66    Blocks(Vec<AnthropicContentBlock>),
67}
68
69/// Anthropic content block structure
70#[derive(Debug, Serialize, Deserialize, Clone)]
71#[serde(tag = "type")]
72pub(super) enum AnthropicContentBlock {
73    #[serde(rename = "text")]
74    Text {
75        text: String,
76        #[serde(skip_serializing_if = "Option::is_none")]
77        cache_control: Option<CacheControl>,
78    },
79    #[serde(rename = "tool_use")]
80    ToolUse {
81        id: String,
82        name: String,
83        input: Value,
84    },
85    #[serde(rename = "tool_result")]
86    ToolResult {
87        tool_use_id: String,
88        content: String,
89    },
90}
91
92/// Anthropic API response structure
93#[derive(Debug, Deserialize, Serialize, Clone)]
94pub(super) struct AnthropicResponse {
95    pub id: String,
96    #[serde(rename = "type")]
97    pub response_type: String,
98    pub role: String,
99    pub content: Vec<AnthropicContentBlock>,
100    pub model: String,
101    pub stop_reason: Option<String>,
102    pub stop_sequence: Option<String>,
103    pub usage: AnthropicUsage,
104}
105
106/// Anthropic usage information with cache statistics
107#[derive(Debug, Deserialize, Serialize, Clone)]
108pub(super) struct AnthropicUsage {
109    pub input_tokens: u32,
110    pub output_tokens: u32,
111    #[serde(default)]
112    pub cache_creation_input_tokens: Option<u32>,
113    #[serde(default)]
114    pub cache_read_input_tokens: Option<u32>,
115    #[serde(default)]
116    pub cache_creation: Option<CacheCreationDetails>,
117}
118
119/// Detailed cache creation breakdown
120#[derive(Debug, Deserialize, Serialize, Clone)]
121pub(super) struct CacheCreationDetails {
122    #[serde(default)]
123    pub ephemeral_5m_input_tokens: Option<u32>,
124    #[serde(default)]
125    pub ephemeral_1h_input_tokens: Option<u32>,
126}