cc-agent-sdk 0.1.7

claude agent sdk
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
//! Example: Comprehensive Multimodal Input Handling
//!
//! This example demonstrates comprehensive multimodal input handling
//! including multiple images, mixed content types, and preprocessing.
//!
//! What it demonstrates:
//! 1. Processing multiple images in a single query
//! 2. Mixed content types (text + images + URLs)
//! 3. Image preprocessing and optimization
//! 4. URL vs base64 encoding comparison
//! 5. Handling large images and batch processing

use anyhow::Result;
use std::time::Instant;

/// Simulated image data structure
#[derive(Debug, Clone)]
struct ImageData {
    name: String,
    format: ImageFormat,
    size_bytes: usize,
    dimensions: (u32, u32),
    data: Vec<u8>,
}

/// Supported image formats
#[derive(Debug, Clone, Copy)]
enum ImageFormat {
    Jpeg,
    Png,
    Gif,
    WebP,
}

impl ImageFormat {
    fn mime_type(&self) -> &'static str {
        match self {
            Self::Jpeg => "image/jpeg",
            Self::Png => "image/png",
            Self::Gif => "image/gif",
            Self::WebP => "image/webp",
        }
    }

    fn extension(&self) -> &'static str {
        match self {
            Self::Jpeg => "jpg",
            Self::Png => "png",
            Self::Gif => "gif",
            Self::WebP => "webp",
        }
    }
}

/// Content block types for multimodal queries
#[derive(Debug, Clone)]
enum ContentBlock {
    Text { content: String },
    ImageBase64 { media_type: String, data: String },
    ImageUrl { url: String },
}

/// Image preprocessing options
#[derive(Debug, Clone)]
struct PreprocessOptions {
    max_width: u32,
    max_height: u32,
    quality: u8, // 1-100 for JPEG
    convert_to: Option<ImageFormat>,
}

impl Default for PreprocessOptions {
    fn default() -> Self {
        Self {
            max_width: 2048,
            max_height: 2048,
            quality: 85,
            convert_to: None,
        }
    }
}

fn main() -> Result<()> {
    println!("=== Comprehensive Multimodal Input Examples ===\n");

    multiple_images_example()?;
    mixed_content_example()?;
    image_preprocessing_example()?;
    url_vs_base64_example()?;
    large_image_handling_example()?;

    Ok(())
}

/// Demonstrates processing multiple images in a single query
fn multiple_images_example() -> Result<()> {
    println!("=== Multiple Images in Single Query ===\n");

    // Simulate multiple images
    let images = vec![
        ImageData {
            name: "screenshot1.png".to_string(),
            format: ImageFormat::Png,
            size_bytes: 150_000,
            dimensions: (1920, 1080),
            data: vec![0; 150_000],
        },
        ImageData {
            name: "diagram.jpg".to_string(),
            format: ImageFormat::Jpeg,
            size_bytes: 85_000,
            dimensions: (1200, 800),
            data: vec![0; 85_000],
        },
        ImageData {
            name: "code_snippet.png".to_string(),
            format: ImageFormat::Png,
            size_bytes: 45_000,
            dimensions: (800, 600),
            data: vec![0; 45_000],
        },
    ];

    println!("Processing {} images:", images.len());

    let mut content_blocks = Vec::new();

    // Add text context first
    content_blocks.push(ContentBlock::Text {
        content: "Please analyze these screenshots and diagrams:".to_string(),
    });

    // Add each image
    for (i, image) in images.iter().enumerate() {
        println!("\n  Image {}: {}", i + 1, image.name);
        println!("    Format: {}", image.format.mime_type());
        println!("    Size: {} bytes ({:.1} KB)", image.size_bytes, image.size_bytes as f64 / 1024.0);
        println!("    Dimensions: {}x{}", image.dimensions.0, image.dimensions.1);

        // In real code, you would base64 encode the image data
        let base64_data = simulate_base64_encode(&image.data);
        content_blocks.push(ContentBlock::ImageBase64 {
            media_type: image.format.mime_type().to_string(),
            data: base64_data,
        });

        // Add label for each image
        content_blocks.push(ContentBlock::Text {
            content: format!("[Image {}: {}]", i + 1, image.name),
        });
    }

    println!("\nTotal content blocks: {}", content_blocks.len());
    println!("  Text blocks: {}", content_blocks.iter().filter(|b| matches!(b, ContentBlock::Text { .. })).count());
    println!("  Image blocks: {}", content_blocks.iter().filter(|b| matches!(b, ContentBlock::ImageBase64 { .. })).count());

    println!("\nBest practices for multiple images:");
    println!("  - Add text labels between images for context");
    println!("  - Keep total image count reasonable (≤5 recommended)");
    println!("  - Optimize image sizes before sending");
    println!("  - Use consistent formats when possible");
    println!();

    Ok(())
}

/// Demonstrates mixed content types (text + images + URLs)
fn mixed_content_example() -> Result<()> {
    println!("=== Mixed Content Types ===\n");

    let content_blocks = vec![
        // Opening text
        ContentBlock::Text {
            content: "I need help understanding this architecture:".to_string(),
        },
        // Local image (base64)
        ContentBlock::ImageBase64 {
            media_type: "image/png".to_string(),
            data: "base64_encoded_diagram_data...".to_string(),
        },
        // More context
        ContentBlock::Text {
            content: "The diagram above shows the current system. Compare with this reference:".to_string(),
        },
        // External image (URL)
        ContentBlock::ImageUrl {
            url: "https://example.com/reference-architecture.png".to_string(),
        },
        // Follow-up question
        ContentBlock::Text {
            content: "What improvements would you suggest?".to_string(),
        },
    ];

    println!("Mixed content query structure:");
    for (i, block) in content_blocks.iter().enumerate() {
        match block {
            ContentBlock::Text { content } => {
                let preview = if content.len() > 50 {
                    &content[..50]
                } else {
                    content
                };
                println!("  {}. [TEXT] \"{}...\"", i + 1, preview);
            }
            ContentBlock::ImageBase64 { media_type, .. } => {
                println!("  {}. [IMAGE_BASE64] {}", i + 1, media_type);
            }
            ContentBlock::ImageUrl { url } => {
                println!("  {}. [IMAGE_URL] {}", i + 1, url);
            }
        }
    }

    println!("\nUse cases for mixed content:");
    println!("  - Comparing local and reference images");
    println!("  - Providing context with visual examples");
    println!("  - Multi-step visual analysis tasks");
    println!("  - Documentation with embedded images");
    println!();

    Ok(())
}

/// Demonstrates image preprocessing and optimization
fn image_preprocessing_example() -> Result<()> {
    println!("=== Image Preprocessing ===\n");

    let original_image = ImageData {
        name: "large_photo.png".to_string(),
        format: ImageFormat::Png,
        size_bytes: 5_200_000, // 5.2 MB
        dimensions: (4000, 3000),
        data: vec![0; 5_200_000],
    };

    println!("Original image:");
    println!("  Name: {}", original_image.name);
    println!("  Format: {}", original_image.format.mime_type());
    println!("  Size: {:.2} MB", original_image.size_bytes as f64 / 1_000_000.0);
    println!("  Dimensions: {}x{}", original_image.dimensions.0, original_image.dimensions.1);
    println!();

    // Apply preprocessing options
    let options = PreprocessOptions {
        max_width: 2048,
        max_height: 2048,
        quality: 85,
        convert_to: Some(ImageFormat::Jpeg),
    };

    println!("Preprocessing options:");
    println!("  Max dimensions: {}x{}", options.max_width, options.max_height);
    println!("  JPEG quality: {}%", options.quality);
    println!("  Convert to: {:?}", options.convert_to.map(|f| f.mime_type()));
    println!();

    // Simulate preprocessing
    let processed = preprocess_image(&original_image, &options)?;

    println!("Processed image:");
    println!("  Format: {}", processed.format.mime_type());
    println!("  Size: {:.2} MB ({:.0}% reduction)",
        processed.size_bytes as f64 / 1_000_000.0,
        (1.0 - processed.size_bytes as f64 / original_image.size_bytes as f64) * 100.0
    );
    println!("  Dimensions: {}x{}", processed.dimensions.0, processed.dimensions.1);
    println!();

    println!("Preprocessing recommendations:");
    println!("  - Resize large images to max 2048x2048");
    println!("  - Convert PNG to JPEG for photos (smaller size)");
    println!("  - Use WebP for best compression (if supported)");
    println!("  - Strip EXIF data for privacy");
    println!("  - Crop to relevant regions when possible");
    println!();

    Ok(())
}

/// Demonstrates URL vs base64 encoding comparison
fn url_vs_base64_example() -> Result<()> {
    println!("=== URL vs Base64 Comparison ===\n");

    let test_image_size = 100_000; // 100KB image

    println!("Comparison for a {}KB image:", test_image_size / 1000);
    println!();

    // Base64 approach
    println!("Base64 Encoding:");
    let base64_overhead = test_image_size * 4 / 3; // ~33% overhead
    println!("  Original size: {} KB", test_image_size / 1000);
    println!("  Base64 size: {} KB", base64_overhead / 1000);
    println!("  Overhead: +{:.0}%", (base64_overhead - test_image_size) as f64 / test_image_size as f64 * 100.0);
    println!("  Transfer: Full payload in request");
    println!("  Latency: Single request");
    println!("  Use case: Small images, privacy-sensitive data");
    println!();

    // URL approach
    println!("URL Reference:");
    println!("  Reference size: ~100 bytes (URL string)");
    println!("  Image size: {} KB (fetched separately)", test_image_size / 1000);
    println!("  Overhead: Minimal in request");
    println!("  Transfer: Two requests (query + image fetch)");
    println!("  Latency: Depends on image server");
    println!("  Use case: Public images, already hosted content");
    println!();

    // Decision matrix
    println!("Decision Matrix:");
    println!("  ┌─────────────────┬─────────┬─────────┐");
    println!("  │ Criteria        │ Base64  │ URL     │");
    println!("  ├─────────────────┼─────────┼─────────┤");
    println!("  │ Small images    │ ✓       │ -       │");
    println!("  │ Large images    │ -       │ ✓       │");
    println!("  │ Privacy         │ ✓       │ -       │");
    println!("  │ Already hosted  │ -       │ ✓       │");
    println!("  │ Offline support │ ✓       │ -       │");
    println!("  │ Speed (local)   │ ✓       │ -       │");
    println!("  └─────────────────┴─────────┴─────────┘");
    println!();

    Ok(())
}

/// Demonstrates handling large images and batch processing
fn large_image_handling_example() -> Result<()> {
    println!("=== Large Image Handling ===\n");

    // Size limits
    let limits = ImageLimits {
        max_base64_size: 15_000_000, // 15MB limit
        max_decoded_size: 20_000_000, // ~20MB decoded
        recommended_max_dimension: 2048,
    };

    println!("Image Size Limits:");
    println!("  Max base64 size: {} MB", limits.max_base64_size / 1_000_000);
    println!("  Max decoded size: {} MB", limits.max_decoded_size / 1_000_000);
    println!("  Recommended max dimension: {}px", limits.recommended_max_dimension);
    println!();

    // Test cases
    let test_images = vec![
        ("Small image", 50_000, (640, 480)),
        ("Medium image", 500_000, (1920, 1080)),
        ("Large image", 5_000_000, (4000, 3000)),
        ("Very large", 20_000_000, (8000, 6000)),
        ("Too large", 25_000_000, (10000, 8000)),
    ];

    println!("Image validation:");
    for (name, size, dims) in &test_images {
        let result = validate_image(size, dims, &limits);
        let status = match result {
            Ok(()) => "✓ OK",
            Err(e) => e,
        };
        println!("  {} ({}x{}, {:.1}MB): {}",
            name, dims.0, dims.1, *size as f64 / 1_000_000.0, status);
    }
    println!();

    // Batch processing strategy
    println!("Batch Processing Strategy for Many Images:");
    println!("  1. Validate all images first");
    println!("  2. Group images by size category");
    println!("  3. Process small/medium images together");
    println!("  4. Handle large images individually");
    println!("  5. For very large images, consider:");
    println!("     - Tiling/splitting into regions");
    println!("     - Progressive loading");
    println!("     - Pre-analysis summary generation");
    println!();

    // Simulate batch processing
    println!("Example batch processing:");
    let batch_images: Vec<ImageData> = (1..=5).map(|i| ImageData {
        name: format!("image_{}.png", i),
        format: ImageFormat::Png,
        size_bytes: 100_000 * i,
        dimensions: ((800 * i) as u32, (600 * i) as u32),
        data: vec![0; 100_000 * i],
    }).collect();

    let start = Instant::now();

    println!("  Processing {} images...", batch_images.len());
    for (i, img) in batch_images.iter().enumerate() {
        println!("    [{}/{}] {} ({:.1}KB)",
            i + 1, batch_images.len(), img.name, img.size_bytes as f64 / 1024.0);
        // Simulate processing
        std::thread::sleep(std::time::Duration::from_millis(10));
    }

    println!("  Batch completed in {:?}", start.elapsed());
    println!();

    Ok(())
}

// ============== Helper Functions ==============

/// Simulates base64 encoding
fn simulate_base64_encode(data: &[u8]) -> String {
    // In real code: use base64 crate
    format!("base64({}_bytes)", data.len())
}

/// Simulates image preprocessing
fn preprocess_image(image: &ImageData, options: &PreprocessOptions) -> Result<ImageData> {
    // Calculate new dimensions maintaining aspect ratio
    let (orig_w, orig_h) = image.dimensions;
    let (max_w, max_h) = (options.max_width, options.max_height);

    let ratio = (orig_w as f64 / max_w as f64).max(orig_h as f64 / max_h as f64);
    let (new_w, new_h) = if ratio > 1.0 {
        ((orig_w as f64 / ratio) as u32, (orig_h as f64 / ratio) as u32)
    } else {
        (orig_w, orig_h)
    };

    // Estimate new size (rough approximation)
    let size_ratio = (new_w * new_h) as f64 / (orig_w * orig_h) as f64;
    let new_size = (image.size_bytes as f64 * size_ratio) as usize;

    // Apply format conversion impact
    let format = options.convert_to.unwrap_or(image.format);
    let format_factor = match format {
        ImageFormat::Jpeg => 0.3, // JPEG is smaller for photos
        ImageFormat::WebP => 0.25,
        ImageFormat::Png => 1.0,
        ImageFormat::Gif => 0.8,
    };

    Ok(ImageData {
        name: image.name.clone(),
        format,
        size_bytes: (new_size as f64 * format_factor) as usize,
        dimensions: (new_w, new_h),
        data: vec![0; (new_size as f64 * format_factor) as usize],
    })
}

/// Image size limits
struct ImageLimits {
    max_base64_size: usize,
    max_decoded_size: usize,
    recommended_max_dimension: u32,
}

/// Validates an image against limits
fn validate_image(size: &usize, dims: &(u32, u32), limits: &ImageLimits) -> Result<(), &'static str> {
    if *size > limits.max_base64_size {
        return Err("✗ Exceeds max size");
    }
    if dims.0 > limits.recommended_max_dimension * 2 || dims.1 > limits.recommended_max_dimension * 2 {
        return Err("⚠ Dimensions too large");
    }
    if dims.0 > limits.recommended_max_dimension || dims.1 > limits.recommended_max_dimension {
        return Err("⚠ Consider resizing");
    }
    Ok(())
}