# adarshs14193/patient-zero architecture

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

- Interactive diagram: https://gitdiagram.com/adarshs14193/patient-zero
- Repository: https://github.com/adarshs14193/patient-zero

## Overview

1. **High-level shape**
   This repository is a three-part system: a React/Vite frontend, an Express/Node backend API, and a separate `rag/` AI pipeline package. The product centers on medical bill ingestion, AI audit, dispute generation, and automated dispute deployment.

2. **Frontend boundary**
   The `frontend/` app is the user-facing dashboard and onboarding surface. It uses React Router for page-level navigation (`Landing`, `Auth`, `Dashboard`, `LegacyDashboard`) and is styled with Tailwind. The dashboard is broken into reusable UI modules such as sidebar, insurance vault, upload bar, settings, and status/toast components.

3. **Dashboard composition**
   Inside `frontend/src/components/dashboard/`, the UI is organized around operational workflows rather than generic pages: insurance policy management, upload/scan actions, theme/state providers, and account/status surfaces. The `views/` folder suggests the main dashboard is tabbed or sectioned into areas like home, bill history, and other proceedings.

4. **Client-side data access**
   The frontend talks to the backend API and also to Supabase directly for auth/state synchronization. `frontend/src/lib/supabase.js` and the presence of realtime-oriented components indicate the UI listens for backend-driven updates, so a dispute can progress without manual refresh.

5. **Backend API layer**
   The `server/` app is the orchestration layer. `server/index.js` bootstraps Express, while `server/src/api/` exposes domain routes for authentication, bills, and insurance. `authGuard.js` indicates protected endpoints, and `passport.js` suggests Google OAuth-based login or session setup.

6. **Core backend domain flow**
   The `bills` API is the main workflow entry point: manual upload, Gmail scan, dispute deployment, and retrieval of compiled “arena” data. `disputeController.js` likely coordinates the bill-to-dispute lifecycle, while route handlers delegate to services rather than doing processing inline.

7. **AI and document-processing services**
   The backend service layer separates responsibilities: `aiExtractionService` handles bill/document extraction, `aiHelper` supports model interaction, `auditService` compiles discrepancies and notes, and `ragService` bridges to the retrieval-augmented generation logic. This is the AI analysis boundary where raw bill data becomes structured findings and a draft letter.

8. **Autopilot and external integrations**
   `gmailService.js` scans incoming mail, `autoPilotService.js` likely coordinates the automated pipeline, and `agentService.js` runs the dispute agent. The agent path has a primary automation channel and fallback behavior, matching the README’s Playwright-first / Twilio-fallback design. These services are the main integration boundary to third-party systems and hospital portals.

9. **Authentication, policy, and secure storage**
   `server/src/config/supabase.js` connects the backend to Supabase, which acts as the persistent store and realtime backend. `utilis/crypto.js` implies sensitive insurance credentials are encrypted before storage, supporting the insurance vault feature in the UI.

10. **Separate RAG package**
    The top-level `rag/` directory is its own Node package with scripts and knowledge-base assets (`knowledge_base.json`, test bill data, audit/matching scripts). This looks like an isolated AI/RAG toolkit or experimental inference pipeline that supports the backend’s `ragService`/audit flow rather than being purely frontend or backend code.

11. **Data lifecycle**
    The main lifecycle is: user logs in → uploads a bill or triggers inbox scan → backend ingests PDF/email data → AI extraction and RAG audit run against bill + policy context → dispute letter and findings are stored/returned → UI updates in realtime → user can deploy the agent to hospital channels. That is the core architecture diagram path.

12. **External systems that matter architecturally**
    Supabase is the persistence and realtime spine; Gmail API is the autonomous ingestion source; Gemini is the reasoning/extraction engine; Playwright and Twilio are the outbound enforcement channels; hospital billing portals are the final external target. These integrations define the system boundaries more than any internal framework choice.

13. **Key repo-level note**
    There is a naming mismatch between the README and the tree: the frontend directory is named `frontend/` in the repo, not `client/`. Architecturally, though, it still plays the same role as the client application.

14. **What to diagram**
    For an accurate architecture diagram, draw three main zones: browser UI, backend orchestration/API, and AI/RAG/external services. Show Supabase as shared state/storage/realtime, with separate arrows to Gmail, Gemini, Playwright/Twilio, and hospital portals from the backend services.

Main entry points: [`frontend/src/pages/Dashboard.jsx`](https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/pages/Dashboard.jsx) (Dashboard), [`server/src/controllers/disputeController.js`](https://github.com/adarshs14193/patient-zero/blob/main/server/src/controllers/disputeController.js) (Dispute Ctrl), [`server/src/services/agentService.js`](https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/agentService.js) (Dispute Agent), [`server/index.js`](https://github.com/adarshs14193/patient-zero/blob/main/server/index.js) (Express App)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_landing["Landing<br/>page<br/>[Landing.jsx]"]
  node_auth["Auth<br/>page<br/>[Auth.jsx]"]
  node_dashboard["Dashboard<br/>page<br/>[Dashboard.jsx]"]
  node_views["Views<br/>sections<br/>[HomeView.jsx]"]
  node_sidebar["Sidebar<br/>navigation<br/>[Sidebar.jsx]"]
  node_vault["Insurance Vault<br/>insurance ui<br/>[InsuranceVault.jsx]"]
  node_uploadbar["Upload Bar<br/>workflow ui"]
  node_supabase_client["Supabase Client<br/>client db<br/>[supabase.js]"]
end

subgraph group_backend["Backend"]
  node_server["Express App<br/>api host<br/>[index.js]"]
  node_auth_api["Auth API<br/>route<br/>[auth.js]"]
  node_bills_api["Bills API<br/>route<br/>[bills.js]"]
  node_insurance_api["Insurance API<br/>route<br/>[insurance.js]"]
  node_dispute_ctrl["Dispute Ctrl<br/>controller"]
  node_ai_services["AI Services<br/>analysis"]
  node_rag_bridge["RAG Bridge<br/>[ragService.js]"]
  node_autopilot["Autopilot<br/>automation"]
  node_gmail["Gmail Scan<br/>integration<br/>[gmailService.js]"]
  node_agent["Dispute Agent<br/>integration<br/>[agentService.js]"]
  node_supabase_cfg["Supabase Sync<br/>backend db<br/>[supabase.js]"]
  node_crypto["Crypto<br/>security<br/>[crypto.js]"]
end

subgraph group_rag["RAG"]
  node_rag_chain["RAG Chain<br/>pipeline<br/>[rag_chain.js]"]
  node_unified_audit["Unified Audit<br/>[unifiedAudit.js]"]
  node_kb[("Knowledge Base<br/>data")]
end

node_supabase_ext[("Supabase<br/>external platform")]
node_gemini(("Gemini<br/>external model"))
node_playwright["Playwright<br/>external automation"]
node_twilio["Twilio<br/>external fallback"]
node_gmail_api["Gmail API<br/>external source"]
node_portal["Hospital Portals<br/>external target"]

node_landing -->|"entry"| node_auth
node_auth -->|"login"| node_supabase_client
node_dashboard -->|"renders"| node_views
node_dashboard -->|"navigates"| node_sidebar
node_dashboard -->|"manages"| node_vault
node_dashboard -->|"scans"| node_uploadbar
node_dashboard -->|"syncs"| node_supabase_client
node_supabase_client -->|"connects"| node_supabase_ext
node_server -->|"routes"| node_auth_api
node_server -->|"routes"| node_bills_api
node_server -->|"routes"| node_insurance_api
node_auth_api -->|"uses"| node_supabase_cfg
node_bills_api -->|"delegates"| node_dispute_ctrl
node_insurance_api -->|"protects"| node_crypto
node_dispute_ctrl -->|"analyzes"| node_ai_services
node_dispute_ctrl -->|"augments"| node_rag_bridge
node_dispute_ctrl -->|"orchestrates"| node_autopilot
node_autopilot -->|"ingests"| node_gmail
node_gmail -->|"reads"| node_gmail_api
node_ai_services -->|"calls"| node_gemini
node_rag_bridge -->|"invokes"| node_rag_chain
node_rag_chain -->|"runs"| node_unified_audit
node_unified_audit -->|"consults"| node_kb
node_autopilot -->|"launches"| node_agent
node_agent -->|"primary"| node_playwright
node_agent -->|"fallback"| node_twilio
node_agent -->|"submits"| node_portal
node_supabase_cfg -->|"stores"| node_supabase_ext

click node_landing "https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/pages/Landing.jsx"
click node_auth "https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/pages/Auth.jsx"
click node_dashboard "https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/pages/Dashboard.jsx"
click node_views "https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/views/HomeView.jsx"
click node_sidebar "https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/components/dashboard/Sidebar.jsx"
click node_vault "https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/components/dashboard/InsuranceVault.jsx"
click node_uploadbar "https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/components/dashboard/FloatingUploadBar.jsx"
click node_supabase_client "https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/lib/supabase.js"
click node_server "https://github.com/adarshs14193/patient-zero/blob/main/server/index.js"
click node_auth_api "https://github.com/adarshs14193/patient-zero/blob/main/server/src/api/auth.js"
click node_bills_api "https://github.com/adarshs14193/patient-zero/blob/main/server/src/api/bills.js"
click node_insurance_api "https://github.com/adarshs14193/patient-zero/blob/main/server/src/api/insurance.js"
click node_dispute_ctrl "https://github.com/adarshs14193/patient-zero/blob/main/server/src/controllers/disputeController.js"
click node_ai_services "https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/aiExtractionService.js"
click node_rag_bridge "https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/ragService.js"
click node_autopilot "https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/autoPilotService.js"
click node_gmail "https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/gmailService.js"
click node_agent "https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/agentService.js"
click node_supabase_cfg "https://github.com/adarshs14193/patient-zero/blob/main/server/src/config/supabase.js"
click node_crypto "https://github.com/adarshs14193/patient-zero/blob/main/server/src/utilis/crypto.js"
click node_rag_chain "https://github.com/adarshs14193/patient-zero/blob/main/rag/rag_chain.js"
click node_unified_audit "https://github.com/adarshs14193/patient-zero/blob/main/rag/unifiedAudit.js"
click node_kb "https://github.com/adarshs14193/patient-zero/blob/main/rag/knowledge_base.json"

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_landing,node_auth,node_dashboard,node_views,node_sidebar,node_vault,node_uploadbar,node_supabase_client toneBlue
class node_server,node_auth_api,node_bills_api,node_insurance_api,node_dispute_ctrl,node_ai_services,node_rag_bridge,node_autopilot,node_gmail,node_agent,node_supabase_cfg,node_crypto toneAmber
class node_rag_chain,node_unified_audit,node_kb toneMint
class node_supabase_ext,node_gemini,node_playwright,node_twilio,node_gmail_api,node_portal toneNeutral
```

## Components

### Frontend

React/Vite client for onboarding and operational dashboard

- [Landing](https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/pages/Landing.jsx): page · Public marketing entry and auth entrypoint · `frontend/src/pages/Landing.jsx`
- [Auth](https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/pages/Auth.jsx): page · Login and onboarding flow · `frontend/src/pages/Auth.jsx`
- [Dashboard](https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/pages/Dashboard.jsx): page · Primary authenticated shell · `frontend/src/pages/Dashboard.jsx`
- [Views](https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/views/HomeView.jsx): sections · Dashboard sections for home, history, and proceedings · `frontend/src/views/HomeView.jsx`
- [Sidebar](https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/components/dashboard/Sidebar.jsx): navigation · Primary dashboard navigation · `frontend/src/components/dashboard/Sidebar.jsx`
- [Insurance Vault](https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/components/dashboard/InsuranceVault.jsx): insurance ui · Policy and credential management surface · `frontend/src/components/dashboard/InsuranceVault.jsx`
- [Upload Bar](https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/components/dashboard/FloatingUploadBar.jsx): workflow ui · Bill upload and scan actions · `frontend/src/components/dashboard/FloatingUploadBar.jsx`
- [Supabase Client](https://github.com/adarshs14193/patient-zero/blob/main/frontend/src/lib/supabase.js): client db · Frontend auth and realtime state access · `frontend/src/lib/supabase.js`

### Backend

Express API, orchestration, and domain services

- [Express App](https://github.com/adarshs14193/patient-zero/blob/main/server/index.js): api host · API bootstrap and request routing · `server/index.js`
- [Auth API](https://github.com/adarshs14193/patient-zero/blob/main/server/src/api/auth.js): route · Authentication and session endpoints · `server/src/api/auth.js`
- [Bills API](https://github.com/adarshs14193/patient-zero/blob/main/server/src/api/bills.js): route · Bill ingestion and dispute endpoints · `server/src/api/bills.js`
- [Insurance API](https://github.com/adarshs14193/patient-zero/blob/main/server/src/api/insurance.js): route · Insurance policy endpoints · `server/src/api/insurance.js`
- [Dispute Ctrl](https://github.com/adarshs14193/patient-zero/blob/main/server/src/controllers/disputeController.js): controller · Coordinates bill-to-dispute lifecycle · `server/src/controllers/disputeController.js`
- [AI Services](https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/aiExtractionService.js): analysis · Extraction, audit, and model helper layer · `server/src/services/aiExtractionService.js`
- [RAG Bridge](https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/ragService.js): service · Backend bridge into retrieval and matching logic · `server/src/services/ragService.js`
- [Autopilot](https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/autoPilotService.js): automation · Automation coordinator and agent routing · `server/src/services/autoPilotService.js`
- [Gmail Scan](https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/gmailService.js): integration · Inbox ingestion for bill discovery · `server/src/services/gmailService.js`
- [Dispute Agent](https://github.com/adarshs14193/patient-zero/blob/main/server/src/services/agentService.js): integration · Outbound deployment agent for hospital portals · `server/src/services/agentService.js`
- [Supabase Sync](https://github.com/adarshs14193/patient-zero/blob/main/server/src/config/supabase.js): backend db · Persistent state and realtime backend connection · `server/src/config/supabase.js`
- [Crypto](https://github.com/adarshs14193/patient-zero/blob/main/server/src/utilis/crypto.js): security · Sensitive credential encryption · `server/src/utilis/crypto.js`

### RAG

Standalone retrieval and audit toolkit

- [RAG Chain](https://github.com/adarshs14193/patient-zero/blob/main/rag/rag_chain.js): pipeline · Standalone retrieval and generation pipeline · `rag/rag_chain.js`
- [Unified Audit](https://github.com/adarshs14193/patient-zero/blob/main/rag/unifiedAudit.js): audit · Audit and matching workflow for bills and policy context · `rag/unifiedAudit.js`
- [Knowledge Base](https://github.com/adarshs14193/patient-zero/blob/main/rag/knowledge_base.json): data · RAG reference corpus and sample bill data · `rag/knowledge_base.json`

### Other components

- **Supabase**: external platform · Persistence and realtime spine
- **Gemini**: external model · Reasoning and extraction engine
- **Playwright**: external automation · Primary portal automation channel
- **Twilio**: external fallback · Fallback outbound channel
- **Gmail API**: external source · Email ingestion source
- **Hospital Portals**: external target · Final dispute destination

## Connections

- Landing → Auth: entry · Users move from public landing into auth flow
- Auth → Supabase Client: login · Client authenticates and syncs state
- Dashboard → Views: renders · Dashboard composes the sectioned operational views
- Dashboard → Sidebar: navigates · Dashboard shell uses sidebar navigation
- Dashboard → Insurance Vault: manages · Insurance policy data is managed in the vault UI
- Dashboard → Upload Bar: scans · Upload bar starts bill ingestion and scanning
- Dashboard → Supabase Client: syncs · Dashboard listens for realtime backend state
- Supabase Client → Supabase: connects · Frontend talks to Supabase directly
- Express App → Auth API: routes · Express mounts authentication routes
- Express App → Bills API: routes · Express mounts bill workflow routes
- Express App → Insurance API: routes · Express mounts insurance routes
- Auth API → Supabase Sync: uses · Auth endpoints persist and read shared state
- Bills API → Dispute Ctrl: delegates · Bill requests are coordinated by the dispute controller
- Insurance API → Crypto: protects · Insurance credentials are encrypted before storage
- Dispute Ctrl → AI Services: analyzes · Controller invokes extraction and audit services
- Dispute Ctrl → RAG Bridge: augments · Controller enriches disputes with RAG context
- Dispute Ctrl → Autopilot: orchestrates · Controller hands off automation execution
- Autopilot → Gmail Scan: ingests · Autopilot can trigger inbox scans
- Gmail Scan → Gmail API: reads · Email scanning uses Gmail as source
- AI Services → Gemini: calls · Extraction and analysis rely on Gemini
- RAG Bridge → RAG Chain: invokes · Backend bridges to the local RAG pipeline
- RAG Chain → Unified Audit: runs · Pipeline produces audit and matching outputs
- Unified Audit → Knowledge Base: consults · Audit logic uses the knowledge base and samples
- Autopilot → Dispute Agent: launches · Automation service starts the dispute agent
- Dispute Agent → Playwright: primary · Primary portal automation uses Playwright
- Dispute Agent → Twilio: fallback · Fallback channel uses Twilio
- Dispute Agent → Hospital Portals: submits · Agent deploys disputes to hospital portals
- Supabase Sync → Supabase: stores · Backend persists system state in Supabase
