Spaces:
Sleeping
Sleeping
File size: 11,320 Bytes
b88ce1b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
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 <div className="p-12 text-center text-zinc-400">加载中...</div>;
return (
<div className="space-y-6 max-w-4xl mx-auto pb-12">
<div className="flex justify-between items-center sticky top-0 bg-zinc-50/90 backdrop-blur-sm py-4 z-10">
<div>
<h2 className="text-2xl font-semibold text-zinc-900 tracking-tight">系统设置</h2>
<p className="text-zinc-500">配置服务器参数和模型默认值</p>
</div>
<button
onClick={handleSave}
disabled={isSaving}
className="flex items-center gap-2 px-6 py-2.5 bg-zinc-900 hover:bg-zinc-800 text-white font-medium rounded-xl transition-colors disabled:opacity-50 shadow-sm"
>
{isSaving ? <Loader2 className="w-4 h-4 animate-spin" /> : <Save className="w-4 h-4" />}
{isSaving ? '保存中...' : '保存设置'}
</button>
</div>
<AnimatePresence>
{message.content && (
<motion.div
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: 'auto' }}
exit={{ opacity: 0, height: 0 }}
className={cn(
"flex items-center gap-2 p-4 rounded-xl text-sm font-medium border",
message.type === 'error'
? "bg-red-50 text-red-600 border-red-100"
: "bg-emerald-50 text-emerald-600 border-emerald-100"
)}
>
<AlertCircle className="w-4 h-4" />
{message.content}
</motion.div>
)}
</AnimatePresence>
{/* Server Config */}
<div className="bg-white rounded-xl border border-zinc-200 p-6 shadow-sm">
<h3 className="font-semibold text-zinc-900 mb-6 flex items-center gap-2 text-base">
<Server className="w-5 h-5 text-zinc-900" />
服务器配置
</h3>
<div className="grid md:grid-cols-2 gap-6">
<FormInput
label="服务端口"
value={settings.port || ''}
onChange={v => handleChange('port', v)}
placeholder="8045"
type="number"
/>
<FormInput
label="监听地址"
value={settings.host || ''}
onChange={v => handleChange('host', v)}
placeholder="0.0.0.0"
/>
</div>
</div>
{/* Security Config */}
<div className="bg-white rounded-xl border border-zinc-200 p-6 shadow-sm">
<h3 className="font-semibold text-zinc-900 mb-6 flex items-center gap-2 text-base">
<Shield className="w-5 h-5 text-zinc-900" />
安全配置
</h3>
<div className="space-y-6">
<FormInput
label="默认 API 密钥"
value={settings.apiKey || ''}
onChange={v => handleChange('apiKey', v)}
placeholder="sk-test"
helper="此密钥不受频率限制约束,用于测试或内部使用"
/>
<FormInput
label="管理员密码"
value={settings.adminPassword || ''}
onChange={v => handleChange('adminPassword', v)}
placeholder="admin123"
type="password"
/>
<FormInput
label="最大请求体大小"
value={settings.maxRequestSize || ''}
onChange={v => handleChange('maxRequestSize', v)}
placeholder="50mb"
/>
</div>
</div>
{/* Model Defaults */}
<div className="bg-white rounded-xl border border-zinc-200 p-6 shadow-sm">
<h3 className="font-semibold text-zinc-900 mb-6 flex items-center gap-2 text-base">
<Sliders className="w-5 h-5 text-zinc-900" />
模型默认参数
</h3>
<div className="grid md:grid-cols-2 gap-6">
<FormInput
label="Temperature"
value={settings.temperature || ''}
onChange={v => handleChange('temperature', parseFloat(v))}
type="number" step="0.1" min="0" max="2"
/>
<FormInput
label="Top P"
value={settings.topP || ''}
onChange={v => handleChange('topP', parseFloat(v))}
type="number" step="0.01" min="0" max="1"
/>
<FormInput
label="Top K"
value={settings.topK || ''}
onChange={v => handleChange('topK', parseInt(v))}
type="number" min="1"
/>
<FormInput
label="最大 Token 数"
value={settings.maxTokens || ''}
onChange={v => handleChange('maxTokens', parseInt(v))}
type="number" min="1"
/>
</div>
</div>
{/* System Instruction */}
<div className="bg-white rounded-xl border border-zinc-200 p-6 shadow-sm">
<h3 className="font-semibold text-zinc-900 mb-6 flex items-center gap-2 text-base">
<MessageSquare className="w-5 h-5 text-zinc-900" />
系统指令
</h3>
<div>
<label className="block text-sm font-medium text-zinc-700 mb-2">System Instruction</label>
<textarea
value={settings.systemInstruction || ''}
onChange={e => handleChange('systemInstruction', e.target.value)}
rows={5}
placeholder="输入系统提示词..."
className="w-full px-4 py-3 bg-zinc-50 border border-zinc-200 rounded-xl focus:ring-2 focus:ring-zinc-900/5 focus:border-zinc-900 outline-none transition-all resize-y text-sm placeholder:text-zinc-400"
/>
</div>
</div>
</div>
);
}
function FormInput({ label, value, onChange, type = "text", placeholder, helper, ...props }) {
return (
<div>
<label className="block text-sm font-medium text-zinc-700 mb-2">{label}</label>
<input
type={type}
value={value}
onChange={e => onChange(e.target.value)}
placeholder={placeholder}
className="w-full px-4 py-2.5 bg-zinc-50 border border-zinc-200 rounded-xl focus:ring-2 focus:ring-zinc-900/5 focus:border-zinc-900 outline-none transition-all text-sm placeholder:text-zinc-400"
{...props}
/>
{helper && <p className="mt-1.5 text-xs text-zinc-400">{helper}</p>}
</div>
);
}
|