Files
Aether/frontend/src/features/providers/composables/useEndpointStatus.ts

97 lines
2.4 KiB
TypeScript
Raw Normal View History

import type { EndpointHealthDetail } from '@/api/endpoints'
// 端点状态枚举
export type EndpointStatus = 'disabled' | 'no_keys' | 'keys_disabled' | 'available'
const ENDPOINT_SORT_ORDER = [
2026-04-29 09:25:19 +08:00
'claude:messages',
'openai:chat',
'openai:responses',
'openai:responses:compact',
2026-05-03 17:32:41 +08:00
'openai:embedding',
'openai:rerank',
2026-04-29 09:25:19 +08:00
'gemini:generate_content',
2026-05-03 17:32:41 +08:00
'gemini:embedding',
'openai:video',
'gemini:video',
2026-04-29 09:25:19 +08:00
'gemini:files',
2026-05-03 17:32:41 +08:00
'jina:embedding',
'jina:rerank',
'doubao:embedding',
]
/**
*
*/
export function sortEndpoints<T extends { api_format: string }>(endpoints: T[]): T[] {
return [...endpoints].sort((a, b) => {
return ENDPOINT_SORT_ORDER.indexOf(a.api_format) - ENDPOINT_SORT_ORDER.indexOf(b.api_format)
})
}
/**
*
*/
export function getEndpointStatus(endpoint: EndpointHealthDetail): EndpointStatus {
if (endpoint.is_active === false) {
return 'disabled'
}
if ((endpoint.active_keys ?? 0) === 0) {
return (endpoint.total_keys ?? 0) > 0 ? 'keys_disabled' : 'no_keys'
}
return 'available'
}
/**
*
*/
export function isEndpointAvailable(endpoint: EndpointHealthDetail): boolean {
return getEndpointStatus(endpoint) === 'available'
}
/**
*
*/
export function getHealthScoreColor(score: number | undefined | null): string {
if (score === undefined || score === null) {
return 'bg-muted-foreground/40'
}
if (score >= 0.8) return 'bg-green-500'
if (score >= 0.5) return 'bg-amber-500'
return 'bg-red-500'
}
/**
*
*/
export function getEndpointDotColor(endpoint: EndpointHealthDetail): string {
if (!isEndpointAvailable(endpoint)) {
return 'bg-muted-foreground/40'
}
return getHealthScoreColor(endpoint.health_score)
}
/**
*
*/
export function getEndpointTooltip(endpoint: EndpointHealthDetail): string {
const format = endpoint.api_format
const status = getEndpointStatus(endpoint)
switch (status) {
case 'disabled':
return `${format}: 端点禁用`
case 'no_keys':
return `${format}: 未配置密钥`
case 'keys_disabled':
return `${format}: 无可用密钥`
case 'available': {
const score = endpoint.health_score
if (score === undefined || score === null) {
return `${format}: 暂无健康数据`
}
return `${format}: 健康度 ${(score * 100).toFixed(0)}%`
}
}
}