@@ -4,13 +4,14 @@ import { t } from 'i18next';
4
4
import { StateCreator } from 'zustand/vanilla' ;
5
5
6
6
import { aiChatService } from '@/services/aiChat' ;
7
- import { agentChatConfigSelectors } from '@/store/agent/selectors' ;
8
- import { getAgentStoreState } from '@/store/agent/store' ;
7
+ import { getAgentStoreState } from '@/store/agent' ;
9
8
import { ChatStore } from '@/store/chat/store' ;
9
+ import { messageMapKey } from '@/store/chat/utils/messageMapKey' ;
10
+ import { getSessionStoreState } from '@/store/session' ;
10
11
import { CreateMessageParams , SendMessageParams } from '@/types/message' ;
11
12
import { cleanObject } from '@/utils/object' ;
12
13
13
- import { chatSelectors } from '../../../selectors' ;
14
+ import { chatSelectors , topicSelectors } from '../../../selectors' ;
14
15
15
16
export interface AIGenerateV2Action {
16
17
/**
@@ -25,8 +26,8 @@ export const generateAIChatV2: StateCreator<
25
26
[ ] ,
26
27
AIGenerateV2Action
27
28
> = ( set , get ) => ( {
28
- sendMessageInServer : async ( { message, files, onlyAddUserMessage } ) => {
29
- const { activeTopicId, activeId, activeThreadId } = get ( ) ;
29
+ sendMessageInServer : async ( { message, files, onlyAddUserMessage, isWelcomeQuestion } ) => {
30
+ const { activeTopicId, activeId, activeThreadId, internal_coreProcessMessage } = get ( ) ;
30
31
if ( ! activeId ) return ;
31
32
32
33
const fileIdList = files ?. map ( ( f ) => f . id ) ;
@@ -36,7 +37,11 @@ export const generateAIChatV2: StateCreator<
36
37
// if message is empty or no files, then stop
37
38
if ( ! message && ! hasFile ) return ;
38
39
39
- // set({ isCreatingMessage: true }, false, n('creatingMessage/start'));
40
+ if ( onlyAddUserMessage ) {
41
+ await get ( ) . addUserMessage ( { message, fileList : fileIdList } ) ;
42
+
43
+ return ;
44
+ }
40
45
41
46
const newMessage : CreateMessageParams = {
42
47
content : message ,
@@ -50,143 +55,66 @@ export const generateAIChatV2: StateCreator<
50
55
} ;
51
56
const messages = chatSelectors . activeBaseChats ( get ( ) ) ;
52
57
53
- const agentConfig = agentChatConfigSelectors . currentChatConfig ( getAgentStoreState ( ) ) ;
58
+ set ( { isCreatingMessage : true } , false , 'creatingMessage/start' ) ;
54
59
55
60
const data = await aiChatService . sendMessageInServer ( {
56
61
newMessage : cleanObject ( newMessage ) as any ,
57
- newTopic :
58
- ! activeTopicId && agentConfig . enableAutoCreateTopic
59
- ? {
60
- topicMessageIds : messages . map ( ( m ) => m . id ) ,
61
- sessionId : activeId ,
62
- title : t ( 'defaultTitle' , { ns : 'topic' } ) ,
63
- }
64
- : undefined ,
62
+ newTopic : ! activeTopicId
63
+ ? {
64
+ topicMessageIds : messages . map ( ( m ) => m . id ) ,
65
+ sessionId : activeId ,
66
+ title : t ( 'defaultTitle' , { ns : 'topic' } ) ,
67
+ }
68
+ : undefined ,
65
69
onlyAddUserMessage,
66
70
} ) ;
67
71
68
- console . log ( 'data:' , data ) ;
69
-
70
- if ( agentConfig . enableAutoCreateTopic ) {
72
+ if ( ! activeTopicId ) {
71
73
await get ( ) . switchTopic ( data . topicId ! , true ) ;
72
74
}
73
75
74
76
get ( ) . internal_dispatchTopic ( { type : 'updateTopics' , value : data . topics as any [ ] } ) ;
75
77
get ( ) . internal_dispatchMessage ( { type : 'updateMessages' , value : data . messages } ) ;
76
78
77
- return ;
79
+ // update assistant update to make it rerank
80
+ getSessionStoreState ( ) . triggerSessionUpdate ( get ( ) . activeId ) ;
78
81
79
- // let tempMessageId: string | undefined = undefined;
80
- // let newTopicId: string | undefined = undefined;
81
- //
82
- // // it should be the default topic, then
83
- // // if autoCreateTopic is enabled, check to whether we need to create a topic
84
- // if (!onlyAddUserMessage && !activeTopicId && agentConfig.enableAutoCreateTopic) {
85
- // // check activeTopic and then auto create topic
86
- // const chats = chatSelectors.activeBaseChats(get());
87
- //
88
- // // we will add two messages (user and assistant), so the finial length should +2
89
- // const featureLength = chats.length + 2;
90
- //
91
- // // if there is no activeTopicId and the feature length is greater than the threshold
92
- // // then create a new topic and active it
93
- // if (!activeTopicId && featureLength >= agentConfig.autoCreateTopicThreshold) {
94
- // // we need to create a temp message for optimistic update
95
- // tempMessageId = get().internal_createTmpMessage(newMessage);
96
- // get().internal_toggleMessageLoading(true, tempMessageId);
97
- //
98
- // const topicId = await get().createTopic();
99
- //
100
- // if (topicId) {
101
- // newTopicId = topicId;
102
- // newMessage.topicId = topicId;
103
- //
104
- // // we need to copy the messages to the new topic or the message will disappear
105
- // const mapKey = chatSelectors.currentChatKey(get());
106
- // const newMaps = {
107
- // ...get().messagesMap,
108
- // [messageMapKey(activeId, topicId)]: get().messagesMap[mapKey],
109
- // };
110
- // set({ messagesMap: newMaps }, false, n('moveMessagesToNewTopic'));
111
- //
112
- // // make the topic loading
113
- // get().internal_updateTopicLoading(topicId, true);
114
- // }
115
- // }
116
- // }
117
- // // update assistant update to make it rerank
118
- // useSessionStore.getState().triggerSessionUpdate(get().activeId);
119
- //
120
- // const id = await get().internal_createMessage(newMessage, {
121
- // tempMessageId,
122
- // skipRefresh: !onlyAddUserMessage && newMessage.fileList?.length === 0,
123
- // });
124
- //
125
- // if (!id) {
126
- // set({ isCreatingMessage: false }, false, n('creatingMessage/start'));
127
- // if (!!newTopicId) get().internal_updateTopicLoading(newTopicId, false);
128
- // return;
129
- // }
130
- //
131
- // if (tempMessageId) get().internal_toggleMessageLoading(false, tempMessageId);
132
- //
133
- // // switch to the new topic if create the new topic
134
- // if (!!newTopicId) {
135
- // await get().switchTopic(newTopicId, true);
136
- // await get().internal_fetchMessages();
137
- //
138
- // // delete previous messages
139
- // // remove the temp message map
140
- // const newMaps = { ...get().messagesMap, [messageMapKey(activeId, null)]: [] };
141
- // set({ messagesMap: newMaps }, false, 'internal_copyMessages');
142
- // }
143
- //
144
- // // if only add user message, then stop
145
- // if (onlyAddUserMessage) {
146
- // set({ isCreatingMessage: false }, false, 'creatingMessage/start');
147
- // return;
148
- // }
149
82
//
150
83
// // Get the current messages to generate AI response
151
- // const messages = chatSelectors.activeBaseChats(get());
152
- // const userFiles = chatSelectors.currentUserFiles(get()).map((f) => f.id);
153
- //
154
- // await internal_coreProcessMessage(messages, id, {
155
- // isWelcomeQuestion,
156
- // ragQuery: get().internal_shouldUseRAG() ? message : undefined,
157
- // threadId: activeThreadId,
158
- // });
159
- //
160
- // set({ isCreatingMessage: false }, false, n('creatingMessage/stop'));
161
- //
162
- // const summaryTitle = async () => {
163
- // // if autoCreateTopic is false, then stop
164
- // if (!agentConfig.enableAutoCreateTopic) return;
84
+ const baseMessages = chatSelectors . activeBaseChats ( get ( ) ) ;
165
85
//
166
- // // check activeTopic and then auto update topic title
167
- // if (newTopicId) {
168
- // const chats = chatSelectors.getBaseChatsByKey(messageMapKey(activeId, newTopicId))(get());
169
- // await get().summaryTopicTitle(newTopicId, chats);
170
- // return;
171
- // }
86
+ await internal_coreProcessMessage ( baseMessages , data . messageId , {
87
+ isWelcomeQuestion,
88
+ ragQuery : get ( ) . internal_shouldUseRAG ( ) ? message : undefined ,
89
+ threadId : activeThreadId ,
90
+ } ) ;
172
91
//
173
- // if (!activeTopicId) return;
174
- // const topic = topicSelectors.getTopicById(activeTopicId)(get());
92
+ set ( { isCreatingMessage : false } , false , 'creatingMessage/stop' ) ;
175
93
//
176
- // if (topic && !topic.title) {
177
- // const chats = chatSelectors.getBaseChatsByKey(messageMapKey(activeId, topic.id))(get());
178
- // await get().summaryTopicTitle(topic.id, chats);
179
- // }
180
- // };
94
+ const summaryTitle = async ( ) => {
95
+ // check activeTopic and then auto update topic title
96
+ if ( data . isCreatNewTopic ) {
97
+ await get ( ) . summaryTopicTitle ( data . topicId , data . messages ) ;
98
+ return ;
99
+ }
100
+
101
+ if ( ! activeTopicId ) return ;
102
+
103
+ const topic = topicSelectors . getTopicById ( activeTopicId ) ( get ( ) ) ;
104
+
105
+ if ( topic && ! topic . title ) {
106
+ const chats = chatSelectors . getBaseChatsByKey ( messageMapKey ( activeId , topic . id ) ) ( get ( ) ) ;
107
+ await get ( ) . summaryTopicTitle ( topic . id , chats ) ;
108
+ }
109
+ } ;
181
110
//
182
111
// // if there is relative files, then add files to agent
183
112
// // only available in server mode
184
- // const addFilesToAgent = async () => {
185
- // if (userFiles.length === 0 || !isServerMode) return;
186
- //
187
- // await useAgentStore.getState().addFilesToAgent(userFiles, false);
188
- // };
189
- //
190
- // await Promise.all([summaryTitle(), addFilesToAgent()]);
113
+ const userFiles = chatSelectors . currentUserFiles ( get ( ) ) . map ( ( f ) => f . id ) ;
114
+ const addFilesToAgent = async ( ) => {
115
+ await getAgentStoreState ( ) . addFilesToAgent ( userFiles , false ) ;
116
+ } ;
117
+
118
+ await Promise . all ( [ summaryTitle ( ) , addFilesToAgent ( ) ] ) ;
191
119
} ,
192
120
} ) ;
0 commit comments