← Back to Architecture

Application Architecture

Application components, service organization, and system structure

Layered Application Architecture

Multi-tiered architecture with clear separation of concerns

graph TB
    subgraph "Presentation Layer"
        Web[Next.js 15 Web App]
        Mobile[React Native Mobile]
        Native[iOS/Android Native]
    end

    subgraph "API Gateway Layer"
        FastAPI[FastAPI Server<br/>Port 8000]
        WS[WebSocket Handler]
        Middleware[Middleware Stack]
    end

    subgraph "Orchestration Layer"
        HM[Host Manager]
        AC[Agent Coordinator]
        WM[Workflow Manager]
        DD[Domain Detector]
        CR[Capability Router]
        CM[Context Manager]
        PL[Preference Learner]
        GM[Goal Manager]
    end

    subgraph "Agent Layer"
        NA[Nutrition Agent]
        EA[Exercise Agent]
        SA[Sleep Agent]
        AA[Anxiety Agent]
        MA[Meditation Agent]
        HA[Hydration Agent]
        CA[Calendar Agent]
        SH[Shopping Agent]
        OA[Oura Agent]
        GA[General Agent]
        FA[Fast Conversational]
    end

    subgraph "Intelligence Layer"
        MO[Memory Orchestrator]
        IS[Intelligence System]
        EP[Energy Predictor]
        MP[Meal Predictor]
        PE[Pattern Engine]
    end

    subgraph "Service Layer"
        AS[Avatar Service]
        MS[Multimodal Processor]
        OS[Oura Insights]
        CS[Calendar Service]
        PS[Protocol Service]
    end

    subgraph "Data Access Layer"
        Repo[Repository Pattern]
        TursoRepo[Turso Repository]
        Cache[Redis Cache]
    end

    subgraph "Integration Layer"
        PM[Plugin Manager]
        OP[Oura Plugin]
        DP[Dexcom Plugin]
        AP[Apple Watch Plugin]
    end

    Web --> FastAPI
    Mobile --> FastAPI
    Native --> FastAPI
    Web --> WS

    FastAPI --> Middleware
    Middleware --> HM

    HM --> AC
    HM --> WM
    HM --> DD
    HM --> CR
    HM --> CM
    HM --> PL
    HM --> GM

    DD --> CR
    CR --> NA
    CR --> EA
    CR --> SA
    CR --> AA
    CR --> MA
    CR --> HA
    CR --> CA
    CR --> SH
    CR --> OA
    CR --> GA
    CR --> FA

    NA --> MO
    EA --> MO
    SA --> MO
    AA --> MO
    MA --> MO

    NA --> IS
    EA --> IS
    SA --> IS

    IS --> EP
    IS --> MP
    IS --> PE

    NA --> AS
    EA --> MS
    SA --> OS
    CA --> CS
    NA --> PS

    NA --> Repo
    EA --> Repo
    SA --> Repo
    Repo --> TursoRepo
    Repo --> Cache

    OA --> PM
    PM --> OP
    PM --> DP
    PM --> AP

    style Web fill:#e3f2fd
    style Mobile fill:#e3f2fd
    style Native fill:#e3f2fd
    style FastAPI fill:#fff3e0
    style HM fill:#f3e5f5
    style NA fill:#e8f5e9
    style MO fill:#e1f5fe
    style AS fill:#fce4ec
    style Repo fill:#ffebee
    style PM fill:#f0f4c3

Application Components

Key application components organized by responsibility

🎯 API Routers (40+)

  • • Wellness & Chat (3)
  • • Health Domains (8)
  • • Goals & Habits (2)
  • • Memory & Intelligence (3)
  • • Integrations (5)
  • • System & Auth (4)
  • • And 15+ more...

🤖 Domain Agents (18+)

  • • Nutrition Agent
  • • Exercise Agent
  • • Sleep Agent
  • • Oura Sleep Agent
  • • Anxiety Agent
  • • Meditation Agent
  • • Hydration Agent
  • • Calendar Agent
  • • Shopping Agent
  • • Oura Agent
  • • General Agent

🧠 Intelligence Components

  • • Memory Orchestrator
  • • Energy Predictor
  • • Meal Timing Predictor
  • • Exercise Goal Predictor
  • • Pattern Engine
  • • IF Analyzer
  • • Hybrid Retrieval

🔧 Business Services (13+)

  • • Avatar Service
  • • Multimodal Processor
  • • Oura Insights Service
  • • Calendar Service
  • • Protocol Service
  • • Authentication Service
  • • And more...

🔌 Integration Plugins (4)

  • • Oura Plugin
  • • Dexcom Plugin
  • • Apple Watch Plugin
  • • Custom Plugin Framework

📦 Data Repositories

  • • Turso Repository (primary)
  • • Repository Factory
  • • Abstract Base Interface
  • • 200+ SQL methods
  • • Transaction support

Request Processing Flow

End-to-end request journey through the application layers

sequenceDiagram
    participant User
    participant Web as Next.js Frontend
    participant API as FastAPI Gateway
    participant MW as Middleware
    participant DD as Domain Detector
    participant CR as Capability Router
    participant CM as Context Manager
    participant Agent as Domain Agent
    participant Mem as Memory System
    participant LLM as Gemini AI
    participant DB as Turso DB
    participant Cache as Redis

    User->>Web: Type query
    Web->>API: POST /wellness/chat
    API->>MW: Request validation
    MW->>DD: Classify query domain
    DD-->>CR: Domain(s) identified

    CR->>CM: Get user context
    CM->>Cache: Check cache
    alt Cache Hit
        Cache-->>CM: Cached context
    else Cache Miss
        CM->>DB: Fetch user data
        DB-->>CM: User profile
        CM->>Cache: Store context
    end
    CM-->>CR: Rich context

    CR->>Agent: Route to agent(s)
    Agent->>Mem: Retrieve memories
    Mem->>DB: Query memory tables
    DB-->>Mem: Memory data
    Mem-->>Agent: Contextual memories

    Agent->>Cache: Check LLM cache
    alt Cache Miss
        Agent->>LLM: Generate response
        LLM-->>Agent: AI response
        Agent->>Cache: Cache response
    end

    Agent->>Mem: Store interaction
    Mem->>DB: Save episode

    Agent-->>API: Response
    API-->>Web: JSON/Stream
    Web-->>User: Display response

Multi-Agent Coordination

How multiple agents collaborate to handle complex queries

graph LR
    Query[Complex Query:<br/>Nutrition + Exercise]
    DD[Domain Detector]
    AC[Agent Coordinator]
    NA[Nutrition Agent]
    EA[Exercise Agent]
    MB[Message Bus]
    Agg[Response Aggregator]
    Final[Final Response]

    Query --> DD
    DD -->|Domains: nutrition,<br/>exercise| AC
    AC -->|Parallel Dispatch| NA
    AC -->|Parallel Dispatch| EA

    NA -->|A2A Message| MB
    EA -->|A2A Message| MB
    MB -->|Cross-agent context| NA
    MB -->|Cross-agent context| EA

    NA -->|Partial Response| Agg
    EA -->|Partial Response| Agg

    Agg -->|Merge & Synthesize| Final

    style Query fill:#e3f2fd
    style DD fill:#fff3e0
    style AC fill:#f3e5f5
    style NA fill:#e8f5e9
    style EA fill:#e8f5e9
    style MB fill:#ffebee
    style Agg fill:#f0f4c3
    style Final fill:#e1f5fe

Module Organization

Directory structure and module responsibilities

backend/
├── api/                    # API layer (FastAPI)
│   ├── main.py            # App initialization
│   ├── dependencies.py     # DI & middleware
│   └── v1/routers/        # 40+ route modules
│
├── clients/                # Agent layer
│   ├── llm_domain_agent.py
│   └── *_agent.py         # 18+ specialized agents
│
├── host/                   # Orchestration layer
│   ├── host_manager.py
│   ├── agent_coordinator.py
│   ├── workflow_manager.py
│   ├── capability_based_router.py
│   └── domain_detector.py
│
├── memory/                 # Memory system
│   ├── memory_orchestrator.py
│   ├── episodic_memory.py
│   ├── semantic_memory.py
│   └── procedural_memory.py
│
├── intelligence/           # Intelligence layer
│   ├── predictors/
│   ├── pattern_engine.py
│   └── if_analyzer.py
│
├── services/               # Business services
│   ├── avatar_service.py
│   ├── multimodal_processor.py
│   └── ...
│
├── repository/             # Data access
│   ├── base.py
│   ├── turso_repository.py
│   └── factory.py
│
├── plugins/                # Plugin system
│   ├── plugin_manager.py
│   ├── oura/
│   ├── dexcom/
│   └── apple_watch/
│
├── llm/                    # LLM integration
│   ├── llm_router.py
│   └── providers/
│
└── messaging/              # Event system
    ├── message_bus.py
    └── a2a_message.py