settings.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import { useState, useEffect, useRef, useMemo } 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 { List, ListItem, Popover, showToast } from "./ui-lib";
  9. import { IconButton } from "./button";
  10. import {
  11. SubmitKey,
  12. useChatStore,
  13. Theme,
  14. ALL_MODELS,
  15. useUpdateStore,
  16. useAccessStore,
  17. } from "../store";
  18. import { Avatar, PromptHints } from "./home";
  19. import Locale, { changeLang, getLang } from "../locales";
  20. import { getCurrentCommitId } from "../utils";
  21. import Link from "next/link";
  22. import { UPDATE_URL } from "../constant";
  23. import { SearchService, usePromptStore } from "../store/prompt";
  24. function SettingItem(props: {
  25. title: string;
  26. subTitle?: string;
  27. children: JSX.Element;
  28. }) {
  29. return (
  30. <ListItem>
  31. <div className={styles["settings-title"]}>
  32. <div>{props.title}</div>
  33. {props.subTitle && (
  34. <div className={styles["settings-sub-title"]}>{props.subTitle}</div>
  35. )}
  36. </div>
  37. {props.children}
  38. </ListItem>
  39. );
  40. }
  41. export function Settings(props: { closeSettings: () => void }) {
  42. const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  43. const [config, updateConfig, resetConfig, clearAllData] = useChatStore(
  44. (state) => [
  45. state.config,
  46. state.updateConfig,
  47. state.resetConfig,
  48. state.clearAllData,
  49. ]
  50. );
  51. const updateStore = useUpdateStore();
  52. const [checkingUpdate, setCheckingUpdate] = useState(false);
  53. const currentId = getCurrentCommitId();
  54. const remoteId = updateStore.remoteId;
  55. const hasNewVersion = currentId !== remoteId;
  56. function checkUpdate(force = false) {
  57. setCheckingUpdate(true);
  58. updateStore.getLatestCommitId(force).then(() => {
  59. setCheckingUpdate(false);
  60. });
  61. }
  62. useEffect(() => {
  63. checkUpdate();
  64. }, []);
  65. const accessStore = useAccessStore();
  66. const enabledAccessControl = useMemo(
  67. () => accessStore.enabledAccessControl(),
  68. []
  69. );
  70. const promptStore = usePromptStore();
  71. const builtinCount = SearchService.count.builtin;
  72. const customCount = promptStore.prompts.size ?? 0;
  73. return (
  74. <>
  75. <div className={styles["window-header"]}>
  76. <div className={styles["window-header-title"]}>
  77. <div className={styles["window-header-main-title"]}>
  78. {Locale.Settings.Title}
  79. </div>
  80. <div className={styles["window-header-sub-title"]}>
  81. {Locale.Settings.SubTitle}
  82. </div>
  83. </div>
  84. <div className={styles["window-actions"]}>
  85. <div className={styles["window-action-button"]}>
  86. <IconButton
  87. icon={<ClearIcon />}
  88. onClick={clearAllData}
  89. bordered
  90. title={Locale.Settings.Actions.ClearAll}
  91. />
  92. </div>
  93. <div className={styles["window-action-button"]}>
  94. <IconButton
  95. icon={<ResetIcon />}
  96. onClick={resetConfig}
  97. bordered
  98. title={Locale.Settings.Actions.ResetAll}
  99. />
  100. </div>
  101. <div className={styles["window-action-button"]}>
  102. <IconButton
  103. icon={<CloseIcon />}
  104. onClick={props.closeSettings}
  105. bordered
  106. title={Locale.Settings.Actions.Close}
  107. />
  108. </div>
  109. </div>
  110. </div>
  111. <div className={styles["settings"]}>
  112. <List>
  113. <SettingItem title={Locale.Settings.Avatar}>
  114. <Popover
  115. onClose={() => setShowEmojiPicker(false)}
  116. content={
  117. <EmojiPicker
  118. lazyLoadEmojis
  119. theme={EmojiTheme.AUTO}
  120. onEmojiClick={(e) => {
  121. updateConfig((config) => (config.avatar = e.unified));
  122. setShowEmojiPicker(false);
  123. }}
  124. />
  125. }
  126. open={showEmojiPicker}
  127. >
  128. <div
  129. className={styles.avatar}
  130. onClick={() => setShowEmojiPicker(true)}
  131. >
  132. <Avatar role="user" />
  133. </div>
  134. </Popover>
  135. </SettingItem>
  136. <SettingItem
  137. title={Locale.Settings.Update.Version(currentId)}
  138. subTitle={
  139. checkingUpdate
  140. ? Locale.Settings.Update.IsChecking
  141. : hasNewVersion
  142. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  143. : Locale.Settings.Update.IsLatest
  144. }
  145. >
  146. {checkingUpdate ? (
  147. <div />
  148. ) : hasNewVersion ? (
  149. <Link href={UPDATE_URL} target="_blank" className="link">
  150. {Locale.Settings.Update.GoToUpdate}
  151. </Link>
  152. ) : (
  153. <IconButton
  154. icon={<ResetIcon></ResetIcon>}
  155. text={Locale.Settings.Update.CheckUpdate}
  156. onClick={() => checkUpdate(true)}
  157. />
  158. )}
  159. </SettingItem>
  160. <SettingItem title={Locale.Settings.SendKey}>
  161. <select
  162. value={config.submitKey}
  163. onChange={(e) => {
  164. updateConfig(
  165. (config) =>
  166. (config.submitKey = e.target.value as any as SubmitKey)
  167. );
  168. }}
  169. >
  170. {Object.values(SubmitKey).map((v) => (
  171. <option value={v} key={v}>
  172. {v}
  173. </option>
  174. ))}
  175. </select>
  176. </SettingItem>
  177. <ListItem>
  178. <div className={styles["settings-title"]}>
  179. {Locale.Settings.Theme}
  180. </div>
  181. <select
  182. value={config.theme}
  183. onChange={(e) => {
  184. updateConfig(
  185. (config) => (config.theme = e.target.value as any as Theme)
  186. );
  187. }}
  188. >
  189. {Object.values(Theme).map((v) => (
  190. <option value={v} key={v}>
  191. {v}
  192. </option>
  193. ))}
  194. </select>
  195. </ListItem>
  196. <SettingItem title={Locale.Settings.Lang.Name}>
  197. <div className="">
  198. <select
  199. value={getLang()}
  200. onChange={(e) => {
  201. changeLang(e.target.value as any);
  202. }}
  203. >
  204. <option value="en" key="en">
  205. {Locale.Settings.Lang.Options.en}
  206. </option>
  207. <option value="cn" key="cn">
  208. {Locale.Settings.Lang.Options.cn}
  209. </option>
  210. <option value="tw" key="tw">
  211. {Locale.Settings.Lang.Options.tw}
  212. </option>
  213. </select>
  214. </div>
  215. </SettingItem>
  216. <div className="no-mobile">
  217. <SettingItem title={Locale.Settings.TightBorder}>
  218. <input
  219. type="checkbox"
  220. checked={config.tightBorder}
  221. onChange={(e) =>
  222. updateConfig(
  223. (config) => (config.tightBorder = e.currentTarget.checked)
  224. )
  225. }
  226. ></input>
  227. </SettingItem>
  228. </div>
  229. </List>
  230. <List>
  231. <SettingItem
  232. title={Locale.Settings.Prompt.Disable.Title}
  233. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  234. >
  235. <input
  236. type="checkbox"
  237. checked={config.disablePromptHint}
  238. onChange={(e) =>
  239. updateConfig(
  240. (config) =>
  241. (config.disablePromptHint = e.currentTarget.checked)
  242. )
  243. }
  244. ></input>
  245. </SettingItem>
  246. <SettingItem
  247. title={Locale.Settings.Prompt.List}
  248. subTitle={Locale.Settings.Prompt.ListCount(
  249. builtinCount,
  250. customCount
  251. )}
  252. >
  253. <IconButton
  254. icon={<EditIcon />}
  255. text={Locale.Settings.Prompt.Edit}
  256. onClick={() => showToast(Locale.WIP)}
  257. />
  258. </SettingItem>
  259. </List>
  260. <List>
  261. {enabledAccessControl ? (
  262. <SettingItem
  263. title={Locale.Settings.AccessCode.Title}
  264. subTitle={Locale.Settings.AccessCode.SubTitle}
  265. >
  266. <input
  267. value={accessStore.accessCode}
  268. type="text"
  269. placeholder={Locale.Settings.AccessCode.Placeholder}
  270. onChange={(e) => {
  271. accessStore.updateCode(e.currentTarget.value);
  272. }}
  273. ></input>
  274. </SettingItem>
  275. ) : (
  276. <></>
  277. )}
  278. <SettingItem
  279. title={Locale.Settings.Token.Title}
  280. subTitle={Locale.Settings.Token.SubTitle}
  281. >
  282. <input
  283. value={accessStore.token}
  284. type="text"
  285. placeholder={Locale.Settings.Token.Placeholder}
  286. onChange={(e) => {
  287. accessStore.updateToken(e.currentTarget.value);
  288. }}
  289. ></input>
  290. </SettingItem>
  291. <SettingItem
  292. title={Locale.Settings.HistoryCount.Title}
  293. subTitle={Locale.Settings.HistoryCount.SubTitle}
  294. >
  295. <input
  296. type="range"
  297. title={config.historyMessageCount.toString()}
  298. value={config.historyMessageCount}
  299. min="2"
  300. max="25"
  301. step="2"
  302. onChange={(e) =>
  303. updateConfig(
  304. (config) =>
  305. (config.historyMessageCount = e.target.valueAsNumber)
  306. )
  307. }
  308. ></input>
  309. </SettingItem>
  310. <SettingItem
  311. title={Locale.Settings.CompressThreshold.Title}
  312. subTitle={Locale.Settings.CompressThreshold.SubTitle}
  313. >
  314. <input
  315. type="number"
  316. min={500}
  317. max={4000}
  318. value={config.compressMessageLengthThreshold}
  319. onChange={(e) =>
  320. updateConfig(
  321. (config) =>
  322. (config.compressMessageLengthThreshold =
  323. e.currentTarget.valueAsNumber)
  324. )
  325. }
  326. ></input>
  327. </SettingItem>
  328. </List>
  329. <List>
  330. <SettingItem title={Locale.Settings.Model}>
  331. <select
  332. value={config.modelConfig.model}
  333. onChange={(e) => {
  334. updateConfig(
  335. (config) => (config.modelConfig.model = e.currentTarget.value)
  336. );
  337. }}
  338. >
  339. {ALL_MODELS.map((v) => (
  340. <option value={v.name} key={v.name} disabled={!v.available}>
  341. {v.name}
  342. </option>
  343. ))}
  344. </select>
  345. </SettingItem>
  346. <SettingItem
  347. title={Locale.Settings.Temperature.Title}
  348. subTitle={Locale.Settings.Temperature.SubTitle}
  349. >
  350. <input
  351. type="range"
  352. value={config.modelConfig.temperature.toFixed(1)}
  353. min="0"
  354. max="1"
  355. step="0.1"
  356. onChange={(e) => {
  357. updateConfig(
  358. (config) =>
  359. (config.modelConfig.temperature =
  360. e.currentTarget.valueAsNumber)
  361. );
  362. }}
  363. ></input>
  364. </SettingItem>
  365. <SettingItem
  366. title={Locale.Settings.MaxTokens.Title}
  367. subTitle={Locale.Settings.MaxTokens.SubTitle}
  368. >
  369. <input
  370. type="number"
  371. min={100}
  372. max={4096}
  373. value={config.modelConfig.max_tokens}
  374. onChange={(e) =>
  375. updateConfig(
  376. (config) =>
  377. (config.modelConfig.max_tokens =
  378. e.currentTarget.valueAsNumber)
  379. )
  380. }
  381. ></input>
  382. </SettingItem>
  383. <SettingItem
  384. title={Locale.Settings.PresencePenlty.Title}
  385. subTitle={Locale.Settings.PresencePenlty.SubTitle}
  386. >
  387. <input
  388. type="range"
  389. value={config.modelConfig.presence_penalty.toFixed(1)}
  390. min="-2"
  391. max="2"
  392. step="0.5"
  393. onChange={(e) => {
  394. updateConfig(
  395. (config) =>
  396. (config.modelConfig.presence_penalty =
  397. e.currentTarget.valueAsNumber)
  398. );
  399. }}
  400. ></input>
  401. </SettingItem>
  402. </List>
  403. </div>
  404. </>
  405. );
  406. }