InputMenu.svelte 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <script lang="ts">
  2. import { DropdownMenu } from 'bits-ui';
  3. import { flyAndScale } from '$lib/utils/transitions';
  4. import { getContext, onMount } from 'svelte';
  5. import { config, user, tools as _tools } from '$lib/stores';
  6. import Dropdown from '$lib/components/common/Dropdown.svelte';
  7. import Tooltip from '$lib/components/common/Tooltip.svelte';
  8. import DocumentArrowUpSolid from '$lib/components/icons/DocumentArrowUpSolid.svelte';
  9. import Switch from '$lib/components/common/Switch.svelte';
  10. import GlobeAltSolid from '$lib/components/icons/GlobeAltSolid.svelte';
  11. import WrenchSolid from '$lib/components/icons/WrenchSolid.svelte';
  12. import { getTools } from '$lib/apis/tools';
  13. const i18n = getContext('i18n');
  14. export let uploadFilesHandler: Function;
  15. export let availableToolIds: string[] = [];
  16. export let selectedToolIds: string[] = [];
  17. export let webSearchEnabled: boolean;
  18. export let onClose: Function;
  19. let tools = {};
  20. let show = false;
  21. $: if (show) {
  22. init();
  23. }
  24. const init = async () => {
  25. if ($_tools === null) {
  26. await _tools.set(await getTools(localStorage.token));
  27. }
  28. tools = $_tools.reduce((a, tool, i, arr) => {
  29. if (availableToolIds.includes(tool.id) || ($user?.role ?? 'user') === 'admin') {
  30. a[tool.id] = {
  31. name: tool.name,
  32. description: tool.meta.description,
  33. enabled: selectedToolIds.includes(tool.id)
  34. };
  35. }
  36. return a;
  37. }, {});
  38. };
  39. </script>
  40. <Dropdown
  41. bind:show
  42. on:change={(e) => {
  43. if (e.detail === false) {
  44. onClose();
  45. }
  46. }}
  47. >
  48. <Tooltip content={$i18n.t('More')}>
  49. <slot />
  50. </Tooltip>
  51. <div slot="content">
  52. <DropdownMenu.Content
  53. class="w-full max-w-[200px] rounded-xl px-1 py-1 border-gray-300/30 dark:border-gray-700/50 z-50 bg-white dark:bg-gray-850 dark:text-white shadow"
  54. sideOffset={15}
  55. alignOffset={-8}
  56. side="top"
  57. align="start"
  58. transition={flyAndScale}
  59. >
  60. {#if Object.keys(tools).length > 0}
  61. <div class=" max-h-28 overflow-y-auto scrollbar-hidden">
  62. {#each Object.keys(tools) as toolId}
  63. <button
  64. class="flex w-full justify-between gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer rounded-xl"
  65. on:click={() => {
  66. selectedToolIds = tools[toolId].enabled
  67. ? selectedToolIds.filter((id) => id !== toolId)
  68. : [...selectedToolIds, toolId];
  69. }}
  70. >
  71. <div class="flex-1 truncate">
  72. <Tooltip
  73. content={tools[toolId]?.description ?? ''}
  74. placement="top-start"
  75. className="flex flex-1 gap-2 items-center"
  76. >
  77. <WrenchSolid />
  78. <div class=" truncate">{tools[toolId].name}</div>
  79. </Tooltip>
  80. </div>
  81. <div class=" shrink-0 flex-shrink-0">
  82. <Switch state={tools[toolId].enabled} />
  83. </div>
  84. </button>
  85. {/each}
  86. </div>
  87. <hr class="border-gray-100 dark:border-gray-800 my-1" />
  88. {/if}
  89. {#if $config?.features?.enable_web_search}
  90. <button
  91. class="flex w-full justify-between gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer rounded-xl"
  92. on:click={() => {
  93. webSearchEnabled = !webSearchEnabled;
  94. }}
  95. >
  96. <div class="flex-1 flex items-center gap-2">
  97. <GlobeAltSolid />
  98. <div class=" line-clamp-1">{$i18n.t('Web Search')}</div>
  99. </div>
  100. <Switch state={webSearchEnabled} />
  101. </button>
  102. <hr class="border-gray-100 dark:border-gray-800 my-1" />
  103. {/if}
  104. <DropdownMenu.Item
  105. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl"
  106. on:click={() => {
  107. uploadFilesHandler();
  108. }}
  109. >
  110. <DocumentArrowUpSolid />
  111. <div class=" line-clamp-1">{$i18n.t('Upload Files')}</div>
  112. </DropdownMenu.Item>
  113. </DropdownMenu.Content>
  114. </div>
  115. </Dropdown>