Skip to content

JichinX/codex-glm-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Codex GLM Proxy

License: MIT Python 3.8+

English | δΈ­ζ–‡

Enable OpenAI Codex CLI to work with GLM (ζ™Ίθ°± AI) models by running a local proxy that converts OpenAI Responses API format to GLM Chat Completions format.

✨ Features

  • βœ… Full Codex Compatibility - Works seamlessly with OpenAI Codex CLI
  • βœ… Streaming Support - Real-time streaming responses
  • βœ… Tool Calling - Supports apply_patch, exec, and other Codex tools
  • βœ… Multi-turn Conversations - Maintains conversation context
  • βœ… Automatic Model Mapping - Maps OpenAI model names to GLM equivalents
  • βœ… Easy Setup - Single Python file, no complex dependencies

πŸ“Š Use Cases

🎯 Perfect For

  • Chinese Developers - Use GLM (ζ™Ίθ°± AI) instead of OpenAI GPT-4
  • Cost-Conscious Teams - GLM offers competitive pricing for Chinese users
  • Local Development - Run Codex with domestic LLM providers
  • Privacy-Focused Projects - Keep code within China's borders
  • Learning & Education - Access powerful coding AI without OpenAI account

πŸ’‘ Common Scenarios

Scenario Example Command
Quick Prototyping codex exec "Create a REST API with authentication"
Code Generation codex exec "Generate TypeScript interfaces from JSON schema"
Bug Fixing codex exec "Fix the null pointer exception in UserService.java"
Testing codex exec "Add unit tests for the calculator module"
Documentation codex exec "Generate API documentation from code comments"
Refactoring codex exec "Refactor this class to use dependency injection"

πŸ”„ Architecture & Data Flow

System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Codex CLI     β”‚  Sends: Responses API Request
β”‚   (User Side)   β”‚  Receives: Responses API Response
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ Responses API Format
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Codex GLM Proxy (localhost:18765)    β”‚
β”‚                                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Request Converter                 β”‚ β”‚
β”‚  β”‚  - Responses API β†’ Chat Completionsβ”‚ β”‚
β”‚  β”‚  - Tool call history handling      β”‚ β”‚
β”‚  β”‚  - Model name mapping              β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Response Converter                β”‚ β”‚
β”‚  β”‚  - Chat Completions β†’ Responses APIβ”‚ β”‚
β”‚  β”‚  - Tool call streaming             β”‚ β”‚
β”‚  β”‚  - Event sequencing                β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ Chat Completions API Format
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   GLM API       β”‚  Sends: Chat Completions Request
β”‚   (ζ™Ίθ°± AI)     β”‚  Receives: Chat Completions Response
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Request Flow

sequenceDiagram
    participant U as User
    participant C as Codex CLI
    participant P as GLM Proxy
    participant G as GLM API

    U->>C: codex exec "Create hello.py"
    C->>P: Responses API Request<br/>(Responses API format)
    Note over P: Convert to Chat Completions API
    P->>G: Chat Completions Request<br/>(GLM format)
    G-->>P: Chat Completions Response<br/>(Streaming)
    Note over P: Convert back to Responses API
    P-->>C: Responses API Response<br/>(Streaming events)
    C-->>U: Display results & apply changes
Loading

Key Conversion Points

1. Request Conversion (Codex β†’ GLM)

// OpenAI Responses API (from Codex)
{
  "model": "gpt-4o",
  "input": [
    {"type": "message", "role": "user", "content": "..."},
    {"type": "function_call", "call_id": "...", "name": "exec"}
  ]
}

        ⬇️ Proxy Converts ⬇️

// GLM Chat Completions (to GLM)
{
  "model": "glm-4-plus",
  "messages": [
    {"role": "user", "content": "..."},
    {"role": "assistant", "tool_calls": [...]}
  ],
  "tools": [...]
}

2. Response Conversion (GLM β†’ Codex)

GLM Chat Completions     β†’  Responses API Event
────────────────────         ────────────────────────
delta.content            β†’  response.output_text.delta
delta.tool_calls         β†’  response.function_call_arguments.delta
finish_reason            β†’  response.completed

Note: Codex CLI sends and receives Responses API format, while GLM API uses Chat Completions format. The proxy handles the bidirectional conversion.

Tool Call Flow

graph LR
    A[Codex Request] --> B[Proxy]
    B --> C{Tool Call?}
    C -->|Yes| D[Execute Tool]
    D --> E[Return Result]
    E --> B
    C -->|No| F[Stream to GLM]
    F --> G[Get Response]
    G --> H[Convert & Return]
    H --> A
Loading

πŸš€ Quick Start

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/JichinX/codex-glm-proxy.git
    cd codex-glm-proxy
  2. Set your GLM API key

    export GLM_API_KEY="your_glm_api_key_here"
  3. Start the proxy

    python3 proxy.py
    # ζˆ–δ½Ώη”¨δΎΏζ·θ„šζœ¬
    ./scripts/start.sh

    Proxy will run on http://localhost:18765

  4. Configure Codex CLI

    Create or update ~/.codex/config.toml:

    model_provider = "glm-proxy"
    model = "gpt-4o"
    
    [model_providers.glm-proxy]
    name = "GLM via Proxy"
    base_url = "http://localhost:18765/v4"
    wire_api = "responses"
  5. Test it!

    mkdir test-codex && cd test-codex && git init
    codex exec "Create a Python hello world program" --full-auto

πŸ“‹ Configuration

Environment Variable Default Description
GLM_API_KEY (required) Your GLM API key
GLM_API_BASE https://open.bigmodel.cn/api/coding/paas/v4 GLM API endpoint
PROXY_PORT 18765 Local proxy port

πŸ—ΊοΈ Model Mapping

The proxy automatically maps OpenAI model names to GLM equivalents:

OpenAI Model GLM Model Notes
gpt-4 glm-4 Standard GPT-4
gpt-4-turbo glm-4 GPT-4 Turbo
gpt-4o glm-4-plus Recommended for best coding
gpt-4o-mini glm-4-flash Faster, cheaper
gpt-3.5-turbo glm-4-flash Legacy support
gpt-5.x-codex glm-5 Future Codex models

Recommendation: Use model = "gpt-4o" in your Codex config for best results.

πŸ”§ Management

# Start proxy (background)
./scripts/start.sh

# Check if running
curl http://localhost:18765/health

# View logs
tail -f /tmp/codex-glm-proxy.log

# Stop proxy
./scripts/stop.sh

πŸ› οΈ How It Works

The proxy sits between Codex CLI and GLM API, converting:

Codex CLI β†’ Responses API β†’ Proxy β†’ Chat Completions API β†’ GLM

Request Conversion

  • Converts OpenAI Responses API format to GLM Chat Completions
  • Handles tool call history and function results
  • Filters unsupported tools

Response Conversion

  • Streams Chat Completions responses back to Responses API format
  • Maintains proper event sequencing
  • Handles tool call streaming

πŸ“ Example Usage

# Simple task
codex exec "Create a Python function to calculate Fibonacci" --full-auto

# More complex project
codex exec "Build a REST API with FastAPI for todo management" --full-auto

# With tests
codex exec "Create a calculator module with unit tests" --full-auto

πŸ› Troubleshooting

"Streaming complete, sent 0 chunks"

Cause: Model name not properly mapped
Solution: Ensure you're using a known model like gpt-4o in config

Codex loops / repeats actions

Cause: Tool call history not properly handled
Solution: Update to latest version of proxy

502 Bad Gateway

Cause: Proxy crashed
Solution: Check logs at /tmp/codex-glm-proxy.log and restart

Connection refused

Cause: Proxy not running
Solution: Start proxy with ./scripts/start.sh

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • OpenAI Codex - The amazing coding agent
  • ζ™Ίθ°± AI GLM - Powerful Chinese LLM
  • Inspired by the need to use Codex with local/alternative LLM providers

πŸ“Š Project Status

βœ… Production Ready - Fully tested with all Codex features

Feature Status
Text conversations βœ… Working
Model mapping βœ… Working
Streaming responses βœ… Working
Tool calling βœ… Working
Multi-turn conversations βœ… Working
Tool call history βœ… Working
Tool call results βœ… Working

Made with ❀️ by the community, for the community

Star ⭐ this repo if you find it useful!

About

πŸ”Œ Enable OpenAI Codex CLI to work with GLM (ζ™Ίθ°± AI) models - Full streaming & tool calling support

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors