in agents/agents-ext/src/commonMain/kotlin/ai/koog/agents/ext/tool/file/EditFileTool.kt [208:228]
override suspend fun execute(args: Args): Result {
val path = fs.fromAbsolutePathString(args.path)
if (fs.exists(path)) {
val fileContentType = fs.getFileContentType(path)
validate(fileContentType == FileMetadata.FileContentType.Text) {
"Can not edit non-text files, tried editing: $path, which is a $fileContentType"
}
}
val content = if (fs.exists(path)) fs.readText(path) else ""
val patch = FilePatch(args.original, args.replacement)
val patchApplyResult = applyTokenNormalizedPatch(content, patch)
if (patchApplyResult.isSuccess()) {
fs.writeText(path, patchApplyResult.updatedContent)
logger.info { "Patch was applied" }
} else {
logger.info { "Patch was NOT applied because of: ${patchApplyResult.reason}" }
}
return Result(patchApplyResult)
}