diff --git a/packages/kiro/README.md b/packages/kiro/README.md index 5170069..1b1ad7e 100644 --- a/packages/kiro/README.md +++ b/packages/kiro/README.md @@ -253,6 +253,13 @@ 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. +Each request's cost is what Kiro's `meteringEvent`s say, in credits, summed +over the request (a web search's turns included). It goes on as +`usage.credit` in the last `message_delta`, or in the message's `usage`, for +magpie's ledger (yetone/magpie#4328). Kiro gives no token counts, so the +input is reckoned from how full the context is, and the cache is unknown +(`magpie_cache_unknown`). + ### Identity Center credit regions An IAM Identity Center account has a profile, and its own month of credits diff --git a/packages/kiro/cache-unknown.test.mjs b/packages/kiro/cache-unknown.test.mjs index 21081a4..48f52a8 100644 --- a/packages/kiro/cache-unknown.test.mjs +++ b/packages/kiro/cache-unknown.test.mjs @@ -93,3 +93,38 @@ test("a reply whose metadataEvent counts tokens tells them, cache included", asy expect(u).toEqual({ input_tokens: 120, output_tokens: 3, cache_read_input_tokens: 9000, cache_creation_input_tokens: 0 }) } }) + +// What a request cost is the one figure Kiro gives: its meteringEvent, in +// credits (yetone/magpie#4328). It goes on as usage.credit, which magpie's +// ledger reads as the plan's credits. +test("Kiro's meteringEvent goes on as the request's credit, buffered and streamed", async () => { + for (const s of [false, true]) { + const u = await finalUsage(await ask(kiroReply(), s), s) + expect(u.credit).toBe(0.09107998018242124) + } +}) + +test("several meteringEvents are summed; none, or one not in credits, says no credit", async () => { + for (const s of [false, true]) { + const two = stream( + ev("assistantResponseEvent", { content: "OK" }), + ev("meteringEvent", { unit: "credit", unitPlural: "credits", usage: 0.25 }), + ev("meteringEvent", { unit: "credit", unitPlural: "credits", usage: 0.5 }), + ) + expect((await finalUsage(await ask(two, s), s)).credit).toBe(0.75) + const none = stream(ev("assistantResponseEvent", { content: "OK" }), ev("metadataEvent", { stopReason: "END_TURN" })) + expect(await finalUsage(await ask(none, s), s)).not.toHaveProperty("credit") + const odd = stream( + ev("assistantResponseEvent", { content: "OK" }), + ev("meteringEvent", { unit: "token", usage: 900 }), + ev("meteringEvent", { unit: "credit", usage: "0.3" }), + ev("meteringEvent", { unit: "credit", usage: -1 }), + ) + expect(await finalUsage(await ask(odd, s), s)).not.toHaveProperty("credit") + } +}) + +test("a request Kiro says cost nothing says credit 0", async () => { + const free = stream(ev("assistantResponseEvent", { content: "OK" }), ev("meteringEvent", { unit: "credit", usage: 0 })) + expect((await finalUsage(await ask(free, false), false)).credit).toBe(0) +}) diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index 8f729ff..c9416cc 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -1542,7 +1542,9 @@ async function* events(body, model, budget) { // kiro-cli 2.32's own trace log shows its MetadataEvent with every token // count None, while a repeated prompt is billed about half the credits, // so Kiro does cache and doesn't say how much - const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reported: false } + // credits: what Kiro's meteringEvents say the request cost, summed; + // undefined when it sent none + const usage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reported: false, credits: undefined } let pct = 0 let said = 0 let failed = "" @@ -1601,6 +1603,8 @@ async function* events(body, model, budget) { } } else if (kind === "contextUsageEvent") { if (m.contextUsagePercentage > 0) pct = m.contextUsagePercentage + } else if (kind === "meteringEvent") { + if (Number.isFinite(m.usage) && m.usage >= 0 && (!m.unit || /^credit/i.test(m.unit))) usage.credits = (usage.credits ?? 0) + m.usage } else if (["error", "throttlingError", "validationError", "serviceUnavailableError", "internalServerException"].includes(kind)) { failed = exception(kind, f.payload) break @@ -1656,6 +1660,9 @@ const anthropicUsage = (u) => ({ : { input_tokens: u.input, output_tokens: u.output, magpie_cache_unknown: true }), // the hosted web searches made (searching) ...(u.searches ? { server_tool_use: { web_search_requests: u.searches } } : {}), + // what Kiro said the request cost in credits (its meteringEvents), as + // magpie reads a plan's credits (yetone/magpie#4328) + ...(u.credits !== undefined ? { credit: u.credits } : {}), }) // quotaWords are magpie's (internal/gateway/fallback.go): how a vendor says @@ -2155,6 +2162,7 @@ async function* searching(creds, auth, req, model, budget, signal, hosted, first usage.cacheRead += stop.usage.cacheRead usage.cacheWrite += stop.usage.cacheWrite usage.reported ||= stop.usage.reported + if (stop.usage.credits !== undefined) usage.credits = (usage.credits ?? 0) + stop.usage.credits for (const b of said) if (b.type === "tool_use") try { diff --git a/packages/kiro/package.json b/packages/kiro/package.json index 3a313b3..794ef3c 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.28", + "version": "0.1.29", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./opencode.mjs", diff --git a/packages/kiro/search.test.mjs b/packages/kiro/search.test.mjs index cae64e7..88b4b72 100644 --- a/packages/kiro/search.test.mjs +++ b/packages/kiro/search.test.mjs @@ -154,6 +154,14 @@ test("a search the model asks for is Kiro's MCP web_search, its pages given back // kiro-cli's agent adds __tool_use_purpose to its tools: the model may say // why it searches. That is kiro-cli's, not Anthropic's: the caller's // server_tool_use has the query alone, and only the query is searched for. +test("a search's turns are one request: their credits are summed", async () => { + const metered = (n) => ev("meteringEvent", { unit: "credit", unitPlural: "credits", usage: n }) + const s = kiro({ turns: [[say("Let me look. "), use("tu1", "web_search", { query: "magpie release" }), metered(0.25)], [say("Magpie 2 is out."), metered(0.5)]] }) + const m = await (await ask({ tools: [SEARCH] })).json() + expect(s.kiro.length).toBe(2) + expect(m.usage.credit).toBe(0.75) +}) + test("a call's __tool_use_purpose is neither searched for nor shown, nor sent back", async () => { const s = kiro({ turns: [[use("tu1", "web_search", { query: "bun 2", __tool_use_purpose: "check the release" })], [say("ok")]] }) const m = await (await ask({ tools: [SEARCH] })).json()