-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_memory.py
More file actions
517 lines (417 loc) · 17.8 KB
/
enhanced_memory.py
File metadata and controls
517 lines (417 loc) · 17.8 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import os
import json
import time
import hashlib
from typing import List, Optional, Dict, Any
from dataclasses import dataclass, field, asdict
from enum import Enum
import logging
import threading
import asyncio
class MemoryType(Enum):
WORKFLOW_STEP = "workflow_step"
USER_PREFERENCE = "user_preference"
TASK_RESULT = "task_result"
ERROR_PATTERN = "error_pattern"
SCREENSHOT_CONTEXT = "screenshot_context"
CODE_SNIPPET = "code_snippet"
@dataclass
class MemoryEntry:
content: str
memory_type: MemoryType
timestamp: float
metadata: Dict[str, Any] = field(default_factory=dict)
task_id: Optional[str] = None
step_number: Optional[int] = None
class ChromaVectorStore:
def __init__(self, storage_path: str = "workspace/chroma_db"):
self.storage_path = storage_path
self._chroma_client = None
self._collection = None
self._chroma_available = None
self._fallback_store: Dict[str, MemoryEntry] = {}
self._lock = threading.RLock()
self._init_chroma()
def _init_chroma(self):
if self._chroma_available is not None:
return
try:
import chromadb
from chromadb.config import Settings
os.makedirs(self.storage_path, exist_ok=True)
self._chroma_client = chromadb.PersistentClient(
path=self.storage_path,
settings=Settings(anonymized_telemetry=False)
)
self._collection = self._chroma_client.get_or_create_collection(
name="memory",
metadata={"hnsw:space": "cosine"}
)
self._chroma_available = True
logging.info("ChromaDB initialized successfully")
except ImportError:
logging.warning("ChromaDB not installed, using fallback dict store")
self._chroma_available = False
except Exception as e:
logging.error(f"Failed to initialize ChromaDB: {e}")
self._chroma_available = False
def _generate_id(self, content: str, memory_type: MemoryType) -> str:
unique_str = f"{content}:{memory_type.value}:{time.time()}"
return hashlib.sha256(unique_str.encode()).hexdigest()[:16]
def add(self, entry: MemoryEntry) -> str:
entry_id = self._generate_id(entry.content, entry.memory_type)
metadata = {
"type": entry.memory_type.value,
"timestamp": entry.timestamp,
"task_id": entry.task_id or "",
"step_number": entry.step_number or 0,
}
metadata.update(entry.metadata)
if self._chroma_available and self._collection is not None:
try:
self._collection.add(
documents=[entry.content],
metadatas=[metadata],
ids=[entry_id]
)
return entry_id
except Exception as e:
logging.error(f"ChromaDB add failed: {e}")
self._chroma_available = False
with self._lock:
self._fallback_store[entry_id] = entry
return entry_id
def search(
self,
query: str,
memory_type: Optional[MemoryType] = None,
limit: int = 5,
min_score: float = 0.0
) -> List[Dict[str, Any]]:
results = []
if self._chroma_available and self._collection is not None:
try:
where_filter = {"type": memory_type.value} if memory_type else None
query_results = self._collection.query(
query_texts=[query],
n_results=limit,
where=where_filter
)
if query_results and query_results.get("documents"):
for i, doc in enumerate(query_results["documents"][0]):
distance = query_results.get("distances", [[0]])[0][i]
score = 1.0 - distance
if score >= min_score:
metadata = query_results.get("metadatas", [[{}]])[0][i]
results.append({
"id": query_results["ids"][0][i],
"content": doc,
"score": score,
"type": metadata.get("type", "unknown"),
"timestamp": metadata.get("timestamp", 0),
"task_id": metadata.get("task_id", ""),
"metadata": {k: v for k, v in metadata.items()
if k not in ["type", "timestamp", "task_id", "step_number"]}
})
return results
except Exception as e:
logging.error(f"ChromaDB query failed: {e}")
self._chroma_available = False
return self._fallback_search(query, memory_type, limit)
def _fallback_search(
self,
query: str,
memory_type: Optional[MemoryType] = None,
limit: int = 5
) -> List[Dict[str, Any]]:
query_words = set(query.lower().split())
with self._lock:
entries = list(self._fallback_store.values())
if memory_type:
entries = [e for e in entries if e.memory_type == memory_type]
scored = []
for entry in entries:
entry_words = set(entry.content.lower().split())
overlap = len(query_words & entry_words)
if overlap > 0:
score = overlap / max(len(query_words), len(entry_words))
scored.append((entry, score))
scored.sort(key=lambda x: x[1], reverse=True)
results = []
for entry, score in scored[:limit]:
results.append({
"id": self._generate_id(entry.content, entry.memory_type),
"content": entry.content,
"score": score,
"type": entry.memory_type.value,
"timestamp": entry.timestamp,
"task_id": entry.task_id or "",
"metadata": entry.metadata
})
return results
def get_recent(self, memory_type: Optional[MemoryType] = None, limit: int = 10) -> List[Dict[str, Any]]:
if self._chroma_available and self._collection is not None:
try:
where_filter = {"type": memory_type.value} if memory_type else None
results = self._collection.get(
where=where_filter,
limit=limit
)
if results and results.get("documents"):
entries = []
for i, doc in enumerate(results["documents"]):
metadata = results.get("metadatas", [{}])[i]
entries.append({
"id": results["ids"][i],
"content": doc,
"type": metadata.get("type", "unknown"),
"timestamp": metadata.get("timestamp", 0),
"task_id": metadata.get("task_id", ""),
"metadata": {k: v for k, v in metadata.items()
if k not in ["type", "timestamp", "task_id", "step_number"]}
})
entries.sort(key=lambda x: x["timestamp"], reverse=True)
return entries[:limit]
except Exception as e:
logging.error(f"ChromaDB get_recent failed: {e}")
with self._lock:
entries = list(self._fallback_store.values())
if memory_type:
entries = [e for e in entries if e.memory_type == memory_type]
entries.sort(key=lambda x: x.timestamp, reverse=True)
results = []
for entry in entries[:limit]:
results.append({
"id": self._generate_id(entry.content, entry.memory_type),
"content": entry.content,
"type": entry.memory_type.value,
"timestamp": entry.timestamp,
"task_id": entry.task_id or "",
"metadata": entry.metadata
})
return results
def delete_old_entries(self, max_age_days: int = 30) -> int:
cutoff = time.time() - (max_age_days * 86400)
deleted = 0
if self._chroma_available and self._collection is not None:
try:
all_entries = self._collection.get()
if all_entries and all_entries.get("ids"):
ids_to_delete = []
for i, metadata in enumerate(all_entries.get("metadatas", [])):
if metadata.get("timestamp", 0) < cutoff:
if metadata.get("type") != MemoryType.USER_PREFERENCE.value:
ids_to_delete.append(all_entries["ids"][i])
if ids_to_delete:
self._collection.delete(ids=ids_to_delete)
deleted = len(ids_to_delete)
except Exception as e:
logging.error(f"ChromaDB cleanup failed: {e}")
with self._lock:
old_keys = [
k for k, v in self._fallback_store.items()
if v.timestamp < cutoff and v.memory_type != MemoryType.USER_PREFERENCE
]
for k in old_keys:
del self._fallback_store[k]
deleted += 1
if deleted > 0:
logging.info(f"Deleted {deleted} old memory entries")
return deleted
class WorkflowCheckpoint:
def __init__(self, checkpoint_dir: str = "workspace/checkpoints"):
self.checkpoint_dir = checkpoint_dir
os.makedirs(checkpoint_dir, exist_ok=True)
def save(
self,
task_id: str,
step_number: int,
state: Dict[str, Any],
workflow_steps: List[Dict]
) -> str:
checkpoint = {
"task_id": task_id,
"step_number": step_number,
"timestamp": time.time(),
"state": state,
"workflow_steps": workflow_steps,
"completed_steps": workflow_steps[:step_number] if step_number > 0 else [],
"remaining_steps": workflow_steps[step_number:] if step_number < len(workflow_steps) else []
}
filename = f"{task_id}_step{step_number}.json"
filepath = os.path.join(self.checkpoint_dir, filename)
try:
with open(filepath, "w", encoding="utf-8") as f:
json.dump(checkpoint, f, indent=2, default=str)
latest_link = os.path.join(self.checkpoint_dir, f"{task_id}_latest.json")
with open(latest_link, "w", encoding="utf-8") as f:
json.dump(checkpoint, f, indent=2, default=str)
logging.info(f"Checkpoint saved: {task_id} step {step_number}")
return filepath
except Exception as e:
logging.error(f"Failed to save checkpoint: {e}")
return ""
def load(self, task_id: str) -> Optional[Dict]:
latest_link = os.path.join(self.checkpoint_dir, f"{task_id}_latest.json")
if not os.path.exists(latest_link):
return None
try:
with open(latest_link, "r", encoding="utf-8") as f:
return json.load(f)
except Exception as e:
logging.error(f"Failed to load checkpoint: {e}")
return None
def list_checkpoints(self) -> List[Dict]:
checkpoints = []
if not os.path.exists(self.checkpoint_dir):
return checkpoints
for filename in os.listdir(self.checkpoint_dir):
if filename.endswith("_latest.json"):
filepath = os.path.join(self.checkpoint_dir, filename)
try:
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
checkpoints.append({
"task_id": data.get("task_id"),
"step_number": data.get("step_number"),
"timestamp": data.get("timestamp"),
"filepath": filepath
})
except:
pass
checkpoints.sort(key=lambda x: x.get("timestamp", 0), reverse=True)
return checkpoints
def delete_checkpoint(self, task_id: str) -> bool:
try:
pattern = f"{task_id}_*.json"
for filename in os.listdir(self.checkpoint_dir):
if filename.startswith(task_id):
os.remove(os.path.join(self.checkpoint_dir, filename))
return True
except Exception as e:
logging.error(f"Failed to delete checkpoint: {e}")
return False
class EnhancedWorkflowMemory:
def __init__(self):
self.vector_store = ChromaVectorStore()
self.checkpoint = WorkflowCheckpoint()
self.current_task_id: Optional[str] = None
self.current_step: int = 0
self.workflow_steps: List[Dict] = []
self._checkpoint_interval: int = 5
def start_task(self, task_description: str, steps: List[Dict] = None) -> str:
import uuid
self.current_task_id = str(uuid.uuid4())[:8]
self.current_step = 0
self.workflow_steps = steps or []
entry = MemoryEntry(
content=f"Task started: {task_description}",
memory_type=MemoryType.WORKFLOW_STEP,
timestamp=time.time(),
task_id=self.current_task_id,
step_number=0
)
self.vector_store.add(entry)
self._save_checkpoint()
return self.current_task_id
def add_step(self, step_description: str, result: str = "") -> int:
if not self.current_task_id:
self.start_task("Unknown")
self.current_step += 1
content = f"Step {self.current_step}: {step_description}"
if result:
content += f" | Result: {result}"
entry = MemoryEntry(
content=content,
memory_type=MemoryType.WORKFLOW_STEP,
timestamp=time.time(),
task_id=self.current_task_id,
step_number=self.current_step,
metadata={"result": result} if result else {}
)
self.vector_store.add(entry)
if self.current_step % self._checkpoint_interval == 0:
self._save_checkpoint()
return self.current_step
def end_task(self, result: str = "completed"):
if not self.current_task_id:
return
entry = MemoryEntry(
content=f"Task ended: {result}",
memory_type=MemoryType.WORKFLOW_STEP,
timestamp=time.time(),
task_id=self.current_task_id,
step_number=self.current_step + 1
)
self.vector_store.add(entry)
self._save_checkpoint(final=True)
self.current_task_id = None
self.current_step = 0
self.workflow_steps = []
def _save_checkpoint(self, final: bool = False):
if not self.current_task_id:
return
if not final and self.current_step % self._checkpoint_interval != 0:
return
state = {
"current_step": self.current_step,
"completed": final
}
self.checkpoint.save(
task_id=self.current_task_id,
step_number=self.current_step,
state=state,
workflow_steps=self.workflow_steps
)
def resume_task(self, task_id: str) -> Optional[Dict]:
checkpoint = self.checkpoint.load(task_id)
if not checkpoint:
return None
self.current_task_id = checkpoint["task_id"]
self.current_step = checkpoint["step_number"]
self.workflow_steps = checkpoint.get("workflow_steps", [])
return {
"task_id": self.current_task_id,
"step_number": self.current_step,
"workflow_steps": self.workflow_steps,
"remaining_steps": checkpoint.get("remaining_steps", [])
}
def get_context(self, query: str, limit: int = 5) -> str:
results = self.vector_store.search(query, limit=limit)
if not results:
return ""
context = "Relevant past context:\n"
for r in results:
age = (time.time() - r["timestamp"]) / 3600
context += f"- [{age:.1f}h ago, score={r['score']:.2f}] {r['content']}\n"
return context
def get_workflow_history(self, limit: int = 50) -> List[str]:
entries = self.vector_store.get_recent(MemoryType.WORKFLOW_STEP, limit=limit)
return [e["content"] for e in entries]
def add_error_pattern(self, error: str, context: str, suggested_fix: str = ""):
entry = MemoryEntry(
content=f"Error: {error} | Context: {context} | Fix: {suggested_fix}",
memory_type=MemoryType.ERROR_PATTERN,
timestamp=time.time(),
metadata={"error": error, "context": context}
)
self.vector_store.add(entry)
def find_similar_error(self, error: str) -> Optional[Dict]:
results = self.vector_store.search(error, MemoryType.ERROR_PATTERN, limit=1)
if results and results[0]["score"] > 0.7:
return results[0]
return None
def add_code_snippet(self, code: str, description: str, language: str = ""):
entry = MemoryEntry(
content=f"{description}\n```{language}\n{code}\n```",
memory_type=MemoryType.CODE_SNIPPET,
timestamp=time.time(),
metadata={"language": language}
)
self.vector_store.add(entry)
_memory_instance: Optional[EnhancedWorkflowMemory] = None
def get_enhanced_memory() -> EnhancedWorkflowMemory:
global _memory_instance
if _memory_instance is None:
_memory_instance = EnhancedWorkflowMemory()
return _memory_instance