cc-agent-sdk 0.1.7

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
//! Type definitions for Subagent system

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

/// A subagent - a specialized Claude instance with specific capabilities
///
/// # Example
///
/// ```
/// use claude_agent_sdk::subagents::Subagent;
///
/// let subagent = Subagent {
///     name: "code-reviewer".to_string(),
///     description: "Expert code reviewer".to_string(),
///     instructions: "Review code for bugs and best practices".to_string(),
///     allowed_tools: vec!["Read".to_string(), "Grep".to_string()],
///     max_turns: Some(5),
///     model: Some("claude-sonnet-4".to_string()),
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Subagent {
    /// Unique name for this subagent
    pub name: String,

    /// Description of the subagent's purpose
    pub description: String,

    /// Specific instructions for the subagent
    pub instructions: String,

    /// Tools that this subagent is allowed to use
    pub allowed_tools: Vec<String>,

    /// Maximum number of turns (None = no limit)
    pub max_turns: Option<u32>,

    /// Model to use (None = use default)
    pub model: Option<String>,
}

/// Configuration for multiple subagents
///
/// # Example
///
/// ```
/// use claude_agent_sdk::subagents::{SubagentConfig, Subagent, DelegationStrategy};
///
/// let config = SubagentConfig {
///     subagents: vec![
///         Subagent {
///             name: "reviewer".to_string(),
///             description: "Code reviewer".to_string(),
///             instructions: "Review code".to_string(),
///             allowed_tools: vec!["Read".to_string()],
///             max_turns: Some(5),
///             model: None,
///         },
///     ],
///     delegation_strategy: DelegationStrategy::Auto,
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubagentConfig {
    /// List of available subagents
    pub subagents: Vec<Subagent>,

    /// Strategy for delegating to subagents
    pub delegation_strategy: DelegationStrategy,
}

impl SubagentConfig {
    /// Create a new subagent configuration
    ///
    /// # Arguments
    ///
    /// * `delegation_strategy` - The delegation strategy to use
    ///
    /// # Example
    ///
    /// ```
    /// # use claude_agent_sdk::subagents::{DelegationStrategy, SubagentConfig};
    /// let config = SubagentConfig::new(DelegationStrategy::Auto);
    /// ```
    pub fn new(delegation_strategy: DelegationStrategy) -> Self {
        Self {
            subagents: Vec::new(),
            delegation_strategy,
        }
    }

    /// Add a subagent to the configuration
    ///
    /// # Arguments
    ///
    /// * `subagent` - The subagent to add
    ///
    /// # Example
    ///
    /// ```
    /// # use claude_agent_sdk::subagents::{SubagentConfig, Subagent, DelegationStrategy};
    /// # let mut config = SubagentConfig::new(DelegationStrategy::Auto);
    /// let subagent = Subagent {
    ///     name: "agent".to_string(),
    ///     description: "Description".to_string(),
    ///     instructions: "Instructions".to_string(),
    ///     allowed_tools: vec![],
    ///     max_turns: None,
    ///     model: None,
    /// };
    /// config.add_subagent(subagent);
    /// ```
    pub fn add_subagent(&mut self, subagent: Subagent) {
        self.subagents.push(subagent);
    }

    /// Get a subagent by name
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the subagent to retrieve
    ///
    /// # Returns
    ///
    /// `Some(subagent)` if found, `None` otherwise
    ///
    /// # Example
    ///
    /// ```
    /// # use claude_agent_sdk::subagents::{SubagentConfig, Subagent, DelegationStrategy};
    /// # let mut config = SubagentConfig::new(DelegationStrategy::Auto);
    /// # let subagent = Subagent {
    /// #     name: "agent".to_string(),
    /// #     description: "Description".to_string(),
    /// #     instructions: "Instructions".to_string(),
    /// #     allowed_tools: vec![],
    /// #     max_turns: None,
    /// #     model: None,
    /// # };
    /// # config.add_subagent(subagent);
    /// if let Some(agent) = config.get_subagent("agent") {
    ///     println!("Found agent: {}", agent.name);
    /// }
    /// ```
    pub fn get_subagent(&self, name: &str) -> Option<&Subagent> {
        self.subagents.iter().find(|s| s.name == name)
    }

    /// Convert to a HashMap for efficient lookup
    ///
    /// # Returns
    ///
    /// A HashMap mapping subagent names to subagents
    ///
    /// # Example
    ///
    /// ```
    /// # use claude_agent_sdk::subagents::{SubagentConfig, DelegationStrategy};
    /// # let config = SubagentConfig::new(DelegationStrategy::Auto);
    /// let map = config.to_map();
    /// ```
    pub fn to_map(&self) -> HashMap<String, Subagent> {
        self.subagents
            .iter()
            .map(|s| (s.name.clone(), s.clone()))
            .collect()
    }
}

/// Strategy for delegating work to subagents
///
/// # Variants
///
/// * `Auto` - Claude automatically decides when to delegate
/// * `Manual` - Requires explicit SubagentTool calls
/// * `ToolCall` - Delegate through tool calls
///
/// # Example
///
/// ```
/// use claude_agent_sdk::subagents::DelegationStrategy;
///
/// let strategy = DelegationStrategy::Auto;
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DelegationStrategy {
    /// Claude automatically decides when to delegate to subagents
    Auto,

    /// Manual delegation requires explicit SubagentTool calls
    Manual,

    /// Delegation happens through tool calls
    ToolCall,
}

/// Represents a single subagent execution call
///
/// # Example
///
/// ```
/// use claude_agent_sdk::subagents::SubagentCall;
///
/// let call = SubagentCall {
///     subagent_name: "reviewer".to_string(),
///     input: "Review this code".to_string(),
///     output: None,
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubagentCall {
    /// Name of the subagent to call
    pub subagent_name: String,

    /// Input to provide to the subagent
    pub input: String,

    /// Output from the subagent (None if not yet executed)
    pub output: Option<String>,
}

impl SubagentCall {
    /// Create a new subagent call
    ///
    /// # Arguments
    ///
    /// * `subagent_name` - Name of the subagent to call
    /// * `input` - Input for the subagent
    ///
    /// # Example
    ///
    /// ```
    /// # use claude_agent_sdk::subagents::SubagentCall;
    /// let call = SubagentCall::new("reviewer", "Review this code");
    /// ```
    pub fn new(subagent_name: impl Into<String>, input: impl Into<String>) -> Self {
        Self {
            subagent_name: subagent_name.into(),
            input: input.into(),
            output: None,
        }
    }

    /// Check if the call has been executed
    ///
    /// # Returns
    ///
    /// `true` if the call has an output, `false` otherwise
    ///
    /// # Example
    ///
    /// ```
    /// # use claude_agent_sdk::subagents::SubagentCall;
    /// let call = SubagentCall::new("agent", "input");
    /// assert!(!call.is_executed());
    /// ```
    pub fn is_executed(&self) -> bool {
        self.output.is_some()
    }
}

/// Output from a subagent execution
///
/// # Example
///
/// ```
/// use claude_agent_sdk::subagents::SubagentOutput;
///
/// let output = SubagentOutput {
///     subagent_name: "reviewer".to_string(),
///     messages: vec![],
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubagentOutput {
    /// Name of the subagent that produced this output
    pub subagent_name: String,

    /// Messages produced by the subagent
    /// Note: This is a placeholder - in a full implementation,
    /// this would contain actual Message types from the SDK
    pub messages: Vec<serde_json::Value>,
}

/// Errors that can occur in subagent operations
///
/// # Variants
///
/// * `NotFound` - Subagent not found
/// * `AlreadyExists` - Subagent with this name already exists
/// * `ExecutionFailed` - Subagent execution failed
/// * `InvalidInput` - Invalid input provided
///
/// # Example
///
/// ```
/// use claude_agent_sdk::subagents::SubagentError;
///
/// let error = SubagentError::NotFound("my-agent".to_string());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SubagentError {
    /// Subagent not found
    NotFound(String),

    /// Subagent with this name already exists
    AlreadyExists(String),

    /// Subagent execution failed
    ExecutionFailed(String),

    /// Invalid input provided
    InvalidInput(String),
}

impl std::fmt::Display for SubagentError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SubagentError::NotFound(name) => write!(f, "Subagent not found: {}", name),
            SubagentError::AlreadyExists(name) => write!(f, "Subagent already exists: {}", name),
            SubagentError::ExecutionFailed(msg) => write!(f, "Execution failed: {}", msg),
            SubagentError::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
        }
    }
}

impl std::error::Error for SubagentError {}

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

    #[test]
    fn test_subagent_creation() {
        let subagent = Subagent {
            name: "test-agent".to_string(),
            description: "Test".to_string(),
            instructions: "Instructions".to_string(),
            allowed_tools: vec!["Read".to_string()],
            max_turns: Some(5),
            model: Some("claude-sonnet-4".to_string()),
        };

        assert_eq!(subagent.name, "test-agent");
        assert_eq!(subagent.max_turns, Some(5));
    }

    #[test]
    fn test_subagent_config_new() {
        let config = SubagentConfig::new(DelegationStrategy::Auto);
        assert!(config.subagents.is_empty());
        assert_eq!(config.delegation_strategy, DelegationStrategy::Auto);
    }

    #[test]
    fn test_subagent_config_add() {
        let mut config = SubagentConfig::new(DelegationStrategy::Manual);

        let subagent = Subagent {
            name: "agent".to_string(),
            description: "Description".to_string(),
            instructions: "Instructions".to_string(),
            allowed_tools: vec![],
            max_turns: None,
            model: None,
        };

        config.add_subagent(subagent);
        assert_eq!(config.subagents.len(), 1);
    }

    #[test]
    fn test_subagent_config_get() {
        let mut config = SubagentConfig::new(DelegationStrategy::Auto);

        let subagent = Subagent {
            name: "agent".to_string(),
            description: "Description".to_string(),
            instructions: "Instructions".to_string(),
            allowed_tools: vec![],
            max_turns: None,
            model: None,
        };

        config.add_subagent(subagent);
        assert!(config.get_subagent("agent").is_some());
        assert!(config.get_subagent("nonexistent").is_none());
    }

    #[test]
    fn test_subagent_config_to_map() {
        let mut config = SubagentConfig::new(DelegationStrategy::ToolCall);

        config.add_subagent(Subagent {
            name: "agent1".to_string(),
            description: "Agent 1".to_string(),
            instructions: "Instructions 1".to_string(),
            allowed_tools: vec![],
            max_turns: None,
            model: None,
        });

        config.add_subagent(Subagent {
            name: "agent2".to_string(),
            description: "Agent 2".to_string(),
            instructions: "Instructions 2".to_string(),
            allowed_tools: vec![],
            max_turns: None,
            model: None,
        });

        let map = config.to_map();
        assert_eq!(map.len(), 2);
        assert!(map.contains_key("agent1"));
        assert!(map.contains_key("agent2"));
    }

    #[test]
    fn test_delegation_strategy_equality() {
        assert_eq!(DelegationStrategy::Auto, DelegationStrategy::Auto);
        assert_ne!(DelegationStrategy::Auto, DelegationStrategy::Manual);
    }

    #[test]
    fn test_subagent_call_new() {
        let call = SubagentCall::new("agent", "input");
        assert_eq!(call.subagent_name, "agent");
        assert_eq!(call.input, "input");
        assert!(!call.is_executed());
    }

    #[test]
    fn test_subagent_call_executed() {
        let mut call = SubagentCall::new("agent", "input");
        assert!(!call.is_executed());

        call.output = Some("output".to_string());
        assert!(call.is_executed());
    }

    #[test]
    fn test_subagent_error_display() {
        let error = SubagentError::NotFound("agent".to_string());
        assert_eq!(format!("{}", error), "Subagent not found: agent");

        let error = SubagentError::AlreadyExists("agent".to_string());
        assert_eq!(format!("{}", error), "Subagent already exists: agent");
    }

    #[test]
    fn test_subagent_output() {
        let output = SubagentOutput {
            subagent_name: "agent".to_string(),
            messages: vec![],
        };

        assert_eq!(output.subagent_name, "agent");
        assert!(output.messages.is_empty());
    }
}