fix: Edit file tool take only absolute or relative path to file

This commit is contained in:
Petr Mironychev
2025-11-27 01:53:27 +01:00
parent 9b0ae98f02
commit 627a821115
4 changed files with 37 additions and 20 deletions

View File

@ -70,4 +70,18 @@ QString ProjectUtils::findFileInProject(const QString &filename)
return QString(); return QString();
} }
QString ProjectUtils::getProjectRoot()
{
QList<ProjectExplorer::Project *> projects = ProjectExplorer::ProjectManager::projects();
if (!projects.isEmpty()) {
auto project = projects.first();
if (project) {
return project->projectDirectory().toFSPathString();
}
}
return QString();
}
} // namespace QodeAssist::Context } // namespace QodeAssist::Context

View File

@ -52,6 +52,16 @@ public:
* @return Absolute file path if found, empty string otherwise * @return Absolute file path if found, empty string otherwise
*/ */
static QString findFileInProject(const QString &filename); static QString findFileInProject(const QString &filename);
/**
* @brief Get the project root directory
*
* Returns the root directory of the first open project.
* If multiple projects are open, returns the first one.
*
* @return Absolute path to project root, or empty string if no project is open
*/
static QString getProjectRoot();
}; };
} // namespace QodeAssist::Context } // namespace QodeAssist::Context

View File

@ -25,6 +25,7 @@
#include <logger/Logger.hpp> #include <logger/Logger.hpp>
#include <settings/GeneralSettings.hpp> #include <settings/GeneralSettings.hpp>
#include <settings/ToolsSettings.hpp> #include <settings/ToolsSettings.hpp>
#include <QDir>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QJsonDocument> #include <QJsonDocument>
@ -36,7 +37,6 @@ namespace QodeAssist::Tools {
EditFileTool::EditFileTool(QObject *parent) EditFileTool::EditFileTool(QObject *parent)
: BaseTool(parent) : BaseTool(parent)
, m_ignoreManager(new Context::IgnoreManager(this))
{} {}
QString EditFileTool::name() const QString EditFileTool::name() const
@ -52,11 +52,12 @@ QString EditFileTool::stringName() const
QString EditFileTool::description() const QString EditFileTool::description() const
{ {
return "Edit a file by replacing old content with new content. " return "Edit a file by replacing old content with new content. "
"Provide the filename (or absolute path), old_content to find and replace, " "Provide the file path (absolute or relative to project root), old_content to find and replace, "
"and new_content to replace it with. Changes are applied immediately if auto-apply " "and new_content to replace it with. Changes are applied immediately if auto-apply "
"is enabled in settings. The user can undo or reapply changes at any time. " "is enabled in settings. The user can undo or reapply changes at any time. "
"\n\nIMPORTANT:" "\n\nIMPORTANT:"
"\n- ALWAYS read the current file content before editing to ensure accuracy." "\n- ALWAYS read the current file content before editing to ensure accuracy."
"\n- Path can be absolute (e.g., /path/to/file.cpp) or relative to project root (e.g., src/main.cpp)."
"\n- For EMPTY files: use empty old_content (empty string or omit parameter)." "\n- For EMPTY files: use empty old_content (empty string or omit parameter)."
"\n- To append at the END of file: use empty old_content." "\n- To append at the END of file: use empty old_content."
"\n- To insert at the BEGINNING of a file (e.g., copyright header), you MUST provide " "\n- To insert at the BEGINNING of a file (e.g., copyright header), you MUST provide "
@ -77,8 +78,8 @@ QJsonObject EditFileTool::getDefinition(LLMCore::ToolSchemaFormat format) const
QJsonObject filenameProperty; QJsonObject filenameProperty;
filenameProperty["type"] = "string"; filenameProperty["type"] = "string";
filenameProperty["description"] filenameProperty["description"]
= "The filename or absolute path of the file to edit. If only filename is provided, " = "The path of the file to edit. Can be an absolute path (e.g., /path/to/file.cpp) "
"it will be searched in the project"; "or a relative path from the project root (e.g., src/main.cpp)";
properties["filename"] = filenameProperty; properties["filename"] = filenameProperty;
QJsonObject oldContentProperty; QJsonObject oldContentProperty;
@ -139,24 +140,22 @@ QFuture<QString> EditFileTool::executeAsync(const QJsonObject &input)
} }
QString filePath;
QFileInfo fileInfo(filename); QFileInfo fileInfo(filename);
QString filePath;
if (fileInfo.isAbsolute() && fileInfo.exists()) { if (fileInfo.isAbsolute()) {
filePath = filename; filePath = filename;
} else { } else {
FileSearchUtils::FileMatch match = FileSearchUtils::findBestMatch( QString projectRoot = Context::ProjectUtils::getProjectRoot();
filename, QString(), 10, m_ignoreManager); if (projectRoot.isEmpty()) {
if (match.absolutePath.isEmpty()) {
throw ToolRuntimeError( throw ToolRuntimeError(
QString("File '%1' not found in project. " QString("Cannot resolve relative path '%1': no project is open. "
"Please provide a valid filename or absolute path.") "Please provide an absolute path or open a project.")
.arg(filename)); .arg(filename));
} }
filePath = match.absolutePath; filePath = QDir(projectRoot).absoluteFilePath(filename);
LOG_MESSAGE(QString("EditFileTool: Found file '%1' at '%2'") LOG_MESSAGE(QString("EditFileTool: Resolved relative path '%1' to '%2'")
.arg(filename, filePath)); .arg(filename, filePath));
} }

View File

@ -19,9 +19,6 @@
#pragma once #pragma once
#include "FileSearchUtils.hpp"
#include <context/IgnoreManager.hpp>
#include <llmcore/BaseTool.hpp> #include <llmcore/BaseTool.hpp>
namespace QodeAssist::Tools { namespace QodeAssist::Tools {
@ -39,9 +36,6 @@ public:
LLMCore::ToolPermissions requiredPermissions() const override; LLMCore::ToolPermissions requiredPermissions() const override;
QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override; QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override;
private:
Context::IgnoreManager *m_ignoreManager;
}; };
} // namespace QodeAssist::Tools } // namespace QodeAssist::Tools