50 lines
2.1 KiB
Python
50 lines
2.1 KiB
Python
"""Continuity checkpoint. No LLM. Session end must not mutate threads."""
|
|
from __future__ import annotations
|
|
|
|
from db import get_db, row_to_dict
|
|
from dialogue_store import StoreError, end_usage_session, list_messages, thread_snapshot
|
|
|
|
|
|
def checkpoint_usage_session(profile_id: str, usage_session_id: str) -> dict:
|
|
"""Ensure originals are stored. Does not insert summaries or change thread status."""
|
|
threads_before = thread_snapshot(profile_id)
|
|
with get_db() as conn:
|
|
session = row_to_dict(
|
|
conn.execute(
|
|
"SELECT * FROM usage_sessions WHERE id = ? AND profile_id = ?",
|
|
(usage_session_id, profile_id),
|
|
).fetchone()
|
|
)
|
|
if not session:
|
|
raise StoreError("not_found", "Nutzungssitzung nicht gefunden", 404)
|
|
conv_rows = conn.execute(
|
|
"SELECT id FROM conversations WHERE profile_id = ? AND usage_session_id = ?",
|
|
(profile_id, usage_session_id),
|
|
).fetchall()
|
|
conversations = []
|
|
message_count = 0
|
|
for row in conv_rows:
|
|
messages = list_messages(profile_id, row["id"])
|
|
message_count += len(messages)
|
|
conversations.append({"id": row["id"], "message_count": len(messages)})
|
|
if thread_snapshot(profile_id) != threads_before:
|
|
raise StoreError("thread_mutated", "Checkpoint darf Thread-Felder nicht ändern", 500)
|
|
return {
|
|
"usage_session_id": usage_session_id,
|
|
"ended_at": session.get("ended_at"),
|
|
"conversations": conversations,
|
|
"message_count": message_count,
|
|
"threads_unchanged": True,
|
|
"llm": False,
|
|
}
|
|
|
|
|
|
def close_usage_session(profile_id: str, usage_session_id: str) -> dict:
|
|
threads_before = thread_snapshot(profile_id)
|
|
ended = end_usage_session(profile_id, usage_session_id)
|
|
checkpoint = checkpoint_usage_session(profile_id, usage_session_id)
|
|
if thread_snapshot(profile_id) != threads_before:
|
|
raise StoreError("thread_mutated", "Session-Ende darf Thread-Status nicht ändern", 500)
|
|
checkpoint["usage_session"] = ended
|
|
return checkpoint
|