toJson method
Converts a ChatMessage to a JSON map representation.
The map contains the following keys:
- 'origin': The origin of the message (user or model).
- 'text': The text content of the message.
- 'attachments': A list of attachments, each represented as a map with:
- 'type': The type of the attachment ('file' or 'link').
- 'name': The name of the attachment.
- 'mimeType': The MIME type of the attachment.
- 'data': The data of the attachment, either as a base64 encoded string (for files) or a URL (for links).
Implementation
Map<String, dynamic> toJson() => {
'origin': origin.name,
'text': text,
'attachments': [
for (final attachment in attachments)
{
'type': switch (attachment) {
(FileAttachment _) => 'file',
(LinkAttachment _) => 'link',
},
'name': attachment.name,
'mimeType': switch (attachment) {
(final FileAttachment a) => a.mimeType,
(final LinkAttachment a) => a.mimeType,
},
'data': switch (attachment) {
(final FileAttachment a) => base64Encode(a.bytes),
(final LinkAttachment a) => a.url,
},
},
],
};