No Description

Lukas Goldschmidt 9a5c41ce00 Capture current input only 1 month ago
hook 9a5c41ce00 Capture current input only 1 month ago
PROJECT.md a8a400249e messages array, chat logger support 1 month ago
README.md b627bee1d2 Refine logging and copy latest hook 1 month ago
package.json 59b813ef70 Initial commit 2 months ago
tsconfig.json a8a400249e messages array, chat logger support 1 month ago

README.md

mem0-auto-capture hook

A self-contained OpenClaw hook that wires incoming messages to your Mem0 instance for auto-capture and auto-recall. It runs as a message hook inside the OpenClaw gateway, transcribes audio, decides what to store, queries memories/knowledge before the agent responds, and injects the results back into the prompt.

Key behaviors

  1. Auto-Capture — whenever a received message matches the configured trigger (default: every message or the phrase “please remember”), the hook gathers the most recent lines from that session (controlled by recentKeep) and POSTs them to <baseUrl>/memories with the resolved user id. It deduplicates captures by hashing the text and short-circuiting identical payloads within one minute.

  2. Auto-Recall — before the agent answers (on the preprocessed or transcribed event) it queries <baseUrl>/memories/search (conversational memory only), filters by rerank score, and injects [MEMORY - Personal] into bodyForAgent. Knowledge base retrieval is now tool-only.

  3. Audio support — if the event includes media, the hook posts the file to the local STT service at http://192.168.0.200:5005/transcribe, caches transcripts, and uses them for both capture and recall. It also rewrites bodyForAgent so that audio messages appear as text when the agent eats them.

Configuration

All options are read from environment variables (prefixed MEM0_) or a JSON file at ~/.openclaw/mem0.json. Missing values fall back to sensible defaults.

Option Source Default Notes
baseUrl MEM0_BASE_URL or config file (baseUrl) http://192.168.0.200:8420 The Mem0 server root.
userId MEM0_USER_ID or config file derived from sessionKey → agent ID, then fallback to userId or default Used for personal memories.
recallLimit MEM0_RECALL_LIMIT 5 How many personal memories to admit.
knowledgeLimit MEM0_KNOWLEDGE_LIMIT 5 How many knowledge-base hits to inject.
knowledgeUserId MEM0_KNOWLEDGE_USER_ID knowledge_base The user id that holds knowledge-base memories.
rerankThreshold MEM0_RERANK_THRESHOLD 0.002 Filters injected memories with low rerank scores.
captureTrigger MEM0_CAPTURE_TRIGGER always always / phrase / explicit.
triggerPhrase MEM0_TRIGGER_PHRASE please remember Used when captureTrigger is phrase.
autoCapture MEM0_AUTO_CAPTURE true Enable/disable captures.
autoRecall MEM0_AUTO_RECALL true Enable/disable recalls.
recentKeep MEM0_RECENT_KEEP 5 How many lines of recent context to bundle for a capture.
debugCapture MEM0_DEBUG_CAPTURE false When true, logs full outgoing messages payload for capture; otherwise logs only summary counts.

Example ~/.openclaw/mem0.json:

> {
>   "baseUrl": "http://192.168.0.200:8420",
>   "userId": "lukas",
>   "captureTrigger": "phrase",
>   "triggerPhrase": "please remember",
>   "autoCapture": true,
>   "autoRecall": true
> }
> ```

## Deployment

1. Build + install dependencies (TypeScript only needs dev-time tooling; the runtime runs the `.ts` file directly):

bash cd mem0-auto-capture-hook npm install


2. Link the hook into OpenClaw (this installs the compiled `hook/handler.ts` directory):

bash openclaw hooks install --link $(pwd)/hook


3. Restart the gateway so it reloads hooks:

bash openclaw gateway restart ```

  1. Confirm via openclaw hooks list that the mem0-auto-capture hook is ready.

Testing & troubleshooting

  • Logs flow through a single helper and now print as [mem0] <tag> {...} (e.g., [mem0] auto-recall-query, [mem0] auto-capture, [mem0] auto-recall-injection). Filter on the tag to track capture/recall steps in /tmp/openclaw/... logs.
  • Use openclaw hooks simulate message (or your own integration tests) to feed the hook a fake event and watch the HTTP calls.
  • If the hook can’t reach Mem0, the recall/capture branches simply log errors and continue; the agent still runs but without those enhancements.

What’s in the repo

  • hook/handler.ts — the TypeScript entry point that implements capture, recall, STT, and caching.
  • package.json + tsconfig.json — minimal metadata so other developers can install dependencies and build if needed.
  • README.md & PROJECT.md — explain intent, configuration, and deployment.

Recent implementation notes

  • Assistant lookup fallback: auto-capture first tries to fetch the previous assistant message by matching sessionKey in /tmp/openclaw-chat.log. If that fails, it falls back to the latest assistant message globally, logs a warning, and still sends the capture.
  • Capture payload shape: auto-capture sends messages: [...] to /memories in all cases. Minimum payload is one user item; assistant entry is appended when available.
  • Personal memory age prefix: injected [MEMORY - Personal] lines prepend age from created_at when present (for example [25m ago] / [1d ago]).

Next steps

  • Tag releases (e.g., v1.0.0) and push to Git so you can openclaw hooks install directly from the repo.
  • Automate builds/tests if you plan to extend the hook (e.g., add unit tests around the Mem0 request helpers).
  • If you want the hook to share code with other Mem0 extensions, consider turning it into a proper plugin that registers both the hook and helper tools.

Happy to help set up the Git repo or draft a release template—shall I do that next?