mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-02-03 13:50:19 -05:00
@ -50,29 +50,85 @@ void ToolsManager::executeToolCall(
|
||||
const QString &toolName,
|
||||
const QJsonObject &input)
|
||||
{
|
||||
LOG_MESSAGE(QString("ToolsManager: Executing tool %1 (ID: %2) for request %3")
|
||||
LOG_MESSAGE(QString("ToolsManager: Queueing tool %1 (ID: %2) for request %3")
|
||||
.arg(toolName, toolId, requestId));
|
||||
|
||||
if (!m_pendingTools.contains(requestId)) {
|
||||
m_pendingTools[requestId] = QHash<QString, PendingTool>();
|
||||
if (!m_toolQueues.contains(requestId)) {
|
||||
m_toolQueues[requestId] = ToolQueue();
|
||||
}
|
||||
|
||||
if (m_pendingTools[requestId].contains(toolId)) {
|
||||
LOG_MESSAGE(QString("Tool %1 already in progress for request %2").arg(toolId, requestId));
|
||||
return;
|
||||
}
|
||||
|
||||
auto tool = m_toolsFactory->getToolByName(toolName);
|
||||
if (!tool) {
|
||||
LOG_MESSAGE(QString("ToolsManager: Tool not found: %1").arg(toolName));
|
||||
auto &queue = m_toolQueues[requestId];
|
||||
|
||||
// Check if tool already exists in queue or completed
|
||||
for (const auto &tool : queue.queue) {
|
||||
if (tool.id == toolId) {
|
||||
LOG_MESSAGE(QString("Tool %1 already in queue for request %2").arg(toolId, requestId));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (queue.completed.contains(toolId)) {
|
||||
LOG_MESSAGE(
|
||||
QString("Tool %1 already completed for request %2").arg(toolId, requestId));
|
||||
return;
|
||||
}
|
||||
|
||||
// Add tool to queue
|
||||
PendingTool pendingTool{toolId, toolName, input, "", false};
|
||||
m_pendingTools[requestId][toolId] = pendingTool;
|
||||
queue.queue.append(pendingTool);
|
||||
|
||||
m_toolHandler->executeToolAsync(requestId, toolId, tool, input);
|
||||
LOG_MESSAGE(QString("ToolsManager: Started async execution of %1").arg(toolName));
|
||||
LOG_MESSAGE(QString("ToolsManager: Tool %1 added to queue (position %2)")
|
||||
.arg(toolName)
|
||||
.arg(queue.queue.size()));
|
||||
|
||||
// Start execution if not already running
|
||||
if (!queue.isExecuting) {
|
||||
executeNextTool(requestId);
|
||||
}
|
||||
}
|
||||
|
||||
void ToolsManager::executeNextTool(const QString &requestId)
|
||||
{
|
||||
if (!m_toolQueues.contains(requestId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto &queue = m_toolQueues[requestId];
|
||||
|
||||
// Check if queue is empty
|
||||
if (queue.queue.isEmpty()) {
|
||||
LOG_MESSAGE(QString("ToolsManager: All tools complete for request %1, emitting results")
|
||||
.arg(requestId));
|
||||
QHash<QString, QString> results = getToolResults(requestId);
|
||||
emit toolExecutionComplete(requestId, results);
|
||||
queue.isExecuting = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Get next tool from queue
|
||||
PendingTool tool = queue.queue.takeFirst();
|
||||
queue.isExecuting = true;
|
||||
|
||||
LOG_MESSAGE(QString("ToolsManager: Executing tool %1 (ID: %2) for request %3 (%4 remaining)")
|
||||
.arg(tool.name, tool.id, requestId)
|
||||
.arg(queue.queue.size()));
|
||||
|
||||
auto toolInstance = m_toolsFactory->getToolByName(tool.name);
|
||||
if (!toolInstance) {
|
||||
LOG_MESSAGE(QString("ToolsManager: Tool not found: %1").arg(tool.name));
|
||||
// Mark as failed and continue to next tool
|
||||
tool.result = QString("Error: Tool not found: %1").arg(tool.name);
|
||||
tool.complete = true;
|
||||
queue.completed[tool.id] = tool;
|
||||
executeNextTool(requestId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Store tool in completed map (will be updated when finished)
|
||||
queue.completed[tool.id] = tool;
|
||||
|
||||
m_toolHandler->executeToolAsync(requestId, tool.id, toolInstance, tool.input);
|
||||
LOG_MESSAGE(QString("ToolsManager: Started async execution of %1").arg(tool.name));
|
||||
}
|
||||
|
||||
QJsonArray ToolsManager::getToolsDefinitions(LLMCore::ToolSchemaFormat format) const
|
||||
@ -86,10 +142,10 @@ QJsonArray ToolsManager::getToolsDefinitions(LLMCore::ToolSchemaFormat format) c
|
||||
|
||||
void ToolsManager::cleanupRequest(const QString &requestId)
|
||||
{
|
||||
if (m_pendingTools.contains(requestId)) {
|
||||
if (m_toolQueues.contains(requestId)) {
|
||||
LOG_MESSAGE(QString("ToolsManager: Canceling pending tools for request %1").arg(requestId));
|
||||
m_toolHandler->cleanupRequest(requestId);
|
||||
m_pendingTools.remove(requestId);
|
||||
m_toolQueues.remove(requestId);
|
||||
}
|
||||
|
||||
LOG_MESSAGE(QString("ToolsManager: Cleaned up request %1").arg(requestId));
|
||||
@ -98,13 +154,20 @@ void ToolsManager::cleanupRequest(const QString &requestId)
|
||||
void ToolsManager::onToolFinished(
|
||||
const QString &requestId, const QString &toolId, const QString &result, bool success)
|
||||
{
|
||||
if (!m_pendingTools.contains(requestId) || !m_pendingTools[requestId].contains(toolId)) {
|
||||
if (!m_toolQueues.contains(requestId)) {
|
||||
LOG_MESSAGE(QString("ToolsManager: Tool result for unknown request %1").arg(requestId));
|
||||
return;
|
||||
}
|
||||
|
||||
auto &queue = m_toolQueues[requestId];
|
||||
|
||||
if (!queue.completed.contains(toolId)) {
|
||||
LOG_MESSAGE(QString("ToolsManager: Tool result for unknown tool %1 in request %2")
|
||||
.arg(toolId, requestId));
|
||||
return;
|
||||
}
|
||||
|
||||
PendingTool &tool = m_pendingTools[requestId][toolId];
|
||||
PendingTool &tool = queue.completed[toolId];
|
||||
tool.result = success ? result : QString("Error: %1").arg(result);
|
||||
tool.complete = true;
|
||||
|
||||
@ -113,14 +176,8 @@ void ToolsManager::onToolFinished(
|
||||
.arg(success ? QString("completed") : QString("failed"))
|
||||
.arg(requestId));
|
||||
|
||||
if (isExecutionComplete(requestId)) {
|
||||
QHash<QString, QString> results = getToolResults(requestId);
|
||||
LOG_MESSAGE(QString("ToolsManager: All tools complete for request %1, emitting results")
|
||||
.arg(requestId));
|
||||
emit toolExecutionComplete(requestId, results);
|
||||
} else {
|
||||
LOG_MESSAGE(QString("ToolsManager: Tools still pending for request %1").arg(requestId));
|
||||
}
|
||||
// Execute next tool in queue
|
||||
executeNextTool(requestId);
|
||||
}
|
||||
|
||||
ToolsFactory *ToolsManager::toolsFactory() const
|
||||
@ -128,29 +185,13 @@ ToolsFactory *ToolsManager::toolsFactory() const
|
||||
return m_toolsFactory;
|
||||
}
|
||||
|
||||
bool ToolsManager::isExecutionComplete(const QString &requestId) const
|
||||
{
|
||||
if (!m_pendingTools.contains(requestId)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const auto &tools = m_pendingTools[requestId];
|
||||
for (auto it = tools.begin(); it != tools.end(); ++it) {
|
||||
if (!it.value().complete) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QHash<QString, QString> ToolsManager::getToolResults(const QString &requestId) const
|
||||
{
|
||||
QHash<QString, QString> results;
|
||||
|
||||
if (m_pendingTools.contains(requestId)) {
|
||||
const auto &tools = m_pendingTools[requestId];
|
||||
for (auto it = tools.begin(); it != tools.end(); ++it) {
|
||||
if (m_toolQueues.contains(requestId)) {
|
||||
const auto &queue = m_toolQueues[requestId];
|
||||
for (auto it = queue.completed.begin(); it != queue.completed.end(); ++it) {
|
||||
if (it.value().complete) {
|
||||
results[it.key()] = it.value().result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user