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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! LLM 驱动的协作协议实现
//!
//! 本模块提供 MoFA 框架的标准协作协议实现,**所有协议都可以选择性地使用 LLM**。
//!
//! # 核心理念
//!
//! 在 Agent 框架中,协作协议应该能够:
//! 1. 使用 LLM 来理解和处理自然语言消息
//! 2. 通过 LLM 进行智能决策
//! 3. 记录 LLM 的推理过程

// 导出类型定义
pub mod types;

// 重新导出核心类型
pub use types::*;

use crate::llm::LLMClient;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// 获取协作模式的描述
fn get_mode_description(mode: &CollaborationMode) -> &'static str {
    match mode {
        CollaborationMode::RequestResponse => "一对一的请求-响应,适合确定性任务",
        CollaborationMode::PublishSubscribe => "一对多的发布订阅,适合发散性任务",
        CollaborationMode::Consensus => "多 Agent 共识决策,适合需要达成一致的场景",
        CollaborationMode::Debate => "多轮辩论优化,适合需要迭代改进的审查任务",
        CollaborationMode::Parallel => "并行处理独立任务,适合可分解的工作",
        CollaborationMode::Sequential => "顺序执行有依赖的任务链",
        CollaborationMode::Custom(_) => "自定义模式",
    }
}

// ============================================================================
// LLM 辅助的协作协议基类
// ============================================================================

/// LLM 辅助协议基类
///
/// 提供协议与 LLM 交互的基础设施
pub struct LLMProtocolHelper {
    /// Agent ID
    agent_id: String,
    /// LLM 客户端(可选)
    llm_client: Option<Arc<LLMClient>>,
    /// 是否使用 LLM 处理消息
    use_llm: bool,
}

impl LLMProtocolHelper {
    /// 创建新的 LLM 协议辅助器
    pub fn new(agent_id: impl Into<String>) -> Self {
        Self {
            agent_id: agent_id.into(),
            llm_client: None,
            use_llm: false,
        }
    }

    /// 设置 LLM 客户端
    pub fn with_llm(mut self, llm_client: Arc<LLMClient>) -> Self {
        self.llm_client = Some(llm_client);
        self.use_llm = true;
        self
    }

    /// 启用/禁用 LLM
    pub fn with_use_llm(mut self, use_llm: bool) -> Self {
        self.use_llm = use_llm;
        self
    }

    /// 获取 Agent ID
    pub fn agent_id(&self) -> &str {
        &self.agent_id
    }

    /// 使用 LLM 处理消息
    pub async fn process_with_llm(
        &self,
        msg: &CollaborationMessage,
        system_prompt: &str,
    ) -> anyhow::Result<CollaborationContent> {
        let llm_client = self
            .llm_client
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("LLM client not configured"))?;

        let user_prompt = format!(
            "你是 {}。收到一条协作消息:\n\n发送者: {}\n内容: {}\n\n请处理这条消息并返回响应。",
            self.agent_id,
            msg.sender,
            msg.content.to_text()
        );

        let response = llm_client
            .chat()
            .system(system_prompt)
            .user(&user_prompt)
            .send()
            .await?;

        Ok(CollaborationContent::LLMResponse {
            reasoning: "通过 LLM 分析和处理协作消息".to_string(),
            conclusion: response.content().unwrap_or("").to_string(),
            data: serde_json::json!({
                "original_sender": msg.sender,
                "original_content": msg.content.to_text(),
            }),
        })
    }
}

// ============================================================================
// 请求-响应协议
// ============================================================================

/// 请求-响应协作协议
///
/// 可选择使用 LLM 来智能处理请求
pub struct RequestResponseProtocol {
    helper: LLMProtocolHelper,
    message_queue: Arc<RwLock<Vec<CollaborationMessage>>>,
}

impl RequestResponseProtocol {
    /// 创建新的请求-响应协议(不使用 LLM)
    pub fn new(agent_id: impl Into<String>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id),
            message_queue: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// 创建支持 LLM 的请求-响应协议
    pub fn with_llm(agent_id: impl Into<String>, llm_client: Arc<LLMClient>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id).with_llm(llm_client),
            message_queue: Arc::new(RwLock::new(Vec::new())),
        }
    }
}

#[async_trait]
impl CollaborationProtocol for RequestResponseProtocol {
    fn name(&self) -> &str {
        "request_response"
    }

    fn description(&self) -> &str {
        "请求-响应协作协议:一对一通信,同步等待结果"
    }

    fn applicable_scenarios(&self) -> Vec<String> {
        vec![
            "数据查询和处理".to_string(),
            "确定性任务执行".to_string(),
            "状态获取".to_string(),
            "简单问答".to_string(),
        ]
    }

    fn mode(&self) -> CollaborationMode {
        CollaborationMode::RequestResponse
    }

    async fn send_message(&self, msg: CollaborationMessage) -> anyhow::Result<()> {
        let mut queue = self.message_queue.write().await;
        queue.push(msg);
        Ok(())
    }

    async fn receive_message(&self) -> anyhow::Result<Option<CollaborationMessage>> {
        let mut queue = self.message_queue.write().await;
        Ok(queue.pop())
    }

    async fn process_message(
        &self,
        msg: CollaborationMessage,
    ) -> anyhow::Result<CollaborationResult> {
        let start = std::time::Instant::now();

        tracing::debug!(
            "[RequestResponse] {} processing message from {}",
            self.helper.agent_id(),
            msg.sender
        );

        // 如果配置了 LLM,使用 LLM 处理
        let content = if self.helper.use_llm {
            self.helper
                .process_with_llm(
                    &msg,
                    "你是一个协作 Agent,负责处理请求-响应模式的协作消息。请理解请求内容并提供准确的响应。",
                )
                .await?
        } else {
            CollaborationContent::Text(format!("已收到来自 {} 的请求并处理", msg.sender))
        };

        let duration = start.elapsed().as_millis() as u64;

        Ok(
            CollaborationResult::success(content, duration, CollaborationMode::RequestResponse)
                .with_participant(self.helper.agent_id().to_string())
                .with_participant(msg.sender),
        )
    }

    fn stats(&self) -> HashMap<String, serde_json::Value> {
        HashMap::from([
            (
                "agent_id".to_string(),
                serde_json::json!(self.helper.agent_id()),
            ),
            (
                "use_llm".to_string(),
                serde_json::json!(self.helper.use_llm),
            ),
        ])
    }
}

// ============================================================================
// 发布-订阅协议
// ============================================================================

/// 发布-订阅协作协议
pub struct PublishSubscribeProtocol {
    helper: LLMProtocolHelper,
    message_queue: Arc<RwLock<Vec<CollaborationMessage>>>,
    subscribed_topics: Arc<RwLock<std::collections::HashSet<String>>>,
}

impl PublishSubscribeProtocol {
    /// 创建新的发布-订阅协议(不使用 LLM)
    pub fn new(agent_id: impl Into<String>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id),
            message_queue: Arc::new(RwLock::new(Vec::new())),
            subscribed_topics: Arc::new(RwLock::new(std::collections::HashSet::new())),
        }
    }

    /// 创建支持 LLM 的发布-订阅协议
    pub fn with_llm(agent_id: impl Into<String>, llm_client: Arc<LLMClient>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id).with_llm(llm_client),
            message_queue: Arc::new(RwLock::new(Vec::new())),
            subscribed_topics: Arc::new(RwLock::new(std::collections::HashSet::new())),
        }
    }

    /// 订阅主题
    pub async fn subscribe(&self, topic: String) -> anyhow::Result<()> {
        let mut subscribed = self.subscribed_topics.write().await;
        subscribed.insert(topic.clone());
        tracing::debug!(
            "[PublishSubscribe] {} subscribed to topic: {}",
            self.helper.agent_id(),
            topic
        );
        Ok(())
    }
}

#[async_trait]
impl CollaborationProtocol for PublishSubscribeProtocol {
    fn name(&self) -> &str {
        "publish_subscribe"
    }

    fn description(&self) -> &str {
        "发布-订阅协作协议:一对多通信,适合发散性任务和创意生成"
    }

    fn applicable_scenarios(&self) -> Vec<String> {
        vec![
            "创意生成和发散".to_string(),
            "通知广播".to_string(),
            "事件传播".to_string(),
            "多人协作".to_string(),
        ]
    }

    fn mode(&self) -> CollaborationMode {
        CollaborationMode::PublishSubscribe
    }

    async fn send_message(&self, msg: CollaborationMessage) -> anyhow::Result<()> {
        let mut queue = self.message_queue.write().await;
        queue.push(msg);
        Ok(())
    }

    async fn receive_message(&self) -> anyhow::Result<Option<CollaborationMessage>> {
        let mut queue = self.message_queue.write().await;
        Ok(queue.pop())
    }

    async fn process_message(
        &self,
        msg: CollaborationMessage,
    ) -> anyhow::Result<CollaborationResult> {
        let start = std::time::Instant::now();

        tracing::debug!(
            "[PublishSubscribe] {} processing message from {} on topic {:?}",
            self.helper.agent_id(),
            msg.sender,
            msg.topic
        );

        let content = if self.helper.use_llm {
            self.helper
                .process_with_llm(
                    &msg,
                    "你是一个协作 Agent,负责处理发布-订阅模式的协作消息。消息是发布给多个订阅者的,请提供适当的响应。",
                )
                .await?
        } else {
            CollaborationContent::Text(format!("已发布消息到主题 {:?}", msg.topic))
        };

        let duration = start.elapsed().as_millis() as u64;

        Ok(
            CollaborationResult::success(content, duration, CollaborationMode::PublishSubscribe)
                .with_participant(self.helper.agent_id().to_string())
                .with_participant(msg.sender),
        )
    }

    fn stats(&self) -> HashMap<String, serde_json::Value> {
        let topics = self.subscribed_topics.blocking_read();
        HashMap::from([
            (
                "agent_id".to_string(),
                serde_json::json!(self.helper.agent_id()),
            ),
            (
                "use_llm".to_string(),
                serde_json::json!(self.helper.use_llm),
            ),
            (
                "subscribed_topics".to_string(),
                serde_json::json!(topics.len()),
            ),
        ])
    }
}

// ============================================================================
// 共识协议
// ============================================================================

/// 共识协作协议
pub struct ConsensusProtocol {
    helper: LLMProtocolHelper,
    message_queue: Arc<RwLock<Vec<CollaborationMessage>>>,
    threshold: f32,
}

impl ConsensusProtocol {
    /// 创建新的共识协议(不使用 LLM)
    pub fn new(agent_id: impl Into<String>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id),
            message_queue: Arc::new(RwLock::new(Vec::new())),
            threshold: 0.7,
        }
    }

    /// 创建支持 LLM 的共识协议
    pub fn with_llm(agent_id: impl Into<String>, llm_client: Arc<LLMClient>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id).with_llm(llm_client),
            message_queue: Arc::new(RwLock::new(Vec::new())),
            threshold: 0.7,
        }
    }
}

#[async_trait]
impl CollaborationProtocol for ConsensusProtocol {
    fn name(&self) -> &str {
        "consensus"
    }

    fn description(&self) -> &str {
        "共识协作协议:多 Agent 协商决策,适合需要达成一致的场景"
    }

    fn applicable_scenarios(&self) -> Vec<String> {
        vec![
            "决策制定".to_string(),
            "投票评估".to_string(),
            "方案选择".to_string(),
            "质量评审".to_string(),
        ]
    }

    fn mode(&self) -> CollaborationMode {
        CollaborationMode::Consensus
    }

    async fn send_message(&self, msg: CollaborationMessage) -> anyhow::Result<()> {
        let mut queue = self.message_queue.write().await;
        queue.push(msg);
        Ok(())
    }

    async fn receive_message(&self) -> anyhow::Result<Option<CollaborationMessage>> {
        let mut queue = self.message_queue.write().await;
        Ok(queue.pop())
    }

    async fn process_message(
        &self,
        msg: CollaborationMessage,
    ) -> anyhow::Result<CollaborationResult> {
        let start = std::time::Instant::now();

        tracing::debug!(
            "[Consensus] {} processing message from {}",
            self.helper.agent_id(),
            msg.sender
        );

        let content = if self.helper.use_llm {
            self.helper
                .process_with_llm(
                    &msg,
                    "你是一个协作 Agent,负责处理共识机制的协作消息。请分析多方意见并提供综合判断。",
                )
                .await?
        } else {
            CollaborationContent::Text(format!("已参与共识决策,阈值: {}", self.threshold))
        };

        let duration = start.elapsed().as_millis() as u64;

        Ok(
            CollaborationResult::success(content, duration, CollaborationMode::Consensus)
                .with_participant(self.helper.agent_id().to_string())
                .with_participant(msg.sender),
        )
    }

    fn stats(&self) -> HashMap<String, serde_json::Value> {
        HashMap::from([
            (
                "agent_id".to_string(),
                serde_json::json!(self.helper.agent_id()),
            ),
            (
                "use_llm".to_string(),
                serde_json::json!(self.helper.use_llm),
            ),
            ("threshold".to_string(), serde_json::json!(self.threshold)),
        ])
    }
}

// ============================================================================
// 辩论协议
// ============================================================================

/// 辩论协作协议
pub struct DebateProtocol {
    helper: LLMProtocolHelper,
    message_queue: Arc<RwLock<Vec<CollaborationMessage>>>,
    max_rounds: usize,
}

impl DebateProtocol {
    /// 创建新的辩论协议(不使用 LLM)
    pub fn new(agent_id: impl Into<String>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id),
            message_queue: Arc::new(RwLock::new(Vec::new())),
            max_rounds: 3,
        }
    }

    /// 创建支持 LLM 的辩论协议
    pub fn with_llm(agent_id: impl Into<String>, llm_client: Arc<LLMClient>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id).with_llm(llm_client),
            message_queue: Arc::new(RwLock::new(Vec::new())),
            max_rounds: 3,
        }
    }
}

#[async_trait]
impl CollaborationProtocol for DebateProtocol {
    fn name(&self) -> &str {
        "debate"
    }

    fn description(&self) -> &str {
        "辩论协作协议:多轮讨论优化,适合需要迭代改进的审查任务"
    }

    fn applicable_scenarios(&self) -> Vec<String> {
        vec![
            "代码审查".to_string(),
            "方案优化".to_string(),
            "争议解决".to_string(),
            "质量改进".to_string(),
        ]
    }

    fn mode(&self) -> CollaborationMode {
        CollaborationMode::Debate
    }

    async fn send_message(&self, msg: CollaborationMessage) -> anyhow::Result<()> {
        let mut queue = self.message_queue.write().await;
        queue.push(msg);
        Ok(())
    }

    async fn receive_message(&self) -> anyhow::Result<Option<CollaborationMessage>> {
        let mut queue = self.message_queue.write().await;
        Ok(queue.pop())
    }

    async fn process_message(
        &self,
        msg: CollaborationMessage,
    ) -> anyhow::Result<CollaborationResult> {
        let start = std::time::Instant::now();

        tracing::debug!(
            "[Debate] {} processing message from {}",
            self.helper.agent_id(),
            msg.sender
        );

        let content = if self.helper.use_llm {
            self.helper
                .process_with_llm(
                    &msg,
                    "你是一个协作 Agent,负责处理辩论模式的协作消息。请提供有建设性的观点和反驳。",
                )
                .await?
        } else {
            CollaborationContent::Text(format!("已参与辩论,最大轮数: {}", self.max_rounds))
        };

        let duration = start.elapsed().as_millis() as u64;

        Ok(
            CollaborationResult::success(content, duration, CollaborationMode::Debate)
                .with_participant(self.helper.agent_id().to_string())
                .with_participant(msg.sender),
        )
    }

    fn stats(&self) -> HashMap<String, serde_json::Value> {
        HashMap::from([
            (
                "agent_id".to_string(),
                serde_json::json!(self.helper.agent_id()),
            ),
            (
                "use_llm".to_string(),
                serde_json::json!(self.helper.use_llm),
            ),
            ("max_rounds".to_string(), serde_json::json!(self.max_rounds)),
        ])
    }
}

// ============================================================================
// 并行协议
// ============================================================================

/// 并行协作协议
pub struct ParallelProtocol {
    helper: LLMProtocolHelper,
    message_queue: Arc<RwLock<Vec<CollaborationMessage>>>,
    max_workers: usize,
}

impl ParallelProtocol {
    /// 创建新的并行协议(不使用 LLM)
    pub fn new(agent_id: impl Into<String>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id),
            message_queue: Arc::new(RwLock::new(Vec::new())),
            max_workers: 4,
        }
    }

    /// 创建支持 LLM 的并行协议
    pub fn with_llm(agent_id: impl Into<String>, llm_client: Arc<LLMClient>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id).with_llm(llm_client),
            message_queue: Arc::new(RwLock::new(Vec::new())),
            max_workers: 4,
        }
    }
}

#[async_trait]
impl CollaborationProtocol for ParallelProtocol {
    fn name(&self) -> &str {
        "parallel"
    }

    fn description(&self) -> &str {
        "并行协作协议:同时执行独立任务,适合可分解的工作"
    }

    fn applicable_scenarios(&self) -> Vec<String> {
        vec![
            "数据分析".to_string(),
            "批量处理".to_string(),
            "分布式搜索".to_string(),
            "并行计算".to_string(),
        ]
    }

    fn mode(&self) -> CollaborationMode {
        CollaborationMode::Parallel
    }

    async fn send_message(&self, msg: CollaborationMessage) -> anyhow::Result<()> {
        let mut queue = self.message_queue.write().await;
        queue.push(msg);
        Ok(())
    }

    async fn receive_message(&self) -> anyhow::Result<Option<CollaborationMessage>> {
        let mut queue = self.message_queue.write().await;
        Ok(queue.pop())
    }

    async fn process_message(
        &self,
        msg: CollaborationMessage,
    ) -> anyhow::Result<CollaborationResult> {
        let start = std::time::Instant::now();

        tracing::debug!(
            "[Parallel] {} processing message from {}",
            self.helper.agent_id(),
            msg.sender
        );

        let content = if self.helper.use_llm {
            self.helper
                .process_with_llm(
                    &msg,
                    "你是一个协作 Agent,负责处理并行模式的协作消息。任务会被分解并并行处理。",
                )
                .await?
        } else {
            CollaborationContent::Text(format!(
                "已启动并行处理,最大工作线程: {}",
                self.max_workers
            ))
        };

        let duration = start.elapsed().as_millis() as u64;

        Ok(
            CollaborationResult::success(content, duration, CollaborationMode::Parallel)
                .with_participant(self.helper.agent_id().to_string())
                .with_participant(msg.sender),
        )
    }

    fn stats(&self) -> HashMap<String, serde_json::Value> {
        HashMap::from([
            (
                "agent_id".to_string(),
                serde_json::json!(self.helper.agent_id()),
            ),
            (
                "use_llm".to_string(),
                serde_json::json!(self.helper.use_llm),
            ),
            (
                "max_workers".to_string(),
                serde_json::json!(self.max_workers),
            ),
        ])
    }
}

// ============================================================================
// 顺序协议
// ============================================================================

/// 顺序协作协议
pub struct SequentialProtocol {
    helper: LLMProtocolHelper,
    message_queue: Arc<RwLock<Vec<CollaborationMessage>>>,
}

impl SequentialProtocol {
    /// 创建新的顺序协议(不使用 LLM)
    pub fn new(agent_id: impl Into<String>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id),
            message_queue: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// 创建支持 LLM 的顺序协议
    pub fn with_llm(agent_id: impl Into<String>, llm_client: Arc<LLMClient>) -> Self {
        Self {
            helper: LLMProtocolHelper::new(agent_id).with_llm(llm_client),
            message_queue: Arc::new(RwLock::new(Vec::new())),
        }
    }
}

#[async_trait]
impl CollaborationProtocol for SequentialProtocol {
    fn name(&self) -> &str {
        "sequential"
    }

    fn description(&self) -> &str {
        "顺序协作协议:串行执行有依赖的任务链,适合流水线处理"
    }

    fn applicable_scenarios(&self) -> Vec<String> {
        vec![
            "流水线处理".to_string(),
            "依赖任务链".to_string(),
            "分步执行".to_string(),
            "阶段式工作流".to_string(),
        ]
    }

    fn mode(&self) -> CollaborationMode {
        CollaborationMode::Sequential
    }

    async fn send_message(&self, msg: CollaborationMessage) -> anyhow::Result<()> {
        let mut queue = self.message_queue.write().await;
        queue.push(msg);
        Ok(())
    }

    async fn receive_message(&self) -> anyhow::Result<Option<CollaborationMessage>> {
        let mut queue = self.message_queue.write().await;
        Ok(queue.pop())
    }

    async fn process_message(
        &self,
        msg: CollaborationMessage,
    ) -> anyhow::Result<CollaborationResult> {
        let start = std::time::Instant::now();

        tracing::debug!(
            "[Sequential] {} processing message from {}",
            self.helper.agent_id(),
            msg.sender
        );

        let content = if self.helper.use_llm {
            self.helper
                .process_with_llm(
                    &msg,
                    "你是一个协作 Agent,负责处理顺序模式的协作消息。任务会按依赖关系依次执行。",
                )
                .await?
        } else {
            CollaborationContent::Text("已执行顺序任务链".to_string())
        };

        let duration = start.elapsed().as_millis() as u64;

        Ok(
            CollaborationResult::success(content, duration, CollaborationMode::Sequential)
                .with_participant(self.helper.agent_id().to_string())
                .with_participant(msg.sender),
        )
    }

    fn stats(&self) -> HashMap<String, serde_json::Value> {
        HashMap::from([
            (
                "agent_id".to_string(),
                serde_json::json!(self.helper.agent_id()),
            ),
            (
                "use_llm".to_string(),
                serde_json::json!(self.helper.use_llm),
            ),
        ])
    }
}