Tools.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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-5">
  91. <div class=" font-semibold flex items-center gap-1.5">
  92. <div>
  93. {tool.name}
  94. </div>
  95. <div class=" text-gray-500 text-xs font-medium">{tool.id}</div>
  96. </div>
  97. <div class=" text-xs overflow-hidden text-ellipsis line-clamp-1">
  98. {tool.meta.description}
  99. </div>
  100. </div>
  101. </a>
  102. </div>
  103. <div class="flex flex-row space-x-1 self-center">
  104. <Tooltip content="Edit">
  105. <a
  106. 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"
  107. type="button"
  108. href={`/workspace/tools/edit?id=${encodeURIComponent(tool.id)}`}
  109. >
  110. <svg
  111. xmlns="http://www.w3.org/2000/svg"
  112. fill="none"
  113. viewBox="0 0 24 24"
  114. stroke-width="1.5"
  115. stroke="currentColor"
  116. class="w-4 h-4"
  117. >
  118. <path
  119. stroke-linecap="round"
  120. stroke-linejoin="round"
  121. 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"
  122. />
  123. </svg>
  124. </a>
  125. </Tooltip>
  126. <Tooltip content="Clone">
  127. <button
  128. 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"
  129. type="button"
  130. on:click={async (e) => {
  131. e.stopPropagation();
  132. const _tool = await getToolById(localStorage.token, tool.id).catch((error) => {
  133. toast.error(error);
  134. return null;
  135. });
  136. if (_tool) {
  137. sessionStorage.tool = JSON.stringify({
  138. ..._tool,
  139. id: `${_tool.id}_clone`,
  140. name: `${_tool.name} (Clone)`
  141. });
  142. goto('/workspace/tools/create');
  143. }
  144. }}
  145. >
  146. <svg
  147. xmlns="http://www.w3.org/2000/svg"
  148. fill="none"
  149. viewBox="0 0 24 24"
  150. stroke-width="1.5"
  151. stroke="currentColor"
  152. class="w-4 h-4"
  153. >
  154. <path
  155. stroke-linecap="round"
  156. stroke-linejoin="round"
  157. 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"
  158. />
  159. </svg>
  160. </button>
  161. </Tooltip>
  162. <Tooltip content="Export">
  163. <button
  164. 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"
  165. type="button"
  166. on:click={async (e) => {
  167. e.stopPropagation();
  168. const _tool = await getToolById(localStorage.token, tool.id).catch((error) => {
  169. toast.error(error);
  170. return null;
  171. });
  172. if (_tool) {
  173. let blob = new Blob([JSON.stringify([_tool])], {
  174. type: 'application/json'
  175. });
  176. saveAs(blob, `tool-${_tool.id}-export-${Date.now()}.json`);
  177. }
  178. }}
  179. >
  180. <ArrowDownTray />
  181. </button>
  182. </Tooltip>
  183. <Tooltip content="Delete">
  184. <button
  185. 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"
  186. type="button"
  187. on:click={async (e) => {
  188. e.stopPropagation();
  189. const res = await deleteToolById(localStorage.token, tool.id).catch((error) => {
  190. toast.error(error);
  191. return null;
  192. });
  193. if (res) {
  194. toast.success('Tool deleted successfully');
  195. tools.set(await getTools(localStorage.token));
  196. }
  197. }}
  198. >
  199. <svg
  200. xmlns="http://www.w3.org/2000/svg"
  201. fill="none"
  202. viewBox="0 0 24 24"
  203. stroke-width="1.5"
  204. stroke="currentColor"
  205. class="w-4 h-4"
  206. >
  207. <path
  208. stroke-linecap="round"
  209. stroke-linejoin="round"
  210. 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"
  211. />
  212. </svg>
  213. </button>
  214. </Tooltip>
  215. </div>
  216. </button>
  217. {/each}
  218. </div>
  219. <div class=" text-gray-500 text-xs mt-1 mb-2">
  220. ⓘ {$i18n.t(
  221. 'Admins have access to all tools at all times; users need tools assigned per model in the workspace.'
  222. )}
  223. </div>
  224. <div class=" flex justify-end w-full mb-2">
  225. <div class="flex space-x-2">
  226. <input
  227. id="documents-import-input"
  228. bind:this={toolsImportInputElement}
  229. bind:files={importFiles}
  230. type="file"
  231. accept=".json"
  232. hidden
  233. on:change={() => {
  234. console.log(importFiles);
  235. showConfirm = true;
  236. }}
  237. />
  238. <button
  239. 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"
  240. on:click={() => {
  241. toolsImportInputElement.click();
  242. }}
  243. >
  244. <div class=" self-center mr-2 font-medium">{$i18n.t('Import Tools')}</div>
  245. <div class=" self-center">
  246. <svg
  247. xmlns="http://www.w3.org/2000/svg"
  248. viewBox="0 0 16 16"
  249. fill="currentColor"
  250. class="w-4 h-4"
  251. >
  252. <path
  253. fill-rule="evenodd"
  254. 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"
  255. clip-rule="evenodd"
  256. />
  257. </svg>
  258. </div>
  259. </button>
  260. <button
  261. 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"
  262. on:click={async () => {
  263. const _tools = await exportTools(localStorage.token).catch((error) => {
  264. toast.error(error);
  265. return null;
  266. });
  267. if (_tools) {
  268. let blob = new Blob([JSON.stringify(_tools)], {
  269. type: 'application/json'
  270. });
  271. saveAs(blob, `tools-export-${Date.now()}.json`);
  272. }
  273. }}
  274. >
  275. <div class=" self-center mr-2 font-medium">{$i18n.t('Export Tools')}</div>
  276. <div class=" self-center">
  277. <svg
  278. xmlns="http://www.w3.org/2000/svg"
  279. viewBox="0 0 16 16"
  280. fill="currentColor"
  281. class="w-4 h-4"
  282. >
  283. <path
  284. fill-rule="evenodd"
  285. 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"
  286. clip-rule="evenodd"
  287. />
  288. </svg>
  289. </div>
  290. </button>
  291. </div>
  292. </div>
  293. <ConfirmDialog
  294. bind:show={showConfirm}
  295. on:confirm={() => {
  296. const reader = new FileReader();
  297. reader.onload = async (event) => {
  298. const _tools = JSON.parse(event.target.result);
  299. console.log(_tools);
  300. for (const tool of _tools) {
  301. const res = await createNewTool(localStorage.token, tool).catch((error) => {
  302. toast.error(error);
  303. return null;
  304. });
  305. }
  306. toast.success('Tool imported successfully');
  307. tools.set(await getTools(localStorage.token));
  308. };
  309. reader.readAsText(importFiles[0]);
  310. }}
  311. >
  312. <div class="text-sm text-gray-500">
  313. <div class=" bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3">
  314. <div>Please carefully review the following warnings:</div>
  315. <ul class=" mt-1 list-disc pl-4 text-xs">
  316. <li>Tools have a function calling system that allows arbitrary code execution.</li>
  317. <li>Do not install tools from sources you do not fully trust.</li>
  318. </ul>
  319. </div>
  320. <div class="my-3">
  321. I acknowledge that I have read and I understand the implications of my action. I am aware of
  322. the risks associated with executing arbitrary code and I have verified the trustworthiness of
  323. the source.
  324. </div>
  325. </div>
  326. </ConfirmDialog>