Skip to main content

Overview

SwarmRearrange is a class for orchestrating multiple swarms in a sequential or parallel flow pattern. It provides thread-safe operations for managing swarm execution, history tracking, and flow validation.

Installation

Attributes

str
default:"UUID"
Unique identifier for the swarm arrangement.
str
default:"SwarmRearrange"
Name of the swarm arrangement.
str
default:"A swarm of swarms..."
Description of the arrangement.
List[Any]
default:"[]"
List of swarm objects to be managed. Despite the default of [], the constructor runs reliability_checks() and raises ValueError if swarms is empty — a non-empty list is effectively required.
str
default:"None"
Flow pattern for swarm execution. Uses arrow notation to define execution order. Despite the default of None, reliability_checks() raises ValueError if flow is falsy — a flow string is effectively required.
int
default:"1"
Maximum number of execution loops.
bool
default:"True"
Enable detailed logging.
bool
default:"False"
Enable human intervention during execution.
Callable
default:"None"
Custom function for human interaction.
bool
default:"False"
Return results in JSON format.
HistoryOutputType
default:"dict-all-except-first"
Format applied to the conversation history. Options include "dict-all-except-first", "dict", "list", "str", "final".

Methods

add_swarm()

Adds a single swarm to the arrangement.
Parameters:
  • swarm (Any): The swarm object to add

remove_swarm()

Removes a swarm by name from the arrangement.
Parameters:
  • swarm_name (str): Name of the swarm to remove

add_swarms()

Adds multiple swarms to the arrangement.
Parameters:
  • swarms (List[Any]): List of swarm objects to add

validate_flow()

Validates the flow pattern syntax and swarm names.
Returns: True if the flow is valid. Raises: ValueError if "->" is missing from the flow, a referenced swarm name isn’t registered (and isn’t "H"), or the flow contains duplicate swarm names.

set_custom_flow()

Overrides the current flow pattern.
Parameters:
  • flow (str): The new flow pattern string

track_history()

Appends a result to a swarm’s execution history. Called internally; can also be invoked directly for custom tracking.
Parameters:
  • swarm_name (str): Name of the swarm whose history to update
  • result (str): Result to append to that swarm’s history

run()

Executes the swarm arrangement according to the flow pattern.
Parameters:
  • task (str, optional): The task to be executed
  • img (str, optional): Image input for the task
  • custom_tasks (Dict[str, str], optional): Custom tasks mapped to specific swarms
  • *args, **kwargs: Forwarded to each swarm’s run() call
Returns: The result of the swarm execution

Flow Pattern Syntax

The flow pattern uses arrow notation (->) to define execution order:
  • Sequential: "SwarmA -> SwarmB -> SwarmC"
  • Parallel: "SwarmA, SwarmB -> SwarmC"
  • Human intervention: Use "H" in the flow

Usage Examples

Basic Sequential Flow

Human-in-the-Loop

Best Practices

Source Code

View the source code on GitHub