@@ -1,7 +1,22 @@
|
||||
use crate::db::schema::{deserialize_embedding, serialize_embedding};
|
||||
use crate::models::*;
|
||||
use chrono::{TimeZone, Utc};
|
||||
use rusqlite::{params, Connection, OptionalExtension, Result, Row};
|
||||
use chrono::{
|
||||
TimeZone,
|
||||
Utc,
|
||||
};
|
||||
use rusqlite::{
|
||||
Connection,
|
||||
OptionalExtension,
|
||||
Result,
|
||||
Row,
|
||||
params,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
db::schema::{
|
||||
deserialize_embedding,
|
||||
serialize_embedding,
|
||||
},
|
||||
models::*,
|
||||
};
|
||||
|
||||
/// Insert a new message into the database
|
||||
pub fn insert_message(conn: &Connection, msg: &lib::Message) -> Result<i64> {
|
||||
@@ -71,7 +86,10 @@ pub fn insert_message_embedding(
|
||||
}
|
||||
|
||||
/// Get message embedding
|
||||
pub fn get_message_embedding(conn: &Connection, message_id: i64) -> Result<Option<MessageEmbedding>> {
|
||||
pub fn get_message_embedding(
|
||||
conn: &Connection,
|
||||
message_id: i64,
|
||||
) -> Result<Option<MessageEmbedding>> {
|
||||
conn.query_row(
|
||||
"SELECT id, message_id, embedding, model_name, created_at
|
||||
FROM message_embeddings WHERE message_id = ?1",
|
||||
@@ -203,7 +221,7 @@ pub fn list_emotions(
|
||||
) -> Result<Vec<Emotion>> {
|
||||
let mut query = String::from(
|
||||
"SELECT id, message_id, emotion, confidence, model_version, created_at, updated_at
|
||||
FROM emotions WHERE 1=1"
|
||||
FROM emotions WHERE 1=1",
|
||||
);
|
||||
|
||||
if emotion_filter.is_some() {
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use rusqlite::{Connection, Result};
|
||||
use rusqlite::{
|
||||
Connection,
|
||||
Result,
|
||||
};
|
||||
use tracing::info;
|
||||
|
||||
pub fn initialize_database(conn: &Connection) -> Result<()> {
|
||||
@@ -9,14 +12,17 @@ pub fn initialize_database(conn: &Connection) -> Result<()> {
|
||||
|
||||
// Try to load the vector extension (non-fatal if it fails for now)
|
||||
match unsafe { conn.load_extension_enable() } {
|
||||
Ok(_) => {
|
||||
| Ok(_) => {
|
||||
match unsafe { conn.load_extension(vec_path, None::<&str>) } {
|
||||
Ok(_) => info!("Loaded sqlite-vec extension"),
|
||||
Err(e) => info!("Could not load sqlite-vec extension: {}. Vector operations will not be available.", e),
|
||||
| Ok(_) => info!("Loaded sqlite-vec extension"),
|
||||
| Err(e) => info!(
|
||||
"Could not load sqlite-vec extension: {}. Vector operations will not be available.",
|
||||
e
|
||||
),
|
||||
}
|
||||
let _ = unsafe { conn.load_extension_disable() };
|
||||
}
|
||||
Err(e) => info!("Extension loading not enabled: {}", e),
|
||||
},
|
||||
| Err(e) => info!("Extension loading not enabled: {}", e),
|
||||
}
|
||||
|
||||
// Create messages table
|
||||
@@ -172,10 +178,7 @@ pub fn initialize_database(conn: &Connection) -> Result<()> {
|
||||
|
||||
/// Helper function to serialize f32 vector to bytes for storage
|
||||
pub fn serialize_embedding(embedding: &[f32]) -> Vec<u8> {
|
||||
embedding
|
||||
.iter()
|
||||
.flat_map(|f| f.to_le_bytes())
|
||||
.collect()
|
||||
embedding.iter().flat_map(|f| f.to_le_bytes()).collect()
|
||||
}
|
||||
|
||||
/// Helper function to deserialize bytes back to f32 vector
|
||||
|
||||
Reference in New Issue
Block a user