Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GLM Web Proxy

OpenAI-compatible local API proxy for Z.ai / GLM Web (chat.z.ai).

Allows you to use GLM (GLM Chat with native thinking/reasoning and web search toggles) with OpenCode or any OpenAI-compatible client.


Disclaimer

This project is for educational and research purposes only. It is not affiliated with or endorsed by Zhipu AI or Z.ai.


Features

  • OpenAI API Compatibility: Exposes http://127.0.0.1:1341/v1/chat/completions and /v1/models.
  • Thinking / Reasoning: Streams thinking / reasoning process into reasoning_content delta chunks in real-time.
  • UI Setting Respect: Uses glm-chat, which automatically respects whatever you toggle in the Z.ai Web UI (Thinking mode, Web Search).
  • Tool Calling: Translates tool schemas and parses <tool_call> outputs into OpenAI function call structures for agent tools (write, edit, bash, read).
  • Real-Time Token Tracking: Reports native token usage (prompt_tokens, completion_tokens, total_tokens).
  • Chat Management: Send /clear, /reset, or /new in chat to start a clean conversation session.
  • Lightweight: Pure Python (aiohttp) + Tampermonkey script with no heavy automation frameworks.

Quick Setup

1. Install Dependencies

pip install -r requirements.txt

2. Install Userscript

  1. Install Tampermonkey or Violentmonkey in your browser.
  2. Create a new userscript and paste the contents of glm-bridge.user.js.
  3. Open chat.z.ai and log in.
  4. You will see a badge at the bottom-right: Bridge: Connected (Ready) once the proxy is running.

3. Start the Proxy

python glm-proxy.py

Options:

  • --host 127.0.0.1: Listening host (default: 127.0.0.1).
  • --port 1341: Listening port (default: 1341).

OpenCode Configuration

Add this provider to your OpenCode config (opencode.jsonc):

{
  "provider": {
    "glm-proxy": {
      "api": "openai",
      "name": "GLM Web Proxy",
      "options": {
        "baseURL": "http://127.0.0.1:1341/v1",
        "apiKey": "nah",
        "timeout": 3600000,
        "chunkTimeout": 3600000
      },
      "models": {
        "glm-chat": {
          "id": "glm-chat",
          "name": "GLM Chat (Web Proxy)",
          "tool_call": true,
          "reasoning": true,
          "temperature": true
        },
        "glm-thinking": {
          "id": "glm-thinking",
          "name": "GLM Thinking (Web Proxy)",
          "tool_call": true,
          "reasoning": true,
          "temperature": true
        }
      }
    }
  }
}

Other Clients (Cline, Cursor, etc.)

  • Base URL: http://127.0.0.1:1341/v1
  • API Key: nah
  • Models:
    • glm-chat: Recommended default (respects active toggles in Web UI)
    • glm-thinking: Forces thinking / reasoning mode via API

Python Example

from openai import OpenAI

client = OpenAI(
    base_url="http://127.0.0.1:1341/v1",
    api_key="nah",
)

response = client.chat.completions.create(
    model="glm-chat",
    messages=[{"role": "user", "content": "Hello GLM"}],
    stream=True,
)

for chunk in response:
    reasoning = getattr(chunk.choices[0].delta, "reasoning_content", None)
    if reasoning:
        print(reasoning, end="", flush=True)
    content = chunk.choices[0].delta.content or ""
    print(content, end="", flush=True)

Notes & Chat Management

  • Keep the browser tab open while using the proxy.
  • If the badge shows disconnected, click it to reconnect immediately.
  • To reset manually, send /clear, /reset, or /new directly from your client prompt (or run window.deleteCurrentChat() in the browser console).

License

MIT

About

OpenAI-compatible local API proxy for GLM Web.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages