How aritako/spottr works
1. What this service is
Spottr is a single backend service for workout logging plus AI-powered coaching. The core architecture splits into two major paths: ordinary CRUD-style workout/exercise API traffic, and an AI reasoning path that answers natural-language training questions from structured workout data and coaching knowledge.
2. Request entrypoint
app/main.py is the application bootstrap. It wires the HTTP surface, config, and the internal modules together. The repo is organized as a service, not a frontend app or batch job.
3. API layer
The public API is organized by domain in app/controllers/:
- exercises.py
- workouts.py
These modules define the route-level boundary for clients. They delegate almost all real work to handlers instead of talking directly to the database or AI components.
4. Handler layer
app/handlers/ sits between controllers and the rest of the system. This is the main orchestration layer for business logic:
- validate and normalize incoming requests
- call repositories for persistence
- call the AI engine when a request requires reasoning
- translate domain errors into API-safe failures
5. Persistence boundary
app/models.py, app/db.py, app/repository/, and alembic/ define the data layer:
- models describe the persisted workout/exercise entities
- repositories encapsulate database reads/writes
- alembic/versions/bbd4c970a463_initial_schema.py shows schema migration management
- alembic.ini and alembic/env.py indicate relational DB migrations are part of the runtime model
This keeps direct database access out of controllers and AI code.
6. AI reasoning subsystem
app/engine/ is the core AI orchestration boundary:
- coach.py likely coordinates how a question is answered
- tools.py defines callable tools over structured workout data
- helpers.py contains supporting logic for prompt/tool orchestration
This is where the service decides whether to consult workout history, coaching knowledge, or both.
7. OpenAI integration
app/openai/client.py isolates the external LLM dependency behind a dedicated client wrapper. That is the main outbound integration point for the AI features, so the rest of the app can treat model calls as an internal service rather than sprinkling API calls throughout the codebase.
8. Domain typing and validation
app/schemas/ provides request/response shapes, while app/enums/ captures fixed domain values for exercises, workouts, and coach-related states. This gives the service a typed contract at the API boundary and a consistent set of domain constants internally.
9. Error handling
app/errors/ defines domain-specific failures, especially for exercise-related flows. This suggests the handlers convert lower-level exceptions into consistent application errors before they reach the API layer.
10. Configuration and deployment
app/config.py, Dockerfile, and docker-compose.yaml show the service is designed to run locally in containers. Configuration is externalized, and Docker Compose is the primary composition mechanism for running the app with its dependencies.
11. Testing
The test suite is split across API and engine behavior:
- tests/test_health.py validates service liveness
- tests/test_exercises.py covers exercise-facing API behavior
- tests/engine/test_coach.py and tests/engine/test_tools.py cover AI orchestration and tool logic
That split mirrors the architecture: HTTP/API correctness on one side, reasoning/tooling correctness on the other.
12. Key architectural flow
The main data flow is: client request → controller → handler → repository/database for structured data, or controller → handler → AI engine → OpenAI client for reasoning. For hybrid questions, the engine can combine both paths before returning a synthesized answer.
13. What to draw in a diagram
The most important boundaries are:
- external clients
- HTTP API/controllers
- handlers/orchestration
- persistence/repository/database
- AI engine/tooling
- external OpenAI service
- migrations/runtime packaging
The repo is intentionally layered so the AI logic stays separate from data access and the API surface.
Main entry points: main.py Main · coach.py Coach · workouts.py Workout Handler · exercises.py Exercise Handler
Components 21
Runtime
Application bootstrap and deployment wiring
HTTP API
Route boundaries for client requests
- Exercises API · Exercise route layer for HTTP requests
app/controllers/exercises.py - Workouts API · Workout route layer for HTTP requests
app/controllers/workouts.py
Core Logic
Request orchestration and domain validation
- Exercise Handler · Validates requests and coordinates exercise business logic
app/handlers/exercises.py - Workout Handler · Validates requests and coordinates workout and coaching flows
app/handlers/workouts.py - Schemas · Request and response shapes for the service boundary
app/schemas - Enums · Fixed domain values for exercises, workouts, and coaching states
app/enums - Errors · Application-specific failures translated to safe API responses
app/errors
Persistence
Database models, repositories, and migrations
- Exercise Repo · Persistence access for exercise data
app/repository/exercises.py - Workout Repo · Persistence access for workout data
app/repository/workouts.py - Models · Persisted workout and exercise entity definitions
app/models.py - DB · Database connection and session boundary
app/db.py - Migrations · Relational schema migration environment and versions
alembic
AI Engine
Coach orchestration and model integration
- Coach · Coordinates answer generation for training questions
app/engine/coach.py - Tools · Callable tools over structured workout data
app/engine/tools.py - Helpers · Supporting logic for prompts and orchestration
app/engine/helpers.py - OpenAI Client · Wrapper around the external model provider
app/openai/client.py
Tests
Behavior checks for API and AI flows
- API Tests · Checks HTTP behavior and health endpoints
tests/test_exercises.py - Engine Tests · Checks coach orchestration and tool logic
tests/engine/test_coach.py
Other components
- Clients · API consumers sending workout and coaching requests
Connections 28
- Clients → Main: requests · Clients send HTTP traffic into the service entrypoint
- Main → Exercises API: routes · Bootstrap wires exercise routes into the HTTP surface
- Main → Workouts API: routes · Bootstrap wires workout routes into the HTTP surface
- Main → Config: loads · Startup reads runtime configuration
- Main → DB: initializes · Startup prepares database access
- Main → Coach: wires · Startup connects AI orchestration into the service
- Exercises API → Exercise Handler: delegates · Exercise routes pass work to the handler layer
- Workouts API → Workout Handler: delegates · Workout routes pass work to the handler layer
- Exercise Handler → Schemas: validates · Handlers use typed request and response contracts
- Workout Handler → Schemas: validates · Handlers use typed request and response contracts
- Exercise Handler → Exercise Repo: persists · Exercise flows read and write data through repositories
- Workout Handler → Workout Repo: persists · Workout flows read and write data through repositories
- Exercise Handler → Errors: maps · Domain failures are converted into API-safe errors
- Workout Handler → Errors: maps · Domain failures are converted into API-safe errors
- Workout Handler → Coach: consults · Workout coaching requests can invoke AI reasoning
- Exercise Repo → Models: uses · Exercise repository works with persisted entities
- Workout Repo → Models: uses · Workout repository works with persisted entities
- Exercise Repo → DB: queries · Repository reads and writes through the database boundary
- Workout Repo → DB: queries · Repository reads and writes through the database boundary
- Migrations → DB: migrates · Migration setup targets the relational database schema
- Coach → Tools: calls · Coach coordinates tool use over workout data
- Coach → Helpers: uses · Coach relies on orchestration helpers
- Coach → OpenAI Client: queries · Coach sends prompts to the external LLM client
- Tools → Workout Repo: reads · Tools can inspect structured workout history
- API Tests → Exercises API: covers · API tests verify exercise routes and behaviors
- API Tests → Workouts API: covers · API tests verify workout routes and behaviors
- Engine Tests → Coach: covers · Engine tests verify coaching orchestration
- Engine Tests → Tools: covers · Engine tests verify tool behavior