Skip to main content

MoFAAgent

Trait MoFAAgent 

Source
pub trait MoFAAgent:
    Send
    + Sync
    + 'static {
    // Required methods
    fn id(&self) -> &str;
    fn name(&self) -> &str;
    fn capabilities(&self) -> &AgentCapabilities;
    fn initialize<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        ctx: &'life1 AgentContext,
    ) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        input: AgentInput,
        ctx: &'life1 AgentContext,
    ) -> Pin<Box<dyn Future<Output = AgentResult<AgentOutput>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn shutdown<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn state(&self) -> AgentState;

    // Provided method
    fn interrupt<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = AgentResult<InterruptResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

MoFA Agent 统一接口

这是 MoFA 框架中所有 Agent 必须实现的统一接口。 整合了之前的 AgentCore 和 MoFAAgent 功能,提供一致的抽象。

§设计原则

  1. 统一接口:所有 Agent 实现同一个 trait
  2. 最小化核心:只包含最基本的方法
  3. 可选扩展:通过扩展 trait 提供额外功能
  4. 一致性:统一接口与扩展约定

§必须实现的方法

  • id() - 唯一标识符
  • name() - 人类可读名称
  • capabilities() - 能力描述
  • initialize() - 初始化
  • execute() - 执行任务(核心方法)
  • shutdown() - 关闭
  • state() - 获取状态

§示例

use mofa_kernel::agent::core::MoFAAgent;
use mofa_kernel::agent::prelude::*;

struct MyAgent {
    id: String,
    name: String,
    capabilities: AgentCapabilities,
    state: AgentState,
}

#[async_trait]
impl MoFAAgent for MyAgent {
    fn id(&self) -> &str { &self.id }
    fn name(&self) -> &str { &self.name }
    fn capabilities(&self) -> &AgentCapabilities { &self.capabilities }
    fn state(&self) -> AgentState { self.state.clone() }

    async fn initialize(&mut self, _ctx: &CoreAgentContext) -> AgentResult<()> {
        self.state = AgentState::Ready;
        Ok(())
    }

    async fn execute(&mut self, input: AgentInput, _ctx: &CoreAgentContext) -> AgentResult<AgentOutput> {
        // 处理输入并返回输出
        Ok(AgentOutput::text("Response"))
    }

    async fn shutdown(&mut self) -> AgentResult<()> {
        self.state = AgentState::Shutdown;
        Ok(())
    }
}

Required Methods§

Source

fn id(&self) -> &str

获取唯一标识符

ID 应该在 Agent 的整个生命周期内保持不变。 用于在注册中心、日志、监控系统中唯一标识 Agent。

Source

fn name(&self) -> &str

获取人类可读名称

名称用于显示和日志记录,不需要唯一。

Source

fn capabilities(&self) -> &AgentCapabilities

获取能力描述

返回 Agent 支持的能力,用于:

  • Agent 发现和路由
  • 能力匹配和验证
  • 多智能体协调
Source

fn initialize<'life0, 'life1, 'async_trait>( &'life0 mut self, ctx: &'life1 AgentContext, ) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

初始化 Agent

在执行任务前调用,用于:

  • 加载资源和配置
  • 建立连接(数据库、网络等)
  • 初始化内部状态
§参数
  • ctx: 执行上下文,提供配置、事件总线等
§状态转换

Created -> Initializing -> Ready

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 mut self, input: AgentInput, ctx: &'life1 AgentContext, ) -> Pin<Box<dyn Future<Output = AgentResult<AgentOutput>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

执行任务 - 核心方法

这是 Agent 的主要执行方法,处理输入并返回输出。

§参数
  • input: 输入数据,支持多种格式(文本、JSON、二进制等)
  • ctx: 执行上下文,提供状态、事件、中断等
§返回

返回执行结果,包含输出内容、元数据、工具使用等。

§状态转换

Ready -> Executing -> Ready

Source

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

关闭 Agent

优雅关闭,释放资源:

  • 保存状态
  • 关闭连接
  • 清理资源
§状态转换
  • -> ShuttingDown -> Shutdown
Source

fn state(&self) -> AgentState

获取当前状态

返回 Agent 的当前状态,用于:

  • 健康检查
  • 状态监控
  • 状态转换验证

Provided Methods§

Source

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

中断 Agent

发送中断信号,Agent 可以选择如何响应:

  • 立即停止
  • 完成当前步骤后停止
  • 忽略中断
§返回

返回中断处理结果。

§默认实现

默认返回 InterruptResult::Acknowledged

§状态转换

Executing -> Interrupted

Implementors§