Skip to main content

agentmux_srv/server/
http_layout.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Layout, window, workspace and tab queries and actions.
5//! Split out of server/mod.rs unchanged (SPEC_LARGE_FILE_MODULE_ANALYSIS_2026_09_30.md §4.1).
6
7use super::*;
8
9/// `GET /api/v1/layout` — read-only window → workspace → tab → pane tree.
10/// The floating panes agents opened say so and where they are (`pane_place::report_floaters`); the docked panes in the
11/// window's showing tab say how big they are drawn (`pane_measure::report_docked`).
12pub(super) async fn handle_layout(State(state): State<AppState>) -> impl IntoResponse {
13    let mut layout = service::agent_layout(&state.mstore);
14    pane_place::report_floaters(&state, &mut layout).await;
15    pane_measure::report_docked(&state, &mut layout).await;
16    Json(layout)
17}
18
19/// `GET /api/v1/windows` — flat list of windows.
20pub(super) async fn handle_list_windows(State(state): State<AppState>) -> impl IntoResponse {
21    Json(service::agent_windows(&state.mstore))
22}
23
24/// `GET /api/v1/workspaces` — flat list of workspaces.
25pub(super) async fn handle_list_workspaces(State(state): State<AppState>) -> impl IntoResponse {
26    Json(service::agent_workspaces(&state.mstore))
27}
28
29#[derive(serde::Deserialize)]
30pub(super) struct ListTabsQuery {
31    /// Limit to this workspace's tabs. Omit (or pass `block_id`) for all tabs.
32    #[serde(default)]
33    pub(super) workspace_id: Option<String>,
34    /// Calling agent's block id — scopes to the caller's own workspace when
35    /// `workspace_id` is omitted.
36    #[serde(default)]
37    pub(super) block_id: Option<String>,
38}
39
40/// `GET /api/v1/tabs` — flat list of tabs, optionally scoped to a workspace
41/// (explicit `workspace_id`, or the caller's own via `block_id`).
42pub(super) async fn handle_list_tabs(
43    State(state): State<AppState>,
44    Query(q): Query<ListTabsQuery>,
45) -> impl IntoResponse {
46    let ws_id = q.workspace_id.filter(|w| !w.is_empty()).or_else(|| {
47        q.block_id
48            .filter(|b| !b.is_empty())
49            .and_then(|b| service::resolve_agent_context(&state.mstore, &b).ok())
50            .and_then(|ctx| ctx.workspace_id)
51    });
52    Json(service::agent_tabs(&state.mstore, ws_id.as_deref()))
53}
54
55/// `POST /api/v1/tab/activate` — make `tab_id` the active tab in its
56/// workspace. Routes through `workspace.SetActiveTab`.
57pub(super) async fn handle_tab_activate(
58    State(state): State<AppState>,
59    Json(req): Json<TabActivateRequest>,
60) -> impl IntoResponse {
61    let tab_id = req.tab_id.trim().to_string();
62    if tab_id.is_empty() {
63        return (StatusCode::BAD_REQUEST, Json(json!({ "error": "missing tab_id" }))).into_response();
64    }
65    let ws_id = match service::workspace_id_for_tab(&state.mstore, &tab_id) {
66        Some(w) => w,
67        None => return (StatusCode::NOT_FOUND, Json(json!({ "error": format!("no workspace owns tab {tab_id}") }))).into_response(),
68    };
69    let call = crate::backend::service::WebCallType {
70        service: "workspace".to_string(),
71        method: "SetActiveTab".to_string(),
72        uicontext: None,
73        args: vec![json!(ws_id), json!(tab_id)],
74    };
75    finish_name_call(&state, call, json!({ "success": true, "tab_id": tab_id })).await
76}
77
78/// `POST /api/v1/tab/new` — create (and activate) a new tab in the caller's
79/// workspace (or an explicit `workspace_id`). Routes through
80/// `workspace.CreateTab`.
81pub(super) async fn handle_tab_new(
82    State(state): State<AppState>,
83    Json(req): Json<TabNewRequest>,
84) -> impl IntoResponse {
85    let name = req.name.map(|n| n.trim().chars().take(128).collect::<String>()).unwrap_or_default();
86    let workspace_id = match req.workspace_id.filter(|w| !w.is_empty()) {
87        Some(w) => w,
88        None => match resolve_own(&state, req.block_id, |c| c.workspace_id.clone()) {
89            Ok(w) => w,
90            Err(resp) => return resp,
91        },
92    };
93    let call = crate::backend::service::WebCallType {
94        service: "workspace".to_string(),
95        method: "CreateTab".to_string(),
96        uicontext: None,
97        // [ws_id, name, activate]; empty name → backend auto-names tab{N}.
98        args: vec![json!(workspace_id), json!(name), json!(true)],
99    };
100    finish_name_call(&state, call, json!({ "success": true, "workspace_id": workspace_id })).await
101}
102
103/// `POST /api/v1/window/focus` — bring a window to the foreground. Defaults to
104/// the caller's own window. Routes through `client.FocusWindow`.
105pub(super) async fn handle_window_focus(
106    State(state): State<AppState>,
107    Json(req): Json<WindowFocusRequest>,
108) -> impl IntoResponse {
109    let window_id = match req.window_id.filter(|w| !w.is_empty()) {
110        Some(w) => w,
111        None => match resolve_own(&state, req.block_id, |c| c.window_id.clone()) {
112            Ok(w) => w,
113            Err(resp) => return resp,
114        },
115    };
116    let call = crate::backend::service::WebCallType {
117        service: "client".to_string(),
118        method: "FocusWindow".to_string(),
119        uicontext: None,
120        args: vec![json!(window_id)],
121    };
122    finish_name_call(&state, call, json!({ "success": true, "window_id": window_id })).await
123}