[Session] Add session_messages table for conversation history #37

Closed
opened 2026-03-21 00:34:20 +00:00 by replit · 1 comment
Owner

What

Add a session_messages table 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

CREATE TABLE session_messages (
  id          UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  session_id  UUID NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
  role        TEXT NOT NULL CHECK (role IN ('user', 'assistant')),
  content     TEXT NOT NULL,
  token_count INTEGER NOT NULL DEFAULT 0,
  created_at  TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX ON session_messages(session_id, created_at);

Drizzle migration

  • Add session_messages table to packages/db/src/schema.ts
  • Export sessionMessages table object and the SessionMessage type
  • Generate and apply migration via pnpm drizzle-kit generate + migrate

Retrieval function

Create getSessionHistory(sessionId, maxMessages=8, tokenBudget=4000) in packages/db/src/index.ts or a new helper:

  • Query session_messages WHERE session_id = $1 ORDER BY created_at DESC LIMIT $maxMessages
  • Reverse result to chronological order
  • Trim leading messages until total token_count <= tokenBudget
  • Return Array<{ role, content }>

Token counting

Use a lightweight heuristic: Math.ceil(content.length / 4) characters → tokens. Store this at insert time as token_count.

Relevant files

  • packages/db/src/schema.ts
  • packages/db/src/index.ts
  • artifacts/api-server/src/routes/sessions.ts

Acceptance

  • Migration runs cleanly on a fresh DB
  • getSessionHistory returns messages in chronological order, trimmed to token budget
  • Unit test (or manual curl) confirms inserts and retrieval work
## What Add a `session_messages` table 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 ```sql CREATE TABLE session_messages ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), session_id UUID NOT NULL REFERENCES sessions(id) ON DELETE CASCADE, role TEXT NOT NULL CHECK (role IN ('user', 'assistant')), content TEXT NOT NULL, token_count INTEGER NOT NULL DEFAULT 0, created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE INDEX ON session_messages(session_id, created_at); ``` ## Drizzle migration - Add `session_messages` table to `packages/db/src/schema.ts` - Export `sessionMessages` table object and the `SessionMessage` type - Generate and apply migration via `pnpm drizzle-kit generate` + `migrate` ## Retrieval function Create `getSessionHistory(sessionId, maxMessages=8, tokenBudget=4000)` in `packages/db/src/index.ts` or a new helper: - Query `session_messages WHERE session_id = $1 ORDER BY created_at DESC LIMIT $maxMessages` - Reverse result to chronological order - Trim leading messages until total `token_count <= tokenBudget` - Return `Array<{ role, content }>` ## Token counting Use a lightweight heuristic: `Math.ceil(content.length / 4)` characters → tokens. Store this at insert time as `token_count`. ## Relevant files - `packages/db/src/schema.ts` - `packages/db/src/index.ts` - `artifacts/api-server/src/routes/sessions.ts` ## Acceptance - Migration runs cleanly on a fresh DB - `getSessionHistory` returns messages in chronological order, trimmed to token budget - Unit test (or manual curl) confirms inserts and retrieval work
replit added the aibackend labels 2026-03-21 00:34:20 +00:00
claude was assigned by Rockachopa 2026-03-22 23:37:31 +00:00
Collaborator

PR #80 created.

Added session_messages table with schema, migration (0008), and a GET /sessions/:id/messages endpoint. User and assistant messages are persisted transactionally during POST /sessions/:id/request. Each message links back to its session_request_id for cross-referencing.

PR #80 created. Added `session_messages` table with schema, migration (0008), and a `GET /sessions/:id/messages` endpoint. User and assistant messages are persisted transactionally during `POST /sessions/:id/request`. Each message links back to its `session_request_id` for cross-referencing.
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: replit/timmy-tower#37