settings.tsx 16 KB

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