Skip to main content

matrixcode_core/tools/workflow/
discover.rs

1//! Workflow Discovery Tool
2//!
3//! 让 AI 发现已有的自动化工作流
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 discover available workflows
12pub struct WorkflowDiscoverTool;
13
14#[async_trait]
15impl Tool for WorkflowDiscoverTool {
16    fn definition(&self) -> ToolDefinition {
17        ToolDefinition {
18            name: "workflow_discover".to_string(),
19            description: "发现可执行的自动化流程。返回 workflow ID、描述和所需输入参数列表。"
20                .to_string(),
21            parameters: serde_json::json!({
22                "type": "object",
23                "properties": {},
24                "required": []
25            }),
26            ..Default::default()
27        }
28    }
29
30    async fn execute(&self, _params: Value) -> Result<String> {
31        let project_path = std::env::current_dir().ok();
32        let registry = WorkflowRegistry::new(project_path.as_ref());
33
34        if registry.is_empty() {
35            return Ok("未发现 workflow。在 .matrix/workflows/ 或 ~/.matrix/workflows/ 目录创建 YAML 文件。".to_string());
36        }
37
38        let mut result = format!("发现 {} 个 workflow:\n\n", registry.count());
39        for info in registry.list() {
40            result.push_str(&format!("• {} - ", info.id));
41            if let Some(ref desc) = info.description {
42                result.push_str(desc);
43            } else {
44                result.push_str(&info.name);
45            }
46            if !info.required_inputs.is_empty() {
47                result.push_str(&format!(" [需要: {}]", info.required_inputs.join(", ")));
48            }
49            result.push('\n');
50        }
51
52        Ok(result)
53    }
54}