From 71fc7456d0e7fc3c2284a562054884fea943e5f3 Mon Sep 17 00:00:00 2001 From: anor Date: Fri, 27 Mar 2026 03:39:54 +1000 Subject: [PATCH] =?UTF-8?q?feat:=20Sonnet=20bridge=20=E2=80=94=20=D0=B8?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D1=80=D0=B8=D1=8F=20=D0=B8=D0=B7=20SQLite=20?= =?UTF-8?q?=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20RAM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Загружает последние 2 обмена из БД перед каждым запросом. Контекст переживает рестарт сервиса. Co-Authored-By: Claude Opus 4.6 (1M context) --- claude_bridge_sonnet.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/claude_bridge_sonnet.py b/claude_bridge_sonnet.py index c7c45a0..f4bddd4 100644 --- a/claude_bridge_sonnet.py +++ b/claude_bridge_sonnet.py @@ -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})