# ai4os/ai4-papi architecture

> Architecture diagram of the GitHub repository ai4os/ai4-papi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ai4os/ai4-papi
- Repository: https://github.com/ai4os/ai4-papi

## Overview

##### Main Components
- API Server (FastAPI): At the heart is the FastAPI application (see ai4papi/main.py) that routes incoming HTTP requests.
- Routers / Endpoints: The project organizes its endpoints in a versioned manner under ai4papi/routers/v1. These include:
- Catalog – for metadata access to modules/tools
- Deployments – for launching and managing module deployments (secured endpoints)
- Inference – for inference-related endpoints
- Try_me – for demo deployments
- Stats and others (e.g., secrets, storage)
- Authentication Module: A dedicated auth.py in the package handles OIDC token validation and related authentication flows.
- Integration with External Services:
- Nomad – used for scheduling and deploying jobs (see ai4papi/nomad and configuration files in etc/nomad-related hcl)
- OSCAR – hinted at in the README, representing decoupled underlying tooling.
- Configuration Management:
- The etc folder holds YAML and HCL configuration files (main_conf.yaml, module and tool configurations) that drive the application's behavior.
- Deployment & DevOps Infrastructure:
- Docker-related files, docker-compose, and GitHub workflows (in .github/workflows) indicate containerization and CI/CD practices.
- Testing & Quality Assurance:
- A dedicated tests directory with test suites for individual endpoints.
- Pre-commit and formatting tools (Ruff) integrated in development.

##### Relationships and Interactions
- Clients (e.g., Swagger UI accessed through a browser, or Python scripts using the API) send HTTP requests, which get routed to the FastAPI server.
- FastAPI then directs requests to the relevant router modules (catalog, deployments, etc.).
- The Deployments endpoints interface with Nomad (via the Nomad integration modules) to schedule containerized jobs.
- Configuration data is loaded from the etc folder to control dynamic settings for deployments, quotas, and custom configurations.
- External systems (like OSCAR for additional services) and the Docker container registry/infrastructure form part of the broader ecosystem.

##### Architectural Patterns and Design Principles
- Modular Design: Separation of API endpoints into clear versioned modules allows scalability and independent evolution of service areas.
- Decoupling: The API abstracts away the complexities of directly interacting with underlying systems (Nomad, OSCAR) by acting as an intermediary.
- Configuration-Driven Behavior: Extensive use of external YAML/HCL configurations supports flexibility in deploying and operating the application.
- Containerization & CI/CD: Docker, GitHub actions, and pre-commit hooks enforce quality and streamline deployment.

##### Relevant Technologies and Frameworks
- FastAPI (API layer)
- Nomad (job scheduling and orchestration)
- OAuth/OIDC for authentication (integration with EGI Check-In)
- Docker (containerization)
- Python testing frameworks (pytest or standard unittest in tests/)
- YAML/HCL configuration for module/tool settings

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    CLIENTS["Clients (Browser/Swagger UI, CLI)"]:::client

    %% API Gateway Layer
    FASTAPI["FastAPI Server"]:::api
    AUTH["Authentication Module"]:::api

    %% Router Endpoints
    CATALOG["Catalog Router"]:::router
    DEPLOY["Deployments Router"]:::router
    INFER["Inference Router"]:::router
    TRYME["Try_me Router"]:::router
    STATS["Stats Router"]:::router
    SECRETS["Secrets Endpoint"]:::router
    STORAGE["Storage Endpoint"]:::router
    SNAPSHOTS["Snapshots Endpoint"]:::router

    %% External Integration Layer
    NOMAD_INT["Nomad Integration"]:::external
    OSCAR_INT["OSCAR Integration"]:::external

    %% Configuration Layer
    CONFIG["Configuration Management"]:::config

    %% DevOps & Testing Layer
    DEVOPS["DevOps: Docker & CI/CD Pipeline"]:::devops
    QA["Testing & Quality Assurance"]:::qa

    %% Data Flow
    CLIENTS -->|"HTTP request"| FASTAPI
    FASTAPI -->|"Validates"| AUTH
    FASTAPI -->|"Routes"| CATALOG
    FASTAPI -->|"Routes"| DEPLOY
    FASTAPI -->|"Routes"| INFER
    FASTAPI -->|"Routes"| TRYME
    FASTAPI -->|"Routes"| STATS
    FASTAPI -->|"Routes"| SECRETS
    FASTAPI -->|"Routes"| STORAGE
    FASTAPI -->|"Routes"| SNAPSHOTS

    %% Endpoints to Integrations
    DEPLOY -->|"Deploy jobs"| NOMAD_INT
    TRYME -->|"Deploy demo jobs"| NOMAD_INT
    INFER -->|"Inference calls"| OSCAR_INT

    %% API uses configuration
    FASTAPI -->|"Loads settings"| CONFIG

    %% DevOps and QA
    CONFIG -->|"Drives build"| DEVOPS
    FASTAPI -->|"Tested by"| QA

    %% Click Events
    click FASTAPI "https://github.com/ai4os/ai4-papi/blob/master/ai4papi/main.py"
    click AUTH "https://github.com/ai4os/ai4-papi/blob/master/ai4papi/auth.py"
    click CATALOG "https://github.com/ai4os/ai4-papi/tree/master/ai4papi/routers/v1/catalog"
    click DEPLOY "https://github.com/ai4os/ai4-papi/tree/master/ai4papi/routers/v1/deployments"
    click INFER "https://github.com/ai4os/ai4-papi/blob/master/ai4papi/routers/v1/inference/oscar.py"
    click TRYME "https://github.com/ai4os/ai4-papi/tree/master/ai4papi/routers/v1/try_me"
    click STATS "https://github.com/ai4os/ai4-papi/tree/master/ai4papi/routers/v1/stats"
    click SECRETS "https://github.com/ai4os/ai4-papi/blob/master/ai4papi/routers/v1/secrets.py"
    click STORAGE "https://github.com/ai4os/ai4-papi/blob/master/ai4papi/routers/v1/storage.py"
    click SNAPSHOTS "https://github.com/ai4os/ai4-papi/blob/master/ai4papi/routers/v1/snapshots.py"
    click NOMAD_INT "https://github.com/ai4os/ai4-papi/tree/master/ai4papi/nomad"
    click OSCAR_INT "https://github.com/ai4os/ai4-papi/blob/master/ai4papi/routers/v1/inference/oscar.py"
    click CONFIG "https://github.com/ai4os/ai4-papi/tree/master/etc"
    click DEVOPS "https://github.com/ai4os/ai4-papi/tree/master/docker"
    click QA "https://github.com/ai4os/ai4-papi/tree/master/tests"
    
    %% Additional click events for Nomad configurations and OSCAR config
    %% (These are represented as annotations from the integration components)
    click NOMAD_INT "https://github.com/ai4os/ai4-papi/blob/master/etc/modules/nomad.hcl"
    click OSCAR_INT "https://github.com/ai4os/ai4-papi/blob/master/etc/oscar.yaml"

    %% Styles
    classDef client fill:#cce5ff,stroke:#003366,stroke-width:2px;
    classDef api fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef router fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef external fill:#fde2e4,stroke:#d63384,stroke-width:2px;
    classDef config fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef devops fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef qa fill:#f8d7da,stroke:#721c24,stroke-width:2px;
```
