Skip to content

Refactor HTML generation to support SQLite storage and serving#83

Closed
ananis25 wants to merge 5 commits intosimonw:mainfrom
ananis25:claude/migrate-sqlite-transcripts-YpVdu
Closed

Refactor HTML generation to support SQLite storage and serving#83
ananis25 wants to merge 5 commits intosimonw:mainfrom
ananis25:claude/migrate-sqlite-transcripts-YpVdu

Conversation

@ananis25
Copy link

@ananis25 ananis25 commented Mar 2, 2026

Summary

This PR refactors the HTML generation pipeline to decouple rendering from file I/O, enabling storage of rendered transcripts in SQLite and serving them via HTTP. The changes introduce a new database layer, new CLI commands (sync and serve), and update existing commands to support both legacy file-based output and new database-backed workflows.

Key Changes

  • Refactored generate_html() function: Split into three focused functions:

    • render_session_pages(): Pure rendering function that returns HTML pages and metadata in memory
    • write_pages_to_dir(): Writes rendered pages to filesystem
    • generate_html(): Orchestrates the pipeline (maintains backward compatibility)
  • New database module (src/claude_code_transcripts/db.py):

    • SQLite schema with sessions and session_pages tables
    • Core operations: get_db(), session_exists(), import_session(), get_session_page(), list_projects(), list_sessions()
    • WAL mode enabled for concurrent access
    • Deduplication on import (skips existing session IDs)
  • New HTTP server module (src/claude_code_transcripts/serve.py):

    • make_handler() factory creates request handlers bound to a database
    • Routes: master index (/), project index (/<project>/), session pages (/<project>/<session>/<page>.html)
    • Renders dynamic index pages from database queries
  • New import functions:

    • import_session_file_to_db(): Import from JSONL/JSON file path
    • import_session_data_to_db(): Import from parsed session data dict
  • Updated CLI commands:

    • local and json commands: Default to database import (unless -o, --output-auto, or --gist specified)
    • Maintains backward compatibility with file-based output when flags are provided
    • Updated output messages to reflect database storage
  • Comprehensive test coverage:

    • tests/test_db.py: Database operations (schema, CRUD, dedup)
    • tests/test_sync.py: Sync command functionality
    • tests/test_serve.py: HTTP server routing and rendering

Implementation Details

  • Metadata (total pages, prompts, messages, tool calls, commits, GitHub repo) is extracted during rendering and stored alongside HTML pages
  • Session summaries are extracted from first user message for quick preview
  • File metadata (mtime, size) is preserved during import for change detection
  • Database uses foreign keys and transactions for data integrity
  • HTTP server dynamically renders index pages from database queries rather than storing static HTML
  • Backward compatibility maintained: existing -o flag behavior unchanged

https://claude.ai/code/session_019CmHK1C5R4AHwmLAsw2PEt

claude added 5 commits March 2, 2026 06:28
Introduces db.py with schema creation, session import with dedup,
page retrieval, and project/session listing queries. Uses stdlib
sqlite3 with WAL mode for future Turso compatibility.

https://claude.ai/code/session_019CmHK1C5R4AHwmLAsw2PEt
Refactors generate_html() and generate_html_from_session_data() to both
use a shared render_session_pages() that returns pages in memory as a dict.
Also adds write_pages_to_dir() helper. This enables the upcoming SQLite
storage flow where HTML is generated in memory and stored in the database.

https://claude.ai/code/session_019CmHK1C5R4AHwmLAsw2PEt
New CLI command: `claude-code-transcripts sync` scans ~/.claude/projects,
renders HTML in memory, and stores sessions in SQLite with dedup.
Skips sessions already present by session_id.

https://claude.ai/code/session_019CmHK1C5R4AHwmLAsw2PEt
New serve.py module with HTTP handler that routes URLs to SQLite queries.
Master index and project index pages are rendered dynamically from DB.
Session pages are served directly from the session_pages table.

URL routing: / -> master index, /<project>/ -> project index,
/<project>/<session>/<page>.html -> session page content.

https://claude.ai/code/session_019CmHK1C5R4AHwmLAsw2PEt
Commands now import sessions into SQLite instead of writing static HTML
files. The -o/--output and --gist flags still produce file output as an
escape hatch. Also adds import_session_file_to_db() and
import_session_data_to_db() helpers for the common import flow.

https://claude.ai/code/session_019CmHK1C5R4AHwmLAsw2PEt
@ananis25 ananis25 closed this Mar 2, 2026
@ananis25
Copy link
Author

ananis25 commented Mar 2, 2026

Sorry, I didn't notice this opened a PR against your repo.

@ananis25 ananis25 deleted the claude/migrate-sqlite-transcripts-YpVdu branch March 2, 2026 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants