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、描述和所需输入参数列表。".to_string(),
20            parameters: serde_json::json!({
21                "type": "object",
22                "properties": {},
23                "required": []
24            }),
25            ..Default::default()
26        }
27    }
28
29    async fn execute(&self, _params: Value) -> Result<String> {
30        let project_path = std::env::current_dir().ok();
31        let registry = WorkflowRegistry::new(project_path.as_ref());
32
33        if registry.is_empty() {
34            return Ok("未发现 workflow。在 .matrix/workflows/ 或 ~/.matrix/workflows/ 目录创建 YAML 文件。".to_string());
35        }
36
37        let mut result = format!("发现 {} 个 workflow:\n\n", registry.count());
38        for info in registry.list() {
39            result.push_str(&format!("• {} - ", info.id));
40            if let Some(ref desc) = info.description {
41                result.push_str(desc);
42            } else {
43                result.push_str(&info.name);
44            }
45            if !info.required_inputs.is_empty() {
46                result.push_str(&format!(" [需要: {}]", info.required_inputs.join(", ")));
47            }
48            result.push('\n');
49        }
50
51        Ok(result)
52    }
53}