Tools.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. const i18n = getContext('i18n');
  20. let toolsImportInputElement: HTMLInputElement;
  21. let importFiles;
  22. let showConfirm = false;
  23. let query = '';
  24. </script>
  25. <svelte:head>
  26. <title>
  27. {$i18n.t('Tools')} | {$WEBUI_NAME}
  28. </title>
  29. </svelte:head>
  30. <div class="mb-3 flex justify-between items-center">
  31. <div class=" text-lg font-semibold self-center">{$i18n.t('Tools')}</div>
  32. </div>
  33. <div class=" flex w-full space-x-2">
  34. <div class="flex flex-1">
  35. <div class=" self-center ml-1 mr-3">
  36. <svg
  37. xmlns="http://www.w3.org/2000/svg"
  38. viewBox="0 0 20 20"
  39. fill="currentColor"
  40. class="w-4 h-4"
  41. >
  42. <path
  43. fill-rule="evenodd"
  44. 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"
  45. clip-rule="evenodd"
  46. />
  47. </svg>
  48. </div>
  49. <input
  50. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-none bg-transparent"
  51. bind:value={query}
  52. placeholder={$i18n.t('Search Tools')}
  53. />
  54. </div>
  55. <div>
  56. <a
  57. 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"
  58. href="/workspace/tools/create"
  59. >
  60. <svg
  61. xmlns="http://www.w3.org/2000/svg"
  62. viewBox="0 0 16 16"
  63. fill="currentColor"
  64. class="w-4 h-4"
  65. >
  66. <path
  67. 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"
  68. />
  69. </svg>
  70. </a>
  71. </div>
  72. </div>
  73. <hr class=" dark:border-gray-850 my-2.5" />
  74. <div class="my-3 mb-5">
  75. {#each $tools.filter((t) => query === '' || t.name
  76. .toLowerCase()
  77. .includes(query.toLowerCase()) || t.id.toLowerCase().includes(query.toLowerCase())) as tool}
  78. <button
  79. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl"
  80. type="button"
  81. on:click={() => {
  82. goto(`/workspace/tools/edit?id=${encodeURIComponent(tool.id)}`);
  83. }}
  84. >
  85. <div class=" flex flex-1 space-x-4 cursor-pointer w-full">
  86. <a
  87. href={`/workspace/tools/edit?id=${encodeURIComponent(tool.id)}`}
  88. class="flex items-center text-left"
  89. >
  90. <div class=" flex-1 self-center pl-1">
  91. <div class=" font-semibold flex items-center gap-1.5">
  92. <div
  93. class=" text-xs font-black px-1 rounded uppercase line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
  94. >
  95. TOOL
  96. </div>
  97. <div>
  98. {tool.name}
  99. </div>
  100. </div>
  101. <div class="flex gap-1.5 px-1">
  102. <div class=" text-gray-500 text-xs font-medium">{tool.id}</div>
  103. <div class=" text-xs overflow-hidden text-ellipsis line-clamp-1">
  104. {tool.meta.description}
  105. </div>
  106. </div>
  107. </div>
  108. </a>
  109. </div>
  110. <div class="flex flex-row space-x-1 self-center">
  111. <Tooltip content="Edit">
  112. <a
  113. 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"
  114. type="button"
  115. href={`/workspace/tools/edit?id=${encodeURIComponent(tool.id)}`}
  116. >
  117. <svg
  118. xmlns="http://www.w3.org/2000/svg"
  119. fill="none"
  120. viewBox="0 0 24 24"
  121. stroke-width="1.5"
  122. stroke="currentColor"
  123. class="w-4 h-4"
  124. >
  125. <path
  126. stroke-linecap="round"
  127. stroke-linejoin="round"
  128. 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"
  129. />
  130. </svg>
  131. </a>
  132. </Tooltip>
  133. <Tooltip content="Clone">
  134. <button
  135. 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"
  136. type="button"
  137. on:click={async (e) => {
  138. e.stopPropagation();
  139. const _tool = await getToolById(localStorage.token, tool.id).catch((error) => {
  140. toast.error(error);
  141. return null;
  142. });
  143. if (_tool) {
  144. sessionStorage.tool = JSON.stringify({
  145. ..._tool,
  146. id: `${_tool.id}_clone`,
  147. name: `${_tool.name} (Clone)`
  148. });
  149. goto('/workspace/tools/create');
  150. }
  151. }}
  152. >
  153. <svg
  154. xmlns="http://www.w3.org/2000/svg"
  155. fill="none"
  156. viewBox="0 0 24 24"
  157. stroke-width="1.5"
  158. stroke="currentColor"
  159. class="w-4 h-4"
  160. >
  161. <path
  162. stroke-linecap="round"
  163. stroke-linejoin="round"
  164. d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"
  165. />
  166. </svg>
  167. </button>
  168. </Tooltip>
  169. <Tooltip content="Export">
  170. <button
  171. 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"
  172. type="button"
  173. on:click={async (e) => {
  174. e.stopPropagation();
  175. const _tool = await getToolById(localStorage.token, tool.id).catch((error) => {
  176. toast.error(error);
  177. return null;
  178. });
  179. if (_tool) {
  180. let blob = new Blob([JSON.stringify([_tool])], {
  181. type: 'application/json'
  182. });
  183. saveAs(blob, `tool-${_tool.id}-export-${Date.now()}.json`);
  184. }
  185. }}
  186. >
  187. <ArrowDownTray />
  188. </button>
  189. </Tooltip>
  190. <Tooltip content="Delete">
  191. <button
  192. 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"
  193. type="button"
  194. on:click={async (e) => {
  195. e.stopPropagation();
  196. const res = await deleteToolById(localStorage.token, tool.id).catch((error) => {
  197. toast.error(error);
  198. return null;
  199. });
  200. if (res) {
  201. toast.success('Tool deleted successfully');
  202. tools.set(await getTools(localStorage.token));
  203. }
  204. }}
  205. >
  206. <svg
  207. xmlns="http://www.w3.org/2000/svg"
  208. fill="none"
  209. viewBox="0 0 24 24"
  210. stroke-width="1.5"
  211. stroke="currentColor"
  212. class="w-4 h-4"
  213. >
  214. <path
  215. stroke-linecap="round"
  216. stroke-linejoin="round"
  217. d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"
  218. />
  219. </svg>
  220. </button>
  221. </Tooltip>
  222. </div>
  223. </button>
  224. {/each}
  225. </div>
  226. <div class=" text-gray-500 text-xs mt-1 mb-2">
  227. ⓘ {$i18n.t(
  228. 'Admins have access to all tools at all times; users need tools assigned per model in the workspace.'
  229. )}
  230. </div>
  231. <div class=" flex justify-end w-full mb-2">
  232. <div class="flex space-x-2">
  233. <input
  234. id="documents-import-input"
  235. bind:this={toolsImportInputElement}
  236. bind:files={importFiles}
  237. type="file"
  238. accept=".json"
  239. hidden
  240. on:change={() => {
  241. console.log(importFiles);
  242. showConfirm = true;
  243. }}
  244. />
  245. <button
  246. 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"
  247. on:click={() => {
  248. toolsImportInputElement.click();
  249. }}
  250. >
  251. <div class=" self-center mr-2 font-medium">{$i18n.t('Import Tools')}</div>
  252. <div class=" self-center">
  253. <svg
  254. xmlns="http://www.w3.org/2000/svg"
  255. viewBox="0 0 16 16"
  256. fill="currentColor"
  257. class="w-4 h-4"
  258. >
  259. <path
  260. fill-rule="evenodd"
  261. 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"
  262. clip-rule="evenodd"
  263. />
  264. </svg>
  265. </div>
  266. </button>
  267. <button
  268. 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"
  269. on:click={async () => {
  270. const _tools = await exportTools(localStorage.token).catch((error) => {
  271. toast.error(error);
  272. return null;
  273. });
  274. if (_tools) {
  275. let blob = new Blob([JSON.stringify(_tools)], {
  276. type: 'application/json'
  277. });
  278. saveAs(blob, `tools-export-${Date.now()}.json`);
  279. }
  280. }}
  281. >
  282. <div class=" self-center mr-2 font-medium">{$i18n.t('Export Tools')}</div>
  283. <div class=" self-center">
  284. <svg
  285. xmlns="http://www.w3.org/2000/svg"
  286. viewBox="0 0 16 16"
  287. fill="currentColor"
  288. class="w-4 h-4"
  289. >
  290. <path
  291. fill-rule="evenodd"
  292. 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"
  293. clip-rule="evenodd"
  294. />
  295. </svg>
  296. </div>
  297. </button>
  298. </div>
  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>