Interface.svelte 12 KB

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