mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 01:10:23 +08:00
refactor: 用 EndpointFetchConfig 纯数据类替代 ORM 对象传递,统一上游模型缓存管理
- 引入 EndpointFetchConfig dataclass 替代直接传递 ProviderEndpoint ORM 对象, 避免 DB session 关闭后 DetachedInstanceError - 新增 build_format_to_config() 统一构建 api_format -> EndpointFetchConfig 映射 - KeyAllowedModelsDialog 改用 useUpstreamModelsCache composable 管理上游模型获取 - useUpstreamModelsCache 增加 error 字段透传部分格式获取失败的 warning - 删除废弃的 queryProviderUpstreamModels API 函数 - ProviderCandidate 添加 __lt__ 方法支持排序比较
This commit is contained in:
@@ -122,29 +122,6 @@ export async function batchAssignModelsToProvider(
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询提供商的上游模型列表
|
||||
*/
|
||||
export async function queryProviderUpstreamModels(
|
||||
providerId: string
|
||||
): Promise<{
|
||||
success: boolean
|
||||
data: {
|
||||
models: UpstreamModel[]
|
||||
error: string | null
|
||||
}
|
||||
provider: {
|
||||
id: string
|
||||
name: string
|
||||
display_name: string
|
||||
}
|
||||
}> {
|
||||
const response = await client.post('/api/admin/provider-query/models', {
|
||||
provider_id: providerId,
|
||||
})
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 从上游提供商导入模型
|
||||
* @param providerId 提供商 ID
|
||||
|
||||
@@ -192,7 +192,6 @@ import Badge from '@/components/ui/badge.vue'
|
||||
import Checkbox from '@/components/ui/checkbox.vue'
|
||||
import { useToast } from '@/composables/useToast'
|
||||
import { parseUpstreamModelError } from '@/utils/errorParser'
|
||||
import { adminApi } from '@/api/admin'
|
||||
import {
|
||||
importModelsFromUpstream,
|
||||
getProviderModels,
|
||||
@@ -200,6 +199,7 @@ import {
|
||||
type UpstreamModel,
|
||||
API_FORMAT_LABELS,
|
||||
} from '@/api/endpoints'
|
||||
import { useUpstreamModelsCache } from '../composables/useUpstreamModelsCache'
|
||||
|
||||
const props = defineProps<{
|
||||
open: boolean
|
||||
@@ -213,6 +213,7 @@ const emit = defineEmits<{
|
||||
}>()
|
||||
|
||||
const { success, error: showError } = useToast()
|
||||
const { fetchModels: fetchCachedModels } = useUpstreamModelsCache()
|
||||
|
||||
const isOpen = computed(() => props.open)
|
||||
const loading = ref(false)
|
||||
@@ -275,7 +276,7 @@ async function loadExistingModels() {
|
||||
}
|
||||
}
|
||||
|
||||
// 获取上游模型(获取所有 Key 的聚合结果)
|
||||
// 获取上游模型(获取所有 Key 的聚合结果,通过 useUpstreamModelsCache 统一管理)
|
||||
async function fetchUpstreamModels() {
|
||||
if (!props.providerId) return
|
||||
|
||||
@@ -285,27 +286,26 @@ async function fetchUpstreamModels() {
|
||||
try {
|
||||
// 不传 apiKeyId,后端会遍历所有 Key 并聚合结果。
|
||||
// 已查询过再点“刷新”时,强制跳过后端缓存,避免长期 TTL 导致模型列表不更新。
|
||||
const response = await adminApi.queryProviderModels(props.providerId, undefined, hasQueried.value)
|
||||
const result = await fetchCachedModels(props.providerId, undefined, hasQueried.value)
|
||||
|
||||
if (response.success && response.data?.models) {
|
||||
upstreamModels.value = response.data.models
|
||||
if (result.models.length > 0) {
|
||||
upstreamModels.value = result.models
|
||||
// 默认选中所有新模型
|
||||
selectedModels.value = response.data.models
|
||||
selectedModels.value = result.models
|
||||
.filter((m: UpstreamModel) => !existingModelIds.value.has(m.id))
|
||||
.map((m: UpstreamModel) => m.id)
|
||||
hasQueried.value = true
|
||||
// 如果有部分失败,显示警告提示
|
||||
if (response.data.error) {
|
||||
// 使用友好的错误解析
|
||||
showError(`部分格式获取失败: ${parseUpstreamModelError(response.data.error)}`, '警告')
|
||||
if (result.error) {
|
||||
showError(`部分格式获取失败: ${result.error}`, '警告')
|
||||
}
|
||||
} else if (result.error) {
|
||||
errorMessage.value = result.error
|
||||
} else {
|
||||
// 使用友好的错误解析
|
||||
const rawError = response.data?.error || '获取上游模型失败'
|
||||
errorMessage.value = parseUpstreamModelError(rawError)
|
||||
// 上游返回空列表但无错误
|
||||
hasQueried.value = true
|
||||
}
|
||||
} catch (err: any) {
|
||||
// 使用友好的错误解析
|
||||
const rawError = err.response?.data?.detail || err.message || '获取上游模型失败'
|
||||
errorMessage.value = parseUpstreamModelError(rawError)
|
||||
} finally {
|
||||
|
||||
@@ -54,6 +54,8 @@ export function useUpstreamModelsCache() {
|
||||
if (response.success && response.data?.models) {
|
||||
return {
|
||||
models: response.data.models,
|
||||
// 传递部分格式获取失败的 warning(后端 success=true 但仍可能附带 error)
|
||||
error: response.data.error ? parseUpstreamModelError(response.data.error) : undefined,
|
||||
fromCache: response.data.from_cache
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user