mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-06-14 02:09:22 -04:00
refactor: Agent roaster improve
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user