From 9f7497d15c3a9f97330c6467427bbbd8305cb418 Mon Sep 17 00:00:00 2001 From: Petr Mironychev <9195189+Palm1r@users.noreply.github.com> Date: Fri, 15 May 2026 11:00:49 +0200 Subject: [PATCH] refactor: Move out agent roles from Chat root view controller --- ChatView/CMakeLists.txt | 1 + ChatView/ChatRootView.cpp | 91 ++++++++++----------------------------- ChatView/ChatRootView.hpp | 5 +-- 3 files changed, 25 insertions(+), 72 deletions(-) diff --git a/ChatView/CMakeLists.txt b/ChatView/CMakeLists.txt index e93aed4..0fdb99e 100644 --- a/ChatView/CMakeLists.txt +++ b/ChatView/CMakeLists.txt @@ -69,6 +69,7 @@ qt_add_qml_module(QodeAssistChatView FileItem.hpp FileItem.cpp ChatFileManager.hpp ChatFileManager.cpp ChatCompressor.hpp ChatCompressor.cpp + AgentRoleController.hpp AgentRoleController.cpp FileMentionItem.hpp FileMentionItem.cpp ) diff --git a/ChatView/ChatRootView.cpp b/ChatView/ChatRootView.cpp index 3a2ea0f..d69658b 100644 --- a/ChatView/ChatRootView.cpp +++ b/ChatView/ChatRootView.cpp @@ -26,7 +26,7 @@ #include #include -#include "AgentRole.hpp" +#include "AgentRoleController.hpp" #include "ChatAssistantSettings.hpp" #include "ChatCompressor.hpp" #include "ChatSerializer.hpp" @@ -51,6 +51,7 @@ ChatRootView::ChatRootView(QQuickItem *parent) , m_fileManager(new ChatFileManager(this)) , m_isRequestInProgress(false) , m_chatCompressor(new ChatCompressor(this)) + , m_agentRoleController(new AgentRoleController(this)) { m_isSyncOpenFiles = Settings::chatAssistantSettings().linkOpenFiles(); connect( @@ -128,8 +129,18 @@ ChatRootView::ChatRootView(QQuickItem *parent) updateInputTokensCount(); }); connect( - &Settings::chatAssistantSettings().systemPrompt, - &Utils::BaseAspect::changed, + m_agentRoleController, + &AgentRoleController::availableRolesChanged, + this, + &ChatRootView::availableAgentRolesChanged); + connect( + m_agentRoleController, + &AgentRoleController::currentRoleChanged, + this, + &ChatRootView::currentAgentRoleChanged); + connect( + m_agentRoleController, + &AgentRoleController::baseSystemPromptChanged, this, &ChatRootView::baseSystemPromptChanged); @@ -246,7 +257,6 @@ ChatRootView::ChatRootView(QQuickItem *parent) updateInputTokensCount(); refreshRules(); loadAvailableConfigurations(); - loadAvailableAgentRoles(); connect( ProjectExplorer::ProjectManager::instance(), @@ -1557,99 +1567,42 @@ QString ChatRootView::currentConfiguration() const void ChatRootView::loadAvailableAgentRoles() { - const QList roles = Settings::AgentRolesManager::loadAllRoles(); - - m_availableAgentRoles.clear(); - m_availableAgentRoles.append(Settings::AgentRolesManager::getNoRole().name); - - for (const auto &role : roles) - m_availableAgentRoles.append(role.name); - - const QString lastRoleId = Settings::chatAssistantSettings().lastUsedRoleId(); - m_currentAgentRole = Settings::AgentRolesManager::getNoRole().name; - - if (!lastRoleId.isEmpty()) { - for (const auto &role : roles) { - if (role.id == lastRoleId) { - m_currentAgentRole = role.name; - break; - } - } - } - - emit availableAgentRolesChanged(); - emit currentAgentRoleChanged(); + m_agentRoleController->loadAvailableRoles(); } void ChatRootView::applyAgentRole(const QString &roleName) { - auto &settings = Settings::chatAssistantSettings(); - - if (roleName == Settings::AgentRolesManager::getNoRole().name) { - settings.lastUsedRoleId.setValue(""); - settings.writeSettings(); - m_currentAgentRole = roleName; - emit currentAgentRoleChanged(); - return; - } - - const QList roles = Settings::AgentRolesManager::loadAllRoles(); - - for (const auto &role : roles) { - if (role.name == roleName) { - settings.lastUsedRoleId.setValue(role.id); - settings.writeSettings(); - m_currentAgentRole = role.name; - emit currentAgentRoleChanged(); - break; - } - } + m_agentRoleController->applyRole(roleName); } QStringList ChatRootView::availableAgentRoles() const { - return m_availableAgentRoles; + return m_agentRoleController->availableRoles(); } QString ChatRootView::currentAgentRole() const { - return m_currentAgentRole; + return m_agentRoleController->currentRole(); } QString ChatRootView::baseSystemPrompt() const { - return Settings::chatAssistantSettings().systemPrompt(); + return m_agentRoleController->baseSystemPrompt(); } QString ChatRootView::currentAgentRoleDescription() const { - const QString lastRoleId = Settings::chatAssistantSettings().lastUsedRoleId(); - if (lastRoleId.isEmpty()) - return Settings::AgentRolesManager::getNoRole().description; - - const Settings::AgentRole role = Settings::AgentRolesManager::loadRole(lastRoleId); - if (role.id.isEmpty()) - return Settings::AgentRolesManager::getNoRole().description; - - return role.description; + return m_agentRoleController->currentRoleDescription(); } QString ChatRootView::currentAgentRoleSystemPrompt() const { - const QString lastRoleId = Settings::chatAssistantSettings().lastUsedRoleId(); - if (lastRoleId.isEmpty()) - return QString(); - - const Settings::AgentRole role = Settings::AgentRolesManager::loadRole(lastRoleId); - if (role.id.isEmpty()) - return QString(); - - return role.systemPrompt; + return m_agentRoleController->currentRoleSystemPrompt(); } void ChatRootView::openAgentRolesSettings() { - Settings::showSettings(Utils::Id("QodeAssist.AgentRoles")); + m_agentRoleController->openSettings(); } void ChatRootView::compressCurrentChat() diff --git a/ChatView/ChatRootView.hpp b/ChatView/ChatRootView.hpp index 11126d5..e90b123 100644 --- a/ChatView/ChatRootView.hpp +++ b/ChatView/ChatRootView.hpp @@ -15,6 +15,7 @@ namespace QodeAssist::Chat { class ChatCompressor; +class AgentRoleController; class ChatRootView : public QQuickItem { @@ -271,10 +272,8 @@ private: QStringList m_availableConfigurations; QString m_currentConfiguration; - QStringList m_availableAgentRoles; - QString m_currentAgentRole; - ChatCompressor *m_chatCompressor; + AgentRoleController *m_agentRoleController; }; } // namespace QodeAssist::Chat