Tools.svelte 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import fileSaver from 'file-saver';
  4. const { saveAs } = fileSaver;
  5. import { onMount, getContext } from 'svelte';
  6. import { WEBUI_NAME, config, prompts, tools } from '$lib/stores';
  7. import { createNewPrompt, deletePromptByCommand, getPrompts } from '$lib/apis/prompts';
  8. import { goto } from '$app/navigation';
  9. import {
  10. createNewTool,
  11. deleteToolById,
  12. exportTools,
  13. getToolById,
  14. getTools
  15. } from '$lib/apis/tools';
  16. import ArrowDownTray from '../icons/ArrowDownTray.svelte';
  17. import Tooltip from '../common/Tooltip.svelte';
  18. import ConfirmDialog from '../common/ConfirmDialog.svelte';
  19. import ToolMenu from './Tools/ToolMenu.svelte';
  20. import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte';
  21. import ValvesModal from './common/ValvesModal.svelte';
  22. import ManifestModal from './common/ManifestModal.svelte';
  23. import Heart from '../icons/Heart.svelte';
  24. import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  25. import GarbageBin from '../icons/GarbageBin.svelte';
  26. import Search from '../icons/Search.svelte';
  27. import Plus from '../icons/Plus.svelte';
  28. const i18n = getContext('i18n');
  29. let shiftKey = false;
  30. let toolsImportInputElement: HTMLInputElement;
  31. let importFiles;
  32. let showConfirm = false;
  33. let query = '';
  34. let showManifestModal = false;
  35. let showValvesModal = false;
  36. let selectedTool = null;
  37. let showDeleteConfirm = false;
  38. let filteredItems = [];
  39. $: filteredItems = $tools.filter(
  40. (t) =>
  41. query === '' ||
  42. t.name.toLowerCase().includes(query.toLowerCase()) ||
  43. t.id.toLowerCase().includes(query.toLowerCase())
  44. );
  45. const shareHandler = async (tool) => {
  46. const item = await getToolById(localStorage.token, tool.id).catch((error) => {
  47. toast.error(error);
  48. return null;
  49. });
  50. toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
  51. const url = 'https://openwebui.com';
  52. const tab = await window.open(`${url}/tools/create`, '_blank');
  53. // Define the event handler function
  54. const messageHandler = (event) => {
  55. if (event.origin !== url) return;
  56. if (event.data === 'loaded') {
  57. tab.postMessage(JSON.stringify(item), '*');
  58. // Remove the event listener after handling the message
  59. window.removeEventListener('message', messageHandler);
  60. }
  61. };
  62. window.addEventListener('message', messageHandler, false);
  63. console.log(item);
  64. };
  65. const cloneHandler = async (tool) => {
  66. const _tool = await getToolById(localStorage.token, tool.id).catch((error) => {
  67. toast.error(error);
  68. return null;
  69. });
  70. if (_tool) {
  71. sessionStorage.tool = JSON.stringify({
  72. ..._tool,
  73. id: `${_tool.id}_clone`,
  74. name: `${_tool.name} (Clone)`
  75. });
  76. goto('/workspace/tools/create');
  77. }
  78. };
  79. const exportHandler = async (tool) => {
  80. const _tool = await getToolById(localStorage.token, tool.id).catch((error) => {
  81. toast.error(error);
  82. return null;
  83. });
  84. if (_tool) {
  85. let blob = new Blob([JSON.stringify([_tool])], {
  86. type: 'application/json'
  87. });
  88. saveAs(blob, `tool-${_tool.id}-export-${Date.now()}.json`);
  89. }
  90. };
  91. const deleteHandler = async (tool) => {
  92. const res = await deleteToolById(localStorage.token, tool.id).catch((error) => {
  93. toast.error(error);
  94. return null;
  95. });
  96. if (res) {
  97. toast.success($i18n.t('Tool deleted successfully'));
  98. tools.set(await getTools(localStorage.token));
  99. }
  100. };
  101. onMount(() => {
  102. const onKeyDown = (event) => {
  103. if (event.key === 'Shift') {
  104. shiftKey = true;
  105. }
  106. };
  107. const onKeyUp = (event) => {
  108. if (event.key === 'Shift') {
  109. shiftKey = false;
  110. }
  111. };
  112. const onBlur = () => {
  113. shiftKey = false;
  114. };
  115. window.addEventListener('keydown', onKeyDown);
  116. window.addEventListener('keyup', onKeyUp);
  117. window.addEventListener('blur', onBlur);
  118. return () => {
  119. window.removeEventListener('keydown', onKeyDown);
  120. window.removeEventListener('keyup', onKeyUp);
  121. window.removeEventListener('blur', onBlur);
  122. };
  123. });
  124. </script>
  125. <svelte:head>
  126. <title>
  127. {$i18n.t('Tools')} | {$WEBUI_NAME}
  128. </title>
  129. </svelte:head>
  130. <div class="flex flex-col gap-1 mt-1.5 mb-2">
  131. <div class="flex justify-between items-center">
  132. <div class="flex md:self-center text-xl font-medium px-0.5 items-center">
  133. {$i18n.t('Tools')}
  134. <div class="flex self-center w-[1px] h-6 mx-2.5 bg-gray-50 dark:bg-gray-850" />
  135. <span class="text-lg font-medium text-gray-500 dark:text-gray-300"
  136. >{filteredItems.length}</span
  137. >
  138. </div>
  139. </div>
  140. <div class=" flex w-full space-x-2">
  141. <div class="flex flex-1">
  142. <div class=" self-center ml-1 mr-3">
  143. <Search className="size-3.5" />
  144. </div>
  145. <input
  146. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-none bg-transparent"
  147. bind:value={query}
  148. placeholder={$i18n.t('Search Tools')}
  149. />
  150. </div>
  151. <div>
  152. <a
  153. class=" px-2 py-2 rounded-xl hover:bg-gray-700/10 dark:hover:bg-gray-100/10 dark:text-gray-300 dark:hover:text-white transition font-medium text-sm flex items-center space-x-1"
  154. href="/workspace/tools/create"
  155. >
  156. <Plus className="size-3.5" />
  157. </a>
  158. </div>
  159. </div>
  160. </div>
  161. <div class="mb-5">
  162. {#each filteredItems as tool}
  163. <div
  164. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl"
  165. >
  166. <a
  167. class=" flex flex-1 space-x-3.5 cursor-pointer w-full"
  168. href={`/workspace/tools/edit?id=${encodeURIComponent(tool.id)}`}
  169. >
  170. <div class="flex items-center text-left">
  171. <div class=" flex-1 self-center pl-1">
  172. <div class=" font-semibold flex items-center gap-1.5">
  173. <div
  174. class=" text-xs font-bold px-1 rounded uppercase line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
  175. >
  176. TOOL
  177. </div>
  178. {#if tool?.meta?.manifest?.version}
  179. <div
  180. class="text-xs font-bold px-1 rounded line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
  181. >
  182. v{tool?.meta?.manifest?.version ?? ''}
  183. </div>
  184. {/if}
  185. <div class="line-clamp-1">
  186. {tool.name}
  187. </div>
  188. </div>
  189. <div class="flex gap-1.5 px-1">
  190. <div class=" text-gray-500 text-xs font-medium flex-shrink-0">{tool.id}</div>
  191. <div class=" text-xs overflow-hidden text-ellipsis line-clamp-1">
  192. {tool.meta.description}
  193. </div>
  194. </div>
  195. </div>
  196. </div>
  197. </a>
  198. <div class="flex flex-row gap-0.5 self-center">
  199. {#if shiftKey}
  200. <Tooltip content={$i18n.t('Delete')}>
  201. <button
  202. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  203. type="button"
  204. on:click={() => {
  205. deleteHandler(tool);
  206. }}
  207. >
  208. <GarbageBin />
  209. </button>
  210. </Tooltip>
  211. {:else}
  212. {#if tool?.meta?.manifest?.funding_url ?? false}
  213. <Tooltip content="Support">
  214. <button
  215. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  216. type="button"
  217. on:click={() => {
  218. selectedTool = tool;
  219. showManifestModal = true;
  220. }}
  221. >
  222. <Heart />
  223. </button>
  224. </Tooltip>
  225. {/if}
  226. <Tooltip content={$i18n.t('Valves')}>
  227. <button
  228. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  229. type="button"
  230. on:click={() => {
  231. selectedTool = tool;
  232. showValvesModal = true;
  233. }}
  234. >
  235. <svg
  236. xmlns="http://www.w3.org/2000/svg"
  237. fill="none"
  238. viewBox="0 0 24 24"
  239. stroke-width="1.5"
  240. stroke="currentColor"
  241. class="size-4"
  242. >
  243. <path
  244. stroke-linecap="round"
  245. stroke-linejoin="round"
  246. d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 0 1 1.37.49l1.296 2.247a1.125 1.125 0 0 1-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a7.723 7.723 0 0 1 0 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 0 1-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.47 6.47 0 0 1-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 0 1-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 0 1-1.369-.49l-1.297-2.247a1.125 1.125 0 0 1 .26-1.431l1.004-.827c.292-.24.437-.613.43-.991a6.932 6.932 0 0 1 0-.255c.007-.38-.138-.751-.43-.992l-1.004-.827a1.125 1.125 0 0 1-.26-1.43l1.297-2.247a1.125 1.125 0 0 1 1.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.086.22-.128.332-.183.582-.495.644-.869l.214-1.28Z"
  247. />
  248. <path
  249. stroke-linecap="round"
  250. stroke-linejoin="round"
  251. d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  252. />
  253. </svg>
  254. </button>
  255. </Tooltip>
  256. <ToolMenu
  257. editHandler={() => {
  258. goto(`/workspace/tools/edit?id=${encodeURIComponent(tool.id)}`);
  259. }}
  260. shareHandler={() => {
  261. shareHandler(tool);
  262. }}
  263. cloneHandler={() => {
  264. cloneHandler(tool);
  265. }}
  266. exportHandler={() => {
  267. exportHandler(tool);
  268. }}
  269. deleteHandler={async () => {
  270. selectedTool = tool;
  271. showDeleteConfirm = true;
  272. }}
  273. onClose={() => {}}
  274. >
  275. <button
  276. class="self-center w-fit text-sm p-1.5 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  277. type="button"
  278. >
  279. <EllipsisHorizontal className="size-5" />
  280. </button>
  281. </ToolMenu>
  282. {/if}
  283. </div>
  284. </div>
  285. {/each}
  286. </div>
  287. <div class=" text-gray-500 text-xs mt-1 mb-2">
  288. ⓘ {$i18n.t(
  289. 'Admins have access to all tools at all times; users need tools assigned per model in the workspace.'
  290. )}
  291. </div>
  292. <div class=" flex justify-end w-full mb-2">
  293. <div class="flex space-x-2">
  294. <input
  295. id="documents-import-input"
  296. bind:this={toolsImportInputElement}
  297. bind:files={importFiles}
  298. type="file"
  299. accept=".json"
  300. hidden
  301. on:change={() => {
  302. console.log(importFiles);
  303. showConfirm = true;
  304. }}
  305. />
  306. <button
  307. class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
  308. on:click={() => {
  309. toolsImportInputElement.click();
  310. }}
  311. >
  312. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Import Tools')}</div>
  313. <div class=" self-center">
  314. <svg
  315. xmlns="http://www.w3.org/2000/svg"
  316. viewBox="0 0 16 16"
  317. fill="currentColor"
  318. class="w-4 h-4"
  319. >
  320. <path
  321. fill-rule="evenodd"
  322. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 9.5a.75.75 0 0 1-.75-.75V8.06l-.72.72a.75.75 0 0 1-1.06-1.06l2-2a.75.75 0 0 1 1.06 0l2 2a.75.75 0 1 1-1.06 1.06l-.72-.72v2.69a.75.75 0 0 1-.75.75Z"
  323. clip-rule="evenodd"
  324. />
  325. </svg>
  326. </div>
  327. </button>
  328. <button
  329. class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
  330. on:click={async () => {
  331. const _tools = await exportTools(localStorage.token).catch((error) => {
  332. toast.error(error);
  333. return null;
  334. });
  335. if (_tools) {
  336. let blob = new Blob([JSON.stringify(_tools)], {
  337. type: 'application/json'
  338. });
  339. saveAs(blob, `tools-export-${Date.now()}.json`);
  340. }
  341. }}
  342. >
  343. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Export Tools')}</div>
  344. <div class=" self-center">
  345. <svg
  346. xmlns="http://www.w3.org/2000/svg"
  347. viewBox="0 0 16 16"
  348. fill="currentColor"
  349. class="w-4 h-4"
  350. >
  351. <path
  352. fill-rule="evenodd"
  353. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 3.5a.75.75 0 0 1 .75.75v2.69l.72-.72a.75.75 0 1 1 1.06 1.06l-2 2a.75.75 0 0 1-1.06 0l-2-2a.75.75 0 0 1 1.06-1.06l.72.72V6.25A.75.75 0 0 1 8 5.5Z"
  354. clip-rule="evenodd"
  355. />
  356. </svg>
  357. </div>
  358. </button>
  359. </div>
  360. </div>
  361. {#if $config?.features.enable_community_sharing}
  362. <div class=" my-16">
  363. <div class=" text-lg font-semibold mb-3 line-clamp-1">
  364. {$i18n.t('Made by OpenWebUI Community')}
  365. </div>
  366. <a
  367. class=" flex space-x-4 cursor-pointer w-full mb-2 px-3 py-2"
  368. href="https://openwebui.com/#open-webui-community"
  369. target="_blank"
  370. >
  371. <div class=" self-center w-10 flex-shrink-0">
  372. <div
  373. class="w-full h-10 flex justify-center rounded-full bg-transparent dark:bg-gray-700 border border-dashed border-gray-200"
  374. >
  375. <svg
  376. xmlns="http://www.w3.org/2000/svg"
  377. viewBox="0 0 24 24"
  378. fill="currentColor"
  379. class="w-6"
  380. >
  381. <path
  382. fill-rule="evenodd"
  383. d="M12 3.75a.75.75 0 01.75.75v6.75h6.75a.75.75 0 010 1.5h-6.75v6.75a.75.75 0 01-1.5 0v-6.75H4.5a.75.75 0 010-1.5h6.75V4.5a.75.75 0 01.75-.75z"
  384. clip-rule="evenodd"
  385. />
  386. </svg>
  387. </div>
  388. </div>
  389. <div class=" self-center">
  390. <div class=" font-semibold line-clamp-1">{$i18n.t('Discover a tool')}</div>
  391. <div class=" text-sm line-clamp-1">
  392. {$i18n.t('Discover, download, and explore custom tools')}
  393. </div>
  394. </div>
  395. </a>
  396. </div>
  397. {/if}
  398. <DeleteConfirmDialog
  399. bind:show={showDeleteConfirm}
  400. title={$i18n.t('Delete tool?')}
  401. on:confirm={() => {
  402. deleteHandler(selectedTool);
  403. }}
  404. >
  405. <div class=" text-sm text-gray-500">
  406. {$i18n.t('This will delete')} <span class=" font-semibold">{selectedTool.name}</span>.
  407. </div>
  408. </DeleteConfirmDialog>
  409. <ValvesModal bind:show={showValvesModal} type="tool" id={selectedTool?.id ?? null} />
  410. <ManifestModal bind:show={showManifestModal} manifest={selectedTool?.meta?.manifest ?? {}} />
  411. <ConfirmDialog
  412. bind:show={showConfirm}
  413. on:confirm={() => {
  414. const reader = new FileReader();
  415. reader.onload = async (event) => {
  416. const _tools = JSON.parse(event.target.result);
  417. console.log(_tools);
  418. for (const tool of _tools) {
  419. const res = await createNewTool(localStorage.token, tool).catch((error) => {
  420. toast.error(error);
  421. return null;
  422. });
  423. }
  424. toast.success($i18n.t('Tool imported successfully'));
  425. tools.set(await getTools(localStorage.token));
  426. };
  427. reader.readAsText(importFiles[0]);
  428. }}
  429. >
  430. <div class="text-sm text-gray-500">
  431. <div class=" bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3">
  432. <div>{$i18n.t('Please carefully review the following warnings:')}</div>
  433. <ul class=" mt-1 list-disc pl-4 text-xs">
  434. <li>
  435. {$i18n.t('Tools have a function calling system that allows arbitrary code execution')}.
  436. </li>
  437. <li>{$i18n.t('Do not install tools from sources you do not fully trust.')}</li>
  438. </ul>
  439. </div>
  440. <div class="my-3">
  441. {$i18n.t(
  442. 'I acknowledge that I have read and I understand the implications of my action. I am aware of the risks associated with executing arbitrary code and I have verified the trustworthiness of the source.'
  443. )}
  444. </div>
  445. </div>
  446. </ConfirmDialog>