A Python middleware server that sits between OpenCode and GLM 4.7, logging all requests and responses.
- OpenAI-Compatible API:
/v1/chat/completionsendpoint - Request Logging: Pretty prints all incoming requests
- Response Logging: Pretty prints all outgoing responses
- Streaming Support: Native Python async generators
- Tool Call Markers: Detects and marks tool requests in logs
- Functional Design: No OOP, pure functions
cd LocalCode
pip install -r requirements.txtpython main.pyServer starts on http://0.0.0.0:4242
Add to OpenCode provider configuration:
{
"localcode": {
"npm": "@ai-sdk/openai-compatible",
"api": "http://localhost:4242/v1",
"name": "LocalCode",
"models": {
"glm-4.7": {
"id": "glm-4.7",
"name": "GLM-4.7 (via LocalCode)",
"family": "glm-4.7",
"reasoning": true,
"tool_call": true,
"temperature": true,
"cost": {
"input": 0,
"output": 0
},
"limit": {
"context": 204800,
"output": 131072
}
}
}
}
}Or configure via environment:
export OPENCODE_PROVIDER_URL="http://localhost:4242/v1"OpenAI-compatible chat completions endpoint.
Request Format:
{
"model": "glm-4.7",
"messages": [{ "role": "user", "content": "Hello" }],
"stream": false,
"temperature": 0.8,
"max_tokens": 2000
}Response Format:
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "glm-4.7",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello!"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 5,
"total_tokens": 15
}
}Tool Call Example:
{
"model": "glm-4.7",
"messages": [{ "role": "user", "content": "Edit file" }],
"tools": [
{
"type": "function",
"function": {
"name": "edit",
"parameters": {
"type": "object",
"properties": {
"filePath": { "type": "string" },
"oldString": { "type": "string" },
"newString": { "type": "string" }
}
}
}
}
]
}Health check endpoint.
Response:
{
"status": "ok",
"provider": "LocalCode",
"model": "GLM-4.7"
}The middleware prints detailed logs to console:
================================================================================
[REQUEST] 14:32:15
Model: glm-4.7
Stream: false
Messages count: 2
[0] system: You are a coding agent...
[1] user: Edit the function...
[Tool Definitions] 3 tools
[0] edit: Edits a file...
================================================================================
--------------------------------------------------------------------------------
[RESPONSE] 14:32:20
Content: Sure, I'll help you edit the function...
Finish reason: stop
Usage - prompt: 150, completion: 45, total: 195
--------------------------------------------------------------------------------
[Tool Request] Detected tools in request
[Tool Call] edit
Args: {"filePath": "/src/app.ts", "oldString": "...", "newString": "..."}
[STREAM CHUNK] 14:32:18 Sure
[STREAM CHUNK] 14:32:18 , I'll
[STREAM CHUNK] 14:32:18 help
Constants in main.py:
GLM_API_URL = "https://api.z.ai/api/coding/paas/v4"
PORT = 4242
API_KEY = "dummy" # GLM 4.7 is free# Edit main.py
PORT = 8080Logs are always printed to stdout. Redirect to file:
python main.py 2>&1 | tee middleware.loglsof -ti:4242 | xargs kill -9pip install --upgrade pip
pip install -r requirements.txtSame as parent OpenCode project.