Skip to main content

claude_code_rs/types/
control.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4/// A control command sent from the SDK to the CLI.
5#[derive(Debug, Clone)]
6pub enum SDKControlCommand {
7    Interrupt,
8    SetPermissionMode { mode: String },
9    SetModel { model: String },
10    RewindFiles { user_message_id: String },
11    GetMcpStatus,
12}
13
14impl SDKControlCommand {
15    pub fn interrupt() -> Self {
16        Self::Interrupt
17    }
18
19    pub fn set_permission_mode(mode: &str) -> Self {
20        Self::SetPermissionMode { mode: mode.into() }
21    }
22
23    pub fn set_model(model: &str) -> Self {
24        Self::SetModel { model: model.into() }
25    }
26
27    pub fn rewind_files(user_message_id: &str) -> Self {
28        Self::RewindFiles { user_message_id: user_message_id.into() }
29    }
30
31    pub fn get_mcp_status() -> Self {
32        Self::GetMcpStatus
33    }
34
35    /// Build the full request body for the control protocol.
36    pub fn to_request_body(&self) -> Value {
37        match self {
38            Self::Interrupt => serde_json::json!({"subtype": "interrupt"}),
39            Self::SetPermissionMode { mode } => serde_json::json!({"subtype": "set_permission_mode", "mode": mode}),
40            Self::SetModel { model } => serde_json::json!({"subtype": "set_model", "model": model}),
41            Self::RewindFiles { user_message_id } => serde_json::json!({"subtype": "rewind_files", "user_message_id": user_message_id}),
42            Self::GetMcpStatus => serde_json::json!({"subtype": "get_mcp_status"}),
43        }
44    }
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize, Default)]
48pub struct SDKCapabilities {
49    #[serde(default)]
50    pub hooks: bool,
51    #[serde(default)]
52    pub permissions: bool,
53    #[serde(default)]
54    pub mcp: bool,
55    #[serde(default, skip_serializing_if = "Vec::is_empty")]
56    pub agent_definitions: Vec<Value>,
57    #[serde(default, skip_serializing_if = "Vec::is_empty")]
58    pub mcp_servers: Vec<Value>,
59}
60