Commands.svelte 2.8 KB

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