Skip to content

Commit

Permalink
add ability to truncate profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
aprxi committed Aug 14, 2024
1 parent 58fc138 commit 22b8d0e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lumni/src/apps/builtin/llm/prompt/src/chat/db/user_profile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,29 @@ impl UserProfileDbHandler {
_ => JsonValue::Null,
}
}

pub async fn truncate_and_vacuum(&self) -> Result<(), ApplicationError> {
let mut db = self.db.lock().await;
db.process_queue_with_result(|tx| {
// Disable foreign key constraints temporarily
tx.execute("PRAGMA foreign_keys = OFF", [])?;

// NOTE: encryption_keys is currently only used in user_profiles, if this changes (e.g. keys used to encrypt conversations) we should add a check so we only delete unused keys -- for now, just delete everything
tx.execute_batch(
"
DELETE FROM user_profiles;
DELETE FROM encryption_keys;
",
)?;

// Re-enable foreign key constraints
tx.execute("PRAGMA foreign_keys = ON", [])?;

Ok(())
})?;

// Vacuum the database to reclaim unused space
db.vacuum()?;
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command};
use clap::{Arg, ArgAction, ArgMatches, Command};
use lumni::api::error::ApplicationError;

use super::ConversationDatabase;
Expand Down
36 changes: 36 additions & 0 deletions lumni/src/apps/builtin/llm/prompt/src/cli/subcommands/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn create_profile_subcommand() -> Command {
.subcommand(create_edit_subcommand())
.subcommand(create_key_subcommand())
.subcommand(create_export_subcommand())
.subcommand(create_truncate_subcommand())
}

fn create_list_subcommand() -> Command {
Expand Down Expand Up @@ -170,6 +171,18 @@ fn create_export_subcommand() -> Command {
)
}

fn create_truncate_subcommand() -> Command {
Command::new("truncate")
.about("Remove all profiles and related data")
.arg(
Arg::new("confirm")
.short('y')
.long("yes")
.help("Confirm the truncation without prompting")
.action(ArgAction::SetTrue),
)
}

pub async fn handle_profile_subcommand(
profile_matches: &ArgMatches,
db_handler: &mut UserProfileDbHandler,
Expand Down Expand Up @@ -429,6 +442,29 @@ pub async fn handle_profile_subcommand(
)?;
}

Some(("truncate", truncate_matches)) => {
if truncate_matches.get_flag("confirm") {
db_handler.truncate_and_vacuum().await?;
println!("All profiles and related data have been removed.");
} else {
println!(
"Are you sure you want to remove all profiles and related \
data? This action cannot be undone."
);
println!("Type 'yes' to confirm or any other input to cancel:");
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
if input.trim().eq_ignore_ascii_case("yes") {
db_handler.truncate_and_vacuum().await?;
println!(
"All profiles and related data have been removed."
);
} else {
println!("Operation cancelled.");
}
}
}

_ => {
create_profile_subcommand().print_help()?;
}
Expand Down

0 comments on commit 22b8d0e

Please sign in to comment.