Functions.svelte 12 KB

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