InputMenu.svelte 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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, knowledge, chats } 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 DocumentArrowUp from '$lib/components/icons/DocumentArrowUp.svelte';
  11. import Camera from '$lib/components/icons/Camera.svelte';
  12. import Note from '$lib/components/icons/Note.svelte';
  13. import Clip from '$lib/components/icons/Clip.svelte';
  14. import ChatBubbleOval from '$lib/components/icons/ChatBubbleOval.svelte';
  15. import Refresh from '$lib/components/icons/Refresh.svelte';
  16. import Agile from '$lib/components/icons/Agile.svelte';
  17. import ClockRotateRight from '$lib/components/icons/ClockRotateRight.svelte';
  18. import Database from '$lib/components/icons/Database.svelte';
  19. const i18n = getContext('i18n');
  20. export let selectedToolIds: string[] = [];
  21. export let selectedModels: string[] = [];
  22. export let fileUploadCapableModels: string[] = [];
  23. export let screenCaptureHandler: Function;
  24. export let uploadFilesHandler: Function;
  25. export let inputFilesHandler: Function;
  26. export let uploadGoogleDriveHandler: Function;
  27. export let uploadOneDriveHandler: Function;
  28. export let onClose: Function;
  29. let show = false;
  30. let fileUploadEnabled = true;
  31. $: fileUploadEnabled =
  32. fileUploadCapableModels.length === selectedModels.length &&
  33. ($user?.role === 'admin' || $user?.permissions?.chat?.file_upload);
  34. const detectMobile = () => {
  35. const userAgent = navigator.userAgent || navigator.vendor || window.opera;
  36. return /android|iphone|ipad|ipod|windows phone/i.test(userAgent);
  37. };
  38. function handleFileChange(event) {
  39. const inputFiles = Array.from(event.target?.files);
  40. if (inputFiles && inputFiles.length > 0) {
  41. console.log(inputFiles);
  42. inputFilesHandler(inputFiles);
  43. }
  44. }
  45. </script>
  46. <!-- Hidden file input used to open the camera on mobile -->
  47. <input
  48. id="camera-input"
  49. type="file"
  50. accept="image/*"
  51. capture="environment"
  52. on:change={handleFileChange}
  53. style="display: none;"
  54. />
  55. <Dropdown
  56. bind:show
  57. on:change={(e) => {
  58. if (e.detail === false) {
  59. onClose();
  60. }
  61. }}
  62. >
  63. <Tooltip content={$i18n.t('More')}>
  64. <slot />
  65. </Tooltip>
  66. <div slot="content">
  67. <DropdownMenu.Content
  68. class="w-full max-w-[240px] rounded-2xl px-1 py-1 border border-gray-100 dark:border-gray-800 z-50 bg-white dark:bg-gray-850 dark:text-white shadow-lg transition"
  69. sideOffset={4}
  70. alignOffset={-6}
  71. side="bottom"
  72. align="start"
  73. transition={flyAndScale}
  74. >
  75. <Tooltip
  76. content={fileUploadCapableModels.length !== selectedModels.length
  77. ? $i18n.t('Model(s) do not support file upload')
  78. : !fileUploadEnabled
  79. ? $i18n.t('You do not have permission to upload files.')
  80. : ''}
  81. className="w-full"
  82. >
  83. <DropdownMenu.Item
  84. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl {!fileUploadEnabled
  85. ? 'opacity-50'
  86. : ''}"
  87. on:click={() => {
  88. if (fileUploadEnabled) {
  89. uploadFilesHandler();
  90. }
  91. }}
  92. >
  93. <Clip />
  94. <div class="line-clamp-1">{$i18n.t('Upload Files')}</div>
  95. </DropdownMenu.Item>
  96. </Tooltip>
  97. <Tooltip
  98. content={fileUploadCapableModels.length !== selectedModels.length
  99. ? $i18n.t('Model(s) do not support file upload')
  100. : !fileUploadEnabled
  101. ? $i18n.t('You do not have permission to upload files.')
  102. : ''}
  103. className="w-full"
  104. >
  105. <DropdownMenu.Item
  106. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl {!fileUploadEnabled
  107. ? 'opacity-50'
  108. : ''}"
  109. on:click={() => {
  110. if (fileUploadEnabled) {
  111. if (!detectMobile()) {
  112. screenCaptureHandler();
  113. } else {
  114. const cameraInputElement = document.getElementById('camera-input');
  115. if (cameraInputElement) {
  116. cameraInputElement.click();
  117. }
  118. }
  119. }
  120. }}
  121. >
  122. <Camera />
  123. <div class=" line-clamp-1">{$i18n.t('Capture')}</div>
  124. </DropdownMenu.Item>
  125. </Tooltip>
  126. {#if $config?.features?.enable_notes ?? false}
  127. <Tooltip
  128. content={fileUploadCapableModels.length !== selectedModels.length
  129. ? $i18n.t('Model(s) do not support file upload')
  130. : !fileUploadEnabled
  131. ? $i18n.t('You do not have permission to upload files.')
  132. : ''}
  133. className="w-full"
  134. >
  135. <DropdownMenu.Item
  136. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl {!fileUploadEnabled
  137. ? 'opacity-50'
  138. : ''}"
  139. on:click={() => {
  140. if (fileUploadEnabled) {
  141. if (!detectMobile()) {
  142. screenCaptureHandler();
  143. } else {
  144. const cameraInputElement = document.getElementById('camera-input');
  145. if (cameraInputElement) {
  146. cameraInputElement.click();
  147. }
  148. }
  149. }
  150. }}
  151. >
  152. <Note />
  153. <div class=" line-clamp-1">{$i18n.t('Attach Notes')}</div>
  154. </DropdownMenu.Item>
  155. </Tooltip>
  156. {/if}
  157. <Tooltip
  158. content={fileUploadCapableModels.length !== selectedModels.length
  159. ? $i18n.t('Model(s) do not support file upload')
  160. : !fileUploadEnabled
  161. ? $i18n.t('You do not have permission to upload files.')
  162. : ''}
  163. className="w-full"
  164. >
  165. <DropdownMenu.Item
  166. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl {!fileUploadEnabled
  167. ? 'opacity-50'
  168. : ''}"
  169. on:click={() => {
  170. if (fileUploadEnabled) {
  171. if (!detectMobile()) {
  172. screenCaptureHandler();
  173. } else {
  174. const cameraInputElement = document.getElementById('camera-input');
  175. if (cameraInputElement) {
  176. cameraInputElement.click();
  177. }
  178. }
  179. }
  180. }}
  181. >
  182. <Database />
  183. <div class=" line-clamp-1">{$i18n.t('Attach Knowledge')}</div>
  184. </DropdownMenu.Item>
  185. </Tooltip>
  186. {#if ($chats ?? []).length > 0}
  187. <Tooltip
  188. content={fileUploadCapableModels.length !== selectedModels.length
  189. ? $i18n.t('Model(s) do not support file upload')
  190. : !fileUploadEnabled
  191. ? $i18n.t('You do not have permission to upload files.')
  192. : ''}
  193. className="w-full"
  194. >
  195. <DropdownMenu.Item
  196. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl {!fileUploadEnabled
  197. ? 'opacity-50'
  198. : ''}"
  199. on:click={() => {
  200. if (fileUploadEnabled) {
  201. if (!detectMobile()) {
  202. screenCaptureHandler();
  203. } else {
  204. const cameraInputElement = document.getElementById('camera-input');
  205. if (cameraInputElement) {
  206. cameraInputElement.click();
  207. }
  208. }
  209. }
  210. }}
  211. >
  212. <ClockRotateRight />
  213. <div class=" line-clamp-1">{$i18n.t('Reference Chats')}</div>
  214. </DropdownMenu.Item>
  215. </Tooltip>
  216. {/if}
  217. {#if fileUploadEnabled}
  218. {#if $config?.features?.enable_google_drive_integration}
  219. <DropdownMenu.Item
  220. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl"
  221. on:click={() => {
  222. uploadGoogleDriveHandler();
  223. }}
  224. >
  225. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 87.3 78" class="w-5 h-5">
  226. <path
  227. 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"
  228. fill="#0066da"
  229. />
  230. <path
  231. 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"
  232. fill="#00ac47"
  233. />
  234. <path
  235. 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"
  236. fill="#ea4335"
  237. />
  238. <path
  239. 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"
  240. fill="#00832d"
  241. />
  242. <path
  243. 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"
  244. fill="#2684fc"
  245. />
  246. <path
  247. 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"
  248. fill="#ffba00"
  249. />
  250. </svg>
  251. <div class="line-clamp-1">{$i18n.t('Google Drive')}</div>
  252. </DropdownMenu.Item>
  253. {/if}
  254. {#if $config?.features?.enable_onedrive_integration}
  255. <DropdownMenu.Sub>
  256. <DropdownMenu.SubTrigger
  257. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl w-full"
  258. >
  259. <svg
  260. xmlns="http://www.w3.org/2000/svg"
  261. viewBox="0 0 32 32"
  262. class="w-5 h-5"
  263. fill="none"
  264. >
  265. <mask
  266. id="mask0_87_7796"
  267. style="mask-type:alpha"
  268. maskUnits="userSpaceOnUse"
  269. x="0"
  270. y="6"
  271. width="32"
  272. height="20"
  273. >
  274. <path
  275. d="M7.82979 26C3.50549 26 0 22.5675 0 18.3333C0 14.1921 3.35322 10.8179 7.54613 10.6716C9.27535 7.87166 12.4144 6 16 6C20.6308 6 24.5169 9.12183 25.5829 13.3335C29.1316 13.3603 32 16.1855 32 19.6667C32 23.0527 29 26 25.8723 25.9914L7.82979 26Z"
  276. fill="#C4C4C4"
  277. />
  278. </mask>
  279. <g mask="url(#mask0_87_7796)">
  280. <path
  281. d="M7.83017 26.0001C5.37824 26.0001 3.18957 24.8966 1.75391 23.1691L18.0429 16.3335L30.7089 23.4647C29.5926 24.9211 27.9066 26.0001 26.0004 25.9915C23.1254 26.0001 12.0629 26.0001 7.83017 26.0001Z"
  282. fill="url(#paint0_linear_87_7796)"
  283. />
  284. <path
  285. d="M25.5785 13.3149L18.043 16.3334L30.709 23.4647C31.5199 22.4065 32.0004 21.0916 32.0004 19.6669C32.0004 16.1857 29.1321 13.3605 25.5833 13.3337C25.5817 13.3274 25.5801 13.3212 25.5785 13.3149Z"
  286. fill="url(#paint1_linear_87_7796)"
  287. />
  288. <path
  289. d="M7.06445 10.7028L18.0423 16.3333L25.5779 13.3148C24.5051 9.11261 20.6237 6 15.9997 6C12.4141 6 9.27508 7.87166 7.54586 10.6716C7.3841 10.6773 7.22358 10.6877 7.06445 10.7028Z"
  290. fill="url(#paint2_linear_87_7796)"
  291. />
  292. <path
  293. d="M1.7535 23.1687L18.0425 16.3331L7.06471 10.7026C3.09947 11.0792 0 14.3517 0 18.3331C0 20.1665 0.657197 21.8495 1.7535 23.1687Z"
  294. fill="url(#paint3_linear_87_7796)"
  295. />
  296. </g>
  297. <defs>
  298. <linearGradient
  299. id="paint0_linear_87_7796"
  300. x1="4.42591"
  301. y1="24.6668"
  302. x2="27.2309"
  303. y2="23.2764"
  304. gradientUnits="userSpaceOnUse"
  305. >
  306. <stop stop-color="#2086B8" />
  307. <stop offset="1" stop-color="#46D3F6" />
  308. </linearGradient>
  309. <linearGradient
  310. id="paint1_linear_87_7796"
  311. x1="23.8302"
  312. y1="19.6668"
  313. x2="30.2108"
  314. y2="15.2082"
  315. gradientUnits="userSpaceOnUse"
  316. >
  317. <stop stop-color="#1694DB" />
  318. <stop offset="1" stop-color="#62C3FE" />
  319. </linearGradient>
  320. <linearGradient
  321. id="paint2_linear_87_7796"
  322. x1="8.51037"
  323. y1="7.33333"
  324. x2="23.3335"
  325. y2="15.9348"
  326. gradientUnits="userSpaceOnUse"
  327. >
  328. <stop stop-color="#0D3D78" />
  329. <stop offset="1" stop-color="#063B83" />
  330. </linearGradient>
  331. <linearGradient
  332. id="paint3_linear_87_7796"
  333. x1="-0.340429"
  334. y1="19.9998"
  335. x2="14.5634"
  336. y2="14.4649"
  337. gradientUnits="userSpaceOnUse"
  338. >
  339. <stop stop-color="#16589B" />
  340. <stop offset="1" stop-color="#1464B7" />
  341. </linearGradient>
  342. </defs>
  343. </svg>
  344. <div class="line-clamp-1">{$i18n.t('Microsoft OneDrive')}</div>
  345. </DropdownMenu.SubTrigger>
  346. <DropdownMenu.SubContent
  347. class="w-[calc(100vw-2rem)] max-w-[280px] rounded-xl px-1 py-1 border border-gray-100 dark:border-gray-800 z-50 bg-white dark:bg-gray-850 dark:text-white shadow-sm"
  348. side={$mobile ? 'bottom' : 'right'}
  349. sideOffset={$mobile ? 5 : 0}
  350. alignOffset={$mobile ? 0 : -8}
  351. >
  352. <DropdownMenu.Item
  353. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl"
  354. on:click={() => {
  355. uploadOneDriveHandler('personal');
  356. }}
  357. >
  358. <div class="line-clamp-1">{$i18n.t('Microsoft OneDrive (personal)')}</div>
  359. </DropdownMenu.Item>
  360. <DropdownMenu.Item
  361. class="flex gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl"
  362. on:click={() => {
  363. uploadOneDriveHandler('organizations');
  364. }}
  365. >
  366. <div class="flex flex-col">
  367. <div class="line-clamp-1">{$i18n.t('Microsoft OneDrive (work/school)')}</div>
  368. <div class="text-xs text-gray-500">{$i18n.t('Includes SharePoint')}</div>
  369. </div>
  370. </DropdownMenu.Item>
  371. </DropdownMenu.SubContent>
  372. </DropdownMenu.Sub>
  373. {/if}
  374. {/if}
  375. </DropdownMenu.Content>
  376. </div>
  377. </Dropdown>