cloudflare / cloudflare/moltworker

OpenAI-compatible gateways (like z.ai) incorrectly detected as Anthropic causing crash

Open
#97 1 comment 8 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
10k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

The current logic in `start-moltbot.sh` and `src/gateway/env.ts` assumes that any OpenAI-compatible gateway must have a URL ending in `/openai`.
Some providers, like z.ai (`https://api.z.ai/api/paas/v4`), conform to the OpenAI API spec but do not have a URL ending in `/openai`.

When using z.ai as `AI_GATEWAY_BASE_URL`, the gateway incorrectly identifies the provider as Anthropic (the fallback).
This leads to the container sending `ANTHROPIC_API_KEY` instead of `OPENAI_API_KEY`, causing the Moltbot/Clawdbot process to crash with `ProcessExitedBeforeReadyError` or fail to initialize correctly.

- `start-moltbot.sh` (Line ~220)
- `src/gateway/env.ts` (Line ~14)

## Proposed Fix
Update the detection logic to check for known OpenAI-compatible domains or be less restrictive.

### `start-moltbot.sh`
const baseUrl = (process.env.AI_GATEWAY_BASE_URL || process.env.ANTHROPIC_BASE_URL || '').replace(/\/+$/, '');
// Fix: Include z.ai and other openai-compatible domains
const isOpenAI = baseUrl.endsWith('/openai') ||
baseUrl.includes('z.ai') ||
baseUrl.includes('openai.com');

### `src/gateway/env.ts`
// Fix: Check for z.ai
const isOpenAIGateway = normalizedBaseUrl?.endsWith('/openai') ||
normalizedBaseUrl?.includes('z.ai') ||
normalizedBaseUrl?.includes('openai.com');

Inside `start-moltbot.sh`:
Use the improved detection logic to set correct keys.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.