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
//! **claude-code-agent** — Rust SDK for Claude Code.
//!
//! Drives the `claude` CLI (verified against 2.1.328) over its stream-json
//! control protocol: one persistent process per session, Rust closures as
//! hook callbacks and permission handlers, in-process tools over the SDK MCP
//! bridge, record/replay, and a session pool.
//!
//! # Quick start
//!
//! ```rust,no_run
//! use claude_code_agent::{Session, SessionOptions, Initialize, Agent, AgentEvent};
//! use tokio_stream::StreamExt;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let session = Session::spawn(
//! SessionOptions { cwd: Some("/my/project".into()), ..Default::default() },
//! Initialize::default(),
//! ).await?;
//!
//! let agent = Agent::new(session);
//! let mut stream = agent.prompt("hello").await?;
//!
//! while let Some(event) = stream.next().await {
//! match event {
//! AgentEvent::Text(t) => print!("{}", t),
//! AgentEvent::Done(r) => {
//! eprintln!("\ncost: ${:.4}", r.total_cost_usd.unwrap_or(0.0));
//! break;
//! }
//! AgentEvent::Error(e) => eprintln!("error: {}", e),
//! _ => {}
//! }
//! }
//! Ok(())
//! }
//! ```
//!
//! # With hooks
//!
//! ```rust,no_run
//! use claude_code_agent::{Agent, AgentEvent, CanUseTool, HookRegistry, Initialize, Session, SessionOptions};
//! use claude_code_agent::protocol::{HookOutput, ToolDecision};
//! use tokio_stream::StreamExt;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! // Hooks are declared before spawn: the CLI learns them from `initialize`.
//! let mut hooks = HookRegistry::new();
//! hooks.on_pre_tool_use(|payload| async move {
//! match payload.tool_name.as_deref() {
//! Some("Bash") => HookOutput::allow(),
//! Some(name) => HookOutput::block(format!("{name} not allowed")),
//! None => HookOutput::allow(),
//! }
//! });
//!
//! let opts = SessionOptions {
//! hooks,
//! // Asked whenever the CLI's own permission rules would prompt.
//! can_use_tool: Some(CanUseTool::new(|req| async move {
//! if req.tool_name == "Bash" { ToolDecision::allow() } else { ToolDecision::deny("read-only") }
//! })),
//! ..Default::default()
//! };
//! let session = Session::spawn(opts, Initialize::default()).await?;
//! let agent = Agent::new(session);
//!
//! let mut stream = agent.prompt("list files in /tmp").await?;
//! while let Some(ev) = stream.next().await {
//! if let AgentEvent::Text(t) = &ev { print!("{}", t); }
//! if matches!(ev, AgentEvent::Done(_) | AgentEvent::Error(_)) { break; }
//! }
//! Ok(())
//! }
//! ```
//!
//! # With session pool (high throughput)
//!
//! ```rust,no_run
//! use claude_code_agent::{SessionPool, PoolConfig, Agent, AgentEvent};
//! use tokio_stream::StreamExt;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let pool = SessionPool::new(PoolConfig { max_sessions: 4, ..Default::default() });
//! pool.warm(2).await; // pre-warm 2 processes
//!
//! // Parallel prompts — each reuses an idle session from the pool
//! let (r1, r2) = tokio::join!(
//! run_prompt(&pool, "task one"),
//! run_prompt(&pool, "task two"),
//! );
//! Ok(())
//! }
//!
//! async fn run_prompt(pool: &SessionPool, text: &str) -> anyhow::Result<String> {
//! let borrowed = pool.acquire().await?;
//! let agent = Agent::new(borrowed.session.clone());
//! let mut stream = agent.prompt(text).await?;
//! let mut out = String::new();
//! while let Some(ev) = stream.next().await {
//! if let AgentEvent::Text(t) = &ev { out.push_str(t); }
//! if matches!(ev, AgentEvent::Done(_) | AgentEvent::Error(_)) { break; }
//! }
//! Ok(out)
//! }
//! ```
// ── Top-level re-exports ───────────────────────────────────────────────────────
pub use ;
pub use HookRegistry;
pub use ;
/// `#[claude_tool]` — enable with the `macros` feature.
pub use claude_tool;
pub use ;
pub use ;
pub use ;
/// Re-exports used by code generated from `#[claude_tool]`. Not public API.