validateAndGetCallable method

Function validateAndGetCallable(
  1. Map<String, dynamic> arguments
)

Validates and returns the registered tool function with the given arguments. arguments: A map where keys are parameter names and values are their corresponding arguments. The names must match the parameter names of the function. Example:

final weatherTool = GroqToolItem(
  functionName: 'get_weather',
  functionDescription: 'Get weather information for a specified location',
  parameters: [
    GroqToolParameter(
      parameterName: 'location',
      parameterDescription: 'City or location name',
      parameterType: GroqToolParameterType.string,
      isRequired: true,
    ),
    GroqToolParameter(
      parameterName: 'units',
      parameterDescription: 'Temperature units (metric or imperial)',
      parameterType: GroqToolParameterType.string,
      isRequired: false,
      allowedValues: ['metric', 'imperial'],
    ),
  ],
  function: (args) {
    final location = args['location'] as String;
    final units = args['units'] as String? ?? 'metric';
    return {
      'location': location,
      'temperature': units == 'metric' ? 22 : 71.6,
      'units': units,
    };
  },
);
try {
  final result = weatherTool.execute({
    'location': 'London',
    'units': 'metric',
  });
  print('Weather result: $result');
} catch (e) {
  print('Error: $e');
}

Validates the arguments and returns a callable function.

arguments: A map where keys are parameter names and values are their corresponding arguments.

Returns a callable function that, when invoked, executes the registered function.

Implementation

Function validateAndGetCallable(Map<String, dynamic> arguments) {
  for (var param in parameters) {
    if (param.isRequired && !arguments.containsKey(param.parameterName)) {
      print(
          'Missing required parameter: ${param.parameterName} in $arguments');
      throw ArgumentError(
          'Missing required parameter: ${param.parameterName}');
    }

    if (arguments.containsKey(param.parameterName)) {
      final value = arguments[param.parameterName];

      // Validate type
      switch (param.parameterType) {
        case GroqToolParameterType.string:
          if (value is! String) {
            throw ArgumentError(
                'Parameter ${param.parameterName} must be a String');
          }
          break;
        case GroqToolParameterType.number:
          if (value is! num) {
            throw ArgumentError(
                'Parameter ${param.parameterName} must be a Number');
          }
          break;
        case GroqToolParameterType.boolean:
          if (value is! bool) {
            throw ArgumentError(
                'Parameter ${param.parameterName} must be a Boolean');
          }
          break;
      }

      // Validate allowed values
      if (param.allowedValues.isNotEmpty &&
          !param.allowedValues.contains(value)) {
        throw ArgumentError(
            'Parameter ${param.parameterName} must be one of ${param.allowedValues}');
      }
    }
  }

  // Return a callable function
  return () => function(arguments);
}