Skip to content

Refactor: split community.py into focused modules (1976 lines) #326

Description

@neuromechanist

Summary

src/api/routers/community.py is the single largest file in the codebase at 1,976 lines. This violates the single responsibility principle and makes it difficult to reason about, test, and modify.

Current Structure (problematic)

The file mixes 4 distinct concerns:

  • Pydantic model definitions (ChatRequest, ChatMessage, PageContext, etc.)
  • Router factory logic for generating per-community endpoints
  • Knowledge search and retrieval integration
  • Metrics logging and cost tracking

Proposed Split

Module Contents Approx Lines
models.py Pydantic request/response models ~150 lines
router_factory.py create_community_router() factory ~400 lines
endpoints/ask.py /ask endpoint implementation ~200 lines
endpoints/chat.py /chat endpoint (SSE streaming) ~300 lines
endpoints/sessions.py Session management endpoints ~150 lines

Acceptance Criteria

  • No file exceeds 600 lines after split
  • All existing tests continue to pass without modification
  • Import paths updated in dependent modules (main.py, test files)
  • Public API behavior is unchanged (API contract preserved)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Priority 1: Critical, fix as soon as possibledeveloper-experienceImproves developer experienceenhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions