@magpie-community/opencode-zcode-auth 0.1.65 → 0.1.67
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 +8 -1
- package/index.mjs +75 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -117,7 +117,14 @@ the plans again on the next request:
|
|
|
117
117
|
(`/api/biz/subscription/list`), its requests go to that plan. A model
|
|
118
118
|
the account's gift plans also serve is listed again as
|
|
119
119
|
`<model>-Trial`; asking that entry spends the gift (see below).
|
|
120
|
-
- Otherwise they go to the gift plans.
|
|
120
|
+
- Otherwise they go to the gift plans. A model the gift plans' balance
|
|
121
|
+
doesn't name (an agent left on GLM-5.2 while the gift serves only
|
|
122
|
+
GLM-5.3-Flash) would spend none of the free quota, so it is answered
|
|
123
|
+
with the models the balance names and not sent. The balance is the one
|
|
124
|
+
the model list read; a model it doesn't name has it read again first.
|
|
125
|
+
When the plan list can't be read, an account whose last read found no
|
|
126
|
+
plan stays on the gift plans rather than sending its key to the coding
|
|
127
|
+
plan's host, which bills a key with no plan pay-as-you-go.
|
|
121
128
|
|
|
122
129
|
Every request carries the key as both `x-api-key` and
|
|
123
130
|
`Authorization: Bearer`.
|
package/index.mjs
CHANGED
|
@@ -263,13 +263,20 @@ async function plansOf(s) {
|
|
|
263
263
|
// Plan's 405 until the route was read again (#369)
|
|
264
264
|
if (start) sure = !(s.key && r && !r.start)
|
|
265
265
|
} catch {
|
|
266
|
-
// the plan list could not be read
|
|
267
|
-
//
|
|
268
|
-
//
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
266
|
+
// the plan list could not be read. An account the last read found with
|
|
267
|
+
// no coding plan stays on the Start Plan for the minute: the dual route
|
|
268
|
+
// would send its key to the coding plan's host, which bills a key with
|
|
269
|
+
// no plan pay-as-you-go while the free quota sits unspent (bg5eau on
|
|
270
|
+
// X). Any other key account takes the dual route (start stays false,
|
|
271
|
+
// the gift is kept, a minute's ttl), so a gift refusal still replays to
|
|
272
|
+
// the coding plan rather than blacking out
|
|
273
|
+
if (r?.start) start = true
|
|
274
|
+
else {
|
|
275
|
+
try {
|
|
276
|
+
gift = await startPlan(s.jwt, s.device)
|
|
277
|
+
} catch {}
|
|
278
|
+
}
|
|
279
|
+
sure = false
|
|
273
280
|
}
|
|
274
281
|
const out = { start, gift, at: Date.now(), ttl: Math.min(sure ? 600_000 : 60_000, gift?.ttl ?? 600_000) }
|
|
275
282
|
routes.set(id, out)
|
|
@@ -724,6 +731,26 @@ async function claimHint(s) {
|
|
|
724
731
|
// only that there is none. Kept ten minutes per session, so an agent
|
|
725
732
|
// retrying the pick doesn't read the feed each time; a read that fails
|
|
726
733
|
// names none and is not kept.
|
|
734
|
+
// aloneSeen is a gift-only account's balance as last read (the model list
|
|
735
|
+
// reads it), kept ten minutes. A request reads no balance on its own: only
|
|
736
|
+
// a model the kept balance doesn't name has it read again (aloneGift), so
|
|
737
|
+
// a gift claimed since is not refused. A read that fails is not kept.
|
|
738
|
+
const aloneSeen = new Map()
|
|
739
|
+
const aloneNames = (s) => {
|
|
740
|
+
const c = aloneSeen.get(s.jwt)
|
|
741
|
+
return c && Date.now() - c.at < 600_000 ? giftNames(c.gift) : []
|
|
742
|
+
}
|
|
743
|
+
async function aloneGift(s) {
|
|
744
|
+
let gift
|
|
745
|
+
try {
|
|
746
|
+
gift = await startPlan(s.jwt, s.device)
|
|
747
|
+
} catch {
|
|
748
|
+
return null
|
|
749
|
+
}
|
|
750
|
+
aloneSeen.set(s.jwt, { at: Date.now(), gift })
|
|
751
|
+
return gift
|
|
752
|
+
}
|
|
753
|
+
|
|
727
754
|
const unclaimedSeen = new Map()
|
|
728
755
|
async function unclaimed(s) {
|
|
729
756
|
if (!s.jwt || jwtExpired(s.jwt)) return []
|
|
@@ -1072,7 +1099,11 @@ async function claimDaily(s) {
|
|
|
1072
1099
|
// translate it; why goes in the display. Set aside: it stops nothing.
|
|
1073
1100
|
const GIFT_SIGN_IN = "ZCode gift plans (-Trial models) need ZCode's sign-in"
|
|
1074
1101
|
function giftSignIn(s, key) {
|
|
1075
|
-
if (!s.jwt)
|
|
1102
|
+
if (!s.jwt) {
|
|
1103
|
+
if (key) return { name: GIFT_SIGN_IN, used: 0, aside: true, display: "not read with an API key: add the account with a ZCode sign-in" }
|
|
1104
|
+
if (s.source === "zcode" && ownStoreUnreadable()) return { name: GIFT_SIGN_IN, used: 0, aside: true, display: UNREADABLE_SIGN_IN }
|
|
1105
|
+
return { name: GIFT_SIGN_IN, used: 0, aside: true, display: "this sign-in has no ZCode session: sign in again" }
|
|
1106
|
+
}
|
|
1076
1107
|
if (jwtExpired(s.jwt)) return { name: GIFT_SIGN_IN, used: 0, aside: true, display: "ZCode's sign-in has expired: sign in again" }
|
|
1077
1108
|
return null
|
|
1078
1109
|
}
|
|
@@ -1583,6 +1614,20 @@ function decrypt(key, v) {
|
|
|
1583
1614
|
}
|
|
1584
1615
|
}
|
|
1585
1616
|
|
|
1617
|
+
// UNREADABLE_SIGN_IN says why a sign-in "as ZCode" has no session to
|
|
1618
|
+
// read: ZCode 3.18 writes credentials.json's values as "enc:v2:os:…",
|
|
1619
|
+
// which looks bound to the system's own key store, and this plugin reads
|
|
1620
|
+
// enc:v1 alone (#1196).
|
|
1621
|
+
const UNREADABLE_SIGN_IN = "ZCode 3.18 keeps its sign-in in a new form this plugin can't read yet: add the account with a Z.ai or BigModel sign-in"
|
|
1622
|
+
|
|
1623
|
+
// ownStoreUnreadable says whether ZCode's credentials.json holds values
|
|
1624
|
+
// in a form decrypt can't read.
|
|
1625
|
+
function ownStoreUnreadable() {
|
|
1626
|
+
const store = readJSON(join(zcodeDir(), "credentials.json"))
|
|
1627
|
+
if (!store || typeof store !== "object") return false
|
|
1628
|
+
return Object.values(store).some((v) => typeof v === "string" && v.startsWith("enc:") && !v.startsWith("enc:v1:"))
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1586
1631
|
function readJSON(path) {
|
|
1587
1632
|
try {
|
|
1588
1633
|
return JSON.parse(readFileSync(path, "utf8"))
|
|
@@ -1849,7 +1894,7 @@ function asZCode() {
|
|
|
1849
1894
|
method: "auto",
|
|
1850
1895
|
async callback() {
|
|
1851
1896
|
const own = ownSignIn()
|
|
1852
|
-
if (!own) return { type: "failed", error: "ZCode isn't signed in on this computer; sign in to ZCode, or sign in to Z.ai or BigModel here" }
|
|
1897
|
+
if (!own) return { type: "failed", error: ownStoreUnreadable() ? UNREADABLE_SIGN_IN : "ZCode isn't signed in on this computer; sign in to ZCode, or sign in to Z.ai or BigModel here" }
|
|
1853
1898
|
const { user, uid, ...s } = own
|
|
1854
1899
|
const state = { source: "zcode", device: anyDevice, ...s }
|
|
1855
1900
|
return { type: "success", refresh: JSON.stringify(state), access: s.key || s.jwt || "", expires: 0, accountId: user, ...(uid ? { uid } : {}) }
|
|
@@ -2992,7 +3037,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
2992
3037
|
const s = await located(stateOf(auth))
|
|
2993
3038
|
if (!s) return provider.models
|
|
2994
3039
|
const { start, gift: known } = await plansOf(s).catch(() => ({ start: false, gift: null }))
|
|
2995
|
-
const gift = start ? await
|
|
3040
|
+
const gift = start ? await aloneGift(s) : known
|
|
2996
3041
|
const base = start ? START_BASE : s.base
|
|
2997
3042
|
const cfg = await zcodeConfig().catch(() => null)
|
|
2998
3043
|
let ms = modelsOf(cfg, planID(base))
|
|
@@ -3169,6 +3214,25 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
3169
3214
|
m = raw
|
|
3170
3215
|
freeGift = true
|
|
3171
3216
|
giftName = giftID(raw)
|
|
3217
|
+
} else if (alone && /\/v1\/messages$/.test(new URL(url).pathname)) {
|
|
3218
|
+
// a gift-only account's Start Plan serves the models its
|
|
3219
|
+
// balance names, and ZCode's app offers only those
|
|
3220
|
+
// (resolveStartPlanBalanceAvailability). Any other model
|
|
3221
|
+
// spends none of the free quota, so it is not sent: an
|
|
3222
|
+
// agent left on another model would be billed while the
|
|
3223
|
+
// gift stayed unspent (bg5eau on X). The balance is the
|
|
3224
|
+
// one the model list read (aloneSeen); a model it doesn't
|
|
3225
|
+
// name has it read again before the refusal. One not read
|
|
3226
|
+
// yet, one that fails, or one naming none leaves the
|
|
3227
|
+
// request as it was.
|
|
3228
|
+
const named = (ns) => ns.some((x) => x.toLowerCase() === m.toLowerCase())
|
|
3229
|
+
const kept0 = aloneNames(s)
|
|
3230
|
+
const names = kept0.length && !named(kept0) ? giftNames(await aloneGift(s)) : []
|
|
3231
|
+
if (names.length && !named(names)) {
|
|
3232
|
+
const shown = uniqueModels(names.map(wireModel))
|
|
3233
|
+
const error = { type: "invalid_request_error", message: `ZCode's Start Plan on this account serves ${shown.join(", ")}; ${m} is not among them and would spend none of its free quota, so it was not sent. Pick ${shown.length > 1 ? "one of those" : shown[0]} for this account` }
|
|
3234
|
+
return kept(new Response(JSON.stringify({ type: "error", error }), { status: 400, headers: { "content-type": "application/json" } }))
|
|
3235
|
+
}
|
|
3172
3236
|
}
|
|
3173
3237
|
start = alone || forcedGift || freeGift
|
|
3174
3238
|
}
|
|
@@ -3524,4 +3588,4 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
|
|
3524
3588
|
// config sets what zcodeConfig has read, for tests: null is ZCode's config
|
|
3525
3589
|
// out of reach, undefined forgets it
|
|
3526
3590
|
const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() })
|
|
3527
|
-
export const _internal = { wallAnswer, unreached, planFull, notePlan, planFullUntil, locatedKeys, lastKeys, edge, EDGE_HOLD, busyHold, startGate, NARROW_FOR, limitOf, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, unclaimed, unclaimedSeen, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, queryOf, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS }
|
|
3591
|
+
export const _internal = { wallAnswer, unreached, planFull, notePlan, planFullUntil, locatedKeys, lastKeys, edge, EDGE_HOLD, busyHold, startGate, NARROW_FOR, limitOf, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, unclaimed, unclaimedSeen, aloneGift, aloneSeen, aloneNames, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, giftSignIn, located, dress, queryOf, 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.67",
|
|
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": "./opencode.mjs",
|