@magpie-community/opencode-grok-auth 0.1.17 → 0.2.0

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 +33 -0
  2. package/index.mjs +30 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -87,12 +87,45 @@ leaves these out:
87
87
  - tools of types it doesn't take (a freeform `apply_patch`, namespaces)
88
88
  - `external_web_access` on web search
89
89
  - a `tool_choice` naming a dropped type
90
+ - a `tool_choice` with no tools beside it (magpie sends `"none"` so when a
91
+ client turned tools off)
90
92
  - a reasoning item's `"content": null`
91
93
 
92
94
  Plain Codex collaboration items (`agent_message`) reach Grok as user
93
95
  messages, with their sender, recipient and content preserved. Sealed or
94
96
  unrecognized content stays unchanged; the plugin does not decrypt tasks.
95
97
 
98
+ ### Native X search (optional)
99
+
100
+ In magpie, open the Grok provider, turn on **Native X search** under
101
+ **Plugin options**, and save. It is off by default; changes apply to the
102
+ next request without restarting or signing in again.
103
+
104
+ When enabled, the plugin offers Grok its native `x_search` tool. Grok
105
+ decides whether to search; enabling it does not force a search on every
106
+ request. Existing function tools and X search filters are preserved, and
107
+ an existing `x_search` is not duplicated. Explicit choices such as `none`,
108
+ `required`, a named tool, or `allowed_tools` never gain another tool.
109
+
110
+ The option applies to Responses requests, including Chat Completions,
111
+ Anthropic Messages and Gemini requests that magpie converts to Responses,
112
+ in streaming and non-streaming mode. Use magpie with the fix for
113
+ [yetone/magpie#3110](https://github.com/yetone/magpie/issues/3110) so that
114
+ those clients' tool-disable choices reach the plugin.
115
+
116
+ For compatibility with older gateways, a request that reaches the plugin
117
+ with neither tools nor a `tool_choice` is left unchanged. This can happen
118
+ when magpie translates a client's automatic choice on an empty tool list.
119
+ To use X search through a translated API, send at least one function tool;
120
+ the plugin adds X search alongside it. An explicit tool-disable choice
121
+ always takes precedence.
122
+
123
+ This uses the existing `grok` provider's login, models and quota reporting;
124
+ no extra plugin is needed. It applies to that provider's models when the
125
+ upstream supports X search, not to Grok models served through Cursor or
126
+ another provider. Report problems in
127
+ [this repository](https://github.com/magpie-community/plugins/issues).
128
+
96
129
  ## Models
97
130
 
98
131
  Grok 4.7 until signed in. Signed in, the models are the ones the account
package/index.mjs CHANGED
@@ -314,14 +314,17 @@ const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v)
314
314
 
315
315
  // rewrite leaves out what Grok's backend doesn't take: tools of other
316
316
  // types (a freeform apply_patch, a namespace of sub-agent tools), a web
317
- // search's external_web_access, a tool_choice naming a dropped type, and a
317
+ // search's external_web_access, a tool_choice naming a dropped type or
318
+ // with no tools beside it (magpie sends "none" so when a client turned
319
+ // tools off, yetone/magpie#3110; Grok refuses it: "A tool_choice was set on
320
+ // the request but no tools were specified"), and a
318
321
  // reasoning item's "content": null, beside which it can't read the
319
322
  // encrypted reasoning. A function's parameters with a union at the root
320
323
  // (Codex desktop's codex_app automation_update) are folded to an object,
321
324
  // which Grok turns away otherwise: "tool parameter root must be an object
322
325
  // type" (magpie#1271).
323
- function rewrite(body) {
324
- if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"reasoning"') && !body.includes('"agent_message"'))) return body
326
+ function rewrite(body, xSearch = false) {
327
+ if (typeof body !== "string" || (!xSearch && !body.includes('"tools"') && !body.includes('"tool_choice"') && !body.includes('"reasoning"') && !body.includes('"agent_message"'))) return body
325
328
  let m
326
329
  try {
327
330
  m = JSON.parse(body)
@@ -329,6 +332,10 @@ function rewrite(body) {
329
332
  return body
330
333
  }
331
334
  if (!m || typeof m !== "object" || Array.isArray(m)) return body
335
+ // Read the client's choice before dropping unsupported tools or choices.
336
+ const search = xSearch && (m.tool_choice == null || m.tool_choice === "auto") &&
337
+ (m.tools == null || Array.isArray(m.tools)) &&
338
+ (m.tool_choice === "auto" || Array.isArray(m.tools) && m.tools.length > 0)
332
339
  let dirty = false
333
340
  if (Array.isArray(m.tools)) {
334
341
  m.tools = m.tools.filter((t) => {
@@ -356,6 +363,16 @@ function rewrite(body) {
356
363
  dirty = true
357
364
  }
358
365
  }
366
+ // No choice and no tools can mean a translated "none" on older gateways.
367
+ if (search && (m.tool_choice === "auto" || m.tools?.length) && !m.tools?.some((t) => t?.type === "x_search")) {
368
+ m.tools ??= []
369
+ m.tools.push({ type: "x_search" })
370
+ dirty = true
371
+ }
372
+ if (Object.hasOwn(m, "tool_choice") && !(Array.isArray(m.tools) && m.tools.length)) {
373
+ delete m.tool_choice
374
+ dirty = true
375
+ }
359
376
  if (Array.isArray(m.input)) {
360
377
  m.input = m.input.map((it) => {
361
378
  if (!isObj(it) || it.type !== "agent_message" || Object.hasOwn(it, "encrypted_content") ||
@@ -796,7 +813,7 @@ function kept(res) {
796
813
 
797
814
  // ---- the plugin ---------------------------------------------------------------
798
815
 
799
- export const GrokAuthPlugin = async ({ client }) => {
816
+ export const GrokAuthPlugin = async ({ client, magpie }) => {
800
817
  const remember = async (auth, c) => {
801
818
  if (auth.access === c.key && auth.expires === c.expires) return
802
819
  try {
@@ -817,6 +834,13 @@ export const GrokAuthPlugin = async ({ client }) => {
817
834
 
818
835
  auth: {
819
836
  provider: PROVIDER,
837
+ options: [
838
+ {
839
+ key: "x_search",
840
+ label: "Native X search",
841
+ description: "Offers X search to Grok when tools are automatic. Keeps existing search filters and explicit tool choices. Grok decides whether to search.",
842
+ },
843
+ ],
820
844
  // magpie renews the sign-in LEAD_MS before its end, once for the
821
845
  // account, before its requests, models and usage ask for it: the CLI
822
846
  // renews it in its home, as before a request, and what it gives is
@@ -857,7 +881,8 @@ export const GrokAuthPlugin = async ({ client }) => {
857
881
  const home = auth?.refresh
858
882
  if (!home) throw new Error("Grok is not signed in; run `grok login`")
859
883
  const req = new Request(input, init)
860
- let body = rewrite(bodyText(init?.body))
884
+ const search = new URL(req.url).pathname.endsWith("/responses") && magpie?.option?.(PROVIDER, "x_search") === true
885
+ let body = rewrite(bodyText(init?.body), search)
861
886
  let parsed = {}
862
887
  if (typeof body === "string") {
863
888
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-grok-auth",
3
- "version": "0.1.17",
3
+ "version": "0.2.0",
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",