Skip to content

Commit a218064

Browse files
authored
refactor: Introduce base class for RequestHandler (#125)
This will make it possible to write a mock implementation.
1 parent 13cd12b commit a218064

8 files changed

+90
-15
lines changed

LLMClientInterface.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <QNetworkAccessManager>
2424
#include <QNetworkReply>
2525

26-
#include <llmcore/RequestConfig.hpp>
2726
#include <texteditor/textdocument.h>
2827

2928
#include "CodeHandler.hpp"
@@ -35,6 +34,7 @@
3534
#include "logger/Logger.hpp"
3635
#include "settings/CodeCompletionSettings.hpp"
3736
#include "settings/GeneralSettings.hpp"
37+
#include <llmcore/RequestConfig.hpp>
3838

3939
namespace QodeAssist {
4040

@@ -43,12 +43,13 @@ LLMClientInterface::LLMClientInterface(
4343
const Settings::CodeCompletionSettings &completeSettings,
4444
LLMCore::IProviderRegistry &providerRegistry,
4545
LLMCore::IPromptProvider *promptProvider,
46+
LLMCore::RequestHandlerBase &requestHandler,
4647
IRequestPerformanceLogger &performanceLogger)
47-
: m_requestHandler(this)
48-
, m_generalSettings(generalSettings)
48+
: m_generalSettings(generalSettings)
4949
, m_completeSettings(completeSettings)
5050
, m_providerRegistry(providerRegistry)
5151
, m_promptProvider(promptProvider)
52+
, m_requestHandler(requestHandler)
5253
, m_performanceLogger(performanceLogger)
5354
{
5455
connect(

LLMClientInterface.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class LLMClientInterface : public LanguageClient::BaseClientInterface
4646
const Settings::CodeCompletionSettings &completeSettings,
4747
LLMCore::IProviderRegistry &providerRegistry,
4848
LLMCore::IPromptProvider *promptProvider,
49+
LLMCore::RequestHandlerBase &requestHandler,
4950
IRequestPerformanceLogger &performanceLogger);
5051

5152
Utils::FilePath serverDeviceTemplate() const override;
@@ -75,7 +76,7 @@ class LLMClientInterface : public LanguageClient::BaseClientInterface
7576
const Settings::GeneralSettings &m_generalSettings;
7677
LLMCore::IPromptProvider *m_promptProvider = nullptr;
7778
LLMCore::IProviderRegistry &m_providerRegistry;
78-
LLMCore::RequestHandler m_requestHandler;
79+
LLMCore::RequestHandlerBase &m_requestHandler;
7980
IRequestPerformanceLogger &m_performanceLogger;
8081
QElapsedTimer m_completionTimer;
8182
};

llmcore/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library(LLMCore STATIC
1010
PromptTemplate.hpp
1111
PromptTemplateManager.hpp PromptTemplateManager.cpp
1212
RequestConfig.hpp
13+
RequestHandlerBase.hpp RequestHandlerBase.cpp
1314
RequestHandler.hpp RequestHandler.cpp
1415
OllamaMessage.hpp OllamaMessage.cpp
1516
OpenAIMessage.hpp OpenAIMessage.cpp

llmcore/RequestHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace QodeAssist::LLMCore {
2727

2828
RequestHandler::RequestHandler(QObject *parent)
29-
: QObject(parent)
29+
: RequestHandlerBase(parent)
3030
, m_manager(new QNetworkAccessManager(this))
3131
{}
3232

llmcore/RequestHandler.hpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,20 @@
2424
#include <QObject>
2525

2626
#include "RequestConfig.hpp"
27+
#include "RequestHandlerBase.hpp"
2728

2829
class QNetworkReply;
2930

3031
namespace QodeAssist::LLMCore {
3132

32-
class RequestHandler : public QObject
33+
class RequestHandler : public RequestHandlerBase
3334
{
34-
Q_OBJECT
35-
3635
public:
3736
explicit RequestHandler(QObject *parent = nullptr);
3837

39-
void sendLLMRequest(const LLMConfig &config, const QJsonObject &request);
38+
void sendLLMRequest(const LLMConfig &config, const QJsonObject &request) override;
39+
bool cancelRequest(const QString &id) override;
4040
void handleLLMResponse(QNetworkReply *reply, const QJsonObject &request, const LLMConfig &config);
41-
bool cancelRequest(const QString &id);
42-
43-
signals:
44-
void completionReceived(const QString &completion, const QJsonObject &request, bool isComplete);
45-
void requestFinished(const QString &requestId, bool success, const QString &errorString);
46-
void requestCancelled(const QString &id);
4741

4842
private:
4943
QNetworkAccessManager *m_manager;

llmcore/RequestHandlerBase.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2024 Petr Mironychev
3+
*
4+
* This file is part of QodeAssist.
5+
*
6+
* QodeAssist is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* QodeAssist is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "RequestHandlerBase.hpp"
21+
22+
namespace QodeAssist::LLMCore {
23+
24+
RequestHandlerBase::RequestHandlerBase(QObject *parent)
25+
: QObject(parent)
26+
{}
27+
28+
RequestHandlerBase::~RequestHandlerBase() = default;
29+
30+
} // namespace QodeAssist::LLMCore

llmcore/RequestHandlerBase.hpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2024 Petr Mironychev
3+
*
4+
* This file is part of QodeAssist.
5+
*
6+
* QodeAssist is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* QodeAssist is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#pragma once
21+
22+
#include "RequestConfig.hpp"
23+
#include <QJsonObject>
24+
#include <QObject>
25+
26+
namespace QodeAssist::LLMCore {
27+
28+
class RequestHandlerBase : public QObject
29+
{
30+
Q_OBJECT
31+
32+
public:
33+
explicit RequestHandlerBase(QObject *parent = nullptr);
34+
~RequestHandlerBase() override;
35+
36+
virtual void sendLLMRequest(const LLMConfig &config, const QJsonObject &request) = 0;
37+
virtual bool cancelRequest(const QString &id) = 0;
38+
39+
signals:
40+
void completionReceived(const QString &completion, const QJsonObject &request, bool isComplete);
41+
void requestFinished(const QString &requestId, bool success, const QString &errorString);
42+
void requestCancelled(const QString &id);
43+
};
44+
45+
} // namespace QodeAssist::LLMCore

qodeassist.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class QodeAssistPlugin final : public ExtensionSystem::IPlugin
7272
QodeAssistPlugin()
7373
: m_updater(new PluginUpdater(this))
7474
, m_promptProvider(LLMCore::PromptTemplateManager::instance())
75+
, m_requestHandler(this)
7576
{}
7677

7778
~QodeAssistPlugin() final
@@ -144,6 +145,7 @@ class QodeAssistPlugin final : public ExtensionSystem::IPlugin
144145
Settings::codeCompletionSettings(),
145146
LLMCore::ProvidersManager::instance(),
146147
&m_promptProvider,
148+
m_requestHandler,
147149
m_performanceLogger));
148150
}
149151

@@ -186,6 +188,7 @@ class QodeAssistPlugin final : public ExtensionSystem::IPlugin
186188

187189
QPointer<QodeAssistClient> m_qodeAssistClient;
188190
LLMCore::PromptProviderFim m_promptProvider;
191+
LLMCore::RequestHandler m_requestHandler{this};
189192
RequestPerformanceLogger m_performanceLogger;
190193
QPointer<Chat::ChatOutputPane> m_chatOutputPane;
191194
QPointer<Chat::NavigationPanel> m_navigationPanel;

0 commit comments

Comments
 (0)