execute method
Execute the tool with the given input.
Implementation
@override
Future<ToolResult> execute(Map<String, dynamic> input) async {
final validation = validateInput(input);
if (!validation.isValid) return ToolResult.error(validation.error!);
final parsed = DiffApplyInput.fromJson(input);
final file = File(parsed.filePath);
if (!await file.exists()) {
return ToolResult.error('File not found: ${parsed.filePath}');
}
try {
final originalLines = await file.readAsLines();
final patchedLines = _applyUnifiedDiff(originalLines, parsed.diffContent);
final newContent = patchedLines.join('\n');
await file.writeAsString(
newContent.endsWith('\n') ? newContent : '$newContent\n',
);
final changed =
(patchedLines.length - originalLines.length).abs() +
_countChangedLines(originalLines, patchedLines);
final out = DiffApplyOutput(
success: true,
linesChanged: changed,
newContent: newContent,
);
return ToolResult.success(out.toString());
} catch (e) {
return ToolResult.error('Error applying diff: $e');
}
}