ToolContent.fromJson constructor

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

Reads one of the protocol's content objects: text, image, audio, resource and resource_link by their own classes, anything else as ToolOtherContent.

Implementation

factory ToolContent.fromJson(Map<String, Object?> json) =>
    switch (json['type']) {
      'text' when json['text'] is String => ToolText(json['text']! as String),
      'image' when json['data'] is String => ToolImage(
        base64Decode(json['data']! as String),
        mimeType: json['mimeType'] as String? ?? 'image/png',
      ),
      'audio' when json['data'] is String && json['mimeType'] is String =>
        ToolAudio(
          base64Decode(json['data']! as String),
          mimeType: json['mimeType']! as String,
        ),
      'resource' => switch (_object(json['resource'])) {
        final Map<String, Object?> r when r['uri'] is String => ToolResource(
          uri: r['uri']! as String,
          mimeType: r['mimeType'] as String?,
          text: r['text'] as String?,
          blob: switch (r['blob']) {
            final String data => base64Decode(data),
            _ => null,
          },
        ),
        _ => ToolOtherContent(json),
      },
      'resource_link' when json['uri'] is String && json['name'] is String =>
        ToolResourceLink(
          uri: json['uri']! as String,
          name: json['name']! as String,
          description: json['description'] as String?,
          mimeType: json['mimeType'] as String?,
        ),
      _ => ToolOtherContent(json),
    };