/** * 本地模型API服务 * * 提供本地模型的查询和连接测试功能(仅查看,无管理功能) * 需求: 4 */ import { authService } from './authService'; const API_BASE = import.meta.env.VITE_API_BASE_URL || ''; export interface LocalModel { id: number; name: string; supplier?: string; base_url: string; has_api_key: boolean; visibility: string; category: number; created_at: string; updated_at: string; } export interface ConnectionTestResult { success: boolean; message: string; latency_ms?: number; } interface ApiResponse { code: number; message: string; data: T; } async function request(url: string, options: RequestInit = {}): Promise> { const token = authService.getToken(); const headers: HeadersInit = { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}), ...options.headers, }; const response = await fetch(`${API_BASE}${url}`, { ...options, headers, }); if (!response.ok) { const error = await response.json().catch(() => ({ message: '请求失败' })); throw new Error(error.detail || error.message || '请求失败'); } return response.json(); } export const localModelApi = { /** * 获取本地模型列表 */ async getLocalModels(): Promise { const res = await request('/api/models/local'); return res.data; }, /** * 测试本地模型连接 */ async testConnection(base_url: string, api_key?: string, model_name?: string): Promise { const res = await request('/api/models/local/test', { method: 'POST', body: JSON.stringify({ base_url, api_key, model_name }), }); return res.data; }, };