@magpie-community/opencode-qoder-auth 0.2.66 → 0.2.68

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.
Files changed (3) hide show
  1. package/index.mjs +134 -1
  2. package/opencode2.mjs +25 -1
  3. package/package.json +1 -1
package/index.mjs CHANGED
@@ -2036,6 +2036,71 @@ function quoted(text) {
2036
2036
  // the XML function form
2037
2037
  const callish = (v) => v.trimStart().startsWith("{") || v.includes("\x3cfunction=")
2038
2038
 
2039
+ // plugins#1113: a reply that is one JSON object holding OpenAI's own
2040
+ // tool_calls array, written as text ({"tool_calls":[{"name", "arguments"}]}),
2041
+ // is those calls. It is told apart from text by where it starts: a line of
2042
+ // its own whose object opens with the "tool_calls" key, outside code.
2043
+ const JSON_KEY = '{"tool_calls"'
2044
+ const JSON_AT_ALL = /(^|\n)[ \t]*\{\s*"tool_calls"\s*:/g
2045
+
2046
+ // jsonHold is how much of comb's end may still become JSON_KEY on a line
2047
+ // of its own (start says comb starts a line), so it is held for more
2048
+ function jsonHold(comb, start) {
2049
+ const n = comb.lastIndexOf("\n")
2050
+ if (n < 0 && !start) return 0
2051
+ const line = comb.slice(n + 1)
2052
+ const s = line.replace(/\s+/g, "")
2053
+ if (!JSON_KEY.startsWith(s)) return 0
2054
+ return line.length
2055
+ }
2056
+
2057
+ // jsonEnd is where the JSON object at the start of v ends, -1 while it is
2058
+ // not whole yet
2059
+ function jsonEnd(v) {
2060
+ let depth = 0
2061
+ let str = false
2062
+ for (let k = 0; k < v.length; k++) {
2063
+ const c = v[k]
2064
+ if (str) {
2065
+ if (c === "\\") k++
2066
+ else if (c === '"') str = false
2067
+ } else if (c === '"') str = true
2068
+ else if (c === "{" || c === "[") depth++
2069
+ else if (c === "}" || c === "]") {
2070
+ if (--depth === 0) return k + 1
2071
+ }
2072
+ }
2073
+ return -1
2074
+ }
2075
+
2076
+ // jsonCalls reads a whole {"tool_calls": [...]} object: its calls, null
2077
+ // when it holds none (it stays text), or false when one can't be read
2078
+ function jsonCalls(v) {
2079
+ let f
2080
+ try {
2081
+ f = JSON.parse(v)
2082
+ } catch {
2083
+ return false
2084
+ }
2085
+ const list = f?.tool_calls
2086
+ if (!Array.isArray(list) || !list.length) return null
2087
+ const calls = []
2088
+ for (const e of list) {
2089
+ const fn = e?.function ?? e
2090
+ let args = fn?.arguments
2091
+ if (typeof args === "string") {
2092
+ try {
2093
+ args = JSON.parse(args)
2094
+ } catch {
2095
+ return false
2096
+ }
2097
+ }
2098
+ if (!String(fn?.name ?? "").trim() || !args || typeof args !== "object" || Array.isArray(args)) return false
2099
+ calls.push({ name: fn.name.trim(), args: JSON.stringify(args) })
2100
+ }
2101
+ return calls
2102
+ }
2103
+
2039
2104
  const CUT_SHORT = "the model's tool call, written as text, was cut short; it is not sent on as the answer"
2040
2105
  const UNREADABLE = "the model's tool call, written as text, can't be read; it is not sent on as the answer"
2041
2106
 
@@ -2054,6 +2119,7 @@ class Splitter {
2054
2119
  sawTool = false
2055
2120
  after = false // a bare call just ended: the closes and blanks after it are dropped
2056
2121
  sent = "" // the text given out so far, to tell a block in code from a call
2122
+ jsonBuf = null // a {"tool_calls": ...} object being read (plugins#1113)
2057
2123
  constructor(tools = []) {
2058
2124
  this.tools = tools.filter((t) => (t?.function ?? t)?.name)
2059
2125
  }
@@ -2100,9 +2166,43 @@ class Splitter {
2100
2166
  this.after = false
2101
2167
  return rest
2102
2168
  }
2169
+ // json reads the {"tool_calls": ...} object held in jsonBuf: the input
2170
+ // left after it, null while it is not whole, or false when the turn
2171
+ // ended in an error
2172
+ json(out, end) {
2173
+ const v = this.jsonBuf
2174
+ const e = jsonEnd(v)
2175
+ if (e < 0) {
2176
+ if (!end) return null
2177
+ this.jsonBuf = null
2178
+ out.push({ error: CUT_SHORT })
2179
+ return false
2180
+ }
2181
+ this.jsonBuf = null
2182
+ const calls = jsonCalls(v.slice(0, e))
2183
+ if (calls === false) {
2184
+ out.push({ error: UNREADABLE })
2185
+ return false
2186
+ }
2187
+ if (!calls) {
2188
+ this.text(out, v.slice(0, e))
2189
+ return v.slice(e)
2190
+ }
2191
+ this.sawTool = true
2192
+ for (const c of calls) out.push({ call: c })
2193
+ return v.slice(e).replace(/^\s+/, "")
2194
+ }
2103
2195
  feed(input) {
2104
2196
  const out = []
2105
2197
  while (input) {
2198
+ if (this.jsonBuf !== null) {
2199
+ this.jsonBuf += input
2200
+ const next = this.json(out, false)
2201
+ if (next === false) return out
2202
+ if (next === null) break
2203
+ input = next
2204
+ continue
2205
+ }
2106
2206
  if (!this.inCall) {
2107
2207
  let comb = this.textBuf + input
2108
2208
  this.textBuf = ""
@@ -2117,6 +2217,25 @@ class Splitter {
2117
2217
  }
2118
2218
  const i = comb.indexOf(CALL_OPEN)
2119
2219
  const x = this.tools.length ? comb.search(XML_AT) : -1
2220
+ const start = !this.sent || this.sent.endsWith("\n")
2221
+ let k = -1
2222
+ for (const jm of comb.matchAll(JSON_AT_ALL)) {
2223
+ const at = jm.index + jm[1].length
2224
+ if ((jm[1] || start) && !quoted(this.sent + comb.slice(0, at))) {
2225
+ k = at
2226
+ break
2227
+ }
2228
+ }
2229
+ if (k >= 0 && (i < 0 || k < i) && (x < 0 || k < x)) {
2230
+ if (k > 0) this.text(out, comb.slice(0, k))
2231
+ this.jsonBuf = comb.slice(k).replace(/^[ \t]+/, "")
2232
+ input = ""
2233
+ const next = this.json(out, false)
2234
+ if (next === false) return out
2235
+ if (next === null) break
2236
+ input = next
2237
+ continue
2238
+ }
2120
2239
  if (x >= 0 && (i < 0 || x < i)) {
2121
2240
  const next = this.bare(out, comb, x, false)
2122
2241
  if (next === false) return out
@@ -2134,6 +2253,7 @@ class Splitter {
2134
2253
  }
2135
2254
  const h = this.tools.length ? comb.search(XML_HOLD) : -1
2136
2255
  if (h >= 0) keep = Math.max(keep, comb.length - h)
2256
+ keep = Math.max(keep, jsonHold(comb, !this.sent || this.sent.endsWith("\n")))
2137
2257
  if (keep < comb.length) this.text(out, comb.slice(0, comb.length - keep))
2138
2258
  this.textBuf = comb.slice(comb.length - keep)
2139
2259
  break
@@ -2166,6 +2286,12 @@ class Splitter {
2166
2286
  return out
2167
2287
  }
2168
2288
  flush() {
2289
+ if (this.jsonBuf !== null) {
2290
+ const out = []
2291
+ const next = this.json(out, true)
2292
+ if (next) this.text(out, next)
2293
+ return out
2294
+ }
2169
2295
  if (this.inCall) {
2170
2296
  const body = this.callBuf
2171
2297
  this.inCall = false
@@ -2889,7 +3015,14 @@ function parseUsage(env) {
2889
3015
  const used = turn.reduce((a, p) => a + p.used, 0)
2890
3016
  const total = turn.reduce((a, p) => a + p.total, 0)
2891
3017
  for (const p of turn) p.w.aside = true
2892
- out.windows.unshift({ name: "Total credits", used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${turn[0].unit}`, amount: used, limit: total, unit: turn[0].unit })
3018
+ const sum = { name: "Total credits", used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${turn[0].unit}`, amount: used, limit: total, unit: turn[0].unit }
3019
+ // the plan's credits renew when the plan says, and what is left of them
3020
+ // is lost then: routing reads no window set aside, so the whole renews
3021
+ // with the plan's, as WorkBuddy's does (a Teams account renewing
3022
+ // tomorrow was ranked as one that never renews, behind an account
3023
+ // renewing in 18 days: @tnt12231 on X)
3024
+ if (until && turn.some((p) => p.w.resetsAt)) sum.resetsAt = until
3025
+ out.windows.unshift(sum)
2893
3026
  }
2894
3027
  const dedicated = field("dedicatedResourcePackages", "dedicated_resource_packages")
2895
3028
  for (const b of Array.isArray(dedicated) ? dedicated : []) add(b, "Dedicated credits")
package/opencode2.mjs CHANGED
@@ -575,7 +575,31 @@ async function setupPlugin(ctx, fn, live) {
575
575
  }
576
576
  } catch {}
577
577
  })()
578
- void refresh()
578
+ // A plugin still reading its list in the background when the first read
579
+ // returned (agy-cli: `agy models` and a session probe take seconds, and
580
+ // the first read gets its static list) says so through
581
+ // provider.warming(): the list is read once more when that ends, once,
582
+ // and no later than a minute on.
583
+ const warmed = async () => {
584
+ let w
585
+ try {
586
+ w = typeof hooks.provider?.warming === "function" ? hooks.provider.warming() : null
587
+ } catch {
588
+ return
589
+ }
590
+ if (!w || typeof w.then !== "function") return
591
+ let timer
592
+ await Promise.race([
593
+ Promise.resolve(w).catch(() => {}),
594
+ new Promise((r) => {
595
+ timer = setTimeout(r, 60_000)
596
+ timer.unref?.()
597
+ }),
598
+ ])
599
+ clearTimeout(timer)
600
+ if (live.on) await refresh()
601
+ }
602
+ void refresh().then(warmed, () => {})
579
603
  }
580
604
 
581
605
  export async function setup(ctx, mod = v1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.66",
3
+ "version": "0.2.68",
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",