import { getBackendBaseURL } from "../config"; import type { MemoryFactInput, MemoryFactPatchInput, UserMemory, } from "./types"; async function readMemoryResponse( response: Response, fallbackMessage: string, ): Promise { if (!response.ok) { const errorData = (await response.json().catch(() => ({}))) as { detail?: string; }; throw new Error( errorData.detail ?? `${fallbackMessage}: ${response.statusText}`, ); } return response.json() as Promise; } export async function loadMemory(): Promise { const response = await fetch(`${getBackendBaseURL()}/api/memory`); return readMemoryResponse(response, "Failed to fetch memory"); } export async function clearMemory(): Promise { const response = await fetch(`${getBackendBaseURL()}/api/memory`, { method: "DELETE", }); return readMemoryResponse(response, "Failed to clear memory"); } export async function deleteMemoryFact(factId: string): Promise { const response = await fetch( `${getBackendBaseURL()}/api/memory/facts/${encodeURIComponent(factId)}`, { method: "DELETE", }, ); return readMemoryResponse(response, "Failed to delete memory fact"); } export async function createMemoryFact( input: MemoryFactInput, ): Promise { const response = await fetch(`${getBackendBaseURL()}/api/memory/facts`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(input), }); return readMemoryResponse(response, "Failed to create memory fact"); } export async function updateMemoryFact( factId: string, input: MemoryFactPatchInput, ): Promise { const response = await fetch( `${getBackendBaseURL()}/api/memory/facts/${encodeURIComponent(factId)}`, { method: "PATCH", headers: { "Content-Type": "application/json", }, body: JSON.stringify(input), }, ); return readMemoryResponse(response, "Failed to update memory fact"); }