magi-core 0.1.2

LLM-agnostic multi-perspective analysis system inspired by MAGI
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
// Author: Julian Bolivar
// Version: 1.0.0
// Date: 2026-04-05

use crate::error::ProviderError;
use crate::provider::{CompletionConfig, LlmProvider, resolve_claude_alias};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::fmt;

/// Anthropic API base URL.
const API_BASE_URL: &str = "https://api.anthropic.com/v1/messages";

/// Anthropic API version header value.
const ANTHROPIC_VERSION: &str = "2023-06-01";

/// LLM provider that communicates with the Claude Messages API via HTTP.
///
/// Uses `reqwest::Client` for connection pooling — a single client is created
/// at construction time and reused across all requests.
///
/// Feature-gated behind `claude-api`.
///
/// # Examples
///
/// ```no_run
/// use magi_core::providers::claude::ClaudeProvider;
/// use magi_core::provider::{LlmProvider, CompletionConfig};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let provider = ClaudeProvider::new("sk-ant-api03-...", "claude-sonnet-4-6");
/// let response = provider.complete("You are helpful", "Hello", &CompletionConfig::default()).await?;
/// # Ok(())
/// # }
/// ```
pub struct ClaudeProvider {
    client: Client,
    api_key: String,
    model: String,
}

impl fmt::Debug for ClaudeProvider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ClaudeProvider")
            .field("model", &self.model)
            .field("api_key", &"[REDACTED]")
            .finish()
    }
}

/// Request body for the Claude Messages API.
#[derive(Debug, Serialize)]
pub struct ClaudeRequest {
    /// Model identifier.
    pub model: String,
    /// Maximum tokens in the response.
    pub max_tokens: u32,
    /// Sampling temperature.
    pub temperature: f64,
    /// System prompt.
    pub system: String,
    /// Conversation messages.
    pub messages: Vec<ClaudeMessage>,
}

/// A single message in the Claude Messages API request.
#[derive(Debug, Serialize)]
pub struct ClaudeMessage {
    /// Message role ("user" or "assistant").
    pub role: String,
    /// Message content.
    pub content: String,
}

/// Response from the Claude Messages API.
#[derive(Debug, Deserialize)]
struct ClaudeResponse {
    content: Vec<ContentBlock>,
}

/// A content block in the Claude Messages API response.
#[derive(Debug, Deserialize)]
struct ContentBlock {
    #[serde(rename = "type")]
    type_: String,
    text: Option<String>,
}

impl ClaudeProvider {
    /// Creates a new `ClaudeProvider` with the given API key and model.
    ///
    /// Supports model aliases: `"sonnet"`, `"opus"`, `"haiku"`, or any
    /// full model identifier containing `"claude-"`.
    ///
    /// A `reqwest::Client` is created once and reused for all subsequent
    /// requests (connection pooling).
    ///
    /// # Parameters
    /// - `api_key`: Anthropic API key (e.g., `"sk-ant-api03-..."`).
    /// - `model`: Model alias or full identifier (e.g., `"opus"` or `"claude-opus-4-6"`).
    ///
    /// # Errors
    ///
    /// Returns `ProviderError::Auth` if the model alias is unknown.
    pub fn new(
        api_key: impl Into<String>,
        model: impl Into<String>,
    ) -> Result<Self, ProviderError> {
        let model_id = resolve_claude_alias(&model.into())?;
        Ok(Self {
            client: Client::new(),
            api_key: api_key.into(),
            model: model_id,
        })
    }

    /// Returns the provider name.
    pub fn name(&self) -> &str {
        "claude"
    }

    /// Returns the configured model identifier.
    pub fn model(&self) -> &str {
        &self.model
    }

    /// Builds the request body for the Claude Messages API.
    ///
    /// # Parameters
    /// - `system_prompt`: System-level instruction for the LLM.
    /// - `user_prompt`: User's input content.
    /// - `config`: Completion parameters (max_tokens, temperature).
    pub fn build_request_body(
        &self,
        system_prompt: &str,
        user_prompt: &str,
        config: &CompletionConfig,
    ) -> ClaudeRequest {
        ClaudeRequest {
            model: self.model.clone(),
            max_tokens: config.max_tokens,
            temperature: config.temperature,
            system: system_prompt.to_string(),
            messages: vec![ClaudeMessage {
                role: "user".to_string(),
                content: user_prompt.to_string(),
            }],
        }
    }

    /// Parses a Claude Messages API response JSON string and extracts the
    /// first text content block.
    ///
    /// # Parameters
    /// - `body`: Raw JSON response body from the API.
    ///
    /// # Returns
    /// The text content of the first `"text"` content block, or a
    /// `ProviderError` if parsing fails or no text block is found.
    pub fn parse_response(body: &str) -> Result<String, ProviderError> {
        let response: ClaudeResponse =
            serde_json::from_str(body).map_err(|e| ProviderError::Http {
                status: 0,
                body: format!("failed to parse response: {e}"),
            })?;

        response
            .content
            .into_iter()
            .find(|block| block.type_ == "text")
            .and_then(|block| block.text)
            .ok_or_else(|| ProviderError::Http {
                status: 0,
                body: "no text content block in response".to_string(),
            })
    }

    /// Maps an HTTP status code and response body to the appropriate
    /// `ProviderError` variant.
    ///
    /// - 401 and 403 map to `ProviderError::Auth`.
    /// - All other non-2xx status codes map to `ProviderError::Http`.
    pub fn map_status_to_error(status: u16, body: &str) -> ProviderError {
        match status {
            401 | 403 => ProviderError::Auth {
                message: body.to_string(),
            },
            _ => ProviderError::Http {
                status,
                body: body.to_string(),
            },
        }
    }
}

#[async_trait::async_trait]
impl LlmProvider for ClaudeProvider {
    /// Sends a completion request to the Claude Messages API.
    ///
    /// Builds a POST request with the appropriate headers and body, sends it,
    /// and parses the response to extract the text content.
    ///
    /// # Errors
    /// - `ProviderError::Timeout` if the request times out.
    /// - `ProviderError::Network` on connection failures.
    /// - `ProviderError::Auth` on 401/403 responses.
    /// - `ProviderError::Http` on other non-2xx responses.
    async fn complete(
        &self,
        system_prompt: &str,
        user_prompt: &str,
        config: &CompletionConfig,
    ) -> Result<String, ProviderError> {
        let body = self.build_request_body(system_prompt, user_prompt, config);

        let response = self
            .client
            .post(API_BASE_URL)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", ANTHROPIC_VERSION)
            .header("content-type", "application/json")
            .json(&body)
            .send()
            .await
            .map_err(|e| {
                if e.is_timeout() {
                    ProviderError::Timeout {
                        message: e.to_string(),
                    }
                } else {
                    ProviderError::Network {
                        message: e.to_string(),
                    }
                }
            })?;

        let status = response.status().as_u16();
        if !(200..300).contains(&status) {
            let response_body = response.text().await.unwrap_or_default();
            return Err(Self::map_status_to_error(status, &response_body));
        }

        let response_body = response.text().await.map_err(|e| ProviderError::Network {
            message: format!("failed to read response body: {e}"),
        })?;

        Self::parse_response(&response_body)
    }

    fn name(&self) -> &str {
        "claude"
    }

    fn model(&self) -> &str {
        &self.model
    }
}

#[cfg(test)]
mod tests {
    // -- Construction and accessors --

    /// ClaudeProvider::new creates provider with api_key and model.
    #[test]
    fn test_claude_provider_new_creates_with_key_and_model() {
        let provider = super::ClaudeProvider::new("sk-test-key", "claude-sonnet-4-6").unwrap();
        assert_eq!(provider.name(), "claude");
        assert_eq!(provider.model(), "claude-sonnet-4-6");
    }

    /// provider.name() returns "claude".
    #[test]
    fn test_claude_provider_name_returns_claude() {
        let provider = super::ClaudeProvider::new("key", "claude-sonnet-4-6").unwrap();
        assert_eq!(provider.name(), "claude");
    }

    /// provider.model() returns the configured model string.
    #[test]
    fn test_claude_provider_model_returns_configured_model() {
        let provider = super::ClaudeProvider::new("key", "claude-opus-4-6").unwrap();
        assert_eq!(provider.model(), "claude-opus-4-6");
    }

    /// new("opus") resolves alias to full model identifier.
    #[test]
    fn test_claude_provider_new_resolves_alias() {
        let provider = super::ClaudeProvider::new("key", "opus").unwrap();
        assert_eq!(provider.model(), "claude-opus-4-6");
    }

    /// new("unknown") returns ProviderError::Auth.
    #[test]
    fn test_claude_provider_new_rejects_unknown_model() {
        let result = super::ClaudeProvider::new("key", "unknown");
        assert!(result.is_err());
    }

    // -- Request building --

    /// build_request_body produces correct JSON structure with all required fields.
    #[test]
    fn test_build_request_body_contains_all_required_fields() {
        use crate::provider::CompletionConfig;

        let provider = super::ClaudeProvider::new("sk-test", "claude-sonnet-4-6").unwrap();
        let config = CompletionConfig::default();
        let body = provider.build_request_body("You are helpful", "Hello", &config);

        assert_eq!(body.model, "claude-sonnet-4-6");
        assert_eq!(body.max_tokens, 4096);
        assert!((body.temperature - 0.0).abs() < f64::EPSILON);
        assert_eq!(body.system, "You are helpful");
        assert_eq!(body.messages.len(), 1);
        assert_eq!(body.messages[0].role, "user");
        assert_eq!(body.messages[0].content, "Hello");
    }

    // -- Response parsing --

    /// parse_response extracts text content from Claude response format.
    #[test]
    fn test_parse_claude_response_extracts_text_content() {
        let json = r#"{"content": [{"type": "text", "text": "response text"}], "id": "msg_1", "model": "claude-sonnet-4-6", "role": "assistant"}"#;
        let result = super::ClaudeProvider::parse_response(json);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "response text");
    }

    /// parse_response handles multiple content blocks, extracting first text block.
    #[test]
    fn test_parse_response_extracts_first_text_block() {
        let json = r#"{"content": [{"type": "text", "text": "first"}, {"type": "text", "text": "second"}], "id": "msg_1", "model": "m", "role": "assistant"}"#;
        let result = super::ClaudeProvider::parse_response(json);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "first");
    }

    /// parse_response returns error when no text content block found.
    #[test]
    fn test_parse_response_error_when_no_text_block() {
        let json = r#"{"content": [], "id": "msg_1", "model": "m", "role": "assistant"}"#;
        let result = super::ClaudeProvider::parse_response(json);
        assert!(result.is_err());
    }

    /// parse_response returns error on invalid JSON.
    #[test]
    fn test_parse_response_error_on_invalid_json() {
        let result = super::ClaudeProvider::parse_response("not json");
        assert!(result.is_err());
    }

    // -- Error mapping --

    /// map_status_to_error maps 401 to ProviderError::Auth.
    #[test]
    fn test_map_status_401_to_auth_error() {
        let err = super::ClaudeProvider::map_status_to_error(401, "unauthorized");
        assert!(matches!(err, crate::error::ProviderError::Auth { .. }));
    }

    /// map_status_to_error maps 403 to ProviderError::Auth.
    #[test]
    fn test_map_status_403_to_auth_error() {
        let err = super::ClaudeProvider::map_status_to_error(403, "forbidden");
        assert!(matches!(err, crate::error::ProviderError::Auth { .. }));
    }

    /// map_status_to_error maps 500 to ProviderError::Http.
    #[test]
    fn test_map_status_500_to_http_error() {
        let err = super::ClaudeProvider::map_status_to_error(500, "server error");
        match err {
            crate::error::ProviderError::Http { status, body } => {
                assert_eq!(status, 500);
                assert_eq!(body, "server error");
            }
            other => panic!("expected Http, got: {other}"),
        }
    }

    /// map_status_to_error maps 429 to ProviderError::Http (not Auth).
    #[test]
    fn test_map_status_429_to_http_error() {
        let err = super::ClaudeProvider::map_status_to_error(429, "rate limited");
        assert!(matches!(
            err,
            crate::error::ProviderError::Http { status: 429, .. }
        ));
    }

    // -- Client reuse --

    /// reqwest::Client is stored in struct (structural test).
    #[test]
    fn test_client_is_stored_in_struct() {
        let provider = super::ClaudeProvider::new("key", "sonnet").unwrap();
        assert_eq!(provider.name(), "claude");
        let provider2 = super::ClaudeProvider::new("key2", "opus").unwrap();
        assert_eq!(provider2.model(), "claude-opus-4-6");
    }

    // -- Debug does not expose API key --

    /// Debug output does not contain the API key.
    #[test]
    fn test_debug_does_not_expose_api_key() {
        let provider = super::ClaudeProvider::new("sk-super-secret-key-12345", "sonnet").unwrap();
        let debug_str = format!("{:?}", provider);
        assert!(
            !debug_str.contains("sk-super-secret-key-12345"),
            "Debug output must not contain API key, got: {debug_str}"
        );
    }
}