refactor(api): migrate config-api to ApiResponse pattern

This commit is contained in:
DrSmoothl
2026-03-01 17:43:47 +08:00
parent 88e157040f
commit d4bfc9591c
9 changed files with 288 additions and 156 deletions

View File

@@ -3,6 +3,7 @@
* 监听 models 和 taskConfig 变化,自动保存到服务器
*/
import { useRef, useEffect, useCallback } from 'react'
import type { RefObject } from 'react'
import { updateModelConfigSection } from '@/lib/config-api'
import type { ModelInfo, ModelTaskConfig } from '../types'
@@ -23,7 +24,7 @@ interface UseModelAutoSaveReturn {
/** 清除所有待执行的保存定时器 */
clearTimers: () => void
/** 初始加载状态标记引用 (用于设置初始加载完成) */
initialLoadRef: React.MutableRefObject<boolean>
initialLoadRef: RefObject<boolean>
}
/**
@@ -84,7 +85,10 @@ export function useModelAutoSave(
onSavingChange?.(true)
// 清理每个模型中的 null 值
const cleanedModels = newModels.map(cleanModelForSave)
await updateModelConfigSection('models', cleanedModels)
const result = await updateModelConfigSection('models', cleanedModels)
if (!result.success) {
throw new Error(result.error)
}
onUnsavedChange?.(false)
} catch (error) {
console.error('自动保存模型列表失败:', error)
@@ -98,7 +102,10 @@ export function useModelAutoSave(
const autoSaveTaskConfig = useCallback(async (newTaskConfig: ModelTaskConfig) => {
try {
onSavingChange?.(true)
await updateModelConfigSection('model_task_config', newTaskConfig)
const result = await updateModelConfigSection('model_task_config', newTaskConfig)
if (!result.success) {
throw new Error(result.error)
}
onUnsavedChange?.(false)
} catch (error) {
console.error('自动保存任务配置失败:', error)

View File

@@ -88,11 +88,15 @@ export function useModelFetcher(options: UseModelFetcherOptions): UseModelFetche
setModelFetchError(null)
try {
const models = await fetchProviderModels(
const result = await fetchProviderModels(
providerName,
template.modelFetcher.parser,
template.modelFetcher.endpoint
)
if (!result.success) {
throw new Error(result.error)
}
const models = result.data
setAvailableModels(models)
// 更新缓存
modelListCache.set(cacheKey, { models, timestamp: Date.now() })