settings.tsx 16 KB

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