settings.tsx 56 KB

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