claude-agents-sdk 0.1.2

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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! Bidirectional Claude client for streaming interactions.
//!
//! This module provides [`ClaudeClient`], a full-featured client for
//! bidirectional communication with Claude. It supports:
//!
//! - Multiple queries in a single session
//! - Tool permission callbacks
//! - Hook callbacks
//! - Runtime model and permission changes
//! - File checkpointing and rewinding

use std::pin::Pin;
use tokio::sync::mpsc;
use tokio_stream::{Stream, StreamExt};

use crate::_internal::client::InternalClient;
use crate::_internal::transport::Transport;
use crate::errors::{ClaudeSDKError, Result};
use crate::types::*;

/// Bidirectional client for streaming Claude interactions.
///
/// `ClaudeClient` provides a full-featured interface for interactive
/// conversations with Claude. Unlike the simple [`query`](crate::query)
/// function, this client maintains a persistent connection and supports
/// multiple queries, callbacks, and runtime configuration changes.
///
/// # Examples
///
/// ## Basic Usage
///
/// ```rust,no_run
/// use claude_agents_sdk::ClaudeClient;
/// use tokio_stream::StreamExt;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut client = ClaudeClient::new(None, None);
///     client.connect().await?;
///
///     // Send first query
///     client.query("What is Rust?").await?;
///
///     // Process responses
///     while let Some(msg) = client.receive_messages().next().await {
///         println!("{:?}", msg?);
///     }
///
///     client.disconnect().await?;
///     Ok(())
/// }
/// ```
///
/// ## With Tool Permission Callback
///
/// ```rust,no_run
/// use claude_agents_sdk::{ClaudeClient, ClaudeAgentOptions, PermissionResult};
/// use std::sync::Arc;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let options = ClaudeAgentOptions::new()
///         .with_can_use_tool(|tool_name, input, _ctx| async move {
///             println!("Tool request: {} with {:?}", tool_name, input);
///             PermissionResult::allow()
///         });
///
///     let mut client = ClaudeClient::new(Some(options), None);
///     client.connect().await?;
///
///     // Queries will now invoke the callback for tool permissions
///
///     Ok(())
/// }
/// ```
pub struct ClaudeClient {
    /// Internal client implementation.
    internal: InternalClient,
    /// Message receiver from the internal client.
    message_rx: Option<mpsc::Receiver<Result<Message>>>,
    /// Custom transport if provided.
    _transport: Option<Box<dyn Transport>>,
}

impl ClaudeClient {
    /// Create a new Claude client.
    ///
    /// # Arguments
    ///
    /// * `options` - Optional configuration for the client
    /// * `transport` - Optional custom transport (uses subprocess by default)
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::{ClaudeClient, ClaudeAgentOptions, PermissionMode};
    ///
    /// // Default configuration
    /// let client = ClaudeClient::new(None, None);
    ///
    /// // With custom options
    /// let options = ClaudeAgentOptions::new()
    ///     .with_model("claude-3-opus")
    ///     .with_permission_mode(PermissionMode::AcceptEdits);
    /// let client = ClaudeClient::new(Some(options), None);
    /// ```
    pub fn new(options: Option<ClaudeAgentOptions>, transport: Option<Box<dyn Transport>>) -> Self {
        Self {
            internal: InternalClient::new(options.unwrap_or_default()),
            message_rx: None,
            _transport: transport,
        }
    }

    /// Connect to the Claude CLI.
    ///
    /// This establishes a connection to the CLI process and initializes
    /// the streaming session. Must be called before sending queries.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The CLI is not found
    /// - The CLI version is incompatible
    /// - Connection fails
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::ClaudeClient;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut client = ClaudeClient::new(None, None);
    ///     client.connect().await?;
    ///     // Client is now ready for queries
    ///     Ok(())
    /// }
    /// ```
    pub async fn connect(&mut self) -> Result<()> {
        self.internal.connect().await?;
        self.message_rx = self.internal.take_message_rx();
        Ok(())
    }

    /// Send a query to Claude.
    ///
    /// Sends a new prompt to Claude. Responses can be received using
    /// [`receive_messages`](Self::receive_messages) or
    /// [`receive_response`](Self::receive_response).
    ///
    /// # Arguments
    ///
    /// * `prompt` - The prompt to send
    ///
    /// # Errors
    ///
    /// Returns an error if the client is not connected.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::ClaudeClient;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut client = ClaudeClient::new(None, None);
    ///     client.connect().await?;
    ///
    ///     client.query("Hello!").await?;
    ///     client.query("Follow-up question").await?;
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn query(&mut self, prompt: &str) -> Result<()> {
        self.internal.send_message(prompt).await
    }

    /// Get a stream of messages from the current query.
    ///
    /// Returns a stream that yields messages as they are received from
    /// Claude. The stream ends when a result message is received.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::{ClaudeClient, Message};
    /// use tokio_stream::StreamExt;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut client = ClaudeClient::new(None, None);
    ///     client.connect().await?;
    ///     client.query("Tell me a joke").await?;
    ///
    ///     while let Some(msg) = client.receive_messages().next().await {
    ///         match msg? {
    ///             Message::Assistant(asst) => println!("{}", asst.text()),
    ///             Message::Result(_) => break,
    ///             _ => {}
    ///         }
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub fn receive_messages(&mut self) -> impl Stream<Item = Result<Message>> + '_ {
        futures::stream::poll_fn(move |cx| {
            if let Some(ref mut rx) = self.message_rx {
                Pin::new(rx).poll_recv(cx)
            } else {
                std::task::Poll::Ready(None)
            }
        })
    }

    /// Receive the complete response for the current query.
    ///
    /// Collects all messages until a result message is received and returns
    /// the combined response text along with the result metadata.
    ///
    /// # Returns
    ///
    /// A tuple of (response_text, result_message).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::ClaudeClient;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut client = ClaudeClient::new(None, None);
    ///     client.connect().await?;
    ///     client.query("What is 2 + 2?").await?;
    ///
    ///     let (response, result) = client.receive_response().await?;
    ///     println!("Response: {}", response);
    ///     println!("Turns: {}", result.num_turns);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn receive_response(&mut self) -> Result<(String, ResultMessage)> {
        let mut response_parts: Vec<String> = Vec::new();

        while let Some(msg) = self.receive_messages().next().await {
            match msg? {
                Message::Assistant(asst) => {
                    let text = asst.text();
                    if !text.is_empty() {
                        response_parts.push(text);
                    }
                }
                Message::Result(result) => {
                    return Ok((response_parts.concat(), result));
                }
                _ => {}
            }
        }

        Err(ClaudeSDKError::internal("Connection closed without result"))
    }

    /// Interrupt the current operation.
    ///
    /// Sends an interrupt signal to Claude, stopping the current response.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::ClaudeClient;
    /// use tokio::time::{timeout, Duration};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut client = ClaudeClient::new(None, None);
    ///     client.connect().await?;
    ///     client.query("Write a very long story").await?;
    ///
    ///     // Interrupt after 5 seconds
    ///     tokio::time::sleep(Duration::from_secs(5)).await;
    ///     client.interrupt().await?;
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn interrupt(&self) -> Result<()> {
        self.internal.interrupt().await
    }

    /// Change the permission mode for the session.
    ///
    /// # Arguments
    ///
    /// * `mode` - The new permission mode
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::{ClaudeClient, PermissionMode};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut client = ClaudeClient::new(None, None);
    ///     client.connect().await?;
    ///
    ///     // Switch to accept edits mode
    ///     client.set_permission_mode(PermissionMode::AcceptEdits).await?;
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn set_permission_mode(&self, mode: PermissionMode) -> Result<()> {
        self.internal.set_permission_mode(mode).await
    }

    /// Change the model for the session.
    ///
    /// # Arguments
    ///
    /// * `model` - The new model identifier
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::ClaudeClient;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut client = ClaudeClient::new(None, None);
    ///     client.connect().await?;
    ///
    ///     // Switch to a different model
    ///     client.set_model("claude-3-opus").await?;
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn set_model(&self, model: impl Into<String>) -> Result<()> {
        self.internal.set_model(model).await
    }

    /// Rewind files to a specific user message.
    ///
    /// This is only available when file checkpointing is enabled.
    ///
    /// # Arguments
    ///
    /// * `user_message_id` - The UUID of the user message to rewind to
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::{ClaudeClient, ClaudeAgentOptions};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut options = ClaudeAgentOptions::new();
    ///     options.enable_file_checkpointing = true;
    ///
    ///     let mut client = ClaudeClient::new(Some(options), None);
    ///     client.connect().await?;
    ///
    ///     // Later, rewind to a previous state
    ///     client.rewind_files("user-message-uuid").await?;
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn rewind_files(&self, user_message_id: impl Into<String>) -> Result<()> {
        self.internal.rewind_files(user_message_id).await
    }

    /// Get server initialization info.
    ///
    /// Returns the initialization response from the CLI, which includes
    /// available commands, output styles, and server capabilities.
    ///
    /// # Returns
    ///
    /// `Some(Value)` with server info if connected and initialized, `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::ClaudeClient;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut client = ClaudeClient::new(None, None);
    ///     client.connect().await?;
    ///
    ///     if let Some(info) = client.get_server_info().await {
    ///         println!("Commands: {:?}", info.get("commands"));
    ///         println!("Output style: {:?}", info.get("output_style"));
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_server_info(&self) -> Option<serde_json::Value> {
        self.internal.get_server_info().await
    }

    /// Disconnect from the Claude CLI.
    ///
    /// Gracefully closes the connection to the CLI process.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use claude_agents_sdk::ClaudeClient;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut client = ClaudeClient::new(None, None);
    ///     client.connect().await?;
    ///
    ///     // Use the client...
    ///
    ///     client.disconnect().await?;
    ///     Ok(())
    /// }
    /// ```
    pub async fn disconnect(&mut self) -> Result<()> {
        self.message_rx = None;
        self.internal.disconnect().await
    }

    /// Check if the client is connected.
    pub fn is_connected(&self) -> bool {
        self.internal.is_connected()
    }
}

/// Builder for creating a [`ClaudeClient`] with configuration.
///
/// Provides a fluent API for configuring the client before connecting.
///
/// # Examples
///
/// ```rust,no_run
/// use claude_agents_sdk::{ClaudeClientBuilder, PermissionMode, PermissionResult};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut client = ClaudeClientBuilder::new()
///         .model("claude-3-sonnet")
///         .permission_mode(PermissionMode::AcceptEdits)
///         .max_turns(10)
///         .can_use_tool(|tool, input, _| async move {
///             println!("Tool: {} Input: {:?}", tool, input);
///             PermissionResult::allow()
///         })
///         .build();
///
///     client.connect().await?;
///     Ok(())
/// }
/// ```
pub struct ClaudeClientBuilder {
    options: ClaudeAgentOptions,
}

impl ClaudeClientBuilder {
    /// Create a new builder with default options.
    pub fn new() -> Self {
        Self {
            options: ClaudeAgentOptions::new(),
        }
    }

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

    /// Set the system prompt.
    pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.options.system_prompt = Some(SystemPromptConfig::Text(prompt.into()));
        self
    }

    /// Set the permission mode.
    pub fn permission_mode(mut self, mode: PermissionMode) -> Self {
        self.options.permission_mode = Some(mode);
        self
    }

    /// Set the maximum number of turns.
    pub fn max_turns(mut self, turns: u32) -> Self {
        self.options.max_turns = Some(turns);
        self
    }

    /// Set the maximum budget in USD.
    pub fn max_budget_usd(mut self, budget: f64) -> Self {
        self.options.max_budget_usd = Some(budget);
        self
    }

    /// Set the working directory.
    pub fn cwd(mut self, path: impl Into<std::path::PathBuf>) -> Self {
        self.options.cwd = Some(path.into());
        self
    }

    /// Set the tool permission callback.
    pub fn can_use_tool<F, Fut>(mut self, callback: F) -> Self
    where
        F: Fn(String, serde_json::Value, ToolPermissionContext) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = PermissionResult> + Send + 'static,
    {
        self.options = self.options.with_can_use_tool(callback);
        self
    }

    /// Enable partial message streaming.
    pub fn include_partial_messages(mut self) -> Self {
        self.options.include_partial_messages = true;
        self
    }

    /// Enable file checkpointing.
    pub fn enable_file_checkpointing(mut self) -> Self {
        self.options.enable_file_checkpointing = true;
        self
    }

    /// Set allowed tools.
    pub fn allowed_tools(mut self, tools: Vec<String>) -> Self {
        self.options.allowed_tools = tools;
        self
    }

    /// Set disallowed tools.
    pub fn disallowed_tools(mut self, tools: Vec<String>) -> Self {
        self.options.disallowed_tools = tools;
        self
    }

    /// Build the client.
    pub fn build(self) -> ClaudeClient {
        ClaudeClient::new(Some(self.options), None)
    }
}

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

/// A guard that automatically disconnects a [`ClaudeClient`] when dropped.
///
/// This provides RAII-style resource management for the client connection,
/// similar to Python's async context manager (`async with`).
///
/// # Examples
///
/// ```rust,no_run
/// use claude_agents_sdk::{ClaudeClient, ClientGuard};
/// use tokio_stream::StreamExt;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut client = ClaudeClient::new(None, None);
///     client.connect().await?;
///
///     // Create guard - client will be disconnected when guard is dropped
///     let mut guard = ClientGuard::new(client);
///
///     guard.client_mut().query("Hello!").await?;
///     let (response, _) = guard.client_mut().receive_response().await?;
///     println!("{}", response);
///
///     // Client automatically disconnected when guard goes out of scope
///     Ok(())
/// }
/// ```
///
/// Or using the convenience method:
///
/// ```rust,no_run
/// use claude_agents_sdk::ClaudeClient;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut client = ClaudeClient::new(None, None);
///     client.connect().await?;
///
///     let guard = client.into_guard();
///     // Use guard.client() for all operations
///     // Automatically disconnects on drop
///
///     Ok(())
/// }
/// ```
pub struct ClientGuard {
    client: Option<ClaudeClient>,
    runtime: tokio::runtime::Handle,
}

impl ClientGuard {
    /// Create a new guard for the client.
    pub fn new(client: ClaudeClient) -> Self {
        Self {
            client: Some(client),
            runtime: tokio::runtime::Handle::current(),
        }
    }

    /// Get a reference to the client.
    pub fn client(&self) -> &ClaudeClient {
        self.client.as_ref().expect("Client already taken")
    }

    /// Get a mutable reference to the client.
    pub fn client_mut(&mut self) -> &mut ClaudeClient {
        self.client.as_mut().expect("Client already taken")
    }

    /// Take ownership of the client, preventing automatic disconnect.
    pub fn into_inner(mut self) -> ClaudeClient {
        self.client.take().expect("Client already taken")
    }
}

impl Drop for ClientGuard {
    fn drop(&mut self) {
        if let Some(mut client) = self.client.take() {
            // Spawn a task to disconnect - we can't do async in drop
            self.runtime.spawn(async move {
                let _ = client.disconnect().await;
            });
        }
    }
}

impl ClaudeClient {
    /// Convert this client into a guard that automatically disconnects on drop.
    ///
    /// This is the Rust equivalent of Python's `async with ClaudeSDKClient() as client:`
    /// pattern.
    pub fn into_guard(self) -> ClientGuard {
        ClientGuard::new(self)
    }
}

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

    #[test]
    fn test_client_builder() {
        let client = ClaudeClientBuilder::new()
            .model("claude-3-sonnet")
            .max_turns(5)
            .build();

        assert!(!client.is_connected());
    }
}