mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(admin): mirror backend detail parsing in the mock API
The mock now parses the detail query like the backend. Only 1 and true turn detail on. Other unknown values return 400. detail=0 and detail=false no longer return the prompt-cache fields.
This commit is contained in:
+13
-4
@@ -47,9 +47,17 @@ function validQuery(path: string, query: URLSearchParams): string | null {
|
||||
|
||||
const PROMPT_CACHE_FIELDS = ['promptCacheReadTokens', 'promptCacheWriteTokens', 'promptCacheSavedMicroUsd']
|
||||
|
||||
/** The live API returns the prompt-cache fields only with detail=1. */
|
||||
function stripDetailOnly(path: string, query: URLSearchParams, body: Obj): Obj {
|
||||
if (path !== MODELS || query.get('detail') || !Array.isArray(body.models)) return body
|
||||
/** Mirrors admin.go parseUsageModelsDetail: ''/'0'/'false' are off, '1'/'true' on (case-sensitive), anything else is null (400). */
|
||||
export function isDetail(query: URLSearchParams): boolean | null {
|
||||
const raw = query.get('detail') ?? ''
|
||||
if (raw === '' || raw === '0' || raw === 'false') return false
|
||||
if (raw === '1' || raw === 'true') return true
|
||||
return null
|
||||
}
|
||||
|
||||
/** The live API returns the prompt-cache fields only with detail on. */
|
||||
export function stripDetailOnly(path: string, query: URLSearchParams, body: Obj): Obj {
|
||||
if (path !== MODELS || isDetail(query) || !Array.isArray(body.models)) return body
|
||||
const models = body.models.map((m: Obj) => Object.fromEntries(Object.entries(m).filter(([k]) => !PROMPT_CACHE_FIELDS.includes(k))))
|
||||
return { ...body, models }
|
||||
}
|
||||
@@ -70,12 +78,13 @@ function resolveBase(i: RespondInput): { status: number; body: unknown } {
|
||||
const invalid = seriesQueryError(query)
|
||||
if (invalid) return { status: 400, body: errorEnvelope(400, 'invalid_request_error', invalid) }
|
||||
}
|
||||
if (path === MODELS && isDetail(query) === null) return { status: 400, body: errorEnvelope(400, 'invalid_request_error', 'invalid detail') }
|
||||
const bad = validQuery(path, query)
|
||||
if (bad) return notFound(bad)
|
||||
|
||||
const exact = matchFixture(manifest, path, query)
|
||||
if (exact && exact.status !== 200) return { status: exact.status, body: i.readFixture(exact.file) }
|
||||
const detail = query.get('detail')
|
||||
const detail = isDetail(query)
|
||||
const window = query.get('window') ?? ''
|
||||
if (path === MODELS && detail && Number(query.get('span')) >= (DETAIL_MAX_SPAN[window] ?? Infinity)) {
|
||||
return { status: 400, body: errorEnvelope(400, 'invalid_request_error', RANGE_TOO_LARGE) }
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
|
||||
|
||||
import manifestJson from '../../mock/manifest.json'
|
||||
import { matchFixture, type Fixture } from '../../mock/match'
|
||||
import { respond } from '../../mock/respond'
|
||||
import { isDetail, respond, stripDetailOnly } from '../../mock/respond'
|
||||
import { applyScenarios, hotUserIds, SCENARIOS, scenarioDelayMs, parseScenarios } from '../../mock/scenarios'
|
||||
import { bucketsFor, CAPTURE_AT, shiftTimestamps } from '../../mock/time-shift'
|
||||
|
||||
@@ -388,3 +388,46 @@ describe('huge scenario consistency', () => {
|
||||
for (const s of [...prov.series, ...model.series]) expect(s.points).toHaveLength(7)
|
||||
})
|
||||
})
|
||||
|
||||
describe('usage/models detail parsing', () => {
|
||||
const MODELS = '/admin/api/usage/models'
|
||||
const base = 'metric=req&window=day&span=7&limit=100'
|
||||
const hasField = (query: string): boolean => (get(MODELS, query).body as Any).models.some((m: Any) => 'promptCacheReadTokens' in m)
|
||||
|
||||
it('mirrors parseUsageModelsDetail', () => {
|
||||
for (const v of ['', '0', 'false']) expect(isDetail(q(v === '' ? 'detail=' : `detail=${v}`))).toBe(false)
|
||||
for (const v of ['1', 'true']) expect(isDetail(q(`detail=${v}`))).toBe(true)
|
||||
for (const v of ['TRUE', 'yes', '2']) expect(isDetail(q(`detail=${v}`))).toBeNull()
|
||||
expect(isDetail(q(''))).toBe(false)
|
||||
})
|
||||
|
||||
it('returns the prompt-cache fields only for detail=1 and detail=true', () => {
|
||||
expect(hasField(`${base}&detail=1`)).toBe(true)
|
||||
expect(hasField(`${base}&detail=true`)).toBe(true)
|
||||
expect(hasField(`${base}&detail=0`)).toBe(false)
|
||||
expect(hasField(`${base}&detail=false`)).toBe(false)
|
||||
expect(hasField(base)).toBe(false)
|
||||
})
|
||||
|
||||
it('rejects an unknown detail value with 400', () => {
|
||||
expect(get(MODELS, `${base}&detail=yes`).status).toBe(400)
|
||||
})
|
||||
|
||||
it('does not apply the range limit to detail=0', () => {
|
||||
expect(get(MODELS, 'metric=req&window=day&span=30&limit=100&detail=0').status).toBe(200)
|
||||
expect(get(MODELS, 'metric=req&window=day&span=30&limit=100&detail=1').status).toBe(400)
|
||||
})
|
||||
})
|
||||
|
||||
describe('stripDetailOnly', () => {
|
||||
const body = { models: [{ id: 'a', tokensIn: 5, promptCacheReadTokens: 1, promptCacheWriteTokens: 2, promptCacheSavedMicroUsd: 3 }] }
|
||||
|
||||
it('removes only the prompt-cache fields when detail is off', () => {
|
||||
expect((stripDetailOnly('/admin/api/usage/models', q('detail=0'), body) as Any).models).toEqual([{ id: 'a', tokensIn: 5 }])
|
||||
})
|
||||
|
||||
it('keeps the body when detail is on or the path is another route', () => {
|
||||
expect(stripDetailOnly('/admin/api/usage/models', q('detail=1'), body)).toBe(body)
|
||||
expect(stripDetailOnly('/admin/api/usage/totals', q(''), body)).toBe(body)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user