ToolCall.fromJson constructor

ToolCall.fromJson(
  1. Map<String, Object?> json
)

Creates a tool call from JSON-compatible values.

Implementation

factory ToolCall.fromJson(Map<String, Object?> json) {
  final function = json['function'];
  if (function is! Map<String, Object?>) {
    throw FormatException('Unsupported tool call JSON: $json');
  }
  final argumentsValue = function['arguments'];
  var arguments = const <String, Object?>{};
  if (argumentsValue is Map) {
    arguments = {
      for (final entry in argumentsValue.entries)
        entry.key.toString(): entry.value,
    };
  } else if (argumentsValue is String && argumentsValue.isNotEmpty) {
    final decoded = jsonDecode(argumentsValue);
    if (decoded is Map) {
      arguments = {
        for (final entry in decoded.entries)
          entry.key.toString(): entry.value,
      };
    }
  }
  return ToolCall(
    name: function['name']?.toString() ?? '',
    arguments: arguments,
  );
}