diff --git a/packages/kiro/README.md b/packages/kiro/README.md index 1fdb029..a1a9aaf 100644 --- a/packages/kiro/README.md +++ b/packages/kiro/README.md @@ -257,9 +257,13 @@ its own with its count and when it lapses. Bonus windows are shown only. Kiro spends the plan's credits first, so a month used up while a bonus has credits left is shown only too, and the account isn't taken for spent. -With overage on (`overageConfiguration.overageStatus` `ENABLED`), the -month's window counts against the overage cap (`overageCap`), which is the -month's whole limit; with it off, against the plan's credits. +With overage on (`overageConfiguration.overageStatus` `ENABLED`), the month +is two windows, as the Kiro IDE shows it: the plan's credits, shown only +(using them up doesn't stop the account), then the overage +(`currentOveragesWithPrecision`) against the overage cap (`overageCap`), +which counts the overage alone, not the plan's credits with it. A Power +account that used 12589.24 shows `10000 / 10000` and `2589.24 / 37500`. +With overage off, the plan's credits are the month. Each request's cost is what Kiro's `meteringEvent`s say, in credits, summed over the request (a web search's turns included). Kiro has already applied @@ -287,8 +291,9 @@ again. - With every enabled region used up, the answer is a 429 naming them, and a region not enabled, so magpie goes on to another account. - `auth.usage` reads both regions: a window for each, shown only, and their - sum, which magpie weighs the account by. From 80% of the sum, a region - not enabled is pointed out in its text. + sum, which magpie weighs the account by. With overage on, the plans' sum + comes first, shown only, and the overages' sum is the one magpie weighs. + From 80% of the sum, a region not enabled is pointed out in its text. Builder ID, Google, GitHub and API key sign-ins keep one region. diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index 33b9920..5a38262 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -494,18 +494,28 @@ function goG(x) { // first, then these, so a month used up while a bonus has credits left // is set aside too, and the account isn't taken for spent. // -// With overage on, Kiro goes on past the plan's credits up to the overage -// cap, which is the month's whole limit (a 2000-credit plan used 4104, of -// them 2104 overage, under a cap of 10000), so the month's window counts -// against the cap. With overage off the cap Kiro still sends means nothing. +// With overage on, Kiro goes on past the plan's credits, billed as post-paid +// credits, until those reach the overage cap. The cap counts the overage +// alone, not the plan's credits with it: it is the profile's "maximum +// allowed overage" quota (in USD, $0.04 a credit; $400 is 10000 credits). +// So with overage on the month is two windows, as the Kiro IDE shows it: +// the plan's credits (12589.24 used of a 10000 plan is "10000 / 10000"), +// shown only, as using them up doesn't stop the account, and the overage +// (2589.24 / 37500), which does (wizcheu on X). With overage off the cap Kiro +// still sends means nothing, and the plan's credits are the month. const count = (used, limit) => used.toFixed(2).replace(/0+$/, "").replace(/\.$/, "") + " / " + goG(limit) +const num = (x) => (typeof x === "number" && Number.isFinite(x) ? x : 0) const overageOn = (l) => String(l?.overageConfiguration?.overageStatus ?? "").toUpperCase() === "ENABLED" || l?.overageConfiguration?.overageEnabled === true -function usageOf(l, now = Date.now()) { + +// readUsage is usageOf with the month's windows named: plan, the plan's +// credits, and stop, the window that stops the account (the overage's +// with overage on, else plan itself). +function readUsage(l, now = Date.now()) { const at = (secs) => (secs > 0 ? new Date(Math.trunc(secs) * 1000).toISOString() : undefined) - const num = (x) => (typeof x === "number" && Number.isFinite(x) ? x : 0) const overage = overageOn(l) const windows = [] + let plan, stop for (const u of l?.usageBreakdownList ?? []) { const t = u?.freeTrialInfo if (t && String(t.freeTrialStatus ?? "").toUpperCase() === "ACTIVE" && num(t.usageLimitWithPrecision) > 0) { @@ -521,36 +531,53 @@ function usageOf(l, now = Date.now()) { bonuses.push({ name: b?.displayName || "Bonus credits", used: (100 * used) / limit, display: count(used, limit), amount: used, limit, unit: "credits", ...(expires > 0 && left > 0 ? { runsOutAt: at(expires), runsOut: left } : {}), aside: true }) } - const used = num(u?.currentUsageWithPrecision) - const limit = Math.max(num(u?.usageLimitWithPrecision), overage ? num(u?.overageCapWithPrecision) || num(u?.overageCap) : 0) - if (limit > 0) { - const reset = num(u?.nextDateReset) || num(l?.nextDateReset) - const carried = used >= limit && bonuses.some((b) => b.amount < b.limit) - windows.push({ name: u?.displayNamePlural || u?.displayName || "Credits", used: (100 * used) / limit, resetsAt: at(reset), - display: count(used, limit), amount: used, limit, unit: "credits", span: 30 * 24 * 3600, ...(carried ? { aside: true } : {}) }) + const used = num(u?.currentUsageWithPrecision), limit = num(u?.usageLimitWithPrecision) + const cap = overage ? num(u?.overageCapWithPrecision) || num(u?.overageCap) : 0 + const reset = at(num(u?.nextDateReset) || num(l?.nextDateReset)) + const month = { resetsAt: reset, unit: "credits", span: 30 * 24 * 3600 } + const name = u?.displayNamePlural || u?.displayName || "Credits" + const left = bonuses.some((b) => b.amount < b.limit) + if (cap > 0) { + // what Kiro says of the overage, else what is past the plan + const said = typeof u?.currentOveragesWithPrecision === "number" ? u.currentOveragesWithPrecision : u?.currentOverages + const over = typeof said === "number" && Number.isFinite(said) ? said : Math.max(0, used - limit) + const inPlan = Math.min(used, limit) + plan = limit > 0 ? { name, used: (100 * inPlan) / limit, display: count(inPlan, limit), amount: inPlan, limit, ...month, aside: true } : undefined + stop = { name: "Overage", used: (100 * over) / cap, display: count(over, cap), amount: over, limit: cap, ...month, + ...(over >= cap && left ? { aside: true } : {}) } + windows.push(...(plan ? [plan] : []), stop) + plan ??= stop + } else if (limit > 0) { + plan = stop = { name, used: (100 * used) / limit, display: count(used, limit), amount: used, limit, ...month, + ...(used >= limit && left ? { aside: true } : {}) } + windows.push(plan) } windows.push(...bonuses) } const email = l?.userInfo?.email - return { plan: planName(l?.subscriptionInfo?.subscriptionTitle), ...(typeof email === "string" && email ? { user: email } : {}), windows } + const usage = { plan: planName(l?.subscriptionInfo?.subscriptionTitle), ...(typeof email === "string" && email ? { user: email } : {}), windows } + return { usage, plan, stop } } +const usageOf = (l, now = Date.now()) => readUsage(l, now).usage + // regionsUsage is an Identity Center account's credits from each region's // read (reads: { region, l } read, { region, error } not, { region, // missing } with no profile, not enabled): each region's month shown only, // and their sum the window magpie weighs the account by, used up only when -// every region read is. A free trial or bonus credits are the first read -// region's. regions says, of each region read, whether it is used up and -// when it resets. Regions read come first, then those that couldn't be read, -// then those not enabled: a card that shows only the first few windows then -// shows the credits an account has. +// every region read is. With overage on, the plans' sum comes first, shown +// only, then the overages' sum weighs the account. A free trial or bonus +// credits are the first read region's. regions says, of each region read, +// whether it is used up and when it resets. Regions read come first, then +// those that couldn't be read, then those not enabled: a card that shows +// only the first few windows then shows the credits an account has. function regionsUsage(reads, now = Date.now()) { const shown = [] const failed = [] const off = [] const regions = [] const missing = [] - let amount = 0, limit = 0, resets = 0, first, monthName + let amount = 0, limit = 0, inPlan = 0, ofPlan = 0, resets = 0, first, monthName, anyOver = false, allOver = true for (const r of reads) { const name = `Credits (${r.region})` if (r.error) { @@ -562,23 +589,32 @@ function regionsUsage(reads, now = Date.now()) { off.push({ name, display: "not enabled", aside: true }) continue } - const u = usageOf(r.l, now) + const { usage: u, plan, stop } = readUsage(r.l, now) first ??= u - const month = u.windows.find((w) => w.span) - if (!month) continue - monthName ??= month.name - amount += month.amount - limit += month.limit - const at = Date.parse(month.resetsAt ?? "") + if (!stop) continue + const over = stop !== plan + anyOver ||= over + allOver &&= over + monthName ??= plan.name + amount += stop.amount + limit += stop.limit + inPlan += plan.amount + ofPlan += plan.limit + const at = Date.parse(stop.resetsAt ?? "") if (at > 0 && (!resets || at < resets)) resets = at - shown.push({ ...month, name: `${month.name} (${r.region})`, aside: true }) - regions.push({ region: r.region, full: month.amount >= month.limit, resetsAt: at > 0 ? at : 0 }) + shown.push({ ...plan, name: `${plan.name} (${r.region})`, aside: true }) + if (over) shown.push({ ...stop, name: `${stop.name} (${r.region})`, aside: true }) + regions.push({ region: r.region, full: stop.amount >= stop.limit, resetsAt: at > 0 ? at : 0 }) } const windows = [] + const resetsAt = resets ? { resetsAt: new Date(resets).toISOString() } : {} if (limit > 0) { + if (anyOver && ofPlan > 0) + windows.push({ name: monthName, used: (100 * inPlan) / ofPlan, ...resetsAt, display: count(inPlan, ofPlan), amount: inPlan, limit: ofPlan, + unit: "credits", span: 30 * 24 * 3600, aside: true }) let display = count(amount, limit) if (missing.length && (100 * amount) / limit >= REMIND_SHARE) display += ` ยท ${missing.join(", ")} not enabled: ask your admin to enable it` - windows.push({ name: monthName, used: (100 * amount) / limit, ...(resets ? { resetsAt: new Date(resets).toISOString() } : {}), + windows.push({ name: allOver ? "Overage" : monthName, used: (100 * amount) / limit, ...resetsAt, display, amount, limit, unit: "credits", span: 30 * 24 * 3600 }) } windows.push(...shown, ...failed, ...off, ...(first?.windows ?? []).filter((w) => !w.span)) diff --git a/packages/kiro/package.json b/packages/kiro/package.json index 9c170f0..a7be8ae 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.35", + "version": "0.1.36", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./opencode.mjs", diff --git a/packages/kiro/region.test.mjs b/packages/kiro/region.test.mjs index f128524..1036244 100644 --- a/packages/kiro/region.test.mjs +++ b/packages/kiro/region.test.mjs @@ -33,7 +33,7 @@ const auth = () => ({ type: "oauth", access: "t", refresh: "r", expires: later() region: "us-east-1", profileArn: "", clientId: "c", clientSecret: "s", accountId: "dev@corp.example (IAM Identity Center)" }) // limits is Get-Usage-Limits' answer for a month of used credits, a plan -// of 2000 with overage on up to 10000, as Kiro answers it +// of 2000 with overage on up to 10000 more, as Kiro answers it const limits = (used, { overage = true } = {}) => ({ subscriptionInfo: { subscriptionTitle: "KIRO POWER" }, userInfo: { email: "dev@corp.example" }, nextDateReset: RESET, overageConfiguration: { overageStatus: overage ? "ENABLED" : "DISABLED" }, @@ -80,12 +80,31 @@ const ask = async (h, a) => { return { status: res.status, wait: res.headers.get("Retry-After"), message: (await res.json())?.error?.message ?? "" } } -test("overage on counts the month against the overage cap", () => { - const u = _internal.usageOf(limits(4104.73)) - expect(u.windows[0]).toMatchObject({ name: "Credits", amount: 4104.73, limit: 10000, display: "4104.73 / 10000" }) - expect(u.windows[0].used).toBeCloseTo(41.0473) +// wizcheu on X: a Kiro Power account (10000 credits) that used 12589.24 +// this month, 2589.24 of them post-paid, as the Kiro IDE shows it ("10000 +// used / 10000 covered in plan", "2589.24 used"), under an overage cap of +// 37500. magpie showed 12589.24 / 37500, the cap taken for the month. +const POWER = { subscriptionInfo: { subscriptionTitle: "KIRO POWER" }, nextDateReset: RESET, overageConfiguration: { overageStatus: "ENABLED" }, + usageBreakdownList: [{ displayName: "Credit", displayNamePlural: "Credits", currentUsage: 12589, currentUsageWithPrecision: 12589.24, + usageLimit: 10000, usageLimitWithPrecision: 10000.0, overageCap: 37500, overageCapWithPrecision: 37500.0, currentOverages: 2589, + currentOveragesWithPrecision: 2589.24, overageRate: 0.04, overageCharges: 103.57, nextDateReset: RESET }] } + +test("overage on: the plan's credits, shown only, then the overage against its cap", () => { + const u = _internal.usageOf(POWER) + const month = { resetsAt: new Date(RESET * 1000).toISOString(), unit: "credits", span: 30 * 24 * 3600 } + expect(u.windows).toEqual([ + { name: "Credits", used: 100, display: "10000 / 10000", amount: 10000, limit: 10000, ...month, aside: true }, + { name: "Overage", used: (100 * 2589.24) / 37500, display: "2589.24 / 37500", amount: 2589.24, limit: 37500, ...month }, + ]) + // Kiro's integer count when it sends no precise one, and what is past the plan when it sends neither + const breakdown = (over) => ({ ...POWER, usageBreakdownList: [{ ...POWER.usageBreakdownList[0], ...over }] }) + expect(_internal.usageOf(breakdown({ currentOveragesWithPrecision: undefined })).windows[1]).toMatchObject({ amount: 2589 }) + expect(_internal.usageOf(breakdown({ currentOveragesWithPrecision: undefined, currentOverages: undefined })).windows[1].amount).toBeCloseTo(2589.24) + // within the plan, the overage is none yet + expect(_internal.usageOf(limits(1500)).windows.map((w) => [w.name, w.display, w.aside])).toEqual([["Credits", "1500 / 2000", true], ["Overage", "0 / 10000", undefined]]) // and off, the plan's credits alone - expect(_internal.usageOf(limits(1500, { overage: false })).windows[0]).toMatchObject({ limit: 2000, used: 75 }) + expect(_internal.usageOf(limits(1500, { overage: false })).windows).toEqual([expect.objectContaining({ limit: 2000, used: 75 })]) + expect(_internal.usageOf(limits(1500, { overage: false })).windows[0].aside).toBeUndefined() }) test("both regions' profiles are found and kept with the account", async () => { @@ -107,21 +126,38 @@ test("each region's credits are shown, and their sum is the account's", async () const u = await (await hooks()).auth.usage(async () => auth()) expect(seen.usage).toEqual(expect.arrayContaining([["us-east-1", US], ["eu-central-1", EU]])) expect(u.user).toBe("dev@corp.example (IAM Identity Center)") - const [sum, us, eu] = u.windows - expect(sum).toMatchObject({ name: "Credits", amount: 12500, limit: 20000, used: 62.5, resetsAt: new Date(RESET * 1000).toISOString() }) + const [plans, sum, ...each] = u.windows + expect(plans).toMatchObject({ name: "Credits", amount: 4000, limit: 4000, used: 100, aside: true }) + expect(sum).toMatchObject({ name: "Overage", amount: 8500, limit: 20000, used: 42.5, resetsAt: new Date(RESET * 1000).toISOString() }) expect(sum.aside).toBeUndefined() - expect(us).toMatchObject({ name: "Credits (us-east-1)", used: 100, aside: true }) - expect(eu).toMatchObject({ name: "Credits (eu-central-1)", used: 25, aside: true }) + expect(each.map((w) => [w.name, w.display, w.aside])).toEqual([ + ["Credits (us-east-1)", "2000 / 2000", true], ["Overage (us-east-1)", "8000 / 10000", true], + ["Credits (eu-central-1)", "2000 / 2000", true], ["Overage (eu-central-1)", "500 / 10000", true], + ]) + // with overage off, the plans' sum is the account's + kiro({ usage: { "us-east-1": limits(2000, { overage: false }), "eu-central-1": limits(500, { overage: false }) } }) + const off = await (await hooks()).auth.usage(async () => auth()) + expect(off.windows.map((w) => [w.name, w.display, w.aside])).toEqual([ + ["Credits", "2500 / 4000", undefined], ["Credits (us-east-1)", "2000 / 2000", true], ["Credits (eu-central-1)", "500 / 2000", true], + ]) }) test("a region used up per the usage read is skipped by the next request", async () => { - const seen = kiro({ usage: { "us-east-1": limits(10000), "eu-central-1": limits(100) } }) + const seen = kiro({ usage: { "us-east-1": limits(12000), "eu-central-1": limits(100) } }) const h = await hooks() await h.auth.usage(async () => auth()) await ask(h, auth()) expect(seen.chat).toEqual([["eu-central-1", EU]]) }) +test("a region past its plan with overage left is still used", async () => { + const seen = kiro({ usage: { "us-east-1": limits(10000), "eu-central-1": limits(100) } }) + const h = await hooks() + await h.auth.usage(async () => auth()) + await ask(h, auth()) + expect(seen.chat).toEqual([["us-east-1", US]]) +}) + test("a region Kiro says is used up goes on in the other, and stays there", async () => { const seen = kiro({ chat: { "us-east-1": USED_UP } }) const h = await hooks() @@ -175,11 +211,11 @@ test("a rate limit isn't taken for credits used up: no other region is asked", a }) test("a region not enabled is shown so, pointed out near the end, and named when the other is used up", async () => { - kiro({ profiles: { "us-east-1": [US] }, usage: { "us-east-1": limits(8500) } }) + kiro({ profiles: { "us-east-1": [US] }, usage: { "us-east-1": limits(10500) } }) const u = await (await hooks()).auth.usage(async () => auth()) - expect(u.windows[0]).toMatchObject({ amount: 8500, limit: 10000 }) - expect(u.windows[0].display).toContain("eu-central-1 not enabled: ask your admin to enable it") - expect(u.windows[2]).toMatchObject({ name: "Credits (eu-central-1)", display: "not enabled", aside: true }) + expect(u.windows[1]).toMatchObject({ amount: 8500, limit: 10000 }) + expect(u.windows[1].display).toContain("eu-central-1 not enabled: ask your admin to enable it") + expect(u.windows[4]).toMatchObject({ name: "Credits (eu-central-1)", display: "not enabled", aside: true }) const seen = kiro({ profiles: { "us-east-1": [US] }, chat: { "us-east-1": USED_UP } }) const r = await ask(await hooks(), auth()) @@ -192,8 +228,10 @@ test("the region enabled comes before the one not enabled, whichever it is", asy kiro({ profiles: { "eu-central-1": [EU] }, usage: { "eu-central-1": limits(2744.89) } }) const u = await (await hooks()).auth.usage(async () => auth()) expect(u.windows.map((w) => [w.name, w.display])).toEqual([ - ["Credits", "2744.89 / 10000"], - ["Credits (eu-central-1)", "2744.89 / 10000"], + ["Credits", "2000 / 2000"], + ["Overage", "744.89 / 10000"], + ["Credits (eu-central-1)", "2000 / 2000"], + ["Overage (eu-central-1)", "744.89 / 10000"], ["Credits (us-east-1)", "not enabled"], ]) }) @@ -217,5 +255,5 @@ test("a social sign-in keeps one region", async () => { const u = await (await hooks()).auth.usage(async () => a) expect(seen.profiles).toBe(0) expect(seen.usage).toEqual([["us-east-1", US]]) - expect(u.windows.map((w) => w.name)).toEqual(["Credits"]) + expect(u.windows.map((w) => w.name)).toEqual(["Credits", "Overage"]) })