# qyjoy/ai-worker architecture

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

- Interactive diagram: https://gitdiagram.com/qyjoy/ai-worker
- Repository: https://github.com/qyjoy/ai-worker

## Overview

#### Purpose and Version Layout
This repository implements a serverless AI API forwarder for Cloudflare Workers. It serves a browser UI and relays structured requests to selected AI APIs, principally Google Gemini and Imagen, while keeping provider credentials in Worker environment variables. The maintained implementation appears to be `V4(latest)/`; earlier stable, mobile-oriented, and obsolete variants are kept in parallel:
- `V4(latest)/worker.js`
- `V3(stable)/worker.js`
- `V4 EX(For mobile support Expand)/worker.js`

#### Runtime Entry Point
Each version is a modular JavaScript Worker deployment intended to be copied into Cloudflare’s Worker editor. The Worker entry receives HTTP requests and delegates routing rather than exposing a conventional server process or package-based application:
- `V4(latest)/worker.js`
- `V4(latest)/request-handler.js`

Cloudflare Workers is the material runtime and edge infrastructure. `GEMINI_API_KEY` is supplied through encrypted Worker environment bindings; it is not client-side configuration.

#### Request Routing and HTTP Boundary
The request handler is the application’s HTTP boundary. It distinguishes browser/UI requests from API requests, applies CORS behavior, and routes eligible requests into the API forwarding layer. The service is intentionally limited to supported upstream AI API operations rather than arbitrary URL or network forwarding:
- `V4(latest)/request-handler.js`
- `V4(latest)/utils.js`

Control flow is: client browser → Cloudflare Worker entry → request classification/CORS → API handler or HTML renderer → HTTP response.

#### AI Provider Forwarding
The API handler is the provider boundary. It validates or constrains requested models, reads the server-side API key, constructs upstream calls, and returns provider responses to the client. It supports conversational/model requests, streaming text responses, and image-generation-related flows described by the project:
- `V4(latest)/api-handler.js`
- `V3(stable)/api-handler.js`

External dependencies are Google AI services, including Gemini and Imagen, accessed over HTTPS. The Worker’s role is application-layer API forwarding, not a VPN, SOCKS service, or general-purpose network proxy.

#### Browser Interface Delivery
The Worker can directly provide a self-contained HTML frontend for choosing models/themes, submitting prompts, and presenting responses. This keeps the frontend coupled to the same Worker origin and API routes rather than requiring a separate web hosting system:
- `V4(latest)/html-renderer.js`
- `V4 EX(For mobile support Expand)/html-renderer.js`

The “EX” variant is an alternate UI/runtime implementation oriented toward expanded mobile support; its modules mirror the same architectural roles.

#### Shared Utilities and Cross-Cutting Behavior
Shared utility code centralizes reusable response/request concerns used across routing and provider integration, likely including CORS and error/response normalization:
- `V4(latest)/utils.js`
- `V3(stable)/utils.js`

No R2, D1, or other persistent data layer is present in the supplied tree. The README mentions them only as future expansion, so current state is stateless apart from Cloudflare environment configuration.

#### Evolution and Deployment Boundary
The repository preserves independent release snapshots rather than a single importable source hierarchy. Deploy one coherent directory set—especially `V4(latest)`—because each contains its own matched entry, routing, API, renderer, and utility modules. The legacy all-in-one distribution predates the modular design:
- `All in one Portable(outdated)/AI-WorkerOldRelease.js`
- `V4(latest)/worker.js`

Main entry points: [`V4(latest)/api-handler.js`](https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/api-handler.js) (API handler), [`V4(latest)/request-handler.js`](https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/request-handler.js) (Request handler), [`V4(latest)/worker.js`](https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/worker.js) (Worker entry), [`V3(stable)/api-handler.js`](https://github.com/qyjoy/ai-worker/blob/main/V3(stable)/api-handler.js) (V3 API handler)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_latest["V4 latest"]
  node_v4_worker["Worker entry<br/>worker module<br/>[worker.js]"]
  node_v4_router["Request handler<br/>HTTP routing<br/>[request-handler.js]"]
  node_v4_api["API handler<br/>provider forwarder<br/>[api-handler.js]"]
  node_v4_html["HTML renderer<br/>browser UI<br/>[html-renderer.js]"]
  node_v4_utils["Shared utilities<br/>response utilities<br/>[utils.js]"]
end

subgraph group_alternates["Release snapshots"]
  node_v3_release["V3 stable modules<br/>release snapshot<br/>[worker.js]"]
  node_v3_api["V3 API handler<br/>provider forwarder<br/>[api-handler.js]"]
  node_v3_utils["V3 utilities<br/>response utilities<br/>[utils.js]"]
  node_v4ex_release["V4 EX Worker<br/>mobile variant<br/>[worker.js]"]
  node_v4ex_html["V4 EX renderer<br/>mobile UI<br/>[html-renderer.js]"]
  node_legacy["Portable old release<br/>all-in-one Worker"]
end

subgraph group_cloudflare["Cloudflare edge"]
  node_worker_runtime{{"Cloudflare Workers<br/>edge runtime"}}
  node_api_key["GEMINI_API_KEY<br/>encrypted env binding"]
end

subgraph group_providers["Google AI"]
  node_gemini{{"Gemini API<br/>AI provider"}}
  node_imagen{{"Imagen API<br/>image provider"}}
end

node_browser(("Client browser"))

node_browser -->|"HTTPS request"| node_worker_runtime
node_worker_runtime -->|"runs"| node_v4_worker
node_v4_worker -->|"delegates"| node_v4_router
node_v4_router -->|"UI request"| node_v4_html
node_v4_router -->|"supported API request"| node_v4_api
node_v4_router -->|"CORS and responses"| node_v4_utils
node_v4_api -->|"normalizes responses"| node_v4_utils
node_api_key -.->|"credential binding"| node_v4_api
node_v4_api -->|"HTTPS forwarding"| node_gemini
node_v4_api -->|"image requests"| node_imagen
node_v4_html -->|"HTML response"| node_browser
node_v3_release -->|"routes to"| node_v3_api
node_v3_api -->|"uses"| node_v3_utils
node_v4ex_release -->|"serves"| node_v4ex_html
node_v3_api -.->|"forwards to"| node_gemini
node_v4_worker -.->|"supersedes"| node_legacy

click node_v4_worker "https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/worker.js"
click node_v4_router "https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/request-handler.js"
click node_v4_api "https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/api-handler.js"
click node_v4_html "https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/html-renderer.js"
click node_v4_utils "https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/utils.js"
click node_v3_release "https://github.com/qyjoy/ai-worker/blob/main/V3(stable)/worker.js"
click node_v3_api "https://github.com/qyjoy/ai-worker/blob/main/V3(stable)/api-handler.js"
click node_v3_utils "https://github.com/qyjoy/ai-worker/blob/main/V3(stable)/utils.js"
click node_v4ex_release "https://github.com/qyjoy/ai-worker/blob/main/V4%20EX(For%20mobile%20support%20Expand)/worker.js"
click node_v4ex_html "https://github.com/qyjoy/ai-worker/blob/main/V4%20EX(For%20mobile%20support%20Expand)/html-renderer.js"
click node_legacy "https://github.com/qyjoy/ai-worker/blob/main/All%20in%20one%20Portable(outdated)/AI-WorkerOldRelease.js"

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_v4_worker,node_v4_router,node_v4_api,node_v4_html,node_v4_utils toneBlue
class node_v3_release,node_v3_api,node_v3_utils,node_v4ex_release,node_v4ex_html,node_legacy toneAmber
class node_worker_runtime,node_api_key toneMint
class node_gemini,node_imagen toneRose
class node_browser toneNeutral
```

## Components

### V4 latest

Maintained modular Worker deployment.

- [Worker entry](https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/worker.js): worker module · `V4(latest)/worker.js`
- [Request handler](https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/request-handler.js): HTTP routing · Classifies UI and supported API requests and applies CORS. · `V4(latest)/request-handler.js`
- [API handler](https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/api-handler.js): provider forwarder · Constrains models and builds upstream AI requests. · `V4(latest)/api-handler.js`
- [HTML renderer](https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/html-renderer.js): browser UI · Serves the self-contained frontend from the Worker origin. · `V4(latest)/html-renderer.js`
- [Shared utilities](https://github.com/qyjoy/ai-worker/blob/main/V4(latest)/utils.js): response utilities · Centralizes CORS and response/error handling. · `V4(latest)/utils.js`

### Release snapshots

Independent deployable variants.

- [V3 stable modules](https://github.com/qyjoy/ai-worker/blob/main/V3(stable)/worker.js): release snapshot · Earlier modular stable implementation. · `V3(stable)/worker.js`
- [V3 API handler](https://github.com/qyjoy/ai-worker/blob/main/V3(stable)/api-handler.js): provider forwarder · `V3(stable)/api-handler.js`
- [V3 utilities](https://github.com/qyjoy/ai-worker/blob/main/V3(stable)/utils.js): response utilities · `V3(stable)/utils.js`
- [V4 EX Worker](https://github.com/qyjoy/ai-worker/blob/main/V4%20EX(For%20mobile%20support%20Expand)/worker.js): mobile variant · Alternate expanded mobile-support implementation. · `V4 EX(For mobile support Expand)/worker.js`
- [V4 EX renderer](https://github.com/qyjoy/ai-worker/blob/main/V4%20EX(For%20mobile%20support%20Expand)/html-renderer.js): mobile UI · `V4 EX(For mobile support Expand)/html-renderer.js`
- [Portable old release](https://github.com/qyjoy/ai-worker/blob/main/All%20in%20one%20Portable(outdated)/AI-WorkerOldRelease.js): all-in-one Worker · Obsolete pre-modular distribution. · `All in one Portable(outdated)/AI-WorkerOldRelease.js`

### Cloudflare edge

- **Cloudflare Workers**: edge runtime
- **GEMINI_API_KEY**: encrypted env binding · Server-side provider credential.

### Google AI

- **Gemini API**: AI provider · HTTPS model and streaming text operations.
- **Imagen API**: image provider · HTTPS image-generation operations.

### Other components

- **Client browser**: client

## Connections

- Client browser → Cloudflare Workers: HTTPS request
- Cloudflare Workers → Worker entry: runs
- Worker entry → Request handler: delegates
- Request handler → HTML renderer: UI request
- Request handler → API handler: supported API request
- Request handler → Shared utilities: CORS and responses
- API handler → Shared utilities: normalizes responses
- GEMINI_API_KEY → API handler: credential binding
- API handler → Gemini API: HTTPS forwarding
- API handler → Imagen API: image requests
- HTML renderer → Client browser: HTML response
- V3 stable modules → V3 API handler: routes to
- V3 API handler → V3 utilities: uses
- V4 EX Worker → V4 EX renderer: serves
- V3 API handler → Gemini API: forwards to
- Worker entry → Portable old release: supersedes
