Skip to content

Commit fa9ad75

Browse files
committed
Fix: Fix the index error of msg versioning
1 parent 1f0cd28 commit fa9ad75

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

src/services/database-integration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class DatabaseIntegrationService {
128128
tokens: 0,
129129
fatherMessageId: null,
130130
childrenMessageIds: [],
131-
preferIndex: 0
131+
preferIndex: -1
132132
};
133133

134134
// Save system message
@@ -265,6 +265,8 @@ export class DatabaseIntegrationService {
265265
conversationId: conversationId,
266266
};
267267

268+
console.log('db integration updateChatMessage', dbMessage);
269+
268270
await this.dbService.updateChatMessage(dbMessage);
269271
} catch (error) {
270272
console.error('Error updating message:', error);

src/services/message-helper.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { v4 as uuidv4 } from 'uuid';
55
export class MessageHelper {
66

77
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);
109

1110
const dbService = DatabaseIntegrationService.getInstance();
1211

@@ -20,12 +19,14 @@ export class MessageHelper {
2019
0,
2120
latestMessage ? latestMessage.messageId : null,
2221
[],
23-
0
22+
-1
2423
);
2524

2625
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+
2930
await dbService.updateChatMessage(latestMessage.messageId, latestMessage, conversation.conversationId);
3031
}
3132

@@ -35,9 +36,11 @@ export class MessageHelper {
3536
? content.substring(0, 30) + (content.length > 30 ? '...' : '')
3637
: conversation.title;
3738

38-
3939
const messages = new Map(conversation.messages);
4040
messages.set(userMessage.messageId, userMessage);
41+
if(latestMessage) {
42+
messages.set(latestMessage.messageId, latestMessage);
43+
}
4144

4245
const updatedConversation = {
4346
...conversation,
@@ -68,7 +71,7 @@ export class MessageHelper {
6871
0,
6972
fatherMessage.messageId,
7073
[],
71-
0
74+
-1
7275
);
7376

7477
fatherMessage.childrenMessageIds.push(userMessage.messageId);
@@ -103,8 +106,11 @@ export class MessageHelper {
103106
fatherMessageId: fatherMessage.messageId,
104107
}
105108

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+
108114
await dbService.updateChatMessage(fatherMessage.messageId, fatherMessage, conversation.conversationId);
109115

110116
await dbService.saveChatMessage(
@@ -122,6 +128,8 @@ export class MessageHelper {
122128

123129
const messages = new Map(updatedConversation.messages);
124130
messages.set(updatedAiResponse.messageId, updatedAiResponse);
131+
messages.delete(fatherMessage.messageId);
132+
messages.set(fatherMessage.messageId, fatherMessage);
125133

126134
const finalConversation: Conversation = {
127135
...updatedConversation,
@@ -140,7 +148,9 @@ export class MessageHelper {
140148

141149
for(const message of filteredMessages) {
142150
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+
}
144154
}
145155

146156
return {
@@ -161,7 +171,7 @@ export class MessageHelper {
161171
tokens: 0,
162172
fatherMessageId: null,
163173
childrenMessageIds: [],
164-
preferIndex: 0
174+
preferIndex: -1
165175
};
166176
}
167177

@@ -212,8 +222,8 @@ export class MessageHelper {
212222
if(conversation.messages.size === 0) return null;
213223

214224
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;
217227
}
218228

219229
public static pureTextMessage(contentText: string): MessageContent[] {
@@ -234,4 +244,21 @@ export class MessageHelper {
234244
return '';
235245
}).join('');
236246
}
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+
}
237264
}

src/services/providers/anthropic-service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class AnthropicService implements AiServiceProvider {
167167
tokens: 0,
168168
fatherMessageId: null,
169169
childrenMessageIds: [],
170-
preferIndex: 0
170+
preferIndex: -1
171171
};
172172
}
173173

@@ -218,7 +218,7 @@ export class AnthropicService implements AiServiceProvider {
218218
tokens: 0,
219219
fatherMessageId: null,
220220
childrenMessageIds: [],
221-
preferIndex: 0
221+
preferIndex: -1
222222
};
223223

224224
resolve(aiResponse);
@@ -234,7 +234,7 @@ export class AnthropicService implements AiServiceProvider {
234234
tokens: 0,
235235
fatherMessageId: null,
236236
childrenMessageIds: [],
237-
preferIndex: 0
237+
preferIndex: -1
238238
};
239239

240240
resolve(aiResponse);

src/services/providers/common-provider-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class CommonProviderHelper implements AiServiceProvider {
213213
tokens: 0,
214214
fatherMessageId: null,
215215
childrenMessageIds: [],
216-
preferIndex: 0
216+
preferIndex: -1
217217
};
218218

219219
} catch (error) {

0 commit comments

Comments
 (0)