Skip to main content

rucora_tools/basic/
echo.rs

1//! Echo 工具模块。
2//!
3//! 提供简单的回显功能,用于测试和调试。
4
5use async_trait::async_trait;
6use rucora_core::{
7    error::ToolError,
8    tool::{Tool, ToolCategory},
9};
10use serde_json::{Value, json};
11
12/// Echo 工具:原样返回输入。
13///
14/// 这是最简单的工具实现,用于测试和调试工具调用流程。
15/// 它不会对输入进行任何处理,直接返回原始数据。
16///
17/// 适用场景:
18/// - 测试工具调用机制
19/// - 调试数据传递流程
20/// - 作为其他工具实现的参考模板
21///
22/// 输入格式:
23/// ```json
24/// {
25///   "text": "要回显的文本"
26/// }
27/// ```
28pub struct EchoTool;
29
30#[async_trait]
31impl Tool for EchoTool {
32    /// 返回工具名称。
33    fn name(&self) -> &str {
34        "echo"
35    }
36
37    /// 返回工具描述。
38    fn description(&self) -> Option<&str> {
39        Some("回显输入参数,用于测试和调试")
40    }
41
42    /// 返回工具分类。
43    fn categories(&self) -> &'static [ToolCategory] {
44        &[ToolCategory::Basic]
45    }
46
47    /// 返回输入参数的 JSON Schema。
48    fn input_schema(&self) -> Value {
49        json!({
50            "type": "object",
51            "properties": {
52                "text": {
53                    "type": "string",
54                    "description": "要回显的文本内容"
55                }
56            },
57            "required": ["text"]
58        })
59    }
60
61    /// 执行工具的核心逻辑。
62    async fn call(&self, input: Value) -> Result<Value, ToolError> {
63        // 验证输入是否包含必需的字段
64        if input.get("text").is_none() {
65            return Err(ToolError::Message("缺少必需的 'text' 字段".to_string()));
66        }
67
68        Ok(input)
69    }
70}