Commands.svelte 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <script>
  2. import { createEventDispatcher, onMount } from 'svelte';
  3. import { toast } from 'svelte-sonner';
  4. const dispatch = createEventDispatcher();
  5. import { knowledge, prompts } from '$lib/stores';
  6. import { removeLastWordFromString } from '$lib/utils';
  7. import { getPrompts } from '$lib/apis/prompts';
  8. import { getKnowledgeBases } from '$lib/apis/knowledge';
  9. import Prompts from './Commands/Prompts.svelte';
  10. import Knowledge from './Commands/Knowledge.svelte';
  11. import Models from './Commands/Models.svelte';
  12. import Spinner from '$lib/components/common/Spinner.svelte';
  13. export let prompt = '';
  14. export let files = [];
  15. let loading = false;
  16. let commandElement = null;
  17. export const selectUp = () => {
  18. commandElement?.selectUp();
  19. };
  20. export const selectDown = () => {
  21. commandElement?.selectDown();
  22. };
  23. let command = '';
  24. $: command = prompt?.split('\n').pop()?.split(' ')?.pop() ?? '';
  25. let show = false;
  26. $: show = ['/', '#', '@'].includes(command?.charAt(0)) || '\\#' === command.slice(0, 2);
  27. $: if (show) {
  28. init();
  29. }
  30. const init = async () => {
  31. loading = true;
  32. await Promise.all([
  33. (async () => {
  34. prompts.set(await getPrompts(localStorage.token));
  35. })(),
  36. (async () => {
  37. knowledge.set(await getKnowledgeBases(localStorage.token));
  38. })()
  39. ]);
  40. loading = false;
  41. };
  42. </script>
  43. {#if show}
  44. {#if !loading}
  45. {#if command?.charAt(0) === '/'}
  46. <Prompts bind:this={commandElement} bind:prompt bind:files {command} />
  47. {:else if (command?.charAt(0) === '#' && command.startsWith('#') && !command.includes('# ')) || ('\\#' === command.slice(0, 2) && command.startsWith('#') && !command.includes('# '))}
  48. <Knowledge
  49. bind:this={commandElement}
  50. bind:prompt
  51. command={command.includes('\\#') ? command.slice(2) : command}
  52. on:youtube={(e) => {
  53. console.log(e);
  54. dispatch('upload', {
  55. type: 'youtube',
  56. data: e.detail
  57. });
  58. }}
  59. on:url={(e) => {
  60. console.log(e);
  61. dispatch('upload', {
  62. type: 'web',
  63. data: e.detail
  64. });
  65. }}
  66. on:select={(e) => {
  67. console.log(e);
  68. files = [
  69. ...files,
  70. {
  71. ...e.detail,
  72. status: 'processed'
  73. }
  74. ];
  75. dispatch('select');
  76. }}
  77. />
  78. {:else if command?.charAt(0) === '@'}
  79. <Models
  80. bind:this={commandElement}
  81. {command}
  82. on:select={(e) => {
  83. prompt = removeLastWordFromString(prompt, command);
  84. dispatch('select', {
  85. type: 'model',
  86. data: e.detail
  87. });
  88. }}
  89. />
  90. {/if}
  91. {:else}
  92. <div
  93. id="commands-container"
  94. class="px-2 mb-2 text-left w-full absolute bottom-0 left-0 right-0 z-10"
  95. >
  96. <div class="flex w-full rounded-xl border border-gray-50 dark:border-gray-850">
  97. <div
  98. class="max-h-60 flex flex-col w-full rounded-xl bg-white dark:bg-gray-900 dark:text-gray-100"
  99. >
  100. <Spinner />
  101. </div>
  102. </div>
  103. </div>
  104. {/if}
  105. {/if}