Skip to main content

agentmux_srv_core/backend/blockcontroller/
session_agent.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The controllers that keep one agent process and a protocol session in it:
5//! ACP agents (`acp`) and Muse Code over MSP (`msp`). The message paths
6//! (`agentinput`, `agent.send`, reactive delivery) treat them alike: start the
7//! agent again if it exited, then hand it the message.
8
9use super::acp::AcpController;
10use crate::backend::message_origin::RecordOrigin;
11use super::msp::MspController;
12use super::{Controller, SendOutcome};
13
14pub trait SessionAgent {
15    /// Its process is running.
16    fn is_running(&self) -> bool;
17    /// Start it again from the pane's meta if its process exited.
18    fn ensure_started(&self, block_meta: crate::backend::obj::MetaMapType) -> Result<(), String>;
19    /// Send a user message as a prompt (a turn). `record` says where it came from, for the transcript
20    /// record of it where the agent's protocol leaves none ([`AcpController::send_message_from`]). `Deferred`: the
21    /// agent has not seen it yet (no session yet, or a turn in flight that it must end first).
22    fn send_message(&self, message: String, message_id: Option<&str>, record: Option<&RecordOrigin>) -> Result<SendOutcome, String>;
23}
24
25impl SessionAgent for AcpController {
26    fn is_running(&self) -> bool {
27        AcpController::is_running(self)
28    }
29    fn ensure_started(&self, block_meta: crate::backend::obj::MetaMapType) -> Result<(), String> {
30        AcpController::ensure_started(self, block_meta)
31    }
32    fn send_message(&self, message: String, message_id: Option<&str>, record: Option<&RecordOrigin>) -> Result<SendOutcome, String> {
33        AcpController::send_message_from(self, message, message_id, record)
34    }
35}
36
37impl SessionAgent for MspController {
38    fn is_running(&self) -> bool {
39        MspController::is_running(self)
40    }
41    fn ensure_started(&self, block_meta: crate::backend::obj::MetaMapType) -> Result<(), String> {
42        MspController::ensure_started(self, block_meta)
43    }
44    // Muse Code's own frames carry the user's message (`msp-translator.ts`), so there is no record to add.
45    fn send_message(&self, message: String, message_id: Option<&str>, _record: Option<&RecordOrigin>) -> Result<SendOutcome, String> {
46        MspController::send_message(self, message, message_id)?;
47        Ok(SendOutcome::Sent)
48    }
49}
50
51/// `ctrl` as a session agent, when it is one.
52pub fn session_agent(ctrl: &dyn Controller) -> Option<&dyn SessionAgent> {
53    let any = ctrl.as_any();
54    if let Some(acp) = any.downcast_ref::<AcpController>() {
55        return Some(acp);
56    }
57    any.downcast_ref::<MspController>().map(|msp| msp as &dyn SessionAgent)
58}
59
60/// Whether `controller_type` names a session agent's controller.
61pub fn is_session_agent_type(controller_type: &str) -> bool {
62    controller_type == super::BLOCK_CONTROLLER_ACP || controller_type == super::BLOCK_CONTROLLER_MSP
63}