Skip to main content

agentmux_srv/server/app_api/agent_spawn/
team.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Where a leader's workers open: a tab of their own, "<Leader> team", in the leader's workspace,
5//! made the first time and reused after. Without it a leader that lives in the user's main tab
6//! fills that tab with its workers.
7
8use crate::backend::obj::{Tab, Workspace};
9use crate::backend::service::WebCallType;
10use crate::backend::storage::store::Store;
11
12use super::*;
13
14/// The tab named `name` among `workspace`'s tabs (any case), if there is one.
15pub(in crate::server::app_api) fn tab_named(mstore: &Store, workspace: &Workspace, name: &str) -> Option<String> {
16    workspace.tabids.iter().find(|id| mstore.get::<Tab>(id).ok().flatten().is_some_and(|t| t.name.trim().eq_ignore_ascii_case(name))).cloned()
17}
18
19/// The tab `leader`'s workers open in. `leaders_tab` (where the leader's pane is, or where it works from
20/// when parked) is the way out: if the workspace can't be found or the tab can't be made, the workers
21/// open there as they did before. Both callers hold the leader's lock (`leader_lock`) from before the
22/// user is asked until the last worker is open, so one leader's find-or-make never overlaps itself.
23pub(super) async fn team_tab(state: &AppState, leader: &Leader, leaders_tab: &str) -> String {
24    let Some(workspace) = state.mstore.get_all::<Workspace>().unwrap_or_default().into_iter().find(|w| w.tabids.iter().any(|t| t == leaders_tab)) else {
25        return leaders_tab.to_string();
26    };
27    let name = format!("{} team", leader.def.name.trim());
28    if let Some(existing) = tab_named(&state.mstore, &workspace, &name) {
29        return existing;
30    }
31    // Not shown: the user's view stays on the tab they are in.
32    let call = WebCallType {
33        service: "workspace".to_string(),
34        method: "CreateTab".to_string(),
35        uicontext: None,
36        args: vec![json!(workspace.oid), json!(name), json!(false)],
37    };
38    let made = crate::server::service::handle_create_tab(state, &call).await;
39    match made.data.as_ref().and_then(|d| d.as_str()) {
40        Some(id) if made.error.is_none() => id.to_string(),
41        _ => {
42            tracing::warn!(leader = %leader.def.name, error = ?made.error, "agent.spawn: no team tab, the worker opens beside the leader");
43            leaders_tab.to_string()
44        }
45    }
46}