decodeToolInput function

Map<String, Object?> decodeToolInput(
  1. Object? raw
)

Decodes a provider's tool-call arguments, which arrive as a JSON string in the OpenAI shape and as an object in the Anthropic one.

Implementation

Map<String, Object?> decodeToolInput(Object? raw) {
  if (raw == null) return const {};
  if (raw is Map) return raw.cast<String, Object?>();
  if (raw is String) {
    if (raw.trim().isEmpty) return const {};
    try {
      final decoded = jsonDecode(raw);
      if (decoded is Map) return decoded.cast<String, Object?>();
    } on FormatException {
      // A model can emit invalid JSON, especially when truncated; report it
      // as an empty input so the tool's own validation explains the problem.
      return const {};
    }
  }
  return const {};
}