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
64 lines
1.5 KiB
class Message {
|
|
final String role; // 'user' or 'assistant'
|
|
final String content;
|
|
final Map? meta;
|
|
final DateTime timestamp;
|
|
final bool isIntermediate; // 中间状态标记,替代isLoading
|
|
|
|
Message({
|
|
required this.role,
|
|
required this.content,
|
|
required this.timestamp,
|
|
this.meta,
|
|
this.isIntermediate = false, // 默认不是中间状态
|
|
});
|
|
|
|
// 从JSON构造函数
|
|
factory Message.fromJson(Map<String, dynamic> json) {
|
|
return Message(
|
|
role: json['role'] as String,
|
|
content: json['content'] as String,
|
|
meta: json['meta'],
|
|
timestamp: DateTime.parse(json['timestamp'] as String),
|
|
isIntermediate: json['isIntermediate'] as bool? ?? false,
|
|
);
|
|
}
|
|
|
|
// 转换为JSON
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'role': role,
|
|
'content': content,
|
|
'meta': meta,
|
|
'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? meta,
|
|
DateTime? timestamp,
|
|
bool? isIntermediate,
|
|
}) {
|
|
return Message(
|
|
role: role ?? this.role,
|
|
content: content ?? this.content,
|
|
meta: meta ?? this.meta,
|
|
timestamp: timestamp ?? this.timestamp,
|
|
isIntermediate: isIntermediate ?? this.isIntermediate,
|
|
);
|
|
}
|
|
}
|
|
|