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
462
463
464
465
466
//! Slash Commands system for Claude Agent SDK
//!
//! Provides a flexible command registration and execution system.

use std::collections::HashMap;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

/// Error type for command operations
#[derive(Debug, Clone)]
pub enum CommandError {
    /// Command not found in registry
    NotFound(String),
    /// Command execution failed
    ExecutionFailed(String),
    /// Invalid command name
    InvalidName(String),
    /// Command already registered
    AlreadyRegistered(String),
}

impl fmt::Display for CommandError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CommandError::NotFound(name) => write!(f, "Command not found: {}", name),
            CommandError::ExecutionFailed(msg) => write!(f, "Command execution failed: {}", msg),
            CommandError::InvalidName(name) => write!(f, "Invalid command name: {}", name),
            CommandError::AlreadyRegistered(name) => {
                write!(f, "Command already registered: {}", name)
            }
        }
    }
}

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

/// Type alias for async command handlers
///
/// Commands receive:
/// - The command name (for handlers that serve multiple commands)
/// - Command arguments as a vector of strings
///
/// Commands return:
/// - A result containing either a String output or a CommandError
pub type CommandHandler = Arc<
    dyn Fn(&str, Vec<String>) -> Pin<Box<dyn Future<Output = Result<String, CommandError>> + Send>>
        + Send
        + Sync,
>;

/// A slash command with metadata and handler
#[derive(Clone)]
pub struct SlashCommand {
    /// Unique command name (e.g., "help", "status", "deploy")
    pub name: String,
    /// Human-readable description
    pub description: String,
    /// Async handler function
    pub handler: CommandHandler,
}

impl SlashCommand {
    /// Create a new slash command
    ///
    /// # Arguments
    /// * `name` - Unique command identifier
    /// * `description` - Human-readable description
    /// * `handler` - Async function handling command execution
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        handler: CommandHandler,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            handler,
        }
    }

    /// Validate command name
    fn validate_name(name: &str) -> Result<(), CommandError> {
        if name.is_empty() {
            return Err(CommandError::InvalidName("Command name cannot be empty".to_string()));
        }
        if name.contains(' ') {
            return Err(CommandError::InvalidName(
                "Command name cannot contain spaces".to_string(),
            ));
        }
        if !name.chars().next().unwrap().is_alphabetic() {
            return Err(CommandError::InvalidName(
                "Command name must start with a letter".to_string(),
            ));
        }
        Ok(())
    }
}

/// Registry for managing slash commands
#[derive(Default)]
pub struct CommandRegistry {
    commands: HashMap<String, SlashCommand>,
}

impl CommandRegistry {
    /// Create a new empty command registry
    pub fn new() -> Self {
        Self {
            commands: HashMap::new(),
        }
    }

    /// Register a new command
    ///
    /// # Arguments
    /// * `command` - SlashCommand to register
    ///
    /// # Returns
    /// * `Ok(())` if registration successful
    /// * `Err(CommandError)` if name is invalid or already registered
    pub fn register(&mut self, command: SlashCommand) -> Result<(), CommandError> {
        SlashCommand::validate_name(&command.name)?;

        if self.commands.contains_key(&command.name) {
            return Err(CommandError::AlreadyRegistered(command.name));
        }

        self.commands.insert(command.name.clone(), command);
        Ok(())
    }

    /// Execute a command by name
    ///
    /// # Arguments
    /// * `name` - Command name to execute
    /// * `args` - Command arguments
    ///
    /// # Returns
    /// * `Ok(String)` - Command output
    /// * `Err(CommandError)` - If command not found or execution fails
    pub async fn execute(&self, name: &str, args: Vec<String>) -> Result<String, CommandError> {
        let command = self
            .commands
            .get(name)
            .ok_or_else(|| CommandError::NotFound(name.to_string()))?;

        (command.handler)(name, args).await
    }

    /// Check if a command exists
    pub fn exists(&self, name: &str) -> bool {
        self.commands.contains_key(name)
    }

    /// Get a command by name
    pub fn get(&self, name: &str) -> Option<&SlashCommand> {
        self.commands.get(name)
    }

    /// Get all registered command names
    pub fn list_names(&self) -> Vec<String> {
        self.commands.keys().cloned().collect()
    }

    /// Get all commands
    pub fn list_all(&self) -> Vec<&SlashCommand> {
        self.commands.values().collect()
    }

    /// Get the number of registered commands
    pub fn len(&self) -> usize {
        self.commands.len()
    }

    /// Check if registry is empty
    pub fn is_empty(&self) -> bool {
        self.commands.is_empty()
    }

    /// Unregister a command
    ///
    /// # Returns
    /// * `Ok(())` if command was removed
    /// * `Err(CommandError::NotFound)` if command doesn't exist
    pub fn unregister(&mut self, name: &str) -> Result<(), CommandError> {
        self.commands
            .remove(name)
            .ok_or_else(|| CommandError::NotFound(name.to_string()))?;
        Ok(())
    }

    /// Clear all commands
    pub fn clear(&mut self) {
        self.commands.clear();
    }
}

impl fmt::Debug for SlashCommand {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SlashCommand")
            .field("name", &self.name)
            .field("description", &self.description)
            .field("handler", &"<function>")
            .finish()
    }
}

impl fmt::Debug for CommandRegistry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CommandRegistry")
            .field("commands_count", &self.commands.len())
            .field("command_names", &self.list_names())
            .finish()
    }
}

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

    // Helper function to create a test command
    fn create_test_command(name: &str, description: &str) -> SlashCommand {
        SlashCommand::new(
            name,
            description,
            Arc::new(|_name, args| {
                Box::pin(async move {
                    Ok(format!("Executed with args: {:?}", args))
                })
            }),
        )
    }

    #[test]
    fn test_registry_creation() {
        let registry = CommandRegistry::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_registry_default() {
        let registry = CommandRegistry::default();
        assert!(registry.is_empty());
    }

    #[test]
    fn test_register_command() {
        let mut registry = CommandRegistry::new();
        let cmd = create_test_command("test", "A test command");

        assert!(registry.register(cmd).is_ok());
        assert_eq!(registry.len(), 1);
        assert!(registry.exists("test"));
    }

    #[test]
    fn test_register_duplicate_fails() {
        let mut registry = CommandRegistry::new();
        let cmd1 = create_test_command("test", "First command");
        let cmd2 = create_test_command("test", "Duplicate command");

        assert!(registry.register(cmd1).is_ok());
        let result = registry.register(cmd2);
        assert!(matches!(result, Err(CommandError::AlreadyRegistered(_))));
    }

    #[test]
    fn test_invalid_name_empty() {
        let cmd = SlashCommand::new(
            "",
            "description",
            Arc::new(|_name, _args| Box::pin(async { Ok(String::new()) })),
        );

        let result = SlashCommand::validate_name(&cmd.name);
        assert!(matches!(result, Err(CommandError::InvalidName(_))));
    }

    #[test]
    fn test_invalid_name_contains_space() {
        let cmd = SlashCommand::new(
            "test command",
            "description",
            Arc::new(|_name, _args| Box::pin(async { Ok(String::new()) })),
        );

        let result = SlashCommand::validate_name(&cmd.name);
        assert!(matches!(result, Err(CommandError::InvalidName(_))));
    }

    #[test]
    fn test_invalid_name_starts_with_number() {
        let cmd = SlashCommand::new(
            "123test",
            "description",
            Arc::new(|_name, _args| Box::pin(async { Ok(String::new()) })),
        );

        let result = SlashCommand::validate_name(&cmd.name);
        assert!(matches!(result, Err(CommandError::InvalidName(_))));
    }

    #[test]
    fn test_valid_name() {
        assert!(SlashCommand::validate_name("test").is_ok());
        assert!(SlashCommand::validate_name("test_command").is_ok());
        assert!(SlashCommand::validate_name("test-command").is_ok());
        assert!(SlashCommand::validate_name("TestCommand").is_ok());
    }

    #[test]
    fn test_execute_command() {
        let mut registry = CommandRegistry::new();
        let cmd = create_test_command("echo", "Echo arguments");
        registry.register(cmd).unwrap();

        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(registry.execute("echo", vec!["hello".to_string()]));

        assert!(result.is_ok());
        assert!(result.unwrap().contains("hello"));
    }

    #[test]
    fn test_execute_nonexistent_command() {
        let registry = CommandRegistry::new();

        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(registry.execute("nonexistent", vec![]));

        assert!(matches!(result, Err(CommandError::NotFound(_))));
    }

    #[test]
    fn test_get_command() {
        let mut registry = CommandRegistry::new();
        let cmd = create_test_command("test", "A test command");
        registry.register(cmd).unwrap();

        let retrieved = registry.get("test");
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().name, "test");
    }

    #[test]
    fn test_get_nonexistent_command() {
        let registry = CommandRegistry::new();
        assert!(registry.get("nonexistent").is_none());
    }

    #[test]
    fn test_list_names() {
        let mut registry = CommandRegistry::new();
        registry.register(create_test_command("cmd1", "First")).unwrap();
        registry.register(create_test_command("cmd2", "Second")).unwrap();
        registry.register(create_test_command("cmd3", "Third")).unwrap();

        let names = registry.list_names();
        assert_eq!(names.len(), 3);
        assert!(names.contains(&"cmd1".to_string()));
        assert!(names.contains(&"cmd2".to_string()));
        assert!(names.contains(&"cmd3".to_string()));
    }

    #[test]
    fn test_list_all() {
        let mut registry = CommandRegistry::new();
        registry.register(create_test_command("cmd1", "First")).unwrap();
        registry.register(create_test_command("cmd2", "Second")).unwrap();

        let commands = registry.list_all();
        assert_eq!(commands.len(), 2);
    }

    #[test]
    fn test_unregister_command() {
        let mut registry = CommandRegistry::new();
        registry.register(create_test_command("test", "A test command")).unwrap();

        assert!(registry.unregister("test").is_ok());
        assert!(!registry.exists("test"));
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_unregister_nonexistent_command() {
        let mut registry = CommandRegistry::new();
        let result = registry.unregister("nonexistent");
        assert!(matches!(result, Err(CommandError::NotFound(_))));
    }

    #[test]
    fn test_clear_commands() {
        let mut registry = CommandRegistry::new();
        registry.register(create_test_command("cmd1", "First")).unwrap();
        registry.register(create_test_command("cmd2", "Second")).unwrap();

        registry.clear();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_command_error_display() {
        assert!(format!("{}", CommandError::NotFound("test".to_string())).contains("test"));
        assert!(format!("{}", CommandError::ExecutionFailed("error".to_string())).contains("error"));
        assert!(format!("{}", CommandError::InvalidName("bad".to_string())).contains("bad"));
        assert!(format!("{}", CommandError::AlreadyRegistered("cmd".to_string())).contains("cmd"));
    }

    #[test]
    fn test_complex_command_handler() {
        let mut registry = CommandRegistry::new();

        let cmd = SlashCommand::new(
            "sum",
            "Sum numbers",
            Arc::new(|_name, args| {
                Box::pin(async move {
                    let sum: i32 = args
                        .iter()
                        .map(|s| s.parse::<i32>().unwrap_or(0))
                        .sum();
                    Ok(format!("Sum: {}", sum))
                })
            }),
        );

        registry.register(cmd).unwrap();

        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(registry.execute(
            "sum",
            vec!["10".to_string(), "20".to_string(), "30".to_string()],
        ));

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "Sum: 60");
    }

    #[test]
    fn test_async_error_handling() {
        let mut registry = CommandRegistry::new();

        let cmd = SlashCommand::new(
            "failing",
            "Always fails",
            Arc::new(|_name, _args| {
                Box::pin(async move {
                    Err(CommandError::ExecutionFailed("Intentional failure".to_string()))
                })
            }),
        );

        registry.register(cmd).unwrap();

        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(registry.execute("failing", vec![]));

        assert!(matches!(result, Err(CommandError::ExecutionFailed(_))));
    }
}