From 61f1f0ae4f9fc62190017171fb4b23fba15c6aa1 Mon Sep 17 00:00:00 2001 From: Petr Mironychev <9195189+Palm1r@users.noreply.github.com> Date: Mon, 11 Nov 2024 00:38:55 +0100 Subject: [PATCH] Add sharing current file with model --- ConfigurationManager.cpp | 4 +-- chatview/ChatRootView.cpp | 16 ++++++++-- chatview/ChatRootView.hpp | 8 ++++- chatview/ClientInterface.cpp | 51 ++++++++++++++++++++++++++++-- chatview/ClientInterface.hpp | 3 +- chatview/qml/RootItem.qml | 9 +++++- settings/ChatAssistantSettings.cpp | 7 +++- settings/ChatAssistantSettings.hpp | 1 + settings/SettingsConstants.hpp | 1 + 9 files changed, 89 insertions(+), 11 deletions(-) diff --git a/ConfigurationManager.cpp b/ConfigurationManager.cpp index e9e4e44..5ab05ff 100644 --- a/ConfigurationManager.cpp +++ b/ConfigurationManager.cpp @@ -88,8 +88,8 @@ void ConfigurationManager::selectModel() return; const auto providerUrl = (settingsButton == &m_generalSettings.ccSelectModel) - ? m_generalSettings.ccUrl.volatileValue() - : m_generalSettings.caUrl.volatileValue(); + ? m_generalSettings.ccUrl() + : m_generalSettings.caUrl(); const auto modelList = m_providersManager.getProviderByName(providerName) ->getInstalledModels(providerUrl); diff --git a/chatview/ChatRootView.cpp b/chatview/ChatRootView.cpp index 136a3aa..c3b58c8 100644 --- a/chatview/ChatRootView.cpp +++ b/chatview/ChatRootView.cpp @@ -22,6 +22,7 @@ #include #include +#include "ChatAssistantSettings.hpp" #include "GeneralSettings.hpp" namespace QodeAssist::Chat { @@ -37,6 +38,12 @@ ChatRootView::ChatRootView(QQuickItem *parent) &Utils::BaseAspect::changed, this, &ChatRootView::currentTemplateChanged); + + connect(&Settings::chatAssistantSettings().sharingCurrentFile, + &Utils::BaseAspect::changed, + this, + &ChatRootView::isSharingCurrentFileChanged); + generateColors(); } @@ -50,9 +57,9 @@ QColor ChatRootView::backgroundColor() const return Utils::creatorColor(Utils::Theme::BackgroundColorNormal); } -void ChatRootView::sendMessage(const QString &message) const +void ChatRootView::sendMessage(const QString &message, bool sharingCurrentFile) const { - m_clientInterface->sendMessage(message); + m_clientInterface->sendMessage(message, sharingCurrentFile); } void ChatRootView::copyToClipboard(const QString &text) @@ -129,4 +136,9 @@ QColor ChatRootView::codeColor() const return m_codeColor; } +bool ChatRootView::isSharingCurrentFile() const +{ + return Settings::chatAssistantSettings().sharingCurrentFile(); +} + } // namespace QodeAssist::Chat diff --git a/chatview/ChatRootView.hpp b/chatview/ChatRootView.hpp index f0d7272..98206f6 100644 --- a/chatview/ChatRootView.hpp +++ b/chatview/ChatRootView.hpp @@ -35,6 +35,8 @@ class ChatRootView : public QQuickItem Q_PROPERTY(QColor primaryColor READ primaryColor CONSTANT FINAL) Q_PROPERTY(QColor secondaryColor READ secondaryColor CONSTANT FINAL) Q_PROPERTY(QColor codeColor READ codeColor CONSTANT FINAL) + Q_PROPERTY(bool isSharingCurrentFile READ isSharingCurrentFile NOTIFY + isSharingCurrentFileChanged FINAL) QML_ELEMENT public: @@ -49,8 +51,10 @@ public: QColor codeColor() const; + bool isSharingCurrentFile() const; + public slots: - void sendMessage(const QString &message) const; + void sendMessage(const QString &message, bool sharingCurrentFile = false) const; void copyToClipboard(const QString &text); void cancelRequest(); @@ -58,6 +62,8 @@ signals: void chatModelChanged(); void currentTemplateChanged(); + void isSharingCurrentFileChanged(); + private: void generateColors(); QColor generateColor(const QColor &baseColor, diff --git a/chatview/ClientInterface.cpp b/chatview/ClientInterface.cpp index a75e99a..483d193 100644 --- a/chatview/ClientInterface.cpp +++ b/chatview/ClientInterface.cpp @@ -19,9 +19,18 @@ #include "ClientInterface.hpp" +#include #include #include #include +#include + +#include +#include +#include + +#include +#include #include "ChatAssistantSettings.hpp" #include "GeneralSettings.hpp" @@ -55,7 +64,7 @@ ClientInterface::ClientInterface(ChatModel *chatModel, QObject *parent) ClientInterface::~ClientInterface() = default; -void ClientInterface::sendMessage(const QString &message) +void ClientInterface::sendMessage(const QString &message, bool includeCurrentFile) { cancelRequest(); @@ -71,8 +80,20 @@ void ClientInterface::sendMessage(const QString &message) LLMCore::ContextData context; context.prefix = message; context.suffix = ""; - if (chatAssistantSettings.useSystemPrompt()) - context.systemPrompt = chatAssistantSettings.systemPrompt(); + + QString systemPrompt = chatAssistantSettings.systemPrompt(); + if (includeCurrentFile) { + QString fileContext = getCurrentFileContext(); + if (!fileContext.isEmpty()) { + context.systemPrompt = QString("%1\n\n%2").arg(systemPrompt, fileContext); + LOG_MESSAGE("Using system prompt with file context"); + } else { + context.systemPrompt = systemPrompt; + LOG_MESSAGE("Failed to get file context, using default system prompt"); + } + } else { + context.systemPrompt = systemPrompt; + } QJsonObject providerRequest; providerRequest["model"] = Settings::generalSettings().caModel(); @@ -128,4 +149,28 @@ void ClientInterface::handleLLMResponse(const QString &response, } } +QString ClientInterface::getCurrentFileContext() const +{ + auto currentEditor = Core::EditorManager::currentEditor(); + if (!currentEditor) { + LOG_MESSAGE("No active editor found"); + return QString(); + } + + auto textDocument = qobject_cast(currentEditor->document()); + if (!textDocument) { + LOG_MESSAGE("Current document is not a text document"); + return QString(); + } + + QString fileInfo = QString("Language: %1\nFile: %2\n\n") + .arg(textDocument->mimeType(), textDocument->filePath().toString()); + + QString content = textDocument->document()->toPlainText(); + + LOG_MESSAGE(QString("Got context from file: %1").arg(textDocument->filePath().toString())); + + return QString("Current file context:\n%1\nFile content:\n%2").arg(fileInfo, content); +} + } // namespace QodeAssist::Chat diff --git a/chatview/ClientInterface.hpp b/chatview/ClientInterface.hpp index 4de4ea7..0fb1225 100644 --- a/chatview/ClientInterface.hpp +++ b/chatview/ClientInterface.hpp @@ -36,7 +36,7 @@ public: explicit ClientInterface(ChatModel *chatModel, QObject *parent = nullptr); ~ClientInterface(); - void sendMessage(const QString &message); + void sendMessage(const QString &message, bool includeCurrentFile = false); void clearMessages(); void cancelRequest(); @@ -45,6 +45,7 @@ signals: private: void handleLLMResponse(const QString &response, const QJsonObject &request, bool isComplete); + QString getCurrentFileContext() const; LLMCore::RequestHandler *m_requestHandler; ChatModel *m_chatModel; diff --git a/chatview/qml/RootItem.qml b/chatview/qml/RootItem.qml index 26b85c4..675526b 100644 --- a/chatview/qml/RootItem.qml +++ b/chatview/qml/RootItem.qml @@ -137,6 +137,13 @@ ChatRootView { text: qsTr("Clear Chat") onClicked: clearChat() } + + CheckBox { + id: sharingCurrentFile + + text: "Share current file with models" + checked: root.isSharingCurrentFile + } } } @@ -169,7 +176,7 @@ ChatRootView { } function sendChatMessage() { - root.sendMessage(messageInput.text); + root.sendMessage(messageInput.text, sharingCurrentFile.checked) messageInput.text = "" scrollToBottom() } diff --git a/settings/ChatAssistantSettings.cpp b/settings/ChatAssistantSettings.cpp index f4d7dc6..0e3ae0f 100644 --- a/settings/ChatAssistantSettings.cpp +++ b/settings/ChatAssistantSettings.cpp @@ -50,6 +50,10 @@ ChatAssistantSettings::ChatAssistantSettings() chatTokensThreshold.setRange(1000, 16000); chatTokensThreshold.setDefaultValue(8000); + sharingCurrentFile.setSettingsKey(Constants::CA_SHARING_CURRENT_FILE); + sharingCurrentFile.setLabelText(Tr::tr("Share Current File With Assistant by Default")); + sharingCurrentFile.setDefaultValue(true); + // General Parameters Settings temperature.setSettingsKey(Constants::CA_TEMPERATURE); temperature.setLabelText(Tr::tr("Temperature:")); @@ -156,7 +160,8 @@ ChatAssistantSettings::ChatAssistantSettings() return Column{Row{Stretch{1}, resetToDefaults}, Space{8}, - Group{title(Tr::tr("Chat Settings")), Row{chatTokensThreshold, Stretch{1}}}, + Group{title(Tr::tr("Chat Settings")), + Column{Row{chatTokensThreshold, Stretch{1}}, sharingCurrentFile}}, Space{8}, Group{ title(Tr::tr("General Parameters")), diff --git a/settings/ChatAssistantSettings.hpp b/settings/ChatAssistantSettings.hpp index 3a4dde1..303d068 100644 --- a/settings/ChatAssistantSettings.hpp +++ b/settings/ChatAssistantSettings.hpp @@ -34,6 +34,7 @@ public: // Chat settings Utils::IntegerAspect chatTokensThreshold{this}; + Utils::BoolAspect sharingCurrentFile{this}; // General Parameters Settings Utils::DoubleAspect temperature{this}; diff --git a/settings/SettingsConstants.hpp b/settings/SettingsConstants.hpp index 1eae254..124429b 100644 --- a/settings/SettingsConstants.hpp +++ b/settings/SettingsConstants.hpp @@ -51,6 +51,7 @@ const char MAX_FILE_THRESHOLD[] = "QodeAssist.maxFileThreshold"; const char CC_MULTILINE_COMPLETION[] = "QodeAssist.ccMultilineCompletion"; const char CUSTOM_JSON_TEMPLATE[] = "QodeAssist.customJsonTemplate"; const char CA_TOKENS_THRESHOLD[] = "QodeAssist.caTokensThreshold"; +const char CA_SHARING_CURRENT_FILE[] = "QodeAssist.caSharingCurrentFile"; const char QODE_ASSIST_GENERAL_OPTIONS_ID[] = "QodeAssist.GeneralOptions"; const char QODE_ASSIST_GENERAL_SETTINGS_PAGE_ID[] = "QodeAssist.1GeneralSettingsPageId";