From fb95895d1cab81250a2e6d545cb9d3422a57e52b Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Thu, 5 Jun 2025 19:36:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(data/models):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=A8=A1=E5=9E=8B=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将`role`字段改为更明确的`isUser`布尔值 - 重命名`content`为`text`以提高可读性 - 添加`hasImage`和`imagePath`支持图片消息 - 新增`meta`字段用于存储卡片数据 - 将`isLoading`改为`isRecognizing`以更准确描述语音识别状态 --- lib/data/models/message_model.dart | 67 +++++++++++++++++++----------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/lib/data/models/message_model.dart b/lib/data/models/message_model.dart index 94aebc3ac..aef99cbc9 100644 --- a/lib/data/models/message_model.dart +++ b/lib/data/models/message_model.dart @@ -1,58 +1,77 @@ class Message { - final String role; // 'user' or 'assistant' - final String content; - final DateTime timestamp; - final bool isLoading; + final bool isUser; //标识信息是否来自用户还是AI + final String text; //信息文本内容 + final DateTime timestamp; // 消息时间戳 + final bool isRecognizing; // 是否为语音识别中的临时消息 + final bool hasImage; // 是否包含图片 + final String? imagePath; // 图片路径 + final Map? meta; //卡片数据 Message({ - required this.role, - required this.content, - required this.timestamp, - this.isLoading = false, - }); + // 构造函数 + required this.isUser, + required this.text, + DateTime? timestamp, + this.isRecognizing = false, + this.hasImage = false, + this.imagePath, + this.meta, + }) : this.timestamp = timestamp ?? DateTime.now(); // 从JSON构造函数 factory Message.fromJson(Map json) { return Message( - role: json['role'] as String, - content: json['content'] as String, + isUser: json['isUser'] as bool, + text: json['text'] as String, timestamp: DateTime.parse(json['timestamp'] as String), - isLoading: json['isLoading'] as bool? ?? false, + isRecognizing: json['isRecognizing'] as bool? ?? false, + hasImage: json['hasImage'] as bool? ?? false, + imagePath: json['imagePath'] as String?, + meta: json['meta'] as Map?, ); } // 转换为JSON Map toJson() { return { - 'role': role, - 'content': content, + 'isUser': isUser, + 'text': text, 'timestamp': timestamp.toIso8601String(), - 'isLoading': isLoading, + 'isRecognizing': isRecognizing, + 'hasImage': hasImage, + 'imagePath': imagePath, + 'meta': meta, }; } // 创建一个加载中的消息 factory Message.loading() { return Message( - role: 'assistant', - content: '', + isUser: false, + text: '', timestamp: DateTime.now(), - isLoading: true, + isRecognizing: true, ); } // 复制并修改 Message copyWith({ - String? role, - String? content, + bool? isUser, + String? text, DateTime? timestamp, - bool? isLoading, + bool? isRecognizing, + bool? hasImage, + String? imagePath, + Map? meta, }) { return Message( - role: role ?? this.role, - content: content ?? this.content, + isUser: isUser ?? this.isUser, + text: text ?? this.text, timestamp: timestamp ?? this.timestamp, - isLoading: isLoading ?? this.isLoading, + isRecognizing: isRecognizing ?? this.isRecognizing, + hasImage: hasImage ?? this.hasImage, + imagePath: imagePath ?? this.imagePath, + meta: meta ?? this.meta, ); } }