from __future__ import annotations import sqlite3 from contextlib import contextmanager from pathlib import Path from typing import Iterator import numpy as np SCHEMA = """ PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL; CREATE TABLE IF NOT EXISTS media ( id TEXT PRIMARY KEY, source_path TEXT NOT NULL, title TEXT NOT NULL, content_sha256 TEXT NOT NULL, size_bytes INTEGER NOT NULL, mtime_ns INTEGER NOT NULL, interval_ms INTEGER NOT NULL, model_id TEXT NOT NULL, embedding_dim INTEGER NOT NULL, source_kind TEXT NOT NULL DEFAULT 'file', jellyfin_item_id TEXT, jellyfin_media_source_id TEXT, jellyfin_library_id TEXT, jellyfin_item_type TEXT, jellyfin_tags TEXT, jellyfin_etag TEXT, trickplay_width INTEGER, trickplay_tile_width INTEGER, trickplay_tile_height INTEGER, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS frames ( id TEXT PRIMARY KEY, media_id TEXT NOT NULL REFERENCES media(id) ON DELETE CASCADE, frame_number INTEGER NOT NULL, timestamp_ms INTEGER NOT NULL, thumb_path TEXT NOT NULL, embedding_f16 BLOB NOT NULL, caption TEXT, caption_model TEXT, captioned_at TEXT, caption_route TEXT, caption_route_reason TEXT, route_score REAL, route_model TEXT, route_policy TEXT, routed_at TEXT, meili_synced_at TEXT, UNIQUE(media_id, frame_number) ); CREATE INDEX IF NOT EXISTS idx_frames_media_timestamp ON frames(media_id, timestamp_ms); CREATE TABLE IF NOT EXISTS caption_windows ( id TEXT PRIMARY KEY, media_id TEXT NOT NULL REFERENCES media(id) ON DELETE CASCADE, start_ms INTEGER NOT NULL, end_ms INTEGER NOT NULL, representative_frame_id TEXT NOT NULL REFERENCES frames(id) ON DELETE CASCADE, caption TEXT, caption_model TEXT, captioned_at TEXT, caption_route TEXT, caption_route_reason TEXT, route_score REAL, route_model TEXT, route_policy TEXT, routed_at TEXT, window_policy TEXT NOT NULL, meili_synced_at TEXT, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE(media_id, start_ms, end_ms, window_policy) ); CREATE TABLE IF NOT EXISTS caption_window_frames ( window_id TEXT NOT NULL REFERENCES caption_windows(id) ON DELETE CASCADE, frame_id TEXT NOT NULL REFERENCES frames(id) ON DELETE CASCADE, position INTEGER NOT NULL, PRIMARY KEY(window_id, frame_id), UNIQUE(frame_id), UNIQUE(window_id, position) ); CREATE INDEX IF NOT EXISTS idx_caption_windows_media_start ON caption_windows(media_id, start_ms); CREATE INDEX IF NOT EXISTS idx_caption_windows_unsynced ON caption_windows(meili_synced_at) WHERE caption IS NOT NULL; CREATE TABLE IF NOT EXISTS jellyfin_overview_drafts ( -- Do NOT cascade on media deletion: the indexing code temporarily deletes -- and recreates the media row when an ETag changes, including after metadata -- edits. Original Overview backups must survive that reindex lifecycle. media_id TEXT PRIMARY KEY, jellyfin_item_id TEXT NOT NULL, overview TEXT NOT NULL, model TEXT NOT NULL, style TEXT NOT NULL DEFAULT 'general', caption_fingerprint TEXT NOT NULL, sampled_captions INTEGER NOT NULL, original_overview TEXT, applied_overview TEXT, applied_at TEXT, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP ); """ @contextmanager def connect(path: Path) -> Iterator[sqlite3.Connection]: con = sqlite3.connect(path) con.row_factory = sqlite3.Row con.execute("PRAGMA foreign_keys = ON") try: yield con con.commit() finally: con.close() def _ensure_media_jellyfin_columns(con: sqlite3.Connection) -> None: columns = {row["name"] for row in con.execute("PRAGMA table_info(media)")} additions = { "source_kind": "TEXT NOT NULL DEFAULT 'file'", "jellyfin_item_id": "TEXT", "jellyfin_media_source_id": "TEXT", "jellyfin_library_id": "TEXT", "jellyfin_item_type": "TEXT", "jellyfin_tags": "TEXT", "jellyfin_etag": "TEXT", "trickplay_width": "INTEGER", "trickplay_tile_width": "INTEGER", "trickplay_tile_height": "INTEGER", } for name, sql_type in additions.items(): if name not in columns: con.execute(f"ALTER TABLE media ADD COLUMN {name} {sql_type}") con.execute( """ CREATE UNIQUE INDEX IF NOT EXISTS idx_media_jellyfin_item ON media(jellyfin_item_id) WHERE jellyfin_item_id IS NOT NULL """ ) def _ensure_frame_caption_columns(con: sqlite3.Connection) -> None: columns = {row["name"] for row in con.execute("PRAGMA table_info(frames)")} additions = { "caption": "TEXT", "caption_model": "TEXT", "captioned_at": "TEXT", "caption_route": "TEXT", "caption_route_reason": "TEXT", "route_score": "REAL", "route_model": "TEXT", "route_policy": "TEXT", "routed_at": "TEXT", "meili_synced_at": "TEXT", } for name, sql_type in additions.items(): if name not in columns: con.execute(f"ALTER TABLE frames ADD COLUMN {name} {sql_type}") con.execute( """ CREATE INDEX IF NOT EXISTS idx_frames_unsynced_caption ON frames(meili_synced_at) WHERE caption IS NOT NULL """ ) def _ensure_overview_columns(con: sqlite3.Connection) -> None: """Upgrade existing overview drafts without dropping publish/restore backups.""" columns = { row["name"] for row in con.execute("PRAGMA table_info(jellyfin_overview_drafts)") } if "style" not in columns: con.execute( "ALTER TABLE jellyfin_overview_drafts " "ADD COLUMN style TEXT NOT NULL DEFAULT 'general'" ) def init_db(path: Path) -> None: with connect(path) as con: con.executescript(SCHEMA) _ensure_media_jellyfin_columns(con) _ensure_frame_caption_columns(con) _ensure_overview_columns(con) def pack_vector(vector: np.ndarray) -> bytes: # Canonical store uses float16 to halve disk usage. Meilisearch receives float32. return np.asarray(vector, dtype=np.float16).tobytes() def unpack_vector(blob: bytes, dim: int) -> np.ndarray: return np.frombuffer(blob, dtype=np.float16, count=dim).astype(np.float32)