settings.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. } from "../store";
  20. import { Avatar } from "./chat";
  21. import Locale, { AllLangs, changeLang, getLang } from "../locales";
  22. import { getCurrentVersion } from "../utils";
  23. import Link from "next/link";
  24. import { UPDATE_URL } from "../constant";
  25. import { SearchService, usePromptStore } from "../store/prompt";
  26. import { requestUsage } from "../requests";
  27. function SettingItem(props: {
  28. title: string;
  29. subTitle?: string;
  30. children: JSX.Element;
  31. }) {
  32. return (
  33. <ListItem>
  34. <div className={styles["settings-title"]}>
  35. <div>{props.title}</div>
  36. {props.subTitle && (
  37. <div className={styles["settings-sub-title"]}>{props.subTitle}</div>
  38. )}
  39. </div>
  40. {props.children}
  41. </ListItem>
  42. );
  43. }
  44. function PasswordInput(props: HTMLProps<HTMLInputElement>) {
  45. const [visible, setVisible] = useState(false);
  46. function changeVisibility() {
  47. setVisible(!visible);
  48. }
  49. return (
  50. <span style={{ display: "flex", justifyContent: "end" }}>
  51. <input
  52. {...props}
  53. style={{ minWidth: "150px" }}
  54. type={visible ? "text" : "password"}
  55. />
  56. <IconButton
  57. icon={visible ? <EyeIcon /> : <EyeOffIcon />}
  58. onClick={changeVisibility}
  59. />
  60. </span>
  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. }>();
  87. const [loadingUsage, setLoadingUsage] = useState(false);
  88. function checkUsage() {
  89. setLoadingUsage(true);
  90. requestUsage()
  91. .then((res) =>
  92. setUsage({
  93. used: res,
  94. }),
  95. )
  96. .finally(() => {
  97. setLoadingUsage(false);
  98. });
  99. }
  100. useEffect(() => {
  101. checkUpdate();
  102. checkUsage();
  103. }, []);
  104. const accessStore = useAccessStore();
  105. const enabledAccessControl = useMemo(
  106. () => accessStore.enabledAccessControl(),
  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. <>
  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="2"
  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 = e.currentTarget.value),
  425. );
  426. }}
  427. >
  428. {ALL_MODELS.map((v) => (
  429. <option value={v.name} key={v.name} disabled={!v.available}>
  430. {v.name}
  431. </option>
  432. ))}
  433. </select>
  434. </SettingItem>
  435. <SettingItem
  436. title={Locale.Settings.Temperature.Title}
  437. subTitle={Locale.Settings.Temperature.SubTitle}
  438. >
  439. <input
  440. type="range"
  441. value={config.modelConfig.temperature.toFixed(1)}
  442. min="0"
  443. max="2"
  444. step="0.1"
  445. onChange={(e) => {
  446. updateConfig(
  447. (config) =>
  448. (config.modelConfig.temperature =
  449. e.currentTarget.valueAsNumber),
  450. );
  451. }}
  452. ></input>
  453. </SettingItem>
  454. <SettingItem
  455. title={Locale.Settings.MaxTokens.Title}
  456. subTitle={Locale.Settings.MaxTokens.SubTitle}
  457. >
  458. <input
  459. type="number"
  460. min={100}
  461. max={4096}
  462. value={config.modelConfig.max_tokens}
  463. onChange={(e) =>
  464. updateConfig(
  465. (config) =>
  466. (config.modelConfig.max_tokens =
  467. e.currentTarget.valueAsNumber),
  468. )
  469. }
  470. ></input>
  471. </SettingItem>
  472. <SettingItem
  473. title={Locale.Settings.PresencePenlty.Title}
  474. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  475. >
  476. <input
  477. type="range"
  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. e.currentTarget.valueAsNumber),
  487. );
  488. }}
  489. ></input>
  490. </SettingItem>
  491. </List>
  492. </div>
  493. </>
  494. );
  495. }