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.

64 lines
1.5 KiB

class Message {
final String role; // 'user' or 'assistant'
final String content;
final Map? data;
final DateTime timestamp;
final bool isIntermediate; // 中间状态标记,替代isLoading
Message({
required this.role,
required this.content,
required this.timestamp,
this.data,
this.isIntermediate = false, // 默认不是中间状态
});
// 从JSON构造函数
factory Message.fromJson(Map<String, dynamic> json) {
return Message(
role: json['role'] as String,
content: json['content'] as String,
data: json['data'],
timestamp: DateTime.parse(json['timestamp'] as String),
isIntermediate: json['isIntermediate'] as bool? ?? false,
);
}
// 转换为JSON
Map<String, dynamic> toJson() {
return {
'role': role,
'content': content,
'data': data,
'timestamp': timestamp.toIso8601String(),
'isIntermediate': isIntermediate,
};
}
// 创建一个中间状态的消息
factory Message.intermediate(String role) {
return Message(
role: role,
content: '',
timestamp: DateTime.now(),
isIntermediate: true,
);
}
// 复制并修改
Message copyWith({
String? role,
String? content,
Map? data,
DateTime? timestamp,
bool? isIntermediate,
}) {
return Message(
role: role ?? this.role,
content: content ?? this.content,
data: data ?? this.data,
timestamp: timestamp ?? this.timestamp,
isIntermediate: isIntermediate ?? this.isIntermediate,
);
}
}