mirror of
https://gitee.com/zhangsken/opencode.git
synced 2026-10-16 10:30:01 +08:00
fix(console): merge duplicate Go usage rows (#45503)
Co-authored-by: MrMushrooooom <19261047+MrMushrooooom@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
c2eacd72af
commit
6568a82455
@@ -18,7 +18,20 @@ export type LiteUsageBreakdownItem = {
|
||||
}
|
||||
|
||||
export function buildLiteUsageBreakdown(input: { usage: number; limit: number; sources: LiteUsageBreakdownSource[] }) {
|
||||
const rows: LiteUsageBreakdownItem[] = input.sources
|
||||
// Legacy usage can resolve to the same rate as a separately grouped recorded multiplier.
|
||||
const groups = new Map<string, LiteUsageBreakdownSource>()
|
||||
input.sources.forEach((item) => {
|
||||
const key = JSON.stringify([item.model, item.multiplier])
|
||||
const row = groups.get(key)
|
||||
if (!row) {
|
||||
groups.set(key, { ...item })
|
||||
return
|
||||
}
|
||||
row.cost += item.cost
|
||||
row.quotaCost += item.quotaCost
|
||||
row.estimated ||= item.estimated
|
||||
})
|
||||
const rows: LiteUsageBreakdownItem[] = Array.from(groups.values())
|
||||
.filter((item) => item.cost !== 0 || item.quotaCost !== 0)
|
||||
.sort((a, b) => b.quotaCost - a.quotaCost)
|
||||
.map((item) => ({
|
||||
|
||||
@@ -72,4 +72,62 @@ describe("Go usage breakdown", () => {
|
||||
expect(result.rows.map((row) => row.multiplier)).toEqual([2, 1])
|
||||
expect(result.rows.map((row) => row.contributionPercent)).toEqual([40, 10])
|
||||
})
|
||||
|
||||
test.each([false, true])("merges same-rate usage (estimated first: %s)", (estimated) => {
|
||||
const sources = [
|
||||
{ model: "deepseek-v4-flash", name: "DeepSeek V4 Flash", cost: 200, quotaCost: 400, multiplier: 2, estimated },
|
||||
{ model: "other", name: "Other", cost: 500, quotaCost: 500, multiplier: 1, estimated: false },
|
||||
{
|
||||
model: "deepseek-v4-flash",
|
||||
name: "DeepSeek V4 Flash",
|
||||
cost: 100,
|
||||
quotaCost: 199,
|
||||
multiplier: 2,
|
||||
estimated: !estimated,
|
||||
},
|
||||
]
|
||||
const original = structuredClone(sources)
|
||||
const result = buildLiteUsageBreakdown({ usage: 1_050, limit: 6_000, sources })
|
||||
|
||||
expect(result.rows).toHaveLength(2)
|
||||
expect(result.rows[0]).toMatchObject({
|
||||
model: "deepseek-v4-flash",
|
||||
cost: 300,
|
||||
quotaCost: 599,
|
||||
multiplier: 2,
|
||||
estimated: true,
|
||||
})
|
||||
expect(getModelQuotaLimit(result.limit, result.rows[0].multiplier)).toBe(3_000)
|
||||
expect(result.usage).toBe(1_050)
|
||||
expect(result.usagePercent).toBe(17.5)
|
||||
expect(result.rows.reduce((total, row) => total + row.contributionPercent, 0)).toBeCloseTo(result.usagePercent)
|
||||
expect(sources).toEqual(original)
|
||||
})
|
||||
|
||||
test("keeps distinct model IDs with the same display name separate", () => {
|
||||
const result = buildLiteUsageBreakdown({
|
||||
usage: 300,
|
||||
limit: 1_000,
|
||||
sources: [
|
||||
{ model: "first", name: "Model", cost: 100, quotaCost: 100, multiplier: 1, estimated: false },
|
||||
{ model: "second", name: "Model", cost: 200, quotaCost: 200, multiplier: 1, estimated: false },
|
||||
],
|
||||
})
|
||||
|
||||
expect(result.rows.map((row) => row.model)).toEqual(["second", "first"])
|
||||
})
|
||||
|
||||
test("does not merge unknown rates with recorded rates", () => {
|
||||
const result = buildLiteUsageBreakdown({
|
||||
usage: 300,
|
||||
limit: 1_000,
|
||||
sources: [
|
||||
{ model: "glm", name: "GLM", cost: 100, quotaCost: 100, estimated: true },
|
||||
{ model: "glm", name: "GLM", cost: 200, quotaCost: 200, multiplier: 1, estimated: false },
|
||||
],
|
||||
})
|
||||
|
||||
expect(result.rows.map((row) => row.multiplier)).toEqual([1, undefined])
|
||||
expect(getModelQuotaLimit(result.limit, result.rows[1].multiplier)).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user