# aritako/spottr architecture

> Architecture diagram of the GitHub repository aritako/spottr, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aritako/spottr
- Repository: https://github.com/aritako/spottr

## Overview

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: [`app/main.py`](https://github.com/aritako/spottr/blob/main/app/main.py) (Main), [`app/engine/coach.py`](https://github.com/aritako/spottr/blob/main/app/engine/coach.py) (Coach), [`app/handlers/workouts.py`](https://github.com/aritako/spottr/blob/main/app/handlers/workouts.py) (Workout Handler), [`app/handlers/exercises.py`](https://github.com/aritako/spottr/blob/main/app/handlers/exercises.py) (Exercise Handler)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_app_main["Main<br/>bootstrap<br/>[main.py]"]
  node_config["Config<br/>settings<br/>[config.py]"]
end

subgraph group_api["HTTP API"]
  node_controllers_exercises["Exercises API<br/>controller<br/>[exercises.py]"]
  node_controllers_workouts["Workouts API<br/>controller<br/>[workouts.py]"]
end

subgraph group_core["Core Logic"]
  node_handlers_exercises["Exercise Handler<br/>orchestration<br/>[exercises.py]"]
  node_handlers_workouts["Workout Handler<br/>orchestration<br/>[workouts.py]"]
  node_schemas["Schemas<br/>contracts"]
  node_enums["Enums<br/>domain constants"]
  node_errors["Errors"]
end

subgraph group_data["Persistence"]
  node_repo_exercises[("Exercise Repo<br/>[exercises.py]")]
  node_repo_workouts[("Workout Repo<br/>[workouts.py]")]
  node_models["Models<br/>[models.py]"]
  node_db[("DB<br/>database session<br/>[db.py]")]
  node_alembic["Migrations"]
end

subgraph group_ai["AI Engine"]
  node_engine_coach["Coach<br/>ai orchestration<br/>[coach.py]"]
  node_engine_tools["Tools<br/>[tools.py]"]
  node_engine_helpers["Helpers<br/>ai support<br/>[helpers.py]"]
  node_openai_client["OpenAI Client<br/>llm client<br/>[client.py]"]
end

subgraph group_tests["Tests"]
  node_tests_api["API Tests<br/>test suite<br/>[test_exercises.py]"]
  node_tests_engine["Engine Tests<br/>test suite<br/>[test_coach.py]"]
end

node_external_clients(("Clients<br/>external"))

node_external_clients -->|"requests"| node_app_main
node_app_main -->|"routes"| node_controllers_exercises
node_app_main -->|"routes"| node_controllers_workouts
node_app_main -->|"loads"| node_config
node_app_main -->|"initializes"| node_db
node_app_main -->|"wires"| node_engine_coach
node_controllers_exercises -->|"delegates"| node_handlers_exercises
node_controllers_workouts -->|"delegates"| node_handlers_workouts
node_handlers_exercises -->|"validates"| node_schemas
node_handlers_workouts -->|"validates"| node_schemas
node_handlers_exercises -->|"persists"| node_repo_exercises
node_handlers_workouts -->|"persists"| node_repo_workouts
node_handlers_exercises -->|"maps"| node_errors
node_handlers_workouts -->|"maps"| node_errors
node_handlers_workouts -->|"consults"| node_engine_coach
node_repo_exercises -->|"uses"| node_models
node_repo_workouts -->|"uses"| node_models
node_repo_exercises -->|"queries"| node_db
node_repo_workouts -->|"queries"| node_db
node_alembic -->|"migrates"| node_db
node_engine_coach -->|"calls"| node_engine_tools
node_engine_coach -->|"uses"| node_engine_helpers
node_engine_coach -->|"queries"| node_openai_client
node_engine_tools -->|"reads"| node_repo_workouts
node_tests_api -.->|"covers"| node_controllers_exercises
node_tests_api -.->|"covers"| node_controllers_workouts
node_tests_engine -.->|"covers"| node_engine_coach
node_tests_engine -.->|"covers"| node_engine_tools

click node_app_main "https://github.com/aritako/spottr/blob/main/app/main.py"
click node_config "https://github.com/aritako/spottr/blob/main/app/config.py"
click node_controllers_exercises "https://github.com/aritako/spottr/blob/main/app/controllers/exercises.py"
click node_controllers_workouts "https://github.com/aritako/spottr/blob/main/app/controllers/workouts.py"
click node_handlers_exercises "https://github.com/aritako/spottr/blob/main/app/handlers/exercises.py"
click node_handlers_workouts "https://github.com/aritako/spottr/blob/main/app/handlers/workouts.py"
click node_schemas "https://github.com/aritako/spottr/tree/main/app/schemas"
click node_enums "https://github.com/aritako/spottr/tree/main/app/enums"
click node_errors "https://github.com/aritako/spottr/tree/main/app/errors"
click node_repo_exercises "https://github.com/aritako/spottr/blob/main/app/repository/exercises.py"
click node_repo_workouts "https://github.com/aritako/spottr/blob/main/app/repository/workouts.py"
click node_models "https://github.com/aritako/spottr/blob/main/app/models.py"
click node_db "https://github.com/aritako/spottr/blob/main/app/db.py"
click node_alembic "https://github.com/aritako/spottr/tree/main/alembic"
click node_engine_coach "https://github.com/aritako/spottr/blob/main/app/engine/coach.py"
click node_engine_tools "https://github.com/aritako/spottr/blob/main/app/engine/tools.py"
click node_engine_helpers "https://github.com/aritako/spottr/blob/main/app/engine/helpers.py"
click node_openai_client "https://github.com/aritako/spottr/blob/main/app/openai/client.py"
click node_tests_api "https://github.com/aritako/spottr/blob/main/tests/test_exercises.py"
click node_tests_engine "https://github.com/aritako/spottr/blob/main/tests/engine/test_coach.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app_main,node_config toneBlue
class node_controllers_exercises,node_controllers_workouts toneAmber
class node_handlers_exercises,node_handlers_workouts,node_schemas,node_enums,node_errors toneMint
class node_repo_exercises,node_repo_workouts,node_models,node_db,node_alembic toneRose
class node_engine_coach,node_engine_tools,node_engine_helpers,node_openai_client toneIndigo
class node_tests_api,node_tests_engine toneTeal
class node_external_clients toneNeutral
```

## Components

### Runtime

Application bootstrap and deployment wiring

- [Main](https://github.com/aritako/spottr/blob/main/app/main.py): bootstrap · Application entrypoint that wires HTTP, config, and internal modules · `app/main.py`
- [Config](https://github.com/aritako/spottr/blob/main/app/config.py): settings · Runtime configuration and environment binding · `app/config.py`

### HTTP API

Route boundaries for client requests

- [Exercises API](https://github.com/aritako/spottr/blob/main/app/controllers/exercises.py): controller · Exercise route layer for HTTP requests · `app/controllers/exercises.py`
- [Workouts API](https://github.com/aritako/spottr/blob/main/app/controllers/workouts.py): controller · Workout route layer for HTTP requests · `app/controllers/workouts.py`

### Core Logic

Request orchestration and domain validation

- [Exercise Handler](https://github.com/aritako/spottr/blob/main/app/handlers/exercises.py): orchestration · Validates requests and coordinates exercise business logic · `app/handlers/exercises.py`
- [Workout Handler](https://github.com/aritako/spottr/blob/main/app/handlers/workouts.py): orchestration · Validates requests and coordinates workout and coaching flows · `app/handlers/workouts.py`
- [Schemas](https://github.com/aritako/spottr/tree/main/app/schemas): contracts · Request and response shapes for the service boundary · `app/schemas`
- [Enums](https://github.com/aritako/spottr/tree/main/app/enums): domain constants · Fixed domain values for exercises, workouts, and coaching states · `app/enums`
- [Errors](https://github.com/aritako/spottr/tree/main/app/errors): domain errors · Application-specific failures translated to safe API responses · `app/errors`

### Persistence

Database models, repositories, and migrations

- [Exercise Repo](https://github.com/aritako/spottr/blob/main/app/repository/exercises.py): repository · Persistence access for exercise data · `app/repository/exercises.py`
- [Workout Repo](https://github.com/aritako/spottr/blob/main/app/repository/workouts.py): repository · Persistence access for workout data · `app/repository/workouts.py`
- [Models](https://github.com/aritako/spottr/blob/main/app/models.py): orm models · Persisted workout and exercise entity definitions · `app/models.py`
- [DB](https://github.com/aritako/spottr/blob/main/app/db.py): database session · Database connection and session boundary · `app/db.py`
- [Migrations](https://github.com/aritako/spottr/tree/main/alembic): schema migrations · Relational schema migration environment and versions · `alembic`

### AI Engine

Coach orchestration and model integration

- [Coach](https://github.com/aritako/spottr/blob/main/app/engine/coach.py): ai orchestration · Coordinates answer generation for training questions · `app/engine/coach.py`
- [Tools](https://github.com/aritako/spottr/blob/main/app/engine/tools.py): ai tools · Callable tools over structured workout data · `app/engine/tools.py`
- [Helpers](https://github.com/aritako/spottr/blob/main/app/engine/helpers.py): ai support · Supporting logic for prompts and orchestration · `app/engine/helpers.py`
- [OpenAI Client](https://github.com/aritako/spottr/blob/main/app/openai/client.py): llm client · Wrapper around the external model provider · `app/openai/client.py`

### Tests

Behavior checks for API and AI flows

- [API Tests](https://github.com/aritako/spottr/blob/main/tests/test_exercises.py): test suite · Checks HTTP behavior and health endpoints · `tests/test_exercises.py`
- [Engine Tests](https://github.com/aritako/spottr/blob/main/tests/engine/test_coach.py): test suite · Checks coach orchestration and tool logic · `tests/engine/test_coach.py`

### Other components

- **Clients**: external · API consumers sending workout and coaching requests

## Connections

- 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
