MessageInput.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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">
  70. <div class=" pt-5">
  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 class="bg-gradient-to-t from-white dark:from-gray-800 from-40% pb-2">
  100. <input
  101. bind:this={filesInputElement}
  102. bind:files={inputFiles}
  103. type="file"
  104. hidden
  105. on:change={() => {
  106. let reader = new FileReader();
  107. reader.onload = (event) => {
  108. files = [
  109. ...files,
  110. {
  111. type: 'image',
  112. url: `${event.target.result}`
  113. }
  114. ];
  115. inputFiles = null;
  116. };
  117. if (
  118. inputFiles &&
  119. inputFiles.length > 0 &&
  120. ['image/gif', 'image/jpeg', 'image/png'].includes(inputFiles[0]['type'])
  121. ) {
  122. reader.readAsDataURL(inputFiles[0]);
  123. } else {
  124. toast.error(`Unsupported File Type '${inputFiles[0]['type']}'.`);
  125. inputFiles = null;
  126. }
  127. }}
  128. />
  129. <form
  130. class=" flex flex-col relative w-full rounded-xl border dark:border-gray-600 bg-white dark:bg-gray-800 dark:text-gray-100"
  131. on:submit|preventDefault={() => {
  132. submitPrompt(prompt);
  133. }}
  134. >
  135. {#if files.length > 0}
  136. <div class="ml-2 mt-2 mb-1 flex space-x-2">
  137. {#each files as file, fileIdx}
  138. <div class=" relative group">
  139. <img src={file.url} alt="input" class=" h-16 w-16 rounded-xl bg-cover" />
  140. <div class=" absolute -top-1 -right-1">
  141. <button
  142. class=" bg-gray-400 text-white border border-white rounded-full group-hover:visible invisible transition"
  143. type="button"
  144. on:click={() => {
  145. files.splice(fileIdx, 1);
  146. files = files;
  147. }}
  148. >
  149. <svg
  150. xmlns="http://www.w3.org/2000/svg"
  151. viewBox="0 0 20 20"
  152. fill="currentColor"
  153. class="w-4 h-4"
  154. >
  155. <path
  156. 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"
  157. />
  158. </svg>
  159. </button>
  160. </div>
  161. </div>
  162. {/each}
  163. </div>
  164. {/if}
  165. <div class=" flex">
  166. {#if fileUploadEnabled}
  167. <div class=" self-end mb-2 ml-1.5">
  168. <button
  169. class=" text-gray-600 dark:text-gray-200 transition rounded-lg p-1 ml-1"
  170. type="button"
  171. on:click={() => {
  172. filesInputElement.click();
  173. }}
  174. >
  175. <svg
  176. xmlns="http://www.w3.org/2000/svg"
  177. viewBox="0 0 20 20"
  178. fill="currentColor"
  179. class="w-5 h-5"
  180. >
  181. <path
  182. fill-rule="evenodd"
  183. 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"
  184. clip-rule="evenodd"
  185. />
  186. </svg>
  187. </button>
  188. </div>
  189. {/if}
  190. <textarea
  191. id="chat-textarea"
  192. class=" dark:bg-gray-800 dark:text-gray-100 outline-none w-full py-3 px-2 {fileUploadEnabled
  193. ? ''
  194. : ' pl-4'} rounded-xl resize-none"
  195. placeholder={speechRecognitionListening ? 'Listening...' : 'Send a message'}
  196. bind:value={prompt}
  197. on:keypress={(e) => {
  198. if (e.keyCode == 13 && !e.shiftKey) {
  199. e.preventDefault();
  200. }
  201. if (prompt !== '' && e.keyCode == 13 && !e.shiftKey) {
  202. submitPrompt(prompt);
  203. }
  204. }}
  205. rows="1"
  206. on:input={(e) => {
  207. e.target.style.height = '';
  208. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
  209. }}
  210. />
  211. <div class="self-end mb-2 flex space-x-0.5 mr-2">
  212. {#if messages.length == 0 || messages.at(-1).done == true}
  213. {#if speechRecognitionEnabled}
  214. <button
  215. class=" text-gray-600 dark:text-gray-300 transition rounded-lg p-1.5 mr-0.5 self-center"
  216. type="button"
  217. on:click={() => {
  218. speechRecognitionHandler();
  219. }}
  220. >
  221. {#if speechRecognitionListening}
  222. <svg
  223. class=" w-5 h-5 translate-y-[0.5px]"
  224. fill="currentColor"
  225. viewBox="0 0 24 24"
  226. xmlns="http://www.w3.org/2000/svg"
  227. ><style>
  228. .spinner_qM83 {
  229. animation: spinner_8HQG 1.05s infinite;
  230. }
  231. .spinner_oXPr {
  232. animation-delay: 0.1s;
  233. }
  234. .spinner_ZTLf {
  235. animation-delay: 0.2s;
  236. }
  237. @keyframes spinner_8HQG {
  238. 0%,
  239. 57.14% {
  240. animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
  241. transform: translate(0);
  242. }
  243. 28.57% {
  244. animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
  245. transform: translateY(-6px);
  246. }
  247. 100% {
  248. transform: translate(0);
  249. }
  250. }
  251. </style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
  252. class="spinner_qM83 spinner_oXPr"
  253. cx="12"
  254. cy="12"
  255. r="2.5"
  256. /><circle class="spinner_qM83 spinner_ZTLf" cx="20" cy="12" r="2.5" /></svg
  257. >
  258. {:else}
  259. <svg
  260. xmlns="http://www.w3.org/2000/svg"
  261. viewBox="0 0 20 20"
  262. fill="currentColor"
  263. class="w-5 h-5 translate-y-[0.5px]"
  264. >
  265. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  266. <path
  267. 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"
  268. />
  269. </svg>
  270. {/if}
  271. </button>
  272. {/if}
  273. <button
  274. class="{prompt !== ''
  275. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  276. : '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"
  277. type="submit"
  278. disabled={prompt === ''}
  279. >
  280. <svg
  281. xmlns="http://www.w3.org/2000/svg"
  282. viewBox="0 0 20 20"
  283. fill="currentColor"
  284. class="w-5 h-5"
  285. >
  286. <path
  287. fill-rule="evenodd"
  288. 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"
  289. clip-rule="evenodd"
  290. />
  291. </svg>
  292. </button>
  293. {:else}
  294. <button
  295. 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"
  296. on:click={stopResponse}
  297. >
  298. <svg
  299. xmlns="http://www.w3.org/2000/svg"
  300. viewBox="0 0 24 24"
  301. fill="currentColor"
  302. class="w-5 h-5"
  303. >
  304. <path
  305. fill-rule="evenodd"
  306. 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"
  307. clip-rule="evenodd"
  308. />
  309. </svg>
  310. </button>
  311. {/if}
  312. </div>
  313. </div>
  314. </form>
  315. <div class="mt-1.5 text-xs text-gray-500 text-center">
  316. LLMs can make mistakes. Verify important information.
  317. </div>
  318. </div>
  319. </div>
  320. </div>
  321. </div>