Skip to main content

agentmux_srv/server/app_api/
pane_open_steps.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The paths `pane.open` takes once it knows what to open, one function each. `open_pane` (pane.rs) calls them;
5//! the code is moved from it unchanged.
6
7use super::*;
8
9/// The Media pane is folded into the Editor: a media open is an Editor open that follows the folder.
10pub(super) fn fold_media_into_editor(cmd: &mut CommandPaneOpenData) -> Result<(), String> {
11    // The Media pane is folded into the Editor
12    // (SPEC_EDITOR_REVAMP_UNIFIED_VIEWERS_2026_10_15.md §4.11): a media open
13    // is an Editor open that follows the folder, and may reuse the Editor
14    // pane on screen like OpenEditor does.
15    if cmd.view == "media" && cmd.meta.is_none() {
16        if let Some(conn) = cmd.connection.as_deref().map(str::trim).filter(|c| !c.is_empty() && *c != "local") {
17            if crate::backend::fs_ops::remote::ssh_connection(Some(conn)).is_some() {
18                return Err(format!("INVALID_ARG: media files on {conn} can't be shown yet"));
19            }
20        }
21        cmd.view = "editor".to_string();
22        cmd.follow_folder.get_or_insert(true);
23    }
24    Ok(())
25}
26
27/// The Reviews pane is folded into the Kanban (SPEC_FOLD_REVIEWS_INTO_THE_KANBAN_2026_11_22.md §7): an open of the
28/// `reviews` view is an open of the Kanban on the filter that showed what the pane did, cards with open pull requests.
29/// `reviews:task` (a task to show) becomes `kanban:open`. An agent's pane tool, an old pin or a saved link that still
30/// says `reviews` is not an error.
31pub(super) fn fold_reviews_into_kanban(cmd: &mut CommandPaneOpenData) {
32    if cmd.view != "reviews" {
33        return;
34    }
35    cmd.view = "kanban".to_string();
36    let mut meta = cmd.meta.take().unwrap_or_default();
37    meta.insert("view".to_string(), serde_json::json!("kanban"));
38    meta.entry("kanban:waits".to_string()).or_insert_with(|| serde_json::json!("open"));
39    if let Some(task) = meta.remove("reviews:task") {
40        meta.entry("kanban:open".to_string()).or_insert(task);
41    }
42    cmd.meta = Some(meta);
43}
44
45/// The Editor pane a `target_id` names, if any; it is only valid with an Editor open that is not floating.
46pub(super) fn named_editor_target(cmd: &CommandPaneOpenData) -> Result<Option<&str>, String> {
47    // A `target_block_id` names the Editor pane to put the file in: it needs no other opt-in.
48    let named = cmd.target_block_id.as_deref().map(str::trim).filter(|t| !t.is_empty());
49    if named.is_some() {
50        if cmd.view != "editor" {
51            return Err("INVALID_ARG: target_id only names an Editor pane (view editor or media)".to_string());
52        }
53        if cmd.floating == Some(true) {
54            return Err("INVALID_ARG: target_id can't be combined with floating".to_string());
55        }
56    }
57    Ok(named)
58}
59
60/// Add the requested file as a tab of an Editor pane that is already open, when the caller asked for reuse.
61pub(super) async fn reuse_editor_pane(state: &AppState, cmd: &CommandPaneOpenData, named: Option<&str>, viewer: Option<&'static str>) -> Result<Option<PaneOpenResult>, String> {
62    // Reuse (SPEC_EDITOR_MCP_OPEN_BLANK_PREVIEW_AND_PANE_REUSE_2026_08_03.md Part 2) needs the explicit `reuse_editor_pane`
63    // opt-in, never inferred from `split_reference_block_id`: openToTheSide and openInTerminal set it to their own block id
64    // and must not reuse (#2404). A floating open always gets its own window. An explicit `tree_expanded` does not bypass
65    // reuse: a reused pane keeps its own tree state, and bypassing made a new pane on every open of an agent that always passes it.
66    if cmd.view == "editor" && (cmd.reuse_editor_pane == Some(true) || named.is_some()) && cmd.floating != Some(true) {
67        if let Some(file) = cmd.file.as_deref() {
68            let connection = cmd.connection.as_deref().unwrap_or("");
69            let follow = cmd.follow_folder == Some(true);
70            let caller = cmd.split_reference_block_id.as_deref();
71            if let Some(result) = pane::maybe_reuse_editor_pane(state, caller, named, file, connection, follow, viewer, pane::OpenAt::of(cmd)).await? {
72                return Ok(Some(result));
73            }
74        }
75    }
76    Ok(None)
77}
78
79/// Stack path (SPEC_PANE_TABS_REDUCER_COMMANDS_2026_09_18.md §3.3): create the block as a new tab of the pane holding
80/// `target`, in one reducer step, then queue a `stackpush` action for the frontend.
81pub(super) async fn open_stacked(state: &AppState, cmd: &CommandPaneOpenData, target: String, meta: MetaMapType) -> Result<PaneOpenResult, String> {
82    let mstore = state.mstore.clone();
83    // The new tab is the visible one unless the caller said not to take focus
84    // (`focus: false`: an agent's popup must not switch the person's tab).
85    let activate = cmd.focus.unwrap_or(true);
86    let tab_id = resolve_tab_id_for_block(&mstore, &target)
87        .map_err(|e| format!("pane.open: stack_onto_block_id: {e}"))?;
88    let meta_val = serde_json::to_value(&meta)
89        .map_err(|e| format!("pane.open: stack_onto_block_id: meta serialize: {e}"))?;
90    let create = agentmux_common::ipc::Command::CreateBlockInStack {
91        tab_id: tab_id.clone(),
92        target_block_id: target.clone(),
93        meta: meta_val,
94        activate,
95    };
96    let block_id =
97        crate::server::service::create_via_reducer(state, create, "pane.open", "pane.open: CreateBlockInStack", "pane.open: stack_onto_block_id:").await?;
98    if let Err(e) =
99        crate::server::service::layout_helpers::queue_target_stack_push(state, &tab_id, &block_id, &target, activate).await
100    {
101        tracing::warn!(block_id = %block_id, "pane.open: stack_onto_block_id: stackpush queue failed: {e}");
102    }
103    tracing::info!(block_id = %block_id, target = %target, view = %cmd.view, "pane.open: block created as a pane tab");
104    Ok(PaneOpenResult::docked(block_id, tab_id, cmd.view.clone(), true))
105}
106
107/// Skip-placement path (SPEC_PANE_TAB_STRIP_AGENT_TERMINAL_2026_07_20.md §4.2): create the block through the reducer and
108/// return at once, with no layout action; the caller attaches it to an existing pane's block stack.
109pub(super) async fn open_unplaced(state: &AppState, view: String, tab_id: String, meta: MetaMapType) -> Result<PaneOpenResult, String> {
110    let meta_val = serde_json::to_value(&meta)
111        .map_err(|e| format!("pane.open: skip_placement: meta serialize: {e}"))?;
112    let block_id = crate::server::service::create_block_in_tab(state, &tab_id, meta_val, "pane.open: skip_placement").await?;
113    tracing::info!(block_id = %block_id, view = %view, "pane.open: block created, placement skipped");
114    Ok(PaneOpenResult::docked(block_id, tab_id, view, true))
115}
116
117/// Docked path: create the block through the reducer, place it (split if asked, else appended), and tell every window.
118pub(super) async fn open_docked(state: &AppState, cmd: &CommandPaneOpenData, tab_id: String, meta: MetaMapType) -> Result<PaneOpenResult, String> {
119    // Create the block through the reducer (#1681), not wcore-direct: a store-only `create_block` never reaches the
120    // reducer's `state.blocks`, so a later TearOffBlock / RedockFloatingPane is rejected "block not found".
121    // Mirrors the open_pane_floating path.
122    let meta_val = serde_json::to_value(&meta)
123        .map_err(|e| format!("pane.open: meta serialize: {e}"))?;
124    let block_id = crate::server::service::create_block_in_tab(state, &tab_id, meta_val, "pane.open").await?;
125
126    // Place it, split if requested, else append: srv's tree, then the queued
127    // action for the window. Best-effort: a failure leaves the block created
128    // but not laid out.
129    let placement = pane::resolve_placement(
130        cmd.split_direction.as_deref(),
131        cmd.split_reference_block_id.as_deref(),
132        cmd.focus.unwrap_or(true),
133    );
134    if let Err(e) = crate::server::service::layout_intents::place_block(state, &tab_id, &block_id, placement).await {
135        tracing::warn!("pane.open: placing the pane failed: {e}");
136    }
137
138    tracing::info!(
139        block_id = %block_id,
140        view = %cmd.view,
141        "pane.open: block created + layout updated"
142    );
143
144    super::open_updates::broadcast_block_in_tab(state, &block_id, &tab_id);
145
146    Ok(PaneOpenResult::docked(block_id, tab_id, cmd.view.clone(), true))
147}