# fatwang2/dify2openai architecture

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

- Interactive diagram: https://gitdiagram.com/fatwang2/dify2openai
- Repository: https://github.com/fatwang2/dify2openai

## Overview

##### Project type and purpose
- This is a single‐service Node.js “API adapter” microservice. It exposes OpenAI‐compatible HTTP endpoints (v1/chat/completions, v1/completions, v1/agent, v1/workflow) and under the hood forwards requests to the Dify API, then remaps Dify’s JSON payload back into the OpenAI schema.

##### File structure
- Top‐level files:
- app.js – the Express.js HTTP server and router
- Dockerfile, docker-compose.yml / docker-compose-image.yml – container build and orchestration
- vercel.json – serverless deployment config
- .github/workflows/build.yaml – CI pipeline
- .env.template – environment variable template
- pictures/usage.png – usage example
- No frontend code, no database folders – purely a backend adapter.

##### Insights from README
- Core function: “Convert Dify API into an OpenAI API.”
- Supports streaming vs. blocking, Chat/Completion/Agent/Workflow bots.
- Deployable on Vercel (serverless, 10-sec limit), Zeabur, Docker.
- Key environment variables: DIFY_API_URL, BOT_TYPE, INPUT_VARIABLE, OUTPUT_VARIABLE, MODELS_NAME.

##### Main components
a. Clients
- Any OpenAI‐compatible client (ChatGPT plugin, curl, Postman, custom app)
b. API Adapter Service (Node.js/Express)
- Routes incoming v1/* requests
- Applies BOT_TYPE logic (Chat vs. Completion vs. Workflow)
- Supports streaming (Server‐Sent Events/WebSockets) and blocking
- Transforms request/response payloads between OpenAI and Dify formats
c. External Dify API
- The real LLM, knowledge base, tools, and workflows endpoint (DIFY_API_URL)
d. Deployment & Infrastructure
- CI/CD (GitHub Actions)
- Container orchestration (Docker Compose) or serverless (Vercel/Zeabur)

##### Relationships & interactions
- Clients → HTTP POST to /v1/* on the Adapter Service
- Adapter Service reads .env (DIFY_API_URL, BOT_TYPE, etc.)
- Adapter Service → HTTP(s) call to Dify API (with user’s Bearer token)
- Dify API → JSON response (stream or full) → Adapter Service
- Adapter Service → remapped JSON → Client

##### Architectural patterns & principles
- Single microservice, stateless
- Config‐driven behavior via environment variables
- HTTP proxy/adapter pattern
- Polyglot deployment flexibility (container or serverless)

##### Technologies, frameworks & libraries
- Node.js + Express.js (app.js)
- GitHub Actions (build workflow)
- Docker (+ Docker Compose)
- Vercel serverless functions
- Dify LLM platform (external API)

##### Details
- That the service is stateless and horizontally scalable.
- That environment variables tune the behavior at runtime.
- 10-second serverless timeout constraint on Vercel.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Clients"
        Client["OpenAI-compatible client\n(Chat, Completion, Agent, Workflow)"]:::clients
    end

    subgraph "Adapter Service"
        Adapter["Node.js Adapter Service\n- Express.js server (app.js)\n- Env vars (.env.template)\n- Stream vs Blocking Handler\n- Payload Transformer"]:::adapter
    end

    subgraph "External API"
        Dify["Dify API\n(v1 workflows, LLM endpoints)"]:::external
    end

    subgraph "CI/CD & Deployment"
        GitHub["GitHub"]:::infra
        Actions["GitHub Actions\n(.github/workflows/build.yaml)"]:::infra
        DockerReg["Docker Registry\n(Dockerfile,\n docker-compose.yml,\n docker-compose-image.yml)"]:::infra
        Vercel["Vercel/Zeabur\n(vercel.json)"]:::infra
    end

    Client -->|"HTTP POST /v1/* (JSON)"| Adapter
    Adapter -->|"HTTP request to DIFY_API_URL/v1/*"| Dify
    Dify -->|"JSON response / stream"| Adapter
    Adapter -->|"OpenAI-format JSON / SSE stream"| Client

    GitHub -->|"Triggers"| Actions
    Actions -->|"Build & Push"| DockerReg
    DockerReg -->|"Deploy (Docker)"| Adapter
    Actions -->|"Deploy"| Vercel
    Vercel -->|"Serverless Instantiation"| Adapter

    classDef clients fill:#cce5ff,stroke:#000,stroke-width:1px
    classDef adapter fill:#d4edda,stroke:#000,stroke-width:1px
    classDef external fill:#ffe5b4,stroke:#000,stroke-width:1px
    classDef infra fill:#e2e3e5,stroke:#000,stroke-width:1px

    click Adapter "https://github.com/fatwang2/dify2openai/blob/main/app.js"
    click Adapter "https://github.com/fatwang2/dify2openai/blob/main/.env.template"
    click DockerReg "https://github.com/fatwang2/dify2openai/tree/main/Dockerfile"
    click DockerReg "https://github.com/fatwang2/dify2openai/blob/main/docker-compose.yml"
    click DockerReg "https://github.com/fatwang2/dify2openai/blob/main/docker-compose-image.yml"
    click Vercel "https://github.com/fatwang2/dify2openai/blob/main/vercel.json"
    click Actions "https://github.com/fatwang2/dify2openai/blob/main/.github/workflows/build.yaml"
    click Client "https://github.com/fatwang2/dify2openai/blob/main/pictures/usage.png"
```
