[Session] Add session_messages table for conversation history #37
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Add a
session_messagestable to persist conversation history within a funded session, and a retrieval function that returns the last N messages within a configurable token budget.Why
Currently each request inside a session is treated as an independent, stateless call. Without stored history, follow-up requests like "make it shorter" or "explain step 2" are meaningless — Timmy has no context for what was said before.
Schema
Drizzle migration
session_messagestable topackages/db/src/schema.tssessionMessagestable object and theSessionMessagetypepnpm drizzle-kit generate+migrateRetrieval function
Create
getSessionHistory(sessionId, maxMessages=8, tokenBudget=4000)inpackages/db/src/index.tsor a new helper:session_messages WHERE session_id = $1 ORDER BY created_at DESC LIMIT $maxMessagestoken_count <= tokenBudgetArray<{ role, content }>Token counting
Use a lightweight heuristic:
Math.ceil(content.length / 4)characters → tokens. Store this at insert time astoken_count.Relevant files
packages/db/src/schema.tspackages/db/src/index.tsartifacts/api-server/src/routes/sessions.tsAcceptance
getSessionHistoryreturns messages in chronological order, trimmed to token budgetPR #80 created.
Added
session_messagestable with schema, migration (0008), and aGET /sessions/:id/messagesendpoint. User and assistant messages are persisted transactionally duringPOST /sessions/:id/request. Each message links back to itssession_request_idfor cross-referencing.