Skip to content

Commit 3961cc8

Browse files
authored
Merge pull request #34 from demian-bot/fix/start-plan-anthropic-gateway
fix(start-plan): route to the live Anthropic gateway Fixed start-plan endpoints and updated to new url
2 parents d5d6df1 + 15d756e commit 3961cc8

9 files changed

Lines changed: 308 additions & 114 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,22 +238,22 @@ Client Request
238238
Proxy API Key Auth (shared secret)
239239
│
240240
▼
241-
Route Detection + Plan-aware Routing (v2.3: coding-plan mirrors the real ZCode client)
241+
Route Detection + Plan-aware Routing (both plans post Anthropic upstream, v2.4+)
242242
/v1/chat/completions (OpenAI client format)
243243
├─ coding-plan → TRANSLATE OpenAI→Anthropic → provider's anthropic endpoint
244244
│ (remapped to zcode.z.ai ultra endpoints via server-controlled mapping)
245-
└─ start-plan → zcode.z.ai OpenAI-compatible gateway (JWT + captcha), passthrough
245+
└─ start-plan → TRANSLATE OpenAI→Anthropic → zcode.z.ai
246+
/api/v1/zcode-plan/anthropic/v1/messages (JWT + captcha)
246247
/v1/messages (Anthropic client format)
247248
├─ coding-plan → NATIVE PASSTHROUGH to the provider's anthropic endpoint (same format)
248-
└─ start-plan → TRANSLATE Anthropic→OpenAI → zcode.z.ai gateway
249+
└─ start-plan → NATIVE PASSTHROUGH → zcode.z.ai
250+
/api/v1/zcode-plan/anthropic/v1/messages (JWT + captcha)
249251
/v1/responses (Responses client format)
250-
├─ coding-plan → TRANSLATE Responses→Chat→Anthropic → anthropic endpoint
251-
└─ start-plan → TRANSLATE Responses→Chat → gateway
252+
├─ both plans → TRANSLATE Responses→Chat→Anthropic → plan's anthropic endpoint
252253
│
253254
▼
254255
Body Transformation (ZCode-equivalent mutations)
255256
Anthropic upstream → cache_control on last message + metadata.user_id (oauth)
256-
OpenAI streaming → inject stream_options.include_usage
257257
start-plan → prepend ZCode system messages
258258
│
259259
▼

‎src/provider/models.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ export const MODELS: ModelDef[] = [
2222
{ id: "glm-5.1", name: "GLM 5.1", contextWindow: 200_000, maxOutputTokens: 128_000, reasoning: true },
2323
{ id: "glm-5.2", name: "GLM 5.2", contextWindow: 1_000_000, maxOutputTokens: 128_000, reasoning: true },
2424
{ id: "glm-5.3", name: "GLM 5.3", contextWindow: 1_000_000, maxOutputTokens: 128_000, reasoning: true },
25+
// start-plan gateway serves the -flash variant (used by claimed trial plans
26+
// like the weekend package); advertised so client-side discovery lists it.
27+
{ id: "glm-5.3-flash", name: "GLM 5.3 Flash", contextWindow: 1_000_000, maxOutputTokens: 128_000, reasoning: true },
2528
];

‎src/provider/providers.test.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ describe("providers", () => {
3737
});
3838

3939
describe("models", () => {
40-
it("MODELS contains exactly the 10 pinned coding-plan models", () => {
41-
expect(MODELS).toHaveLength(10);
40+
it("MODELS contains exactly the 11 pinned coding-plan models", () => {
41+
expect(MODELS).toHaveLength(11);
4242
const ids = MODELS.map((m) => m.id);
4343
expect(ids).toEqual([
4444
"glm-4.5-air", "glm-4.6", "glm-4.6v", "glm-4.7",
4545
"glm-5", "glm-5-turbo", "glm-5v-turbo", "glm-5.1", "glm-5.2", "glm-5.3",
46+
"glm-5.3-flash",
4647
]);
4748
});
4849

@@ -55,20 +56,23 @@ describe("models", () => {
5556
}
5657
});
5758

58-
it("all models except glm-5.2/glm-5.3 have 200k context", () => {
59+
it("all models except glm-5.2/glm-5.3/glm-5.3-flash have 200k context", () => {
5960
for (const m of MODELS) {
60-
if (m.id === "glm-5.2" || m.id === "glm-5.3") continue;
61+
if (m.id === "glm-5.2" || m.id === "glm-5.3" || m.id === "glm-5.3-flash") continue;
6162
expect(m.contextWindow).toBe(200_000);
6263
}
6364
});
6465

65-
it("glm-5.2 and glm-5.3 have 1M context", () => {
66+
it("glm-5.2, glm-5.3 and glm-5.3-flash have 1M context", () => {
6667
const glm52 = MODELS.find((m) => m.id === "glm-5.2");
6768
expect(glm52).toBeDefined();
6869
expect(glm52!.contextWindow).toBe(1_000_000);
6970
const glm53 = MODELS.find((m) => m.id === "glm-5.3");
7071
expect(glm53).toBeDefined();
7172
expect(glm53!.contextWindow).toBe(1_000_000);
73+
const glm53f = MODELS.find((m) => m.id === "glm-5.3-flash");
74+
expect(glm53f).toBeDefined();
75+
expect(glm53f!.contextWindow).toBe(1_000_000);
7276
});
7377

7478
it("includes key GLM models", () => {

‎src/proxy/captcha-happy.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,14 @@ async function createDom(region, prefix) {
17511751
const HOST_CRITICAL_GLOBALS = new Set([
17521752
"process", "Bun", "console", "performance", "crypto", "fetch",
17531753
"queueMicrotask", "structuredClone", "TextEncoder", "TextDecoder",
1754-
"requestAnimationFrame", "cancelAnimationFrame", "print",
1754+
"requestAnimationFrame", "cancelAnimationFrame",
1755+
// NOTE: `print` was removed from this list (2026-08-29). The polyfill
1756+
// defines a harmless no-op on the window, but the alias pass skipped it,
1757+
// so under Bun (guest scripts run in the HOST realm) the Aliyun pe risk
1758+
// engine hit a bare `print` reference → ReferenceError → broken
1759+
// fingerprint chain → degraded solve success rate (711 WINDOW-ERRORs in
1760+
// one day). Bun's host global has no native `print`, so aliasing the
1761+
// stub shadows nothing critical.
17551762
"URL", "URLSearchParams", "AbortController", "AbortSignal",
17561763
"ReadableStream", "WritableStream", "TransformStream", "Blob", "File",
17571764
"FormData", "Headers", "Request", "Response", "Event", "EventTarget",
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* Tests for the two resilience behaviors added after the PR #34 review:
3+
*
4+
* 1. In-body captcha challenge detection: a start-plan upstream response with
5+
* HTTP 400 + {"code":3007,...} in the JSON body (no captcha header) must
6+
* be treated as a captcha challenge and retried with a fresh token.
7+
* 2. Connect-retry freshness: after a connect-level failure, the retried
8+
* dispatch must receive a FRESH Request (body not marked used). A mock
9+
* fetch that throws-then-succeeds does NOT catch the bug (mocks don't
10+
* enforce bodyUsed), so the mock asserts `req.bodyUsed === false` on
11+
* every call per the review's test note.
12+
*
13+
* Both tests use the start-plan path with an injected captcha module via
14+
* `mock.module("./captcha.js")` (same technique as captcha-pool.test.ts).
15+
*/
16+
import { describe, it, expect, mock } from "bun:test";
17+
import { proxyRequest } from "./handler.js";
18+
import type { ProxyConfig, ProxyIdentity } from "../config/types.js";
19+
import { AuthManager } from "../auth/manager.js";
20+
21+
const IDENTITY: ProxyIdentity = {
22+
appVersion: "test-1.0.0",
23+
sourceTitle: "cli",
24+
refererOrigin: "https://zcode.z.ai",
25+
};
26+
27+
const TEST_CONFIG: ProxyConfig = {
28+
server: { port: 8080, host: "0.0.0.0" },
29+
auth: { mode: "apikey", apiKey: "testk...ret" },
30+
provider: "zai",
31+
plan: "start-plan",
32+
providers: {
33+
zai: { anthropicBase: "https://api.z.ai/api/anthropic", openaiBase: "https://api.z.ai/api/coding/paas/v4" },
34+
bigmodel: { anthropicBase: "https://open.bigmodel.cn/api/anthropic", openaiBase: "https://open.bigmodel.cn/api/coding/paas/v4" },
35+
},
36+
defaultModel: "glm-4.6",
37+
models: ["glm-4.6"],
38+
identity: IDENTITY,
39+
clientIdentity: { mode: "observe", ttlSeconds: 900, maxSessions: 1024 },
40+
responses: { enabled: true, storeMaxEntries: 1000, storeTtlMs: 86400000 },
41+
endpointRouting: { enabled: false, origin: "https://zcode.z.ai" },
42+
clientSigning: { enabled: false, origin: "https://zcode.z.ai" },
43+
mcp: { enabled: true, webSearch: true, webReader: false, zread: false },
44+
async: { enabled: false, origin: "https://zcode.z.ai", pollIntervalMs: 5000, keepAliveIntervalMs: 3000, maxWaitMs: 0, maxRetries: 3, settleTimeoutMs: 8000, controlTimeoutMs: 15000, defaultModel: "" },
45+
claim: { enabled: false, auto: true, origin: "https://zcode.z.ai", pollIntervalMs: 300000, cooldownMs: 600000, planId: "" },
46+
logging: { level: "info" },
47+
};
48+
49+
const GATEWAY_URL = "https://zcode.z.ai/api/v1/zcode-plan/anthropic/v1/messages";
50+
51+
const ANTHROPIC_OK = JSON.stringify({
52+
id: "msg_resilience",
53+
type: "message",
54+
role: "assistant",
55+
model: "glm-4.6",
56+
content: [{ type: "text", text: "resilience reply" }],
57+
stop_reason: "end_turn",
58+
stop_sequence: null,
59+
usage: { input_tokens: 5, output_tokens: 3 },
60+
});
61+
62+
describe("proxyRequest — start-plan resilience (PR #34 review P1/P3)", () => {
63+
it("retries an in-body 3007 captcha challenge with a fresh token", async () => {
64+
// Mock the captcha module: config enabled, token take returns distinct
65+
// tokens per call so we can assert the retry used a FRESH token.
66+
let tokenSeq = 0;
67+
mock.module("./captcha.js", () => ({
68+
detectCaptchaChallenge: (resp: Response): string | null => {
69+
const v = resp.headers.get("x-aliyun-captcha-verify-param");
70+
return v && v.trim().length > 0 ? v.trim() : null;
71+
},
72+
getCaptchaToken: async (_appVersion: string) => {
73+
tokenSeq += 1;
74+
return { verifyParam: `tok-${tokenSeq}`, region: "sgp" };
75+
},
76+
RETRY_HEADERS: { PARAM: "x-aliyun-captcha-verify-param", REGION: "x-aliyun-captcha-verify-region" },
77+
}));
78+
79+
// Upstream: first call = HTTP 400 with {"code":3007} in the body (no
80+
// captcha header), second call = success. The mock also records the
81+
// captcha header of each call so we can assert the retry used a FRESH
82+
// token (tok-2, not the consumed tok-1).
83+
const seenCaptchaHeaders: (string | null)[] = [];
84+
let calls = 0;
85+
const fetchMock = mock(async (req: Request): Promise<Response> => {
86+
if (req.url.includes("/client/configs")) {
87+
return new Response(JSON.stringify({ data: { configs: { captcha: { enabled: true } } } }), {
88+
status: 200, headers: { "content-type": "application/json" },
89+
});
90+
}
91+
calls += 1;
92+
seenCaptchaHeaders.push(req.headers.get("x-aliyun-captcha-verify-param"));
93+
if (calls === 1) {
94+
return new Response(JSON.stringify({ code: 3007, msg: "captcha verify failed" }), {
95+
status: 400, headers: { "content-type": "application/json" },
96+
});
97+
}
98+
return new Response(ANTHROPIC_OK, { status: 200, headers: { "content-type": "application/json" } });
99+
});
100+
101+
const auth = new AuthManager({ mode: "oauth", provider: "zai" });
102+
auth.setOAuthCredential({ apiKey: "key-mock", provider: "zai", jwt: "jwt-mock" });
103+
const clientReq = new Request("http://localhost:8080/v1/chat/completions", {
104+
method: "POST",
105+
headers: { "content-type": "application/json" },
106+
body: '{"model":"glm-4.6","messages":[{"role":"user","content":"hi"}]}',
107+
});
108+
109+
const resp = await proxyRequest(clientReq, "openai", { config: TEST_CONFIG, auth, fetchImpl: fetchMock as any });
110+
111+
// The in-body 3007 challenge was detected and retried with a fresh token.
112+
expect(calls).toBe(2);
113+
expect(seenCaptchaHeaders[0]).toBe("tok-1");
114+
expect(seenCaptchaHeaders[1]).toBe("tok-2");
115+
expect(resp.status).toBe(200);
116+
const body = await resp.json();
117+
expect(body.choices[0].message.content).toBe("resilience reply");
118+
});
119+
120+
it("retries a connect failure with a FRESH Request (body not marked used)", async () => {
121+
// Upstream: first call = connect-level failure (the bug shape), second
122+
// call = success. The mock asserts req.bodyUsed === false on EVERY call
123+
// per the review's test note: mocks don't enforce bodyUsed the way real
124+
// fetch does, so the freshness assertion must be explicit.
125+
let calls = 0;
126+
const fetchMock = mock(async (req: Request): Promise<Response> => {
127+
calls += 1;
128+
// The freshness assertion: if the handler re-dispatched the SAME
129+
// Request object, this would be true on the second call.
130+
expect(req.bodyUsed).toBe(false);
131+
if (calls === 1) {
132+
throw new Error("Unable to connect. Is the computer able to access the url?");
133+
}
134+
return new Response(ANTHROPIC_OK, { status: 200, headers: { "content-type": "application/json" } });
135+
});
136+
137+
const auth = new AuthManager({ mode: "oauth", provider: "zai" });
138+
auth.setOAuthCredential({ apiKey: "key-mock", provider: "zai", jwt: "jwt-mock" });
139+
const clientReq = new Request("http://localhost:8080/v1/chat/completions", {
140+
method: "POST",
141+
headers: { "content-type": "application/json" },
142+
body: '{"model":"glm-4.6","messages":[{"role":"user","content":"hi"}]}',
143+
});
144+
145+
const resp = await proxyRequest(clientReq, "openai", { config: TEST_CONFIG, auth, fetchImpl: fetchMock as any });
146+
147+
// The connect failure was retried with a FRESH Request (bodyUsed false
148+
// on the second call — the freshness assertion inside the mock passed).
149+
expect(calls).toBe(2);
150+
expect(resp.status).toBe(200);
151+
const body = await resp.json();
152+
expect(body.choices[0].message.content).toBe("resilience reply");
153+
});
154+
});

‎src/proxy/handler.ts‎

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,16 @@ export async function proxyRequest(
122122
return errorResponse(503, "credential_unavailable", (err as Error).message);
123123
}
124124

125-
// v2.3 shape alignment: coding-plan mirrors the real ZCode client — Anthropic
126-
// upstream (api.z.ai/api/anthropic → ultra via endpoint routing). start-plan
127-
// stays on the zcode.z.ai OpenAI gateway.
125+
// v2.6: both plans use the Anthropic upstream. coding-plan mirrors the real
126+
// ZCode client (api.z.ai/api/anthropic → ultra via endpoint routing);
127+
// start-plan's old OpenAI gateway (/api/v1/zcode-plan/chat/completions) was
128+
// retired server-side (404 as of 2026-08-28) — the live desktop client now
129+
// posts Anthropic messages to /api/v1/zcode-plan/anthropic/v1/messages with
130+
// the start-plan JWT, so we do the same (no OpenAI translation either way).
128131
const startPlan = config.plan === "start-plan";
129-
const translateAnthropicToOpenAI = format === "anthropic" && startPlan;
130-
const translateOpenAIToAnthropic = format === "openai" && !startPlan;
131-
const upstreamFormat: Format = startPlan ? "openai" : "anthropic";
132+
const translateAnthropicToOpenAI = false;
133+
const translateOpenAIToAnthropic = format === "openai";
134+
const upstreamFormat: Format = "anthropic";
132135
const clientSession = resolveSessionContext({ clientReq, body, upstreamFormat, model: meta.model, config });
133136
if (debug && clientSession) {
134137
const shortSession = clientSession.sessionId ? clientSession.sessionId.slice(0, 10) : "-";
@@ -223,7 +226,34 @@ export async function proxyRequest(
223226

224227
let upstreamResp: Response;
225228
try {
226-
upstreamResp = await dispatch(upstreamReq, upstreamHeaderPairs);
229+
// Transient connect failures (DNS blip, TLS reset, Bun "Unable to
230+
// connect") happen a few times a day against the gateway. Retry the
231+
// CONNECT twice with a short backoff before surfacing a 502 — the
232+
// request never reached upstream, so resending is side-effect-free.
233+
// Guard rails (review P1/P2 + nits): skip retry when the client already
234+
// aborted, and re-dispatch a FRESH Request each attempt — a reused
235+
// Request has its body stream marked used after the first fetch, so
236+
// attempt 2+ would throw "Request body already used" before any I/O
237+
// (start-plan hits the plain pass-through path where dispatch does NOT
238+
// rebuild the Request).
239+
const maxConnectAttempts = 3;
240+
for (let attempt = 1; ; attempt++) {
241+
if (clientReq.signal.aborted) throw new Error("client aborted before upstream connect");
242+
try {
243+
upstreamResp = await dispatch(upstreamReq, upstreamHeaderPairs);
244+
break;
245+
} catch (err) {
246+
if (attempt >= maxConnectAttempts) throw err;
247+
const backoffMs = 500 * attempt;
248+
if (debug) debugError(reqId, "upstream_connect_retry", `attempt ${attempt}/${maxConnectAttempts - 1} failed (${(err as Error).message}), retrying in ${backoffMs}ms`);
249+
console.log(`${reqId} upstream connect failed (${(err as Error).message}), retry ${attempt + 1}/${maxConnectAttempts} in ${backoffMs}ms`);
250+
await new Promise((r) => setTimeout(r, backoffMs));
251+
// Rebuild the Request so the body stream is fresh for the next
252+
// dispatch (mirrors the captcha retry, which already constructs a
253+
// new Request per attempt).
254+
upstreamReq = buildUpstreamRequest(clientReq, upstreamFormat, provider, cred, transformedBody, config.identity, config.plan, captchaHeaders, clientSession);
255+
}
256+
}
227257
} catch (err) {
228258
if (debug) debugError(reqId, "upstream_unreachable", (err as Error).message);
229259
printRow(reqId, format, meta, 502, started, Date.now(), 0, 0, 0);
@@ -257,7 +287,24 @@ export async function proxyRequest(
257287
// getCaptchaToken takes the next pre-solved one). `&& captcha` looks
258288
// redundant but is required for TS null-narrowing.
259289
const captcha = startPlan ? await loadCaptcha() : null;
260-
const captchaChallenge = captcha ? captcha.detectCaptchaChallenge(upstreamResp) : null;
290+
let captchaChallenge = captcha ? captcha.detectCaptchaChallenge(upstreamResp) : null;
291+
// In-body challenge variant (observed 2026-08-29): the gateway sometimes
292+
// returns the challenge as HTTP 400 with {"code":3007,...} in the JSON body
293+
// instead of the captcha response header, so detectCaptchaChallenge never
294+
// fires and the client just gets a raw 502. Peek a clone of the body (error
295+
// responses are small; the original body stays untouched for the retry or
296+
// the error path) and treat it as a challenge too.
297+
if (!captchaChallenge && captcha && !upstreamResp.ok) {
298+
const ctype = upstreamResp.headers.get("content-type") ?? "";
299+
if (!ctype.includes("text/event-stream")) {
300+
try {
301+
const peek = await upstreamResp.clone().text();
302+
if (peek.includes('"code":3007') || peek.includes('"code": 3007')) {
303+
captchaChallenge = "in-body-3007";
304+
}
305+
} catch {}
306+
}
307+
}
261308
if (captchaChallenge && captcha) {
262309
if (debug) debugLine(reqId, "captcha challenge — re-solving and retrying once");
263310
try { upstreamResp.body?.cancel(); } catch {}

‎src/proxy/responses-handler.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ export async function handleResponses(
144144
const providerDef = resolveProviderDef(opts.config);
145145

146146
// ── 5. body transform (start-plan system / anthropic cache_control + user_id) ──
147+
// Both plans post Anthropic upstream (mirrors handler.ts): the start-plan
148+
// OpenAI gateway was retired server-side (404 as of 2026-08-28), so the
149+
// Responses → Chat → Anthropic translator chain runs unconditionally.
147150
const startPlan = opts.config.plan === "start-plan";
148-
const upstreamFormat: "openai" | "anthropic" = startPlan ? "openai" : "anthropic";
151+
const upstreamFormat: "openai" | "anthropic" = "anthropic";
149152
let upstreamRequestBody: string;
150-
if (upstreamFormat === "anthropic") {
151-
// v2.3: coding-plan upstream is Anthropic (mirrors the real client).
152-
// Chain the existing translators: Responses → Chat → Anthropic request;
153-
// Anthropic → Chat → Responses on the way back.
153+
{
154154
let anthropicReq: AnthropicMessagesRequest;
155155
try {
156156
anthropicReq = translateRequestOpenAIToAnthropic(chatRequest);
@@ -160,14 +160,8 @@ export async function handleResponses(
160160
upstreamRequestBody = transformRequestBody(JSON.stringify(anthropicReq), {
161161
format: "anthropic",
162162
userId: cred.userId,
163-
startPlan: false,
164-
}) ?? JSON.stringify(anthropicReq);
165-
} else {
166-
upstreamRequestBody = transformRequestBody(JSON.stringify(chatRequest), {
167-
format: "openai",
168-
userId: undefined,
169163
startPlan,
170-
}) ?? JSON.stringify(chatRequest);
164+
}) ?? JSON.stringify(anthropicReq);
171165
}
172166
const transformedBody = upstreamRequestBody;
173167

0 commit comments

Comments
 (0)