refinement fixes

This commit is contained in:
Storme-bit
2026-08-17 00:37:17 -07:00
parent 8f8177615c
commit 7757de76b7
5 changed files with 324 additions and 34 deletions
@@ -172,6 +172,30 @@ function getRecentEpisodes(sessionId, limit = EPISODIC.DEFAULT_RECENT_LIMIT) {
}
// Aggregate stats for a session — count, total tokens, and highest episode id.
// Lets summarization check the token threshold without pulling every episode row.
function getSessionEpisodeStats(sessionId) {
const db = getDB();
return db.prepare(`
SELECT COUNT(*) AS count,
COALESCE(SUM(token_count), 0) AS totalTokens,
COALESCE(MAX(id), 0) AS maxId
FROM episodes
WHERE session_id = ?
`).get(sessionId);
}
// Episodes newer than a given id, in chronological order. Summarization uses
// this to fetch only the un-summarized tail rather than the whole session.
function getEpisodesSince(sessionId, afterId) {
const db = getDB();
return db.prepare(`
SELECT * FROM episodes
WHERE session_id = ? AND id > ?
ORDER BY id ASC
`).all(sessionId, afterId).map(parseRow);
}
// Searches episodes using FTS5 full-text search, ordered by relevance, with a limit
function searchEpisodes(query, limit = EPISODIC.DEFAULT_SEARCH_LIMIT, sessionIds = null) {
const db = getDB();
@@ -247,6 +271,8 @@ module.exports = {
getEpisode,
getEpisodesBySession,
getRecentEpisodes,
getSessionEpisodeStats,
getEpisodesSince,
searchEpisodes,
deleteEpisode,
getEpisodesByProject
+12
View File
@@ -156,6 +156,18 @@ app.get('/sessions/:id/episodes', (req, res) => {
res.json(episodes);
});
// Aggregate episode stats for a session (count, total tokens, max id).
// Used by summarization to check the token threshold cheaply.
app.get('/sessions/:id/episode-stats', (req, res) => {
res.json(episodic.getSessionEpisodeStats(Number(req.params.id)));
});
// Episodes newer than :afterId, chronological — the un-summarized tail.
app.get('/sessions/:id/episodes/since/:afterId', (req, res) => {
const episodes = episodic.getEpisodesSince(Number(req.params.id), Number(req.params.afterId));
res.json(episodes);
});
app.delete('/episodes/:id', (req, res) => {
const id = Number(req.params.id);
episodic.deleteEpisode(id);