# adaline/gateway architecture

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

- Interactive diagram: https://gitdiagram.com/adaline/gateway
- Repository: https://github.com/adaline/gateway

## Overview

##### Project Type and Purpose
- This is an open‑source, production‑grade SDK (“Adaline Gateway”) for unifying access to 200+ LLMs completely locally.
- It’s a monorepo (pnpm + Turbo) containing a core gateway engine, many provider adapters, shared types, and helper packages.

##### High‑Level File Structure & Architectural Patterns
- Top‑level “core” houses:
- core/gateway – the Gateway engine (handlers, plugin interfaces, queue, cache, telemetry, etc.)
- core/providers/* – individual provider adapters (Anthropic, OpenAI, Azure, Bedrock, etc.) each following an adapter/strategy pattern
- “packages” contains:
- packages/types – shared TypeScript types & schemas (MessageType, Config, errors)
- packages/provider – v1 provider SDK interfaces, schemas, error definitions (legacy compatibility)
- Tools & config at root (husky, lint, turborepo, tsconfig) orchestrate builds and CI.
- Architectural patterns:
- Plugin architecture in the gateway (cache, logger, HTTP, queue, telemetry are pluggable)
- Adapter/Strategy for providers (each provider implements a uniform interface)
- Monorepo + Turborepo for code sharing and parallel builds

##### Core Components to Capture
a. Client Code
- “Your App” or “User Code” that imports Gateway, Config, and Provider(s)
b. Gateway Engine
- Gateway object instance
- Internals:
- Queue Manager
- Cache Manager
- Retry / Backoff logic
- HTTP Client Plugin
- Logger Plugin
- Telemetry (OpenTelemetry) Plugin
- Handlers for streamChat, completeChat, getEmbeddings
c. Provider Adapters
- e.g., @adaline/openai, @adaline/anthropic, @adaline/azure…
- Each adapter defines: Configs → Model objects → Provider vX interface → transforms calls → underlying HTTP calls
d. Shared Types & Config
- packages/types exports the super-types (MessageType, Config schemas) used by both gateway and providers
e. External LLM Endpoints
- OpenAI API, Anthropic API, Azure OpenAI endpoint, AWS Bedrock, etc.

##### Relationships & Data Flow
1) Client invokes Gateway.streamChat or Gateway.completeChat with:
- Model (from a provider adapter)
- Config (from shared types)
- Messages (MessageType[])
2) Gateway routes the request through its pipeline:
- (optional) Cache lookup → hit/miss
- Enqueue request in custom queue → batching
- Retry policy wraps HTTP client plugin call
- Logger & Telemetry wrap every stage
3) Gateway calls the provider adapter’s transformModelRequest → builds provider‑specific payload
4) HTTP Client plugin makes the actual network call to the external LLM endpoint
5) Response flows back through retry logic, handlers parse streams or completions, callbacks invoked, then returned to client

##### Key Architectural Patterns / Design Principles
- Separation of Concerns: Gateway core is independent of any particular LLM vendor
- Inversion of Control: Plugins and providers are injected into the Gateway object
- Single Responsibility: Each provider module only cares about model definitions & payload transformation
- Reusability via Monorepo: Shared types and error handling live in central packages

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    Client["Your App / User Code"]:::client

    %% Shared Types
    subgraph "Shared Types & Config"
        packages_types["packages/types/src/index.ts"]:::shared
        packages_config["packages/types/src/config/config.ts"]:::shared
    end

    %% Gateway Core
    subgraph "Gateway Core" 
        gatewaySdk["gateway.ts"]:::core
        gatewayTypes["gateway.types.ts"]:::core
        subgraph "Handlers"
            streamChat["stream-chat.handler.ts"]:::core
            completeChat["complete-chat.handler.ts"]:::core
            getEmbeddings["get-embeddings.handler.ts"]:::core
        end
        subgraph "Plugins"
            queueIF["queue.interface.ts"]:::plugin
            queueImpl["simple.queue.ts"]:::plugin
            cacheIF["cache.interface.ts"]:::plugin
            cacheImpl["lru.cache.ts"]:::plugin
            httpIF["http-client.interface.ts"]:::plugin
            httpImpl["isomorphic.http-client.ts"]:::plugin
            loggerIF["logger.interface.ts"]:::plugin
            consoleLogger["console.logger.ts"]:::plugin
            loggerManager["logger.manager.ts"]:::plugin
            telemetryManager["telemetry.manager.ts"]:::plugin
            telemetryIndex["telemetry/index.ts"]:::plugin
        end
    end

    %% Provider Adapters
    subgraph "Provider Adapters"
        openai["provider.openai.ts"]:::provider
        anthropic["provider.anthropic.ts"]:::provider
        azure["provider.azure.ts"]:::provider
        bedrock["provider.bedrock.ts"]:::provider
        google["provider.google.ts"]:::provider
        together["provider.together-ai.ts"]:::provider
        vertex["provider.vertex.ts"]:::provider
        groq["provider.groq.ts"]:::provider
        openrouter["provider.open-router.ts"]:::provider
        custom["provider.custom.ts"]:::provider
    end

    %% Legacy Provider SDK
    subgraph "Legacy Provider SDK"
        provider_v1["provider.v1.ts"]:::legacy
        chat_model_v1["chat-model.interface.v1.ts"]:::legacy
        embedding_model_v1["embedding-model.interface.v1.ts"]:::legacy
    end

    %% External LLM Endpoints
    subgraph "External LLM Endpoints"
        OpenAI_API["OpenAI API"]:::external
        Anthropic_API["Anthropic API"]:::external
        Azure_API["Azure OpenAI"]:::external
        Bedrock_API["AWS Bedrock"]:::external
        Google_API["Google Gemini"]:::external
        Together_API["Together AI"]:::external
        Vertex_API["Vertex AI"]:::external
        GROQ_API["GROQ API"]:::external
        OpenRouter_API["OpenRouter API"]:::external
        Custom_API["Custom Endpoint"]:::external
    end

    %% Data Flows
    Client -->|invoke streamChat / completeChat / getEmbeddings| gatewaySdk
    packages_types -->|types & schemas| gatewaySdk
    packages_config -->|config schemas| gatewaySdk

    gatewaySdk -->|uses| gatewayTypes
    gatewaySdk -->|streams| streamChat
    gatewaySdk -->|completes| completeChat
    gatewaySdk -->|embeddings| getEmbeddings

    gatewaySdk -->|cache lookup| cacheIF
    cacheIF --> cacheImpl
    gatewaySdk -->|enqueue request| queueIF
    queueIF --> queueImpl

    gatewaySdk -->|log events| loggerIF
    loggerIF --> consoleLogger
    loggerIF --> loggerManager
    gatewaySdk -->|trace| telemetryManager
    telemetryManager --> telemetryIndex

    gatewaySdk -->|http call| httpIF
    httpIF --> httpImpl

    %% Gateway to Providers
    gatewaySdk -->|transformModelRequest| openai
    gatewaySdk -->|transformModelRequest| anthropic
    gatewaySdk -->|transformModelRequest| azure
    gatewaySdk -->|transformModelRequest| bedrock
    gatewaySdk -->|transformModelRequest| google
    gatewaySdk -->|transformModelRequest| together
    gatewaySdk -->|transformModelRequest| vertex
    gatewaySdk -->|transformModelRequest| groq
    gatewaySdk -->|transformModelRequest| openrouter
    gatewaySdk -->|transformModelRequest| custom

    packages_types -->|shared types| openai
    packages_types --> anthropic
    packages_types --> azure
    packages_types --> bedrock
    packages_types --> google
    packages_types --> together
    packages_types --> vertex
    packages_types --> groq
    packages_types --> openrouter
    packages_types --> custom

    %% Providers to HTTP to External
    openai -->|HTTP payload| httpImpl
    anthropic --> httpImpl
    azure --> httpImpl
    bedrock --> httpImpl
    google --> httpImpl
    together --> httpImpl
    vertex --> httpImpl
    groq --> httpImpl
    openrouter --> httpImpl
    custom --> httpImpl

    httpImpl --> OpenAI_API
    httpImpl --> Anthropic_API
    httpImpl --> Azure_API
    httpImpl --> Bedrock_API
    httpImpl --> Google_API
    httpImpl --> Together_API
    httpImpl --> Vertex_API
    httpImpl --> GROQ_API
    httpImpl --> OpenRouter_API
    httpImpl --> Custom_API

    %% Legacy SDK
    gatewaySdk -->|legacy| provider_v1
    provider_v1 --> chat_model_v1
    provider_v1 --> embedding_model_v1

    %% Click Events
    click gatewaySdk "https://github.com/adaline/gateway/blob/main/core/gateway/src/gateway.ts"
    click gatewayTypes "https://github.com/adaline/gateway/blob/main/core/gateway/src/gateway.types.ts"
    click streamChat "https://github.com/adaline/gateway/blob/main/core/gateway/src/handlers/stream-chat/stream-chat.handler.ts"
    click completeChat "https://github.com/adaline/gateway/blob/main/core/gateway/src/handlers/complete-chat/complete-chat.handler.ts"
    click getEmbeddings "https://github.com/adaline/gateway/blob/main/core/gateway/src/handlers/get-embeddings/get-embeddings.handler.ts"
    click queueIF "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/queue/queue.interface.ts"
    click queueImpl "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/queue/simple.queue.ts"
    click cacheIF "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/cache/cache.interface.ts"
    click cacheImpl "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/cache/lru.cache.ts"
    click httpIF "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/http-client/http-client.interface.ts"
    click httpImpl "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/http-client/isomorphic.http-client.ts"
    click loggerIF "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/logger/logger.interface.ts"
    click consoleLogger "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/logger/console.logger.ts"
    click loggerManager "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/logger/logger.manager.ts"
    click telemetryManager "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/telemetry/telemetry.manager.ts"
    click telemetryIndex "https://github.com/adaline/gateway/blob/main/core/gateway/src/plugins/telemetry/index.ts"
    click packages_types "https://github.com/adaline/gateway/blob/main/packages/types/src/index.ts"
    click packages_config "https://github.com/adaline/gateway/blob/main/packages/types/src/config/config.ts"
    click provider_v1 "https://github.com/adaline/gateway/blob/main/packages/provider/src/provider/provider.v1.ts"
    click chat_model_v1 "https://github.com/adaline/gateway/blob/main/packages/provider/src/model/chat-model/chat-model.interface.v1.ts"
    click embedding_model_v1 "https://github.com/adaline/gateway/blob/main/packages/provider/src/model/embedding-model/embedding-model.interface.v1.ts"
    click openai "https://github.com/adaline/gateway/blob/main/core/providers/openai/src/provider/provider.openai.ts"
    click anthropic "https://github.com/adaline/gateway/blob/main/core/providers/anthropic/src/provider/provider.anthropic.ts"
    click azure "https://github.com/adaline/gateway/blob/main/core/providers/azure/src/provider/provider.azure.ts"
    click bedrock "https://github.com/adaline/gateway/blob/main/core/providers/bedrock/src/provider/provider.bedrock.ts"
    click google "https://github.com/adaline/gateway/blob/main/core/providers/google/src/provider/provider.google.ts"
    click together "https://github.com/adaline/gateway/blob/main/core/providers/together-ai/src/provider/provider.together-ai.ts"
    click vertex "https://github.com/adaline/gateway/blob/main/core/providers/vertex/src/provider/provider.vertex.ts"
    click groq "https://github.com/adaline/gateway/blob/main/core/providers/groq/src/provider/provider.groq.ts"
    click openrouter "https://github.com/adaline/gateway/blob/main/core/providers/open-router/src/provider/provider.open-router.ts"
    click custom "https://github.com/adaline/gateway/blob/main/core/providers/custom/src/provider/provider.custom.ts"

    %% Styles
    classDef client fill:#ffe4b5,stroke:#cd853f
    classDef shared fill:#e0ffff,stroke:#20b2aa
    classDef core fill:#f0e68c,stroke:#bdb76b
    classDef plugin fill:#dcdcdc,stroke:#a9a9a9,stroke-dasharray: 5 5
    classDef provider fill:#add8e6,stroke:#4682b4
    classDef legacy fill:#dda0dd,stroke:#8b008b
    classDef external fill:#fafad2,stroke:#daa520
```
