# izaart95-jpg/glm-free-api architecture

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

- Interactive diagram: https://gitdiagram.com/izaart95-jpg/glm-free-api
- Repository: https://github.com/izaart95-jpg/glm-free-api

## Overview

GLM Bridge is an HTTP proxy that accepts OpenAI- and Anthropic-compatible requests, translates them for Z.AI, and returns streamed or complete responses. The main workflow covers protocol handling, optional agent/tool and vision translation, upstream completion, and session cleanup. The README also documents live model/feature resolution, token collection, and an adjacent image-generation utility. Some links below reflect documented capabilities rather than inspected call sites; the source excerpts do not establish every internal dispatch.

Main entry points: [`internal/zbridge/handlers.go`](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/handlers.go) (OpenAI handlers), [`internal/zbridge/anthropic.go`](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/anthropic.go) (Anthropic protocol), [`internal/zbridge/zai.go`](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/zai.go) (Z.AI chat client), [`internal/zbridge/vision.go`](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/vision.go) (Image input processing)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_server["HTTP proxy"]
  node_entry["Server entry<br/>[main.go]"]
  node_runtime["HTTP runtime<br/>[run.go]"]
  node_middleware["Auth and middleware<br/>[middleware.go]"]
  node_handlers["OpenAI handlers<br/>[handlers.go]"]
  node_anthropic["Anthropic protocol<br/>[anthropic.go]"]
end

subgraph group_translation["Request translation"]
  node_agent["Modern agent shim<br/>[agent.go]"]
  node_legacy["Legacy agent shim<br/>[agent_legacy.go]"]
  node_ultra["Tool repair<br/>[agent_ultra.go]"]
  node_parser["LLM repair backend<br/>[toolparser_llm.go]"]
  node_vision["Image input processing<br/>[vision.go]"]
  node_features["Model features<br/>[features.go]"]
  node_models["Model catalog<br/>[models.go]"]
end

subgraph group_upstream["Z.AI integration"]
  node_zai["Z.AI chat client<br/>[zai.go]"]
  node_captcha["Captcha support<br/>[captcha.go]"]
  node_waf["WAF support<br/>[waf.go]"]
end

subgraph group_state["Sessions and tokens"]
  node_sessions["Session pool<br/>[session_pool.go]"]
  node_database[("Token database<br/>[db.go]")]
  node_collector["Token collector<br/>[main.go]"]
end

subgraph group_adjacent["Related utilities"]
  node_imagegen["Image generation utility<br/>[image-gen.js]"]
end

node_client(("API client"))
node_zai_service(("Z.AI service"))

node_client -->|"sends requests"| node_entry
node_entry -->|"starts runtime"| node_runtime
node_client -->|"uses OpenAI API"| node_handlers
node_client -->|"uses Messages API"| node_anthropic
node_handlers -->|"translates tools"| node_agent
node_handlers -->|"selects legacy shim"| node_legacy
node_handlers -->|"repairs tool calls"| node_ultra
node_ultra -->|"requests repair"| node_parser
node_anthropic -->|"converts to OpenAI form"| node_handlers
node_handlers -->|"processes image inputs"| node_vision
node_anthropic -->|"supports image inputs"| node_vision
node_handlers -->|"requests completions"| node_zai
node_anthropic -->|"uses upstream completion"| node_zai
node_zai -->|"sends HTTP requests"| node_zai_service
node_vision -->|"uploads images"| node_zai_service
node_zai -->|"resolves features"| node_features
node_client -->|"requests model list"| node_models
node_sessions -->|"deletes chats"| node_zai_service
node_collector -->|"seeds tokens"| node_database
node_collector -->|"harvests device tokens"| node_zai_service
node_client -.->|"runs separately"| node_imagegen

click node_entry "https://github.com/izaart95-jpg/glm-free-api/blob/main/main.go"
click node_runtime "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/run.go"
click node_middleware "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/middleware.go"
click node_handlers "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/handlers.go"
click node_anthropic "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/anthropic.go"
click node_agent "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/agent.go"
click node_legacy "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/agent_legacy.go"
click node_ultra "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/agent_ultra.go"
click node_parser "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/toolparser_llm.go"
click node_vision "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/vision.go"
click node_features "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/features.go"
click node_models "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/models.go"
click node_zai "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/zai.go"
click node_captcha "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/captcha.go"
click node_waf "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/waf.go"
click node_sessions "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/session_pool.go"
click node_database "https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/db.go"
click node_collector "https://github.com/izaart95-jpg/glm-free-api/blob/main/cmd/token-collector/main.go"
click node_imagegen "https://github.com/izaart95-jpg/glm-free-api/blob/main/image-gen/image-gen.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_entry,node_runtime,node_middleware,node_handlers,node_anthropic,node_client toneBlue
class node_agent,node_legacy,node_ultra,node_parser,node_vision,node_features,node_models toneAmber
class node_zai,node_captcha,node_waf toneMint
class node_sessions,node_database,node_collector toneRose
class node_imagegen,node_zai_service toneIndigo
```

## Components

### HTTP proxy

- [Server entry](https://github.com/izaart95-jpg/glm-free-api/blob/main/main.go): `main.go`
- [HTTP runtime](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/run.go): `internal/zbridge/run.go`
- [Auth and middleware](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/middleware.go): `internal/zbridge/middleware.go`
- [OpenAI handlers](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/handlers.go): `internal/zbridge/handlers.go`
- [Anthropic protocol](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/anthropic.go): `internal/zbridge/anthropic.go`

### Request translation

- [Modern agent shim](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/agent.go): `internal/zbridge/agent.go`
- [Legacy agent shim](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/agent_legacy.go): `internal/zbridge/agent_legacy.go`
- [Tool repair](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/agent_ultra.go): `internal/zbridge/agent_ultra.go`
- [LLM repair backend](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/toolparser_llm.go): `internal/zbridge/toolparser_llm.go`
- [Image input processing](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/vision.go): `internal/zbridge/vision.go`
- [Model features](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/features.go): `internal/zbridge/features.go`
- [Model catalog](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/models.go): `internal/zbridge/models.go`

### Z.AI integration

- [Z.AI chat client](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/zai.go): `internal/zbridge/zai.go`
- [Captcha support](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/captcha.go): `internal/zbridge/captcha.go`
- [WAF support](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/waf.go): `internal/zbridge/waf.go`

### Sessions and tokens

- [Session pool](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/session_pool.go): `internal/zbridge/session_pool.go`
- [Token database](https://github.com/izaart95-jpg/glm-free-api/blob/main/internal/zbridge/db.go): `internal/zbridge/db.go`
- [Token collector](https://github.com/izaart95-jpg/glm-free-api/blob/main/cmd/token-collector/main.go): `cmd/token-collector/main.go`

### Related utilities

- [Image generation utility](https://github.com/izaart95-jpg/glm-free-api/blob/main/image-gen/image-gen.js): `image-gen/image-gen.js`

### Other components

- **API client**
- **Z.AI service**

## Connections

- API client → Server entry: sends requests (evidence: `README.md`)
- Server entry → HTTP runtime: starts runtime (evidence: `main.go`)
- API client → OpenAI handlers: uses OpenAI API (evidence: `README.md`)
- API client → Anthropic protocol: uses Messages API (evidence: `README.md`)
- OpenAI handlers → Modern agent shim: translates tools (evidence: `README.md`)
- OpenAI handlers → Legacy agent shim: selects legacy shim (evidence: `README.md`)
- OpenAI handlers → Tool repair: repairs tool calls
- Tool repair → LLM repair backend: requests repair (evidence: `internal/zbridge/toolparser_llm.go`)
- Anthropic protocol → OpenAI handlers: converts to OpenAI form (evidence: `internal/zbridge/anthropic.go`)
- OpenAI handlers → Image input processing: processes image inputs (evidence: `internal/zbridge/vision.go`)
- Anthropic protocol → Image input processing: supports image inputs (evidence: `internal/zbridge/anthropic.go`)
- OpenAI handlers → Z.AI chat client: requests completions (evidence: `README.md`)
- Anthropic protocol → Z.AI chat client: uses upstream completion (evidence: `README.md`)
- Z.AI chat client → Z.AI service: sends HTTP requests (evidence: `internal/zbridge/zai.go`)
- Image input processing → Z.AI service: uploads images (evidence: `internal/zbridge/vision.go`)
- Z.AI chat client → Model features: resolves features (evidence: `internal/zbridge/zai.go`)
- API client → Model catalog: requests model list (evidence: `README.md`)
- Session pool → Z.AI service: deletes chats (evidence: `internal/zbridge/session_pool.go`)
- Token collector → Token database: seeds tokens (evidence: `README.md`)
- Token collector → Z.AI service: harvests device tokens (evidence: `cmd/token-collector/main.go`)
- API client → Image generation utility: runs separately
