Skip to content

Commit

Permalink
Add stopping chat requests and button
Browse files Browse the repository at this point in the history
  • Loading branch information
Palm1r committed Oct 16, 2024
1 parent 2257e6e commit 9c2516c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions chatview/ChatModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions chatview/ChatModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ChatModel : public QAbstractListModel
int tokensThreshold() const;

QString currentModel() const;
QString lastMessageId() const;

signals:
void totalTokensChanged();
Expand Down
5 changes: 5 additions & 0 deletions chatview/ChatRootView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions chatview/ChatRootView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ChatRootView : public QQuickItem
public slots:
void sendMessage(const QString &message) const;
void copyToClipboard(const QString &text);
void cancelRequest();

signals:
void chatModelChanged();
Expand Down
13 changes: 11 additions & 2 deletions chatview/ClientInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);

Expand All @@ -89,18 +94,22 @@ 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);
}

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)
Expand Down
2 changes: 1 addition & 1 deletion chatview/ClientInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ClientInterface : public QObject

void sendMessage(const QString &message);
void clearMessages();
void cancelRequest();

signals:
void errorOccurred(const QString &error);
Expand All @@ -46,7 +47,6 @@ class ClientInterface : public QObject
void handleLLMResponse(const QString &response, const QJsonObject &request, bool isComplete);

LLMCore::RequestHandler *m_requestHandler;
QString m_accumulatedResponse;
ChatModel *m_chatModel;
};

Expand Down
9 changes: 9 additions & 0 deletions chatview/qml/RootItem.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9c2516c

Please sign in to comment.