generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 204
Add "reset" action to mem0_memory actions
#293
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
acarbonetto
wants to merge
4
commits into
strands-agents:main
Choose a base branch
from
acarbonetto:acarbo/add_mem0_memory_reset
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| """ | ||
| Tool for managing memories using Mem0 (store, delete, list, get, and retrieve) | ||
| Tool for managing memories using Mem0 (store, delete, list, get, retrieve, and reset) | ||
|
|
||
| This module provides comprehensive memory management capabilities using | ||
| Mem0 as the backend. It handles all aspects of memory management with | ||
|
|
@@ -13,6 +13,7 @@ | |
| • list: Retrieve all memories for a user or agent | ||
| • get: Retrieve specific memories by memory ID | ||
| • retrieve: Perform semantic search across all memories | ||
| • reset: Remove all existing memories and relationships | ||
|
|
||
| 2. Safety Features: | ||
| • User confirmation for mutative operations | ||
|
|
@@ -95,14 +96,16 @@ | |
| "2. Retrieve memories by ID or semantic search (requires user_id or agent_id)\n" | ||
| "3. List all memories for a user/agent (requires user_id or agent_id)\n" | ||
| "4. Delete memories\n" | ||
| "5. Get memory history\n\n" | ||
| "5. Get memory history\n" | ||
| "6. Reset all memories\n\n" | ||
| "Actions:\n" | ||
| "- store: Store new memory (requires user_id or agent_id)\n" | ||
| "- get: Get memory by ID\n" | ||
| "- list: List all memories (requires user_id or agent_id)\n" | ||
| "- retrieve: Semantic search (requires user_id or agent_id)\n" | ||
| "- delete: Delete memory\n" | ||
| "- history: Get memory history\n\n" | ||
| "- history: Get memory history\n" | ||
| "- reset: Delete all memories\n\n" | ||
| "Note: Most operations require either user_id or agent_id to be specified. The tool will automatically " | ||
| "attempt to retrieve relevant memories when user_id or agent_id is available." | ||
| ), | ||
|
|
@@ -112,8 +115,8 @@ | |
| "properties": { | ||
| "action": { | ||
| "type": "string", | ||
| "description": ("Action to perform (store, get, list, retrieve, delete, history)"), | ||
| "enum": ["store", "get", "list", "retrieve", "delete", "history"], | ||
| "description": ("Action to perform (store, get, list, retrieve, delete, history, reset)"), | ||
| "enum": ["store", "get", "list", "retrieve", "delete", "history", "reset"], | ||
| }, | ||
| "content": { | ||
| "type": "string", | ||
|
|
@@ -129,11 +132,11 @@ | |
| }, | ||
| "user_id": { | ||
| "type": "string", | ||
| "description": "User ID for the memory operations (required for store, list, retrieve actions)", | ||
| "description": "User ID for the memory operations (either user_id or agent_id is required for store, list, retrieve, and reset actions)", | ||
| }, | ||
| "agent_id": { | ||
| "type": "string", | ||
| "description": "Agent ID for the memory operations (required for store, list, retrieve actions)", | ||
| "description": "Agent ID for the memory operations (either user_id or agent_id is required for store, list, retrieve, and reset actions)", | ||
| }, | ||
| "metadata": { | ||
| "type": "object", | ||
|
|
@@ -415,6 +418,13 @@ def get_memory_history(self, memory_id: str): | |
| """Get the history of a memory by ID.""" | ||
| return self.mem0.history(memory_id) | ||
|
|
||
| def reset_memories(self, user_id: Optional[str] = None, agent_id: Optional[str] = None): | ||
| """Delete all memories for a user or agent.""" | ||
| if not user_id and not agent_id: | ||
| raise ValueError("Either user_id or agent_id must be provided") | ||
|
|
||
| return self.mem0.delete_all(user_id=user_id, agent_id=agent_id) | ||
|
|
||
|
|
||
| def format_get_response(memory: Dict) -> Panel: | ||
| """Format get memory response.""" | ||
|
|
@@ -672,7 +682,7 @@ def mem0_memory(tool: ToolUse, **kwargs: Any) -> ToolResult: | |
| action = tool_input["action"] | ||
|
|
||
| # For mutative operations, show confirmation dialog unless in BYPASS_TOOL_CONSENT mode | ||
| mutative_actions = {"store", "delete"} | ||
| mutative_actions = {"store", "delete", "reset"} | ||
| needs_confirmation = action in mutative_actions and not strands_dev | ||
|
|
||
| if needs_confirmation: | ||
|
|
@@ -724,6 +734,27 @@ def mem0_memory(tool: ToolUse, **kwargs: Any) -> ToolResult: | |
| border_style="red", | ||
| ) | ||
| ) | ||
| elif action == "reset": | ||
| # Try to get memory info first for better context | ||
| try: | ||
| client.reset_memories(tool_input.get("user_id"), tool_input.get("agent_id")) | ||
|
|
||
| console.print( | ||
| Panel( | ||
| "Deleting memories...", | ||
| title="[bold red]⚠️ All Memories to be permanently deleted", | ||
|
Contributor
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. Should we print the
Contributor
Author
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. We don't do that anywhere else. |
||
| border_style="red", | ||
| ) | ||
| ) | ||
| except Exception: | ||
| # Fall back to basic info if we can't get memory details | ||
| console.print( | ||
| Panel( | ||
| "Deleting memories...", | ||
| title="[bold red]⚠️ All Memories to be permanently deleted", | ||
| border_style="red", | ||
| ) | ||
| ) | ||
|
|
||
| # Execute the requested action | ||
| if action == "store": | ||
|
|
@@ -827,6 +858,16 @@ def mem0_memory(tool: ToolUse, **kwargs: Any) -> ToolResult: | |
| content=[ToolResultContent(text=f"Memory {tool_input['memory_id']} deleted successfully")], | ||
| ) | ||
|
|
||
| elif action == "reset": | ||
| client.reset_memories(tool_input.get("user_id"), tool_input.get("agent_id")) | ||
| panel = Panel("All memories deleted.", title="[bold green]Memories Deleted", border_style="green") | ||
| console.print(panel) | ||
| return ToolResult( | ||
| toolUseId=tool_use_id, | ||
| status="success", | ||
| content=[ToolResultContent(text=f"Memories deleted successfully")], | ||
| ) | ||
|
|
||
| elif action == "history": | ||
| if not tool_input.get("memory_id"): | ||
| raise ValueError("memory_id is required for history action") | ||
|
|
||
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.
Did i miss anything