diff --git a/chatview/ChatModel.cpp b/chatview/ChatModel.cpp index ce63a55..c5cf9f7 100644 --- a/chatview/ChatModel.cpp +++ b/chatview/ChatModel.cpp @@ -187,4 +187,9 @@ int ChatModel::tokensThreshold() const return settings.chatTokensThreshold(); } +QString ChatModel::lastMessageId() const +{ + return !m_messages.isEmpty() ? m_messages.last().id : ""; +} + } // namespace QodeAssist::Chat diff --git a/chatview/ChatModel.hpp b/chatview/ChatModel.hpp index df6c8b1..03343e1 100644 --- a/chatview/ChatModel.hpp +++ b/chatview/ChatModel.hpp @@ -66,6 +66,7 @@ public: int tokensThreshold() const; QString currentModel() const; + QString lastMessageId() const; signals: void totalTokensChanged(); diff --git a/chatview/ChatRootView.cpp b/chatview/ChatRootView.cpp index c04f01a..6450c51 100644 --- a/chatview/ChatRootView.cpp +++ b/chatview/ChatRootView.cpp @@ -60,6 +60,11 @@ void ChatRootView::copyToClipboard(const QString &text) QGuiApplication::clipboard()->setText(text); } +void ChatRootView::cancelRequest() +{ + m_clientInterface->cancelRequest(); +} + void ChatRootView::generateColors() { QColor baseColor = backgroundColor(); diff --git a/chatview/ChatRootView.hpp b/chatview/ChatRootView.hpp index 2f32ec6..f0d7272 100644 --- a/chatview/ChatRootView.hpp +++ b/chatview/ChatRootView.hpp @@ -52,6 +52,7 @@ public: public slots: void sendMessage(const QString &message) const; void copyToClipboard(const QString &text); + void cancelRequest(); signals: void chatModelChanged(); diff --git a/chatview/ClientInterface.cpp b/chatview/ClientInterface.cpp index e20fa55..2d1c1b6 100644 --- a/chatview/ClientInterface.cpp +++ b/chatview/ClientInterface.cpp @@ -56,6 +56,8 @@ ClientInterface::~ClientInterface() = default; void ClientInterface::sendMessage(const QString &message) { + cancelRequest(); + LOG_MESSAGE("Sending message: " + message); LOG_MESSAGE("chatProvider " + Settings::generalSettings().chatLlmProviders.stringValue()); LOG_MESSAGE("chatTemplate " + Settings::generalSettings().chatPrompts.stringValue()); @@ -74,6 +76,9 @@ void ClientInterface::sendMessage(const QString &message) providerRequest["stream"] = true; providerRequest["messages"] = m_chatModel->prepareMessagesForRequest(context); + if (!chatTemplate || !chatProvider) { + LOG_MESSAGE("Check settings, provider or template are not set"); + } chatTemplate->prepareRequest(providerRequest, context); chatProvider->prepareRequest(providerRequest, LLMCore::RequestType::Chat); @@ -89,7 +94,6 @@ void ClientInterface::sendMessage(const QString &message) QJsonObject request; request["id"] = QUuid::createUuid().toString(); - m_accumulatedResponse.clear(); m_chatModel->addMessage(message, ChatModel::ChatRole::User, ""); m_requestHandler->sendLLMRequest(config, request); } @@ -97,10 +101,15 @@ void ClientInterface::sendMessage(const QString &message) void ClientInterface::clearMessages() { m_chatModel->clear(); - m_accumulatedResponse.clear(); LOG_MESSAGE("Chat history cleared"); } +void ClientInterface::cancelRequest() +{ + auto id = m_chatModel->lastMessageId(); + m_requestHandler->cancelRequest(id); +} + void ClientInterface::handleLLMResponse(const QString &response, const QJsonObject &request, bool isComplete) diff --git a/chatview/ClientInterface.hpp b/chatview/ClientInterface.hpp index bd24f4b..4de4ea7 100644 --- a/chatview/ClientInterface.hpp +++ b/chatview/ClientInterface.hpp @@ -38,6 +38,7 @@ public: void sendMessage(const QString &message); void clearMessages(); + void cancelRequest(); signals: void errorOccurred(const QString &error); @@ -46,7 +47,6 @@ private: void handleLLMResponse(const QString &response, const QJsonObject &request, bool isComplete); LLMCore::RequestHandler *m_requestHandler; - QString m_accumulatedResponse; ChatModel *m_chatModel; }; diff --git a/chatview/qml/RootItem.qml b/chatview/qml/RootItem.qml index 29556a7..805d747 100644 --- a/chatview/qml/RootItem.qml +++ b/chatview/qml/RootItem.qml @@ -121,6 +121,15 @@ ChatRootView { text: qsTr("Send") onClicked: sendChatMessage() } + + Button { + id: stopButton + + Layout.alignment: Qt.AlignBottom + text: qsTr("Stop") + onClicked: root.cancelRequest() + } + Button { id: clearButton