@magpie-community/opencode-qoder-auth 0.2.35 → 0.2.37
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 +13 -0
- package/index.mjs +253 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -144,6 +144,13 @@ itself.
|
|
|
144
144
|
the agent or magpie can try again. A call whose JSON is whole when the
|
|
145
145
|
stream ends without its close is still the call; a tag in code or prose
|
|
146
146
|
stays text.
|
|
147
|
+
- A call written in Anthropic's or Qwen's tags with no `<tool_call>`
|
|
148
|
+
around it (since 0.2.36, #323), `<function_calls>` around one or more
|
|
149
|
+
`<invoke name="…"><parameter name="k">v</parameter></invoke>`, or
|
|
150
|
+
`<function=name><parameter=k>v</parameter></function>`, is a call too,
|
|
151
|
+
but only for a tool the request declared; for any other, or in
|
|
152
|
+
backticks or a code fence, it stays text. A parameter's value is JSON
|
|
153
|
+
unless the tool's schema says string, in these and in `<tool_call>`.
|
|
147
154
|
|
|
148
155
|
**Errors**
|
|
149
156
|
|
|
@@ -264,6 +271,12 @@ on this same protocol, with the desktop client's client id, an RSA key the
|
|
|
264
271
|
Qoder one's equal, its own `qwork` scene, and the device token as the chat
|
|
265
272
|
token. The public cloud's sign-in is the desktop app's device flow, with
|
|
266
273
|
`redirect_uri=qwenwork-cn://`; usage reads the sign-in's account-context.
|
|
274
|
+
A personal account's quota there has `total` and `used` null, only
|
|
275
|
+
`remaining`, so the card shows what is left as its balance, with each pool
|
|
276
|
+
(daily, monthly, long-term) from `/api/v1/adapter/user/wallets`, as
|
|
277
|
+
QwenWork's own client reads them; no share used is made up. An account
|
|
278
|
+
with a total keeps its used / total window, and a biz account (or the
|
|
279
|
+
enterprise flagship) never asks for the wallets.
|
|
267
280
|
There is no PAT and no daily check-in on it: the `pat: false` and
|
|
268
281
|
`checkin: false` site fields turn both off, so Qoder's device identity is
|
|
269
282
|
never sent there.
|
package/index.mjs
CHANGED
|
@@ -1273,8 +1273,9 @@ const FUNC = /\x3cfunction=([^>]+)\x3e([\s\S]*?)\x3c\/function\x3e/
|
|
|
1273
1273
|
const PARAM = /\x3cparameter=([^>]+)\x3e([\s\S]*?)\x3c\/parameter\x3e/g
|
|
1274
1274
|
|
|
1275
1275
|
// parseCall reads one tool-call block: JSON {name, arguments}, or the XML
|
|
1276
|
-
// function/parameter form
|
|
1277
|
-
|
|
1276
|
+
// function/parameter form, its values typed as the tool's schema says
|
|
1277
|
+
// (xmlArgs).
|
|
1278
|
+
function parseCall(v, tools = []) {
|
|
1278
1279
|
try {
|
|
1279
1280
|
const f = JSON.parse(v.trim())
|
|
1280
1281
|
if (String(f?.name ?? "").trim() && f.arguments && typeof f.arguments === "object" && !Array.isArray(f.arguments))
|
|
@@ -1282,18 +1283,106 @@ function parseCall(v) {
|
|
|
1282
1283
|
} catch {}
|
|
1283
1284
|
const m = FUNC.exec(v)
|
|
1284
1285
|
if (!m || !m[1].trim()) return null
|
|
1286
|
+
const name = m[1].trim()
|
|
1287
|
+
const props = propsOf(tools, name)
|
|
1285
1288
|
const args = {}
|
|
1286
1289
|
for (const pm of m[2].matchAll(PARAM)) {
|
|
1287
1290
|
const key = pm[1].trim()
|
|
1288
1291
|
if (!key) continue
|
|
1289
|
-
|
|
1290
|
-
try {
|
|
1291
|
-
args[key] = JSON.parse(val)
|
|
1292
|
-
} catch {
|
|
1293
|
-
args[key] = val
|
|
1294
|
-
}
|
|
1292
|
+
args[key] = valueOf(pm[2].trim(), props[key])
|
|
1295
1293
|
}
|
|
1296
|
-
return { name
|
|
1294
|
+
return { name, args: JSON.stringify(args) }
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
// propsOf is the parameters the request declared for tool name
|
|
1298
|
+
const propsOf = (tools, name) => (tools.find((t) => (t.function ?? t)?.name === name)?.function ?? {}).parameters?.properties ?? {}
|
|
1299
|
+
|
|
1300
|
+
// valueOf is a parameter's value: its text where the tool's schema says
|
|
1301
|
+
// string, else its JSON (an array a tool takes), else its text (#323: a
|
|
1302
|
+
// string holding [1, 2] became an array)
|
|
1303
|
+
function valueOf(v, prop) {
|
|
1304
|
+
if (prop?.type === "string") return v
|
|
1305
|
+
try {
|
|
1306
|
+
return JSON.parse(v.trim())
|
|
1307
|
+
} catch {
|
|
1308
|
+
return v
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// Anthropic's and Qwen's call tags, written bare (no <tool_call> around
|
|
1313
|
+
// them), as Kimi, MiMo and Qwen models may (#323, as trae's since 0.2.19):
|
|
1314
|
+
// where one starts, an opener still being written, the openers a wrapper
|
|
1315
|
+
// is followed by, the next call's opener, a parameter's opener, and what
|
|
1316
|
+
// is left after a call
|
|
1317
|
+
const XML_AT = /<(?:antml:)?(?:function_calls>|invoke\s+name\s*=|function=)/
|
|
1318
|
+
const XML_HOLD = /<(?:antml:)?(?:invoke\s[^<>]{0,300}|function=[^<>]{0,300})$/
|
|
1319
|
+
const XML_STARTS = ["<invoke ", "<antml:invoke ", "<function="]
|
|
1320
|
+
const XML_MARKS = ["<function_calls>", "<antml:function_calls>", ...XML_STARTS]
|
|
1321
|
+
const XML_NEXT = /<(?:antml:)?invoke\s+name\s*=|<function=/
|
|
1322
|
+
const XML_PARAM = /<(?:antml:)?parameter(?:\s+name\s*=\s*"([^"]+)"|=([^\s>]+))\s*>/g
|
|
1323
|
+
const XML_CLOSES = ["</function_calls>", "</antml:function_calls>", "</invoke>", "</antml:invoke>", "</function>"]
|
|
1324
|
+
const XML_LEFT = /^(?:\s+|<\/(?:antml:)?(?:function_calls|invoke|function)>)+/
|
|
1325
|
+
|
|
1326
|
+
// xmlArgs reads the parameters of an invoke or function body, typed by
|
|
1327
|
+
// the tool's schema; closed is whether every one was closed
|
|
1328
|
+
function xmlArgs(s, props) {
|
|
1329
|
+
const opens = [...s.matchAll(XML_PARAM)]
|
|
1330
|
+
const args = {}
|
|
1331
|
+
let closed = opens.length > 0
|
|
1332
|
+
opens.forEach((m, k) => {
|
|
1333
|
+
let v = s.slice(m.index + m[0].length, k + 1 < opens.length ? opens[k + 1].index : s.length)
|
|
1334
|
+
const c = v.search(/<\/(?:antml:)?parameter>/)
|
|
1335
|
+
if (c >= 0) v = v.slice(0, c)
|
|
1336
|
+
else closed = false
|
|
1337
|
+
args[m[1] ?? m[2]] = valueOf(v.replace(/^\r?\n|\r?\n$/g, ""), props[m[1] ?? m[2]])
|
|
1338
|
+
})
|
|
1339
|
+
return { args, closed }
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
// xmlCall reads a bare call in Anthropic's tags (<function_calls> around
|
|
1343
|
+
// one or more <invoke name="…"><parameter name="k">v</parameter>
|
|
1344
|
+
// </invoke>) or Qwen's (<function=…><parameter=k>v</parameter>
|
|
1345
|
+
// </function>), s starting at its first tag: {call, rest}, {wait} while
|
|
1346
|
+
// it may still become one, {text} when it is no call (a tool the request
|
|
1347
|
+
// didn't declare, a tag named in prose), {error} for one cut short. Only
|
|
1348
|
+
// a declared tool's call counts: these tags are common in prose.
|
|
1349
|
+
function xmlCall(s, end, tools) {
|
|
1350
|
+
const w = s.match(/^<(?:antml:)?function_calls>/)
|
|
1351
|
+
if (w) {
|
|
1352
|
+
const rest = s.slice(w[0].length).replace(/^\s+/, "")
|
|
1353
|
+
if (!end && (!rest || XML_STARTS.some((k) => k.startsWith(rest)))) return { wait: true }
|
|
1354
|
+
s = rest
|
|
1355
|
+
}
|
|
1356
|
+
let m = s.match(/^<(?:antml:)?invoke\s+name\s*=\s*"([^"<>]+)"\s*>/)
|
|
1357
|
+
let closeRe = /<\/(?:antml:)?invoke\s*>/
|
|
1358
|
+
if (!m) {
|
|
1359
|
+
m = s.match(/^<function=([^\s<>]+?)\s*>/)
|
|
1360
|
+
closeRe = /<\/function\s*>/
|
|
1361
|
+
}
|
|
1362
|
+
if (!m) {
|
|
1363
|
+
if (!end && (XML_STARTS.some((k) => k.startsWith(s)) || /^<(?:antml:)?(?:invoke\s|function=)[^<>]{0,300}$/.test(s))) return { wait: true }
|
|
1364
|
+
return { text: true }
|
|
1365
|
+
}
|
|
1366
|
+
const name = m[1].trim()
|
|
1367
|
+
if (!tools.some((t) => (t.function ?? t)?.name === name)) return { text: true }
|
|
1368
|
+
const body = s.slice(m[0].length)
|
|
1369
|
+
const c = body.match(closeRe)
|
|
1370
|
+
const n = body.search(XML_NEXT)
|
|
1371
|
+
let stop = c ? c.index : -1
|
|
1372
|
+
if (n >= 0 && (stop < 0 || n < stop)) stop = n
|
|
1373
|
+
if (stop < 0) {
|
|
1374
|
+
if (!end) return { wait: true }
|
|
1375
|
+
stop = body.length
|
|
1376
|
+
}
|
|
1377
|
+
const whole = !!c && c.index === stop
|
|
1378
|
+
const p = xmlArgs(body.slice(0, stop), propsOf(tools, name))
|
|
1379
|
+
if (!whole && !p.closed) {
|
|
1380
|
+
// no parameter and no close: a tag named in passing; a parameter
|
|
1381
|
+
// left open: the call cut short
|
|
1382
|
+
if (!/<(?:antml:)?parameter\b/.test(body.slice(0, stop))) return { text: true }
|
|
1383
|
+
return { error: CUT_SHORT }
|
|
1384
|
+
}
|
|
1385
|
+
return { call: { name, args: JSON.stringify(p.args) }, rest: whole ? body.slice(stop + c[0].length) : body.slice(stop) }
|
|
1297
1386
|
}
|
|
1298
1387
|
|
|
1299
1388
|
// quoted says text ends inside a code fence or an inline code span: a
|
|
@@ -1316,31 +1405,96 @@ const UNREADABLE = "the model's tool call, written as text, can't be read; it is
|
|
|
1316
1405
|
// that the stream ended inside, is an error, never the answer (plugins#132,
|
|
1317
1406
|
// as trae's since plugins#38): the turn fails so it can be tried again. A
|
|
1318
1407
|
// block whose JSON is whole when the stream ends without its close is still
|
|
1319
|
-
// the call; one that is no call at all, or one in code, stays text.
|
|
1408
|
+
// the call; one that is no call at all, or one in code, stays text. A bare
|
|
1409
|
+
// call in Anthropic's or Qwen's tags (xmlCall) is a call too, for a tool
|
|
1410
|
+
// the request declared (tools).
|
|
1320
1411
|
class Splitter {
|
|
1321
1412
|
textBuf = ""
|
|
1322
1413
|
callBuf = ""
|
|
1323
1414
|
inCall = false
|
|
1324
1415
|
sawTool = false
|
|
1416
|
+
after = false // a bare call just ended: the closes and blanks after it are dropped
|
|
1325
1417
|
sent = "" // the text given out so far, to tell a block in code from a call
|
|
1418
|
+
constructor(tools = []) {
|
|
1419
|
+
this.tools = tools.filter((t) => (t?.function ?? t)?.name)
|
|
1420
|
+
}
|
|
1326
1421
|
text(out, t) {
|
|
1327
1422
|
this.sent += t
|
|
1328
1423
|
out.push({ text: t })
|
|
1329
1424
|
}
|
|
1425
|
+
// bare reads text that may hold a bare call, comb starting at its tag x:
|
|
1426
|
+
// the input left to read, null when comb is held for more, or false
|
|
1427
|
+
// when the turn ended in an error
|
|
1428
|
+
bare(out, comb, x, end) {
|
|
1429
|
+
const head = comb.slice(0, x)
|
|
1430
|
+
const at = comb.slice(x)
|
|
1431
|
+
const opener = at.match(/^<[^<>]*>?/)[0]
|
|
1432
|
+
if (quoted(this.sent + head)) {
|
|
1433
|
+
this.text(out, head + opener)
|
|
1434
|
+
return at.slice(opener.length)
|
|
1435
|
+
}
|
|
1436
|
+
const b = xmlCall(at, end, this.tools)
|
|
1437
|
+
if (b.wait) {
|
|
1438
|
+
if (head) this.text(out, head)
|
|
1439
|
+
this.textBuf = at
|
|
1440
|
+
return null
|
|
1441
|
+
}
|
|
1442
|
+
if (b.text) {
|
|
1443
|
+
this.text(out, head + opener)
|
|
1444
|
+
return at.slice(opener.length)
|
|
1445
|
+
}
|
|
1446
|
+
if (head) this.text(out, head)
|
|
1447
|
+
if (b.error) {
|
|
1448
|
+
out.push({ error: b.error })
|
|
1449
|
+
return false
|
|
1450
|
+
}
|
|
1451
|
+
this.sawTool = true
|
|
1452
|
+
out.push({ call: b.call })
|
|
1453
|
+
this.after = true
|
|
1454
|
+
return b.rest
|
|
1455
|
+
}
|
|
1456
|
+
// left drops what is left after a bare call; null while it may still be
|
|
1457
|
+
// one of its closes
|
|
1458
|
+
left(comb, end) {
|
|
1459
|
+
const rest = comb.replace(XML_LEFT, "")
|
|
1460
|
+
if (!end && (!rest || XML_CLOSES.some((c) => c.length > rest.length && c.startsWith(rest)))) return null
|
|
1461
|
+
this.after = false
|
|
1462
|
+
return rest
|
|
1463
|
+
}
|
|
1330
1464
|
feed(input) {
|
|
1331
1465
|
const out = []
|
|
1332
1466
|
while (input) {
|
|
1333
1467
|
if (!this.inCall) {
|
|
1334
|
-
|
|
1468
|
+
let comb = this.textBuf + input
|
|
1335
1469
|
this.textBuf = ""
|
|
1470
|
+
if (this.after) {
|
|
1471
|
+
const rest = this.left(comb, false)
|
|
1472
|
+
if (rest === null) {
|
|
1473
|
+
this.textBuf = comb
|
|
1474
|
+
break
|
|
1475
|
+
}
|
|
1476
|
+
comb = rest
|
|
1477
|
+
if (!comb) break
|
|
1478
|
+
}
|
|
1336
1479
|
const i = comb.indexOf(CALL_OPEN)
|
|
1480
|
+
const x = this.tools.length ? comb.search(XML_AT) : -1
|
|
1481
|
+
if (x >= 0 && (i < 0 || x < i)) {
|
|
1482
|
+
const next = this.bare(out, comb, x, false)
|
|
1483
|
+
if (next === false) return out
|
|
1484
|
+
if (next === null) break
|
|
1485
|
+
input = next
|
|
1486
|
+
continue
|
|
1487
|
+
}
|
|
1337
1488
|
if (i < 0) {
|
|
1338
1489
|
let keep = 0
|
|
1339
|
-
for (
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1490
|
+
for (const mark of this.tools.length ? [CALL_OPEN, ...XML_MARKS] : [CALL_OPEN])
|
|
1491
|
+
for (let n = Math.min(mark.length - 1, comb.length); n > keep; n--)
|
|
1492
|
+
if (comb.endsWith(mark.slice(0, n))) {
|
|
1493
|
+
keep = n
|
|
1494
|
+
break
|
|
1495
|
+
}
|
|
1496
|
+
const h = this.tools.length ? comb.search(XML_HOLD) : -1
|
|
1497
|
+
if (h >= 0) keep = Math.max(keep, comb.length - h)
|
|
1344
1498
|
if (keep < comb.length) this.text(out, comb.slice(0, comb.length - keep))
|
|
1345
1499
|
this.textBuf = comb.slice(comb.length - keep)
|
|
1346
1500
|
break
|
|
@@ -1358,7 +1512,7 @@ class Splitter {
|
|
|
1358
1512
|
break
|
|
1359
1513
|
}
|
|
1360
1514
|
const body = comb.slice(0, j)
|
|
1361
|
-
const c = parseCall(body)
|
|
1515
|
+
const c = parseCall(body, this.tools)
|
|
1362
1516
|
if (c) {
|
|
1363
1517
|
this.sawTool = true
|
|
1364
1518
|
out.push({ call: c })
|
|
@@ -1378,7 +1532,7 @@ class Splitter {
|
|
|
1378
1532
|
this.inCall = false
|
|
1379
1533
|
this.callBuf = ""
|
|
1380
1534
|
if (callish(body) && !quoted(this.sent)) {
|
|
1381
|
-
const c = parseCall(body)
|
|
1535
|
+
const c = parseCall(body, this.tools)
|
|
1382
1536
|
if (!c) return [{ error: CUT_SHORT }]
|
|
1383
1537
|
this.sawTool = true
|
|
1384
1538
|
return [{ call: c }]
|
|
@@ -1388,10 +1542,23 @@ class Splitter {
|
|
|
1388
1542
|
return [{ text: t }]
|
|
1389
1543
|
}
|
|
1390
1544
|
if (this.textBuf) {
|
|
1391
|
-
|
|
1545
|
+
let t = this.textBuf
|
|
1392
1546
|
this.textBuf = ""
|
|
1393
|
-
this.
|
|
1394
|
-
|
|
1547
|
+
if (this.after) t = this.left(t, true)
|
|
1548
|
+
// what was held for a bare call, read to its end: calls, then the
|
|
1549
|
+
// text left, or the error of one cut short
|
|
1550
|
+
const out = []
|
|
1551
|
+
while (t) {
|
|
1552
|
+
const x = this.tools.length ? t.search(XML_AT) : -1
|
|
1553
|
+
if (x < 0) {
|
|
1554
|
+
this.text(out, t)
|
|
1555
|
+
break
|
|
1556
|
+
}
|
|
1557
|
+
const next = this.bare(out, t, x, true)
|
|
1558
|
+
if (next === false) break
|
|
1559
|
+
t = this.after ? this.left(next, true) : next
|
|
1560
|
+
}
|
|
1561
|
+
return out
|
|
1395
1562
|
}
|
|
1396
1563
|
return []
|
|
1397
1564
|
}
|
|
@@ -1414,8 +1581,8 @@ async function* sseLines(body) {
|
|
|
1414
1581
|
// events turns Qoder's SSE into chat completion pieces: each data line's
|
|
1415
1582
|
// envelope has a "body" that is an OpenAI chunk, whose content may hold
|
|
1416
1583
|
// tool calls to lift out.
|
|
1417
|
-
async function* events(body) {
|
|
1418
|
-
const a = new Splitter()
|
|
1584
|
+
async function* events(body, tools = []) {
|
|
1585
|
+
const a = new Splitter(tools)
|
|
1419
1586
|
let index = -1 // the tool call being given
|
|
1420
1587
|
const native = new Map() // upstream index -> call and its output index
|
|
1421
1588
|
// one piece of the split text as an event: a call, text, or a call that
|
|
@@ -1513,8 +1680,8 @@ async function* events(body) {
|
|
|
1513
1680
|
// opened is a chat reply's events and the first of them, read before
|
|
1514
1681
|
// anything is answered: a refusal in the stream (Qoder's queue among them)
|
|
1515
1682
|
// comes first.
|
|
1516
|
-
async function opened(res) {
|
|
1517
|
-
const it = events(res.body)
|
|
1683
|
+
async function opened(res, tools) {
|
|
1684
|
+
const it = events(res.body, tools)
|
|
1518
1685
|
return { it, first: await it.next() }
|
|
1519
1686
|
}
|
|
1520
1687
|
|
|
@@ -1620,7 +1787,7 @@ class UsageStatus extends Error {
|
|
|
1620
1787
|
|
|
1621
1788
|
// fetchUsage is the usage envelope, {displayMode, qoderUsage}; a site with
|
|
1622
1789
|
// its own usage path (QwenWork's account-context) is read by
|
|
1623
|
-
//
|
|
1790
|
+
// the usage hook's site.usagePath branch instead (parseAccountContext).
|
|
1624
1791
|
async function fetchUsage(site, deviceToken) {
|
|
1625
1792
|
if (!String(deviceToken ?? "").trim()) throw new Error("qoder usage: missing device token")
|
|
1626
1793
|
let res
|
|
@@ -1916,28 +2083,59 @@ XcW+ML9FoCI6AOvOzwIDAQAB
|
|
|
1916
2083
|
-----END PUBLIC KEY-----`
|
|
1917
2084
|
// parseAccountContext reads the account-context QwenWork's sign-in already
|
|
1918
2085
|
// reads: plan {name, next_refresh / next_due} and quota {total, used,
|
|
1919
|
-
// remaining} in the plan's credit unit
|
|
2086
|
+
// remaining, exceeded} in the plan's credit unit, and the wallets
|
|
2087
|
+
// (/api/v1/adapter/user/wallets) where the public cloud has them: each
|
|
2088
|
+
// pool's total_balance (daily_credits, monthly_credits, longterm_credits).
|
|
2089
|
+
// That is what QwenWork's own client reads (QwenWorkCN.app 1.2.1,
|
|
2090
|
+
// out/main/main.js: fetchQuotaUsage, fetchUserWallets,
|
|
2091
|
+
// mergeWalletsIntoCreditBreakdown). A personal account's quota has total
|
|
2092
|
+
// and used null, so only what is left is known: that is the balance, with
|
|
2093
|
+
// each pool, and no share used is made up. An account with a total keeps
|
|
2094
|
+
// its window of used / total.
|
|
1920
2095
|
const num = (v) => {
|
|
1921
2096
|
const n = Number(v)
|
|
1922
2097
|
if (!Number.isFinite(n)) return "0"
|
|
1923
2098
|
return String(Math.round(n * 100) / 100)
|
|
1924
2099
|
}
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
2100
|
+
// the client's unwrapAdapterResponse: the envelope's data object, else the
|
|
2101
|
+
// envelope itself
|
|
2102
|
+
const unwrap = (v) => (v?.data && typeof v.data === "object" && !Array.isArray(v.data) ? v.data : (v && typeof v === "object" ? v : {}))
|
|
2103
|
+
const finite = (v) => (typeof v === "number" && Number.isFinite(v) ? v : undefined)
|
|
2104
|
+
const POOLS = [["daily_credits", "daily"], ["monthly_credits", "monthly"], ["longterm_credits", "long-term"]]
|
|
2105
|
+
const walletPools = (w) => {
|
|
2106
|
+
const d = unwrap(w)
|
|
2107
|
+
const out = []
|
|
2108
|
+
for (const [key, name] of POOLS) {
|
|
2109
|
+
const p = d[key]
|
|
2110
|
+
const b = p && typeof p === "object" ? finite(p.total_balance ?? p.totalBalance) : undefined
|
|
2111
|
+
if (b !== undefined && b > 0) out.push(`${name} ${num(b)}`)
|
|
2112
|
+
}
|
|
2113
|
+
return out
|
|
2114
|
+
}
|
|
2115
|
+
const parseAccountContext = (ctx, wallets) => {
|
|
2116
|
+
const data = unwrap(ctx)
|
|
2117
|
+
const q = data.quota && typeof data.quota === "object" && !Array.isArray(data.quota) ? data.quota : null
|
|
1928
2118
|
const out = {}
|
|
1929
2119
|
if (data.plan?.name) out.plan = String(data.plan.name)
|
|
1930
2120
|
const refresh = data.plan?.next_refresh_date ?? data.plan?.next_due_date
|
|
1931
2121
|
if (refresh) out.until = new Date(refresh).toISOString()
|
|
1932
|
-
if (q
|
|
1933
|
-
const total =
|
|
1934
|
-
const remaining =
|
|
1935
|
-
const used =
|
|
1936
|
-
if (
|
|
2122
|
+
if (q) {
|
|
2123
|
+
const total = finite(q.total)
|
|
2124
|
+
const remaining = finite(q.remaining)
|
|
2125
|
+
const used = finite(q.used) ?? (total !== undefined && remaining !== undefined ? total - remaining : undefined)
|
|
2126
|
+
if (total !== undefined && total > 0 && used !== undefined && used >= 0) {
|
|
1937
2127
|
out.windows = [{ name: "Credits", used: Math.min(100, (100 * used) / total), display: `${num(used)} / ${num(total)} credits` }]
|
|
1938
2128
|
}
|
|
2129
|
+
if (remaining !== undefined) {
|
|
2130
|
+
const pools = wallets ? walletPools(wallets) : []
|
|
2131
|
+
out.balance = `${num(remaining)} credits${pools.length ? ` (${pools.join(" · ")})` : ""}`
|
|
2132
|
+
// nothing left stops the account: say so as a spent window, since
|
|
2133
|
+
// there is no total to show a share of
|
|
2134
|
+
const exceeded = q.exceeded === true || q.is_quota_exceeded === true || q.isQuotaExceeded === true || remaining <= 0
|
|
2135
|
+
if (!out.windows && exceeded) out.windows = [{ name: "Credits", used: 100, display: "no credits left" }]
|
|
2136
|
+
}
|
|
1939
2137
|
}
|
|
1940
|
-
if (!out.windows) out.error = "QwenWork: the account reports no credits"
|
|
2138
|
+
if (!out.windows && out.balance === undefined) out.error = "QwenWork: the account reports no credits"
|
|
1941
2139
|
return out
|
|
1942
2140
|
}
|
|
1943
2141
|
|
|
@@ -1995,6 +2193,9 @@ const SITES = {
|
|
|
1995
2193
|
...QWENWORK_COMMON,
|
|
1996
2194
|
id: "qwenwork",
|
|
1997
2195
|
name: "QwenWork",
|
|
2196
|
+
// the public cloud's personal accounts have wallets; the client asks
|
|
2197
|
+
// for them beside the account-context, and not for an enterprise one
|
|
2198
|
+
walletsPath: "/api/v1/adapter/user/wallets",
|
|
1998
2199
|
web: "https://gateway.qwenwork.cn",
|
|
1999
2200
|
openapi: "https://gateway.qwenwork.cn",
|
|
2000
2201
|
api: "https://gateway.qwenwork.cn",
|
|
@@ -2171,9 +2372,9 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2171
2372
|
// like any other call and answered with its own envelope) is read
|
|
2172
2373
|
// there; a 401 rotates the device token once, as the qoder path does
|
|
2173
2374
|
if (site.usagePath) {
|
|
2174
|
-
const read = async (token) => {
|
|
2375
|
+
const read = async (token, path = site.usagePath) => {
|
|
2175
2376
|
const s = siteOf(site, cred)
|
|
2176
|
-
const res = await fetch(`${s.openapi}${
|
|
2377
|
+
const res = await fetch(`${s.openapi}${path}`, {
|
|
2177
2378
|
headers: { Accept: "application/json", Authorization: `Bearer ${token}` },
|
|
2178
2379
|
signal: AbortSignal.timeout(20_000),
|
|
2179
2380
|
})
|
|
@@ -2181,14 +2382,23 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2181
2382
|
return res.json()
|
|
2182
2383
|
}
|
|
2183
2384
|
let env
|
|
2385
|
+
let token = cred.deviceToken ?? cred.access
|
|
2184
2386
|
try {
|
|
2185
|
-
env = await read(
|
|
2387
|
+
env = await read(token)
|
|
2186
2388
|
} catch (e) {
|
|
2187
2389
|
if (!(e instanceof UsageStatus) || (e.status !== 401 && e.status !== 403)) throw e
|
|
2188
2390
|
if (!cred.deviceToken) throw e
|
|
2189
|
-
|
|
2391
|
+
token = await deviceToken(getAuth, cred.deviceToken)
|
|
2392
|
+
env = await read(token)
|
|
2393
|
+
}
|
|
2394
|
+
// the pools behind the balance, as the client asks for a personal
|
|
2395
|
+
// account's; a failed read leaves the balance without them
|
|
2396
|
+
let wallets
|
|
2397
|
+
const ctxUser = (env?.data ?? env)?.user
|
|
2398
|
+
if (site.walletsPath && ctxUser?.is_biz !== true && ctxUser?.isBiz !== true) {
|
|
2399
|
+
try { wallets = await read(token, site.walletsPath) } catch {}
|
|
2190
2400
|
}
|
|
2191
|
-
return { ...(site.parseUsage ? site.parseUsage(env) : { windows: [] }), signIn }
|
|
2401
|
+
return { ...(site.parseUsage ? site.parseUsage(env, wallets) : { windows: [] }), signIn }
|
|
2192
2402
|
}
|
|
2193
2403
|
// A PAT account has no device token: the usage endpoint takes the job
|
|
2194
2404
|
// token the exchange gave — a live PAT account answered 200 with a
|
|
@@ -2365,7 +2575,7 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2365
2575
|
let res, open
|
|
2366
2576
|
try {
|
|
2367
2577
|
res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal: init.signal })
|
|
2368
|
-
if (res.ok) open = await opened(res)
|
|
2578
|
+
if (res.ok) open = await opened(res, chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function") : [])
|
|
2369
2579
|
} catch (e) {
|
|
2370
2580
|
return errorResponse({ status: 502, message: e.message })
|
|
2371
2581
|
}
|
|
@@ -2621,4 +2831,4 @@ export async function QwenWorkEnterpriseAuthPlugin({ client }) {
|
|
|
2621
2831
|
export const protocol = { makePlugin, deviceSignIn, openapi, siteOf }
|
|
2622
2832
|
|
|
2623
2833
|
// for tests
|
|
2624
|
-
export const _internal = { typedUnions, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
|
|
2834
|
+
export const _internal = { typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpie-community/opencode-qoder-auth",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.37",
|
|
4
4
|
"description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship (VPC)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.mjs",
|