refactor: split api.py monolith into 19 route modules (Phase B)
Split 3,598-line api.py into thin orchestrator (128 lines) + 19 route modules in bot/routes/: core.py (7 routes), mood.py (10), language.py (3), evil_mode.py (6), bipolar_mode.py (9), gpu.py (2), bot_actions.py (4), autonomous.py (13), profile_picture.py (26), manual_send.py (3), servers.py (6), figurines.py (5), dms.py (18), image_generation.py (4), chat.py (1), config.py (7), logging_config.py (9), voice.py (3), memory.py (10) All 146 routes verified present via test_route_split.py (149 tests). 21/21 regression tests (test_config_state.py) pass. Monolith backup: bot/api_monolith_backup.py (revert: cp it to api.py).
This commit is contained in:
139
bot/routes/core.py
Normal file
139
bot/routes/core.py
Normal file
@@ -0,0 +1,139 @@
|
||||
"""Core routes: index, logs, prompts, status, conversation."""
|
||||
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import FileResponse
|
||||
import globals
|
||||
from server_manager import server_manager
|
||||
from utils.conversation_history import conversation_history
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger('api')
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/")
|
||||
def read_index():
|
||||
return FileResponse("static/index.html")
|
||||
|
||||
|
||||
@router.get("/logs")
|
||||
def get_logs():
|
||||
try:
|
||||
# Read last 100 lines of the log file
|
||||
with open("/app/bot.log", "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
last_100 = lines[-100:] if len(lines) >= 100 else lines
|
||||
return "".join(last_100)
|
||||
except Exception as e:
|
||||
return f"Error reading log file: {e}"
|
||||
|
||||
|
||||
@router.get("/prompt")
|
||||
def get_last_prompt():
|
||||
return {"prompt": globals.LAST_FULL_PROMPT or "No prompt has been issued yet."}
|
||||
|
||||
|
||||
@router.get("/prompt/cat")
|
||||
def get_last_cat_prompt():
|
||||
"""Get the last Cheshire Cat interaction (full prompt + response) for Web UI."""
|
||||
interaction = globals.LAST_CAT_INTERACTION
|
||||
if not interaction.get("full_prompt"):
|
||||
return {"full_prompt": "No Cheshire Cat interaction has occurred yet.", "response": "", "user": "", "mood": "", "timestamp": ""}
|
||||
return interaction
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
def status():
|
||||
# Get per-server mood summary
|
||||
server_moods = {}
|
||||
for guild_id in server_manager.servers:
|
||||
mood_name, _ = server_manager.get_server_mood(guild_id)
|
||||
server_moods[str(guild_id)] = mood_name
|
||||
|
||||
# Return evil mood when in evil mode
|
||||
current_mood = globals.EVIL_DM_MOOD if globals.EVIL_MODE else globals.DM_MOOD
|
||||
|
||||
return {
|
||||
"status": "online",
|
||||
"mood": current_mood,
|
||||
"evil_mode": globals.EVIL_MODE,
|
||||
"servers": len(server_manager.servers),
|
||||
"active_schedulers": len(server_manager.schedulers),
|
||||
"server_moods": server_moods
|
||||
}
|
||||
|
||||
|
||||
@router.get("/autonomous/stats")
|
||||
def get_autonomous_stats():
|
||||
"""Get autonomous engine stats for all servers"""
|
||||
from utils.autonomous import autonomous_engine
|
||||
|
||||
stats = {}
|
||||
for guild_id in server_manager.servers:
|
||||
server_info = server_manager.servers[guild_id]
|
||||
mood_name, _ = server_manager.get_server_mood(guild_id)
|
||||
|
||||
# Get context signals for this server
|
||||
if guild_id in autonomous_engine.server_contexts:
|
||||
ctx = autonomous_engine.server_contexts[guild_id]
|
||||
|
||||
# Get mood profile
|
||||
mood_profile = autonomous_engine.mood_profiles.get(mood_name, {
|
||||
"energy": 0.5,
|
||||
"sociability": 0.5,
|
||||
"impulsiveness": 0.5
|
||||
})
|
||||
|
||||
# Sanitize float values for JSON serialization (replace inf with large number)
|
||||
time_since_action = ctx.time_since_last_action
|
||||
if time_since_action == float('inf'):
|
||||
time_since_action = 999999
|
||||
|
||||
time_since_interaction = ctx.time_since_last_interaction
|
||||
if time_since_interaction == float('inf'):
|
||||
time_since_interaction = 999999
|
||||
|
||||
stats[str(guild_id)] = {
|
||||
"guild_name": server_info.guild_name,
|
||||
"mood": mood_name,
|
||||
"mood_profile": mood_profile,
|
||||
"context": {
|
||||
"messages_last_5min": ctx.messages_last_5min,
|
||||
"messages_last_hour": ctx.messages_last_hour,
|
||||
"unique_users_active": ctx.unique_users_active,
|
||||
"conversation_momentum": round(ctx.conversation_momentum, 2),
|
||||
"users_joined_recently": ctx.users_joined_recently,
|
||||
"users_status_changed": ctx.users_status_changed,
|
||||
"users_started_activity": ctx.users_started_activity,
|
||||
"time_since_last_action": round(time_since_action, 1),
|
||||
"time_since_last_interaction": round(time_since_interaction, 1),
|
||||
"messages_since_last_appearance": ctx.messages_since_last_appearance,
|
||||
"hour_of_day": ctx.hour_of_day,
|
||||
"is_weekend": ctx.is_weekend,
|
||||
"mood_energy_level": round(ctx.mood_energy_level, 2)
|
||||
}
|
||||
}
|
||||
else:
|
||||
# Server not yet initialized in autonomous engine
|
||||
mood_profile = autonomous_engine.mood_profiles.get(mood_name, {
|
||||
"energy": 0.5,
|
||||
"sociability": 0.5,
|
||||
"impulsiveness": 0.5
|
||||
})
|
||||
|
||||
stats[str(guild_id)] = {
|
||||
"guild_name": server_info.guild_name,
|
||||
"mood": mood_name,
|
||||
"mood_profile": mood_profile,
|
||||
"context": None
|
||||
}
|
||||
|
||||
return {"servers": stats}
|
||||
|
||||
|
||||
@router.get("/conversation/{user_id}")
|
||||
def get_conversation(user_id: str):
|
||||
"""Get conversation history for a user/channel (uses centralized ConversationHistory)."""
|
||||
messages = conversation_history.get_recent_messages(user_id)
|
||||
return {"conversation": [{"author": author, "content": content, "is_bot": is_bot} for author, content, is_bot in messages]}
|
||||
Reference in New Issue
Block a user