Functions.svelte 12 KB

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