Commands.svelte 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <script>
  2. import { createEventDispatcher } from 'svelte';
  3. import { toast } from 'svelte-sonner';
  4. const dispatch = createEventDispatcher();
  5. import Prompts from './Commands/Prompts.svelte';
  6. import Documents from './Commands/Documents.svelte';
  7. import Models from './Commands/Models.svelte';
  8. import { removeLastWordFromString } from '$lib/utils';
  9. import { uploadWebToVectorDB, uploadYoutubeTranscriptionToVectorDB } from '$lib/apis/rag';
  10. export let prompt = '';
  11. export let files = [];
  12. let commandElement = null;
  13. export const selectUp = () => {
  14. commandElement?.selectUp();
  15. };
  16. export const selectDown = () => {
  17. commandElement?.selectDown();
  18. };
  19. let command = '';
  20. $: command = (prompt?.trim() ?? '').split(' ')?.at(-1) ?? '';
  21. const uploadWeb = async (url) => {
  22. console.log(url);
  23. const doc = {
  24. type: 'doc',
  25. name: url,
  26. collection_name: '',
  27. status: false,
  28. url: url,
  29. error: ''
  30. };
  31. try {
  32. files = [...files, doc];
  33. const res = await uploadWebToVectorDB(localStorage.token, '', url);
  34. if (res) {
  35. doc.status = 'processed';
  36. doc.collection_name = res.collection_name;
  37. files = files;
  38. }
  39. } catch (e) {
  40. // Remove the failed doc from the files array
  41. files = files.filter((f) => f.name !== url);
  42. toast.error(e);
  43. }
  44. };
  45. const uploadYoutubeTranscription = async (url) => {
  46. console.log(url);
  47. const doc = {
  48. type: 'doc',
  49. name: url,
  50. collection_name: '',
  51. status: false,
  52. url: url,
  53. error: ''
  54. };
  55. try {
  56. files = [...files, doc];
  57. const res = await uploadYoutubeTranscriptionToVectorDB(localStorage.token, url);
  58. if (res) {
  59. doc.status = 'processed';
  60. doc.collection_name = res.collection_name;
  61. files = files;
  62. }
  63. } catch (e) {
  64. // Remove the failed doc from the files array
  65. files = files.filter((f) => f.name !== url);
  66. toast.error(e);
  67. }
  68. };
  69. </script>
  70. {#if ['/', '#', '@'].includes(command?.charAt(0))}
  71. {#if command?.charAt(0) === '/'}
  72. <Prompts bind:this={commandElement} bind:prompt bind:files {command} />
  73. {:else if command?.charAt(0) === '#'}
  74. <Documents
  75. bind:this={commandElement}
  76. bind:prompt
  77. {command}
  78. on:youtube={(e) => {
  79. console.log(e);
  80. uploadYoutubeTranscription(e.detail);
  81. }}
  82. on:url={(e) => {
  83. console.log(e);
  84. uploadWeb(e.detail);
  85. }}
  86. on:select={(e) => {
  87. console.log(e);
  88. files = [
  89. ...files,
  90. {
  91. type: e?.detail?.type ?? 'file',
  92. ...e.detail,
  93. status: 'processed'
  94. }
  95. ];
  96. dispatch('select');
  97. }}
  98. />
  99. {:else if command?.charAt(0) === '@'}
  100. <Models
  101. bind:this={commandElement}
  102. {command}
  103. on:select={(e) => {
  104. prompt = removeLastWordFromString(prompt, command);
  105. dispatch('select', {
  106. type: 'model',
  107. data: e.detail
  108. });
  109. }}
  110. />
  111. {/if}
  112. {/if}