@magpie-community/opencode-grok-auth 0.1.8 → 0.1.10

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 (2) hide show
  1. package/index.mjs +90 -2
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -202,11 +202,91 @@ function bodyText(body) {
202
202
  return body
203
203
  }
204
204
 
205
+ // objectRoot is a tool's parameters as a plain object at the root, with no
206
+ // anyOf, oneOf or allOf there, folded as magpie's built-in folds them
207
+ // (provider.ObjectRoot): allOf's branches are all merged; of anyOf's and
208
+ // oneOf's object branches, a union itself folded first, the properties are
209
+ // merged, a field each of them requires stays required, and the other
210
+ // branches go. Branches may be local $refs. A schema already a plain object
211
+ // is returned as it came; the caller's own is never changed.
212
+ function objectRoot(ps) {
213
+ return foldRoot(ps, ps, 0)
214
+ }
215
+
216
+ function foldRoot(root, ps, depth) {
217
+ if (!isObj(ps)) return ps
218
+ const has = (k) => Object.hasOwn(ps, k)
219
+ if (ps.type === "object" && !has("anyOf") && !has("oneOf") && !has("allOf")) return ps
220
+ const out = { ...ps }
221
+ const props = { ...(isObj(ps.properties) ? ps.properties : {}) }
222
+ let required = Array.isArray(ps.required) ? [...ps.required] : []
223
+ const ref = (b) => {
224
+ if (!isObj(b)) return null
225
+ if (typeof b.$ref !== "string" || !b.$ref) return b
226
+ for (const k of ["$defs", "definitions"]) {
227
+ const pre = "#/" + k + "/"
228
+ if (b.$ref.startsWith(pre)) {
229
+ const d = root[k]?.[b.$ref.slice(pre.length)]
230
+ return isObj(d) ? d : null
231
+ }
232
+ }
233
+ return null
234
+ }
235
+ for (const b of Array.isArray(ps.allOf) ? ps.allOf : []) {
236
+ const bm = ref(b)
237
+ if (!bm) continue
238
+ if (isObj(bm.properties)) for (const [k, v] of Object.entries(bm.properties)) if (!Object.hasOwn(props, k)) props[k] = v
239
+ if (Array.isArray(bm.required)) required.push(...bm.required)
240
+ }
241
+ delete out.allOf
242
+ const branches = []
243
+ for (const k of ["anyOf", "oneOf"]) {
244
+ for (const b of Array.isArray(ps[k]) ? ps[k] : []) {
245
+ let bm = ref(b)
246
+ if (!bm) continue
247
+ // a branch that is itself a union (zod's discriminated create and
248
+ // update in automation_update) is folded first, not dropped
249
+ if (depth < 8 && ["anyOf", "oneOf", "allOf"].some((u) => Object.hasOwn(bm, u))) bm = foldRoot(root, bm, depth + 1)
250
+ if (bm.type !== "object" && !Object.hasOwn(bm, "properties")) continue
251
+ branches.push(bm)
252
+ }
253
+ delete out[k]
254
+ }
255
+ // a field every branch requires stays required, beside the root's and
256
+ // allOf's own, which no branch can drop; a field the root doesn't define
257
+ // takes each schema the branches give it, as anyOf when they differ
258
+ let common = null
259
+ const from = new Map()
260
+ for (const b of branches) {
261
+ for (const [k, v] of Object.entries(isObj(b.properties) ? b.properties : {})) {
262
+ if (Object.hasOwn(props, k)) continue
263
+ const vs = from.get(k) ?? []
264
+ if (!vs.some((w) => JSON.stringify(w) === JSON.stringify(v))) vs.push(v)
265
+ from.set(k, vs)
266
+ }
267
+ const br = Array.isArray(b.required) ? b.required : []
268
+ common = common === null ? [...br] : common.filter((r) => br.includes(r))
269
+ }
270
+ for (const [k, vs] of from) props[k] = vs.length === 1 ? vs[0] : { anyOf: vs }
271
+ for (const r of common ?? []) if (!required.includes(r)) required.push(r)
272
+ required = [...new Set(required)]
273
+ out.type = "object"
274
+ out.properties = props
275
+ if (required.length) out.required = required
276
+ else delete out.required
277
+ return out
278
+ }
279
+
280
+ const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v)
281
+
205
282
  // rewrite leaves out what Grok's backend doesn't take: tools of other
206
283
  // types (a freeform apply_patch, a namespace of sub-agent tools), a web
207
284
  // search's external_web_access, a tool_choice naming a dropped type, and a
208
285
  // reasoning item's "content": null, beside which it can't read the
209
- // encrypted reasoning.
286
+ // encrypted reasoning. A function's parameters with a union at the root
287
+ // (Codex desktop's codex_app automation_update) are folded to an object,
288
+ // which Grok turns away otherwise: "tool parameter root must be an object
289
+ // type" (magpie#1271).
210
290
  function rewrite(body) {
211
291
  if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"reasoning"'))) return body
212
292
  let m
@@ -228,6 +308,14 @@ function rewrite(body) {
228
308
  delete t.external_web_access
229
309
  dirty = true
230
310
  }
311
+ if (t.type === "function") {
312
+ const fn = isObj(t.function) ? t.function : t
313
+ const ps = objectRoot(fn.parameters)
314
+ if (ps !== fn.parameters) {
315
+ fn.parameters = ps
316
+ dirty = true
317
+ }
318
+ }
231
319
  return true
232
320
  })
233
321
  if (m.tool_choice && typeof m.tool_choice === "object" && !TOOLS.has(m.tool_choice.type)) {
@@ -677,4 +765,4 @@ export const GrokAuthPlugin = async ({ client }) => {
677
765
  }
678
766
 
679
767
  // for tests
680
- export const _internal = { rewrite, bodyText, limited, HOLD_MS }
768
+ export const _internal = { rewrite, objectRoot, bodyText, limited, HOLD_MS }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-grok-auth",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",