@magpie-community/opencode-kiro-auth 0.1.36 → 0.1.37

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 (3) hide show
  1. package/README.md +22 -0
  2. package/index.mjs +75 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -297,6 +297,28 @@ again.
297
297
 
298
298
  Builder ID, Google, GitHub and API key sign-ins keep one region.
299
299
 
300
+ Two switches in the provider's editor, "Use us-east-1 only" and "Use
301
+ eu-central-1 only" (`us-east-1-only`, `eu-central-1-only`), pin an Identity
302
+ Center account to one region. They are read at each request, so a change
303
+ needs no restart:
304
+
305
+ - With one on, every request goes to that region and none to the other,
306
+ whatever credits the other has. The region not enabled for the account, or
307
+ used up, is a 429 naming the switch, and magpie goes on to another
308
+ account. The other region is never tried.
309
+ - `auth.usage` counts the pinned region alone in the window magpie weighs
310
+ the account by; the other region's windows are shown only. So the account
311
+ is used up for magpie when the pinned region is, not when both are.
312
+ - Both off, or both on, is the default: the regions in turn.
313
+
314
+ Credits past a region's plan are overage, billed at $0.04 each: twice what
315
+ a plan's credits cost (a Power plan is $200 for 10000, $0.02 each), and a
316
+ plan's credits do not roll over to the next month. So when one region is
317
+ already in its overage and the other still has plan credits, pinning the
318
+ account to the other spends the cheaper credits first. Left on the first,
319
+ the account goes on into overage while the other region's plan credits
320
+ expire unused.
321
+
300
322
  ## Not included
301
323
 
302
324
  - Switching between several accounts. OpenCode keeps one sign-in per
package/index.mjs CHANGED
@@ -423,6 +423,21 @@ async function profileOf(c) {
423
423
  // only with every enabled region used up is it used up for magpie.
424
424
  const CREDIT_REGIONS = ["us-east-1", "eu-central-1"]
425
425
  const twoRegions = (c) => c.method === "idc" && !c.builderID
426
+ // magpie's switches for the provider (auth.options), one for each credit
427
+ // region: with one on, an Identity Center account uses that region alone.
428
+ // They are read at each request, so a change needs no restart. A request is
429
+ // never sent to the other region: this one not enabled for the account, or
430
+ // used up, is a 429 for magpie to go on to another account. Both off is the
431
+ // default, the regions in turn; both on says nothing, and is the default too.
432
+ const onlyOption = (region) => region + "-only"
433
+ const onlyRegion = (magpie) => {
434
+ try {
435
+ const on = CREDIT_REGIONS.filter((r) => magpie?.option?.(ID, onlyOption(r)) === true)
436
+ return on.length === 1 ? on[0] : ""
437
+ } catch {
438
+ return ""
439
+ }
440
+ }
426
441
  const SPENT_MS = 60 * 60 * 1000 // a region Kiro said is used up, with no reset known, rests this long
427
442
  const PROBE_MS = 10 * 60 * 1000 // a region with no profile is asked for one again at most this often
428
443
  const REMIND_SHARE = 80 // the share at which a region not enabled is pointed out
@@ -571,7 +586,7 @@ const usageOf = (l, now = Date.now()) => readUsage(l, now).usage
571
586
  // whether it is used up and when it resets. Regions read come first, then
572
587
  // those that couldn't be read, then those not enabled: a card that shows
573
588
  // only the first few windows then shows the credits an account has.
574
- function regionsUsage(reads, now = Date.now()) {
589
+ function regionsUsage(reads, now = Date.now(), only = "") {
575
590
  const shown = []
576
591
  const failed = []
577
592
  const off = []
@@ -593,9 +608,20 @@ function regionsUsage(reads, now = Date.now()) {
593
608
  first ??= u
594
609
  if (!stop) continue
595
610
  const over = stop !== plan
596
- anyOver ||= over
597
- allOver &&= over
611
+ // an account set to one region counts that region alone, the other
612
+ // shown only: its credits are not the account's to spend
613
+ const counted = !only || r.region === only
614
+ if (counted) {
615
+ anyOver ||= over
616
+ allOver &&= over
617
+ }
598
618
  monthName ??= plan.name
619
+ if (!counted) {
620
+ shown.push({ ...plan, name: `${plan.name} (${r.region})`, aside: true })
621
+ if (over) shown.push({ ...stop, name: `${stop.name} (${r.region})`, aside: true })
622
+ regions.push({ region: r.region, full: stop.amount >= stop.limit, resetsAt: Date.parse(stop.resetsAt ?? "") > 0 ? Date.parse(stop.resetsAt) : 0 })
623
+ continue
624
+ }
599
625
  amount += stop.amount
600
626
  limit += stop.limit
601
627
  inPlan += plan.amount
@@ -613,9 +639,19 @@ function regionsUsage(reads, now = Date.now()) {
613
639
  windows.push({ name: monthName, used: (100 * inPlan) / ofPlan, ...resetsAt, display: count(inPlan, ofPlan), amount: inPlan, limit: ofPlan,
614
640
  unit: "credits", span: 30 * 24 * 3600, aside: true })
615
641
  let display = count(amount, limit)
616
- if (missing.length && (100 * amount) / limit >= REMIND_SHARE) display += ` · ${missing.join(", ")} not enabled: ask your admin to enable it`
642
+ // a region the user said not to use is not asked to be enabled
643
+ const ask = only ? missing.filter((r) => r === only) : missing
644
+ if (ask.length && (100 * amount) / limit >= REMIND_SHARE) display += ` · ${ask.join(", ")} not enabled: ask your admin to enable it`
617
645
  windows.push({ name: allOver ? "Overage" : monthName, used: (100 * amount) / limit, ...resetsAt,
618
646
  display, amount, limit, unit: "credits", span: 30 * 24 * 3600 })
647
+ } else if (only) {
648
+ // the region the account is pinned to is not enabled or couldn't be read:
649
+ // no window to weigh, so magpie would go on asking an account every
650
+ // request of which is a 429. A window that is used up leaves it out of
651
+ // routing, and says why.
652
+ const why = reads.find((r) => r.region === only)?.error ? `${only} couldn't be read` : `${only} is not enabled`
653
+ windows.push({ name: monthName ?? "Credits", used: 100, display: `${why}, and the ${onlyOption(only)} switch is on: no other region is used`,
654
+ span: 30 * 24 * 3600 })
619
655
  }
620
656
  windows.push(...shown, ...failed, ...off, ...(first?.windows ?? []).filter((w) => !w.span))
621
657
  const { windows: _w, ...who } = first ?? {}
@@ -647,7 +683,7 @@ async function credOf(auth) {
647
683
  // sign-in read with that token (its save failed, or magpie hasn't yet
648
684
  // saved what auth.refresh gave) goes on with the newer one rather than
649
685
  // spending the replaced refresh token again.
650
- function account(client) {
686
+ function account(client, magpie) {
651
687
  let held = null // { id, c }
652
688
  const after = new Map()
653
689
  const newest = (c) => {
@@ -766,9 +802,13 @@ function account(client) {
766
802
  if (!twoRegions(c) || st.builderID || !c.profiles) return a
767
803
  // the first enabled region with credits left, in CREDIT_REGIONS' order
768
804
  const now = Date.now()
769
- const open = CREDIT_REGIONS.filter((r) => c.profiles[r])
805
+ const only = onlyRegion(magpie)
806
+ const open = CREDIT_REGIONS.filter((r) => c.profiles[r] && (!only || r === only))
770
807
  const pick = open.find((r) => !(st.spent[r] > now))
771
808
  if (pick) Object.assign(a, { region: pick, profile: c.profiles[pick] })
809
+ // pinned to a region this account lacks, or has used up: no region to
810
+ // send to, which ask() answers as every region used up
811
+ else if (only) return { ...a, region: only, profile: "", key, profiles: { ...c.profiles }, open, pinned: only }
772
812
  return { ...a, key, profiles: { ...c.profiles }, open }
773
813
  })
774
814
  // spend marks a region of an account used up till until (0: SPENT_MS from
@@ -2594,6 +2634,7 @@ async function ask(creds, auth, req, model, budget, signal, conversation) {
2594
2634
  } catch (e) {
2595
2635
  return { status: 401, message: e.message }
2596
2636
  }
2637
+ if (a.pinned) return { status: 429, message: pinnedUnavailable(a) }
2597
2638
  let res
2598
2639
  try {
2599
2640
  res = await paced(a, await kiroBody(req, model, a.profile, budget, conversation), signal)
@@ -2631,6 +2672,7 @@ async function ask(creds, auth, req, model, budget, signal, conversation) {
2631
2672
  } catch (e) {
2632
2673
  return { status: 401, message: e.message }
2633
2674
  }
2675
+ if (next.pinned) return { status: 429, message: pinnedUnavailable(next) + f.message, wait: f.wait }
2634
2676
  if (tried.has(next.region)) return { status: 429, message: regionsUsedUp(next) + f.message, wait: f.wait }
2635
2677
  a = next
2636
2678
  try {
@@ -2648,6 +2690,16 @@ async function ask(creds, auth, req, model, budget, signal, conversation) {
2648
2690
  return { res, a }
2649
2691
  }
2650
2692
 
2693
+ // pinnedUnavailable heads the 429 of an account set to one region (a
2694
+ // "<region>-only" switch) whose region is not enabled or used up: the other
2695
+ // region is not tried, as the switch says.
2696
+ function pinnedUnavailable(a) {
2697
+ const enabled = a.open?.includes(a.pinned)
2698
+ return enabled
2699
+ ? `Kiro credits used up in ${a.pinned}, the only region this account is set to use (magpie's ${onlyOption(a.pinned)} switch is on). `
2700
+ : `${a.pinned} is not enabled for this account, and magpie's ${onlyOption(a.pinned)} switch is on, so no other region is used: ask your admin to enable it. `
2701
+ }
2702
+
2651
2703
  // regionsUsedUp heads the 429 of an account with every enabled region used
2652
2704
  // up: the regions, and those not enabled, which an admin may enable.
2653
2705
  function regionsUsedUp(a) {
@@ -3086,8 +3138,8 @@ async function bodyOf(input, init) {
3086
3138
  return JSON.parse(typeof b === "string" ? b : new TextDecoder().decode(b))
3087
3139
  }
3088
3140
 
3089
- export async function KiroAuthPlugin({ client } = {}) {
3090
- const creds = account(client)
3141
+ export async function KiroAuthPlugin({ client, magpie } = {}) {
3142
+ const creds = account(client, magpie)
3091
3143
  return {
3092
3144
  auth: {
3093
3145
  provider: ID,
@@ -3145,7 +3197,7 @@ export async function KiroAuthPlugin({ client } = {}) {
3145
3197
  }
3146
3198
  }))
3147
3199
  if (reads.every((r) => r.error || !r.l)) throw reads.find((r) => r.error)?.error ?? new Error("Kiro read no credits")
3148
- const got = regionsUsage(reads)
3200
+ const got = regionsUsage(reads, Date.now(), onlyRegion(magpie))
3149
3201
  for (const r of got.regions) creds.spend(a.key, r.region, r.full ? r.resetsAt || 0 : -1)
3150
3202
  u = got.usage
3151
3203
  } else u = usageOf(await usageLimits(a, true))
@@ -3158,6 +3210,20 @@ export async function KiroAuthPlugin({ client } = {}) {
3158
3210
  },
3159
3211
  // labels, messages and hints are magpie's own fields: the same words
3160
3212
  // by the window's language (OpenCode shows the English)
3213
+ // magpie's switches for the provider (#2038): shown in its editor, read
3214
+ // with input.magpie.option at each request
3215
+ options: [
3216
+ ...CREDIT_REGIONS.map((region, i) => {
3217
+ const other = CREDIT_REGIONS[1 - i]
3218
+ return {
3219
+ key: onlyOption(region),
3220
+ label: `Use ${region} only`,
3221
+ description: `Send every request to ${region}, none to ${other}; with it not enabled or used up, magpie moves to another account.`,
3222
+ labels: { zh: `只使用 ${region}` },
3223
+ descriptions: { zh: `所有请求发往 ${region},不发往 ${other};该区域未开通或额度用完时,magpie 换下一个账号。` },
3224
+ }
3225
+ }),
3226
+ ],
3161
3227
  methods: [
3162
3228
  { type: "oauth", label: "Kiro (Google, GitHub, AWS Builder ID, IAM Identity Center)", labels: WORDS.browser,
3163
3229
  prompts: [{ type: "select", key: "loginMethod", message: "Choose a sign-in method", messages: WORDS.choose, options: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-kiro-auth",
3
- "version": "0.1.36",
3
+ "version": "0.1.37",
4
4
  "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",