fix: Change behavior of cancel request

*now cancel request cancel all requests
This commit is contained in:
Petr Mironychev
2025-09-28 15:28:01 +02:00
parent c688cba3dd
commit ac53296e85
3 changed files with 23 additions and 22 deletions

View File

@ -151,18 +151,17 @@ void ClientInterface::clearMessages()
void ClientInterface::cancelRequest() void ClientInterface::cancelRequest()
{ {
auto id = m_chatModel->lastMessageId();
for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) { 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()); 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( void ClientInterface::handleLLMResponse(

View File

@ -115,19 +115,16 @@ void LLMClientInterface::sendData(const QByteArray &data)
void LLMClientInterface::handleCancelRequest(const QJsonObject &request) void LLMClientInterface::handleCancelRequest(const QJsonObject &request)
{ {
QString id = request["id"].toString(); for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) {
auto it = m_activeRequests.find(id);
if (it != m_activeRequests.end()) {
const RequestContext &ctx = it.value(); const RequestContext &ctx = it.value();
if (ctx.provider && ctx.provider->httpClient()) {
ctx.provider->httpClient()->cancelRequest(id); ctx.provider->httpClient()->cancelRequest(it.key());
}
m_activeRequests.erase(it);
LOG_MESSAGE(QString("Request %1 cancelled successfully").arg(id));
} else {
LOG_MESSAGE(QString("Request %1 not found").arg(id));
} }
m_activeRequests.clear();
LOG_MESSAGE("All requests cancelled and state cleared");
} }
void LLMClientInterface::handleInitialize(const QJsonObject &request) void LLMClientInterface::handleInitialize(const QJsonObject &request)

View File

@ -126,9 +126,14 @@ QString HttpClient::addActiveRequest(QNetworkReply *reply, const QString &reques
void HttpClient::cancelRequest(const QString &requestId) void HttpClient::cancelRequest(const QString &requestId)
{ {
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
if (auto it = m_activeRequests.find(requestId); it != m_activeRequests.end()) { auto it = m_activeRequests.find(requestId);
it.value()->abort(); if (it != m_activeRequests.end()) {
it.value()->deleteLater(); QNetworkReply *reply = it.value();
if (reply) {
reply->disconnect();
reply->abort();
reply->deleteLater();
}
m_activeRequests.erase(it); m_activeRequests.erase(it);
LOG_MESSAGE(QString("HttpClient: Cancelled request: %1").arg(requestId)); LOG_MESSAGE(QString("HttpClient: Cancelled request: %1").arg(requestId));
} }