Skip to content

Commit 1397e6e

Browse files
committed
feat(multiagent): introduce Swarm multi-agent orchestrator
1 parent 10555be commit 1397e6e

File tree

8 files changed

+758
-11
lines changed

8 files changed

+758
-11
lines changed

src/strands/agent/agent.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ def __init__(
317317
self.hooks.add_hook(hook)
318318
self.hooks.invoke_callbacks(AgentInitializedEvent(agent=self))
319319

320+
# When True, force stops the agent's event loop
321+
self.stop_event_loop = False
322+
320323
@property
321324
def tool(self) -> ToolCaller:
322325
"""Call tool as a function.

src/strands/event_loop/event_loop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def tool_handler(tool_use: ToolUse) -> ToolGenerator:
462462
tracer = get_tracer()
463463
tracer.end_event_loop_cycle_span(span=cycle_span, message=message, tool_result_message=tool_result_message)
464464

465-
if invocation_state["request_state"].get("stop_event_loop", False):
465+
if agent.stop_event_loop or invocation_state["request_state"].get("stop_event_loop", False):
466466
agent.event_loop_metrics.end_cycle(cycle_start_time, cycle_trace)
467467
yield {"stop": (stop_reason, message, agent.event_loop_metrics, invocation_state["request_state"])}
468468
return

src/strands/multiagent/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
from . import a2a
1212
from .base import MultiAgentBase, MultiAgentResult
1313
from .graph import GraphBuilder, GraphResult
14+
from .swarm import Swarm, SwarmConfig, SwarmResult
1415

1516
__all__ = [
1617
"a2a",
1718
"GraphBuilder",
1819
"GraphResult",
1920
"MultiAgentBase",
2021
"MultiAgentResult",
22+
"Swarm",
23+
"SwarmConfig",
24+
"SwarmResult",
2125
]

src/strands/multiagent/base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,15 @@ def get_agent_results(self) -> list[AgentResult]:
5858

5959
@dataclass
6060
class MultiAgentResult:
61-
"""Result from multi-agent execution with accumulated metrics."""
61+
"""Result from multi-agent execution with accumulated metrics.
6262
63-
results: dict[str, NodeResult]
63+
The status field represents the outcome of the MultiAgentBase execution:
64+
- COMPLETED: The execution was successfully accomplished
65+
- FAILED: The execution failed or produced an error
66+
"""
67+
68+
status: Status = Status.PENDING
69+
results: dict[str, NodeResult] = field(default_factory=lambda: {})
6470
accumulated_usage: Usage = field(default_factory=lambda: Usage(inputTokens=0, outputTokens=0, totalTokens=0))
6571
accumulated_metrics: Metrics = field(default_factory=lambda: Metrics(latencyMs=0))
6672
execution_count: int = 0

src/strands/multiagent/graph.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,8 @@ class GraphState:
6666

6767
@dataclass
6868
class GraphResult(MultiAgentResult):
69-
"""Result from graph execution - extends MultiAgentResult with graph-specific details.
69+
"""Result from graph execution - extends MultiAgentResult with graph-specific details."""
7070

71-
The status field represents the outcome of the graph execution:
72-
- COMPLETED: The graph execution was successfully accomplished
73-
- FAILED: The graph execution failed or produced an error
74-
"""
75-
76-
status: Status = Status.PENDING
7771
total_nodes: int = 0
7872
completed_nodes: int = 0
7973
failed_nodes: int = 0
@@ -468,12 +462,12 @@ def _build_node_input(self, node: GraphNode) -> str:
468462
def _build_result(self) -> GraphResult:
469463
"""Build graph result from current state."""
470464
return GraphResult(
465+
status=self.state.status,
471466
results=self.state.results,
472467
accumulated_usage=self.state.accumulated_usage,
473468
accumulated_metrics=self.state.accumulated_metrics,
474469
execution_count=self.state.execution_count,
475470
execution_time=self.state.execution_time,
476-
status=self.state.status,
477471
total_nodes=self.state.total_nodes,
478472
completed_nodes=len(self.state.completed_nodes),
479473
failed_nodes=len(self.state.failed_nodes),

0 commit comments

Comments
 (0)