This repository provides an OpenAI-compatible proxy and CLI for GLM on chat.z.ai.
This project is exprimental and no need to say that the official GLM API is away better from the whole repo.
Use this exact order:
- Install and build.
npm run install:all
npm run build:all- Install Playwright once, then login from CLI.
cd ts_glm
npx playwright install
npm run start:cli -- login- Start the local GLM proxy.
npm run start:proxy- Open OpenCode, choose model
glm-local/glm-4.7, then go.
opencode- Optional quick one-liner test (no UI selection needed).
opencode run -m glm-local/glm-4.7 "Say hello in one line"This README is organized around 5 code-backed pillars:
- Runtime map: where request handling, planning, guards, and confirmation logic actually live.
- Verified runbook: commands that match current
package.jsonscripts and CLI commands. - Guard behavior: what is allowed, what is confirmed, and what is blocked.
- Configuration truth table: env vars and defaults from
ts_glm/src/proxy/constants.ts. - Failure-driven troubleshooting: common issues (including
questionpayload shape errors) with valid payload examples.
TypeScript is the active implementation path.
| Area | Source |
|---|---|
| Proxy entrypoint | ts_glm/src/proxy/index.ts |
| Main request handler | ts_glm/src/proxy/handler.ts |
| Request prep (tools, directives, history, heuristics) | ts_glm/src/proxy/handler/request.ts |
| Planner flow | ts_glm/src/proxy/handler/plannerFlow.ts |
| Raw tool-call dispatch | ts_glm/src/proxy/handler/rawDispatchFlow.ts |
| Tool-call validation and guards | ts_glm/src/proxy/handler/guardValidation.ts |
| Mutation boundary logic | ts_glm/src/proxy/handler/guardMutation.ts |
| Confirmation builder | ts_glm/src/proxy/handler/guardValidation/confirm.ts |
| Shell safety (allowlist+denylist) | ts_glm/src/proxy/tools/shellSafety.ts, web_wrapper/src/safety.ts |
| Path safety | ts_glm/src/proxy/tools/pathSafety.ts |
| CLI entrypoint | ts_glm/src/cli.ts |
Python (glm_cli/, glm_proxy/) remains legacy-compatible and read-only by default.
npm run install:all
npm run build:allnpm --prefix ts_glm run start:cli -- config --token "YOUR_TOKEN"Token loading order is:
GLM_TOKENenv var~/.config/glm-cli/config.json
The CLI also writes GLM_TOKEN=... into .env in repo root when possible.
npm --prefix ts_glm run start:proxyDefault bind is http://127.0.0.1:8787 (HOST and PORT are supported).
npm --prefix ts_glm run start:cli -- --help
npm --prefix ts_glm run start:cli -- chats
npm --prefix ts_glm run start:cli -- new --title "My Chat" --model glm-4.7
npm --prefix ts_glm run start:cli -- chat CHAT_ID "Hello"
npm --prefix ts_glm run start:cli -- interactive
npm --prefix ts_glm run start:cli -- whoamicd ts_glm
npx playwright install
npm run start:cli -- loginUseful options from login command:
--check--channel <chrome|chromium|msedge>--connect-cdp <url>--remote-debugging-port <port>--chrome-bin <path>--allow-guest
GET /v1/modelsandGET /modelsPOST /v1/chat/completionsandPOST /chat/completions
curl -X POST http://127.0.0.1:8787/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "glm-4.7",
"messages": [{"role":"user","content":"Say hello"}],
"stream": false
}'You can pass flags directly in request body (enable_thinking, web_search, auto_web_search) or with prompt directives:
/thinking on|off/think on|off/search on|off/web on|off/auto-search on|off
Directives are stripped before model content is sent.
- Tool calls are validated by
validateToolCalls(). - Guard failures are usually confirmable if a confirmation tool exists (
question,askquestion,confirm, etc.). - Non-confirmable guard reasons are limited (
unexpected_arg,invalid_content_type). read/listwith missing path are auto-repaired to".".writewithout content still requires confirmation (or blocks if no confirm tool).tool_choicemismatches are routed through confirmation flow when possible.- Unknown
tool_choicefunction name is returned as content error early. - Planner mutation boundary allows at most one mutation action in a planner response.
PROXY_MAX_ACTIONS_PER_TURNdefault is unlimited (0means no cap).- Raw tool-call early gate no longer drops unknown non-mutation tool names.
- Shell allowlist checks are active only when
PROXY_ALLOW_ANY_COMMAND=0. rm/rmdir/delare only treated as dangerous by heuristic when high-risk flags/targets are present.
If client already exposes a question/askquestion tool, proxy uses that schema. If no confirmation tool is provided, proxy injects a minimal fallback:
{
"question": "Proceed?"
}If your tool expects questions[], each option must be an object (not a plain string):
{
"questions": [
{
"header": "Confirm",
"id": "confirm_abc123",
"question": "Proceed with this action?",
"options": [
{
"label": "Proceed (Recommended)",
"description": "Approve the action and continue."
},
{
"label": "Cancel",
"description": "Decline the action and stop."
}
]
}
]
}If you pass string options like "Yes", schema validation can fail with errors like “expected object, received string”.
Key proxy env vars and defaults:
| Env var | Default | Effect |
|---|---|---|
PROXY_DEBUG |
0 |
Enable proxy debug logs/dumps |
PROXY_NEW_CHAT_PER_REQUEST |
0 |
Create fresh GLM chat per request |
PROXY_ALLOW_WEB_SEARCH |
1 |
Allow web tools (webfetch, websearch) |
PROXY_ALLOW_NETWORK |
1 |
Allow networked shell commands |
PROXY_ALLOW_EXPLICIT_MUTATIONS |
1 |
Allow mutation tools from explicit tool calls |
PROXY_ALLOW_RAW_MUTATIONS |
1 |
Allow mutation tools from raw tool_calls |
PROXY_ALLOW_ANY_COMMAND |
1 |
Skip shell allowlist-first checks |
PROXY_CONFIRM_DANGEROUS_COMMANDS |
1 |
Require confirmation for dangerous commands |
PROXY_MAX_ACTIONS_PER_TURN |
0 |
Max actions per turn (<=0 = unlimited) |
PROXY_TOOL_LOOP_LIMIT |
0 |
Max tool loop rounds (<=0 = unlimited) |
PROXY_PLANNER_MAX_RETRIES |
1 |
Planner parse retry attempts |
PROXY_PLANNER_COERCE |
1 |
Coerce planner failures into direct final answer |
PROXY_ALLOW_USER_HEURISTICS |
1 |
Enable fallback heuristic tool inference |
PROXY_USE_GLM_HISTORY |
1 |
Reuse GLM-side history across requests |
PROXY_STRIP_HISTORY |
0 |
Strip prior history from incoming messages |
PROXY_DEFAULT_THINKING |
1 |
Enable thinking by default |
PROXY_TOOL_PROMPT_SCHEMA_MAX_CHARS |
500 |
Max schema chars included per tool when schema inclusion is enabled |
PROXY_TOOL_PROMPT_EXTRA_SYSTEM_MAX_CHARS |
500 |
Max chars from extra system instructions merged into tool prompt |
PROXY_WORKSPACE_ROOT |
unset | Optional workspace roots for path containment |
Context sizing vars also exist (PROXY_CONTEXT_*) in ts_glm/src/proxy/constants.ts.
opencode.json is preconfigured for:
- Provider:
glm-local - Base URL:
http://127.0.0.1:8787/v1 - Model:
glm-4.7
Run:
opencode run -m glm-local/glm-4.7 "Your prompt"For detailed OpenCode-specific notes and file-attachment workarounds, see OPENCODE_USAGE.md.
- Changes in
web_wrapper/:
npm --prefix web_wrapper run test- Changes in
ts_glm/src/:
npm --prefix ts_glm run build
npm --prefix ts_glm run test- Cross-cutting protocol/request format changes:
npm run build:all
npm --prefix web_wrapper run test- Python legacy changes:
python -m py_compile glm_cli/*.py glm_proxy/*.pyPython implementations (glm_cli/, glm_proxy/) are legacy-compatible.
By default, Python mode is read-only for safer behavior:
- Proxy tool-call emission is disabled.
- Mutation/auth commands are blocked unless explicitly re-enabled.
To temporarily re-enable legacy mutation/tool behavior:
export GLM_PY_LEGACY_ENABLE_MUTATIONS=1
export GLM_PY_LEGACY_ENABLE_TOOLS=1MIT