-
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
Open
aditya270520
wants to merge
8
commits into
strands-agents:main
Choose a base branch
from
aditya270520:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+367
−216
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b82496c
feat: expose user-defined state in MultiAgent Graph
0a8f464
refactor: address reviewer feedback for backward compatibility
aditya270520 1087bd6
Merge branch 'strands-agents:main' into main
aditya270520 caa9d1e
fix: restore missing Swarm methods and fix node object handling
aditya270520 d081102
Merge remote changes
aditya270520 84cebea
style: fix import sorting and formatting issues
aditya270520 b4314f5
style: fix formatting and ensure code quality
aditya270520 a648268
Merge branch 'strands-agents:main' into main
aditya270520 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
Provides minimal foundation for multi-agent patterns (Swarm, Graph). | ||
""" | ||
|
||
import copy | ||
import json | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass, field | ||
from enum import Enum | ||
|
@@ -22,6 +24,88 @@ class Status(Enum): | |
FAILED = "failed" | ||
|
||
|
||
@dataclass | ||
class SharedContext: | ||
"""Shared context between multi-agent nodes. | ||
|
||
This class provides a key-value store for sharing information across nodes | ||
in multi-agent systems like Graph and Swarm. It validates that all values | ||
are JSON serializable to ensure compatibility. | ||
""" | ||
|
||
context: dict[str, dict[str, Any]] = field(default_factory=dict) | ||
|
||
def add_context(self, node_id: str, key: str, value: Any) -> None: | ||
"""Add context for a specific node. | ||
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. It also looks like unit tests are failing. May need to rebase or address them if they are still failing |
||
|
||
Args: | ||
node_id: The ID of the node adding the context | ||
key: The key to store the value under | ||
value: The value to store (must be JSON serializable) | ||
|
||
Raises: | ||
ValueError: If key is invalid or value is not JSON serializable | ||
""" | ||
self._validate_key(key) | ||
self._validate_json_serializable(value) | ||
|
||
if node_id not in self.context: | ||
self.context[node_id] = {} | ||
self.context[node_id][key] = value | ||
|
||
def get_context(self, node_id: str, key: str | None = None) -> Any: | ||
"""Get context for a specific node. | ||
|
||
Args: | ||
node_id: The ID of the node to get context for | ||
key: The specific key to retrieve (if None, returns all context for the node) | ||
|
||
Returns: | ||
The stored value, entire context dict for the node, or None if not found | ||
""" | ||
if node_id not in self.context: | ||
return None if key else {} | ||
|
||
if key is None: | ||
return copy.deepcopy(self.context[node_id]) | ||
else: | ||
value = self.context[node_id].get(key) | ||
return copy.deepcopy(value) if value is not None else None | ||
|
||
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 NodeResult: | ||
"""Unified result from node execution - handles both Agent and nested MultiAgentBase results. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thanks for raising this!
I have a couple of concerns relating to backwards compatibility.
It looks like we switched from SwarmNode to node_id. Can we instead retain the Node object. Refactor SwarmNode into base.py as some MultiAgentNode.
Then we need to maintain backwards compatibility via aliases in swarm. meaning we do not want to break imports as right now it will be broken if a user has an import like
from strands.multiagent.swarm import SharedContext
so we need to avoid breaking consumers for SharedContext and Node.