-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_watcher.py
More file actions
269 lines (224 loc) · 8.47 KB
/
session_watcher.py
File metadata and controls
269 lines (224 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3.12
"""Session Watcher - extracts learnings from completed OpenClaw sessions."""
import argparse
import json
import logging
from datetime import datetime, timezone
from pathlib import Path
import requests
import yaml
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [session_watcher] %(levelname)s %(message)s",
)
log = logging.getLogger(__name__)
BASE_DIR = Path(__file__).resolve().parent
PROCESSED_PATH = BASE_DIR / "processed_sessions.json"
SESSION_DIRS = [
Path("~/.openclaw/agents/main/sessions").expanduser(),
Path("~/.openclaw/cron/runs").expanduser(),
]
MIN_MESSAGES = 5
MAX_MESSAGES = 50
MAX_CHARS = 8000
def load_config():
with open(BASE_DIR / "config.yaml") as f:
return yaml.safe_load(f)
def ask_ollama(cfg, prompt, temperature=0.3):
url = cfg["ollama"]["url"]
for model in [cfg["ollama"]["model"], cfg["ollama"]["fallback_model"]]:
try:
resp = requests.post(url, json={
"model": model,
"prompt": prompt,
"stream": False,
"options": {"temperature": temperature},
}, timeout=120)
resp.raise_for_status()
return resp.json().get("response", "")
except Exception as e:
log.warning("Ollama call failed (model=%s): %s", model, e)
return None
def load_processed():
if PROCESSED_PATH.exists():
try:
return set(json.loads(PROCESSED_PATH.read_text()))
except (json.JSONDecodeError, TypeError):
return set()
return set()
def save_processed(processed):
PROCESSED_PATH.write_text(json.dumps(sorted(processed), indent=2))
def find_new_sessions(processed):
"""Find unprocessed .jsonl session files."""
new = []
for d in SESSION_DIRS:
if not d.exists():
continue
for f in d.glob("*.jsonl"):
if f.name not in processed:
new.append(f)
return sorted(new, key=lambda p: p.stat().st_mtime)
def extract_conversation(session_path):
"""Read session JSONL and return user/assistant text messages."""
messages = []
try:
for line in session_path.read_text().splitlines():
if not line.strip():
continue
entry = json.loads(line)
if entry.get("type") != "message":
continue
msg = entry.get("message", {})
role = msg.get("role")
if role not in ("user", "assistant"):
continue
for block in msg.get("content", []):
if block.get("type") == "text" and block.get("text"):
messages.append({"role": role, "text": block["text"]})
except Exception as e:
log.warning("Failed to read %s: %s", session_path, e)
return messages
def truncate_conversation(messages):
"""Keep last MAX_MESSAGES, truncate to MAX_CHARS total."""
recent = messages[-MAX_MESSAGES:]
result = []
total = 0
for m in reversed(recent):
text = m["text"]
if total + len(text) > MAX_CHARS:
remaining = MAX_CHARS - total
if remaining > 100:
result.append({"role": m["role"], "text": text[:remaining] + "..."})
break
result.append(m)
total += len(text)
return list(reversed(result))
def format_for_prompt(messages):
return "\n".join(f"[{m['role']}]: {m['text']}" for m in messages)
def analyze_session(cfg, messages):
"""Ask Ollama to extract learnings from a conversation."""
convo = format_for_prompt(truncate_conversation(messages))
prompt = f"""Analyze this AI assistant conversation and extract learnings.
{convo}
Return ONLY valid JSON with these keys:
- "title": short title for what was done (string)
- "summary": 1-2 sentence summary (string)
- "outcome": "success", "failure", or "partial" (string)
- "errors": list of errors/problems encountered (list of strings, empty if none)
- "lessons": list of reusable lessons or patterns learned (list of strings, empty if none)
JSON:"""
raw = ask_ollama(cfg, prompt)
if not raw:
return None
try:
start = raw.index("{")
end = raw.rindex("}") + 1
return json.loads(raw[start:end])
except (ValueError, json.JSONDecodeError) as e:
log.warning("Failed to parse analysis JSON: %s", e)
return None
def store_experience(cfg, session_id, analysis, dry_run=False):
"""Store session experience in AOMS episodic tier."""
body = {
"type": "experience",
"payload": {
"title": analysis.get("title", "Untitled session"),
"summary": analysis.get("summary", ""),
"outcome": analysis.get("outcome", "partial"),
"session_id": session_id,
},
"tags": ["session"],
}
if dry_run:
log.info("[DRY RUN] Would store episodic: %s", json.dumps(body, indent=2))
return True
try:
resp = requests.post(cfg["aoms"]["url"] + "/memory/episodic", json=body, timeout=10)
resp.raise_for_status()
return True
except Exception as e:
log.warning("AOMS episodic store failed: %s", e)
return False
def store_lessons(cfg, analysis, dry_run=False):
"""Store lessons as procedural skills in AOMS."""
stored = 0
for lesson in analysis.get("lessons", []):
if not lesson or len(lesson) < 10:
continue
body = {
"type": "skill",
"payload": {
"skill_name": lesson[:60],
"description": lesson,
"when_to_use": "When encountering similar situations in future sessions",
"procedure": lesson,
"category": "session_learning",
},
"tags": ["session_learning"],
}
if dry_run:
log.info("[DRY RUN] Would store procedural: %s", body["payload"]["skill_name"])
stored += 1
continue
try:
resp = requests.post(cfg["aoms"]["url"] + "/memory/procedural", json=body, timeout=10)
resp.raise_for_status()
stored += 1
except Exception as e:
log.warning("AOMS procedural store failed: %s", e)
return stored
def log_result(session_id, analysis):
log_dir = BASE_DIR / "logs"
log_dir.mkdir(exist_ok=True)
log_file = log_dir / "session_watcher.log"
entry = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"session_id": session_id,
"title": analysis.get("title", ""),
"outcome": analysis.get("outcome", ""),
"lessons_count": len(analysis.get("lessons", [])),
}
with open(log_file, "a") as f:
f.write(json.dumps(entry) + "\n")
def main():
parser = argparse.ArgumentParser(description="Session Watcher - learn from sessions")
parser.add_argument("--dry-run", action="store_true", help="Log actions without writing to AOMS")
args = parser.parse_args()
cfg = load_config()
processed = load_processed()
new_sessions = find_new_sessions(processed)
if not new_sessions:
log.info("No new sessions to process.")
return
log.info("Found %d new session(s)", len(new_sessions))
for session_path in new_sessions:
sid = session_path.name
messages = extract_conversation(session_path)
if len(messages) < MIN_MESSAGES:
log.info("Skipping %s (%d messages, need %d)", sid, len(messages), MIN_MESSAGES)
processed.add(sid)
continue
log.info("Analyzing %s (%d messages)", sid, len(messages))
analysis = analyze_session(cfg, messages)
if not analysis:
log.warning("Analysis failed for %s, will retry next run", sid)
continue
store_experience(cfg, sid, analysis, dry_run=args.dry_run)
lessons_stored = store_lessons(cfg, analysis, dry_run=args.dry_run)
log.info("Session %s: %s (outcome=%s, lessons=%d)",
sid, analysis.get("title", "?"), analysis.get("outcome", "?"), lessons_stored)
log_result(sid, analysis)
if not args.dry_run:
processed.add(sid)
if not args.dry_run:
save_processed(processed)
# Also run auto-research scanner
try:
from auto_research import main as auto_research_main
log.info("Running auto-research scanner...")
auto_research_main()
except Exception as e:
log.warning("Auto-research scanner failed: %s", e)
log.info("Session watcher complete.")
if __name__ == "__main__":
main()