Parity with magpie's built-ins: a failed sign-in says why, CLI-backed Command Code and Devin accounts follow the CLI's key, Factory's refused renewal is a 401 in the caller's API shape
- every {type:"failed"} carries an error reason magpie shows
- commandcode/devin: accounts taken from the CLI are marked metadata.cli and re-read its credentials on each request, usage and models
- factory: a refused refresh answers 401 authentication_error (Anthropic shape on /llm/a/, OpenAI's elsewhere) instead of throwing; the whoami back-off is per account; 403 hints name the account to sign in again
- versions: factory/devin/commandcode/cursor/kiro/mimo/zed/zcode 0.1.2, workbuddy 0.1.3
This commit is contained in:
+34
-21
@@ -253,8 +253,19 @@ const orgRefused = (status, text) => status === 403 && text.toLowerCase().includ
|
||||
// the request carries what droid sends.
|
||||
function explain(status, text) {
|
||||
if (status !== 403) return ""
|
||||
if (orgRefused(status, text)) return "the Factory account's organization changed; sign in to it again"
|
||||
return "Factory refused this account the request; check that `droid`, signed in to the same account and organization, can use this model (an organization's model policy or the plan may not allow it), and if it can, sign in to the Factory account again"
|
||||
if (orgRefused(status, text)) return "the Factory account's organization changed; remove the account in magpie and sign in to it again"
|
||||
return "Factory refused this account the request; check that `droid`, signed in to the same account and organization, can use this model (an organization's model policy or the plan may not allow it), and if it can, remove the Factory account in magpie and sign in to it again"
|
||||
}
|
||||
|
||||
// errorReply is an error as the API the request was for words one:
|
||||
// Anthropic's on /llm/a/, OpenAI's on the others.
|
||||
function errorReply(url, status, type, message, headers = new Headers()) {
|
||||
headers.set("content-type", "application/json")
|
||||
headers.delete("content-length")
|
||||
headers.delete("content-encoding")
|
||||
const anthropic = new URL(url).pathname.includes("/llm/a/")
|
||||
const body = anthropic ? { type: "error", error: { type, message } } : { error: { message, type, code: null } }
|
||||
return new Response(JSON.stringify(body), { status, headers })
|
||||
}
|
||||
|
||||
// ---- sign-in ----------------------------------------------------------------
|
||||
@@ -462,9 +473,11 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
||||
lock = run.catch(() => {})
|
||||
return run
|
||||
}
|
||||
// when the account last asked whoami for its org, so one whoami can't
|
||||
// answer doesn't ask before every request
|
||||
let asked = 0
|
||||
// when each account last asked whoami for its org, so one whoami can't
|
||||
// answer doesn't ask before every request, nor hold back the others
|
||||
const asked = new Map()
|
||||
const askedOf = (c) => asked.get(c.accountId || c.refresh || "") ?? 0
|
||||
const ask = (c) => asked.set(c.accountId || c.refresh || "", Date.now())
|
||||
|
||||
// account is the account getAuth reads, with what keeps its token live
|
||||
// and its org right.
|
||||
@@ -483,8 +496,8 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
||||
// whoami as soon as it holds a token and sends the orgId it answers
|
||||
// as X-Factory-Org-Id on every request after.
|
||||
const orgOf = async (c) => {
|
||||
if (c.activeOrganizationId || Date.now() - asked < ASK_AGAIN) return c
|
||||
asked = Date.now()
|
||||
if (c.activeOrganizationId || Date.now() - askedOf(c) < ASK_AGAIN) return c
|
||||
ask(c)
|
||||
if (await reconcile(c)) await save(c)
|
||||
return c
|
||||
}
|
||||
@@ -500,7 +513,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
||||
} catch (e) {
|
||||
// a hiccup while the token still runs: go on with it
|
||||
if (!refused(e) && c.expires > 0 && Date.now() < c.expires) return orgOf(c)
|
||||
if (refused(e)) throw new Error(`${c.accountId || "the account"}'s Factory sign-in has expired — sign in again (${e.message})`)
|
||||
if (refused(e)) throw Object.assign(new Error(`${c.accountId || "the account"}'s Factory sign-in has expired — sign in again (${e.message})`), { lapsed: true })
|
||||
throw e
|
||||
}
|
||||
c.access = t.access_token
|
||||
@@ -508,7 +521,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
||||
if (t.refresh_token) c.refresh = t.refresh_token
|
||||
// droid asks whoami again for each new token, keeping the org it names
|
||||
await reconcile(c)
|
||||
asked = Date.now()
|
||||
ask(c)
|
||||
await save(c)
|
||||
return c
|
||||
})
|
||||
@@ -528,12 +541,12 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
||||
const was = c.activeOrganizationId
|
||||
c.activeOrganizationId = ""
|
||||
if ((await reconcile(c)) && c.activeOrganizationId === was) c.activeOrganizationId = "" // whoami names the org refused: send none
|
||||
asked = Date.now()
|
||||
ask(c)
|
||||
await save(c)
|
||||
return true
|
||||
}
|
||||
if (!isOrg) {
|
||||
asked = Date.now()
|
||||
ask(c)
|
||||
if ((await reconcile(c)) && c.activeOrganizationId) {
|
||||
await save(c)
|
||||
return true
|
||||
@@ -651,7 +664,15 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
||||
let body = init?.body
|
||||
if (body == null && input instanceof Request && input.body) body = new Uint8Array(await input.arrayBuffer())
|
||||
if (body instanceof ReadableStream) body = new Uint8Array(await new Response(body).arrayBuffer())
|
||||
let res = await send(input, init, body)
|
||||
const url = input instanceof Request ? input.url : String(input)
|
||||
let res
|
||||
try {
|
||||
res = await send(input, init, body)
|
||||
} catch (e) {
|
||||
// Factory refused to renew the sign-in: the account's own 401
|
||||
if (e?.lapsed) return errorReply(url, 401, "authentication_error", e.message)
|
||||
throw e
|
||||
}
|
||||
if (res.status !== 403) return res
|
||||
let text = await res.text()
|
||||
if (await mendOrg(res.status, text).catch(() => false)) {
|
||||
@@ -661,15 +682,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
|
||||
}
|
||||
const why = explain(res.status, text)
|
||||
const msg = `${apiError(text, "Forbidden")}: ${why}`
|
||||
const headers = new Headers(res.headers)
|
||||
headers.set("content-type", "application/json")
|
||||
headers.delete("content-length")
|
||||
headers.delete("content-encoding")
|
||||
return new Response(JSON.stringify({ type: "error", error: { type: "permission_error", message: msg } }), {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers,
|
||||
})
|
||||
return errorReply(url, res.status, "permission_error", msg, new Headers(res.headers))
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// An account whose sign-in Factory won't renew answers as the built-in's
|
||||
// does: a 401 in the shape of the API the request was for, not a throw.
|
||||
import { afterEach, expect, test } from "bun:test"
|
||||
import { FactoryAuthPlugin } from "./index.mjs"
|
||||
|
||||
const real = globalThis.fetch
|
||||
afterEach(() => (globalThis.fetch = real))
|
||||
|
||||
const expired = {
|
||||
type: "oauth",
|
||||
access: "tok-old",
|
||||
refresh: "r-old",
|
||||
expires: Date.now() - 1000,
|
||||
accountId: "ada",
|
||||
activeOrganizationId: "fac_A",
|
||||
region: "",
|
||||
premBaseHost: "",
|
||||
}
|
||||
|
||||
async function loaded() {
|
||||
const seen = []
|
||||
globalThis.fetch = async (url) => {
|
||||
const u = new URL(String(url))
|
||||
seen.push(u.pathname)
|
||||
if (u.pathname.endsWith("/authenticate")) return new Response(JSON.stringify({ error: "invalid_grant" }), { status: 400 })
|
||||
return new Response("upstream reached", { status: 500 })
|
||||
}
|
||||
let auth = { ...expired }
|
||||
const client = { auth: { set: async ({ body }) => (auth = body) } }
|
||||
const hooks = await FactoryAuthPlugin({ client })
|
||||
const l = await hooks.auth.loader(async () => auth)
|
||||
return { l, seen }
|
||||
}
|
||||
|
||||
test("a refused renewal is Anthropic's 401 on /llm/a/", async () => {
|
||||
const { l, seen } = await loaded()
|
||||
const res = await l.fetch("https://api.factory.ai/api/llm/a/v1/messages", { method: "POST", body: JSON.stringify({ model: "claude-x" }) })
|
||||
expect(res.status).toBe(401)
|
||||
const b = await res.json()
|
||||
expect(b.type).toBe("error")
|
||||
expect(b.error.type).toBe("authentication_error")
|
||||
expect(b.error.message).toContain("sign in")
|
||||
expect(seen.some((p) => p.includes("/llm/"))).toBe(false)
|
||||
})
|
||||
|
||||
test("and OpenAI's on /llm/o/", async () => {
|
||||
const { l } = await loaded()
|
||||
const res = await l.fetch("https://api.factory.ai/api/llm/o/v1/responses", { method: "POST", body: JSON.stringify({ model: "gpt-x" }) })
|
||||
expect(res.status).toBe(401)
|
||||
const b = await res.json()
|
||||
expect(b.error.type).toBe("authentication_error")
|
||||
expect(b.error.code).toBe(null)
|
||||
expect(b.type).toBeUndefined()
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-factory-auth",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.2",
|
||||
"description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
Reference in New Issue
Block a user