Skip to content

Session Management

Every conversation in Juca is a session — a persistent container of ordered blocks. Sessions are stored in SQLite, listed in the SessionSidebar, and loaded into the WorkCanvas.

A session consists of:

FieldTypePurpose
idTEXT (PK)Unique session identifier
user_idTEXTOwner of the session
titleTEXTDisplay title (auto-generated or user-set)
statusTEXTSession state
created_atTEXTCreation timestamp
updated_atTEXTLast modification timestamp
message_countINTEGERNumber of messages/blocks
metadataTEXT (JSON)Phase state, preferences, etc.

Sessions contain messages (blocks) in a child table:

FieldTypePurpose
idTEXT (PK)Block identifier
session_idTEXT (FK)Parent session (CASCADE delete)
roleTEXTBlock role (user, assistant, system)
contentTEXTBlock content (JSON)
timestampTEXTCreation time
metadataTEXT (JSON)Block type, phase, etc.

Session operations are centralized in src/lib/db/sessions.ts (the SessionsDB class):

// Database configuration
const db = new Database(process.env.SQLITE_PATH || './data/juca.db');
db.pragma('journal_mode = WAL'); // Write-Ahead Logging for concurrency
db.pragma('foreign_keys = ON'); // Enforce referential integrity

All session access from other parts of the codebase goes through this module — direct SQLite access from components or API routes is not allowed.

The SessionSidebar component (src/components/shell/SessionSidebar.tsx) displays the user’s session history:

  • Lists sessions ordered by updated_at DESC
  • Shows title, date, and phase status for each session
  • Click loads the session’s blocks into the WorkCanvas
  • Supports creating new sessions

Session mutations happen through Server Actions in src/actions/:

Action FileKey FunctionsPurpose
session.tsCreate, load, delete sessionsCRUD operations
briefing.tsPhase transitions, evaluationsBriefing-specific state
message.tsAdd messages/blocksContent creation

All actions use requireActionAuth() from src/actions/utils.ts:

// Every server action starts with auth
const user = await requireActionAuth();
// In dev mode (ENABLE_DEV_AUTH=true), returns:
// { id: 'dev-user', email: 'dev@localhost', name: 'Dev User', role: 'admin' }