claude_rust_tools/infrastructure/
sleep_tool.rs1use claude_rust_errors::{AppError, AppResult};
2use claude_rust_types::{PermissionLevel, Tool};
3use serde_json::{Value, json};
4
5pub struct SleepTool;
6
7impl SleepTool {
8 pub fn new() -> Self {
9 Self
10 }
11}
12
13#[async_trait::async_trait]
14impl Tool for SleepTool {
15 fn name(&self) -> &str {
16 "sleep"
17 }
18
19 fn description(&self) -> &str {
20 "Sleep for a specified number of seconds (max 300)."
21 }
22
23 fn input_schema(&self) -> Value {
24 json!({
25 "type": "object",
26 "properties": {
27 "seconds": {
28 "type": "integer",
29 "description": "Number of seconds to sleep (max 300)"
30 }
31 },
32 "required": ["seconds"]
33 })
34 }
35
36 fn permission_level(&self) -> PermissionLevel {
37 PermissionLevel::ReadOnly
38 }
39
40 fn is_read_only(&self, _input: &Value) -> bool { true }
41 fn is_concurrent_safe(&self, _input: &Value) -> bool { true }
42
43 async fn execute(&self, input: Value) -> AppResult<String> {
44 let seconds = input
45 .get("seconds")
46 .and_then(|v| v.as_u64())
47 .ok_or_else(|| AppError::Tool("missing 'seconds' field".into()))?;
48
49 let seconds = seconds.min(300);
50
51 tracing::info!(seconds, "sleeping");
52
53 let start = std::time::Instant::now();
54 tokio::time::sleep(std::time::Duration::from_secs(seconds)).await;
55 let elapsed = start.elapsed();
56
57 Ok(format!("Slept for {:.2} seconds.", elapsed.as_secs_f64()))
58 }
59}