π OpenHands: AI-Driven Development
fix(frontend): promote compact token counts to the next unit when rounding reaches it (#17469)
Shinji8784a5946a9a
diff --git a/.pr/token-count-rounding-repro.png b/.pr/token-count-rounding-repro.pngnew file mode 100644index 0000000000..dc9a94f8eaBinary files /dev/null and b/.pr/token-count-rounding-repro.png differdiff --git a/__tests__/utils/format-token-count.test.ts b/__tests__/utils/format-token-count.test.tsindex 90a353c2d4..32ab60ab66 100644--- a/__tests__/utils/format-token-count.test.ts+++ b/__tests__/utils/format-token-count.test.ts@@ -9,11 +9,16 @@ describe("formatCompactTokenCount", () => {[500, "500"],[1_985, "2.0k"],[198_500, "198.5k"],+ [999_949, "999.9k"],[1_000_000, "1.0M"],[1_050_000, "1.1M"],])("formats %i as %s", (value, expected) => {expect(formatCompactTokenCount(value)).toBe(expected);});++ it("promotes to the next unit when rounding reaches it", () => {+ expect(formatCompactTokenCount(999_999)).toBe("1.0M");+ });});describe("getContextWindowUsagePercentage", () => {diff --git a/src/utils/format-token-count.ts b/src/utils/format-token-count.tsindex 0240b2e40a..40d23dc51e 100644--- a/src/utils/format-token-count.ts+++ b/src/utils/format-token-count.ts@@ -13,7 +13,13 @@ export function formatCompactTokenCount(value: number): string {if (Number.isInteger(thousands)) {return `${thousands.toFixed(0)}k`;}- return `${thousands.toFixed(1)}k`;+ const rounded = thousands.toFixed(1);+ // Rounding can carry the value into the next unit (999_999 -> "1000.0k"),+ // which reads as a four-digit count. Promote it to M instead.+ if (Number(rounded) >= 1_000) {+ return `${(Number(rounded) / 1_000).toFixed(1)}M`;+ }+ return `${rounded}k`;}return value.toLocaleString();