{ "id": "memory_store", "name": "Memory Store", "version": "1.0.0", "description": "In-memory key/value store with optional TTL (time-to-live). Agents can use this for short-term working memory, scratchpads, and cross-step state sharing within a task session.", "author": "Chris4K", "tags": ["memory", "state", "storage", "cache"], "dependencies": [], "schema": { "input": { "action": "str — 'set' | 'get' | 'delete' | 'list' | 'clear'", "key": "str — storage key", "value": "any — value to store (for 'set')", "ttl": "int — seconds until expiry (optional, for 'set')" }, "output": { "ok": "bool", "value": "any", "keys": "list[str]", "message": "str" } }, "code": "import time\nfrom typing import Any, Optional\n\n# Module-level store (persists within the agent session)\n_STORE: dict[str, dict] = {}\n\n\ndef _is_expired(entry: dict) -> bool:\n if entry.get(\"expires_at\") is None:\n return False\n return time.time() > entry[\"expires_at\"]\n\n\ndef _cleanup():\n expired = [k for k, v in _STORE.items() if _is_expired(v)]\n for k in expired:\n del _STORE[k]\n\n\ndef execute(\n action: str,\n key: Optional[str] = None,\n value: Any = None,\n ttl: Optional[int] = None,\n namespace: str = \"default\",\n) -> dict:\n \"\"\"\n Perform a memory operation.\n\n Actions:\n set — store key=value (optional ttl in seconds)\n get — retrieve value by key\n delete — remove a key\n list — list all keys in namespace\n clear — wipe the entire namespace\n stats — return store statistics\n \"\"\"\n _cleanup()\n ns_prefix = f\"{namespace}:\"\n\n action = action.lower().strip()\n\n if action == \"set\":\n if key is None:\n return {\"ok\": False, \"message\": \"'key' is required for set\"}\n full_key = f\"{ns_prefix}{key}\"\n entry = {\"value\": value, \"created_at\": time.time(), \"expires_at\": None}\n if ttl is not None and ttl > 0:\n entry[\"expires_at\"] = time.time() + ttl\n _STORE[full_key] = entry\n return {\"ok\": True, \"key\": key, \"message\": f\"Stored '{key}' in namespace '{namespace}'\"}\n\n elif action == \"get\":\n if key is None:\n return {\"ok\": False, \"message\": \"'key' is required for get\"}\n full_key = f\"{ns_prefix}{key}\"\n entry = _STORE.get(full_key)\n if entry is None or _is_expired(entry):\n if entry:\n del _STORE[full_key]\n return {\"ok\": False, \"value\": None, \"message\": f\"Key '{key}' not found\"}\n return {\"ok\": True, \"key\": key, \"value\": entry[\"value\"]}\n\n elif action == \"delete\":\n full_key = f\"{ns_prefix}{key}\"\n existed = full_key in _STORE\n _STORE.pop(full_key, None)\n return {\"ok\": True, \"message\": f\"Deleted '{key}'\" if existed else f\"Key '{key}' not found\"}\n\n elif action == \"list\":\n keys = [\n k[len(ns_prefix):] for k, v in _STORE.items()\n if k.startswith(ns_prefix) and not _is_expired(v)\n ]\n return {\"ok\": True, \"keys\": sorted(keys), \"count\": len(keys), \"namespace\": namespace}\n\n elif action == \"clear\":\n to_delete = [k for k in _STORE if k.startswith(ns_prefix)]\n for k in to_delete:\n del _STORE[k]\n return {\"ok\": True, \"message\": f\"Cleared {len(to_delete)} keys from namespace '{namespace}'\"}\n\n elif action == \"stats\":\n _cleanup()\n return {\n \"ok\": True,\n \"total_keys\": len(_STORE),\n \"namespaces\": list({k.split(\":\")[0] for k in _STORE}),\n }\n\n else:\n return {\"ok\": False, \"message\": f\"Unknown action: '{action}'. Use: set, get, delete, list, clear, stats\"}\n", "downloads": 0, "created_at": 1710000003 }