-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
57 lines (45 loc) · 1.51 KB
/
Copy pathmemory.py
File metadata and controls
57 lines (45 loc) · 1.51 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
"""
Lightweight SQLite-backed conversation memory, keyed by session_id.
Survives process restarts, unlike Streamlit's in-memory session state,
and works fine for a single-instance production deployment.
"""
import sqlite3
from contextlib import contextmanager
import config
_SCHEMA = """
CREATE TABLE IF NOT EXISTS messages (
session_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
"""
@contextmanager
def _conn():
conn = sqlite3.connect(config.SESSION_DB_PATH)
try:
yield conn
conn.commit()
finally:
conn.close()
def init_db() -> None:
with _conn() as conn:
conn.execute(_SCHEMA)
def add_message(session_id: str, role: str, content: str) -> None:
with _conn() as conn:
conn.execute(
"INSERT INTO messages (session_id, role, content) VALUES (?, ?, ?)",
(session_id, role, content),
)
def get_history(session_id: str, max_turns: int = None) -> list[dict]:
max_turns = max_turns or config.MAX_HISTORY_TURNS
with _conn() as conn:
rows = conn.execute(
"SELECT role, content FROM messages WHERE session_id = ? "
"ORDER BY created_at DESC LIMIT ?",
(session_id, max_turns * 2),
).fetchall()
return [{"role": r, "content": c} for r, c in reversed(rows)]
def clear_session(session_id: str) -> None:
with _conn() as conn:
conn.execute("DELETE FROM messages WHERE session_id = ?", (session_id,))