@magpie-community/opencode-zcode-auth 0.1.38 → 0.1.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -4
- package/index.mjs +105 -32
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -196,6 +196,19 @@ number written into the plugin would hold a Max account to a Lite one's
|
|
|
196
196
|
with no way to raise it. A user who knows their plan's cap sets it on the
|
|
197
197
|
account or the provider in magpie, and magpie's queue follows that.
|
|
198
198
|
|
|
199
|
+
## Two accounts at once
|
|
200
|
+
|
|
201
|
+
Three ZCode accounts used at once from one address can get the address
|
|
202
|
+
refused with a 405, where two are served (01huadalang on Discord). So the
|
|
203
|
+
plugin declares `maxAccounts` two, on the auth hook and as
|
|
204
|
+
`magpie.maxAccounts` in its `package.json`: magpie sends requests to the
|
|
205
|
+
first two accounts of the user's list only, and holds the others in
|
|
206
|
+
reserve. One that runs out or fails rests, and the first in reserve takes
|
|
207
|
+
its place until it is back. A user changes the number on the provider in
|
|
208
|
+
magpie (Advanced → Accounts at once, or `magpie provider at-once zcode
|
|
209
|
+
<n>`), 0 for every account at once. A magpie older than the one that
|
|
210
|
+
reads it uses every account, as before.
|
|
211
|
+
|
|
199
212
|
## The usage card
|
|
200
213
|
|
|
201
214
|
An account with both plans shows both allowances: the coding plan's
|
|
@@ -233,8 +246,11 @@ left, shows no count.
|
|
|
233
246
|
**Gift plans ZCode holds but has not granted.** ZCode hands out one-off
|
|
234
247
|
trust-build / gift plans (a day's ZCode Trust Build, say) that are not
|
|
235
248
|
on the account until the ZCode app claims them. The plugin reads the
|
|
236
|
-
app's own
|
|
237
|
-
|
|
249
|
+
app's own lists of them with the card and names them there: its claim
|
|
250
|
+
banners' feed (`GET /api/v1/marketing/touch`, each delivery's
|
|
251
|
+
`claim_zcode_plan` button) and `GET /api/v1/zcode-plan/billing/preview`.
|
|
252
|
+
A banner plan (a weekend's ZCODE WEEKEND BUILD, 10-30) is in the feed
|
|
253
|
+
alone, never in the preview (#503) — the window is named "Gift plans to
|
|
238
254
|
claim in the ZCode app" and the display shows "1 · ZCode Trust Build"
|
|
239
255
|
— set aside, so the line stops nothing and routing, usage caps and the
|
|
240
256
|
menu bar pass it over. The name is a fixed sentence magpie can
|
|
@@ -249,13 +265,17 @@ the next read shows it as an ordinary gift bucket.
|
|
|
249
265
|
With ZCode turned on in magpie's Settings → Daily check-in (it is off
|
|
250
266
|
until you turn it on), magpie has the plugin claim each account's gift
|
|
251
267
|
plans once a day (`auth.checkin`; `magpie accounts checkin` asks now). It
|
|
252
|
-
reads the
|
|
268
|
+
reads both lists above (the banners' feed and the preview, each plan
|
|
269
|
+
once) and posts each plan to
|
|
253
270
|
`POST /api/v1/zcode-plan/billing/claim` (`{"plan_id": …}`) with the
|
|
254
271
|
account's ZCode session, as the app does. The answer is judged by the
|
|
255
272
|
body's code, not the HTTP status:
|
|
256
273
|
|
|
257
274
|
- nothing listed: *inactive* (ZCode answers an account with nothing to
|
|
258
|
-
claim `{"code":0,"msg":""}`, with no list at all)
|
|
275
|
+
claim `{"code":0,"msg":""}`, with no list at all). A list that doesn't
|
|
276
|
+
read is *failed*, never nothing to claim. A plan just claimed may show
|
|
277
|
+
in neither `billing/preview` nor `billing/current` until its
|
|
278
|
+
`effective_at`, so neither list is read as "claimed or not";
|
|
259
279
|
- code 0: *claimed* (the next request uses the plan);
|
|
260
280
|
- code 1003 "target already exists" (HTTP 200): *done*, claimed before;
|
|
261
281
|
- code 3007 "captcha verify failed": *captcha*, the plan is claimed in
|
package/index.mjs
CHANGED
|
@@ -644,7 +644,8 @@ async function giftOf(s) {
|
|
|
644
644
|
const CLAIM_HINT = "Gift plans to claim in the ZCode app"
|
|
645
645
|
|
|
646
646
|
// claimHint is the card's line for gift plans ZCode is holding for the
|
|
647
|
-
// account but has not put on it:
|
|
647
|
+
// account but has not put on it: the app's banners (marketing/touch) and
|
|
648
|
+
// GET /billing/preview list them (toClaim). The
|
|
648
649
|
// card only names them: they are claimed in the ZCode app, or by the
|
|
649
650
|
// daily claim (claimDaily) when the user turned it on; on 10-06 a claim
|
|
650
651
|
// without the app's Aliyun captcha attestation was refused (3007). A window, set aside (it is no
|
|
@@ -657,16 +658,10 @@ const CLAIM_HINT = "Gift plans to claim in the ZCode app"
|
|
|
657
658
|
// own.
|
|
658
659
|
async function claimHint(s) {
|
|
659
660
|
if (!s.jwt || jwtExpired(s.jwt)) return null
|
|
660
|
-
|
|
661
|
-
try {
|
|
662
|
-
d = await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/preview?app_version=${APP_VERSION}&platform=${platform()}`, { auth: "Bearer " + s.jwt, device: s.device })
|
|
663
|
-
} catch {
|
|
664
|
-
return null
|
|
665
|
-
}
|
|
666
|
-
const plans = (previewPlans(d) ?? []).filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
|
|
661
|
+
const { plans } = await toClaim(s)
|
|
667
662
|
if (!plans.length) return null
|
|
668
663
|
const n = plans.length
|
|
669
|
-
const plan = first(...plans.map((p) =>
|
|
664
|
+
const plan = first(...plans.map((p) => p.name), "ZCode gift plan")
|
|
670
665
|
return { name: CLAIM_HINT, used: 0, aside: true, display: `${n} · ${plan}` }
|
|
671
666
|
}
|
|
672
667
|
|
|
@@ -691,8 +686,8 @@ const zcodeHeaders = () => ({ "X-ZCode-App-Version": APP_VERSION, "X-Platform":
|
|
|
691
686
|
// "claimed" (code 0), "done" (code 1003, claimed before), "captcha" (the
|
|
692
687
|
// claim wants the app's captcha), else "failed" with why. Only a body
|
|
693
688
|
// that reads decides: an HTTP error without one, or text that isn't
|
|
694
|
-
// JSON, is failed (unknown), never claimed.
|
|
695
|
-
//
|
|
689
|
+
// JSON, is failed (unknown), never claimed. Only the code is read (#503
|
|
690
|
+
// has a real code 0 reply, "Plan claimed successfully.").
|
|
696
691
|
async function claimOne(s, planID) {
|
|
697
692
|
let res, text
|
|
698
693
|
try {
|
|
@@ -732,41 +727,109 @@ function previewPlans(d) {
|
|
|
732
727
|
return Array.isArray(d.plans) ? d.plans : null
|
|
733
728
|
}
|
|
734
729
|
|
|
735
|
-
//
|
|
736
|
-
//
|
|
737
|
-
//
|
|
738
|
-
//
|
|
739
|
-
//
|
|
740
|
-
//
|
|
730
|
+
// TOUCH is ZCode's marketing feed: the ZCode app's claim banners come
|
|
731
|
+
// from it, not from /billing/preview. A banner plan ("ZCODE WEEKEND
|
|
732
|
+
// BUILD", 10-30) is in neither /billing/preview nor /billing/current
|
|
733
|
+
// while the app shows its banner, and the app claims it through the same
|
|
734
|
+
// /billing/claim (#503's capture of 10-30, kongbai01203: GET
|
|
735
|
+
// /api/v1/marketing/touch?seq=<n, counting up from 0> with the session,
|
|
736
|
+
// X-Device-Mid, X-Client-Language and X-ZCode-App-Version, answered
|
|
737
|
+
// data.deliveries[].banner.buttons[].action = {"type":"claim_zcode_plan",
|
|
738
|
+
// "args":{"plan_id":…}} and the plan's name in
|
|
739
|
+
// banner.background.args.zcode_plan.name).
|
|
740
|
+
const TOUCH = "/api/v1/marketing/touch"
|
|
741
|
+
const CLAIM_ACTION = "claim_zcode_plan"
|
|
742
|
+
let touchSeq = 0
|
|
743
|
+
const clientLanguage = () => {
|
|
744
|
+
try {
|
|
745
|
+
return Intl.DateTimeFormat().resolvedOptions().locale || "en-US"
|
|
746
|
+
} catch {
|
|
747
|
+
return "en-US"
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
// touchPlans is the plans marketing/touch's deliveries offer to claim:
|
|
752
|
+
// [{plan_id, name}], [] for a feed with no deliveries, null for one that
|
|
753
|
+
// doesn't read. Each delivery is searched for its claim_zcode_plan
|
|
754
|
+
// actions and its zcode_plan's name, wherever the banner keeps them, so
|
|
755
|
+
// a button moved inside the banner is still found.
|
|
756
|
+
function touchPlans(d) {
|
|
757
|
+
if (d == null) return []
|
|
758
|
+
if (typeof d !== "object" || Array.isArray(d)) return null
|
|
759
|
+
if (d.deliveries == null) return []
|
|
760
|
+
if (!Array.isArray(d.deliveries)) return null
|
|
761
|
+
const out = []
|
|
762
|
+
for (const dv of d.deliveries) {
|
|
763
|
+
const ids = [], names = []
|
|
764
|
+
const walk = (v, depth) => {
|
|
765
|
+
if (!v || typeof v !== "object" || depth > 12) return
|
|
766
|
+
if (Array.isArray(v)) return v.forEach((x) => walk(x, depth + 1))
|
|
767
|
+
if (v.type === CLAIM_ACTION && typeof v.args?.plan_id === "string" && v.args.plan_id.trim()) ids.push(v.args.plan_id.trim())
|
|
768
|
+
if (v.zcode_plan && typeof v.zcode_plan === "object" && typeof v.zcode_plan.name === "string") names.push(v.zcode_plan.name)
|
|
769
|
+
for (const k of Object.keys(v)) walk(v[k], depth + 1)
|
|
770
|
+
}
|
|
771
|
+
walk(dv, 0)
|
|
772
|
+
for (const id of ids) out.push({ plan_id: id, name: first(names[0] ?? "", id) })
|
|
773
|
+
}
|
|
774
|
+
return out
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// toClaim is every gift plan the account can claim: what the app's
|
|
778
|
+
// marketing feed offers (its banners) and what /billing/preview lists,
|
|
779
|
+
// once each by plan_id. unread names a list that didn't read: its
|
|
780
|
+
// plans are unknown, never none.
|
|
781
|
+
async function toClaim(s) {
|
|
782
|
+
const plans = [], unread = []
|
|
783
|
+
const add = (p) => {
|
|
784
|
+
if (!plans.some((q) => q.plan_id === p.plan_id)) plans.push(p)
|
|
785
|
+
}
|
|
786
|
+
const ask = async (what, url, headers, read) => {
|
|
787
|
+
let d
|
|
788
|
+
try {
|
|
789
|
+
d = await call("GET", url, { auth: "Bearer " + s.jwt, device: s.device, headers })
|
|
790
|
+
} catch (e) {
|
|
791
|
+
return unread.push(`${what}: ${e?.message ?? String(e)}`)
|
|
792
|
+
}
|
|
793
|
+
const got = read(d)
|
|
794
|
+
if (!got) return unread.push(`${what}: the plan list doesn't read`)
|
|
795
|
+
got.forEach(add)
|
|
796
|
+
}
|
|
797
|
+
await ask("gift plans to claim", `${ZCODE}${TOUCH}?seq=${touchSeq++}`, { "X-ZCode-App-Version": APP_VERSION, "X-Client-Language": clientLanguage() }, touchPlans)
|
|
798
|
+
await ask("gift plans to claim", `${ZCODE}/api/v1/zcode-plan/billing/preview?app_version=${APP_VERSION}&platform=${platform()}`, zcodeHeaders(), (d) => {
|
|
799
|
+
const l = previewPlans(d)
|
|
800
|
+
return l && l.filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim()).map((p) => ({ plan_id: p.plan_id.trim(), name: first(typeof p.name === "string" ? p.name : "", p.plan_id.trim()) }))
|
|
801
|
+
})
|
|
802
|
+
return { plans, unread }
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// claimDaily claims every gift plan ZCode offers the account (#503:
|
|
806
|
+
// toClaim, the app's banners and its preview), as auth.checkin's answer:
|
|
807
|
+
// inactive when neither lists one, claimed when one was claimed now,
|
|
808
|
+
// done when each was claimed before, captcha when ZCode wants the app's
|
|
809
|
+
// captcha for one (it is claimed in the ZCode app), failed when a list
|
|
810
|
+
// or a claim can't be read. A failed one is asked again later in the
|
|
811
|
+
// day, when a plan claimed now answers 1003.
|
|
741
812
|
// Nothing here marks the account: a failed claim keeps its sign-in.
|
|
742
813
|
async function claimDaily(s) {
|
|
743
814
|
if (!s?.jwt) return { outcome: "ineligible", message: "ZCode's gift plans are claimed with a ZCode sign-in; this account has none" }
|
|
744
815
|
if (jwtExpired(s.jwt)) return { outcome: "failed", message: EXPIRED }
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
d = await call("GET", `${ZCODE}/api/v1/zcode-plan/billing/preview?app_version=${APP_VERSION}&platform=${platform()}`, { auth: "Bearer " + s.jwt, device: s.device, headers: zcodeHeaders() })
|
|
748
|
-
} catch (e) {
|
|
749
|
-
return { outcome: "failed", message: `gift plans to claim: ${e?.message ?? String(e)}` }
|
|
750
|
-
}
|
|
751
|
-
const listed = previewPlans(d)
|
|
752
|
-
if (!listed) return { outcome: "failed", message: "gift plans to claim: the plan list doesn't read" }
|
|
753
|
-
const plans = listed.filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim())
|
|
816
|
+
const { plans, unread } = await toClaim(s)
|
|
817
|
+
if (!plans.length && unread.length) return { outcome: "failed", message: unread.join("; ") }
|
|
754
818
|
if (!plans.length) return { outcome: "inactive", message: "no ZCode gift plan to claim" }
|
|
755
819
|
const got = []
|
|
756
|
-
for (const p of plans) {
|
|
757
|
-
const name = first(typeof p.name === "string" ? p.name : "", p.plan_id)
|
|
758
|
-
got.push({ name, ...(await claimOne(s, p.plan_id.trim())) })
|
|
759
|
-
}
|
|
820
|
+
for (const p of plans) got.push({ name: p.name, ...(await claimOne(s, p.plan_id)) })
|
|
760
821
|
const named = (o) => got.filter((g) => g.outcome === o).map((g) => g.name)
|
|
761
822
|
const claimed = named("claimed")
|
|
762
823
|
const said = [
|
|
763
824
|
claimed.length ? `claimed ${claimed.join(", ")}` : "",
|
|
764
825
|
...got.filter((g) => g.outcome === "captcha").map((g) => `${g.name}: claim it in the ZCode app (${g.why})`),
|
|
765
826
|
...got.filter((g) => g.outcome === "failed").map((g) => `${g.name}: ${g.why}`),
|
|
827
|
+
...unread,
|
|
766
828
|
].filter(Boolean).join("; ")
|
|
767
829
|
// a plan claimed now is used by the next request, not ten minutes on
|
|
768
830
|
if (claimed.length) routes.delete(s.key + "\0" + s.jwt)
|
|
769
|
-
|
|
831
|
+
// a list that didn't read may hold a plan not claimed: asked again
|
|
832
|
+
if (unread.length || got.some((g) => g.outcome === "failed")) return { outcome: "failed", message: said }
|
|
770
833
|
if (got.some((g) => g.outcome === "captcha")) return { outcome: "captcha", message: said }
|
|
771
834
|
if (claimed.length) return { outcome: "claimed", message: said }
|
|
772
835
|
return { outcome: "done", message: `already claimed: ${named("done").join(", ")}` }
|
|
@@ -1683,6 +1746,13 @@ function kept(res, text) {
|
|
|
1683
1746
|
// (https://docs.z.ai/devpack/usage-policy), so a number written here would
|
|
1684
1747
|
// hold a Max account to a Lite one's, with no way for the user to raise it.
|
|
1685
1748
|
const DEFAULT_CONCURRENCY = 3
|
|
1749
|
+
// DEFAULT_ACCOUNTS is how many of a user's ZCode accounts magpie sends
|
|
1750
|
+
// requests to at the same time (maxAccounts, on the auth hook and in
|
|
1751
|
+
// package.json): the first in the user's list, the next taking over once
|
|
1752
|
+
// one runs out or fails. 01huadalang on Discord: three accounts used at
|
|
1753
|
+
// once from one address got the address refused with a 405, two didn't. A
|
|
1754
|
+
// user sets their own on the provider (Accounts at once), 0 for no limit.
|
|
1755
|
+
const DEFAULT_ACCOUNTS = 2
|
|
1686
1756
|
const BUSY_CODES = new Set(["3008", "3009"])
|
|
1687
1757
|
// BUSY_WAITS are the waits between asks, longest last: about ten seconds of
|
|
1688
1758
|
// them. Past that the account is saturated rather than busy, and magpie's
|
|
@@ -2231,6 +2301,9 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2231
2301
|
// own cap follows its tier (Max > Pro > Lite) and the time of day, and
|
|
2232
2302
|
// a user who knows theirs can raise this on the account
|
|
2233
2303
|
maxConcurrency: DEFAULT_CONCURRENCY,
|
|
2304
|
+
// how many accounts take requests at once, the others in reserve
|
|
2305
|
+
// (DEFAULT_ACCOUNTS); a magpie that doesn't read it uses them all
|
|
2306
|
+
maxAccounts: DEFAULT_ACCOUNTS,
|
|
2234
2307
|
async loader(getAuth, provider) {
|
|
2235
2308
|
const s0 = stateOf(await getAuth())
|
|
2236
2309
|
if (!s0) return {}
|
|
@@ -2587,4 +2660,4 @@ export async function ZCodeAuthPlugin({ client }, options = {}) {
|
|
|
2587
2660
|
// config sets what zcodeConfig has read, for tests: null is ZCode's config
|
|
2588
2661
|
// out of reach, undefined forgets it
|
|
2589
2662
|
const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
|
|
2590
|
-
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 }
|
|
2663
|
+
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 }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpie-community/opencode-zcode-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.40",
|
|
4
4
|
"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",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.mjs",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"directory": "packages/zcode"
|
|
15
15
|
},
|
|
16
16
|
"magpie": {
|
|
17
|
-
"maxConcurrency": 3
|
|
17
|
+
"maxConcurrency": 3,
|
|
18
|
+
"maxAccounts": 2
|
|
18
19
|
}
|
|
19
20
|
}
|