open-webui/src/routes/(app)/workspace/functions/create/+page.svelte

58 lines
1.2 KiB
Svelte
Raw Normal View History

2024-06-18 17:23:04 +00:00
<script>
import { toast } from 'svelte-sonner';
2024-06-20 07:54:58 +00:00
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
2024-06-20 08:44:52 +00:00
import { functions } from '$lib/stores';
2024-06-20 07:54:58 +00:00
import { createNewFunction, getFunctions } from '$lib/apis/functions';
import FunctionEditor from '$lib/components/workspace/Functions/FunctionEditor.svelte';
2024-06-18 17:23:04 +00:00
let mounted = false;
let clone = false;
2024-06-20 07:54:58 +00:00
let func = null;
2024-06-18 17:23:04 +00:00
const saveHandler = async (data) => {
console.log(data);
2024-06-20 07:54:58 +00:00
const res = await createNewFunction(localStorage.token, {
2024-06-18 17:23:04 +00:00
id: data.id,
name: data.name,
meta: data.meta,
content: data.content
}).catch((error) => {
toast.error(error);
return null;
});
if (res) {
2024-06-20 07:54:58 +00:00
toast.success('Function created successfully');
2024-06-20 08:44:52 +00:00
functions.set(await getFunctions(localStorage.token));
2024-06-20 07:54:58 +00:00
await goto('/workspace/functions');
2024-06-18 17:23:04 +00:00
}
};
onMount(() => {
2024-06-20 07:54:58 +00:00
if (sessionStorage.function) {
func = JSON.parse(sessionStorage.function);
sessionStorage.removeItem('function');
2024-06-18 17:23:04 +00:00
2024-06-20 07:54:58 +00:00
console.log(func);
2024-06-18 17:23:04 +00:00
clone = true;
}
mounted = true;
});
</script>
{#if mounted}
2024-06-20 07:54:58 +00:00
<FunctionEditor
id={func?.id ?? ''}
name={func?.name ?? ''}
meta={func?.meta ?? { description: '' }}
content={func?.content ?? ''}
2024-06-18 17:23:04 +00:00
{clone}
on:save={(e) => {
saveHandler(e.detail);
}}
/>
{/if}