swarms-rs 0.2.0

The Enterprise-Grade Production-Ready Multi-Agent Orchestration Framework in Rust
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
use std::{collections::HashMap, path::Path};

use chrono::Local;
use dashmap::DashSet;
use erased_serde::Serialize as ErasedSerialize;
use futures::{StreamExt, TryStreamExt, future::BoxFuture, stream};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

use crate::structs::{
    agent::{Agent, AgentError},
    conversation::{AgentConversation, Role},
    persistence::{self, PersistenceError},
    swarm::{MetadataSchemaMap, Swarm, SwarmError},
};

/// Errors that can occur during agent rearrangement operations
#[derive(Debug, Error)]
pub enum AgentRearrangeError {
    #[error("Agent error: {0}")]
    AgentError(#[from] AgentError),
    #[error("FilePersistence error: {0}")]
    FilePersistenceError(#[from] PersistenceError),
    #[error("Flow validation error: {0}")]
    FlowValidationError(String),
    #[error("Agent '{0}' not found")]
    AgentNotFound(String),
    #[error("Invalid flow format: {0}")]
    InvalidFlowFormat(String),
    #[error("Duplicate agent names in flow are not allowed")]
    DuplicateAgentNames,
    #[error("Tasks or Agents are empty")]
    EmptyTasksOrAgents,
    #[error("Json error: {0}")]
    JsonError(#[from] serde_json::Error),
    #[error("Execution error: {0}")]
    ExecutionError(String),
    #[error("Join error: {0}")]
    JoinError(#[from] tokio::task::JoinError),
}

/// Output format options for agent rearrange results
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum OutputType {
    /// Return all agent responses concatenated
    All,
    /// Return only the final agent's response
    Final,
    /// Return a list of all agent responses
    List,
    /// Return a dictionary mapping agent names to responses
    Dict,
}

impl Default for OutputType {
    fn default() -> Self {
        OutputType::All
    }
}

/// Configuration builder for AgentRearrange
#[derive(Default)]
pub struct AgentRearrangeBuilder {
    name: String,
    description: String,
    agents: Vec<Box<dyn Agent>>,
    flow: Option<String>,
    max_loops: u32,
    verbose: bool,
    output_type: OutputType,
    autosave: bool,
    return_json: bool,
    metadata_output_dir: String,
    rules: Option<String>,
    team_awareness: bool,
}

impl AgentRearrangeBuilder {
    /// Set the name of the agent rearrange instance
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// Set the description of the agent rearrange instance
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Add an agent to the rearrange configuration
    pub fn add_agent(mut self, agent: Box<dyn Agent>) -> Self {
        self.agents.push(agent);
        self
    }

    /// Set all agents at once
    pub fn agents(self, agents: Vec<Box<dyn Agent>>) -> Self {
        agents
            .into_iter()
            .fold(self, |builder, agent| builder.add_agent(agent))
    }

    /// Set the flow pattern for task execution
    pub fn flow(mut self, flow: impl Into<String>) -> Self {
        self.flow = Some(flow.into());
        self
    }

    /// Set the maximum number of execution loops
    pub fn max_loops(mut self, max_loops: u32) -> Self {
        self.max_loops = max_loops;
        self
    }

    /// Enable or disable verbose logging
    pub fn verbose(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }

    /// Set the output format type
    pub fn output_type(mut self, output_type: OutputType) -> Self {
        self.output_type = output_type;
        self
    }

    /// Enable or disable autosave functionality
    pub fn autosave(mut self, autosave: bool) -> Self {
        self.autosave = autosave;
        self
    }

    /// Enable or disable JSON return format
    pub fn return_json(mut self, return_json: bool) -> Self {
        self.return_json = return_json;
        self
    }

    /// Set the metadata output directory
    pub fn metadata_output_dir(mut self, dir: impl Into<String>) -> Self {
        self.metadata_output_dir = dir.into();
        self
    }

    /// Set rules to be injected into all agents
    pub fn rules(mut self, rules: impl Into<String>) -> Self {
        self.rules = Some(rules.into());
        self
    }

    /// Enable team awareness functionality
    pub fn team_awareness(mut self, team_awareness: bool) -> Self {
        self.team_awareness = team_awareness;
        self
    }

    /// Build the AgentRearrange instance
    pub fn build(self) -> AgentRearrange {
        AgentRearrange {
            id: Uuid::new_v4().to_string(),
            name: self.name,
            description: self.description,
            agents: self
                .agents
                .into_iter()
                .map(|agent| (agent.name(), agent))
                .collect(),
            flow: self.flow.unwrap_or_default(),
            max_loops: if self.max_loops > 0 {
                self.max_loops
            } else {
                1
            },
            verbose: self.verbose,
            output_type: self.output_type,
            autosave: self.autosave,
            return_json: self.return_json,
            metadata_output_dir: self.metadata_output_dir,
            conversation: AgentConversation::new("AgentRearrange".to_string()),
            metadata_map: MetadataSchemaMap::default(),
            tasks: DashSet::new(),
            rules: self.rules,
            team_awareness: self.team_awareness,
        }
    }
}

/// A swarm of agents for rearranging and executing tasks in specified flow patterns.
///
/// AgentRearrange enables flexible task execution by allowing agents to be organized
/// in sequential or parallel flows. It supports various output formats, concurrent
/// execution, batch processing, and extensive configuration options.
///
/// # Features
///
/// - **Flow-based execution**: Define custom flows with `->`  syntax for sequential and parallel execution
/// - **Multiple output formats**: Support for different output types (All, Final, List, Dict)
/// - **Concurrent processing**: Leverage tokio for efficient async task execution
/// - **Team awareness**: Optional team information sharing between agents
/// - **Autosave**: Automatic persistence of agent interactions and metadata
/// - **Batch processing**: Execute multiple tasks efficiently
///
/// # Flow Syntax
///
/// - Sequential: `"agent1 -> agent2 -> agent3"`
/// - Parallel: `"agent1, agent2 -> agent3"`
/// - Mixed: `"agent1 -> agent2, agent3 -> agent4"`
///
/// # Examples
///
/// ```rust,no_run
/// use swarms_rs::structs::rearrange::AgentRearrange;
///
/// let rearrange = AgentRearrange::builder()
///     .name("TaskProcessor")
///     .description("Processes tasks through multiple agents")
///     .flow("researcher -> analyst, reviewer -> summarizer")
///     .max_loops(3)
///     .verbose(true)
///     .build();
/// ```
pub struct AgentRearrange {
    /// Unique identifier for the swarm instance
    id: String,
    /// Name of the agent rearrange instance
    name: String,
    /// Description of the agent rearrange instance's purpose
    description: String,
    /// Map of agent names to Agent objects
    agents: HashMap<String, Box<dyn Agent>>,
    /// Flow pattern defining task execution order
    flow: String,
    /// Maximum number of execution loops
    max_loops: u32,
    /// Whether to enable verbose logging
    verbose: bool,
    /// Format of output (All, Final, List, Dict)
    output_type: OutputType,
    /// Whether to enable autosave functionality
    autosave: bool,
    /// Whether to return output in JSON format
    return_json: bool,
    /// Directory for metadata output
    metadata_output_dir: String,
    /// Conversation history and management
    conversation: AgentConversation,
    /// Metadata mapping for agent interactions
    #[allow(dead_code)]
    metadata_map: MetadataSchemaMap,
    /// Set of active tasks
    #[allow(dead_code)]
    tasks: DashSet<String>,
    /// Rules to inject into all agents
    rules: Option<String>,
    /// Whether team awareness is enabled
    team_awareness: bool,
}

impl Default for AgentRearrange {
    fn default() -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            name: "AgentRearrange".to_string(),
            description: "A swarm of agents for rearranging tasks.".to_string(),
            agents: HashMap::new(),
            flow: String::new(),
            max_loops: 1,
            verbose: false,
            output_type: OutputType::All,
            autosave: false,
            return_json: false,
            metadata_output_dir: String::new(),
            conversation: AgentConversation::new("AgentRearrange".to_string()),
            metadata_map: MetadataSchemaMap::default(),
            tasks: DashSet::new(),
            rules: None,
            team_awareness: false,
        }
    }
}

impl AgentRearrange {
    /// Create a new builder for AgentRearrange
    ///
    /// # Returns
    ///
    /// A new `AgentRearrangeBuilder` instance for configuring the agent rearrange system.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use swarms_rs::structs::rearrange::AgentRearrange;
    ///
    /// let rearrange = AgentRearrange::builder()
    ///     .name("MySwarm")
    ///     .description("A custom swarm configuration")
    ///     .build();
    /// ```
    pub fn builder() -> AgentRearrangeBuilder {
        AgentRearrangeBuilder::default()
    }

    /// Set a custom flow pattern for task execution
    ///
    /// # Arguments
    ///
    /// * `flow` - The flow pattern string defining execution order
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use swarms_rs::structs::rearrange::AgentRearrange;
    /// let mut rearrange = AgentRearrange::default();
    /// rearrange.set_custom_flow("agent1 -> agent2, agent3 -> agent4");
    /// ```
    pub fn set_custom_flow(&mut self, flow: impl Into<String>) {
        self.flow = flow.into();
        if self.verbose {
            tracing::info!("Custom flow set: {}", self.flow);
        }
    }

    /// Add an agent to the swarm
    ///
    /// # Arguments
    ///
    /// * `agent` - The agent to be added to the swarm
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use swarms_rs::structs::rearrange::AgentRearrange;
    /// let mut rearrange = AgentRearrange::default();
    /// // rearrange.add_agent(my_agent);
    /// ```
    pub fn add_agent(&mut self, agent: Box<dyn Agent>) {
        let agent_name = agent.name();
        if self.verbose {
            tracing::info!("Adding agent {} to the swarm", agent_name);
        }
        self.agents.insert(agent_name, agent);
    }

    /// Remove an agent from the swarm
    ///
    /// # Arguments
    ///
    /// * `agent_name` - The name of the agent to be removed
    ///
    /// # Returns
    ///
    /// The removed agent if it existed, None otherwise
    pub fn remove_agent(&mut self, agent_name: &str) -> Option<Box<dyn Agent>> {
        if self.verbose {
            tracing::info!("Removing agent {} from the swarm", agent_name);
        }
        self.agents.remove(agent_name)
    }

    /// Add multiple agents to the swarm
    ///
    /// # Arguments
    ///
    /// * `agents` - A vector of agents to be added
    pub fn add_agents(&mut self, agents: Vec<Box<dyn Agent>>) {
        for agent in agents {
            self.add_agent(agent);
        }
    }

    /// Validate the flow pattern for correctness
    ///
    /// This method checks that:
    /// - The flow contains the `->` separator
    /// - All referenced agents are registered in the swarm
    /// - The flow syntax is properly formatted
    ///
    /// # Returns
    ///
    /// `Result<(), AgentRearrangeError>` - Ok if valid, error with details if invalid
    ///
    /// # Errors
    ///
    /// - `FlowValidationError` if the flow format is incorrect
    /// - `AgentNotFound` if referenced agents are not registered
    pub fn validate_flow(&self) -> Result<(), AgentRearrangeError> {
        if self.flow.is_empty() {
            return Err(AgentRearrangeError::FlowValidationError(
                "Flow cannot be empty".to_string(),
            ));
        }

        // Handle both sequential (with ->) and parallel-only (without ->) flows
        let tasks: Vec<&str> = if self.flow.contains("->") {
            self.flow.split("->").collect()
        } else {
            // For parallel-only flows like "agent1, agent2"
            vec![self.flow.as_str()]
        };

        for task in tasks {
            let agent_names: Vec<&str> = task.split(',').map(|name| name.trim()).collect();

            for agent_name in agent_names {
                if agent_name != "H" && !self.agents.contains_key(agent_name) {
                    return Err(AgentRearrangeError::AgentNotFound(agent_name.to_string()));
                }
            }
        }

        if self.verbose {
            tracing::info!("Flow: {} is valid", self.flow);
        }

        Ok(())
    }

    /// Execute the agent rearrangement task
    ///
    /// This method processes a task through the configured flow of agents,
    /// supporting both sequential and parallel execution patterns.
    ///
    /// # Arguments
    ///
    /// * `task` - The task to be processed by the agents
    ///
    /// # Returns
    ///
    /// `Result<String, AgentRearrangeError>` - The processed output in the specified format
    ///
    /// # Errors
    ///
    /// - `FlowValidationError` if the flow validation fails
    /// - `AgentError` if any agent execution fails
    /// - `ExecutionError` for other execution-related issues
    pub async fn run(&mut self, task: impl Into<String>) -> Result<String, AgentRearrangeError> {
        self.run_internal(task, None, None).await
    }

    /// Internal execution method with full parameter support
    async fn run_internal(
        &mut self,
        task: impl Into<String>,
        _img: Option<String>,
        _custom_tasks: Option<HashMap<String, String>>,
    ) -> Result<String, AgentRearrangeError> {
        let task = task.into();

        if self.verbose {
            tracing::info!("Starting task execution: {}", task);
        }

        // Add initial task to conversation
        self.conversation
            .add(Role::User("System".to_string()), task.clone());

        // Validate flow before execution
        self.validate_flow()?;

        // Apply rules if configured
        if let Some(rules) = &self.rules {
            self.conversation.add(
                Role::User("System".to_string()),
                format!("Rules: {}", rules),
            );
        }

        let tasks: Vec<&str> = if self.flow.contains("->") {
            self.flow.split("->").collect()
        } else {
            // For parallel-only flows like "agent1, agent2"
            vec![self.flow.as_str()]
        };
        let mut current_task = task.clone();
        let mut response_map = HashMap::new();

        for loop_count in 0..self.max_loops {
            if self.verbose {
                tracing::info!("Starting loop {}/{}", loop_count + 1, self.max_loops);
            }

            for task_step in tasks.iter() {
                let agent_names: Vec<&str> = task_step.split(',').map(|name| name.trim()).collect();

                if agent_names.len() > 1 {
                    // Parallel processing
                    if self.verbose {
                        tracing::info!("Running agents in parallel: {:?}", agent_names);
                    }

                    let parallel_results = self
                        .execute_agents_parallel(&agent_names, &current_task)
                        .await?;

                    for (agent_name, result) in parallel_results {
                        self.conversation
                            .add(Role::Assistant(agent_name.clone()), result.clone());
                        response_map.insert(agent_name, result);
                    }
                } else {
                    // Sequential processing
                    let agent_name = agent_names[0];

                    if self.verbose {
                        tracing::info!("Running agent sequentially: {}", agent_name);
                    }

                    if agent_name == "H" {
                        // Human-in-the-loop placeholder
                        if self.verbose {
                            tracing::info!("Human intervention point reached");
                        }
                        continue;
                    }

                    let agent = self.agents.get(agent_name).ok_or_else(|| {
                        AgentRearrangeError::AgentNotFound(agent_name.to_string())
                    })?;

                    let result = agent
                        .run(self.conversation.to_string())
                        .await
                        .map_err(AgentRearrangeError::AgentError)?;

                    self.conversation
                        .add(Role::Assistant(agent_name.to_string()), result.clone());

                    response_map.insert(agent_name.to_string(), result.clone());
                    current_task = result;
                }
            }
        }

        if self.verbose {
            tracing::info!("Task execution completed");
        }

        // Format output based on output_type
        let output = self.format_output(&response_map, &current_task);

        if self.autosave {
            self.save_metadata().await?;
        }

        Ok(output)
    }

    /// Execute multiple agents in parallel
    async fn execute_agents_parallel(
        &self,
        agent_names: &[&str],
        task: &str,
    ) -> Result<HashMap<String, String>, AgentRearrangeError> {
        let mut handles = Vec::new();

        for agent_name in agent_names {
            if *agent_name == "H" {
                continue; // Skip human-in-the-loop for parallel execution
            }

            let agent = self
                .agents
                .get(*agent_name)
                .ok_or_else(|| AgentRearrangeError::AgentNotFound(agent_name.to_string()))?;

            let task_clone = task.to_string();
            let agent_name_clone = agent_name.to_string();

            // Clone the agent for parallel execution
            let agent_clone = agent.clone_box();

            let handle = tokio::spawn(async move {
                let result = agent_clone.run(task_clone).await;
                (agent_name_clone, result)
            });

            handles.push(handle);
        }

        // Wait for all parallel tasks to complete
        let mut results = HashMap::new();
        for handle in handles {
            let (agent_name, result) = handle.await?;

            let result = result.map_err(AgentRearrangeError::AgentError)?;
            results.insert(agent_name, result);
        }

        Ok(results)
    }

    /// Format the output based on the configured output type
    fn format_output(&self, response_map: &HashMap<String, String>, final_result: &str) -> String {
        match self.output_type {
            OutputType::All => {
                let mut output = String::new();
                for (agent_name, response) in response_map {
                    output.push_str(&format!("{}: {}\n", agent_name, response));
                }
                output
            },
            OutputType::Final => final_result.to_string(),
            OutputType::List => {
                let responses: Vec<String> = response_map.values().cloned().collect();
                if self.return_json {
                    serde_json::to_string(&responses).unwrap_or_else(|_| "[]".to_string())
                } else {
                    responses.join("\n")
                }
            },
            OutputType::Dict => {
                if self.return_json {
                    serde_json::to_string(response_map).unwrap_or_else(|_| "{}".to_string())
                } else {
                    response_map
                        .iter()
                        .map(|(k, v)| format!("{}: {}", k, v))
                        .collect::<Vec<_>>()
                        .join("\n")
                }
            },
        }
    }

    /// Process multiple tasks in batches
    ///
    /// # Arguments
    ///
    /// * `tasks` - Vector of tasks to process
    /// * `batch_size` - Number of tasks to process simultaneously
    /// * `img` - Optional image paths corresponding to tasks
    ///
    /// # Returns
    ///
    /// `Result<Vec<String>, AgentRearrangeError>` - Vector of results corresponding to input tasks
    pub async fn batch_run(
        &mut self,
        tasks: Vec<String>,
        batch_size: usize,
        img: Option<Vec<String>>,
    ) -> Result<Vec<String>, AgentRearrangeError> {
        if tasks.is_empty() {
            return Err(AgentRearrangeError::EmptyTasksOrAgents);
        }

        let mut results = Vec::with_capacity(tasks.len());

        for chunk in tasks.chunks(batch_size) {
            let mut batch_handles = Vec::new();

            for (i, task) in chunk.iter().enumerate() {
                let img_path = img.as_ref().and_then(|imgs| imgs.get(i)).cloned();
                let task_clone = task.clone();

                // Create a clone of self for each task
                let mut rearrange_clone = self.clone_for_task();

                let handle = tokio::spawn(async move {
                    rearrange_clone
                        .run_internal(task_clone, img_path, None)
                        .await
                });

                batch_handles.push(handle);
            }

            // Wait for batch to complete
            for handle in batch_handles {
                let result = handle.await??;
                results.push(result);
            }
        }

        Ok(results)
    }

    /// Process multiple tasks concurrently without batching
    ///
    /// # Arguments
    ///
    /// * `tasks` - Vector of tasks to process concurrently
    /// * `img` - Optional image paths corresponding to tasks
    /// * `max_concurrent` - Maximum number of concurrent tasks (None for unlimited)
    ///
    /// # Returns
    ///
    /// `Result<Vec<String>, AgentRearrangeError>` - Vector of results corresponding to input tasks
    pub async fn concurrent_run(
        &mut self,
        tasks: Vec<String>,
        img: Option<Vec<String>>,
        max_concurrent: Option<usize>,
    ) -> Result<Vec<String>, AgentRearrangeError> {
        if tasks.is_empty() {
            return Err(AgentRearrangeError::EmptyTasksOrAgents);
        }

        let stream = stream::iter(tasks.into_iter().enumerate().map(|(i, task)| {
            let img_path = img.as_ref().and_then(|imgs| imgs.get(i)).cloned();
            let mut rearrange_clone = self.clone_for_task();

            async move { rearrange_clone.run_internal(task, img_path, None).await }
        }));

        let results: Result<Vec<_>, _> = if let Some(max_concurrent) = max_concurrent {
            stream.buffer_unordered(max_concurrent).try_collect().await
        } else {
            stream.buffer_unordered(8).try_collect().await // Default to 8 concurrent tasks
        };

        results
    }

    /// Create a lightweight clone for task execution
    fn clone_for_task(&self) -> Self {
        let mut cloned_agents = HashMap::new();
        for (name, agent) in &self.agents {
            cloned_agents.insert(name.clone(), agent.clone_box());
        }

        Self {
            id: self.id.clone(),
            name: self.name.clone(),
            description: self.description.clone(),
            agents: cloned_agents,
            flow: self.flow.clone(),
            max_loops: self.max_loops,
            verbose: self.verbose,
            output_type: self.output_type.clone(),
            autosave: false, // Disable autosave for clones
            return_json: self.return_json,
            metadata_output_dir: self.metadata_output_dir.clone(),
            conversation: AgentConversation::new(format!("{}-clone", self.name)),
            metadata_map: MetadataSchemaMap::default(),
            tasks: DashSet::new(),
            rules: self.rules.clone(),
            team_awareness: self.team_awareness,
        }
    }

    /// Save metadata to the configured output directory
    async fn save_metadata(&self) -> Result<(), AgentRearrangeError> {
        if !self.metadata_output_dir.is_empty() {
            let metadata = self.to_metadata();
            let path = Path::new(&self.metadata_output_dir).join(format!("{}.json", self.id));
            let json_data = serde_json::to_string_pretty(&metadata)?;
            persistence::save_to_file(json_data.as_bytes(), &path).await?;
        }
        Ok(())
    }

    /// Convert the agent rearrange instance to metadata for persistence
    fn to_metadata(&self) -> serde_json::Value {
        serde_json::json!({
            "id": self.id,
            "name": self.name,
            "description": self.description,
            "flow": self.flow,
            "max_loops": self.max_loops,
            "agents": self.agents.keys().collect::<Vec<_>>(),
            "conversation_length": self.conversation.history.len(),
            "timestamp": Local::now().timestamp_millis(),
        })
    }

    /// Get the unique identifier of this agent rearrange instance
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Get the name of this agent rearrange instance
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the description of this agent rearrange instance
    pub fn description(&self) -> &str {
        &self.description
    }

    /// Get the current flow pattern
    pub fn flow(&self) -> &str {
        &self.flow
    }

    /// Get the list of agent names
    pub fn agent_names(&self) -> Vec<&String> {
        self.agents.keys().collect()
    }

    /// Get the conversation history
    pub fn conversation(&self) -> &AgentConversation {
        &self.conversation
    }

    /// Get the maximum number of loops
    pub fn max_loops(&self) -> u32 {
        self.max_loops
    }

    /// Get the number of agents in the swarm
    pub fn agent_count(&self) -> usize {
        self.agents.len()
    }
}

/// Convenience function to create and run an agent rearrangement task
///
/// # Arguments
///
/// * `name` - Name of the agent rearrange instance
/// * `description` - Description of the purpose
/// * `agents` - Vector of agents to use
/// * `flow` - Flow pattern for execution
/// * `task` - Task to execute
/// * `img` - Optional image path
///
/// # Returns
///
/// `Result<String, AgentRearrangeError>` - The result of executing the task
///
/// # Examples
///
/// ```rust,no_run
/// use swarms_rs::structs::rearrange::rearrange;
///
/// async fn example() {
///     let result = rearrange(
///         "TaskProcessor",
///         "Processes tasks through agents",
///         vec![/* agents */],
///         "agent1 -> agent2",
///         "Analyze this data",
///         None,
///     ).await;
/// }
/// ```
/// Convenience function to create and run an agent rearrangement task
///
/// # Arguments
///
/// * `name` - Name of the agent rearrange instance
/// * `description` - Description of the purpose
/// * `agents` - Vector of agents to use
/// * `flow` - Flow pattern for execution
/// * `task` - Task to execute
/// * `img` - Optional image path
///
/// # Returns
///
/// `Result<String, AgentRearrangeError>` - The result of executing the task
///
/// # Examples
///
/// ```rust,no_run
/// use swarms_rs::structs::rearrange::rearrange;
///
/// async fn example() {
///     let result = rearrange(
///         "TaskProcessor",
///         "Processes tasks through agents",
///         vec![/* agents */],
///         "agent1 -> agent2",
///         "Analyze this data",
///         None,
///     ).await;
/// }
/// ```
pub async fn rearrange(
    name: impl Into<String>,
    description: impl Into<String>,
    agents: Vec<Box<dyn Agent>>,
    flow: impl Into<String>,
    task: impl Into<String>,
    img: Option<String>,
) -> Result<String, AgentRearrangeError> {
    let mut agent_system = AgentRearrange::builder()
        .name(name)
        .description(description)
        .agents(agents)
        .flow(flow)
        .build();

    agent_system.run_internal(task, img, None).await
}

impl Swarm for AgentRearrange {
    fn run(&self, task: String) -> BoxFuture<'_, Result<Box<dyn ErasedSerialize>, SwarmError>> {
        Box::pin(async move {
            // Create a mutable clone to work with
            let mut rearrange_clone = self.clone_for_task();

            match rearrange_clone.run_internal(task, None, None).await {
                Ok(result) => {
                    let serialized: Box<dyn ErasedSerialize> = Box::new(result);
                    Ok(serialized)
                },
                Err(e) => Err(SwarmError::AgentRearrangeError(e)),
            }
        })
    }

    fn name(&self) -> &str {
        &self.name
    }
}