+page.svelte 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <script>
  2. import { v4 as uuidv4 } from 'uuid';
  3. import { toast } from 'svelte-sonner';
  4. import { goto } from '$app/navigation';
  5. import { models } from '$lib/stores';
  6. import { onMount, tick, getContext } from 'svelte';
  7. import { createNewModel, getModelById } from '$lib/apis/models';
  8. import { getModels } from '$lib/apis';
  9. import ModelEditor from '$lib/components/workspace/Models/ModelEditor.svelte';
  10. const i18n = getContext('i18n');
  11. const onSubmit = async (modelInfo) => {
  12. if ($models.find((m) => m.id === modelInfo.id)) {
  13. toast.error(
  14. `Error: A model with the ID '${modelInfo.id}' already exists. Please select a different ID to proceed.`
  15. );
  16. return;
  17. }
  18. if (modelInfo) {
  19. const res = await createNewModel(localStorage.token, {
  20. ...modelInfo,
  21. meta: {
  22. ...modelInfo.meta,
  23. profile_image_url: modelInfo.meta.profile_image_url ?? '/static/favicon.png',
  24. suggestion_prompts: modelInfo.meta.suggestion_prompts
  25. ? modelInfo.meta.suggestion_prompts.filter((prompt) => prompt.content !== '')
  26. : null
  27. },
  28. params: { ...modelInfo.params }
  29. });
  30. if (res) {
  31. await models.set(await getModels(localStorage.token));
  32. toast.success($i18n.t('Model created successfully!'));
  33. await goto('/workspace/models');
  34. }
  35. }
  36. };
  37. let model = null;
  38. onMount(async () => {
  39. window.addEventListener('message', async (event) => {
  40. if (
  41. !['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:5173'].includes(
  42. event.origin
  43. )
  44. )
  45. return;
  46. model = JSON.parse(event.data);
  47. });
  48. if (window.opener ?? false) {
  49. window.opener.postMessage('loaded', '*');
  50. }
  51. if (sessionStorage.model) {
  52. model = JSON.parse(sessionStorage.model);
  53. sessionStorage.removeItem('model');
  54. }
  55. });
  56. </script>
  57. {#key model}
  58. <ModelEditor {model} {onSubmit} />
  59. {/key}