/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { useEffectEvent } from '../../../hooks/common/useEffectEvent';
import React, { useEffect, useState, useRef } from 'react';
import { Button, Col, Form, Row, Spin, Banner } from '@/components/ui';
import {
compareObjects,
API,
isRoot,
showError,
showSuccess,
showWarning,
} from '../../../helpers';
import { useTranslation } from 'react-i18next';
export default function SettingsFallback(props) {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const [inputs, setInputs] = useState({
// Phase 1
'fallback_setting.enabled': false,
'fallback_setting.max_depth': 3,
'fallback_setting.timeout_ms': 60000,
'fallback_setting.total_timeout_ms': 180000,
'fallback_setting.billing_mode': 'actual',
'fallback_setting.edit_permission': 'root_only',
// Phase 2
'fallback_setting.allow_cross_group': false,
'fallback_setting.cross_border_allowed': false,
'fallback_setting.bound_user_allowed': false,
// Phase 3
'fallback_setting.cooling_seconds': 30,
'fallback_setting.per_token_max_per_minute': 0,
'fallback_setting.rollout_percent': 100,
'fallback_setting.max_attempts_per_request': 10,
});
const refForm = useRef();
const [inputsRow, setInputsRow] = useState(inputs);
function onSubmit() {
const updateArray = compareObjects(inputs, inputsRow);
if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
// 前端先校验总超时 ≥ 单跳超时(单跳超时为 0 表示关闭,不校验);
// 后端只校验各自范围,不校验两者关系
const timeoutMs = Number(inputs['fallback_setting.timeout_ms']);
const totalTimeoutMs = Number(inputs['fallback_setting.total_timeout_ms']);
if (timeoutMs > 0 && totalTimeoutMs < timeoutMs) {
return showError(t('整条链总超时应不小于单跳超时'));
}
const requestQueue = updateArray.map((item) =>
API.put('/api/option/', {
key: item.key,
value: String(inputs[item.key]),
}),
);
setLoading(true);
// 逐个检查业务结果:后端校验失败返回 HTTP 200 + success:false,
// 只看 Promise 是否 resolve 会把失败误报为保存成功
Promise.allSettled(requestQueue)
.then((results) => {
const failedMessages = [];
results.forEach((r) => {
if (r.status === 'fulfilled' && r.value?.data?.success) return;
const message =
r.status === 'fulfilled'
? r.value?.data?.message
: r.reason?.response?.data?.message || r.reason?.message;
failedMessages.push(message || t('保存失败,请重试'));
});
if (failedMessages.length === 0) {
showSuccess(t('保存成功'));
} else {
showError(
t('保存失败: ') + Array.from(new Set(failedMessages)).join(';'),
);
}
})
.finally(() => {
setLoading(false);
// 无论成败都回读,让页面与库中实际值保持一致
props.refresh();
});
}
const onOptionsChange = useEffectEvent(() => {
const currentInputs = {};
for (let key in props.options) {
if (Object.keys(inputs).includes(key)) {
currentInputs[key] = props.options[key];
}
}
// 合并已有 inputs 默认值,避免覆盖丢失
const merged = { ...inputs, ...currentInputs };
setInputs(merged);
setInputsRow(structuredClone(merged));
refForm.current?.setValues(merged);
});
useEffect(() => onOptionsChange(), [onOptionsChange, props.options]);
return (
setInputs({ ...inputs, 'fallback_setting.enabled': value })
}
extraText={t('关闭后所有兜底逻辑跳过,行为与改造前一致')}
/>