settings.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. 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. getEmojiUrl={getEmojiUrl}
  166. onEmojiClick={(e) => {
  167. updateConfig((config) => (config.avatar = e.unified));
  168. setShowEmojiPicker(false);
  169. }}
  170. />
  171. }
  172. open={showEmojiPicker}
  173. >
  174. <div
  175. className={styles.avatar}
  176. onClick={() => setShowEmojiPicker(true)}
  177. >
  178. <Avatar role="user" />
  179. </div>
  180. </Popover>
  181. </SettingItem>
  182. <SettingItem
  183. title={Locale.Settings.Update.Version(currentId)}
  184. subTitle={
  185. checkingUpdate
  186. ? Locale.Settings.Update.IsChecking
  187. : hasNewVersion
  188. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  189. : Locale.Settings.Update.IsLatest
  190. }
  191. >
  192. {checkingUpdate ? (
  193. <div />
  194. ) : hasNewVersion ? (
  195. <Link href={UPDATE_URL} target="_blank" className="link">
  196. {Locale.Settings.Update.GoToUpdate}
  197. </Link>
  198. ) : (
  199. <IconButton
  200. icon={<ResetIcon></ResetIcon>}
  201. text={Locale.Settings.Update.CheckUpdate}
  202. onClick={() => checkUpdate(true)}
  203. />
  204. )}
  205. </SettingItem>
  206. <SettingItem title={Locale.Settings.SendKey}>
  207. <select
  208. value={config.submitKey}
  209. onChange={(e) => {
  210. updateConfig(
  211. (config) =>
  212. (config.submitKey = e.target.value as any as SubmitKey),
  213. );
  214. }}
  215. >
  216. {Object.values(SubmitKey).map((v) => (
  217. <option value={v} key={v}>
  218. {v}
  219. </option>
  220. ))}
  221. </select>
  222. </SettingItem>
  223. <ListItem>
  224. <div className={styles["settings-title"]}>
  225. {Locale.Settings.Theme}
  226. </div>
  227. <select
  228. value={config.theme}
  229. onChange={(e) => {
  230. updateConfig(
  231. (config) => (config.theme = e.target.value as any as Theme),
  232. );
  233. }}
  234. >
  235. {Object.values(Theme).map((v) => (
  236. <option value={v} key={v}>
  237. {v}
  238. </option>
  239. ))}
  240. </select>
  241. </ListItem>
  242. <SettingItem title={Locale.Settings.Lang.Name}>
  243. <select
  244. value={getLang()}
  245. onChange={(e) => {
  246. changeLang(e.target.value as any);
  247. }}
  248. >
  249. {AllLangs.map((lang) => (
  250. <option value={lang} key={lang}>
  251. {Locale.Settings.Lang.Options[lang]}
  252. </option>
  253. ))}
  254. </select>
  255. </SettingItem>
  256. <SettingItem
  257. title={Locale.Settings.FontSize.Title}
  258. subTitle={Locale.Settings.FontSize.SubTitle}
  259. >
  260. <input
  261. type="range"
  262. title={`${config.fontSize ?? 14}px`}
  263. value={config.fontSize}
  264. min="12"
  265. max="18"
  266. step="1"
  267. onChange={(e) =>
  268. updateConfig(
  269. (config) =>
  270. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  271. )
  272. }
  273. ></input>
  274. </SettingItem>
  275. <SettingItem title={Locale.Settings.TightBorder}>
  276. <input
  277. type="checkbox"
  278. checked={config.tightBorder}
  279. onChange={(e) =>
  280. updateConfig(
  281. (config) => (config.tightBorder = e.currentTarget.checked),
  282. )
  283. }
  284. ></input>
  285. </SettingItem>
  286. <SettingItem title={Locale.Settings.SendPreviewBubble}>
  287. <input
  288. type="checkbox"
  289. checked={config.sendPreviewBubble}
  290. onChange={(e) =>
  291. updateConfig(
  292. (config) =>
  293. (config.sendPreviewBubble = e.currentTarget.checked),
  294. )
  295. }
  296. ></input>
  297. </SettingItem>
  298. </List>
  299. <List>
  300. <SettingItem
  301. title={Locale.Settings.Prompt.Disable.Title}
  302. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  303. >
  304. <input
  305. type="checkbox"
  306. checked={config.disablePromptHint}
  307. onChange={(e) =>
  308. updateConfig(
  309. (config) =>
  310. (config.disablePromptHint = e.currentTarget.checked),
  311. )
  312. }
  313. ></input>
  314. </SettingItem>
  315. <SettingItem
  316. title={Locale.Settings.Prompt.List}
  317. subTitle={Locale.Settings.Prompt.ListCount(
  318. builtinCount,
  319. customCount,
  320. )}
  321. >
  322. <IconButton
  323. icon={<EditIcon />}
  324. text={Locale.Settings.Prompt.Edit}
  325. onClick={() => showToast(Locale.WIP)}
  326. />
  327. </SettingItem>
  328. </List>
  329. <List>
  330. {enabledAccessControl ? (
  331. <SettingItem
  332. title={Locale.Settings.AccessCode.Title}
  333. subTitle={Locale.Settings.AccessCode.SubTitle}
  334. >
  335. <PasswordInput
  336. value={accessStore.accessCode}
  337. type="text"
  338. placeholder={Locale.Settings.AccessCode.Placeholder}
  339. onChange={(e) => {
  340. accessStore.updateCode(e.currentTarget.value);
  341. }}
  342. />
  343. </SettingItem>
  344. ) : (
  345. <></>
  346. )}
  347. <SettingItem
  348. title={Locale.Settings.Token.Title}
  349. subTitle={Locale.Settings.Token.SubTitle}
  350. >
  351. <PasswordInput
  352. value={accessStore.token}
  353. type="text"
  354. placeholder={Locale.Settings.Token.Placeholder}
  355. onChange={(e) => {
  356. accessStore.updateToken(e.currentTarget.value);
  357. }}
  358. />
  359. </SettingItem>
  360. <SettingItem
  361. title={Locale.Settings.Usage.Title}
  362. subTitle={
  363. showUsage
  364. ? loadingUsage
  365. ? Locale.Settings.Usage.IsChecking
  366. : Locale.Settings.Usage.SubTitle(usage?.used ?? "[?]")
  367. : Locale.Settings.Usage.NoAccess
  368. }
  369. >
  370. {!showUsage || loadingUsage ? (
  371. <div />
  372. ) : (
  373. <IconButton
  374. icon={<ResetIcon></ResetIcon>}
  375. text={Locale.Settings.Usage.Check}
  376. onClick={checkUsage}
  377. />
  378. )}
  379. </SettingItem>
  380. <SettingItem
  381. title={Locale.Settings.HistoryCount.Title}
  382. subTitle={Locale.Settings.HistoryCount.SubTitle}
  383. >
  384. <input
  385. type="range"
  386. title={config.historyMessageCount.toString()}
  387. value={config.historyMessageCount}
  388. min="0"
  389. max="25"
  390. step="1"
  391. onChange={(e) =>
  392. updateConfig(
  393. (config) =>
  394. (config.historyMessageCount = e.target.valueAsNumber),
  395. )
  396. }
  397. ></input>
  398. </SettingItem>
  399. <SettingItem
  400. title={Locale.Settings.CompressThreshold.Title}
  401. subTitle={Locale.Settings.CompressThreshold.SubTitle}
  402. >
  403. <input
  404. type="number"
  405. min={500}
  406. max={4000}
  407. value={config.compressMessageLengthThreshold}
  408. onChange={(e) =>
  409. updateConfig(
  410. (config) =>
  411. (config.compressMessageLengthThreshold =
  412. e.currentTarget.valueAsNumber),
  413. )
  414. }
  415. ></input>
  416. </SettingItem>
  417. </List>
  418. <List>
  419. <SettingItem title={Locale.Settings.Model}>
  420. <select
  421. value={config.modelConfig.model}
  422. onChange={(e) => {
  423. updateConfig(
  424. (config) =>
  425. (config.modelConfig.model = ModalConfigValidator.model(
  426. e.currentTarget.value,
  427. )),
  428. );
  429. }}
  430. >
  431. {ALL_MODELS.map((v) => (
  432. <option value={v.name} key={v.name} disabled={!v.available}>
  433. {v.name}
  434. </option>
  435. ))}
  436. </select>
  437. </SettingItem>
  438. <SettingItem
  439. title={Locale.Settings.Temperature.Title}
  440. subTitle={Locale.Settings.Temperature.SubTitle}
  441. >
  442. <input
  443. type="range"
  444. value={config.modelConfig.temperature?.toFixed(1)}
  445. min="0"
  446. max="2"
  447. step="0.1"
  448. onChange={(e) => {
  449. updateConfig(
  450. (config) =>
  451. (config.modelConfig.temperature =
  452. ModalConfigValidator.temperature(
  453. e.currentTarget.valueAsNumber,
  454. )),
  455. );
  456. }}
  457. ></input>
  458. </SettingItem>
  459. <SettingItem
  460. title={Locale.Settings.MaxTokens.Title}
  461. subTitle={Locale.Settings.MaxTokens.SubTitle}
  462. >
  463. <input
  464. type="number"
  465. min={100}
  466. max={32000}
  467. value={config.modelConfig.max_tokens}
  468. onChange={(e) =>
  469. updateConfig(
  470. (config) =>
  471. (config.modelConfig.max_tokens =
  472. ModalConfigValidator.max_tokens(
  473. e.currentTarget.valueAsNumber,
  474. )),
  475. )
  476. }
  477. ></input>
  478. </SettingItem>
  479. <SettingItem
  480. title={Locale.Settings.PresencePenlty.Title}
  481. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  482. >
  483. <input
  484. type="range"
  485. value={config.modelConfig.presence_penalty?.toFixed(1)}
  486. min="-2"
  487. max="2"
  488. step="0.5"
  489. onChange={(e) => {
  490. updateConfig(
  491. (config) =>
  492. (config.modelConfig.presence_penalty =
  493. ModalConfigValidator.presence_penalty(
  494. e.currentTarget.valueAsNumber,
  495. )),
  496. );
  497. }}
  498. ></input>
  499. </SettingItem>
  500. </List>
  501. </div>
  502. </ErrorBoundary>
  503. );
  504. }