Skip to main content

matrixcode_core/tools/workflow/
match.rs

1//! Workflow Match Tool
2//!
3//! 根据意图匹配工作流
4
5use crate::tools::{Tool, ToolDefinition};
6use crate::workflow::WorkflowRegistry;
7use anyhow::Result;
8use async_trait::async_trait;
9use serde_json::Value;
10
11/// Tool to match workflows by intent
12pub struct WorkflowMatchTool;
13
14#[async_trait]
15impl Tool for WorkflowMatchTool {
16    fn definition(&self) -> ToolDefinition {
17        ToolDefinition {
18            name: "workflow_match".to_string(),
19            description: "根据意图查找匹配的 workflow。传入自然语言描述,返回最相关的 workflow 列表。".to_string(),
20            parameters: serde_json::json!({
21                "type": "object",
22                "properties": {
23                    "query": {
24                        "type": "string",
25                        "description": "意图描述,如 '处理文本'、'生成代码'、'验证输出'"
26                    }
27                },
28                "required": ["query"]
29            }),
30            ..Default::default()
31        }
32    }
33
34    async fn execute(&self, params: Value) -> Result<String> {
35        let query = params.get("query")
36            .and_then(|v| v.as_str())
37            .ok_or_else(|| anyhow::anyhow!("缺少 query 参数"))?;
38
39        let project_path = std::env::current_dir().ok();
40        let registry = WorkflowRegistry::new(project_path.as_ref());
41
42        let matches = registry.match_workflows(query);
43
44        if matches.is_empty() {
45            return Ok(format!("未找到匹配 '{}' 的 workflow。用 workflow_discover 查看全部。", query));
46        }
47
48        let mut result = format!("匹配 '{}' 的 workflow:\n\n", query);
49        for info in matches.iter().take(5) {
50            result.push_str(&format!("• {} - ", info.id));
51            if let Some(ref desc) = info.description {
52                result.push_str(desc);
53            } else {
54                result.push_str(&info.name);
55            }
56            result.push('\n');
57        }
58
59        result.push_str("\n调用: workflow_run {\"workflow_id\": \"选定的ID\"}");
60        Ok(result)
61    }
62}