@magpie-community/opencode-grok-auth 0.1.18 → 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 +31 -0
  2. package/index.mjs +22 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -95,6 +95,37 @@ Plain Codex collaboration items (`agent_message`) reach Grok as user
95
95
  messages, with their sender, recipient and content preserved. Sealed or
96
96
  unrecognized content stays unchanged; the plugin does not decrypt tasks.
97
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
+
98
129
  ## Models
99
130
 
100
131
  Grok 4.7 until signed in. Signed in, the models are the ones the account
package/index.mjs CHANGED
@@ -323,8 +323,8 @@ const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v)
323
323
  // (Codex desktop's codex_app automation_update) are folded to an object,
324
324
  // which Grok turns away otherwise: "tool parameter root must be an object
325
325
  // type" (magpie#1271).
326
- function rewrite(body) {
327
- if (typeof body !== "string" || (!body.includes('"tools"') && !body.includes('"tool_choice"') && !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
328
328
  let m
329
329
  try {
330
330
  m = JSON.parse(body)
@@ -332,6 +332,10 @@ function rewrite(body) {
332
332
  return body
333
333
  }
334
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)
335
339
  let dirty = false
336
340
  if (Array.isArray(m.tools)) {
337
341
  m.tools = m.tools.filter((t) => {
@@ -359,6 +363,12 @@ function rewrite(body) {
359
363
  dirty = true
360
364
  }
361
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
+ }
362
372
  if (Object.hasOwn(m, "tool_choice") && !(Array.isArray(m.tools) && m.tools.length)) {
363
373
  delete m.tool_choice
364
374
  dirty = true
@@ -803,7 +813,7 @@ function kept(res) {
803
813
 
804
814
  // ---- the plugin ---------------------------------------------------------------
805
815
 
806
- export const GrokAuthPlugin = async ({ client }) => {
816
+ export const GrokAuthPlugin = async ({ client, magpie }) => {
807
817
  const remember = async (auth, c) => {
808
818
  if (auth.access === c.key && auth.expires === c.expires) return
809
819
  try {
@@ -824,6 +834,13 @@ export const GrokAuthPlugin = async ({ client }) => {
824
834
 
825
835
  auth: {
826
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
+ ],
827
844
  // magpie renews the sign-in LEAD_MS before its end, once for the
828
845
  // account, before its requests, models and usage ask for it: the CLI
829
846
  // renews it in its home, as before a request, and what it gives is
@@ -864,7 +881,8 @@ export const GrokAuthPlugin = async ({ client }) => {
864
881
  const home = auth?.refresh
865
882
  if (!home) throw new Error("Grok is not signed in; run `grok login`")
866
883
  const req = new Request(input, init)
867
- 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)
868
886
  let parsed = {}
869
887
  if (typeof body === "string") {
870
888
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-grok-auth",
3
- "version": "0.1.18",
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",