#!/usr/bin/env python3 import json import subprocess import sys # Path to the session file session_file = "/home/wdjones/.openclaw/agents/main/sessions/b2a4c390-00b6-44e6-84b8-0267aa31bfde.jsonl" def extract_messages(file_path): messages = [] with open(file_path, 'r') as f: for line in f: data = json.loads(line.strip()) if data.get('type') == 'message' and 'message' in data: messages.append({ 'role': data['message']['role'], 'content': data['message']['content'], 'id': data['id'], 'timestamp': data['timestamp'] }) return messages def get_content_text(content): """Extract text from content array""" if isinstance(content, list): text_parts = [] for item in content: if isinstance(item, dict): if item.get('type') == 'text': text_parts.append(item.get('text', '')) elif item.get('type') == 'thinking': # Skip thinking blocks for memory indexing continue else: text_parts.append(str(item)) return ' '.join(text_parts).strip() elif isinstance(content, str): return content else: return str(content) def find_assistant_turns(messages): """Find assistant turns with their preceding user messages""" turns = [] for i, msg in enumerate(messages): if msg['role'] == 'assistant': # Find the most recent user message before this assistant message user_msg = None for j in range(i-1, -1, -1): if messages[j]['role'] == 'user': user_msg = messages[j] break if user_msg: user_text = get_content_text(user_msg['content']) assistant_text = get_content_text(msg['content']) # Only include if both have meaningful content if user_text.strip() and assistant_text.strip(): turns.append({ 'user': user_text, 'assistant': assistant_text, 'agent_id': 'case', 'session': 'main' }) return turns def main(): try: print("Extracting messages from session file...") messages = extract_messages(session_file) print(f"Found {len(messages)} total messages") turns = find_assistant_turns(messages) print(f"Found {len(turns)} assistant turns") # Get the last 10 turns last_10_turns = turns[-10:] if len(turns) >= 10 else turns print(f"Processing {len(last_10_turns)} turns for memory indexing") # Process each turn through the auto-memory hook for i, turn in enumerate(last_10_turns): print(f"Processing turn {i+1}/{len(last_10_turns)}") json_data = json.dumps(turn) # Pipe to the auto-memory-hook script try: result = subprocess.run([ 'python3', '/home/wdjones/.openclaw/workspace/tools/auto-memory-hook.py' ], input=json_data, text=True, capture_output=True) if result.returncode != 0: print(f"Warning: auto-memory-hook returned error code {result.returncode}") print(f"stderr: {result.stderr}") else: print(f"Turn {i+1} processed successfully") except Exception as e: print(f"Error processing turn {i+1}: {e}") print("Memory indexing complete") except Exception as e: print(f"Error: {e}") return 1 return 0 if __name__ == "__main__": sys.exit(main())