@magpie-community/opencode-factory-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.
Files changed (2) hide show
  1. package/index.mjs +34 -21
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -253,8 +253,19 @@ const orgRefused = (status, text) => status === 403 && text.toLowerCase().includ
253
253
  // the request carries what droid sends.
254
254
  function explain(status, text) {
255
255
  if (status !== 403) return ""
256
- if (orgRefused(status, text)) return "the Factory account's organization changed; sign in to it again"
257
- 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"
256
+ if (orgRefused(status, text)) return "the Factory account's organization changed; remove the account in magpie and sign in to it again"
257
+ 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"
258
+ }
259
+
260
+ // errorReply is an error as the API the request was for words one:
261
+ // Anthropic's on /llm/a/, OpenAI's on the others.
262
+ function errorReply(url, status, type, message, headers = new Headers()) {
263
+ headers.set("content-type", "application/json")
264
+ headers.delete("content-length")
265
+ headers.delete("content-encoding")
266
+ const anthropic = new URL(url).pathname.includes("/llm/a/")
267
+ const body = anthropic ? { type: "error", error: { type, message } } : { error: { message, type, code: null } }
268
+ return new Response(JSON.stringify(body), { status, headers })
258
269
  }
259
270
 
260
271
  // ---- sign-in ----------------------------------------------------------------
@@ -462,9 +473,11 @@ export const FactoryAuthPlugin = async ({ client }) => {
462
473
  lock = run.catch(() => {})
463
474
  return run
464
475
  }
465
- // when the account last asked whoami for its org, so one whoami can't
466
- // answer doesn't ask before every request
467
- let asked = 0
476
+ // when each account last asked whoami for its org, so one whoami can't
477
+ // answer doesn't ask before every request, nor hold back the others
478
+ const asked = new Map()
479
+ const askedOf = (c) => asked.get(c.accountId || c.refresh || "") ?? 0
480
+ const ask = (c) => asked.set(c.accountId || c.refresh || "", Date.now())
468
481
 
469
482
  // account is the account getAuth reads, with what keeps its token live
470
483
  // and its org right.
@@ -483,8 +496,8 @@ export const FactoryAuthPlugin = async ({ client }) => {
483
496
  // whoami as soon as it holds a token and sends the orgId it answers
484
497
  // as X-Factory-Org-Id on every request after.
485
498
  const orgOf = async (c) => {
486
- if (c.activeOrganizationId || Date.now() - asked < ASK_AGAIN) return c
487
- asked = Date.now()
499
+ if (c.activeOrganizationId || Date.now() - askedOf(c) < ASK_AGAIN) return c
500
+ ask(c)
488
501
  if (await reconcile(c)) await save(c)
489
502
  return c
490
503
  }
@@ -500,7 +513,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
500
513
  } catch (e) {
501
514
  // a hiccup while the token still runs: go on with it
502
515
  if (!refused(e) && c.expires > 0 && Date.now() < c.expires) return orgOf(c)
503
- if (refused(e)) throw new Error(`${c.accountId || "the account"}'s Factory sign-in has expired — sign in again (${e.message})`)
516
+ 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 })
504
517
  throw e
505
518
  }
506
519
  c.access = t.access_token
@@ -508,7 +521,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
508
521
  if (t.refresh_token) c.refresh = t.refresh_token
509
522
  // droid asks whoami again for each new token, keeping the org it names
510
523
  await reconcile(c)
511
- asked = Date.now()
524
+ ask(c)
512
525
  await save(c)
513
526
  return c
514
527
  })
@@ -528,12 +541,12 @@ export const FactoryAuthPlugin = async ({ client }) => {
528
541
  const was = c.activeOrganizationId
529
542
  c.activeOrganizationId = ""
530
543
  if ((await reconcile(c)) && c.activeOrganizationId === was) c.activeOrganizationId = "" // whoami names the org refused: send none
531
- asked = Date.now()
544
+ ask(c)
532
545
  await save(c)
533
546
  return true
534
547
  }
535
548
  if (!isOrg) {
536
- asked = Date.now()
549
+ ask(c)
537
550
  if ((await reconcile(c)) && c.activeOrganizationId) {
538
551
  await save(c)
539
552
  return true
@@ -651,7 +664,15 @@ export const FactoryAuthPlugin = async ({ client }) => {
651
664
  let body = init?.body
652
665
  if (body == null && input instanceof Request && input.body) body = new Uint8Array(await input.arrayBuffer())
653
666
  if (body instanceof ReadableStream) body = new Uint8Array(await new Response(body).arrayBuffer())
654
- let res = await send(input, init, body)
667
+ const url = input instanceof Request ? input.url : String(input)
668
+ let res
669
+ try {
670
+ res = await send(input, init, body)
671
+ } catch (e) {
672
+ // Factory refused to renew the sign-in: the account's own 401
673
+ if (e?.lapsed) return errorReply(url, 401, "authentication_error", e.message)
674
+ throw e
675
+ }
655
676
  if (res.status !== 403) return res
656
677
  let text = await res.text()
657
678
  if (await mendOrg(res.status, text).catch(() => false)) {
@@ -661,15 +682,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
661
682
  }
662
683
  const why = explain(res.status, text)
663
684
  const msg = `${apiError(text, "Forbidden")}: ${why}`
664
- const headers = new Headers(res.headers)
665
- headers.set("content-type", "application/json")
666
- headers.delete("content-length")
667
- headers.delete("content-encoding")
668
- return new Response(JSON.stringify({ type: "error", error: { type: "permission_error", message: msg } }), {
669
- status: res.status,
670
- statusText: res.statusText,
671
- headers,
672
- })
685
+ return errorReply(url, res.status, "permission_error", msg, new Headers(res.headers))
673
686
  },
674
687
  }
675
688
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",