settings.tsx 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. import { useState, useEffect, useMemo } from "react";
  2. import styles from "./settings.module.scss";
  3. import ResetIcon from "../icons/reload.svg";
  4. import AddIcon from "../icons/add.svg";
  5. import CloseIcon from "../icons/close.svg";
  6. import CopyIcon from "../icons/copy.svg";
  7. import ClearIcon from "../icons/clear.svg";
  8. import LoadingIcon from "../icons/three-dots.svg";
  9. import EditIcon from "../icons/edit.svg";
  10. import EyeIcon from "../icons/eye.svg";
  11. import DownloadIcon from "../icons/download.svg";
  12. import UploadIcon from "../icons/upload.svg";
  13. import ConfigIcon from "../icons/config.svg";
  14. import ConfirmIcon from "../icons/confirm.svg";
  15. import ConnectionIcon from "../icons/connection.svg";
  16. import CloudSuccessIcon from "../icons/cloud-success.svg";
  17. import CloudFailIcon from "../icons/cloud-fail.svg";
  18. import {
  19. Input,
  20. List,
  21. ListItem,
  22. Modal,
  23. PasswordInput,
  24. Popover,
  25. Select,
  26. showConfirm,
  27. showToast,
  28. } from "./ui-lib";
  29. import { ModelConfigList } from "./model-config";
  30. import { IconButton } from "./button";
  31. import {
  32. SubmitKey,
  33. useChatStore,
  34. Theme,
  35. useUpdateStore,
  36. useAccessStore,
  37. useAppConfig,
  38. } from "../store";
  39. import Locale, {
  40. AllLangs,
  41. ALL_LANG_OPTIONS,
  42. changeLang,
  43. getLang,
  44. } from "../locales";
  45. import { copyToClipboard } from "../utils";
  46. import Link from "next/link";
  47. import {
  48. Anthropic,
  49. Azure,
  50. Baidu,
  51. ByteDance,
  52. Alibaba,
  53. Google,
  54. GoogleSafetySettingsThreshold,
  55. OPENAI_BASE_URL,
  56. Path,
  57. RELEASE_URL,
  58. STORAGE_KEY,
  59. ServiceProvider,
  60. SlotID,
  61. UPDATE_URL,
  62. } from "../constant";
  63. import { Prompt, SearchService, usePromptStore } from "../store/prompt";
  64. import { ErrorBoundary } from "./error";
  65. import { InputRange } from "./input-range";
  66. import { useNavigate } from "react-router-dom";
  67. import { Avatar, AvatarPicker } from "./emoji";
  68. import { getClientConfig } from "../config/client";
  69. import { useSyncStore } from "../store/sync";
  70. import { nanoid } from "nanoid";
  71. import { useMaskStore } from "../store/mask";
  72. import { ProviderType } from "../utils/cloud";
  73. function EditPromptModal(props: { id: string; onClose: () => void }) {
  74. const promptStore = usePromptStore();
  75. const prompt = promptStore.get(props.id);
  76. return prompt ? (
  77. <div className="modal-mask">
  78. <Modal
  79. title={Locale.Settings.Prompt.EditModal.Title}
  80. onClose={props.onClose}
  81. actions={[
  82. <IconButton
  83. key=""
  84. onClick={props.onClose}
  85. text={Locale.UI.Confirm}
  86. bordered
  87. />,
  88. ]}
  89. >
  90. <div className={styles["edit-prompt-modal"]}>
  91. <input
  92. type="text"
  93. value={prompt.title}
  94. readOnly={!prompt.isUser}
  95. className={styles["edit-prompt-title"]}
  96. onInput={(e) =>
  97. promptStore.updatePrompt(
  98. props.id,
  99. (prompt) => (prompt.title = e.currentTarget.value),
  100. )
  101. }
  102. ></input>
  103. <Input
  104. value={prompt.content}
  105. readOnly={!prompt.isUser}
  106. className={styles["edit-prompt-content"]}
  107. rows={10}
  108. onInput={(e) =>
  109. promptStore.updatePrompt(
  110. props.id,
  111. (prompt) => (prompt.content = e.currentTarget.value),
  112. )
  113. }
  114. ></Input>
  115. </div>
  116. </Modal>
  117. </div>
  118. ) : null;
  119. }
  120. function UserPromptModal(props: { onClose?: () => void }) {
  121. const promptStore = usePromptStore();
  122. const userPrompts = promptStore.getUserPrompts();
  123. const builtinPrompts = SearchService.builtinPrompts;
  124. const allPrompts = userPrompts.concat(builtinPrompts);
  125. const [searchInput, setSearchInput] = useState("");
  126. const [searchPrompts, setSearchPrompts] = useState<Prompt[]>([]);
  127. const prompts = searchInput.length > 0 ? searchPrompts : allPrompts;
  128. const [editingPromptId, setEditingPromptId] = useState<string>();
  129. useEffect(() => {
  130. if (searchInput.length > 0) {
  131. const searchResult = SearchService.search(searchInput);
  132. setSearchPrompts(searchResult);
  133. } else {
  134. setSearchPrompts([]);
  135. }
  136. }, [searchInput]);
  137. return (
  138. <div className="modal-mask">
  139. <Modal
  140. title={Locale.Settings.Prompt.Modal.Title}
  141. onClose={() => props.onClose?.()}
  142. actions={[
  143. <IconButton
  144. key="add"
  145. onClick={() => {
  146. const promptId = promptStore.add({
  147. id: nanoid(),
  148. createdAt: Date.now(),
  149. title: "Empty Prompt",
  150. content: "Empty Prompt Content",
  151. });
  152. setEditingPromptId(promptId);
  153. }}
  154. icon={<AddIcon />}
  155. bordered
  156. text={Locale.Settings.Prompt.Modal.Add}
  157. />,
  158. ]}
  159. >
  160. <div className={styles["user-prompt-modal"]}>
  161. <input
  162. type="text"
  163. className={styles["user-prompt-search"]}
  164. placeholder={Locale.Settings.Prompt.Modal.Search}
  165. value={searchInput}
  166. onInput={(e) => setSearchInput(e.currentTarget.value)}
  167. ></input>
  168. <div className={styles["user-prompt-list"]}>
  169. {prompts.map((v, _) => (
  170. <div className={styles["user-prompt-item"]} key={v.id ?? v.title}>
  171. <div className={styles["user-prompt-header"]}>
  172. <div className={styles["user-prompt-title"]}>{v.title}</div>
  173. <div className={styles["user-prompt-content"] + " one-line"}>
  174. {v.content}
  175. </div>
  176. </div>
  177. <div className={styles["user-prompt-buttons"]}>
  178. {v.isUser && (
  179. <IconButton
  180. icon={<ClearIcon />}
  181. className={styles["user-prompt-button"]}
  182. onClick={() => promptStore.remove(v.id!)}
  183. />
  184. )}
  185. {v.isUser ? (
  186. <IconButton
  187. icon={<EditIcon />}
  188. className={styles["user-prompt-button"]}
  189. onClick={() => setEditingPromptId(v.id)}
  190. />
  191. ) : (
  192. <IconButton
  193. icon={<EyeIcon />}
  194. className={styles["user-prompt-button"]}
  195. onClick={() => setEditingPromptId(v.id)}
  196. />
  197. )}
  198. <IconButton
  199. icon={<CopyIcon />}
  200. className={styles["user-prompt-button"]}
  201. onClick={() => copyToClipboard(v.content)}
  202. />
  203. </div>
  204. </div>
  205. ))}
  206. </div>
  207. </div>
  208. </Modal>
  209. {editingPromptId !== undefined && (
  210. <EditPromptModal
  211. id={editingPromptId!}
  212. onClose={() => setEditingPromptId(undefined)}
  213. />
  214. )}
  215. </div>
  216. );
  217. }
  218. function DangerItems() {
  219. const chatStore = useChatStore();
  220. const appConfig = useAppConfig();
  221. return (
  222. <List>
  223. <ListItem
  224. title={Locale.Settings.Danger.Reset.Title}
  225. subTitle={Locale.Settings.Danger.Reset.SubTitle}
  226. >
  227. <IconButton
  228. text={Locale.Settings.Danger.Reset.Action}
  229. onClick={async () => {
  230. if (await showConfirm(Locale.Settings.Danger.Reset.Confirm)) {
  231. appConfig.reset();
  232. }
  233. }}
  234. type="danger"
  235. />
  236. </ListItem>
  237. <ListItem
  238. title={Locale.Settings.Danger.Clear.Title}
  239. subTitle={Locale.Settings.Danger.Clear.SubTitle}
  240. >
  241. <IconButton
  242. text={Locale.Settings.Danger.Clear.Action}
  243. onClick={async () => {
  244. if (await showConfirm(Locale.Settings.Danger.Clear.Confirm)) {
  245. chatStore.clearAllData();
  246. }
  247. }}
  248. type="danger"
  249. />
  250. </ListItem>
  251. </List>
  252. );
  253. }
  254. function CheckButton() {
  255. const syncStore = useSyncStore();
  256. const couldCheck = useMemo(() => {
  257. return syncStore.cloudSync();
  258. }, [syncStore]);
  259. const [checkState, setCheckState] = useState<
  260. "none" | "checking" | "success" | "failed"
  261. >("none");
  262. async function check() {
  263. setCheckState("checking");
  264. const valid = await syncStore.check();
  265. setCheckState(valid ? "success" : "failed");
  266. }
  267. if (!couldCheck) return null;
  268. return (
  269. <IconButton
  270. text={Locale.Settings.Sync.Config.Modal.Check}
  271. bordered
  272. onClick={check}
  273. icon={
  274. checkState === "none" ? (
  275. <ConnectionIcon />
  276. ) : checkState === "checking" ? (
  277. <LoadingIcon />
  278. ) : checkState === "success" ? (
  279. <CloudSuccessIcon />
  280. ) : checkState === "failed" ? (
  281. <CloudFailIcon />
  282. ) : (
  283. <ConnectionIcon />
  284. )
  285. }
  286. ></IconButton>
  287. );
  288. }
  289. function SyncConfigModal(props: { onClose?: () => void }) {
  290. const syncStore = useSyncStore();
  291. return (
  292. <div className="modal-mask">
  293. <Modal
  294. title={Locale.Settings.Sync.Config.Modal.Title}
  295. onClose={() => props.onClose?.()}
  296. actions={[
  297. <CheckButton key="check" />,
  298. <IconButton
  299. key="confirm"
  300. onClick={props.onClose}
  301. icon={<ConfirmIcon />}
  302. bordered
  303. text={Locale.UI.Confirm}
  304. />,
  305. ]}
  306. >
  307. <List>
  308. <ListItem
  309. title={Locale.Settings.Sync.Config.SyncType.Title}
  310. subTitle={Locale.Settings.Sync.Config.SyncType.SubTitle}
  311. >
  312. <select
  313. value={syncStore.provider}
  314. onChange={(e) => {
  315. syncStore.update(
  316. (config) =>
  317. (config.provider = e.target.value as ProviderType),
  318. );
  319. }}
  320. >
  321. {Object.entries(ProviderType).map(([k, v]) => (
  322. <option value={v} key={k}>
  323. {k}
  324. </option>
  325. ))}
  326. </select>
  327. </ListItem>
  328. <ListItem
  329. title={Locale.Settings.Sync.Config.Proxy.Title}
  330. subTitle={Locale.Settings.Sync.Config.Proxy.SubTitle}
  331. >
  332. <input
  333. type="checkbox"
  334. checked={syncStore.useProxy}
  335. onChange={(e) => {
  336. syncStore.update(
  337. (config) => (config.useProxy = e.currentTarget.checked),
  338. );
  339. }}
  340. ></input>
  341. </ListItem>
  342. {syncStore.useProxy ? (
  343. <ListItem
  344. title={Locale.Settings.Sync.Config.ProxyUrl.Title}
  345. subTitle={Locale.Settings.Sync.Config.ProxyUrl.SubTitle}
  346. >
  347. <input
  348. type="text"
  349. value={syncStore.proxyUrl}
  350. onChange={(e) => {
  351. syncStore.update(
  352. (config) => (config.proxyUrl = e.currentTarget.value),
  353. );
  354. }}
  355. ></input>
  356. </ListItem>
  357. ) : null}
  358. </List>
  359. {syncStore.provider === ProviderType.WebDAV && (
  360. <>
  361. <List>
  362. <ListItem title={Locale.Settings.Sync.Config.WebDav.Endpoint}>
  363. <input
  364. type="text"
  365. value={syncStore.webdav.endpoint}
  366. onChange={(e) => {
  367. syncStore.update(
  368. (config) =>
  369. (config.webdav.endpoint = e.currentTarget.value),
  370. );
  371. }}
  372. ></input>
  373. </ListItem>
  374. <ListItem title={Locale.Settings.Sync.Config.WebDav.UserName}>
  375. <input
  376. type="text"
  377. value={syncStore.webdav.username}
  378. onChange={(e) => {
  379. syncStore.update(
  380. (config) =>
  381. (config.webdav.username = e.currentTarget.value),
  382. );
  383. }}
  384. ></input>
  385. </ListItem>
  386. <ListItem title={Locale.Settings.Sync.Config.WebDav.Password}>
  387. <PasswordInput
  388. value={syncStore.webdav.password}
  389. onChange={(e) => {
  390. syncStore.update(
  391. (config) =>
  392. (config.webdav.password = e.currentTarget.value),
  393. );
  394. }}
  395. ></PasswordInput>
  396. </ListItem>
  397. </List>
  398. </>
  399. )}
  400. {syncStore.provider === ProviderType.UpStash && (
  401. <List>
  402. <ListItem title={Locale.Settings.Sync.Config.UpStash.Endpoint}>
  403. <input
  404. type="text"
  405. value={syncStore.upstash.endpoint}
  406. onChange={(e) => {
  407. syncStore.update(
  408. (config) =>
  409. (config.upstash.endpoint = e.currentTarget.value),
  410. );
  411. }}
  412. ></input>
  413. </ListItem>
  414. <ListItem title={Locale.Settings.Sync.Config.UpStash.UserName}>
  415. <input
  416. type="text"
  417. value={syncStore.upstash.username}
  418. placeholder={STORAGE_KEY}
  419. onChange={(e) => {
  420. syncStore.update(
  421. (config) =>
  422. (config.upstash.username = e.currentTarget.value),
  423. );
  424. }}
  425. ></input>
  426. </ListItem>
  427. <ListItem title={Locale.Settings.Sync.Config.UpStash.Password}>
  428. <PasswordInput
  429. value={syncStore.upstash.apiKey}
  430. onChange={(e) => {
  431. syncStore.update(
  432. (config) => (config.upstash.apiKey = e.currentTarget.value),
  433. );
  434. }}
  435. ></PasswordInput>
  436. </ListItem>
  437. </List>
  438. )}
  439. </Modal>
  440. </div>
  441. );
  442. }
  443. function SyncItems() {
  444. const syncStore = useSyncStore();
  445. const chatStore = useChatStore();
  446. const promptStore = usePromptStore();
  447. const maskStore = useMaskStore();
  448. const couldSync = useMemo(() => {
  449. return syncStore.cloudSync();
  450. }, [syncStore]);
  451. const [showSyncConfigModal, setShowSyncConfigModal] = useState(false);
  452. const stateOverview = useMemo(() => {
  453. const sessions = chatStore.sessions;
  454. const messageCount = sessions.reduce((p, c) => p + c.messages.length, 0);
  455. return {
  456. chat: sessions.length,
  457. message: messageCount,
  458. prompt: Object.keys(promptStore.prompts).length,
  459. mask: Object.keys(maskStore.masks).length,
  460. };
  461. }, [chatStore.sessions, maskStore.masks, promptStore.prompts]);
  462. return (
  463. <>
  464. <List>
  465. <ListItem
  466. title={Locale.Settings.Sync.CloudState}
  467. subTitle={
  468. syncStore.lastProvider
  469. ? `${new Date(syncStore.lastSyncTime).toLocaleString()} [${
  470. syncStore.lastProvider
  471. }]`
  472. : Locale.Settings.Sync.NotSyncYet
  473. }
  474. >
  475. <div style={{ display: "flex" }}>
  476. <IconButton
  477. icon={<ConfigIcon />}
  478. text={Locale.UI.Config}
  479. onClick={() => {
  480. setShowSyncConfigModal(true);
  481. }}
  482. />
  483. {couldSync && (
  484. <IconButton
  485. icon={<ResetIcon />}
  486. text={Locale.UI.Sync}
  487. onClick={async () => {
  488. try {
  489. await syncStore.sync();
  490. showToast(Locale.Settings.Sync.Success);
  491. } catch (e) {
  492. showToast(Locale.Settings.Sync.Fail);
  493. console.error("[Sync]", e);
  494. }
  495. }}
  496. />
  497. )}
  498. </div>
  499. </ListItem>
  500. <ListItem
  501. title={Locale.Settings.Sync.LocalState}
  502. subTitle={Locale.Settings.Sync.Overview(stateOverview)}
  503. >
  504. <div style={{ display: "flex" }}>
  505. <IconButton
  506. icon={<UploadIcon />}
  507. text={Locale.UI.Export}
  508. onClick={() => {
  509. syncStore.export();
  510. }}
  511. />
  512. <IconButton
  513. icon={<DownloadIcon />}
  514. text={Locale.UI.Import}
  515. onClick={() => {
  516. syncStore.import();
  517. }}
  518. />
  519. </div>
  520. </ListItem>
  521. </List>
  522. {showSyncConfigModal && (
  523. <SyncConfigModal onClose={() => setShowSyncConfigModal(false)} />
  524. )}
  525. </>
  526. );
  527. }
  528. export function Settings() {
  529. const navigate = useNavigate();
  530. const [showEmojiPicker, setShowEmojiPicker] = useState(false);
  531. const config = useAppConfig();
  532. const updateConfig = config.update;
  533. const updateStore = useUpdateStore();
  534. const [checkingUpdate, setCheckingUpdate] = useState(false);
  535. const currentVersion = updateStore.formatVersion(updateStore.version);
  536. const remoteId = updateStore.formatVersion(updateStore.remoteVersion);
  537. const hasNewVersion = currentVersion !== remoteId;
  538. const updateUrl = getClientConfig()?.isApp ? RELEASE_URL : UPDATE_URL;
  539. function checkUpdate(force = false) {
  540. setCheckingUpdate(true);
  541. updateStore.getLatestVersion(force).then(() => {
  542. setCheckingUpdate(false);
  543. });
  544. console.log("[Update] local version ", updateStore.version);
  545. console.log("[Update] remote version ", updateStore.remoteVersion);
  546. }
  547. const accessStore = useAccessStore();
  548. const shouldHideBalanceQuery = useMemo(() => {
  549. const isOpenAiUrl = accessStore.openaiUrl.includes(OPENAI_BASE_URL);
  550. return (
  551. accessStore.hideBalanceQuery ||
  552. isOpenAiUrl ||
  553. accessStore.provider === ServiceProvider.Azure
  554. );
  555. }, [
  556. accessStore.hideBalanceQuery,
  557. accessStore.openaiUrl,
  558. accessStore.provider,
  559. ]);
  560. const usage = {
  561. used: updateStore.used,
  562. subscription: updateStore.subscription,
  563. };
  564. const [loadingUsage, setLoadingUsage] = useState(false);
  565. function checkUsage(force = false) {
  566. if (shouldHideBalanceQuery) {
  567. return;
  568. }
  569. setLoadingUsage(true);
  570. updateStore.updateUsage(force).finally(() => {
  571. setLoadingUsage(false);
  572. });
  573. }
  574. const enabledAccessControl = useMemo(
  575. () => accessStore.enabledAccessControl(),
  576. // eslint-disable-next-line react-hooks/exhaustive-deps
  577. [],
  578. );
  579. const promptStore = usePromptStore();
  580. const builtinCount = SearchService.count.builtin;
  581. const customCount = promptStore.getUserPrompts().length ?? 0;
  582. const [shouldShowPromptModal, setShowPromptModal] = useState(false);
  583. const showUsage = accessStore.isAuthorized();
  584. useEffect(() => {
  585. // checks per minutes
  586. checkUpdate();
  587. showUsage && checkUsage();
  588. // eslint-disable-next-line react-hooks/exhaustive-deps
  589. }, []);
  590. useEffect(() => {
  591. const keydownEvent = (e: KeyboardEvent) => {
  592. if (e.key === "Escape") {
  593. navigate(Path.Home);
  594. }
  595. };
  596. if (clientConfig?.isApp) {
  597. // Force to set custom endpoint to true if it's app
  598. accessStore.update((state) => {
  599. state.useCustomConfig = true;
  600. });
  601. }
  602. document.addEventListener("keydown", keydownEvent);
  603. return () => {
  604. document.removeEventListener("keydown", keydownEvent);
  605. };
  606. // eslint-disable-next-line react-hooks/exhaustive-deps
  607. }, []);
  608. const clientConfig = useMemo(() => getClientConfig(), []);
  609. const showAccessCode = enabledAccessControl && !clientConfig?.isApp;
  610. return (
  611. <ErrorBoundary>
  612. <div className="window-header" data-tauri-drag-region>
  613. <div className="window-header-title">
  614. <div className="window-header-main-title">
  615. {Locale.Settings.Title}
  616. </div>
  617. <div className="window-header-sub-title">
  618. {Locale.Settings.SubTitle}
  619. </div>
  620. </div>
  621. <div className="window-actions">
  622. <div className="window-action-button"></div>
  623. <div className="window-action-button"></div>
  624. <div className="window-action-button">
  625. <IconButton
  626. icon={<CloseIcon />}
  627. onClick={() => navigate(Path.Home)}
  628. bordered
  629. />
  630. </div>
  631. </div>
  632. </div>
  633. <div className={styles["settings"]}>
  634. <List>
  635. <ListItem title={Locale.Settings.Avatar}>
  636. <Popover
  637. onClose={() => setShowEmojiPicker(false)}
  638. content={
  639. <AvatarPicker
  640. onEmojiClick={(avatar: string) => {
  641. updateConfig((config) => (config.avatar = avatar));
  642. setShowEmojiPicker(false);
  643. }}
  644. />
  645. }
  646. open={showEmojiPicker}
  647. >
  648. <div
  649. className={styles.avatar}
  650. onClick={() => {
  651. setShowEmojiPicker(!showEmojiPicker);
  652. }}
  653. >
  654. <Avatar avatar={config.avatar} />
  655. </div>
  656. </Popover>
  657. </ListItem>
  658. <ListItem
  659. title={Locale.Settings.Update.Version(currentVersion ?? "unknown")}
  660. subTitle={
  661. checkingUpdate
  662. ? Locale.Settings.Update.IsChecking
  663. : hasNewVersion
  664. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  665. : Locale.Settings.Update.IsLatest
  666. }
  667. >
  668. {checkingUpdate ? (
  669. <LoadingIcon />
  670. ) : hasNewVersion ? (
  671. <Link href={updateUrl} target="_blank" className="link">
  672. {Locale.Settings.Update.GoToUpdate}
  673. </Link>
  674. ) : (
  675. <IconButton
  676. icon={<ResetIcon></ResetIcon>}
  677. text={Locale.Settings.Update.CheckUpdate}
  678. onClick={() => checkUpdate(true)}
  679. />
  680. )}
  681. </ListItem>
  682. <ListItem title={Locale.Settings.SendKey}>
  683. <Select
  684. value={config.submitKey}
  685. onChange={(e) => {
  686. updateConfig(
  687. (config) =>
  688. (config.submitKey = e.target.value as any as SubmitKey),
  689. );
  690. }}
  691. >
  692. {Object.values(SubmitKey).map((v) => (
  693. <option value={v} key={v}>
  694. {v}
  695. </option>
  696. ))}
  697. </Select>
  698. </ListItem>
  699. <ListItem title={Locale.Settings.Theme}>
  700. <Select
  701. value={config.theme}
  702. onChange={(e) => {
  703. updateConfig(
  704. (config) => (config.theme = e.target.value as any as Theme),
  705. );
  706. }}
  707. >
  708. {Object.values(Theme).map((v) => (
  709. <option value={v} key={v}>
  710. {v}
  711. </option>
  712. ))}
  713. </Select>
  714. </ListItem>
  715. <ListItem title={Locale.Settings.Lang.Name}>
  716. <Select
  717. value={getLang()}
  718. onChange={(e) => {
  719. changeLang(e.target.value as any);
  720. }}
  721. >
  722. {AllLangs.map((lang) => (
  723. <option value={lang} key={lang}>
  724. {ALL_LANG_OPTIONS[lang]}
  725. </option>
  726. ))}
  727. </Select>
  728. </ListItem>
  729. <ListItem
  730. title={Locale.Settings.FontSize.Title}
  731. subTitle={Locale.Settings.FontSize.SubTitle}
  732. >
  733. <InputRange
  734. title={`${config.fontSize ?? 14}px`}
  735. value={config.fontSize}
  736. min="12"
  737. max="40"
  738. step="1"
  739. onChange={(e) =>
  740. updateConfig(
  741. (config) =>
  742. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  743. )
  744. }
  745. ></InputRange>
  746. </ListItem>
  747. <ListItem
  748. title={Locale.Settings.AutoGenerateTitle.Title}
  749. subTitle={Locale.Settings.AutoGenerateTitle.SubTitle}
  750. >
  751. <input
  752. type="checkbox"
  753. checked={config.enableAutoGenerateTitle}
  754. onChange={(e) =>
  755. updateConfig(
  756. (config) =>
  757. (config.enableAutoGenerateTitle = e.currentTarget.checked),
  758. )
  759. }
  760. ></input>
  761. </ListItem>
  762. <ListItem
  763. title={Locale.Settings.SendPreviewBubble.Title}
  764. subTitle={Locale.Settings.SendPreviewBubble.SubTitle}
  765. >
  766. <input
  767. type="checkbox"
  768. checked={config.sendPreviewBubble}
  769. onChange={(e) =>
  770. updateConfig(
  771. (config) =>
  772. (config.sendPreviewBubble = e.currentTarget.checked),
  773. )
  774. }
  775. ></input>
  776. </ListItem>
  777. </List>
  778. <SyncItems />
  779. <List>
  780. <ListItem
  781. title={Locale.Settings.Mask.Splash.Title}
  782. subTitle={Locale.Settings.Mask.Splash.SubTitle}
  783. >
  784. <input
  785. type="checkbox"
  786. checked={!config.dontShowMaskSplashScreen}
  787. onChange={(e) =>
  788. updateConfig(
  789. (config) =>
  790. (config.dontShowMaskSplashScreen =
  791. !e.currentTarget.checked),
  792. )
  793. }
  794. ></input>
  795. </ListItem>
  796. <ListItem
  797. title={Locale.Settings.Mask.Builtin.Title}
  798. subTitle={Locale.Settings.Mask.Builtin.SubTitle}
  799. >
  800. <input
  801. type="checkbox"
  802. checked={config.hideBuiltinMasks}
  803. onChange={(e) =>
  804. updateConfig(
  805. (config) =>
  806. (config.hideBuiltinMasks = e.currentTarget.checked),
  807. )
  808. }
  809. ></input>
  810. </ListItem>
  811. </List>
  812. <List>
  813. <ListItem
  814. title={Locale.Settings.Prompt.Disable.Title}
  815. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  816. >
  817. <input
  818. type="checkbox"
  819. checked={config.disablePromptHint}
  820. onChange={(e) =>
  821. updateConfig(
  822. (config) =>
  823. (config.disablePromptHint = e.currentTarget.checked),
  824. )
  825. }
  826. ></input>
  827. </ListItem>
  828. <ListItem
  829. title={Locale.Settings.Prompt.List}
  830. subTitle={Locale.Settings.Prompt.ListCount(
  831. builtinCount,
  832. customCount,
  833. )}
  834. >
  835. <IconButton
  836. icon={<EditIcon />}
  837. text={Locale.Settings.Prompt.Edit}
  838. onClick={() => setShowPromptModal(true)}
  839. />
  840. </ListItem>
  841. </List>
  842. <List id={SlotID.CustomModel}>
  843. {showAccessCode && (
  844. <ListItem
  845. title={Locale.Settings.Access.AccessCode.Title}
  846. subTitle={Locale.Settings.Access.AccessCode.SubTitle}
  847. >
  848. <PasswordInput
  849. value={accessStore.accessCode}
  850. type="text"
  851. placeholder={Locale.Settings.Access.AccessCode.Placeholder}
  852. onChange={(e) => {
  853. accessStore.update(
  854. (access) => (access.accessCode = e.currentTarget.value),
  855. );
  856. }}
  857. />
  858. </ListItem>
  859. )}
  860. {!accessStore.hideUserApiKey && (
  861. <>
  862. {
  863. // Conditionally render the following ListItem based on clientConfig.isApp
  864. !clientConfig?.isApp && ( // only show if isApp is false
  865. <ListItem
  866. title={Locale.Settings.Access.CustomEndpoint.Title}
  867. subTitle={Locale.Settings.Access.CustomEndpoint.SubTitle}
  868. >
  869. <input
  870. type="checkbox"
  871. checked={accessStore.useCustomConfig}
  872. onChange={(e) =>
  873. accessStore.update(
  874. (access) =>
  875. (access.useCustomConfig = e.currentTarget.checked),
  876. )
  877. }
  878. ></input>
  879. </ListItem>
  880. )
  881. }
  882. {accessStore.useCustomConfig && (
  883. <>
  884. <ListItem
  885. title={Locale.Settings.Access.Provider.Title}
  886. subTitle={Locale.Settings.Access.Provider.SubTitle}
  887. >
  888. <Select
  889. value={accessStore.provider}
  890. onChange={(e) => {
  891. accessStore.update(
  892. (access) =>
  893. (access.provider = e.target
  894. .value as ServiceProvider),
  895. );
  896. }}
  897. >
  898. {Object.entries(ServiceProvider).map(([k, v]) => (
  899. <option value={v} key={k}>
  900. {k}
  901. </option>
  902. ))}
  903. </Select>
  904. </ListItem>
  905. {accessStore.provider === ServiceProvider.OpenAI && (
  906. <>
  907. <ListItem
  908. title={Locale.Settings.Access.OpenAI.Endpoint.Title}
  909. subTitle={
  910. Locale.Settings.Access.OpenAI.Endpoint.SubTitle
  911. }
  912. >
  913. <input
  914. type="text"
  915. value={accessStore.openaiUrl}
  916. placeholder={OPENAI_BASE_URL}
  917. onChange={(e) =>
  918. accessStore.update(
  919. (access) =>
  920. (access.openaiUrl = e.currentTarget.value),
  921. )
  922. }
  923. ></input>
  924. </ListItem>
  925. <ListItem
  926. title={Locale.Settings.Access.OpenAI.ApiKey.Title}
  927. subTitle={Locale.Settings.Access.OpenAI.ApiKey.SubTitle}
  928. >
  929. <PasswordInput
  930. value={accessStore.openaiApiKey}
  931. type="text"
  932. placeholder={
  933. Locale.Settings.Access.OpenAI.ApiKey.Placeholder
  934. }
  935. onChange={(e) => {
  936. accessStore.update(
  937. (access) =>
  938. (access.openaiApiKey = e.currentTarget.value),
  939. );
  940. }}
  941. />
  942. </ListItem>
  943. </>
  944. )}
  945. {accessStore.provider === ServiceProvider.Azure && (
  946. <>
  947. <ListItem
  948. title={Locale.Settings.Access.Azure.Endpoint.Title}
  949. subTitle={
  950. Locale.Settings.Access.Azure.Endpoint.SubTitle +
  951. Azure.ExampleEndpoint
  952. }
  953. >
  954. <input
  955. type="text"
  956. value={accessStore.azureUrl}
  957. placeholder={Azure.ExampleEndpoint}
  958. onChange={(e) =>
  959. accessStore.update(
  960. (access) =>
  961. (access.azureUrl = e.currentTarget.value),
  962. )
  963. }
  964. ></input>
  965. </ListItem>
  966. <ListItem
  967. title={Locale.Settings.Access.Azure.ApiKey.Title}
  968. subTitle={Locale.Settings.Access.Azure.ApiKey.SubTitle}
  969. >
  970. <PasswordInput
  971. value={accessStore.azureApiKey}
  972. type="text"
  973. placeholder={
  974. Locale.Settings.Access.Azure.ApiKey.Placeholder
  975. }
  976. onChange={(e) => {
  977. accessStore.update(
  978. (access) =>
  979. (access.azureApiKey = e.currentTarget.value),
  980. );
  981. }}
  982. />
  983. </ListItem>
  984. <ListItem
  985. title={Locale.Settings.Access.Azure.ApiVerion.Title}
  986. subTitle={
  987. Locale.Settings.Access.Azure.ApiVerion.SubTitle
  988. }
  989. >
  990. <input
  991. type="text"
  992. value={accessStore.azureApiVersion}
  993. placeholder="2023-08-01-preview"
  994. onChange={(e) =>
  995. accessStore.update(
  996. (access) =>
  997. (access.azureApiVersion =
  998. e.currentTarget.value),
  999. )
  1000. }
  1001. ></input>
  1002. </ListItem>
  1003. </>
  1004. )}
  1005. {accessStore.provider === ServiceProvider.Google && (
  1006. <>
  1007. <ListItem
  1008. title={Locale.Settings.Access.Google.Endpoint.Title}
  1009. subTitle={
  1010. Locale.Settings.Access.Google.Endpoint.SubTitle +
  1011. Google.ExampleEndpoint
  1012. }
  1013. >
  1014. <input
  1015. type="text"
  1016. value={accessStore.googleUrl}
  1017. placeholder={Google.ExampleEndpoint}
  1018. onChange={(e) =>
  1019. accessStore.update(
  1020. (access) =>
  1021. (access.googleUrl = e.currentTarget.value),
  1022. )
  1023. }
  1024. ></input>
  1025. </ListItem>
  1026. <ListItem
  1027. title={Locale.Settings.Access.Google.ApiKey.Title}
  1028. subTitle={Locale.Settings.Access.Google.ApiKey.SubTitle}
  1029. >
  1030. <PasswordInput
  1031. value={accessStore.googleApiKey}
  1032. type="text"
  1033. placeholder={
  1034. Locale.Settings.Access.Google.ApiKey.Placeholder
  1035. }
  1036. onChange={(e) => {
  1037. accessStore.update(
  1038. (access) =>
  1039. (access.googleApiKey = e.currentTarget.value),
  1040. );
  1041. }}
  1042. />
  1043. </ListItem>
  1044. <ListItem
  1045. title={Locale.Settings.Access.Google.ApiVersion.Title}
  1046. subTitle={
  1047. Locale.Settings.Access.Google.ApiVersion.SubTitle
  1048. }
  1049. >
  1050. <input
  1051. type="text"
  1052. value={accessStore.googleApiVersion}
  1053. placeholder="2023-08-01-preview"
  1054. onChange={(e) =>
  1055. accessStore.update(
  1056. (access) =>
  1057. (access.googleApiVersion =
  1058. e.currentTarget.value),
  1059. )
  1060. }
  1061. ></input>
  1062. </ListItem>
  1063. <ListItem
  1064. title={
  1065. Locale.Settings.Access.Google.GoogleSafetySettings
  1066. .Title
  1067. }
  1068. subTitle={
  1069. Locale.Settings.Access.Google.GoogleSafetySettings
  1070. .SubTitle
  1071. }
  1072. >
  1073. <Select
  1074. value={accessStore.googleSafetySettings}
  1075. onChange={(e) => {
  1076. accessStore.update(
  1077. (access) =>
  1078. (access.googleSafetySettings = e.target
  1079. .value as GoogleSafetySettingsThreshold),
  1080. );
  1081. }}
  1082. >
  1083. {Object.entries(GoogleSafetySettingsThreshold).map(
  1084. ([k, v]) => (
  1085. <option value={v} key={k}>
  1086. {k}
  1087. </option>
  1088. ),
  1089. )}
  1090. </Select>
  1091. </ListItem>
  1092. </>
  1093. )}
  1094. {accessStore.provider === ServiceProvider.Anthropic && (
  1095. <>
  1096. <ListItem
  1097. title={Locale.Settings.Access.Anthropic.Endpoint.Title}
  1098. subTitle={
  1099. Locale.Settings.Access.Anthropic.Endpoint.SubTitle +
  1100. Anthropic.ExampleEndpoint
  1101. }
  1102. >
  1103. <input
  1104. type="text"
  1105. value={accessStore.anthropicUrl}
  1106. placeholder={Anthropic.ExampleEndpoint}
  1107. onChange={(e) =>
  1108. accessStore.update(
  1109. (access) =>
  1110. (access.anthropicUrl = e.currentTarget.value),
  1111. )
  1112. }
  1113. ></input>
  1114. </ListItem>
  1115. <ListItem
  1116. title={Locale.Settings.Access.Anthropic.ApiKey.Title}
  1117. subTitle={
  1118. Locale.Settings.Access.Anthropic.ApiKey.SubTitle
  1119. }
  1120. >
  1121. <PasswordInput
  1122. value={accessStore.anthropicApiKey}
  1123. type="text"
  1124. placeholder={
  1125. Locale.Settings.Access.Anthropic.ApiKey.Placeholder
  1126. }
  1127. onChange={(e) => {
  1128. accessStore.update(
  1129. (access) =>
  1130. (access.anthropicApiKey =
  1131. e.currentTarget.value),
  1132. );
  1133. }}
  1134. />
  1135. </ListItem>
  1136. <ListItem
  1137. title={Locale.Settings.Access.Anthropic.ApiVerion.Title}
  1138. subTitle={
  1139. Locale.Settings.Access.Anthropic.ApiVerion.SubTitle
  1140. }
  1141. >
  1142. <input
  1143. type="text"
  1144. value={accessStore.anthropicApiVersion}
  1145. placeholder={Anthropic.Vision}
  1146. onChange={(e) =>
  1147. accessStore.update(
  1148. (access) =>
  1149. (access.anthropicApiVersion =
  1150. e.currentTarget.value),
  1151. )
  1152. }
  1153. ></input>
  1154. </ListItem>
  1155. </>
  1156. )}
  1157. {accessStore.provider === ServiceProvider.Baidu && (
  1158. <>
  1159. <ListItem
  1160. title={Locale.Settings.Access.Baidu.Endpoint.Title}
  1161. subTitle={
  1162. Locale.Settings.Access.Baidu.Endpoint.SubTitle
  1163. }
  1164. >
  1165. <input
  1166. type="text"
  1167. value={accessStore.baiduUrl}
  1168. placeholder={Baidu.ExampleEndpoint}
  1169. onChange={(e) =>
  1170. accessStore.update(
  1171. (access) =>
  1172. (access.baiduUrl = e.currentTarget.value),
  1173. )
  1174. }
  1175. ></input>
  1176. </ListItem>
  1177. <ListItem
  1178. title={Locale.Settings.Access.Baidu.ApiKey.Title}
  1179. subTitle={Locale.Settings.Access.Baidu.ApiKey.SubTitle}
  1180. >
  1181. <PasswordInput
  1182. value={accessStore.baiduApiKey}
  1183. type="text"
  1184. placeholder={
  1185. Locale.Settings.Access.Baidu.ApiKey.Placeholder
  1186. }
  1187. onChange={(e) => {
  1188. accessStore.update(
  1189. (access) =>
  1190. (access.baiduApiKey = e.currentTarget.value),
  1191. );
  1192. }}
  1193. />
  1194. </ListItem>
  1195. <ListItem
  1196. title={Locale.Settings.Access.Baidu.SecretKey.Title}
  1197. subTitle={
  1198. Locale.Settings.Access.Baidu.SecretKey.SubTitle
  1199. }
  1200. >
  1201. <PasswordInput
  1202. value={accessStore.baiduSecretKey}
  1203. type="text"
  1204. placeholder={
  1205. Locale.Settings.Access.Baidu.SecretKey.Placeholder
  1206. }
  1207. onChange={(e) => {
  1208. accessStore.update(
  1209. (access) =>
  1210. (access.baiduSecretKey = e.currentTarget.value),
  1211. );
  1212. }}
  1213. />
  1214. </ListItem>
  1215. </>
  1216. )}
  1217. {accessStore.provider === ServiceProvider.ByteDance && (
  1218. <>
  1219. <ListItem
  1220. title={Locale.Settings.Access.ByteDance.Endpoint.Title}
  1221. subTitle={
  1222. Locale.Settings.Access.ByteDance.Endpoint.SubTitle +
  1223. ByteDance.ExampleEndpoint
  1224. }
  1225. >
  1226. <input
  1227. type="text"
  1228. value={accessStore.bytedanceUrl}
  1229. placeholder={ByteDance.ExampleEndpoint}
  1230. onChange={(e) =>
  1231. accessStore.update(
  1232. (access) =>
  1233. (access.bytedanceUrl = e.currentTarget.value),
  1234. )
  1235. }
  1236. ></input>
  1237. </ListItem>
  1238. <ListItem
  1239. title={Locale.Settings.Access.ByteDance.ApiKey.Title}
  1240. subTitle={
  1241. Locale.Settings.Access.ByteDance.ApiKey.SubTitle
  1242. }
  1243. >
  1244. <PasswordInput
  1245. value={accessStore.bytedanceApiKey}
  1246. type="text"
  1247. placeholder={
  1248. Locale.Settings.Access.ByteDance.ApiKey.Placeholder
  1249. }
  1250. onChange={(e) => {
  1251. accessStore.update(
  1252. (access) =>
  1253. (access.bytedanceApiKey =
  1254. e.currentTarget.value),
  1255. );
  1256. }}
  1257. />
  1258. </ListItem>
  1259. </>
  1260. )}
  1261. {accessStore.provider === ServiceProvider.Alibaba && (
  1262. <>
  1263. <ListItem
  1264. title={Locale.Settings.Access.Alibaba.Endpoint.Title}
  1265. subTitle={
  1266. Locale.Settings.Access.Alibaba.Endpoint.SubTitle +
  1267. Alibaba.ExampleEndpoint
  1268. }
  1269. >
  1270. <input
  1271. type="text"
  1272. value={accessStore.alibabaUrl}
  1273. placeholder={Alibaba.ExampleEndpoint}
  1274. onChange={(e) =>
  1275. accessStore.update(
  1276. (access) =>
  1277. (access.alibabaUrl = e.currentTarget.value),
  1278. )
  1279. }
  1280. ></input>
  1281. </ListItem>
  1282. <ListItem
  1283. title={Locale.Settings.Access.Alibaba.ApiKey.Title}
  1284. subTitle={
  1285. Locale.Settings.Access.Alibaba.ApiKey.SubTitle
  1286. }
  1287. >
  1288. <PasswordInput
  1289. value={accessStore.alibabaApiKey}
  1290. type="text"
  1291. placeholder={
  1292. Locale.Settings.Access.Alibaba.ApiKey.Placeholder
  1293. }
  1294. onChange={(e) => {
  1295. accessStore.update(
  1296. (access) =>
  1297. (access.alibabaApiKey = e.currentTarget.value),
  1298. );
  1299. }}
  1300. />
  1301. </ListItem>
  1302. </>
  1303. )}
  1304. </>
  1305. )}
  1306. </>
  1307. )}
  1308. {!shouldHideBalanceQuery && !clientConfig?.isApp ? (
  1309. <ListItem
  1310. title={Locale.Settings.Usage.Title}
  1311. subTitle={
  1312. showUsage
  1313. ? loadingUsage
  1314. ? Locale.Settings.Usage.IsChecking
  1315. : Locale.Settings.Usage.SubTitle(
  1316. usage?.used ?? "[?]",
  1317. usage?.subscription ?? "[?]",
  1318. )
  1319. : Locale.Settings.Usage.NoAccess
  1320. }
  1321. >
  1322. {!showUsage || loadingUsage ? (
  1323. <div />
  1324. ) : (
  1325. <IconButton
  1326. icon={<ResetIcon></ResetIcon>}
  1327. text={Locale.Settings.Usage.Check}
  1328. onClick={() => checkUsage(true)}
  1329. />
  1330. )}
  1331. </ListItem>
  1332. ) : null}
  1333. <ListItem
  1334. title={Locale.Settings.Access.CustomModel.Title}
  1335. subTitle={Locale.Settings.Access.CustomModel.SubTitle}
  1336. >
  1337. <input
  1338. type="text"
  1339. value={config.customModels}
  1340. placeholder="model1,model2,model3"
  1341. onChange={(e) =>
  1342. config.update(
  1343. (config) => (config.customModels = e.currentTarget.value),
  1344. )
  1345. }
  1346. ></input>
  1347. </ListItem>
  1348. </List>
  1349. <List>
  1350. <ModelConfigList
  1351. modelConfig={config.modelConfig}
  1352. updateConfig={(updater) => {
  1353. const modelConfig = { ...config.modelConfig };
  1354. updater(modelConfig);
  1355. config.update((config) => (config.modelConfig = modelConfig));
  1356. }}
  1357. />
  1358. </List>
  1359. {shouldShowPromptModal && (
  1360. <UserPromptModal onClose={() => setShowPromptModal(false)} />
  1361. )}
  1362. <DangerItems />
  1363. </div>
  1364. </ErrorBoundary>
  1365. );
  1366. }