settings.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. import { useState, useEffect, useMemo, HTMLProps } from "react";
  2. import EmojiPicker, { Theme as EmojiTheme } from "emoji-picker-react";
  3. import styles from "./settings.module.scss";
  4. import ResetIcon from "../icons/reload.svg";
  5. import CloseIcon from "../icons/close.svg";
  6. import ClearIcon from "../icons/clear.svg";
  7. import EditIcon from "../icons/edit.svg";
  8. import EyeIcon from "../icons/eye.svg";
  9. import EyeOffIcon from "../icons/eye-off.svg";
  10. import { List, ListItem, Popover, showToast } from "./ui-lib";
  11. import { IconButton } from "./button";
  12. import {
  13. SubmitKey,
  14. useChatStore,
  15. Theme,
  16. ALL_MODELS,
  17. useUpdateStore,
  18. useAccessStore,
  19. ModalConfigValidator,
  20. } from "../store";
  21. import { Avatar } from "./chat";
  22. import Locale, { AllLangs, changeLang, getLang } from "../locales";
  23. import { getCurrentVersion, getEmojiUrl } from "../utils";
  24. import Link from "next/link";
  25. import { UPDATE_URL } from "../constant";
  26. import { SearchService, usePromptStore } from "../store/prompt";
  27. import { requestUsage } from "../requests";
  28. import { ErrorBoundary } from "./error";
  29. import { InputRange } from "./input-range";
  30. function SettingItem(props: {
  31. title: string;
  32. subTitle?: string;
  33. children: JSX.Element;
  34. }) {
  35. return (
  36. <ListItem>
  37. <div className={styles["settings-title"]}>
  38. <div>{props.title}</div>
  39. {props.subTitle && (
  40. <div className={styles["settings-sub-title"]}>{props.subTitle}</div>
  41. )}
  42. </div>
  43. {props.children}
  44. </ListItem>
  45. );
  46. }
  47. function PasswordInput(props: HTMLProps<HTMLInputElement>) {
  48. const [visible, setVisible] = useState(false);
  49. function changeVisibility() {
  50. setVisible(!visible);
  51. }
  52. return (
  53. <div className={styles["password-input"]}>
  54. <IconButton
  55. icon={visible ? <EyeIcon /> : <EyeOffIcon />}
  56. onClick={changeVisibility}
  57. className={styles["password-eye"]}
  58. />
  59. <input {...props} type={visible ? "text" : "password"} />
  60. </div>
  61. );
  62. }
  63. export function Settings(props: { closeSettings: () => void }) {
  64. const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  65. const [config, updateConfig, resetConfig, clearAllData, clearSessions] =
  66. useChatStore((state) => [
  67. state.config,
  68. state.updateConfig,
  69. state.resetConfig,
  70. state.clearAllData,
  71. state.clearSessions,
  72. ]);
  73. const updateStore = useUpdateStore();
  74. const [checkingUpdate, setCheckingUpdate] = useState(false);
  75. const currentId = getCurrentVersion();
  76. const remoteId = updateStore.remoteId;
  77. const hasNewVersion = currentId !== remoteId;
  78. function checkUpdate(force = false) {
  79. setCheckingUpdate(true);
  80. updateStore.getLatestCommitId(force).then(() => {
  81. setCheckingUpdate(false);
  82. });
  83. }
  84. const [usage, setUsage] = useState<{
  85. used?: number;
  86. subscription?: number;
  87. }>();
  88. const [loadingUsage, setLoadingUsage] = useState(false);
  89. function checkUsage() {
  90. setLoadingUsage(true);
  91. requestUsage()
  92. .then((res) => setUsage(res))
  93. .finally(() => {
  94. setLoadingUsage(false);
  95. });
  96. }
  97. const accessStore = useAccessStore();
  98. const enabledAccessControl = useMemo(
  99. () => accessStore.enabledAccessControl(),
  100. // eslint-disable-next-line react-hooks/exhaustive-deps
  101. [],
  102. );
  103. const promptStore = usePromptStore();
  104. const builtinCount = SearchService.count.builtin;
  105. const customCount = promptStore.prompts.size ?? 0;
  106. const showUsage = !!accessStore.token || !!accessStore.accessCode;
  107. useEffect(() => {
  108. checkUpdate();
  109. showUsage && checkUsage();
  110. // eslint-disable-next-line react-hooks/exhaustive-deps
  111. }, []);
  112. return (
  113. <ErrorBoundary>
  114. <div className={styles["window-header"]}>
  115. <div className={styles["window-header-title"]}>
  116. <div className={styles["window-header-main-title"]}>
  117. {Locale.Settings.Title}
  118. </div>
  119. <div className={styles["window-header-sub-title"]}>
  120. {Locale.Settings.SubTitle}
  121. </div>
  122. </div>
  123. <div className={styles["window-actions"]}>
  124. <div className={styles["window-action-button"]}>
  125. <IconButton
  126. icon={<ClearIcon />}
  127. onClick={clearSessions}
  128. bordered
  129. title={Locale.Settings.Actions.ClearAll}
  130. />
  131. </div>
  132. <div className={styles["window-action-button"]}>
  133. <IconButton
  134. icon={<ResetIcon />}
  135. onClick={resetConfig}
  136. bordered
  137. title={Locale.Settings.Actions.ResetAll}
  138. />
  139. </div>
  140. <div className={styles["window-action-button"]}>
  141. <IconButton
  142. icon={<CloseIcon />}
  143. onClick={props.closeSettings}
  144. bordered
  145. title={Locale.Settings.Actions.Close}
  146. />
  147. </div>
  148. </div>
  149. </div>
  150. <div className={styles["settings"]}>
  151. <List>
  152. <SettingItem title={Locale.Settings.Avatar}>
  153. <Popover
  154. onClose={() => setShowEmojiPicker(false)}
  155. content={
  156. <EmojiPicker
  157. lazyLoadEmojis
  158. theme={EmojiTheme.AUTO}
  159. getEmojiUrl={getEmojiUrl}
  160. onEmojiClick={(e) => {
  161. updateConfig((config) => (config.avatar = e.unified));
  162. setShowEmojiPicker(false);
  163. }}
  164. />
  165. }
  166. open={showEmojiPicker}
  167. >
  168. <div
  169. className={styles.avatar}
  170. onClick={() => setShowEmojiPicker(true)}
  171. >
  172. <Avatar role="user" />
  173. </div>
  174. </Popover>
  175. </SettingItem>
  176. <SettingItem
  177. title={Locale.Settings.Update.Version(currentId)}
  178. subTitle={
  179. checkingUpdate
  180. ? Locale.Settings.Update.IsChecking
  181. : hasNewVersion
  182. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  183. : Locale.Settings.Update.IsLatest
  184. }
  185. >
  186. {checkingUpdate ? (
  187. <div />
  188. ) : hasNewVersion ? (
  189. <Link href={UPDATE_URL} target="_blank" className="link">
  190. {Locale.Settings.Update.GoToUpdate}
  191. </Link>
  192. ) : (
  193. <IconButton
  194. icon={<ResetIcon></ResetIcon>}
  195. text={Locale.Settings.Update.CheckUpdate}
  196. onClick={() => checkUpdate(true)}
  197. />
  198. )}
  199. </SettingItem>
  200. <SettingItem title={Locale.Settings.SendKey}>
  201. <select
  202. value={config.submitKey}
  203. onChange={(e) => {
  204. updateConfig(
  205. (config) =>
  206. (config.submitKey = e.target.value as any as SubmitKey),
  207. );
  208. }}
  209. >
  210. {Object.values(SubmitKey).map((v) => (
  211. <option value={v} key={v}>
  212. {v}
  213. </option>
  214. ))}
  215. </select>
  216. </SettingItem>
  217. <ListItem>
  218. <div className={styles["settings-title"]}>
  219. {Locale.Settings.Theme}
  220. </div>
  221. <select
  222. value={config.theme}
  223. onChange={(e) => {
  224. updateConfig(
  225. (config) => (config.theme = e.target.value as any as Theme),
  226. );
  227. }}
  228. >
  229. {Object.values(Theme).map((v) => (
  230. <option value={v} key={v}>
  231. {v}
  232. </option>
  233. ))}
  234. </select>
  235. </ListItem>
  236. <SettingItem title={Locale.Settings.Lang.Name}>
  237. <select
  238. value={getLang()}
  239. onChange={(e) => {
  240. changeLang(e.target.value as any);
  241. }}
  242. >
  243. {AllLangs.map((lang) => (
  244. <option value={lang} key={lang}>
  245. {Locale.Settings.Lang.Options[lang]}
  246. </option>
  247. ))}
  248. </select>
  249. </SettingItem>
  250. <SettingItem
  251. title={Locale.Settings.FontSize.Title}
  252. subTitle={Locale.Settings.FontSize.SubTitle}
  253. >
  254. <InputRange
  255. title={`${config.fontSize ?? 14}px`}
  256. value={config.fontSize}
  257. min="12"
  258. max="18"
  259. step="1"
  260. onChange={(e) =>
  261. updateConfig(
  262. (config) =>
  263. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  264. )
  265. }
  266. ></InputRange>
  267. </SettingItem>
  268. <SettingItem title={Locale.Settings.TightBorder}>
  269. <input
  270. type="checkbox"
  271. checked={config.tightBorder}
  272. onChange={(e) =>
  273. updateConfig(
  274. (config) => (config.tightBorder = e.currentTarget.checked),
  275. )
  276. }
  277. ></input>
  278. </SettingItem>
  279. <SettingItem title={Locale.Settings.SendPreviewBubble}>
  280. <input
  281. type="checkbox"
  282. checked={config.sendPreviewBubble}
  283. onChange={(e) =>
  284. updateConfig(
  285. (config) =>
  286. (config.sendPreviewBubble = e.currentTarget.checked),
  287. )
  288. }
  289. ></input>
  290. </SettingItem>
  291. </List>
  292. <List>
  293. <SettingItem
  294. title={Locale.Settings.Prompt.Disable.Title}
  295. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  296. >
  297. <input
  298. type="checkbox"
  299. checked={config.disablePromptHint}
  300. onChange={(e) =>
  301. updateConfig(
  302. (config) =>
  303. (config.disablePromptHint = e.currentTarget.checked),
  304. )
  305. }
  306. ></input>
  307. </SettingItem>
  308. <SettingItem
  309. title={Locale.Settings.Prompt.List}
  310. subTitle={Locale.Settings.Prompt.ListCount(
  311. builtinCount,
  312. customCount,
  313. )}
  314. >
  315. <IconButton
  316. icon={<EditIcon />}
  317. text={Locale.Settings.Prompt.Edit}
  318. onClick={() => showToast(Locale.WIP)}
  319. />
  320. </SettingItem>
  321. </List>
  322. <List>
  323. {enabledAccessControl ? (
  324. <SettingItem
  325. title={Locale.Settings.AccessCode.Title}
  326. subTitle={Locale.Settings.AccessCode.SubTitle}
  327. >
  328. <PasswordInput
  329. value={accessStore.accessCode}
  330. type="text"
  331. placeholder={Locale.Settings.AccessCode.Placeholder}
  332. onChange={(e) => {
  333. accessStore.updateCode(e.currentTarget.value);
  334. }}
  335. />
  336. </SettingItem>
  337. ) : (
  338. <></>
  339. )}
  340. <SettingItem
  341. title={Locale.Settings.Token.Title}
  342. subTitle={Locale.Settings.Token.SubTitle}
  343. >
  344. <PasswordInput
  345. value={accessStore.token}
  346. type="text"
  347. placeholder={Locale.Settings.Token.Placeholder}
  348. onChange={(e) => {
  349. accessStore.updateToken(e.currentTarget.value);
  350. }}
  351. />
  352. </SettingItem>
  353. <SettingItem
  354. title={Locale.Settings.Usage.Title}
  355. subTitle={
  356. showUsage
  357. ? loadingUsage
  358. ? Locale.Settings.Usage.IsChecking
  359. : Locale.Settings.Usage.SubTitle(
  360. usage?.used ?? "[?]",
  361. usage?.subscription ?? "[?]",
  362. )
  363. : Locale.Settings.Usage.NoAccess
  364. }
  365. >
  366. {!showUsage || loadingUsage ? (
  367. <div />
  368. ) : (
  369. <IconButton
  370. icon={<ResetIcon></ResetIcon>}
  371. text={Locale.Settings.Usage.Check}
  372. onClick={checkUsage}
  373. />
  374. )}
  375. </SettingItem>
  376. <SettingItem
  377. title={Locale.Settings.HistoryCount.Title}
  378. subTitle={Locale.Settings.HistoryCount.SubTitle}
  379. >
  380. <InputRange
  381. title={config.historyMessageCount.toString()}
  382. value={config.historyMessageCount}
  383. min="0"
  384. max="25"
  385. step="1"
  386. onChange={(e) =>
  387. updateConfig(
  388. (config) =>
  389. (config.historyMessageCount = e.target.valueAsNumber),
  390. )
  391. }
  392. ></InputRange>
  393. </SettingItem>
  394. <SettingItem
  395. title={Locale.Settings.CompressThreshold.Title}
  396. subTitle={Locale.Settings.CompressThreshold.SubTitle}
  397. >
  398. <input
  399. type="number"
  400. min={500}
  401. max={4000}
  402. value={config.compressMessageLengthThreshold}
  403. onChange={(e) =>
  404. updateConfig(
  405. (config) =>
  406. (config.compressMessageLengthThreshold =
  407. e.currentTarget.valueAsNumber),
  408. )
  409. }
  410. ></input>
  411. </SettingItem>
  412. </List>
  413. <List>
  414. <SettingItem title={Locale.Settings.Model}>
  415. <select
  416. value={config.modelConfig.model}
  417. onChange={(e) => {
  418. updateConfig(
  419. (config) =>
  420. (config.modelConfig.model = ModalConfigValidator.model(
  421. e.currentTarget.value,
  422. )),
  423. );
  424. }}
  425. >
  426. {ALL_MODELS.map((v) => (
  427. <option value={v.name} key={v.name} disabled={!v.available}>
  428. {v.name}
  429. </option>
  430. ))}
  431. </select>
  432. </SettingItem>
  433. <SettingItem
  434. title={Locale.Settings.Temperature.Title}
  435. subTitle={Locale.Settings.Temperature.SubTitle}
  436. >
  437. <InputRange
  438. value={config.modelConfig.temperature?.toFixed(1)}
  439. min="0"
  440. max="2"
  441. step="0.1"
  442. onChange={(e) => {
  443. updateConfig(
  444. (config) =>
  445. (config.modelConfig.temperature =
  446. ModalConfigValidator.temperature(
  447. e.currentTarget.valueAsNumber,
  448. )),
  449. );
  450. }}
  451. ></InputRange>
  452. </SettingItem>
  453. <SettingItem
  454. title={Locale.Settings.MaxTokens.Title}
  455. subTitle={Locale.Settings.MaxTokens.SubTitle}
  456. >
  457. <input
  458. type="number"
  459. min={100}
  460. max={32000}
  461. value={config.modelConfig.max_tokens}
  462. onChange={(e) =>
  463. updateConfig(
  464. (config) =>
  465. (config.modelConfig.max_tokens =
  466. ModalConfigValidator.max_tokens(
  467. e.currentTarget.valueAsNumber,
  468. )),
  469. )
  470. }
  471. ></input>
  472. </SettingItem>
  473. <SettingItem
  474. title={Locale.Settings.PresencePenlty.Title}
  475. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  476. >
  477. <InputRange
  478. value={config.modelConfig.presence_penalty?.toFixed(1)}
  479. min="-2"
  480. max="2"
  481. step="0.5"
  482. onChange={(e) => {
  483. updateConfig(
  484. (config) =>
  485. (config.modelConfig.presence_penalty =
  486. ModalConfigValidator.presence_penalty(
  487. e.currentTarget.valueAsNumber,
  488. )),
  489. );
  490. }}
  491. ></InputRange>
  492. </SettingItem>
  493. </List>
  494. </div>
  495. </ErrorBoundary>
  496. );
  497. }