Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP INDEX CONCURRENTLY IF EXISTS idx_user_embeddings_user_model_conversation_created_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_user_embeddings_user_model_source_created_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_user_embeddings_user_model_created_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[general]
run_in_transaction = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Hot RAG retrieval paths: active embedding model for a user, newest first.
CREATE INDEX CONCURRENTLY idx_user_embeddings_user_model_created_id
ON user_embeddings(user_id, embedding_model, created_at DESC, id DESC);

CREATE INDEX CONCURRENTLY idx_user_embeddings_user_model_source_created_id
ON user_embeddings(user_id, embedding_model, source_type, created_at DESC, id DESC);

CREATE INDEX CONCURRENTLY idx_user_embeddings_user_model_conversation_created_id
ON user_embeddings(user_id, embedding_model, conversation_id, created_at DESC, id DESC);
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ mod os_flags;
mod private_key;
mod provider_routing;
mod proxy_config;
mod rag;
#[cfg(test)]
mod security_invariants;
mod seed_wrapping;
Expand Down Expand Up @@ -465,6 +466,7 @@ pub struct AppState {
enclave_key: Vec<u8>,
proxy_router: Arc<ProxyRouter>,
provider_router: Arc<ProviderRouter>,
rag_cache: Arc<tokio::sync::Mutex<rag::RagCache>>,
resend_api_key: Option<String>,
ephemeral_keys: Arc<RwLock<HashMap<String, EphemeralSecret>>>,
session_states: Arc<tokio::sync::RwLock<HashMap<Uuid, SessionState>>>,
Expand Down Expand Up @@ -768,6 +770,7 @@ impl AppStateBuilder {
enclave_key,
proxy_router,
provider_router,
rag_cache: Arc::new(tokio::sync::Mutex::new(rag::RagCache::default())),
resend_api_key: self.resend_api_key,
ephemeral_keys: Arc::new(RwLock::new(HashMap::new())),
session_states: Arc::new(tokio::sync::RwLock::new(HashMap::new())),
Expand Down Expand Up @@ -1706,6 +1709,7 @@ impl AppState {
encrypted_password,
new_wrapping,
)?;
self.rag_cache.lock().await.evict_user(user.uuid);

// Send confirmation email in the background
let app_state = self.clone();
Expand Down Expand Up @@ -2096,6 +2100,7 @@ impl AppState {
// Secret verification succeeded, proceed with account deletion
// Use the transaction-based method for atomicity
self.db.mark_and_delete_user(&user, &deletion_request)?;
self.rag_cache.lock().await.evict_user(user.uuid);

// Send confirmation email in the background if the user has an email
if let Some(email) = user.email.clone() {
Expand Down
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod responses;
pub mod schema;
pub mod token_usage;
pub mod user_api_keys;
pub mod user_embeddings;
pub mod user_kv;
pub mod user_seed_wrappings;
pub mod users;
85 changes: 85 additions & 0 deletions src/models/user_embeddings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::models::schema::user_embeddings;
use chrono::{DateTime, Utc};
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

#[derive(Error, Debug)]
pub enum UserEmbeddingError {
#[error("Database error: {0}")]
DatabaseError(#[from] diesel::result::Error),
}

#[derive(Queryable, Identifiable, AsChangeset, Serialize, Deserialize, Clone, Debug)]
#[diesel(table_name = user_embeddings)]
pub struct UserEmbedding {
pub id: i64,
pub uuid: Uuid,
pub user_id: Uuid,
pub source_type: String,
pub user_message_id: Option<i64>,
pub assistant_message_id: Option<i64>,
pub conversation_id: Option<i64>,
pub vector_enc: Vec<u8>,
pub embedding_model: String,
pub vector_dim: i32,
pub content_enc: Vec<u8>,
pub metadata_enc: Option<Vec<u8>>,
pub tags_enc: Vec<Option<String>>,
pub token_count: i32,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}

#[derive(Insertable, Debug, Clone)]
#[diesel(table_name = user_embeddings)]
pub struct NewUserEmbedding {
pub uuid: Uuid,
pub user_id: Uuid,
pub source_type: String,
pub user_message_id: Option<i64>,
pub assistant_message_id: Option<i64>,
pub conversation_id: Option<i64>,
pub vector_enc: Vec<u8>,
pub embedding_model: String,
pub vector_dim: i32,
pub content_enc: Vec<u8>,
pub metadata_enc: Option<Vec<u8>>,
pub tags_enc: Vec<Option<String>>,
pub token_count: i32,
}

impl NewUserEmbedding {
pub fn insert(&self, conn: &mut PgConnection) -> Result<UserEmbedding, UserEmbeddingError> {
diesel::insert_into(user_embeddings::table)
.values(self)
.get_result::<UserEmbedding>(conn)
.map_err(UserEmbeddingError::DatabaseError)
}
}

impl UserEmbedding {
pub fn delete_all_for_user(
conn: &mut PgConnection,
user_uuid: Uuid,
) -> Result<usize, UserEmbeddingError> {
diesel::delete(user_embeddings::table.filter(user_embeddings::user_id.eq(user_uuid)))
.execute(conn)
.map_err(UserEmbeddingError::DatabaseError)
}

pub fn delete_by_uuid_for_user(
conn: &mut PgConnection,
user_uuid: Uuid,
embedding_uuid: Uuid,
) -> Result<usize, UserEmbeddingError> {
diesel::delete(
user_embeddings::table
.filter(user_embeddings::user_id.eq(user_uuid))
.filter(user_embeddings::uuid.eq(embedding_uuid)),
)
.execute(conn)
.map_err(UserEmbeddingError::DatabaseError)
}
}
Loading
Loading