updated updateSession and deleteSession routes to use external Ids, and added 'name' column to sessions table
This commit is contained in:
@@ -14,6 +14,11 @@ function getDB() {
|
|||||||
|
|
||||||
db.exec(schema);
|
db.exec(schema);
|
||||||
|
|
||||||
|
try{
|
||||||
|
db.exec(`ALTER TABLE sessions ADD COLUMN name TEXT`)
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
|
||||||
// Sync FTS index with any existing episodes data
|
// Sync FTS index with any existing episodes data
|
||||||
db.exec(`INSERT OR REPLACE INTO episodes_fts(rowid, user_message, ai_response)
|
db.exec(`INSERT OR REPLACE INTO episodes_fts(rowid, user_message, ai_response)
|
||||||
SELECT id, user_message, ai_response FROM episodes`);
|
SELECT id, user_message, ai_response FROM episodes`);
|
||||||
|
|||||||
@@ -52,6 +52,29 @@ function deleteSession(id) {
|
|||||||
db.prepare(`DELETE FROM sessions WHERE id = ?`).run(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 --------------------------------------------------
|
// --Episodes --------------------------------------------------
|
||||||
// Creates a new episode linked to a session, with user message, AI response, optional token count, and metadata
|
// 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) {
|
async function createEpisode(sessionId, userMessage, aiResponse, tokenCount = null, metadata = null) {
|
||||||
@@ -165,6 +188,9 @@ module.exports = {
|
|||||||
getSessions,
|
getSessions,
|
||||||
getSessionByExternalId,
|
getSessionByExternalId,
|
||||||
deleteSession,
|
deleteSession,
|
||||||
|
updateSession,
|
||||||
|
updateSessionByExternalId,
|
||||||
|
deleteSessionByExternalId,
|
||||||
createEpisode,
|
createEpisode,
|
||||||
getEpisode,
|
getEpisode,
|
||||||
getEpisodesBySession,
|
getEpisodesBySession,
|
||||||
|
|||||||
@@ -64,12 +64,21 @@ app.get('/sessions/:id', (req, res) => {
|
|||||||
res.json(session);
|
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
|
// Updates the session's updated_at timestamp to now
|
||||||
app.delete('/sessions/:id', (req, res) => {
|
app.delete('/sessions/by-external/:externalId', (req, res) => {
|
||||||
episodic.deleteSession(req.params.id);
|
episodic.deleteSessionByExternalId(req.params.externalId);
|
||||||
res.status(204).send();
|
res.status(204).send();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user