Skip to content

Commit

Permalink
wip - adding ability to change provider for existing profile
Browse files Browse the repository at this point in the history
  • Loading branch information
aprxi committed Sep 25, 2024
1 parent 72cb3ab commit 88a8610
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 14 deletions.
5 changes: 4 additions & 1 deletion lumni/src/apps/builtin/llm/prompt/src/chat/prompt/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ impl PromptInstructionBuilder {
}
Ok(_) => None,
Err(e) => {
log::warn!("Failed to check if conversation is equal: {}", e);
log::warn!(
"Failed to check if conversation is equal: {}",
e
);
None
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,19 +462,58 @@ impl ConfigItemManager {
return Ok(WindowMode::Modal(ModalEvent::UpdateUI));
}

if self.settings_editor.edit_mode == EditMode::NotEditing
&& (key_event.code == KeyCode::Left
|| key_event.code == KeyCode::Char('q')
|| key_event.code == KeyCode::Esc
|| key_event.code == KeyCode::Tab)
{
*tab_focus = TabFocus::List;
return Ok(WindowMode::Modal(ModalEvent::UpdateUI));
if self.settings_editor.edit_mode == EditMode::NotEditing {
match key_event.code {
KeyCode::Char('e') => {
if let Some(current_field) = self
.settings_editor
.get_current_field()
.split('.')
.next()
{
if current_field == "__section" {
return self
.start_provider_selection(tab_focus)
.await;
}
}
}
KeyCode::Left
| KeyCode::Char('q')
| KeyCode::Esc
| KeyCode::Tab => {
*tab_focus = TabFocus::List;
return Ok(WindowMode::Modal(ModalEvent::UpdateUI));
}
_ => {}
}
}

Ok(WindowMode::Modal(ModalEvent::UpdateUI))
}

async fn start_provider_selection(
&mut self,
tab_focus: &mut TabFocus,
) -> Result<WindowMode, ApplicationError> {
if let Some(ConfigItem::UserProfile(profile)) =
self.list.get_selected_item()
{
let mut creator =
ProfileCreator::new(self.db_handler.clone()).await?;
creator.set_editing_mode(profile.clone());

self.creator = Some(Box::new(creator));
*tab_focus = TabFocus::Creation;

Ok(WindowMode::Modal(ModalEvent::UpdateUI))
} else {
Err(ApplicationError::InvalidState(
"No profile selected".to_string(),
))
}
}

async fn handle_creation_input(
&mut self,
key_event: KeyEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct ProfileCreator {
selected_provider_index: usize,
sub_part_creation_state: SubPartCreationState,
text_area: Option<TextArea<ReadDocument>>,
editing_profile: Option<UserProfile>,
}

impl ProfileCreator {
Expand All @@ -54,9 +55,16 @@ impl ProfileCreator {
selected_provider_index: 0,
sub_part_creation_state: SubPartCreationState::NotCreating,
text_area: None,
editing_profile: None,
})
}

pub fn set_editing_mode(&mut self, profile: UserProfile) {
self.editing_profile = Some(profile.clone());
self.new_profile_name = profile.name.clone();
self.creation_step = ProfileCreationStep::SelectProvider;
}

pub fn render_creator(&mut self, f: &mut Frame, area: Rect) {
match self.sub_part_creation_state {
SubPartCreationState::NotCreating => match self.creation_step {
Expand Down Expand Up @@ -157,6 +165,46 @@ impl ProfileCreator {
Ok(CreatorAction::CreateItem)
}

pub async fn update_profile(
&mut self,
) -> Result<CreatorAction<UserProfile>, ApplicationError> {
if let (Some(profile), Some(ConfigItem::DatabaseConfig(config))) =
(&self.editing_profile, &self.selected_provider)
{
let mut profile_settings = json!({
"name": self.new_profile_name,
"provider_type": config.section
});

if let Ok(section_configuration) = self
.db_handler
.get_configuration_parameters(config, MaskMode::Unmask)
.await
{
let section_key = format!("__section.{}", config.section);
profile_settings[section_key] = section_configuration;
}

self.db_handler
.update_configuration_item(&profile.into(), &profile_settings)
.await?;

// Fetch the updated profile
let updated_profile =
self.db_handler.get_profile_by_id(profile.id).await?.ok_or(
ApplicationError::InvalidState(
"Updated profile not found".to_string(),
),
)?;

Ok(CreatorAction::Finish(updated_profile))
} else {
Err(ApplicationError::InvalidState(
"No profile or provider selected for update".to_string(),
))
}
}

fn render_select_provider(&self, f: &mut Frame, area: Rect) {
let mut items: Vec<ListItem> = self
.provider_configs
Expand Down Expand Up @@ -319,7 +367,7 @@ impl ProfileCreator {
self.handle_select_provider(input).await
}
ProfileCreationStep::ConfirmCreate => {
self.handle_confirm_create(input)
self.handle_confirm_create(input).await
}
ProfileCreationStep::CreatingProfile => {
Ok(CreatorAction::Continue)
Expand Down Expand Up @@ -388,15 +436,19 @@ impl ProfileCreator {
self.text_area = Some(TextArea::with_read_document(Some(text_lines)));
}

fn handle_confirm_create(
async fn handle_confirm_create(
&mut self,
input: KeyEvent,
) -> Result<CreatorAction<UserProfile>, ApplicationError> {
match input.code {
KeyCode::Enter => {
self.text_area = None;
self.creation_step = ProfileCreationStep::CreatingProfile;
Ok(CreatorAction::CreateItem)
if self.editing_profile.is_some() {
self.update_profile().await
} else {
self.creation_step = ProfileCreationStep::CreatingProfile;
Ok(CreatorAction::CreateItem)
}
}
KeyCode::Esc => {
self.text_area = None;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod creator;

pub use creator::{ProfileCreationStep, ProfileCreator, SubPartCreationState};
pub use creator::ProfileCreator;

use super::*;

0 comments on commit 88a8610

Please sign in to comment.