@magpie-community/opencode-kiro-auth 0.1.40 → 0.1.42

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 +18 -7
  2. package/index.mjs +39 -16
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -55,8 +55,10 @@ them, are under "Other ways to sign in…".
55
55
  again the same way goes beside it rather than replacing it: magpie
56
56
  matches a sign-in by its name, and the plugin can't see the other
57
57
  accounts to match it by `uid`.
58
- - A company's own identity provider (`external_idp`) is not supported
59
- here. Sign in with `kiro-cli login` and use the next method instead.
58
+ - A company's own identity provider (`external_idp`) is not offered as a
59
+ browser sign-in here. Sign in with `kiro-cli login` or the Kiro IDE and
60
+ use the next method instead; its management calls carry
61
+ `TokenType: EXTERNAL_IDP`.
60
62
  - **Kiro CLI's or Kiro IDE's sign-in**: uses the account kiro-cli is signed
61
63
  in to, or else the Kiro IDE's. Nothing is copied. It is read each time
62
64
  from where they keep it:
@@ -68,7 +70,8 @@ them, are under "Other ways to sign in…".
68
70
 
69
71
  kiro-cli refreshes its own token first (`kiro-cli debug refresh-auth-token`).
70
72
  A token the plugin refreshes itself is written back, so kiro-cli and the
71
- IDE go on with it.
73
+ IDE go on with it. An external-IdP sign-in read from either source sends
74
+ the control-plane `TokenType` that Kiro's own clients send.
72
75
  - **Kiro API key (`ksk_…`)**: sent with `tokentype: API_KEY`, as kiro-cli
73
76
  sends it. `GetProfile` is asked in us-east-1, then eu-central-1, for the
74
77
  region the key's profile is in; a key it finds none for is used in
@@ -290,10 +293,18 @@ beside the model. Claude models and Auto have thinking budget variants. Models w
290
293
 
291
294
  `auth.usage` reads `Get-Usage-Limits`: the month's credits, the Free trial
292
295
  while it is active, and each bonus (`bonuses`: a first upgrade's trial
293
- credits, a promotion's) while it is `ACTIVE` and not expired, as a window of
294
- its own with its count and when it lapses. Bonus windows are shown only.
295
- Kiro spends the plan's credits first, so a month used up while a bonus has
296
- credits left is shown only too, and the account isn't taken for spent.
296
+ credits, a promotion's) while it is `ACTIVE` or `EXHAUSTED` (used up) and
297
+ not expired, as a window of its own with its count and when it lapses.
298
+ Bonus windows are shown only. Kiro spends the plan's credits first, then
299
+ the bonus, so a month used up while a bonus has credits left goes on on
300
+ credits (`credits: true`): magpie asks that account after the provider's
301
+ accounts that still have plan credits, since the plan's credits are lost at
302
+ the month's reset and a trial's last a year. The window is also set aside
303
+ (`aside: true`), so a magpie that doesn't read `credits` doesn't take the
304
+ account for spent. Only an `ACTIVE` bonus counts as credits left (an
305
+ `EXHAUSTED` one is shown only), and when a bonus left lapses before the
306
+ month's reset the month is only set aside, so that bonus isn't asked last
307
+ and lost.
297
308
 
298
309
  With overage on (`overageConfiguration.overageStatus` `ENABLED`), the month
299
310
  is two windows, as the Kiro IDE shows it: the plan's credits, shown only
package/index.mjs CHANGED
@@ -213,6 +213,7 @@ async function readIDE() {
213
213
  return null
214
214
  }
215
215
  if (!t?.accessToken) return null
216
+ const authMethod = String(t.authMethod ?? t.provider ?? "").toLowerCase().replace(/[^a-z]/g, "")
216
217
  const c = { access: t.accessToken, refresh: t.refreshToken ?? "", expires: time(t.expiresAt), region: t.region || "us-east-1",
217
218
  profile: arnOf(t.profileArn), method: "idc", idePath: path, builderID: isBuilderID(t.provider) }
218
219
  if (t.clientIdHash) {
@@ -221,7 +222,9 @@ async function readIDE() {
221
222
  ;[c.clientId, c.clientSecret] = [reg.clientId ?? "", reg.clientSecret ?? ""]
222
223
  } catch {}
223
224
  }
224
- if (String(t.authMethod).toLowerCase() === "social" || !c.clientId) c.method = "social"
225
+ if (authMethod === "externalidp") c.method = "external-idp"
226
+ else if (authMethod === "machinetoken") c.method = "machine-token"
227
+ else if (authMethod === "social" || !c.clientId) c.method = "social"
225
228
  return c
226
229
  }
227
230
 
@@ -347,7 +350,10 @@ function authHeaders(a) {
347
350
  if (a.tokenType) h.tokentype = a.tokenType
348
351
  return h
349
352
  }
350
- const tokenType = (c) => (c.method === "apikey" ? "API_KEY" : c.method === "external-idp" ? "EXTERNAL_IDP" : "")
353
+ const tokenType = (c) => c.method === "apikey" ? "API_KEY"
354
+ : c.method === "external-idp" ? "EXTERNAL_IDP"
355
+ : c.method === "machine-token" ? "KIRO_MACHINE_TOKEN"
356
+ : ""
351
357
 
352
358
  // region is where the account's API is: the region its profile is in, else
353
359
  // the nearer of Kiro's two to where it signed in. It goes into the API's host
@@ -542,11 +548,16 @@ function goG(x) {
542
548
  // round.
543
549
  //
544
550
  // Bonus credits (each allowance's bonuses: a first upgrade's trial credits,
545
- // a promotion's) are a window each while ACTIVE and not expired, after the
546
- // month's: their count, and when they lapse (runsOutAt), as they don't
547
- // reset. They are shown only (aside). Kiro spends the plan's credits
548
- // first, then these, so a month used up while a bonus has credits left
549
- // is set aside too, and the account isn't taken for spent.
551
+ // a promotion's) are a window each while ACTIVE or EXHAUSTED (used up) and
552
+ // not expired, after the month's: their count, and when they lapse
553
+ // (runsOutAt), as they don't reset. They are shown only (aside). Kiro
554
+ // spends the plan's credits first, then these, so a month used up while a
555
+ // bonus has credits left goes on on them (credits): magpie asks the
556
+ // account after those with plan credits left, as the plan's end at the
557
+ // month's reset and a trial's run a year. It is set aside too, for a
558
+ // magpie that doesn't know credits, so the account isn't taken for spent.
559
+ // Only an ACTIVE bonus counts as credits left, and a bonus left that
560
+ // lapses before the month's reset keeps the month set aside only.
550
561
  //
551
562
  // With overage on, Kiro goes on past the plan's credits, billed as post-paid
552
563
  // credits, until those reach the overage cap. The cap counts the overage
@@ -559,6 +570,8 @@ function goG(x) {
559
570
  // still sends means nothing, and the plan's credits are the month.
560
571
  const count = (used, limit) => used.toFixed(2).replace(/0+$/, "").replace(/\.$/, "") + " / " + goG(limit)
561
572
  const num = (x) => (typeof x === "number" && Number.isFinite(x) ? x : 0)
573
+ const BONUS_SHOWN = new Set(["ACTIVE", "EXHAUSTED"])
574
+ const ON_BONUS = { aside: true, credits: true }
562
575
  const overageOn = (l) =>
563
576
  String(l?.overageConfiguration?.overageStatus ?? "").toUpperCase() === "ENABLED" || l?.overageConfiguration?.overageEnabled === true
564
577
 
@@ -577,20 +590,28 @@ function readUsage(l, now = Date.now()) {
577
590
  windows.push({ name: "Free trial", used: (100 * used) / limit, resetsAt: at(num(t.freeTrialExpiry)), display: count(used, limit),
578
591
  amount: used, limit, unit: "credits" })
579
592
  }
580
- const bonuses = []
593
+ const bonuses = [], spendable = []
581
594
  for (const b of Array.isArray(u?.bonuses) ? u.bonuses : []) {
582
595
  const used = num(b?.currentUsage), limit = num(b?.usageLimit), expires = num(b?.expiresAt)
583
- if (String(b?.status ?? "").toUpperCase() !== "ACTIVE" || limit <= 0 || (expires > 0 && expires * 1000 <= now)) continue
596
+ if (!BONUS_SHOWN.has(String(b?.status ?? "").toUpperCase()) || limit <= 0 || (expires > 0 && expires * 1000 <= now)) continue
584
597
  const left = Math.max(0, limit - used)
585
598
  bonuses.push({ name: b?.displayName || "Bonus credits", used: (100 * used) / limit, display: count(used, limit),
586
599
  amount: used, limit, unit: "credits", ...(expires > 0 && left > 0 ? { runsOutAt: at(expires), runsOut: left } : {}), aside: true })
600
+ // only an ACTIVE bonus is spent: one Kiro marks EXHAUSTED has nothing
601
+ // to go on with, whatever its count says
602
+ if (left > 0 && String(b?.status ?? "").toUpperCase() === "ACTIVE") spendable.push(expires)
587
603
  }
588
604
  const used = num(u?.currentUsageWithPrecision), limit = num(u?.usageLimitWithPrecision)
589
605
  const cap = overage ? num(u?.overageCapWithPrecision) || num(u?.overageCap) : 0
590
606
  const reset = at(num(u?.nextDateReset) || num(l?.nextDateReset))
591
607
  const month = { resetsAt: reset, unit: "credits", span: 30 * 24 * 3600 }
592
608
  const name = u?.displayNamePlural || u?.displayName || "Credits"
593
- const left = bonuses.some((b) => b.amount < b.limit)
609
+ const left = spendable.length > 0
610
+ // credits only when what is left outlasts the month: a bonus that
611
+ // lapses before the reset would be lost if asked last, so then the
612
+ // month is only set aside and the account keeps its listed place
613
+ const resetMs = Date.parse(reset ?? "")
614
+ const onBonus = spendable.some((e) => e > 0 && resetMs > 0 && e * 1000 < resetMs) ? { aside: true } : ON_BONUS
594
615
  if (cap > 0) {
595
616
  // what Kiro says of the overage, else what is past the plan
596
617
  const said = typeof u?.currentOveragesWithPrecision === "number" ? u.currentOveragesWithPrecision : u?.currentOverages
@@ -598,12 +619,12 @@ function readUsage(l, now = Date.now()) {
598
619
  const inPlan = Math.min(used, limit)
599
620
  plan = limit > 0 ? { name, used: (100 * inPlan) / limit, display: count(inPlan, limit), amount: inPlan, limit, ...month, aside: true } : undefined
600
621
  stop = { name: "Overage", used: (100 * over) / cap, display: count(over, cap), amount: over, limit: cap, ...month,
601
- ...(over >= cap && left ? { aside: true } : {}) }
622
+ ...(over >= cap && left ? onBonus : {}) }
602
623
  windows.push(...(plan ? [plan] : []), stop)
603
624
  plan ??= stop
604
625
  } else if (limit > 0) {
605
626
  plan = stop = { name, used: (100 * used) / limit, display: count(used, limit), amount: used, limit, ...month,
606
- ...(used >= limit && left ? { aside: true } : {}) }
627
+ ...(used >= limit && left ? onBonus : {}) }
607
628
  windows.push(plan)
608
629
  }
609
630
  windows.push(...bonuses)
@@ -655,9 +676,11 @@ function regionsUsage(reads, now = Date.now(), only = "") {
655
676
  allOver &&= over
656
677
  }
657
678
  monthName ??= plan.name
679
+ // a region's own windows are shown only: the sum below is weighed
680
+ const own = ({ credits, ...w }, n) => ({ ...w, name: `${n} (${r.region})`, aside: true })
658
681
  if (!counted) {
659
- shown.push({ ...plan, name: `${plan.name} (${r.region})`, aside: true })
660
- if (over) shown.push({ ...stop, name: `${stop.name} (${r.region})`, aside: true })
682
+ shown.push(own(plan, plan.name))
683
+ if (over) shown.push(own(stop, stop.name))
661
684
  regions.push({ region: r.region, full: stop.amount >= stop.limit, resetsAt: Date.parse(stop.resetsAt ?? "") > 0 ? Date.parse(stop.resetsAt) : 0 })
662
685
  continue
663
686
  }
@@ -667,8 +690,8 @@ function regionsUsage(reads, now = Date.now(), only = "") {
667
690
  ofPlan += plan.limit
668
691
  const at = Date.parse(stop.resetsAt ?? "")
669
692
  if (at > 0 && (!resets || at < resets)) resets = at
670
- shown.push({ ...plan, name: `${plan.name} (${r.region})`, aside: true })
671
- if (over) shown.push({ ...stop, name: `${stop.name} (${r.region})`, aside: true })
693
+ shown.push(own(plan, plan.name))
694
+ if (over) shown.push(own(stop, stop.name))
672
695
  regions.push({ region: r.region, full: stop.amount >= stop.limit, resetsAt: at > 0 ? at : 0 })
673
696
  }
674
697
  const windows = []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-kiro-auth",
3
- "version": "0.1.40",
3
+ "version": "0.1.42",
4
4
  "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",