Skip to main content

mofa_foundation/react/
mod.rs

1//! ReAct (Reasoning + Acting) Agent 框架
2//!
3//! 基于 ractor Actor 模型实现的 ReAct Agent,支持:
4//!
5//! - **思考-行动-观察循环**: 标准 ReAct 推理模式
6//! - **工具调用**: 支持自定义工具注册和执行
7//! - **Actor 模型**: 基于 ractor 实现,支持并发和消息传递
8//! - **AutoAgent**: 自动选择最佳行动策略
9//! - **流式输出**: 支持流式思考过程输出
10//!
11//! # 架构
12//!
13//! ```text
14//! ┌─────────────────────────────────────────────────────────────────┐
15//! │                     ReAct Agent 架构                             │
16//! ├─────────────────────────────────────────────────────────────────┤
17//! │                                                                 │
18//! │  ┌─────────────┐      ┌─────────────┐      ┌─────────────┐     │
19//! │  │   Input     │─────▶│  Thought    │─────▶│   Action    │     │
20//! │  │  (任务)     │      │  (推理)     │      │  (行动)     │     │
21//! │  └─────────────┘      └─────────────┘      └──────┬──────┘     │
22//! │                                                   │            │
23//! │                                                   ▼            │
24//! │  ┌─────────────┐      ┌─────────────┐      ┌─────────────┐     │
25//! │  │   Output    │◀─────│   Final     │◀─────│ Observation │     │
26//! │  │   (结果)    │      │  Answer     │      │  (观察)     │     │
27//! │  └─────────────┘      └─────────────┘      └─────────────┘     │
28//! │                                                                 │
29//! │  ┌─────────────────────────────────────────────────────────┐   │
30//! │  │                     Tool Registry                        │   │
31//! │  │  ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐               │   │
32//! │  │  │Tool1│ │Tool2│ │Tool3│ │Tool4│ │ ... │               │   │
33//! │  │  └─────┘ └─────┘ └─────┘ └─────┘ └─────┘               │   │
34//! │  └─────────────────────────────────────────────────────────┘   │
35//! │                                                                 │
36//! └─────────────────────────────────────────────────────────────────┘
37//! ```
38//!
39//! # 示例
40//!
41//! ## 基本用法
42//!
43//! ```rust,ignore
44//! use mofa_foundation::react::{ReActAgent, ReActTool};
45//! use std::sync::Arc;
46//!
47//! // 定义工具
48//! struct SearchTool;
49//!
50//! #[async_trait::async_trait]
51//! impl ReActTool for SearchTool {
52//!     fn name(&self) -> &str { "search" }
53//!     fn description(&self) -> &str { "Search the web for information" }
54//!
55//!     async fn execute(&self, input: &str) -> Result<String, String> {
56//!         Ok(format!("Search results for: {}", input))
57//!     }
58//! }
59//!
60//! // 创建 ReAct Agent
61//! let agent = ReActAgent::builder()
62//!     .with_llm(llm_agent)
63//!     .with_tool(Arc::new(SearchTool))
64//!     .with_max_iterations(5)
65//!     .build()?;
66//!
67//! // 执行任务
68//! let result = agent.run("What is the capital of France?").await?;
69//! info!("Answer: {}", result.answer);
70//! ```
71//!
72//! ## 使用 Actor 模型
73//!
74//! ```rust,ignore
75//! use mofa_foundation::react::{ReActActorRef, spawn_react_agent};
76//!
77//! // 启动 ReAct Actor
78//! let (actor, handle) = spawn_react_agent(config).await?;
79//!
80//! // 发送任务
81//! let result = actor.run_task("Analyze this data").await?;
82//! ```
83
84mod actor;
85mod core;
86pub mod patterns;
87pub mod tools;
88
89pub use actor::*;
90pub use core::*;
91pub use patterns::*;
92pub use tools::*;
93
94/// 便捷 prelude 模块
95pub mod prelude {
96    pub use super::patterns::{
97        AgentOutput, AgentUnit, AggregationStrategy, ChainAgent, ChainResult, ChainStepResult,
98        MapReduceAgent, MapReduceResult, ParallelAgent, ParallelResult, ParallelStepResult,
99        chain_agents, parallel_agents, parallel_agents_with_summarizer,
100    };
101    pub use super::tools::prelude::*;
102}