Commands.svelte 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 { processWeb, processYoutubeVideo } from '$lib/apis/retrieval';
  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 fileItem = {
  24. type: 'doc',
  25. name: url,
  26. collection_name: '',
  27. status: false,
  28. url: url,
  29. error: ''
  30. };
  31. try {
  32. files = [...files, fileItem];
  33. const res = await processWeb(localStorage.token, '', url);
  34. if (res) {
  35. fileItem.status = 'processed';
  36. fileItem.collection_name = res.collection_name;
  37. fileItem.file = {
  38. content: res.content,
  39. ...fileItem.file
  40. };
  41. files = files;
  42. }
  43. } catch (e) {
  44. // Remove the failed doc from the files array
  45. files = files.filter((f) => f.name !== url);
  46. toast.error(JSON.stringify(e));
  47. }
  48. };
  49. const uploadYoutubeTranscription = async (url) => {
  50. console.log(url);
  51. const fileItem = {
  52. type: 'doc',
  53. name: url,
  54. collection_name: '',
  55. status: false,
  56. url: url,
  57. error: ''
  58. };
  59. try {
  60. files = [...files, fileItem];
  61. const res = await processYoutubeVideo(localStorage.token, url);
  62. if (res) {
  63. fileItem.status = 'processed';
  64. fileItem.collection_name = res.collection_name;
  65. fileItem.file = {
  66. content: res.content,
  67. ...fileItem.file
  68. };
  69. files = files;
  70. }
  71. } catch (e) {
  72. // Remove the failed doc from the files array
  73. files = files.filter((f) => f.name !== url);
  74. toast.error(e);
  75. }
  76. };
  77. </script>
  78. {#if ['/', '#', '@'].includes(command?.charAt(0))}
  79. {#if command?.charAt(0) === '/'}
  80. <Prompts bind:this={commandElement} bind:prompt bind:files {command} />
  81. {:else if command?.charAt(0) === '#'}
  82. <Documents
  83. bind:this={commandElement}
  84. bind:prompt
  85. {command}
  86. on:youtube={(e) => {
  87. console.log(e);
  88. uploadYoutubeTranscription(e.detail);
  89. }}
  90. on:url={(e) => {
  91. console.log(e);
  92. uploadWeb(e.detail);
  93. }}
  94. on:select={(e) => {
  95. console.log(e);
  96. files = [
  97. ...files,
  98. {
  99. type: e?.detail?.type ?? 'file',
  100. ...e.detail,
  101. status: 'processed'
  102. }
  103. ];
  104. dispatch('select');
  105. }}
  106. />
  107. {:else if command?.charAt(0) === '@'}
  108. <Models
  109. bind:this={commandElement}
  110. {command}
  111. on:select={(e) => {
  112. prompt = removeLastWordFromString(prompt, command);
  113. dispatch('select', {
  114. type: 'model',
  115. data: e.detail
  116. });
  117. }}
  118. />
  119. {/if}
  120. {/if}