-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
45 lines (33 loc) · 1.36 KB
/
Copy pathmemory.py
File metadata and controls
45 lines (33 loc) · 1.36 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
"""Local conversation storage for the dashboard. Investor OS markdown remains canonical."""
from __future__ import annotations
from datetime import datetime, timezone
from pathlib import Path
import sqlite3
DATABASE = Path(__file__).resolve().parent / "data" / "memory.db"
def _connection() -> sqlite3.Connection:
DATABASE.parent.mkdir(parents=True, exist_ok=True)
connection = sqlite3.connect(DATABASE)
connection.execute(
"""CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL
)"""
)
return connection
def add_message(role: str, content: str) -> None:
with _connection() as connection:
connection.execute(
"INSERT INTO messages (created_at, role, content) VALUES (?, ?, ?)",
(datetime.now(timezone.utc).isoformat(), role, content),
)
def recent_messages(limit: int = 12) -> list[dict[str, str]]:
with _connection() as connection:
rows = connection.execute(
"SELECT role, content FROM messages ORDER BY id DESC LIMIT ?", (limit,)
).fetchall()
return [{"role": role, "content": content} for role, content in reversed(rows)]
def clear_messages() -> None:
with _connection() as connection:
connection.execute("DELETE FROM messages")