feat: Add list project files tool

This commit is contained in:
Petr Mironychev
2025-09-22 23:37:40 +02:00
parent d0f8c1098f
commit ff750c271a
11 changed files with 248 additions and 50 deletions

View File

@ -0,0 +1,129 @@
/*
* 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 "ListProjectFilesTool.hpp"
#include <logger/Logger.hpp>
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <QDir>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonObject>
#include <QtConcurrent>
namespace QodeAssist::Tools {
ListProjectFilesTool::ListProjectFilesTool(QObject *parent)
: BaseTool(parent)
{}
QString ListProjectFilesTool::name() const
{
return "list_project_files";
}
QString ListProjectFilesTool::description() const
{
return "Get a list of all source files in the current project. "
"Returns a structured list of files with their relative paths from the project root. "
"Useful for understanding project structure and finding specific files. "
"No parameters required.";
}
QJsonObject ListProjectFilesTool::getDefinition(LLMCore::ToolSchemaFormat format) const
{
QJsonObject definition;
definition["type"] = "object";
definition["properties"] = QJsonObject();
definition["required"] = QJsonArray();
switch (format) {
case LLMCore::ToolSchemaFormat::OpenAI:
return customizeForOpenAI(definition);
case LLMCore::ToolSchemaFormat::Claude:
return customizeForClaude(definition);
case LLMCore::ToolSchemaFormat::Ollama:
return customizeForOllama(definition);
}
return definition;
}
QFuture<QString> ListProjectFilesTool::executeAsync(const QJsonObject &input)
{
Q_UNUSED(input)
return QtConcurrent::run([this]() -> QString {
QList<ProjectExplorer::Project *> projects = ProjectExplorer::ProjectManager::projects();
if (projects.isEmpty()) {
QString error = "Error: No projects found";
throw std::runtime_error(error.toStdString());
}
QString result;
for (auto project : projects) {
if (!project)
continue;
Utils::FilePaths projectFiles = project->files(ProjectExplorer::Project::SourceFiles);
if (projectFiles.isEmpty()) {
result += QString("Project '%1': No source files found\n\n")
.arg(project->displayName());
continue;
}
QStringList fileList;
QString projectPath = project->projectDirectory().toString();
for (const auto &filePath : projectFiles) {
QString absolutePath = filePath.toString();
QString relativePath = QDir(projectPath).relativeFilePath(absolutePath);
fileList.append(relativePath);
}
fileList.sort();
result += QString("Project '%1' (%2 files):\n")
.arg(project->displayName())
.arg(fileList.size());
for (const QString &file : fileList) {
result += QString("- %1\n").arg(file);
}
result += "\n";
}
return result.trimmed();
});
}
QString ListProjectFilesTool::formatFileList(const QStringList &files) const
{
QString result = QString("Project files (%1 total):\n\n").arg(files.size());
for (const QString &file : files) {
result += QString("- %1\n").arg(file);
}
return result;
}
} // 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 ListProjectFilesTool : public LLMCore::BaseTool
{
Q_OBJECT
public:
explicit ListProjectFilesTool(QObject *parent = nullptr);
QString name() const override;
QString description() const override;
QJsonObject getDefinition(LLMCore::ToolSchemaFormat format) const override;
QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override;
private:
QString formatFileList(const QStringList &files) const;
};
} // namespace QodeAssist::Tools

View File

@ -57,6 +57,34 @@ QString ReadProjectFileByNameTool::description() const
"in the editor.";
}
QJsonObject ReadProjectFileByNameTool::getDefinition(LLMCore::ToolSchemaFormat format) const
{
QJsonObject properties;
QJsonObject filenameProperty;
filenameProperty["type"] = "string";
filenameProperty["description"] = "The filename or relative path to read";
properties["filename"] = filenameProperty;
QJsonObject definition;
definition["type"] = "object";
definition["properties"] = properties;
QJsonArray required;
required.append("filename");
definition["required"] = required;
switch (format) {
case LLMCore::ToolSchemaFormat::OpenAI:
return customizeForOpenAI(definition);
case LLMCore::ToolSchemaFormat::Claude:
return customizeForClaude(definition);
case LLMCore::ToolSchemaFormat::Ollama:
return customizeForOllama(definition);
}
return definition;
}
QFuture<QString> ReadProjectFileByNameTool::executeAsync(const QJsonObject &input)
{
return QtConcurrent::run([this, input]() -> QString {
@ -85,31 +113,36 @@ QFuture<QString> ReadProjectFileByNameTool::executeAsync(const QJsonObject &inpu
QString ReadProjectFileByNameTool::findFileInProject(const QString &fileName) const
{
auto project = ProjectExplorer::ProjectManager::startupProject();
if (!project) {
LOG_MESSAGE("No startup project found");
QList<ProjectExplorer::Project *> projects = ProjectExplorer::ProjectManager::projects();
if (projects.isEmpty()) {
LOG_MESSAGE("No projects found");
return QString();
}
Utils::FilePaths projectFiles = project->files(ProjectExplorer::Project::SourceFiles);
for (auto project : projects) {
if (!project)
continue;
for (const auto &projectFile : projectFiles) {
QFileInfo fileInfo(projectFile.path());
if (fileInfo.fileName() == fileName) {
return projectFile.path();
Utils::FilePaths projectFiles = project->files(ProjectExplorer::Project::SourceFiles);
for (const auto &projectFile : std::as_const(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 : std::as_const(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();
for (const auto &projectFile : std::as_const(projectFiles)) {
QFileInfo fileInfo(projectFile.path());
if (fileInfo.fileName().contains(fileName, Qt::CaseInsensitive)) {
return projectFile.path();
}
}
}

View File

@ -31,6 +31,7 @@ public:
QString name() const override;
QString description() const override;
QJsonObject getDefinition(LLMCore::ToolSchemaFormat format) const override;
QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override;
private:

View File

@ -45,6 +45,25 @@ QString ReadVisibleFilesTool::description() const
"No parameters required.";
}
QJsonObject ReadVisibleFilesTool::getDefinition(LLMCore::ToolSchemaFormat format) const
{
QJsonObject definition;
definition["type"] = "object";
definition["properties"] = QJsonObject();
definition["required"] = QJsonArray();
switch (format) {
case LLMCore::ToolSchemaFormat::OpenAI:
return customizeForOpenAI(definition);
case LLMCore::ToolSchemaFormat::Claude:
return customizeForClaude(definition);
case LLMCore::ToolSchemaFormat::Ollama:
return customizeForOllama(definition);
}
return definition;
}
QFuture<QString> ReadVisibleFilesTool::executeAsync(const QJsonObject &input)
{
Q_UNUSED(input)

View File

@ -31,6 +31,7 @@ public:
QString name() const override;
QString description() const override;
QJsonObject getDefinition(LLMCore::ToolSchemaFormat format) const override;
QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override;
};

View File

@ -23,8 +23,9 @@
#include <QJsonArray>
#include <QJsonObject>
#include "ReadVisibleFilesTool.hpp"
#include "ListProjectFilesTool.hpp"
#include "ReadProjectFileByNameTool.hpp"
#include "ReadVisibleFilesTool.hpp"
namespace QodeAssist::Tools {
@ -38,6 +39,7 @@ void ToolsFactory::registerTools()
{
registerTool(new ReadVisibleFilesTool(this));
registerTool(new ReadProjectFileByNameTool(this));
registerTool(new ListProjectFilesTool(this));
LOG_MESSAGE(QString("Registered %1 tools").arg(m_tools.size()));
}