Full sync - all projects, memory, configs
This commit is contained in:
89
tools/extract_memory_turns.py
Executable file
89
tools/extract_memory_turns.py
Executable file
@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import sys
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
def extract_turns(session_file):
|
||||
"""Extract user-assistant turn pairs from session transcript"""
|
||||
turns = []
|
||||
messages = []
|
||||
|
||||
# Read and parse JSONL
|
||||
with open(session_file, 'r') as f:
|
||||
for line in f:
|
||||
try:
|
||||
data = json.loads(line.strip())
|
||||
if data.get('type') == 'message' and 'message' in data:
|
||||
msg = data['message']
|
||||
if msg.get('role') in ['user', 'assistant']:
|
||||
messages.append(msg)
|
||||
except:
|
||||
continue
|
||||
|
||||
# Pair user messages with assistant responses
|
||||
for i in range(len(messages) - 1):
|
||||
if (messages[i].get('role') == 'user' and
|
||||
messages[i+1].get('role') == 'assistant'):
|
||||
|
||||
user_content = extract_text_content(messages[i])
|
||||
assistant_content = extract_text_content(messages[i+1])
|
||||
|
||||
if user_content and assistant_content:
|
||||
turns.append({
|
||||
'user': user_content,
|
||||
'assistant': assistant_content,
|
||||
'agent_id': 'case',
|
||||
'session': 'main'
|
||||
})
|
||||
|
||||
return turns[-10:] # Last 10 turns
|
||||
|
||||
def extract_text_content(message):
|
||||
"""Extract text content from message"""
|
||||
content = message.get('content', [])
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
|
||||
text_parts = []
|
||||
for part in content:
|
||||
if isinstance(part, dict) and part.get('type') == 'text':
|
||||
text_parts.append(part.get('text', ''))
|
||||
|
||||
return ' '.join(text_parts).strip()
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: extract_memory_turns.py <session_file>")
|
||||
sys.exit(1)
|
||||
|
||||
session_file = sys.argv[1]
|
||||
turns = extract_turns(session_file)
|
||||
|
||||
print(f"Extracted {len(turns)} turns from {session_file}")
|
||||
|
||||
# Send each turn to auto-memory-hook.py
|
||||
for i, turn in enumerate(turns):
|
||||
try:
|
||||
proc = subprocess.Popen(
|
||||
['python3', '/home/wdjones/.openclaw/workspace/tools/auto-memory-hook.py'],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True
|
||||
)
|
||||
|
||||
json_input = json.dumps(turn)
|
||||
stdout, stderr = proc.communicate(input=json_input)
|
||||
|
||||
if proc.returncode == 0:
|
||||
print(f"Turn {i+1}: Processed successfully")
|
||||
else:
|
||||
print(f"Turn {i+1}: Error - {stderr}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Turn {i+1}: Failed to process - {e}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user