Skip to content

Commit

Permalink
Fix crash after removing a model file previouly used
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbejar committed Oct 11, 2024
1 parent b0bf47c commit f1951d2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 36 deletions.
14 changes: 0 additions & 14 deletions src/chat/chat_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,19 +507,6 @@ impl WidgetMatchEvent for ChatPanel {
_ => {}
}

if let ChatPanelAction::UnloadIfActive(file_id) = action.cast() {
if store
.chats
.loaded_model
.as_ref()
.map_or(false, |file| file.id == file_id)
{
store.chats.eject_model().expect("Failed to eject model");
self.redraw(cx);
//self.unload_model(cx);
}
}

match action.cast() {
ChatLineAction::Delete(id) => {
store.chats.delete_chat_message(id);
Expand Down Expand Up @@ -953,7 +940,6 @@ impl ChatPanel {

#[derive(Clone, DefaultNone, Debug)]
pub enum ChatPanelAction {
UnloadIfActive(FileID),
NavigateToDiscover,
None,
}
Expand Down
4 changes: 0 additions & 4 deletions src/chat/model_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,6 @@ impl ModelSelector {
}
}

// fn options_to_display(store: &Store) -> bool {
// !store.downloads.downloaded_files.is_empty()
// }

fn no_active_model(store: &Store) -> bool {
let chat_entity = store
.chats
Expand Down
12 changes: 12 additions & 0 deletions src/data/chats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ impl Chats {
Ok(())
}

pub fn remove_file_from_associated_entity(&mut self, file_id: &FileID) {
for chat in &self.saved_chats {
let mut chat = chat.borrow_mut();
if let Some(ChatEntity::ModelFile(chat_file_id)) = &chat.associated_entity {
if chat_file_id == file_id {
chat.associated_entity = None;
chat.save();
}
}
}
}

/// Get the file id to use with this chat, or the loaded file id as a fallback.
/// The fallback is used if the chat does not have a file id set, or, if it has
/// one but references a no longer existing (deleted) file.
Expand Down
34 changes: 21 additions & 13 deletions src/data/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,16 @@ impl Store {
.send(TestServer(tx.clone()))
.expect("failed to update MoFa server address");

std::thread::spawn(move || {
match rx.recv() {
Ok(TestServerResponse::Success(server_address)) => {
Cx::post_action(MoFaTestServerAction::Success(server_address));
}
Ok(TestServerResponse::Failure(server_address)) => {
Cx::post_action(MoFaTestServerAction::Failure(Some(server_address)));
}
Err(e) => {
println!("Error receiving response from MoFa backend: {:?}", e);
Cx::post_action(MoFaTestServerAction::Failure(None));
}
std::thread::spawn(move || match rx.recv() {
Ok(TestServerResponse::Success(server_address)) => {
Cx::post_action(MoFaTestServerAction::Success(server_address));
}
Ok(TestServerResponse::Failure(server_address)) => {
Cx::post_action(MoFaTestServerAction::Failure(Some(server_address)));
}
Err(e) => {
println!("Error receiving response from MoFa backend: {:?}", e);
Cx::post_action(MoFaTestServerAction::Failure(None));
}
});
}
Expand Down Expand Up @@ -330,6 +328,16 @@ impl Store {
}

pub fn delete_file(&mut self, file_id: FileID) -> Result<()> {
if self
.chats
.loaded_model
.as_ref()
.map_or(false, |file| file.id == file_id)
{
self.chats.eject_model().expect("Failed to eject model");
}

self.chats.remove_file_from_associated_entity(&file_id);
self.downloads.delete_file(file_id.clone())?;
self.search
.update_downloaded_file_in_search_results(&file_id, false);
Expand Down Expand Up @@ -408,4 +416,4 @@ impl Store {
// support having no chat selected
self.init_current_chat();
}
}
}
6 changes: 1 addition & 5 deletions src/my_models/delete_model_modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,7 @@ impl WidgetMatchEvent for DeleteModelModal {
.clicked(actions)
{
let store = scope.data.get_mut::<Store>().unwrap();
cx.widget_action(
widget_uid,
&scope.path,
ChatPanelAction::UnloadIfActive(self.file_id.clone()),
);

store
.delete_file(self.file_id.clone())
.expect("Failed to delete file");
Expand Down

0 comments on commit f1951d2

Please sign in to comment.