rusty_claw 0.1.0

Rust implementation of the Claude Agent SDK
Documentation
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Control protocol message types for bidirectional communication with Claude CLI
//!
//! This module defines the message types used in the control protocol:
//! - [`ControlRequest`] - Outgoing requests from SDK to CLI
//! - [`ControlResponse`] - Responses to control requests
//! - [`IncomingControlRequest`] - Incoming requests from CLI to SDK
//!
//! # Message Flow
//!
//! **SDK → CLI (Outgoing):**
//! ```text
//! {
//!   "type": "control_request",
//!   "request_id": "uuid",
//!   "request": { "subtype": "initialize", ... }
//! }
//! ```
//!
//! **CLI → SDK (Response):**
//! ```text
//! {
//!   "type": "control_response",
//!   "request_id": "uuid",
//!   "response": { "subtype": "success", ... }
//! }
//! ```
//!
//! **CLI → SDK (Incoming Request):**
//! ```text
//! {
//!   "type": "control_request",
//!   "request_id": "uuid",
//!   "request": { "subtype": "can_use_tool", ... }
//! }
//! ```

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

use crate::options::{AgentDefinition, HookEvent, HookMatcher};

/// Outgoing control requests from SDK to Claude CLI
///
/// These messages are sent from the SDK to control CLI behavior during a session.
/// All requests follow the format:
/// ```json
/// {
///   "type": "control_request",
///   "request_id": "uuid",
///   "request": { "subtype": "...", ... }
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "subtype", rename_all = "snake_case")]
pub enum ControlRequest {
    /// Initialize the agent session with hooks, agents, and MCP servers
    ///
    /// Must be sent before any other interaction. Configures:
    /// - Hook callbacks for events (tool use, messages, etc.)
    /// - Agent definitions for spawning subagents
    /// - SDK-hosted MCP servers for tool execution
    ///
    /// Note: Permission mode is set via the `--permission-mode` CLI flag,
    /// not in the initialize request.
    ///
    /// # Example
    /// ```json
    /// {
    ///   "subtype": "initialize",
    ///   "hooks": {},
    ///   "agents": {},
    ///   "sdk_mcp_servers": []
    /// }
    /// ```
    Initialize {
        /// Hook event matchers for callback registration
        #[serde(skip_serializing_if = "HashMap::is_empty", default)]
        hooks: HashMap<HookEvent, Vec<HookMatcher>>,

        /// Agent definitions for spawning subagents
        #[serde(skip_serializing_if = "HashMap::is_empty", default)]
        agents: HashMap<String, AgentDefinition>,

        /// SDK-hosted MCP server names (strings, not objects)
        ///
        /// The CLI expects `sdkMcpServers: ["name1", "name2"]` — just names.
        #[serde(
            rename = "sdkMcpServers",
            skip_serializing_if = "Vec::is_empty",
            default
        )]
        sdk_mcp_servers: Vec<String>,
    },

    /// Interrupt the current agent execution
    ///
    /// Sends a cancellation signal to stop ongoing processing.
    /// The CLI will finish the current operation and return control.
    Interrupt,

    /// Change the permission mode during execution
    ///
    /// Dynamically adjusts how tool permissions are handled.
    ///
    /// # Example
    /// ```json
    /// {
    ///   "subtype": "set_permission_mode",
    ///   "mode": "accept_edits"
    /// }
    /// ```
    SetPermissionMode {
        /// New permission mode (e.g., "default", "accept_edits", "bypass_permissions")
        mode: String,
    },

    /// Switch the active model during execution
    ///
    /// Changes which Claude model processes subsequent turns.
    ///
    /// # Example
    /// ```json
    /// {
    ///   "subtype": "set_model",
    ///   "model": "claude-sonnet-4"
    /// }
    /// ```
    SetModel {
        /// Model identifier (e.g., "claude-sonnet-4", "claude-opus-4")
        model: String,
    },

    /// Query MCP server connection status
    ///
    /// Returns information about connected MCP servers and their tools.
    McpStatus,

    /// Rewind file state to a specific message
    ///
    /// Rolls back filesystem changes to the state at the given message ID.
    ///
    /// # Example
    /// ```json
    /// {
    ///   "subtype": "rewind_files",
    ///   "user_message_id": "msg_123"
    /// }
    /// ```
    RewindFiles {
        /// Message ID to rewind to
        ///
        /// Renamed from `message_id` to `user_message_id` for parity with the
        /// Python SDK's `SDKControlRewindFilesRequest.user_message_id`.
        user_message_id: String,
    },

    /// Get server information from the Claude CLI
    ///
    /// Returns version and capability information from the connected CLI process.
    /// The response data includes at least a `"version"` field.
    GetServerInfo,
}

/// Response to a control request
///
/// Sent from CLI → SDK or SDK → CLI in response to control requests.
/// All responses include a `subtype` of either "success" or "error".
///
/// # Success Example
/// ```json
/// {
///   "subtype": "success",
///   "data": { "foo": "bar" }
/// }
/// ```
///
/// # Error Example
/// ```json
/// {
///   "subtype": "error",
///   "error": "Tool not found",
///   "code": "tool_not_found"
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "subtype", rename_all = "snake_case")]
pub enum ControlResponse {
    /// Successful control request response
    Success {
        /// Response data (structure depends on request type)
        #[serde(flatten)]
        data: Value,
    },

    /// Error response
    Error {
        /// Human-readable error message
        error: String,

        /// Additional error context (error codes, details, etc.)
        #[serde(flatten)]
        extra: Value,
    },
}

/// Incoming control requests from Claude CLI to SDK
///
/// These messages are sent from the CLI to request SDK actions or permissions.
/// The SDK must respond with a [`ControlResponse`].
///
/// # Message Format
/// ```json
/// {
///   "type": "control_request",
///   "request_id": "uuid",
///   "request": { "subtype": "...", ... }
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "subtype", rename_all = "snake_case")]
pub enum IncomingControlRequest {
    /// Request permission to use a tool
    ///
    /// The CLI asks the SDK whether a specific tool invocation should be allowed.
    /// The SDK should respond with `Success { data: { "allowed": true/false } }`.
    ///
    /// # Example Request
    /// ```json
    /// {
    ///   "subtype": "can_use_tool",
    ///   "tool_name": "Bash",
    ///   "tool_input": { "command": "ls -la" }
    /// }
    /// ```
    ///
    /// # Example Response
    /// ```json
    /// {
    ///   "subtype": "success",
    ///   "allowed": true
    /// }
    /// ```
    CanUseTool {
        /// Name of the tool to be used
        tool_name: String,

        /// Tool input parameters
        tool_input: Value,
    },

    /// Invoke a registered hook callback
    ///
    /// The CLI triggers a hook based on registered matchers.
    /// The SDK should execute the hook and return the result.
    ///
    /// # Example Request
    /// ```json
    /// {
    ///   "subtype": "hook_callback",
    ///   "hook_id": "pre_commit_hook",
    ///   "hook_event": "tool_use",
    ///   "hook_input": { "tool": "Bash", "command": "git commit" }
    /// }
    /// ```
    ///
    /// # Example Response
    /// ```json
    /// {
    ///   "subtype": "success",
    ///   "output": "Hook executed successfully"
    /// }
    /// ```
    HookCallback {
        /// Unique hook identifier
        hook_id: String,

        /// Event that triggered the hook
        hook_event: HookEvent,

        /// Hook input data
        hook_input: Value,
    },

    /// Route an MCP message to an SDK-hosted server
    ///
    /// The CLI forwards a JSON-RPC message to an MCP server hosted by the SDK.
    /// The SDK should route to the appropriate server and return the JSON-RPC response.
    ///
    /// # Example Request
    /// ```json
    /// {
    ///   "subtype": "mcp_message",
    ///   "server_name": "my_tool_server",
    ///   "message": {
    ///     "jsonrpc": "2.0",
    ///     "id": 1,
    ///     "method": "tools/call",
    ///     "params": { "name": "my_tool", "arguments": {} }
    ///   }
    /// }
    /// ```
    ///
    /// # Example Response
    /// ```json
    /// {
    ///   "subtype": "success",
    ///   "jsonrpc": "2.0",
    ///   "id": 1,
    ///   "result": { "content": [{ "type": "text", "text": "Done" }] }
    /// }
    /// ```
    McpMessage {
        /// Name of the SDK-hosted MCP server
        server_name: String,

        /// JSON-RPC message to forward
        message: Value,
    },
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_control_request_initialize_minimal() {
        let req = ControlRequest::Initialize {
            hooks: HashMap::new(),
            agents: HashMap::new(),
            sdk_mcp_servers: vec![],
        };

        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "initialize");
        // Empty collections should be omitted
        assert!(json.get("hooks").is_none());
        assert!(json.get("agents").is_none());
        assert!(json.get("sdkMcpServers").is_none());
    }

    #[test]
    fn test_control_request_initialize_roundtrip() {
        let req = ControlRequest::Initialize {
            hooks: HashMap::new(),
            agents: HashMap::new(),
            sdk_mcp_servers: vec![],
        };

        let json = serde_json::to_string(&req).unwrap();
        let parsed: ControlRequest = serde_json::from_str(&json).unwrap();

        match parsed {
            ControlRequest::Initialize {
                hooks,
                agents,
                sdk_mcp_servers,
            } => {
                assert!(hooks.is_empty());
                assert!(agents.is_empty());
                assert!(sdk_mcp_servers.is_empty());
            }
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_control_request_interrupt() {
        let req = ControlRequest::Interrupt;
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "interrupt");
    }

    #[test]
    fn test_control_request_set_permission_mode() {
        let req = ControlRequest::SetPermissionMode {
            mode: "accept_edits".to_string(),
        };

        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "set_permission_mode");
        assert_eq!(json["mode"], "accept_edits");
    }

    #[test]
    fn test_control_request_set_model() {
        let req = ControlRequest::SetModel {
            model: "claude-sonnet-4".to_string(),
        };

        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "set_model");
        assert_eq!(json["model"], "claude-sonnet-4");
    }

    #[test]
    fn test_control_request_mcp_status() {
        let req = ControlRequest::McpStatus;
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "mcp_status");
    }

    #[test]
    fn test_control_request_rewind_files() {
        let req = ControlRequest::RewindFiles {
            user_message_id: "msg_123".to_string(),
        };

        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "rewind_files");
        assert_eq!(json["user_message_id"], "msg_123");
    }

    #[test]
    fn test_control_request_get_server_info() {
        let req = ControlRequest::GetServerInfo;
        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "get_server_info");
    }

    #[test]
    fn test_control_response_success() {
        let resp = ControlResponse::Success {
            data: json!({ "allowed": true }),
        };

        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["subtype"], "success");
        assert_eq!(json["allowed"], true);
    }

    #[test]
    fn test_control_response_error() {
        let resp = ControlResponse::Error {
            error: "Tool not found".to_string(),
            extra: json!({ "code": "tool_not_found" }),
        };

        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["subtype"], "error");
        assert_eq!(json["error"], "Tool not found");
        assert_eq!(json["code"], "tool_not_found");
    }

    #[test]
    fn test_control_response_roundtrip() {
        let resp = ControlResponse::Success {
            data: json!({ "foo": "bar", "count": 42 }),
        };

        let json = serde_json::to_string(&resp).unwrap();
        let parsed: ControlResponse = serde_json::from_str(&json).unwrap();

        match parsed {
            ControlResponse::Success { data } => {
                assert_eq!(data["foo"], "bar");
                assert_eq!(data["count"], 42);
            }
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_incoming_control_request_can_use_tool() {
        let req = IncomingControlRequest::CanUseTool {
            tool_name: "Bash".to_string(),
            tool_input: json!({ "command": "ls -la" }),
        };

        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "can_use_tool");
        assert_eq!(json["tool_name"], "Bash");
        assert_eq!(json["tool_input"]["command"], "ls -la");
    }

    #[test]
    fn test_incoming_control_request_hook_callback() {
        let req = IncomingControlRequest::HookCallback {
            hook_id: "pre_commit".to_string(),
            hook_event: crate::options::HookEvent::PreToolUse,
            hook_input: json!({ "tool": "Bash" }),
        };

        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "hook_callback");
        assert_eq!(json["hook_id"], "pre_commit");
    }

    #[test]
    fn test_incoming_control_request_mcp_message() {
        let req = IncomingControlRequest::McpMessage {
            server_name: "my_server".to_string(),
            message: json!({
                "jsonrpc": "2.0",
                "id": 1,
                "method": "tools/call"
            }),
        };

        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["subtype"], "mcp_message");
        assert_eq!(json["server_name"], "my_server");
        assert_eq!(json["message"]["method"], "tools/call");
    }

    #[test]
    fn test_incoming_control_request_roundtrip() {
        let req = IncomingControlRequest::CanUseTool {
            tool_name: "Read".to_string(),
            tool_input: json!({ "file_path": "/tmp/test.txt" }),
        };

        let json = serde_json::to_string(&req).unwrap();
        let parsed: IncomingControlRequest = serde_json::from_str(&json).unwrap();

        match parsed {
            IncomingControlRequest::CanUseTool {
                tool_name,
                tool_input,
            } => {
                assert_eq!(tool_name, "Read");
                assert_eq!(tool_input["file_path"], "/tmp/test.txt");
            }
            _ => panic!("Wrong variant"),
        }
    }
}