(value: number)
| 1 | /** Compact token counts for context-window UI (e.g. 198.5k, 1.0M). */ |
| 2 | export function formatCompactTokenCount(value: number): string { |
| 3 | if (value >= 1_000_000) { |
| 4 | const millions = value / 1_000_000; |
| 5 | if (millions >= 10 && Number.isInteger(millions)) { |
| 6 | return `${millions.toFixed(0)}M`; |
| 7 | } |
| 8 | return `${millions.toFixed(1)}M`; |
| 9 | } |
| 10 | |
| 11 | if (value >= 1_000) { |
| 12 | const thousands = value / 1_000; |
| 13 | if (Number.isInteger(thousands)) { |
| 14 | return `${thousands.toFixed(0)}k`; |
| 15 | } |
| 16 | const rounded = thousands.toFixed(1); |
| 17 | // Rounding can carry the value into the next unit (999_999 -> "1000.0k"), |
| 18 | // which reads as a four-digit count. Promote it to M instead. |
| 19 | if (Number(rounded) >= 1_000) { |
| 20 | return `${(Number(rounded) / 1_000).toFixed(1)}M`; |
| 21 | } |
| 22 | return `${rounded}k`; |
| 23 | } |
| 24 | |
| 25 | return value.toLocaleString(); |
| 26 | } |
| 27 | |
| 28 | export function getContextWindowUsagePercentage( |
| 29 | perTurnToken: number, |
no outgoing calls
no test coverage detected