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:"generate_id('swarm-rearrange')"
Unique identifier for the swarm arrangement, e.g.
swarm-rearrange-<32 hex> — not a UUID.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"
Currently has no effect:
self.output_type is stored but never read. run() never calls history_output_formatter and always returns current_task, a plain string (see run() below).Methods
add_swarm()
Adds a single swarm to the arrangement.swarm(Any): The swarm object to add
remove_swarm()
Removes a swarm by name from the arrangement.swarm_name(str): Name of the swarm to remove
add_swarms()
Adds multiple swarms to the arrangement.swarms(List[Any]): List of swarm objects to add
validate_flow()
Validates the flow pattern syntax and swarm names.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.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.swarm_name(str): Name of the swarm whose history to updateresult(str): Result to append to that swarm’s history
run()
Executes the swarm arrangement according to the flow pattern.task(str, optional): The task to be executedimg(str, optional): Image input for the taskcustom_tasks(Dict[str, str], optional): Custom tasks mapped to specific swarms*args,**kwargs: Forwarded to each swarm’srun()call
str — always current_task (the final swarm’s textual output), regardless of output_type. run() catches all exceptions internally and, on failure, returns str(e) instead of raising — so callers must check the returned string for error text rather than relying on a try/except around run().
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