diff --git a/packages/zcode/README.md b/packages/zcode/README.md index 4cf7a37..a4d21df 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -196,6 +196,19 @@ number written into the plugin would hold a Max account to a Lite one's with no way to raise it. A user who knows their plan's cap sets it on the account or the provider in magpie, and magpie's queue follows that. +## Two accounts at once + +Three ZCode accounts used at once from one address can get the address +refused with a 405, where two are served (01huadalang on Discord). So the +plugin declares `maxAccounts` two, on the auth hook and as +`magpie.maxAccounts` in its `package.json`: magpie sends requests to the +first two accounts of the user's list only, and holds the others in +reserve. One that runs out or fails rests, and the first in reserve takes +its place until it is back. A user changes the number on the provider in +magpie (Advanced → Accounts at once, or `magpie provider at-once zcode +`), 0 for every account at once. A magpie older than the one that +reads it uses every account, as before. + ## The usage card An account with both plans shows both allowances: the coding plan's diff --git a/packages/zcode/concurrency.test.mjs b/packages/zcode/concurrency.test.mjs index a0dd7e0..d70a08b 100644 --- a/packages/zcode/concurrency.test.mjs +++ b/packages/zcode/concurrency.test.mjs @@ -10,7 +10,7 @@ import { homedir, tmpdir } from "node:os" import { readFileSync, realpathSync } from "node:fs" import { ZCodeAuthPlugin, _internal } from "./index.mjs" -const { busy, spentUp, BUSY_WAITS, DEFAULT_CONCURRENCY } = _internal +const { busy, spentUp, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS } = _internal const WAITS = [...BUSY_WAITS] let server @@ -181,3 +181,14 @@ test("the account's cap is declared, and declared as the same number twice", asy expect(hooks.auth.maxConcurrency).toBe(DEFAULT_CONCURRENCY) expect(pkg.magpie.maxConcurrency).toBe(DEFAULT_CONCURRENCY) }) + +test("two accounts at once are declared, the same number twice", async () => { + // 01huadalang on Discord: three ZCode accounts used at once from one + // address got it refused with a 405, two didn't. magpie keeps the first + // two of the user's list taking requests and the others in reserve + const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8")) + const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } }) + expect(DEFAULT_ACCOUNTS).toBe(2) + expect(hooks.auth.maxAccounts).toBe(DEFAULT_ACCOUNTS) + expect(pkg.magpie.maxAccounts).toBe(DEFAULT_ACCOUNTS) +}) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index fffe39c..79b54b0 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -1746,6 +1746,13 @@ function kept(res, text) { // (https://docs.z.ai/devpack/usage-policy), so a number written here would // hold a Max account to a Lite one's, with no way for the user to raise it. const DEFAULT_CONCURRENCY = 3 +// DEFAULT_ACCOUNTS is how many of a user's ZCode accounts magpie sends +// requests to at the same time (maxAccounts, on the auth hook and in +// package.json): the first in the user's list, the next taking over once +// one runs out or fails. 01huadalang on Discord: three accounts used at +// once from one address got the address refused with a 405, two didn't. A +// user sets their own on the provider (Accounts at once), 0 for no limit. +const DEFAULT_ACCOUNTS = 2 const BUSY_CODES = new Set(["3008", "3009"]) // BUSY_WAITS are the waits between asks, longest last: about ten seconds of // them. Past that the account is saturated rather than busy, and magpie's @@ -2294,6 +2301,9 @@ export async function ZCodeAuthPlugin({ client }, options = {}) { // own cap follows its tier (Max > Pro > Lite) and the time of day, and // a user who knows theirs can raise this on the account maxConcurrency: DEFAULT_CONCURRENCY, + // how many accounts take requests at once, the others in reserve + // (DEFAULT_ACCOUNTS); a magpie that doesn't read it uses them all + maxAccounts: DEFAULT_ACCOUNTS, async loader(getAuth, provider) { const s0 = stateOf(await getAuth()) if (!s0) return {} @@ -2650,4 +2660,4 @@ export async function ZCodeAuthPlugin({ client }, options = {}) { // config sets what zcodeConfig has read, for tests: null is ZCode's config // out of reach, undefined forgets it const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() }) -export const _internal = { config, entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY } +export const _internal = { config, entry, limitWindows, termOf, startUsage, routes, blocked, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS } diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 362cf9d..0d19037 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.39", + "version": "0.1.40", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan, and claims its gift plans daily", "type": "module", "main": "./index.mjs", @@ -14,6 +14,7 @@ "directory": "packages/zcode" }, "magpie": { - "maxConcurrency": 3 + "maxConcurrency": 3, + "maxAccounts": 2 } }