import { useState, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { Save, Server, Shield, Sliders, MessageSquare, AlertCircle, Loader2 } from 'lucide-react'; import { useAuth } from '../context/AuthContext'; import { cn } from '../lib/utils'; export default function Settings() { const { token: adminToken } = useAuth(); const [settings, setSettings] = useState({}); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [message, setMessage] = useState({ type: '', content: '' }); useEffect(() => { const fetchSettings = async () => { try { const res = await fetch('/admin/settings', { headers: { 'X-Admin-Token': adminToken } }); const data = await res.json(); // Map nested backend data to flat UI state setSettings({ port: data.server?.port, host: data.server?.host, apiKey: data.security?.apiKey, adminPassword: data.security?.adminPassword, maxRequestSize: data.security?.maxRequestSize, temperature: data.defaults?.temperature, topP: data.defaults?.top_p, topK: data.defaults?.top_k, maxTokens: data.defaults?.max_tokens, systemInstruction: data.systemInstruction }); } catch (error) { console.error('Failed to fetch settings', error); setMessage({ type: 'error', content: '加载设置失败' }); } finally { setIsLoading(false); } }; fetchSettings(); }, [adminToken]); const handleChange = (key, value) => { setSettings(prev => ({ ...prev, [key]: value })); }; const handleSave = async () => { setIsSaving(true); setMessage({ type: '', content: '' }); try { // Map flat UI state back to nested backend structure const payload = { server: { port: settings.port, host: settings.host }, security: { apiKey: settings.apiKey, adminPassword: settings.adminPassword, maxRequestSize: settings.maxRequestSize }, defaults: { temperature: settings.temperature, top_p: settings.topP, top_k: settings.topK, max_tokens: settings.maxTokens }, systemInstruction: settings.systemInstruction }; const res = await fetch('/admin/settings', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Admin-Token': adminToken }, body: JSON.stringify(payload) }); const data = await res.json(); if (data.success) { setMessage({ type: 'success', content: '设置保存成功' }); } else { setMessage({ type: 'error', content: '保存失败' }); } } catch (error) { setMessage({ type: 'error', content: '保存失败: ' + error.message }); } finally { setIsSaving(false); } }; if (isLoading) return
配置服务器参数和模型默认值
{helper}
}