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
//! Claude CLI Runner implementation
//!
//! Wraps the `claude` CLI with stream-json output parsing.
use crate::types::*;
use std::process::Stdio;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::{Child, Command};
use tokio::sync::Mutex;
use tracing::{debug, warn};
/// Claude Code CLI Runner
///
/// Wraps `claude --print --output-format stream-json` execution.
pub struct ClaudeRunner {
/// Running process handle
process: Arc<Mutex<Option<Child>>>,
/// Cancellation flag
cancelled: Arc<AtomicBool>,
}
impl Default for ClaudeRunner {
fn default() -> Self {
Self::new()
}
}
impl ClaudeRunner {
/// Create a new runner
pub fn new() -> Self {
Self {
process: Arc::new(Mutex::new(None)),
cancelled: Arc::new(AtomicBool::new(false)),
}
}
/// Execute Claude CLI
pub async fn run(&self, options: RunOptions) -> Result<RunResult, RunnerError> {
let RunOptions {
prompt,
cwd,
session_id,
mcp_config,
timeout,
on_progress,
} = options;
// Reset cancellation flag
self.cancelled.store(false, Ordering::SeqCst);
// Build command arguments
let mut args = vec![
"--print".to_string(),
prompt,
"--output-format".to_string(),
"stream-json".to_string(),
"--verbose".to_string(),
];
if let Some(sid) = session_id {
args.push("--resume".to_string());
args.push(sid);
}
if let Some(mcp) = mcp_config {
args.push("--mcp-config".to_string());
args.push(mcp.to_string_lossy().to_string());
}
// Build command
let mut cmd = Command::new("claude");
cmd.args(&args)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
if let Some(dir) = cwd {
cmd.current_dir(dir);
}
debug!(?args, "Starting Claude CLI");
// Spawn process
let mut child = cmd.spawn()?;
// Get stdout/stderr handles
let stdout = child
.stdout
.take()
.ok_or_else(|| RunnerError::CliFailed("Failed to capture stdout".into()))?;
let stderr = child
.stderr
.take()
.ok_or_else(|| RunnerError::CliFailed("Failed to capture stderr".into()))?;
// Store child for potential cancellation
*self.process.lock().await = Some(child);
// Parse stream-json output
let mut result_event: Option<ResultEvent> = None;
let mut errors: Vec<String> = Vec::new();
// Read stdout lines
let mut stdout_reader = BufReader::new(stdout).lines();
let mut stderr_reader = BufReader::new(stderr);
// Spawn stderr reader
let errors_handle = {
let errors = Arc::new(Mutex::new(errors.clone()));
let errors_ref = errors.clone();
tokio::spawn(async move {
let mut buf = String::new();
loop {
buf.clear();
match tokio::io::AsyncBufReadExt::read_line(&mut stderr_reader, &mut buf).await {
Ok(0) => break,
Ok(_) => {
let line = buf.trim().to_string();
if !line.is_empty() {
errors_ref.lock().await.push(line);
}
}
Err(_) => break,
}
}
errors_ref.lock().await.clone()
})
};
// Process stdout with timeout
let cancelled = self.cancelled.clone();
let process_result = tokio::time::timeout(timeout, async {
while let Some(line) = stdout_reader.next_line().await? {
// Check cancellation
if cancelled.load(Ordering::SeqCst) {
return Err(RunnerError::Cancelled);
}
if line.trim().is_empty() {
continue;
}
// Parse JSON line
match serde_json::from_str::<serde_json::Value>(&line) {
Ok(value) => {
// Try to parse as StreamEvent
if let Some(event_type) = value.get("type").and_then(|v| v.as_str()) {
match event_type {
"result" => {
if let Ok(evt) = serde_json::from_value::<ResultEvent>(value.clone()) {
result_event = Some(evt.clone());
if let Some(ref cb) = on_progress {
cb(StreamEvent::Result(evt));
}
}
}
"assistant" => {
if let Ok(evt) = serde_json::from_value::<AssistantEvent>(value.clone()) {
if let Some(ref cb) = on_progress {
cb(StreamEvent::Assistant(evt));
}
}
}
"user" => {
if let Ok(evt) = serde_json::from_value::<UserEvent>(value.clone()) {
if let Some(ref cb) = on_progress {
cb(StreamEvent::User(evt));
}
}
}
"system" => {
if let Ok(evt) = serde_json::from_value::<SystemEvent>(value.clone()) {
if let Some(ref cb) = on_progress {
cb(StreamEvent::System(evt));
}
}
}
_ => {
debug!(?event_type, "Unknown event type");
}
}
}
}
Err(e) => {
// Non-JSON line, might be debug output
debug!(?line, ?e, "Non-JSON line from Claude CLI");
}
}
}
Ok::<_, RunnerError>(())
})
.await;
// Wait for stderr reader
errors = errors_handle.await.unwrap_or_default();
// Wait for process to complete
let mut proc_guard = self.process.lock().await;
if let Some(mut child) = proc_guard.take() {
let _ = child.wait().await;
}
// Handle timeout
match process_result {
Err(_) => {
return Err(RunnerError::Timeout(timeout));
}
Ok(Err(e)) => {
return Err(e);
}
Ok(Ok(())) => {}
}
// Check for result
let result_event = result_event.ok_or_else(|| {
RunnerError::NoResult(errors.join("\n"))
})?;
// Convert to RunResult
Ok(RunResult {
session_id: result_event.session_id,
result: result_event.result,
is_error: result_event.is_error,
duration_ms: result_event.duration_ms,
duration_api_ms: result_event.duration_api_ms,
num_turns: result_event.num_turns,
total_cost_usd: result_event.total_cost_usd,
usage: result_event.usage.map(|u| Usage {
input_tokens: u.input_tokens,
output_tokens: u.output_tokens,
}),
})
}
/// Cancel the running process
pub async fn cancel(&self) {
// Set cancellation flag
self.cancelled.store(true, Ordering::SeqCst);
// Try to kill the process
let mut proc = self.process.lock().await;
if let Some(ref mut child) = *proc {
// Send SIGTERM first
if let Err(e) = child.kill().await {
warn!(?e, "Failed to kill Claude CLI process");
}
}
}
/// Check if a process is running
pub async fn is_running(&self) -> bool {
let proc = self.process.lock().await;
proc.is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_result_event() {
let json = r#"{
"type": "result",
"subtype": "success",
"session_id": "abc123",
"result": "Hello, world!",
"is_error": false,
"duration_ms": 1000,
"duration_api_ms": 800,
"num_turns": 1,
"total_cost_usd": 0.001,
"usage": {
"input_tokens": 100,
"output_tokens": 50
}
}"#;
let event: ResultEvent = serde_json::from_str(json).unwrap();
assert_eq!(event.session_id, "abc123");
assert_eq!(event.result, "Hello, world!");
assert!(!event.is_error);
assert_eq!(event.num_turns, 1);
}
#[test]
fn test_parse_assistant_event() {
let json = r#"{
"type": "assistant",
"message": {
"content": [
{"type": "text", "text": "Hello!"},
{"type": "tool_use", "id": "tool1", "name": "bash", "input": {"command": "ls"}}
]
}
}"#;
let value: serde_json::Value = serde_json::from_str(json).unwrap();
let event: AssistantEvent = serde_json::from_value(value).unwrap();
assert_eq!(event.message.content.len(), 2);
}
}