Skip to main content

mofa_kernel/agent/
mod.rs

1//! 统一 Agent 框架
2//!
3//! 提供模块化、可组合、可扩展的 Agent 架构
4//!
5//! # 架构概述
6//!
7//! ```text
8//! ┌─────────────────────────────────────────────────────────────────────┐
9//! │                         统一 Agent 框架                              │
10//! ├─────────────────────────────────────────────────────────────────────┤
11//! │  ┌─────────────────────────────────────────────────────────────┐    │
12//! │  │                    MoFAAgent Trait                            │    │
13//! │  │  (统一 Agent 接口:id, capabilities, execute, interrupt)      │    │
14//! │  └───────────────────────────┬─────────────────────────────────┘    │
15//! │                              │                                       │
16//! │  ┌───────────────────────────┼───────────────────────────────────┐  │
17//! │  │           Modular Components (组件化设计)                      │  │
18//! │  │  ┌──────────┐  ┌──────────┐  ┌────────┐  ┌──────────────┐    │  │
19//! │  │  │ Reasoner │  │  Tool    │  │ Memory │  │  Coordinator │    │  │
20//! │  │  │   推理器  │  │  工具    │  │ 记忆    │  │   协调器      │    │  │
21//! │  │  └──────────┘  └──────────┘  └────────┘  └──────────────┘    │  │
22//! │  └───────────────────────────────────────────────────────────────┘  │
23//! │                                                                      │
24//! │  ┌─────────────────────────────────────────────────────────────┐    │
25//! │  │        AgentRegistry (runtime 注册中心实现)                    │    │
26//! │  └─────────────────────────────────────────────────────────────┘    │
27//! │                                                                      │
28//! │  ┌─────────────────────────────────────────────────────────────┐    │
29//! │  │               CoreAgentContext (统一上下文)                       │    │
30//! │  └─────────────────────────────────────────────────────────────┘    │
31//! └─────────────────────────────────────────────────────────────────────┘
32//! ```
33//!
34//! # 核心概念
35//!
36//! ## MoFAAgent Trait
37//!
38//! 所有 Agent 实现的统一接口:
39//!
40//! ```rust,ignore
41//! use mofa_kernel::agent::prelude::*;
42//!
43//! #[async_trait]
44//! impl MoFAAgent for MyAgent {
45//!     fn id(&self) -> &str { "my-agent" }
46//!     fn name(&self) -> &str { "My Agent" }
47//!     fn capabilities(&self) -> &AgentCapabilities { &self.caps }
48//!
49//!     async fn initialize(&mut self, ctx: &CoreAgentContext) -> AgentResult<()> {
50//!         Ok(())
51//!     }
52//!
53//!     async fn execute(&mut self, input: AgentInput, ctx: &CoreAgentContext) -> AgentResult<AgentOutput> {
54//!         Ok(AgentOutput::text("Hello!"))
55//!     }
56//!
57//!     async fn interrupt(&mut self) -> AgentResult<InterruptResult> {
58//!         Ok(InterruptResult::Acknowledged)
59//!     }
60//!
61//!     async fn shutdown(&mut self) -> AgentResult<()> {
62//!         Ok(())
63//!     }
64//!
65//!     fn state(&self) -> AgentState {
66//!         AgentState::Ready
67//!     }
68//! }
69//! ```
70//!
71//! ## AgentCapabilities
72//!
73//! 描述 Agent 的能力,用于发现和路由:
74//!
75//! ```rust,ignore
76//! let caps = AgentCapabilities::builder()
77//!     .tag("llm")
78//!     .tag("coding")
79//!     .input_type(InputType::Text)
80//!     .output_type(OutputType::Text)
81//!     .supports_streaming(true)
82//!     .supports_tools(true)
83//!     .build();
84//! ```
85//!
86//! ## CoreAgentContext
87//!
88//! 执行上下文,在 Agent 执行过程中传递状态:
89//!
90//! ```rust,ignore
91//! let ctx = CoreAgentContext::new("execution-123");
92//! ctx.set("user_id", "user-456").await;
93//! ctx.emit_event(AgentEvent::new("task_started", json!({}))).await;
94//! ```
95//!
96//! # 模块结构
97//!
98//! - `core` - AgentCore 微内核接口(最小化核心)
99//! - `traits` - MoFAAgent trait 定义
100//! - `types` - AgentInput, AgentOutput, AgentState 等类型
101//! - `capabilities` - AgentCapabilities 能力描述
102//! - `context` - CoreAgentContext 执行上下文
103//! - `error` - 错误类型定义
104//! - `components` - 组件 trait (Reasoner, Tool, Memory, Coordinator)
105//! - `config` - 配置系统
106//! - `registry` - Agent 注册中心
107//! - `tools` - 统一工具系统
108
109// 核心模块
110pub mod capabilities;
111pub mod context;
112pub mod core;
113pub mod error;
114pub mod traits;
115pub mod types;
116
117// 组件模块
118pub mod components;
119
120// 配置模块
121pub mod config;
122
123// 注册中心
124pub mod registry;
125
126// 工具系统
127
128// 执行引擎与运行器已迁移到 mofa-runtime
129
130// 秘书Agent抽象
131pub mod plugins;
132pub mod secretary;
133
134// AgentPlugin 统一到 plugin 模块
135pub use crate::plugin::AgentPlugin;
136// 重新导出核心类型
137pub use capabilities::{
138    AgentCapabilities, AgentCapabilitiesBuilder, AgentRequirements, AgentRequirementsBuilder,
139    ReasoningStrategy,
140};
141pub use context::{AgentContext, AgentEvent, ContextConfig, EventBus};
142pub use core::{
143    // MoFAAgent - 统一的 Agent 接口
144    AgentLifecycle,
145    AgentMessage,
146    AgentMessaging,
147    AgentPluginSupport,
148    MoFAAgent,
149};
150pub use error::{AgentError, AgentResult};
151pub use traits::{AgentMetadata, AgentStats, DynAgent, HealthStatus};
152pub use types::event::execution as execution_events;
153// Event type constants are available via types::event::lifecycle, types::event::execution, etc.
154// Note: Aliased to avoid conflict with existing modules (plugins, etc.)
155pub use types::event::lifecycle;
156pub use types::event::message as message_events;
157pub use types::event::plugin as plugin_events;
158pub use types::event::state as state_events;
159pub use types::{
160    AgentInput,
161    AgentOutput,
162    AgentState,
163    // LLM types
164    ChatCompletionRequest,
165    ChatCompletionResponse,
166    ChatMessage,
167    ErrorCategory,
168    ErrorContext,
169    EventBuilder,
170    GlobalError,
171    GlobalEvent,
172    GlobalMessage,
173    GlobalResult,
174    InputType,
175    InterruptResult,
176    LLMProvider,
177    MessageContent,
178    MessageMetadata,
179    OutputContent,
180    // Global types
181    OutputType,
182    ReasoningStep,
183    ReasoningStepType,
184    TokenUsage,
185    ToolCall,
186    ToolDefinition,
187    ToolUsage,
188};
189
190// 重新导出组件
191pub use components::{
192    coordinator::{CoordinationPattern, Coordinator},
193    mcp::{McpClient, McpServerConfig, McpServerInfo, McpToolInfo, McpTransportConfig},
194    memory::{Memory, MemoryItem, MemoryStats, MemoryValue, Message, MessageRole},
195    reasoner::{Reasoner, ReasoningResult},
196    tool::{Tool, ToolDescriptor, ToolInput, ToolMetadata, ToolResult},
197};
198
199// 重新导出工厂接口
200pub use registry::AgentFactory;
201
202// 重新导出配置
203pub use config::{AgentConfig, AgentType};
204#[cfg(feature = "config")]
205pub use config::{ConfigFormat, ConfigLoader};
206
207/// Prelude 模块 - 常用类型导入
208pub mod prelude {
209    pub use super::capabilities::{
210        AgentCapabilities, AgentCapabilitiesBuilder, AgentRequirements, ReasoningStrategy,
211    };
212    pub use super::context::{AgentContext, AgentEvent, ContextConfig};
213    pub use super::core::{
214        // MoFAAgent - 统一的 Agent 接口
215        AgentLifecycle,
216        AgentMessage,
217        AgentMessaging,
218        AgentPluginSupport,
219        MoFAAgent,
220    };
221    pub use super::error::{AgentError, AgentResult};
222    pub use super::traits::{AgentMetadata, DynAgent, HealthStatus};
223    pub use super::types::{
224        AgentInput,
225        AgentOutput,
226        AgentState,
227        // LLM types
228        ChatCompletionRequest,
229        ChatMessage,
230        InputType,
231        InterruptResult,
232        LLMProvider,
233        OutputType,
234        TokenUsage,
235        ToolUsage,
236    };
237    // AgentPlugin 统一到 plugin 模块
238    pub use crate::plugin::AgentPlugin;
239    pub use async_trait::async_trait;
240}