run method

  1. @override
Future<ToolResult> run(
  1. Map<String, dynamic> input
)
override

Executes the tool with the given input map.

Should never throw — return ToolResult.failure on errors.

Implementation

@override
Future<ToolResult> run(Map<String, dynamic> input) async {
  final path = (input['path'] as String? ?? '').trim();
  final content = (input['content'] as String? ?? '');

  if (path.isEmpty) {
    return ToolResult.failure(name, 'Input "path" must not be empty.');
  }

  if (allowedDirectory != null && !path.startsWith(allowedDirectory!)) {
    return ToolResult.failure(
        name, 'Access denied: path must be within "$allowedDirectory".');
  }

  _logger.info('Writing file: $path (${content.length} chars)');

  try {
    final file = File(path);
    await file.parent.create(recursive: true);
    await file.writeAsString(content);
    return ToolResult.success(name, 'Written ${content.length} bytes to $path',
        metadata: {'path': path, 'bytes': content.length});
  } catch (e) {
    return ToolResult.failure(name, 'File write error: $e');
  }
}