An always-on second brain you talk to. Voice notes in Telegram → typed, linked knowledge in your Obsidian vault. Runs 24/7 on the Claude subscription you already have.
Python ai-agentsai-automationclaudeclaude-codeebbinghausknowledge-graphllmmemory-systemobsidianpersonal-assistantpkmproductivitypythonsecond-braintelegram-botvoice-assistant
feat: port ChatSessionManager from production
smixs68fe33c6d98c
diff --git a/src/d_brain/services/chat_session.py b/src/d_brain/services/chat_session.pynew file mode 100644index 0000000..ed10397--- /dev/null+++ b/src/d_brain/services/chat_session.py@@ -0,0 +1,64 @@+"""Persistent chat session manager — backed by ONE interactive Claude session.++Migrated from per-user `claude -p --resume` (which moves to the paid Agent SDK+credit on 2026-06-15) to a single long-lived interactive tmux session shared+across the bot. Conversational continuity lives in the live session itself+plus the vault (durable-state-first), so per-user --resume bookkeeping is+gone. The public interface (send_message / reset / compact) is unchanged so+the chat handlers don't need to change.+"""++import asyncio+import logging+from pathlib import Path+from typing import Any++from d_brain.config import get_settings+from d_brain.services.runtime import get_ask_lock, get_session++logger = logging.getLogger(__name__)++_STATUS_MESSAGES = {+ "rate_limited": "⏳ Лимит подписки исчерпан. Вернусь, когда он обновится.",+ "logged_out": "🔑 Нужен повторный вход. Админу: dbrain login.",+ "timeout": "⌛ Превышено время ожидания ответа. Попробуй ещё раз.",+ "error": "❌ Ошибка сессии. Попробуй позже.",+}+++class ChatSessionManager:+ """Routes chat messages to the shared interactive session."""++ def __init__(+ self,+ vault_path: Path | str,+ todoist_api_key: str = "",+ session: Any | None = None,+ ) -> None:+ self.vault_path = Path(vault_path)+ self.todoist_api_key = todoist_api_key+ self._session = session if session is not None else get_session(get_settings())++ async def send_message(self, user_id: int, prompt: str) -> str:+ """Send a message to the session and return the reply text.++ Serialized via the process-wide ask-lock; runs the blocking ask() in a+ worker thread so the event loop stays responsive.+ """+ async with get_ask_lock():+ res = await asyncio.to_thread(self._session.ask, prompt)+ if res.ok:+ return res.reply or ""+ logger.warning("session ask for user %d returned %s", user_id, res.status)+ return _STATUS_MESSAGES.get(res.status, _STATUS_MESSAGES["error"])++ def reset(self, user_id: int) -> None:+ """Clear the live session context (durable data in files is kept)."""+ self._session.clear()+ logger.info("session cleared (reset) requested by user %d", user_id)++ async def compact(self, user_id: int) -> str:+ """Durable-state-first: clearing is the compaction; memory lives in+ files, so there is nothing to summarize into the session."""+ self._session.clear()+ return "🧹 Сессия очищена (важные данные сохранены в файлах)."diff --git a/tests/test_chat_session.py b/tests/test_chat_session.pynew file mode 100644index 0000000..d7b5fb7--- /dev/null+++ b/tests/test_chat_session.py@@ -0,0 +1,44 @@+"""Tests for ChatSessionManager — chat messages routed to the shared session."""++import asyncio++from d_brain.services.claude_session import AskResult+++class FakeSession:+ def __init__(self, result: AskResult) -> None:+ self.result = result+ self.prompts: list[str] = []+ self.cleared = 0++ def ask(self, prompt: str, **kwargs) -> AskResult:+ self.prompts.append(prompt)+ return self.result++ def clear(self) -> None:+ self.cleared += 1+++def _manager(tmp_path, result: AskResult):+ from d_brain.services.chat_session import ChatSessionManager++ return ChatSessionManager(tmp_path, session=FakeSession(result))+++def test_send_message_returns_reply_on_ok(tmp_path):+ m = _manager(tmp_path, AskResult("ok", reply="привет"))+ reply = asyncio.run(m.send_message(1, "здравствуй"))+ assert reply == "привет"+ assert m._session.prompts == ["здравствуй"]+++def test_send_message_maps_rate_limited(tmp_path):+ m = _manager(tmp_path, AskResult("rate_limited"))+ reply = asyncio.run(m.send_message(1, "x"))+ assert "Лимит" in reply+++def test_reset_clears_live_session(tmp_path):+ m = _manager(tmp_path, AskResult("ok", reply=""))+ m.reset(1)+ assert m._session.cleared == 1