refac: move functions (plugin) to admin panel
This commit is contained in:
@@ -65,6 +65,13 @@
|
||||
href="/admin/evaluations">{$i18n.t('Evaluations')}</a
|
||||
>
|
||||
|
||||
<a
|
||||
class="min-w-fit rounded-full p-1.5 {$page.url.pathname.includes('/admin/functions')
|
||||
? ''
|
||||
: 'text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'} transition"
|
||||
href="/admin/functions">{$i18n.t('Functions')}</a
|
||||
>
|
||||
|
||||
<a
|
||||
class="min-w-fit rounded-full p-1.5 {$page.url.pathname.includes('/admin/settings')
|
||||
? ''
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { functions } from '$lib/stores';
|
||||
|
||||
import { getFunctions } from '$lib/apis/functions';
|
||||
import Functions from '$lib/components/admin/Functions.svelte';
|
||||
|
||||
onMount(async () => {
|
||||
await Promise.all([
|
||||
(async () => {
|
||||
functions.set(await getFunctions(localStorage.token));
|
||||
})()
|
||||
]);
|
||||
});
|
||||
</script>
|
||||
|
||||
{#if $functions !== null}
|
||||
<Functions />
|
||||
{/if}
|
||||
@@ -0,0 +1,98 @@
|
||||
<script>
|
||||
import { toast } from 'svelte-sonner';
|
||||
import { onMount, getContext } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
|
||||
import { functions, models } 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));
|
||||
|
||||
await goto('/workspace/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}
|
||||
on:save={(e) => {
|
||||
saveHandler(e.detail);
|
||||
}}
|
||||
/>
|
||||
{/key}
|
||||
{/if}
|
||||
@@ -0,0 +1,88 @@
|
||||
<script>
|
||||
import { toast } from 'svelte-sonner';
|
||||
import { onMount, getContext } from 'svelte';
|
||||
|
||||
import { goto } from '$app/navigation';
|
||||
import { page } from '$app/stores';
|
||||
import { functions, models } from '$lib/stores';
|
||||
import { updateFunctionById, getFunctions, getFunctionById } from '$lib/apis/functions';
|
||||
|
||||
import FunctionEditor from '$lib/components/admin/Functions/FunctionEditor.svelte';
|
||||
import Spinner from '$lib/components/common/Spinner.svelte';
|
||||
import { getModels } from '$lib/apis';
|
||||
import { compareVersion, extractFrontmatter } from '$lib/utils';
|
||||
import { WEBUI_VERSION } from '$lib/constants';
|
||||
|
||||
const i18n = getContext('i18n');
|
||||
|
||||
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 updateFunctionById(localStorage.token, func.id, {
|
||||
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 updated successfully'));
|
||||
functions.set(await getFunctions(localStorage.token));
|
||||
models.set(await getModels(localStorage.token));
|
||||
}
|
||||
};
|
||||
|
||||
onMount(async () => {
|
||||
console.log('mounted');
|
||||
const id = $page.url.searchParams.get('id');
|
||||
|
||||
if (id) {
|
||||
func = await getFunctionById(localStorage.token, id).catch((error) => {
|
||||
toast.error(error);
|
||||
goto('/workspace/functions');
|
||||
return null;
|
||||
});
|
||||
|
||||
console.log(func);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
{#if func}
|
||||
<FunctionEditor
|
||||
edit={true}
|
||||
id={func.id}
|
||||
name={func.name}
|
||||
meta={func.meta}
|
||||
content={func.content}
|
||||
on:save={(e) => {
|
||||
saveHandler(e.detail);
|
||||
}}
|
||||
/>
|
||||
{:else}
|
||||
<div class="flex items-center justify-center h-full">
|
||||
<div class=" pb-16">
|
||||
<Spinner />
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
Reference in New Issue
Block a user