#!/usr/bin/env python3 import json import sys from pathlib import Path def extract_last_assistant_turns(session_file, count=10): """Extract the last N assistant turns with their preceding user messages""" with open(session_file, 'r') as f: lines = f.readlines() # Parse JSONL and extract message entries messages = [] for line in lines: try: data = json.loads(line.strip()) if data.get('type') == 'message' and 'message' in data: msg = data['message'] content = "" if 'content' in msg: # Extract text content from content array for item in msg['content']: if isinstance(item, dict) and item.get('type') == 'text': content += item.get('text', '') messages.append({ 'role': msg['role'], 'content': content.strip(), 'timestamp': data['timestamp'] }) except (json.JSONDecodeError, KeyError): continue # Find last N assistant turns with their preceding user messages assistant_turns = [] user_msg = "" for i, msg in enumerate(messages): if msg['role'] == 'user': user_msg = msg['content'] elif msg['role'] == 'assistant' and user_msg: assistant_turns.append({ 'user': user_msg, 'assistant': msg['content'], 'agent_id': 'case', 'session': 'main' }) user_msg = "" # Reset for next turn return assistant_turns[-count:] if __name__ == "__main__": session_file = sys.argv[1] if len(sys.argv) > 1 else "/home/wdjones/.openclaw/agents/main/sessions/2c022034-fccc-4c2c-b8f0-dce45ad22e68.jsonl" turns = extract_last_assistant_turns(session_file) # Send each turn to the auto-memory hook for turn in turns: print(json.dumps(turn))