@magpie-community/opencode-qoder-auth 0.2.63 → 0.2.65

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 CHANGED
@@ -162,6 +162,9 @@ itself.
162
162
  reasoning and usage.
163
163
  - Tool calls Qoder writes as XML or JSON in its text become tool calls.
164
164
  Tool calls it sends the OpenAI way keep Qoder's id.
165
+ - Two calls sent the OpenAI way at one index, each with its own id, are
166
+ two calls (since 0.2.65, #930). A piece with no id, or the open call's
167
+ id, is more of the open call.
165
168
  - A call sent the OpenAI way that never gets its function name is never
166
169
  sent on, and its arguments go nowhere (since 0.2.51, #579). If nothing
167
170
  of the answer came before it, the request is sent once more; a second
@@ -189,6 +192,7 @@ itself.
189
192
  |---|---|
190
193
  | A refused sign-in (no code, or code 105) | 401 |
191
194
  | A quota (codes 110, 112–119, 122) | 429 |
195
+ | A model the account's listing has off while its usage says no credits are left (since 0.2.64, #880) | 429 usage limit reached, with the credits used and the model; a model off with credits left, or whose usage can't be read, stays 400 `unknown or disabled model`, and an id Qoder never served is that 400 at once |
192
196
  | Busy or queued for the model (code 10605) | Since 0.2.28 the request waits in Qoder's queue as Qoder's client does (below), then is sent again; since 0.2.62 a streamed request is answered 200 at once and kept alive with SSE comments for up to an hour, its outcome in the stream; past the wait, 503 with `Retry-After` from Qoder's `retryAfterSeconds` (30s when it gives none). Since 0.2.40, when magpie has another candidate after this request (`x-magpie-fallback: 1`), the 503 comes at once, saying the queue and how many are ahead, and magpie asks the next. The sign-in is untouched |
193
197
  | A refusal of the request itself (codes 406, 416, 430, 80411, 80412, 100400–100403) | 400, with Qoder's words |
194
198
  | "Error in upstream response", the model's own vendor behind Qoder failing, which Qoder sends as a 400 (since 0.2.24) | 502, so magpie asks again or another account; the sign-in is untouched |
@@ -484,7 +488,8 @@ the account's credentials.
484
488
  (`Qoder.app` on macOS, `%LOCALAPPDATA%\Programs\Qoder` or, since
485
489
  0.2.26, `%ProgramFiles%\Qoder` on Windows, `/opt/Qoder` on Linux) or the runtime the CLI caches in `~/.qoder/.bin`
486
490
  (`runtime-info-<platform>-<arch>-*` on macOS and Linux,
487
- `umid-win32-x64-*\runtime-info.exe` on Windows; `QODER_CONFIG_DIR`,
491
+ `umid-win32-x64-*\runtime-info.exe` or, as qodercli 1.21.2 caches it
492
+ since 0.2.64, `runtime-info-win32-x64-*.exe` on Windows; `QODER_CONFIG_DIR`,
488
493
  `QODER_CLI_HOME` and `QODER_CONFIG_DIR_NAME` move it as they move the
489
494
  CLI's folder).
490
495
  - For another installation location, set `QODER_RUNTIME_INFO` to the
package/index.mjs CHANGED
@@ -2224,6 +2224,7 @@ async function* events(body, tools = []) {
2224
2224
  const a = new Splitter(tools)
2225
2225
  let index = -1 // the tool call being given
2226
2226
  const native = new Map() // upstream index -> call and its output index
2227
+ const begun = [] // every native call, one an index has moved past too
2227
2228
  // one piece of the split text as an event: a call, text, or a call that
2228
2229
  // can't be read, which ends the turn as a 502
2229
2230
  const piece = (f) =>
@@ -2281,9 +2282,14 @@ async function* events(body, tools = []) {
2281
2282
  const args = tc.function?.arguments ?? ""
2282
2283
  if (!tc.id && !name && !args) continue
2283
2284
  let call = native.get(i)
2284
- if (!call) {
2285
+ // A new id at an index already in use is a new call: Qoder Ultimate
2286
+ // has sent two calls at index 0, and the second's arguments ran
2287
+ // into the first's as {"a"}{"b"} (plugins#930). No id, or the same
2288
+ // one, is the open call going on.
2289
+ if (!call || (tc.id && call.id && tc.id !== call.id)) {
2285
2290
  call = { id: "", args: "" }
2286
2291
  native.set(i, call)
2292
+ begun.push(call)
2287
2293
  }
2288
2294
  if (tc.id) call.id = tc.id
2289
2295
  if (call.index === undefined) {
@@ -2331,7 +2337,7 @@ async function* events(body, tools = []) {
2331
2337
  // text, reasoning and named calls already given are kept: qoder-cn's
2332
2338
  // qfmodel did this now and then, and the whole turn failed with an
2333
2339
  // error the client didn't retry (plugins#579).
2334
- const nameless = [...native.values()].filter((call) => call.index === undefined).length
2340
+ const nameless = begun.filter((call) => call.index === undefined).length
2335
2341
  if (nameless && !said) {
2336
2342
  yield { error: { status: 502, message: NAMELESS, nameless: true } }
2337
2343
  return
@@ -2607,10 +2613,16 @@ async function campaignRuntime({ home = homedir(), platform = process.platform,
2607
2613
  const cache = join(config, ".bin")
2608
2614
  const entries = await readdir(cache, { withFileTypes: true }).catch(() => [])
2609
2615
  const found = []
2616
+ // qodercli 1.1.66 cached it on Windows as a folder umid-win32-<arch>-<hash>
2617
+ // holding runtime-info.exe and sgsdk.dll; qodercli 1.21.2 caches the file
2618
+ // itself, runtime-info-win32-<arch>-<hash>.exe, beside sgsdk.dll in .bin,
2619
+ // as elsewhere (plugins#879). Either is taken, the newest first.
2610
2620
  for (const d of entries) {
2611
2621
  let path
2612
- if (platform === "win32") {
2613
- if (d.isDirectory() && d.name.startsWith(`umid-win32-${arch}-`)) path = join(cache, d.name, name)
2622
+ if (platform === "win32" && d.isDirectory()) {
2623
+ if (d.name.startsWith(`umid-win32-${arch}-`)) path = join(cache, d.name, name)
2624
+ } else if (platform === "win32") {
2625
+ if (d.isFile() && d.name.startsWith(`runtime-info-win32-${arch}-`) && d.name.toLowerCase().endsWith(".exe")) path = join(cache, d.name)
2614
2626
  } else if (d.isFile() && d.name.startsWith(`runtime-info-${platform}-${arch}-`) && !d.name.endsWith(".tmp")) {
2615
2627
  path = join(cache, d.name)
2616
2628
  }
@@ -2811,6 +2823,31 @@ function when(v) {
2811
2823
  // account used up (magpie-community/plugins#454).
2812
2824
  const SPENT_IN_TURN = new Set(["Credits", "Add-on credits", "Shared credits"])
2813
2825
 
2826
+ // spentOf is the credits a usage envelope says the account has used of
2827
+ // what it has ("800 / 800 credits") when the pools spent in turn
2828
+ // (SPENT_IN_TURN) have none left together, and "" when they have some, or
2829
+ // when it says no pool at all. A free account's pools of 0 have none.
2830
+ function spentOf(env) {
2831
+ const u = env?.qoderUsage
2832
+ if (env?.displayMode !== "qoder" || !u || typeof u !== "object" || Array.isArray(u)) return ""
2833
+ let used = 0
2834
+ let total = 0
2835
+ let pools = 0
2836
+ for (const [a, b] of [["userQuota", "user_quota"], ["addOnQuota", "add_on_quota"], ["orgResourcePackage", "org_resource_package"]]) {
2837
+ const p = u[a] !== undefined ? u[a] : u[b]
2838
+ if (p === null || typeof p !== "object" || Array.isArray(p)) continue
2839
+ const t = p.total ?? p.cap
2840
+ if (typeof t !== "number" || !Number.isFinite(t) || t < 0) continue
2841
+ const n = typeof p.used === "number" ? p.used : typeof p.remaining === "number" ? t - p.remaining : NaN
2842
+ if (t > 0 && !Number.isFinite(n)) return ""
2843
+ pools++
2844
+ total += t
2845
+ used += t > 0 ? Math.min(Math.max(n, 0), t) : 0
2846
+ }
2847
+ if (!pools || used < total) return ""
2848
+ return `${gfmt(used)} / ${gfmt(total)} credits`
2849
+ }
2850
+
2814
2851
  // parseUsage is the plan and each pool of credits the envelope tells. When
2815
2852
  // more than one of the pools spent in turn holds credits, "Total credits"
2816
2853
  // is what they hold together, the one routing counts, and each pool is
@@ -3252,11 +3289,16 @@ const makePlugin = (site) => async ({ client }) => {
3252
3289
  if (!l || again) {
3253
3290
  const s = siteOf(site, cred)
3254
3291
  const listing = await fetchListing(s, cred)
3292
+ const off = new Set()
3255
3293
  for (const m of s.modelScene ? (Array.isArray(listing?.[s.modelScene]) ? listing[s.modelScene] : []) : sceneModels(listing)) {
3256
3294
  const k = String(m?.key ?? "").trim()
3257
3295
  if (k) served.add(k)
3296
+ if (k && !m.enable) off.add(k)
3258
3297
  }
3259
3298
  l = modelInfos(listing, s)
3299
+ // the keys this listing names and has off (ask tells a model off for
3300
+ // the credits apart by them)
3301
+ l.off = off
3260
3302
  if (!s.modelScene) l.freeView = freeView(sceneModels(listing))
3261
3303
  listings.set(key, l)
3262
3304
  }
@@ -3350,19 +3392,40 @@ const makePlugin = (site) => async ({ client }) => {
3350
3392
  const env = await fetchUsage(siteOf(site, cred), cred.access)
3351
3393
  return { ...(await spareFree(cred, parseUsage(env))), signIn, ...who }
3352
3394
  }
3353
- let env
3354
- try {
3355
- env = await fetchUsage(siteOf(site, cred), cred.deviceToken)
3356
- } catch (e) {
3357
- if (!(e instanceof UsageStatus) || (e.status !== 401 && e.status !== 403)) throw e
3358
- env = await fetchUsage(siteOf(site, cred), await deviceToken(getAuth, cred.deviceToken))
3359
- }
3360
- return { ...(await spareFree(cred, parseUsage(env))), signIn, ...who }
3395
+ return { ...(await spareFree(cred, parseUsage(await usageEnv(getAuth, cred)))), signIn, ...who }
3361
3396
  } catch (e) {
3362
3397
  return { error: e?.message ?? String(e), signIn: e?.expired ? "expired" : signIn }
3363
3398
  }
3364
3399
  }
3365
3400
 
3401
+ // usageEnv is the account's usage envelope on its device token, rotated
3402
+ // once when refused, as usage reads it
3403
+ const usageEnv = async (getAuth, cred) => {
3404
+ try {
3405
+ return await fetchUsage(siteOf(site, cred), cred.deviceToken)
3406
+ } catch (e) {
3407
+ if (!(e instanceof UsageStatus) || (e.status !== 401 && e.status !== 403)) throw e
3408
+ return fetchUsage(siteOf(site, cred), await deviceToken(getAuth, cred.deviceToken))
3409
+ }
3410
+ }
3411
+
3412
+ // creditsSpent is the credits the account has used of what it has
3413
+ // ("800 / 800 credits") when its usage says none are left (spentOf), and
3414
+ // "" when some are, or when it can't be read. Qoder and Qoder CN only:
3415
+ // QwenWork reads its usage elsewhere. A PAT account's is read on its job
3416
+ // token, as usage reads it.
3417
+ const creditsSpent = async (getAuth) => {
3418
+ if (site.usagePath) return ""
3419
+ try {
3420
+ const cred = await fresh(getAuth)
3421
+ if (cred.lingma) return ""
3422
+ if (cred.patAuth && !cred.deviceToken) return spentOf(await fetchUsage(siteOf(site, cred), cred.access))
3423
+ return spentOf(await usageEnv(getAuth, cred))
3424
+ } catch {
3425
+ return ""
3426
+ }
3427
+ }
3428
+
3366
3429
  // campaign is a campaigns page (the daily check-in) asked as the account:
3367
3430
  // a refused device token is rotated once, as usage does, and asked again
3368
3431
  const campaign = async (getAuth, input, url, init) => {
@@ -3514,6 +3577,26 @@ const makePlugin = (site) => async ({ client }) => {
3514
3577
  // could be refused and renew it. An id Qoder has never served is said
3515
3578
  // unknown at once, with no renewal spent on it.
3516
3579
  if (!m && getAuth && knows(chat.model)) {
3580
+ // Off because the credits are used up (plugins#880): on an
3581
+ // international personal_professional_trial account Qoder's listing
3582
+ // had dfmodel off once the 800 credits were spent, and on again
3583
+ // when the daily credits came back. That is the account's quota,
3584
+ // not the request's fault, so it is said as Qoder's own quota codes
3585
+ // are (429 usage limit reached), which a routing group fails over
3586
+ // on and rests the account for. Only a model the live listing names
3587
+ // and has off, on an account whose usage read says no credits are
3588
+ // left; a usage that can't be read says nothing, and the model goes
3589
+ // on to the renewal and the 400 below as before.
3590
+ if ((await models(cred)).off?.has(chat.model)) {
3591
+ const spent = await creditsSpent(getAuth)
3592
+ if (spent) {
3593
+ return errorResponse({
3594
+ status: 429,
3595
+ message: `usage limit reached: the account's credits are used up (${spent}), and Qoder lists "${chat.model}" off for it until they refresh`,
3596
+ signIn: "",
3597
+ })
3598
+ }
3599
+ }
3517
3600
  relogged = true
3518
3601
  const next = await relogin(getAuth, cred.access)
3519
3602
  if (next) {
@@ -3999,4 +4082,4 @@ export async function QwenWorkEnterpriseAuthPlugin({ client }) {
3999
4082
  export const protocol = { makePlugin, deviceSignIn, openapi, siteOf }
4000
4083
 
4001
4084
  // for tests
4002
- export const _internal = { OUTPUT_CAPS, LEARNED_CAPS, outputCap, fetchListing, accountIdentity, deviceRefreshBody, qwenworkClientHeaders, freeView, maskPhone, phoneLabelOf, realEmailOf, chatHeaders, machineOS, machineHostname, cosyHeaders, typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, dedicatedOrigin, dedicatedHost, dedicatedSite, lingmaRoots, lingmaDecode, lingmaSite, lingmaHeaders, LINGMA_MODELS, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
4085
+ export const _internal = { spentOf, OUTPUT_CAPS, LEARNED_CAPS, outputCap, fetchListing, accountIdentity, deviceRefreshBody, qwenworkClientHeaders, freeView, maskPhone, phoneLabelOf, realEmailOf, chatHeaders, machineOS, machineHostname, cosyHeaders, typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, dedicatedOrigin, dedicatedHost, dedicatedSite, lingmaRoots, lingmaDecode, lingmaSite, lingmaHeaders, LINGMA_MODELS, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
package/opencode2.mjs CHANGED
@@ -85,8 +85,10 @@ export function fieldsOf(prompts) {
85
85
  if (p.placeholder) f.placeholder = String(p.placeholder)
86
86
  if (p.type === "select" && Array.isArray(p.options))
87
87
  f.options = p.options.map((o) => ({ value: String(o.value), label: String(o.label ?? o.value), ...(o.hint ? { description: String(o.hint) } : {}) }))
88
- // a prompt shown only when another answer says so can't be required
89
- if (!p.condition) f.required = true
88
+ // a prompt shown only when another answer says so can't be required, and
89
+ // neither can one whose own validate() accepts an empty answer (the
90
+ // browser sign-in's optional API key)
91
+ if (!p.condition && p.required !== false) f.required = true
90
92
  out.push(f)
91
93
  }
92
94
  return out.length ? out : undefined
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.63",
3
+ "version": "0.2.65",
4
4
  "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship, VPC and dedicated domains",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",