@@ -5,8 +5,7 @@ import { v4 as uuidv4 } from 'uuid';
5
5
export class MessageHelper {
6
6
7
7
public static async addUserMessageToConversation ( content : string , conversation : Conversation ) : Promise < { conversation : Conversation , message : Message } > {
8
-
9
- const latestMessage = MessageHelper . getLastMessage ( conversation ) ;
8
+ let latestMessage = MessageHelper . getLastMessage ( conversation ) ;
10
9
11
10
const dbService = DatabaseIntegrationService . getInstance ( ) ;
12
11
@@ -20,12 +19,14 @@ export class MessageHelper {
20
19
0 ,
21
20
latestMessage ? latestMessage . messageId : null ,
22
21
[ ] ,
23
- 0
22
+ - 1
24
23
) ;
25
24
26
25
if ( latestMessage ) {
27
- latestMessage . childrenMessageIds . push ( userMessage . messageId ) ;
28
- latestMessage . preferIndex = latestMessage . childrenMessageIds . length - 1 ;
26
+ latestMessage = MessageHelper . insertMessageIdToFathterMessage ( latestMessage , userMessage . messageId ) ;
27
+
28
+ console . log ( 'Updated latest message:' , latestMessage ) ;
29
+
29
30
await dbService . updateChatMessage ( latestMessage . messageId , latestMessage , conversation . conversationId ) ;
30
31
}
31
32
@@ -35,9 +36,11 @@ export class MessageHelper {
35
36
? content . substring ( 0 , 30 ) + ( content . length > 30 ? '...' : '' )
36
37
: conversation . title ;
37
38
38
-
39
39
const messages = new Map ( conversation . messages ) ;
40
40
messages . set ( userMessage . messageId , userMessage ) ;
41
+ if ( latestMessage ) {
42
+ messages . set ( latestMessage . messageId , latestMessage ) ;
43
+ }
41
44
42
45
const updatedConversation = {
43
46
...conversation ,
@@ -68,7 +71,7 @@ export class MessageHelper {
68
71
0 ,
69
72
fatherMessage . messageId ,
70
73
[ ] ,
71
- 0
74
+ - 1
72
75
) ;
73
76
74
77
fatherMessage . childrenMessageIds . push ( userMessage . messageId ) ;
@@ -103,8 +106,11 @@ export class MessageHelper {
103
106
fatherMessageId : fatherMessage . messageId ,
104
107
}
105
108
106
- fatherMessage . childrenMessageIds . push ( updatedAiResponse . messageId ) ;
107
- fatherMessage . preferIndex = fatherMessage . childrenMessageIds . length - 1 ;
109
+ const fathterPreferIndex = fatherMessage . preferIndex ;
110
+
111
+ fatherMessage . childrenMessageIds . splice ( fathterPreferIndex + 1 , 0 , updatedAiResponse . messageId ) ;
112
+ fatherMessage . preferIndex = fathterPreferIndex + 1 ;
113
+
108
114
await dbService . updateChatMessage ( fatherMessage . messageId , fatherMessage , conversation . conversationId ) ;
109
115
110
116
await dbService . saveChatMessage (
@@ -122,6 +128,8 @@ export class MessageHelper {
122
128
123
129
const messages = new Map ( updatedConversation . messages ) ;
124
130
messages . set ( updatedAiResponse . messageId , updatedAiResponse ) ;
131
+ messages . delete ( fatherMessage . messageId ) ;
132
+ messages . set ( fatherMessage . messageId , fatherMessage ) ;
125
133
126
134
const finalConversation : Conversation = {
127
135
...updatedConversation ,
@@ -140,7 +148,9 @@ export class MessageHelper {
140
148
141
149
for ( const message of filteredMessages ) {
142
150
message . childrenMessageIds = message . childrenMessageIds . filter ( id => ! id . startsWith ( 'streaming-' ) ) ;
143
- message . preferIndex = message . childrenMessageIds . length - 1 ;
151
+ if ( message . preferIndex >= message . childrenMessageIds . length ) {
152
+ message . preferIndex = message . childrenMessageIds . length - 1 ;
153
+ }
144
154
}
145
155
146
156
return {
@@ -161,7 +171,7 @@ export class MessageHelper {
161
171
tokens : 0 ,
162
172
fatherMessageId : null ,
163
173
childrenMessageIds : [ ] ,
164
- preferIndex : 0
174
+ preferIndex : - 1
165
175
} ;
166
176
}
167
177
@@ -212,8 +222,8 @@ export class MessageHelper {
212
222
if ( conversation . messages . size === 0 ) return null ;
213
223
214
224
const mapedMessages = MessageHelper . mapMessagesTreeToList ( conversation , false , null ) ;
215
- console . log ( 'Maped messages:' , mapedMessages ) ;
216
- return mapedMessages [ mapedMessages . length - 1 ] ;
225
+ const lastMessage = mapedMessages [ mapedMessages . length - 1 ] ;
226
+ return lastMessage ;
217
227
}
218
228
219
229
public static pureTextMessage ( contentText : string ) : MessageContent [ ] {
@@ -234,4 +244,21 @@ export class MessageHelper {
234
244
return '' ;
235
245
} ) . join ( '' ) ;
236
246
}
247
+
248
+ public static insertMessageIdToFathterMessage ( fatherMessage : Message , messageId : string ) : Message {
249
+ if ( fatherMessage . childrenMessageIds . length === 0 ) {
250
+ fatherMessage . childrenMessageIds . push ( messageId ) ;
251
+ fatherMessage . preferIndex = 0 ;
252
+ }
253
+ else if ( fatherMessage . preferIndex >= 0 && fatherMessage . preferIndex < fatherMessage . childrenMessageIds . length ) {
254
+ fatherMessage . childrenMessageIds . splice ( fatherMessage . preferIndex + 1 , 0 , messageId ) ;
255
+ fatherMessage . preferIndex = fatherMessage . preferIndex + 1 ;
256
+ }
257
+ else {
258
+ fatherMessage . childrenMessageIds . push ( messageId ) ;
259
+ fatherMessage . preferIndex = fatherMessage . childrenMessageIds . length - 1 ;
260
+ }
261
+
262
+ return fatherMessage ;
263
+ }
237
264
}
0 commit comments