Commands.svelte 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if (files.find((f) => f.id === e.detail.id)) {
  69. return;
  70. }
  71. files = [
  72. ...files,
  73. {
  74. ...e.detail,
  75. status: 'processed'
  76. }
  77. ];
  78. dispatch('select');
  79. }}
  80. />
  81. {:else if command?.charAt(0) === '@'}
  82. <Models
  83. bind:this={commandElement}
  84. {command}
  85. on:select={(e) => {
  86. prompt = removeLastWordFromString(prompt, command);
  87. dispatch('select', {
  88. type: 'model',
  89. data: e.detail
  90. });
  91. }}
  92. />
  93. {/if}
  94. {:else}
  95. <div
  96. id="commands-container"
  97. class="px-2 mb-2 text-left w-full absolute bottom-0 left-0 right-0 z-10"
  98. >
  99. <div class="flex w-full rounded-xl border border-gray-100 dark:border-gray-850">
  100. <div
  101. class="max-h-60 flex flex-col w-full rounded-xl bg-white dark:bg-gray-900 dark:text-gray-100"
  102. >
  103. <Spinner />
  104. </div>
  105. </div>
  106. </div>
  107. {/if}
  108. {/if}