settings.tsx 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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. const accessCodeComponent = showAccessCode && (
  611. <ListItem
  612. title={Locale.Settings.Access.AccessCode.Title}
  613. subTitle={Locale.Settings.Access.AccessCode.SubTitle}
  614. >
  615. <PasswordInput
  616. value={accessStore.accessCode}
  617. type="text"
  618. placeholder={Locale.Settings.Access.AccessCode.Placeholder}
  619. onChange={(e) => {
  620. accessStore.update(
  621. (access) => (access.accessCode = e.currentTarget.value),
  622. );
  623. }}
  624. />
  625. </ListItem>
  626. );
  627. const useCustomConfigComponent = // Conditionally render the following ListItem based on clientConfig.isApp
  628. !clientConfig?.isApp && ( // only show if isApp is false
  629. <ListItem
  630. title={Locale.Settings.Access.CustomEndpoint.Title}
  631. subTitle={Locale.Settings.Access.CustomEndpoint.SubTitle}
  632. >
  633. <input
  634. type="checkbox"
  635. checked={accessStore.useCustomConfig}
  636. onChange={(e) =>
  637. accessStore.update(
  638. (access) => (access.useCustomConfig = e.currentTarget.checked),
  639. )
  640. }
  641. ></input>
  642. </ListItem>
  643. );
  644. const openAIConfigComponent = accessStore.provider ===
  645. ServiceProvider.OpenAI && (
  646. <>
  647. <ListItem
  648. title={Locale.Settings.Access.OpenAI.Endpoint.Title}
  649. subTitle={Locale.Settings.Access.OpenAI.Endpoint.SubTitle}
  650. >
  651. <input
  652. type="text"
  653. value={accessStore.openaiUrl}
  654. placeholder={OPENAI_BASE_URL}
  655. onChange={(e) =>
  656. accessStore.update(
  657. (access) => (access.openaiUrl = e.currentTarget.value),
  658. )
  659. }
  660. ></input>
  661. </ListItem>
  662. <ListItem
  663. title={Locale.Settings.Access.OpenAI.ApiKey.Title}
  664. subTitle={Locale.Settings.Access.OpenAI.ApiKey.SubTitle}
  665. >
  666. <PasswordInput
  667. value={accessStore.openaiApiKey}
  668. type="text"
  669. placeholder={Locale.Settings.Access.OpenAI.ApiKey.Placeholder}
  670. onChange={(e) => {
  671. accessStore.update(
  672. (access) => (access.openaiApiKey = e.currentTarget.value),
  673. );
  674. }}
  675. />
  676. </ListItem>
  677. </>
  678. );
  679. const azureConfigComponent = accessStore.provider ===
  680. ServiceProvider.Azure && (
  681. <>
  682. <ListItem
  683. title={Locale.Settings.Access.Azure.Endpoint.Title}
  684. subTitle={
  685. Locale.Settings.Access.Azure.Endpoint.SubTitle + Azure.ExampleEndpoint
  686. }
  687. >
  688. <input
  689. type="text"
  690. value={accessStore.azureUrl}
  691. placeholder={Azure.ExampleEndpoint}
  692. onChange={(e) =>
  693. accessStore.update(
  694. (access) => (access.azureUrl = e.currentTarget.value),
  695. )
  696. }
  697. ></input>
  698. </ListItem>
  699. <ListItem
  700. title={Locale.Settings.Access.Azure.ApiKey.Title}
  701. subTitle={Locale.Settings.Access.Azure.ApiKey.SubTitle}
  702. >
  703. <PasswordInput
  704. value={accessStore.azureApiKey}
  705. type="text"
  706. placeholder={Locale.Settings.Access.Azure.ApiKey.Placeholder}
  707. onChange={(e) => {
  708. accessStore.update(
  709. (access) => (access.azureApiKey = e.currentTarget.value),
  710. );
  711. }}
  712. />
  713. </ListItem>
  714. <ListItem
  715. title={Locale.Settings.Access.Azure.ApiVerion.Title}
  716. subTitle={Locale.Settings.Access.Azure.ApiVerion.SubTitle}
  717. >
  718. <input
  719. type="text"
  720. value={accessStore.azureApiVersion}
  721. placeholder="2023-08-01-preview"
  722. onChange={(e) =>
  723. accessStore.update(
  724. (access) => (access.azureApiVersion = e.currentTarget.value),
  725. )
  726. }
  727. ></input>
  728. </ListItem>
  729. </>
  730. );
  731. const googleConfigComponent = accessStore.provider ===
  732. ServiceProvider.Google && (
  733. <>
  734. <ListItem
  735. title={Locale.Settings.Access.Google.Endpoint.Title}
  736. subTitle={
  737. Locale.Settings.Access.Google.Endpoint.SubTitle +
  738. Google.ExampleEndpoint
  739. }
  740. >
  741. <input
  742. type="text"
  743. value={accessStore.googleUrl}
  744. placeholder={Google.ExampleEndpoint}
  745. onChange={(e) =>
  746. accessStore.update(
  747. (access) => (access.googleUrl = e.currentTarget.value),
  748. )
  749. }
  750. ></input>
  751. </ListItem>
  752. <ListItem
  753. title={Locale.Settings.Access.Google.ApiKey.Title}
  754. subTitle={Locale.Settings.Access.Google.ApiKey.SubTitle}
  755. >
  756. <PasswordInput
  757. value={accessStore.googleApiKey}
  758. type="text"
  759. placeholder={Locale.Settings.Access.Google.ApiKey.Placeholder}
  760. onChange={(e) => {
  761. accessStore.update(
  762. (access) => (access.googleApiKey = e.currentTarget.value),
  763. );
  764. }}
  765. />
  766. </ListItem>
  767. <ListItem
  768. title={Locale.Settings.Access.Google.ApiVersion.Title}
  769. subTitle={Locale.Settings.Access.Google.ApiVersion.SubTitle}
  770. >
  771. <input
  772. type="text"
  773. value={accessStore.googleApiVersion}
  774. placeholder="2023-08-01-preview"
  775. onChange={(e) =>
  776. accessStore.update(
  777. (access) => (access.googleApiVersion = e.currentTarget.value),
  778. )
  779. }
  780. ></input>
  781. </ListItem>
  782. <ListItem
  783. title={Locale.Settings.Access.Google.GoogleSafetySettings.Title}
  784. subTitle={Locale.Settings.Access.Google.GoogleSafetySettings.SubTitle}
  785. >
  786. <Select
  787. value={accessStore.googleSafetySettings}
  788. onChange={(e) => {
  789. accessStore.update(
  790. (access) =>
  791. (access.googleSafetySettings = e.target
  792. .value as GoogleSafetySettingsThreshold),
  793. );
  794. }}
  795. >
  796. {Object.entries(GoogleSafetySettingsThreshold).map(([k, v]) => (
  797. <option value={v} key={k}>
  798. {k}
  799. </option>
  800. ))}
  801. </Select>
  802. </ListItem>
  803. </>
  804. );
  805. const anthropicConfigComponent = accessStore.provider ===
  806. ServiceProvider.Anthropic && (
  807. <>
  808. <ListItem
  809. title={Locale.Settings.Access.Anthropic.Endpoint.Title}
  810. subTitle={
  811. Locale.Settings.Access.Anthropic.Endpoint.SubTitle +
  812. Anthropic.ExampleEndpoint
  813. }
  814. >
  815. <input
  816. type="text"
  817. value={accessStore.anthropicUrl}
  818. placeholder={Anthropic.ExampleEndpoint}
  819. onChange={(e) =>
  820. accessStore.update(
  821. (access) => (access.anthropicUrl = e.currentTarget.value),
  822. )
  823. }
  824. ></input>
  825. </ListItem>
  826. <ListItem
  827. title={Locale.Settings.Access.Anthropic.ApiKey.Title}
  828. subTitle={Locale.Settings.Access.Anthropic.ApiKey.SubTitle}
  829. >
  830. <PasswordInput
  831. value={accessStore.anthropicApiKey}
  832. type="text"
  833. placeholder={Locale.Settings.Access.Anthropic.ApiKey.Placeholder}
  834. onChange={(e) => {
  835. accessStore.update(
  836. (access) => (access.anthropicApiKey = e.currentTarget.value),
  837. );
  838. }}
  839. />
  840. </ListItem>
  841. <ListItem
  842. title={Locale.Settings.Access.Anthropic.ApiVerion.Title}
  843. subTitle={Locale.Settings.Access.Anthropic.ApiVerion.SubTitle}
  844. >
  845. <input
  846. type="text"
  847. value={accessStore.anthropicApiVersion}
  848. placeholder={Anthropic.Vision}
  849. onChange={(e) =>
  850. accessStore.update(
  851. (access) => (access.anthropicApiVersion = e.currentTarget.value),
  852. )
  853. }
  854. ></input>
  855. </ListItem>
  856. </>
  857. );
  858. const baiduConfigComponent = accessStore.provider ===
  859. ServiceProvider.Baidu && (
  860. <>
  861. <ListItem
  862. title={Locale.Settings.Access.Baidu.Endpoint.Title}
  863. subTitle={Locale.Settings.Access.Baidu.Endpoint.SubTitle}
  864. >
  865. <input
  866. type="text"
  867. value={accessStore.baiduUrl}
  868. placeholder={Baidu.ExampleEndpoint}
  869. onChange={(e) =>
  870. accessStore.update(
  871. (access) => (access.baiduUrl = e.currentTarget.value),
  872. )
  873. }
  874. ></input>
  875. </ListItem>
  876. <ListItem
  877. title={Locale.Settings.Access.Baidu.ApiKey.Title}
  878. subTitle={Locale.Settings.Access.Baidu.ApiKey.SubTitle}
  879. >
  880. <PasswordInput
  881. value={accessStore.baiduApiKey}
  882. type="text"
  883. placeholder={Locale.Settings.Access.Baidu.ApiKey.Placeholder}
  884. onChange={(e) => {
  885. accessStore.update(
  886. (access) => (access.baiduApiKey = e.currentTarget.value),
  887. );
  888. }}
  889. />
  890. </ListItem>
  891. <ListItem
  892. title={Locale.Settings.Access.Baidu.SecretKey.Title}
  893. subTitle={Locale.Settings.Access.Baidu.SecretKey.SubTitle}
  894. >
  895. <PasswordInput
  896. value={accessStore.baiduSecretKey}
  897. type="text"
  898. placeholder={Locale.Settings.Access.Baidu.SecretKey.Placeholder}
  899. onChange={(e) => {
  900. accessStore.update(
  901. (access) => (access.baiduSecretKey = e.currentTarget.value),
  902. );
  903. }}
  904. />
  905. </ListItem>
  906. </>
  907. );
  908. const byteDanceConfigComponent = accessStore.provider ===
  909. ServiceProvider.ByteDance && (
  910. <>
  911. <ListItem
  912. title={Locale.Settings.Access.ByteDance.Endpoint.Title}
  913. subTitle={
  914. Locale.Settings.Access.ByteDance.Endpoint.SubTitle +
  915. ByteDance.ExampleEndpoint
  916. }
  917. >
  918. <input
  919. type="text"
  920. value={accessStore.bytedanceUrl}
  921. placeholder={ByteDance.ExampleEndpoint}
  922. onChange={(e) =>
  923. accessStore.update(
  924. (access) => (access.bytedanceUrl = e.currentTarget.value),
  925. )
  926. }
  927. ></input>
  928. </ListItem>
  929. <ListItem
  930. title={Locale.Settings.Access.ByteDance.ApiKey.Title}
  931. subTitle={Locale.Settings.Access.ByteDance.ApiKey.SubTitle}
  932. >
  933. <PasswordInput
  934. value={accessStore.bytedanceApiKey}
  935. type="text"
  936. placeholder={Locale.Settings.Access.ByteDance.ApiKey.Placeholder}
  937. onChange={(e) => {
  938. accessStore.update(
  939. (access) => (access.bytedanceApiKey = e.currentTarget.value),
  940. );
  941. }}
  942. />
  943. </ListItem>
  944. </>
  945. );
  946. const alibabaConfigComponent = accessStore.provider ===
  947. ServiceProvider.Alibaba && (
  948. <>
  949. <ListItem
  950. title={Locale.Settings.Access.Alibaba.Endpoint.Title}
  951. subTitle={
  952. Locale.Settings.Access.Alibaba.Endpoint.SubTitle +
  953. Alibaba.ExampleEndpoint
  954. }
  955. >
  956. <input
  957. type="text"
  958. value={accessStore.alibabaUrl}
  959. placeholder={Alibaba.ExampleEndpoint}
  960. onChange={(e) =>
  961. accessStore.update(
  962. (access) => (access.alibabaUrl = e.currentTarget.value),
  963. )
  964. }
  965. ></input>
  966. </ListItem>
  967. <ListItem
  968. title={Locale.Settings.Access.Alibaba.ApiKey.Title}
  969. subTitle={Locale.Settings.Access.Alibaba.ApiKey.SubTitle}
  970. >
  971. <PasswordInput
  972. value={accessStore.alibabaApiKey}
  973. type="text"
  974. placeholder={Locale.Settings.Access.Alibaba.ApiKey.Placeholder}
  975. onChange={(e) => {
  976. accessStore.update(
  977. (access) => (access.alibabaApiKey = e.currentTarget.value),
  978. );
  979. }}
  980. />
  981. </ListItem>
  982. </>
  983. );
  984. return (
  985. <ErrorBoundary>
  986. <div className="window-header" data-tauri-drag-region>
  987. <div className="window-header-title">
  988. <div className="window-header-main-title">
  989. {Locale.Settings.Title}
  990. </div>
  991. <div className="window-header-sub-title">
  992. {Locale.Settings.SubTitle}
  993. </div>
  994. </div>
  995. <div className="window-actions">
  996. <div className="window-action-button"></div>
  997. <div className="window-action-button"></div>
  998. <div className="window-action-button">
  999. <IconButton
  1000. icon={<CloseIcon />}
  1001. onClick={() => navigate(Path.Home)}
  1002. bordered
  1003. />
  1004. </div>
  1005. </div>
  1006. </div>
  1007. <div className={styles["settings"]}>
  1008. <List>
  1009. <ListItem title={Locale.Settings.Avatar}>
  1010. <Popover
  1011. onClose={() => setShowEmojiPicker(false)}
  1012. content={
  1013. <AvatarPicker
  1014. onEmojiClick={(avatar: string) => {
  1015. updateConfig((config) => (config.avatar = avatar));
  1016. setShowEmojiPicker(false);
  1017. }}
  1018. />
  1019. }
  1020. open={showEmojiPicker}
  1021. >
  1022. <div
  1023. className={styles.avatar}
  1024. onClick={() => {
  1025. setShowEmojiPicker(!showEmojiPicker);
  1026. }}
  1027. >
  1028. <Avatar avatar={config.avatar} />
  1029. </div>
  1030. </Popover>
  1031. </ListItem>
  1032. <ListItem
  1033. title={Locale.Settings.Update.Version(currentVersion ?? "unknown")}
  1034. subTitle={
  1035. checkingUpdate
  1036. ? Locale.Settings.Update.IsChecking
  1037. : hasNewVersion
  1038. ? Locale.Settings.Update.FoundUpdate(remoteId ?? "ERROR")
  1039. : Locale.Settings.Update.IsLatest
  1040. }
  1041. >
  1042. {checkingUpdate ? (
  1043. <LoadingIcon />
  1044. ) : hasNewVersion ? (
  1045. <Link href={updateUrl} target="_blank" className="link">
  1046. {Locale.Settings.Update.GoToUpdate}
  1047. </Link>
  1048. ) : (
  1049. <IconButton
  1050. icon={<ResetIcon></ResetIcon>}
  1051. text={Locale.Settings.Update.CheckUpdate}
  1052. onClick={() => checkUpdate(true)}
  1053. />
  1054. )}
  1055. </ListItem>
  1056. <ListItem title={Locale.Settings.SendKey}>
  1057. <Select
  1058. value={config.submitKey}
  1059. onChange={(e) => {
  1060. updateConfig(
  1061. (config) =>
  1062. (config.submitKey = e.target.value as any as SubmitKey),
  1063. );
  1064. }}
  1065. >
  1066. {Object.values(SubmitKey).map((v) => (
  1067. <option value={v} key={v}>
  1068. {v}
  1069. </option>
  1070. ))}
  1071. </Select>
  1072. </ListItem>
  1073. <ListItem title={Locale.Settings.Theme}>
  1074. <Select
  1075. value={config.theme}
  1076. onChange={(e) => {
  1077. updateConfig(
  1078. (config) => (config.theme = e.target.value as any as Theme),
  1079. );
  1080. }}
  1081. >
  1082. {Object.values(Theme).map((v) => (
  1083. <option value={v} key={v}>
  1084. {v}
  1085. </option>
  1086. ))}
  1087. </Select>
  1088. </ListItem>
  1089. <ListItem title={Locale.Settings.Lang.Name}>
  1090. <Select
  1091. value={getLang()}
  1092. onChange={(e) => {
  1093. changeLang(e.target.value as any);
  1094. }}
  1095. >
  1096. {AllLangs.map((lang) => (
  1097. <option value={lang} key={lang}>
  1098. {ALL_LANG_OPTIONS[lang]}
  1099. </option>
  1100. ))}
  1101. </Select>
  1102. </ListItem>
  1103. <ListItem
  1104. title={Locale.Settings.FontSize.Title}
  1105. subTitle={Locale.Settings.FontSize.SubTitle}
  1106. >
  1107. <InputRange
  1108. title={`${config.fontSize ?? 14}px`}
  1109. value={config.fontSize}
  1110. min="12"
  1111. max="40"
  1112. step="1"
  1113. onChange={(e) =>
  1114. updateConfig(
  1115. (config) =>
  1116. (config.fontSize = Number.parseInt(e.currentTarget.value)),
  1117. )
  1118. }
  1119. ></InputRange>
  1120. </ListItem>
  1121. <ListItem
  1122. title={Locale.Settings.AutoGenerateTitle.Title}
  1123. subTitle={Locale.Settings.AutoGenerateTitle.SubTitle}
  1124. >
  1125. <input
  1126. type="checkbox"
  1127. checked={config.enableAutoGenerateTitle}
  1128. onChange={(e) =>
  1129. updateConfig(
  1130. (config) =>
  1131. (config.enableAutoGenerateTitle = e.currentTarget.checked),
  1132. )
  1133. }
  1134. ></input>
  1135. </ListItem>
  1136. <ListItem
  1137. title={Locale.Settings.SendPreviewBubble.Title}
  1138. subTitle={Locale.Settings.SendPreviewBubble.SubTitle}
  1139. >
  1140. <input
  1141. type="checkbox"
  1142. checked={config.sendPreviewBubble}
  1143. onChange={(e) =>
  1144. updateConfig(
  1145. (config) =>
  1146. (config.sendPreviewBubble = e.currentTarget.checked),
  1147. )
  1148. }
  1149. ></input>
  1150. </ListItem>
  1151. </List>
  1152. <SyncItems />
  1153. <List>
  1154. <ListItem
  1155. title={Locale.Settings.Mask.Splash.Title}
  1156. subTitle={Locale.Settings.Mask.Splash.SubTitle}
  1157. >
  1158. <input
  1159. type="checkbox"
  1160. checked={!config.dontShowMaskSplashScreen}
  1161. onChange={(e) =>
  1162. updateConfig(
  1163. (config) =>
  1164. (config.dontShowMaskSplashScreen =
  1165. !e.currentTarget.checked),
  1166. )
  1167. }
  1168. ></input>
  1169. </ListItem>
  1170. <ListItem
  1171. title={Locale.Settings.Mask.Builtin.Title}
  1172. subTitle={Locale.Settings.Mask.Builtin.SubTitle}
  1173. >
  1174. <input
  1175. type="checkbox"
  1176. checked={config.hideBuiltinMasks}
  1177. onChange={(e) =>
  1178. updateConfig(
  1179. (config) =>
  1180. (config.hideBuiltinMasks = e.currentTarget.checked),
  1181. )
  1182. }
  1183. ></input>
  1184. </ListItem>
  1185. </List>
  1186. <List>
  1187. <ListItem
  1188. title={Locale.Settings.Prompt.Disable.Title}
  1189. subTitle={Locale.Settings.Prompt.Disable.SubTitle}
  1190. >
  1191. <input
  1192. type="checkbox"
  1193. checked={config.disablePromptHint}
  1194. onChange={(e) =>
  1195. updateConfig(
  1196. (config) =>
  1197. (config.disablePromptHint = e.currentTarget.checked),
  1198. )
  1199. }
  1200. ></input>
  1201. </ListItem>
  1202. <ListItem
  1203. title={Locale.Settings.Prompt.List}
  1204. subTitle={Locale.Settings.Prompt.ListCount(
  1205. builtinCount,
  1206. customCount,
  1207. )}
  1208. >
  1209. <IconButton
  1210. icon={<EditIcon />}
  1211. text={Locale.Settings.Prompt.Edit}
  1212. onClick={() => setShowPromptModal(true)}
  1213. />
  1214. </ListItem>
  1215. </List>
  1216. <List id={SlotID.CustomModel}>
  1217. {accessCodeComponent}
  1218. {!accessStore.hideUserApiKey && (
  1219. <>
  1220. {useCustomConfigComponent}
  1221. {accessStore.useCustomConfig && (
  1222. <>
  1223. <ListItem
  1224. title={Locale.Settings.Access.Provider.Title}
  1225. subTitle={Locale.Settings.Access.Provider.SubTitle}
  1226. >
  1227. <Select
  1228. value={accessStore.provider}
  1229. onChange={(e) => {
  1230. accessStore.update(
  1231. (access) =>
  1232. (access.provider = e.target
  1233. .value as ServiceProvider),
  1234. );
  1235. }}
  1236. >
  1237. {Object.entries(ServiceProvider).map(([k, v]) => (
  1238. <option value={v} key={k}>
  1239. {k}
  1240. </option>
  1241. ))}
  1242. </Select>
  1243. </ListItem>
  1244. {openAIConfigComponent}
  1245. {azureConfigComponent}
  1246. {googleConfigComponent}
  1247. {anthropicConfigComponent}
  1248. {baiduConfigComponent}
  1249. {byteDanceConfigComponent}
  1250. {alibabaConfigComponent}
  1251. </>
  1252. )}
  1253. </>
  1254. )}
  1255. {!shouldHideBalanceQuery && !clientConfig?.isApp ? (
  1256. <ListItem
  1257. title={Locale.Settings.Usage.Title}
  1258. subTitle={
  1259. showUsage
  1260. ? loadingUsage
  1261. ? Locale.Settings.Usage.IsChecking
  1262. : Locale.Settings.Usage.SubTitle(
  1263. usage?.used ?? "[?]",
  1264. usage?.subscription ?? "[?]",
  1265. )
  1266. : Locale.Settings.Usage.NoAccess
  1267. }
  1268. >
  1269. {!showUsage || loadingUsage ? (
  1270. <div />
  1271. ) : (
  1272. <IconButton
  1273. icon={<ResetIcon></ResetIcon>}
  1274. text={Locale.Settings.Usage.Check}
  1275. onClick={() => checkUsage(true)}
  1276. />
  1277. )}
  1278. </ListItem>
  1279. ) : null}
  1280. <ListItem
  1281. title={Locale.Settings.Access.CustomModel.Title}
  1282. subTitle={Locale.Settings.Access.CustomModel.SubTitle}
  1283. >
  1284. <input
  1285. type="text"
  1286. value={config.customModels}
  1287. placeholder="model1,model2,model3"
  1288. onChange={(e) =>
  1289. config.update(
  1290. (config) => (config.customModels = e.currentTarget.value),
  1291. )
  1292. }
  1293. ></input>
  1294. </ListItem>
  1295. </List>
  1296. <List>
  1297. <ModelConfigList
  1298. modelConfig={config.modelConfig}
  1299. updateConfig={(updater) => {
  1300. const modelConfig = { ...config.modelConfig };
  1301. updater(modelConfig);
  1302. config.update((config) => (config.modelConfig = modelConfig));
  1303. }}
  1304. />
  1305. </List>
  1306. {shouldShowPromptModal && (
  1307. <UserPromptModal onClose={() => setShowPromptModal(false)} />
  1308. )}
  1309. <DangerItems />
  1310. </div>
  1311. </ErrorBoundary>
  1312. );
  1313. }