1
1
/* eslint-disable sort-keys-fix/sort-keys-fix, typescript-sort-keys/interface */
2
2
// Disable the auto sort key eslint rule to make the code more logic and readable
3
+ import { ChatMessage , ChatTopic , CreateMessageParams , SendMessageParams } from '@lobechat/types' ;
3
4
import { t } from 'i18next' ;
4
5
import { StateCreator } from 'zustand/vanilla' ;
5
6
@@ -8,7 +9,6 @@ import { getAgentStoreState } from '@/store/agent';
8
9
import { ChatStore } from '@/store/chat/store' ;
9
10
import { messageMapKey } from '@/store/chat/utils/messageMapKey' ;
10
11
import { getSessionStoreState } from '@/store/session' ;
11
- import { CreateMessageParams , SendMessageParams } from '@/types/message' ;
12
12
import { cleanObject } from '@/utils/object' ;
13
13
14
14
import { chatSelectors , topicSelectors } from '../../../selectors' ;
@@ -18,6 +18,12 @@ export interface AIGenerateV2Action {
18
18
* Sends a new message to the AI chat system
19
19
*/
20
20
sendMessageInServer : ( params : SendMessageParams ) => Promise < void > ;
21
+ internal_refreshAiChat : ( params : {
22
+ topics ?: ChatTopic [ ] ;
23
+ messages : ChatMessage [ ] ;
24
+ sessionId : string ;
25
+ topicId ?: string ;
26
+ } ) => void ;
21
27
}
22
28
23
29
export const generateAIChatV2 : StateCreator <
@@ -55,6 +61,10 @@ export const generateAIChatV2: StateCreator<
55
61
} ;
56
62
const messages = chatSelectors . activeBaseChats ( get ( ) ) ;
57
63
64
+ // use optimistic update to avoid the slow waiting
65
+ const tempId = get ( ) . internal_createTmpMessage ( newMessage ) ;
66
+
67
+ get ( ) . internal_toggleMessageLoading ( true , tempId ) ;
58
68
set ( { isCreatingMessage : true } , false , 'creatingMessage/start' ) ;
59
69
60
70
const data = await aiChatService . sendMessageInServer ( {
@@ -66,15 +76,21 @@ export const generateAIChatV2: StateCreator<
66
76
title : t ( 'defaultTitle' , { ns : 'topic' } ) ,
67
77
}
68
78
: undefined ,
69
- onlyAddUserMessage,
79
+ } ) ;
80
+
81
+ // refresh the total data
82
+ get ( ) . internal_refreshAiChat ( {
83
+ messages : data . messages ,
84
+ topics : data . topics ,
85
+ sessionId : activeId ,
86
+ topicId : data . topicId ,
70
87
} ) ;
71
88
72
89
if ( ! activeTopicId ) {
73
90
await get ( ) . switchTopic ( data . topicId ! , true ) ;
74
91
}
75
92
76
- get ( ) . internal_dispatchTopic ( { type : 'updateTopics' , value : data . topics as any [ ] } ) ;
77
- get ( ) . internal_dispatchMessage ( { type : 'updateMessages' , value : data . messages } ) ;
93
+ get ( ) . internal_toggleMessageLoading ( false , tempId ) ;
78
94
79
95
// update assistant update to make it rerank
80
96
getSessionStoreState ( ) . triggerSessionUpdate ( get ( ) . activeId ) ;
@@ -117,4 +133,15 @@ export const generateAIChatV2: StateCreator<
117
133
118
134
await Promise . all ( [ summaryTitle ( ) , addFilesToAgent ( ) ] ) ;
119
135
} ,
136
+
137
+ internal_refreshAiChat : ( { topics, messages, sessionId, topicId } ) => {
138
+ set (
139
+ {
140
+ topicMaps : topics ? { ...get ( ) . topicMaps , [ sessionId ] : topics } : get ( ) . topicMaps ,
141
+ messagesMap : { ...get ( ) . messagesMap , [ messageMapKey ( sessionId , topicId ) ] : messages } ,
142
+ } ,
143
+ false ,
144
+ 'refreshAiChat' ,
145
+ ) ;
146
+ } ,
120
147
} ) ;
0 commit comments