How codex-mine/sales_pilot works
Purpose and topology
SalesPilot is a Phase-1 foundation for a multi-tenant AI SDR SaaS: it currently establishes identity, organization tenancy, API conventions, and a dashboard shell rather than AI or sales automation. It is a monorepo with a Next.js web client and FastAPI backend, containerized for local deployment. Key deployment paths: docker-compose.yml, apps/api/Dockerfile, apps/web/Dockerfile.
Runtime entry points
The API starts from apps/api/app/main.py, mounting versioned routes through apps/api/app/api/v1/router.py; health and auth endpoints live under /api/v1. The web runtime is Next.js App Router, rooted at apps/web/src/app/layout.tsx, with landing, login, dashboard, and settings pages. API health is the operational liveness entry point: apps/api/app/api/v1/health.py.
HTTP/API boundary
FastAPI route modules should remain thin: request validation and response schemas are at the edge, while services own application behavior. Auth and organization endpoints are defined in apps/api/app/api/v1/auth.py and apps/api/app/api/v1/organizations.py; shared API schema/envelope serialization is represented by apps/api/app/schemas/common.py and apps/api/app/schemas/serializers.py. All externally supplied API data is Pydantic-validated.
Identity, sessions, and authorization
Authentication is the central implemented workflow. Routes invoke service-layer orchestration in apps/api/app/services/auth_service.py and apps/api/app/services/session_service.py, backed by repositories such as apps/api/app/repositories/user_repository.py and apps/api/app/repositories/refresh_token_repository.py. Short-lived JWT access tokens and hashed opaque refresh tokens form the session boundary; cookie, CSRF, password, device, rate-limit, permission, and token concerns are isolated in apps/api/app/auth/cookies.py, apps/api/app/security/tokens.py, and apps/api/app/security/permissions.py.
Tenancy and persistence
PostgreSQL/SQLAlchemy is the system of record. Database initialization/session management is isolated in apps/api/app/database/base.py and apps/api/app/database/session.py. Domain models establish identity, organizations, CRM, campaign, communication, and AI-adjacent data boundaries under apps/api/app/models/identity/models.py, apps/api/app/models/crm/models.py, and apps/api/app/models/campaigns/models.py. Future data access is intended to be organization-scoped through repositories and dependency injection; organization behavior is concentrated in apps/api/app/services/organization_service.py.
Schema evolution and asynchronous infrastructure
Alembic owns relational schema changes, with runtime migration setup at apps/api/alembic/env.py and versioned migrations in apps/api/alembic/versions/. Redis is a material infrastructure dependency configured via apps/api/app/core/redis.py; Celery worker initialization in apps/api/app/workers/celery_app.py provides the future async-work boundary.
Web client flow
The browser application uses a typed HTTP layer (apps/web/src/api/client.ts, apps/web/src/api/interceptors.ts) and feature-local auth API calls (apps/web/src/features/auth/api/login.ts). Authentication state is held in apps/web/src/store/auth-store.ts; middleware at apps/web/src/middleware.ts is the route-access boundary. User flow is landing → login → dashboard/settings via apps/web/src/app/page.tsx, apps/web/src/app/login/page.tsx, and apps/web/src/app/dashboard/page.tsx. The present dashboard is a UI shell, not a sales-data workflow.
Cross-cutting contracts and operations
Configuration is environment-driven at apps/api/app/core/config.py, with example environment files at .env.example and apps/web/.env.example. Shared package directories are reserved extension points rather than active core dependencies: packages/types, packages/ui, and packages/config. CI is defined in .github/workflows/ci.yml.
Main entry points: router.py V1 route boundary · page.tsx Browser user flow · main.py FastAPI application · auth_service.py Auth and session services
Components 20
Web client
- Browser user flow · Landing, login, dashboard, and settings shell.
apps/web/src/app/page.tsx - Route access middleware
apps/web/src/middleware.ts - Auth state
apps/web/src/store/auth-store.ts - Typed API client · Interceptors centralize browser API behavior.
apps/web/src/api/client.ts - Login API feature
apps/web/src/features/auth/api/login.ts
API service
- FastAPI application · Mounts the versioned API.
apps/api/app/main.py - V1 route boundary · Thin validated HTTP endpoint layer.
apps/api/app/api/v1/router.py - Auth endpoints
apps/api/app/api/v1/auth.py - Organization endpoints
apps/api/app/api/v1/organizations.py - API contracts · Validates external data and serializes envelopes.
apps/api/app/schemas/common.py - Auth and session services · Orchestrates login, sessions, and token refresh.
apps/api/app/services/auth_service.py - Organization service · Tenancy behavior and future organization scoping.
apps/api/app/services/organization_service.py - Session security · JWT access tokens, hashed refresh tokens, cookies, CSRF, and permissions.
apps/api/app/security/tokens.py - Repositories · User and refresh-token access boundary.
apps/api/app/repositories/user_repository.py
Data and async
- PostgreSQL / SQLAlchemy · Identity, organization, CRM, campaign, communication, and AI-adjacent models.
apps/api/app/database/session.py - Alembic migrations
apps/api/alembic/env.py - Redis
apps/api/app/core/redis.py - Celery worker boundary · Reserved boundary for future asynchronous work.
apps/api/app/workers/celery_app.py
Delivery
- Local containers · Runs web, API, and supporting services locally.
docker-compose.yml - CI pipeline
.github/workflows/ci.yml
Connections 21
- Browser user flow → Route access middleware: route access
- Browser user flow → Auth state: reads session state
- Browser user flow → Login API feature: submits login
- Login API feature → Typed API client: uses
- Typed API client → FastAPI application: HTTPS /api/v1
- FastAPI application → V1 route boundary: mounts
- V1 route boundary → Auth endpoints: auth routes
- V1 route boundary → Organization endpoints: organization routes
- V1 route boundary → API contracts: validates and serializes
- Auth endpoints → Auth and session services: delegates
- Organization endpoints → Organization service: delegates
- Auth and session services → Session security: issues and verifies sessions
- Auth and session services → Repositories: persists identity
- Organization service → Repositories: organization-scoped access
- Repositories → PostgreSQL / SQLAlchemy: SQLAlchemy
- Alembic migrations → PostgreSQL / SQLAlchemy: evolves schema
- FastAPI application → Redis: uses
- Celery worker boundary → Redis: broker/backend boundary
- Local containers → FastAPI application: runs API image
- Local containers → Browser user flow: runs web image
- CI pipeline → Local containers: validates delivery