Skip to main content

claude_code_sdk_rust/
options.rs

1use crate::types::*;
2use std::collections::HashMap;
3
4/// Builder for ClaudeAgentOptions
5#[derive(Debug, Default)]
6pub struct ClaudeAgentOptionsBuilder {
7    options: ClaudeAgentOptions,
8}
9
10impl ClaudeAgentOptionsBuilder {
11    pub fn new() -> Self {
12        Self::default()
13    }
14
15    pub fn tools(mut self, tools: Vec<String>) -> Self {
16        self.options.tools = tools;
17        self.options.tools_set = true;
18        self
19    }
20
21    pub fn tools_preset(mut self, preset: ToolsPreset) -> Self {
22        self.options.tools_preset = Some(preset);
23        self.options.tools_set = false;
24        self
25    }
26
27    pub fn allowed_tools(mut self, tools: Vec<String>) -> Self {
28        self.options.allowed_tools = tools;
29        self
30    }
31
32    pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
33        self.options.system_prompt = Some(prompt.into());
34        self
35    }
36
37    pub fn system_prompt_preset(mut self, preset: SystemPromptPreset) -> Self {
38        self.options.system_prompt_preset = Some(preset);
39        self
40    }
41
42    pub fn system_prompt_file(mut self, file: SystemPromptFile) -> Self {
43        self.options.system_prompt_file = Some(file);
44        self
45    }
46
47    pub fn mcp_server(mut self, name: impl Into<String>, config: MCPServerConfig) -> Self {
48        self.options.mcp_servers.insert(name.into(), config);
49        self
50    }
51
52    pub fn sdk_mcp_server(
53        mut self,
54        name: impl Into<String>,
55        server: crate::mcp::SimpleMCPServer,
56    ) -> Self {
57        let name = name.into();
58        self.options
59            .mcp_servers
60            .insert(name.clone(), MCPServerConfig::Sdk { name: name.clone() });
61        self.options.sdk_mcp_servers.insert(name, server);
62        self
63    }
64
65    pub fn mcp_servers(mut self, servers: HashMap<String, MCPServerConfig>) -> Self {
66        self.options.mcp_servers = servers;
67        self.options.mcp_servers_config = None;
68        self
69    }
70
71    pub fn mcp_servers_config(mut self, config: impl Into<String>) -> Self {
72        self.options.mcp_servers.clear();
73        self.options.mcp_servers_config = Some(config.into());
74        self
75    }
76
77    pub fn permission_mode(mut self, mode: PermissionMode) -> Self {
78        self.options.permission_mode = Some(mode);
79        self
80    }
81
82    pub fn continue_conversation(mut self, continue_conv: bool) -> Self {
83        self.options.continue_conversation = continue_conv;
84        self
85    }
86
87    pub fn resume(mut self, session_id: impl Into<String>) -> Self {
88        self.options.resume = Some(session_id.into());
89        self
90    }
91
92    pub fn session_id(mut self, session_id: impl Into<String>) -> Self {
93        self.options.session_id = Some(session_id.into());
94        self
95    }
96
97    pub fn fork_session(mut self, fork: bool) -> Self {
98        self.options.fork_session = fork;
99        self
100    }
101
102    pub fn max_turns(mut self, turns: i32) -> Self {
103        self.options.max_turns = Some(turns);
104        self
105    }
106
107    pub fn max_budget_usd(mut self, budget: f64) -> Self {
108        self.options.max_budget_usd = Some(budget);
109        self
110    }
111
112    pub fn task_budget(mut self, budget: TaskBudget) -> Self {
113        self.options.task_budget = Some(budget);
114        self
115    }
116
117    pub fn task_budget_total(mut self, total: i32) -> Self {
118        self.options.task_budget = Some(TaskBudget { total });
119        self
120    }
121
122    pub fn disallowed_tools(mut self, tools: Vec<String>) -> Self {
123        self.options.disallowed_tools = tools;
124        self
125    }
126
127    pub fn model(mut self, model: impl Into<String>) -> Self {
128        self.options.model = Some(model.into());
129        self
130    }
131
132    pub fn fallback_model(mut self, model: impl Into<String>) -> Self {
133        self.options.fallback_model = Some(model.into());
134        self
135    }
136
137    pub fn betas(mut self, betas: Vec<SdkBeta>) -> Self {
138        self.options.betas = betas;
139        self
140    }
141
142    pub fn add_beta(mut self, beta: SdkBeta) -> Self {
143        self.options.betas.push(beta);
144        self
145    }
146
147    pub fn permission_prompt_tool_name(mut self, name: impl Into<String>) -> Self {
148        self.options.permission_prompt_tool_name = Some(name.into());
149        self
150    }
151
152    pub fn can_use_tool<F, Fut>(mut self, callback: F) -> Self
153    where
154        F: Fn(String, serde_json::Map<String, serde_json::Value>, ToolPermissionContext) -> Fut
155            + Send
156            + Sync
157            + 'static,
158        Fut: std::future::Future<Output = crate::error::Result<PermissionResult>> + Send + 'static,
159    {
160        self.options.can_use_tool = Some(CanUseToolCallback::new(callback));
161        self
162    }
163
164    pub fn hook(mut self, event: impl Into<String>, matcher: HookMatcher) -> Self {
165        self.options
166            .hooks
167            .entry(event.into())
168            .or_default()
169            .push(matcher);
170        self
171    }
172
173    pub fn hooks(mut self, hooks: HookMap) -> Self {
174        self.options.hooks = hooks;
175        self
176    }
177
178    pub fn agent(mut self, name: impl Into<String>, agent: AgentDefinition) -> Self {
179        self.options.agents.insert(name.into(), agent);
180        self
181    }
182
183    pub fn agents(mut self, agents: HashMap<String, AgentDefinition>) -> Self {
184        self.options.agents = agents;
185        self
186    }
187
188    pub fn cwd(mut self, cwd: impl Into<String>) -> Self {
189        self.options.cwd = Some(cwd.into());
190        self
191    }
192
193    pub fn cli_path(mut self, path: impl Into<String>) -> Self {
194        self.options.cli_path = Some(path.into());
195        self
196    }
197
198    pub fn settings(mut self, settings: impl Into<String>) -> Self {
199        self.options.settings = Some(settings.into());
200        self
201    }
202
203    pub fn add_dir(mut self, dir: impl Into<String>) -> Self {
204        self.options.add_dirs.push(dir.into());
205        self
206    }
207
208    pub fn add_dirs(mut self, dirs: Vec<String>) -> Self {
209        self.options.add_dirs = dirs;
210        self
211    }
212
213    pub fn env(mut self, env: HashMap<String, String>) -> Self {
214        self.options.env = env;
215        self
216    }
217
218    pub fn env_var(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
219        self.options.env.insert(key.into(), value.into());
220        self
221    }
222
223    pub fn extra_arg(mut self, key: impl Into<String>, value: Option<String>) -> Self {
224        self.options.extra_args.insert(key.into(), value);
225        self
226    }
227
228    pub fn max_buffer_size(mut self, size: usize) -> Self {
229        self.options.max_buffer_size = Some(size);
230        self
231    }
232
233    pub fn user(mut self, user: impl Into<String>) -> Self {
234        self.options.user = Some(user.into());
235        self
236    }
237
238    pub fn include_partial_messages(mut self, include: bool) -> Self {
239        self.options.include_partial_messages = include;
240        self
241    }
242
243    pub fn include_hook_events(mut self, include: bool) -> Self {
244        self.options.include_hook_events = include;
245        self
246    }
247
248    pub fn strict_mcp_config(mut self, strict: bool) -> Self {
249        self.options.strict_mcp_config = strict;
250        self
251    }
252
253    pub fn setting_sources(mut self, sources: Vec<SettingSource>) -> Self {
254        self.options.setting_sources = Some(sources);
255        self
256    }
257
258    pub fn add_setting_source(mut self, source: SettingSource) -> Self {
259        self.options
260            .setting_sources
261            .get_or_insert_with(Vec::new)
262            .push(source);
263        self
264    }
265
266    pub fn skills_all(mut self) -> Self {
267        self.options.skills = Some(SkillsConfig::All);
268        self
269    }
270
271    pub fn skills(mut self, skills: Vec<String>) -> Self {
272        self.options.skills = Some(SkillsConfig::Names(skills));
273        self
274    }
275
276    pub fn sandbox(mut self, sandbox: SandboxSettings) -> Self {
277        self.options.sandbox = Some(sandbox);
278        self
279    }
280
281    pub fn plugin(mut self, plugin: SDKPluginConfig) -> Self {
282        self.options.plugins.push(plugin);
283        self
284    }
285
286    pub fn plugins(mut self, plugins: Vec<SDKPluginConfig>) -> Self {
287        self.options.plugins = plugins;
288        self
289    }
290
291    pub fn max_thinking_tokens(mut self, tokens: i32) -> Self {
292        self.options.max_thinking_tokens = Some(tokens);
293        self
294    }
295
296    pub fn thinking(mut self, thinking: ThinkingConfig) -> Self {
297        self.options.thinking = Some(thinking);
298        self
299    }
300
301    pub fn effort(mut self, effort: impl Into<String>) -> Self {
302        self.options.effort = Some(effort.into());
303        self
304    }
305
306    pub fn output_format(mut self, format: serde_json::Map<String, serde_json::Value>) -> Self {
307        self.options.output_format = Some(format);
308        self
309    }
310
311    pub fn enable_file_checkpointing(mut self, enable: bool) -> Self {
312        self.options.enable_file_checkpointing = enable;
313        self
314    }
315
316    pub fn stderr<F>(mut self, callback: F) -> Self
317    where
318        F: Fn(String) + Send + Sync + 'static,
319    {
320        self.options.stderr = Some(StderrCallback::new(callback));
321        self
322    }
323
324    pub fn session_store<S>(mut self, store: S) -> Self
325    where
326        S: crate::session_store::SessionStore + 'static,
327    {
328        self.options.session_store = Some(crate::session_store::SessionStoreHandle::new(store));
329        self
330    }
331
332    pub fn session_store_handle(mut self, store: crate::session_store::SessionStoreHandle) -> Self {
333        self.options.session_store = Some(store);
334        self
335    }
336
337    pub fn session_store_flush(mut self, mode: SessionStoreFlushMode) -> Self {
338        self.options.session_store_flush = mode;
339        self
340    }
341
342    pub fn load_timeout_ms(mut self, timeout_ms: i32) -> Self {
343        self.options.load_timeout_ms = timeout_ms;
344        self
345    }
346
347    pub fn build(self) -> ClaudeAgentOptions {
348        self.options
349    }
350}
351
352/// Extension trait for ClaudeAgentOptions to add builder method
353impl ClaudeAgentOptions {
354    pub fn builder() -> ClaudeAgentOptionsBuilder {
355        ClaudeAgentOptionsBuilder::new()
356    }
357}
358
359/// Options for listing sessions
360#[derive(Debug, Clone, Default)]
361pub struct ListSessionsOptions {
362    pub directory: Option<String>,
363    pub limit: Option<usize>,
364    pub include_worktrees: Option<bool>,
365}
366
367impl ListSessionsOptions {
368    pub fn new() -> Self {
369        Self::default()
370    }
371
372    pub fn directory(mut self, dir: impl Into<String>) -> Self {
373        self.directory = Some(dir.into());
374        self
375    }
376
377    pub fn limit(mut self, limit: usize) -> Self {
378        self.limit = Some(limit);
379        self
380    }
381
382    pub fn include_worktrees(mut self, include: bool) -> Self {
383        self.include_worktrees = Some(include);
384        self
385    }
386}
387
388/// Options for querying session data
389#[derive(Debug, Clone, Default)]
390pub struct SessionQueryOptions {
391    pub directory: Option<String>,
392    pub limit: Option<usize>,
393    pub offset: Option<usize>,
394}
395
396impl SessionQueryOptions {
397    pub fn new() -> Self {
398        Self::default()
399    }
400
401    pub fn directory(mut self, dir: impl Into<String>) -> Self {
402        self.directory = Some(dir.into());
403        self
404    }
405
406    pub fn limit(mut self, limit: usize) -> Self {
407        self.limit = Some(limit);
408        self
409    }
410
411    pub fn offset(mut self, offset: usize) -> Self {
412        self.offset = Some(offset);
413        self
414    }
415}
416
417/// Options for session mutations
418#[derive(Debug, Clone, Default)]
419pub struct SessionMutationOptions {
420    pub directory: Option<String>,
421}
422
423impl SessionMutationOptions {
424    pub fn new() -> Self {
425        Self::default()
426    }
427
428    pub fn directory(mut self, dir: impl Into<String>) -> Self {
429        self.directory = Some(dir.into());
430        self
431    }
432}