Functions.svelte 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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, tick } 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. toggleFunctionById
  16. } from '$lib/apis/functions';
  17. import ArrowDownTray from '../icons/ArrowDownTray.svelte';
  18. import Tooltip from '../common/Tooltip.svelte';
  19. import ConfirmDialog from '../common/ConfirmDialog.svelte';
  20. import { getModels } from '$lib/apis';
  21. import FunctionMenu from './Functions/FunctionMenu.svelte';
  22. import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte';
  23. import Switch from '../common/Switch.svelte';
  24. import ValvesModal from './common/ValvesModal.svelte';
  25. import ManifestModal from './common/ManifestModal.svelte';
  26. import Heart from '../icons/Heart.svelte';
  27. import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  28. const i18n = getContext('i18n');
  29. let functionsImportInputElement: HTMLInputElement;
  30. let importFiles;
  31. let showConfirm = false;
  32. let query = '';
  33. let showManifestModal = false;
  34. let showValvesModal = false;
  35. let selectedFunction = null;
  36. let showDeleteConfirm = false;
  37. const shareHandler = async (item) => {
  38. toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
  39. const url = 'https://openwebui.com';
  40. const tab = await window.open(`${url}/tools/create`, '_blank');
  41. // Define the event handler function
  42. const messageHandler = (event) => {
  43. if (event.origin !== url) return;
  44. if (event.data === 'loaded') {
  45. tab.postMessage(JSON.stringify(item), '*');
  46. // Remove the event listener after handling the message
  47. window.removeEventListener('message', messageHandler);
  48. }
  49. };
  50. window.addEventListener('message', messageHandler, false);
  51. console.log(item);
  52. };
  53. const cloneHandler = async (func) => {
  54. const _function = await getFunctionById(localStorage.token, func.id).catch((error) => {
  55. toast.error(error);
  56. return null;
  57. });
  58. if (_function) {
  59. sessionStorage.function = JSON.stringify({
  60. ..._function,
  61. id: `${_function.id}_clone`,
  62. name: `${_function.name} (Clone)`
  63. });
  64. goto('/workspace/functions/create');
  65. }
  66. };
  67. const exportHandler = async (func) => {
  68. const _function = await getFunctionById(localStorage.token, func.id).catch((error) => {
  69. toast.error(error);
  70. return null;
  71. });
  72. if (_function) {
  73. let blob = new Blob([JSON.stringify([_function])], {
  74. type: 'application/json'
  75. });
  76. saveAs(blob, `function-${_function.id}-export-${Date.now()}.json`);
  77. }
  78. };
  79. const deleteHandler = async (func) => {
  80. const res = await deleteFunctionById(localStorage.token, func.id).catch((error) => {
  81. toast.error(error);
  82. return null;
  83. });
  84. if (res) {
  85. toast.success($i18n.t('Function deleted successfully'));
  86. functions.set(await getFunctions(localStorage.token));
  87. models.set(await getModels(localStorage.token));
  88. }
  89. };
  90. </script>
  91. <svelte:head>
  92. <title>
  93. {$i18n.t('Functions')} | {$WEBUI_NAME}
  94. </title>
  95. </svelte:head>
  96. <div class="mb-3 flex justify-between items-center">
  97. <div class=" text-lg font-semibold self-center">{$i18n.t('Functions')}</div>
  98. </div>
  99. <div class=" flex w-full space-x-2">
  100. <div class="flex flex-1">
  101. <div class=" self-center ml-1 mr-3">
  102. <svg
  103. xmlns="http://www.w3.org/2000/svg"
  104. viewBox="0 0 20 20"
  105. fill="currentColor"
  106. class="w-4 h-4"
  107. >
  108. <path
  109. fill-rule="evenodd"
  110. 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"
  111. clip-rule="evenodd"
  112. />
  113. </svg>
  114. </div>
  115. <input
  116. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-none bg-transparent"
  117. bind:value={query}
  118. placeholder={$i18n.t('Search Functions')}
  119. />
  120. </div>
  121. <div>
  122. <a
  123. 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"
  124. href="/workspace/functions/create"
  125. >
  126. <svg
  127. xmlns="http://www.w3.org/2000/svg"
  128. viewBox="0 0 16 16"
  129. fill="currentColor"
  130. class="w-4 h-4"
  131. >
  132. <path
  133. 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"
  134. />
  135. </svg>
  136. </a>
  137. </div>
  138. </div>
  139. <hr class=" dark:border-gray-850 my-2.5" />
  140. <div class="my-3 mb-5">
  141. {#each $functions.filter((f) => query === '' || f.name
  142. .toLowerCase()
  143. .includes(query.toLowerCase()) || f.id.toLowerCase().includes(query.toLowerCase())) as func}
  144. <div
  145. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl"
  146. >
  147. <a
  148. class=" flex flex-1 space-x-3.5 cursor-pointer w-full"
  149. href={`/workspace/functions/edit?id=${encodeURIComponent(func.id)}`}
  150. >
  151. <div class="flex items-center text-left">
  152. <div class=" flex-1 self-center pl-1">
  153. <div class=" font-semibold flex items-center gap-1.5">
  154. <div
  155. class=" text-xs font-black px-1 rounded uppercase line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
  156. >
  157. {func.type}
  158. </div>
  159. {#if func?.meta?.manifest?.version}
  160. <div
  161. class="text-xs font-black px-1 rounded line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
  162. >
  163. v{func?.meta?.manifest?.version ?? ''}
  164. </div>
  165. {/if}
  166. <div class=" line-clamp-1">
  167. {func.name}
  168. </div>
  169. </div>
  170. <div class="flex gap-1.5 px-1">
  171. <div class=" text-gray-500 text-xs font-medium flex-shrink-0">{func.id}</div>
  172. <div class=" text-xs overflow-hidden text-ellipsis line-clamp-1">
  173. {func.meta.description}
  174. </div>
  175. </div>
  176. </div>
  177. </div>
  178. </a>
  179. <div class="flex flex-row gap-0.5 self-center">
  180. {#if func?.meta?.manifest?.funding_url ?? false}
  181. <Tooltip content="Support">
  182. <button
  183. 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"
  184. type="button"
  185. on:click={() => {
  186. selectedFunction = func;
  187. showManifestModal = true;
  188. }}
  189. >
  190. <Heart />
  191. </button>
  192. </Tooltip>
  193. {/if}
  194. <Tooltip content="Valves">
  195. <button
  196. 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"
  197. type="button"
  198. on:click={() => {
  199. selectedFunction = func;
  200. showValvesModal = true;
  201. }}
  202. >
  203. <svg
  204. xmlns="http://www.w3.org/2000/svg"
  205. fill="none"
  206. viewBox="0 0 24 24"
  207. stroke-width="1.5"
  208. stroke="currentColor"
  209. class="size-4"
  210. >
  211. <path
  212. stroke-linecap="round"
  213. stroke-linejoin="round"
  214. d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 0 1 1.37.49l1.296 2.247a1.125 1.125 0 0 1-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a7.723 7.723 0 0 1 0 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 0 1-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.47 6.47 0 0 1-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 0 1-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 0 1-1.369-.49l-1.297-2.247a1.125 1.125 0 0 1 .26-1.431l1.004-.827c.292-.24.437-.613.43-.991a6.932 6.932 0 0 1 0-.255c.007-.38-.138-.751-.43-.992l-1.004-.827a1.125 1.125 0 0 1-.26-1.43l1.297-2.247a1.125 1.125 0 0 1 1.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.086.22-.128.332-.183.582-.495.644-.869l.214-1.28Z"
  215. />
  216. <path
  217. stroke-linecap="round"
  218. stroke-linejoin="round"
  219. d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  220. />
  221. </svg>
  222. </button>
  223. </Tooltip>
  224. <FunctionMenu
  225. editHandler={() => {
  226. goto(`/workspace/functions/edit?id=${encodeURIComponent(func.id)}`);
  227. }}
  228. shareHandler={() => {
  229. shareHandler(func);
  230. }}
  231. cloneHandler={() => {
  232. cloneHandler(func);
  233. }}
  234. exportHandler={() => {
  235. exportHandler(func);
  236. }}
  237. deleteHandler={async () => {
  238. selectedFunction = func;
  239. showDeleteConfirm = true;
  240. }}
  241. onClose={() => {}}
  242. >
  243. <button
  244. 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"
  245. type="button"
  246. >
  247. <EllipsisHorizontal className="size-5" />
  248. </button>
  249. </FunctionMenu>
  250. <div class=" self-center mx-1">
  251. <Switch
  252. bind:state={func.is_active}
  253. on:change={async (e) => {
  254. toggleFunctionById(localStorage.token, func.id);
  255. models.set(await getModels(localStorage.token));
  256. }}
  257. />
  258. </div>
  259. </div>
  260. </div>
  261. {/each}
  262. </div>
  263. <!-- <div class=" text-gray-500 text-xs mt-1 mb-2">
  264. ⓘ {$i18n.t(
  265. 'Admins have access to all tools at all times; users need tools assigned per model in the workspace.'
  266. )}
  267. </div> -->
  268. <div class=" flex justify-end w-full mb-2">
  269. <div class="flex space-x-2">
  270. <input
  271. id="documents-import-input"
  272. bind:this={functionsImportInputElement}
  273. bind:files={importFiles}
  274. type="file"
  275. accept=".json"
  276. hidden
  277. on:change={() => {
  278. console.log(importFiles);
  279. showConfirm = true;
  280. }}
  281. />
  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={() => {
  285. functionsImportInputElement.click();
  286. }}
  287. >
  288. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Import 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 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"
  299. clip-rule="evenodd"
  300. />
  301. </svg>
  302. </div>
  303. </button>
  304. <button
  305. 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"
  306. on:click={async () => {
  307. const _functions = await exportFunctions(localStorage.token).catch((error) => {
  308. toast.error(error);
  309. return null;
  310. });
  311. if (_functions) {
  312. let blob = new Blob([JSON.stringify(_functions)], {
  313. type: 'application/json'
  314. });
  315. saveAs(blob, `functions-export-${Date.now()}.json`);
  316. }
  317. }}
  318. >
  319. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Export Functions')}</div>
  320. <div class=" self-center">
  321. <svg
  322. xmlns="http://www.w3.org/2000/svg"
  323. viewBox="0 0 16 16"
  324. fill="currentColor"
  325. class="w-4 h-4"
  326. >
  327. <path
  328. fill-rule="evenodd"
  329. 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"
  330. clip-rule="evenodd"
  331. />
  332. </svg>
  333. </div>
  334. </button>
  335. </div>
  336. </div>
  337. <div class=" my-16">
  338. <div class=" text-lg font-semibold mb-3 line-clamp-1">
  339. {$i18n.t('Made by OpenWebUI Community')}
  340. </div>
  341. <a
  342. class=" flex space-x-4 cursor-pointer w-full mb-2 px-3 py-2"
  343. href="https://openwebui.com/#open-webui-community"
  344. target="_blank"
  345. >
  346. <div class=" self-center w-10 flex-shrink-0">
  347. <div
  348. class="w-full h-10 flex justify-center rounded-full bg-transparent dark:bg-gray-700 border border-dashed border-gray-200"
  349. >
  350. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6">
  351. <path
  352. fill-rule="evenodd"
  353. 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"
  354. clip-rule="evenodd"
  355. />
  356. </svg>
  357. </div>
  358. </div>
  359. <div class=" self-center">
  360. <div class=" font-bold line-clamp-1">{$i18n.t('Discover a function')}</div>
  361. <div class=" text-sm line-clamp-1">
  362. {$i18n.t('Discover, download, and explore custom functions')}
  363. </div>
  364. </div>
  365. </a>
  366. </div>
  367. <DeleteConfirmDialog
  368. bind:show={showDeleteConfirm}
  369. title={$i18n.t('Delete function?')}
  370. on:confirm={() => {
  371. deleteHandler(selectedFunction);
  372. }}
  373. >
  374. <div class=" text-sm text-gray-500">
  375. {$i18n.t('This will delete')} <span class=" font-semibold">{selectedFunction.name}</span>.
  376. </div>
  377. </DeleteConfirmDialog>
  378. <ManifestModal bind:show={showManifestModal} manifest={selectedFunction?.meta?.manifest ?? {}} />
  379. <ValvesModal
  380. bind:show={showValvesModal}
  381. type="function"
  382. id={selectedFunction?.id ?? null}
  383. on:save={async () => {
  384. await tick();
  385. models.set(await getModels(localStorage.token));
  386. }}
  387. />
  388. <ConfirmDialog
  389. bind:show={showConfirm}
  390. on:confirm={() => {
  391. const reader = new FileReader();
  392. reader.onload = async (event) => {
  393. const _functions = JSON.parse(event.target.result);
  394. console.log(_functions);
  395. for (const func of _functions) {
  396. const res = await createNewFunction(localStorage.token, func).catch((error) => {
  397. toast.error(error);
  398. return null;
  399. });
  400. }
  401. toast.success($i18n.t('Functions imported successfully'));
  402. functions.set(await getFunctions(localStorage.token));
  403. models.set(await getModels(localStorage.token));
  404. };
  405. reader.readAsText(importFiles[0]);
  406. }}
  407. >
  408. <div class="text-sm text-gray-500">
  409. <div class=" bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3">
  410. <div>Please carefully review the following warnings:</div>
  411. <ul class=" mt-1 list-disc pl-4 text-xs">
  412. <li>Functions allow arbitrary code execution.</li>
  413. <li>Do not install functions from sources you do not fully trust.</li>
  414. </ul>
  415. </div>
  416. <div class="my-3">
  417. I acknowledge that I have read and I understand the implications of my action. I am aware of
  418. the risks associated with executing arbitrary code and I have verified the trustworthiness of
  419. the source.
  420. </div>
  421. </div>
  422. </ConfirmDialog>