claude_wrapper/lib.rs
1//! A type-safe Claude Code CLI wrapper for Rust.
2//!
3//! `claude-wrapper` provides a builder-pattern interface for invoking the
4//! `claude` CLI programmatically. Each subcommand is a typed builder that
5//! produces typed output. The design follows the same shape as
6//! [`docker-wrapper`](https://crates.io/crates/docker-wrapper) and
7//! [`terraform-wrapper`](https://crates.io/crates/terraform-wrapper).
8//!
9//! # Feature flags
10//!
11//! | Feature | Default | Purpose |
12//! |---|---|---|
13//! | `async` | yes | tokio-backed async API. Disabling drops tokio from the runtime dep tree. |
14//! | `json` | yes | JSON output parsing and the JSON-backed surface ([`QueryCommand::execute_json`], [`streaming`], [`session::Session`], [`duplex`], [`conversation`], and the [`history`] / [`jobs`] / [`settings`] introspection modules). |
15//! | `tempfile` | yes | [`TempMcpConfig`] for one-shot MCP config files. |
16//! | `sync` | no | Blocking API: `*_sync` methods on [`exec`], [`retry`], every command builder, and [`Claude`]. |
17//!
18//! Sync-only (tokio-free) build:
19//!
20//! ```toml
21//! claude-wrapper = { version = "0.12", default-features = false, features = ["json", "sync"] }
22//! ```
23//!
24//! # Quick start (async)
25//!
26//! ```no_run
27//! # #[cfg(feature = "async")] {
28//! use claude_wrapper::{Claude, ClaudeCommand, QueryCommand};
29//!
30//! # async fn example() -> claude_wrapper::Result<()> {
31//! let claude = Claude::builder().build()?;
32//! let output = QueryCommand::new("explain this error: file not found")
33//! .model("sonnet")
34//! .execute(&claude)
35//! .await?;
36//! println!("{}", output.stdout);
37//! # Ok(()) }
38//! # }
39//! ```
40//!
41//! # Quick start (sync)
42//!
43//! Enable the `sync` feature and bring [`ClaudeCommandSyncExt`] into scope:
44//!
45//! ```no_run
46//! # #[cfg(feature = "sync")] {
47//! use claude_wrapper::{Claude, ClaudeCommandSyncExt, QueryCommand};
48//!
49//! # fn example() -> claude_wrapper::Result<()> {
50//! let claude = Claude::builder().build()?;
51//! let output = QueryCommand::new("explain this error")
52//! .execute_sync(&claude)?;
53//! println!("{}", output.stdout);
54//! # Ok(()) }
55//! # }
56//! ```
57//!
58//! # Two-layer builder
59//!
60//! The [`Claude`] client holds shared config (binary path, env, timeout,
61//! default retry policy). Command builders hold per-invocation options
62//! and call `execute(&claude)` (or `execute_sync`).
63//!
64//! ```no_run
65//! # #[cfg(feature = "async")] {
66//! use claude_wrapper::{Claude, ClaudeCommand, Effort, PermissionMode, QueryCommand};
67//!
68//! # async fn example() -> claude_wrapper::Result<()> {
69//! let claude = Claude::builder()
70//! .env("AWS_REGION", "us-west-2")
71//! .timeout_secs(300)
72//! .build()?;
73//!
74//! let output = QueryCommand::new("review src/main.rs")
75//! .model("opus")
76//! .system_prompt("You are a senior Rust developer")
77//! .permission_mode(PermissionMode::Plan)
78//! .effort(Effort::High)
79//! .max_turns(5)
80//! .no_session_persistence()
81//! .execute(&claude)
82//! .await?;
83//! # Ok(()) }
84//! # }
85//! ```
86//!
87//! # JSON output
88//!
89//! ```no_run
90//! # #[cfg(all(feature = "async", feature = "json"))] {
91//! use claude_wrapper::{Claude, QueryCommand};
92//!
93//! # async fn example() -> claude_wrapper::Result<()> {
94//! let claude = Claude::builder().build()?;
95//! let result = QueryCommand::new("what is 2+2?")
96//! .execute_json(&claude)
97//! .await?;
98//! println!("answer: {}", result.result);
99//! println!("cost: ${:.4}", result.cost_usd.unwrap_or(0.0));
100//! # Ok(()) }
101//! # }
102//! ```
103//!
104//! # Multi-turn conversations
105//!
106//! Two shapes for multi-turn work, each suited to a different
107//! process model. [`DuplexSession`] is the recommended choice for
108//! long-running hosts; [`Session`] is the right fit for short-lived
109//! processes.
110//!
111//! | | [`DuplexSession`] | [`Session`] |
112//! |---|---|---|
113//! | Process model | one child held open across turns | new subprocess per turn, `--resume` continuity |
114//! | Mid-turn interrupt | yes ([`DuplexSession::interrupt`](duplex::DuplexSession::interrupt)) | no (only `child.kill()` via SIGKILL) |
115//! | Mid-turn permission prompts | yes ([`PermissionHandler`]) | no |
116//! | Broadcast event subscribers | yes ([`DuplexSession::subscribe`](duplex::DuplexSession::subscribe)) | no (per-turn `stream_query`) |
117//! | Built-in cost / history tracking | no ([`TurnResult`] is per-turn) | yes ([`Session::total_cost_usd`], [`Session::history`], [`BudgetTracker`]) |
118//! | Right for | long-running hosts (IDE backends, daemons, agent servers, chat UIs) | short-lived processes (CLIs, build scripts, batch jobs, lambdas) |
119//!
120//! ## `DuplexSession` (recommended for long-running hosts)
121//!
122//! ```no_run
123//! # #[cfg(all(feature = "async", feature = "json"))] {
124//! use claude_wrapper::Claude;
125//! use claude_wrapper::duplex::{DuplexOptions, DuplexSession};
126//!
127//! # async fn example() -> claude_wrapper::Result<()> {
128//! let claude = Claude::builder().build()?;
129//! let session = DuplexSession::spawn(
130//! &claude,
131//! DuplexOptions::default().model("haiku"),
132//! ).await?;
133//!
134//! let turn = session.send("what's 2 + 2?").await?;
135//! println!("answer: {}", turn.result_text().unwrap_or(""));
136//!
137//! session.close().await?;
138//! # Ok(()) }
139//! # }
140//! ```
141//!
142//! See the [duplex module docs](duplex) for the full API including
143//! `subscribe`, `interrupt`, and `respond_to_permission`.
144//!
145//! For host-side bookkeeping (history, cumulative cost, optional
146//! [`BudgetTracker`] hard stop) on top of a [`DuplexSession`], wrap
147//! it in a [`Conversation`]. See the
148//! [conversation module docs](conversation).
149//!
150//! ## `Session` (for short-lived processes)
151//!
152//! ```no_run
153//! # #[cfg(all(feature = "async", feature = "json"))] {
154//! use std::sync::Arc;
155//! use claude_wrapper::Claude;
156//! use claude_wrapper::session::Session;
157//!
158//! # async fn example() -> claude_wrapper::Result<()> {
159//! let claude = Arc::new(Claude::builder().build()?);
160//! let mut session = Session::new(claude);
161//! let _first = session.send("what's 2 + 2?").await?;
162//! let _second = session.send("and squared?").await?;
163//! println!("cost: ${:.4}", session.total_cost_usd());
164//! # Ok(()) }
165//! # }
166//! ```
167//!
168//! See the [session module docs](session) for the full API.
169//!
170//! # Budget tracking
171//!
172//! Attach a [`BudgetTracker`] to a session (or share one across several
173//! sessions) to enforce a cumulative USD ceiling. Callbacks fire
174//! exactly once when thresholds are crossed; pre-turn checks
175//! short-circuit with [`Error::BudgetExceeded`]
176//! once the ceiling is hit.
177//!
178//! ```no_run
179//! # #[cfg(all(feature = "async", feature = "json"))] {
180//! use std::sync::Arc;
181//! use claude_wrapper::{BudgetTracker, Claude};
182//! use claude_wrapper::session::Session;
183//!
184//! # async fn example() -> claude_wrapper::Result<()> {
185//! let budget = BudgetTracker::builder()
186//! .max_usd(5.00)
187//! .warn_at_usd(4.00)
188//! .on_warning(|t| eprintln!("warning: ${t:.2}"))
189//! .on_exceeded(|t| eprintln!("budget hit: ${t:.2}"))
190//! .build();
191//!
192//! let claude = Arc::new(Claude::builder().build()?);
193//! let mut session = Session::new(claude).with_budget(budget.clone());
194//! session.send("hello").await?;
195//! println!("spent: ${:.4}", budget.total_usd());
196//! # Ok(()) }
197//! # }
198//! ```
199//!
200//! # Tool permissions
201//!
202//! Use [`ToolPattern`] for typed `--allowed-tools` / `--disallowed-tools`
203//! entries. Typed constructors always produce valid patterns; loose
204//! `From<&str>` keeps bare strings working for back-compat.
205//!
206//! ```
207//! use claude_wrapper::{QueryCommand, ToolPattern};
208//!
209//! let cmd = QueryCommand::new("review")
210//! .allowed_tool(ToolPattern::tool("Read"))
211//! .allowed_tool(ToolPattern::tool_with_args("Bash", "git log:*"))
212//! .allowed_tool(ToolPattern::all("Write"))
213//! .allowed_tool(ToolPattern::mcp("my-server", "*"))
214//! .disallowed_tool(ToolPattern::tool_with_args("Bash", "rm*"));
215//! ```
216//!
217//! # Streaming
218//!
219//! Process NDJSON events in real time with [`streaming::stream_query`]
220//! (async) or [`streaming::stream_query_sync`] (blocking; non-`Send`
221//! handler supported).
222//!
223//! ```no_run
224//! # #[cfg(all(feature = "async", feature = "json"))] {
225//! use claude_wrapper::{Claude, OutputFormat, QueryCommand};
226//! use claude_wrapper::streaming::{StreamEvent, stream_query};
227//!
228//! # async fn example() -> claude_wrapper::Result<()> {
229//! let claude = Claude::builder().build()?;
230//! let cmd = QueryCommand::new("explain quicksort")
231//! .output_format(OutputFormat::StreamJson);
232//!
233//! stream_query(&claude, &cmd, |event: StreamEvent| {
234//! if event.is_result() {
235//! println!("result: {}", event.result_text().unwrap_or(""));
236//! }
237//! }).await?;
238//! # Ok(()) }
239//! # }
240//! ```
241//!
242//! # MCP config generation
243//!
244//! Generate `.mcp.json` files for `--mcp-config`:
245//!
246//! ```no_run
247//! # #[cfg(feature = "async")] {
248//! use claude_wrapper::{Claude, ClaudeCommand, McpConfigBuilder, QueryCommand};
249//!
250//! # async fn example() -> claude_wrapper::Result<()> {
251//! McpConfigBuilder::new()
252//! .http_server("hub", "http://127.0.0.1:9090")
253//! .stdio_server("tool", "npx", ["my-server"])
254//! .write_to("/tmp/my-project/.mcp.json")?;
255//!
256//! let claude = Claude::builder().build()?;
257//! QueryCommand::new("list tools")
258//! .mcp_config("/tmp/my-project/.mcp.json")
259//! .execute(&claude)
260//! .await?;
261//! # Ok(()) }
262//! # }
263//! ```
264//!
265//! # On-disk introspection
266//!
267//! A family of read-only modules parses Claude Code's on-disk state
268//! under `~/.claude` directly, without spawning the CLI. Each exposes a
269//! root/loader with `list` / `get` accessors and degrades to an empty
270//! result when the directory is absent: [`history`] (sessions and
271//! transcripts), [`artifacts`] (agents), [`skills`], [`commands`]
272//! (custom slash commands), [`settings`] (the four merged layers),
273//! [`jobs`] (background-agent state), and [`worktrees`]. See the
274//! `inspect_state` example for an end-to-end tour.
275//!
276//! ```no_run
277//! # #[cfg(feature = "json")] {
278//! use claude_wrapper::history::HistoryRoot;
279//!
280//! # fn example() -> claude_wrapper::Result<()> {
281//! let history = HistoryRoot::home()?;
282//! for project in history.list_projects()? {
283//! println!(
284//! "{} ({} sessions)",
285//! project.decoded_path.display(),
286//! project.session_count,
287//! );
288//! }
289//! # Ok(()) }
290//! # }
291//! ```
292//!
293//! # Dangerous: bypass mode
294//!
295//! `--permission-mode bypassPermissions` is isolated behind
296//! [`dangerous::DangerousClient`], which requires an env-var
297//! acknowledgement ([`dangerous::ALLOW_ENV`] = `"1"`) at process start.
298//! See the [dangerous module docs](dangerous) for details.
299//!
300//! # Escape hatch
301//!
302//! For subcommands not yet wrapped, use [`RawCommand`]:
303//!
304//! ```no_run
305//! # #[cfg(feature = "async")] {
306//! use claude_wrapper::{Claude, ClaudeCommand, RawCommand};
307//!
308//! # async fn example() -> claude_wrapper::Result<()> {
309//! let claude = Claude::builder().build()?;
310//! let output = RawCommand::new("some-future-command")
311//! .arg("--new-flag")
312//! .arg("value")
313//! .execute(&claude)
314//! .await?;
315//! # Ok(()) }
316//! # }
317//! ```
318
319pub mod artifacts;
320pub mod auth;
321pub mod budget;
322pub mod command;
323pub mod commands;
324#[cfg(all(feature = "json", feature = "async"))]
325pub mod conversation;
326pub mod dangerous;
327#[cfg(all(feature = "json", feature = "async"))]
328pub mod duplex;
329pub mod error;
330pub mod exec;
331#[cfg(feature = "json")]
332pub mod history;
333#[cfg(feature = "json")]
334pub mod jobs;
335pub mod mcp_config;
336pub mod retry;
337#[cfg(all(feature = "json", feature = "async"))]
338pub mod session;
339#[cfg(feature = "json")]
340pub mod settings;
341pub mod skills;
342pub mod slash;
343pub mod streaming;
344pub mod tool_pattern;
345pub mod types;
346pub mod version;
347pub mod worktrees;
348
349use std::collections::HashMap;
350use std::path::{Path, PathBuf};
351use std::time::Duration;
352
353pub use budget::{BudgetBuilder, BudgetTracker};
354pub use command::ClaudeCommand;
355#[cfg(feature = "sync")]
356pub use command::ClaudeCommandSyncExt;
357#[allow(deprecated)]
358pub use command::agents::AgentsCommand;
359pub use command::auth::{
360 AuthLoginCommand, AuthLogoutCommand, AuthStatusCommand, LoginMode, SetupTokenCommand,
361};
362pub use command::auto_mode::{
363 AutoModeConfigCommand, AutoModeCritiqueCommand, AutoModeDefaultsCommand,
364};
365pub use command::doctor::DoctorCommand;
366pub use command::install::InstallCommand;
367pub use command::marketplace::{
368 MarketplaceAddCommand, MarketplaceListCommand, MarketplaceRemoveCommand,
369 MarketplaceUpdateCommand,
370};
371pub use command::mcp::{
372 McpAddCommand, McpAddFromDesktopCommand, McpAddJsonCommand, McpGetCommand, McpListCommand,
373 McpLoginCommand, McpLogoutCommand, McpRemoveCommand, McpResetProjectChoicesCommand,
374 McpServeCommand,
375};
376pub use command::plugin::{
377 PluginDetailsCommand, PluginDisableCommand, PluginEnableCommand, PluginInstallCommand,
378 PluginListCommand, PluginPruneCommand, PluginTagCommand, PluginUninstallCommand,
379 PluginUpdateCommand, PluginValidateCommand,
380};
381pub use command::project::ProjectPurgeCommand;
382pub use command::query::QueryCommand;
383pub use command::raw::RawCommand;
384pub use command::ultrareview::UltrareviewCommand;
385pub use command::update::UpdateCommand;
386pub use command::version::VersionCommand;
387#[cfg(all(feature = "json", feature = "async"))]
388pub use conversation::Conversation;
389#[cfg(all(feature = "json", feature = "async"))]
390pub use duplex::{
391 DuplexOptions, DuplexSession, InboundEvent, PermissionDecision, PermissionHandler,
392 PermissionRequest, TurnResult,
393};
394pub use error::{Error, Result};
395pub use exec::CommandOutput;
396#[cfg(feature = "tempfile")]
397pub use mcp_config::TempMcpConfig;
398pub use mcp_config::{McpConfigBuilder, McpServerConfig};
399pub use retry::{BackoffStrategy, RetryPolicy};
400#[cfg(all(feature = "json", feature = "async"))]
401pub use session::Session;
402pub use tool_pattern::{PatternError, ToolPattern};
403pub use types::*;
404pub use version::{CliVersion, CliVersionStatus, VersionParseError};
405
406/// The Claude CLI client. Holds shared configuration applied to all commands.
407///
408/// Create one via [`Claude::builder()`] and reuse it across commands.
409#[derive(Debug, Clone)]
410pub struct Claude {
411 pub(crate) binary: PathBuf,
412 pub(crate) working_dir: Option<PathBuf>,
413 pub(crate) env: HashMap<String, String>,
414 pub(crate) global_args: Vec<String>,
415 pub(crate) timeout: Option<Duration>,
416 pub(crate) retry_policy: Option<RetryPolicy>,
417 pub(crate) tested_cli_version_range: Option<(CliVersion, CliVersion)>,
418}
419
420impl Claude {
421 /// Create a new builder for configuring the Claude client.
422 #[must_use]
423 pub fn builder() -> ClaudeBuilder {
424 ClaudeBuilder::default()
425 }
426
427 /// Get the path to the claude binary.
428 #[must_use]
429 pub fn binary(&self) -> &Path {
430 &self.binary
431 }
432
433 /// Get the working directory, if set.
434 #[must_use]
435 pub fn working_dir(&self) -> Option<&Path> {
436 self.working_dir.as_deref()
437 }
438
439 /// Create a clone of this client with a different working directory.
440 #[must_use]
441 pub fn with_working_dir(&self, dir: impl Into<PathBuf>) -> Self {
442 let mut clone = self.clone();
443 clone.working_dir = Some(dir.into());
444 clone
445 }
446
447 /// Query the installed CLI version.
448 ///
449 /// Runs `claude --version` and parses the output into a [`CliVersion`].
450 ///
451 /// # Example
452 ///
453 /// ```no_run
454 /// # async fn example() -> claude_wrapper::Result<()> {
455 /// let claude = claude_wrapper::Claude::builder().build()?;
456 /// let version = claude.cli_version().await?;
457 /// println!("Claude CLI {version}");
458 /// # Ok(())
459 /// # }
460 /// ```
461 #[cfg(feature = "async")]
462 pub async fn cli_version(&self) -> Result<CliVersion> {
463 let output = VersionCommand::new().execute(self).await?;
464 CliVersion::parse_version_output(&output.stdout).map_err(|e| Error::Io {
465 message: format!("failed to parse CLI version: {e}"),
466 source: std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()),
467 working_dir: None,
468 })
469 }
470
471 /// Check that the installed CLI version meets a minimum requirement.
472 ///
473 /// Returns the detected version on success, or an error if the version
474 /// is below the minimum.
475 ///
476 /// # Example
477 ///
478 /// ```no_run
479 /// use claude_wrapper::CliVersion;
480 ///
481 /// # async fn example() -> claude_wrapper::Result<()> {
482 /// let claude = claude_wrapper::Claude::builder().build()?;
483 /// let version = claude.check_version(&CliVersion::new(2, 1, 0)).await?;
484 /// println!("CLI version {version} meets minimum requirement");
485 /// # Ok(())
486 /// # }
487 /// ```
488 #[cfg(feature = "async")]
489 pub async fn check_version(&self, minimum: &CliVersion) -> Result<CliVersion> {
490 let version = self.cli_version().await?;
491 if version.satisfies_minimum(minimum) {
492 Ok(version)
493 } else {
494 Err(Error::VersionMismatch {
495 found: version,
496 minimum: *minimum,
497 })
498 }
499 }
500
501 /// Blocking mirror of [`Claude::cli_version`]. Requires the
502 /// `sync` feature.
503 #[cfg(feature = "sync")]
504 pub fn cli_version_sync(&self) -> Result<CliVersion> {
505 let output = VersionCommand::new().execute_sync(self)?;
506 CliVersion::parse_version_output(&output.stdout).map_err(|e| Error::Io {
507 message: format!("failed to parse CLI version: {e}"),
508 source: std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()),
509 working_dir: None,
510 })
511 }
512
513 /// Blocking mirror of [`Claude::check_version`]. Requires the
514 /// `sync` feature.
515 #[cfg(feature = "sync")]
516 pub fn check_version_sync(&self, minimum: &CliVersion) -> Result<CliVersion> {
517 let version = self.cli_version_sync()?;
518 if version.satisfies_minimum(minimum) {
519 Ok(version)
520 } else {
521 Err(Error::VersionMismatch {
522 found: version,
523 minimum: *minimum,
524 })
525 }
526 }
527
528 /// The tested-against `[min, max]` range declared at build time
529 /// via [`ClaudeBuilder::tested_cli_version_range`], if any.
530 #[must_use]
531 pub fn tested_cli_version_range(&self) -> Option<(CliVersion, CliVersion)> {
532 self.tested_cli_version_range
533 }
534
535 /// Classify the installed CLI against the declared
536 /// tested-against range. Logs a `tracing::warn!` when outside
537 /// the range; returns the typed status either way. If no range
538 /// was declared via [`ClaudeBuilder::tested_cli_version_range`],
539 /// returns [`CliVersionStatus::Tested`] -- callers that didn't
540 /// opt in get the silent-success path.
541 ///
542 /// Intended for one-shot use at startup, not on every command.
543 #[cfg(feature = "async")]
544 pub async fn cli_version_status(&self) -> Result<CliVersionStatus> {
545 let Some((min, max)) = self.tested_cli_version_range else {
546 return Ok(CliVersionStatus::Tested);
547 };
548 let found = self.cli_version().await?;
549 let status = found.status_within(&min, &max);
550 warn_on_drift(&status);
551 Ok(status)
552 }
553
554 /// Blocking mirror of [`Claude::cli_version_status`]. Requires
555 /// the `sync` feature.
556 #[cfg(feature = "sync")]
557 pub fn cli_version_status_sync(&self) -> Result<CliVersionStatus> {
558 let Some((min, max)) = self.tested_cli_version_range else {
559 return Ok(CliVersionStatus::Tested);
560 };
561 let found = self.cli_version_sync()?;
562 let status = found.status_within(&min, &max);
563 warn_on_drift(&status);
564 Ok(status)
565 }
566}
567
568#[allow(dead_code)] // unused with neither `async` nor `sync` feature
569fn warn_on_drift(status: &CliVersionStatus) {
570 match status {
571 CliVersionStatus::Tested => {}
572 CliVersionStatus::NewerUntested {
573 found, tested_max, ..
574 } => {
575 tracing::warn!(
576 found = %found,
577 tested_max = %tested_max,
578 "claude CLI is newer than the wrapper's tested-against range; \
579 semantics may have drifted -- proceed with caution"
580 );
581 }
582 CliVersionStatus::OlderThanMinimum { found, minimum, .. } => {
583 tracing::warn!(
584 found = %found,
585 minimum = %minimum,
586 "claude CLI is older than the wrapper's declared minimum; \
587 incorrect behavior is likely (missing flags, different shapes)"
588 );
589 }
590 }
591}
592
593/// Builder for creating a [`Claude`] client.
594///
595/// # Example
596///
597/// ```no_run
598/// use claude_wrapper::Claude;
599///
600/// # fn example() -> claude_wrapper::Result<()> {
601/// let claude = Claude::builder()
602/// .env("AWS_REGION", "us-west-2")
603/// .timeout_secs(120)
604/// .build()?;
605/// # Ok(())
606/// # }
607/// ```
608#[derive(Debug, Default)]
609pub struct ClaudeBuilder {
610 binary: Option<PathBuf>,
611 working_dir: Option<PathBuf>,
612 env: HashMap<String, String>,
613 global_args: Vec<String>,
614 timeout: Option<Duration>,
615 retry_policy: Option<RetryPolicy>,
616 tested_cli_version_range: Option<(CliVersion, CliVersion)>,
617}
618
619impl ClaudeBuilder {
620 /// Set the path to the claude binary.
621 ///
622 /// If not set, the binary is resolved from PATH using `which`.
623 #[must_use]
624 pub fn binary(mut self, path: impl Into<PathBuf>) -> Self {
625 self.binary = Some(path.into());
626 self
627 }
628
629 /// Set the working directory for all commands.
630 ///
631 /// The spawned process will use this as its current directory.
632 #[must_use]
633 pub fn working_dir(mut self, path: impl Into<PathBuf>) -> Self {
634 self.working_dir = Some(path.into());
635 self
636 }
637
638 /// Add an environment variable to pass to all commands.
639 #[must_use]
640 pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
641 self.env.insert(key.into(), value.into());
642 self
643 }
644
645 /// Add multiple environment variables.
646 #[must_use]
647 pub fn envs(
648 mut self,
649 vars: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
650 ) -> Self {
651 for (k, v) in vars {
652 self.env.insert(k.into(), v.into());
653 }
654 self
655 }
656
657 /// Set a default timeout for all commands (in seconds).
658 #[must_use]
659 pub fn timeout_secs(mut self, seconds: u64) -> Self {
660 self.timeout = Some(Duration::from_secs(seconds));
661 self
662 }
663
664 /// Set a default timeout for all commands.
665 #[must_use]
666 pub fn timeout(mut self, duration: Duration) -> Self {
667 self.timeout = Some(duration);
668 self
669 }
670
671 /// Add a global argument applied to all commands.
672 ///
673 /// This is an escape hatch for flags not yet covered by the API.
674 #[must_use]
675 pub fn arg(mut self, arg: impl Into<String>) -> Self {
676 self.global_args.push(arg.into());
677 self
678 }
679
680 /// Enable verbose output for all commands (`--verbose`).
681 #[must_use]
682 pub fn verbose(mut self) -> Self {
683 self.global_args.push("--verbose".into());
684 self
685 }
686
687 /// Enable debug output for all commands (`--debug`).
688 #[must_use]
689 pub fn debug(mut self) -> Self {
690 self.global_args.push("--debug".into());
691 self
692 }
693
694 /// Set a default retry policy for all commands.
695 ///
696 /// Individual commands can override this via their own retry settings.
697 ///
698 /// # Example
699 ///
700 /// ```no_run
701 /// use claude_wrapper::{Claude, RetryPolicy};
702 /// use std::time::Duration;
703 ///
704 /// # fn example() -> claude_wrapper::Result<()> {
705 /// let claude = Claude::builder()
706 /// .retry(RetryPolicy::new()
707 /// .max_attempts(3)
708 /// .initial_backoff(Duration::from_secs(2))
709 /// .exponential()
710 /// .retry_on_timeout(true))
711 /// .build()?;
712 /// # Ok(())
713 /// # }
714 /// ```
715 #[must_use]
716 pub fn retry(mut self, policy: RetryPolicy) -> Self {
717 self.retry_policy = Some(policy);
718 self
719 }
720
721 /// Declare the inclusive `[min, max]` range of `claude` CLI
722 /// versions this client has been tested against.
723 ///
724 /// The wrapper does not enforce the range -- nothing errors when
725 /// it's set wrong. Use [`Claude::cli_version_status`] (or its
726 /// sync mirror) at startup to classify the actually-installed CLI
727 /// against this declaration; that call returns a typed
728 /// [`CliVersionStatus`] AND emits a `tracing::warn!` when
729 /// outside the range. Hosts (claude-server, application code)
730 /// can additionally surface the status to operators.
731 ///
732 /// # Why this exists
733 ///
734 /// CLI semantics drift across minor / patch releases (e.g.
735 /// `claude agents` was repurposed in 2.1.143). The min floor
736 /// lets us say "we know it's broken below this"; the max ceiling
737 /// lets us say "we haven't verified above this -- proceed but
738 /// expect surprises."
739 ///
740 /// # Example
741 ///
742 /// ```no_run
743 /// use claude_wrapper::{Claude, CliVersion};
744 ///
745 /// # async fn example() -> claude_wrapper::Result<()> {
746 /// let claude = Claude::builder()
747 /// .tested_cli_version_range(CliVersion::new(2, 1, 0), CliVersion::new(2, 1, 999))
748 /// .build()?;
749 /// // Run once at startup to log a warning if the CLI is out of range.
750 /// let _status = claude.cli_version_status().await?;
751 /// # Ok(()) }
752 /// ```
753 #[must_use]
754 pub fn tested_cli_version_range(mut self, min: CliVersion, max: CliVersion) -> Self {
755 self.tested_cli_version_range = Some((min, max));
756 self
757 }
758
759 /// Build the Claude client, resolving the binary path.
760 pub fn build(self) -> Result<Claude> {
761 let binary = match self.binary {
762 Some(path) => path,
763 None => which::which("claude").map_err(|_| Error::NotFound)?,
764 };
765
766 Ok(Claude {
767 binary,
768 working_dir: self.working_dir,
769 env: self.env,
770 global_args: self.global_args,
771 timeout: self.timeout,
772 retry_policy: self.retry_policy,
773 tested_cli_version_range: self.tested_cli_version_range,
774 })
775 }
776}
777
778#[cfg(test)]
779mod tests {
780 use super::*;
781
782 #[test]
783 fn test_builder_with_binary() {
784 let claude = Claude::builder()
785 .binary("/usr/local/bin/claude")
786 .env("FOO", "bar")
787 .timeout_secs(60)
788 .build()
789 .unwrap();
790
791 assert_eq!(claude.binary, PathBuf::from("/usr/local/bin/claude"));
792 assert_eq!(claude.env.get("FOO").unwrap(), "bar");
793 assert_eq!(claude.timeout, Some(Duration::from_secs(60)));
794 }
795
796 #[test]
797 fn test_builder_global_args() {
798 let claude = Claude::builder()
799 .binary("/usr/local/bin/claude")
800 .arg("--verbose")
801 .build()
802 .unwrap();
803
804 assert_eq!(claude.global_args, vec!["--verbose"]);
805 }
806
807 #[test]
808 fn test_builder_verbose() {
809 let claude = Claude::builder()
810 .binary("/usr/local/bin/claude")
811 .verbose()
812 .build()
813 .unwrap();
814 assert!(claude.global_args.contains(&"--verbose".to_string()));
815 }
816
817 #[test]
818 fn test_builder_debug() {
819 let claude = Claude::builder()
820 .binary("/usr/local/bin/claude")
821 .debug()
822 .build()
823 .unwrap();
824 assert!(claude.global_args.contains(&"--debug".to_string()));
825 }
826}