updated updateSession and deleteSession routes to use external Ids, and added 'name' column to sessions table

This commit is contained in:
Storme-bit
2026-04-13 02:50:38 -07:00
parent 1f0d9acea8
commit f6d538f68a
3 changed files with 42 additions and 2 deletions

View File

@@ -14,6 +14,11 @@ function getDB() {
db.exec(schema);
try{
db.exec(`ALTER TABLE sessions ADD COLUMN name TEXT`)
} catch {}
// Sync FTS index with any existing episodes data
db.exec(`INSERT OR REPLACE INTO episodes_fts(rowid, user_message, ai_response)
SELECT id, user_message, ai_response FROM episodes`);

View File

@@ -52,6 +52,29 @@ function deleteSession(id) {
db.prepare(`DELETE FROM sessions WHERE id = ?`).run(id);
}
function updateSession(id, { name } = {}){
const db = getDB();
db.prepare(`
UPDATE sessions
SET name = ?, updated_at = unixepoch()
WHERE id = ?
`).run(name ?? null, id);
return getSession(id);
}
function updateSessionByExternalId(externalId, fields) {
const session = getSessionByExternalId(externalId);
if (!session) throw new Error('Session not found');
return updateSession(session.id, fields);
}
function deleteSessionByExternalId(externalId) {
const session = getSessionByExternalId(externalId);
if(!session) throw new Error('Session not found');
deleteSession(session.id);
}
// --Episodes --------------------------------------------------
// Creates a new episode linked to a session, with user message, AI response, optional token count, and metadata
async function createEpisode(sessionId, userMessage, aiResponse, tokenCount = null, metadata = null) {
@@ -165,6 +188,9 @@ module.exports = {
getSessions,
getSessionByExternalId,
deleteSession,
updateSession,
updateSessionByExternalId,
deleteSessionByExternalId,
createEpisode,
getEpisode,
getEpisodesBySession,

View File

@@ -64,12 +64,21 @@ app.get('/sessions/:id', (req, res) => {
res.json(session);
});
app.patch('/sessions/by-external/:externalId', (req, res) => {
const { name } = req.body;
try {
const session = episodic.updateSessionByExternalId(req.params.externalId, {name });
res.json(session);
} catch (err) {
res.status(500).json({error: err.message });
}
});
// Updates the session's updated_at timestamp to now
app.delete('/sessions/:id', (req, res) => {
episodic.deleteSession(req.params.id);
app.delete('/sessions/by-external/:externalId', (req, res) => {
episodic.deleteSessionByExternalId(req.params.externalId);
res.status(204).send();
});