Commit e1fbf73a by CaIon

fix: improve input validation and error handling in ModelSetting and SettingGeminiModel components

parent eea5967e
...@@ -39,8 +39,10 @@ const ModelSetting = () => { ...@@ -39,8 +39,10 @@ const ModelSetting = () => {
item.key === 'claude.default_max_tokens' || item.key === 'claude.default_max_tokens' ||
item.key === 'gemini.supported_imagine_models' item.key === 'gemini.supported_imagine_models'
) { ) {
if (item.value !== '') {
item.value = JSON.stringify(JSON.parse(item.value), null, 2); item.value = JSON.stringify(JSON.parse(item.value), null, 2);
} }
}
if (item.key.endsWith('Enabled') || item.key.endsWith('enabled')) { if (item.key.endsWith('Enabled') || item.key.endsWith('enabled')) {
newInputs[item.key] = item.value === 'true' ? true : false; newInputs[item.key] = item.value === 'true' ? true : false;
} else { } else {
...@@ -60,6 +62,7 @@ const ModelSetting = () => { ...@@ -60,6 +62,7 @@ const ModelSetting = () => {
// showSuccess('刷新成功'); // showSuccess('刷新成功');
} catch (error) { } catch (error) {
showError('刷新失败'); showError('刷新失败');
console.error(error);
} finally { } finally {
setLoading(false); setLoading(false);
} }
......
...@@ -27,14 +27,17 @@ export default function SettingGeminiModel(props) { ...@@ -27,14 +27,17 @@ export default function SettingGeminiModel(props) {
const [inputs, setInputs] = useState({ const [inputs, setInputs] = useState({
'gemini.safety_settings': '', 'gemini.safety_settings': '',
'gemini.version_settings': '', 'gemini.version_settings': '',
'gemini.supported_imagine_models': [], 'gemini.supported_imagine_models': '',
'gemini.thinking_adapter_enabled': false, 'gemini.thinking_adapter_enabled': false,
'gemini.thinking_adapter_budget_tokens_percentage': 0.6, 'gemini.thinking_adapter_budget_tokens_percentage': 0.6,
}); });
const refForm = useRef(); const refForm = useRef();
const [inputsRow, setInputsRow] = useState(inputs); const [inputsRow, setInputsRow] = useState(inputs);
function onSubmit() { async function onSubmit() {
await refForm.current
.validate()
.then(() => {
const updateArray = compareObjects(inputs, inputsRow); const updateArray = compareObjects(inputs, inputsRow);
if (!updateArray.length) return showWarning(t('你似乎并没有修改什么')); if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
const requestQueue = updateArray.map((item) => { const requestQueue = updateArray.map((item) => {
...@@ -62,6 +65,11 @@ export default function SettingGeminiModel(props) { ...@@ -62,6 +65,11 @@ export default function SettingGeminiModel(props) {
.finally(() => { .finally(() => {
setLoading(false); setLoading(false);
}); });
})
.catch((error) => {
console.error('Validation failed:', error);
showError(t('请检查输入'));
});
} }
useEffect(() => { useEffect(() => {
...@@ -146,6 +154,14 @@ export default function SettingGeminiModel(props) { ...@@ -146,6 +154,14 @@ export default function SettingGeminiModel(props) {
label={t('支持的图像模型')} label={t('支持的图像模型')}
placeholder={t('例如:') + '\n' + JSON.stringify(['gemini-2.0-flash-exp-image-generation'], null, 2)} placeholder={t('例如:') + '\n' + JSON.stringify(['gemini-2.0-flash-exp-image-generation'], null, 2)}
onChange={(value) => setInputs({ ...inputs, 'gemini.supported_imagine_models': value })} onChange={(value) => setInputs({ ...inputs, 'gemini.supported_imagine_models': value })}
trigger='blur'
stopValidateWithError
rules={[
{
validator: (rule, value) => verifyJSON(value),
message: t('不是合法的 JSON 字符串'),
},
]}
/> />
</Col> </Col>
</Row> </Row>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment