mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(admin-ui): keep sign and precision in CSV, YAML and context labels
CSV export prefixed negative numbers with an apostrophe, which corrupted the data. YAML emitted NaN and Infinity as plain strings. Context windows such as 1500 read as 2k. Numbers now skip the CSV formula guard, non-finite values use YAML float forms, and counts below 10k keep one decimal.
This commit is contained in:
@@ -84,3 +84,13 @@ describe('toCsv: formula injection (P4)', () => {
|
||||
expect(toCsv(['note'], [['cost=5']])).toBe('note\r\ncost=5\r\n')
|
||||
})
|
||||
})
|
||||
|
||||
describe('toCsv numbers', () => {
|
||||
it.each([
|
||||
[-5, '-5'],
|
||||
[-0.25, '-0.25'],
|
||||
[12, '12'],
|
||||
])('writes the number %s unguarded', (n, want) => {
|
||||
expect(toCsv(['v'], [[n]])).toBe(`v\r\n${want}\r\n`)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -41,7 +41,7 @@ const CSV_FORMULA_LEAD_CHARS = /^[=+\-@\t\r]/
|
||||
*/
|
||||
function csvField(value: unknown): string {
|
||||
let s = value === null || value === undefined ? '' : String(value)
|
||||
if (CSV_FORMULA_LEAD_CHARS.test(s)) s = `'${s}`
|
||||
if (typeof value !== 'number' && CSV_FORMULA_LEAD_CHARS.test(s)) s = `'${s}`
|
||||
if (!CSV_SPECIAL_CHARS.test(s)) return s
|
||||
return `"${s.replace(/"/g, '""')}"`
|
||||
}
|
||||
|
||||
@@ -72,6 +72,9 @@ describe('formatContextWindow', () => {
|
||||
// when they happen to be exact multiples of 1024 (128000 = 125 ×
|
||||
// 1024) — decimal-K/M.
|
||||
{ tokens: 128_000, want: '128k' }, // review regression: was "125k"
|
||||
{ tokens: 1_500, want: '1.5k' },
|
||||
{ tokens: 9_950, want: '10k' },
|
||||
{ tokens: 1_500_000, want: '1.5M' },
|
||||
{ tokens: 1_000_000, want: '1M' }, // review regression: was "977k"
|
||||
{ tokens: 2_000_000, want: '2M' }, // review regression: was "1953k"
|
||||
// Plain digits under 1024, either path.
|
||||
|
||||
@@ -194,8 +194,13 @@ export function formatContextWindow(tokens: number): string {
|
||||
}
|
||||
|
||||
// 999_500+ would round to "1000k"; the M branch renders it as "1M".
|
||||
if (tokens < 999_500) return `${Math.round(tokens / 1_000)}k`
|
||||
return `${Math.round(tokens / 1_000_000)}M`
|
||||
if (tokens < 999_500) return `${roundScaled(tokens / 1_000)}k`
|
||||
return `${roundScaled(tokens / 1_000_000)}M`
|
||||
}
|
||||
|
||||
/** roundScaled keeps one decimal below 10 ("1.5") so 1500 does not read as 2k, and rounds to whole units from 10 up. */
|
||||
function roundScaled(value: number): string {
|
||||
return value < 10 ? String(Math.round(value * 10) / 10) : String(Math.round(value))
|
||||
}
|
||||
|
||||
/** formatUsdPerMTok renders a USD-per-million-tokens float as "$0.19" (2 decimals) — the shared building block for both the visible cost chip and ModelChip's hover detail. */
|
||||
|
||||
@@ -285,3 +285,14 @@ describe('toYamlValue', () => {
|
||||
expect(toYamlValue(weird)).toBe(String(weird))
|
||||
})
|
||||
})
|
||||
|
||||
describe('emitYamlScalar non-finite numbers', () => {
|
||||
it.each([
|
||||
[Number.NaN, '.nan'],
|
||||
[Infinity, '.inf'],
|
||||
[-Infinity, '-.inf'],
|
||||
[-3.5, '-3.5'],
|
||||
])('%s -> %s', (n, want) => {
|
||||
expect(emitYamlScalar(n)).toBe(want)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -148,10 +148,19 @@ export function emitYamlKey(key: string): string {
|
||||
return yamlKeyNeedsQuoting(key) ? escapeYamlUnsafeControls(JSON.stringify(key)) : key
|
||||
}
|
||||
|
||||
/** yamlNumber spells non-finite values with their YAML 1.2 float forms; String(NaN) is a plain string in YAML. */
|
||||
function yamlNumber(n: number): string {
|
||||
if (Number.isNaN(n)) return '.nan'
|
||||
if (n === Infinity) return '.inf'
|
||||
if (n === -Infinity) return '-.inf'
|
||||
return String(n)
|
||||
}
|
||||
|
||||
/** emitYamlScalar renders one leaf value in "key: <here>" position OR a flow list/map element — quoted (JSON-style) whenever needsQuoting says the plain form is unsafe, plain otherwise. Booleans/numbers/null render via their own JS literal, never through the string quoting path (a real `true`/`false`/`null`/number is safe unquoted by construction — only a STRING value can accidentally spell one of those). */
|
||||
export function emitYamlScalar(value: YamlPrimitive): string {
|
||||
if (value === null) return 'null'
|
||||
if (typeof value === 'boolean' || typeof value === 'number') return String(value)
|
||||
if (typeof value === 'number') return yamlNumber(value)
|
||||
if (typeof value === 'boolean') return String(value)
|
||||
return needsQuoting(value) ? escapeYamlUnsafeControls(JSON.stringify(value)) : value
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user