settings.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. subscription?: number;
  86. }>();
  87. const [loadingUsage, setLoadingUsage] = useState(false);
  88. function checkUsage() {
  89. setLoadingUsage(true);
  90. requestUsage()
  91. .then((res) => setUsage(res))
  92. .finally(() => {
  93. setLoadingUsage(false);
  94. });
  95. }
  96. const accessStore = useAccessStore();
  97. const enabledAccessControl = useMemo(
  98. () => accessStore.enabledAccessControl(),
  99. // eslint-disable-next-line react-hooks/exhaustive-deps
  100. [],
  101. );
  102. const promptStore = usePromptStore();
  103. const builtinCount = SearchService.count.builtin;
  104. const customCount = promptStore.prompts.size ?? 0;
  105. const showUsage = !!accessStore.token || !!accessStore.accessCode;
  106. useEffect(() => {
  107. checkUpdate();
  108. showUsage && checkUsage();
  109. // eslint-disable-next-line react-hooks/exhaustive-deps
  110. }, []);
  111. return (
  112. <ErrorBoundary>
  113. <div className={styles["window-header"]}>
  114. <div className={styles["window-header-title"]}>
  115. <div className={styles["window-header-main-title"]}>
  116. {Locale.Settings.Title}
  117. </div>
  118. <div className={styles["window-header-sub-title"]}>
  119. {Locale.Settings.SubTitle}
  120. </div>
  121. </div>
  122. <div className={styles["window-actions"]}>
  123. <div className={styles["window-action-button"]}>
  124. <IconButton
  125. icon={<ClearIcon />}
  126. onClick={clearSessions}
  127. bordered
  128. title={Locale.Settings.Actions.ClearAll}
  129. />
  130. </div>
  131. <div className={styles["window-action-button"]}>
  132. <IconButton
  133. icon={<ResetIcon />}
  134. onClick={resetConfig}
  135. bordered
  136. title={Locale.Settings.Actions.ResetAll}
  137. />
  138. </div>
  139. <div className={styles["window-action-button"]}>
  140. <IconButton
  141. icon={<CloseIcon />}
  142. onClick={props.closeSettings}
  143. bordered
  144. title={Locale.Settings.Actions.Close}
  145. />
  146. </div>
  147. </div>
  148. </div>
  149. <div className={styles["settings"]}>
  150. <List>
  151. <SettingItem title={Locale.Settings.Avatar}>
  152. <Popover
  153. onClose={() => setShowEmojiPicker(false)}
  154. content={
  155. <EmojiPicker
  156. lazyLoadEmojis
  157. theme={EmojiTheme.AUTO}
  158. getEmojiUrl={getEmojiUrl}
  159. onEmojiClick={(e) => {
  160. updateConfig((config) => (config.avatar = e.unified));
  161. setShowEmojiPicker(false);
  162. }}
  163. />
  164. }
  165. open={showEmojiPicker}
  166. >
  167. <div
  168. className={styles.avatar}
  169. onClick={() => setShowEmojiPicker(true)}
  170. >
  171. <Avatar role="user" />
  172. </div>
  173. </Popover>
  174. </SettingItem>
  175. <SettingItem
  176. title={Locale.Settings.Update.Version(currentId)}
  177. subTitle={
  178. checkingUpdate
  179. ? Locale.Settings.Update.IsChecking
  180. : hasNewVersion
  181. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  182. : Locale.Settings.Update.IsLatest
  183. }
  184. >
  185. {checkingUpdate ? (
  186. <div />
  187. ) : hasNewVersion ? (
  188. <Link href={UPDATE_URL} target="_blank" className="link">
  189. {Locale.Settings.Update.GoToUpdate}
  190. </Link>
  191. ) : (
  192. <IconButton
  193. icon={<ResetIcon></ResetIcon>}
  194. text={Locale.Settings.Update.CheckUpdate}
  195. onClick={() => checkUpdate(true)}
  196. />
  197. )}
  198. </SettingItem>
  199. <SettingItem title={Locale.Settings.SendKey}>
  200. <select
  201. value={config.submitKey}
  202. onChange={(e) => {
  203. updateConfig(
  204. (config) =>
  205. (config.submitKey = e.target.value as any as SubmitKey),
  206. );
  207. }}
  208. >
  209. {Object.values(SubmitKey).map((v) => (
  210. <option value={v} key={v}>
  211. {v}
  212. </option>
  213. ))}
  214. </select>
  215. </SettingItem>
  216. <ListItem>
  217. <div className={styles["settings-title"]}>
  218. {Locale.Settings.Theme}
  219. </div>
  220. <select
  221. value={config.theme}
  222. onChange={(e) => {
  223. updateConfig(
  224. (config) => (config.theme = e.target.value as any as Theme),
  225. );
  226. }}
  227. >
  228. {Object.values(Theme).map((v) => (
  229. <option value={v} key={v}>
  230. {v}
  231. </option>
  232. ))}
  233. </select>
  234. </ListItem>
  235. <SettingItem title={Locale.Settings.Lang.Name}>
  236. <select
  237. value={getLang()}
  238. onChange={(e) => {
  239. changeLang(e.target.value as any);
  240. }}
  241. >
  242. {AllLangs.map((lang) => (
  243. <option value={lang} key={lang}>
  244. {Locale.Settings.Lang.Options[lang]}
  245. </option>
  246. ))}
  247. </select>
  248. </SettingItem>
  249. <SettingItem
  250. title={Locale.Settings.FontSize.Title}
  251. subTitle={Locale.Settings.FontSize.SubTitle}
  252. >
  253. <input
  254. type="range"
  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. ></input>
  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. <input
  381. type="range"
  382. title={config.historyMessageCount.toString()}
  383. value={config.historyMessageCount}
  384. min="0"
  385. max="25"
  386. step="1"
  387. onChange={(e) =>
  388. updateConfig(
  389. (config) =>
  390. (config.historyMessageCount = e.target.valueAsNumber),
  391. )
  392. }
  393. ></input>
  394. </SettingItem>
  395. <SettingItem
  396. title={Locale.Settings.CompressThreshold.Title}
  397. subTitle={Locale.Settings.CompressThreshold.SubTitle}
  398. >
  399. <input
  400. type="number"
  401. min={500}
  402. max={4000}
  403. value={config.compressMessageLengthThreshold}
  404. onChange={(e) =>
  405. updateConfig(
  406. (config) =>
  407. (config.compressMessageLengthThreshold =
  408. e.currentTarget.valueAsNumber),
  409. )
  410. }
  411. ></input>
  412. </SettingItem>
  413. </List>
  414. <List>
  415. <SettingItem title={Locale.Settings.Model}>
  416. <select
  417. value={config.modelConfig.model}
  418. onChange={(e) => {
  419. updateConfig(
  420. (config) =>
  421. (config.modelConfig.model = ModalConfigValidator.model(
  422. e.currentTarget.value,
  423. )),
  424. );
  425. }}
  426. >
  427. {ALL_MODELS.map((v) => (
  428. <option value={v.name} key={v.name} disabled={!v.available}>
  429. {v.name}
  430. </option>
  431. ))}
  432. </select>
  433. </SettingItem>
  434. <SettingItem
  435. title={Locale.Settings.Temperature.Title}
  436. subTitle={Locale.Settings.Temperature.SubTitle}
  437. >
  438. <input
  439. type="range"
  440. value={config.modelConfig.temperature?.toFixed(1)}
  441. min="0"
  442. max="2"
  443. step="0.1"
  444. onChange={(e) => {
  445. updateConfig(
  446. (config) =>
  447. (config.modelConfig.temperature =
  448. ModalConfigValidator.temperature(
  449. e.currentTarget.valueAsNumber,
  450. )),
  451. );
  452. }}
  453. ></input>
  454. </SettingItem>
  455. <SettingItem
  456. title={Locale.Settings.MaxTokens.Title}
  457. subTitle={Locale.Settings.MaxTokens.SubTitle}
  458. >
  459. <input
  460. type="number"
  461. min={100}
  462. max={32000}
  463. value={config.modelConfig.max_tokens}
  464. onChange={(e) =>
  465. updateConfig(
  466. (config) =>
  467. (config.modelConfig.max_tokens =
  468. ModalConfigValidator.max_tokens(
  469. e.currentTarget.valueAsNumber,
  470. )),
  471. )
  472. }
  473. ></input>
  474. </SettingItem>
  475. <SettingItem
  476. title={Locale.Settings.PresencePenlty.Title}
  477. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  478. >
  479. <input
  480. type="range"
  481. value={config.modelConfig.presence_penalty?.toFixed(1)}
  482. min="-2"
  483. max="2"
  484. step="0.5"
  485. onChange={(e) => {
  486. updateConfig(
  487. (config) =>
  488. (config.modelConfig.presence_penalty =
  489. ModalConfigValidator.presence_penalty(
  490. e.currentTarget.valueAsNumber,
  491. )),
  492. );
  493. }}
  494. ></input>
  495. </SettingItem>
  496. </List>
  497. </div>
  498. </ErrorBoundary>
  499. );
  500. }