claude-agent 0.2.25

Rust SDK for building AI agents with Anthropic's Claude - Direct API, no CLI dependency
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Built-in Tools Unit Test
//!
//! Direct verification of all 14 built-in tools without API calls:
//! - File: Read, Write, Edit, Glob, Grep
//! - Process: Bash, KillShell
//! - Session: TodoWrite, Plan
//! - Subagent: Task, TaskOutput (via TaskRegistry)
//! - Skills: SkillRegistry, SkillExecutor
//!
//! Run: cargo run --example all_tools_test

use claude_agent::ToolOutput;
use claude_agent::agent::{AgentMetrics, AgentState, TaskOutputTool, TaskRegistry};
use claude_agent::common::{ContentSource, IndexRegistry};
use claude_agent::security::SecurityContext;
use claude_agent::session::{MemoryPersistence, SessionId, SessionState, ToolState};
use claude_agent::skills::{SkillExecutor, SkillIndex};
use claude_agent::tools::{
    BashTool, EditTool, ExecutionContext, GlobTool, GrepTool, KillShellTool, PlanTool,
    ProcessManager, ReadTool, TodoWriteTool, Tool, WriteTool,
};
use claude_agent::types::{StopReason, Usage};
use std::sync::Arc;

struct TestRunner {
    passed: usize,
    failed: usize,
}

impl TestRunner {
    fn new() -> Self {
        Self {
            passed: 0,
            failed: 0,
        }
    }

    fn check(&mut self, name: &str, result: Result<(), String>) {
        match result {
            Ok(()) => {
                println!("  [PASS] {}", name);
                self.passed += 1;
            }
            Err(e) => {
                println!("  [FAIL] {} - {}", name, e);
                self.failed += 1;
            }
        }
    }

    fn summary(&self) -> bool {
        println!("\n------------------------------------------------------------------------");
        println!("  Result: {} passed, {} failed", self.passed, self.failed);
        println!("------------------------------------------------------------------------");
        self.failed == 0
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("========================================================================");
    println!("                  Built-in Tools Unit Test                              ");
    println!("========================================================================\n");

    let temp_dir = tempfile::tempdir()?;
    let working_dir = temp_dir.path().to_path_buf();

    let security = SecurityContext::builder()
        .root(&working_dir)
        .build()
        .unwrap_or_else(|_| SecurityContext::permissive());
    let ctx = ExecutionContext::new(security);

    let session_id = SessionId::new();
    let session_ctx = ToolState::new(session_id);
    let process_manager = Arc::new(ProcessManager::new());

    let mut runner = TestRunner::new();

    // =========================================================================
    // Section 1: File Tools
    // =========================================================================
    println!("Section 1: File Tools");
    println!("------------------------------------------------------------------------");

    let write = WriteTool;
    let test_file = working_dir.join("test.txt");
    let result = write
        .execute(
            serde_json::json!({
                "file_path": test_file.to_str().unwrap(),
                "content": "Hello World\nLine 2\nLine 3"
            }),
            &ctx,
        )
        .await;
    runner.check(
        "Write",
        match &result.output {
            ToolOutput::Success(_) | ToolOutput::Empty => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let read = ReadTool;
    let result = read
        .execute(
            serde_json::json!({"file_path": test_file.to_str().unwrap()}),
            &ctx,
        )
        .await;
    runner.check(
        "Read",
        match &result.output {
            ToolOutput::Success(s) if s.contains("Hello World") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let result = read
        .execute(
            serde_json::json!({
                "file_path": test_file.to_str().unwrap(),
                "offset": 1,
                "limit": 2
            }),
            &ctx,
        )
        .await;
    runner.check(
        "Read (offset/limit)",
        match &result.output {
            ToolOutput::Success(s) if s.contains("Line 2") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let edit = EditTool;
    let result = edit
        .execute(
            serde_json::json!({
                "file_path": test_file.to_str().unwrap(),
                "old_string": "World",
                "new_string": "Rust"
            }),
            &ctx,
        )
        .await;
    let edited = tokio::fs::read_to_string(&test_file).await?;
    runner.check(
        "Edit",
        match &result.output {
            ToolOutput::Success(_) | ToolOutput::Empty if edited.contains("Hello Rust") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    tokio::fs::write(
        working_dir.join("file1.rs"),
        "fn main() { println!(\"hello\"); }",
    )
    .await?;
    tokio::fs::write(working_dir.join("file2.rs"), "fn test() { assert!(true); }").await?;
    tokio::fs::write(working_dir.join("readme.md"), "# README").await?;

    let glob = GlobTool;
    let result = glob
        .execute(
            serde_json::json!({"pattern": "*.rs", "path": working_dir.to_str().unwrap()}),
            &ctx,
        )
        .await;
    runner.check(
        "Glob",
        match &result.output {
            ToolOutput::Success(s) if s.contains("file1.rs") && s.contains("file2.rs") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let grep = GrepTool;
    let result = grep
        .execute(
            serde_json::json!({"pattern": "fn main", "path": working_dir.to_str().unwrap()}),
            &ctx,
        )
        .await;
    runner.check(
        "Grep",
        match &result.output {
            ToolOutput::Success(s) if s.contains("file1.rs") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    // =========================================================================
    // Section 2: Process Tools
    // =========================================================================
    println!("\nSection 2: Process Tools");
    println!("------------------------------------------------------------------------");

    let bash = BashTool::process_manager(process_manager.clone());
    let result = bash
        .execute(
            serde_json::json!({"command": "echo 'Hello from Bash'"}),
            &ctx,
        )
        .await;
    runner.check(
        "Bash",
        match &result.output {
            ToolOutput::Success(s) if s.contains("Hello from Bash") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let result = bash
        .execute(
            serde_json::json!({"command": "sleep 0.01 && echo done", "timeout": 5000}),
            &ctx,
        )
        .await;
    runner.check(
        "Bash (timeout)",
        match &result.output {
            ToolOutput::Success(s) if s.contains("done") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let result = bash
        .execute(
            serde_json::json!({
                "command": "sleep 0.1 && echo 'background done'",
                "run_in_background": true
            }),
            &ctx,
        )
        .await;
    runner.check(
        "Bash (background)",
        match &result.output {
            ToolOutput::Success(s) if s.contains("Background") || s.contains("process") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let kill = KillShellTool::process_manager(process_manager.clone());
    let result = kill
        .execute(serde_json::json!({"shell_id": "nonexistent_12345"}), &ctx)
        .await;
    runner.check(
        "KillShell (error)",
        match &result.output {
            ToolOutput::Error(e)
                if e.to_string().contains("not found") || e.to_string().contains("No process") =>
            {
                Ok(())
            }
            _ => Err(format!("Expected error: {:?}", result)),
        },
    );

    // =========================================================================
    // Section 3: Session Tools
    // =========================================================================
    println!("\nSection 3: Session Tools");
    println!("------------------------------------------------------------------------");

    let todo = TodoWriteTool::new(session_ctx.clone(), session_id);
    let result = todo
        .execute(
            serde_json::json!({
                "todos": [
                    {"content": "Task A", "status": "pending", "activeForm": "Working on A"},
                    {"content": "Task B", "status": "in_progress", "activeForm": "Working on B"},
                    {"content": "Task C", "status": "completed", "activeForm": "Done C"}
                ]
            }),
            &ctx,
        )
        .await;
    runner.check(
        "TodoWrite",
        match &result.output {
            ToolOutput::Success(s) if s.contains("Task") || s.contains("todo") => Ok(()),
            ToolOutput::Empty => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let plan = PlanTool::new(session_ctx.clone());

    let result = plan
        .execute(
            serde_json::json!({"action": "start", "name": "Test Plan"}),
            &ctx,
        )
        .await;
    runner.check(
        "Plan (start)",
        match &result.output {
            ToolOutput::Success(s) if s.contains("Plan mode started") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let result = plan
        .execute(
            serde_json::json!({"action": "update", "content": "Step 1: Analyze\nStep 2: Implement"}),
            &ctx,
        )
        .await;
    runner.check(
        "Plan (update)",
        match &result.output {
            ToolOutput::Success(s) if s.contains("Plan content updated") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let result = plan
        .execute(serde_json::json!({"action": "status"}), &ctx)
        .await;
    runner.check(
        "Plan (status)",
        match &result.output {
            ToolOutput::Success(s) if s.contains("Step 1") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    let result = plan
        .execute(serde_json::json!({"action": "complete"}), &ctx)
        .await;
    runner.check(
        "Plan (complete)",
        match &result.output {
            ToolOutput::Success(s) if s.contains("Plan completed") => Ok(()),
            _ => Err(format!("{:?}", result)),
        },
    );

    // =========================================================================
    // Section 4: Skills
    // =========================================================================
    println!("\nSection 4: Skills");
    println!("------------------------------------------------------------------------");

    let mut skill_registry = IndexRegistry::<SkillIndex>::new();
    skill_registry.register(
        SkillIndex::new("calculator", "Math calculator")
            .source(ContentSource::in_memory("Calculate: $ARGUMENTS"))
            .triggers(["calculate", "math"]),
    );
    skill_registry.register(
        SkillIndex::new("greeter", "Greeting generator")
            .source(ContentSource::in_memory(
                "Generate greeting for: $ARGUMENTS",
            ))
            .triggers(["greet"]),
    );

    let executor = SkillExecutor::new(skill_registry);

    runner.check("SkillRegistry", {
        if executor.has_skill("calculator") && executor.has_skill("greeter") {
            Ok(())
        } else {
            Err("Skills not registered".into())
        }
    });

    let calc_result = executor.execute("calculator", Some("15 * 4")).await;
    runner.check("SkillExecutor", {
        if calc_result.success {
            Ok(())
        } else {
            Err("Skill execution failed".into())
        }
    });

    let triggered = executor
        .execute_by_trigger("please calculate 100 / 5")
        .await;
    runner.check("SkillExecutor (trigger)", {
        if triggered.is_some() {
            Ok(())
        } else {
            Err("Trigger activation failed".into())
        }
    });

    // =========================================================================
    // Section 5: Task Registry
    // =========================================================================
    println!("\nSection 5: Task Registry");
    println!("------------------------------------------------------------------------");

    let persistence = Arc::new(MemoryPersistence::new());
    let task_registry = TaskRegistry::new(persistence.clone());

    let task_id = uuid::Uuid::new_v4().to_string();
    let _cancel_rx = task_registry
        .register(
            task_id.clone(),
            "explore".to_string(),
            "Test task".to_string(),
        )
        .await;
    runner.check("TaskRegistry (register)", {
        let status = task_registry.get_status(&task_id).await;
        if status == Some(SessionState::Active) {
            Ok(())
        } else {
            Err(format!("Expected Active, got {:?}", status))
        }
    });

    let complete_id = uuid::Uuid::new_v4().to_string();
    drop(
        task_registry
            .register(
                complete_id.clone(),
                "general".to_string(),
                "Complete test".to_string(),
            )
            .await,
    );
    let result = claude_agent::AgentResult {
        text: "Task completed".to_string(),
        messages: vec![],
        tool_calls: 0,
        iterations: 1,
        stop_reason: StopReason::EndTurn,
        usage: Usage::default(),
        metrics: AgentMetrics::default(),
        state: AgentState::Completed,
        session_id: complete_id.clone(),
        structured_output: None,
        uuid: uuid::Uuid::new_v4().to_string(),
    };
    task_registry.complete(&complete_id, result).await;
    runner.check("TaskRegistry (complete)", {
        let status = task_registry.get_status(&complete_id).await;
        if status == Some(SessionState::Completed) {
            Ok(())
        } else {
            Err(format!("Expected Completed, got {:?}", status))
        }
    });

    let fail_id = uuid::Uuid::new_v4().to_string();
    drop(
        task_registry
            .register(fail_id.clone(), "plan".to_string(), "Fail test".to_string())
            .await,
    );
    task_registry
        .fail(&fail_id, "Simulated error".to_string())
        .await;
    runner.check("TaskRegistry (fail)", {
        let status = task_registry.get_status(&fail_id).await;
        if status == Some(SessionState::Failed) {
            Ok(())
        } else {
            Err(format!("Expected Failed, got {:?}", status))
        }
    });

    let cancel_id = uuid::Uuid::new_v4().to_string();
    drop(
        task_registry
            .register(
                cancel_id.clone(),
                "explore".to_string(),
                "Cancel test".to_string(),
            )
            .await,
    );
    let cancelled = task_registry.cancel(&cancel_id).await;
    runner.check("TaskRegistry (cancel)", {
        if cancelled {
            let status = task_registry.get_status(&cancel_id).await;
            if status == Some(SessionState::Cancelled) {
                Ok(())
            } else {
                Err(format!("Expected Cancelled, got {:?}", status))
            }
        } else {
            Err("Cancel returned false".into())
        }
    });

    runner.check("TaskOutputTool (schema)", {
        let output_tool = TaskOutputTool::new(task_registry.clone());
        let schema = output_tool.input_schema().to_string();
        if schema.contains("task_id") && schema.contains("block") && schema.contains("timeout") {
            Ok(())
        } else {
            Err("Missing required fields".into())
        }
    });

    // =========================================================================
    // Summary
    // =========================================================================
    let success = runner.summary();

    if success {
        println!("========================================================================");
        println!("                    All tests passed!                                   ");
        println!("========================================================================");
    } else {
        println!("========================================================================");
        println!("                    Some tests failed!                                  ");
        println!("========================================================================");
        std::process::exit(1);
    }

    Ok(())
}