Tools.svelte 15 KB

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