Message router, typing indicator, emoji reactions, auto start/resume sessions, message splitting >2000 chars. 34 new tests (141 total). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
"""Echo Core message router — routes messages to Claude or commands."""
|
|
|
|
import logging
|
|
from src.config import Config
|
|
from src.claude_session import send_message, clear_session, get_active_session, list_sessions
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
# Module-level config instance (lazy singleton)
|
|
_config: Config | None = None
|
|
|
|
|
|
def _get_config() -> Config:
|
|
"""Return the module-level config, creating it on first access."""
|
|
global _config
|
|
if _config is None:
|
|
_config = Config()
|
|
return _config
|
|
|
|
|
|
def route_message(channel_id: str, user_id: str, text: str, model: str | None = None) -> tuple[str, bool]:
|
|
"""Route an incoming message. Returns (response_text, is_command).
|
|
|
|
If text starts with / it's a command (handled here for text-based commands).
|
|
Otherwise it goes to Claude via send_message (auto start/resume).
|
|
"""
|
|
text = text.strip()
|
|
|
|
# Text-based commands (not slash commands — these work in any adapter)
|
|
if text.lower() == "/clear":
|
|
cleared = clear_session(channel_id)
|
|
if cleared:
|
|
return "Session cleared.", True
|
|
return "No active session.", True
|
|
|
|
if text.lower() == "/status":
|
|
return _status(channel_id), True
|
|
|
|
if text.startswith("/"):
|
|
return f"Unknown command: {text.split()[0]}", True
|
|
|
|
# Regular message → Claude
|
|
if not model:
|
|
# Get channel default model or global default
|
|
channel_cfg = _get_channel_config(channel_id)
|
|
model = (channel_cfg or {}).get("default_model") or _get_config().get("bot.default_model", "sonnet")
|
|
|
|
try:
|
|
response = send_message(channel_id, text, model=model)
|
|
return response, False
|
|
except Exception as e:
|
|
log.error(f"Claude error for channel {channel_id}: {e}")
|
|
return f"Error: {e}", False
|
|
|
|
|
|
def _status(channel_id: str) -> str:
|
|
"""Build status message for a channel."""
|
|
session = get_active_session(channel_id)
|
|
if not session:
|
|
return "No active session."
|
|
|
|
model = session.get("model", "unknown")
|
|
sid = session.get("session_id", "unknown")[:12]
|
|
count = session.get("message_count", 0)
|
|
|
|
return f"Model: {model} | Session: {sid}... | Messages: {count}"
|
|
|
|
|
|
def _get_channel_config(channel_id: str) -> dict | None:
|
|
"""Find channel config by ID."""
|
|
channels = _get_config().get("channels", {})
|
|
for alias, ch in channels.items():
|
|
if ch.get("id") == channel_id:
|
|
return ch
|
|
return None
|