Full sync - all projects, memory, configs
This commit is contained in:
96
extract_sessions.py
Normal file
96
extract_sessions.py
Normal file
@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Extract assistant turns from OpenClaw session files for auto-memory indexing.
|
||||
"""
|
||||
import sys
|
||||
import json
|
||||
import subprocess
|
||||
from collections import deque
|
||||
|
||||
def extract_assistant_turns(session_file, max_turns=10):
|
||||
"""Extract the last N assistant turns with their preceding user messages."""
|
||||
turns = []
|
||||
user_msg = None
|
||||
|
||||
try:
|
||||
with open(session_file, 'r') as f:
|
||||
for line in f:
|
||||
try:
|
||||
entry = json.loads(line.strip())
|
||||
if entry.get('type') != 'message':
|
||||
continue
|
||||
|
||||
message = entry.get('message', {})
|
||||
role = message.get('role')
|
||||
content = message.get('content', [])
|
||||
|
||||
if role == 'user':
|
||||
# Extract text from user message
|
||||
text_content = []
|
||||
for item in content:
|
||||
if item.get('type') == 'text':
|
||||
text_content.append(item.get('text', ''))
|
||||
user_msg = ' '.join(text_content).strip()
|
||||
|
||||
elif role == 'assistant' and user_msg:
|
||||
# Extract text from assistant message (skip thinking and tool calls)
|
||||
text_content = []
|
||||
for item in content:
|
||||
if item.get('type') == 'text':
|
||||
text_content.append(item.get('text', ''))
|
||||
assistant_text = ' '.join(text_content).strip()
|
||||
|
||||
if assistant_text: # Only store if there's actual text content
|
||||
turns.append({
|
||||
'user': user_msg,
|
||||
'assistant': assistant_text,
|
||||
'agent_id': 'case',
|
||||
'session': 'main'
|
||||
})
|
||||
# Keep only the last max_turns
|
||||
if len(turns) > max_turns:
|
||||
turns.pop(0)
|
||||
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error reading session file: {e}", file=sys.stderr)
|
||||
return []
|
||||
|
||||
return turns
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python3 extract_sessions.py <session_file>", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
session_file = sys.argv[1]
|
||||
turns = extract_assistant_turns(session_file)
|
||||
|
||||
if not turns:
|
||||
print("No assistant turns found", file=sys.stderr)
|
||||
return 0
|
||||
|
||||
# Pipe each turn to the auto-memory-hook.py script
|
||||
hook_script = "/home/wdjones/.openclaw/workspace/tools/auto-memory-hook.py"
|
||||
|
||||
for turn in turns:
|
||||
try:
|
||||
json_data = json.dumps(turn)
|
||||
result = subprocess.run(
|
||||
["python3", hook_script],
|
||||
input=json_data,
|
||||
text=True,
|
||||
capture_output=True
|
||||
)
|
||||
if result.returncode != 0:
|
||||
print(f"Error processing turn: {result.stderr}", file=sys.stderr)
|
||||
except Exception as e:
|
||||
print(f"Error running auto-memory-hook: {e}", file=sys.stderr)
|
||||
|
||||
print(f"Processed {len(turns)} assistant turns")
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user