1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! LLM 模块
//!
//! 提供 LLM (Large Language Model) 集成支持
//!
//! # 架构
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────────┐
//! │ LLM 模块架构 │
//! ├─────────────────────────────────────────────────────────────────────┤
//! │ │
//! │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
//! │ │ LLMClient │───▶│ Provider │───▶│ 具体实现 │ │
//! │ │ (高级API) │ │ (trait) │ │ - OpenAI │ │
//! │ └─────────────┘ └─────────────┘ │ - Anthropic │ │
//! │ │ │ - Ollama │ │
//! │ ▼ │ - 自定义... │ │
//! │ ┌─────────────┐ └─────────────────────────┘ │
//! │ │ ChatSession │ │
//! │ │ (会话管理) │ │
//! │ └─────────────┘ │
//! │ │ │
//! │ ▼ │
//! │ ┌─────────────┐ ┌─────────────┐ │
//! │ │ LLMPlugin │───▶│ AgentPlugin │ ← 集成到 MoFA Agent │
//! │ │ (插件封装) │ │ (trait) │ │
//! │ └─────────────┘ └─────────────┘ │
//! │ │ │
//! │ ▼ │
//! │ ┌─────────────────────────────────────────────────────────────┐ │
//! │ │ 高级 API │ │
//! │ ├─────────────────────────────────────────────────────────────┤ │
//! │ │ AgentWorkflow │ 多 Agent 工作流编排 │ │
//! │ │ AgentTeam │ 团队协作模式 (链式/并行/辩论/监督) │ │
//! │ │ Pipeline │ 函数式流水线 API │ │
//! │ └─────────────────────────────────────────────────────────────┘ │
//! │ │
//! └─────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # 快速开始
//!
//! ## 1. 实现自定义 LLM Provider
//!
//! ```rust,ignore
//! use mofa_foundation::llm::{LLMProvider, ChatCompletionRequest, ChatCompletionResponse, LLMResult};
//!
//! struct MyLLMProvider {
//! api_key: String,
//! }
//!
//! #[async_trait::async_trait]
//! impl LLMProvider for MyLLMProvider {
//! fn name(&self) -> &str {
//! "my-llm"
//! }
//!
//! fn default_model(&self) -> &str {
//! "my-model-v1"
//! }
//!
//! async fn chat(&self, request: ChatCompletionRequest) -> LLMResult<ChatCompletionResponse> {
//! // 实现具体的 API 调用逻辑
//! todo!()
//! }
//! }
//! ```
//!
//! ## 2. 使用 LLMClient 进行对话
//!
//! ```rust,ignore
//! use mofa_foundation::llm::{LLMClient, ChatMessage};
//! use std::sync::Arc;
//!
//! let provider = Arc::new(MyLLMProvider::new("api-key"));
//! let client = LLMClient::new(provider);
//!
//! // 简单问答
//! let answer = client.ask("What is Rust?").await?;
//!
//! // 带系统提示的对话
//! let response = client
//! .chat()
//! .system("You are a helpful coding assistant.")
//! .user("How do I read a file in Rust?")
//! .temperature(0.7)
//! .max_tokens(1000)
//! .send()
//! .await?;
//!
//! info!("{}", response.content().unwrap());
//! ```
//!
//! ## 3. 使用工具调用
//!
//! ```rust,ignore
//! use mofa_foundation::llm::{LLMClient, Tool, ToolExecutor};
//! use serde_json::json;
//!
//! // 定义工具
//! let weather_tool = Tool::function(
//! "get_weather",
//! "Get weather for a location",
//! json!({
//! "type": "object",
//! "properties": {
//! "location": { "type": "string" }
//! },
//! "required": ["location"]
//! })
//! );
//!
//! // 实现工具执行器
//! struct MyToolExecutor;
//!
//! #[async_trait::async_trait]
//! impl ToolExecutor for MyToolExecutor {
//! async fn execute(&self, name: &str, arguments: &str) -> LLMResult<String> {
//! match name {
//! "get_weather" => Ok(r#"{"temp": 22, "condition": "sunny"}"#.to_string()),
//! _ => Err(LLMError::Other("Unknown tool".to_string()))
//! }
//! }
//!
//! async fn available_tools(&self) -> LLMResult<Vec<Tool>> {
//! Ok(vec![weather_tool.clone()])
//! }
//! }
//!
//! // 使用自动工具调用
//! let response = client
//! .chat()
//! .system("You can use tools to help answer questions.")
//! .user("What's the weather in Tokyo?")
//! .tool(weather_tool)
//! .with_tool_executor(Arc::new(MyToolExecutor))
//! .send_with_tools()
//! .await?;
//! ```
//!
//! ## 4. 作为插件集成到 Agent
//!
//! ```rust,ignore
//! use mofa_foundation::llm::{LLMPlugin, LLMConfig};
//! use mofa_sdk::kernel::MoFAAgent;
//! use mofa_sdk::runtime::AgentBuilder;
//!
//! // 创建 LLM 插件
//! let llm_plugin = LLMPlugin::new("openai-llm", provider);
//!
//! // 添加到 Agent
//! let runtime = AgentBuilder::new("my-agent", "My Agent")
//! .with_plugin(Box::new(llm_plugin))
//! .with_agent(agent)
//! .await?;
//! ```
//!
//! ## 5. 使用会话管理
//!
//! ```rust,ignore
//! use mofa_foundation::llm::{LLMClient, ChatSession};
//!
//! let client = LLMClient::new(provider);
//! let mut session = ChatSession::new(client)
//! .with_system("You are a helpful assistant.");
//!
//! // 多轮对话
//! let r1 = session.send("Hello!").await?;
//! let r2 = session.send("What did I just say?").await?; // 会记住上下文
//!
//! // 清空历史
//! session.clear();
//! ```
//!
//! # 高级 API
//!
//! ## 6. Agent 工作流编排 (AgentWorkflow)
//!
//! 创建复杂的多 Agent 工作流,支持条件分支、并行执行、聚合等。
//!
//! ```rust,ignore
//! use mofa_foundation::llm::{AgentWorkflow, LLMAgent};
//! use std::sync::Arc;
//!
//! // 创建简单的 Agent 链
//! let workflow = agent_chain("content-pipeline", vec![
//! ("researcher", researcher_agent.clone()),
//! ("writer", writer_agent.clone()),
//! ("editor", editor_agent.clone()),
//! ]);
//!
//! let result = workflow.run("Write an article about Rust").await?;
//!
//! // 使用构建器创建更复杂的工作流
//! let workflow = AgentWorkflow::new("complex-pipeline")
//! .add_agent("analyzer", analyzer_agent)
//! .add_agent("writer", writer_agent)
//! .add_llm_router("router", router_agent, vec!["technical", "creative"])
//! .connect("start", "analyzer")
//! .connect("analyzer", "router")
//! .connect_on("router", "technical", "technical")
//! .connect_on("router", "creative", "creative")
//! .build();
//! ```
//!
//! ## 7. Agent 团队协作 (AgentTeam)
//!
//! 支持多种协作模式:链式、并行、辩论、监督、MapReduce。
//!
//! ```rust,ignore
//! use mofa_foundation::llm::{AgentTeam, TeamPattern, AgentRole};
//!
//! // 使用预定义的团队模式
//! let team = content_creation_team(researcher, writer, editor);
//! let article = team.run("Write about AI safety").await?;
//!
//! // 自定义团队
//! let team = AgentTeam::new("analysis-team")
//! .add_member("expert1", expert1_agent)
//! .add_member("expert2", expert2_agent)
//! .add_member("synthesizer", synthesizer_agent)
//! .with_pattern(TeamPattern::MapReduce)
//! .with_aggregate_prompt("Synthesize: {results}")
//! .build();
//!
//! // 辩论模式
//! let debate = debate_team(agent1, agent2, 3); // 3 轮辩论
//! let conclusion = debate.run("Is Rust better than Go?").await?;
//! ```
//!
//! ## 8. 函数式流水线 (Pipeline)
//!
//! 提供简洁的函数式 API 构建 Agent 处理流程。
//!
//! ```rust,ignore
//! use mofa_foundation::llm::Pipeline;
//!
//! // 简单流水线
//! let result = Pipeline::new()
//! .with_agent(translator)
//! .map(|s| s.to_uppercase())
//! .with_agent(summarizer)
//! .run("Translate and summarize this text")
//! .await?;
//!
//! // 带模板的流水线
//! let result = Pipeline::new()
//! .with_agent_template(agent, "Please analyze: {input}")
//! .map(|s| format!("Analysis: {}", s))
//! .run("Some data to analyze")
//! .await?;
//!
//! // 流式流水线
//! let stream = StreamPipeline::new(agent)
//! .with_template("Tell me about {input}")
//! .run_stream("Rust programming")
//! .await?;
//! ```
// 高级 API
// Framework components
// Audio processing
// Re-export 核心类型
pub use ;
pub use ;
pub use ;
pub use RetryExecutor;
pub use ToolExecutor;
pub use ;
pub use *;
// Re-export 标准 LLM Agent
pub use ;
// Re-export agent_from_config (when openai feature is enabled)
pub use agent_from_config;
// Re-export OpenAI Provider (when enabled)
pub use ;
// Re-export Anthropic Provider
pub use ;
// Re-export Google Gemini Provider
pub use ;
// Re-export Ollama Provider
pub use ;
// Re-export 高级 API
pub use ;
pub use ;
pub use ;
// Re-export framework components
pub use ;
pub use ;
pub use ;
pub use ;
// ImageDetail is already re-exported via types::*;
// Compatibility re-export for older AgentLoopToolExecutor name
pub use ToolExecutor as AgentLoopToolExecutor;
// Re-export transcription module
pub use ;