You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB

class Message {
final String role; // 'user' or 'assistant'
final String content;
final DateTime timestamp;
final bool isLoading;
Message({
required this.role,
required this.content,
required this.timestamp,
this.isLoading = false,
});
// 从JSON构造函数
factory Message.fromJson(Map<String, dynamic> json) {
return Message(
role: json['role'] as String,
content: json['content'] as String,
timestamp: DateTime.parse(json['timestamp'] as String),
isLoading: json['isLoading'] as bool? ?? false,
);
}
// 转换为JSON
Map<String, dynamic> toJson() {
return {
'role': role,
'content': content,
'timestamp': timestamp.toIso8601String(),
'isLoading': isLoading,
};
}
// 创建一个加载中的消息
factory Message.loading() {
return Message(
role: 'assistant',
content: '',
timestamp: DateTime.now(),
isLoading: true,
);
}
// 复制并修改
Message copyWith({
String? role,
String? content,
DateTime? timestamp,
bool? isLoading,
}) {
return Message(
role: role ?? this.role,
content: content ?? this.content,
timestamp: timestamp ?? this.timestamp,
isLoading: isLoading ?? this.isLoading,
);
}
}