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(chat): three modes for concurrent input — steer / interrupt / ask
smixs59cec2332b47
diff --git a/src/d_brain/bot/handlers/chat.py b/src/d_brain/bot/handlers/chat.pyindex 2f08a87..11dfdea 100644--- a/src/d_brain/bot/handlers/chat.py+++ b/src/d_brain/bot/handlers/chat.py@@ -55,6 +55,20 @@ def classify_command(text: str) -> str:return "turn"+_STOP_WORDS = {"/stop", "stop", "стоп"}+++def classify_concurrent_input(text: str, turn_active: bool) -> str:+ """'ask' | 'steer' | 'interrupt' — what to do with input that arrives+ while the agent may be busy. Plain text during an active turn STEERS it+ (injected mid-turn); a stop word interrupts; otherwise a normal turn."""+ if not turn_active:+ return "ask"+ if text.strip().lower() in _STOP_WORDS:+ return "interrupt"+ return "steer"++def _get_manager() -> ChatSessionManager:"""Lazy-init ChatSessionManager singleton."""global _manager # noqa: PLW0603@@ -79,6 +93,17 @@ async def _dispatch_text(bot: Bot, chat_id: int, user_id: int, text: str) -> Non"через <code>dbrain attach</code> на сервере.",)return++ manager = _get_manager()+ mode = classify_concurrent_input(text, manager.is_turn_active())+ if mode == "interrupt":+ await manager.interrupt()+ await bot.send_message(chat_id, "⏹ Останавливаю текущий ответ.")+ return+ if mode == "steer":+ await manager.steer(text)+ await bot.send_message(chat_id, "↪️ Передал в текущую задачу.")+ returnawait _process_and_reply(bot, chat_id, user_id, text)diff --git a/src/d_brain/services/chat_session.py b/src/d_brain/services/chat_session.pyindex 49fdd14..210d60e 100644--- a/src/d_brain/services/chat_session.py+++ b/src/d_brain/services/chat_session.py@@ -55,6 +55,18 @@ class ChatSessionManager:async with get_ask_lock():await asyncio.to_thread(self._session.send_control, text)+ # ── steering: deliberately NOT under the ask-lock — the lock is held by+ # the in-flight turn these calls are aimed at.++ def is_turn_active(self) -> bool:+ return self._session.is_turn_active()++ async def steer(self, text: str) -> None:+ await asyncio.to_thread(self._session.steer, text)++ async def interrupt(self) -> None:+ await asyncio.to_thread(self._session.interrupt)+def reset(self, user_id: int) -> None:"""Clear the live session context (durable data in files is kept)."""self._session.clear()diff --git a/tests/test_chat_handler.py b/tests/test_chat_handler.pyindex 8abe6db..5e459d3 100644--- a/tests/test_chat_handler.py+++ b/tests/test_chat_handler.py@@ -105,3 +105,66 @@ def test_tui_command_rejected_with_hint(monkeypatch):assert mgr.sent == []assert bot.messages and "attach" in bot.messages[0][1]+++# ── concurrent input: steer / interrupt / queue-as-ask ─────────────────────+++def test_classify_concurrent_input_modes():+ from d_brain.bot.handlers.chat import classify_concurrent_input++ assert classify_concurrent_input("привет", turn_active=False) == "ask"+ assert classify_concurrent_input("пиши короче", turn_active=True) == "steer"+ assert classify_concurrent_input("стоп", turn_active=True) == "interrupt"+ assert classify_concurrent_input("/stop", turn_active=True) == "interrupt"+ assert classify_concurrent_input("стоп", turn_active=False) == "ask"+++class SteerableManager(FakeManager):+ def __init__(self, *, active=False):+ super().__init__()+ self.active = active+ self.steered: list[str] = []+ self.interrupts = 0++ def is_turn_active(self) -> bool:+ return self.active++ async def steer(self, text: str) -> None:+ self.steered.append(text)++ async def interrupt(self) -> None:+ self.interrupts += 1+++def test_plain_text_during_active_turn_steers(monkeypatch):+ from d_brain.bot.handlers import chat++ mgr = SteerableManager(active=True)+ monkeypatch.setattr(chat, "_get_manager", lambda: mgr)+ bot = FakeBot()+ asyncio.run(chat._dispatch_text(bot, chat_id=10, user_id=1, text="пиши короче"))+ assert mgr.steered == ["пиши короче"]+ assert mgr.sent == [] # no new turn started+++def test_stop_word_interrupts_active_turn(monkeypatch):+ from d_brain.bot.handlers import chat++ mgr = SteerableManager(active=True)+ monkeypatch.setattr(chat, "_get_manager", lambda: mgr)+ bot = FakeBot()+ asyncio.run(chat._dispatch_text(bot, chat_id=10, user_id=1, text="стоп"))+ assert mgr.interrupts == 1+ assert mgr.sent == [] and mgr.steered == []+++def test_text_when_idle_goes_to_normal_turn(monkeypatch):+ from d_brain.bot.handlers import chat++ mgr = SteerableManager(active=False)+ monkeypatch.setattr(chat, "_get_manager", lambda: mgr)+ bot = FakeBot()+ asyncio.run(chat._dispatch_text(bot, chat_id=10, user_id=1, text="привет"))+ assert mgr.sent == [(1, "привет")]+ assert mgr.steered == []