feat: Konversationsgedaechtnis - Kurzzeit-Gespraechsverlauf pro Session

- Store: messages-Tabelle + append_message/get_recent_messages (mit Mandanten-Schutz)
- LLMProvider.complete um history erweitert (openrouter + local bauen system+history+user)
- Orchestrator.chat_text reicht den Verlauf ans LLM weiter
- chat.py: bei session_id letzte HISTORY_MAX_MESSAGES laden, danach User-/Assistant-Turn speichern
- ohne session_id weiterhin zustandslos; ?debug zeigt history_len
- Config HISTORY_MAX_MESSAGES (Default 10)
- Tests: 32 gruen (3 neue Gedaechtnis-Tests)
- Doku aktualisiert (README, BEDIENUNGSANLEITUNG, Architektur)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Dieter Schlüter 2026-06-17 04:16:35 +02:00
commit 16a964032e
12 changed files with 225 additions and 40 deletions

View file

@ -70,6 +70,14 @@ class Store(ABC):
def update_session(self, session_id: str, user_id: str, values: dict) -> Session:
"""Erstellt/aktualisiert eine Session des Nutzers. Fremde Session -> SessionOwnershipError."""
@abstractmethod
def append_message(self, session_id: str, user_id: str, role: str, content: str) -> None:
"""Haengt eine Nachricht an die Session an. Fremde Session -> SessionOwnershipError."""
@abstractmethod
def get_recent_messages(self, session_id: str, limit: int) -> list[dict]:
"""Liefert die letzten `limit` Nachrichten chronologisch ([{'role','content'}, ...])."""
class SQLiteStore(Store):
def __init__(self, db_path: str):
@ -102,6 +110,15 @@ class SQLiteStore(Store):
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_messages_session
ON messages(session_id, id);
"""
)
@ -195,3 +212,35 @@ class SQLiteStore(Store):
(session_id, user_id, payload, now, now),
)
return Session(id=session_id, user_id=user_id, data=data)
# ----- Nachrichten / Gespraechsverlauf ----------------------------------
def append_message(self, session_id: str, user_id: str, role: str, content: str) -> None:
existing = self.get_session(session_id)
if existing and existing.user_id != user_id:
raise SessionOwnershipError(
f"Session {session_id!r} gehoert einem anderen Nutzer"
)
now = _now()
with self._connect() as conn:
if not existing:
conn.execute(
"INSERT INTO sessions (id, user_id, data_json, created_at, updated_at)"
" VALUES (?, ?, ?, ?, ?)",
(session_id, user_id, "{}", now, now),
)
conn.execute(
"INSERT INTO messages (session_id, role, content, created_at)"
" VALUES (?, ?, ?, ?)",
(session_id, role, content, now),
)
def get_recent_messages(self, session_id: str, limit: int) -> list[dict]:
if limit <= 0:
return []
with self._connect() as conn:
rows = conn.execute(
"SELECT role, content FROM messages WHERE session_id = ?"
" ORDER BY id DESC LIMIT ?",
(session_id, limit),
).fetchall()
return [{"role": row["role"], "content": row["content"]} for row in reversed(rows)]