baochuan 0.1.0

A multi-provider AI API client for Rust — connecting your code to every major AI provider.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use async_trait::async_trait;
use reqwest::{Client, header};
use serde::{Deserialize, Serialize};
use tracing::{debug, error};

use crate::error::BaochuanError;
use crate::provider::{ChunkStream, Provider};
use crate::providers::helpers::parse_data_url;
use crate::providers::sse::anthropic_sse_to_chunks;
use crate::types::{
    ChatMessage, ChatRequest, ChatResponse, ChatChoice, ContentPart, DocumentInput, FunctionCall,
    MessageContent, ModelInfo, Role, ToolCall, ToolChoice, Usage,
};

const BASE_URL: &str = "https://api.anthropic.com/v1";
const ANTHROPIC_VERSION: &str = "2023-06-01";
const DEFAULT_MAX_TOKENS: u32 = 4096;

// ── Model list wire types ─────────────────────────────────────────────────────

#[derive(Deserialize)]
struct AnthropicModelList {
    data: Vec<AnthropicModelEntry>,
}

#[derive(Deserialize)]
struct AnthropicModelEntry {
    id: String,
    display_name: Option<String>,
}

// ── Wire types ────────────────────────────────────────────────────────────────

#[derive(Serialize)]
struct AnthropicRequest<'a> {
    model: &'a str,
    max_tokens: u32,
    messages: Vec<AnthropicMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    system: Option<String>,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    stream: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tools: Option<Vec<AnthropicTool>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tool_choice: Option<AnthropicToolChoice>,
}

#[derive(Serialize)]
struct AnthropicTool {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    input_schema: serde_json::Value,
}

#[derive(Serialize)]
struct AnthropicToolChoice {
    #[serde(rename = "type")]
    choice_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
}

// Anthropic messages can have either plain-text content or an array of typed
// content blocks. We use an untagged enum so serde picks the right variant.
#[derive(Serialize)]
struct AnthropicMessage {
    role: String,
    content: AnthropicMessageContent,
}

#[derive(Serialize)]
#[serde(untagged)]
enum AnthropicMessageContent {
    Text(String),
    Parts(Vec<AnthropicContentBlock>),
}

#[derive(Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum AnthropicContentBlock {
    Text { text: String },
    Image { source: AnthropicImageSource },
    Document { source: AnthropicDocumentSource },
    ToolUse { id: String, name: String, input: serde_json::Value },
    ToolResult { tool_use_id: String, content: String },
}

#[derive(Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum AnthropicImageSource {
    Base64 { media_type: String, data: String },
    Url { url: String },
}

#[derive(Serialize)]
#[serde(tag = "type")]
enum AnthropicDocumentSource {
    #[serde(rename = "base64")]
    Base64 { media_type: String, data: String },
}

#[derive(Deserialize)]
struct AnthropicResponse {
    id: String,
    model: String,
    content: Vec<AnthropicContent>,
    stop_reason: Option<String>,
    usage: AnthropicUsage,
}

#[derive(Deserialize)]
struct AnthropicContent {
    #[serde(rename = "type")]
    content_type: String,
    text: Option<String>,
    /// Present on `tool_use` content blocks.
    id: Option<String>,
    name: Option<String>,
    input: Option<serde_json::Value>,
}

#[derive(Deserialize)]
struct AnthropicUsage {
    input_tokens: u32,
    output_tokens: u32,
}

// ── Conversion helpers ────────────────────────────────────────────────────────

fn content_parts_to_anthropic(parts: &[ContentPart]) -> Vec<AnthropicContentBlock> {
    parts.iter().filter_map(|p| match p {
        ContentPart::Text { text } => {
            Some(AnthropicContentBlock::Text { text: text.clone() })
        }
        ContentPart::ImageUrl { image_url } => {
            if let Some((media_type, data)) = parse_data_url(&image_url.url) {
                Some(AnthropicContentBlock::Image {
                    source: AnthropicImageSource::Base64 { media_type, data },
                })
            } else {
                Some(AnthropicContentBlock::Image {
                    source: AnthropicImageSource::Url { url: image_url.url.clone() },
                })
            }
        }
        ContentPart::Document { document: DocumentInput { data, media_type } } => {
            Some(AnthropicContentBlock::Document {
                source: AnthropicDocumentSource::Base64 {
                    media_type: media_type.clone(),
                    data: data.clone(),
                },
            })
        }
        // Anthropic does not support audio input — drop the part.
        ContentPart::InputAudio { .. } => None,
    }).collect()
}

fn to_anthropic_message(m: &ChatMessage) -> AnthropicMessage {
    // Tool result messages: Role::Tool → user role with tool_result block
    if m.role == Role::Tool {
        let tool_use_id = m.tool_call_id.clone().unwrap_or_default();
        return AnthropicMessage {
            role: "user".to_string(),
            content: AnthropicMessageContent::Parts(vec![
                AnthropicContentBlock::ToolResult {
                    tool_use_id,
                    content: m.content.to_text_lossy(),
                }
            ]),
        };
    }

    // Assistant messages with tool calls: emit text block(s) + tool_use blocks
    if let Some(tool_calls) = &m.tool_calls {
        let mut blocks: Vec<AnthropicContentBlock> = match &m.content {
            MessageContent::Text(s) if !s.is_empty() => {
                vec![AnthropicContentBlock::Text { text: s.clone() }]
            }
            MessageContent::Parts(parts) => content_parts_to_anthropic(parts),
            _ => vec![],
        };
        for tc in tool_calls {
            let input: serde_json::Value =
                serde_json::from_str(&tc.function.arguments).unwrap_or(serde_json::Value::Null);
            blocks.push(AnthropicContentBlock::ToolUse {
                id: tc.id.clone(),
                name: tc.function.name.clone(),
                input,
            });
        }
        return AnthropicMessage {
            role: "assistant".to_string(),
            content: AnthropicMessageContent::Parts(blocks),
        };
    }

    // Standard message
    let content = match &m.content {
        MessageContent::Text(s) => AnthropicMessageContent::Text(s.clone()),
        MessageContent::Parts(parts) => {
            AnthropicMessageContent::Parts(content_parts_to_anthropic(parts))
        }
    };

    AnthropicMessage {
        role: match m.role {
            Role::User => "user".to_string(),
            Role::Assistant => "assistant".to_string(),
            _ => "user".to_string(),
        },
        content,
    }
}

fn to_anthropic_tool_choice(tc: &ToolChoice) -> AnthropicToolChoice {
    use crate::types::tools::{ToolChoicePreset};
    match tc {
        ToolChoice::Preset(ToolChoicePreset::Auto) => {
            AnthropicToolChoice { choice_type: "auto".to_string(), name: None }
        }
        ToolChoice::Preset(ToolChoicePreset::Required) => {
            // Anthropic uses "any" to mean "must use at least one tool"
            AnthropicToolChoice { choice_type: "any".to_string(), name: None }
        }
        ToolChoice::Preset(ToolChoicePreset::None) => {
            AnthropicToolChoice { choice_type: "none".to_string(), name: None }
        }
        ToolChoice::Function(f) => {
            AnthropicToolChoice { choice_type: "tool".to_string(), name: Some(f.function.name.clone()) }
        }
    }
}

fn to_anthropic_request(request: &ChatRequest, stream: bool) -> AnthropicRequest<'_> {
    // Extract system messages into the top-level `system` field
    let system: Option<String> = {
        let parts: Vec<String> = request
            .messages
            .iter()
            .filter(|m| m.role == Role::System)
            .map(|m| m.content.to_text_lossy())
            .collect();
        if parts.is_empty() {
            None
        } else {
            Some(parts.join("\n"))
        }
    };

    let messages = request
        .messages
        .iter()
        .filter(|m| m.role != Role::System)
        .map(to_anthropic_message)
        .collect();

    let tools = request.tools.as_ref().map(|tools| {
        tools.iter().map(|t| AnthropicTool {
            name: t.function.name.clone(),
            description: t.function.description.clone(),
            input_schema: t.function.parameters.clone(),
        }).collect()
    });

    let tool_choice = request.tool_choice.as_ref().map(to_anthropic_tool_choice);

    AnthropicRequest {
        model: &request.model,
        max_tokens: request.max_tokens.unwrap_or(DEFAULT_MAX_TOKENS),
        messages,
        system,
        stream,
        temperature: request.temperature,
        tools,
        tool_choice,
    }
}

fn from_anthropic_response(resp: AnthropicResponse) -> ChatResponse {
    let mut text = String::new();
    let mut tool_calls: Vec<ToolCall> = Vec::new();

    for block in resp.content {
        match block.content_type.as_str() {
            "text" => {
                if let Some(t) = block.text {
                    text.push_str(&t);
                }
            }
            "tool_use" => {
                if let (Some(id), Some(name)) = (block.id, block.name) {
                    let arguments = block.input
                        .map(|v| v.to_string())
                        .unwrap_or_else(|| "{}".to_string());
                    tool_calls.push(ToolCall {
                        id,
                        call_type: "function".to_string(),
                        function: FunctionCall { name, arguments },
                    });
                }
            }
            _ => {}
        }
    }

    let mut message = ChatMessage::assistant(text);
    if !tool_calls.is_empty() {
        message.tool_calls = Some(tool_calls);
    }

    ChatResponse {
        id: resp.id,
        model: resp.model,
        choices: vec![ChatChoice {
            index: 0,
            message,
            finish_reason: resp.stop_reason,
        }],
        usage: Some(Usage {
            prompt_tokens: resp.usage.input_tokens,
            completion_tokens: resp.usage.output_tokens,
            total_tokens: resp.usage.input_tokens + resp.usage.output_tokens,
        }),
        citations: None,
    }
}

// ── Provider ──────────────────────────────────────────────────────────────────

/// A provider that connects to the [Anthropic](https://www.anthropic.com/) API (Claude models).
///
/// The Anthropic API uses a different request/response format from the
/// OpenAI-compatible providers. baochuan handles the conversion automatically —
/// system prompts, message roles, and usage statistics are all mapped to the
/// common types.
///
/// `max_tokens` is required by Anthropic; if you don't set it on the request,
/// baochuan defaults to 4096.
///
/// # Example
/// ```rust,no_run
/// use baochuan::{providers::AnthropicProvider, ChatMessage, ChatRequestBuilder, Provider};
///
/// #[tokio::main]
/// async fn main() {
///     let provider = AnthropicProvider::new(std::env::var("ANTHROPIC_API_KEY").unwrap());
///
///     let request = ChatRequestBuilder::new("claude-opus-4-6")
///         .message(ChatMessage::system("You are a helpful assistant."))
///         .message(ChatMessage::user("What is the capital of France?"))
///         .build()
///         .unwrap();
///
///     let response = provider.chat(&request).await.unwrap();
///     println!("{}", response.content().unwrap_or(""));
/// }
/// ```
pub struct AnthropicProvider {
    client: Client,
    api_key: String,
    base_url: String,
}

impl AnthropicProvider {
    /// Create a new Anthropic provider.
    ///
    /// ```rust,no_run
    /// let provider = baochuan::providers::AnthropicProvider::new(
    ///     std::env::var("ANTHROPIC_API_KEY").expect("ANTHROPIC_API_KEY not set"),
    /// );
    /// ```
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            client: Client::new(),
            api_key: api_key.into(),
            base_url: BASE_URL.to_string(),
        }
    }

    /// Override the base URL (useful for proxies).
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into();
        self
    }

    fn messages_url(&self) -> String {
        format!("{}/messages", self.base_url)
    }

    fn base_request(&self, body: &impl Serialize) -> reqwest::RequestBuilder {
        self.client
            .post(self.messages_url())
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", ANTHROPIC_VERSION)
            .header(header::CONTENT_TYPE, "application/json")
            .json(body)
    }
}

#[async_trait]
impl Provider for AnthropicProvider {
    fn name(&self) -> &str {
        "anthropic"
    }

    async fn models(&self) -> Result<Vec<ModelInfo>, BaochuanError> {
        let url = format!("{}/models", self.base_url);
        let response = self
            .client
            .get(&url)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", ANTHROPIC_VERSION)
            .send()
            .await?;

        let status = response.status();
        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(BaochuanError::Api { status: status.as_u16(), message: body });
        }

        let list: AnthropicModelList = response.json().await?;
        Ok(list.data.into_iter().map(|m| ModelInfo {
            id: m.id,
            owned_by: Some("anthropic".to_string()),
            context_length: None,
            display_name: m.display_name,
        }).collect())
    }

    async fn chat(&self, request: &ChatRequest) -> Result<ChatResponse, BaochuanError> {
        debug!(model = %request.model, "sending chat request to Anthropic");

        let body = to_anthropic_request(request, false);
        let response = self.base_request(&body).send().await?;

        let status = response.status();
        if !status.is_success() {
            let text = response.text().await.unwrap_or_default();
            error!(status = %status, body = %text, "Anthropic API error");
            return Err(BaochuanError::Api {
                status: status.as_u16(),
                message: text,
            });
        }

        let anthropic_response: AnthropicResponse = response.json().await?;
        debug!(id = %anthropic_response.id, "received Anthropic response");
        Ok(from_anthropic_response(anthropic_response))
    }

    async fn stream_chat(&self, request: &ChatRequest) -> Result<ChunkStream, BaochuanError> {
        debug!(model = %request.model, "starting streaming chat request to Anthropic");

        let body = to_anthropic_request(request, true);
        let response = self.base_request(&body).send().await?;

        let status = response.status();
        if !status.is_success() {
            let text = response.text().await.unwrap_or_default();
            error!(status = %status, body = %text, "Anthropic stream error");
            return Err(BaochuanError::Api {
                status: status.as_u16(),
                message: text,
            });
        }

        Ok(Box::pin(anthropic_sse_to_chunks(response.bytes_stream())))
    }
}