From c36dffea9366caaab6e0ced87997caeeefd9d922 Mon Sep 17 00:00:00 2001 From: Petr Mironychev <9195189+Palm1r@users.noreply.github.com> Date: Sun, 17 Aug 2025 22:01:26 +0200 Subject: [PATCH] refactor: Add model output settings instead smartprocessing setting (#220) --- CodeHandler.cpp | 13 +++++ CodeHandler.hpp | 5 ++ LLMClientInterface.cpp | 20 +++++-- settings/CodeCompletionSettings.cpp | 82 ++++++++++++++++------------- settings/CodeCompletionSettings.hpp | 2 +- settings/SettingsConstants.hpp | 2 +- test/LLMClientInterfaceTests.cpp | 2 +- 7 files changed, 80 insertions(+), 46 deletions(-) diff --git a/CodeHandler.cpp b/CodeHandler.cpp index 9960218..45b5a0c 100644 --- a/CodeHandler.cpp +++ b/CodeHandler.cpp @@ -99,6 +99,19 @@ const QVector &getKnownLanguages() return knownLanguages; } +bool CodeHandler::hasCodeBlocks(const QString &text) +{ + QStringList lines = text.split('\n'); + + for (const QString &line : lines) { + if (line.trimmed().startsWith("```")) { + return true; + } + } + + return false; +} + static QHash buildLanguageToCommentPrefixMap() { QHash result; diff --git a/CodeHandler.hpp b/CodeHandler.hpp index fd0ad8a..4d4a91f 100644 --- a/CodeHandler.hpp +++ b/CodeHandler.hpp @@ -40,6 +40,11 @@ public: */ static QString detectLanguageFromExtension(const QString &extension); + /** + * Detects if text contains code blocks, or returns false if this was not possible + */ + static bool hasCodeBlocks(const QString &text); + private: static QString getCommentPrefix(const QString &language); diff --git a/LLMClientInterface.cpp b/LLMClientInterface.cpp index a6749ac..334182b 100644 --- a/LLMClientInterface.cpp +++ b/LLMClientInterface.cpp @@ -347,11 +347,21 @@ void LLMClientInterface::sendCompletionToClient( LOG_MESSAGE(QString("Completions before filter: \n%1").arg(completion)); - QString processedCompletion - = promptTemplate->type() == LLMCore::TemplateType::Chat - && m_completeSettings.smartProcessInstuctText() - ? CodeHandler::processText(completion, Context::extractFilePathFromRequest(request)) - : completion; + QString outputHandler = m_completeSettings.modelOutputHandler.stringValue(); + QString processedCompletion; + + if (outputHandler == "Raw text") { + processedCompletion = completion; + } else if (outputHandler == "Force processing") { + processedCompletion = CodeHandler::processText(completion, + Context::extractFilePathFromRequest(request)); + } else { // "Auto" + processedCompletion = CodeHandler::hasCodeBlocks(completion) + ? CodeHandler::processText(completion, + Context::extractFilePathFromRequest( + request)) + : completion; + } completionItem[LanguageServerProtocol::textKey] = processedCompletion; QJsonObject range; diff --git a/settings/CodeCompletionSettings.cpp b/settings/CodeCompletionSettings.cpp index 5ff5ac5..f9425e4 100644 --- a/settings/CodeCompletionSettings.cpp +++ b/settings/CodeCompletionSettings.cpp @@ -55,9 +55,19 @@ CodeCompletionSettings::CodeCompletionSettings() stream.setDefaultValue(true); stream.setLabelText(Tr::tr("Enable stream option")); - smartProcessInstuctText.setSettingsKey(Constants::CC_SMART_PROCESS_INSTRUCT_TEXT); - smartProcessInstuctText.setDefaultValue(true); - smartProcessInstuctText.setLabelText(Tr::tr("Enable smart process text from instruct model")); + modelOutputHandler.setLabelText(Tr::tr("Text output proccessing mode:")); + modelOutputHandler.setSettingsKey(Constants::CC_MODEL_OUTPUT_HANDLER); + modelOutputHandler.setDisplayStyle(Utils::SelectionAspect::DisplayStyle::ComboBox); + modelOutputHandler.addOption("Auto"); + modelOutputHandler.addOption("Force processing"); + modelOutputHandler.addOption("Raw text"); + modelOutputHandler.setDefaultValue("Auto"); + modelOutputHandler.setToolTip( + Tr::tr("Auto: Automatically detects codeblock and applies processing when found, other " + "text as comments\n" + "Force Processing: Always processes text with codeblock formatting and other text " + "as comments\n" + "Raw Text: Shows unprocessed text without any formatting")); startSuggestionTimer.setSettingsKey(Constants::СС_START_SUGGESTION_TIMER); startSuggestionTimer.setLabelText(Tr::tr("with delay(ms)")); @@ -287,40 +297,36 @@ CodeCompletionSettings::CodeCompletionSettings() }}, Row{useProjectChangesCache, maxChangesCacheSize, Stretch{1}}}; - return Column{ - Row{Stretch{1}, resetToDefaults}, - Space{8}, - Group{ - title(TrConstants::AUTO_COMPLETION_SETTINGS), - Column{ - autoCompletion, - Space{8}, - multiLineCompletion, - stream, - smartProcessInstuctText, - Row{autoCompletionCharThreshold, - autoCompletionTypingInterval, - startSuggestionTimer, - Stretch{1}}, - showProgressWidget, - useOpenFilesContext}}, - Space{8}, - Group{ - title(Tr::tr("General Parameters")), - Column{ - Row{genGrid, Stretch{1}}, - }}, - Space{8}, - Group{title(Tr::tr("Advanced Parameters")), Column{Row{advancedGrid, Stretch{1}}}}, - Space{8}, - Group{title(Tr::tr("Context Settings")), contextItem}, - Space{8}, - Group{ - title(Tr::tr("Quick Refactor Settings")), - Column{useOpenFilesInQuickRefactor, quickRefactorSystemPrompt}}, - Space{8}, - Group{title(Tr::tr("Ollama Settings")), Column{Row{ollamaGrid, Stretch{1}}}}, - Stretch{1}}; + return Column{Row{Stretch{1}, resetToDefaults}, + Space{8}, + Group{title(TrConstants::AUTO_COMPLETION_SETTINGS), + Column{autoCompletion, + Space{8}, + multiLineCompletion, + stream, + Row{modelOutputHandler, Stretch{1}}, + Row{autoCompletionCharThreshold, + autoCompletionTypingInterval, + startSuggestionTimer, + Stretch{1}}, + showProgressWidget, + useOpenFilesContext}}, + Space{8}, + Group{title(Tr::tr("General Parameters")), + Column{ + Row{genGrid, Stretch{1}}, + }}, + Space{8}, + Group{title(Tr::tr("Advanced Parameters")), + Column{Row{advancedGrid, Stretch{1}}}}, + Space{8}, + Group{title(Tr::tr("Context Settings")), contextItem}, + Space{8}, + Group{title(Tr::tr("Quick Refactor Settings")), + Column{useOpenFilesInQuickRefactor, quickRefactorSystemPrompt}}, + Space{8}, + Group{title(Tr::tr("Ollama Settings")), Column{Row{ollamaGrid, Stretch{1}}}}, + Stretch{1}}; }); } @@ -360,7 +366,6 @@ void CodeCompletionSettings::resetSettingsToDefaults() resetAspect(autoCompletion); resetAspect(multiLineCompletion); resetAspect(stream); - resetAspect(smartProcessInstuctText); resetAspect(temperature); resetAspect(maxTokens); resetAspect(useTopP); @@ -389,6 +394,7 @@ void CodeCompletionSettings::resetSettingsToDefaults() resetAspect(useOpenFilesContext); resetAspect(useOpenFilesInQuickRefactor); resetAspect(quickRefactorSystemPrompt); + resetAspect(modelOutputHandler); } } diff --git a/settings/CodeCompletionSettings.hpp b/settings/CodeCompletionSettings.hpp index b81dcdd..0c4ca8d 100644 --- a/settings/CodeCompletionSettings.hpp +++ b/settings/CodeCompletionSettings.hpp @@ -36,7 +36,7 @@ public: Utils::BoolAspect autoCompletion{this}; Utils::BoolAspect multiLineCompletion{this}; Utils::BoolAspect stream{this}; - Utils::BoolAspect smartProcessInstuctText{this}; + Utils::SelectionAspect modelOutputHandler{this}; Utils::IntegerAspect startSuggestionTimer{this}; Utils::IntegerAspect autoCompletionCharThreshold{this}; diff --git a/settings/SettingsConstants.hpp b/settings/SettingsConstants.hpp index ae6b714..354138e 100644 --- a/settings/SettingsConstants.hpp +++ b/settings/SettingsConstants.hpp @@ -76,7 +76,7 @@ const char СС_AUTO_COMPLETION_TYPING_INTERVAL[] = "QodeAssist.autoCompletionTy const char MAX_FILE_THRESHOLD[] = "QodeAssist.maxFileThreshold"; const char CC_MULTILINE_COMPLETION[] = "QodeAssist.ccMultilineCompletion"; const char CC_STREAM[] = "QodeAssist.ccStream"; -const char CC_SMART_PROCESS_INSTRUCT_TEXT[] = "QodeAssist.ccSmartProcessInstructText"; +const char CC_MODEL_OUTPUT_HANDLER[] = "QodeAssist.ccModelOutputHandler"; const char CUSTOM_JSON_TEMPLATE[] = "QodeAssist.customJsonTemplate"; const char CA_TOKENS_THRESHOLD[] = "QodeAssist.caTokensThreshold"; const char CA_LINK_OPEN_FILES[] = "QodeAssist.caLinkOpenFiles"; diff --git a/test/LLMClientInterfaceTests.cpp b/test/LLMClientInterfaceTests.cpp index cbd97b2..306ca2a 100644 --- a/test/LLMClientInterfaceTests.cpp +++ b/test/LLMClientInterfaceTests.cpp @@ -278,7 +278,7 @@ if __name__ == "__main__": "/path/to/file.py", "text/python"); - m_completeSettings.smartProcessInstuctText.setValue(true); + m_completeSettings.modelOutputHandler.setValue(0); m_requestHandler->setFakeCompletion( "Here's the code: ```cpp\nint main() {\n return 0;\n}\n```");