GeezerVison is showing github.com — fix(console): merge duplicate Go usage rows (#45503) · Archipel/opencode@dfa91ed
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/console/app/src/lib/lite-usage.ts +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,7 +18,20 @@ export type LiteUsageBreakdownItem = { | 18 | 18 | } | 19 | 19 |
| 20 | 20 | export function buildLiteUsageBreakdown(input: { usage: number; limit: number; sources: LiteUsageBreakdownSource[] }) { | 21 | | - const rows: LiteUsageBreakdownItem[] = input.sources | | 21 | + // Legacy usage can resolve to the same rate as a separately grouped recorded multiplier. | | 22 | + const groups = new Map<string, LiteUsageBreakdownSource>() | | 23 | + input.sources.forEach((item) => { | | 24 | + const key = JSON.stringify([item.model, item.multiplier]) | | 25 | + const row = groups.get(key) | | 26 | + if (!row) { | | 27 | + groups.set(key, { ...item }) | | 28 | + return | | 29 | + } | | 30 | + row.cost += item.cost | | 31 | + row.quotaCost += item.quotaCost | | 32 | + row.estimated ||= item.estimated | | 33 | + }) | | 34 | + const rows: LiteUsageBreakdownItem[] = Array.from(groups.values()) | 22 | 35 | .filter((item) => item.cost !== 0 || item.quotaCost !== 0) | 23 | 36 | .sort((a, b) => b.quotaCost - a.quotaCost) | 24 | 37 | .map((item) => ({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/console/app/test/liteUsage.test.ts +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,4 +72,62 @@ describe("Go usage breakdown", () => { | 72 | 72 | expect(result.rows.map((row) => row.multiplier)).toEqual([2, 1]) | 73 | 73 | expect(result.rows.map((row) => row.contributionPercent)).toEqual([40, 10]) | 74 | 74 | }) | | 75 | + | | 76 | + test.each([false, true])("merges same-rate usage (estimated first: %s)", (estimated) => { | | 77 | + const sources = [ | | 78 | + { model: "deepseek-v4-flash", name: "DeepSeek V4 Flash", cost: 200, quotaCost: 400, multiplier: 2, estimated }, | | 79 | + { model: "other", name: "Other", cost: 500, quotaCost: 500, multiplier: 1, estimated: false }, | | 80 | + { | | 81 | + model: "deepseek-v4-flash", | | 82 | + name: "DeepSeek V4 Flash", | | 83 | + cost: 100, | | 84 | + quotaCost: 199, | | 85 | + multiplier: 2, | | 86 | + estimated: !estimated, | | 87 | + }, | | 88 | + ] | | 89 | + const original = structuredClone(sources) | | 90 | + const result = buildLiteUsageBreakdown({ usage: 1_050, limit: 6_000, sources }) | | 91 | + | | 92 | + expect(result.rows).toHaveLength(2) | | 93 | + expect(result.rows[0]).toMatchObject({ | | 94 | + model: "deepseek-v4-flash", | | 95 | + cost: 300, | | 96 | + quotaCost: 599, | | 97 | + multiplier: 2, | | 98 | + estimated: true, | | 99 | + }) | | 100 | + expect(getModelQuotaLimit(result.limit, result.rows[0].multiplier)).toBe(3_000) | | 101 | + expect(result.usage).toBe(1_050) | | 102 | + expect(result.usagePercent).toBe(17.5) | | 103 | + expect(result.rows.reduce((total, row) => total + row.contributionPercent, 0)).toBeCloseTo(result.usagePercent) | | 104 | + expect(sources).toEqual(original) | | 105 | + }) | | 106 | + | | 107 | + test("keeps distinct model IDs with the same display name separate", () => { | | 108 | + const result = buildLiteUsageBreakdown({ | | 109 | + usage: 300, | | 110 | + limit: 1_000, | | 111 | + sources: [ | | 112 | + { model: "first", name: "Model", cost: 100, quotaCost: 100, multiplier: 1, estimated: false }, | | 113 | + { model: "second", name: "Model", cost: 200, quotaCost: 200, multiplier: 1, estimated: false }, | | 114 | + ], | | 115 | + }) | | 116 | + | | 117 | + expect(result.rows.map((row) => row.model)).toEqual(["second", "first"]) | | 118 | + }) | | 119 | + | | 120 | + test("does not merge unknown rates with recorded rates", () => { | | 121 | + const result = buildLiteUsageBreakdown({ | | 122 | + usage: 300, | | 123 | + limit: 1_000, | | 124 | + sources: [ | | 125 | + { model: "glm", name: "GLM", cost: 100, quotaCost: 100, estimated: true }, | | 126 | + { model: "glm", name: "GLM", cost: 200, quotaCost: 200, multiplier: 1, estimated: false }, | | 127 | + ], | | 128 | + }) | | 129 | + | | 130 | + expect(result.rows.map((row) => row.multiplier)).toEqual([1, undefined]) | | 131 | + expect(getModelQuotaLimit(result.limit, result.rows[1].multiplier)).toBeUndefined() | | 132 | + }) | 75 | 133 | }) |
|