Execute a memory tool and return the result as a string.
(
store: MemoryScopeStore, tool_name: str, arguments: dict
)
| 101 | |
| 102 | |
| 103 | async def handle_memory_tool( |
| 104 | store: MemoryScopeStore, tool_name: str, arguments: dict |
| 105 | ) -> str: |
| 106 | """Execute a memory tool and return the result as a string.""" |
| 107 | try: |
| 108 | if tool_name == "remember": |
| 109 | scope = MemoryScope(arguments["scope"]) |
| 110 | entry = store.remember(scope, arguments["key"], arguments["content"]) |
| 111 | return f"Remembered '{entry.key}' in {scope.value} scope." |
| 112 | |
| 113 | if tool_name == "recall": |
| 114 | scope_str = arguments.get("scope") |
| 115 | recall_scope: MemoryScope | None = ( |
| 116 | MemoryScope(scope_str) if scope_str else None |
| 117 | ) |
| 118 | key = arguments.get("key") |
| 119 | query = arguments.get("query") |
| 120 | |
| 121 | entries = store.recall(scope=recall_scope, key=key, query=query) |
| 122 | if not entries: |
| 123 | return "No memories found." |
| 124 | |
| 125 | lines = [] |
| 126 | for e in entries: |
| 127 | lines.append(f"- [{e.key}]: {e.content}") |
| 128 | return "\n".join(lines) |
| 129 | |
| 130 | if tool_name == "forget": |
| 131 | scope = MemoryScope(arguments["scope"]) |
| 132 | removed = store.forget(scope, arguments["key"]) |
| 133 | if removed: |
| 134 | return f"Forgot '{arguments['key']}' from {scope.value} scope." |
| 135 | return ( |
| 136 | f"No memory with key '{arguments['key']}' found in {scope.value} scope." |
| 137 | ) |
| 138 | |
| 139 | return f"Unknown memory tool: {tool_name}" |
| 140 | |
| 141 | except Exception as exc: |
| 142 | logger.warning("Memory tool %s failed: %s", tool_name, exc) |
| 143 | return f"Memory tool error: {exc}" |