Skip to main content

agentmux_srv/server/app_api/
agent_quit.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Ending an agent from the person's window: `/shutdown` in its composer (`agentquit`) and Shut down in its pane
5//! menu or My agents (`agentshutdown`). Both are the person's own choice, so only the AgentMux window makes them:
6//! an agent's connection, or one that has not proven it is the window, is refused. An agent ends another with
7//! `Agents stop`, which gives the person time to keep it running; the generic service route does the same
8//! (`service::object_methods`).
9
10use super::*;
11use crate::sagas::self_quit::{self, QuitOrigin};
12
13/// What a caller is told when it is not the window.
14pub(crate) const ENDING_AN_AGENT_IS_THE_PERSONS: &str =
15    "only the person shuts an agent down from its composer or menu: an agent stops another with Agents stop, which lets the person keep it running";
16
17fn summary_of(result: Result<self_quit::QuitSummary, String>) -> Result<serde_json::Value, String> {
18    result.map(|summary| serde_json::to_value(&summary).unwrap_or_default())
19}
20
21pub fn register(engine: &Arc<WshRpcEngine>, state: &AppState) {
22    let quit_state = state.clone();
23    engine.register_typed(COMMAND_AGENT_QUIT, move |cmd: CommandAgentQuitData, ctx| {
24        let state = quit_state.clone();
25        async move {
26            ctx.operator_only(ENDING_AN_AGENT_IS_THE_PERSONS)?;
27            summary_of(self_quit::run(&state, &cmd.blockid, QuitOrigin::UserSlash).await)
28        }
29    });
30    let shutdown_state = state.clone();
31    engine.register_typed(COMMAND_AGENT_SHUTDOWN, move |cmd: CommandAgentShutdownData, ctx| {
32        let state = shutdown_state.clone();
33        async move {
34            ctx.operator_only(ENDING_AN_AGENT_IS_THE_PERSONS)?;
35            let stop_background = cmd.stop_background.unwrap_or(false);
36            summary_of(self_quit::run_shutdown(&state, &cmd.blockid, QuitOrigin::UserMenu, stop_background).await)
37        }
38    });
39}