修复:查询局部刷新与智能分析会话交互,完善分析收尾和配置切换

This commit is contained in:
2026-10-23 15:30:06 +08:00
parent 5fbeb52b93
commit 6394e057bf
40 changed files with 2107 additions and 239 deletions
+27 -10
View File
@@ -1,10 +1,11 @@
import { useRef, useState } from 'react';
import { Alert, App, Button, Form, Input, Modal, Spin } from 'antd';
import { Alert, App, Button, Form, Input, Modal, Select, Spin, Tag } from 'antd';
import { SettingOutlined } from '@ant-design/icons';
import { api } from './api';
export type AnalysisModelSettings = { baseUrl: string; model: string; hasApiKey: boolean; revision: string };
export type AnalysisModelSettingsInput = { baseUrl: string; model: string; apiKey?: string; revision?: string };
export type AnalysisModelProfile = { id: string; name: string; baseUrl: string; model: string; hasApiKey: boolean };
export type AnalysisModelSettings = { baseUrl: string; model: string; hasApiKey: boolean; revision: string; activeProfileId: string; profiles: AnalysisModelProfile[] };
export type AnalysisModelSettingsInput = { baseUrl: string; model: string; apiKey?: string; revision?: string; profileId?: string };
export function AnalysisApiSettings({ onUnauthorized }: { onUnauthorized: (error: unknown) => boolean }) {
const { message } = App.useApp();
@@ -14,16 +15,18 @@ export function AnalysisApiSettings({ onUnauthorized }: { onUnauthorized: (error
const [saving, setSaving] = useState(false);
const [testing, setTesting] = useState(false);
const [settings, setSettings] = useState<AnalysisModelSettings>();
const [profileId, setProfileId] = useState<string>();
const selectedProfile = settings?.profiles.find(profile => profile.id === profileId);
const [notice, setNotice] = useState<{ type: 'success' | 'error'; text: string }>();
const sequence = useRef(0);
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); setSettings(undefined); setNotice(undefined); form.resetFields();
setOpen(true); setLoading(true); setSettings(undefined); setProfileId(undefined); setNotice(undefined); form.resetFields();
try {
const value = await api.analysisSettings();
if (sequence.current !== current) return;
setSettings(value); form.setFieldsValue({ baseUrl: value.baseUrl, model: value.model, apiKey: '' });
setSettings(value); setProfileId(value.activeProfileId); form.setFieldsValue({ baseUrl: value.baseUrl, model: value.model, apiKey: '' });
} catch (error) {
if (sequence.current === current && !onUnauthorized(error)) setNotice({ type: 'error', text: error instanceof Error ? error.message : '读取配置失败' });
} finally { if (sequence.current === current) setLoading(false); }
@@ -35,15 +38,15 @@ export function AnalysisApiSettings({ onUnauthorized }: { onUnauthorized: (error
const current = sequence.current;
setNotice(undefined); test ? setTesting(true) : setSaving(true);
try {
const body = { ...values, revision: settings.revision };
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); form.setFieldsValue({ apiKey: '' });
setNotice({ type: 'success', text: '已保存,下一轮分析使用新配置。' }); message.success('API 配置已保存');
setSettings(result); setProfileId(result.activeProfileId); form.setFieldsValue({ baseUrl: result.baseUrl, model: result.model, apiKey: '' });
setNotice({ type: 'success', text: '已使用此配置,后续新分析将使用它;正在运行的分析继续完成。' }); message.success('API 配置已切换');
}
} catch (error) {
if (sequence.current === current && !onUnauthorized(error)) setNotice({ type: 'error', text: error instanceof Error ? error.message : '操作失败,请重试' });
@@ -54,12 +57,26 @@ export function AnalysisApiSettings({ onUnauthorized }: { onUnauthorized: (error
<Modal title="API 配置" open={open} width={540} onCancel={close} footer={<>
<Button onClick={close} disabled={saving}>关闭</Button>
<Button loading={testing} disabled={loading || saving || !settings} onClick={() => void submit(true)}>测试连接</Button>
<Button type="primary" loading={saving} disabled={loading || testing || !settings} onClick={() => void submit(false)}>保存配置</Button>
<Button type="primary" loading={saving} disabled={loading || testing || !settings} onClick={() => void submit(false)}>保存并使用</Button>
</>}>
{loading ? <div style={{ padding: 32, textAlign: 'center' }}><Spin /></div> : <>
{settings && <div style={{ marginBottom: 20 }}>
<div style={{ marginBottom: 8 }}>选择配置 <Tag color="blue">当前使用:{settings.profiles.find(profile => profile.id === settings.activeProfileId)?.name || settings.model}</Tag></div>
<div style={{ display: 'flex', gap: 8 }}>
<Select aria-label="选择 API 配置" style={{ flex: 1, minWidth: 0 }} value={profileId} disabled={saving || testing}
options={settings.profiles.map(profile => ({ value: profile.id, label: `${profile.name} · ${profile.model}` }))}
onChange={id => {
const profile = settings.profiles.find(item => item.id === id);
if (!profile) return;
sequence.current += 1; setProfileId(id); setNotice(undefined);
form.resetFields(); form.setFieldsValue({ baseUrl: profile.baseUrl, model: profile.model, apiKey: '' });
}} />
<Button disabled={saving || testing || profileId === settings.activeProfileId} loading={saving} onClick={() => void submit(false)}>立即使用</Button>
</div>
</div>}
<Form form={form} layout="vertical" disabled={saving || testing} onValuesChange={() => setNotice(undefined)}>
<Form.Item name="baseUrl" label="接口地址" rules={[{ required: true, whitespace: true, message: '请填写接口地址' }]} extra="支持 OpenAI 兼容接口,可填写含 /v1 的基础地址或完整的 /chat/completions 地址。"><Input placeholder="https://api.example.com/v1" autoComplete="off" maxLength={2048} /></Form.Item>
<Form.Item name="apiKey" label="API Key" extra={settings?.hasApiKey ? '已配置密钥;留空保留。更换接口地址时需重新填写。' : '密钥仅保存在服务端,保存后不回显。'} rules={settings?.hasApiKey ? [] : [{ required: true, whitespace: true, message: '请填写 API Key' }]}><Input.Password placeholder={settings?.hasApiKey ? '已配置,留空保留' : '请输入 API Key'} autoComplete="new-password" maxLength={4096} /></Form.Item>
<Form.Item name="apiKey" label="API Key" extra={selectedProfile?.hasApiKey ? '此配置已保存密钥;留空保留。更换接口地址时需重新填写。' : '密钥仅保存在服务端,保存后不回显。'} rules={selectedProfile?.hasApiKey ? [] : [{ required: true, whitespace: true, message: '请填写 API Key' }]}><Input.Password placeholder={selectedProfile?.hasApiKey ? '已配置,留空保留' : '请输入 API Key'} autoComplete="new-password" maxLength={4096} /></Form.Item>
<Form.Item name="model" label="模型名称" rules={[{ required: true, whitespace: true, message: '请填写模型名称' }]}><Input placeholder="例如 qwen3.8-27b-local" autoComplete="off" maxLength={160} /></Form.Item>
</Form>
{notice && <Alert showIcon type={notice.type} message={notice.text} />}