From 5f372bd881f5b0a13bb79def1e5d275f5b97bb9f Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Mon, 8 May 2023 20:51:03 -0400 Subject: [PATCH] Gracefully handle when we have a previous chat where the model that it used has gone away. --- chat.cpp | 1 + chat.h | 1 + chatlistmodel.cpp | 1 + chatlistmodel.h | 9 +++++++++ chatllm.cpp | 3 ++- chatllm.h | 1 + 6 files changed, 15 insertions(+), 1 deletion(-) diff --git a/chat.cpp b/chat.cpp index 75cad7eb..c1147bfa 100644 --- a/chat.cpp +++ b/chat.cpp @@ -22,6 +22,7 @@ Chat::Chat(QObject *parent) connect(m_llmodel, &ChatLLM::responseStarted, this, &Chat::responseStarted, Qt::QueuedConnection); connect(m_llmodel, &ChatLLM::responseStopped, this, &Chat::responseStopped, Qt::QueuedConnection); connect(m_llmodel, &ChatLLM::modelNameChanged, this, &Chat::handleModelNameChanged, Qt::QueuedConnection); + connect(m_llmodel, &ChatLLM::modelLoadingError, this, &Chat::modelLoadingError, Qt::QueuedConnection); connect(m_llmodel, &ChatLLM::recalcChanged, this, &Chat::handleRecalculating, Qt::QueuedConnection); connect(m_llmodel, &ChatLLM::generatedNameChanged, this, &Chat::generatedNameChanged, Qt::QueuedConnection); diff --git a/chat.h b/chat.h index 8e46b311..4ec97ee6 100644 --- a/chat.h +++ b/chat.h @@ -82,6 +82,7 @@ Q_SIGNALS: void reloadModelRequested(const QString &modelName); void generateNameRequested(); void modelListChanged(); + void modelLoadingError(const QString &error); private Q_SLOTS: void handleResponseChanged(); diff --git a/chatlistmodel.cpp b/chatlistmodel.cpp index 1fbd0110..3fd2246f 100644 --- a/chatlistmodel.cpp +++ b/chatlistmodel.cpp @@ -213,6 +213,7 @@ void ChatListModel::restoreChat(Chat *chat) { chat->setParent(this); connect(chat, &Chat::nameChanged, this, &ChatListModel::nameChanged); + connect(chat, &Chat::modelLoadingError, this, &ChatListModel::handleModelLoadingError); if (m_dummyChat) { beginResetModel(); diff --git a/chatlistmodel.h b/chatlistmodel.h index 1dd05f3b..c695e05d 100644 --- a/chatlistmodel.h +++ b/chatlistmodel.h @@ -105,6 +105,8 @@ public: this, &ChatListModel::newChatCountChanged); connect(m_newChat, &Chat::nameChanged, this, &ChatListModel::nameChanged); + connect(m_newChat, &Chat::modelLoadingError, + this, &ChatListModel::handleModelLoadingError); setCurrentChat(m_newChat); } @@ -204,6 +206,13 @@ private Q_SLOTS: emit dataChanged(index, index, {NameRole}); } + void handleModelLoadingError(const QString &error) + { + Chat *chat = qobject_cast(sender()); + qWarning() << "ERROR:" << qPrintable(error) << "id" << chat->id(); + removeChat(chat); + } + void printChats() { for (auto c : m_chats) { diff --git a/chatllm.cpp b/chatllm.cpp index 7e95bf0b..e5033679 100644 --- a/chatllm.cpp +++ b/chatllm.cpp @@ -127,7 +127,8 @@ bool ChatLLM::loadModel(const QString &modelName) else emit sendModelLoaded(); } else { - qWarning() << "ERROR: Could not find model at" << filePath; + const QString error = QString("Could not find model %1").arg(modelName); + emit modelLoadingError(error); } if (m_llmodel) diff --git a/chatllm.h b/chatllm.h index 9e0b932f..bb488b16 100644 --- a/chatllm.h +++ b/chatllm.h @@ -58,6 +58,7 @@ public Q_SLOTS: Q_SIGNALS: void isModelLoadedChanged(); + void modelLoadingError(const QString &error); void responseChanged(); void responseStarted(); void responseStopped();