import sqlite3 import json from src.config import DB_PATH from src.models import Trace class Database: def __init__(self, db_path: str = DB_PATH): self.db_path = db_path self._init_db() def _init_db(self): with sqlite3.connect(self.db_path) as conn: conn.executescript(""" CREATE TABLE IF NOT EXISTS tasks ( task_id TEXT PRIMARY KEY, question_text TEXT, file_name TEXT, difficulty_level TEXT, source TEXT, created_at TEXT DEFAULT (datetime('now')) ); CREATE TABLE IF NOT EXISTS retrieval_traces ( trace_id INTEGER PRIMARY KEY AUTOINCREMENT, task_id TEXT, tool_name TEXT, query_or_input TEXT, output_summary TEXT, timestamp TEXT DEFAULT (datetime('now')) ); CREATE TABLE IF NOT EXISTS answers ( task_id TEXT PRIMARY KEY, raw_answer TEXT, normalized_answer TEXT, verification_status TEXT, submitted_at TEXT DEFAULT (datetime('now')) ); CREATE TABLE IF NOT EXISTS eval_runs ( run_id INTEGER PRIMARY KEY AUTOINCREMENT, model_name TEXT, prompt_version TEXT, score REAL, notes TEXT, timestamp TEXT DEFAULT (datetime('now')) ); """) def save_trace(self, trace: Trace): with sqlite3.connect(self.db_path) as conn: conn.execute( "INSERT OR REPLACE INTO tasks (task_id, question_text) VALUES (?, ?)", (trace.task_id, trace.question) ) conn.execute( "INSERT OR REPLACE INTO answers (task_id, raw_answer, normalized_answer, verification_status) VALUES (?, ?, ?, ?)", (trace.task_id, trace.answer.raw, trace.answer.normalized, trace.answer.verification) )