# rbarbieri13/fuelwell architecture

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

- Interactive diagram: https://gitdiagram.com/rbarbieri13/fuelwell
- Repository: https://github.com/rbarbieri13/fuelwell

## Overview

#### Purpose and entry points
FuelWell is a health, nutrition, fitness, and AI-coaching product with two clients sharing Supabase-backed product concepts: a Next.js web application and a native SwiftUI iOS app. The generic root README is not representative of the repository’s product scope.

Web routing starts at `src/app/layout.tsx`, with authenticated product surfaces under `src/app/app/layout.tsx` and public/auth route groups under `src/app/(marketing)/page.tsx` and `src/app/(auth)/login/page.tsx`. iOS starts at `ios/FuelWellApp/Sources/FuelWellApp.swift`, composed through `ios/Features/App/Sources/App/AppCoordinator.swift`.

#### Web application shell and product surfaces
The web client uses the Next.js App Router with server routes and interactive client components. It organizes user workflows around dashboard, food logging, daily review, meal plans/recipes/groceries, workouts, profile/settings, and a conversational coach. Shared responsive navigation is centralized in `src/components/layout/sidebar.tsx`, `src/components/layout/mobile-nav.tsx`, and the authenticated layout.

The primary interactive pages are route-level boundaries; their client components and feature-specific UI call hooks/repositories rather than directly owning persistence. Examples: `src/app/app/dashboard/dashboard-client.tsx`, `src/app/app/log/page.tsx`, and `src/app/app/workouts/[id]/live/page.tsx`.

#### Coach engine: request, policy, tools, and artifacts
The coach is an API-mediated agent system. A user turn reaches `src/app/api/coach/turn/route.ts`, which coordinates provider access, system prompt/context, intent/tool execution, mutation application, auditing, and response persistence. The core integration boundary is `src/lib/coach/provider-client.ts`; prompt/policy composition lives in `src/lib/coach/system-prompt.ts` and `src/lib/coach/voice-filter.ts`.

Tools are grouped by product domain in `src/lib/coach/tools/index.ts`, `src/lib/coach/tools/meal-tools.ts`, and `src/lib/coach/tools/workout-tools.ts`. Mutations are applied through `src/lib/coach/apply-mutation.ts`, separating agent-proposed actions from persisted effects. Structured results are rendered in the chat UI through `src/components/coach/artifacts/index.tsx` and its artifact contract, rather than requiring prose-only responses.

#### Health-data domain and persistence flow
Nutrition, body, workout, and grocery actions flow from pages/hooks to API routes and repository modules, then to Supabase when configured. Core web repositories include `src/lib/day-log-repository.ts`, `src/lib/body-log-repository.ts`, and `src/lib/workout-log-repository.ts`; endpoint boundaries include `src/app/api/day-log/route.ts`, `src/app/api/body-log/route.ts`, and `src/app/api/workout-log/route.ts`.

Food acquisition has distinct barcode, image-estimation, and restaurant paths at `src/app/api/food/barcode/route.ts`, `src/app/api/food/photo-estimate/route.ts`, and `src/app/api/restaurants/nearby/route.ts`. Meal and workout logging therefore feed both deterministic tracking views and coach context.

#### Supabase, identity, and data boundaries
Supabase provides authentication/session handling and product persistence. Web client/server adapters are separated in `src/lib/supabase/client.ts`, `src/lib/supabase/server.ts`, and `src/lib/supabase/middleware.ts`; the auth callback is `src/app/(auth)/callback/route.ts`. Schema evolution is maintained as SQL migrations, including coach, profile-preference, uploaded-artifact, fitness/grocery, and body-log data in `supabase/migrations/20260611180100_coach_tables.sql`, `supabase/migrations/20260612120000_profiles_preferences_jsonb.sql`, and `supabase/migrations/20260712200713_fitness_grocery_foundation.sql`.

#### Native iOS architecture
The iOS application is SwiftUI with feature packages and reusable domain/service packages. App-level routing and tabs live in `ios/Features/App/Sources/App/AppFeature.swift` and `ios/Features/App/Sources/App/RootTabView.swift`; nutrition workflows are encapsulated in `ios/Features/Nutrition/Sources/Nutrition/DailyLogFeature.swift` and `ios/Features/Nutrition/Sources/Nutrition/AddMealSheet.swift`.

Domain calculations are isolated in `ios/Packages/NutritionDomain/Sources/NutritionDomain/MacroDecisionEngine.swift`. Networking, persistence, HealthKit, Supabase feature flags, subscriptions, analytics, crash reporting, and an Anthropic client are explicit package boundaries under `ios/Packages/Networking/Sources/Networking/APIClient.swift`, `ios/Packages/HealthKitClient/Sources/HealthKitClient/HealthKitClient.swift`, and `ios/Packages/SupabaseClient/Sources/SupabaseClient/SupabaseClient.swift`.

#### Runtime and delivery
The web runtime is TypeScript/React on Next.js, deployable to Vercel and also containerized via `Dockerfile` with Fly configuration in `fly.toml`. CI includes web deployment and iOS/TestFlight workflows in `.github/workflows/deploy.yml` and `.github/workflows/ios-testflight.yml`. iOS release automation is Fastlane-based at `ios/fastlane/Fastfile`, with Supabase migration/operational scripts under `tools/supabase/`.

Main entry points: [`src/app/app/dashboard/dashboard-client.tsx`](https://github.com/rbarbieri13/fuelwell/blob/main/src/app/app/dashboard/dashboard-client.tsx) (Product workflows), [`src/app/api/coach/turn/route.ts`](https://github.com/rbarbieri13/fuelwell/blob/main/src/app/api/coach/turn/route.ts) (Coach turn API), [`src/lib/day-log-repository.ts`](https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/day-log-repository.ts) (Health repositories), [`ios/Features/Nutrition/Sources/Nutrition/DailyLogFeature.swift`](https://github.com/rbarbieri13/fuelwell/blob/main/ios/Features/Nutrition/Sources/Nutrition/DailyLogFeature.swift) (Nutrition workflows)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Next.js web"]
  node_web_entry["Web route entry<br/>Next.js routes<br/>[layout.tsx]"]
  node_web_shell["Authenticated shell<br/>layout &amp; navigation<br/>[layout.tsx]"]
  node_web_surfaces["Product workflows<br/>route clients"]
  node_coach_chat["Coach artifacts<br/>chat UI<br/>[index.tsx]"]
end

subgraph group_coach["Coach engine"]
  node_coach_turn{{"Coach turn API<br/>agent orchestrator<br/>[route.ts]"}}
  node_coach_policy["Prompt &amp; policy<br/>guardrails<br/>[system-prompt.ts]"]
  node_coach_provider["AI provider client<br/>provider boundary<br/>[provider-client.ts]"]
  node_coach_tools["Domain tools<br/>agent tools<br/>[index.ts]"]
  node_mutation_apply["Mutation applier<br/>effect boundary<br/>[apply-mutation.ts]"]
end

subgraph group_data["Supabase &amp; APIs"]
  node_health_apis{{"Health logging APIs<br/>route handlers<br/>[route.ts]"}}
  node_food_apis{{"Food acquisition APIs<br/>route handlers<br/>[route.ts]"}}
  node_repositories["Health repositories<br/>persistence layer"]
  node_supabase_adapters["Supabase adapters<br/>auth &amp; session<br/>[server.ts]"]
  node_supabase[("Supabase backend<br/>auth &amp; database")]
  node_migrations["Database migrations<br/>SQL schema"]
end

subgraph group_ios["iOS SwiftUI"]
  node_ios_entry["iOS app composition<br/>SwiftUI app<br/>[FuelWellApp.swift]"]
  node_ios_app["App routing &amp; tabs<br/>app feature<br/>[AppFeature.swift]"]
  node_ios_nutrition["Nutrition workflows<br/>feature package"]
  node_ios_domain["Nutrition domain<br/>calculation package"]
  node_ios_services["Native service clients<br/>platform packages<br/>[APIClient.swift]"]
end

subgraph group_delivery["Delivery"]
  node_delivery_node["Build &amp; release automation<br/>CI and deployment<br/>[deploy.yml]"]
end

node_web_entry -->|"authenticated routes"| node_web_shell
node_web_shell -->|"hosts"| node_web_surfaces
node_web_surfaces -->|"chat results"| node_coach_chat
node_coach_chat -->|"user turn"| node_coach_turn
node_web_surfaces -->|"logs health data"| node_health_apis
node_web_surfaces -->|"acquires food data"| node_food_apis
node_coach_turn -->|"builds context"| node_coach_policy
node_coach_turn -->|"requests generation"| node_coach_provider
node_coach_turn -->|"selects tools"| node_coach_tools
node_coach_tools -->|"proposed actions"| node_mutation_apply
node_mutation_apply -->|"persists effects"| node_repositories
node_health_apis -->|"uses"| node_repositories
node_food_apis -.->|"feeds tracking"| node_repositories
node_repositories -->|"accesses through"| node_supabase_adapters
node_supabase_adapters -->|"auth &amp; data"| node_supabase
node_migrations -->|"evolves schema"| node_supabase
node_ios_entry -->|"composes"| node_ios_app
node_ios_app -->|"routes to"| node_ios_nutrition
node_ios_nutrition -->|"calculates macros"| node_ios_domain
node_ios_nutrition -->|"uses"| node_ios_services
node_ios_services -->|"shared backend"| node_supabase
node_delivery_node -.->|"deploys web"| node_web_entry
node_delivery_node -.->|"releases iOS"| node_ios_entry

click node_web_entry "https://github.com/rbarbieri13/fuelwell/blob/main/src/app/layout.tsx"
click node_web_shell "https://github.com/rbarbieri13/fuelwell/blob/main/src/app/app/layout.tsx"
click node_web_surfaces "https://github.com/rbarbieri13/fuelwell/blob/main/src/app/app/dashboard/dashboard-client.tsx"
click node_coach_chat "https://github.com/rbarbieri13/fuelwell/blob/main/src/components/coach/artifacts/index.tsx"
click node_coach_turn "https://github.com/rbarbieri13/fuelwell/blob/main/src/app/api/coach/turn/route.ts"
click node_coach_policy "https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/coach/system-prompt.ts"
click node_coach_provider "https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/coach/provider-client.ts"
click node_coach_tools "https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/coach/tools/index.ts"
click node_mutation_apply "https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/coach/apply-mutation.ts"
click node_health_apis "https://github.com/rbarbieri13/fuelwell/blob/main/src/app/api/day-log/route.ts"
click node_food_apis "https://github.com/rbarbieri13/fuelwell/blob/main/src/app/api/food/barcode/route.ts"
click node_repositories "https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/day-log-repository.ts"
click node_supabase_adapters "https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/supabase/server.ts"
click node_migrations "https://github.com/rbarbieri13/fuelwell/blob/main/supabase/migrations/20260712200713_fitness_grocery_foundation.sql"
click node_ios_entry "https://github.com/rbarbieri13/fuelwell/blob/main/ios/FuelWellApp/Sources/FuelWellApp.swift"
click node_ios_app "https://github.com/rbarbieri13/fuelwell/blob/main/ios/Features/App/Sources/App/AppFeature.swift"
click node_ios_nutrition "https://github.com/rbarbieri13/fuelwell/blob/main/ios/Features/Nutrition/Sources/Nutrition/DailyLogFeature.swift"
click node_ios_domain "https://github.com/rbarbieri13/fuelwell/blob/main/ios/Packages/NutritionDomain/Sources/NutritionDomain/MacroDecisionEngine.swift"
click node_ios_services "https://github.com/rbarbieri13/fuelwell/blob/main/ios/Packages/Networking/Sources/Networking/APIClient.swift"
click node_delivery_node "https://github.com/rbarbieri13/fuelwell/blob/main/.github/workflows/deploy.yml"

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_web_entry,node_web_shell,node_web_surfaces,node_coach_chat toneBlue
class node_coach_turn,node_coach_policy,node_coach_provider,node_coach_tools,node_mutation_apply toneAmber
class node_health_apis,node_food_apis,node_repositories,node_supabase_adapters,node_supabase,node_migrations toneMint
class node_ios_entry,node_ios_app,node_ios_nutrition,node_ios_domain,node_ios_services toneRose
class node_delivery_node toneIndigo
```

## Components

### Next.js web

- [Web route entry](https://github.com/rbarbieri13/fuelwell/blob/main/src/app/layout.tsx): Next.js routes · Root, marketing, and login route groups. · `src/app/layout.tsx`
- [Authenticated shell](https://github.com/rbarbieri13/fuelwell/blob/main/src/app/app/layout.tsx): layout & navigation · Shared app layout, sidebar, and mobile navigation. · `src/app/app/layout.tsx`
- [Product workflows](https://github.com/rbarbieri13/fuelwell/blob/main/src/app/app/dashboard/dashboard-client.tsx): route clients · Dashboard, logging, review, meals, workouts, profile, and chat. · `src/app/app/dashboard/dashboard-client.tsx`
- [Coach artifacts](https://github.com/rbarbieri13/fuelwell/blob/main/src/components/coach/artifacts/index.tsx): chat UI · Renders structured coach results. · `src/components/coach/artifacts/index.tsx`

### Coach engine

- [Coach turn API](https://github.com/rbarbieri13/fuelwell/blob/main/src/app/api/coach/turn/route.ts): agent orchestrator · Coordinates context, execution, auditing, and response persistence. · `src/app/api/coach/turn/route.ts`
- [Prompt & policy](https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/coach/system-prompt.ts): guardrails · System context and voice filtering. · `src/lib/coach/system-prompt.ts`
- [AI provider client](https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/coach/provider-client.ts): provider boundary · `src/lib/coach/provider-client.ts`
- [Domain tools](https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/coach/tools/index.ts): agent tools · Meal and workout tool registry. · `src/lib/coach/tools/index.ts`
- [Mutation applier](https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/coach/apply-mutation.ts): effect boundary · Applies approved agent-proposed changes. · `src/lib/coach/apply-mutation.ts`

### Supabase & APIs

- [Health logging APIs](https://github.com/rbarbieri13/fuelwell/blob/main/src/app/api/day-log/route.ts): route handlers · Day, body, and workout endpoint boundaries. · `src/app/api/day-log/route.ts`
- [Food acquisition APIs](https://github.com/rbarbieri13/fuelwell/blob/main/src/app/api/food/barcode/route.ts): route handlers · Barcode, photo estimation, and nearby restaurants. · `src/app/api/food/barcode/route.ts`
- [Health repositories](https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/day-log-repository.ts): persistence layer · Day, body, and workout log repositories. · `src/lib/day-log-repository.ts`
- [Supabase adapters](https://github.com/rbarbieri13/fuelwell/blob/main/src/lib/supabase/server.ts): auth & session · Separated browser, server, and middleware adapters. · `src/lib/supabase/server.ts`
- **Supabase backend**: auth & database · Shared identity and product persistence.
- [Database migrations](https://github.com/rbarbieri13/fuelwell/blob/main/supabase/migrations/20260712200713_fitness_grocery_foundation.sql): SQL schema · Coach, preferences, fitness, grocery, and body-log schema. · `supabase/migrations/20260712200713_fitness_grocery_foundation.sql`

### iOS SwiftUI

- [iOS app composition](https://github.com/rbarbieri13/fuelwell/blob/main/ios/FuelWellApp/Sources/FuelWellApp.swift): SwiftUI app · Application entry and coordinator. · `ios/FuelWellApp/Sources/FuelWellApp.swift`
- [App routing & tabs](https://github.com/rbarbieri13/fuelwell/blob/main/ios/Features/App/Sources/App/AppFeature.swift): app feature · Root feature and tab navigation. · `ios/Features/App/Sources/App/AppFeature.swift`
- [Nutrition workflows](https://github.com/rbarbieri13/fuelwell/blob/main/ios/Features/Nutrition/Sources/Nutrition/DailyLogFeature.swift): feature package · Daily log and add-meal flows. · `ios/Features/Nutrition/Sources/Nutrition/DailyLogFeature.swift`
- [Nutrition domain](https://github.com/rbarbieri13/fuelwell/blob/main/ios/Packages/NutritionDomain/Sources/NutritionDomain/MacroDecisionEngine.swift): calculation package · Macro decision engine. · `ios/Packages/NutritionDomain/Sources/NutritionDomain/MacroDecisionEngine.swift`
- [Native service clients](https://github.com/rbarbieri13/fuelwell/blob/main/ios/Packages/Networking/Sources/Networking/APIClient.swift): platform packages · Networking, HealthKit, and Supabase access. · `ios/Packages/Networking/Sources/Networking/APIClient.swift`

### Delivery

- [Build & release automation](https://github.com/rbarbieri13/fuelwell/blob/main/.github/workflows/deploy.yml): CI and deployment · Vercel/Fly web deployment and TestFlight release workflows. · `.github/workflows/deploy.yml`

## Connections

- Web route entry → Authenticated shell: authenticated routes
- Authenticated shell → Product workflows: hosts
- Product workflows → Coach artifacts: chat results
- Coach artifacts → Coach turn API: user turn
- Product workflows → Health logging APIs: logs health data
- Product workflows → Food acquisition APIs: acquires food data
- Coach turn API → Prompt & policy: builds context
- Coach turn API → AI provider client: requests generation
- Coach turn API → Domain tools: selects tools
- Domain tools → Mutation applier: proposed actions
- Mutation applier → Health repositories: persists effects
- Health logging APIs → Health repositories: uses
- Food acquisition APIs → Health repositories: feeds tracking
- Health repositories → Supabase adapters: accesses through
- Supabase adapters → Supabase backend: auth & data
- Database migrations → Supabase backend: evolves schema
- iOS app composition → App routing & tabs: composes
- App routing & tabs → Nutrition workflows: routes to
- Nutrition workflows → Nutrition domain: calculates macros
- Nutrition workflows → Native service clients: uses
- Native service clients → Supabase backend: shared backend
- Build & release automation → Web route entry: deploys web
- Build & release automation → iOS app composition: releases iOS
