ソースを参照

Document current capture behavior

Lukas Goldschmidt 1 ヶ月 前
コミット
892afeb4e0
2 ファイル変更4 行追加20 行削除
  1. 4 3
      README.md
  2. 0 17
      hook/handler.ts

+ 4 - 3
README.md

@@ -1,10 +1,12 @@
 # mem0-auto-capture hook
 
+**Version:** v1.0.1
+
 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.
+1. **Auto-Capture** — whenever a received message matches the configured trigger (default: every message or the phrase “please remember”), the hook POSTs the **current user input** plus the **previous assistant reply** to `<baseUrl>/memories` with the resolved user id. It deduplicates captures by hashing the payload and short-circuiting identical sends 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.
 
@@ -26,7 +28,6 @@ All options are read from environment variables (prefixed `MEM0_`) or a JSON fil
 | `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`:
@@ -78,7 +79,7 @@ All options are read from environment variables (prefixed `MEM0_`) or a JSON fil
 - `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.
+- **Assistant lookup:** auto-capture grabs the previous assistant reply by scanning the session JSONL (via `sessions.json`) for the last assistant message **before** the current user event. It falls back to `/tmp/openclaw-chat.log` only if the session file is unavailable.
 - **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]`).
 

+ 0 - 17
hook/handler.ts

@@ -45,9 +45,6 @@ function loadPluginCfg() {
     autoRecall:
       (process.env.MEM0_AUTO_RECALL ||
         String(fileCfg.autoRecall ?? "true")) !== "false",
-    recentKeep: Number(
-      process.env.MEM0_RECENT_KEEP || fileCfg.recentKeep || 5
-    ),
     // Knowledge-base settings (tool-only; hook no longer injects knowledge)
     knowledgeUserId:
       process.env.MEM0_KNOWLEDGE_USER_ID ||
@@ -106,7 +103,6 @@ interface SearchResponse {
 // ---------------------------------------------------------------------------
 // In-memory state — capped to avoid unbounded growth
 // ---------------------------------------------------------------------------
-const MAX_SESSIONS = 500;
 const MAX_TRANSCRIPTS = 1000;
 
 class LRUMap<K, V> extends Map<K, V> {
@@ -123,7 +119,6 @@ class LRUMap<K, V> extends Map<K, V> {
   }
 }
 
-const recentBySession = new LRUMap<string, string[]>(MAX_SESSIONS);
 const transcriptByMessageId = new LRUMap<string, string>(MAX_TRANSCRIPTS);
 
 // ---------------------------------------------------------------------------
@@ -139,18 +134,6 @@ function getAgentIdFromSessionKey(sessionKey?: string): string | undefined {
   return undefined;
 }
 
-function pushRecent(
-  sessionKey: string,
-  text: string,
-  keep: number
-): string[] {
-  const list = recentBySession.get(sessionKey) || [];
-  list.push(text);
-  while (list.length > keep) list.shift();
-  recentBySession.set(sessionKey, list);
-  return list;
-}
-
 function isMediaPlaceholder(text: string): boolean {
   return /<media:[^>]+>/i.test(text);
 }