mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-05-28 03:10:28 -04:00
feat: Add sharing opened files with code completion requests (#156)
This commit is contained in:
parent
ffaf6bd61b
commit
9d2d70fc63
@ -230,6 +230,19 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
|
|||||||
if (updatedContext.fileContext.has_value())
|
if (updatedContext.fileContext.has_value())
|
||||||
systemPrompt.append(updatedContext.fileContext.value());
|
systemPrompt.append(updatedContext.fileContext.value());
|
||||||
|
|
||||||
|
if (m_completeSettings.useOpenFilesContext()) {
|
||||||
|
if (provider->providerID() == LLMCore::ProviderID::LlamaCpp) {
|
||||||
|
for (const auto openedFilePath : m_contextManager->openedFiles({filePath})) {
|
||||||
|
if (!updatedContext.filesMetadata) {
|
||||||
|
updatedContext.filesMetadata = QList<LLMCore::FileMetadata>();
|
||||||
|
}
|
||||||
|
updatedContext.filesMetadata->append({openedFilePath.first, openedFilePath.second});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
systemPrompt.append(m_contextManager->openedFilesContext({filePath}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
updatedContext.systemPrompt = systemPrompt;
|
updatedContext.systemPrompt = systemPrompt;
|
||||||
|
|
||||||
if (promptTemplate->type() == LLMCore::TemplateType::Chat) {
|
if (promptTemplate->type() == LLMCore::TemplateType::Chat) {
|
||||||
@ -330,6 +343,4 @@ void LLMClientInterface::sendCompletionToClient(
|
|||||||
emit messageReceived(LanguageServerProtocol::JsonRpcMessage(response));
|
emit messageReceived(LanguageServerProtocol::JsonRpcMessage(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
void LLMClientInterface::parseCurrentMessage() {}
|
|
||||||
|
|
||||||
} // namespace QodeAssist
|
} // namespace QodeAssist
|
||||||
|
@ -64,7 +64,6 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void startImpl() override;
|
void startImpl() override;
|
||||||
void parseCurrentMessage() override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void handleInitialize(const QJsonObject &request);
|
void handleInitialize(const QJsonObject &request);
|
||||||
|
@ -25,8 +25,10 @@
|
|||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
|
||||||
#include "settings/GeneralSettings.hpp"
|
#include "settings/GeneralSettings.hpp"
|
||||||
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
#include <projectexplorer/project.h>
|
#include <projectexplorer/project.h>
|
||||||
#include <projectexplorer/projectnodes.h>
|
#include <projectexplorer/projectnodes.h>
|
||||||
|
#include <texteditor/textdocument.h>
|
||||||
|
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
|
|
||||||
@ -107,4 +109,48 @@ bool ContextManager::isSpecifyCompletion(const DocumentInfo &documentInfo) const
|
|||||||
return generalSettings.specifyPreset1() && documentLanguage == preset1Language;
|
return generalSettings.specifyPreset1() && documentLanguage == preset1Language;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QList<QPair<QString, QString>> ContextManager::openedFiles(const QStringList excludeFiles) const
|
||||||
|
{
|
||||||
|
auto documents = Core::DocumentModel::openedDocuments();
|
||||||
|
|
||||||
|
QList<QPair<QString, QString>> files;
|
||||||
|
|
||||||
|
for (const auto *document : std::as_const(documents)) {
|
||||||
|
auto textDocument = qobject_cast<const TextEditor::TextDocument *>(document);
|
||||||
|
if (!textDocument)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto filePath = textDocument->filePath().toUrlishString();
|
||||||
|
if (!excludeFiles.contains(filePath)) {
|
||||||
|
files.append({filePath, textDocument->plainText()});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString ContextManager::openedFilesContext(const QStringList excludeFiles)
|
||||||
|
{
|
||||||
|
QString context = "User files context:\n";
|
||||||
|
|
||||||
|
auto documents = Core::DocumentModel::openedDocuments();
|
||||||
|
|
||||||
|
for (const auto *document : documents) {
|
||||||
|
auto textDocument = qobject_cast<const TextEditor::TextDocument *>(document);
|
||||||
|
if (!textDocument)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto filePath = textDocument->filePath().toUrlishString();
|
||||||
|
if (excludeFiles.contains(filePath))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
context += QString("File: %1\n").arg(filePath);
|
||||||
|
context += textDocument->plainText();
|
||||||
|
|
||||||
|
context += "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Context
|
} // namespace QodeAssist::Context
|
||||||
|
@ -47,6 +47,8 @@ public:
|
|||||||
|
|
||||||
ProgrammingLanguage getDocumentLanguage(const DocumentInfo &documentInfo) const override;
|
ProgrammingLanguage getDocumentLanguage(const DocumentInfo &documentInfo) const override;
|
||||||
bool isSpecifyCompletion(const DocumentInfo &documentInfo) const override;
|
bool isSpecifyCompletion(const DocumentInfo &documentInfo) const override;
|
||||||
|
QList<QPair<QString, QString>> openedFiles(const QStringList excludeFiles = QStringList{}) const;
|
||||||
|
QString openedFilesContext(const QStringList excludeFiles = QStringList{});
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Context
|
} // namespace QodeAssist::Context
|
||||||
|
@ -34,6 +34,13 @@ struct Message
|
|||||||
// clang-format on
|
// clang-format on
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct FileMetadata
|
||||||
|
{
|
||||||
|
QString filePath;
|
||||||
|
QString content;
|
||||||
|
bool operator==(const FileMetadata &) const = default;
|
||||||
|
};
|
||||||
|
|
||||||
struct ContextData
|
struct ContextData
|
||||||
{
|
{
|
||||||
std::optional<QString> systemPrompt;
|
std::optional<QString> systemPrompt;
|
||||||
@ -41,6 +48,7 @@ struct ContextData
|
|||||||
std::optional<QString> suffix;
|
std::optional<QString> suffix;
|
||||||
std::optional<QString> fileContext;
|
std::optional<QString> fileContext;
|
||||||
std::optional<QVector<Message>> history;
|
std::optional<QVector<Message>> history;
|
||||||
|
std::optional<QList<FileMetadata>> filesMetadata;
|
||||||
|
|
||||||
bool operator==(const ContextData &) const = default;
|
bool operator==(const ContextData &) const = default;
|
||||||
};
|
};
|
||||||
|
@ -206,6 +206,10 @@ CodeCompletionSettings::CodeCompletionSettings()
|
|||||||
showProgressWidget.setLabelText(Tr::tr("Show progress indicator during code completion"));
|
showProgressWidget.setLabelText(Tr::tr("Show progress indicator during code completion"));
|
||||||
showProgressWidget.setDefaultValue(true);
|
showProgressWidget.setDefaultValue(true);
|
||||||
|
|
||||||
|
useOpenFilesContext.setSettingsKey(Constants::CC_USE_OPEN_FILES_CONTEXT);
|
||||||
|
useOpenFilesContext.setLabelText(Tr::tr("Include context from open files"));
|
||||||
|
useOpenFilesContext.setDefaultValue(false);
|
||||||
|
|
||||||
useProjectChangesCache.setSettingsKey(Constants::CC_USE_PROJECT_CHANGES_CACHE);
|
useProjectChangesCache.setSettingsKey(Constants::CC_USE_PROJECT_CHANGES_CACHE);
|
||||||
useProjectChangesCache.setDefaultValue(true);
|
useProjectChangesCache.setDefaultValue(true);
|
||||||
useProjectChangesCache.setLabelText(Tr::tr("Max Changes Cache Size:"));
|
useProjectChangesCache.setLabelText(Tr::tr("Max Changes Cache Size:"));
|
||||||
@ -286,7 +290,8 @@ CodeCompletionSettings::CodeCompletionSettings()
|
|||||||
autoCompletionTypingInterval,
|
autoCompletionTypingInterval,
|
||||||
startSuggestionTimer,
|
startSuggestionTimer,
|
||||||
Stretch{1}},
|
Stretch{1}},
|
||||||
showProgressWidget}},
|
showProgressWidget,
|
||||||
|
useOpenFilesContext}},
|
||||||
Space{8},
|
Space{8},
|
||||||
Group{
|
Group{
|
||||||
title(Tr::tr("General Parameters")),
|
title(Tr::tr("General Parameters")),
|
||||||
@ -364,6 +369,8 @@ void CodeCompletionSettings::resetSettingsToDefaults()
|
|||||||
resetAspect(userMessageTemplateForCC);
|
resetAspect(userMessageTemplateForCC);
|
||||||
resetAspect(systemPromptForNonFimModels);
|
resetAspect(systemPromptForNonFimModels);
|
||||||
resetAspect(customLanguages);
|
resetAspect(customLanguages);
|
||||||
|
resetAspect(showProgressWidget);
|
||||||
|
resetAspect(useOpenFilesContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ public:
|
|||||||
Utils::StringListAspect customLanguages{this};
|
Utils::StringListAspect customLanguages{this};
|
||||||
|
|
||||||
Utils::BoolAspect showProgressWidget{this};
|
Utils::BoolAspect showProgressWidget{this};
|
||||||
|
Utils::BoolAspect useOpenFilesContext{this};
|
||||||
|
|
||||||
// General Parameters Settings
|
// General Parameters Settings
|
||||||
Utils::DoubleAspect temperature{this};
|
Utils::DoubleAspect temperature{this};
|
||||||
|
@ -56,6 +56,7 @@ const char CC_PRESET1_URL_HISTORY[] = "QodeAssist.ccPreset1UrlHistory";
|
|||||||
const char ENABLE_QODE_ASSIST[] = "QodeAssist.enableQodeAssist";
|
const char ENABLE_QODE_ASSIST[] = "QodeAssist.enableQodeAssist";
|
||||||
const char CC_AUTO_COMPLETION[] = "QodeAssist.ccAutoCompletion";
|
const char CC_AUTO_COMPLETION[] = "QodeAssist.ccAutoCompletion";
|
||||||
const char CC_SHOW_PROGRESS_WIDGET[] = "QodeAssist.ccShowProgressWidget";
|
const char CC_SHOW_PROGRESS_WIDGET[] = "QodeAssist.ccShowProgressWidget";
|
||||||
|
const char CC_USE_OPEN_FILES_CONTEXT[] = "QodeAssist.ccUseOpenFilesContext";
|
||||||
const char ENABLE_LOGGING[] = "QodeAssist.enableLogging";
|
const char ENABLE_LOGGING[] = "QodeAssist.enableLogging";
|
||||||
const char ENABLE_CHECK_UPDATE[] = "QodeAssist.enableCheckUpdate";
|
const char ENABLE_CHECK_UPDATE[] = "QodeAssist.enableCheckUpdate";
|
||||||
const char ENABLE_CHAT[] = "QodeAssist.enableChat";
|
const char ENABLE_CHAT[] = "QodeAssist.enableChat";
|
||||||
|
@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QJsonArray>
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "llmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
@ -34,6 +36,17 @@ public:
|
|||||||
{
|
{
|
||||||
request["input_prefix"] = context.prefix.value_or("");
|
request["input_prefix"] = context.prefix.value_or("");
|
||||||
request["input_suffix"] = context.suffix.value_or("");
|
request["input_suffix"] = context.suffix.value_or("");
|
||||||
|
|
||||||
|
if (context.filesMetadata && !context.filesMetadata->isEmpty()) {
|
||||||
|
QJsonArray filesArray;
|
||||||
|
for (const auto &file : *context.filesMetadata) {
|
||||||
|
QJsonObject fileObj;
|
||||||
|
fileObj["filename"] = file.filePath;
|
||||||
|
fileObj["text"] = file.content;
|
||||||
|
filesArray.append(fileObj);
|
||||||
|
}
|
||||||
|
request["input_extra"] = filesArray;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString description() const override
|
QString description() const override
|
||||||
|
Loading…
Reference in New Issue
Block a user