mofa-foundation 0.1.1

MoFA Foundation - Core building blocks and utilities
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! 多 Agent 协作模式
//!
//! 提供高级的多 Agent 协作模式,包括:
//!
//! - **链式协作**: Agent 串行执行,前一个输出是后一个输入
//! - **并行协作**: 多个 Agent 同时处理,结果聚合
//! - **辩论模式**: 多个 Agent 交替辩论,达成共识
//! - **监督模式**: 一个监督 Agent 评估其他 Agent 的输出
//! - **MapReduce**: 并行处理后归约
//!
//! # 示例
//!
//! ```rust,ignore
//! use mofa_foundation::llm::multi_agent::{AgentTeam, TeamPattern};
//!
//! // 创建 Agent 团队
//! let team = AgentTeam::new()
//!     .add_agent("analyst", analyst_agent)
//!     .add_agent("writer", writer_agent)
//!     .add_agent("editor", editor_agent)
//!     .with_pattern(TeamPattern::Chain)
//!     .build();
//!
//! let result = team.run("Analyze and write about Rust").await?;
//! ```

use super::agent::LLMAgent;
use super::types::{LLMError, LLMResult};
use std::collections::HashMap;
use std::sync::Arc;

/// Agent 团队协作模式
#[derive(Debug, Clone)]
pub enum TeamPattern {
    /// 链式:按顺序执行
    Chain,
    /// 并行:同时执行,结果聚合
    Parallel,
    /// 辩论:多个 Agent 交替发言
    Debate {
        /// 最大轮数
        max_rounds: usize,
    },
    /// 监督:一个监督者评估结果
    Supervised,
    /// MapReduce:并行处理后归约
    MapReduce,
    /// 自定义
    Custom,
}

/// Agent 角色
#[derive(Debug, Clone)]
pub struct AgentRole {
    /// 角色 ID
    pub id: String,
    /// 角色名称
    pub name: String,
    /// 角色描述(会添加到系统提示中)
    pub description: String,
    /// 提示词模板
    pub prompt_template: Option<String>,
}

impl AgentRole {
    pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            description: String::new(),
            prompt_template: None,
        }
    }

    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    pub fn with_template(mut self, template: impl Into<String>) -> Self {
        self.prompt_template = Some(template.into());
        self
    }
}

/// Agent 成员
pub struct AgentMember {
    /// 角色信息
    pub role: AgentRole,
    /// Agent 实例
    pub agent: Arc<LLMAgent>,
}

impl AgentMember {
    pub fn new(id: impl Into<String>, agent: Arc<LLMAgent>) -> Self {
        let id = id.into();
        Self {
            role: AgentRole::new(&id, &id),
            agent,
        }
    }

    pub fn with_role(mut self, role: AgentRole) -> Self {
        self.role = role;
        self
    }

    /// 执行任务
    pub async fn execute(&self, input: &str, context: Option<&str>) -> LLMResult<String> {
        let prompt = if let Some(ref template) = self.role.prompt_template {
            let mut p = template.replace("{input}", input);
            if let Some(ctx) = context {
                p = p.replace("{context}", ctx);
            }
            p
        } else if let Some(ctx) = context {
            format!("Context:\n{}\n\nTask:\n{}", ctx, input)
        } else {
            input.to_string()
        };

        self.agent.ask(&prompt).await
    }
}

/// Agent 团队
pub struct AgentTeam {
    /// 团队 ID
    pub id: String,
    /// 团队名称
    pub name: String,
    /// 成员列表
    members: Vec<AgentMember>,
    /// 成员映射(按 ID)
    member_map: HashMap<String, usize>,
    /// 协作模式
    pattern: TeamPattern,
    /// 监督者 ID(用于 Supervised 模式)
    supervisor_id: Option<String>,
    /// 聚合提示词(用于并行和 MapReduce 模式)
    aggregate_prompt: Option<String>,
}

impl AgentTeam {
    /// 创建新的 Agent 团队
    pub fn new(id: impl Into<String>) -> AgentTeamBuilder {
        AgentTeamBuilder::new(id)
    }

    /// 链式执行
    async fn run_chain(&self, input: &str) -> LLMResult<String> {
        let mut current_output = input.to_string();

        for member in &self.members {
            current_output = member.execute(&current_output, None).await?;
        }

        Ok(current_output)
    }

    /// 并行执行
    async fn run_parallel(&self, input: &str) -> LLMResult<String> {
        let mut results = Vec::new();

        // 由于 Agent 包含不可跨线程的闭包,这里顺序执行
        // 未来可以通过重构 Agent 来实现真正的并行
        for member in &self.members {
            let result = member.execute(input, None).await?;
            results.push((member.role.id.clone(), result));
        }

        // 聚合结果
        let aggregated = results
            .iter()
            .map(|(id, result)| format!("=== {} ===\n{}", id, result))
            .collect::<Vec<_>>()
            .join("\n\n");

        // 如果有聚合提示词,使用第一个 Agent 进行聚合
        if let Some(ref aggregate_prompt) = self.aggregate_prompt
            && let Some(first_member) = self.members.first()
        {
            let prompt = aggregate_prompt
                .replace("{results}", &aggregated)
                .replace("{input}", input);
            return first_member.agent.ask(&prompt).await;
        }

        Ok(aggregated)
    }

    /// 辩论执行
    async fn run_debate(&self, input: &str, max_rounds: usize) -> LLMResult<String> {
        if self.members.len() < 2 {
            return Err(LLMError::Other(
                "Debate requires at least 2 agents".to_string(),
            ));
        }

        let mut context = format!("Initial topic: {}\n\n", input);
        let mut last_response = String::new();

        for round in 0..max_rounds {
            for (i, member) in self.members.iter().enumerate() {
                let prompt = format!(
                    "Round {}, Speaker {}: {}\n\n\
                    Previous discussion:\n{}\n\n\
                    Please provide your perspective. Be constructive and build on previous points.",
                    round + 1,
                    i + 1,
                    member.role.name,
                    context
                );

                let response = member.execute(&prompt, None).await?;
                context.push_str(&format!(
                    "[{} - Round {}]:\n{}\n\n",
                    member.role.name,
                    round + 1,
                    response
                ));
                last_response = response;
            }
        }

        // 最后总结
        if let Some(first_member) = self.members.first() {
            let summary_prompt = format!(
                "Based on the following debate, provide a concise summary of the key points \
                and conclusions:\n\n{}",
                context
            );
            first_member.agent.ask(&summary_prompt).await
        } else {
            Ok(last_response)
        }
    }

    /// 监督执行
    async fn run_supervised(&self, input: &str) -> LLMResult<String> {
        let supervisor_id = self.supervisor_id.as_ref().ok_or_else(|| {
            LLMError::Other("Supervisor not specified for Supervised pattern".to_string())
        })?;

        let supervisor_idx = self
            .member_map
            .get(supervisor_id)
            .ok_or_else(|| LLMError::Other(format!("Supervisor '{}' not found", supervisor_id)))?;

        // 收集工作者结果
        let mut worker_results = Vec::new();
        for (i, member) in self.members.iter().enumerate() {
            if i != *supervisor_idx {
                let result = member.execute(input, None).await?;
                worker_results.push((member.role.id.clone(), member.role.name.clone(), result));
            }
        }

        // 让监督者评估
        let results_text = worker_results
            .iter()
            .map(|(id, name, result)| format!("=== {} ({}) ===\n{}", name, id, result))
            .collect::<Vec<_>>()
            .join("\n\n");

        let supervisor = &self.members[*supervisor_idx];
        let eval_prompt = format!(
            "You are the supervisor. Evaluate the following responses to the task: \"{}\"\n\n\
            Responses:\n{}\n\n\
            Please provide:\n\
            1. An evaluation of each response\n\
            2. The best response or a synthesized improved response\n\
            3. Suggestions for improvement",
            input, results_text
        );

        supervisor.agent.ask(&eval_prompt).await
    }

    /// MapReduce 执行
    async fn run_map_reduce(&self, input: &str) -> LLMResult<String> {
        // Map 阶段:每个 Agent 处理输入
        let mut mapped_results = Vec::new();
        for member in &self.members {
            let result = member.execute(input, None).await?;
            mapped_results.push((member.role.id.clone(), result));
        }

        // Reduce 阶段:聚合结果
        let reduce_input = mapped_results
            .iter()
            .map(|(id, result)| format!("[{}]: {}", id, result))
            .collect::<Vec<_>>()
            .join("\n\n");

        let reduce_prompt = if let Some(ref aggregate_prompt) = self.aggregate_prompt {
            aggregate_prompt
                .replace("{results}", &reduce_input)
                .replace("{input}", input)
        } else {
            format!(
                "Synthesize the following results into a coherent response:\n\n{}\n\n\
                Original task: {}",
                reduce_input, input
            )
        };

        // 使用第一个 Agent 进行 reduce
        if let Some(first_member) = self.members.first() {
            first_member.agent.ask(&reduce_prompt).await
        } else {
            Ok(reduce_input)
        }
    }

    /// 执行团队任务
    pub async fn run(&self, input: impl Into<String>) -> LLMResult<String> {
        let input = input.into();

        match &self.pattern {
            TeamPattern::Chain => self.run_chain(&input).await,
            TeamPattern::Parallel => self.run_parallel(&input).await,
            TeamPattern::Debate { max_rounds } => self.run_debate(&input, *max_rounds).await,
            TeamPattern::Supervised => self.run_supervised(&input).await,
            TeamPattern::MapReduce => self.run_map_reduce(&input).await,
            TeamPattern::Custom => {
                // 自定义模式默认使用链式
                self.run_chain(&input).await
            }
        }
    }

    /// 获取成员
    pub fn get_member(&self, id: &str) -> Option<&AgentMember> {
        self.member_map.get(id).map(|idx| &self.members[*idx])
    }

    /// 获取所有成员 ID
    pub fn member_ids(&self) -> Vec<&str> {
        self.members.iter().map(|m| m.role.id.as_str()).collect()
    }
}

/// Agent 团队构建器
pub struct AgentTeamBuilder {
    id: String,
    name: String,
    members: Vec<AgentMember>,
    pattern: TeamPattern,
    supervisor_id: Option<String>,
    aggregate_prompt: Option<String>,
}

impl AgentTeamBuilder {
    pub fn new(id: impl Into<String>) -> Self {
        let id = id.into();
        Self {
            name: id.clone(),
            id,
            members: Vec::new(),
            pattern: TeamPattern::Chain,
            supervisor_id: None,
            aggregate_prompt: None,
        }
    }

    /// 设置名称
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// 添加成员
    pub fn add_member(mut self, id: impl Into<String>, agent: Arc<LLMAgent>) -> Self {
        self.members.push(AgentMember::new(id, agent));
        self
    }

    /// 添加带角色的成员
    pub fn add_member_with_role(mut self, agent: Arc<LLMAgent>, role: AgentRole) -> Self {
        let member = AgentMember::new(&role.id, agent).with_role(role);
        self.members.push(member);
        self
    }

    /// 设置协作模式
    pub fn with_pattern(mut self, pattern: TeamPattern) -> Self {
        self.pattern = pattern;
        self
    }

    /// 设置监督者
    pub fn with_supervisor(mut self, supervisor_id: impl Into<String>) -> Self {
        self.supervisor_id = Some(supervisor_id.into());
        self.pattern = TeamPattern::Supervised;
        self
    }

    /// 设置聚合提示词
    pub fn with_aggregate_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.aggregate_prompt = Some(prompt.into());
        self
    }

    /// 构建团队
    pub fn build(self) -> AgentTeam {
        let member_map: HashMap<String, usize> = self
            .members
            .iter()
            .enumerate()
            .map(|(i, m)| (m.role.id.clone(), i))
            .collect();

        AgentTeam {
            id: self.id,
            name: self.name,
            members: self.members,
            member_map,
            pattern: self.pattern,
            supervisor_id: self.supervisor_id,
            aggregate_prompt: self.aggregate_prompt,
        }
    }
}

// ============================================================================
// 预定义团队模式
// ============================================================================

/// 创建内容创作团队
///
/// 包含:研究员、写手、编辑
pub fn content_creation_team(
    researcher: Arc<LLMAgent>,
    writer: Arc<LLMAgent>,
    editor: Arc<LLMAgent>,
) -> AgentTeam {
    AgentTeamBuilder::new("content-creation")
        .with_name("Content Creation Team")
        .add_member_with_role(
            researcher,
            AgentRole::new("researcher", "Researcher")
                .with_description("Research and gather information on the topic")
                .with_template(
                    "Research the following topic thoroughly and provide key findings:\n\n{input}",
                ),
        )
        .add_member_with_role(
            writer,
            AgentRole::new("writer", "Writer")
                .with_description("Write engaging content based on research")
                .with_template(
                    "Based on the following research, write an engaging article:\n\n{input}",
                ),
        )
        .add_member_with_role(
            editor,
            AgentRole::new("editor", "Editor")
                .with_description("Edit and polish the content")
                .with_template(
                    "Edit and improve the following article for clarity and engagement:\n\n{input}",
                ),
        )
        .with_pattern(TeamPattern::Chain)
        .build()
}

/// 创建代码审查团队
///
/// 包含:安全审查员、性能审查员、风格审查员、监督者
pub fn code_review_team(
    security_reviewer: Arc<LLMAgent>,
    performance_reviewer: Arc<LLMAgent>,
    style_reviewer: Arc<LLMAgent>,
    supervisor: Arc<LLMAgent>,
) -> AgentTeam {
    AgentTeamBuilder::new("code-review")
        .with_name("Code Review Team")
        .add_member_with_role(
            security_reviewer,
            AgentRole::new("security", "Security Reviewer")
                .with_description("Review code for security vulnerabilities"),
        )
        .add_member_with_role(
            performance_reviewer,
            AgentRole::new("performance", "Performance Reviewer")
                .with_description("Review code for performance issues"),
        )
        .add_member_with_role(
            style_reviewer,
            AgentRole::new("style", "Style Reviewer")
                .with_description("Review code for style and best practices"),
        )
        .add_member_with_role(
            supervisor,
            AgentRole::new("supervisor", "Lead Reviewer")
                .with_description("Synthesize reviews and provide final feedback"),
        )
        .with_supervisor("supervisor")
        .build()
}

/// 创建辩论团队
///
/// 两个 Agent 进行辩论
pub fn debate_team(agent1: Arc<LLMAgent>, agent2: Arc<LLMAgent>, max_rounds: usize) -> AgentTeam {
    AgentTeamBuilder::new("debate")
        .with_name("Debate Team")
        .add_member_with_role(
            agent1,
            AgentRole::new("debater1", "Debater 1")
                .with_description("Present and defend your position"),
        )
        .add_member_with_role(
            agent2,
            AgentRole::new("debater2", "Debater 2")
                .with_description("Present an alternative perspective"),
        )
        .with_pattern(TeamPattern::Debate { max_rounds })
        .build()
}

/// 创建分析团队
///
/// 多个 Agent 并行分析,然后聚合结果
pub fn analysis_team(analysts: Vec<(impl Into<String>, Arc<LLMAgent>)>) -> AgentTeam {
    let mut builder = AgentTeamBuilder::new("analysis")
        .with_name("Analysis Team")
        .with_pattern(TeamPattern::MapReduce)
        .with_aggregate_prompt(
            "Synthesize the following analyses into a comprehensive report:\n\n{results}\n\n\
            Original question: {input}",
        );

    for (id, agent) in analysts {
        builder = builder.add_member(id, agent);
    }

    builder.build()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_team_builder() {
        // 创建一个没有实际 Agent 的团队(仅测试构建器)
        let builder = AgentTeamBuilder::new("test-team")
            .with_name("Test Team")
            .with_pattern(TeamPattern::Chain);

        // 只测试构建器的配置,不测试实际执行
        assert_eq!(builder.id, "test-team");
        assert_eq!(builder.name, "Test Team");
    }

    #[test]
    fn test_agent_role() {
        let role = AgentRole::new("researcher", "Researcher")
            .with_description("Research topics")
            .with_template("{input}");

        assert_eq!(role.id, "researcher");
        assert_eq!(role.name, "Researcher");
        assert_eq!(role.description, "Research topics");
        assert!(role.prompt_template.is_some());
    }
}