File size: 2,650 Bytes
bc27e65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<script>

	import { toast } from 'svelte-sonner';

	import { onMount, getContext } from 'svelte';

	import { goto } from '$app/navigation';



	import { config, functions, models, settings } from '$lib/stores';

	import { createNewFunction, getFunctions } from '$lib/apis/functions';

	import FunctionEditor from '$lib/components/admin/Functions/FunctionEditor.svelte';

	import { getModels } from '$lib/apis';

	import { compareVersion, extractFrontmatter } from '$lib/utils';

	import { WEBUI_VERSION } from '$lib/constants';



	const i18n = getContext('i18n');



	let mounted = false;

	let clone = false;

	let func = null;



	const saveHandler = async (data) => {

		console.log(data);



		const manifest = extractFrontmatter(data.content);

		if (compareVersion(manifest?.required_open_webui_version ?? '0.0.0', WEBUI_VERSION)) {

			console.log('Version is lower than required');

			toast.error(

				$i18n.t(

					'Open WebUI version (v{{OPEN_WEBUI_VERSION}}) is lower than required version (v{{REQUIRED_VERSION}})',

					{

						OPEN_WEBUI_VERSION: WEBUI_VERSION,

						REQUIRED_VERSION: manifest?.required_open_webui_version ?? '0.0.0'

					}

				)

			);

			return;

		}



		const res = await createNewFunction(localStorage.token, {

			id: data.id,

			name: data.name,

			meta: data.meta,

			content: data.content

		}).catch((error) => {

			toast.error(`${error}`);

			return null;

		});



		if (res) {

			toast.success($i18n.t('Function created successfully'));

			functions.set(await getFunctions(localStorage.token));

			models.set(

				await getModels(

					localStorage.token,

					$config?.features?.enable_direct_connections && ($settings?.directConnections ?? null)

				)

			);



			await goto('/admin/functions');

		}

	};



	onMount(() => {

		window.addEventListener('message', async (event) => {

			if (

				!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes(

					event.origin

				)

			)

				return;



			func = JSON.parse(event.data);

			console.log(func);

		});



		if (window.opener ?? false) {

			window.opener.postMessage('loaded', '*');

		}



		if (sessionStorage.function) {

			func = JSON.parse(sessionStorage.function);

			sessionStorage.removeItem('function');



			console.log(func);

			clone = true;

		}



		mounted = true;

	});

</script>

{#if mounted}
	{#key func?.content}
		<FunctionEditor

			id={func?.id ?? ''}

			name={func?.name ?? ''}

			meta={func?.meta ?? { description: '' }}

			content={func?.content ?? ''}

			{clone}

			onSave={(value) => {
				saveHandler(value);
			}}
		/>
	{/key}
{/if}