Skip to main content

agentmux_srv/drone/executor/blocks/
call.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The Drone block: calls another drone as a step
5//! (docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md ยง3.5).
6//!
7//! Block config (`node.data`):
8//!   * `drone`    required, the id of the drone to call. Written in the graph, never a
9//!                `{{reference}}`: a value from outside must not pick what runs.
10//!   * `version`  optional, the saved version to call. Without it the drone as it is saved now.
11//!   * `inputs`   optional, `{ "name": "template" }`: what the callee reads as `{{trigger.name}}`.
12//!
13//! The scheduler does the calling (`scheduler/call.rs`), because the callee runs on
14//! the caller's remaining budget and under its cancel.
15
16use serde_json::Value;
17
18use crate::backend::storage::drone_types::FlowNode;
19
20/// What the block calls.
21#[derive(Debug, Clone, PartialEq)]
22pub struct CallSpec {
23    pub drone: String,
24    pub version: Option<i64>,
25    /// Each input as the graph wrote it, to be filled in when the call is made.
26    pub inputs: Vec<(String, String)>,
27}
28
29/// The block's settings, or what is wrong with them.
30pub fn parse(node: &FlowNode) -> Result<CallSpec, String> {
31    let id = &node.id;
32    let drone = node.data.get("drone").and_then(Value::as_str).map(str::trim).unwrap_or("");
33    if drone.is_empty() {
34        return Err(format!("{id}: a Drone block needs `drone`, the id of the drone it calls"));
35    }
36    if drone.contains("{{") {
37        return Err(format!("{id}: `drone` is written in the graph and cannot be a {{{{reference}}}}: a value from outside must not pick what runs"));
38    }
39    let version = match node.data.get("version") {
40        None | Some(Value::Null) => None,
41        Some(v) => Some(v.as_i64().filter(|n| *n > 0).ok_or_else(|| format!("{id}: `version` must be a saved version number, 1 or more"))?),
42    };
43    let inputs = match node.data.get("inputs") {
44        None | Some(Value::Null) => Vec::new(),
45        Some(Value::Object(map)) => map
46            .iter()
47            .map(|(k, v)| match v {
48                Value::String(t) => Ok((k.clone(), t.clone())),
49                other => Ok((k.clone(), other.to_string())),
50            })
51            .collect::<Result<Vec<_>, String>>()?,
52        Some(_) => return Err(format!("{id}: `inputs` must be an object of names and values")),
53    };
54    let mut inputs = inputs;
55    inputs.sort();
56    Ok(CallSpec { drone: drone.to_string(), version, inputs })
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62    use crate::drone::types::NodePosition;
63    use serde_json::json;
64
65    fn call_node(extra: Value) -> FlowNode {
66        let mut data = json!({ "kind": "drone" });
67        data.as_object_mut().unwrap().extend(extra.as_object().cloned().unwrap_or_default());
68        FlowNode { id: "c".to_string(), position: NodePosition::default(), data, node_type: None }
69    }
70
71    #[test]
72    fn a_call_names_its_drone_and_may_pin_a_version_and_give_inputs() {
73        let spec = parse(&call_node(json!({ "drone": " other ", "version": 3, "inputs": { "n": "{{var.x}}", "k": 5 } }))).unwrap();
74        assert_eq!(spec.drone, "other");
75        assert_eq!(spec.version, Some(3));
76        assert_eq!(spec.inputs, [("k".to_string(), "5".to_string()), ("n".to_string(), "{{var.x}}".to_string())]);
77        assert_eq!(parse(&call_node(json!({ "drone": "d" }))).unwrap(), CallSpec { drone: "d".into(), version: None, inputs: vec![] });
78    }
79
80    #[test]
81    fn a_call_that_is_set_wrongly_says_what_to_fix() {
82        assert!(parse(&call_node(json!({}))).unwrap_err().contains("needs `drone`"));
83        assert!(parse(&call_node(json!({ "drone": "{{trigger.d}}" }))).unwrap_err().contains("cannot be a"));
84        assert!(parse(&call_node(json!({ "drone": "d", "version": 0 }))).unwrap_err().contains("`version` must be"));
85        assert!(parse(&call_node(json!({ "drone": "d", "version": "latest" }))).unwrap_err().contains("`version` must be"));
86        assert!(parse(&call_node(json!({ "drone": "d", "inputs": [1] }))).unwrap_err().contains("`inputs` must be an object"));
87    }
88}