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 功能,提供一致的抽象。
§设计原则
- 统一接口:所有 Agent 实现同一个 trait
- 最小化核心:只包含最基本的方法
- 可选扩展:通过扩展 trait 提供额外功能
- 一致性:统一接口与扩展约定
§必须实现的方法
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§
Sourcefn capabilities(&self) -> &AgentCapabilities
fn capabilities(&self) -> &AgentCapabilities
获取能力描述
返回 Agent 支持的能力,用于:
- Agent 发现和路由
- 能力匹配和验证
- 多智能体协调
Sourcefn 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 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,
Sourcefn 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 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,
Sourcefn shutdown<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: '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,
Sourcefn state(&self) -> AgentState
fn state(&self) -> AgentState
获取当前状态
返回 Agent 的当前状态,用于:
- 健康检查
- 状态监控
- 状态转换验证