claude-agents-sdk 0.1.7

Rust SDK for building agents with Claude Code CLI
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
//! Comprehensive examples of using ClaudeClient for streaming mode (Rust port of streaming_mode.py).
//!
//! This file demonstrates various patterns for building applications with
//! the ClaudeClient streaming interface.
//!
//! Run with: cargo run --example streaming_mode [example_name]
//!
//! Available examples:
//! - all: Run all examples
//! - basic_streaming
//! - multi_turn
//! - with_interrupt
//! - with_options
//! - bash_command
//! - control_protocol
//! - error_handling

use claude_agents_sdk::{
    ClaudeAgentOptions, ClaudeClient, ContentBlock, Message, UserMessageContent,
};
use std::env;
use std::future::Future;
use std::pin::Pin;
use tokio_stream::StreamExt;

/// Type alias for async example functions.
type ExampleFn =
    fn() -> Pin<Box<dyn Future<Output = Result<(), Box<dyn std::error::Error>>> + Send>>;

/// Display a message in a standardized format.
fn display_message(msg: &Message) {
    match msg {
        Message::User(user) => match &user.content {
            UserMessageContent::Text(text) => {
                println!("User: {}", text);
            }
            UserMessageContent::Blocks(blocks) => {
                for block in blocks {
                    if let ContentBlock::Text(text) = block {
                        println!("User: {}", text.text);
                    }
                }
            }
        },
        Message::Assistant(asst) => {
            for block in &asst.content {
                if let ContentBlock::Text(text) = block {
                    println!("Claude: {}", text.text);
                }
            }
        }
        Message::System(_) => {
            // Ignore system messages
        }
        Message::Result(_) => {
            println!("Result ended");
        }
        Message::StreamEvent(_) => {
            // Streaming events handled separately
        }
    }
}

/// Basic streaming with ClaudeClient.
async fn example_basic_streaming() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Basic Streaming Example ===");

    let mut client = ClaudeClient::new(None);
    client.connect().await?;

    println!("User: What is 2+2?");
    client.query("What is 2+2?").await?;

    let (response, _) = client.receive_response().await?;
    println!("Claude: {}", response);

    client.disconnect().await?;
    println!("\n");

    Ok(())
}

/// Multi-turn conversation using receive_response helper.
async fn example_multi_turn_conversation() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Multi-Turn Conversation Example ===");

    let mut client = ClaudeClient::new(None);
    client.connect().await?;

    // First turn
    println!("User: What's the capital of France?");
    client.query("What's the capital of France?").await?;

    let (response, _) = client.receive_response().await?;
    println!("Claude: {}", response);

    // Second turn - follow-up
    println!("\nUser: What's the population of that city?");
    client.query("What's the population of that city?").await?;

    let (response, _) = client.receive_response().await?;
    println!("Claude: {}", response);

    client.disconnect().await?;
    println!("\n");

    Ok(())
}

/// Demonstrate interrupt capability.
async fn example_with_interrupt() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Interrupt Example ===");
    println!("IMPORTANT: Interrupts require active message consumption.");

    let mut client = ClaudeClient::new(None);
    client.connect().await?;

    // Start a long-running task
    println!("\nUser: Count from 1 to 100 slowly");
    client
        .query("Count from 1 to 100 slowly, with a brief pause between each number")
        .await?;

    // Spawn a task to consume messages for a short time
    let start = std::time::Instant::now();

    let mut message_stream = client.receive_messages();
    while start.elapsed() < std::time::Duration::from_secs(2) {
        tokio::select! {
            msg = message_stream.next() => {
                if let Some(Ok(msg)) = msg {
                    display_message(&msg);
                    if matches!(msg, Message::Result(_)) {
                        break;
                    }
                }
            }
            _ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {}
        }
    }
    drop(message_stream);

    println!("\n[After 2 seconds, sending interrupt...]");
    client.interrupt().await?;

    // Drain remaining messages
    while let Some(Ok(msg)) = client.receive_messages().next().await {
        if matches!(msg, Message::Result(_)) {
            break;
        }
    }

    // Send new instruction after interrupt
    println!("\nUser: Never mind, just tell me a quick joke");
    client
        .query("Never mind, just tell me a quick joke")
        .await?;

    let (response, _) = client.receive_response().await?;
    println!("Claude: {}", response);

    client.disconnect().await?;
    println!("\n");

    Ok(())
}

/// Use ClaudeAgentOptions to configure the client.
async fn example_with_options() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Custom Options Example ===");

    let options = ClaudeAgentOptions::new()
        .with_allowed_tools(vec!["Read".to_string(), "Write".to_string()])
        .with_system_prompt("You are a helpful coding assistant.");

    let mut client = ClaudeClient::new(Some(options));
    client.connect().await?;

    println!("User: Create a simple hello.txt file with a greeting message");
    client
        .query("Create a simple hello.txt file with a greeting message")
        .await?;

    let mut tool_uses = Vec::new();

    while let Some(msg) = client.receive_messages().next().await {
        let msg = msg?;
        match &msg {
            Message::Assistant(asst) => {
                display_message(&msg);
                for block in &asst.content {
                    if let ContentBlock::ToolUse(tool) = block {
                        tool_uses.push(tool.name.clone());
                    }
                }
            }
            Message::Result(_) => {
                display_message(&msg);
                break;
            }
            _ => display_message(&msg),
        }
    }

    if !tool_uses.is_empty() {
        println!("Tools used: {}", tool_uses.join(", "));
    }

    client.disconnect().await?;
    println!("\n");

    Ok(())
}

/// Example showing tool use blocks when running bash commands.
async fn example_bash_command() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Bash Command Example ===");

    let options = ClaudeAgentOptions::new().with_allowed_tools(vec!["Bash".to_string()]);

    let mut client = ClaudeClient::new(Some(options));
    client.connect().await?;

    println!("User: Run a bash echo command");
    client
        .query("Run a bash echo command that says 'Hello from bash!'")
        .await?;

    let mut message_types = Vec::new();

    while let Some(msg) = client.receive_messages().next().await {
        let msg = msg?;
        message_types.push(format!("{:?}", std::mem::discriminant(&msg)));

        match &msg {
            Message::User(user) => match &user.content {
                UserMessageContent::Text(text) => {
                    println!("User: {}", text);
                }
                UserMessageContent::Blocks(blocks) => {
                    for block in blocks {
                        match block {
                            ContentBlock::Text(text) => {
                                println!("User: {}", text.text);
                            }
                            ContentBlock::ToolResult(result) => {
                                let content_preview = result
                                    .content
                                    .as_ref()
                                    .map(|c| {
                                        let s = c.to_string();
                                        if s.len() > 100 {
                                            format!("{}...", &s[..100])
                                        } else {
                                            s
                                        }
                                    })
                                    .unwrap_or_else(|| "None".to_string());
                                println!(
                                    "Tool Result (id: {}): {}",
                                    result.tool_use_id, content_preview
                                );
                            }
                            _ => {}
                        }
                    }
                }
            },
            Message::Assistant(asst) => {
                for block in &asst.content {
                    match block {
                        ContentBlock::Text(text) => {
                            println!("Claude: {}", text.text);
                        }
                        ContentBlock::ToolUse(tool) => {
                            println!("Tool Use: {} (id: {})", tool.name, tool.id);
                            if tool.name == "Bash" {
                                if let Some(cmd) = tool.input.get("command") {
                                    println!("  Command: {}", cmd);
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
            Message::Result(result) => {
                println!("Result ended");
                if let Some(cost) = result.total_cost_usd {
                    println!("Cost: ${:.4}", cost);
                }
                break;
            }
            _ => {}
        }
    }

    println!("\nMessage types received: {:?}", message_types.len());

    client.disconnect().await?;
    println!("\n");

    Ok(())
}

/// Demonstrate server info and interrupt capabilities.
async fn example_control_protocol() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Control Protocol Example ===");
    println!("Shows server info retrieval and interrupt capability\n");

    let mut client = ClaudeClient::new(None);
    client.connect().await?;

    // 1. Get server initialization info
    println!("1. Getting server info...");
    if let Some(server_info) = client.get_server_info().await {
        println!("✓ Server info retrieved successfully!");
        if let Some(commands) = server_info.get("commands").and_then(|v| v.as_array()) {
            println!("  - Available commands: {}", commands.len());
        }
        if let Some(style) = server_info.get("output_style") {
            println!("  - Output style: {}", style);
        }
    } else {
        println!("✗ No server info available (may not be in streaming mode)");
    }

    println!("\n2. Testing interrupt capability...");

    // Start a long-running task
    println!("User: Count from 1 to 20 slowly");
    client
        .query("Count from 1 to 20 slowly, pausing between each number")
        .await?;

    // Start consuming messages for a short time
    let start = std::time::Instant::now();
    let mut message_stream = client.receive_messages();
    while start.elapsed() < std::time::Duration::from_secs(2) {
        tokio::select! {
            msg = message_stream.next() => {
                if let Some(Ok(msg)) = msg {
                    if let Message::Assistant(asst) = &msg {
                        for block in &asst.content {
                            if let ContentBlock::Text(text) = block {
                                let preview = if text.text.len() > 50 {
                                    format!("{}...", &text.text[..50])
                                } else {
                                    text.text.clone()
                                };
                                println!("Claude: {}", preview);
                                break;
                            }
                        }
                    }
                    if matches!(msg, Message::Result(_)) {
                        break;
                    }
                }
            }
            _ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {}
        }
    }
    drop(message_stream);

    println!("\n[Sending interrupt after 2 seconds...]");

    match client.interrupt().await {
        Ok(()) => println!("✓ Interrupt sent successfully"),
        Err(e) => println!("✗ Interrupt failed: {}", e),
    }

    // Drain remaining messages
    while let Some(Ok(msg)) = client.receive_messages().next().await {
        if matches!(msg, Message::Result(_)) {
            break;
        }
    }

    // Send new query after interrupt
    println!("\nUser: Just say 'Hello!'");
    client.query("Just say 'Hello!'").await?;

    let (response, _) = client.receive_response().await?;
    println!("Claude: {}", response);

    client.disconnect().await?;
    println!("\n");

    Ok(())
}

/// Demonstrate proper error handling.
async fn example_error_handling() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Error Handling Example ===");

    let mut client = ClaudeClient::new(None);

    match client.connect().await {
        Ok(()) => {
            // Send a message that will take time to process
            println!("User: Run a bash sleep command for 60 seconds");
            client
                .query("Run a bash sleep command for 60 seconds not in the background")
                .await?;

            // Try to receive response with a short timeout
            let mut messages_count = 0;
            let timeout = tokio::time::timeout(std::time::Duration::from_secs(10), async {
                while let Some(msg) = client.receive_messages().next().await {
                    let msg = msg?;
                    messages_count += 1;

                    match &msg {
                        Message::Assistant(asst) => {
                            for block in &asst.content {
                                if let ContentBlock::Text(text) = block {
                                    let preview = if text.text.len() > 50 {
                                        format!("{}...", &text.text[..50])
                                    } else {
                                        text.text.clone()
                                    };
                                    println!("Claude: {}", preview);
                                }
                            }
                        }
                        Message::Result(_) => {
                            display_message(&msg);
                            return Ok::<_, claude_agents_sdk::ClaudeSDKError>(());
                        }
                        _ => {}
                    }
                }
                Ok(())
            })
            .await;

            match timeout {
                Ok(Ok(())) => println!("Response completed successfully"),
                Ok(Err(e)) => println!("Error during response: {}", e),
                Err(_) => {
                    println!(
                        "\nResponse timeout after 10 seconds - demonstrating graceful handling"
                    );
                    println!("Received {} messages before timeout", messages_count);
                }
            }

            // Always disconnect
            client.disconnect().await?;
        }
        Err(e) => {
            println!("Connection error: {}", e);
        }
    }

    println!("\n");

    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args: Vec<String> = env::args().collect();

    let examples: Vec<(&str, ExampleFn)> = vec![
        ("basic_streaming", || Box::pin(example_basic_streaming())),
        ("multi_turn", || Box::pin(example_multi_turn_conversation())),
        ("with_interrupt", || Box::pin(example_with_interrupt())),
        ("with_options", || Box::pin(example_with_options())),
        ("bash_command", || Box::pin(example_bash_command())),
        ("control_protocol", || Box::pin(example_control_protocol())),
        ("error_handling", || Box::pin(example_error_handling())),
    ];

    if args.len() < 2 {
        println!("Usage: cargo run --example streaming_mode <example_name>");
        println!("\nAvailable examples:");
        println!("  all - Run all examples");
        for (name, _) in &examples {
            println!("  {}", name);
        }
        return Ok(());
    }

    let example_name = &args[1];

    if example_name == "all" {
        for (name, func) in &examples {
            println!("Running: {}", name);
            func().await?;
            println!("{}", "-".repeat(50));
        }
    } else {
        let found = examples.iter().find(|(name, _)| name == example_name);
        match found {
            Some((_, func)) => {
                func().await?;
            }
            None => {
                println!("Error: Unknown example '{}'", example_name);
                println!("\nAvailable examples:");
                println!("  all - Run all examples");
                for (name, _) in &examples {
                    println!("  {}", name);
                }
                std::process::exit(1);
            }
        }
    }

    Ok(())
}