MessageInput.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <script lang="ts">
  2. import { settings } from '$lib/stores';
  3. import toast from 'svelte-french-toast';
  4. import Suggestions from './MessageInput/Suggestions.svelte';
  5. export let submitPrompt: Function;
  6. export let stopResponse: Function;
  7. export let suggestions = 'true';
  8. export let autoScroll = true;
  9. let filesInputElement;
  10. let inputFiles;
  11. export let files = [];
  12. export let fileUploadEnabled = false;
  13. export let speechRecognitionEnabled = true;
  14. export let speechRecognitionListening = false;
  15. export let prompt = '';
  16. export let messages = [];
  17. let speechRecognition;
  18. const speechRecognitionHandler = () => {
  19. // Check if SpeechRecognition is supported
  20. if (speechRecognitionListening) {
  21. speechRecognition.stop();
  22. } else {
  23. if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) {
  24. // Create a SpeechRecognition object
  25. speechRecognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
  26. // Set continuous to true for continuous recognition
  27. speechRecognition.continuous = true;
  28. // Set the timeout for turning off the recognition after inactivity (in milliseconds)
  29. const inactivityTimeout = 3000; // 3 seconds
  30. let timeoutId;
  31. // Start recognition
  32. speechRecognition.start();
  33. speechRecognitionListening = true;
  34. // Event triggered when speech is recognized
  35. speechRecognition.onresult = function (event) {
  36. // Clear the inactivity timeout
  37. clearTimeout(timeoutId);
  38. // Handle recognized speech
  39. console.log(event);
  40. const transcript = event.results[Object.keys(event.results).length - 1][0].transcript;
  41. prompt = `${prompt}${transcript}`;
  42. // Restart the inactivity timeout
  43. timeoutId = setTimeout(() => {
  44. console.log('Speech recognition turned off due to inactivity.');
  45. speechRecognition.stop();
  46. }, inactivityTimeout);
  47. };
  48. // Event triggered when recognition is ended
  49. speechRecognition.onend = function () {
  50. // Restart recognition after it ends
  51. console.log('recognition ended');
  52. speechRecognitionListening = false;
  53. if (prompt !== '' && $settings?.speechAutoSend === true) {
  54. submitPrompt(prompt);
  55. }
  56. };
  57. // Event triggered when an error occurs
  58. speechRecognition.onerror = function (event) {
  59. console.log(event);
  60. toast.error(`Speech recognition error: ${event.error}`);
  61. speechRecognitionListening = false;
  62. };
  63. } else {
  64. toast.error('SpeechRecognition API is not supported in this browser.');
  65. }
  66. }
  67. };
  68. </script>
  69. <div class="fixed bottom-0 w-full bg-white dark:bg-gray-800">
  70. <div class=" absolute right-0 left-0 bottom-0 mb-20">
  71. <div class="max-w-3xl px-2.5 pt-2.5 -mb-0.5 mx-auto inset-x-0">
  72. {#if messages.length == 0 && suggestions !== 'false'}
  73. <Suggestions {submitPrompt} />
  74. {/if}
  75. {#if autoScroll === false && messages.length > 0}
  76. <div class=" flex justify-center mb-4">
  77. <button
  78. class=" bg-white border border-gray-100 dark:border-none dark:bg-white/20 p-1.5 rounded-full"
  79. on:click={() => {
  80. autoScroll = true;
  81. window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
  82. }}
  83. >
  84. <svg
  85. xmlns="http://www.w3.org/2000/svg"
  86. viewBox="0 0 20 20"
  87. fill="currentColor"
  88. class="w-5 h-5"
  89. >
  90. <path
  91. fill-rule="evenodd"
  92. d="M10 3a.75.75 0 01.75.75v10.638l3.96-4.158a.75.75 0 111.08 1.04l-5.25 5.5a.75.75 0 01-1.08 0l-5.25-5.5a.75.75 0 111.08-1.04l3.96 4.158V3.75A.75.75 0 0110 3z"
  93. clip-rule="evenodd"
  94. />
  95. </svg>
  96. </button>
  97. </div>
  98. {/if}
  99. </div>
  100. </div>
  101. <div>
  102. <div class="max-w-3xl px-2.5 -mb-0.5 mx-auto inset-x-0">
  103. <div class="bg-gradient-to-t from-white dark:from-gray-800 from-40% pb-2">
  104. <input
  105. bind:this={filesInputElement}
  106. bind:files={inputFiles}
  107. type="file"
  108. hidden
  109. on:change={() => {
  110. let reader = new FileReader();
  111. reader.onload = (event) => {
  112. files = [
  113. ...files,
  114. {
  115. type: 'image',
  116. url: `${event.target.result}`
  117. }
  118. ];
  119. inputFiles = null;
  120. };
  121. if (
  122. inputFiles &&
  123. inputFiles.length > 0 &&
  124. ['image/gif', 'image/jpeg', 'image/png'].includes(inputFiles[0]['type'])
  125. ) {
  126. reader.readAsDataURL(inputFiles[0]);
  127. } else {
  128. toast.error(`Unsupported File Type '${inputFiles[0]['type']}'.`);
  129. inputFiles = null;
  130. }
  131. }}
  132. />
  133. <form
  134. class=" flex flex-col relative w-full rounded-xl border dark:border-gray-600 bg-white dark:bg-gray-800 dark:text-gray-100"
  135. on:submit|preventDefault={() => {
  136. submitPrompt(prompt);
  137. }}
  138. >
  139. {#if files.length > 0}
  140. <div class="ml-2 mt-2 mb-1 flex space-x-2">
  141. {#each files as file, fileIdx}
  142. <div class=" relative group">
  143. <img src={file.url} alt="input" class=" h-16 w-16 rounded-xl bg-cover" />
  144. <div class=" absolute -top-1 -right-1">
  145. <button
  146. class=" bg-gray-400 text-white border border-white rounded-full group-hover:visible invisible transition"
  147. type="button"
  148. on:click={() => {
  149. files.splice(fileIdx, 1);
  150. files = files;
  151. }}
  152. >
  153. <svg
  154. xmlns="http://www.w3.org/2000/svg"
  155. viewBox="0 0 20 20"
  156. fill="currentColor"
  157. class="w-4 h-4"
  158. >
  159. <path
  160. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  161. />
  162. </svg>
  163. </button>
  164. </div>
  165. </div>
  166. {/each}
  167. </div>
  168. {/if}
  169. <div class=" flex">
  170. {#if fileUploadEnabled}
  171. <div class=" self-end mb-2 ml-1.5">
  172. <button
  173. class=" text-gray-600 dark:text-gray-200 transition rounded-lg p-1 ml-1"
  174. type="button"
  175. on:click={() => {
  176. filesInputElement.click();
  177. }}
  178. >
  179. <svg
  180. xmlns="http://www.w3.org/2000/svg"
  181. viewBox="0 0 20 20"
  182. fill="currentColor"
  183. class="w-5 h-5"
  184. >
  185. <path
  186. fill-rule="evenodd"
  187. d="M15.621 4.379a3 3 0 00-4.242 0l-7 7a3 3 0 004.241 4.243h.001l.497-.5a.75.75 0 011.064 1.057l-.498.501-.002.002a4.5 4.5 0 01-6.364-6.364l7-7a4.5 4.5 0 016.368 6.36l-3.455 3.553A2.625 2.625 0 119.52 9.52l3.45-3.451a.75.75 0 111.061 1.06l-3.45 3.451a1.125 1.125 0 001.587 1.595l3.454-3.553a3 3 0 000-4.242z"
  188. clip-rule="evenodd"
  189. />
  190. </svg>
  191. </button>
  192. </div>
  193. {/if}
  194. <textarea
  195. id="chat-textarea"
  196. class=" dark:bg-gray-800 dark:text-gray-100 outline-none w-full py-3 px-2 {fileUploadEnabled
  197. ? ''
  198. : ' pl-4'} rounded-xl resize-none"
  199. placeholder={speechRecognitionListening ? 'Listening...' : 'Send a message'}
  200. bind:value={prompt}
  201. on:keypress={(e) => {
  202. if (e.keyCode == 13 && !e.shiftKey) {
  203. e.preventDefault();
  204. }
  205. if (prompt !== '' && e.keyCode == 13 && !e.shiftKey) {
  206. submitPrompt(prompt);
  207. }
  208. }}
  209. rows="1"
  210. on:input={(e) => {
  211. e.target.style.height = '';
  212. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  213. }}
  214. />
  215. <div class="self-end mb-2 flex space-x-0.5 mr-2">
  216. {#if messages.length == 0 || messages.at(-1).done == true}
  217. {#if speechRecognitionEnabled}
  218. <button
  219. class=" text-gray-600 dark:text-gray-300 transition rounded-lg p-1.5 mr-0.5 self-center"
  220. type="button"
  221. on:click={() => {
  222. speechRecognitionHandler();
  223. }}
  224. >
  225. {#if speechRecognitionListening}
  226. <svg
  227. class=" w-5 h-5 translate-y-[0.5px]"
  228. fill="currentColor"
  229. viewBox="0 0 24 24"
  230. xmlns="http://www.w3.org/2000/svg"
  231. ><style>
  232. .spinner_qM83 {
  233. animation: spinner_8HQG 1.05s infinite;
  234. }
  235. .spinner_oXPr {
  236. animation-delay: 0.1s;
  237. }
  238. .spinner_ZTLf {
  239. animation-delay: 0.2s;
  240. }
  241. @keyframes spinner_8HQG {
  242. 0%,
  243. 57.14% {
  244. animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
  245. transform: translate(0);
  246. }
  247. 28.57% {
  248. animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
  249. transform: translateY(-6px);
  250. }
  251. 100% {
  252. transform: translate(0);
  253. }
  254. }
  255. </style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
  256. class="spinner_qM83 spinner_oXPr"
  257. cx="12"
  258. cy="12"
  259. r="2.5"
  260. /><circle class="spinner_qM83 spinner_ZTLf" cx="20" cy="12" r="2.5" /></svg
  261. >
  262. {:else}
  263. <svg
  264. xmlns="http://www.w3.org/2000/svg"
  265. viewBox="0 0 20 20"
  266. fill="currentColor"
  267. class="w-5 h-5 translate-y-[0.5px]"
  268. >
  269. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  270. <path
  271. d="M5.5 9.643a.75.75 0 00-1.5 0V10c0 3.06 2.29 5.585 5.25 5.954V17.5h-1.5a.75.75 0 000 1.5h4.5a.75.75 0 000-1.5h-1.5v-1.546A6.001 6.001 0 0016 10v-.357a.75.75 0 00-1.5 0V10a4.5 4.5 0 01-9 0v-.357z"
  272. />
  273. </svg>
  274. {/if}
  275. </button>
  276. {/if}
  277. <button
  278. class="{prompt !== ''
  279. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  280. : 'text-white bg-gray-100 dark:text-gray-800 dark:bg-gray-600 disabled'} transition rounded-lg p-1 mr-0.5 w-7 h-7 self-center"
  281. type="submit"
  282. disabled={prompt === ''}
  283. >
  284. <svg
  285. xmlns="http://www.w3.org/2000/svg"
  286. viewBox="0 0 20 20"
  287. fill="currentColor"
  288. class="w-5 h-5"
  289. >
  290. <path
  291. fill-rule="evenodd"
  292. d="M10 17a.75.75 0 01-.75-.75V5.612L5.29 9.77a.75.75 0 01-1.08-1.04l5.25-5.5a.75.75 0 011.08 0l5.25 5.5a.75.75 0 11-1.08 1.04l-3.96-4.158V16.25A.75.75 0 0110 17z"
  293. clip-rule="evenodd"
  294. />
  295. </svg>
  296. </button>
  297. {:else}
  298. <button
  299. class="bg-white hover:bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-white dark:hover:bg-gray-800 transition rounded-lg p-1.5"
  300. on:click={stopResponse}
  301. >
  302. <svg
  303. xmlns="http://www.w3.org/2000/svg"
  304. viewBox="0 0 24 24"
  305. fill="currentColor"
  306. class="w-5 h-5"
  307. >
  308. <path
  309. fill-rule="evenodd"
  310. d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12zm6-2.438c0-.724.588-1.312 1.313-1.312h4.874c.725 0 1.313.588 1.313 1.313v4.874c0 .725-.588 1.313-1.313 1.313H9.564a1.312 1.312 0 01-1.313-1.313V9.564z"
  311. clip-rule="evenodd"
  312. />
  313. </svg>
  314. </button>
  315. {/if}
  316. </div>
  317. </div>
  318. </form>
  319. <div class="mt-1.5 text-xs text-gray-500 text-center">
  320. LLMs can make mistakes. Verify important information.
  321. </div>
  322. </div>
  323. </div>
  324. </div>
  325. </div>