stakai 0.3.70

A provider-agnostic Rust SDK for AI completions with streaming support - Built by Stakpak
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
476
477
478
479
480
//! Anthropic-specific types

use crate::types::{CacheControl, CacheStrategy};
use serde::{Deserialize, Serialize};

/// Authentication type for Anthropic
#[derive(Debug, Clone)]
pub enum AnthropicAuth {
    /// API key authentication (x-api-key header)
    ApiKey(String),
    /// OAuth 2.0 authentication (Bearer token)
    OAuth {
        /// Access token
        access_token: String,
    },
}

impl AnthropicAuth {
    /// Create API key authentication
    pub fn api_key(key: impl Into<String>) -> Self {
        Self::ApiKey(key.into())
    }

    /// Create OAuth authentication
    pub fn oauth(access_token: impl Into<String>) -> Self {
        Self::OAuth {
            access_token: access_token.into(),
        }
    }

    /// Check if credentials are empty
    pub fn is_empty(&self) -> bool {
        match self {
            Self::ApiKey(key) => key.is_empty(),
            Self::OAuth { access_token } => access_token.is_empty(),
        }
    }

    /// Get the authorization header value
    pub fn to_header(&self) -> (&'static str, String) {
        match self {
            Self::ApiKey(key) => ("x-api-key", key.clone()),
            Self::OAuth { access_token } => ("authorization", format!("Bearer {}", access_token)),
        }
    }
}

/// Configuration for Anthropic provider
#[derive(Debug, Clone)]
pub struct AnthropicConfig {
    /// Authentication (API key or OAuth)
    pub auth: AnthropicAuth,
    /// Base URL (default: https://api.anthropic.com/v1)
    pub base_url: String,
    /// Anthropic API version (default: 2023-06-01)
    pub anthropic_version: String,
    /// Beta features to enable (e.g., ["prompt-caching-2024-07-31"])
    pub beta_features: Vec<String>,
    /// Default caching strategy for requests (can be overridden per-request)
    ///
    /// Defaults to `CacheStrategy::Auto` which applies optimal caching:
    /// - Last tool definition (caches all tools)
    /// - Last system message
    /// - Last 2 non-system messages
    pub default_cache_strategy: CacheStrategy,
}

/// Beta header for OAuth authentication
/// Required headers for Claude Pro/Max OAuth tokens to work:
/// - oauth-2025-04-20: REQUIRED - enables OAuth authentication support
/// - claude-code-20250219: Required for Claude Code product access (OAuth tokens are restricted to this)
/// - interleaved-thinking-2025-05-14: Extended thinking support
/// - fine-grained-tool-streaming-2025-05-14: Tool streaming support
pub const OAUTH_BETA_HEADER: &str = "oauth-2025-04-20,claude-code-20250219,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14";

/// System prompt prefix required for Claude Code OAuth tokens
/// OAuth tokens from Claude Pro/Max subscriptions are restricted to "Claude Code" product.
/// This exact prefix MUST be the first system block with ephemeral cache control
/// for the API to accept requests to advanced models like Opus/Sonnet.
pub const CLAUDE_CODE_SYSTEM_PREFIX: &str =
    "You are Claude Code, Anthropic's official CLI for Claude.";

impl AnthropicConfig {
    /// Create new config with API key
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            auth: AnthropicAuth::api_key(api_key),
            base_url: "https://api.anthropic.com/v1/".to_string(),
            anthropic_version: "2023-06-01".to_string(),
            beta_features: vec![],
            default_cache_strategy: CacheStrategy::Auto,
        }
    }

    /// Create new config with OAuth access token
    pub fn with_oauth(access_token: impl Into<String>) -> Self {
        Self {
            auth: AnthropicAuth::oauth(access_token),
            base_url: "https://api.anthropic.com/v1/".to_string(),
            anthropic_version: "2023-06-01".to_string(),
            beta_features: vec![OAUTH_BETA_HEADER.to_string()],
            default_cache_strategy: CacheStrategy::Auto,
        }
    }

    /// Create new config with authentication
    pub fn with_auth(auth: AnthropicAuth) -> Self {
        let beta_features = match &auth {
            AnthropicAuth::OAuth { .. } => vec![OAUTH_BETA_HEADER.to_string()],
            AnthropicAuth::ApiKey(_) => vec![],
        };

        Self {
            auth,
            base_url: "https://api.anthropic.com/v1/".to_string(),
            anthropic_version: "2023-06-01".to_string(),
            beta_features,
            default_cache_strategy: CacheStrategy::Auto,
        }
    }

    /// Get API key (for backward compatibility)
    /// Returns empty string for OAuth auth
    #[deprecated(note = "Use auth field directly instead")]
    pub fn api_key(&self) -> &str {
        match &self.auth {
            AnthropicAuth::ApiKey(key) => key,
            AnthropicAuth::OAuth { .. } => "",
        }
    }

    /// Set base URL
    /// Normalizes the URL by stripping `/messages` suffix if present,
    /// since the provider appends the endpoint path automatically.
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        let mut url = base_url.into();
        // Strip /messages suffix if user provided full endpoint URL
        if url.ends_with("/messages") {
            url = url.trim_end_matches("/messages").to_string();
        } else if url.ends_with("/messages/") {
            url = url.trim_end_matches("/messages/").to_string();
        }
        // Ensure URL ends with /
        if !url.ends_with('/') {
            url.push('/');
        }
        self.base_url = url;
        self
    }

    /// Set API version
    pub fn with_version(mut self, version: impl Into<String>) -> Self {
        self.anthropic_version = version.into();
        self
    }

    /// Add beta feature
    pub fn with_beta_feature(mut self, feature: impl Into<String>) -> Self {
        self.beta_features.push(feature.into());
        self
    }

    /// Set default caching strategy
    ///
    /// This strategy will be used for all requests unless overridden
    /// via `GenerateOptions::with_cache_strategy()`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use stakai::{providers::anthropic::AnthropicConfig, CacheStrategy};
    ///
    /// // Disable caching by default for this provider
    /// let config = AnthropicConfig::new("api-key")
    ///     .with_cache_strategy(CacheStrategy::None);
    ///
    /// // Custom caching: only cache system prompts
    /// let config = AnthropicConfig::new("api-key")
    ///     .with_cache_strategy(CacheStrategy::anthropic(false, true, 0));
    /// ```
    pub fn with_cache_strategy(mut self, strategy: CacheStrategy) -> Self {
        self.default_cache_strategy = strategy;
        self
    }
}

impl Default for AnthropicConfig {
    fn default() -> Self {
        Self {
            auth: AnthropicAuth::api_key(
                std::env::var("ANTHROPIC_API_KEY").unwrap_or_else(|_| String::new()),
            ),
            base_url: "https://api.anthropic.com/v1/".to_string(),
            anthropic_version: "2023-06-01".to_string(),
            beta_features: vec![],
            default_cache_strategy: CacheStrategy::Auto,
        }
    }
}

/// Anthropic cache control (for prompt caching)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnthropicCacheControl {
    /// Cache type (currently only "ephemeral")
    #[serde(rename = "type")]
    pub type_: String,
    /// Optional TTL (e.g., "1h")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ttl: Option<String>,
}

impl From<&CacheControl> for AnthropicCacheControl {
    fn from(cache: &CacheControl) -> Self {
        match cache {
            CacheControl::Ephemeral { ttl } => Self {
                type_: "ephemeral".to_string(),
                ttl: ttl.clone(),
            },
        }
    }
}

impl AnthropicCacheControl {
    /// Create ephemeral cache control
    pub fn ephemeral() -> Self {
        Self {
            type_: "ephemeral".to_string(),
            ttl: None,
        }
    }

    /// Create ephemeral cache control with TTL
    pub fn ephemeral_with_ttl(ttl: impl Into<String>) -> Self {
        Self {
            type_: "ephemeral".to_string(),
            ttl: Some(ttl.into()),
        }
    }
}

/// Anthropic system content block (with cache control support)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnthropicSystemBlock {
    /// Type (always "text" for system messages)
    #[serde(rename = "type")]
    pub type_: String,
    /// The text content
    pub text: String,
    /// Optional cache control
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<AnthropicCacheControl>,
}

impl AnthropicSystemBlock {
    /// Create a new system block with text
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            type_: "text".to_string(),
            text: text.into(),
            cache_control: None,
        }
    }

    /// Create a new system block with ephemeral cache control
    pub fn with_ephemeral_cache(text: impl Into<String>) -> Self {
        Self {
            type_: "text".to_string(),
            text: text.into(),
            cache_control: Some(AnthropicCacheControl::ephemeral()),
        }
    }

    /// Create a new system block without cache control (alias for new)
    pub fn text(text: impl Into<String>) -> Self {
        Self::new(text)
    }

    /// Add cache control to this block
    pub fn with_cache_control(mut self, cache_control: AnthropicCacheControl) -> Self {
        self.cache_control = Some(cache_control);
        self
    }
}

/// Anthropic system content (can be string or array of blocks)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AnthropicSystemContent {
    /// Simple string (no cache control)
    String(String),
    /// Array of blocks (supports cache control)
    Blocks(Vec<AnthropicSystemBlock>),
}

/// Anthropic messages request
#[derive(Debug, Serialize)]
pub struct AnthropicRequest {
    pub model: String,
    pub messages: Vec<AnthropicMessage>,
    pub max_tokens: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system: Option<AnthropicSystemContent>,
    #[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 = "Option::is_none")]
    pub top_k: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_sequences: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<AnthropicThinkingConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<serde_json::Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<serde_json::Value>,
}

/// Thinking/reasoning configuration
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicThinkingConfig {
    #[serde(rename = "type")]
    pub type_: String,
    pub budget_tokens: u32,
}

/// Anthropic message
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicMessage {
    pub role: String,
    pub content: AnthropicMessageContent,
}

/// Anthropic response
#[derive(Debug, Serialize, Deserialize)]
pub struct AnthropicResponse {
    pub id: String,
    #[serde(rename = "type")]
    pub type_: String,
    pub role: String,
    pub content: Vec<AnthropicContent>,
    pub model: String,
    pub stop_reason: Option<String>,
    pub usage: AnthropicUsage,
}

/// Anthropic content block (with cache control support)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AnthropicContent {
    Text {
        text: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<AnthropicCacheControl>,
    },
    Image {
        source: AnthropicSource,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<AnthropicCacheControl>,
    },
    Thinking {
        thinking: String,
        signature: String,
        // Note: thinking blocks cannot have cache_control directly
    },
    RedactedThinking {
        data: String,
        // Note: redacted thinking blocks cannot have cache_control directly
    },
    ToolUse {
        id: String,
        name: String,
        input: serde_json::Value,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<AnthropicCacheControl>,
    },
    ToolResult {
        tool_use_id: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        content: Option<AnthropicMessageContent>,
        #[serde(skip_serializing_if = "Option::is_none")]
        is_error: Option<bool>,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<AnthropicCacheControl>,
    },
}

/// Anthropic message content (can be string or array of content blocks)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum AnthropicMessageContent {
    String(String),
    Blocks(Vec<AnthropicContent>),
}

impl Default for AnthropicMessageContent {
    fn default() -> Self {
        AnthropicMessageContent::String(String::new())
    }
}

/// Anthropic source (for images/PDFs)
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicSource {
    #[serde(rename = "type")]
    pub type_: String, // "base64"
    pub media_type: String,
    pub data: String,
}

/// Anthropic usage statistics
///
/// Both `input_tokens` and `output_tokens` default to 0 when absent.
/// The `message_start` event includes both, but `message_delta` only has `output_tokens`.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicUsage {
    #[serde(default)]
    pub input_tokens: u32,
    #[serde(default)]
    pub output_tokens: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_creation_input_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_read_input_tokens: Option<u32>,
}

/// Anthropic streaming event
#[derive(Debug, Deserialize)]
pub struct AnthropicStreamEvent {
    #[serde(rename = "type")]
    pub type_: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<AnthropicResponse>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_block: Option<AnthropicContent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delta: Option<AnthropicDelta>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<AnthropicUsage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<AnthropicError>,
}

/// Anthropic streaming delta
#[derive(Debug, Deserialize)]
pub struct AnthropicDelta {
    #[serde(rename = "type")]
    pub type_: Option<String>,
    pub text: Option<String>,
    pub thinking: Option<String>,
    pub _signature: Option<String>,
    pub partial_json: Option<String>,
    pub _stop_reason: Option<String>,
    pub _stop_sequence: Option<String>,
}

/// Anthropic error details
#[derive(Debug, Deserialize)]
pub struct AnthropicError {
    pub message: String,
}

/// Infer max_tokens based on model name
pub fn infer_max_tokens(model: &str) -> u32 {
    if model.contains("opus-4-5") || model.contains("sonnet-4") || model.contains("haiku-4") {
        64000
    } else if model.contains("opus-4") {
        32000
    } else if model.contains("3-5") {
        8192
    } else {
        4096
    }
}