import { usePageCache } from './pageCache'; import { useEffect, useRef, useState } from 'react'; import { Alert, App, Button, Form, Input, Modal, Select, Spin, Switch, Tag } from 'antd'; import { SettingOutlined } from '@ant-design/icons'; import { api } from './api'; import { effectiveThinkingEffort, supportsThinkingEffort } from './analysisThinking'; import { useDelayedIndicator } from './useDelayedBusy'; export type AnalysisModelProfile = { id: string; name: string; baseUrl: string; model: string; hasApiKey: boolean; thinkingEnabled: boolean; thinkingEffort: string }; export type AnalysisModelSettings = { baseUrl: string; model: string; hasApiKey: boolean; revision: string; activeProfileId: string; thinkingEnabled: boolean; thinkingEffort: string; profiles: AnalysisModelProfile[] }; export type AnalysisModelSettingsInput = { baseUrl: string; model: string; apiKey?: string; revision?: string; profileId?: string; thinkingEnabled?: boolean; thinkingEffort?: string }; export function AnalysisApiSettings({ onUnauthorized }: { onUnauthorized: (error: unknown) => boolean }) { const { message } = App.useApp(); const [form] = Form.useForm(); const { active } = usePageCache(); const thinkingEnabled = Form.useWatch('thinkingEnabled', form); const model = Form.useWatch('model', form); const effortSupported = supportsThinkingEffort(model); useEffect(() => { if (!effortSupported) form.setFieldValue('thinkingEffort', 'auto'); }, [effortSupported, form]); const [open, setOpen] = useState(false); const [loading, setLoading] = useState(false); const showLoading = useDelayedIndicator(loading, 150); const [saving, setSaving] = useState(false); const [testing, setTesting] = useState(false); const [settings, setSettings] = useState(); const [profileId, setProfileId] = useState(); const selectedProfile = settings?.profiles.find(profile => profile.id === profileId); const [notice, setNotice] = useState<{ type: 'success' | 'error'; text: string }>(); const sequence = useRef(0); const submitLock = useRef(false); const close = () => { if (saving) return; sequence.current += 1; setOpen(false); form.resetFields(); setNotice(undefined); setTesting(false); }; const load = async () => { const current = ++sequence.current; setOpen(true); setLoading(true); setNotice(undefined); try { const value = await api.analysisSettings(); if (sequence.current !== current) return; setSettings(value); setProfileId(value.activeProfileId); form.setFieldsValue({ baseUrl: value.baseUrl, model: value.model, apiKey: '', thinkingEnabled: value.thinkingEnabled ?? true, thinkingEffort: effectiveThinkingEffort(value.model, value.thinkingEffort) }); } catch (error) { if (sequence.current === current && !onUnauthorized(error)) setNotice({ type: 'error', text: error instanceof Error ? error.message : '读取配置失败' }); } finally { if (sequence.current === current) setLoading(false); } }; const submit = async (test: boolean) => { if (!settings || submitLock.current) return; let values: AnalysisModelSettingsInput; try { values = await form.validateFields(); } catch { return; } submitLock.current = true; const current = sequence.current; setNotice(undefined); test ? setTesting(true) : setSaving(true); try { const body = { ...values, revision: settings.revision, profileId }; if (test) { const result = await api.testAnalysisSettings(body); if (sequence.current === current) setNotice({ type: result.success ? 'success' : 'error', text: result.message }); } else { const result = await api.saveAnalysisSettings(body); if (sequence.current !== current) return; setSettings(result); setProfileId(result.activeProfileId); form.setFieldsValue({ baseUrl: result.baseUrl, model: result.model, apiKey: '', thinkingEnabled: result.thinkingEnabled ?? true, thinkingEffort: effectiveThinkingEffort(result.model, result.thinkingEffort) }); setNotice({ type: 'success', text: '已使用此配置,后续新分析将使用它;正在运行的分析继续完成。' }); message.success('API 配置已切换'); } } catch (error) { if (sequence.current === current && !onUnauthorized(error)) setNotice({ type: 'error', text: error instanceof Error ? error.message : '操作失败,请重试' }); } finally { submitLock.current = false; if (sequence.current === current) { setTesting(false); setSaving(false); } } }; return <> }> {showLoading && !settings ?
: <> {settings &&
选择配置 当前使用:{settings.profiles.find(profile => profile.id === settings.activeProfileId)?.name || settings.model}