# openhealthforall/open-health architecture

> Architecture diagram of the GitHub repository openhealthforall/open-health, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openhealthforall/open-health
- Repository: https://github.com/openhealthforall/open-health

## Overview

##### Project Type and Purpose
- OpenHealth is an AI Health Assistant web application built on Next.js with a heavy emphasis on processing personal health data.
- The system is designed to consolidate health data, parse it intelligently, and leverage various language models (e.g., GPT, Ollama, LLaMA, etc.) for interactive health guidance.
- The project is containerized (using Docker/Docker Compose) and uses Prisma as its database/ORM layer, which hints at a full-stack architecture.

##### Analyzing the File Structure
- Top-Level Overview:
- Configuration & Containerization: Files such as Dockerfile, docker-compose.yaml, .env.example, and various GitHub configuration files.
- Internationalization: The i18n folder holds multi-language README variants.
- Application & API Routes: The src/app folder is organized following Next.js’s file system routing with separate directories for pages, modals, and API endpoints (e.g., /api/chat-rooms, /api/assistant-modes, /api/health-data, etc.).
- Business Logic & Utility Libraries: Under src/lib, you see dedicated modules for handling date-time (dayjs), health data parsing (including both document and vision pipelines), and general utilities.
- Database Layer: The prisma folder (with schema.prisma and seed scripts) indicates the use of Prisma for data modeling and database interactions.
- Architectural Clues:
- The API routing inside src/app/api signifies a REST or possibly RPC-like approach using Next.js.
- There’s a clear separation between user interface components (src/components, src/app) and service/business logic layers (src/lib, src/app/api), suggesting modularity.

##### Key Components
a. Frontend (User Interface)
- Next.js-based UI found in src/app (layout, pages, modals) and the React components (in src/components).
- Dynamic routes for chat rooms, source management, etc.
b. API Layer / Backend Services
- Routes under src/app/api for handling various data operations:
▸ Assistant Modes (src/app/api/assistant-modes)
▸ Chat Rooms and Messages (src/app/api/chat-rooms)
▸ Multiple health data endpoints (health-data, health-data-parser, llm-providers)
c. Data Processing & Parsing Engine
- In the src/lib/health-data/parser folder, show the specialized modules for parsing both document and vision inputs.
- Current use of an external Python server for parsing (as mentioned in the README), with plans to merge into TypeScript code later.
d. Database Layer
- Prisma setup (prisma/schema.prisma, seed data, etc.) which interacts with the API.
e. External Services / Language Model Integrations
- Local LLM integration capabilities (via Ollama and other supported models such as GPT, DeepSeek-V3, etc.).
f. Containerization & Deployment
- Docker and Docker Compose as orchestration layers that containerize the entire environment.
- Next.js’s server component and environment configuration (.env and related Docker configurations).

##### Component Relationships and Interactions
- From the UI, illustrate user interactions that trigger HTTP requests to the API endpoints.
- The API layer acts as a bridge – retrieving input data and then interacting with:
- The health data parsing engine for transforming raw health data into structured information.
- The language model integration (LLM Processing) which uses both local and external AI models.
- Processed and structured data is stored or retrieved via the database (Prisma) and then delivered back to the UI.
- Optionally, add interactions with underlying Docker containers and configuration management.

##### Architectural Patterns & Design Principles
- Modular Design: Emphasize separation of concerns – clear boundaries between UI, API, processing logic, and data persistence.
- Service-Oriented/Modular Monolith: Although built within a Next.js application, the API structure suggests individual services (such as chat, assistant modes, health data management) that are modular.
- Containerization: Show how Docker composes the various parts.
- Internationalization (i18n): An auxiliary module that allows language support.

##### Final Recommendations for the Principal Software Engineer
- Interactions: how health data is ingested, processed (parsed), and used in AI-driven conversations.
- Because the project integrates multiple technologies (Next.js, TypeScript, Prisma, Docker, external AI APIs), ensure that each technology’s role is clearly annotated.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global User
    U("User"):::external

    %% Frontend Layer
    subgraph "Frontend Layer"
        FE("User Interface (Next.js)"):::frontend
        ChatUI("Chat Interface"):::frontend
        UI("Reusable UI Components"):::frontend
        UICompUI("UI Components (ui)"):::frontend
        I18("Internationalization (i18n Support)"):::frontend
    end

    %% API Layer
    subgraph "API Layer"
        API("Backend API Endpoints"):::api
        ChatAPI("Chat Functionality (API)"):::api
        HealthData("Health Data Input API"):::api
        subgraph "Health Data Parsing"
            PD("Documents Parser (API)"):::api
            PLD("Documents Parser (Library)"):::api
            PV("Visions Parser (API)"):::api
            PLV("Visions Parser (Library)"):::api
        end
        LLM("LLM Providers (AI Integration)"):::api
    end

    %% Database & Deployment
    subgraph "Database & Deployment"
        DB1("Prisma Schema"):::db
        DB2("Prisma Seed"):::db
        Docker("Docker Deployment"):::deployment
    end

    %% External Services
    subgraph "External Services"
        Ext("External AI Services (Upstage,OpenAI,Ollama)"):::external
    end

    %% Data Flow
    U -->|"initiates"| FE
    FE -->|"sends API calls"| API
    FE -->|"triggers chat"| ChatUI
    ChatUI -->|"sends chat request"| ChatAPI

    API -->|"handles chat"| ChatAPI
    API -->|"receives health data"| HealthData

    HealthData -->|"invokes"| PD
    HealthData -->|"invokes"| PV

    PD -->|"saves parsed docs"| DB1
    PLD -->|"provides lib support"| DB1
    PV -->|"saves parsed visions"| DB1
    PLV -->|"provides lib support"| DB1

    ChatAPI -->|"stores chat data"| DB1

    LLM -->|"retrieves data"| DB1
    DB1 -->|"data for analysis"| LLM
    LLM -->|"calls"| Ext
    Ext -->|"returns response"| LLM

    LLM -->|"sends AI response"| API
    API -->|"responds to"| FE

    %% Link UI Components within Frontend
    UI -->|"composes views"| UICompUI

    %% Deployment relation (conceptual)
    API --- Docker
    DB1 --- Docker
    DB2 --- Docker

    %% Click Events
    click FE "https://github.com/openhealthforall/open-health/tree/main/src/app"
    click API "https://github.com/openhealthforall/open-health/tree/main/src/app/api"
    click ChatAPI "https://github.com/openhealthforall/open-health/tree/main/src/app/api/chat-rooms"
    click ChatUI "https://github.com/openhealthforall/open-health/tree/main/src/app/chat"
    click HealthData "https://github.com/openhealthforall/open-health/tree/main/src/app/api/health-data"
    click PD "https://github.com/openhealthforall/open-health/tree/main/src/app/api/health-data-parser/documents"
    click PLD "https://github.com/openhealthforall/open-health/tree/main/src/lib/health-data/parser/document"
    click PV "https://github.com/openhealthforall/open-health/tree/main/src/app/api/health-data-parser/visions"
    click PLV "https://github.com/openhealthforall/open-health/tree/main/src/lib/health-data/parser/vision"
    click LLM "https://github.com/openhealthforall/open-health/tree/main/src/app/api/llm-providers"
    click DB1 "https://github.com/openhealthforall/open-health/blob/main/prisma/schema.prisma"
    click DB2 "https://github.com/openhealthforall/open-health/blob/main/prisma/seed.ts"
    click I18 "https://github.com/openhealthforall/open-health/tree/main/i18n"
    click UI "https://github.com/openhealthforall/open-health/tree/main/src/components"
    click UICompUI "https://github.com/openhealthforall/open-health/tree/main/src/components/ui"

    %% Styles
    classDef frontend fill:#cfe2f3,stroke:#036,stroke-width:2px;
    classDef api fill:#f9d6d5,stroke:#b02a37,stroke-width:2px;
    classDef db fill:#e7e6e1,stroke:#797b80,stroke-width:2px,stroke-dasharray: 5 5;
    classDef external fill:#d9ead3,stroke:#6aa84f,stroke-width:2px;
    classDef deployment fill:#fff2cc,stroke:#bf9000,stroke-width:2px,stroke-dasharray: 3 3;

    class FE,ChatUI,UI,UICompUI,I18 frontend;
    class API,ChatAPI,HealthData,PD,PLD,PV,PLV,LLM api;
    class DB1,DB2 db;
    class Ext external;
    class Docker deployment;
    class U external;
```
