Skip to main content

agentic_workflow_mcp/tools/
trigger_tools.rs

1use serde_json::json;
2
3use crate::types::{ToolDefinition, ToolResult};
4use super::registry::EngineState;
5
6pub fn definitions() -> Vec<ToolDefinition> {
7    vec![
8        ToolDefinition {
9            name: "workflow_trigger_create".to_string(),
10            description: "Create a trigger that starts a workflow on an event".to_string(),
11            input_schema: json!({
12                "type": "object",
13                "properties": {
14                    "name": { "type": "string", "description": "Trigger name" },
15                    "workflow_id": { "type": "string", "description": "Workflow to trigger" },
16                    "trigger_type": { "type": "string", "description": "Type: manual, file_watch, webhook, cron, event" },
17                    "debounce_ms": { "type": "integer", "description": "Debounce interval in milliseconds" }
18                },
19                "required": ["name", "workflow_id"]
20            }),
21        },
22        ToolDefinition {
23            name: "workflow_trigger_list".to_string(),
24            description: "List all triggers, optionally filtered by workflow ID".to_string(),
25            input_schema: json!({
26                "type": "object",
27                "properties": {
28                    "workflow_id": { "type": "string", "description": "Filter by workflow ID" }
29                }
30            }),
31        },
32        ToolDefinition {
33            name: "workflow_trigger_test".to_string(),
34            description: "Test a trigger condition against sample event data".to_string(),
35            input_schema: json!({
36                "type": "object",
37                "properties": {
38                    "trigger_id": { "type": "string", "description": "Trigger ID" },
39                    "event_data": { "type": "object", "description": "Sample event data" }
40                },
41                "required": ["trigger_id", "event_data"]
42            }),
43        },
44        ToolDefinition {
45            name: "workflow_trigger_history".to_string(),
46            description: "Get activation history for a trigger".to_string(),
47            input_schema: json!({
48                "type": "object",
49                "properties": {
50                    "trigger_id": { "type": "string", "description": "Trigger ID" }
51                },
52                "required": ["trigger_id"]
53            }),
54        },
55        ToolDefinition {
56            name: "workflow_trigger_replay".to_string(),
57            description: "Replay a trigger activation to re-run its workflow".to_string(),
58            input_schema: json!({
59                "type": "object",
60                "properties": {
61                    "trigger_id": { "type": "string", "description": "Trigger ID" },
62                    "activation_index": { "type": "integer", "description": "Index of activation to replay" }
63                },
64                "required": ["trigger_id"]
65            }),
66        },
67    ]
68}
69
70pub fn dispatch(
71    name: &str,
72    params: serde_json::Value,
73    state: &mut EngineState,
74) -> Result<ToolResult, (i32, String)> {
75    match name {
76        "workflow_trigger_create" => {
77            let tname = params["name"].as_str().unwrap_or("trigger");
78            let wf_id = params["workflow_id"].as_str().unwrap_or("");
79            let trigger_type = match params["trigger_type"].as_str().unwrap_or("manual") {
80                "file_watch" => agentic_workflow::types::TriggerType::FileSystem {
81                    path: params["config"]["path"].as_str().unwrap_or("/tmp").to_string(),
82                    events: vec![agentic_workflow::types::FileEvent::Modified],
83                },
84                "webhook" => agentic_workflow::types::TriggerType::Webhook {
85                    endpoint: params["config"]["endpoint"].as_str().unwrap_or("/hook").to_string(),
86                    method: "POST".to_string(),
87                },
88                _ => agentic_workflow::types::TriggerType::Manual,
89            };
90            let debounce = params["debounce_ms"].as_u64();
91            match state.trigger.create_trigger(tname, wf_id, trigger_type, None, debounce) {
92                Ok(tid) => Ok(ToolResult::text(json!({
93                    "trigger_id": tid,
94                    "status": "created"
95                }).to_string())),
96                Err(e) => Ok(ToolResult::error(format!("{}", e))),
97            }
98        }
99        "workflow_trigger_list" => {
100            let triggers = if let Some(wf_id) = params["workflow_id"].as_str() {
101                state.trigger.triggers_for_workflow(wf_id)
102            } else {
103                state.trigger.list_triggers()
104            };
105            let items: Vec<_> = triggers.iter().map(|t| json!({
106                "trigger_id": t.id,
107                "name": t.name,
108                "workflow_id": t.workflow_id,
109                "enabled": t.enabled
110            })).collect();
111            Ok(ToolResult::text(json!({ "triggers": items }).to_string()))
112        }
113        "workflow_trigger_test" => {
114            let tid = params["trigger_id"].as_str().unwrap_or("");
115            let event_data = &params["event_data"];
116            match state.trigger.test_condition(tid, event_data) {
117                Ok(matches) => Ok(ToolResult::text(json!({
118                    "trigger_id": tid,
119                    "condition_met": matches
120                }).to_string())),
121                Err(e) => Ok(ToolResult::error(format!("{}", e))),
122            }
123        }
124        "workflow_trigger_history" => {
125            let tid = params["trigger_id"].as_str().unwrap_or("");
126            let history = state.trigger.activation_history(tid);
127            let items: Vec<_> = history.iter().map(|a| json!({
128                "trigger_id": a.trigger_id,
129                "execution_id": a.execution_id,
130                "activated_at": a.activated_at.to_rfc3339(),
131                "condition_met": a.condition_met
132            })).collect();
133            Ok(ToolResult::text(json!({ "activations": items }).to_string()))
134        }
135        "workflow_trigger_replay" => {
136            let tid = params["trigger_id"].as_str().unwrap_or("");
137            let idx = params["activation_index"].as_u64().unwrap_or(0) as usize;
138            let history = state.trigger.activation_history(tid);
139            match history.get(idx) {
140                Some(activation) => Ok(ToolResult::text(json!({
141                    "trigger_id": tid,
142                    "replaying_activation": idx,
143                    "original_execution_id": activation.execution_id,
144                    "status": "replay_queued"
145                }).to_string())),
146                None => Ok(ToolResult::error(format!(
147                    "Activation index {} not found for trigger {}",
148                    idx, tid
149                ))),
150            }
151        }
152        _ => Ok(ToolResult::error(format!("Unknown trigger tool: {}", name))),
153    }
154}