Files
zhinian_manage/lib/models/message.dart
2026-05-15 14:41:37 +08:00

60 lines
1.6 KiB
Dart

enum MessageType { text, markdown, image, loading }
enum MessageSender { user, ai }
class ChatMessage {
final String id;
final String content;
final MessageType type;
final MessageSender sender;
final DateTime timestamp;
final List<String>? imageUrls;
const ChatMessage({
required this.id,
required this.content,
required this.type,
required this.sender,
required this.timestamp,
this.imageUrls,
});
ChatMessage copyWith({
String? id,
String? content,
MessageType? type,
MessageSender? sender,
DateTime? timestamp,
List<String>? imageUrls,
}) {
return ChatMessage(
id: id ?? this.id,
content: content ?? this.content,
type: type ?? this.type,
sender: sender ?? this.sender,
timestamp: timestamp ?? this.timestamp,
imageUrls: imageUrls ?? this.imageUrls,
);
}
Map<String, dynamic> toJson() => {
'id': id,
'content': content,
'type': type.name,
'sender': sender.name,
'timestamp': timestamp.toIso8601String(),
'imageUrls': imageUrls,
};
factory ChatMessage.fromJson(Map<String, dynamic> json) => ChatMessage(
id: json['id'] ?? '',
content: json['content'] ?? '',
type: MessageType.values.byName(json['type'] ?? 'text'),
sender: MessageSender.values.byName(json['sender'] ?? 'user'),
timestamp: DateTime.tryParse(json['timestamp'] ?? '') ?? DateTime.now(),
imageUrls: json['imageUrls'] != null
? List<String>.from(json['imageUrls'])
: null,
);
}