Interface.svelte 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <script lang="ts">
  2. import { getBackendConfig } from '$lib/apis';
  3. import { setDefaultPromptSuggestions } from '$lib/apis/configs';
  4. import { config, models, settings, user } from '$lib/stores';
  5. import { createEventDispatcher, onMount, getContext } from 'svelte';
  6. import { toast } from 'svelte-sonner';
  7. import Tooltip from '$lib/components/common/Tooltip.svelte';
  8. import { updateUserInfo } from '$lib/apis/users';
  9. import { getUserPosition } from '$lib/utils';
  10. const dispatch = createEventDispatcher();
  11. const i18n = getContext('i18n');
  12. export let saveSettings: Function;
  13. let backgroundImageUrl = null;
  14. let inputFiles = null;
  15. let filesInputElement;
  16. // Addons
  17. let titleAutoGenerate = true;
  18. let responseAutoCopy = false;
  19. let widescreenMode = false;
  20. let splitLargeChunks = false;
  21. let scrollOnBranchChange = true;
  22. let userLocation = false;
  23. // Interface
  24. let defaultModelId = '';
  25. let showUsername = false;
  26. let chatBubble = true;
  27. let chatDirection: 'LTR' | 'RTL' = 'LTR';
  28. let showEmojiInCall = false;
  29. let voiceInterruption = false;
  30. let hapticFeedback = false;
  31. const toggleSplitLargeChunks = async () => {
  32. splitLargeChunks = !splitLargeChunks;
  33. saveSettings({ splitLargeChunks: splitLargeChunks });
  34. };
  35. const togglesScrollOnBranchChange = async () => {
  36. scrollOnBranchChange = !scrollOnBranchChange;
  37. saveSettings({ scrollOnBranchChange: scrollOnBranchChange });
  38. };
  39. const togglewidescreenMode = async () => {
  40. widescreenMode = !widescreenMode;
  41. saveSettings({ widescreenMode: widescreenMode });
  42. };
  43. const toggleChatBubble = async () => {
  44. chatBubble = !chatBubble;
  45. saveSettings({ chatBubble: chatBubble });
  46. };
  47. const toggleShowUsername = async () => {
  48. showUsername = !showUsername;
  49. saveSettings({ showUsername: showUsername });
  50. };
  51. const toggleEmojiInCall = async () => {
  52. showEmojiInCall = !showEmojiInCall;
  53. saveSettings({ showEmojiInCall: showEmojiInCall });
  54. };
  55. const toggleVoiceInterruption = async () => {
  56. voiceInterruption = !voiceInterruption;
  57. saveSettings({ voiceInterruption: voiceInterruption });
  58. };
  59. const toggleHapticFeedback = async () => {
  60. hapticFeedback = !hapticFeedback;
  61. saveSettings({ hapticFeedback: hapticFeedback });
  62. };
  63. const toggleUserLocation = async () => {
  64. userLocation = !userLocation;
  65. if (userLocation) {
  66. const position = await getUserPosition().catch((error) => {
  67. toast.error(error.message);
  68. return null;
  69. });
  70. if (position) {
  71. await updateUserInfo(localStorage.token, { location: position });
  72. toast.success($i18n.t('User location successfully retrieved.'));
  73. } else {
  74. userLocation = false;
  75. }
  76. }
  77. saveSettings({ userLocation });
  78. };
  79. const toggleTitleAutoGenerate = async () => {
  80. titleAutoGenerate = !titleAutoGenerate;
  81. saveSettings({
  82. title: {
  83. ...$settings.title,
  84. auto: titleAutoGenerate
  85. }
  86. });
  87. };
  88. const toggleResponseAutoCopy = async () => {
  89. const permission = await navigator.clipboard
  90. .readText()
  91. .then(() => {
  92. return 'granted';
  93. })
  94. .catch(() => {
  95. return '';
  96. });
  97. console.log(permission);
  98. if (permission === 'granted') {
  99. responseAutoCopy = !responseAutoCopy;
  100. saveSettings({ responseAutoCopy: responseAutoCopy });
  101. } else {
  102. toast.error(
  103. $i18n.t(
  104. 'Clipboard write permission denied. Please check your browser settings to grant the necessary access.'
  105. )
  106. );
  107. }
  108. };
  109. const toggleChangeChatDirection = async () => {
  110. chatDirection = chatDirection === 'LTR' ? 'RTL' : 'LTR';
  111. saveSettings({ chatDirection });
  112. };
  113. const updateInterfaceHandler = async () => {
  114. saveSettings({
  115. models: [defaultModelId]
  116. });
  117. };
  118. onMount(async () => {
  119. titleAutoGenerate = $settings?.title?.auto ?? true;
  120. responseAutoCopy = $settings.responseAutoCopy ?? false;
  121. showUsername = $settings.showUsername ?? false;
  122. showEmojiInCall = $settings.showEmojiInCall ?? false;
  123. voiceInterruption = $settings.voiceInterruption ?? false;
  124. chatBubble = $settings.chatBubble ?? true;
  125. widescreenMode = $settings.widescreenMode ?? false;
  126. splitLargeChunks = $settings.splitLargeChunks ?? false;
  127. scrollOnBranchChange = $settings.scrollOnBranchChange ?? true;
  128. chatDirection = $settings.chatDirection ?? 'LTR';
  129. userLocation = $settings.userLocation ?? false;
  130. hapticFeedback = $settings.hapticFeedback ?? false;
  131. defaultModelId = $settings?.models?.at(0) ?? '';
  132. if ($config?.default_models) {
  133. defaultModelId = $config.default_models.split(',')[0];
  134. }
  135. backgroundImageUrl = $settings.backgroundImageUrl ?? null;
  136. });
  137. </script>
  138. <form
  139. class="flex flex-col h-full justify-between space-y-3 text-sm"
  140. on:submit|preventDefault={() => {
  141. updateInterfaceHandler();
  142. dispatch('save');
  143. }}
  144. >
  145. <input
  146. bind:this={filesInputElement}
  147. bind:files={inputFiles}
  148. type="file"
  149. hidden
  150. accept="image/*"
  151. on:change={() => {
  152. let reader = new FileReader();
  153. reader.onload = (event) => {
  154. let originalImageUrl = `${event.target.result}`;
  155. backgroundImageUrl = originalImageUrl;
  156. saveSettings({ backgroundImageUrl });
  157. };
  158. if (
  159. inputFiles &&
  160. inputFiles.length > 0 &&
  161. ['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(inputFiles[0]['type'])
  162. ) {
  163. reader.readAsDataURL(inputFiles[0]);
  164. } else {
  165. console.log(`Unsupported File Type '${inputFiles[0]['type']}'.`);
  166. inputFiles = null;
  167. }
  168. }}
  169. />
  170. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[25rem] scrollbar-hidden">
  171. <div class=" space-y-1 mb-3">
  172. <div class="mb-2">
  173. <div class="flex justify-between items-center text-xs">
  174. <div class=" text-sm font-medium">{$i18n.t('Default Model')}</div>
  175. </div>
  176. </div>
  177. <div class="flex-1 mr-2">
  178. <select
  179. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  180. bind:value={defaultModelId}
  181. placeholder="Select a model"
  182. >
  183. <option value="" disabled selected>{$i18n.t('Select a model')}</option>
  184. {#each $models.filter((model) => model.id) as model}
  185. <option value={model.id} class="bg-gray-100 dark:bg-gray-700">{model.name}</option>
  186. {/each}
  187. </select>
  188. </div>
  189. </div>
  190. <hr class=" dark:border-gray-850" />
  191. <div>
  192. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('UI')}</div>
  193. <div>
  194. <div class=" py-0.5 flex w-full justify-between">
  195. <div class=" self-center text-xs">{$i18n.t('Chat Bubble UI')}</div>
  196. <button
  197. class="p-1 px-3 text-xs flex rounded transition"
  198. on:click={() => {
  199. toggleChatBubble();
  200. }}
  201. type="button"
  202. >
  203. {#if chatBubble === true}
  204. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  205. {:else}
  206. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  207. {/if}
  208. </button>
  209. </div>
  210. </div>
  211. {#if !$settings.chatBubble}
  212. <div>
  213. <div class=" py-0.5 flex w-full justify-between">
  214. <div class=" self-center text-xs">
  215. {$i18n.t('Display the username instead of You in the Chat')}
  216. </div>
  217. <button
  218. class="p-1 px-3 text-xs flex rounded transition"
  219. on:click={() => {
  220. toggleShowUsername();
  221. }}
  222. type="button"
  223. >
  224. {#if showUsername === true}
  225. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  226. {:else}
  227. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  228. {/if}
  229. </button>
  230. </div>
  231. </div>
  232. {/if}
  233. <div>
  234. <div class=" py-0.5 flex w-full justify-between">
  235. <div class=" self-center text-xs">{$i18n.t('Widescreen Mode')}</div>
  236. <button
  237. class="p-1 px-3 text-xs flex rounded transition"
  238. on:click={() => {
  239. togglewidescreenMode();
  240. }}
  241. type="button"
  242. >
  243. {#if widescreenMode === true}
  244. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  245. {:else}
  246. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  247. {/if}
  248. </button>
  249. </div>
  250. </div>
  251. <div>
  252. <div class=" py-0.5 flex w-full justify-between">
  253. <div class=" self-center text-xs">{$i18n.t('Chat direction')}</div>
  254. <button
  255. class="p-1 px-3 text-xs flex rounded transition"
  256. on:click={toggleChangeChatDirection}
  257. type="button"
  258. >
  259. {#if chatDirection === 'LTR'}
  260. <span class="ml-2 self-center">{$i18n.t('LTR')}</span>
  261. {:else}
  262. <span class="ml-2 self-center">{$i18n.t('RTL')}</span>
  263. {/if}
  264. </button>
  265. </div>
  266. </div>
  267. <div>
  268. <div class=" py-0.5 flex w-full justify-between">
  269. <div class=" self-center text-xs">
  270. {$i18n.t('Fluidly stream large external response chunks')}
  271. </div>
  272. <button
  273. class="p-1 px-3 text-xs flex rounded transition"
  274. on:click={() => {
  275. toggleSplitLargeChunks();
  276. }}
  277. type="button"
  278. >
  279. {#if splitLargeChunks === true}
  280. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  281. {:else}
  282. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  283. {/if}
  284. </button>
  285. </div>
  286. </div>
  287. <div>
  288. <div class=" py-0.5 flex w-full justify-between">
  289. <div class=" self-center text-xs">
  290. {$i18n.t('Scroll to bottom when switching between branches')}
  291. </div>
  292. <button
  293. class="p-1 px-3 text-xs flex rounded transition"
  294. on:click={() => {
  295. togglesScrollOnBranchChange();
  296. }}
  297. type="button"
  298. >
  299. {#if scrollOnBranchChange === true}
  300. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  301. {:else}
  302. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  303. {/if}
  304. </button>
  305. </div>
  306. </div>
  307. <div>
  308. <div class=" py-0.5 flex w-full justify-between">
  309. <div class=" self-center text-xs">
  310. {$i18n.t('Chat Background Image')}
  311. </div>
  312. <button
  313. class="p-1 px-3 text-xs flex rounded transition"
  314. on:click={() => {
  315. if (backgroundImageUrl !== null) {
  316. backgroundImageUrl = null;
  317. saveSettings({ backgroundImageUrl });
  318. } else {
  319. filesInputElement.click();
  320. }
  321. }}
  322. type="button"
  323. >
  324. {#if backgroundImageUrl !== null}
  325. <span class="ml-2 self-center">{$i18n.t('Reset')}</span>
  326. {:else}
  327. <span class="ml-2 self-center">{$i18n.t('Upload')}</span>
  328. {/if}
  329. </button>
  330. </div>
  331. </div>
  332. <div class=" my-1.5 text-sm font-medium">{$i18n.t('Chat')}</div>
  333. <div>
  334. <div class=" py-0.5 flex w-full justify-between">
  335. <div class=" self-center text-xs">{$i18n.t('Title Auto-Generation')}</div>
  336. <button
  337. class="p-1 px-3 text-xs flex rounded transition"
  338. on:click={() => {
  339. toggleTitleAutoGenerate();
  340. }}
  341. type="button"
  342. >
  343. {#if titleAutoGenerate === true}
  344. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  345. {:else}
  346. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  347. {/if}
  348. </button>
  349. </div>
  350. </div>
  351. <div>
  352. <div class=" py-0.5 flex w-full justify-between">
  353. <div class=" self-center text-xs">
  354. {$i18n.t('Response AutoCopy to Clipboard')}
  355. </div>
  356. <button
  357. class="p-1 px-3 text-xs flex rounded transition"
  358. on:click={() => {
  359. toggleResponseAutoCopy();
  360. }}
  361. type="button"
  362. >
  363. {#if responseAutoCopy === true}
  364. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  365. {:else}
  366. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  367. {/if}
  368. </button>
  369. </div>
  370. </div>
  371. <div>
  372. <div class=" py-0.5 flex w-full justify-between">
  373. <div class=" self-center text-xs">{$i18n.t('Allow User Location')}</div>
  374. <button
  375. class="p-1 px-3 text-xs flex rounded transition"
  376. on:click={() => {
  377. toggleUserLocation();
  378. }}
  379. type="button"
  380. >
  381. {#if userLocation === true}
  382. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  383. {:else}
  384. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  385. {/if}
  386. </button>
  387. </div>
  388. </div>
  389. <div>
  390. <div class=" py-0.5 flex w-full justify-between">
  391. <div class=" self-center text-xs">{$i18n.t('Haptic Feedback')}</div>
  392. <button
  393. class="p-1 px-3 text-xs flex rounded transition"
  394. on:click={() => {
  395. toggleHapticFeedback();
  396. }}
  397. type="button"
  398. >
  399. {#if hapticFeedback === true}
  400. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  401. {:else}
  402. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  403. {/if}
  404. </button>
  405. </div>
  406. </div>
  407. <div class=" my-1.5 text-sm font-medium">{$i18n.t('Voice')}</div>
  408. <div>
  409. <div class=" py-0.5 flex w-full justify-between">
  410. <div class=" self-center text-xs">{$i18n.t('Allow Voice Interruption in Call')}</div>
  411. <button
  412. class="p-1 px-3 text-xs flex rounded transition"
  413. on:click={() => {
  414. toggleVoiceInterruption();
  415. }}
  416. type="button"
  417. >
  418. {#if voiceInterruption === true}
  419. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  420. {:else}
  421. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  422. {/if}
  423. </button>
  424. </div>
  425. </div>
  426. <div>
  427. <div class=" py-0.5 flex w-full justify-between">
  428. <div class=" self-center text-xs">{$i18n.t('Display Emoji in Call')}</div>
  429. <button
  430. class="p-1 px-3 text-xs flex rounded transition"
  431. on:click={() => {
  432. toggleEmojiInCall();
  433. }}
  434. type="button"
  435. >
  436. {#if showEmojiInCall === true}
  437. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  438. {:else}
  439. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  440. {/if}
  441. </button>
  442. </div>
  443. </div>
  444. </div>
  445. </div>
  446. <div class="flex justify-end text-sm font-medium">
  447. <button
  448. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  449. type="submit"
  450. >
  451. {$i18n.t('Save')}
  452. </button>
  453. </div>
  454. </form>