Content.fromJson constructor

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

Creates content from JSON-compatible values.

Implementation

factory Content.fromJson(Map<String, Object?> json) {
  return switch (json['type']) {
    'text' => TextContent(json['text']?.toString() ?? ''),
    'image' when json['blob'] is String => ImageBytesContent(
      base64Decode(json['blob'] as String),
    ),
    'image' => ImageFileContent(json['path']?.toString() ?? ''),
    'audio' when json['blob'] is String => AudioBytesContent(
      base64Decode(json['blob'] as String),
    ),
    'audio' => AudioFileContent(json['path']?.toString() ?? ''),
    'tool_response' => ToolResponseContent(
      name: json['name']?.toString() ?? '',
      response: json['response'],
    ),
    final type => throw FormatException('Unsupported content type: $type'),
  };
}