# mquan/api2ai architecture

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

- Interactive diagram: https://gitdiagram.com/mquan/api2ai
- Repository: https://github.com/mquan/api2ai

## Overview

##### Main Components

a) Core Engine Components:
- OpenAPI Specification Parser
- AI Integration Layer
- Authentication Handler
- Operation Selector
- Request Executor

b) Server Components:
- Next.js API Server
- Configuration Manager
- API Endpoints

c) External Dependencies:
- OpenAI API
- User-provided APIs
- OpenAPI Specifications

**[Client Layer]**

**[API Server Layer]**

**[Core Processing Layer]**

**[External Services Layer]**

##### Component Relationships

a) Input Flow:
- User Request → Next.js API Server
- API Server → Core Engine
- Core Engine → OpenAI API
- Core Engine → Target APIs

b) Processing Flow:
- OpenAPI Specs → OAS Loader
- User Prompt → AI Agent
- AI Agent → Operation Selector
- Operation Selector → Request Executor

##### Specific Implementation Details

**[Client Request]**
- ↓
**[Next.js API Server (server/pages/api)]**
- ↓
**[ApiAgent (core/src/ai/api-agent.ts)]**
- [OAS Loader (core/src/api/oas-loader.ts)]
- [Operation Selector (core/src/ai/tools/select-operation.ts)]
- [Argument Parser (core/src/ai/tools/parse-arguments.ts)]
- [Security Handler (core/src/api/security.ts)]
- ↓
**[External APIs]**

##### Important Notes

- Modular nature of the system, emphasizing how multiple APIs can be configured
- Authentication flow for different security schemes (Bearer, API Key, Basic Auth)
- Demonstrate how the AI agent interfaces with both the OpenAPI specs and the target APIs
- Configuration flow from api2ai.config.ts

##### Technical Details

- Support for OpenAPI Spec 3.0.0+
- Integration with GPT models (3.5-turbo-1106, gpt-4-1106-preview)
- Authentication schemes supported
- Request/Response flow

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        CR[Client Request]
    end

    subgraph "API Server Layer"
        NAS["Next.js API Server"]:::server
        CM["Configuration Manager"]:::config
    end

    subgraph "Core Processing Layer"
        CE["Core Engine"]:::core
        
        subgraph "API Processing"
            OAS["OpenAPI Spec Parser"]:::core
            RE["Request Executor"]:::core
            AH["Authentication Handler"]:::security
        end
        
        subgraph "AI Processing"
            AIL["AI Integration Layer"]:::ai
            OS["Operation Selector"]:::ai
            AP["Argument Parser"]:::ai
        end
    end

    subgraph "External Services"
        OAPI["OpenAI API"]:::external
        TAPI["Target APIs"]:::external
        OSpec["OpenAPI Specifications"]:::external
    end

    %% Connections
    CR --> NAS
    NAS --> CE
    CM --> CE
    
    CE --> OAS
    CE --> AIL
    
    OAS --> OSpec
    AIL --> OAPI
    AIL --> OS
    OS --> AP
    AP --> RE
    RE --> AH
    AH --> TAPI

    %% Styles
    classDef core fill:#2374ab
    classDef ai fill:#7209b7
    classDef server fill:#ffa62b
    classDef security fill:#d62828
    classDef external fill:#38b000
    classDef config fill:#ff9f1c

    %% Click Events
    click OAS "https://github.com/mquan/api2ai/blob/main/core/src/api/oas-loader.ts"
    click AIL "https://github.com/mquan/api2ai/blob/main/core/src/ai/api-agent.ts"
    click AH "https://github.com/mquan/api2ai/blob/main/core/src/api/security.ts"
    click OS "https://github.com/mquan/api2ai/blob/main/core/src/ai/tools/select-operation.ts"
    click RE "https://github.com/mquan/api2ai/blob/main/core/src/api/operation.ts"
    click AP "https://github.com/mquan/api2ai/blob/main/core/src/ai/tools/parse-arguments.ts"
    click NAS "https://github.com/mquan/api2ai/blob/main/server/pages/api/run.ts"
    click CM "https://github.com/mquan/api2ai/blob/main/server/pages/api/api2ai.config.ts"
    click OSpec "https://github.com/mquan/api2ai/tree/main/server/oases/"
    click CE "https://github.com/mquan/api2ai/blob/main/core/index.ts"

    %% Legend
    subgraph Legend
        L1[Core Components]:::core
        L2[AI Components]:::ai
        L3[Server Components]:::server
        L4[Security Components]:::security
        L5[External Services]:::external
        L6[Configuration]:::config
    end
```
