all: each plugin fails with its built-in's status and marks the sign-in as it did, through X-Magpie-Sign-In (要保证插件跟内置的所有逻辑都要一模一样) — Factory's and MiMo's refused renewals are the built-ins' 502 marked expired, not 401s; Qoder's vendor 403 is the built-in's 401 kept, its model list's failure a 400, its job refresh refused marked expired; Zed's retried 401 is a 401 kept, not a 403; Kiro's unreachable refresh a 401 kept; Cursor's expired trial a 401 kept; a vendor 401 WorkBuddy, Grok, Devin, Command Code and ZCode passed on no longer marks the account, as their built-ins never did, and Grok and MiMo no longer retry what their built-ins sent once. Messages take the built-ins' words. New tests fail against the previous index.mjs.
This commit is contained in:
@@ -28,6 +28,19 @@ async function ask(reply, stream = false) {
|
||||
return { status: res.status, message: (await res.json()).error?.message }
|
||||
}
|
||||
|
||||
// asks as ask does, with every answer's X-Magpie-Sign-In
|
||||
async function signIn(reply, plan = "Go", path = "/alpha/generate") {
|
||||
globalThis.fetch = async (url) => (new URL(String(url)).pathname === path ? reply() : new Response("", { status: 404 }))
|
||||
const auth = { type: "api", key: "key", metadata: { plan } }
|
||||
const hooks = await CommandCodePlugin()
|
||||
const opts = await hooks.auth.loader(async () => auth)
|
||||
const res = await opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ model: "deepseek/deepseek-v4-pro", messages: [{ role: "user", content: "hi" }] }),
|
||||
})
|
||||
return { status: res.status, signIn: res.headers.get("X-Magpie-Sign-In") }
|
||||
}
|
||||
|
||||
test("an empty 429 reads as Go's http.StatusText says it, which quotaWords knows", async () => {
|
||||
expect(await ask(() => new Response("", { status: 429 }))).toEqual({ status: 429, message: "Too Many Requests" })
|
||||
})
|
||||
@@ -46,3 +59,20 @@ test("an error line before any content keeps its status", async () => {
|
||||
const line = JSON.stringify({ type: "error", error: { message: '429 {"error":{"message":"RATE_LIMITED: slow down"}}' } })
|
||||
expect(await ask(() => new Response(line + "\n"), true)).toEqual({ status: 429, message: "RATE_LIMITED: slow down" })
|
||||
})
|
||||
|
||||
// the built-in never marked a Command Code account lapsed: a 401 of
|
||||
// /alpha/generate's (cmdFailure) or of the Provider API's goes on with it kept
|
||||
test("a Go key Command Code turns away is a 401, the account kept", async () => {
|
||||
expect(await signIn(() => Response.json({ error: { message: "Invalid API key" } }, { status: 401 }))).toEqual({ status: 401, signIn: "kept" })
|
||||
})
|
||||
|
||||
test("an error line saying 401 keeps the account too", async () => {
|
||||
const line = JSON.stringify({ type: "error", error: { message: "Unauthorized", statusCode: 401 } })
|
||||
expect(await signIn(() => new Response(line + "\n"))).toEqual({ status: 401, signIn: "kept" })
|
||||
})
|
||||
|
||||
test("the Provider API's 401 goes on as it came, the account kept", async () => {
|
||||
expect(await signIn(() => Response.json({ error: { message: "Invalid API key" } }, { status: 401 }), "Pro", "/provider/v1/chat/completions"))
|
||||
.toEqual({ status: 401, signIn: "kept" })
|
||||
expect(await signIn(() => Response.json({}), "Pro", "/provider/v1/chat/completions")).toEqual({ status: 200, signIn: null })
|
||||
})
|
||||
|
||||
@@ -719,12 +719,24 @@ function streamError(e) {
|
||||
return failure(status || 500, msg)
|
||||
}
|
||||
|
||||
// errorResponse is a failure as the built-in answered it, the account
|
||||
// kept: the built-in never marked a Command Code account lapsed, a 401 of
|
||||
// Command Code's among them.
|
||||
const errorResponse = ({ status, message }) =>
|
||||
new Response(JSON.stringify({ error: { message, type: "commandcode_error", code: status } }), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept" },
|
||||
})
|
||||
|
||||
// kept is a Provider API answer, as it came, saying the account is kept.
|
||||
function kept(res) {
|
||||
const headers = new Headers(res.headers)
|
||||
headers.delete("content-length")
|
||||
headers.delete("content-encoding")
|
||||
headers.set("X-Magpie-Sign-In", "kept")
|
||||
return new Response(res.body, { status: res.status, statusText: res.statusText, headers })
|
||||
}
|
||||
|
||||
// lines reads the reply a JSON object a line.
|
||||
async function* lines(body) {
|
||||
const dec = new TextDecoder()
|
||||
@@ -907,7 +919,9 @@ export async function CommandCodePlugin({ client } = {}) {
|
||||
}
|
||||
return generate(key, chat, init.signal)
|
||||
}
|
||||
return fetch(input, { ...init, headers })
|
||||
// the Provider API's 401 went on as it came, the account kept
|
||||
const res = await fetch(input, { ...init, headers })
|
||||
return res.status === 401 ? kept(res) : res
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-commandcode-auth",
|
||||
"version": "0.1.4",
|
||||
"version": "0.1.5",
|
||||
"description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
Reference in New Issue
Block a user