import { useEffect, useState } from "react"; import { Card, Form, Input, Select, Button, Alert, Space, message } from "antd"; import { api, type RuntimeSettings, getApiBaseUrl, setApiBaseUrl, } from "../services/api"; export default function Settings() { const [form] = Form.useForm(); const [loading, setLoading] = useState(false); const [saving, setSaving] = useState(false); const [runtime, setRuntime] = useState(null); const loadSettings = async () => { setLoading(true); try { const data = await api.settings.get(); setRuntime(data); form.setFieldsValue({ system_api_base_url: getApiBaseUrl(), llm_provider: data.llm_provider, custom_openai_base_url: data.base_urls?.custom_openai || "", custom_openai_model: data.models?.custom_openai || "gpt-4o-mini", }); } catch { message.error("加载设置失败"); } finally { setLoading(false); } }; useEffect(() => { loadSettings(); }, []); const onFinish = async (values: { system_api_base_url?: string; llm_provider: "openai" | "anthropic" | "deepseek" | "custom_openai"; openai_api_key?: string; anthropic_api_key?: string; deepseek_api_key?: string; custom_openai_api_key?: string; custom_openai_base_url?: string; custom_openai_model?: string; }) => { setSaving(true); try { setApiBaseUrl(values.system_api_base_url || ""); const payload = { llm_provider: values.llm_provider, openai_api_key: values.openai_api_key?.trim() || undefined, anthropic_api_key: values.anthropic_api_key?.trim() || undefined, deepseek_api_key: values.deepseek_api_key?.trim() || undefined, custom_openai_api_key: values.custom_openai_api_key?.trim() || undefined, custom_openai_base_url: values.custom_openai_base_url?.trim() || undefined, custom_openai_model: values.custom_openai_model?.trim() || undefined, }; const data = await api.settings.update(payload); setRuntime(data); message.success("设置已保存并生效(含系统 API BaseURL)"); } catch { message.error("保存失败"); } finally { setSaving(false); } }; return (
{runtime && (
系统 API BaseURL: {getApiBaseUrl()}
当前提供商: {runtime.llm_provider}
OpenAI Key: {runtime.has_keys.openai ? "已配置" : "未配置"}
Anthropic Key: {runtime.has_keys.anthropic ? "已配置" : "未配置"}
DeepSeek Key: {runtime.has_keys.deepseek ? "已配置" : "未配置"}
自定义厂商 Key: {runtime.has_keys.custom_openai ? "已配置" : "未配置"}
自定义厂商 BaseURL: {runtime.base_urls.custom_openai || "-"}
)}
); }