Tools.svelte 10 KB

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