Session Management
Session Management
Section titled “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.
Session Structure
Section titled “Session Structure”A session consists of:
| Field | Type | Purpose |
|---|---|---|
id | TEXT (PK) | Unique session identifier |
user_id | TEXT | Owner of the session |
title | TEXT | Display title (auto-generated or user-set) |
status | TEXT | Session state |
created_at | TEXT | Creation timestamp |
updated_at | TEXT | Last modification timestamp |
message_count | INTEGER | Number of messages/blocks |
metadata | TEXT (JSON) | Phase state, preferences, etc. |
Sessions contain messages (blocks) in a child table:
| Field | Type | Purpose |
|---|---|---|
id | TEXT (PK) | Block identifier |
session_id | TEXT (FK) | Parent session (CASCADE delete) |
role | TEXT | Block role (user, assistant, system) |
content | TEXT | Block content (JSON) |
timestamp | TEXT | Creation time |
metadata | TEXT (JSON) | Block type, phase, etc. |
Persistence Layer
Section titled “Persistence Layer”Session operations are centralized in src/lib/db/sessions.ts (the SessionsDB class):
// Database configurationconst db = new Database(process.env.SQLITE_PATH || './data/juca.db');db.pragma('journal_mode = WAL'); // Write-Ahead Logging for concurrencydb.pragma('foreign_keys = ON'); // Enforce referential integrityAll session access from other parts of the codebase goes through this module — direct SQLite access from components or API routes is not allowed.
SessionSidebar
Section titled “SessionSidebar”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
Server Actions
Section titled “Server Actions”Session mutations happen through Server Actions in src/actions/:
| Action File | Key Functions | Purpose |
|---|---|---|
session.ts | Create, load, delete sessions | CRUD operations |
briefing.ts | Phase transitions, evaluations | Briefing-specific state |
message.ts | Add messages/blocks | Content creation |
All actions use requireActionAuth() from src/actions/utils.ts:
// Every server action starts with authconst user = await requireActionAuth();// In dev mode (ENABLE_DEV_AUTH=true), returns:// { id: 'dev-user', email: 'dev@localhost', name: 'Dev User', role: 'admin' }