Tools.svelte 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. const i18n = getContext('i18n');
  25. let toolsImportInputElement: HTMLInputElement;
  26. let importFiles;
  27. let showConfirm = false;
  28. let query = '';
  29. let showManifestModal = false;
  30. let showValvesModal = false;
  31. let selectedTool = null;
  32. const shareHandler = async (tool) => {
  33. console.log(tool);
  34. };
  35. const cloneHandler = async (tool) => {
  36. const _tool = await getToolById(localStorage.token, tool.id).catch((error) => {
  37. toast.error(error);
  38. return null;
  39. });
  40. if (_tool) {
  41. sessionStorage.tool = JSON.stringify({
  42. ..._tool,
  43. id: `${_tool.id}_clone`,
  44. name: `${_tool.name} (Clone)`
  45. });
  46. goto('/workspace/tools/create');
  47. }
  48. };
  49. const exportHandler = async (tool) => {
  50. const _tool = await getToolById(localStorage.token, tool.id).catch((error) => {
  51. toast.error(error);
  52. return null;
  53. });
  54. if (_tool) {
  55. let blob = new Blob([JSON.stringify([_tool])], {
  56. type: 'application/json'
  57. });
  58. saveAs(blob, `tool-${_tool.id}-export-${Date.now()}.json`);
  59. }
  60. };
  61. const deleteHandler = async (tool) => {
  62. const res = await deleteToolById(localStorage.token, tool.id).catch((error) => {
  63. toast.error(error);
  64. return null;
  65. });
  66. if (res) {
  67. toast.success('Tool deleted successfully');
  68. tools.set(await getTools(localStorage.token));
  69. }
  70. };
  71. </script>
  72. <svelte:head>
  73. <title>
  74. {$i18n.t('Tools')} | {$WEBUI_NAME}
  75. </title>
  76. </svelte:head>
  77. <div class="mb-3 flex justify-between items-center">
  78. <div class=" text-lg font-semibold self-center">{$i18n.t('Tools')}</div>
  79. </div>
  80. <div class=" flex w-full space-x-2">
  81. <div class="flex flex-1">
  82. <div class=" self-center ml-1 mr-3">
  83. <svg
  84. xmlns="http://www.w3.org/2000/svg"
  85. viewBox="0 0 20 20"
  86. fill="currentColor"
  87. class="w-4 h-4"
  88. >
  89. <path
  90. fill-rule="evenodd"
  91. 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"
  92. clip-rule="evenodd"
  93. />
  94. </svg>
  95. </div>
  96. <input
  97. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-none bg-transparent"
  98. bind:value={query}
  99. placeholder={$i18n.t('Search Tools')}
  100. />
  101. </div>
  102. <div>
  103. <a
  104. 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"
  105. href="/workspace/tools/create"
  106. >
  107. <svg
  108. xmlns="http://www.w3.org/2000/svg"
  109. viewBox="0 0 16 16"
  110. fill="currentColor"
  111. class="w-4 h-4"
  112. >
  113. <path
  114. 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"
  115. />
  116. </svg>
  117. </a>
  118. </div>
  119. </div>
  120. <hr class=" dark:border-gray-850 my-2.5" />
  121. <div class="my-3 mb-5">
  122. {#each $tools.filter((t) => query === '' || t.name
  123. .toLowerCase()
  124. .includes(query.toLowerCase()) || t.id.toLowerCase().includes(query.toLowerCase())) as tool}
  125. <div
  126. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl"
  127. >
  128. <a
  129. class=" flex flex-1 space-x-3.5 cursor-pointer w-full"
  130. href={`/workspace/tools/edit?id=${encodeURIComponent(tool.id)}`}
  131. >
  132. <div class="flex items-center text-left">
  133. <div class=" flex-1 self-center pl-1">
  134. <div class=" font-semibold flex items-center gap-1.5">
  135. <div
  136. class=" text-xs font-black px-1 rounded uppercase line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
  137. >
  138. TOOL
  139. </div>
  140. <div class="line-clamp-1">
  141. {tool.name}
  142. </div>
  143. </div>
  144. <div class="flex gap-1.5 px-1">
  145. <div class=" text-gray-500 text-xs font-medium flex-shrink-0">{tool.id}</div>
  146. <div class=" text-xs overflow-hidden text-ellipsis line-clamp-1">
  147. {tool.meta.description}
  148. </div>
  149. </div>
  150. </div>
  151. </div>
  152. </a>
  153. <div class="flex flex-row gap-0.5 self-center">
  154. {#if tool?.meta?.manifest?.funding_url ?? false}
  155. <Tooltip content="Support">
  156. <button
  157. 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"
  158. type="button"
  159. on:click={() => {
  160. selectedTool = tool;
  161. showManifestModal = true;
  162. }}
  163. >
  164. <Heart />
  165. </button>
  166. </Tooltip>
  167. {/if}
  168. <Tooltip content="Valves">
  169. <button
  170. 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"
  171. type="button"
  172. on:click={() => {
  173. selectedTool = tool;
  174. showValvesModal = true;
  175. }}
  176. >
  177. <svg
  178. xmlns="http://www.w3.org/2000/svg"
  179. fill="none"
  180. viewBox="0 0 24 24"
  181. stroke-width="1.5"
  182. stroke="currentColor"
  183. class="size-4"
  184. >
  185. <path
  186. stroke-linecap="round"
  187. stroke-linejoin="round"
  188. 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"
  189. />
  190. <path
  191. stroke-linecap="round"
  192. stroke-linejoin="round"
  193. d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  194. />
  195. </svg>
  196. </button>
  197. </Tooltip>
  198. <ToolMenu
  199. editHandler={() => {
  200. goto(`/workspace/tools/edit?id=${encodeURIComponent(tool.id)}`);
  201. }}
  202. shareHandler={() => {
  203. shareHandler(tool);
  204. }}
  205. cloneHandler={() => {
  206. cloneHandler(tool);
  207. }}
  208. exportHandler={() => {
  209. exportHandler(tool);
  210. }}
  211. deleteHandler={async () => {
  212. deleteHandler(tool);
  213. }}
  214. onClose={() => {}}
  215. >
  216. <button
  217. 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"
  218. type="button"
  219. >
  220. <EllipsisHorizontal className="size-5" />
  221. </button>
  222. </ToolMenu>
  223. </div>
  224. </div>
  225. {/each}
  226. </div>
  227. <div class=" text-gray-500 text-xs mt-1 mb-2">
  228. ⓘ {$i18n.t(
  229. 'Admins have access to all tools at all times; users need tools assigned per model in the workspace.'
  230. )}
  231. </div>
  232. <div class=" flex justify-end w-full mb-2">
  233. <div class="flex space-x-2">
  234. <input
  235. id="documents-import-input"
  236. bind:this={toolsImportInputElement}
  237. bind:files={importFiles}
  238. type="file"
  239. accept=".json"
  240. hidden
  241. on:change={() => {
  242. console.log(importFiles);
  243. showConfirm = true;
  244. }}
  245. />
  246. <button
  247. 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"
  248. on:click={() => {
  249. toolsImportInputElement.click();
  250. }}
  251. >
  252. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Import Tools')}</div>
  253. <div class=" self-center">
  254. <svg
  255. xmlns="http://www.w3.org/2000/svg"
  256. viewBox="0 0 16 16"
  257. fill="currentColor"
  258. class="w-4 h-4"
  259. >
  260. <path
  261. fill-rule="evenodd"
  262. 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"
  263. clip-rule="evenodd"
  264. />
  265. </svg>
  266. </div>
  267. </button>
  268. <button
  269. 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"
  270. on:click={async () => {
  271. const _tools = await exportTools(localStorage.token).catch((error) => {
  272. toast.error(error);
  273. return null;
  274. });
  275. if (_tools) {
  276. let blob = new Blob([JSON.stringify(_tools)], {
  277. type: 'application/json'
  278. });
  279. saveAs(blob, `tools-export-${Date.now()}.json`);
  280. }
  281. }}
  282. >
  283. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Export Tools')}</div>
  284. <div class=" self-center">
  285. <svg
  286. xmlns="http://www.w3.org/2000/svg"
  287. viewBox="0 0 16 16"
  288. fill="currentColor"
  289. class="w-4 h-4"
  290. >
  291. <path
  292. fill-rule="evenodd"
  293. 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"
  294. clip-rule="evenodd"
  295. />
  296. </svg>
  297. </div>
  298. </button>
  299. </div>
  300. </div>
  301. <div class=" my-16">
  302. <div class=" text-lg font-semibold mb-3 line-clamp-1">
  303. {$i18n.t('Made by OpenWebUI Community')}
  304. </div>
  305. <a
  306. class=" flex space-x-4 cursor-pointer w-full mb-2 px-3 py-2"
  307. href="https://openwebui.com/"
  308. target="_blank"
  309. >
  310. <div class=" self-center w-10 flex-shrink-0">
  311. <div
  312. class="w-full h-10 flex justify-center rounded-full bg-transparent dark:bg-gray-700 border border-dashed border-gray-200"
  313. >
  314. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6">
  315. <path
  316. fill-rule="evenodd"
  317. 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"
  318. clip-rule="evenodd"
  319. />
  320. </svg>
  321. </div>
  322. </div>
  323. <div class=" self-center">
  324. <div class=" font-bold line-clamp-1">{$i18n.t('Discover a tool')}</div>
  325. <div class=" text-sm line-clamp-1">
  326. {$i18n.t('Discover, download, and explore custom tools')}
  327. </div>
  328. </div>
  329. </a>
  330. </div>
  331. <ValvesModal bind:show={showValvesModal} type="tool" id={selectedTool?.id ?? null} />
  332. <ManifestModal bind:show={showManifestModal} manifest={selectedTool?.meta?.manifest ?? {}} />
  333. <ConfirmDialog
  334. bind:show={showConfirm}
  335. on:confirm={() => {
  336. const reader = new FileReader();
  337. reader.onload = async (event) => {
  338. const _tools = JSON.parse(event.target.result);
  339. console.log(_tools);
  340. for (const tool of _tools) {
  341. const res = await createNewTool(localStorage.token, tool).catch((error) => {
  342. toast.error(error);
  343. return null;
  344. });
  345. }
  346. toast.success('Tool imported successfully');
  347. tools.set(await getTools(localStorage.token));
  348. };
  349. reader.readAsText(importFiles[0]);
  350. }}
  351. >
  352. <div class="text-sm text-gray-500">
  353. <div class=" bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3">
  354. <div>Please carefully review the following warnings:</div>
  355. <ul class=" mt-1 list-disc pl-4 text-xs">
  356. <li>Tools have a function calling system that allows arbitrary code execution.</li>
  357. <li>Do not install tools from sources you do not fully trust.</li>
  358. </ul>
  359. </div>
  360. <div class="my-3">
  361. I acknowledge that I have read and I understand the implications of my action. I am aware of
  362. the risks associated with executing arbitrary code and I have verified the trustworthiness of
  363. the source.
  364. </div>
  365. </div>
  366. </ConfirmDialog>