# sherifjavaandroid/ai-api architecture

> Architecture diagram of the GitHub repository sherifjavaandroid/ai-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sherifjavaandroid/ai-api
- Repository: https://github.com/sherifjavaandroid/ai-api

## Overview

DeepSeek V3 Free is a self-hosted API adapter that accepts OpenAI-compatible chat requests and proxies them to DeepSeek using user tokens. The main flow validates and selects a token, dispatches ordinary or tool-enabled requests, translates upstream output (including streaming), and returns an API response. The README also documents Responses, Anthropic-compatible messages, model listing, ping, and token checks. Relationships for unsampled route implementations are included only where the README or route index supports them; other uncertain internals are omitted.

Main entry points: [`src/api/routes/index.ts`](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/index.ts) (Route registry), [`src/lib/server.ts`](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/server.ts) (HTTP server), [`src/api/controllers/chat.ts`](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/controllers/chat.ts) (Chat completion), [`src/index.ts`](https://github.com/sherifjavaandroid/ai-api/blob/master/src/index.ts) (Process bootstrap)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["HTTP entry"]
  node_bootstrap["Process bootstrap<br/>[index.ts]"]
  node_http["HTTP server<br/>[server.ts]"]
  node_request["Request parsing<br/>[Request.ts]"]
  node_response["Response encoding<br/>[Response.ts]"]
  node_routes["Route registry<br/>[index.ts]"]
end

subgraph group_api["API capabilities"]
  node_chatroute["Chat endpoint<br/>[chat.ts]"]
  node_chat["Chat completion<br/>[chat.ts]"]
  node_agent["Tool orchestration<br/>[agent.ts]"]
  node_responses["Responses endpoint<br/>[responses.ts]"]
  node_messages["Messages endpoint<br/>[messages.ts]"]
  node_anthropic["Anthropic adapter<br/>[anthropic.ts]"]
  node_models["Models endpoint<br/>[models.ts]"]
  node_ping["Ping endpoint<br/>[ping.ts]"]
  node_token["Token check<br/>[token.ts]"]
end

subgraph group_upstream["DeepSeek integration"]
  node_challenge["Challenge solver<br/>[challenge.ts]"]
  node_wasm["WASM hashing<br/>[challenge-wasm.ts]"]
end

subgraph group_core["Runtime core"]
  node_config["Configuration<br/>[config.ts]"]
  node_serviceconfig["Service settings<br/>[service-config.ts]"]
  node_systemconfig["System settings<br/>[system-config.ts]"]
  node_environment["Environment<br/>[environment.ts]"]
  node_logger["Logging<br/>[logger.ts]"]
  node_failure["Error handling<br/>[FailureBody.ts]"]
end

node_client(("API client"))
node_deepseek{{"DeepSeek service"}}

node_client -->|"sends requests"| node_http
node_bootstrap -->|"starts server"| node_http
node_bootstrap -->|"attaches routes"| node_routes
node_http -->|"dispatches routes"| node_routes
node_http -->|"builds request"| node_request
node_http -->|"injects response"| node_response
node_http -->|"formats errors"| node_failure
node_routes -->|"registers"| node_chatroute
node_routes -->|"registers"| node_responses
node_routes -->|"registers"| node_messages
node_routes -->|"registers"| node_models
node_routes -->|"registers"| node_ping
node_routes -->|"registers"| node_token
node_chatroute -->|"calls completion"| node_chat
node_chatroute -->|"calls tool flow"| node_agent
node_responses -->|"handles responses"| node_agent
node_messages -->|"calls adapter"| node_anthropic
node_token -->|"checks token"| node_chat
node_agent -->|"requests completion"| node_chat
node_chat -->|"uses challenge"| node_challenge
node_challenge -->|"uses hashing"| node_wasm
node_chat -->|"calls upstream"| node_deepseek
node_config -->|"loads settings"| node_serviceconfig
node_config -->|"loads settings"| node_systemconfig
node_serviceconfig -->|"reads environment"| node_environment
node_systemconfig -->|"reads environment"| node_environment
node_bootstrap -->|"reads settings"| node_config
node_bootstrap -->|"logs startup"| node_logger

click node_bootstrap "https://github.com/sherifjavaandroid/ai-api/blob/master/src/index.ts"
click node_http "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/server.ts"
click node_request "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/request/Request.ts"
click node_response "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/response/Response.ts"
click node_routes "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/index.ts"
click node_chatroute "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/chat.ts"
click node_chat "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/controllers/chat.ts"
click node_agent "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/controllers/agent.ts"
click node_responses "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/responses.ts"
click node_messages "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/messages.ts"
click node_anthropic "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/controllers/anthropic.ts"
click node_models "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/models.ts"
click node_ping "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/ping.ts"
click node_token "https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/token.ts"
click node_challenge "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/challenge.ts"
click node_wasm "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/challenge-wasm.ts"
click node_config "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/config.ts"
click node_serviceconfig "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/configs/service-config.ts"
click node_systemconfig "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/configs/system-config.ts"
click node_environment "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/environment.ts"
click node_logger "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/logger.ts"
click node_failure "https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/response/FailureBody.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bootstrap,node_http,node_request,node_response,node_routes,node_client toneBlue
class node_chatroute,node_chat,node_agent,node_responses,node_messages,node_anthropic,node_models,node_ping,node_token toneAmber
class node_challenge,node_wasm toneMint
class node_config,node_serviceconfig,node_systemconfig,node_environment,node_logger,node_failure toneRose
class node_deepseek toneIndigo
```

## Components

### HTTP entry

- [Process bootstrap](https://github.com/sherifjavaandroid/ai-api/blob/master/src/index.ts): `src/index.ts`
- [HTTP server](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/server.ts): `src/lib/server.ts`
- [Request parsing](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/request/Request.ts): `src/lib/request/Request.ts`
- [Response encoding](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/response/Response.ts): `src/lib/response/Response.ts`
- [Route registry](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/index.ts): `src/api/routes/index.ts`

### API capabilities

- [Chat endpoint](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/chat.ts): `src/api/routes/chat.ts`
- [Chat completion](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/controllers/chat.ts): `src/api/controllers/chat.ts`
- [Tool orchestration](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/controllers/agent.ts): `src/api/controllers/agent.ts`
- [Responses endpoint](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/responses.ts): `src/api/routes/responses.ts`
- [Messages endpoint](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/messages.ts): `src/api/routes/messages.ts`
- [Anthropic adapter](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/controllers/anthropic.ts): `src/api/controllers/anthropic.ts`
- [Models endpoint](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/models.ts): `src/api/routes/models.ts`
- [Ping endpoint](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/ping.ts): `src/api/routes/ping.ts`
- [Token check](https://github.com/sherifjavaandroid/ai-api/blob/master/src/api/routes/token.ts): `src/api/routes/token.ts`

### DeepSeek integration

- [Challenge solver](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/challenge.ts): `src/lib/challenge.ts`
- [WASM hashing](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/challenge-wasm.ts): `src/lib/challenge-wasm.ts`

### Runtime core

- [Configuration](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/config.ts): `src/lib/config.ts`
- [Service settings](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/configs/service-config.ts): `src/lib/configs/service-config.ts`
- [System settings](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/configs/system-config.ts): `src/lib/configs/system-config.ts`
- [Environment](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/environment.ts): `src/lib/environment.ts`
- [Logging](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/logger.ts): `src/lib/logger.ts`
- [Error handling](https://github.com/sherifjavaandroid/ai-api/blob/master/src/lib/response/FailureBody.ts): `src/lib/response/FailureBody.ts`

### Other components

- **API client**
- **DeepSeek service**

## Connections

- API client → HTTP server: sends requests (evidence: `README.md`)
- Process bootstrap → HTTP server: starts server (evidence: `src/index.ts`)
- Process bootstrap → Route registry: attaches routes (evidence: `src/index.ts`)
- HTTP server → Route registry: dispatches routes (evidence: `src/lib/server.ts`)
- HTTP server → Request parsing: builds request (evidence: `src/lib/server.ts`)
- HTTP server → Response encoding: injects response (evidence: `src/lib/server.ts`)
- HTTP server → Error handling: formats errors (evidence: `src/lib/server.ts`)
- Route registry → Chat endpoint: registers (evidence: `src/api/routes/index.ts`)
- Route registry → Responses endpoint: registers (evidence: `src/api/routes/index.ts`)
- Route registry → Messages endpoint: registers (evidence: `src/api/routes/index.ts`)
- Route registry → Models endpoint: registers (evidence: `src/api/routes/index.ts`)
- Route registry → Ping endpoint: registers (evidence: `src/api/routes/index.ts`)
- Route registry → Token check: registers (evidence: `src/api/routes/index.ts`)
- Chat endpoint → Chat completion: calls completion (evidence: `src/api/routes/chat.ts`)
- Chat endpoint → Tool orchestration: calls tool flow (evidence: `src/api/routes/chat.ts`)
- Responses endpoint → Tool orchestration: handles responses (evidence: `src/api/routes/responses.ts`)
- Messages endpoint → Anthropic adapter: calls adapter (evidence: `src/api/routes/messages.ts`)
- Token check → Chat completion: checks token (evidence: `src/api/routes/token.ts`)
- Tool orchestration → Chat completion: requests completion (evidence: `src/api/controllers/agent.ts`)
- Chat completion → Challenge solver: uses challenge (evidence: `src/api/controllers/chat.ts`)
- Challenge solver → WASM hashing: uses hashing (evidence: `src/lib/challenge.ts`)
- Chat completion → DeepSeek service: calls upstream (evidence: `src/api/controllers/chat.ts`)
- Configuration → Service settings: loads settings (evidence: `src/lib/config.ts`)
- Configuration → System settings: loads settings (evidence: `src/lib/config.ts`)
- Service settings → Environment: reads environment (evidence: `src/lib/configs/service-config.ts`)
- System settings → Environment: reads environment (evidence: `src/lib/configs/system-config.ts`)
- Process bootstrap → Configuration: reads settings (evidence: `src/index.ts`)
- Process bootstrap → Logging: logs startup (evidence: `src/index.ts`)
