1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
| | import 'package:uuid/uuid.dart';
| |
| | enum MessageRole { user, assistant, system }
| |
| | enum MessageType { text, voice, image }
| |
| | enum MessageStatus { sending, sent, error }
| |
| | class Message {
| | final String id;
| | final MessageRole role;
| | final MessageType type;
| | final String content;
| | final String? audioUri;
| | final String? imageBase64;
| | final String? mimeType;
| | final int timestamp;
| | final MessageStatus? status;
| | final int? duration;
| |
| | const Message({
| | required this.id,
| | required this.role,
| | required this.type,
| | required this.content,
| | required this.timestamp,
| | this.audioUri,
| | this.imageBase64,
| | this.mimeType,
| | this.status,
| | this.duration,
| | });
| |
| | factory Message.text({
| | required MessageRole role,
| | required String content,
| | MessageStatus? status,
| | }) {
| | return Message(
| | id: const Uuid().v4(),
| | role: role,
| | type: MessageType.text,
| | content: content,
| | timestamp: DateTime.now().millisecondsSinceEpoch,
| | status: status,
| | );
| | }
| |
| | factory Message.voice({
| | required MessageRole role,
| | required String audioUri,
| | String content = '',
| | int? duration,
| | MessageStatus? status,
| | }) {
| | return Message(
| | id: const Uuid().v4(),
| | role: role,
| | type: MessageType.voice,
| | content: content,
| | audioUri: audioUri,
| | timestamp: DateTime.now().millisecondsSinceEpoch,
| | duration: duration,
| | status: status,
| | );
| | }
| |
| | factory Message.image({
| | required MessageRole role,
| | required String imageBase64,
| | String content = '',
| | String? mimeType,
| | MessageStatus? status,
| | }) {
| | return Message(
| | id: const Uuid().v4(),
| | role: role,
| | type: MessageType.image,
| | content: content,
| | imageBase64: imageBase64,
| | mimeType: mimeType,
| | timestamp: DateTime.now().millisecondsSinceEpoch,
| | status: status,
| | );
| | }
| |
| | Message copyWith({
| | String? content,
| | String? audioUri,
| | String? imageBase64,
| | String? mimeType,
| | MessageStatus? status,
| | int? duration,
| | }) {
| | return Message(
| | id: id,
| | role: role,
| | type: type,
| | content: content ?? this.content,
| | audioUri: audioUri ?? this.audioUri,
| | imageBase64: imageBase64 ?? this.imageBase64,
| | mimeType: mimeType ?? this.mimeType,
| | timestamp: timestamp,
| | status: status ?? this.status,
| | duration: duration ?? this.duration,
| | );
| | }
| |
| | Map<String, dynamic> toJson() {
| | return {
| | 'id': id,
| | 'role': role.name,
| | 'type': type.name,
| | 'content': content,
| | if (audioUri != null) 'audioUri': audioUri,
| | if (imageBase64 != null) 'imageBase64': imageBase64,
| | if (mimeType != null) 'mimeType': mimeType,
| | 'timestamp': timestamp,
| | if (status != null) 'status': status!.name,
| | if (duration != null) 'duration': duration,
| | };
| | }
| |
| | /// Lightweight JSON for persistence (strips base64 audio, keeps file paths and images).
| | Map<String, dynamic> toJsonLight() {
| | // Keep audioUri if it's a file path (starts with '/') — these are saved audio files.
| | // Strip base64 audio data (large, temp) — those won't survive restart.
| | final keepAudio = audioUri != null && audioUri!.startsWith('/');
| | return {
| | 'id': id,
| | 'role': role.name,
| | 'type': type.name,
| | 'content': content,
| | if (keepAudio) 'audioUri': audioUri,
| | 'timestamp': timestamp,
| | if (status != null) 'status': status!.name,
| | if (duration != null) 'duration': duration,
| | // Keep imageBase64 — images are typically 50-200 KB and must survive restart.
| | if (imageBase64 != null) 'imageBase64': imageBase64,
| | if (mimeType != null) 'mimeType': mimeType,
| | };
| | }
| |
| | factory Message.fromJson(Map<String, dynamic> json) {
| | return Message(
| | id: json['id'] as String,
| | role: MessageRole.values.byName(json['role'] as String),
| | type: MessageType.values.byName(json['type'] as String),
| | content: json['content'] as String? ?? '',
| | audioUri: json['audioUri'] as String?,
| | imageBase64: json['imageBase64'] as String?,
| | mimeType: json['mimeType'] as String?,
| | timestamp: json['timestamp'] as int,
| | status: json['status'] != null
| | ? MessageStatus.values.byName(json['status'] as String)
| | : null,
| | duration: json['duration'] as int?,
| | );
| | }
| |
| | /// Returns true if this is a voice message that has neither audio nor text.
| | bool get isEmptyVoice =>
| | type == MessageType.voice &&
| | (audioUri == null || audioUri!.isEmpty) &&
| | content.isEmpty;
| |
| | /// Returns true if this is a text message with no content (empty bubble).
| | bool get isEmptyText =>
| | type == MessageType.text &&
| | content.trim().isEmpty &&
| | imageBase64 == null;
| | }
|
|