فهرست منبع

【新增】添加 Question 工具并优化现有工具

- 新增 Question 工具,用于向用户提问并提供可能的回答选项
- 优化 FinishTool 和 RethinkTool 的名称和参数设计
- 更新工具注册逻辑,确保新工具被正确加载
PJ568 1 سال پیش
والد
کامیت
439504fc3a
8فایلهای تغییر یافته به همراه98 افزوده شده و 10 حذف شده
  1. 1 1
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 2 0
      src/agent/tool.rs
  4. 4 4
      src/agent/tools/finish.rs
  5. 2 1
      src/agent/tools/mod.rs
  6. 82 0
      src/agent/tools/question.rs
  7. 3 3
      src/agent/tools/rethink.rs
  8. 3 0
      src/main.rs

+ 1 - 1
Cargo.lock

@@ -2449,7 +2449,7 @@ checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51"
 
 [[package]]
 name = "yaa"
-version = "2.12.5"
+version = "2.13.0"
 dependencies = [
  "actix-cors",
  "actix-web",

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "yaa"
-version = "2.12.5"
+version = "2.13.0"
 edition = "2024"
 
 [dependencies]

+ 2 - 0
src/agent/tool.rs

@@ -2,6 +2,7 @@
 
 use crate::agent::tools::finish::FinishTool;
 use crate::agent::tools::rethink::RethinkTool;
+use crate::agent::tools::question::QuestionTool;
 use crate::core::tool::ToolRegistry;
 use serde_json;
 
@@ -75,6 +76,7 @@ pub fn create_tool_registry() -> ToolRegistry {
     let mut registry = ToolRegistry::new();
     registry.register(RethinkTool);
     registry.register(FinishTool);
+    registry.register(QuestionTool);
     registry
 }
 

+ 4 - 4
src/agent/tools/finish.rs

@@ -8,7 +8,7 @@ pub struct FinishTool;
 #[async_trait]
 impl Tool for FinishTool {
     fn name(&self) -> &str {
-        "完成会话"
+        "finish"
     }
 
     fn description(&self) -> &str {
@@ -17,10 +17,10 @@ impl Tool for FinishTool {
 
     fn parameters(&self) -> Vec<ToolParam> {
         vec![ToolParam {
-            name: "理由".to_string(),
+            name: "reason".to_string(),
             description: "任务完成的总结理由".to_string(),
             required: true,
-            r#type: "string".to_string(),
+            r#type: "string".to_string()
         }]
     }
 
@@ -56,7 +56,7 @@ impl Tool for FinishTool {
                 self.name(),
                 input
                     .param_values
-                    .get("理由")
+                    .get("reason")
                     .and_then(|v| v.as_str())
                     .unwrap_or("未提供理由")
             ),

+ 2 - 1
src/agent/tools/mod.rs

@@ -1,4 +1,5 @@
 //! 工具实现模块
 
 pub mod rethink;
-pub mod finish;
+pub mod finish;
+pub mod question;

+ 82 - 0
src/agent/tools/question.rs

@@ -0,0 +1,82 @@
+use crate::core::session::SessionData;
+use crate::core::tool::{Tool, ToolError, ToolInput, ToolOutput, ToolParam};
+use async_trait::async_trait;
+use serde_json::json;
+
+pub struct QuestionTool;
+
+#[async_trait]
+impl Tool for QuestionTool {
+    fn name(&self) -> &str {
+        "ask"
+    }
+
+    fn description(&self) -> &str {
+        "当无法通过其它方法获取更多信息时,向用户提问。可提供几个不重复的用户可能的回答以减少用户负担。"
+    }
+
+    fn parameters(&self) -> Vec<ToolParam> {
+        vec![
+            ToolParam {
+                name: "question".to_string(),
+                description: "想要询问用户的问题".to_string(),
+                required: true,
+                r#type: "string".to_string()
+            },
+            ToolParam {
+                name: "guess_one".to_string(),
+                description: "用户可能的回答".to_string(),
+                required: false,
+                r#type: "string".to_string()
+            },
+            ToolParam {
+                name: "guess_two".to_string(),
+                description: "用户可能的回答".to_string(),
+                required: false,
+                r#type: "string".to_string()
+            },
+            ToolParam {
+                name: "guess_three".to_string(),
+                description: "用户可能的回答".to_string(),
+                required: false,
+                r#type: "string".to_string()
+            }
+        ]
+    }
+
+    fn default_config(&self) -> crate::core::session::ToolApprovalConfig {
+        crate::core::session::ToolApprovalConfig { auto_approve: true }
+    }
+
+    async fn execute(
+        &self,
+        _input: ToolInput,
+        session: &mut SessionData,
+    ) -> Result<ToolOutput, ToolError> {
+        // 检查自动授权配置
+        if !session
+            .config
+            .tool
+            .tools
+            .get("question")
+            .map(|config| config.auto_approve)
+            .unwrap_or(true)
+        {
+            return Err(ToolError::NotAuthorized);
+        }
+
+        // 添加工具调用消息到会话
+        session.add_message(
+            crate::core::session::Role::System,
+            format!(
+                "[{}]执行成功。",
+                self.name()
+            ),
+        );
+
+        Ok(ToolOutput {
+            name: self.name().to_string(),
+            result: json!({"status": "success"}),
+        })
+    }
+}

+ 3 - 3
src/agent/tools/rethink.rs

@@ -8,7 +8,7 @@ pub struct RethinkTool;
 #[async_trait]
 impl Tool for RethinkTool {
     fn name(&self) -> &str {
-        "再度思考"
+        "rethink"
     }
 
     fn description(&self) -> &str {
@@ -17,7 +17,7 @@ impl Tool for RethinkTool {
 
     fn parameters(&self) -> Vec<ToolParam> {
         vec![ToolParam {
-            name: "理由".to_string(),
+            name: "reason".to_string(),
             description: "调用这个工具的理由".to_string(),
             required: true,
             r#type: "string".to_string(),
@@ -53,7 +53,7 @@ impl Tool for RethinkTool {
                 self.name(),
                 input
                     .param_values
-                    .get("理由")
+                    .get("reason")
                     .and_then(|v| v.as_str())
                     .unwrap_or("未提供理由")
             ),

+ 3 - 0
src/main.rs

@@ -3,6 +3,7 @@ mod cli;
 mod core;
 use actix_cors::Cors;
 use actix_web::{App, HttpServer, web};
+use agent::tools::question::QuestionTool;
 use agent::tools::finish::FinishTool;
 use agent::tools::rethink::RethinkTool;
 use agent::{api::OpenAIClient, process_session};
@@ -88,6 +89,7 @@ async fn handle_request(
     let mut tool_registry = ToolRegistry::new();
     tool_registry.register(FinishTool);
     tool_registry.register(RethinkTool);
+    tool_registry.register(QuestionTool);
 
     // 创建全新的SessionData实例,确保状态隔离
     let mut merged_data = SessionData::default();
@@ -213,6 +215,7 @@ async fn main() -> std::io::Result<()> {
         let mut tool_registry = ToolRegistry::new();
         tool_registry.register(FinishTool);
         tool_registry.register(RethinkTool);
+        tool_registry.register(QuestionTool);
 
         let client = OpenAIClient::new(
             session_data.config.llm_api.provider.api_key.to_string(),