@magpie-community/opencode-qoder-auth 0.2.35 → 0.2.36
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 +7 -0
- package/index.mjs +194 -27
- 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
|
|
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
|
|
|
@@ -2365,7 +2532,7 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2365
2532
|
let res, open
|
|
2366
2533
|
try {
|
|
2367
2534
|
res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal: init.signal })
|
|
2368
|
-
if (res.ok) open = await opened(res)
|
|
2535
|
+
if (res.ok) open = await opened(res, chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function") : [])
|
|
2369
2536
|
} catch (e) {
|
|
2370
2537
|
return errorResponse({ status: 502, message: e.message })
|
|
2371
2538
|
}
|
|
@@ -2621,4 +2788,4 @@ export async function QwenWorkEnterpriseAuthPlugin({ client }) {
|
|
|
2621
2788
|
export const protocol = { makePlugin, deviceSignIn, openapi, siteOf }
|
|
2622
2789
|
|
|
2623
2790
|
// 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 }
|
|
2791
|
+
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.36",
|
|
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",
|