-
Notifications
You must be signed in to change notification settings - Fork 314
feat: expose user-defined state in MultiAgent Graph #703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b82496c
0a8f464
1087bd6
caa9d1e
d081102
84cebea
b4314f5
a648268
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ | |
|
||
import asyncio | ||
import copy | ||
import json | ||
import logging | ||
import time | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
@@ -29,16 +28,15 @@ | |
from ..tools.decorator import tool | ||
from ..types.content import ContentBlock, Messages | ||
from ..types.event_loop import Metrics, Usage | ||
from .base import MultiAgentBase, MultiAgentResult, NodeResult, Status | ||
from .base import MultiAgentBase, MultiAgentNode, MultiAgentResult, NodeResult, SharedContext, Status | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class SwarmNode: | ||
class SwarmNode(MultiAgentNode): | ||
"""Represents a node (e.g. Agent) in the swarm.""" | ||
|
||
node_id: str | ||
executor: Agent | ||
_initial_messages: Messages = field(default_factory=list, init=False) | ||
_initial_state: AgentState = field(default_factory=AgentState, init=False) | ||
|
@@ -73,55 +71,6 @@ def reset_executor_state(self) -> None: | |
self.executor.state = AgentState(self._initial_state.get()) | ||
|
||
|
||
@dataclass | ||
class SharedContext: | ||
"""Shared context between swarm nodes.""" | ||
|
||
context: dict[str, dict[str, Any]] = field(default_factory=dict) | ||
|
||
def add_context(self, node: SwarmNode, key: str, value: Any) -> None: | ||
"""Add context.""" | ||
self._validate_key(key) | ||
self._validate_json_serializable(value) | ||
|
||
if node.node_id not in self.context: | ||
self.context[node.node_id] = {} | ||
self.context[node.node_id][key] = value | ||
|
||
def _validate_key(self, key: str) -> None: | ||
"""Validate that a key is valid. | ||
|
||
Args: | ||
key: The key to validate | ||
|
||
Raises: | ||
ValueError: If key is invalid | ||
""" | ||
if key is None: | ||
raise ValueError("Key cannot be None") | ||
if not isinstance(key, str): | ||
raise ValueError("Key must be a string") | ||
if not key.strip(): | ||
raise ValueError("Key cannot be empty") | ||
|
||
def _validate_json_serializable(self, value: Any) -> None: | ||
"""Validate that a value is JSON serializable. | ||
|
||
Args: | ||
value: The value to validate | ||
|
||
Raises: | ||
ValueError: If value is not JSON serializable | ||
""" | ||
try: | ||
json.dumps(value) | ||
except (TypeError, ValueError) as e: | ||
raise ValueError( | ||
f"Value is not JSON serializable: {type(value).__name__}. " | ||
f"Only JSON-compatible types (str, int, float, bool, list, dict, None) are allowed." | ||
) from e | ||
|
||
|
||
@dataclass | ||
class SwarmState: | ||
"""Current state of swarm execution.""" | ||
|
@@ -654,3 +603,8 @@ def _build_result(self) -> SwarmResult: | |
execution_time=self.state.execution_time, | ||
node_history=self.state.node_history, | ||
) | ||
|
||
|
||
# Backward compatibility aliases | ||
# These ensure that existing imports continue to work | ||
__all__ = ["SwarmNode", "SharedContext", "Status"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amazing, thanks! I will pull this down and test a little bit today but looks great! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello @dbschmigelski , please test and merge this if you find any issue to fix please let me know |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also looks like unit tests are failing. May need to rebase or address them if they are still failing