Files
open-webui/src/lib/components/chat/Settings/Personalization/MemoryModal.svelte
T
2026-07-15 17:50:26 -04:00

154 lines
4.7 KiB
Svelte

<script>
import { createEventDispatcher, getContext } from 'svelte';
import { toast } from 'svelte-sonner';
import { addNewMemory, updateMemoryById } from '$lib/apis/memories';
import Modal from '$lib/components/common/Modal.svelte';
import Spinner from '$lib/components/common/Spinner.svelte';
import XMark from '$lib/components/icons/XMark.svelte';
import { settings } from '$lib/stores';
const dispatch = createEventDispatcher();
export let show;
export let memory = null;
const i18n = getContext('i18n');
let loading = false;
let content = '';
let type = 'user';
let path = '';
const inputClass =
'h-7 w-full rounded-lg border border-gray-100/50 bg-gray-50/40 px-2 text-xs text-gray-700 outline-hidden transition-colors placeholder:text-gray-300 focus:border-blue-400 dark:border-white/[0.04] dark:bg-white/[0.03] dark:text-gray-300 dark:placeholder:text-gray-700 dark:focus:border-blue-500';
const textareaClass =
'w-full resize-y rounded-lg border border-gray-100/50 bg-gray-50/40 px-2 py-1.5 text-xs text-gray-700 outline-hidden transition-colors placeholder:text-gray-300 focus:border-blue-400 dark:border-white/[0.04] dark:bg-white/[0.03] dark:text-gray-300 dark:placeholder:text-gray-700 dark:focus:border-blue-500';
const actionButtonClass =
'text-xs text-gray-500 transition-colors hover:text-gray-900 dark:text-gray-500 dark:hover:text-white';
$: edit = !!memory?.id;
$: if (show) {
content = memory?.content ?? '';
type = memory?.type ?? 'user';
path = memory?.path ?? '';
}
const submitHandler = async () => {
loading = true;
const res = await (
edit
? updateMemoryById(localStorage.token, memory.id, content, type, path)
: addNewMemory(localStorage.token, content, type, path)
).catch((error) => {
toast.error(`${error}`);
return null;
});
if (res) {
toast.success(
edit ? $i18n.t('Memory updated successfully') : $i18n.t('Memory added successfully')
);
show = false;
dispatch('save');
}
loading = false;
};
</script>
<Modal bind:show size="sm" className="bg-white dark:bg-gray-900 rounded-4xl">
<div>
<div class=" flex justify-between text-gray-900 dark:text-white px-4 pt-3 pb-1">
<div class=" text-sm font-medium self-center">
{#if edit}
{$i18n.t('Edit Memory')}
{:else}
{$i18n.t('Add Memory')}
{/if}
</div>
<button
class="self-center rounded-lg p-1 text-gray-500 transition hover:bg-gray-50 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-gray-800 dark:hover:text-gray-200"
on:click={() => (show = false)}
>
<XMark className={'size-4'} />
</button>
</div>
<div
class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 text-gray-600 dark:text-gray-400"
>
<div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
<form class="flex flex-col w-full" on:submit|preventDefault={submitHandler}>
<div class="px-1">
<div class="flex w-full justify-between items-center mb-1.5">
<div class="text-xs text-gray-600 dark:text-gray-400">{$i18n.t('Type')}</div>
<button
type="button"
class={actionButtonClass}
on:click={() => {
type = type === 'user' ? 'context' : 'user';
}}
>
{#if type === 'user'}
{$i18n.t('User')}
{:else}
{$i18n.t('Context')}
{/if}
</button>
</div>
<textarea
bind:value={content}
class={textareaClass}
rows="6"
placeholder={type === 'user'
? $i18n.t('Add a preference, fact, or instruction about you')
: $i18n.t('Add durable context for future chats')}
/>
<div class="flex flex-col w-full mt-1.5">
<label for="memory-path" class="mb-1 text-xs text-gray-600 dark:text-gray-400">
{$i18n.t('Path')}
<span class="opacity-50">({$i18n.t('optional')})</span>
</label>
<input
id="memory-path"
bind:value={path}
class={inputClass}
placeholder={$i18n.t('Path')}
autocomplete="off"
/>
</div>
</div>
<div class="flex justify-end pt-1 text-sm font-normal">
<button
class="px-3.5 py-1.5 text-sm font-normal bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex items-center gap-2 whitespace-nowrap {loading
? ' cursor-not-allowed'
: ''}"
type="submit"
disabled={loading}
>
{#if edit}
{$i18n.t('Update')}
{:else}
{$i18n.t('Add')}
{/if}
{#if loading}
<span class="shrink-0">
<Spinner />
</span>
{/if}
</button>
</div>
</form>
</div>
</div>
</div>
</Modal>