feat: add chat cache (#152)

This commit is contained in:
Buer
2024-04-16 10:36:18 +08:00
committed by GitHub
parent bbaa4eec4b
commit 3c7c13758b
23 changed files with 557 additions and 49 deletions

View File

@@ -30,7 +30,9 @@ const OperationSetting = () => {
ApproximateTokenEnabled: '',
RetryTimes: 0,
RetryCooldownSeconds: 0,
MjNotifyEnabled: ''
MjNotifyEnabled: '',
ChatCacheEnabled: '',
ChatCacheExpireMinute: 5
});
const [originInputs, setOriginInputs] = useState({});
let [loading, setLoading] = useState(false);
@@ -152,6 +154,11 @@ const OperationSetting = () => {
await updateOption('RetryCooldownSeconds', inputs.RetryCooldownSeconds);
}
break;
case 'other':
if (originInputs['ChatCacheExpireMinute'] !== inputs.ChatCacheExpireMinute) {
await updateOption('ChatCacheExpireMinute', inputs.ChatCacheExpireMinute);
}
break;
}
showSuccess('保存成功!');
@@ -292,7 +299,34 @@ const OperationSetting = () => {
label="Midjourney 允许回调会泄露服务器ip地址"
control={<Checkbox checked={inputs.MjNotifyEnabled === 'true'} onChange={handleInputChange} name="MjNotifyEnabled" />}
/>
<FormControlLabel
sx={{ marginLeft: '0px' }}
label="是否开启聊天缓存(如果没有启用Redis将会存储在数据库中)"
control={<Checkbox checked={inputs.ChatCacheEnabled === 'true'} onChange={handleInputChange} name="ChatCacheEnabled" />}
/>
</Stack>
<Stack direction={{ sm: 'column', md: 'row' }} spacing={{ xs: 3, sm: 2, md: 4 }}>
<FormControl>
<InputLabel htmlFor="ChatCacheExpireMinute">缓存时间(分钟)</InputLabel>
<OutlinedInput
id="ChatCacheExpireMinute"
name="ChatCacheExpireMinute"
value={inputs.ChatCacheExpireMinute}
onChange={handleInputChange}
label="缓存时间(分钟)"
placeholder="开启缓存时,数据缓存的时间"
disabled={loading}
/>
</FormControl>
</Stack>
<Button
variant="contained"
onClick={() => {
submitConfig('other').then();
}}
>
保存其他设置
</Button>
</Stack>
</SubCard>
<SubCard title="日志设置">

View File

@@ -17,6 +17,7 @@ import {
OutlinedInput,
InputAdornment,
Switch,
FormControlLabel,
FormHelperText
} from '@mui/material';
@@ -25,6 +26,7 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { renderQuotaWithPrompt, showSuccess, showError } from 'utils/common';
import { API } from 'utils/api';
import { useSelector } from 'react-redux';
require('dayjs/locale/zh-cn');
const validationSchema = Yup.object().shape({
@@ -40,12 +42,14 @@ const originInputs = {
name: '',
remain_quota: 0,
expired_time: -1,
unlimited_quota: false
unlimited_quota: false,
chat_cache: false
};
const EditModal = ({ open, tokenId, onCancel, onOk }) => {
const theme = useTheme();
const [inputs, setInputs] = useState(originInputs);
const siteInfo = useSelector((state) => state.siteInfo);
const submit = async (values, { setErrors, setStatus, setSubmitting }) => {
setSubmitting(true);
@@ -163,17 +167,22 @@ const EditModal = ({ open, tokenId, onCancel, onOk }) => {
)}
</FormControl>
)}
<Switch
checked={values.expired_time === -1}
onClick={() => {
if (values.expired_time === -1) {
setFieldValue('expired_time', Math.floor(Date.now() / 1000));
} else {
setFieldValue('expired_time', -1);
}
}}
/>{' '}
永不过期
<FormControlLabel
control={
<Switch
checked={values.expired_time === -1}
onClick={() => {
if (values.expired_time === -1) {
setFieldValue('expired_time', Math.floor(Date.now() / 1000));
} else {
setFieldValue('expired_time', -1);
}
}}
/>
}
label="永不过期"
/>
<FormControl fullWidth error={Boolean(touched.remain_quota && errors.remain_quota)} sx={{ ...theme.typography.otherInput }}>
<InputLabel htmlFor="channel-remain_quota-label">额度</InputLabel>
<OutlinedInput
@@ -195,13 +204,32 @@ const EditModal = ({ open, tokenId, onCancel, onOk }) => {
</FormHelperText>
)}
</FormControl>
<Switch
checked={values.unlimited_quota === true}
onClick={() => {
setFieldValue('unlimited_quota', !values.unlimited_quota);
}}
/>{' '}
无限额度
<FormControl fullWidth>
<FormControlLabel
control={
<Switch
checked={values.unlimited_quota === true}
onClick={() => {
setFieldValue('unlimited_quota', !values.unlimited_quota);
}}
/>
}
label="无限额度"
/>
{siteInfo.chat_cache_enabled && (
<FormControlLabel
control={
<Switch
checked={values.chat_cache}
onClick={() => {
setFieldValue('chat_cache', !values.chat_cache);
}}
/>
}
label="是否开启缓存(开启后,将会缓存聊天记录,以减少消费)"
/>
)}
</FormControl>
<DialogActions>
<Button onClick={onCancel}>取消</Button>
<Button disableElevation disabled={isSubmitting} type="submit" variant="contained" color="primary">