206 lines
7.4 KiB
React
206 lines
7.4 KiB
React
/*
|
||
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 <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
import React, { useEffect, useRef, useState } from 'react';
|
||
import { Banner, Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
|
||
import { useTranslation } from 'react-i18next';
|
||
import {
|
||
API,
|
||
compareObjects,
|
||
showError,
|
||
showSuccess,
|
||
showWarning,
|
||
toBoolean,
|
||
} from '../../../helpers';
|
||
|
||
// InputNumber 清空时 onChange 会收到 ''/null,parseInt 得到 NaN,
|
||
// String(NaN) 会把字面量 "NaN" 永久落库(同 SettingsMonitoring 的既有教训)。
|
||
// 统一经此转换:非法输入回退空串,后端 Atoi('') 按 0 处理,再由 setting 层的
|
||
// GetXxx() 回退默认值。
|
||
const toIntString = (value) => {
|
||
const parsed = parseInt(value, 10);
|
||
return Number.isNaN(parsed) ? '' : String(parsed);
|
||
};
|
||
|
||
const DEFAULTS = {
|
||
SkillMarketRequireAuth: false,
|
||
SkillInjectEnabled: false,
|
||
SkillInjectMaxSkills: '5',
|
||
SkillInjectMaxRunes: '3000',
|
||
SkillInjectCacheTTLSeconds: '60',
|
||
};
|
||
|
||
export default function SettingsSkillInject(props) {
|
||
const { t } = useTranslation();
|
||
const [loading, setLoading] = useState(false);
|
||
const [inputs, setInputs] = useState(DEFAULTS);
|
||
const [inputsRow, setInputsRow] = useState(DEFAULTS);
|
||
const refForm = useRef();
|
||
|
||
function onSubmit() {
|
||
const updateArray = compareObjects(inputs, inputsRow);
|
||
if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
|
||
const requestQueue = updateArray.map((item) => {
|
||
const raw = inputs[item.key];
|
||
const value = typeof raw === 'boolean' ? String(raw) : String(raw);
|
||
return API.put('/api/option/', { key: item.key, value });
|
||
});
|
||
setLoading(true);
|
||
Promise.all(requestQueue)
|
||
.then((res) => {
|
||
if (res.includes(undefined)) {
|
||
return showError(t('部分保存失败,请重试'));
|
||
}
|
||
showSuccess(t('保存成功'));
|
||
props.refresh();
|
||
})
|
||
.catch(() => showError(t('保存失败,请重试')))
|
||
.finally(() => setLoading(false));
|
||
}
|
||
|
||
useEffect(() => {
|
||
const currentInputs = {};
|
||
for (const key in DEFAULTS) {
|
||
const value = Object.prototype.hasOwnProperty.call(props.options, key)
|
||
? props.options[key]
|
||
: DEFAULTS[key];
|
||
currentInputs[key] =
|
||
typeof DEFAULTS[key] === 'boolean' ? toBoolean(value) : value;
|
||
}
|
||
setInputs(currentInputs);
|
||
setInputsRow(structuredClone(currentInputs));
|
||
refForm.current?.setValues(currentInputs);
|
||
}, [props.options]);
|
||
|
||
return (
|
||
<Spin spinning={loading}>
|
||
<Form
|
||
values={inputs}
|
||
getFormApi={(formAPI) => (refForm.current = formAPI)}
|
||
style={{ marginBottom: 15 }}
|
||
>
|
||
<Form.Section text={t('技能超市')}>
|
||
<Row>
|
||
<Col span={24}>
|
||
<Form.Switch
|
||
field='SkillMarketRequireAuth'
|
||
label={t('技能超市要求登录')}
|
||
extraText={t(
|
||
'默认关闭,未登录访客可浏览技能元数据(不含任何约束正文)。开启后页面与目录接口同时要求登录——只改前端路由不足以防护,技能目录是平台资产的完整清单。',
|
||
)}
|
||
onChange={(value) =>
|
||
setInputs({ ...inputs, SkillMarketRequireAuth: value })
|
||
}
|
||
/>
|
||
</Col>
|
||
</Row>
|
||
<Row>
|
||
<Button size='default' onClick={onSubmit}>
|
||
{t('保存')}
|
||
</Button>
|
||
</Row>
|
||
</Form.Section>
|
||
|
||
<Form.Section text={t('技能注入(网关直连)')}>
|
||
<Banner
|
||
type='warning'
|
||
closeIcon={null}
|
||
description={t(
|
||
'这是双开关:这里打开后,还需要在具体密钥上单独开启「技能注入」才会生效。注入文本按正常 prompt token 计费,归属密钥所属方。前置注入会改变 system 前缀,依赖 prompt 缓存的高频调用方可能出现缓存失效、成本上升。',
|
||
)}
|
||
style={{ marginBottom: 12 }}
|
||
/>
|
||
<Row>
|
||
<Col span={24}>
|
||
<Form.Switch
|
||
field='SkillInjectEnabled'
|
||
label={t('启用技能注入总开关')}
|
||
extraText={t(
|
||
'关闭时注入链路在入口处直接早退,热路径零开销、行为与未上线该功能完全一致。',
|
||
)}
|
||
onChange={(value) =>
|
||
setInputs({ ...inputs, SkillInjectEnabled: value })
|
||
}
|
||
/>
|
||
</Col>
|
||
</Row>
|
||
<Row gutter={16}>
|
||
<Col xs={24} sm={12} md={8}>
|
||
<Form.InputNumber
|
||
field='SkillInjectMaxSkills'
|
||
label={t('单次最多注入技能数')}
|
||
min={1}
|
||
max={20}
|
||
extraText={t(
|
||
'默认 5,与云脑侧口径一致;两条路径取值不同会导致同一用户拿到的技能数不一样,无法向用户解释。',
|
||
)}
|
||
onChange={(value) =>
|
||
setInputs({
|
||
...inputs,
|
||
SkillInjectMaxSkills: toIntString(value),
|
||
})
|
||
}
|
||
/>
|
||
</Col>
|
||
<Col xs={24} sm={12} md={8}>
|
||
<Form.InputNumber
|
||
field='SkillInjectMaxRunes'
|
||
label={t('注入文本长度上限(字符)')}
|
||
min={200}
|
||
max={20000}
|
||
extraText={t(
|
||
'默认 3000。超出预算的技能会被整条丢弃而不是截断——半截约束比没有约束更容易让模型跑偏。',
|
||
)}
|
||
onChange={(value) =>
|
||
setInputs({
|
||
...inputs,
|
||
SkillInjectMaxRunes: toIntString(value),
|
||
})
|
||
}
|
||
/>
|
||
</Col>
|
||
<Col xs={24} sm={12} md={8}>
|
||
<Form.InputNumber
|
||
field='SkillInjectCacheTTLSeconds'
|
||
label={t('技能集缓存时长(秒)')}
|
||
min={5}
|
||
max={600}
|
||
extraText={t(
|
||
'默认 60。多节点部署下这是唯一的失效手段,也就是「改了技能最长多久对新请求生效」的产品承诺,调大请同步修改面向用户的提示文案。',
|
||
)}
|
||
onChange={(value) =>
|
||
setInputs({
|
||
...inputs,
|
||
SkillInjectCacheTTLSeconds: toIntString(value),
|
||
})
|
||
}
|
||
/>
|
||
</Col>
|
||
</Row>
|
||
<Row>
|
||
<Button size='default' onClick={onSubmit}>
|
||
{t('保存')}
|
||
</Button>
|
||
</Row>
|
||
</Form.Section>
|
||
</Form>
|
||
</Spin>
|
||
);
|
||
}
|