Skip to content

Commit c24de4e

Browse files
committed
refactor: Decouple prompt template manager from their users
This makes it possible to test the user classes
1 parent b6f36d6 commit c24de4e

13 files changed

+179
-20
lines changed

ChatView/ChatRootView.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ namespace QodeAssist::Chat {
4545
ChatRootView::ChatRootView(QQuickItem *parent)
4646
: QQuickItem(parent)
4747
, m_chatModel(new ChatModel(this))
48-
, m_clientInterface(new ClientInterface(m_chatModel, this))
48+
, m_promptProvider(LLMCore::PromptTemplateManager::instance())
49+
, m_clientInterface(new ClientInterface(m_chatModel, &m_promptProvider, this))
4950
{
5051
m_isSyncOpenFiles = Settings::chatAssistantSettings().linkOpenFiles();
5152
connect(

ChatView/ChatRootView.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "ChatModel.hpp"
2525
#include "ClientInterface.hpp"
26+
#include "llmcore/PromptProviderChat.hpp"
2627
#include <coreplugin/editormanager/editormanager.h>
2728

2829
namespace QodeAssist::Chat {
@@ -99,6 +100,7 @@ public slots:
99100
QString getSuggestedFileName() const;
100101

101102
ChatModel *m_chatModel;
103+
LLMCore::PromptProviderChat m_promptProvider;
102104
ClientInterface *m_clientInterface;
103105
QString m_currentTemplate;
104106
QString m_recentFilePath;

ChatView/ClientInterface.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@
3636
#include "ContextManager.hpp"
3737
#include "GeneralSettings.hpp"
3838
#include "Logger.hpp"
39-
#include "PromptTemplateManager.hpp"
4039
#include "ProvidersManager.hpp"
4140

4241
namespace QodeAssist::Chat {
4342

44-
ClientInterface::ClientInterface(ChatModel *chatModel, QObject *parent)
43+
ClientInterface::ClientInterface(
44+
ChatModel *chatModel, LLMCore::IPromptProvider *promptProvider, QObject *parent)
4545
: QObject(parent)
4646
, m_requestHandler(new LLMCore::RequestHandler(this))
4747
, m_chatModel(chatModel)
48+
, m_promptProvider(promptProvider)
4849
{
4950
connect(
5051
m_requestHandler,
@@ -86,8 +87,7 @@ void ClientInterface::sendMessage(
8687
}
8788

8889
auto templateName = Settings::generalSettings().caTemplate();
89-
auto promptTemplate = LLMCore::PromptTemplateManager::instance().getChatTemplateByName(
90-
templateName);
90+
auto promptTemplate = m_promptProvider->getTemplateByName(templateName);
9191

9292
if (!promptTemplate) {
9393
LOG_MESSAGE(QString("No template found with name: %1").arg(templateName));

ChatView/ClientInterface.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "ChatModel.hpp"
2727
#include "RequestHandler.hpp"
28+
#include "llmcore/IPromptProvider.hpp"
2829

2930
namespace QodeAssist::Chat {
3031

@@ -33,7 +34,8 @@ class ClientInterface : public QObject
3334
Q_OBJECT
3435

3536
public:
36-
explicit ClientInterface(ChatModel *chatModel, QObject *parent = nullptr);
37+
explicit ClientInterface(
38+
ChatModel *chatModel, LLMCore::IPromptProvider *promptProvider, QObject *parent = nullptr);
3739
~ClientInterface();
3840

3941
void sendMessage(
@@ -53,8 +55,9 @@ class ClientInterface : public QObject
5355
QString getSystemPromptWithLinkedFiles(
5456
const QString &basePrompt, const QList<QString> &linkedFiles) const;
5557

56-
LLMCore::RequestHandler *m_requestHandler;
58+
LLMCore::IPromptProvider *m_promptProvider = nullptr;
5759
ChatModel *m_chatModel;
60+
LLMCore::RequestHandler *m_requestHandler;
5861
};
5962

6063
} // namespace QodeAssist::Chat

LLMClientInterface.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ namespace QodeAssist {
4040

4141
LLMClientInterface::LLMClientInterface(
4242
const Settings::GeneralSettings &generalSettings,
43-
const Settings::CodeCompletionSettings &completeSettings)
43+
const Settings::CodeCompletionSettings &completeSettings,
44+
LLMCore::IPromptProvider *promptProvider)
4445
: m_requestHandler(this)
4546
, m_generalSettings(generalSettings)
4647
, m_completeSettings(completeSettings)
48+
, m_promptProvider(promptProvider)
4749
{
4850
connect(
4951
&m_requestHandler,
@@ -175,8 +177,7 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
175177
auto templateName = !isPreset1Active ? m_generalSettings.ccTemplate()
176178
: m_generalSettings.ccPreset1Template();
177179

178-
auto promptTemplate = LLMCore::PromptTemplateManager::instance().getFimTemplateByName(
179-
templateName);
180+
auto promptTemplate = m_promptProvider->getTemplateByName(templateName);
180181

181182
if (!promptTemplate) {
182183
LOG_MESSAGE(QString("No template found with name: %1").arg(templateName));
@@ -281,8 +282,7 @@ void LLMClientInterface::sendCompletionToClient(
281282
auto templateName = !isPreset1Active ? m_generalSettings.ccTemplate()
282283
: m_generalSettings.ccPreset1Template();
283284

284-
auto promptTemplate = LLMCore::PromptTemplateManager::instance().getFimTemplateByName(
285-
templateName);
285+
auto promptTemplate = m_promptProvider->getTemplateByName(templateName);
286286

287287
QJsonObject position = request["params"].toObject()["doc"].toObject()["position"].toObject();
288288

LLMClientInterface.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <context/ProgrammingLanguage.hpp>
2626
#include <llmcore/ContextData.hpp>
27+
#include <llmcore/IPromptProvider.hpp>
2728
#include <llmcore/RequestHandler.hpp>
2829
#include <settings/CodeCompletionSettings.hpp>
2930
#include <settings/GeneralSettings.hpp>
@@ -40,7 +41,8 @@ class LLMClientInterface : public LanguageClient::BaseClientInterface
4041
public:
4142
LLMClientInterface(
4243
const Settings::GeneralSettings &generalSettings,
43-
const Settings::CodeCompletionSettings &completeSettings);
44+
const Settings::CodeCompletionSettings &completeSettings,
45+
LLMCore::IPromptProvider *promptProvider);
4446

4547
Utils::FilePath serverDeviceTemplate() const override;
4648

@@ -67,6 +69,7 @@ class LLMClientInterface : public LanguageClient::BaseClientInterface
6769

6870
const Settings::CodeCompletionSettings &m_completeSettings;
6971
const Settings::GeneralSettings &m_generalSettings;
72+
LLMCore::IPromptProvider *m_promptProvider = nullptr;
7073
LLMCore::RequestHandler m_requestHandler;
7174
QElapsedTimer m_completionTimer;
7275
QMap<QString, qint64> m_requestStartTimes;

QodeAssistClient.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ using namespace Core;
4444

4545
namespace QodeAssist {
4646

47-
QodeAssistClient::QodeAssistClient()
48-
: LanguageClient::Client(
49-
new LLMClientInterface(Settings::generalSettings(), Settings::codeCompletionSettings()))
47+
QodeAssistClient::QodeAssistClient(LLMCore::IPromptProvider *promptProvider)
48+
: LanguageClient::Client(new LLMClientInterface(
49+
Settings::generalSettings(), Settings::codeCompletionSettings(), promptProvider))
5050
, m_recentCharCount(0)
5151
{
5252
setName("Qode Assist");

QodeAssistClient.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424

2525
#pragma once
2626

27-
#include <languageclient/client.h>
28-
2927
#include "LSPCompletion.hpp"
28+
#include <languageclient/client.h>
29+
#include <llmcore/IPromptProvider.hpp>
3030

3131
namespace QodeAssist {
3232

3333
class QodeAssistClient : public LanguageClient::Client
3434
{
3535
public:
36-
explicit QodeAssistClient();
36+
explicit QodeAssistClient(LLMCore::IPromptProvider *promptProvider);
3737
~QodeAssistClient() override;
3838

3939
void openDocument(TextEditor::TextDocument *document) override;

llmcore/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ add_library(LLMCore STATIC
33
Provider.hpp
44
ProvidersManager.hpp ProvidersManager.cpp
55
ContextData.hpp
6+
IPromptProvider.hpp
7+
PromptProviderChat.hpp
8+
PromptProviderFim.hpp
69
PromptTemplate.hpp
710
PromptTemplateManager.hpp PromptTemplateManager.cpp
811
RequestConfig.hpp

llmcore/IPromptProvider.hpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2025 Povilas Kanapickas <[email protected]>
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 "PromptTemplate.hpp"
23+
#include <QString>
24+
25+
namespace QodeAssist::LLMCore {
26+
27+
class IPromptProvider
28+
{
29+
public:
30+
virtual ~IPromptProvider() = default;
31+
32+
virtual PromptTemplate *getTemplateByName(const QString &templateName) const = 0;
33+
34+
virtual QStringList templatesNames() const = 0;
35+
36+
virtual QStringList getTemplatesForProvider(ProviderID id) const = 0;
37+
};
38+
39+
} // namespace QodeAssist::LLMCore

llmcore/PromptProviderChat.hpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2025 Povilas Kanapickas <[email protected]>
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 "IPromptProvider.hpp"
23+
#include "PromptTemplate.hpp"
24+
#include "PromptTemplateManager.hpp"
25+
26+
namespace QodeAssist::LLMCore {
27+
28+
class PromptProviderChat : public IPromptProvider
29+
{
30+
public:
31+
explicit PromptProviderChat(PromptTemplateManager &templateManager)
32+
: m_templateManager(templateManager)
33+
{}
34+
35+
~PromptProviderChat() = default;
36+
37+
PromptTemplate *getTemplateByName(const QString &templateName) const override
38+
{
39+
return m_templateManager.getChatTemplateByName(templateName);
40+
}
41+
42+
QStringList templatesNames() const override { return m_templateManager.chatTemplatesNames(); }
43+
44+
QStringList getTemplatesForProvider(ProviderID id) const override
45+
{
46+
return m_templateManager.getChatTemplatesForProvider(id);
47+
}
48+
49+
private:
50+
PromptTemplateManager &m_templateManager;
51+
};
52+
53+
} // namespace QodeAssist::LLMCore

llmcore/PromptProviderFim.hpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2025 Povilas Kanapickas <[email protected]>
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 "IPromptProvider.hpp"
23+
#include "PromptTemplateManager.hpp"
24+
25+
namespace QodeAssist::LLMCore {
26+
27+
class PromptProviderFim : public IPromptProvider
28+
{
29+
public:
30+
explicit PromptProviderFim(PromptTemplateManager &templateManager)
31+
: m_templateManager(templateManager)
32+
{}
33+
34+
~PromptProviderFim() = default;
35+
36+
PromptTemplate *getTemplateByName(const QString &templateName) const override
37+
{
38+
return m_templateManager.getFimTemplateByName(templateName);
39+
}
40+
41+
QStringList templatesNames() const override { return m_templateManager.fimTemplatesNames(); }
42+
43+
QStringList getTemplatesForProvider(ProviderID id) const override
44+
{
45+
return m_templateManager.getFimTemplatesForProvider(id);
46+
}
47+
48+
private:
49+
PromptTemplateManager &m_templateManager;
50+
};
51+
52+
} // namespace QodeAssist::LLMCore

qodeassist.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "Version.hpp"
4747
#include "chat/ChatOutputPane.h"
4848
#include "chat/NavigationPanel.hpp"
49+
#include "llmcore/PromptProviderFim.hpp"
4950
#include "settings/GeneralSettings.hpp"
5051
#include "settings/ProjectSettingsPanel.hpp"
5152
#include "settings/SettingsConstants.hpp"
@@ -68,6 +69,7 @@ class QodeAssistPlugin final : public ExtensionSystem::IPlugin
6869
public:
6970
QodeAssistPlugin()
7071
: m_updater(new PluginUpdater(this))
72+
, m_promptProvider(LLMCore::PromptTemplateManager::instance())
7173
{}
7274

7375
~QodeAssistPlugin() final
@@ -135,7 +137,7 @@ class QodeAssistPlugin final : public ExtensionSystem::IPlugin
135137
void restartClient()
136138
{
137139
LanguageClient::LanguageClientManager::shutdownClient(m_qodeAssistClient);
138-
m_qodeAssistClient = new QodeAssistClient();
140+
m_qodeAssistClient = new QodeAssistClient(&m_promptProvider);
139141
}
140142

141143
bool delayedInitialize() final
@@ -176,6 +178,7 @@ class QodeAssistPlugin final : public ExtensionSystem::IPlugin
176178
}
177179

178180
QPointer<QodeAssistClient> m_qodeAssistClient;
181+
LLMCore::PromptProviderFim m_promptProvider;
179182
QPointer<Chat::ChatOutputPane> m_chatOutputPane;
180183
QPointer<Chat::NavigationPanel> m_navigationPanel;
181184
QPointer<PluginUpdater> m_updater;

0 commit comments

Comments
 (0)