diff --git a/packages/autoclaw/index.mjs b/packages/autoclaw/index.mjs index ca4816e..e1d74d0 100644 --- a/packages/autoclaw/index.mjs +++ b/packages/autoclaw/index.mjs @@ -29,8 +29,14 @@ const CHANNEL = "AutoClaw4"; const CLIENT_VERSION = "1.18.1"; const CN_CHANNEL = "AutoClaw26"; const CN_CLIENT_VERSION = "2.1.0"; -const PLATFORM_TM = process.platform === "darwin" ? "mac" : process.platform === "win32" ? "win" : "linux"; -const USER_AGENT = `AutoClaw/${CLIENT_VERSION} (${PLATFORM_TM}-${process.arch})`; +const PLATFORM = process.platform === "darwin" ? "mac" : process.platform === "win32" ? "win" : "linux"; +/** X-Tm: the official Linux client sends "linux-x64"; the overseas gateway + * answers the bare "linux" with 403 {"message":"forbidden"} (#367). */ +function platformTm(platform = process.platform) { + return platform === "darwin" ? "mac" : platform === "win32" ? "win" : "linux-x64"; +} +const PLATFORM_TM = platformTm(); +const USER_AGENT = `AutoClaw/${CLIENT_VERSION} (${PLATFORM}-${process.arch})`; const GLOBAL_HOST = "https://autoglm-api.autoglm.ai"; const CN_HOST = "https://autoglm-api.zhipuai.cn"; @@ -619,7 +625,7 @@ function managedHeaders(token, model) { const rid = randomHex(16); const headers = new Headers(bizHeaders()); headers.set("Accept", "application/json"); - headers.set("User-Agent", `AutoClaw/${CN_CLIENT_VERSION} (${PLATFORM_TM}-${process.arch})`); + headers.set("User-Agent", `AutoClaw/${CN_CLIENT_VERSION} (${PLATFORM}-${process.arch})`); headers.set("X-Channel", CN_CHANNEL); headers.set("X-Version", CN_CLIENT_VERSION); headers.set("X-Trace-Id", rid); @@ -1342,7 +1348,7 @@ export const AutoclawCnAuthPlugin = makeAutoclawPlugin({ export const _internal = { quotaRefusal, accountRefusal, bannedSaid, makePluginForTest: makeAutoclawPlugin, - md5Hex, jwtClaim, tokenFields, codeValue, bizHeaders, bareToken, newDeviceId, + platformTm, md5Hex, jwtClaim, tokenFields, codeValue, bizHeaders, bareToken, newDeviceId, dailySignin, signinStatus, maybeSignin, signinAccountKey, signinConfig, checkinOutcome, refreshTokens, fetchUserProfile, accountDeviceId, accountDisplayName, startLocalOAuth, FLAGGED, unflagged, unflag, autoclawRewriteBody, explained, OFFICIAL_SYSTEM_PROMPT, REFUSED_HINT, diff --git a/packages/autoclaw/index.test.mjs b/packages/autoclaw/index.test.mjs index ae93cb3..0fd4753 100644 --- a/packages/autoclaw/index.test.mjs +++ b/packages/autoclaw/index.test.mjs @@ -612,6 +612,30 @@ test("signinOnUsage signs every account, also two with the same name", async () } }) +// #367 (menhouran): the overseas gateway answers X-Tm "linux" with +// 403 {"message":"forbidden"} for a valid token; the official Linux client +// sends "linux-x64", which it takes +test("X-Tm is the official client's platform id: mac, win, linux-x64, never the bare linux", async () => { + const { platformTm, bizHeaders } = _internal + expect(platformTm("darwin")).toBe("mac") + expect(platformTm("win32")).toBe("win") + expect(platformTm("linux")).toBe("linux-x64") + expect(bizHeaders("t")["X-Tm"]).toBe(platformTm(process.platform)) + expect(bizHeaders("t")["User-Agent"]).not.toMatch(/x64-x64|linux-x64-/) + const realFetch = globalThis.fetch + let sent + globalThis.fetch = async (input, init) => { sent = new Headers(init?.headers); return new Response("{}") } + try { + const hooks = await AutoclawAuthPlugin({ client: { auth: { set: async () => {} } } }) + const loader = await hooks.auth.loader(async () => ({ type: "oauth", access: "a1" })) + await loader.fetch("x", { method: "POST", body: JSON.stringify({ model: "zai_glm-5.3-flash", messages: [{ role: "user", content: "hi" }] }) }) + expect(sent.get("X-Tm")).toBe(platformTm(process.platform)) + expect(sent.get("X-Tm")).not.toBe("linux") + } finally { + globalThis.fetch = realFetch + } +}) + // plugins#274: the CN managed gateway answers a spent quota with a 403 whose // body says so (code 810000); the same token's next request gets as far as // the model check, so it is the quota, not the sign-in diff --git a/packages/autoclaw/package.json b/packages/autoclaw/package.json index 8ab5f09..194f699 100644 --- a/packages/autoclaw/package.json +++ b/packages/autoclaw/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-autoclaw-auth", - "version": "0.2.6", + "version": "0.2.7", "description": "OpenCode provider plugin for AutoClaw (Zhipu OpenClaw): oversea web sign-in (Google/z.ai) and CN phone SMS, multi-account pool, credit balance and daily check-in", "type": "module", "main": "./index.mjs",