Skip to main content

AgentMessaging

Trait AgentMessaging 

Source
pub trait AgentMessaging: MoFAAgent {
    // Required methods
    fn handle_message<'life0, 'async_trait>(
        &'life0 mut self,
        msg: AgentMessage,
    ) -> Pin<Box<dyn Future<Output = AgentResult<AgentMessage>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn handle_event<'life0, 'async_trait>(
        &'life0 mut self,
        event: AgentEvent,
    ) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

Agent 消息处理扩展

提供消息和事件处理能力,用于需要与其他 Agent 或系统交互的场景。 这个 trait 是可选的,只有需要消息处理的 Agent 才需要实现。

§提供的方法

  • handle_message() - 处理消息
  • handle_event() - 处理事件

§示例

use mofa_kernel::agent::core::AgentMessaging;

#[async_trait]
impl AgentMessaging for MyAgent {
    async fn handle_message(&mut self, msg: AgentMessage) -> AgentResult<AgentMessage> {
        // 处理消息并返回响应
        Ok(AgentMessage::new("response"))
    }

    async fn handle_event(&mut self, event: AgentEvent) -> AgentResult<()> {
        // 处理事件
        Ok(())
    }
}

Required Methods§

Source

fn handle_message<'life0, 'async_trait>( &'life0 mut self, msg: AgentMessage, ) -> Pin<Box<dyn Future<Output = AgentResult<AgentMessage>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

处理消息

接收来自其他 Agent 或系统的消息,处理后返回响应。

§参数
  • msg: 接收到的消息
§返回

返回响应消息

Source

fn handle_event<'life0, 'async_trait>( &'life0 mut self, event: AgentEvent, ) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

处理事件

处理来自事件总线的事件,用于响应系统级别的通知。

§参数
  • event: 事件对象

Implementors§