infinity_agent_core/system/local/
sender.rs1use async_trait::async_trait;
4use tokio::sync::mpsc;
5
6use crate::message::InputMessage;
7use crate::traits::InputSender;
8
9#[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#[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}