refactor: Simplified edit tool (#242)

refactor: Re-work edit file tool
This commit is contained in:
Petr Mironychev
2025-10-26 11:47:16 +01:00
committed by GitHub
parent 608103b92e
commit 43b64b9166
29 changed files with 739 additions and 817 deletions

View File

@ -20,7 +20,9 @@
#include "CreateNewFileTool.hpp"
#include "ToolExceptions.hpp"
#include <context/ProjectUtils.hpp>
#include <logger/Logger.hpp>
#include <settings/GeneralSettings.hpp>
#include <QDir>
#include <QFile>
#include <QFileInfo>
@ -95,6 +97,21 @@ QFuture<QString> CreateNewFileTool::executeAsync(const QJsonObject &input)
}
QFileInfo fileInfo(filePath);
QString absolutePath = fileInfo.absoluteFilePath();
// Check if the file path is within the project
bool isInProject = Context::ProjectUtils::isFileInProject(absolutePath);
if (!isInProject) {
const auto &settings = Settings::generalSettings();
if (!settings.allowAccessOutsideProject()) {
throw ToolRuntimeError(
QString("Error: File path '%1' is not within the current project. "
"Enable 'Allow file access outside project' in settings to create files outside project scope.")
.arg(absolutePath));
}
LOG_MESSAGE(QString("Creating file outside project scope: %1").arg(absolutePath));
}
if (fileInfo.exists()) {
throw ToolRuntimeError(
@ -107,17 +124,17 @@ QFuture<QString> CreateNewFileTool::executeAsync(const QJsonObject &input)
.arg(dir.absolutePath()));
}
QFile file(filePath);
QFile file(absolutePath);
if (!file.open(QIODevice::WriteOnly)) {
throw ToolRuntimeError(
QString("Error: Could not create file '%1': %2").arg(filePath, file.errorString()));
QString("Error: Could not create file '%1': %2").arg(absolutePath, file.errorString()));
}
file.close();
LOG_MESSAGE(QString("Successfully created new file: %1").arg(filePath));
LOG_MESSAGE(QString("Successfully created new file: %1").arg(absolutePath));
return QString("Successfully created new file: %1").arg(filePath);
return QString("Successfully created new file: %1").arg(absolutePath);
});
}

284
tools/EditFileTool.cpp Normal file
View File

@ -0,0 +1,284 @@
/*
* 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 "EditFileTool.hpp"
#include "ToolExceptions.hpp"
#include <context/ProjectUtils.hpp>
#include <coreplugin/documentmanager.h>
#include <logger/Logger.hpp>
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <settings/GeneralSettings.hpp>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QRandomGenerator>
#include <QTextStream>
#include <QtConcurrent>
namespace QodeAssist::Tools {
EditFileTool::EditFileTool(QObject *parent)
: BaseTool(parent)
, m_ignoreManager(new Context::IgnoreManager(this))
{}
QString EditFileTool::name() const
{
return "edit_file";
}
QString EditFileTool::stringName() const
{
return {"Editing file"};
}
QString EditFileTool::description() const
{
return "Edit a project file with two distinct modes of operation. "
"IMPORTANT: All text fields must contain COMPLETE LINES with trailing newlines (\\n). "
"If you forget to add \\n, it will be added automatically.\n"
"\n"
"TWO MODES OF OPERATION:\n"
"\n"
"1. REPLACE MODE (mode='replace'):\n"
" - Finds search_text in the file and replaces it with new_text\n"
" - REQUIRED: search_text (the text to find and replace)\n"
" - OPTIONAL: line_before and/or line_after for context verification\n"
" - Context verification strategy:\n"
" * line_before/line_after are searched NEAR search_text (within ~500 chars)\n"
" * They do NOT need to be immediately adjacent to search_text\n"
" * Both provided: BOTH must be found near search_text (most precise)\n"
" * Only line_before: must be found BEFORE search_text\n"
" * Only line_after: must be found AFTER search_text\n"
" * Neither: accepts first occurrence (use when search_text is unique)\n"
" - Use this to replace existing content\n"
"\n"
"2. INSERT_AFTER MODE (mode='insert_after'):\n"
" - Inserts new_text after the line specified in line_before\n"
" - If line_before is empty, inserts at the beginning of the file (useful for empty files)\n"
" - OPTIONAL: line_before (the line to insert after; empty = insert at start)\n"
" - OPTIONAL: line_after (must IMMEDIATELY follow line_before for verification)\n"
" - search_text is IGNORED\n"
" - Use this to insert content after a specific line or at the start of file\n"
" - Note: In this mode, new_text is inserted RIGHT AFTER line_before\n"
"\n"
"BEST PRACTICES for multiple edits to the same file:\n"
"- Use INSERT_AFTER mode for sequential additions (each edit uses previous addition as line_before)\n"
"- Provide stable context lines that won't be modified by current or subsequent edits\n"
"- For empty files: use INSERT_AFTER with empty line_before\n"
"- Example: When adding multiple class properties:\n"
" * First edit: INSERT_AFTER with class declaration as line_before\n"
" * Second edit: INSERT_AFTER with first property as line_before\n"
" * Third edit: INSERT_AFTER with second property as line_before\n"
"\n"
"Parameters:\n"
"- mode: 'replace' or 'insert_after'\n"
"- filepath: absolute or relative file path to edit\n"
"- new_text: complete line(s) to insert/replace (with \\n)\n"
"- search_text: (replace mode only) complete line(s) to find and replace (with \\n)\n"
"- line_before: complete line for context (with \\n), usage depends on mode\n"
"- line_after: complete line for context (with \\n), usage depends on mode";
}
QJsonObject EditFileTool::getDefinition(LLMCore::ToolSchemaFormat format) const
{
QJsonObject properties;
QJsonObject modeProperty;
modeProperty["type"] = "string";
modeProperty["enum"] = QJsonArray({"replace", "insert_after"});
modeProperty["description"] = "Edit mode: 'replace' (replace search_text with new_text), "
"'insert_after' (insert new_text after line_before)";
properties["mode"] = modeProperty;
QJsonObject filepathProperty;
filepathProperty["type"] = "string";
filepathProperty["description"] = "The absolute or relative file path to edit";
properties["filepath"] = filepathProperty;
QJsonObject newTextProperty;
newTextProperty["type"] = "string";
newTextProperty["description"] = "Complete line(s) to insert/replace/append. Trailing newline (\\n) auto-added if missing. "
"Example: 'int main(int argc, char *argv[]) {\\n' or 'void foo();\\nvoid bar();\\n'";
properties["new_text"] = newTextProperty;
QJsonObject searchTextProperty;
searchTextProperty["type"] = "string";
searchTextProperty["description"]
= "Complete line(s) to search for and replace. Trailing newline (\\n) auto-added if missing. "
"REQUIRED for 'replace' mode, IGNORED for other modes. "
"Example: 'int main() {\\n' or 'void foo();\\n'";
properties["search_text"] = searchTextProperty;
QJsonObject lineBeforeProperty;
lineBeforeProperty["type"] = "string";
lineBeforeProperty["description"] = "Complete line for context verification. Trailing newline (\\n) auto-added if missing. "
"Usage depends on mode:\n"
"- 'replace': OPTIONAL, searched BEFORE search_text (within ~500 chars, not necessarily adjacent)\n"
"- 'insert_after': OPTIONAL, new_text inserted RIGHT AFTER this line. "
"If empty, inserts at the beginning of the file (useful for empty files)\n"
"Example: 'class Movie {\\n' or '#include <iostream>\\n'";
properties["line_before"] = lineBeforeProperty;
QJsonObject lineAfterProperty;
lineAfterProperty["type"] = "string";
lineAfterProperty["description"] = "Complete line for context verification. Trailing newline (\\n) auto-added if missing. "
"Usage depends on mode:\n"
"- 'replace': OPTIONAL, searched AFTER search_text (within ~500 chars, not necessarily adjacent)\n"
"- 'insert_after': OPTIONAL, must IMMEDIATELY follow line_before for verification\n"
"Example: '}\\n' or 'public:\\n'";
properties["line_after"] = lineAfterProperty;
QJsonObject definition;
definition["type"] = "object";
definition["properties"] = properties;
QJsonArray required;
required.append("mode");
required.append("filepath");
required.append("new_text");
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);
case LLMCore::ToolSchemaFormat::Google:
return customizeForGoogle(definition);
}
return definition;
}
LLMCore::ToolPermissions EditFileTool::requiredPermissions() const
{
return LLMCore::ToolPermissions(LLMCore::ToolPermission::FileSystemRead)
| LLMCore::ToolPermissions(LLMCore::ToolPermission::FileSystemWrite);
}
QFuture<QString> EditFileTool::executeAsync(const QJsonObject &input)
{
return QtConcurrent::run([this, input]() -> QString {
QString mode = input["mode"].toString();
if (mode.isEmpty()) {
throw ToolInvalidArgument("Error: mode parameter is required. Must be one of: 'replace', 'insert_after'");
}
if (mode != "replace" && mode != "insert_after") {
throw ToolInvalidArgument(QString("Error: invalid mode '%1'. Must be one of: 'replace', 'insert_after'").arg(mode));
}
QString inputFilepath = input["filepath"].toString();
if (inputFilepath.isEmpty()) {
throw ToolInvalidArgument("Error: filepath parameter is required");
}
QString newText = input["new_text"].toString();
if (newText.isEmpty()) {
throw ToolInvalidArgument("Error: new_text parameter is required");
}
QString searchText = input["search_text"].toString();
QString lineBefore = input["line_before"].toString();
QString lineAfter = input["line_after"].toString();
if (mode == "replace" && searchText.isEmpty()) {
throw ToolInvalidArgument("Error: search_text is required for 'replace' mode");
}
// Normalize text fields: ensure trailing newline if not empty
// This handles cases where LLM forgets to add \n
auto normalizeText = [](QString &text) {
if (!text.isEmpty() && !text.endsWith('\n')) {
LOG_MESSAGE(QString("EditFileTool: normalizing text, adding trailing newline (length: %1)").arg(text.length()));
text += '\n';
}
};
normalizeText(newText);
if (!searchText.isEmpty()) normalizeText(searchText);
if (!lineBefore.isEmpty()) normalizeText(lineBefore);
if (!lineAfter.isEmpty()) normalizeText(lineAfter);
QString filePath;
QFileInfo fileInfo(inputFilepath);
if (fileInfo.isAbsolute()) {
filePath = inputFilepath;
} else {
auto projects = ProjectExplorer::ProjectManager::projects();
if (!projects.isEmpty() && projects.first()) {
QString projectDir = projects.first()->projectDirectory().toUrlishString();
filePath = QDir(projectDir).absoluteFilePath(inputFilepath);
} else {
filePath = QFileInfo(inputFilepath).absoluteFilePath();
}
}
if (!QFileInfo::exists(filePath)) {
throw ToolRuntimeError(QString("Error: File '%1' does not exist").arg(filePath));
}
bool isInProject = Context::ProjectUtils::isFileInProject(filePath);
if (!isInProject) {
const auto &settings = Settings::generalSettings();
if (!settings.allowAccessOutsideProject()) {
throw ToolRuntimeError(
QString("Error: File '%1' is outside the project scope. "
"Enable 'Allow file access outside project' in settings to edit files outside project scope.")
.arg(filePath));
}
LOG_MESSAGE(QString("Editing file outside project scope: %1").arg(filePath));
}
auto project = isInProject ? ProjectExplorer::ProjectManager::projectForFile(
Utils::FilePath::fromString(filePath)) : nullptr;
if (project && m_ignoreManager->shouldIgnore(filePath, project)) {
throw ToolRuntimeError(
QString("Error: File '%1' is excluded by .qodeassistignore").arg(inputFilepath));
}
QJsonObject result;
result["type"] = "file_edit";
result["mode"] = mode;
result["filepath"] = filePath;
result["new_text"] = newText;
result["search_text"] = searchText;
result["line_before"] = lineBefore;
result["line_after"] = lineAfter;
QJsonDocument doc(result);
return QString("QODEASSIST_FILE_EDIT:%1")
.arg(QString::fromUtf8(doc.toJson(QJsonDocument::Compact)));
});
}
} // namespace QodeAssist::Tools

View File

@ -24,11 +24,11 @@
namespace QodeAssist::Tools {
class EditProjectFileTool : public LLMCore::BaseTool
class EditFileTool : public LLMCore::BaseTool
{
Q_OBJECT
public:
explicit EditProjectFileTool(QObject *parent = nullptr);
explicit EditFileTool(QObject *parent = nullptr);
QString name() const override;
QString stringName() const override;
@ -39,19 +39,6 @@ public:
QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override;
private:
enum class EditMode { Replace, InsertBefore, InsertAfter, AppendToEnd };
QString findFileInProject(const QString &fileName) const;
QString readFileContent(const QString &filePath) const;
void extractContext(
const QString &content,
EditMode mode,
const QString &searchText,
int lineNumber,
QString &contextBefore,
QString &contextAfter,
int contextLines = 3) const;
Context::IgnoreManager *m_ignoreManager;
};

View File

@ -1,376 +0,0 @@
/*
* 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 "EditProjectFileTool.hpp"
#include "ToolExceptions.hpp"
#include <coreplugin/documentmanager.h>
#include <logger/Logger.hpp>
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QRandomGenerator>
#include <QTextStream>
#include <QtConcurrent>
namespace QodeAssist::Tools {
EditProjectFileTool::EditProjectFileTool(QObject *parent)
: BaseTool(parent)
, m_ignoreManager(new Context::IgnoreManager(this))
{}
QString EditProjectFileTool::name() const
{
return "edit_project_file";
}
QString EditProjectFileTool::stringName() const
{
return {"Editing project file"};
}
QString EditProjectFileTool::description() const
{
return "Edit a project file using different modes: replace text, insert before/after line, or append. "
"Changes require user approval and show a diff preview. "
"Files excluded by .qodeassistignore cannot be edited. "
"Line numbers are 1-based.";
}
QJsonObject EditProjectFileTool::getDefinition(LLMCore::ToolSchemaFormat format) const
{
QJsonObject properties;
QJsonObject filenameProperty;
filenameProperty["type"] = "string";
filenameProperty["description"] = "The filename or relative path to edit";
properties["filename"] = filenameProperty;
QJsonObject modeProperty;
modeProperty["type"] = "string";
modeProperty["description"]
= "Edit mode: 'replace', 'insert_before', 'insert_after', or 'append'";
QJsonArray modeEnum;
modeEnum.append("replace");
modeEnum.append("insert_before");
modeEnum.append("insert_after");
modeEnum.append("append");
modeProperty["enum"] = modeEnum;
properties["mode"] = modeProperty;
QJsonObject searchTextProperty;
searchTextProperty["type"] = "string";
searchTextProperty["description"]
= "Text to search for and replace (required for 'replace' mode)";
properties["search_text"] = searchTextProperty;
QJsonObject newTextProperty;
newTextProperty["type"] = "string";
newTextProperty["description"] = "New text to insert or use as replacement";
properties["new_text"] = newTextProperty;
QJsonObject lineNumberProperty;
lineNumberProperty["type"] = "integer";
lineNumberProperty["description"]
= "Line number for insert operations (1-based, required for insert modes)";
properties["line_number"] = lineNumberProperty;
QJsonObject definition;
definition["type"] = "object";
definition["properties"] = properties;
QJsonArray required;
required.append("filename");
required.append("mode");
required.append("new_text");
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);
case LLMCore::ToolSchemaFormat::Google:
return customizeForGoogle(definition);
}
return definition;
}
LLMCore::ToolPermissions EditProjectFileTool::requiredPermissions() const
{
return LLMCore::ToolPermissions(LLMCore::ToolPermission::FileSystemRead)
| LLMCore::ToolPermissions(LLMCore::ToolPermission::FileSystemWrite);
}
QFuture<QString> EditProjectFileTool::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 ToolInvalidArgument(error);
}
QString modeStr = input["mode"].toString();
if (modeStr.isEmpty()) {
QString error = "Error: mode parameter is required";
throw ToolInvalidArgument(error);
}
EditMode mode;
if (modeStr == "replace") {
mode = EditMode::Replace;
} else if (modeStr == "insert_before") {
mode = EditMode::InsertBefore;
} else if (modeStr == "insert_after") {
mode = EditMode::InsertAfter;
} else if (modeStr == "append") {
mode = EditMode::AppendToEnd;
} else {
QString error = QString("Error: Invalid mode '%1'. Must be one of: replace, "
"insert_before, insert_after, append")
.arg(modeStr);
throw ToolInvalidArgument(error);
}
QString newText = input["new_text"].toString();
if (newText.isEmpty()) {
QString error = "Error: new_text parameter is required";
throw ToolInvalidArgument(error);
}
QString searchText = input["search_text"].toString();
if (mode == EditMode::Replace && searchText.isEmpty()) {
QString error = "Error: search_text parameter is required for replace mode";
throw ToolInvalidArgument(error);
}
int lineNumber = input["line_number"].toInt(0);
if ((mode == EditMode::InsertBefore || mode == EditMode::InsertAfter) && lineNumber <= 0) {
QString error = "Error: line_number parameter is required for insert modes and must "
"be greater than 0";
throw ToolInvalidArgument(error);
}
QString filePath = findFileInProject(filename);
if (filePath.isEmpty()) {
QString error = QString("Error: File '%1' not found in project").arg(filename);
throw ToolRuntimeError(error);
}
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 and cannot be edited")
.arg(filename);
throw ToolRuntimeError(error);
}
// readFileContent throws exception if file cannot be opened
QString originalContent = readFileContent(filePath);
LOG_MESSAGE(QString("Prepared file edit: %1 (mode: %2)").arg(filePath, modeStr));
QString editId = QString("edit_%1_%2")
.arg(QDateTime::currentMSecsSinceEpoch())
.arg(QRandomGenerator::global()->generate());
QString contextBefore, contextAfter;
extractContext(originalContent, mode, searchText, lineNumber, contextBefore, contextAfter);
QJsonObject result;
result["type"] = "file_edit";
result["edit_id"] = editId;
result["file_path"] = filePath;
result["mode"] = modeStr;
result["original_content"] = (mode == EditMode::Replace) ? searchText : "";
result["new_content"] = newText;
result["context_before"] = contextBefore;
result["context_after"] = contextAfter;
result["search_text"] = searchText;
result["line_number"] = lineNumber;
QJsonDocument doc(result);
return QString("QODEASSIST_FILE_EDIT:%1")
.arg(QString::fromUtf8(doc.toJson(QJsonDocument::Compact)));
});
}
QString EditProjectFileTool::findFileInProject(const QString &fileName) const
{
QList<ProjectExplorer::Project *> projects = ProjectExplorer::ProjectManager::projects();
if (projects.isEmpty()) {
LOG_MESSAGE("No projects found");
return QString();
}
struct FileMatch
{
QString path;
int priority; // 1 = exact filename, 2 = ends with, 3 = contains
};
QVector<FileMatch> matches;
for (auto project : projects) {
if (!project)
continue;
Utils::FilePaths projectFiles = project->files(ProjectExplorer::Project::SourceFiles);
for (const auto &projectFile : std::as_const(projectFiles)) {
QString absolutePath = projectFile.path();
if (m_ignoreManager->shouldIgnore(absolutePath, project)) {
continue;
}
QString baseName = projectFile.fileName();
if (baseName == fileName) {
return absolutePath;
}
if (projectFile.endsWith(fileName)) {
matches.append({absolutePath, 2});
} else if (baseName.contains(fileName, Qt::CaseInsensitive)) {
matches.append({absolutePath, 3});
}
}
}
if (!matches.isEmpty()) {
std::sort(matches.begin(), matches.end(), [](const FileMatch &a, const FileMatch &b) {
return a.priority < b.priority;
});
return matches.first().path;
}
return QString();
}
QString EditProjectFileTool::readFileContent(const QString &filePath) const
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
LOG_MESSAGE(QString("Could not open file for reading: %1, error: %2")
.arg(filePath, file.errorString()));
throw ToolRuntimeError(QString("Error: Could not open file '%1': %2")
.arg(filePath, file.errorString()));
}
QTextStream stream(&file);
stream.setAutoDetectUnicode(true);
QString content = stream.readAll();
file.close();
LOG_MESSAGE(QString("Successfully read file for edit: %1, size: %2 bytes")
.arg(filePath)
.arg(content.length()));
return content;
}
void EditProjectFileTool::extractContext(
const QString &content,
EditMode mode,
const QString &searchText,
int lineNumber,
QString &contextBefore,
QString &contextAfter,
int contextLines) const
{
contextBefore.clear();
contextAfter.clear();
QStringList lines = content.split('\n');
int targetLine = -1;
if (mode == EditMode::Replace && !searchText.isEmpty()) {
int bestMatch = -1;
int maxScore = -1;
QStringList searchLines = searchText.split('\n');
for (int i = 0; i < lines.size(); ++i) {
bool matches = true;
if (i + searchLines.size() > lines.size()) {
continue;
}
for (int j = 0; j < searchLines.size(); ++j) {
if (lines[i + j] != searchLines[j]) {
matches = false;
break;
}
}
if (matches) {
int score = 0;
for (int offset = 1; offset <= contextLines; ++offset) {
if (i - offset >= 0) score++;
if (i + searchLines.size() + offset - 1 < lines.size()) score++;
}
if (score > maxScore) {
maxScore = score;
bestMatch = i;
}
}
}
targetLine = bestMatch;
} else if (mode == EditMode::InsertBefore || mode == EditMode::InsertAfter) {
if (lineNumber > 0 && lineNumber <= lines.size()) {
targetLine = lineNumber - 1;
}
} else if (mode == EditMode::AppendToEnd) {
if (!lines.isEmpty()) {
int startLine = qMax(0, lines.size() - contextLines);
contextBefore = lines.mid(startLine).join('\n');
}
return;
}
if (targetLine == -1) {
return;
}
int startBefore = qMax(0, targetLine - contextLines);
int countBefore = targetLine - startBefore;
contextBefore = lines.mid(startBefore, countBefore).join('\n');
int startAfter = targetLine + 1;
int countAfter = qMin(contextLines, lines.size() - startAfter);
contextAfter = lines.mid(startAfter, countAfter).join('\n');
}
} // namespace QodeAssist::Tools

View File

@ -20,6 +20,7 @@
#include "FindFileTool.hpp"
#include "ToolExceptions.hpp"
#include <context/ProjectUtils.hpp>
#include <logger/Logger.hpp>
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
@ -132,14 +133,14 @@ QFuture<QString> FindFileTool::executeAsync(const QJsonObject &input)
QFileInfo queryInfo(query);
if (queryInfo.isAbsolute() && queryInfo.exists() && queryInfo.isFile()) {
QString canonicalPath = queryInfo.canonicalFilePath();
bool isInProject = isFileInProject(canonicalPath);
bool isInProject = Context::ProjectUtils::isFileInProject(canonicalPath);
// Check if reading outside project is allowed
if (!isInProject) {
const auto &settings = Settings::generalSettings();
if (!settings.allowReadOutsideProject()) {
if (!settings.allowAccessOutsideProject()) {
QString error = QString("Error: File '%1' exists but is outside the project scope. "
"Enable 'Allow reading files outside project' in settings to access this file.")
"Enable 'Allow file access outside project' in settings to access files outside project scope.")
.arg(canonicalPath);
throw std::runtime_error(error.toStdString());
}
@ -426,29 +427,4 @@ QString FindFileTool::formatResults(const QList<FileMatch> &matches,
return result.trimmed();
}
bool FindFileTool::isFileInProject(const QString &filePath) const
{
QList<ProjectExplorer::Project *> projects = ProjectExplorer::ProjectManager::projects();
Utils::FilePath targetPath = Utils::FilePath::fromString(filePath);
for (auto project : projects) {
if (!project)
continue;
Utils::FilePaths projectFiles = project->files(ProjectExplorer::Project::SourceFiles);
for (const auto &projectFile : std::as_const(projectFiles)) {
if (projectFile == targetPath) {
return true;
}
}
Utils::FilePath projectDir = project->projectDirectory();
if (targetPath.isChildOf(projectDir)) {
return true;
}
}
return false;
}
} // namespace QodeAssist::Tools

View File

@ -66,7 +66,6 @@ private:
int &currentDepth,
int maxDepth = 10) const;
QString formatResults(const QList<FileMatch> &matches, int totalFound, int maxResults) const;
bool isFileInProject(const QString &filePath) const;
bool matchesFilePattern(const QString &fileName, const QString &pattern) const;
static constexpr int DEFAULT_MAX_RESULTS = 50;

View File

@ -20,6 +20,7 @@
#include "ReadFilesByPathTool.hpp"
#include "ToolExceptions.hpp"
#include <context/ProjectUtils.hpp>
#include <coreplugin/documentmanager.h>
#include <logger/Logger.hpp>
#include <projectexplorer/project.h>
@ -137,31 +138,6 @@ QFuture<QString> ReadFilesByPathTool::executeAsync(const QJsonObject &input)
});
}
bool ReadFilesByPathTool::isFileInProject(const QString &filePath) const
{
QList<ProjectExplorer::Project *> projects = ProjectExplorer::ProjectManager::projects();
Utils::FilePath targetPath = Utils::FilePath::fromString(filePath);
for (auto project : projects) {
if (!project)
continue;
Utils::FilePaths projectFiles = project->files(ProjectExplorer::Project::SourceFiles);
for (const auto &projectFile : std::as_const(projectFiles)) {
if (projectFile == targetPath) {
return true;
}
}
Utils::FilePath projectDir = project->projectDirectory();
if (targetPath.isChildOf(projectDir)) {
return true;
}
}
return false;
}
QString ReadFilesByPathTool::readFileContent(const QString &filePath) const
{
QFile file(filePath);
@ -206,15 +182,15 @@ ReadFilesByPathTool::FileResult ReadFilesByPathTool::processFile(const QString &
QString canonicalPath = fileInfo.canonicalFilePath();
LOG_MESSAGE(QString("Canonical path: %1").arg(canonicalPath));
bool isInProject = isFileInProject(canonicalPath);
bool isInProject = Context::ProjectUtils::isFileInProject(canonicalPath);
if (!isInProject) {
const auto &settings = Settings::generalSettings();
if (!settings.allowReadOutsideProject()) {
if (!settings.allowAccessOutsideProject()) {
result.error = QString(
"File is not part of the project. "
"Enable 'Allow reading files outside project' in settings "
"to access this file.");
"Enable 'Allow file access outside project' in settings "
"to read files outside project scope.");
return result;
}
LOG_MESSAGE(QString("Reading file outside project scope: %1").arg(canonicalPath));

View File

@ -48,7 +48,6 @@ private:
};
QString readFileContent(const QString &filePath) const;
bool isFileInProject(const QString &filePath) const;
FileResult processFile(const QString &filePath) const;
QString formatResults(const QList<FileResult> &results) const;
Context::IgnoreManager *m_ignoreManager;

View File

@ -25,7 +25,7 @@
#include <QJsonObject>
#include "CreateNewFileTool.hpp"
#include "EditProjectFileTool.hpp"
#include "EditFileTool.hpp"
#include "FindFileTool.hpp"
#include "FindSymbolTool.hpp"
#include "GetIssuesListTool.hpp"
@ -49,7 +49,7 @@ void ToolsFactory::registerTools()
registerTool(new ListProjectFilesTool(this));
registerTool(new SearchInProjectTool(this));
registerTool(new GetIssuesListTool(this));
registerTool(new EditProjectFileTool(this));
registerTool(new EditFileTool(this));
registerTool(new FindSymbolTool(this));
registerTool(new FindFileTool(this));
registerTool(new CreateNewFileTool(this));

View File

@ -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;
}

View File

@ -39,6 +39,13 @@ struct PendingTool
bool complete = false;
};
struct ToolQueue
{
QList<PendingTool> queue;
QHash<QString, PendingTool> completed;
bool isExecuting = false;
};
class ToolsManager : public QObject
{
Q_OBJECT
@ -67,9 +74,9 @@ private slots:
private:
ToolsFactory *m_toolsFactory;
ToolHandler *m_toolHandler;
QHash<QString, QHash<QString, PendingTool>> m_pendingTools;
QHash<QString, ToolQueue> m_toolQueues;
bool isExecutionComplete(const QString &requestId) const;
void executeNextTool(const QString &requestId);
QHash<QString, QString> getToolResults(const QString &requestId) const;
};