2024-11-17 04:53:05 +00:00
|
|
|
<script lang="ts">
|
2024-03-01 09:18:07 +00:00
|
|
|
import { toast } from 'svelte-sonner';
|
2024-01-03 05:35:47 +00:00
|
|
|
import { goto } from '$app/navigation';
|
|
|
|
|
import { prompts } from '$lib/stores';
|
2024-03-02 20:38:51 +00:00
|
|
|
import { onMount, tick, getContext } from 'svelte';
|
2024-01-03 05:35:47 +00:00
|
|
|
|
2024-11-17 04:53:05 +00:00
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
|
2024-01-03 05:35:47 +00:00
|
|
|
import { createNewPrompt, getPrompts } from '$lib/apis/prompts';
|
2024-11-11 02:20:46 +00:00
|
|
|
import PromptEditor from '$lib/components/workspace/Prompts/PromptEditor.svelte';
|
2024-01-03 05:35:47 +00:00
|
|
|
|
2024-11-11 02:20:46 +00:00
|
|
|
let prompt = null;
|
2024-11-17 01:49:13 +00:00
|
|
|
const onSubmit = async (_prompt) => {
|
|
|
|
|
const prompt = await createNewPrompt(localStorage.token, _prompt).catch((error) => {
|
|
|
|
|
toast.error(error);
|
|
|
|
|
return null;
|
|
|
|
|
});
|
2024-01-03 04:41:37 +00:00
|
|
|
|
2024-11-11 02:20:46 +00:00
|
|
|
if (prompt) {
|
2024-11-17 04:53:05 +00:00
|
|
|
toast.success($i18n.t('Prompt created successfully'));
|
|
|
|
|
|
2024-11-11 02:20:46 +00:00
|
|
|
await prompts.set(await getPrompts(localStorage.token));
|
|
|
|
|
await goto('/workspace/prompts');
|
|
|
|
|
}
|
2024-01-03 04:41:37 +00:00
|
|
|
};
|
|
|
|
|
|
2024-02-18 23:53:54 +00:00
|
|
|
onMount(async () => {
|
2024-01-03 04:41:37 +00:00
|
|
|
window.addEventListener('message', async (event) => {
|
|
|
|
|
if (
|
2024-06-02 04:16:32 +00:00
|
|
|
!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:5173'].includes(
|
|
|
|
|
event.origin
|
|
|
|
|
)
|
2024-01-03 04:41:37 +00:00
|
|
|
)
|
|
|
|
|
return;
|
2024-11-11 02:20:46 +00:00
|
|
|
const _prompt = JSON.parse(event.data);
|
|
|
|
|
console.log(_prompt);
|
|
|
|
|
|
|
|
|
|
prompt = {
|
|
|
|
|
title: _prompt.title,
|
|
|
|
|
command: _prompt.command,
|
2024-11-17 01:49:13 +00:00
|
|
|
content: _prompt.content,
|
|
|
|
|
access_control: null
|
2024-11-11 02:20:46 +00:00
|
|
|
};
|
2024-01-03 04:41:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (window.opener ?? false) {
|
|
|
|
|
window.opener.postMessage('loaded', '*');
|
|
|
|
|
}
|
2024-02-18 23:53:54 +00:00
|
|
|
|
|
|
|
|
if (sessionStorage.prompt) {
|
2024-11-11 02:20:46 +00:00
|
|
|
const _prompt = JSON.parse(sessionStorage.prompt);
|
2024-02-18 23:53:54 +00:00
|
|
|
|
2024-11-11 02:20:46 +00:00
|
|
|
prompt = {
|
|
|
|
|
title: _prompt.title,
|
|
|
|
|
command: _prompt.command,
|
2024-11-17 01:49:13 +00:00
|
|
|
content: _prompt.content,
|
|
|
|
|
access_control: null
|
2024-11-11 02:20:46 +00:00
|
|
|
};
|
2024-02-18 23:53:54 +00:00
|
|
|
sessionStorage.removeItem('prompt');
|
|
|
|
|
}
|
2024-01-03 04:41:37 +00:00
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
2024-11-11 02:42:52 +00:00
|
|
|
{#key prompt}
|
|
|
|
|
<PromptEditor {prompt} {onSubmit} />
|
|
|
|
|
{/key}
|