@magpie-community/opencode-grok-auth 0.1.9 → 0.1.11

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/README.md +4 -0
  2. package/index.mjs +108 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -66,6 +66,10 @@ leaves these out:
66
66
  - a `tool_choice` naming a dropped type
67
67
  - a reasoning item's `"content": null`
68
68
 
69
+ Plain Codex collaboration items (`agent_message`) reach Grok as user
70
+ messages, with their sender, recipient and content preserved. Sealed or
71
+ unrecognized content stays unchanged; the plugin does not decrypt tasks.
72
+
69
73
  ## Models
70
74
 
71
75
  Grok 4.7 until signed in. Signed in, the models are the ones the account
package/index.mjs CHANGED
@@ -202,13 +202,93 @@ 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
- if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"reasoning"'))) return body
291
+ if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"reasoning"') && !body.includes('"agent_message"'))) return body
212
292
  let m
213
293
  try {
214
294
  m = JSON.parse(body)
@@ -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)) {
@@ -235,6 +323,23 @@ function rewrite(body) {
235
323
  dirty = true
236
324
  }
237
325
  }
326
+ if (Array.isArray(m.input)) {
327
+ m.input = m.input.map((it) => {
328
+ if (!isObj(it) || it.type !== "agent_message" || Object.hasOwn(it, "encrypted_content") ||
329
+ !Array.isArray(it.content) || !it.content.length ||
330
+ !it.content.every((part) => isObj(part) && !Object.hasOwn(part, "encrypted_content") &&
331
+ (part.type === "input_text" && typeof part.text === "string" ||
332
+ part.type === "input_image" && typeof part.image_url === "string"))) return it
333
+ // Grok takes ordinary messages, not Codex's collaboration item.
334
+ // Sealed tasks remain untouched for the gateway's existing guard.
335
+ const from = typeof it.author === "string" ? it.author : ""
336
+ const to = typeof it.recipient === "string" ? it.recipient : ""
337
+ const header = from && to ? `From ${from} to ${to}` : from ? `From ${from}` : to ? `To ${to}` : ""
338
+ dirty = true
339
+ return { type: "message", role: "user", content: header
340
+ ? [{ type: "input_text", text: `${header}\n\n` }, ...it.content] : it.content }
341
+ })
342
+ }
238
343
  for (const it of Array.isArray(m.input) ? m.input : []) {
239
344
  if (it && it.type === "reasoning" && "content" in it && it.content === null) {
240
345
  delete it.content
@@ -677,4 +782,4 @@ export const GrokAuthPlugin = async ({ client }) => {
677
782
  }
678
783
 
679
784
  // for tests
680
- export const _internal = { rewrite, bodyText, limited, HOLD_MS }
785
+ 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.9",
3
+ "version": "0.1.11",
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",