From ac53296e85b4ca2c0163847d23baacc29f8a9fa6 Mon Sep 17 00:00:00 2001 From: Petr Mironychev <9195189+Palm1r@users.noreply.github.com> Date: Sun, 28 Sep 2025 15:28:01 +0200 Subject: [PATCH] fix: Change behavior of cancel request *now cancel request cancel all requests --- ChatView/ClientInterface.cpp | 15 +++++++-------- LLMClientInterface.cpp | 19 ++++++++----------- llmcore/HttpClient.cpp | 11 ++++++++--- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/ChatView/ClientInterface.cpp b/ChatView/ClientInterface.cpp index 64cb974..233553d 100644 --- a/ChatView/ClientInterface.cpp +++ b/ChatView/ClientInterface.cpp @@ -151,18 +151,17 @@ void ClientInterface::clearMessages() void ClientInterface::cancelRequest() { - auto id = m_chatModel->lastMessageId(); - for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) { - if (it.value().originalRequest["id"].toString() == id) { - const RequestContext &ctx = it.value(); + const RequestContext &ctx = it.value(); + if (ctx.provider && ctx.provider->httpClient()) { ctx.provider->httpClient()->cancelRequest(it.key()); - - m_activeRequests.erase(it); - m_accumulatedResponses.remove(it.key()); - break; } } + + m_activeRequests.clear(); + m_accumulatedResponses.clear(); + + LOG_MESSAGE("All requests cancelled and state cleared"); } void ClientInterface::handleLLMResponse( diff --git a/LLMClientInterface.cpp b/LLMClientInterface.cpp index 92fae6d..b8795f9 100644 --- a/LLMClientInterface.cpp +++ b/LLMClientInterface.cpp @@ -115,19 +115,16 @@ void LLMClientInterface::sendData(const QByteArray &data) void LLMClientInterface::handleCancelRequest(const QJsonObject &request) { - QString id = request["id"].toString(); - - auto it = m_activeRequests.find(id); - if (it != m_activeRequests.end()) { + for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) { const RequestContext &ctx = it.value(); - - ctx.provider->httpClient()->cancelRequest(id); - - m_activeRequests.erase(it); - LOG_MESSAGE(QString("Request %1 cancelled successfully").arg(id)); - } else { - LOG_MESSAGE(QString("Request %1 not found").arg(id)); + if (ctx.provider && ctx.provider->httpClient()) { + ctx.provider->httpClient()->cancelRequest(it.key()); + } } + + m_activeRequests.clear(); + + LOG_MESSAGE("All requests cancelled and state cleared"); } void LLMClientInterface::handleInitialize(const QJsonObject &request) diff --git a/llmcore/HttpClient.cpp b/llmcore/HttpClient.cpp index d991e6e..a32de76 100644 --- a/llmcore/HttpClient.cpp +++ b/llmcore/HttpClient.cpp @@ -126,9 +126,14 @@ QString HttpClient::addActiveRequest(QNetworkReply *reply, const QString &reques void HttpClient::cancelRequest(const QString &requestId) { QMutexLocker locker(&m_mutex); - if (auto it = m_activeRequests.find(requestId); it != m_activeRequests.end()) { - it.value()->abort(); - it.value()->deleteLater(); + auto it = m_activeRequests.find(requestId); + if (it != m_activeRequests.end()) { + QNetworkReply *reply = it.value(); + if (reply) { + reply->disconnect(); + reply->abort(); + reply->deleteLater(); + } m_activeRequests.erase(it); LOG_MESSAGE(QString("HttpClient: Cancelled request: %1").arg(requestId)); }