'use client' import { useState, useEffect } from 'react' const API_BASE = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8000' interface Provider { id: number name: string model_id: string base_url: string | null is_active: boolean is_default: boolean } export default function ProvidersPage() { const [providers, setProviders] = useState([]) const [showForm, setShowForm] = useState(false) const [editId, setEditId] = useState(null) const [form, setForm] = useState({ name: '', model_id: '', base_url: '', api_key: '', is_default: false, }) const getToken = () => localStorage.getItem('admin_token') || '' const fetchProviders = async () => { const res = await fetch(`${API_BASE}/api/v1/admin/providers`, { headers: { Authorization: `Bearer ${getToken()}` }, }) if (res.ok) { setProviders(await res.json()) } } useEffect(() => { fetchProviders() }, []) const handleSubmit = async () => { const url = editId ? `${API_BASE}/api/v1/admin/providers/${editId}` : `${API_BASE}/api/v1/admin/providers` const method = editId ? 'PUT' : 'POST' await fetch(url, { method, headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${getToken()}`, }, body: JSON.stringify(form), }) setShowForm(false) setEditId(null) setForm({ name: '', model_id: '', base_url: '', api_key: '', is_default: false }) fetchProviders() } const handleDelete = async (id: number) => { if (!confirm('确定删除?')) return await fetch(`${API_BASE}/api/v1/admin/providers/${id}`, { method: 'DELETE', headers: { Authorization: `Bearer ${getToken()}` }, }) fetchProviders() } return (

AI 配置管理

{/* Provider 列表 */}
{providers.map((p) => ( ))}
名称 模型 ID Base URL 状态 操作
{p.name} {p.is_default && ( 默认 )} {p.model_id} {p.base_url || '-'} {p.is_active ? '启用' : '禁用'}
{/* 添加/编辑表单 */} {showForm && (

{editId ? '编辑' : '添加'} AI 配置

setForm({ ...form, name: e.target.value })} className="w-full p-2 border rounded" />
setForm({ ...form, model_id: e.target.value })} className="w-full p-2 border rounded" placeholder="gpt-4o-mini" />
setForm({ ...form, base_url: e.target.value })} className="w-full p-2 border rounded" placeholder="https://api.openai.com/v1" />
setForm({ ...form, api_key: e.target.value })} className="w-full p-2 border rounded" />
)}
) }