fix frontend openai responses alias display

This commit is contained in:
fawney19
2026-04-26 22:09:16 +08:00
parent 0ec4b4c8a4
commit 5cd2244bc2
7 changed files with 115 additions and 21 deletions

View File

@@ -0,0 +1,55 @@
import { describe, expect, it } from 'vitest'
import {
API_FORMATS,
formatApiFormat,
formatApiFormatShort,
groupApiFormats,
normalizeApiFormatAlias,
sortApiFormats,
} from '@/api/endpoints/types'
const openaiAlias = (kind: string) => ['openai', kind].join(':')
const legacyEnumAlias = (kind: string) => ['OPENAI', kind].join('_')
describe('api format display helpers', () => {
it('maps historical OpenAI response aliases to current display names', () => {
expect(normalizeApiFormatAlias(openaiAlias('cli'))).toBe(API_FORMATS.OPENAI_RESPONSES)
expect(formatApiFormat(openaiAlias('cli'))).toBe('OpenAI Responses')
expect(formatApiFormatShort(openaiAlias('cli'))).toBe('OR')
expect(normalizeApiFormatAlias(openaiAlias('compact'))).toBe(API_FORMATS.OPENAI_RESPONSES_COMPACT)
expect(formatApiFormat(openaiAlias('compact'))).toBe('OpenAI Responses Compact')
expect(formatApiFormatShort(openaiAlias('compact'))).toBe('ORC')
})
it('maps historical uppercase enum aliases to current display names', () => {
expect(normalizeApiFormatAlias(legacyEnumAlias('CLI'))).toBe(API_FORMATS.OPENAI_RESPONSES)
expect(formatApiFormat(legacyEnumAlias('CLI'))).toBe('OpenAI Responses')
expect(formatApiFormatShort(legacyEnumAlias('CLI'))).toBe('OR')
expect(normalizeApiFormatAlias(legacyEnumAlias('COMPACT'))).toBe(API_FORMATS.OPENAI_RESPONSES_COMPACT)
expect(formatApiFormat(legacyEnumAlias('COMPACT'))).toBe('OpenAI Responses Compact')
expect(formatApiFormatShort(legacyEnumAlias('COMPACT'))).toBe('ORC')
})
it('sorts historical aliases in the same slot as their current formats', () => {
expect(sortApiFormats([
openaiAlias('compact'),
API_FORMATS.OPENAI,
openaiAlias('cli'),
])).toEqual([
API_FORMATS.OPENAI,
openaiAlias('cli'),
openaiAlias('compact'),
])
})
it('groups uppercase historical aliases under OpenAI', () => {
expect(groupApiFormats([legacyEnumAlias('CLI')])).toEqual([{
family: 'openai',
label: 'OpenAI',
formats: [legacyEnumAlias('CLI')],
}])
})
})

View File

@@ -111,6 +111,27 @@ export function parseApiFormat(format: string): { family: string; kind: string }
return { family: format.slice(0, idx).toLowerCase(), kind: format.slice(idx + 1).toLowerCase() }
}
export function normalizeApiFormatAlias(format: string | null | undefined): string {
const raw = format?.trim() ?? ''
switch (raw.toLowerCase()) {
case 'openai:cli':
return API_FORMATS.OPENAI_RESPONSES
case 'openai:compact':
return API_FORMATS.OPENAI_RESPONSES_COMPACT
default:
break
}
switch (raw.toUpperCase()) {
case 'OPENAI_CLI':
return API_FORMATS.OPENAI_RESPONSES
case 'OPENAI_COMPACT':
return API_FORMATS.OPENAI_RESPONSES_COMPACT
default:
return raw
}
}
// 工具函数:按 family 分组并排序 API 格式数组
export interface ApiFormatGroup {
family: string
@@ -122,7 +143,7 @@ export function groupApiFormats(formats: string[]): ApiFormatGroup[] {
const sorted = sortApiFormats(formats)
const groups = new Map<string, string[]>()
for (const f of sorted) {
const { family } = parseApiFormat(f)
const { family } = parseApiFormat(normalizeApiFormatAlias(f))
if (!groups.has(family)) groups.set(family, [])
groups.get(family)?.push(f)
}
@@ -145,13 +166,26 @@ export function groupApiFormats(formats: string[]): ApiFormatGroup[] {
// 工具函数:将 API 格式签名转为友好显示名称
export function formatApiFormat(format: string | null | undefined): string {
if (!format) return '-'
const raw = format.trim()
const upper = raw.toUpperCase()
return API_FORMAT_LABELS[raw]
|| API_FORMAT_LABELS[raw.toLowerCase()]
const normalized = normalizeApiFormatAlias(format)
if (!normalized) return '-'
const upper = normalized.toUpperCase()
return API_FORMAT_LABELS[normalized]
|| API_FORMAT_LABELS[normalized.toLowerCase()]
|| API_FORMAT_LABELS[legacyUppercaseApiFormatKey(upper)]
|| API_FORMAT_LABELS[upper]
|| raw
|| normalized
}
export function formatApiFormatShort(format: string | null | undefined): string {
if (!format) return '-'
const normalized = normalizeApiFormatAlias(format)
if (!normalized) return '-'
const upper = normalized.toUpperCase()
return API_FORMAT_SHORT[normalized]
|| API_FORMAT_SHORT[normalized.toLowerCase()]
|| API_FORMAT_SHORT[legacyUppercaseApiFormatKey(upper)]
|| API_FORMAT_SHORT[upper]
|| normalized.substring(0, 2)
}
function legacyUppercaseApiFormatKey(value: string): string {
@@ -168,8 +202,8 @@ function legacyUppercaseApiFormatKey(value: string): string {
// 工具函数:按标准顺序排序 API 格式数组
export function sortApiFormats(formats: string[]): string[] {
return [...formats].sort((a, b) => {
const aIdx = API_FORMAT_ORDER.indexOf(a)
const bIdx = API_FORMAT_ORDER.indexOf(b)
const aIdx = API_FORMAT_ORDER.indexOf(normalizeApiFormatAlias(a))
const bIdx = API_FORMAT_ORDER.indexOf(normalizeApiFormatAlias(b))
if (aIdx === -1 && bIdx === -1) return 0
if (aIdx === -1) return 1
if (bIdx === -1) return -1