Functions.svelte 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import fileSaver from 'file-saver';
  4. const { saveAs } = fileSaver;
  5. import { WEBUI_NAME, config, 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. toggleGlobalById
  17. } from '$lib/apis/functions';
  18. import ArrowDownTray from '../icons/ArrowDownTray.svelte';
  19. import Tooltip from '../common/Tooltip.svelte';
  20. import ConfirmDialog from '../common/ConfirmDialog.svelte';
  21. import { getModels } from '$lib/apis';
  22. import FunctionMenu from './Functions/FunctionMenu.svelte';
  23. import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte';
  24. import Switch from '../common/Switch.svelte';
  25. import ValvesModal from './common/ValvesModal.svelte';
  26. import ManifestModal from './common/ManifestModal.svelte';
  27. import Heart from '../icons/Heart.svelte';
  28. import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  29. import GarbageBin from '../icons/GarbageBin.svelte';
  30. const i18n = getContext('i18n');
  31. let shiftKey = false;
  32. let functionsImportInputElement: HTMLInputElement;
  33. let importFiles;
  34. let showConfirm = false;
  35. let query = '';
  36. let showManifestModal = false;
  37. let showValvesModal = false;
  38. let selectedFunction = null;
  39. let showDeleteConfirm = false;
  40. let filteredItems = [];
  41. $: filteredItems = $functions.filter(
  42. (f) =>
  43. query === '' ||
  44. f.name.toLowerCase().includes(query.toLowerCase()) ||
  45. f.id.toLowerCase().includes(query.toLowerCase())
  46. );
  47. const shareHandler = async (func) => {
  48. const item = await getFunctionById(localStorage.token, func.id).catch((error) => {
  49. toast.error(error);
  50. return null;
  51. });
  52. toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
  53. const url = 'https://openwebui.com';
  54. const tab = await window.open(`${url}/functions/create`, '_blank');
  55. // Define the event handler function
  56. const messageHandler = (event) => {
  57. if (event.origin !== url) return;
  58. if (event.data === 'loaded') {
  59. tab.postMessage(JSON.stringify(item), '*');
  60. // Remove the event listener after handling the message
  61. window.removeEventListener('message', messageHandler);
  62. }
  63. };
  64. window.addEventListener('message', messageHandler, false);
  65. console.log(item);
  66. };
  67. const cloneHandler = 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. sessionStorage.function = JSON.stringify({
  74. ..._function,
  75. id: `${_function.id}_clone`,
  76. name: `${_function.name} (Clone)`
  77. });
  78. goto('/workspace/functions/create');
  79. }
  80. };
  81. const exportHandler = async (func) => {
  82. const _function = await getFunctionById(localStorage.token, func.id).catch((error) => {
  83. toast.error(error);
  84. return null;
  85. });
  86. if (_function) {
  87. let blob = new Blob([JSON.stringify([_function])], {
  88. type: 'application/json'
  89. });
  90. saveAs(blob, `function-${_function.id}-export-${Date.now()}.json`);
  91. }
  92. };
  93. const deleteHandler = async (func) => {
  94. const res = await deleteFunctionById(localStorage.token, func.id).catch((error) => {
  95. toast.error(error);
  96. return null;
  97. });
  98. if (res) {
  99. toast.success($i18n.t('Function deleted successfully'));
  100. functions.set(await getFunctions(localStorage.token));
  101. models.set(await getModels(localStorage.token));
  102. }
  103. };
  104. const toggleGlobalHandler = async (func) => {
  105. const res = await toggleGlobalById(localStorage.token, func.id).catch((error) => {
  106. toast.error(error);
  107. });
  108. if (res) {
  109. if (func.is_global) {
  110. func.type === 'filter'
  111. ? toast.success($i18n.t('Filter is now globally enabled'))
  112. : toast.success($i18n.t('Function is now globally enabled'));
  113. } else {
  114. func.type === 'filter'
  115. ? toast.success($i18n.t('Filter is now globally disabled'))
  116. : toast.success($i18n.t('Function is now globally disabled'));
  117. }
  118. functions.set(await getFunctions(localStorage.token));
  119. models.set(await getModels(localStorage.token));
  120. }
  121. };
  122. onMount(() => {
  123. const onKeyDown = (event) => {
  124. if (event.key === 'Shift') {
  125. shiftKey = true;
  126. }
  127. };
  128. const onKeyUp = (event) => {
  129. if (event.key === 'Shift') {
  130. shiftKey = false;
  131. }
  132. };
  133. const onBlur = () => {
  134. shiftKey = false;
  135. };
  136. window.addEventListener('keydown', onKeyDown);
  137. window.addEventListener('keyup', onKeyUp);
  138. window.addEventListener('blur', onBlur);
  139. return () => {
  140. window.removeEventListener('keydown', onKeyDown);
  141. window.removeEventListener('keyup', onKeyUp);
  142. window.removeEventListener('blur', onBlur);
  143. };
  144. });
  145. </script>
  146. <svelte:head>
  147. <title>
  148. {$i18n.t('Functions')} | {$WEBUI_NAME}
  149. </title>
  150. </svelte:head>
  151. <div class=" flex w-full space-x-2 mb-2.5">
  152. <div class="flex flex-1">
  153. <div class=" self-center ml-1 mr-3">
  154. <svg
  155. xmlns="http://www.w3.org/2000/svg"
  156. viewBox="0 0 20 20"
  157. fill="currentColor"
  158. class="w-4 h-4"
  159. >
  160. <path
  161. fill-rule="evenodd"
  162. 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"
  163. clip-rule="evenodd"
  164. />
  165. </svg>
  166. </div>
  167. <input
  168. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-none bg-transparent"
  169. bind:value={query}
  170. placeholder={$i18n.t('Search Functions')}
  171. />
  172. </div>
  173. <div>
  174. <a
  175. 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"
  176. href="/workspace/functions/create"
  177. >
  178. <svg
  179. xmlns="http://www.w3.org/2000/svg"
  180. viewBox="0 0 16 16"
  181. fill="currentColor"
  182. class="w-4 h-4"
  183. >
  184. <path
  185. 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"
  186. />
  187. </svg>
  188. </a>
  189. </div>
  190. </div>
  191. <div class="mb-3.5">
  192. <div class="flex justify-between items-center">
  193. <div class="flex md:self-center text-base font-medium px-0.5">
  194. {$i18n.t('Functions')}
  195. <div class="flex self-center w-[1px] h-6 mx-2.5 bg-gray-50 dark:bg-gray-850" />
  196. <span class="text-base font-medium text-gray-500 dark:text-gray-300"
  197. >{filteredItems.length}</span
  198. >
  199. </div>
  200. </div>
  201. </div>
  202. <div class="my-3 mb-5">
  203. {#each filteredItems as func}
  204. <div
  205. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl"
  206. >
  207. <a
  208. class=" flex flex-1 space-x-3.5 cursor-pointer w-full"
  209. href={`/workspace/functions/edit?id=${encodeURIComponent(func.id)}`}
  210. >
  211. <div class="flex items-center text-left">
  212. <div class=" flex-1 self-center pl-1">
  213. <div class=" font-semibold flex items-center gap-1.5">
  214. <div
  215. class=" text-xs font-bold px-1 rounded uppercase line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
  216. >
  217. {func.type}
  218. </div>
  219. {#if func?.meta?.manifest?.version}
  220. <div
  221. class="text-xs font-bold px-1 rounded line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
  222. >
  223. v{func?.meta?.manifest?.version ?? ''}
  224. </div>
  225. {/if}
  226. <div class=" line-clamp-1">
  227. {func.name}
  228. </div>
  229. </div>
  230. <div class="flex gap-1.5 px-1">
  231. <div class=" text-gray-500 text-xs font-medium flex-shrink-0">{func.id}</div>
  232. <div class=" text-xs overflow-hidden text-ellipsis line-clamp-1">
  233. {func.meta.description}
  234. </div>
  235. </div>
  236. </div>
  237. </div>
  238. </a>
  239. <div class="flex flex-row gap-0.5 self-center">
  240. {#if shiftKey}
  241. <Tooltip content={$i18n.t('Delete')}>
  242. <button
  243. 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"
  244. type="button"
  245. on:click={() => {
  246. deleteHandler(func);
  247. }}
  248. >
  249. <GarbageBin />
  250. </button>
  251. </Tooltip>
  252. {:else}
  253. {#if func?.meta?.manifest?.funding_url ?? false}
  254. <Tooltip content={$i18n.t('Support')}>
  255. <button
  256. 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"
  257. type="button"
  258. on:click={() => {
  259. selectedFunction = func;
  260. showManifestModal = true;
  261. }}
  262. >
  263. <Heart />
  264. </button>
  265. </Tooltip>
  266. {/if}
  267. <Tooltip content={$i18n.t('Valves')}>
  268. <button
  269. 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"
  270. type="button"
  271. on:click={() => {
  272. selectedFunction = func;
  273. showValvesModal = true;
  274. }}
  275. >
  276. <svg
  277. xmlns="http://www.w3.org/2000/svg"
  278. fill="none"
  279. viewBox="0 0 24 24"
  280. stroke-width="1.5"
  281. stroke="currentColor"
  282. class="size-4"
  283. >
  284. <path
  285. stroke-linecap="round"
  286. stroke-linejoin="round"
  287. 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"
  288. />
  289. <path
  290. stroke-linecap="round"
  291. stroke-linejoin="round"
  292. d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  293. />
  294. </svg>
  295. </button>
  296. </Tooltip>
  297. <FunctionMenu
  298. {func}
  299. editHandler={() => {
  300. goto(`/workspace/functions/edit?id=${encodeURIComponent(func.id)}`);
  301. }}
  302. shareHandler={() => {
  303. shareHandler(func);
  304. }}
  305. cloneHandler={() => {
  306. cloneHandler(func);
  307. }}
  308. exportHandler={() => {
  309. exportHandler(func);
  310. }}
  311. deleteHandler={async () => {
  312. selectedFunction = func;
  313. showDeleteConfirm = true;
  314. }}
  315. toggleGlobalHandler={() => {
  316. if (['filter', 'action'].includes(func.type)) {
  317. toggleGlobalHandler(func);
  318. }
  319. }}
  320. onClose={() => {}}
  321. >
  322. <button
  323. 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"
  324. type="button"
  325. >
  326. <EllipsisHorizontal className="size-5" />
  327. </button>
  328. </FunctionMenu>
  329. {/if}
  330. <div class=" self-center mx-1">
  331. <Tooltip content={func.is_active ? $i18n.t('Enabled') : $i18n.t('Disabled')}>
  332. <Switch
  333. bind:state={func.is_active}
  334. on:change={async (e) => {
  335. toggleFunctionById(localStorage.token, func.id);
  336. models.set(await getModels(localStorage.token));
  337. }}
  338. />
  339. </Tooltip>
  340. </div>
  341. </div>
  342. </div>
  343. {/each}
  344. </div>
  345. <!-- <div class=" text-gray-500 text-xs mt-1 mb-2">
  346. ⓘ {$i18n.t(
  347. 'Admins have access to all tools at all times; users need tools assigned per model in the workspace.'
  348. )}
  349. </div> -->
  350. <div class=" flex justify-end w-full mb-2">
  351. <div class="flex space-x-2">
  352. <input
  353. id="documents-import-input"
  354. bind:this={functionsImportInputElement}
  355. bind:files={importFiles}
  356. type="file"
  357. accept=".json"
  358. hidden
  359. on:change={() => {
  360. console.log(importFiles);
  361. showConfirm = true;
  362. }}
  363. />
  364. <button
  365. 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"
  366. on:click={() => {
  367. functionsImportInputElement.click();
  368. }}
  369. >
  370. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Import Functions')}</div>
  371. <div class=" self-center">
  372. <svg
  373. xmlns="http://www.w3.org/2000/svg"
  374. viewBox="0 0 16 16"
  375. fill="currentColor"
  376. class="w-4 h-4"
  377. >
  378. <path
  379. fill-rule="evenodd"
  380. 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"
  381. clip-rule="evenodd"
  382. />
  383. </svg>
  384. </div>
  385. </button>
  386. <button
  387. 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"
  388. on:click={async () => {
  389. const _functions = await exportFunctions(localStorage.token).catch((error) => {
  390. toast.error(error);
  391. return null;
  392. });
  393. if (_functions) {
  394. let blob = new Blob([JSON.stringify(_functions)], {
  395. type: 'application/json'
  396. });
  397. saveAs(blob, `functions-export-${Date.now()}.json`);
  398. }
  399. }}
  400. >
  401. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Export Functions')}</div>
  402. <div class=" self-center">
  403. <svg
  404. xmlns="http://www.w3.org/2000/svg"
  405. viewBox="0 0 16 16"
  406. fill="currentColor"
  407. class="w-4 h-4"
  408. >
  409. <path
  410. fill-rule="evenodd"
  411. 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"
  412. clip-rule="evenodd"
  413. />
  414. </svg>
  415. </div>
  416. </button>
  417. </div>
  418. </div>
  419. {#if $config?.features.enable_community_sharing}
  420. <div class=" my-16">
  421. <div class=" text-lg font-semibold mb-3 line-clamp-1">
  422. {$i18n.t('Made by OpenWebUI Community')}
  423. </div>
  424. <a
  425. class=" flex space-x-4 cursor-pointer w-full mb-2 px-3 py-2"
  426. href="https://openwebui.com/#open-webui-community"
  427. target="_blank"
  428. >
  429. <div class=" self-center w-10 flex-shrink-0">
  430. <div
  431. class="w-full h-10 flex justify-center rounded-full bg-transparent dark:bg-gray-700 border border-dashed border-gray-200"
  432. >
  433. <svg
  434. xmlns="http://www.w3.org/2000/svg"
  435. viewBox="0 0 24 24"
  436. fill="currentColor"
  437. class="w-6"
  438. >
  439. <path
  440. fill-rule="evenodd"
  441. 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"
  442. clip-rule="evenodd"
  443. />
  444. </svg>
  445. </div>
  446. </div>
  447. <div class=" self-center">
  448. <div class=" font-semibold line-clamp-1">{$i18n.t('Discover a function')}</div>
  449. <div class=" text-sm line-clamp-1">
  450. {$i18n.t('Discover, download, and explore custom functions')}
  451. </div>
  452. </div>
  453. </a>
  454. </div>
  455. {/if}
  456. <DeleteConfirmDialog
  457. bind:show={showDeleteConfirm}
  458. title={$i18n.t('Delete function?')}
  459. on:confirm={() => {
  460. deleteHandler(selectedFunction);
  461. }}
  462. >
  463. <div class=" text-sm text-gray-500">
  464. {$i18n.t('This will delete')} <span class=" font-semibold">{selectedFunction.name}</span>.
  465. </div>
  466. </DeleteConfirmDialog>
  467. <ManifestModal bind:show={showManifestModal} manifest={selectedFunction?.meta?.manifest ?? {}} />
  468. <ValvesModal
  469. bind:show={showValvesModal}
  470. type="function"
  471. id={selectedFunction?.id ?? null}
  472. on:save={async () => {
  473. await tick();
  474. models.set(await getModels(localStorage.token));
  475. }}
  476. />
  477. <ConfirmDialog
  478. bind:show={showConfirm}
  479. on:confirm={() => {
  480. const reader = new FileReader();
  481. reader.onload = async (event) => {
  482. const _functions = JSON.parse(event.target.result);
  483. console.log(_functions);
  484. for (const func of _functions) {
  485. const res = await createNewFunction(localStorage.token, func).catch((error) => {
  486. toast.error(error);
  487. return null;
  488. });
  489. }
  490. toast.success($i18n.t('Functions imported successfully'));
  491. functions.set(await getFunctions(localStorage.token));
  492. models.set(await getModels(localStorage.token));
  493. };
  494. reader.readAsText(importFiles[0]);
  495. }}
  496. >
  497. <div class="text-sm text-gray-500">
  498. <div class=" bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3">
  499. <div>Please carefully review the following warnings:</div>
  500. <ul class=" mt-1 list-disc pl-4 text-xs">
  501. <li>{$i18n.t('Functions allow arbitrary code execution.')}</li>
  502. <li>{$i18n.t('Do not install functions from sources you do not fully trust.')}</li>
  503. </ul>
  504. </div>
  505. <div class="my-3">
  506. {$i18n.t(
  507. 'I acknowledge that I have read and I understand the implications of my action. I am aware of the risks associated with executing arbitrary code and I have verified the trustworthiness of the source.'
  508. )}
  509. </div>
  510. </div>
  511. </ConfirmDialog>