use chrono::Utc;
use rusqlite::params;
use uuid::Uuid;
use crate::memory::lifecycle::MemoryImportance;
use super::{Result, vec_to_blob};
pub(crate) struct NewRow<'a> {
pub project_id: &'a str,
pub content: &'a str,
pub metadata: Option<&'a str>,
pub memory_type: &'a str,
pub status: &'a str,
pub importance: &'a str,
}
impl super::Database {
pub fn insert(
&self,
project_id: &str,
content: &str,
embedding: &[f32],
metadata: Option<&str>,
memory_type: &str,
status: &str,
) -> Result<String> {
self.insert_with_importance(
&NewRow {
project_id,
content,
metadata,
memory_type,
status,
importance: MemoryImportance::default().as_str(),
},
embedding,
)
}
pub(crate) fn insert_with_importance(
&self,
row: &NewRow<'_>,
embedding: &[f32],
) -> Result<String> {
let id = Uuid::new_v4().to_string();
let now = Utc::now().to_rfc3339();
let blob = vec_to_blob(embedding)?;
self.conn.execute(
r#"
INSERT INTO memories (id, project_id, content, embedding, metadata, created_at, updated_at, type, status, importance)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)
"#,
params![
&id,
row.project_id,
row.content,
&blob,
row.metadata,
&now,
&now,
row.memory_type,
row.status,
row.importance,
],
)?;
Ok(id)
}
}