InputMenu.svelte 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, mobile } from '$lib/stores';
  6. import { createPicker } from '$lib/utils/google-drive-picker';
  7. import { getTools } from '$lib/apis/tools';
  8. import Dropdown from '$lib/components/common/Dropdown.svelte';
  9. import Tooltip from '$lib/components/common/Tooltip.svelte';
  10. import DocumentArrowUpSolid from '$lib/components/icons/DocumentArrowUpSolid.svelte';
  11. import Switch from '$lib/components/common/Switch.svelte';
  12. import GlobeAltSolid from '$lib/components/icons/GlobeAltSolid.svelte';
  13. import WrenchSolid from '$lib/components/icons/WrenchSolid.svelte';
  14. import CameraSolid from '$lib/components/icons/CameraSolid.svelte';
  15. import PhotoSolid from '$lib/components/icons/PhotoSolid.svelte';
  16. const i18n = getContext('i18n');
  17. export let screenCaptureHandler: Function;
  18. export let uploadFilesHandler: Function;
  19. export let uploadGoogleDriveHandler: Function;
  20. export let selectedToolIds: string[] = [];
  21. export let webSearchEnabled: boolean;
  22. export let imageGenerationEnabled: boolean;
  23. export let onClose: Function;
  24. let tools = {};
  25. let show = false;
  26. let showImageGeneration = false;
  27. $: showImageGeneration =
  28. $config?.features?.enable_image_generation &&
  29. ($user.role === 'admin' || $user?.permissions?.features?.image_generation);
  30. let showWebSearch = false;
  31. $: showWebSearch =
  32. $config?.features?.enable_web_search &&
  33. ($user.role === 'admin' || $user?.permissions?.features?.web_search);
  34. $: if (show) {
  35. init();
  36. }
  37. const init = async () => {
  38. if ($_tools === null) {
  39. await _tools.set(await getTools(localStorage.token));
  40. }
  41. tools = $_tools.reduce((a, tool, i, arr) => {
  42. a[tool.id] = {
  43. name: tool.name,
  44. description: tool.meta.description,
  45. enabled: selectedToolIds.includes(tool.id)
  46. };
  47. return a;
  48. }, {});
  49. };
  50. </script>
  51. <Dropdown
  52. bind:show
  53. on:change={(e) => {
  54. if (e.detail === false) {
  55. onClose();
  56. }
  57. }}
  58. >
  59. <Tooltip content={$i18n.t('More')}>
  60. <slot />
  61. </Tooltip>
  62. <div slot="content">
  63. <DropdownMenu.Content
  64. class="w-full max-w-[220px] 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"
  65. sideOffset={15}
  66. alignOffset={-8}
  67. side="top"
  68. align="start"
  69. transition={flyAndScale}
  70. >
  71. {#if Object.keys(tools).length > 0}
  72. <div class=" max-h-28 overflow-y-auto scrollbar-hidden">
  73. {#each Object.keys(tools) as toolId}
  74. <button
  75. class="flex w-full justify-between gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer rounded-xl"
  76. on:click={() => {
  77. tools[toolId].enabled = !tools[toolId].enabled;
  78. }}
  79. >
  80. <div class="flex-1 truncate">
  81. <Tooltip
  82. content={tools[toolId]?.description ?? ''}
  83. placement="top-start"
  84. className="flex flex-1 gap-2 items-center"
  85. >
  86. <div class="flex-shrink-0">
  87. <WrenchSolid />
  88. </div>
  89. <div class=" truncate">{tools[toolId].name}</div>
  90. </Tooltip>
  91. </div>
  92. <div class=" flex-shrink-0">
  93. <Switch
  94. state={tools[toolId].enabled}
  95. on:change={async (e) => {
  96. const state = e.detail;
  97. await tick();
  98. if (state) {
  99. selectedToolIds = [...selectedToolIds, toolId];
  100. } else {
  101. selectedToolIds = selectedToolIds.filter((id) => id !== toolId);
  102. }
  103. }}
  104. />
  105. </div>
  106. </button>
  107. {/each}
  108. </div>
  109. <hr class="border-black/5 dark:border-white/5 my-1" />
  110. {/if}
  111. {#if showImageGeneration}
  112. <button
  113. class="flex w-full justify-between gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer rounded-xl"
  114. on:click={() => {
  115. imageGenerationEnabled = !imageGenerationEnabled;
  116. }}
  117. >
  118. <div class="flex-1 flex items-center gap-2">
  119. <PhotoSolid />
  120. <div class=" line-clamp-1">{$i18n.t('Image')}</div>
  121. </div>
  122. <Switch state={imageGenerationEnabled} />
  123. </button>
  124. {/if}
  125. {#if showWebSearch}
  126. <button
  127. class="flex w-full justify-between gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer rounded-xl"
  128. on:click={() => {
  129. webSearchEnabled = !webSearchEnabled;
  130. }}
  131. >
  132. <div class="flex-1 flex items-center gap-2">
  133. <GlobeAltSolid />
  134. <div class=" line-clamp-1">{$i18n.t('Web Search')}</div>
  135. </div>
  136. <Switch state={webSearchEnabled} />
  137. </button>
  138. {/if}
  139. {#if showImageGeneration || showWebSearch}
  140. <hr class="border-black/5 dark:border-white/5 my-1" />
  141. {/if}
  142. {#if !$mobile}
  143. <DropdownMenu.Item
  144. 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"
  145. on:click={() => {
  146. screenCaptureHandler();
  147. }}
  148. >
  149. <CameraSolid />
  150. <div class=" line-clamp-1">{$i18n.t('Capture')}</div>
  151. </DropdownMenu.Item>
  152. {/if}
  153. <DropdownMenu.Item
  154. 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"
  155. on:click={() => {
  156. uploadFilesHandler();
  157. }}
  158. >
  159. <DocumentArrowUpSolid />
  160. <div class="line-clamp-1">{$i18n.t('Upload Files')}</div>
  161. </DropdownMenu.Item>
  162. {#if $config?.features?.enable_google_drive_integration}
  163. <DropdownMenu.Item
  164. 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"
  165. on:click={() => {
  166. uploadGoogleDriveHandler();
  167. }}
  168. >
  169. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 87.3 78" class="w-5 h-5">
  170. <path
  171. d="m6.6 66.85 3.85 6.65c.8 1.4 1.95 2.5 3.3 3.3l13.75-23.8h-27.5c0 1.55.4 3.1 1.2 4.5z"
  172. fill="#0066da"
  173. />
  174. <path
  175. d="m43.65 25-13.75-23.8c-1.35.8-2.5 1.9-3.3 3.3l-25.4 44a9.06 9.06 0 0 0 -1.2 4.5h27.5z"
  176. fill="#00ac47"
  177. />
  178. <path
  179. d="m73.55 76.8c1.35-.8 2.5-1.9 3.3-3.3l1.6-2.75 7.65-13.25c.8-1.4 1.2-2.95 1.2-4.5h-27.502l5.852 11.5z"
  180. fill="#ea4335"
  181. />
  182. <path
  183. d="m43.65 25 13.75-23.8c-1.35-.8-2.9-1.2-4.5-1.2h-18.5c-1.6 0-3.15.45-4.5 1.2z"
  184. fill="#00832d"
  185. />
  186. <path
  187. d="m59.8 53h-32.3l-13.75 23.8c1.35.8 2.9 1.2 4.5 1.2h50.8c1.6 0 3.15-.45 4.5-1.2z"
  188. fill="#2684fc"
  189. />
  190. <path
  191. d="m73.4 26.5-12.7-22c-.8-1.4-1.95-2.5-3.3-3.3l-13.75 23.8 16.15 28h27.45c0-1.55-.4-3.1-1.2-4.5z"
  192. fill="#ffba00"
  193. />
  194. </svg>
  195. <div class="line-clamp-1">{$i18n.t('Google Drive')}</div>
  196. </DropdownMenu.Item>
  197. {/if}
  198. </DropdownMenu.Content>
  199. </div>
  200. </Dropdown>