# 1-abdulrahman/ai-hospital-assistant architecture

> Architecture diagram of the GitHub repository 1-abdulrahman/ai-hospital-assistant, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/1-abdulrahman/ai-hospital-assistant
- Repository: https://github.com/1-abdulrahman/ai-hospital-assistant

## Overview

1. **Repo shape**
   - This is a multi-app monorepo with one Python backend and two React/TypeScript frontends.
   - The backend is the central service layer; the two UIs are separate clients for different users and workflows.

2. **Backend as the orchestration hub**
   - `company/backend/app/main.py` wires the API together.
   - The backend exposes route groups for `auth`, `otp`, `chat`, `scheduling`, `portal`, `integrations`, and `health`.
   - The structure suggests a REST API built around request/response schemas, middleware, dependency injection, and error handling.

3. **Primary business domains in the backend**
   - `modules/orchestration` coordinates multi-step flows and state transitions.
   - `modules/scheduling` handles provider/slot selection, booking logic, idempotency, and reason-code mappings.
   - `modules/otp` manages one-time-password verification and validation.
   - `modules/nlp` provides intent/specialty inference and also includes training/evaluation assets.
   - `modules/fhir_gateway` is the integration boundary to external healthcare systems using FHIR-style resources and mappings.
   - `modules/notification` sends email notifications.
   - `modules/observability` emits operational events/traces.

4. **Healthcare/chat workflow**
   - The `chat` API and orchestration modules likely drive a patient-facing assistant flow.
   - The hospital UI contains chat-specific components for consent, continuity, identity, OTP, and confirmation, which indicates a guided conversational booking/renewal experience.
   - The backend persists assistant session state and continuity context, so conversations are resumable and auditable.

5. **External system boundary: FHIR**
   - The `fhir_gateway` layer isolates all interaction with external hospital/EMR scheduling systems.
   - It includes client code, mappers, schema definitions, and seed data, which suggests the rest of the backend works with normalized internal models and only the gateway speaks FHIR externally.
   - Integration tests for native booking, slots, and schedule seeding reinforce that FHIR is a major backend dependency.

6. **Data storage and persistence**
   - `app/db/models.py`, `session.py`, `engine.py`, and `seed.py` indicate a relational database backing the backend.
   - Alembic migrations under `backend/alembic/versions` show schema evolution for assistant sessions, portal users, renewal fields, and related tables.
   - The DB appears to store operational application state, not just transactional scheduling data.

7. **Identity and access**
   - Backend auth-related routes and security helpers handle user access for the portal side.
   - OTP is part of the patient identity verification flow in the hospital chat experience.
   - `core/security.py`, `core/rate_limit.py`, and API middleware indicate the backend enforces cross-cutting protection and request control.

8. **Portal UI**
   - `company/portal-ui` is an admin/operations dashboard built with Vite, React, and TypeScript.
   - Pages like `Dashboard`, `Sessions`, `Bookings`, `Tenants`, `Traces`, `Audit`, and `NlpMonitoring` suggest it is used to inspect system state, monitor operations, and manage tenants rather than serve patients.
   - Its `api` layer includes real/mock clients and auth state, which implies it can run against live backend endpoints or a mocked mode.

9. **Hospital UI**
   - `hospital/ui` is the patient-facing front end.
   - It centers on a `ChatWidget` and step-based components for renewal/continuity identity, OTP, and confirmation.
   - It also includes health checks and booking-flow hooks, so the UI is tightly coupled to the backend conversation and scheduling APIs.

10. **NLP lifecycle**
   - The backend ships an NLP model artifact under `app/assets/nlp_models/distilbert-specialty`.
   - Training/evaluation scripts and a JSONL dataset are included, meaning model development is part of the repo, not an external dependency.
   - Tooling under `backend/tools` validates model artifacts and startup readiness, so deployment depends on the packaged NLP assets being present and consistent.

11. **Testing and quality gates**
   - There is broad coverage across unit, API, and integration tests in the backend.
   - The frontend also has component and example tests via Vitest.
   - `tools/ci/check_strict_coverage.py` and `.github/workflows/ci.yml` show CI enforces quality gates, likely including coverage and runtime checks.

12. **Local/dev and deployment topology**
   - Each app has its own Dockerfile, and there are compose files for combined local stacks.
   - Repository-level tools support backend-only local runs, a hybrid backend-with-hospital-UI mode, and offline dependency preparation.
   - This suggests the intended deployment model is a containerized backend with separately built frontends, connected through the backend API and external FHIR systems.

Main entry points: [`company/backend/app/api/routes`](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/api/routes) (Routes), [`company/backend/app/modules/orchestration`](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/orchestration) (Orchestration), [`company/backend/app/main.py`](https://github.com/1-abdulrahman/ai-hospital-assistant/blob/main/company/backend/app/main.py) (API app), [`company/backend/app/db`](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/db) (Database)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_backend_main["API app<br/>FastAPI entrypoint<br/>[main.py]"]
  node_backend_routes["Routes<br/>API surface"]
  node_backend_schemas["Schemas<br/>API contracts"]
  node_backend_core{{"Core<br/>cross-cutting"}}
  node_backend_db[("Database<br/>persistence")]
  node_backend_orch["Orchestration<br/>flow engine"]
  node_backend_sched["Scheduling<br/>booking logic"]
  node_backend_otp["OTP<br/>identity verify"]
  node_backend_nlp["NLP<br/>intent model"]
  node_backend_fhir["FHIR gateway<br/>integration boundary"]
  node_backend_notify["Notifications<br/>email delivery"]
  node_backend_obs["Observability<br/>telemetry"]
end

subgraph group_portal["Portal UI"]
  node_portal_app["Portal app<br/>React dashboard<br/>[main.tsx]"]
  node_portal_api["Portal API<br/>client layer"]
  node_portal_pages["Portal pages<br/>dashboard views"]
end

subgraph group_hospital["Hospital UI"]
  node_hospital_app["Hospital app<br/>React patient UI<br/>[main.tsx]"]
  node_hospital_chat["Chat widget<br/>conversation UI<br/>[ChatWidget.tsx]"]
  node_hospital_steps["Chat steps<br/>workflow UI"]
  node_hospital_hooks["Booking hooks<br/>state logic"]
end

node_backend_main -->|"registers"| node_backend_routes
node_backend_main -->|"uses"| node_backend_core
node_backend_main -->|"connects"| node_backend_db
node_backend_routes -->|"validates with"| node_backend_schemas
node_backend_routes -->|"calls"| node_backend_otp
node_backend_routes -->|"calls"| node_backend_sched
node_backend_routes -->|"calls"| node_backend_orch
node_backend_routes -->|"integrates via"| node_backend_fhir
node_backend_orch -->|"infers"| node_backend_nlp
node_backend_orch -->|"drives"| node_backend_sched
node_backend_orch -->|"verifies"| node_backend_otp
node_backend_sched -->|"books through"| node_backend_fhir
node_backend_sched -->|"persists"| node_backend_db
node_backend_nlp -.->|"stores training data"| node_backend_db
node_backend_fhir -->|"reads seed/state"| node_backend_db
node_backend_notify -->|"reads recipients"| node_backend_db
node_backend_obs -->|"hooks into"| node_backend_core
node_portal_app -->|"uses"| node_portal_api
node_portal_app -->|"renders"| node_portal_pages
node_portal_api -->|"calls"| node_backend_main
node_hospital_app -->|"contains"| node_hospital_chat
node_hospital_app -->|"renders"| node_hospital_steps
node_hospital_app -->|"uses"| node_hospital_hooks
node_hospital_hooks -->|"calls"| node_backend_main
node_hospital_chat -->|"drives"| node_backend_orch

click node_backend_main "https://github.com/1-abdulrahman/ai-hospital-assistant/blob/main/company/backend/app/main.py"
click node_backend_routes "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/api/routes"
click node_backend_schemas "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/api/schemas"
click node_backend_core "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/core"
click node_backend_db "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/db"
click node_backend_orch "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/orchestration"
click node_backend_sched "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/scheduling"
click node_backend_otp "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/otp"
click node_backend_nlp "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/nlp"
click node_backend_fhir "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/fhir_gateway"
click node_backend_notify "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/notification"
click node_backend_obs "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/observability"
click node_portal_app "https://github.com/1-abdulrahman/ai-hospital-assistant/blob/main/company/portal-ui/src/main.tsx"
click node_portal_api "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/portal-ui/src/api"
click node_portal_pages "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/portal-ui/src/pages"
click node_hospital_app "https://github.com/1-abdulrahman/ai-hospital-assistant/blob/main/hospital/ui/src/main.tsx"
click node_hospital_chat "https://github.com/1-abdulrahman/ai-hospital-assistant/blob/main/hospital/ui/src/components/ChatWidget.tsx"
click node_hospital_steps "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/hospital/ui/src/components/steps"
click node_hospital_hooks "https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/hospital/ui/src/hooks"

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_backend_main,node_backend_routes,node_backend_schemas,node_backend_core,node_backend_db,node_backend_orch,node_backend_sched,node_backend_otp,node_backend_nlp,node_backend_fhir,node_backend_notify,node_backend_obs toneBlue
class node_portal_app,node_portal_api,node_portal_pages toneAmber
class node_hospital_app,node_hospital_chat,node_hospital_steps,node_hospital_hooks toneMint
```

## Components

### Backend

Python API, domain modules, persistence, and integrations

- [API app](https://github.com/1-abdulrahman/ai-hospital-assistant/blob/main/company/backend/app/main.py): FastAPI entrypoint · Wires routes, middleware, dependencies, and error handling. · `company/backend/app/main.py`
- [Routes](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/api/routes): API surface · Auth, OTP, chat, scheduling, portal, integrations, and health endpoints. · `company/backend/app/api/routes`
- [Schemas](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/api/schemas): API contracts · Request and response models for core backend APIs. · `company/backend/app/api/schemas`
- [Core](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/core): cross-cutting · Config, security, rate limiting, logging, and correlation context. · `company/backend/app/core`
- [Database](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/db): persistence · Relational models, engine, session management, seed data, and migrations. · `company/backend/app/db`
- [Orchestration](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/orchestration): flow engine · Coordinates multi-step assistant flows and state transitions. · `company/backend/app/modules/orchestration`
- [Scheduling](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/scheduling): booking logic · Provider/slot selection, booking rules, idempotency, and reason-code mapping. · `company/backend/app/modules/scheduling`
- [OTP](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/otp): identity verify · One-time-password validation and verification service. · `company/backend/app/modules/otp`
- [NLP](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/nlp): intent model · Specialty/intent inference plus training and evaluation assets. · `company/backend/app/modules/nlp`
- [FHIR gateway](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/fhir_gateway): integration boundary · Normalizes external EMR/FHIR interaction through clients, mappers, schemas, and seeds. · `company/backend/app/modules/fhir_gateway`
- [Notifications](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/notification): email delivery · Sends email notifications from backend workflows. · `company/backend/app/modules/notification`
- [Observability](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/backend/app/modules/observability): telemetry · Emits operational events and traces for monitoring. · `company/backend/app/modules/observability`

### Portal UI

Operations dashboard for monitoring and management

- [Portal app](https://github.com/1-abdulrahman/ai-hospital-assistant/blob/main/company/portal-ui/src/main.tsx): React dashboard · Vite/React/TypeScript admin console for operations and monitoring. · `company/portal-ui/src/main.tsx`
- [Portal API](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/portal-ui/src/api): client layer · Real and mock backend clients plus auth state management. · `company/portal-ui/src/api`
- [Portal pages](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/company/portal-ui/src/pages): dashboard views · Sessions, bookings, tenants, traces, audit, and NLP monitoring screens. · `company/portal-ui/src/pages`

### Hospital UI

Patient-facing chat and booking experience

- [Hospital app](https://github.com/1-abdulrahman/ai-hospital-assistant/blob/main/hospital/ui/src/main.tsx): React patient UI · Patient-facing chat and booking front end. · `hospital/ui/src/main.tsx`
- [Chat widget](https://github.com/1-abdulrahman/ai-hospital-assistant/blob/main/hospital/ui/src/components/ChatWidget.tsx): conversation UI · Guided assistant interface for consent, continuity, identity, OTP, and confirmation. · `hospital/ui/src/components/ChatWidget.tsx`
- [Chat steps](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/hospital/ui/src/components/steps): workflow UI · Step-based renewal and continuity identity, OTP, and confirm screens. · `hospital/ui/src/components/steps`
- [Booking hooks](https://github.com/1-abdulrahman/ai-hospital-assistant/tree/main/hospital/ui/src/hooks): state logic · Widget state, booking flow, API client, and health-check hooks. · `hospital/ui/src/hooks`

## Connections

- API app → Routes: registers · Application startup attaches route groups.
- API app → Core: uses · Applies config, logging, security, and middleware.
- API app → Database: connects · Initializes persistence layer and session handling.
- Routes → Schemas: validates with · Routes exchange typed request/response contracts.
- Routes → OTP: calls · OTP endpoints delegate to verification logic.
- Routes → Scheduling: calls · Scheduling endpoints delegate to booking logic.
- Routes → Orchestration: calls · Chat and assistant flows are coordinated here.
- Routes → FHIR gateway: integrates via · Integration routes expose external-system interactions.
- Orchestration → NLP: infers · Uses NLP output for intent and specialty decisions.
- Orchestration → Scheduling: drives · Orchestration advances booking and slot-selection steps.
- Orchestration → OTP: verifies · Identity checks gate workflow progress.
- Scheduling → FHIR gateway: books through · Scheduling requests are translated to external FHIR operations.
- Scheduling → Database: persists · Stores booking state, idempotency, and related records.
- NLP → Database: stores training data · Model lifecycle uses local datasets and artifacts.
- FHIR gateway → Database: reads seed/state · Gateway relies on seeded mappings and stored normalized state.
- Notifications → Database: reads recipients · Notification delivery uses persisted application data.
- Observability → Core: hooks into · Telemetry is part of the request/runtime infrastructure.
- Portal app → Portal API: uses · UI relies on the client layer for backend access.
- Portal app → Portal pages: renders · Dashboard pages compose the visible admin workflows.
- Portal API → API app: calls · Portal client talks to backend APIs.
- Hospital app → Chat widget: contains · Main patient experience centers on the chat widget.
- Hospital app → Chat steps: renders · Workflow steps implement guided conversational screens.
- Hospital app → Booking hooks: uses · UI state and backend interaction are driven by hooks.
- Booking hooks → API app: calls · Patient UI exchanges data with backend APIs.
- Chat widget → Orchestration: drives · Conversation UI maps to orchestration-managed workflows.
