Skip to main content

infinity_agent_core/system/local/
sender.rs

1//! The in-process [`InputSender`] used by the local driver.
2
3use async_trait::async_trait;
4use tokio::sync::mpsc;
5
6use crate::message::InputMessage;
7use crate::traits::InputSender;
8
9/// Error returned if the local system has been shut down (daemon exit).
10#[derive(Debug)]
11pub struct ChannelSendError;
12
13impl std::fmt::Display for ChannelSendError {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        write!(f, "agent system input channel closed")
16    }
17}
18
19impl std::error::Error for ChannelSendError {}
20
21/// An [`InputSender`] that delivers messages to a running local agent system.
22/// This is the loopback path for everything the runtime schedules for later:
23/// tool results, child-thread seed messages, reports to parents, timer
24/// wake-ups.
25///
26/// A local system runs for the lifetime of its process (threads idle out
27/// individually; see [`RunningSystem`](super::RunningSystem)), so sends only
28/// fail after an explicit whole-system shutdown.
29#[derive(Clone)]
30pub struct ChannelSender {
31    tx: mpsc::UnboundedSender<(InputMessage, String)>,
32}
33
34impl ChannelSender {
35    pub(crate) fn new_pair() -> (Self, mpsc::UnboundedReceiver<(InputMessage, String)>) {
36        let (tx, rx) = mpsc::unbounded_channel();
37        (Self { tx }, rx)
38    }
39}
40
41#[async_trait]
42impl InputSender for ChannelSender {
43    type Error = ChannelSendError;
44
45    async fn send_to_input_queue(
46        &self,
47        message: InputMessage,
48        dedup_id: &str,
49    ) -> Result<(), ChannelSendError> {
50        self.tx
51            .send((message, dedup_id.to_owned()))
52            .map_err(|_| ChannelSendError)
53    }
54}