createReadPubspecTool function

Tool<Object, ToolOptions, Object> createReadPubspecTool({
  1. required String projectDirectory,
})

Implementation

Tool createReadPubspecTool({required String projectDirectory}) {
  return Tool.fromFunction<ReadPubspecInput, String>(
    name: 'read_pubspec',
    description:
        'Read and parse the project\'s pubspec.yaml file to understand current state. '
        'Returns: project name, version, description, SDK constraints, all dependencies, and dev dependencies. '
        'CRITICAL: Call this FIRST before any package recommendations to understand existing dependencies, '
        'avoid duplicates, and check for potential conflicts. Essential for maintaining project integrity.',
    inputJsonSchema: object({}).toJsonSchema(),
    func: (ReadPubspecInput input) async {
      final pubspecFile = File(path.join(projectDirectory, 'pubspec.yaml'));

      if (!pubspecFile.existsSync()) {
        return 'Error: pubspec.yaml not found in $projectDirectory';
      }

      final content = await pubspecFile.readAsString();
      final yaml = loadYaml(content) as Map;

      final dependencies = _extractDependencies(yaml['dependencies']);
      final devDependencies = _extractDependencies(yaml['dev_dependencies']);

      final result = '''
Project Name: ${yaml['name'] ?? 'unknown'}
Version: ${yaml['version']?.toString() ?? 'unknown'}
Description: ${yaml['description'] ?? 'No description'}
SDK Constraint: ${yaml['environment']?['sdk']?.toString() ?? 'unknown'}

Dependencies (${dependencies.length}):
${dependencies.entries.map((e) => '  ${e.key}: ${e.value}').join('\n')}

Dev Dependencies (${devDependencies.length}):
${devDependencies.entries.map((e) => '  ${e.key}: ${e.value}').join('\n')}
''';

      return result;
    },
    getInputFromJson: (json) => ReadPubspecInput.fromJson(json),
    handleToolError: (e) {
      return 'Read pubspec tool error : ${e.toString()}';
    },
  );
}