Files
nexusAI/packages/orchestration-service/src/services/memory.js
Storme-bit 3b5f0afece fixed typo
2026-04-05 22:45:01 -07:00

56 lines
2.0 KiB
JavaScript

const fetch = require('node-fetch');
const { getEnv } = require('@nexusai/shared');
const BASE_URL = getEnv('MEMORY_SERVICE_URL', 'http://localhost:3002');
//function to get session by external id, returns null if not found, throws error for other issues
async function getSessionByExternalId(externalId) {
const res = await fetch(`${BASE_URL}/sessions/by-external/${externalId}`);
if (res.status === 404) return null; // Not found or bad request
if (!res.ok) throw new Error(`Memory service error: ${res.status} ${res.statusText}`); // Other errors
return res.json();
}
// create a new session with an external ID, returns the created session
async function createSession(externalId) {
const res = await fetch(`${BASE_URL}/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ externalId })
});
if (!res.ok) throw new Error(`Failed to create sessions: ${res.status} ${res.statusText}`);
return res.json();
}
async function getRecentEpisodes(sessionId, limit = 10) {
const res = await fetch(`${BASE_URL}/sessions/${sessionId}/episodes?limit=${limit}`);
if (!res.ok) throw new Error(`Failed to fetch episodes: ${res.status} ${res.statusText}`);
return res.json();
}
async function createEpisode(sessionId, userMessage, aiResponse, tokenCount) {
const res = await fetch(`${BASE_URL}/episodes`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sessionId, userMessage, aiResponse, tokenCount })
});
if (!res.ok) throw new Error(`Failed to create episode: ${res.status} ${res.statusText}`);
return res.json();
}
async function getEpisodeById(episodeId) {
const res = await fetch(`${BASE_URL}/episodes/${episodeId}`);
if (res.status === 404) return null;
if (!res.ok) throw new Error(`Failed to fetch episode: ${res.status}`);
return res.json();
}
module.exports = {
getSessionByExternalId,
createSession,
getRecentEpisodes,
createEpisode,
getEpisodeById
}