Skip to main content

agentmux_mcp/tools/
pane_tool.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Pane: one tool for the agent's place in the UI and the panes it shows the
5//! user, with an `action` (docs/specs/SPEC_MUXAPI_TOOL_CONSOLIDATION_2026_10_15.md
6//! §4.2, slice S4). It replaces WhoAmI, Layout, OpenEditor, OpenMedia,
7//! OpenFiles, NewTab, SetActiveTab, FocusWindow and SetName; their handlers
8//! (panes.rs) run each action unchanged. Closing a pane is in Stop.
9
10use super::family::{pick, relabeled, Act};
11use super::*;
12
13pub(super) const ACTIONS: &[&str] = &["whoami", "layout", "open", "new_tab", "activate_tab", "focus_window", "rename", "resize", "move", "place", "dock", "float", "pin", "unpin"];
14
15const ACTS: &[Act] = &[
16    Act { action: "whoami", tool: "WhoAmI", rename: &[] },
17    Act { action: "layout", tool: "Layout", rename: &[] },
18    Act { action: "new_tab", tool: "NewTab", rename: &[] },
19    Act { action: "activate_tab", tool: "SetActiveTab", rename: &[] },
20    Act { action: "focus_window", tool: "FocusWindow", rename: &[] },
21    Act { action: "rename", tool: "SetName", rename: &[] },
22    Act { action: "resize", tool: "ResizePane", rename: &[] },
23    Act { action: "move", tool: "MovePane", rename: &[] },
24    Act { action: "place", tool: "PlacePane", rename: &[] },
25    Act { action: "dock", tool: "DockPane", rename: &[] },
26    Act { action: "float", tool: "FloatPane", rename: &[] },
27    Act { action: "pin", tool: "PinPane", rename: &[] },
28    Act { action: "unpin", tool: "UnpinPane", rename: &[] },
29];
30
31/// `open`'s handler for each view; the editor and media handlers take the
32/// path as `file`.
33const OPEN: &[(&str, Act)] = &[
34    ("editor", Act { action: "open", tool: "OpenEditor", rename: &[("path", "file")] }),
35    ("media", Act { action: "open", tool: "OpenMedia", rename: &[("path", "file")] }),
36    ("files", Act { action: "open", tool: "OpenFiles", rename: &[] }),
37];
38
39/// `open` with a deep link, or a view at a place (docs/specs/SPEC_DEEP_LINKS_ONE_FRAMEWORK_2026_11_20.md R16):
40/// srv's guard decides it.
41const OPEN_LINK: Act = Act { action: "open", tool: "OpenLink", rename: &[] };
42
43/// Whether a Pane open is a deep link: it names `link`, or a view that is not a file view given a path.
44fn is_link_open(arguments: &Value) -> bool {
45    let has = |k: &str| arguments.get(k).is_some_and(|v| !v.is_null() && v.as_str() != Some(""));
46    let view = arguments.get("view").and_then(|v| v.as_str()).unwrap_or("");
47    has("link") || (!view.is_empty() && !has("path") && (has("place") || !OPEN.iter().any(|(v, _)| *v == view)))
48}
49
50/// The handler for a Pane call (as the one-action list it was picked from,
51/// for [`relabeled`]) and its arguments as that handler takes them.
52fn route(arguments: &Value) -> Result<(&'static [Act], Value)> {
53    let action = arguments.get("action").and_then(|v| v.as_str()).unwrap_or("");
54    match action {
55        "open" if is_link_open(arguments) => {
56            let acts = std::slice::from_ref(&OPEN_LINK);
57            let (_, args) = pick("Pane", acts, arguments)?;
58            Ok((acts, args))
59        }
60        "open" => {
61            let view = arguments.get("view").and_then(|v| v.as_str()).unwrap_or("");
62            let Some((_, act)) = OPEN.iter().find(|(v, _)| *v == view) else {
63                anyhow::bail!("Pane open needs link (agentmux://view/place, every view and place AgentMux has), or view: \"editor\" (a file), \"media\" (an image, video or audio file) or \"files\" (a folder) with a path");
64            };
65            // Checked here: the editor and media handlers call it `file`,
66            // which Pane doesn't take.
67            if arguments.get("path").and_then(|v| v.as_str()).map_or(true, |p| p.trim().is_empty()) {
68                anyhow::bail!("Pane open: missing required parameter: path (absolute)");
69            }
70            let acts = std::slice::from_ref(act);
71            let (_, mut args) = pick("Pane", acts, arguments)?;
72            if let Value::Object(m) = &mut args {
73                m.remove("view");
74            }
75            Ok((acts, args))
76        }
77        "" => anyhow::bail!("Pane: missing required parameter: action (one of {})", ACTIONS.join(", ")),
78        a if ACTIONS.contains(&a) => {
79            let (act, args) = pick("Pane", ACTS, arguments)?;
80            Ok((std::slice::from_ref(act), args))
81        }
82        other => anyhow::bail!("Pane: unknown action {other:?}: use one of {}", ACTIONS.join(", ")),
83    }
84}
85
86pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
87    match name {
88        "Pane" => {
89            let (acts, args) = route(arguments)?;
90            relabeled("Pane", acts, panes::call(acts[0].tool, &args, cx).await)
91        }
92        // Still its own tool (a container agent's only one of its kind); its
93        // handler sits with the pane handlers.
94        "RegisterDevServer" => panes::call(name, arguments, cx).await,
95        _ => Err(not_in_family()),
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn every_action_is_in_the_schema() {
105        let schema: Value = serde_json::from_str(crate::tool_schemas::PANE_TOOL).unwrap();
106        let mut listed: Vec<&str> = schema["inputSchema"]["properties"]["action"]["enum"]
107            .as_array()
108            .unwrap()
109            .iter()
110            .filter_map(|v| v.as_str())
111            .collect();
112        let mut want = ACTIONS.to_vec();
113        listed.sort();
114        want.sort();
115        assert_eq!(listed, want);
116        for act in ACTS {
117            assert!(ACTIONS.contains(&act.action), "{}", act.action);
118        }
119    }
120
121    #[test]
122    fn open_runs_the_handler_for_its_view() {
123        let (acts, args) = route(&json!({ "action": "open", "view": "editor", "path": "/w/a.rs", "split": "down" })).unwrap();
124        assert_eq!(acts[0].tool, "OpenEditor");
125        assert_eq!(args, json!({ "file": "/w/a.rs", "split": "down" }));
126        let (acts, args) = route(&json!({ "action": "open", "view": "files", "path": "/w", "select": ["a"] })).unwrap();
127        assert_eq!(acts[0].tool, "OpenFiles");
128        assert_eq!(args, json!({ "path": "/w", "select": ["a"] }));
129        let e = route(&json!({ "action": "open", "path": "/w" })).unwrap_err().to_string();
130        assert!(e.contains("needs link"), "{e}");
131        for view in ["editor", "media", "files"] {
132            let e = route(&json!({ "action": "open", "view": view })).unwrap_err().to_string();
133            assert_eq!(e, "Pane open: missing required parameter: path (absolute)");
134        }
135    }
136
137    #[test]
138    fn a_link_or_a_view_at_a_place_is_a_deep_link_open() {
139        for args in [
140            json!({ "action": "open", "link": "agentmux://settings/recording" }),
141            json!({ "action": "open", "view": "settings", "place": "recording" }),
142            json!({ "action": "open", "view": "vault" }),
143            json!({ "action": "open", "view": "editor", "place": "C:/w/a.md" }),
144        ] {
145            assert_eq!(route(&args).unwrap().0[0].tool, "OpenLink", "{args}");
146        }
147        // A file view with a path keeps its own handler (a container agent's snapshot, the split).
148        assert_eq!(route(&json!({ "action": "open", "view": "editor", "path": "/w/a.rs" })).unwrap().0[0].tool, "OpenEditor");
149    }
150
151    /// The schema's views are the deep-link registry's views of this build, so a model is offered every place
152    /// srv can open and none it can't.
153    #[test]
154    fn the_views_offered_are_the_registrys_of_this_build() {
155        use agentmux_common::deeplinks::REGISTRY;
156        let schema: Value = serde_json::from_str(crate::tool_schemas::PANE_TOOL).unwrap();
157        let mut offered: Vec<&str> = schema["inputSchema"]["properties"]["view"]["enum"].as_array().unwrap().iter().filter_map(|v| v.as_str()).collect();
158        let mut openable: Vec<&str> = REGISTRY.views.iter().filter(|v| v.build <= REGISTRY.build && !v.prefix).map(|v| v.id).collect();
159        offered.sort();
160        openable.sort();
161        assert_eq!(offered, openable);
162    }
163
164    #[test]
165    fn a_link_is_written_in_its_one_form_from_a_view_a_place_and_parameters() {
166        use super::super::panes::link_of;
167        assert_eq!(link_of(&json!({ "link": " agentmux://help " })).unwrap(), "agentmux://help");
168        assert_eq!(link_of(&json!({ "view": "settings", "place": "recording", "anchor": "recording.engine" })).unwrap(), "agentmux://settings/recording#recording.engine");
169        assert_eq!(link_of(&json!({ "view": "kanban", "place": "task/48139" })).unwrap(), "agentmux://kanban/task/48139");
170        // Parameters in the registry's order whatever order they came in; a number is written as its digits.
171        assert_eq!(link_of(&json!({ "view": "editor", "place": "/w/a.md", "params": { "offset": 4, "line": 2 } })).unwrap(), "agentmux://editor/%2Fw%2Fa.md?line=2&offset=4");
172        assert!(link_of(&json!({})).is_err());
173    }
174
175    #[test]
176    fn the_answer_says_what_happened_and_for_a_refusal_what_would_work() {
177        use super::super::panes::describe_open_outcome;
178        let opened = describe_open_outcome(&json!({ "outcome": "opened", "label": "settings › recording", "block_id": "b1", "link": "agentmux://settings/recording" }));
179        assert_eq!(opened, "Opened settings › recording beside this conversation (block b1). Link: agentmux://settings/recording.");
180        let refused = describe_open_outcome(&json!({ "outcome": "refused", "reason": "an agent may open only its own place of audit", "remedy": "agentmux://audit/agent/a1" }));
181        assert_eq!(refused, "Not opened: an agent may open only its own place of audit. agentmux://audit/agent/a1.");
182    }
183
184    #[test]
185    fn the_other_actions_run_their_handlers() {
186        let (acts, args) = route(&json!({ "action": "rename", "target": "tab", "name": "x" })).unwrap();
187        assert_eq!(acts[0].tool, "SetName");
188        assert_eq!(args, json!({ "target": "tab", "name": "x" }));
189        // A file's rename carries its path to the same handler.
190        let (acts, args) = route(&json!({ "action": "rename", "target": "file", "path": "/w/a.rs", "name": "b.rs" })).unwrap();
191        assert_eq!(acts[0].tool, "SetName");
192        assert_eq!(args, json!({ "target": "file", "path": "/w/a.rs", "name": "b.rs" }));
193        assert_eq!(route(&json!({ "action": "whoami" })).unwrap().0[0].tool, "WhoAmI");
194        // A floating pane's size and place: each action has its own handler, since the handler is told which by its name.
195        for (action, tool) in [("resize", "ResizePane"), ("move", "MovePane"), ("place", "PlacePane"), ("dock", "DockPane"), ("float", "FloatPane"), ("pin", "PinPane"), ("unpin", "UnpinPane")] {
196            let (acts, args) = route(&json!({ "action": action, "target_id": "b1", "width": 700 })).unwrap();
197            assert_eq!(acts[0].tool, tool);
198            assert_eq!(args, json!({ "target_id": "b1", "width": 700 }));
199        }
200        let e = route(&json!({ "action": "close" })).unwrap_err().to_string();
201        assert_eq!(e, format!("Pane: unknown action \"close\": use one of {}", ACTIONS.join(", ")));
202    }
203}