Skip to main content

agentmux_srv/server/app_api/
other_build_layouts.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `layout.otherBuilds` / `layout.restoreOtherBuild` — the RPC shell over
5//! `server::service::session_other_builds`.
6
7use super::*;
8use crate::server::service::session_other_builds::{self as other_builds, ScanRoots};
9
10pub fn register(engine: &Arc<WshRpcEngine>, state: &AppState) {
11    let list_state = state.clone();
12    engine.register_typed(COMMAND_LAYOUT_OTHER_BUILDS, move |req: CommandOtherBuildLayoutsData, _ctx| {
13        let state = list_state.clone();
14        async move { list_other_builds(&state, req, ScanRoots::live).await }
15    });
16
17    let restore_state = state.clone();
18    engine.register_typed(COMMAND_LAYOUT_RESTORE_OTHER_BUILD, move |req: CommandRestoreOtherBuildLayoutData, _ctx| {
19        let state = restore_state.clone();
20        async move { restore_other_build(&state, req, ScanRoots::live()).await }
21    });
22}
23
24/// `layout.otherBuilds`. `roots` is asked for only once there is something to look for: nothing is read for an
25/// `offer_only` request from a window with no layout to offer over.
26pub(crate) async fn list_other_builds(
27    state: &AppState,
28    req: CommandOtherBuildLayoutsData,
29    roots: impl FnOnce() -> Option<ScanRoots>,
30) -> Result<OtherBuildLayoutsResult, String> {
31    let offer = state
32        .mstore
33        .get::<obj::Window>(&req.window_id)
34        .ok()
35        .flatten()
36        .and_then(|w| other_builds::untouched_default_tab(&state.mstore, &w.workspaceid))
37        .is_some();
38    if !offer && req.offer_only.unwrap_or(false) {
39        return Ok(OtherBuildLayoutsResult { offer, layouts: Vec::new() });
40    }
41    let Some(roots) = roots() else {
42        return Ok(OtherBuildLayoutsResult { offer, layouts: Vec::new() });
43    };
44    // Opens databases: off the async workers.
45    let layouts = tokio::task::spawn_blocking(move || other_builds::discover(&roots, other_builds::SCAN_BUDGET))
46        .await
47        .map_err(|e| format!("layout.otherBuilds: task: {e}"))?;
48    Ok(OtherBuildLayoutsResult { offer, layouts })
49}
50
51/// `layout.restoreOtherBuild`. `roots` is `None` when this AgentMux's data folders are unknown.
52pub(crate) async fn restore_other_build(
53    state: &AppState,
54    req: CommandRestoreOtherBuildLayoutData,
55    roots: Option<ScanRoots>,
56) -> Result<RestoreOtherBuildLayoutResult, String> {
57    let roots = roots.ok_or_else(|| "layout.restoreOtherBuild: this AgentMux's data folders are unknown".to_string())?;
58    other_builds::restore(state, &roots, &req.window_id, &req.channel, &req.version)
59        .await
60        .map_err(|e| format!("layout.restoreOtherBuild: {e}"))
61}
62
63#[cfg(test)]
64#[path = "other_build_layouts_tests.rs"]
65mod tests;