Tools.svelte 12 KB

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