@magpie-community/opencode-grok-auth 0.1.1 → 0.1.2
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.
- package/index.mjs +56 -13
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -174,6 +174,13 @@ async function sign(headers, key) {
|
|
|
174
174
|
headers.set("User-Agent", `grok-shell/${v} (${os}; ${arch})`)
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
+
// bodyText is a request body handed over as bytes read as the text it
|
|
178
|
+
// is, so it can be reshaped; any other body is as it came.
|
|
179
|
+
function bodyText(body) {
|
|
180
|
+
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) return new TextDecoder().decode(body)
|
|
181
|
+
return body
|
|
182
|
+
}
|
|
183
|
+
|
|
177
184
|
// rewrite leaves out what Grok's backend doesn't take: tools of other
|
|
178
185
|
// types (a freeform apply_patch, a namespace of sub-agent tools), a web
|
|
179
186
|
// search's external_web_access, a tool_choice naming a dropped type, and a
|
|
@@ -353,16 +360,52 @@ const RFC3339 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})
|
|
|
353
360
|
|
|
354
361
|
// statusText is a refused request's status as magpie says it (Go's
|
|
355
362
|
// http.StatusText).
|
|
363
|
+
// http.StatusText); one Go has no words for is "HTTP <n>", never empty.
|
|
356
364
|
const statusText = (s) =>
|
|
357
|
-
(
|
|
365
|
+
((GO_TEXT[s] ?? STATUS_CODES[s]) || `HTTP ${s}`)
|
|
366
|
+
// where Go's words differ from Node's (509 Go has none for)
|
|
367
|
+
const GO_TEXT = { 413: "Request Entity Too Large", 414: "Request URI Too Long", 416: "Requested Range Not Satisfiable", 418: "I'm a teapot", 509: "" }
|
|
368
|
+
|
|
369
|
+
// netError is a request that got no answer, as Go's http client words it
|
|
370
|
+
// (`Get "<url>": …`), with the words magpie's keepLast looks for to keep
|
|
371
|
+
// the card it had: no such host, connection refused, timeout, EOF.
|
|
372
|
+
function netError(url, e) {
|
|
373
|
+
const m = String(e?.message ?? e)
|
|
374
|
+
const code = String(e?.code ?? e?.cause?.code ?? "")
|
|
375
|
+
let why = m
|
|
376
|
+
if (e?.name === "TimeoutError" || e?.name === "AbortError" || /timed? ?out/i.test(m)) why = "timeout"
|
|
377
|
+
else if (code === "ENOTFOUND" || code === "EAI_AGAIN" || /getaddrinfo|ENOTFOUND|resolve|DNS/i.test(m)) why = "dial tcp: lookup: no such host"
|
|
378
|
+
else if (code === "ECONNREFUSED" || /ECONNREFUSED|Unable to connect|refused/i.test(m)) why = "dial tcp: connection refused"
|
|
379
|
+
else if (/ECONNRESET|socket|closed|fetch failed/i.test(m + code)) why = "EOF"
|
|
380
|
+
return `Get "${url}": ${why}`
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// jsonError is JSON that won't read, as Go's encoding/json says it.
|
|
384
|
+
function jsonError(text) {
|
|
385
|
+
const c = text.trimStart()[0]
|
|
386
|
+
return c === undefined ? "unexpected end of JSON input" : `invalid character '${c}' looking for beginning of value`
|
|
387
|
+
}
|
|
358
388
|
|
|
359
389
|
async function usage(key) {
|
|
360
|
-
const
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
390
|
+
const url = `${BASE}/billing?format=credits`
|
|
391
|
+
let res, text
|
|
392
|
+
try {
|
|
393
|
+
res = await fetch(url, {
|
|
394
|
+
headers: { Authorization: `Bearer ${key}`, Accept: "application/json" },
|
|
395
|
+
signal: AbortSignal.timeout(15_000),
|
|
396
|
+
})
|
|
397
|
+
if (res.ok) text = await res.text()
|
|
398
|
+
} catch (e) {
|
|
399
|
+
return { error: netError(url, e), windows: [] }
|
|
400
|
+
}
|
|
364
401
|
if (!res.ok) return { error: statusText(res.status), windows: [] }
|
|
365
|
-
|
|
402
|
+
let data
|
|
403
|
+
try {
|
|
404
|
+
data = JSON.parse(text)
|
|
405
|
+
} catch {
|
|
406
|
+
return { error: jsonError(text), windows: [] }
|
|
407
|
+
}
|
|
408
|
+
const cfg = data?.config ?? {}
|
|
366
409
|
const num = (v) => (typeof v === "number" ? v : 0)
|
|
367
410
|
let [name, span] = ["Allowance", 0]
|
|
368
411
|
let end = cfg.billingPeriodEnd
|
|
@@ -411,7 +454,7 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
411
454
|
const home = auth?.refresh
|
|
412
455
|
if (!home) throw new Error("Grok is not signed in; run `grok login`")
|
|
413
456
|
const req = new Request(input, init)
|
|
414
|
-
const body = rewrite(init?.body)
|
|
457
|
+
const body = rewrite(bodyText(init?.body))
|
|
415
458
|
let parsed = {}
|
|
416
459
|
if (typeof body === "string") {
|
|
417
460
|
try {
|
|
@@ -466,12 +509,9 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
466
509
|
async models(provider, { auth } = {}) {
|
|
467
510
|
const have = provider?.models ?? {}
|
|
468
511
|
if (!auth || auth.type !== "oauth" || !auth.refresh) return have
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
} catch {
|
|
473
|
-
return have
|
|
474
|
-
}
|
|
512
|
+
// a list Grok couldn't give is a failure, not the few configured:
|
|
513
|
+
// magpie keeps the list it had
|
|
514
|
+
const list = await listModels((await token(auth.refresh, false)).key)
|
|
475
515
|
const base = have["grok-4.7"] ?? Object.values(have)[0] ?? {}
|
|
476
516
|
const out = {}
|
|
477
517
|
for (const m of list) {
|
|
@@ -498,3 +538,6 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
498
538
|
},
|
|
499
539
|
}
|
|
500
540
|
}
|
|
541
|
+
|
|
542
|
+
// for tests
|
|
543
|
+
export const _internal = { rewrite, bodyText }
|
package/package.json
CHANGED