import React, { useCallback, useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button, Form, Input, InputNumber, Modal, Select, Space, Spin, Switch, Tag, Typography, } from 'antd'; import { ConsoleAlert as Alert } from '../console/ConsolePrimitives'; import { showError, showSuccess } from '../../helpers'; import { getAdminStatus, loadOptions, saveOption, setJevKey } from './api'; import JevProxySettings from './JevProxySettings'; const { Text } = Typography; const { TextArea } = Input; const PREFIX = 'smart_router_setting.'; // 字段定义:type 决定编辑控件;compliance=true 的是合规相关(只有 root 能改,后端 rootOnlyOptionKeys) const FIELDS = [ { key: 'enabled', type: 'bool', label: '总开关', section: 'main' }, { key: 'rollout_percent', type: 'int', label: '灰度比例(%)', min: 0, max: 100, section: 'main', }, { key: 'single_node_ack', type: 'bool', label: '确认为单节点部署(Redis 未启用时必填)', section: 'main', compliance: true, }, { key: 'max_profiles_per_user', type: 'int', label: '每人最多个人集合数', min: 0, max: 100, section: 'main', }, { key: 'jev_egress_allowed', type: 'bool', label: '允许调用境外判定模型(通过合规评估后开启)', section: 'jev', compliance: true, }, { key: 'jev_primary', type: 'select', label: '主通道', options: ['typesafe', 'openrouter'], section: 'jev', }, { key: 'jev_model_typesafe', type: 'str', label: 'TypeSafe 模型版本', section: 'jev', }, { key: 'jev_model_openrouter', type: 'str', label: 'OpenRouter 模型版本', section: 'jev', }, { key: 'judge_budget_ms', type: 'int', label: '判定总预算(毫秒,以国内压测 P95 为准)', min: 20, max: 5000, section: 'jev', }, { key: 'judge_hedge_enabled', type: 'bool', label: '主备对冲', section: 'jev', }, { key: 'judge_hedge_after_ms', type: 'int', label: '对冲触发(毫秒,取压测 P80)', min: 1, max: 5000, section: 'jev', }, { key: 'state_max_bytes', type: 'int', label: '发送给判定模型的内容上限(字节)', min: 1000, max: 30000, section: 'jev', }, { key: 'default_tau_quality', type: 'str', label: '质量优先 τ1,τ2,τ3', section: 'judge', }, { key: 'default_tau_balanced', type: 'str', label: '平衡 τ1,τ2,τ3', section: 'judge', }, { key: 'default_tau_cost', type: 'str', label: '成本优先 τ1,τ2,τ3', section: 'judge', }, { key: 'skill_injection_token_reserve', type: 'int', label: 'skill 注入预留 token', min: 0, max: 100000, section: 'judge', }, { key: 'strip_incompatible_channel_types', type: 'str', label: '未通过剥离兼容性验证的渠道类型(逗号分隔)', section: 'judge', compliance: true, }, { key: 'rules_judge_config', type: 'json', label: '规则判定器关键词与分数(JSON,留空使用内置)', section: 'judge', }, { key: 'consent_text_versions', type: 'json', label: '同意文案版本(JSON,如 {"judge":{"v":1,"overseas":true}})', section: 'consent', compliance: true, }, { key: 'router_eval_retention_days', type: 'int', label: '判定明细保留天数', min: 1, max: 365, section: 'retention', }, { key: 'simulate_daily_cap_per_user', type: 'int', label: '试路由每人每日上限', min: 0, max: 100000, section: 'retention', }, { key: 'simulate_daily_cap_per_org', type: 'int', label: '试路由每企业每日上限', min: 0, max: 1000000, section: 'retention', }, { key: 'max_profiles_per_org', type: 'int', label: '每个企业最多集合数', min: 0, max: 1000, section: 'retention', }, { key: 'jev_daily_cap_per_user', type: 'int', label: '每人每日模型判定次数上限(0 表示不限)', min: 0, max: 10000000, section: 'retention', }, { key: 'jev_daily_cap_per_org', type: 'int', label: '每个企业每日模型判定次数上限(0 表示不限)', min: 0, max: 100000000, section: 'retention', }, ]; const SECTIONS = [ ['main', '总控'], ['jev', '判定模型'], ['judge', '判定参数'], ['consent', '同意文案'], ['retention', '保留与限流'], ]; // 保存顺序:前置条件(single_node_ack)必须先于依赖它的总开关保存 const SAVE_ORDER = ['single_node_ack', 'consent_text_versions']; const GlobalSettings = () => { const { t } = useTranslation(); const [values, setValues] = useState({}); const [original, setOriginal] = useState({}); const [status, setStatus] = useState(null); const [loading, setLoading] = useState(true); const [loadError, setLoadError] = useState(''); const [saving, setSaving] = useState(false); const [keys, setKeys] = useState({ typesafe: '', openrouter: '' }); const tRef = useRef(t); tRef.current = t; const load = useCallback(async () => { setLoading(true); setLoadError(''); try { const data = await loadOptions(); const v = {}; (data || []).forEach((item) => { if (item.key.startsWith(PREFIX)) v[item.key.slice(PREFIX.length)] = item.value; }); setValues(v); setOriginal(v); setStatus(await getAdminStatus()); } catch (e) { setLoadError(e?.message || tRef.current('加载失败')); } finally { setLoading(false); } }, []); // 不依赖 t:切换语言不能重新加载并丢掉未保存的编辑 useEffect(() => { load(); }, [load]); const save = async () => { const changed = FIELDS.filter( (f) => String(values[f.key] ?? '') !== String(original[f.key] ?? ''), ).sort((a, b) => { const ia = SAVE_ORDER.indexOf(a.key); const ib = SAVE_ORDER.indexOf(b.key); return (ia < 0 ? 99 : ia) - (ib < 0 ? 99 : ib); }); if (changed.length === 0) { showSuccess(t('配置未修改')); return; } setSaving(true); const saved = {}; try { for (const f of changed) { try { await saveOption(PREFIX + f.key, String(values[f.key] ?? '')); saved[f.key] = values[f.key]; } catch (e) { showError(`${t(f.label)} · ${e?.message ? t(e.message) : ''}`); // 已保存的项更新基线,未保存的编辑保留在表单里,便于修改后重试 setOriginal((o) => ({ ...o, ...saved })); return; } } showSuccess(t('已保存')); await load(); } finally { setSaving(false); } }; const saveKey = async (provider, clear = false) => { const run = async () => { try { await setJevKey(provider, keys[provider], clear); setKeys((k) => ({ ...k, [provider]: '' })); showSuccess(clear ? t('已清除') : t('已保存')); setStatus(await getAdminStatus()); } catch (e) { showError(e?.message || t('保存失败,请稍后重试')); } }; if (!clear) return run(); Modal.confirm({ title: t('确认清除密钥?'), content: t( '清除后,此通道将无法调用判定模型。所有通道均不可用时,将使用规则判定。', ), okButtonProps: { danger: true }, onOk: run, }); }; const renderField = (f) => { const v = values[f.key]; const set = (nv) => setValues((s) => ({ ...s, [f.key]: nv })); switch (f.type) { case 'bool': return ( set(c ? 'true' : 'false')} aria-label={t(f.label)} /> ); case 'int': return ( { // antd 清空时回调 null,按原 Semi 的空串语义回落到最小值 const n = raw ?? ''; set( String( Number.isFinite(Number(n)) && n !== '' ? Number(n) : (f.min ?? 0), ), ); }} /> ); case 'select': return (