forked from ethanplusai/jarvis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
450 lines (380 loc) · 15.5 KB
/
memory.py
File metadata and controls
450 lines (380 loc) · 15.5 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
"""
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 remember(content: str, mem_type: str = "fact", source: str = "", importance: int = 5) -> int:
"""Store a memory. Returns the memory ID."""
conn = _get_db()
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. Returns most relevant matches."""
fts_query = _sanitize_fts_query(query)
if not fts_query:
return []
conn = _get_db()
try:
results = conn.execute("""
SELECT m.id, m.type, m.content, m.importance, m.created_at, m.access_count
FROM memory_fts f
JOIN memories m ON f.rowid = m.id
WHERE memory_fts MATCH ?
ORDER BY rank
LIMIT ?
""", (fts_query, limit)).fetchall()
except Exception:
results = []
# Update access counts
for r in results:
conn.execute(
"UPDATE memories SET last_accessed = ?, access_count = access_count + 1 WHERE id = ?",
(time.time(), r["id"])
)
conn.commit()
conn.close()
return [dict(r) for r in results]
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]
# ---------------------------------------------------------------------------
# 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 = []
# 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) if parts else ""
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()