ai_agent/tools/
sleep_tool.rs1use crate::error::AgentError;
3use crate::types::*;
4
5pub const SLEEP_TOOL_NAME: &str = "Sleep";
6
7pub const DESCRIPTION: &str = "Wait for a specified duration";
8
9pub struct SleepTool;
11
12impl SleepTool {
13 pub fn new() -> Self {
14 Self
15 }
16
17 pub fn name(&self) -> &str {
18 SLEEP_TOOL_NAME
19 }
20
21 pub fn description(&self) -> &str {
22 DESCRIPTION
23 }
24
25 pub fn user_facing_name(&self, _input: Option<&serde_json::Value>) -> String {
26 "Sleep".to_string()
27 }
28
29 pub fn get_tool_use_summary(&self, input: Option<&serde_json::Value>) -> Option<String> {
30 input.and_then(|inp| inp["duration"].as_f64().map(|d| format!("{:.1}s", d)))
31 }
32
33 pub fn render_tool_result_message(
34 &self,
35 content: &serde_json::Value,
36 ) -> Option<String> {
37 content["content"].as_str().map(|s| s.to_string())
38 }
39
40 pub fn input_schema(&self) -> ToolInputSchema {
41 ToolInputSchema {
42 schema_type: "object".to_string(),
43 properties: serde_json::json!({
44 "duration": {
45 "type": "number",
46 "description": "Duration to sleep in seconds (default: 60)"
47 }
48 }),
49 required: None,
50 }
51 }
52
53 pub async fn execute(
54 &self,
55 input: serde_json::Value,
56 _context: &ToolContext,
57 ) -> Result<ToolResult, AgentError> {
58 let duration_secs = input["duration"].as_f64().unwrap_or(60.0);
59 let duration_ms = (duration_secs * 1000.0) as u64;
60
61 tokio::time::sleep(std::time::Duration::from_millis(duration_ms)).await;
63
64 Ok(ToolResult {
65 result_type: "text".to_string(),
66 tool_use_id: "".to_string(),
67 content: format!("Slept for {:.1} seconds", duration_secs),
68 is_error: None,
69 was_persisted: None,
70 })
71 }
72}
73
74impl Default for SleepTool {
75 fn default() -> Self {
76 Self::new()
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn test_sleep_tool_name() {
86 let tool = SleepTool::new();
87 assert_eq!(tool.name(), SLEEP_TOOL_NAME);
88 }
89
90 #[test]
91 fn test_sleep_tool_schema() {
92 let tool = SleepTool::new();
93 let schema = tool.input_schema();
94 assert_eq!(schema.schema_type, "object");
95 assert!(schema.properties["duration"].is_object());
96 }
97}