qoder 0.1.4: a job token refresh Qoder refuses while listing the models marks the account, as the built-in's model read through QoderCredentialOf did — the models hook throws with signIn: expired on a 401/403 refresh and falls back to the listed models on any other failure; a renewal made while listing is said renewed by the account's next usage read or answer, and the refresh errors read 'qoder job token refresh' as the built-in's did. Four new tests in usage.test.mjs fail against 0.1.3.
This commit is contained in:
@@ -232,9 +232,9 @@ async function refreshJob(refresh, user) {
|
||||
const r = await openapi("/api/v1/jobToken/refresh", { method: "POST", body: { refresh_token: refresh } })
|
||||
if (r.status === 401 || r.status === 403)
|
||||
throw new SignInGone(`${user}'s Qoder sign-in has expired — sign in again (qoder job token refresh: status ${r.status})`, true)
|
||||
if (r.status !== 200) throw new Error(`Qoder job token refresh: status ${r.status}`)
|
||||
if (r.status !== 200) throw new Error(`qoder job token refresh: status ${r.status}`)
|
||||
if (!String(r.json?.token ?? "").trim() || !String(r.json?.refresh_token ?? "").trim())
|
||||
throw new Error("Qoder job token refresh: incomplete token pair")
|
||||
throw new Error("qoder job token refresh: incomplete token pair")
|
||||
return r.json
|
||||
}
|
||||
|
||||
@@ -894,6 +894,14 @@ export async function QoderAuthPlugin({ client }) {
|
||||
// the accounts fresh renewed: the built-in took the lapse mark off on a
|
||||
// renewed job token (qoderPersist), whatever the request then met
|
||||
const renewals = new WeakSet()
|
||||
// the accounts the models hook renewed: that hook has no way to say so
|
||||
// and still answer the list, so the account's next usage read or answer
|
||||
// says "renewed" for it
|
||||
const unsaid = new Set()
|
||||
const renewed = (cred) => {
|
||||
const was = unsaid.delete(cred.uid)
|
||||
return renewals.has(cred) || was
|
||||
}
|
||||
|
||||
// fresh is the account with a live job token, refreshed and saved near
|
||||
// its end.
|
||||
@@ -937,7 +945,7 @@ export async function QoderAuthPlugin({ client }) {
|
||||
let signIn = "kept"
|
||||
try {
|
||||
const cred = await fresh(getAuth)
|
||||
if (renewals.has(cred)) signIn = "renewed"
|
||||
if (renewed(cred)) signIn = "renewed"
|
||||
let env
|
||||
try {
|
||||
env = await fetchUsage(cred.deviceToken)
|
||||
@@ -1013,7 +1021,7 @@ export async function QoderAuthPlugin({ client }) {
|
||||
} catch (e) {
|
||||
return signedInError(e)
|
||||
}
|
||||
return signed(await ask(chat, cred, init), renewals.has(cred))
|
||||
return signed(await ask(chat, cred, init), renewed(cred))
|
||||
},
|
||||
}
|
||||
},
|
||||
@@ -1036,8 +1044,17 @@ export async function QoderAuthPlugin({ client }) {
|
||||
id: ID,
|
||||
async models(provider, { auth } = {}) {
|
||||
if (auth?.type !== "oauth") return provider.models
|
||||
let cred
|
||||
try {
|
||||
cred = await fresh(async () => auth)
|
||||
} catch (e) {
|
||||
// the built-in listed through QoderCredentialOf: a refused job
|
||||
// refresh marked the account there; anything else marked nothing
|
||||
if (e?.expired) throw Object.assign(new Error(e.message), { signIn: "expired" })
|
||||
return provider.models
|
||||
}
|
||||
if (renewals.has(cred)) unsaid.add(cred.uid)
|
||||
try {
|
||||
const cred = await fresh(async () => auth)
|
||||
const ms = await models(cred, true)
|
||||
if (!ms.length) return provider.models
|
||||
return Object.fromEntries(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-qoder-auth",
|
||||
"version": "0.1.3",
|
||||
"version": "0.1.4",
|
||||
"description": "Qoder subscription in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -209,7 +209,7 @@ test("no refresh token is a 401 the built-in didn't mark; a refresh that failed
|
||||
expect([res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([401, "kept"])
|
||||
res = await due(() => new Response("", { status: 500 }))
|
||||
expect([res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([502, null])
|
||||
expect((await res.json()).error.message).toBe("Qoder job token refresh: status 500")
|
||||
expect((await res.json()).error.message).toBe("qoder job token refresh: status 500")
|
||||
})
|
||||
|
||||
test("errors don't name Qoder, which magpie adds", async () => {
|
||||
@@ -290,3 +290,60 @@ test("an answer that went through keeps the mark, as the built-in's did; after a
|
||||
expect([res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([status, "renewed"])
|
||||
}
|
||||
})
|
||||
|
||||
// the models hook, as the built-in's listing (qoderFetchModels through
|
||||
// QoderCredentialOf) treated the sign-in: a refused job refresh marks it
|
||||
// (thrown with signIn "expired"), a renewal clears it (said by the account's
|
||||
// next usage read or answer), and nothing else marks it
|
||||
async function dueModels(refresh, listing = () => json(LISTING)) {
|
||||
let auth = { ...account(), expires: 0, machineId: "m1", name: "One" }
|
||||
globalThis.fetch = async (url) => {
|
||||
const path = new URL(String(url)).pathname
|
||||
if (path === "/api/v1/jobToken/refresh") return refresh()
|
||||
if (path.endsWith("/model/list")) return listing()
|
||||
if (path === "/sash/api/v2/me/usage") return json(USAGE)
|
||||
return new Response("", { status: 404 })
|
||||
}
|
||||
const hooks = await QoderAuthPlugin({ client: { auth: { set: async ({ body }) => (auth = { ...body }) /* saved, then read back, as magpie does */ } } })
|
||||
const fallback = { models: { stat: { id: "stat" } } }
|
||||
return { hooks, fallback, list: () => hooks.provider.models(fallback, { auth }), usage: () => hooks.auth.usage(async () => auth) }
|
||||
}
|
||||
|
||||
for (const status of [401, 403]) {
|
||||
test(`a job refresh refused ${status} while listing models throws signIn "expired"`, async () => {
|
||||
const m = await dueModels(() => new Response("", { status }))
|
||||
const err = await m.list().catch((e) => e)
|
||||
expect(err).toBeInstanceOf(Error)
|
||||
expect(err.signIn).toBe("expired")
|
||||
expect(err.message).toContain("sign-in has expired")
|
||||
})
|
||||
}
|
||||
|
||||
test("a listing whose refresh fails otherwise, or has no refresh token, marks nothing", async () => {
|
||||
let m = await dueModels(() => new Response("", { status: 500 }))
|
||||
expect(await m.list()).toBe(m.fallback.models)
|
||||
m = await dueModels(RENEWED, () => new Response("", { status: 401 }))
|
||||
// the refresh went through: the refused listing marks nothing
|
||||
expect(await m.list()).toBe(m.fallback.models)
|
||||
const hooks = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } })
|
||||
const fb = { models: {} }
|
||||
expect(await hooks.provider.models(fb, { auth: { ...account(), refresh: "", expires: 0 } })).toBe(fb.models)
|
||||
})
|
||||
|
||||
test("a job token the models hook renewed is said renewed by the next usage read, once", async () => {
|
||||
const m = await dueModels(RENEWED)
|
||||
const ms = await m.list()
|
||||
expect(Object.keys(ms)).toEqual(["qmodel", "fmodel", "pmodel", "cmodel"])
|
||||
expect((await m.usage()).signIn).toBe("renewed")
|
||||
expect((await m.usage()).signIn).toBe("kept")
|
||||
})
|
||||
|
||||
test("a job token the models hook renewed is said renewed by the next answer", async () => {
|
||||
const m = await dueModels(RENEWED)
|
||||
await m.list()
|
||||
const l = await m.hooks.auth.loader(async () => ({ ...account(), machineId: "m1" }))
|
||||
const res = await l.fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "nomodel", messages: [] }) })
|
||||
expect(res.headers.get("X-Magpie-Sign-In")).toBe("renewed")
|
||||
const again = await l.fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "nomodel", messages: [] }) })
|
||||
expect(again.headers.get("X-Magpie-Sign-In")).toBe(null)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user