MCPcopy Create free account
hub / github.com/Palm1r/QodeAssist / executeAsync

Method executeAsync

tools/CreateNewFileTool.cpp:64–125  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

62}
63
64QFuture<LLMQore::ToolResult> CreateNewFileTool::executeAsync(const QJsonObject &input)
65{
66 return QtConcurrent::run([this, input]() -> LLMQore::ToolResult {
67 QString filePath = input["filepath"].toString();
68
69 if (filePath.isEmpty()) {
70 throw LLMQore::ToolInvalidArgument("Error: 'filepath' parameter is required");
71 }
72
73 QFileInfo fileInfo(filePath);
74 QString absolutePath = fileInfo.absoluteFilePath();
75
76 bool isInProject = Context::ProjectUtils::isFileInProject(absolutePath);
77
78 if (!isInProject) {
79 const auto &settings = Settings::toolsSettings();
80 if (!settings.allowAccessOutsideProject()) {
81 const QString projectRoot = Context::ProjectUtils::getProjectRoot();
82 const QString hint = projectRoot.isEmpty()
83 ? QStringLiteral(
84 "No project is currently open. Open a project in Qt Creator or "
85 "enable 'Allow file access outside project' in QodeAssist settings.")
86 : QString(
87 "Retry with a path under the active project root: '%1'. The build "
88 "directory is for compiler output only and cannot accept new source "
89 "files. If you really need to write outside the project, enable "
90 "'Allow file access outside project' in QodeAssist settings.")
91 .arg(projectRoot);
92 throw LLMQore::ToolRuntimeError(
93 QString("Error: File path '%1' is not within the current project. %2")
94 .arg(absolutePath, hint));
95 }
96 LOG_MESSAGE(QString("Creating file outside project scope: %1").arg(absolutePath));
97 }
98
99 if (fileInfo.exists()) {
100 throw LLMQore::ToolRuntimeError(
101 QString("Error: File already exists at path '%1'").arg(filePath));
102 }
103
104 QDir dir = fileInfo.absoluteDir();
105 if (!dir.exists()) {
106 if (!dir.mkpath(".")) {
107 throw LLMQore::ToolRuntimeError(
108 QString("Error: Could not create directory: '%1'").arg(dir.absolutePath()));
109 }
110 LOG_MESSAGE(QString("Created directory path: %1").arg(dir.absolutePath()));
111 }
112
113 QFile file(absolutePath);
114 if (!file.open(QIODevice::WriteOnly)) {
115 throw LLMQore::ToolRuntimeError(
116 QString("Error: Could not create file '%1': %2").arg(absolutePath, file.errorString()));
117 }
118
119 file.close();
120
121 LOG_MESSAGE(QString("Successfully created new file: %1").arg(absolutePath));

Callers

nothing calls this directly

Calls 4

ToolInvalidArgumentClass · 0.85
ToolRuntimeErrorClass · 0.85
toStringMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected