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
//! Simple query function for one-shot interactions
use Stream;
use crateResult;
use crateparse_message;
use crate;
use crate;
use crateTransport;
/// One-shot query function for simple interactions with Claude Code
///
/// This function is ideal for simple, stateless queries where you don't need
/// bidirectional communication or conversation management. For interactive,
/// stateful conversations, use ClaudeSDKClient instead.
///
/// # Key differences from ClaudeSDKClient
/// - **Unidirectional**: Send all messages upfront, receive all responses
/// - **Stateless**: Each query is independent, no conversation state
/// - **Simple**: Fire-and-forget style, no connection management
/// - **No interrupts**: Cannot interrupt or send follow-up messages
///
/// # When to use query()
/// - Simple one-off questions ("What is 2+2?")
/// - Batch processing of independent prompts
/// - Code generation or analysis tasks
/// - Automated scripts and CI/CD pipelines
/// - When you know all inputs upfront
///
/// # When to use ClaudeSDKClient
/// - Interactive conversations with follow-ups
/// - Chat applications or REPL-like interfaces
/// - When you need to send messages based on responses
/// - When you need interrupt capabilities
/// - Long-running sessions with state
///
/// # Arguments
/// * `prompt` - The prompt to send to Claude (string)
/// * `options` - Optional configuration (defaults to ClaudeAgentOptions::default() if None)
///
/// # Returns
/// A stream of Messages from the conversation
///
/// # Errors
/// Returns error if:
/// - Claude Code CLI is not found
/// - Connection fails
/// - Process fails
/// - JSON parsing fails
///
/// # Examples
///
/// Simple query:
/// ```no_run
/// use claude_agent_sdk::query;
/// use futures::StreamExt;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let stream = query("What is the capital of France?", None).await?;
/// let mut stream = Box::pin(stream);
///
/// while let Some(message) = stream.next().await {
/// println!("{:?}", message?);
/// }
/// Ok(())
/// }
/// ```
///
/// With options:
/// ```no_run
/// use claude_agent_sdk::{query, ClaudeAgentOptions};
/// use futures::StreamExt;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let options = ClaudeAgentOptions::builder()
/// .system_prompt("You are a helpful coding assistant")
/// .max_turns(1)
/// .build();
///
/// let stream = query("Write a hello world in Python", Some(options)).await?;
/// let mut stream = Box::pin(stream);
///
/// while let Some(message) = stream.next().await {
/// println!("{:?}", message?);
/// }
/// Ok(())
/// }
/// ```
pub async