feat: Sonnet bridge — история из SQLite вместо RAM

Загружает последние 2 обмена из БД перед каждым запросом.
Контекст переживает рестарт сервиса.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-27 03:39:54 +10:00
co-authored by Claude Opus 4.6
parent 78c37414bc
commit 71fc7456d0
+8 -12
View File
@@ -123,23 +123,21 @@ async def stream_claude(
"""Stream Claude Sonnet response via Docker CLI with CAG+RAG context."""
async with _semaphore:
key = (username, tab_id)
if key not in _conversations:
_conversations[key] = []
history = _conversations[key]
# Load history from SQLite (survives restarts)
from db import get_history
db_history = get_history(username, tab_id, limit=6)
# Build system prompt (CAG) and enriched user message (RAG)
system_prompt = _build_system_prompt()
user_content = _build_context(message, username, tab_id)
history.append({"role": "user", "content": user_content})
if len(history) > MAX_HISTORY:
history[:] = history[-MAX_HISTORY:]
# Build full prompt for Claude CLI
# Build full prompt from DB history + current message
prompt_parts = []
for msg in history[-6:]: # last 3 exchanges
for msg in db_history[-4:]: # last 2 exchanges from DB
role = "Пользователь" if msg["role"] == "user" else "Ассистент"
prompt_parts.append(f"[{role}]: {msg['content']}")
prompt_parts.append(f"[{role}]: {msg['content'][:500]}")
prompt_parts.append(f"[Пользователь]: {user_content}")
full_prompt = "\n\n".join(prompt_parts)
@@ -243,5 +241,3 @@ async def stream_claude(
if verification:
full_response += verification
yield f"event: text\ndata: {json.dumps({'text': verification}, ensure_ascii=False)}\n\n"
history.append({"role": "assistant", "content": full_response})