feat: Add tools, fabric and executable handler (#230)

This commit is contained in:
Petr Mironychev
2025-09-22 12:36:13 +02:00
committed by GitHub
parent 5cde6ffac7
commit d0f8c1098f
12 changed files with 679 additions and 25 deletions

View File

@ -0,0 +1,135 @@
/*
* 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 "ReadProjectFileByNameTool.hpp"
#include <coreplugin/documentmanager.h>
#include <logger/Logger.hpp>
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTextStream>
#include <QtConcurrent>
namespace QodeAssist::Tools {
ReadProjectFileByNameTool::ReadProjectFileByNameTool(QObject *parent)
: BaseTool(parent)
{}
QString ReadProjectFileByNameTool::name() const
{
return "read_project_file_by_name";
}
QString ReadProjectFileByNameTool::description() const
{
return "Read the content of a specific file from the current project by providing its filename "
"or "
"relative path. This tool searches for files within the project scope and supports:\n"
"- Exact filename match (e.g., 'main.cpp')\n"
"- Relative path from project root (e.g., 'src/utils/helper.cpp')\n"
"- Partial path matching (e.g., 'utils/helper.cpp')\n"
"- Case-insensitive filename search as fallback\n"
"Input parameter: 'filename' - the name or path of the file to read.\n"
"Use this when you need to examine specific project files that are not currently open "
"in the editor.";
}
QFuture<QString> ReadProjectFileByNameTool::executeAsync(const QJsonObject &input)
{
return QtConcurrent::run([this, input]() -> QString {
QString filename = input["filename"].toString();
if (filename.isEmpty()) {
QString error = "Error: filename parameter is required";
throw std::invalid_argument(error.toStdString());
}
QString filePath = findFileInProject(filename);
if (filePath.isEmpty()) {
QString error = QString("Error: File '%1' not found").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);
throw std::runtime_error(error.toStdString());
}
QString result = QString("File: %1\n\nContent:\n%2").arg(filePath, content);
return result;
});
}
QString ReadProjectFileByNameTool::findFileInProject(const QString &fileName) const
{
auto project = ProjectExplorer::ProjectManager::startupProject();
if (!project) {
LOG_MESSAGE("No startup project found");
return QString();
}
Utils::FilePaths projectFiles = project->files(ProjectExplorer::Project::SourceFiles);
for (const auto &projectFile : projectFiles) {
QFileInfo fileInfo(projectFile.path());
if (fileInfo.fileName() == fileName) {
return projectFile.path();
}
}
for (const auto &projectFile : projectFiles) {
if (projectFile.endsWith(fileName)) {
return projectFile.path();
}
}
for (const auto &projectFile : projectFiles) {
QFileInfo fileInfo(projectFile.path());
if (fileInfo.fileName().contains(fileName, Qt::CaseInsensitive)) {
return projectFile.path();
}
}
return QString();
}
QString ReadProjectFileByNameTool::readFileContent(const QString &filePath) const
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
LOG_MESSAGE(QString("Could not open file: %1").arg(filePath));
return QString();
}
QTextStream stream(&file);
stream.setAutoDetectUnicode(true);
QString content = stream.readAll();
file.close();
return content;
}
} // namespace QodeAssist::Tools

View File

@ -0,0 +1,41 @@
/*
* 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 "llmcore/BaseTool.hpp"
namespace QodeAssist::Tools {
class ReadProjectFileByNameTool : public LLMCore::BaseTool
{
Q_OBJECT
public:
explicit ReadProjectFileByNameTool(QObject *parent = nullptr);
QString name() const override;
QString description() const override;
QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override;
private:
QString findFileInProject(const QString &fileName) const;
QString readFileContent(const QString &filePath) const;
};
} // namespace QodeAssist::Tools

View File

@ -0,0 +1,86 @@
/*
* 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 "ReadVisibleFilesTool.hpp"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <logger/Logger.hpp>
#include <QJsonArray>
#include <QJsonObject>
#include <QtConcurrent>
namespace QodeAssist::Tools {
ReadVisibleFilesTool::ReadVisibleFilesTool(QObject *parent)
: BaseTool(parent)
{}
QString ReadVisibleFilesTool::name() const
{
return "read_visible_files";
}
QString ReadVisibleFilesTool::description() const
{
return "Read the content of all currently visible files in editor tabs. "
"Returns content from all open tabs that are currently visible, including unsaved "
"changes. "
"No parameters required.";
}
QFuture<QString> ReadVisibleFilesTool::executeAsync(const QJsonObject &input)
{
Q_UNUSED(input)
return QtConcurrent::run([this]() -> QString {
auto editors = Core::EditorManager::visibleEditors();
if (editors.isEmpty()) {
QString error = "Error: No visible files in the editor";
throw std::runtime_error(error.toStdString());
}
QStringList results;
for (auto editor : editors) {
if (!editor || !editor->document()) {
continue;
}
QString filePath = editor->document()->filePath().toFSPathString();
QByteArray contentBytes = editor->document()->contents();
QString fileContent = QString::fromUtf8(contentBytes);
QString fileResult;
if (fileContent.isEmpty()) {
fileResult
= QString("File: %1\n\nThe file is empty or could not be read").arg(filePath);
} else {
fileResult = QString("File: %1\n\nContent:\n%2").arg(filePath, fileContent);
}
results.append(fileResult);
}
return results.join("\n\n" + QString(80, '=') + "\n\n");
});
}
} // namespace QodeAssist::Tools

View File

@ -0,0 +1,37 @@
/*
* 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 "llmcore/BaseTool.hpp"
namespace QodeAssist::Tools {
class ReadVisibleFilesTool : public LLMCore::BaseTool
{
Q_OBJECT
public:
explicit ReadVisibleFilesTool(QObject *parent = nullptr);
QString name() const override;
QString description() const override;
QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override;
};
} // namespace QodeAssist::Tools

106
tools/ToolHandler.cpp Normal file
View File

@ -0,0 +1,106 @@
/*
* 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 "ToolHandler.hpp"
#include <QJsonDocument>
#include <QTimer>
#include <QtConcurrent>
#include "logger/Logger.hpp"
namespace QodeAssist::Tools {
ToolHandler::ToolHandler(QObject *parent)
: QObject(parent)
{}
QFuture<QString> ToolHandler::executeToolAsync(
const QString &requestId,
const QString &toolId,
LLMCore::BaseTool *tool,
const QJsonObject &input)
{
if (!tool) {
return QtConcurrent::run([]() -> QString { throw std::runtime_error("Tool is null"); });
}
auto execution = new ToolExecution();
execution->requestId = requestId;
execution->toolId = toolId;
execution->toolName = tool->name();
execution->watcher = new QFutureWatcher<QString>(this);
connect(execution->watcher, &QFutureWatcher<QString>::finished, this, [this, toolId]() {
onToolExecutionFinished(toolId);
});
LOG_MESSAGE(QString("Starting tool execution: %1 (ID: %2)").arg(tool->name(), toolId));
auto future = tool->executeAsync(input);
execution->watcher->setFuture(future);
m_activeExecutions.insert(toolId, execution);
return future;
}
void ToolHandler::cleanupRequest(const QString &requestId)
{
auto it = m_activeExecutions.begin();
while (it != m_activeExecutions.end()) {
if (it.value()->requestId == requestId) {
auto execution = it.value();
LOG_MESSAGE(
QString("Canceling tool %1 for request %2").arg(execution->toolName, requestId));
if (execution->watcher) {
execution->watcher->cancel();
execution->watcher->deleteLater();
}
delete execution;
it = m_activeExecutions.erase(it);
} else {
++it;
}
}
}
void ToolHandler::onToolExecutionFinished(const QString &toolId)
{
if (!m_activeExecutions.contains(toolId)) {
return;
}
auto execution = m_activeExecutions.take(toolId);
try {
QString result = execution->watcher->result();
LOG_MESSAGE(QString("Tool %1 completed").arg(execution->toolName));
emit toolCompleted(execution->requestId, execution->toolId, result);
} catch (const std::exception &e) {
QString error = QString::fromStdString(e.what());
LOG_MESSAGE(QString("Tool %1 failed: %2").arg(execution->toolName, error));
emit toolFailed(execution->requestId, execution->toolId, error);
}
execution->watcher->deleteLater();
delete execution;
}
} // namespace QodeAssist::Tools

65
tools/ToolHandler.hpp Normal file
View File

@ -0,0 +1,65 @@
/*
* 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 <QFutureWatcher>
#include <QHash>
#include <QJsonObject>
#include <QObject>
#include <QString>
#include <llmcore/BaseTool.hpp>
namespace QodeAssist::Tools {
class ToolHandler : public QObject
{
Q_OBJECT
public:
explicit ToolHandler(QObject *parent = nullptr);
QFuture<QString> executeToolAsync(
const QString &requestId,
const QString &toolId,
LLMCore::BaseTool *tool,
const QJsonObject &input);
void cleanupRequest(const QString &requestId);
signals:
void toolCompleted(const QString &requestId, const QString &toolId, const QString &result);
void toolFailed(const QString &requestId, const QString &toolId, const QString &error);
private:
struct ToolExecution
{
QString requestId;
QString toolId;
QString toolName;
QFutureWatcher<QString> *watcher;
};
QHash<QString, ToolExecution *> m_activeExecutions; // toolId -> execution
void onToolExecutionFinished(const QString &toolId);
};
} // namespace QodeAssist::Tools

83
tools/ToolsFactory.cpp Normal file
View File

@ -0,0 +1,83 @@
/*
* 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 "ToolsFactory.hpp"
#include "logger/Logger.hpp"
#include <QJsonArray>
#include <QJsonObject>
#include "ReadVisibleFilesTool.hpp"
#include "ReadProjectFileByNameTool.hpp"
namespace QodeAssist::Tools {
ToolsFactory::ToolsFactory(QObject *parent)
: QObject(parent)
{
registerTools();
}
void ToolsFactory::registerTools()
{
registerTool(new ReadVisibleFilesTool(this));
registerTool(new ReadProjectFileByNameTool(this));
LOG_MESSAGE(QString("Registered %1 tools").arg(m_tools.size()));
}
void ToolsFactory::registerTool(LLMCore::BaseTool *tool)
{
if (!tool) {
LOG_MESSAGE("Warning: Attempted to register null tool");
return;
}
const QString toolName = tool->name();
if (m_tools.contains(toolName)) {
LOG_MESSAGE(QString("Warning: Tool '%1' already registered, replacing").arg(toolName));
}
m_tools.insert(toolName, tool);
}
QList<LLMCore::BaseTool *> ToolsFactory::getAvailableTools() const
{
return m_tools.values();
}
LLMCore::BaseTool *ToolsFactory::getToolByName(const QString &name) const
{
return m_tools.value(name, nullptr);
}
QJsonArray ToolsFactory::getToolsDefinitions(LLMCore::ToolSchemaFormat format) const
{
QJsonArray toolsArray;
for (auto it = m_tools.constBegin(); it != m_tools.constEnd(); ++it) {
if (it.value()) {
toolsArray.append(it.value()->getDefinition(format));
}
}
return toolsArray;
}
} // namespace QodeAssist::Tools

45
tools/ToolsFactory.hpp Normal file
View File

@ -0,0 +1,45 @@
/*
* 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 <QObject>
#include <llmcore/BaseTool.hpp>
namespace QodeAssist::Tools {
class ToolsFactory : public QObject
{
Q_OBJECT
public:
ToolsFactory(QObject *parent = nullptr);
~ToolsFactory() override = default;
QList<LLMCore::BaseTool *> getAvailableTools() const;
LLMCore::BaseTool *getToolByName(const QString &name) const;
QJsonArray getToolsDefinitions(LLMCore::ToolSchemaFormat format) const;
private:
void registerTools();
void registerTool(LLMCore::BaseTool *tool);
QHash<QString, LLMCore::BaseTool *> m_tools;
};
} // namespace QodeAssist::Tools