Functions.svelte 15 KB

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