refactor: Agent roaster improve

This commit is contained in:
Petr Mironychev
2026-06-11 14:51:49 +02:00
parent e65ac23e66
commit f36173d932
9 changed files with 121 additions and 28 deletions

View File

@@ -4,9 +4,14 @@
#include "SessionManager.hpp"
#include <LLMQore/BaseClient.hpp>
#include <LLMQore/ToolsManager.hpp>
#include "Agent.hpp"
#include "AgentFactory.hpp"
#include "ConversationHistory.hpp"
#include "Session.hpp"
#include "SystemPromptBuilder.hpp"
namespace QodeAssist {
@@ -54,6 +59,64 @@ Session *SessionManager::createSession(
return session;
}
Session *SessionManager::acquire(const QString &agentName, QString *errorOut)
{
auto &bucket = m_pool[agentName];
while (!bucket.isEmpty()) {
QPointer<Session> pooled = bucket.takeLast();
if (pooled && pooled->isValid()) {
resetSession(pooled);
m_sessions.append(pooled);
return pooled.data();
}
if (pooled)
pooled->deleteLater();
}
return createSession(agentName, /*externalHistory=*/nullptr, errorOut);
}
void SessionManager::release(Session *session)
{
if (!session)
return;
const int idx = m_sessions.indexOf(session);
if (idx < 0)
return;
m_sessions.removeAt(idx);
if (session->isInFlight())
session->cancel();
session->disconnect();
resetSession(session);
const QString agentName
= session->agent() ? session->agent()->config().name : QString();
QList<QPointer<Session>> &bucket = m_pool[agentName];
if (agentName.isEmpty() || bucket.size() >= kMaxPooledPerAgent) {
emit sessionRemoved(session);
session->deleteLater();
} else {
bucket.append(session);
}
}
void SessionManager::resetSession(Session *session)
{
if (!session)
return;
if (auto *history = session->history())
history->clear();
if (auto *systemPrompt = session->systemPrompt())
systemPrompt->clear();
if (auto *client = session->client()) {
if (auto *tools = client->tools())
tools->removeAllTools();
}
}
void SessionManager::removeSession(Session *session)
{
if (!session)

View File

@@ -4,6 +4,7 @@
#pragma once
#include <QHash>
#include <QList>
#include <QObject>
#include <QPointer>
@@ -33,6 +34,9 @@ public:
ConversationHistory *externalHistory,
QString *errorOut = nullptr);
Session *acquire(const QString &agentName, QString *errorOut = nullptr);
void release(Session *session);
void removeSession(Session *session);
QList<Session *> sessions() const;
@@ -47,8 +51,13 @@ signals:
void sessionRemoved(Session *session);
private:
void resetSession(Session *session);
static constexpr int kMaxPooledPerAgent = 2;
QPointer<AgentFactory> m_agentFactory;
QList<QPointer<Session>> m_sessions;
QHash<QString, QList<QPointer<Session>>> m_pool;
ToolContributorRegistry m_toolContributors;
};