diff --git a/packages/kiro/README.md b/packages/kiro/README.md index 1b1ad7e..131c64a 100644 --- a/packages/kiro/README.md +++ b/packages/kiro/README.md @@ -237,8 +237,10 @@ own search used instead. The `config` hook declares `auto` (Kiro picks the model). Once signed in, the `provider.models` hook replaces it with the account's own list from `List-Available-Models`, with the default first. Each model carries its -context window, output limit and whether it takes images. Claude models -and Auto have the thinking variants. +context window, output limit and whether it takes images, and its credit +multiplier as `rate` (Kiro's `rateMultiplier` when `rateUnit` is `Credit`: +1.3 for claude-sonnet-4.5, 0.4 for claude-haiku-4.5), which magpie shows +beside the model. Claude models and Auto have the thinking variants. ## Usage in magpie @@ -254,7 +256,8 @@ 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 +over the request (a web search's turns included). Kiro has already applied +the model's multiplier to it, so it isn't multiplied again. 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 diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index ad9e7db..d7fc4ab 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -782,6 +782,8 @@ function configModel(m) { ? { reasoning: true, variants: Object.fromEntries(Object.entries(BUDGETS).map(([k, b]) => [k, { thinking: { type: "enabled", budgetTokens: b } }])) } : {}), tool_call: true, + // magpie's own: the credit multiplier Kiro lists for the model + ...(m.rate ? { rate: m.rate } : {}), } } @@ -807,6 +809,8 @@ function runtimeModel(m) { interleaved: false, }, release_date: "", + // magpie's own: the credit multiplier Kiro lists for the model + ...(m.rate ? { rate: m.rate } : {}), variants: thinks(m.id) ? Object.fromEntries(Object.entries(BUDGETS).map(([k, b]) => [k, { thinking: { type: "enabled", budgetTokens: b } }])) : {}, } } @@ -831,6 +835,21 @@ async function keyModels(a) { return out } +// rateOf is the credits a model's request costs as a multiple, as Kiro's +// list says it (rateMultiplier with rateUnit "Credit": on 11-09 a Free +// account's list had auto 1, claude-sonnet-4.5 1.3, claude-haiku-4.5 0.4, +// glm-5 0.5, deepseek-3.2 and minimax-m2.5 0.25, minimax-m2.1 0.15 and +// qwen3-coder-next 0.05, which kiro-cli 2.x reads as its rate_multiplier). +// Nothing is the rate when Kiro gives none, or gives it in another unit. +// It is only shown: what a request cost is the meteringEvent's credits, +// which Kiro has already multiplied (the same prompt metered 0.00535 on +// claude-haiku-4.5 and 0.01739 on claude-sonnet-4.5, 0.4 to 1.3). +function rateOf(m) { + const n = m?.rateMultiplier + if (typeof n !== "number" || !Number.isFinite(n) || n <= 0) return undefined + return String(m.rateUnit ?? "").toLowerCase() === "credit" ? n : undefined +} + // listModels is List-Available-Models, the default first. async function listModels(a) { const out = a.tokenType === "API_KEY" ? await keyModels(a) : await managementCall(a.region, a, "List-Available-Models", { origin: "KIRO_CLI", profileArn: a.profile }) @@ -839,8 +858,9 @@ async function listModels(a) { if (!m?.modelId) continue let name = m.modelName if (!name || name === m.modelId) name = m.modelId === "auto" ? "Auto" : m.modelId + const rate = rateOf(m) const model = { id: m.modelId, name, context: m.tokenLimits?.maxInputTokens ?? 0, output: m.tokenLimits?.maxOutputTokens ?? 0, - images: (m.supportedInputTypes ?? []).some((t) => String(t).toUpperCase() === "IMAGE") } + images: (m.supportedInputTypes ?? []).some((t) => String(t).toUpperCase() === "IMAGE"), ...(rate ? { rate } : {}) } if (m.modelId === out?.defaultModel?.modelId) list.unshift(model) else list.push(model) } @@ -3133,4 +3153,4 @@ export async function KiroAuthPlugin({ client } = {}) { } // for tests -export const _internal = { generate, responsesBody, crMessages, crRefusal, features, FEATURES, featureKey, waitOf, timing, TRIES, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames } +export const _internal = { generate, responsesBody, crMessages, crRefusal, features, FEATURES, featureKey, waitOf, timing, TRIES, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, rateOf, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames } diff --git a/packages/kiro/package.json b/packages/kiro/package.json index d9ff965..b47c588 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.33", + "version": "0.1.34", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./opencode.mjs", diff --git a/packages/kiro/rate.test.mjs b/packages/kiro/rate.test.mjs new file mode 100644 index 0000000..27ac7ce --- /dev/null +++ b/packages/kiro/rate.test.mjs @@ -0,0 +1,52 @@ +import { test, expect } from "bun:test" +import { _internal } from "./index.mjs" + +// Discord (01huadalang, 11-09): "kiro 加上积分倍率消耗". Kiro charges each +// model at its own credit rate and says it in List-Available-Models +// (rateMultiplier, rateUnit). The fixture is a Free account's answer on +// 11-09, byte for byte for claude-sonnet-4.5 and the default model; the +// other models keep only the fields this reads. +const SONNET = {"additionalModelRequestFieldsSchema":null,"availableOrigins":null,"consentDescription":null,"consentStatus":null,"description":"Claude Sonnet 4.5 model","disclaimers":null,"modelId":"claude-sonnet-4.5","modelName":"claude-sonnet-4.5","modelProvider":null,"promptCaching":{"maximumCacheCheckpointsPerRequest":4,"minimumTokensPerCacheCheckpoint":1024,"supportsPromptCaching":true},"rateMultiplier":1.3,"rateUnit":"Credit","refusalFallbackModels":null,"status":null,"supportedInputTypes":["TEXT","IMAGE"],"termsUrl":null,"tokenLimits":{"maxInputTokens":200000,"maxOutputTokens":64000}} +const DEFAULT = {"additionalModelRequestFieldsSchema":null,"availableOrigins":null,"consentDescription":null,"consentStatus":null,"description":null,"disclaimers":null,"modelId":"auto","modelName":null,"modelProvider":null,"promptCaching":null,"rateMultiplier":null,"rateUnit":null,"refusalFallbackModels":null,"status":null,"supportedInputTypes":null,"termsUrl":null,"tokenLimits":null} +const rated = (modelId, rateMultiplier, rateUnit = "Credit") => ({ modelId, modelName: modelId, rateMultiplier, rateUnit, + tokenLimits: { maxInputTokens: 200000, maxOutputTokens: 64000 } }) +const MODELS = [rated("auto", 1), SONNET, rated("claude-haiku-4.5", 0.4), rated("deepseek-3.2", 0.25), rated("minimax-m2.5", 0.25), + rated("minimax-m2.1", 0.15), rated("glm-5", 0.5), rated("qwen3-coder-next", 0.05)] + +const listed = async (models) => { + const was = globalThis.fetch + globalThis.fetch = async (input) => { + if (new URL(String(input)).pathname !== "/List-Available-Models") throw new Error("no network in tests") + return Response.json({ defaultModel: DEFAULT, models, nextToken: null }) + } + try { + return await _internal.listModels({ region: "us-east-1", token: "t", tokenType: "", profile: "arn:aws:codewhisperer:us-east-1:1:profile/P" }) + } finally { + globalThis.fetch = was + } +} + +test("each model carries the credit multiplier Kiro lists for it", async () => { + const list = await listed(MODELS) + expect(Object.fromEntries(list.map((m) => [m.id, m.rate]))).toEqual({ + auto: 1, "claude-sonnet-4.5": 1.3, "claude-haiku-4.5": 0.4, "deepseek-3.2": 0.25, "minimax-m2.5": 0.25, + "minimax-m2.1": 0.15, "glm-5": 0.5, "qwen3-coder-next": 0.05, + }) + // magpie reads it as m.rate on the account's list and on the config's + const sonnet = list.find((m) => m.id === "claude-sonnet-4.5") + expect(_internal.runtimeModel(sonnet).rate).toBe(1.3) + expect(_internal.configModel(sonnet).rate).toBe(1.3) +}) + +test("no rate is shown where Kiro gives none, or gives it in another unit", async () => { + const list = await listed([{ ...SONNET, rateMultiplier: null, rateUnit: null }, rated("glm-5", 0.5, "Request"), rated("deepseek-3.2", "0.25"), + rated("minimax-m2.1", 0), rated("qwen3-coder-next", -1), rated("claude-haiku-4.5", 0.4, "CREDIT")]) + for (const m of list) { + if (m.id === "claude-haiku-4.5") continue + expect(m.rate).toBeUndefined() + expect("rate" in _internal.runtimeModel(m)).toBe(false) + expect("rate" in _internal.configModel(m)).toBe(false) + } + // the unit's case is Kiro's to choose + expect(list.find((m) => m.id === "claude-haiku-4.5").rate).toBe(0.4) +})