91 lines
3.1 KiB
Python
91 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
|
|
# Read the session file and extract the last 10 assistant turns
|
|
session_file = '/home/wdjones/.openclaw/agents/main/sessions/2c022034-fccc-4c2c-b8f0-dce45ad22e68.jsonl'
|
|
|
|
messages = []
|
|
with open(session_file, 'r') as f:
|
|
for line in f:
|
|
try:
|
|
data = json.loads(line.strip())
|
|
if data.get('type') == 'message':
|
|
messages.append(data)
|
|
except:
|
|
continue
|
|
|
|
# Find assistant turns and their corresponding user messages
|
|
assistant_turns = []
|
|
for i in range(len(messages)):
|
|
msg = messages[i]
|
|
if msg.get('message', {}).get('role') == 'assistant':
|
|
# Find the preceding user message
|
|
user_msg = None
|
|
for j in range(i-1, -1, -1):
|
|
if messages[j].get('message', {}).get('role') == 'user':
|
|
user_msg = messages[j]
|
|
break
|
|
|
|
if user_msg:
|
|
# Extract text content
|
|
user_text = ''
|
|
assistant_text = ''
|
|
|
|
# Get user text
|
|
user_content = user_msg.get('message', {}).get('content', [])
|
|
if isinstance(user_content, list):
|
|
for item in user_content:
|
|
if item.get('type') == 'text':
|
|
user_text += item.get('text', '')
|
|
elif isinstance(user_content, str):
|
|
user_text = user_content
|
|
|
|
# Get assistant text (skip thinking blocks and tool calls)
|
|
assistant_content = msg.get('message', {}).get('content', [])
|
|
if isinstance(assistant_content, list):
|
|
for item in assistant_content:
|
|
if item.get('type') == 'text':
|
|
assistant_text += item.get('text', '')
|
|
elif isinstance(assistant_content, str):
|
|
assistant_text = assistant_content
|
|
|
|
if user_text.strip() and assistant_text.strip():
|
|
assistant_turns.append({
|
|
'user': user_text.strip(),
|
|
'assistant': assistant_text.strip(),
|
|
'agent_id': 'case',
|
|
'session': 'main'
|
|
})
|
|
|
|
# Get the last 10 turns
|
|
recent_turns = assistant_turns[-10:]
|
|
print(f'Found {len(recent_turns)} recent assistant turns to process')
|
|
|
|
# Process each turn through the auto-memory hook
|
|
success_count = 0
|
|
for i, turn in enumerate(recent_turns):
|
|
try:
|
|
# Pipe JSON to the auto-memory hook script
|
|
result = subprocess.run(
|
|
['python3', '/home/wdjones/.openclaw/workspace/tools/auto-memory-hook.py'],
|
|
input=json.dumps(turn),
|
|
text=True,
|
|
capture_output=True,
|
|
timeout=30
|
|
)
|
|
|
|
if result.returncode == 0:
|
|
print(f'Turn {i+1}: SUCCESS')
|
|
success_count += 1
|
|
else:
|
|
print(f'Turn {i+1}: FAILED - {result.stderr.strip()}')
|
|
|
|
except subprocess.TimeoutExpired:
|
|
print(f'Turn {i+1}: TIMEOUT')
|
|
except Exception as e:
|
|
print(f'Turn {i+1}: ERROR - {str(e)}')
|
|
|
|
print(f'\nProcessed {success_count}/{len(recent_turns)} turns successfully') |