@magpie-community/opencode-grok-auth 0.2.5 → 0.2.6
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/README.md +12 -0
- package/index.mjs +120 -1
- package/opencode2.mjs +4 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -107,6 +107,18 @@ API (`@ai-sdk/openai`), signed as the CLI signs them:
|
|
|
107
107
|
- `x-grok-doom-loop-check: 1024` and `x-grok-exact-repetition-check: 64`
|
|
108
108
|
on a Responses request, as Grok Build sends them: the proxy then stops a
|
|
109
109
|
reply that repeats itself after ~1K tokens instead of letting it run on.
|
|
110
|
+
- A Responses stream that dies before its first byte (the connection
|
|
111
|
+
reset, with nothing sent yet) is asked again, up to three times. The
|
|
112
|
+
retry goes out before the answer is returned, so a 429 or a 401 comes
|
|
113
|
+
back as itself. Once a byte has arrived, `response.created` and
|
|
114
|
+
reasoning included, it is passed on at once. A later cut is not asked
|
|
115
|
+
again and not rewritten: the stream ends, and magpie 0.1.3298 or newer
|
|
116
|
+
closes it as `response.failed` with `created_at` and `sequence_number`.
|
|
117
|
+
The frame this plugin used to write had `created_at` and still no
|
|
118
|
+
`sequence_number`, so Grok Build reported
|
|
119
|
+
`missing field sequence_number`. An upstream `response.failed`,
|
|
120
|
+
`error` or `response.incomplete` is passed on unchanged. Cancelling
|
|
121
|
+
the turn stops the read and is not asked again.
|
|
110
122
|
|
|
111
123
|
The backend turns away some of what a request may carry, so the plugin
|
|
112
124
|
leaves these out:
|
package/index.mjs
CHANGED
|
@@ -958,6 +958,121 @@ function kept(res) {
|
|
|
958
958
|
|
|
959
959
|
// ---- the plugin ---------------------------------------------------------------
|
|
960
960
|
|
|
961
|
+
// A Responses stream that dies before its first byte is asked again, up
|
|
962
|
+
// to three times, before this answer is returned. A retry's 429 or 401
|
|
963
|
+
// is that answer: note sees it, and it is not folded into a 200. Once a
|
|
964
|
+
// byte has arrived, response.created and reasoning included, it is
|
|
965
|
+
// passed on at once. A later cut is not asked again and not rewritten.
|
|
966
|
+
// The stream just ends. magpie 0.1.3298 and newer closes a Responses
|
|
967
|
+
// stream that ends short as response.failed, with the stream's id,
|
|
968
|
+
// created_at and the next sequence_number (yetone/magpie 29bfd5dc8).
|
|
969
|
+
// The frame this plugin used to write had created_at and still no
|
|
970
|
+
// sequence_number, and Grok Build failed the turn on it. An upstream
|
|
971
|
+
// response.failed, error or response.incomplete is passed on unchanged.
|
|
972
|
+
// Cancelling stops the upstream read.
|
|
973
|
+
const STREAM_TRIES = 3
|
|
974
|
+
const CUT = /ECONNRESET|EPIPE|ETIMEDOUT|ECONNABORTED|socket connection was closed|connection lost|unexpected EOF|terminated|other side closed|UND_ERR|network connection|ECONNREFUSED/i
|
|
975
|
+
|
|
976
|
+
function eventStream(res) {
|
|
977
|
+
const type = res?.headers?.get?.("content-type") || ""
|
|
978
|
+
return res?.status === 200 && !!res?.body && /text\/event-stream/i.test(type)
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// firstByte reads one chunk. A failure here is still before anything was
|
|
982
|
+
// sent, so the caller can ask again. The reader stays open when a chunk
|
|
983
|
+
// did arrive: the rest of the body follows it.
|
|
984
|
+
async function firstByte(body) {
|
|
985
|
+
const reader = body.getReader()
|
|
986
|
+
try {
|
|
987
|
+
const { done, value } = await reader.read()
|
|
988
|
+
if (done) return { kind: "end", reader }
|
|
989
|
+
return { kind: "byte", value, reader }
|
|
990
|
+
} catch (cause) {
|
|
991
|
+
const reason = String(cause?.message || cause)
|
|
992
|
+
try { await reader.cancel(cause) } catch {}
|
|
993
|
+
return { kind: "cut", reason }
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
function bare(res, stream) {
|
|
998
|
+
const headers = new Headers(res.headers)
|
|
999
|
+
headers.delete("content-length")
|
|
1000
|
+
headers.delete("content-encoding")
|
|
1001
|
+
return new Response(stream, { status: res.status, statusText: res.statusText, headers })
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
// passThrough sends the chunk already read, then one upstream chunk per
|
|
1005
|
+
// pull. Nothing is buffered past that, and cancel stops the upstream read.
|
|
1006
|
+
function passThrough(res, reader, first) {
|
|
1007
|
+
let lead = first
|
|
1008
|
+
let leadWaiting = true
|
|
1009
|
+
const stream = new ReadableStream({
|
|
1010
|
+
async pull(controller) {
|
|
1011
|
+
if (leadWaiting) {
|
|
1012
|
+
leadWaiting = false
|
|
1013
|
+
if (lead) controller.enqueue(lead)
|
|
1014
|
+
lead = undefined
|
|
1015
|
+
return
|
|
1016
|
+
}
|
|
1017
|
+
try {
|
|
1018
|
+
const { done, value } = await reader.read()
|
|
1019
|
+
if (done) {
|
|
1020
|
+
controller.close()
|
|
1021
|
+
return
|
|
1022
|
+
}
|
|
1023
|
+
controller.enqueue(value)
|
|
1024
|
+
} catch (cause) {
|
|
1025
|
+
const reason = String(cause?.message || cause)
|
|
1026
|
+
try { await reader.cancel(cause) } catch {}
|
|
1027
|
+
// cancel already closed this stream: nothing more to report
|
|
1028
|
+
if (controller.desiredSize === null) return
|
|
1029
|
+
console.warn(`[grok-auth] stream failed (${reason.slice(0, 160)})`)
|
|
1030
|
+
try { controller.close() } catch {}
|
|
1031
|
+
}
|
|
1032
|
+
},
|
|
1033
|
+
cancel(reason) {
|
|
1034
|
+
return reader.cancel(reason)
|
|
1035
|
+
},
|
|
1036
|
+
})
|
|
1037
|
+
return bare(res, stream)
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
function ended(res) {
|
|
1041
|
+
return bare(res, new ReadableStream({
|
|
1042
|
+
start(controller) { controller.close() },
|
|
1043
|
+
}))
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
async function cutRetry(res, again, { tries = STREAM_TRIES } = {}) {
|
|
1047
|
+
let current = res
|
|
1048
|
+
for (let attempt = 1; attempt <= tries; attempt++) {
|
|
1049
|
+
if (attempt > 1) {
|
|
1050
|
+
try {
|
|
1051
|
+
current = await again()
|
|
1052
|
+
} catch (cause) {
|
|
1053
|
+
const said = String(cause?.message || cause)
|
|
1054
|
+
console.warn(`[grok-auth] retry ${attempt}/${tries} did not start: ${said.slice(0, 160)}`)
|
|
1055
|
+
if (!CUT.test(said) || attempt === tries) return ended(res)
|
|
1056
|
+
continue
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
if (!eventStream(current)) return current
|
|
1060
|
+
const read = await firstByte(current.body)
|
|
1061
|
+
if (read.kind === "byte") return passThrough(current, read.reader, read.value)
|
|
1062
|
+
if (read.kind === "end") {
|
|
1063
|
+
try { await read.reader.cancel() } catch {}
|
|
1064
|
+
return ended(current)
|
|
1065
|
+
}
|
|
1066
|
+
const reason = read.reason
|
|
1067
|
+
if (!CUT.test(reason) || attempt === tries) {
|
|
1068
|
+
console.warn(`[grok-auth] stream failed (${reason.slice(0, 160)})`)
|
|
1069
|
+
return ended(current)
|
|
1070
|
+
}
|
|
1071
|
+
console.warn(`[grok-auth] stream cut before any byte (${reason.slice(0, 140)}); retry ${attempt + 1}/${tries}`)
|
|
1072
|
+
}
|
|
1073
|
+
return ended(current)
|
|
1074
|
+
}
|
|
1075
|
+
|
|
961
1076
|
export const GrokAuthPlugin = async ({ client, magpie }) => {
|
|
962
1077
|
const remember = async (auth, c) => {
|
|
963
1078
|
if (auth.access === c.key && auth.expires === c.expires) return
|
|
@@ -1106,7 +1221,11 @@ export const GrokAuthPlugin = async ({ client, magpie }) => {
|
|
|
1106
1221
|
res = await send(c.key)
|
|
1107
1222
|
}
|
|
1108
1223
|
note(home, res)
|
|
1109
|
-
return kept(res)
|
|
1224
|
+
return kept(await cutRetry(res, async () => {
|
|
1225
|
+
const next = await send(c.key)
|
|
1226
|
+
note(home, next)
|
|
1227
|
+
return next
|
|
1228
|
+
}))
|
|
1110
1229
|
},
|
|
1111
1230
|
}
|
|
1112
1231
|
},
|
package/opencode2.mjs
CHANGED
|
@@ -85,8 +85,10 @@ export function fieldsOf(prompts) {
|
|
|
85
85
|
if (p.placeholder) f.placeholder = String(p.placeholder)
|
|
86
86
|
if (p.type === "select" && Array.isArray(p.options))
|
|
87
87
|
f.options = p.options.map((o) => ({ value: String(o.value), label: String(o.label ?? o.value), ...(o.hint ? { description: String(o.hint) } : {}) }))
|
|
88
|
-
// a prompt shown only when another answer says so can't be required
|
|
89
|
-
|
|
88
|
+
// a prompt shown only when another answer says so can't be required, and
|
|
89
|
+
// neither can one whose own validate() accepts an empty answer (the
|
|
90
|
+
// browser sign-in's optional API key)
|
|
91
|
+
if (!p.condition && p.required !== false) f.required = true
|
|
90
92
|
out.push(f)
|
|
91
93
|
}
|
|
92
94
|
return out.length ? out : undefined
|
package/package.json
CHANGED