open-webui/src/routes/(app)/admin/functions/edit/+page.svelte

94 lines
2.3 KiB
Svelte
Raw Normal View History

2024-06-18 17:23:04 +00:00
<script>
import { toast } from 'svelte-sonner';
2024-06-24 19:21:19 +00:00
import { onMount, getContext } from 'svelte';
2024-06-18 17:23:04 +00:00
import { goto } from '$app/navigation';
import { page } from '$app/stores';
2025-02-12 09:22:53 +00:00
import { config, functions, models, settings } from '$lib/stores';
import { updateFunctionById, getFunctions, getFunctionById } from '$lib/apis/functions';
import FunctionEditor from '$lib/components/admin/Functions/FunctionEditor.svelte';
2024-06-18 17:23:04 +00:00
import Spinner from '$lib/components/common/Spinner.svelte';
2024-06-20 11:51:51 +00:00
import { getModels } from '$lib/apis';
import { compareVersion, extractFrontmatter } from '$lib/utils';
import { WEBUI_VERSION } from '$lib/constants';
2024-06-18 17:23:04 +00:00
2024-06-24 19:21:19 +00:00
const i18n = getContext('i18n');
let func = null;
2024-06-18 17:23:04 +00:00
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');
2024-07-01 23:52:46 +00:00
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, {
2024-06-18 17:23:04 +00:00
id: data.id,
name: data.name,
meta: data.meta,
content: data.content
}).catch((error) => {
2025-01-21 06:41:32 +00:00
toast.error(`${error}`);
2024-06-18 17:23:04 +00:00
return null;
});
if (res) {
2024-06-24 17:38:26 +00:00
toast.success($i18n.t('Function updated successfully'));
2024-06-20 08:44:52 +00:00
functions.set(await getFunctions(localStorage.token));
2025-02-12 09:22:53 +00:00
models.set(
await getModels(
localStorage.token,
2025-02-12 09:32:49 +00:00
$config?.features?.enable_direct_connections && ($settings?.directConnections ?? null)
2025-02-12 09:22:53 +00:00
)
);
2024-06-18 17:23:04 +00:00
}
};
onMount(async () => {
console.log('mounted');
const id = $page.url.searchParams.get('id');
if (id) {
func = await getFunctionById(localStorage.token, id).catch((error) => {
2025-01-21 06:41:32 +00:00
toast.error(`${error}`);
2024-11-17 22:00:37 +00:00
goto('/admin/functions');
2024-06-18 17:23:04 +00:00
return null;
});
console.log(func);
2024-06-18 17:23:04 +00:00
}
});
</script>
{#if func}
<FunctionEditor
2024-06-18 17:23:04 +00:00
edit={true}
id={func.id}
name={func.name}
meta={func.meta}
content={func.content}
2025-02-25 07:21:03 +00:00
onSave={(value) => {
saveHandler(value);
2024-06-18 17:23:04 +00:00
}}
/>
{:else}
<div class="flex items-center justify-center h-full">
<div class=" pb-16">
2025-06-27 12:15:16 +00:00
<Spinner className="size-5" />
2024-06-18 17:23:04 +00:00
</div>
</div>
{/if}