HIGH: Split api.py (3,219 lines) into FastAPI domain routers #32

Open
opened 2026-02-20 14:43:16 +02:00 by Koko210 · 0 comments
Owner

Problem

bot/api.py is a 3,219-line god module containing 100+ endpoints spanning every domain: mood management, evil/bipolar mode, GPU selection, autonomous triggers, profile picture management, manual messaging, figurine subscriptions, DM logging/blocking/analysis, image generation, voice call initiation, Cheshire Cat memory CRUD, configuration management, logging configuration, server CRUD, SSE chat streaming, and status/stats.

This makes the file extremely difficult to navigate, maintain, and review. Related endpoints are scattered throughout with no clear grouping.

Proposed Solution

Split into FastAPI APIRouter modules by domain:

  • bot/api/init.py — App factory, mounts all routers
  • bot/api/mood_router.py — Mood management (DM + per-server)
  • bot/api/voice_router.py — Voice call initiation, status
  • bot/api/dm_router.py — DM logging, blocking, analysis, export
  • bot/api/memory_router.py — Cheshire Cat memory CRUD
  • bot/api/config_router.py — Configuration get/set, logging config
  • bot/api/server_router.py — Server CRUD, per-server settings
  • bot/api/autonomous_router.py — Autonomous triggers, figurine notifier
  • bot/api/media_router.py — Image generation, profile pictures
  • bot/api/chat_router.py — SSE chat streaming
  • bot/api/status_router.py — Health, stats, GPU status

Each router uses APIRouter(prefix='/domain', tags=['Domain']) and is included via app.include_router() in the factory.

Impact

  • Risk: Very Low — purely structural, no behavior change
  • Effort: Medium (a few hours of careful splitting)
  • Benefit: Massive improvement to maintainability, code review, and navigation

Files Affected

  • bot/api.py → split into bot/api/*.py
  • bot/bot.py (imports the FastAPI app)
  • bot/api_main.py (if it references api.py directly)
## Problem bot/api.py is a 3,219-line god module containing 100+ endpoints spanning every domain: mood management, evil/bipolar mode, GPU selection, autonomous triggers, profile picture management, manual messaging, figurine subscriptions, DM logging/blocking/analysis, image generation, voice call initiation, Cheshire Cat memory CRUD, configuration management, logging configuration, server CRUD, SSE chat streaming, and status/stats. This makes the file extremely difficult to navigate, maintain, and review. Related endpoints are scattered throughout with no clear grouping. ## Proposed Solution Split into FastAPI APIRouter modules by domain: - bot/api/__init__.py — App factory, mounts all routers - bot/api/mood_router.py — Mood management (DM + per-server) - bot/api/voice_router.py — Voice call initiation, status - bot/api/dm_router.py — DM logging, blocking, analysis, export - bot/api/memory_router.py — Cheshire Cat memory CRUD - bot/api/config_router.py — Configuration get/set, logging config - bot/api/server_router.py — Server CRUD, per-server settings - bot/api/autonomous_router.py — Autonomous triggers, figurine notifier - bot/api/media_router.py — Image generation, profile pictures - bot/api/chat_router.py — SSE chat streaming - bot/api/status_router.py — Health, stats, GPU status Each router uses APIRouter(prefix='/domain', tags=['Domain']) and is included via app.include_router() in the factory. ## Impact - Risk: Very Low — purely structural, no behavior change - Effort: Medium (a few hours of careful splitting) - Benefit: Massive improvement to maintainability, code review, and navigation ## Files Affected - bot/api.py → split into bot/api/*.py - bot/bot.py (imports the FastAPI app) - bot/api_main.py (if it references api.py directly)
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Koko210/miku-discord#32