call method

Future<R> call(
  1. dynamic args
)

Implementation

Future<R> call(dynamic args) async {
  Map<String, dynamic> namedArgsToFill;
  // ... (argument parsing logic - same as your original)
  if (args is Map<String, dynamic>)
    namedArgsToFill = args;
  else if (args is List<dynamic>) {
    // ...
    if (args.length != _orderedPlaceholdersInTemplate.length &&
        !(_orderedPlaceholdersInTemplate.isEmpty && args.isEmpty)) {
      // Added check for empty template and empty args
      throw ArgumentError(
        "Fluentic (List input): Expected ${_orderedPlaceholdersInTemplate.length} args, got ${args.length}.",
      );
    }
    namedArgsToFill = {};
    for (int i = 0; i < _orderedPlaceholdersInTemplate.length; i++)
      namedArgsToFill[_orderedPlaceholdersInTemplate[i]] = args[i];
  } else if (args == null ||
      (args is Map && args.isEmpty) ||
      (args is List && args.isEmpty)) {
    if (_orderedPlaceholdersInTemplate.isNotEmpty && args == null) {
      // Only throw if null was passed AND placeholders exist
      throw ArgumentError(
        "Fluentic: Template has placeholders but null arguments were provided.",
      );
    }
    namedArgsToFill = {};
  } else {
    throw ArgumentError(
      'Fluentic: Invalid argument type: ${args.runtimeType}.',
    );
  }

  String finalPrompt = _generateFullPrompt(namedArgsToFill);
  dev.log("DEBUG: Fluentic<$R> Final prompt for LLM:\n$finalPrompt");
  dynamic llmPayload = await _llmClient.executePrompt(finalPrompt);
  dev.log("DEBUG: Fluentic<$R> Received LLM payload: $llmPayload");
  try {
    return _parseResponse(llmPayload);
  } catch (e, s) {
    dev.log("Fluentic Error parsing LLM payload for $R: $e");
    dev.log("LLMPayload was: '$llmPayload'");
    dev.log("Stack trace for parsing error:\n$s");
    rethrow;
  }
}