Skip to content

Commit

Permalink
remove old endpoint modal, wip - profiles modal, add ability to brows…
Browse files Browse the repository at this point in the history
…e through keys
  • Loading branch information
aprxi committed Aug 15, 2024
1 parent f1907d8 commit 625dd0f
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ impl EncryptionHandler {
let handler = EncryptionHandler::new_from_path(private_key_path)?
.ok_or_else(|| {
ApplicationError::EncryptionError(EncryptionError::InvalidKey(
"No encryption handler available".to_string(),
"Encryption handler required to get private key hash"
.to_string(),
))
})?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ impl UserProfileDbHandler {
.map_err(ApplicationError::from)
}

pub async fn get_profile_list(
&self,
) -> Result<Vec<String>, ApplicationError> {
let mut db = self.db.lock().await;
db.process_queue_with_result(|tx| {
let mut stmt =
tx.prepare("SELECT name FROM user_profiles ORDER BY name ASC")?;
let profiles = stmt
.query_map([], |row| row.get(0))?
.collect::<Result<Vec<String>, _>>()
.map_err(|e| DatabaseOperationError::SqliteError(e))?;
Ok(profiles)
})
.map_err(|e| match e {
DatabaseOperationError::SqliteError(sqlite_err) => {
ApplicationError::DatabaseError(sqlite_err.to_string())
}
DatabaseOperationError::ApplicationError(app_err) => app_err,
})
}

pub async fn register_encryption_key(
&self,
name: &str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl UserProfileDbHandler {
"encryption_key": encryption_key,
}))
} else {
eprintln!("No encryption handler available");
eprintln!("Encryption handler required to encrypt value");
Ok(JsonValue::String(content.to_string()))
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ impl UserProfileDbHandler {
} else {
Err(ApplicationError::EncryptionError(
EncryptionError::InvalidKey(
"No encryption handler available".to_string(),
"Encryption handler required to decrypt value".to_string(),
),
))
}
Expand Down Expand Up @@ -114,7 +114,7 @@ impl UserProfileDbHandler {
} else {
Err(ApplicationError::EncryptionError(
EncryptionError::InvalidKey(
"No encryption handler available".to_string(),
"Encryption handler required to validate hash".to_string(),
),
))
}
Expand Down
17 changes: 14 additions & 3 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 @@ -20,13 +20,13 @@ pub struct UserProfileDbHandler {
encryption_handler: Option<Arc<EncryptionHandler>>,
}

#[derive(PartialEq, Eq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum EncryptionMode {
Encrypt,
Decrypt,
}

#[derive(PartialEq, Eq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MaskMode {
Mask,
Unmask,
Expand Down Expand Up @@ -56,7 +56,9 @@ impl UserProfileDbHandler {
// If profile is not yet set, return error as we need to know the profile to validate against existing encryption handler
let profile_name = self.profile_name.as_ref().ok_or_else(|| {
ApplicationError::InvalidInput(
"Profile name must be defined before setting encryption handler".to_string(),
"Profile name must be defined before setting encryption \
handler"
.to_string(),
)
})?;

Expand Down Expand Up @@ -117,6 +119,15 @@ impl UserProfileDbHandler {
Ok(())
}

pub fn set_profile_with_encryption_handler(
&mut self,
profile_name: String,
encryption_handler: Arc<EncryptionHandler>,
) -> Result<(), ApplicationError> {
self.set_profile_name(profile_name);
self.set_encryption_handler(encryption_handler)
}

pub async fn export_profile_settings(
&mut self,
profile_name: &str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ impl UserProfileDbHandler {
// Set new encryption handler outside the closure
if let Some(new_encryption_handler) = created_encryption_handler {
// use method as it protects against overwriting existing encryption configuration
self.set_encryption_handler(Arc::new(new_encryption_handler))?;
self.set_profile_with_encryption_handler(
profile_name.to_string(),
Arc::new(new_encryption_handler),
)?;
} else {
return Err(ApplicationError::InvalidInput(
"Failed to create encryption handler".to_string(),
Expand Down Expand Up @@ -188,6 +191,11 @@ impl UserProfileDbHandler {
profile_name: &str,
mask_mode: MaskMode,
) -> Result<JsonValue, ApplicationError> {
log::debug!(
"Getting settings for profile: {} ({:?})",
profile_name,
mask_mode
);
let (json_string, key_hash, key_path): (String, String, String) = {
let mut db = self.db.lock().await;
db.process_queue_with_result(|tx| {
Expand All @@ -204,27 +212,31 @@ impl UserProfileDbHandler {
.map_err(DatabaseOperationError::SqliteError)
})?
};
self.profile_name = Some(profile_name.to_string());
if self.encryption_handler.is_none() {
if mask_mode == MaskMode::Unmask && self.encryption_handler.is_none() {
// encryption handled required for decryption
let encryption_handler =
EncryptionHandler::new_from_path(&PathBuf::from(&key_path))?
.ok_or_else(|| {
ApplicationError::InvalidInput(
"Failed to load encryption handler".to_string(),
)
})?;
// use method as it protects against overwriting existing encryption configuration
self.set_encryption_handler(Arc::new(encryption_handler))?;
self.set_profile_with_encryption_handler(
profile_name.to_string(),
Arc::new(encryption_handler),
)?;
self.verify_encryption_key_hash(&key_hash)?;
}
self.verify_encryption_key_hash(&key_hash)?;
let settings: JsonValue =
serde_json::from_str(&json_string).map_err(|e| {
ApplicationError::InvalidInput(format!("Invalid JSON: {}", e))
})?;
self.process_settings_with_metadata(
let r = self.process_settings_with_metadata(
&settings,
EncryptionMode::Decrypt,
mask_mode,
)
);
eprintln!("get_profile_settings: {:?}", r);
r
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn handle_command_line_event(
// double-colon opens Modal (Config) window
app_ui.command_line.text_empty();
app_ui.command_line.set_status_inactive();
Ok(Some(WindowEvent::Modal(ModalWindowType::SelectEndpoint)))
Ok(Some(WindowEvent::Modal(ModalWindowType::ProfileEdit)))
}
_ => handle_text_window_event(
key_track,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ pub fn process_leader_key(key_track: &mut KeyTrack) -> Option<WindowEvent> {
MatchOutcome::FullMatch(cmd) => {
// NOTE: should match define_commands! macro
let window_event = match cmd.as_str() {
"pe" => Some(WindowEvent::Modal(
ModalWindowType::ProfileEdit,
)),
"pe" => {
Some(WindowEvent::Modal(ModalWindowType::ProfileEdit))
}
"pc" => Some(WindowEvent::Modal(
ModalWindowType::ConversationList(None),
)),
Expand Down
120 changes: 0 additions & 120 deletions lumni/src/apps/builtin/llm/prompt/src/tui/modals/endpoint/mod.rs

This file was deleted.

Loading

0 comments on commit 625dd0f

Please sign in to comment.