Skip to main content

aios_core/
action_bus.rs

1//! 动作总线 — 事件派发与消费
2//!
3//! 基于 tokio mpsc channel 的多生产者-单消费者事件总线。
4//! 用于 collector → core 的事件传输和 agent → core 的意图回传。
5//!
6//! `Sender` 端可以自由克隆分发给采集/推理任务;
7//! `Receiver` 端由处理管道独占消费,不对外暴露。
8
9use aios_spec::{IngestedRawEvent, IntentBatch};
10use tokio::sync::mpsc;
11
12/// 动作总线
13///
14/// 包含两条独立通道:
15/// - raw_events: collector 向 core 推送已贴上 SourceTier 的原始事件
16/// - intents: agent 向 core 回传意图批次
17pub struct ActionBus {
18    raw_events_rx: mpsc::Receiver<IngestedRawEvent>,
19    raw_events_tx: mpsc::Sender<IngestedRawEvent>,
20    intent_rx: mpsc::Receiver<IntentBatch>,
21    intent_tx: mpsc::Sender<IntentBatch>,
22}
23
24impl ActionBus {
25    pub fn new(capacity: usize) -> Self {
26        let (raw_events_tx, raw_events_rx) = mpsc::channel(capacity);
27        let (intent_tx, intent_rx) = mpsc::channel(capacity);
28        Self {
29            raw_events_rx,
30            raw_events_tx,
31            intent_rx,
32            intent_tx,
33        }
34    }
35
36    /// 获取原始事件发送端的克隆(给 collector 任务)
37    pub fn raw_sender(&self) -> mpsc::Sender<IngestedRawEvent> {
38        self.raw_events_tx.clone()
39    }
40
41    /// 获取意图发送端的克隆(给 agent 任务)
42    pub fn intent_sender(&self) -> mpsc::Sender<IntentBatch> {
43        self.intent_tx.clone()
44    }
45
46    /// 阻塞等待下一个原始事件(处理管道独占调用)
47    pub async fn recv_raw(&mut self) -> Option<IngestedRawEvent> {
48        self.raw_events_rx.recv().await
49    }
50
51    /// 阻塞等待下一个意图批次(策略审查独占调用)
52    pub async fn recv_intent(&mut self) -> Option<IntentBatch> {
53        self.intent_rx.recv().await
54    }
55}
56
57impl Default for ActionBus {
58    fn default() -> Self {
59        Self::new(1024)
60    }
61}