agentik-sdk 0.2.0

Comprehensive, type-safe Rust SDK for the Anthropic API with streaming, tools, vision, files, and batch processing support.
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use std::io::{Read, BufReader};
use std::path::Path;
use std::fs::File as StdFile;
use std::fmt;
use bytes::Bytes;
use mime::Mime;
use sha2::{Sha256, Digest};
use base64::{Engine as _, engine::general_purpose};

/// Represents a file that can be uploaded to the Anthropic API
#[derive(Debug, Clone)]
pub struct File {
    /// File name
    pub name: String,
    /// MIME type
    pub mime_type: Mime,
    /// File data
    pub data: FileData,
    /// File size in bytes
    pub size: u64,
    /// Optional file hash for integrity verification
    pub hash: Option<String>,
}

/// Different sources of file data
#[derive(Debug, Clone)]
pub enum FileData {
    /// In-memory bytes
    Bytes(Bytes),
    /// Base64 encoded data
    Base64(String),
    /// File path for lazy loading
    Path(std::path::PathBuf),
    /// Temporary file
    TempFile(std::path::PathBuf),
}

/// File validation constraints
#[derive(Debug, Clone)]
pub struct FileConstraints {
    /// Maximum file size in bytes (default: 10MB)
    pub max_size: u64,
    /// Allowed MIME types (None = allow all)
    pub allowed_types: Option<Vec<Mime>>,
    /// Require hash verification
    pub require_hash: bool,
}

impl Default for FileConstraints {
    fn default() -> Self {
        Self {
            max_size: 10 * 1024 * 1024, // 10MB
            allowed_types: None,
            require_hash: false,
        }
    }
}

/// Errors that can occur during file operations
#[derive(Debug, thiserror::Error)]
pub enum FileError {
    #[error("File not found: {path}")]
    NotFound { path: String },
    
    #[error("File too large: {size} bytes (max: {max_size} bytes)")]
    TooLarge { size: u64, max_size: u64 },
    
    #[error("Invalid MIME type: {mime_type} (allowed: {allowed:?})")]
    InvalidMimeType { mime_type: String, allowed: Vec<String> },
    
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    
    #[error("Invalid base64 data: {0}")]
    InvalidBase64(#[from] base64::DecodeError),
    
    #[error("MIME detection failed")]
    MimeDetectionFailed,
    
    #[error("Hash verification failed")]
    HashVerificationFailed,
    
    #[error("Invalid file data")]
    InvalidData,
}

impl File {
    /// Create a new file from bytes
    pub fn from_bytes(
        name: impl Into<String>,
        bytes: impl Into<Bytes>,
        mime_type: Option<Mime>,
    ) -> Result<Self, FileError> {
        let name = name.into();
        let bytes = bytes.into();
        let size = bytes.len() as u64;
        
        let mime_type = match mime_type {
            Some(mime) => mime,
            None => detect_mime_type(&name, Some(&bytes))?,
        };

        Ok(Self {
            name,
            mime_type,
            data: FileData::Bytes(bytes),
            size,
            hash: None,
        })
    }

    /// Create a new file from a file path
    pub fn from_path(path: impl AsRef<Path>) -> Result<Self, FileError> {
        let path = path.as_ref();
        
        if !path.exists() {
            return Err(FileError::NotFound {
                path: path.display().to_string(),
            });
        }
        
        let metadata = std::fs::metadata(path)?;
        let size = metadata.len();
        let name = path.file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("file")
            .to_string();
            
        let mime_type = detect_mime_type(&name, None)?;

        Ok(Self {
            name,
            mime_type,
            data: FileData::Path(path.to_path_buf()),
            size,
            hash: None,
        })
    }

    /// Create a new file from base64 data
    pub fn from_base64(
        name: impl Into<String>,
        base64_data: impl Into<String>,
        mime_type: Option<Mime>,
    ) -> Result<Self, FileError> {
        let name = name.into();
        let base64_data = base64_data.into();
        
        // Decode to get size
        let decoded = general_purpose::STANDARD.decode(&base64_data)?;
        let size = decoded.len() as u64;
        
        let mime_type = match mime_type {
            Some(mime) => mime,
            None => detect_mime_type(&name, Some(&decoded))?,
        };

        Ok(Self {
            name,
            mime_type,
            data: FileData::Base64(base64_data),
            size,
            hash: None,
        })
    }

    /// Create a file from a standard library File
    pub fn from_std_file(
        std_file: StdFile,
        name: impl Into<String>,
        mime_type: Option<Mime>,
    ) -> Result<Self, FileError> {
        let name = name.into();
        let metadata = std_file.metadata()?;
        let size = metadata.len();
        
        // Read file contents
        let mut reader = BufReader::new(std_file);
        let mut buffer = Vec::new();
        reader.read_to_end(&mut buffer)?;
        
        let mime_type = match mime_type {
            Some(mime) => mime,
            None => detect_mime_type(&name, Some(&buffer))?,
        };

        Ok(Self {
            name,
            mime_type,
            data: FileData::Bytes(Bytes::from(buffer)),
            size,
            hash: None,
        })
    }

    /// Validate the file against constraints
    pub fn validate(&self, constraints: &FileConstraints) -> Result<(), FileError> {
        // Check size
        if self.size > constraints.max_size {
            return Err(FileError::TooLarge {
                size: self.size,
                max_size: constraints.max_size,
            });
        }

        // Check MIME type
        if let Some(allowed_types) = &constraints.allowed_types {
            if !allowed_types.iter().any(|mime| mime == &self.mime_type) {
                return Err(FileError::InvalidMimeType {
                    mime_type: self.mime_type.to_string(),
                    allowed: allowed_types.iter().map(|m| m.to_string()).collect(),
                });
            }
        }

        Ok(())
    }

    /// Get the file data as bytes
    pub async fn to_bytes(&self) -> Result<Bytes, FileError> {
        match &self.data {
            FileData::Bytes(bytes) => Ok(bytes.clone()),
            FileData::Base64(base64_data) => {
                let decoded = general_purpose::STANDARD.decode(base64_data)?;
                Ok(Bytes::from(decoded))
            },
            FileData::Path(path) => {
                let bytes = tokio::fs::read(path).await?;
                Ok(Bytes::from(bytes))
            },
            FileData::TempFile(path) => {
                let bytes = tokio::fs::read(path).await?;
                Ok(Bytes::from(bytes))
            },
        }
    }

    /// Get the file data as base64 string
    pub async fn to_base64(&self) -> Result<String, FileError> {
        match &self.data {
            FileData::Base64(base64_data) => Ok(base64_data.clone()),
            _ => {
                let bytes = self.to_bytes().await?;
                Ok(general_purpose::STANDARD.encode(&bytes))
            }
        }
    }

    /// Calculate and set the file hash
    pub async fn calculate_hash(&mut self) -> Result<String, FileError> {
        let bytes = self.to_bytes().await?;
        let mut hasher = Sha256::new();
        hasher.update(&bytes);
        let hash = format!("{:x}", hasher.finalize());
        self.hash = Some(hash.clone());
        Ok(hash)
    }

    /// Verify the file hash
    pub async fn verify_hash(&self, expected_hash: &str) -> Result<bool, FileError> {
        let bytes = self.to_bytes().await?;
        let mut hasher = Sha256::new();
        hasher.update(&bytes);
        let actual_hash = format!("{:x}", hasher.finalize());
        Ok(actual_hash == expected_hash)
    }

    /// Check if this is an image file
    pub fn is_image(&self) -> bool {
        self.mime_type.type_() == mime::IMAGE
    }

    /// Check if this is a text file
    pub fn is_text(&self) -> bool {
        self.mime_type.type_() == mime::TEXT
    }

    /// Check if this is an application file (e.g., PDF, document)
    pub fn is_application(&self) -> bool {
        self.mime_type.type_() == mime::APPLICATION
    }
}

/// Utility function to create a File from various sources (like TypeScript SDK's toFile)
pub async fn to_file(
    source: FileSource,
    name: Option<String>,
    mime_type: Option<Mime>,
) -> Result<File, FileError> {
    match source {
        FileSource::Bytes(bytes) => {
            let name = name.unwrap_or_else(|| "file".to_string());
            File::from_bytes(name, bytes, mime_type)
        },
        FileSource::Base64(base64_data) => {
            let name = name.unwrap_or_else(|| "file".to_string());
            File::from_base64(name, base64_data, mime_type)
        },
        FileSource::Path(path) => File::from_path(path),
        FileSource::StdFile(std_file, file_name) => {
            let name = name.or(file_name).unwrap_or_else(|| "file".to_string());
            File::from_std_file(std_file, name, mime_type)
        },
    }
}

/// Different sources for creating files
pub enum FileSource {
    /// Raw bytes
    Bytes(Bytes),
    /// Base64 encoded string
    Base64(String),
    /// File system path
    Path(std::path::PathBuf),
    /// Standard library File with optional name
    StdFile(StdFile, Option<String>),
}

/// Detect MIME type from filename and optional file data
fn detect_mime_type(filename: &str, data: Option<&[u8]>) -> Result<Mime, FileError> {
    // First try to detect from file extension
    if let Some(extension) = Path::new(filename).extension() {
        if let Some(ext_str) = extension.to_str() {
            let mime_type = match ext_str.to_lowercase().as_str() {
                // Images
                "jpg" | "jpeg" => mime::IMAGE_JPEG,
                "png" => mime::IMAGE_PNG,
                "gif" => mime::IMAGE_GIF,
                "webp" => "image/webp".parse().unwrap(),
                "svg" => mime::IMAGE_SVG,
                "bmp" => "image/bmp".parse().unwrap(),
                
                // Documents
                "pdf" => "application/pdf".parse().unwrap(),
                "doc" => "application/msword".parse().unwrap(),
                "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document".parse().unwrap(),
                "txt" => mime::TEXT_PLAIN,
                "md" => "text/markdown".parse().unwrap(),
                "rtf" => "application/rtf".parse().unwrap(),
                
                // Spreadsheets
                "xls" => "application/vnd.ms-excel".parse().unwrap(),
                "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".parse().unwrap(),
                
                // Presentations
                "ppt" => "application/vnd.ms-powerpoint".parse().unwrap(),
                "pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation".parse().unwrap(),
                
                // Audio
                "mp3" => "audio/mpeg".parse().unwrap(),
                "wav" => "audio/wav".parse().unwrap(),
                "ogg" => "audio/ogg".parse().unwrap(),
                
                // Video
                "mp4" => "video/mp4".parse().unwrap(),
                "avi" => "video/x-msvideo".parse().unwrap(),
                "mov" => "video/quicktime".parse().unwrap(),
                
                // Archives
                "zip" => "application/zip".parse().unwrap(),
                "tar" => "application/x-tar".parse().unwrap(),
                "gz" => "application/gzip".parse().unwrap(),
                
                // JSON/XML
                "json" => mime::APPLICATION_JSON,
                "xml" => mime::TEXT_XML,
                
                _ => mime::APPLICATION_OCTET_STREAM,
            };
            return Ok(mime_type);
        }
    }

    // If no extension, try magic bytes detection
    if let Some(bytes) = data {
        if bytes.len() >= 4 {
            let magic = &bytes[0..4];
            
            // PNG magic bytes
            if magic == [0x89, 0x50, 0x4E, 0x47] {
                return Ok(mime::IMAGE_PNG);
            }
            
            // JPEG magic bytes
            if magic[0..2] == [0xFF, 0xD8] {
                return Ok(mime::IMAGE_JPEG);
            }
            
            // PDF magic bytes
            if magic == [0x25, 0x50, 0x44, 0x46] {
                return Ok("application/pdf".parse().unwrap());
            }
            
            // GIF magic bytes
            if magic[0..3] == [0x47, 0x49, 0x46] {
                return Ok(mime::IMAGE_GIF);
            }
        }
    }

    // Default fallback
    Ok(mime::APPLICATION_OCTET_STREAM)
}

/// File upload builder for complex scenarios
#[derive(Debug)]
pub struct FileBuilder {
    name: Option<String>,
    mime_type: Option<Mime>,
    constraints: FileConstraints,
    calculate_hash: bool,
}

impl FileBuilder {
    /// Create a new file builder
    pub fn new() -> Self {
        Self {
            name: None,
            mime_type: None,
            constraints: FileConstraints::default(),
            calculate_hash: false,
        }
    }

    /// Set the file name
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set the MIME type
    pub fn mime_type(mut self, mime_type: Mime) -> Self {
        self.mime_type = Some(mime_type);
        self
    }

    /// Set file constraints
    pub fn constraints(mut self, constraints: FileConstraints) -> Self {
        self.constraints = constraints;
        self
    }

    /// Enable hash calculation
    pub fn with_hash(mut self) -> Self {
        self.calculate_hash = true;
        self
    }

    /// Build file from source
    pub async fn build(self, source: FileSource) -> Result<File, FileError> {
        let mut file = to_file(source, self.name, self.mime_type).await?;
        
        // Validate constraints
        file.validate(&self.constraints)?;
        
        // Calculate hash if requested
        if self.calculate_hash {
            file.calculate_hash().await?;
        }
        
        Ok(file)
    }
}

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

impl fmt::Display for File {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "File {{ name: {}, type: {}, size: {} bytes }}",
            self.name, self.mime_type, self.size
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test] 
    fn test_file_from_bytes() {
        let data = b"Hello, world!";
        let file = File::from_bytes("test.txt", Bytes::from_static(data), None).unwrap();
        
        assert_eq!(file.name, "test.txt");
        assert_eq!(file.size, 13);
        assert_eq!(file.mime_type, mime::TEXT_PLAIN);
    }

    #[test]
    fn test_mime_detection() {
        assert_eq!(detect_mime_type("test.jpg", None).unwrap(), mime::IMAGE_JPEG);
        assert_eq!(detect_mime_type("test.png", None).unwrap(), mime::IMAGE_PNG);
        assert_eq!(detect_mime_type("test.txt", None).unwrap(), mime::TEXT_PLAIN);
        assert_eq!(detect_mime_type("test.json", None).unwrap(), mime::APPLICATION_JSON);
    }

    #[test]
    fn test_file_validation() {
        let data = b"Hello, world!";
        let file = File::from_bytes("test.txt", Bytes::from_static(data), None).unwrap();
        
        let constraints = FileConstraints {
            max_size: 10,
            allowed_types: None,
            require_hash: false,
        };
        
        // Should fail size validation
        assert!(file.validate(&constraints).is_err());
    }

    #[test]
    fn test_file_type_checks() {
        let image_file = File::from_bytes("test.jpg", Bytes::new(), Some(mime::IMAGE_JPEG)).unwrap();
        let text_file = File::from_bytes("test.txt", Bytes::new(), Some(mime::TEXT_PLAIN)).unwrap();
        
        assert!(image_file.is_image());
        assert!(!image_file.is_text());
        
        assert!(text_file.is_text());
        assert!(!text_file.is_image());
    }
}