added endpoints and routes to get sessions
This commit is contained in:
@@ -23,6 +23,16 @@ function getSession(id) {
|
|||||||
return parseSession(stmt.get(id));
|
return parseSession(stmt.get(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSessions(limit = EPISODIC.DEFAULT_PAGE_SIZE, offset = 0) {
|
||||||
|
const db = getDB();
|
||||||
|
const stmt = db.prepare(`
|
||||||
|
SELECT * FROM sessions
|
||||||
|
ORDER BY updated_at DESC
|
||||||
|
LIMIT ? OFFSET ?
|
||||||
|
`);
|
||||||
|
return stmt.all(limit, offset).map(parseSession);
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieves a session by its external ID
|
// Retrieves a session by its external ID
|
||||||
function getSessionByExternalId(externalId) {
|
function getSessionByExternalId(externalId) {
|
||||||
const db = getDB();
|
const db = getDB();
|
||||||
@@ -172,6 +182,7 @@ async function getEpisodeEmbedding(userMessage, aiResponse){
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
createSession,
|
createSession,
|
||||||
getSession,
|
getSession,
|
||||||
|
getSessions,
|
||||||
getSessionByExternalId,
|
getSessionByExternalId,
|
||||||
deleteSession,
|
deleteSession,
|
||||||
createEpisode,
|
createEpisode,
|
||||||
|
|||||||
@@ -41,6 +41,12 @@ app.post('/sessions', (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get('/sessions', (req, res) => {
|
||||||
|
const {limit = 20, offset = 0 } = req.query;
|
||||||
|
const sessions = episodic.getSessions(Number(limit), Number(offset));
|
||||||
|
res.json(sessions);
|
||||||
|
})
|
||||||
|
|
||||||
// Retrieves a session by its external ID
|
// Retrieves a session by its external ID
|
||||||
app.get('/sessions/by-external/:externalId', (req, res) => {
|
app.get('/sessions/by-external/:externalId', (req, res) => {
|
||||||
const session = episodic.getSessionByExternalId(req.params.externalId);
|
const session = episodic.getSessionByExternalId(req.params.externalId);
|
||||||
@@ -48,6 +54,8 @@ app.get('/sessions/by-external/:externalId', (req, res) => {
|
|||||||
res.json(session);
|
res.json(session);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Retrieves a session by its internal ID
|
// Retrieves a session by its internal ID
|
||||||
app.get('/sessions/:id', (req, res) => {
|
app.get('/sessions/:id', (req, res) => {
|
||||||
const session = episodic.getSession(req.params.id);
|
const session = episodic.getSession(req.params.id);
|
||||||
|
|||||||
@@ -18,4 +18,14 @@ router.get('/:sessionId/history', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/', async (req, res) => {
|
||||||
|
const {limit = 20, offset = 0 } = req.query;
|
||||||
|
try {
|
||||||
|
const sessions = await memory.getSessions(Number(limit), Number(offset));
|
||||||
|
res.json(sessions);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({error: err.message});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
@@ -56,11 +56,20 @@ async function getSessionHistory(sessionId, limit = 20, offset = 0) {
|
|||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getSessions(limit = 20, offset = 0) {
|
||||||
|
const res = await fetch(
|
||||||
|
`${BASE_URL}/sessions?limit${limit}&offset=${offset}`
|
||||||
|
);
|
||||||
|
if (!res.ok) throw new Error(`Failed to fetch sessions: ${res.status}`);
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getSessionByExternalId,
|
getSessionByExternalId,
|
||||||
createSession,
|
createSession,
|
||||||
getRecentEpisodes,
|
getRecentEpisodes,
|
||||||
createEpisode,
|
createEpisode,
|
||||||
getEpisodeById,
|
getEpisodeById,
|
||||||
getSessionHistory
|
getSessionHistory,
|
||||||
|
getSessions
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user