use rusqlite::Connection;
use serde_json::Value;
use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::Result;
pub struct SQLiteLogger {
conn: Connection,
}
impl SQLiteLogger {
pub fn new(db_path: &str) -> Result<Self> {
let conn = Connection::open(db_path)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS program_logs (
id TEXT PRIMARY KEY,
timestamp REAL,
function_input TEXT,
function_output TEXT,
llm_input TEXT,
llm_output TEXT,
function_version TEXT,
response_metadata TEXT,
execution_time REAL
)",
[],
)?;
Ok(SQLiteLogger { conn })
}
pub fn log_execution(
&self,
function_input: &Value,
function_output: &Value,
llm_input: &str,
llm_output: &str,
function_version: &str,
response_metadata: &Value,
execution_time: f64,
) -> Result<()> {
let id = uuid::Uuid::new_v4().to_string();
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs_f64();
self.conn.execute(
"INSERT INTO program_logs
(id, timestamp, function_input, function_output, llm_input, llm_output,
function_version, response_metadata, execution_time)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
[
&id,
×tamp.to_string(),
&serde_json::to_string(function_input)?,
&serde_json::to_string(function_output)?,
&llm_input.to_string(),
&llm_output.to_string(),
&function_version.to_string(),
&serde_json::to_string(response_metadata)?,
&execution_time.to_string(),
],
)?;
Ok(())
}
}