Skip to main content

claude_code_rs/types/
options.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3
4use super::agents::AgentDefinition;
5use super::hooks::HookDefinition;
6use super::mcp_config::McpServerConfig;
7use super::permissions::{CanUseToolCallback, PermissionMode};
8use super::sandbox::SandboxSettings;
9use crate::transport::cli_discovery;
10
11/// Configuration options for a Claude Agent SDK query or client.
12///
13/// All fields are public with sensible defaults. Use `..Default::default()` for
14/// fields you don't need to set.
15#[derive(Default)]
16pub struct ClaudeAgentOptions {
17    // --- Core ---
18    /// The prompt/message to send. Can be set here or passed to query().
19    pub prompt: Option<String>,
20
21    /// Model to use (e.g. "claude-sonnet-4-20250514").
22    pub model: Option<String>,
23
24    /// System prompt override.
25    pub system_prompt: Option<String>,
26
27    /// Append system prompt (added after default).
28    pub append_system_prompt: Option<String>,
29
30    /// Maximum turns (agentic loops) before stopping.
31    pub max_turns: Option<u32>,
32
33    /// Maximum tokens in the response.
34    pub max_tokens: Option<u32>,
35
36    // --- Session ---
37    /// Resume an existing session by ID.
38    pub session_id: Option<String>,
39
40    /// Continue the most recent session.
41    pub continue_session: bool,
42
43    // --- Working directory ---
44    /// Working directory for the CLI process.
45    pub cwd: Option<PathBuf>,
46
47    // --- Permission ---
48    /// Permission mode for tool usage.
49    pub permission_mode: PermissionMode,
50
51    /// Specific tools to allow (when using AllowedTools mode).
52    pub allowed_tools: Vec<String>,
53
54    /// Custom permission callback.
55    pub can_use_tool: Option<CanUseToolCallback>,
56
57    // --- Hooks ---
58    /// Registered hook definitions.
59    pub hooks: Vec<HookDefinition>,
60
61    // --- MCP ---
62    /// MCP servers to register with the CLI.
63    pub mcp_servers: HashMap<String, McpServerConfig>,
64
65    // --- Agents ---
66    /// Sub-agent definitions.
67    pub agents: Vec<AgentDefinition>,
68
69    // --- Sandbox ---
70    /// Sandbox configuration.
71    pub sandbox: Option<SandboxSettings>,
72
73    // --- CLI flags ---
74    /// Additional environment variables for the CLI process.
75    pub env: HashMap<String, String>,
76
77    /// Verbose output from CLI.
78    pub verbose: bool,
79
80    /// Path to the claude CLI binary (auto-detected if None).
81    pub cli_path: Option<PathBuf>,
82
83    /// Custom CLI arguments (appended after built-in ones).
84    pub extra_cli_args: Vec<String>,
85
86    /// Timeout for the initial connection handshake.
87    pub connect_timeout: Option<std::time::Duration>,
88
89    /// Timeout for control protocol requests.
90    pub control_timeout: Option<std::time::Duration>,
91
92    /// Stderr callback - receives stderr lines from CLI process.
93    pub on_stderr: Option<StderrCallback>,
94
95    /// Disallow use of the prompt cache
96    pub no_cache: bool,
97
98    /// Temperature setting
99    pub temperature: Option<f64>,
100
101    /// Context window fraction (0.0-1.0) to use before summarizing.
102    pub context_window: Option<f64>,
103}
104
105/// Callback for CLI stderr lines.
106pub type StderrCallback =
107    std::sync::Arc<dyn Fn(String) + Send + Sync>;
108
109impl ClaudeAgentOptions {
110    /// Resolve the CLI path: use the configured one or auto-detect.
111    pub fn resolve_cli_path(&self) -> crate::error::Result<PathBuf> {
112        match self.cli_path {
113            Some(ref p) => Ok(p.clone()),
114            None => cli_discovery::find_cli(),
115        }
116    }
117}
118
119impl std::fmt::Debug for ClaudeAgentOptions {
120    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121        f.debug_struct("ClaudeAgentOptions")
122            .field("model", &self.model)
123            .field("prompt", &self.prompt.as_ref().map(|p| {
124                if p.len() > 50 { format!("{}...", &p[..50]) } else { p.clone() }
125            }))
126            .field("max_turns", &self.max_turns)
127            .field("session_id", &self.session_id)
128            .field("permission_mode", &self.permission_mode)
129            .field("verbose", &self.verbose)
130            .field("hooks_count", &self.hooks.len())
131            .field("mcp_servers_count", &self.mcp_servers.len())
132            .field("agents_count", &self.agents.len())
133            .finish_non_exhaustive()
134    }
135}