InputMenu.svelte 3.8 KB

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