forked from ethanplusai/jarvis
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory.py
More file actions
595 lines (506 loc) · 20.9 KB
/
Copy pathmemory.py
File metadata and controls
595 lines (506 loc) · 20.9 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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
"""
JARVIS Memory & Planning — persistent context, tasks, notes, and smart routing.
Three systems:
1. Memory — facts, preferences, project context JARVIS learns from conversations
2. Tasks — to-do items with priority, due dates, project association
3. Notes — freeform context tied to projects, people, or topics
Everything stored in SQLite. Relevant memories injected into every LLM call
so JARVIS gets smarter over time.
"""
import json
import logging
import sqlite3
import time
from datetime import datetime, timedelta
from pathlib import Path
log = logging.getLogger("jarvis.memory")
DB_PATH = Path(__file__).parent / "data" / "jarvis.db"
def _get_db() -> sqlite3.Connection:
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(str(DB_PATH))
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA journal_mode=WAL")
return conn
def init_db():
"""Create tables if they don't exist."""
conn = _get_db()
conn.executescript("""
CREATE TABLE IF NOT EXISTS memories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT NOT NULL, -- 'fact', 'preference', 'project', 'person', 'decision'
content TEXT NOT NULL,
source TEXT DEFAULT '', -- what conversation/context it came from
importance INTEGER DEFAULT 5, -- 1-10, higher = more important
created_at REAL NOT NULL,
last_accessed REAL,
access_count INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT DEFAULT '',
priority TEXT DEFAULT 'medium', -- 'high', 'medium', 'low'
status TEXT DEFAULT 'open', -- 'open', 'in_progress', 'done', 'cancelled'
due_date TEXT, -- ISO date string
due_time TEXT, -- HH:MM
project TEXT DEFAULT '',
tags TEXT DEFAULT '[]', -- JSON array
notes TEXT DEFAULT '',
created_at REAL NOT NULL,
completed_at REAL
);
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT DEFAULT '',
content TEXT NOT NULL,
topic TEXT DEFAULT '', -- project name, person, or topic
tags TEXT DEFAULT '[]', -- JSON array
created_at REAL NOT NULL,
updated_at REAL
);
CREATE VIRTUAL TABLE IF NOT EXISTS memory_fts USING fts5(
content, type, source,
content='memories', content_rowid='id'
);
CREATE VIRTUAL TABLE IF NOT EXISTS task_fts USING fts5(
title, description, project, notes,
content='tasks', content_rowid='id'
);
CREATE VIRTUAL TABLE IF NOT EXISTS note_fts USING fts5(
title, content, topic,
content='notes', content_rowid='id'
);
""")
conn.close()
log.info("Memory database initialized")
# ---------------------------------------------------------------------------
# Memories — facts JARVIS learns
# ---------------------------------------------------------------------------
def _normalize(text: str) -> set[str]:
"""Lowercased word set for cheap similarity checks."""
return {w for w in "".join(c if c.isalnum() else " " for c in text.lower()).split() if len(w) > 2}
def _find_duplicate(conn: sqlite3.Connection, content: str, mem_type: str) -> dict | None:
"""Find an existing near-duplicate memory of the same type (Jaccard >= 0.7)."""
target = _normalize(content)
if not target:
return None
rows = conn.execute(
"SELECT id, content, importance FROM memories WHERE type = ? ORDER BY created_at DESC LIMIT 200",
(mem_type,),
).fetchall()
for r in rows:
other = _normalize(r["content"])
if not other:
continue
overlap = len(target & other) / len(target | other)
if overlap >= 0.7:
return dict(r)
return None
def remember(content: str, mem_type: str = "fact", source: str = "", importance: int = 5) -> int:
"""Store a memory, de-duplicating near-identical facts. Returns the memory ID."""
content = (content or "").strip()
if not content:
return -1
conn = _get_db()
# De-dup: if a near-identical memory of the same type exists, refresh it instead.
dup = _find_duplicate(conn, content, mem_type)
if dup:
new_importance = max(int(dup["importance"] or 5), int(importance))
conn.execute(
"UPDATE memories SET importance = ?, last_accessed = ?, access_count = access_count + 1 WHERE id = ?",
(new_importance, time.time(), dup["id"]),
)
conn.commit()
conn.close()
log.info(f"Memory deduped [{mem_type}]: {content[:50]} -> #{dup['id']}")
return int(dup["id"])
cur = conn.execute(
"INSERT INTO memories (type, content, source, importance, created_at) VALUES (?, ?, ?, ?, ?)",
(mem_type, content, source, importance, time.time())
)
mem_id = cur.lastrowid
# Update FTS
conn.execute(
"INSERT INTO memory_fts (rowid, content, type, source) VALUES (?, ?, ?, ?)",
(mem_id, content, mem_type, source)
)
conn.commit()
conn.close()
log.info(f"Stored memory [{mem_type}]: {content[:60]}")
return mem_id
def _sanitize_fts_query(query: str) -> str:
"""Clean a query string for FTS5 — remove special characters that break it."""
# Remove apostrophes, quotes, and FTS operators
cleaned = query.replace("'", "").replace('"', "").replace("*", "").replace("-", " ")
# Take meaningful words only
words = [w for w in cleaned.split() if len(w) > 2]
if not words:
return ""
# Join with OR for broader matching
return " OR ".join(words[:5])
def recall(query: str, limit: int = 5) -> list[dict]:
"""Search memories by relevance.
Combines FTS text rank with importance, recency, and how often a memory has
been used — so the most useful matches surface, not just the closest text.
"""
fts_query = _sanitize_fts_query(query)
if not fts_query:
return []
conn = _get_db()
try:
# Pull a wider candidate pool, then re-rank with multiple signals.
candidates = conn.execute("""
SELECT m.id, m.type, m.content, m.importance, m.created_at,
m.access_count, m.last_accessed, f.rank AS fts_rank
FROM memory_fts f
JOIN memories m ON f.rowid = m.id
WHERE memory_fts MATCH ?
ORDER BY rank
LIMIT ?
""", (fts_query, limit * 4)).fetchall()
except Exception:
candidates = []
now = time.time()
scored = []
for r in candidates:
# FTS rank is negative (more negative = better); invert to a positive base.
text_score = -float(r["fts_rank"] or 0)
importance = (int(r["importance"] or 5)) / 10.0 # 0..1
age_days = max(0.0, (now - float(r["created_at"])) / 86400.0)
recency = 1.0 / (1.0 + age_days / 30.0) # ~half-life of a month
usage = min(1.0, (int(r["access_count"] or 0)) / 10.0)
score = text_score + 1.5 * importance + 0.8 * recency + 0.4 * usage
scored.append((score, dict(r)))
scored.sort(key=lambda x: x[0], reverse=True)
results = [d for _, d in scored[:limit]]
# Update access counts for what we surfaced.
for r in results:
conn.execute(
"UPDATE memories SET last_accessed = ?, access_count = access_count + 1 WHERE id = ?",
(now, r["id"])
)
conn.commit()
conn.close()
return results
def decay_importance(half_life_days: float = 90.0, floor: int = 1) -> int:
"""Gently age out rarely-used memories so stale facts stop dominating.
Reduces importance of memories not accessed within the half-life window,
unless they are frequently used. Returns the number adjusted.
"""
conn = _get_db()
cutoff = time.time() - half_life_days * 86400.0
rows = conn.execute(
"SELECT id, importance, access_count FROM memories "
"WHERE importance > ? AND (last_accessed IS NULL OR last_accessed < ?) AND access_count < 3",
(floor, cutoff),
).fetchall()
for r in rows:
conn.execute("UPDATE memories SET importance = ? WHERE id = ?",
(max(floor, int(r["importance"]) - 1), r["id"]))
conn.commit()
conn.close()
if rows:
log.info(f"Decayed importance of {len(rows)} stale memories")
return len(rows)
def get_recent_memories(limit: int = 10) -> list[dict]:
"""Get most recent memories."""
conn = _get_db()
results = conn.execute(
"SELECT * FROM memories ORDER BY created_at DESC LIMIT ?", (limit,)
).fetchall()
conn.close()
return [dict(r) for r in results]
def get_important_memories(limit: int = 10) -> list[dict]:
"""Get highest importance memories."""
conn = _get_db()
results = conn.execute(
"SELECT * FROM memories ORDER BY importance DESC, access_count DESC LIMIT ?", (limit,)
).fetchall()
conn.close()
return [dict(r) for r in results]
def delete_memory(mem_id: int) -> bool:
"""Delete a memory by ID. Returns True if deleted."""
conn = _get_db()
cur = conn.execute("DELETE FROM memories WHERE id = ?", (mem_id,))
# Also remove from FTS
try:
conn.execute("DELETE FROM memory_fts WHERE rowid = ?", (mem_id,))
except Exception:
pass
conn.commit()
deleted = cur.rowcount > 0
conn.close()
if deleted:
log.info(f"Deleted memory #{mem_id}")
return deleted
def get_all_memories(limit: int = 100, offset: int = 0, mem_type: str | None = None) -> list[dict]:
"""Get memories with pagination and optional type filter."""
conn = _get_db()
q = "SELECT * FROM memories"
params: list = []
if mem_type:
q += " WHERE type = ?"
params.append(mem_type)
q += " ORDER BY created_at DESC LIMIT ? OFFSET ?"
params.extend([limit, offset])
results = conn.execute(q, params).fetchall()
conn.close()
return [dict(r) for r in results]
def memory_stats() -> dict:
"""Get memory statistics."""
conn = _get_db()
total = conn.execute("SELECT COUNT(*) as c FROM memories").fetchone()["c"]
by_type = conn.execute(
"SELECT type, COUNT(*) as c FROM memories GROUP BY type ORDER BY c DESC"
).fetchall()
recent = conn.execute(
"SELECT COUNT(*) as c FROM memories WHERE created_at > ?",
(time.time() - 86400,)
).fetchone()["c"]
task_count = conn.execute(
"SELECT COUNT(*) as c FROM tasks WHERE status IN ('open', 'in_progress')"
).fetchone()["c"]
note_count = conn.execute("SELECT COUNT(*) as c FROM notes").fetchone()["c"]
conn.close()
return {
"total_memories": total,
"by_type": {r["type"]: r["c"] for r in by_type},
"last_24h": recent,
"open_tasks": task_count,
"total_notes": note_count,
}
# ---------------------------------------------------------------------------
# Tasks
# ---------------------------------------------------------------------------
def create_task(title: str, description: str = "", priority: str = "medium",
due_date: str = "", due_time: str = "", project: str = "",
tags: list[str] = None) -> int:
"""Create a task. Returns task ID."""
conn = _get_db()
cur = conn.execute(
"""INSERT INTO tasks (title, description, priority, due_date, due_time,
project, tags, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(title, description, priority, due_date, due_time,
project, json.dumps(tags or []), time.time())
)
task_id = cur.lastrowid
conn.execute(
"INSERT INTO task_fts (rowid, title, description, project, notes) VALUES (?, ?, ?, ?, ?)",
(task_id, title, description, project, "")
)
conn.commit()
conn.close()
log.info(f"Created task [{priority}]: {title}")
return task_id
def get_open_tasks(project: str = None) -> list[dict]:
"""Get all open/in-progress tasks, optionally filtered by project."""
conn = _get_db()
if project:
results = conn.execute(
"SELECT * FROM tasks WHERE status IN ('open','in_progress') AND project LIKE ? ORDER BY "
"CASE priority WHEN 'high' THEN 1 WHEN 'medium' THEN 2 ELSE 3 END, due_date",
(f"%{project}%",)
).fetchall()
else:
results = conn.execute(
"SELECT * FROM tasks WHERE status IN ('open','in_progress') ORDER BY "
"CASE priority WHEN 'high' THEN 1 WHEN 'medium' THEN 2 ELSE 3 END, due_date"
).fetchall()
conn.close()
return [dict(r) for r in results]
def get_tasks_for_date(date_str: str) -> list[dict]:
"""Get tasks due on a specific date (YYYY-MM-DD)."""
conn = _get_db()
results = conn.execute(
"SELECT * FROM tasks WHERE due_date = ? AND status != 'cancelled' ORDER BY "
"CASE priority WHEN 'high' THEN 1 WHEN 'medium' THEN 2 ELSE 3 END, due_time",
(date_str,)
).fetchall()
conn.close()
return [dict(r) for r in results]
def complete_task(task_id: int):
"""Mark a task as done."""
conn = _get_db()
conn.execute(
"UPDATE tasks SET status = 'done', completed_at = ? WHERE id = ?",
(time.time(), task_id)
)
conn.commit()
conn.close()
def search_tasks(query: str, limit: int = 10) -> list[dict]:
"""Search tasks by text."""
fts_query = _sanitize_fts_query(query)
if not fts_query:
return []
conn = _get_db()
try:
results = conn.execute("""
SELECT t.* FROM task_fts f
JOIN tasks t ON f.rowid = t.id
WHERE task_fts MATCH ?
ORDER BY rank LIMIT ?
""", (fts_query, limit)).fetchall()
except Exception:
results = []
conn.close()
return [dict(r) for r in results]
# ---------------------------------------------------------------------------
# Notes
# ---------------------------------------------------------------------------
def create_note(content: str, title: str = "", topic: str = "", tags: list[str] = None) -> int:
"""Create a note. Returns note ID."""
conn = _get_db()
now = time.time()
cur = conn.execute(
"INSERT INTO notes (title, content, topic, tags, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
(title, content, topic, json.dumps(tags or []), now, now)
)
note_id = cur.lastrowid
conn.execute(
"INSERT INTO note_fts (rowid, title, content, topic) VALUES (?, ?, ?, ?)",
(note_id, title, content, topic)
)
conn.commit()
conn.close()
log.info(f"Created note: {title or content[:40]}")
return note_id
def search_notes(query: str, limit: int = 10) -> list[dict]:
"""Search notes by text."""
fts_query = _sanitize_fts_query(query)
if not fts_query:
return []
conn = _get_db()
try:
results = conn.execute("""
SELECT n.* FROM note_fts f
JOIN notes n ON f.rowid = n.id
WHERE note_fts MATCH ?
ORDER BY rank LIMIT ?
""", (fts_query, limit)).fetchall()
except Exception:
results = []
conn.close()
return [dict(r) for r in results]
def get_notes_by_topic(topic: str) -> list[dict]:
"""Get all notes for a topic/project."""
conn = _get_db()
results = conn.execute(
"SELECT * FROM notes WHERE topic LIKE ? ORDER BY updated_at DESC",
(f"%{topic}%",)
).fetchall()
conn.close()
return [dict(r) for r in results]
# ---------------------------------------------------------------------------
# Context Builder — smart context for LLM calls
# ---------------------------------------------------------------------------
def build_memory_context(user_message: str) -> str:
"""Build relevant context from memories, tasks, and notes for the LLM.
Searches for relevant memories based on what the user is talking about.
Fast — runs FTS queries, no heavy computation.
"""
parts = [
"MEMORY PROTOCOL: use stored memories as helpful context, never as an excuse to reveal private data unnecessarily; update memory only through explicit action tags when the user shares durable facts, preferences, decisions, or plans."
]
# Always include: open high-priority tasks
high_tasks = [t for t in get_open_tasks() if t["priority"] == "high"]
if high_tasks:
task_lines = [f" - [{t['priority']}] {t['title']}" +
(f" (due {t['due_date']})" if t["due_date"] else "")
for t in high_tasks[:5]]
parts.append("HIGH PRIORITY TASKS:\n" + "\n".join(task_lines))
# Search memories relevant to what user is saying
if len(user_message) > 5:
relevant = recall(user_message, limit=3)
if relevant:
mem_lines = [f" - [{m['type']}] {m['content']}" for m in relevant]
parts.append("RELEVANT MEMORIES:\n" + "\n".join(mem_lines))
# Recent important memories (always available)
important = get_important_memories(limit=3)
if important:
imp_lines = [f" - {m['content']}" for m in important
if not any(m["content"] == r["content"] for r in (relevant if 'relevant' in dir() else []))]
if imp_lines:
parts.append("KEY FACTS:\n" + "\n".join(imp_lines[:3]))
return "\n\n".join(parts)
def format_tasks_for_voice(tasks: list[dict]) -> str:
"""Format tasks for voice response."""
if not tasks:
return "No tasks on the list, sir."
count = len(tasks)
high = [t for t in tasks if t["priority"] == "high"]
if count == 1:
t = tasks[0]
return f"One task: {t['title']}." + (f" Due {t['due_date']}." if t["due_date"] else "")
result = f"You have {count} open tasks."
if high:
result += f" {len(high)} are high priority."
top = tasks[:3]
for t in top:
result += f" {t['title']}."
if count > 3:
result += f" And {count - 3} more."
return result
def format_plan_for_voice(tasks: list[dict], events: list[dict]) -> str:
"""Format a day plan combining tasks and calendar events."""
if not tasks and not events:
return "Your day looks clear, sir. No events or tasks scheduled."
parts = []
if events:
parts.append(f"{len(events)} events on the calendar")
if tasks:
high = [t for t in tasks if t["priority"] == "high"]
parts.append(f"{len(tasks)} tasks" + (f", {len(high)} high priority" if high else ""))
result = f"For tomorrow: {', '.join(parts)}. "
# List events first
if events:
for e in events[:3]:
result += f"{e.get('start', '')} {e['title']}. "
# Then high priority tasks
if tasks:
for t in [t for t in tasks if t["priority"] == "high"][:2]:
result += f"Priority: {t['title']}. "
result += "Shall I adjust anything?"
return result
# ---------------------------------------------------------------------------
# Memory extraction — learn from conversations
# ---------------------------------------------------------------------------
async def extract_memories(user_text: str, jarvis_response: str, anthropic_client) -> list[str]:
"""After a conversation turn, extract any facts worth remembering.
Uses Haiku to decide if anything in the exchange is worth storing.
Returns list of memories stored.
"""
if not anthropic_client or len(user_text) < 15:
return []
try:
response = await anthropic_client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=200,
system=(
"Extract facts worth remembering from this conversation. "
"Only extract CONCRETE facts: preferences, decisions, names, dates, plans, goals. "
"NOT opinions, greetings, or casual chat. "
"Return JSON array of objects: [{\"type\": \"fact|preference|project|person|decision\", \"content\": \"...\", \"importance\": 1-10}] "
"Return [] if nothing worth remembering. Be very selective."
),
messages=[{"role": "user", "content": f"User: {user_text}\nJARVIS: {jarvis_response}"}],
)
text = response.content[0].text.strip()
# Parse JSON
if text.startswith("["):
items = json.loads(text)
stored = []
for item in items:
if isinstance(item, dict) and "content" in item:
remember(
content=item["content"],
mem_type=item.get("type", "fact"),
source=user_text[:50],
importance=item.get("importance", 5),
)
stored.append(item["content"])
return stored
except Exception as e:
log.debug(f"Memory extraction failed: {e}")
return []
# Initialize on import
init_db()