mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-10-05 03:25:12 -04:00
Feat: Add Claude tools support to plugin (#231)
* feat: Add settings for handle using tools in chat * feat: Add Claude tools support * fix: Add ai ignore to read project files list tool * fix: Add ai ignore to read project file by name tool * fix: Add ai ignore to read current opened files tool
This commit is contained in:
@ -32,6 +32,8 @@ namespace QodeAssist::Tools {
|
||||
|
||||
ListProjectFilesTool::ListProjectFilesTool(QObject *parent)
|
||||
: BaseTool(parent)
|
||||
, m_ignoreManager(new Context::IgnoreManager(this))
|
||||
|
||||
{}
|
||||
|
||||
QString ListProjectFilesTool::name() const
|
||||
@ -92,14 +94,27 @@ QFuture<QString> ListProjectFilesTool::executeAsync(const QJsonObject &input)
|
||||
}
|
||||
|
||||
QStringList fileList;
|
||||
QString projectPath = project->projectDirectory().toString();
|
||||
QString projectPath = project->projectDirectory().toUrlishString();
|
||||
|
||||
for (const auto &filePath : projectFiles) {
|
||||
QString absolutePath = filePath.toString();
|
||||
QString absolutePath = filePath.toUrlishString();
|
||||
|
||||
if (m_ignoreManager->shouldIgnore(absolutePath, project)) {
|
||||
LOG_MESSAGE(
|
||||
QString("Ignoring file due to .qodeassistignore: %1").arg(absolutePath));
|
||||
continue;
|
||||
}
|
||||
|
||||
QString relativePath = QDir(projectPath).relativeFilePath(absolutePath);
|
||||
fileList.append(relativePath);
|
||||
}
|
||||
|
||||
if (fileList.isEmpty()) {
|
||||
result += QString("Project '%1': No files after applying .qodeassistignore\n\n")
|
||||
.arg(project->displayName());
|
||||
continue;
|
||||
}
|
||||
|
||||
fileList.sort();
|
||||
|
||||
result += QString("Project '%1' (%2 files):\n")
|
||||
|
@ -19,7 +19,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "llmcore/BaseTool.hpp"
|
||||
#include <llmcore/BaseTool.hpp>
|
||||
|
||||
#include <context/IgnoreManager.hpp>
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
|
||||
@ -36,6 +38,7 @@ public:
|
||||
|
||||
private:
|
||||
QString formatFileList(const QStringList &files) const;
|
||||
Context::IgnoreManager *m_ignoreManager;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::Tools
|
||||
|
@ -36,6 +36,7 @@ namespace QodeAssist::Tools {
|
||||
|
||||
ReadProjectFileByNameTool::ReadProjectFileByNameTool(QObject *parent)
|
||||
: BaseTool(parent)
|
||||
, m_ignoreManager(new Context::IgnoreManager(this))
|
||||
{}
|
||||
|
||||
QString ReadProjectFileByNameTool::name() const
|
||||
@ -100,6 +101,14 @@ QFuture<QString> ReadProjectFileByNameTool::executeAsync(const QJsonObject &inpu
|
||||
throw std::runtime_error(error.toStdString());
|
||||
}
|
||||
|
||||
auto project = ProjectExplorer::ProjectManager::projectForFile(
|
||||
Utils::FilePath::fromString(filePath));
|
||||
if (project && m_ignoreManager->shouldIgnore(filePath, project)) {
|
||||
QString error
|
||||
= QString("Error: File '%1' is excluded by .qodeassistignore").arg(filename);
|
||||
throw std::runtime_error(error.toStdString());
|
||||
}
|
||||
|
||||
QString content = readFileContent(filePath);
|
||||
if (content.isNull()) {
|
||||
QString error = QString("Error: Could not read file '%1'").arg(filePath);
|
||||
@ -126,22 +135,40 @@ QString ReadProjectFileByNameTool::findFileInProject(const QString &fileName) co
|
||||
Utils::FilePaths projectFiles = project->files(ProjectExplorer::Project::SourceFiles);
|
||||
|
||||
for (const auto &projectFile : std::as_const(projectFiles)) {
|
||||
QFileInfo fileInfo(projectFile.path());
|
||||
QString absolutePath = projectFile.path();
|
||||
|
||||
if (m_ignoreManager->shouldIgnore(absolutePath, project)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QFileInfo fileInfo(absolutePath);
|
||||
if (fileInfo.fileName() == fileName) {
|
||||
return projectFile.path();
|
||||
return absolutePath;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &projectFile : std::as_const(projectFiles)) {
|
||||
QString absolutePath = projectFile.path();
|
||||
|
||||
if (m_ignoreManager->shouldIgnore(absolutePath, project)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (projectFile.endsWith(fileName)) {
|
||||
return projectFile.path();
|
||||
return absolutePath;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &projectFile : std::as_const(projectFiles)) {
|
||||
QFileInfo fileInfo(projectFile.path());
|
||||
QString absolutePath = projectFile.path();
|
||||
|
||||
if (m_ignoreManager->shouldIgnore(absolutePath, project)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QFileInfo fileInfo(absolutePath);
|
||||
if (fileInfo.fileName().contains(fileName, Qt::CaseInsensitive)) {
|
||||
return projectFile.path();
|
||||
return absolutePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "llmcore/BaseTool.hpp"
|
||||
#include <context/IgnoreManager.hpp>
|
||||
#include <llmcore/BaseTool.hpp>
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
|
||||
@ -37,6 +38,7 @@ public:
|
||||
private:
|
||||
QString findFileInProject(const QString &fileName) const;
|
||||
QString readFileContent(const QString &filePath) const;
|
||||
Context::IgnoreManager *m_ignoreManager;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::Tools
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <coreplugin/editormanager/ieditor.h>
|
||||
#include <logger/Logger.hpp>
|
||||
#include <projectexplorer/projectmanager.h>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QtConcurrent>
|
||||
@ -30,6 +31,7 @@ namespace QodeAssist::Tools {
|
||||
|
||||
ReadVisibleFilesTool::ReadVisibleFilesTool(QObject *parent)
|
||||
: BaseTool(parent)
|
||||
, m_ignoreManager(new Context::IgnoreManager(this))
|
||||
{}
|
||||
|
||||
QString ReadVisibleFilesTool::name() const
|
||||
@ -84,6 +86,15 @@ QFuture<QString> ReadVisibleFilesTool::executeAsync(const QJsonObject &input)
|
||||
}
|
||||
|
||||
QString filePath = editor->document()->filePath().toFSPathString();
|
||||
|
||||
auto project = ProjectExplorer::ProjectManager::projectForFile(
|
||||
editor->document()->filePath());
|
||||
if (project && m_ignoreManager->shouldIgnore(filePath, project)) {
|
||||
LOG_MESSAGE(
|
||||
QString("Ignoring visible file due to .qodeassistignore: %1").arg(filePath));
|
||||
continue;
|
||||
}
|
||||
|
||||
QByteArray contentBytes = editor->document()->contents();
|
||||
QString fileContent = QString::fromUtf8(contentBytes);
|
||||
|
||||
@ -98,6 +109,11 @@ QFuture<QString> ReadVisibleFilesTool::executeAsync(const QJsonObject &input)
|
||||
results.append(fileResult);
|
||||
}
|
||||
|
||||
if (results.isEmpty()) {
|
||||
QString error = "Error: All visible files are excluded by .qodeassistignore";
|
||||
throw std::runtime_error(error.toStdString());
|
||||
}
|
||||
|
||||
return results.join("\n\n" + QString(80, '=') + "\n\n");
|
||||
});
|
||||
}
|
||||
|
@ -19,7 +19,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "llmcore/BaseTool.hpp"
|
||||
#include <context/IgnoreManager.hpp>
|
||||
#include <llmcore/BaseTool.hpp>
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
|
||||
@ -33,6 +34,9 @@ public:
|
||||
QString description() const override;
|
||||
QJsonObject getDefinition(LLMCore::ToolSchemaFormat format) const override;
|
||||
QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override;
|
||||
|
||||
private:
|
||||
Context::IgnoreManager *m_ignoreManager;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::Tools
|
||||
|
158
tools/ToolsManager.cpp
Normal file
158
tools/ToolsManager.cpp
Normal file
@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Petr Mironychev
|
||||
*
|
||||
* This file is part of QodeAssist.
|
||||
*
|
||||
* QodeAssist is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QodeAssist is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ToolsManager.hpp"
|
||||
#include "logger/Logger.hpp"
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
|
||||
ToolsManager::ToolsManager(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_toolsFactory(new ToolsFactory(this))
|
||||
, m_toolHandler(new ToolHandler(this))
|
||||
{
|
||||
connect(
|
||||
m_toolHandler,
|
||||
&ToolHandler::toolCompleted,
|
||||
this,
|
||||
[this](const QString &requestId, const QString &toolId, const QString &result) {
|
||||
onToolFinished(requestId, toolId, result, true);
|
||||
});
|
||||
|
||||
connect(
|
||||
m_toolHandler,
|
||||
&ToolHandler::toolFailed,
|
||||
this,
|
||||
[this](const QString &requestId, const QString &toolId, const QString &error) {
|
||||
onToolFinished(requestId, toolId, error, false);
|
||||
});
|
||||
}
|
||||
|
||||
void ToolsManager::executeToolCall(
|
||||
const QString &requestId,
|
||||
const QString &toolId,
|
||||
const QString &toolName,
|
||||
const QJsonObject &input)
|
||||
{
|
||||
LOG_MESSAGE(QString("ToolsManager: Executing tool %1 (ID: %2) for request %3")
|
||||
.arg(toolName, toolId, requestId));
|
||||
|
||||
if (!m_pendingTools.contains(requestId)) {
|
||||
m_pendingTools[requestId] = QHash<QString, PendingTool>();
|
||||
}
|
||||
|
||||
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));
|
||||
return;
|
||||
}
|
||||
|
||||
PendingTool pendingTool{toolId, toolName, input, "", false};
|
||||
m_pendingTools[requestId][toolId] = pendingTool;
|
||||
|
||||
m_toolHandler->executeToolAsync(requestId, toolId, tool, input);
|
||||
LOG_MESSAGE(QString("ToolsManager: Started async execution of %1").arg(toolName));
|
||||
}
|
||||
|
||||
QJsonArray ToolsManager::getToolsDefinitions(ToolSchemaFormat format) const
|
||||
{
|
||||
if (!m_toolsFactory) {
|
||||
return QJsonArray();
|
||||
}
|
||||
|
||||
LLMCore::ToolSchemaFormat coreFormat = (format == ToolSchemaFormat::OpenAI)
|
||||
? LLMCore::ToolSchemaFormat::OpenAI
|
||||
: LLMCore::ToolSchemaFormat::Claude;
|
||||
|
||||
return m_toolsFactory->getToolsDefinitions(coreFormat);
|
||||
}
|
||||
|
||||
void ToolsManager::cleanupRequest(const QString &requestId)
|
||||
{
|
||||
m_pendingTools.remove(requestId);
|
||||
m_toolHandler->cleanupRequest(requestId);
|
||||
LOG_MESSAGE(QString("ToolsManager: Cleaned up request %1").arg(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)) {
|
||||
LOG_MESSAGE(QString("ToolsManager: Tool result for unknown tool %1 in request %2")
|
||||
.arg(toolId, requestId));
|
||||
return;
|
||||
}
|
||||
|
||||
PendingTool &tool = m_pendingTools[requestId][toolId];
|
||||
tool.result = success ? result : QString("Error: %1").arg(result);
|
||||
tool.complete = true;
|
||||
|
||||
LOG_MESSAGE(QString("ToolsManager: Tool %1 %2 for request %3")
|
||||
.arg(toolId)
|
||||
.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));
|
||||
}
|
||||
}
|
||||
|
||||
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 (it.value().complete) {
|
||||
results[it.key()] = it.value().result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
} // namespace QodeAssist::Tools
|
75
tools/ToolsManager.hpp
Normal file
75
tools/ToolsManager.hpp
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Petr Mironychev
|
||||
*
|
||||
* This file is part of QodeAssist.
|
||||
*
|
||||
* QodeAssist is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QodeAssist is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QHash>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QObject>
|
||||
|
||||
#include "ToolHandler.hpp"
|
||||
#include "ToolsFactory.hpp"
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
|
||||
enum class ToolSchemaFormat { OpenAI, Claude };
|
||||
|
||||
struct PendingTool
|
||||
{
|
||||
QString id;
|
||||
QString name;
|
||||
QJsonObject input;
|
||||
QString result;
|
||||
bool complete = false;
|
||||
};
|
||||
|
||||
class ToolsManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ToolsManager(QObject *parent = nullptr);
|
||||
|
||||
void executeToolCall(
|
||||
const QString &requestId,
|
||||
const QString &toolId,
|
||||
const QString &toolName,
|
||||
const QJsonObject &input);
|
||||
|
||||
QJsonArray getToolsDefinitions(ToolSchemaFormat format) const;
|
||||
void cleanupRequest(const QString &requestId);
|
||||
|
||||
signals:
|
||||
void toolExecutionComplete(const QString &requestId, const QHash<QString, QString> &toolResults);
|
||||
|
||||
private slots:
|
||||
void onToolFinished(
|
||||
const QString &requestId, const QString &toolId, const QString &result, bool success);
|
||||
|
||||
private:
|
||||
ToolsFactory *m_toolsFactory;
|
||||
ToolHandler *m_toolHandler;
|
||||
QHash<QString, QHash<QString, PendingTool>> m_pendingTools;
|
||||
|
||||
bool isExecutionComplete(const QString &requestId) const;
|
||||
QHash<QString, QString> getToolResults(const QString &requestId) const;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::Tools
|
Reference in New Issue
Block a user