MCPcopy Create free account
hub / github.com/anus-dev/ANUS / execute

Method execute

packages/core/src/tools/write-file.ts:222–379  ·  view source on GitHub ↗
(abortSignal: AbortSignal)

Source from the content-addressed store, hash-verified

220 }
221
222 async execute(abortSignal: AbortSignal): Promise<ToolResult> {
223 const { file_path, content, ai_proposed_content, modified_by_user } =
224 this.params;
225 const correctedContentResult = await getCorrectedFileContent(
226 this.config,
227 file_path,
228 content,
229 abortSignal,
230 );
231
232 if (correctedContentResult.error) {
233 const errDetails = correctedContentResult.error;
234 const errorMsg = errDetails.code
235 ? `Error checking existing file '${file_path}': ${errDetails.message} (${errDetails.code})`
236 : `Error checking existing file: ${errDetails.message}`;
237 return {
238 llmContent: errorMsg,
239 returnDisplay: errorMsg,
240 error: {
241 message: errorMsg,
242 type: ToolErrorType.FILE_WRITE_FAILURE,
243 },
244 };
245 }
246
247 const {
248 originalContent,
249 correctedContent: fileContent,
250 fileExists,
251 } = correctedContentResult;
252 // fileExists is true if the file existed (and was readable or unreadable but caught by readError).
253 // fileExists is false if the file did not exist (ENOENT).
254 const isNewFile =
255 !fileExists ||
256 (correctedContentResult.error !== undefined &&
257 !correctedContentResult.fileExists);
258
259 try {
260 const dirName = path.dirname(file_path);
261 if (!fs.existsSync(dirName)) {
262 fs.mkdirSync(dirName, { recursive: true });
263 }
264
265 fs.writeFileSync(file_path, fileContent, 'utf8');
266
267 // Generate diff for display result
268 const fileName = path.basename(file_path);
269 // If there was a readError, originalContent in correctedContentResult is '',
270 // but for the diff, we want to show the original content as it was before the write if possible.
271 // However, if it was unreadable, currentContentForDiff will be empty.
272 const currentContentForDiff = correctedContentResult.error
273 ? '' // Or some indicator of unreadable content
274 : originalContent;
275
276 const fileDiff = Diff.createPatch(
277 fileName,
278 currentContentForDiff,
279 fileContent,

Callers

nothing calls this directly

Calls 6

getCorrectedFileContentFunction · 0.85
getDiffStatFunction · 0.85
getSpecificMimeTypeFunction · 0.85
isNodeErrorFunction · 0.85
getDebugModeMethod · 0.80

Tested by

no test coverage detected