SettingsChats.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import {
  3. Banner,
  4. Button,
  5. Col,
  6. Form,
  7. Popconfirm,
  8. Row,
  9. Space,
  10. Spin,
  11. } from '@douyinfe/semi-ui';
  12. import {
  13. compareObjects,
  14. API,
  15. showError,
  16. showSuccess,
  17. showWarning,
  18. verifyJSON,
  19. verifyJSONPromise,
  20. } from '../../../helpers';
  21. import { useTranslation } from 'react-i18next';
  22. export default function SettingsChats(props) {
  23. const { t } = useTranslation();
  24. const [loading, setLoading] = useState(false);
  25. const [inputs, setInputs] = useState({
  26. Chats: '[]',
  27. });
  28. const refForm = useRef();
  29. const [inputsRow, setInputsRow] = useState(inputs);
  30. async function onSubmit() {
  31. try {
  32. console.log('Starting validation...');
  33. await refForm.current
  34. .validate()
  35. .then(() => {
  36. console.log('Validation passed');
  37. const updateArray = compareObjects(inputs, inputsRow);
  38. if (!updateArray.length)
  39. return showWarning(t('你似乎并没有修改什么'));
  40. const requestQueue = updateArray.map((item) => {
  41. let value = '';
  42. if (typeof inputs[item.key] === 'boolean') {
  43. value = String(inputs[item.key]);
  44. } else {
  45. value = inputs[item.key];
  46. }
  47. return API.put('/api/option/', {
  48. key: item.key,
  49. value,
  50. });
  51. });
  52. setLoading(true);
  53. Promise.all(requestQueue)
  54. .then((res) => {
  55. if (requestQueue.length === 1) {
  56. if (res.includes(undefined)) return;
  57. } else if (requestQueue.length > 1) {
  58. if (res.includes(undefined))
  59. return showError(t('部分保存失败,请重试'));
  60. }
  61. showSuccess(t('保存成功'));
  62. props.refresh();
  63. })
  64. .catch(() => {
  65. showError(t('保存失败,请重试'));
  66. })
  67. .finally(() => {
  68. setLoading(false);
  69. });
  70. })
  71. .catch((error) => {
  72. console.error('Validation failed:', error);
  73. showError(t('请检查输入'));
  74. });
  75. } catch (error) {
  76. showError(t('请检查输入'));
  77. console.error(error);
  78. }
  79. }
  80. async function resetModelRatio() {
  81. try {
  82. let res = await API.post(`/api/option/rest_model_ratio`);
  83. // return {success, message}
  84. if (res.data.success) {
  85. showSuccess(res.data.message);
  86. props.refresh();
  87. } else {
  88. showError(res.data.message);
  89. }
  90. } catch (error) {
  91. showError(error);
  92. }
  93. }
  94. useEffect(() => {
  95. const currentInputs = {};
  96. for (let key in props.options) {
  97. if (Object.keys(inputs).includes(key)) {
  98. if (key === 'Chats') {
  99. const obj = JSON.parse(props.options[key]);
  100. currentInputs[key] = JSON.stringify(obj, null, 2);
  101. } else {
  102. currentInputs[key] = props.options[key];
  103. }
  104. }
  105. }
  106. setInputs(currentInputs);
  107. setInputsRow(structuredClone(currentInputs));
  108. refForm.current.setValues(currentInputs);
  109. }, [props.options]);
  110. return (
  111. <Spin spinning={loading}>
  112. <Form
  113. values={inputs}
  114. getFormApi={(formAPI) => (refForm.current = formAPI)}
  115. style={{ marginBottom: 15 }}
  116. >
  117. <Form.Section text={t('令牌聊天设置')}>
  118. <Banner
  119. type='warning'
  120. description={t(
  121. '必须将上方聊天链接全部设置为空,才能使用下方聊天设置功能',
  122. )}
  123. />
  124. <Banner
  125. type='info'
  126. description={t(
  127. '链接中的{key}将自动替换为sk-xxxx,{address}将自动替换为系统设置的服务器地址,末尾不带/和/v1',
  128. )}
  129. />
  130. <Form.TextArea
  131. label={t('聊天配置')}
  132. extraText={''}
  133. placeholder={t('为一个 JSON 文本')}
  134. field={'Chats'}
  135. autosize={{ minRows: 6, maxRows: 12 }}
  136. trigger='blur'
  137. stopValidateWithError
  138. rules={[
  139. {
  140. validator: (rule, value) => {
  141. return verifyJSON(value);
  142. },
  143. message: t('不是合法的 JSON 字符串'),
  144. },
  145. ]}
  146. onChange={(value) =>
  147. setInputs({
  148. ...inputs,
  149. Chats: value,
  150. })
  151. }
  152. />
  153. </Form.Section>
  154. </Form>
  155. <Space>
  156. <Button onClick={onSubmit}>{t('保存聊天设置')}</Button>
  157. </Space>
  158. </Spin>
  159. );
  160. }