stage-6: model selection and advanced commands

/model (show/change), /restart (owner), /logs, set_session_model API, model reset on /clear. 20 new tests (161 total).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
MoltBot Service
2026-02-13 13:13:38 +00:00
parent a1a6ca9a3f
commit 5bdceff732
6 changed files with 475 additions and 10 deletions

View File

@@ -2,12 +2,21 @@
import asyncio
import logging
import os
import signal
from pathlib import Path
import discord
from discord import app_commands
from src.config import Config
from src.claude_session import clear_session, get_active_session
from src.claude_session import (
clear_session,
get_active_session,
set_session_model,
PROJECT_ROOT,
VALID_MODELS,
)
from src.router import route_message
logger = logging.getLogger("echo-core.discord")
@@ -103,6 +112,10 @@ def create_bot(config: Config) -> discord.Client:
"`/admin add <user_id>` — Add an admin (owner only)",
"`/clear` — Clear the session for this channel",
"`/status` — Show session status for this channel",
"`/model` — View current model and available models",
"`/model <choice>` — Change model for this channel's session",
"`/logs [n]` — Show last N log lines (default 10)",
"`/restart` — Restart the bot process (owner only)",
]
await interaction.response.send_message(
"\n".join(lines), ephemeral=True
@@ -191,10 +204,12 @@ def create_bot(config: Config) -> discord.Client:
@tree.command(name="clear", description="Clear the session for this channel")
async def clear(interaction: discord.Interaction) -> None:
channel_id = str(interaction.channel_id)
default_model = config.get("bot.default_model", "sonnet")
removed = clear_session(channel_id)
if removed:
await interaction.response.send_message(
"Session cleared.", ephemeral=True
f"Session cleared. Model reset to {default_model}.",
ephemeral=True,
)
else:
await interaction.response.send_message(
@@ -221,6 +236,107 @@ def create_bot(config: Config) -> discord.Client:
ephemeral=True,
)
@tree.command(name="model", description="View or change the AI model")
@app_commands.describe(choice="Model to switch to")
@app_commands.choices(choice=[
app_commands.Choice(name="opus", value="opus"),
app_commands.Choice(name="sonnet", value="sonnet"),
app_commands.Choice(name="haiku", value="haiku"),
])
async def model_cmd(
interaction: discord.Interaction,
choice: app_commands.Choice[str] | None = None,
) -> None:
channel_id = str(interaction.channel_id)
if choice is None:
# Show current model and available models
session = get_active_session(channel_id)
if session:
current = session.get("model", "unknown")
else:
current = config.get("bot.default_model", "sonnet")
available = ", ".join(sorted(VALID_MODELS))
await interaction.response.send_message(
f"**Current model:** {current}\n"
f"**Available:** {available}",
ephemeral=True,
)
else:
model = choice.value
session = get_active_session(channel_id)
if session:
set_session_model(channel_id, model)
else:
# No session yet — pre-set in active.json so next message uses it
from src.claude_session import _load_sessions, _save_sessions
from datetime import datetime, timezone
sessions = _load_sessions()
sessions[channel_id] = {
"session_id": "",
"model": model,
"created_at": datetime.now(timezone.utc).isoformat(),
"last_message_at": datetime.now(timezone.utc).isoformat(),
"message_count": 0,
}
_save_sessions(sessions)
await interaction.response.send_message(
f"Model changed to **{model}**.", ephemeral=True
)
@tree.command(name="restart", description="Restart the bot process")
async def restart(interaction: discord.Interaction) -> None:
if not is_owner(str(interaction.user.id)):
await interaction.response.send_message(
"Owner only.", ephemeral=True
)
return
pid_file = PROJECT_ROOT / "echo-core.pid"
if not pid_file.exists():
await interaction.response.send_message(
"No PID file found (echo-core.pid).", ephemeral=True
)
return
try:
pid = int(pid_file.read_text().strip())
os.kill(pid, signal.SIGTERM)
await interaction.response.send_message(
"Restarting...", ephemeral=True
)
except ProcessLookupError:
await interaction.response.send_message(
f"Process {pid} not found.", ephemeral=True
)
except ValueError:
await interaction.response.send_message(
"Invalid PID file content.", ephemeral=True
)
@tree.command(name="logs", description="Show recent log lines")
@app_commands.describe(n="Number of lines to show (default 10)")
async def logs_cmd(
interaction: discord.Interaction, n: int = 10
) -> None:
log_path = PROJECT_ROOT / "logs" / "echo-core.log"
if not log_path.exists():
await interaction.response.send_message(
"No log file found.", ephemeral=True
)
return
try:
all_lines = log_path.read_text(encoding="utf-8").splitlines()
tail = all_lines[-n:] if len(all_lines) >= n else all_lines
text = "\n".join(tail)
# Truncate to fit Discord message limit (2000 - code block overhead)
if len(text) > 1900:
text = text[-1900:]
await interaction.response.send_message(
f"```\n{text}\n```", ephemeral=True
)
except Exception as e:
await interaction.response.send_message(
f"Error reading logs: {e}", ephemeral=True
)
# --- Events ---
@client.event