import { WEBUI_API_BASE_URL } from '$lib/constants'; export const getMemories = async (token: string) => { let error = null; const res = await fetch(`${WEBUI_API_BASE_URL}/memories/`, { method: 'GET', headers: { Accept: 'application/json', 'Content-Type': 'application/json', authorization: `Bearer ${token}` } }) .then(async (res) => { if (!res.ok) throw await res.json(); return res.json(); }) .catch((err) => { error = err.detail; console.error(err); return null; }); if (error) { throw error; } return res; }; export const addNewMemory = async (token: string, content: string, type = 'user', path = '') => { let error = null; const res = await fetch(`${WEBUI_API_BASE_URL}/memories/add`, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', authorization: `Bearer ${token}` }, body: JSON.stringify({ content: content, type, path }) }) .then(async (res) => { if (!res.ok) throw await res.json(); return res.json(); }) .catch((err) => { error = err.detail; console.error(err); return null; }); if (error) { throw error; } return res; }; export const updateMemoryById = async ( token: string, id: string, content: string, type?: string, path?: string ) => { let error = null; const body = { content, ...(type ? { type } : {}), ...(path !== undefined ? { path } : {}) }; const res = await fetch(`${WEBUI_API_BASE_URL}/memories/${id}/update`, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', authorization: `Bearer ${token}` }, body: JSON.stringify(body) }) .then(async (res) => { if (!res.ok) throw await res.json(); return res.json(); }) .catch((err) => { error = err.detail; console.error(err); return null; }); if (error) { throw error; } return res; }; export const queryMemory = async (token: string, content: string) => { let error = null; const res = await fetch(`${WEBUI_API_BASE_URL}/memories/query`, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', authorization: `Bearer ${token}` }, body: JSON.stringify({ content: content }) }) .then(async (res) => { if (!res.ok) throw await res.json(); return res.json(); }) .catch((err) => { error = err.detail; console.error(err); return null; }); if (error) { throw error; } return res; }; export const deleteMemoryById = async (token: string, id: string) => { let error = null; const res = await fetch(`${WEBUI_API_BASE_URL}/memories/${id}`, { method: 'DELETE', headers: { Accept: 'application/json', 'Content-Type': 'application/json', authorization: `Bearer ${token}` } }) .then(async (res) => { if (!res.ok) throw await res.json(); return res.json(); }) .then((json) => { return json; }) .catch((err) => { error = err.detail; console.error(err); return null; }); if (error) { throw error; } return res; }; export const deleteMemoriesByUserId = async (token: string) => { let error = null; const res = await fetch(`${WEBUI_API_BASE_URL}/memories/delete/user`, { method: 'DELETE', headers: { Accept: 'application/json', 'Content-Type': 'application/json', authorization: `Bearer ${token}` } }) .then(async (res) => { if (!res.ok) throw await res.json(); return res.json(); }) .then((json) => { return json; }) .catch((err) => { error = err.detail; console.error(err); return null; }); if (error) { throw error; } return res; };