Overview
SwarmRearrange is a class for orchestrating multiple swarms in a flow pattern. Each swarm is looked up by its name attribute, and its output becomes the next step’s task. Every swarm’s output is also added to arrangement.conversation.
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"
Number of passes through the whole flow. Values of 0 or less are replaced with 1.
bool
default:"True"
Stored but not read. Logging happens either way.
bool
default:"False"
Enable human intervention during execution.
Callable
default:"None"
Custom function for human interaction.
bool
default:"False"
Stored but not read.
run() always returns a string.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
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. See the warning below*args,**kwargs: Forwarded to each swarm’srun()call, aftertaskandimg
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" - Fan-in:
"SwarmA, SwarmB -> SwarmC". Comma-separated swarms run one after another, not concurrently, each on the same input. Their outputs are joined with"; "and passed to the next step - Human intervention: Use
"H"in the flow. Withhuman_in_the_loop=Trueand acustom_human_in_the_loopfunction, that function receives the current task and its return value becomes the next task. Otherwiserun()prompts on stdin withinput()
->, and each swarm name may appear only once.