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
//! Rust SDK wrapping Google's Gemini CLI as a subprocess via JSON-RPC 2.0.
//!
//! # Architecture
//!
//! The SDK communicates with the `gemini` binary using the `--experimental-acp`
//! JSON-RPC 2.0 mode. Each [`Client`] manages a single subprocess session.
//! Responses are translated from raw wire types to the public [`Message`] enum.
//!
//! # Quick Start — one-shot query
//!
//! ```rust,no_run
//! #[tokio::main]
//! async fn main() -> gemini_cli_sdk::Result<()> {
//! let messages = gemini_cli_sdk::query("Explain quantum computing in one sentence").await?;
//! for msg in messages {
//! if let Some(text) = msg.assistant_text() {
//! println!("{text}");
//! }
//! }
//! Ok(())
//! }
//! ```
//!
//! # Quick Start — stateful multi-turn session
//!
//! ```rust,no_run
//! use gemini_cli_sdk::{Client, ClientConfig};
//!
//! #[tokio::main]
//! async fn main() -> gemini_cli_sdk::Result<()> {
//! let config = ClientConfig::builder()
//! .prompt("Explain quantum computing")
//! .build();
//! let mut client = Client::new(config)?;
//! let _info = client.connect().await?;
//! // send() returns a Stream; consume it then close.
//! client.close().await?;
//! Ok(())
//! }
//! ```
// ── Module declarations ───────────────────────────────────────────────────────
//
// Modules referenced in existing doctests via `gemini_cli_sdk::<module>::` must
// be `pub` so rustdoc can compile those examples.
// ── Core re-exports ───────────────────────────────────────────────────────────
pub use Client;
pub use ;
// ── Config re-exports ─────────────────────────────────────────────────────────
pub use ;
// ── Callback re-exports ───────────────────────────────────────────────────────
pub use ;
// ── Discovery re-exports ──────────────────────────────────────────────────────
pub use ;
// ── Hooks re-exports ──────────────────────────────────────────────────────────
pub use ;
// ── MCP re-exports ────────────────────────────────────────────────────────────
pub use ;
// ── Permissions re-exports ────────────────────────────────────────────────────
pub use ;
// ── Transport re-exports ──────────────────────────────────────────────────────
pub use ;
// ── Message type re-exports ───────────────────────────────────────────────────
pub use ;
// ── Content type re-exports ───────────────────────────────────────────────────
pub use ;
// ── Free-function helpers ─────────────────────────────────────────────────────
/// Run a one-shot query with a plain-text prompt, collecting all messages.
///
/// Creates a temporary [`Client`] with default configuration, connects it,
/// sends the prompt, collects the full response into a [`Vec`], and closes
/// the session. Equivalent to calling [`query_with_content`] with a single
/// [`UserContent::text`] block.
///
/// # Errors
///
/// Propagates all errors from [`Client::new`], [`Client::connect`],
/// [`Client::send`], and [`Client::close`].
///
/// # Example
///
/// ```rust,no_run
/// #[tokio::main]
/// async fn main() -> gemini_cli_sdk::Result<()> {
/// let msgs = gemini_cli_sdk::query("What is 2+2?").await?;
/// for m in msgs {
/// if let Some(t) = m.assistant_text() {
/// println!("{t}");
/// }
/// }
/// Ok(())
/// }
/// ```
pub async
/// Run a one-shot query with structured content, collecting all messages.
///
/// Identical to [`query`] but accepts a [`Vec<UserContent>`] instead of a
/// plain string, allowing images and mixed content to be sent.
///
/// The `prompt` parameter is used only to construct the [`ClientConfig`];
/// the actual content sent to the session is taken from `content`.
///
/// # Errors
///
/// Propagates all errors from [`Client::new`], [`Client::connect`],
/// [`Client::send_content`], and [`Client::close`].
///
/// # Example
///
/// ```rust,no_run
/// use gemini_cli_sdk::{UserContent, query_with_content};
///
/// #[tokio::main]
/// async fn main() -> gemini_cli_sdk::Result<()> {
/// let content = vec![
/// UserContent::text("Describe this image:"),
/// UserContent::image_url("https://example.com/img.png"),
/// ];
/// let msgs = query_with_content("Describe this image:", content).await?;
/// println!("{} messages", msgs.len());
/// Ok(())
/// }
/// ```
pub async
/// Run a one-shot query, yielding messages via a static boxed stream.
///
/// Collects the full response into a [`Vec`] and returns it wrapped in a
/// `tokio_stream::Stream`. This avoids the lifetime problem of streaming
/// directly from an owned [`Client`] without boxing the client.
///
/// For direct access to the underlying `Vec`, prefer [`query`].
///
/// # Errors
///
/// Any connection or streaming error is emitted as the first `Err` item in
/// the returned stream.
///
/// # Example
///
/// ```rust,no_run
/// use tokio_stream::StreamExt as _;
///
/// #[tokio::main]
/// async fn main() -> gemini_cli_sdk::Result<()> {
/// let stream = gemini_cli_sdk::query_stream("List five prime numbers").await;
/// tokio::pin!(stream);
/// while let Some(item) = stream.next().await {
/// let msg = item?;
/// if let Some(text) = msg.assistant_text() {
/// print!("{text}");
/// }
/// }
/// Ok(())
/// }
/// ```
pub async
/// Run a one-shot query with structured content, yielding messages as a stream
/// as they arrive.
///
/// Each message is forwarded to the caller immediately via an internal channel,
/// providing true streaming backpressure rather than buffering the full response.
///
/// Equivalent to [`query_stream`] but accepts a [`Vec<UserContent>`] for
/// mixed text/image inputs.
///
/// # Errors
///
/// Any connection or streaming error is emitted as the first `Err` item in
/// the returned stream.
///
/// # Example
///
/// ```rust,no_run
/// use gemini_cli_sdk::{UserContent, query_stream_with_content};
/// use tokio_stream::StreamExt as _;
///
/// #[tokio::main]
/// async fn main() -> gemini_cli_sdk::Result<()> {
/// let content = vec![UserContent::text("Hello!")];
/// let stream = query_stream_with_content("Hello!", content).await;
/// tokio::pin!(stream);
/// while let Some(item) = stream.next().await {
/// println!("{:?}", item?);
/// }
/// Ok(())
/// }
/// ```
pub async
/// Extract the prompt string from a [`Client`] and return it.
///
/// Convenience wrapper for the free-function wrappers that need to inspect
/// the prompt on an already-constructed client.
///
/// # Example
///
/// ```rust
/// use gemini_cli_sdk::{Client, ClientConfig, client_prompt};
///
/// let config = ClientConfig::builder().prompt("hello").build();
/// // NOTE: Client::new requires a real CLI binary — this example shows
/// // the function signature only.
/// // let client = Client::new(config).unwrap();
/// // assert_eq!(client_prompt(&client), "hello");
/// ```