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:
20                "根据意图查找匹配的 workflow。传入自然语言描述,返回最相关的 workflow 列表。"
21                    .to_string(),
22            parameters: serde_json::json!({
23                "type": "object",
24                "properties": {
25                    "query": {
26                        "type": "string",
27                        "description": "意图描述,如 '处理文本'、'生成代码'、'验证输出'"
28                    }
29                },
30                "required": ["query"]
31            }),
32            ..Default::default()
33        }
34    }
35
36    async fn execute(&self, params: Value) -> Result<String> {
37        let query = params
38            .get("query")
39            .and_then(|v| v.as_str())
40            .ok_or_else(|| anyhow::anyhow!("缺少 query 参数"))?;
41
42        let project_path = std::env::current_dir().ok();
43        let registry = WorkflowRegistry::new(project_path.as_ref());
44
45        let matches = registry.match_workflows(query);
46
47        if matches.is_empty() {
48            return Ok(format!(
49                "未找到匹配 '{}' 的 workflow。用 workflow_discover 查看全部。",
50                query
51            ));
52        }
53
54        let mut result = format!("匹配 '{}' 的 workflow:\n\n", query);
55        for info in matches.iter().take(5) {
56            result.push_str(&format!("• {} - ", info.id));
57            if let Some(ref desc) = info.description {
58                result.push_str(desc);
59            } else {
60                result.push_str(&info.name);
61            }
62            result.push('\n');
63        }
64
65        result.push_str("\n调用: workflow_run {\"workflow_id\": \"选定的ID\"}");
66        Ok(result)
67    }
68}