@@ -6,10 +6,13 @@ import useRequest from '@/services/request';
6
6
import { getRequestBaseUrl } from ' @/utils' ;
7
7
import { debounce } from ' lodash' ;
8
8
import { ElMessage } from ' element-plus' ;
9
-
9
+ import { AxiosError } from ' axios' ;
10
+ import { useRouter } from ' vue-router' ;
10
11
const { t } = useI18n ();
11
12
const { get } = useRequest ();
12
13
14
+ const router = useRouter ();
15
+
13
16
// Add current conversation ref
14
17
const currentConversation = ref <ChatConversation | null >(null );
15
18
@@ -43,7 +46,6 @@ const streamError = ref('');
43
46
44
47
// Add conversation management
45
48
const conversations = ref <ChatConversation []>([]);
46
- const selectedConversationId = ref <string >(' ' );
47
49
const currentConversationId = ref <string >(' ' );
48
50
const isLoadingConversations = ref (false );
49
51
const isLoadingMessages = ref (false );
@@ -74,7 +76,7 @@ const loadConversations = async () => {
74
76
};
75
77
76
78
// Load messages for a conversation
77
- const loadConversationMessages = async (conversationId : string ) => {
79
+ const loadConversationMessages = debounce ( async (conversationId : string ) => {
78
80
if (! conversationId ) return ;
79
81
80
82
isLoadingMessages .value = true ;
@@ -92,34 +94,32 @@ const loadConversationMessages = async (conversationId: string) => {
92
94
(a : ChatMessageType , b : ChatMessageType ) =>
93
95
a .timestamp .getTime () - b .timestamp .getTime ()
94
96
);
95
- console .debug (messages [messages .length - 1 ].content );
96
97
97
98
chatHistory .splice (0 , chatHistory .length , ... messages );
98
99
currentConversationId .value = conversationId ;
99
100
} catch (error ) {
100
101
console .error (' Failed to load conversation messages:' , error );
101
- streamError . value =
102
+ const errorMessage =
102
103
error instanceof Error ? error .message : ' Failed to load messages' ;
103
104
} finally {
104
105
isLoadingMessages .value = false ;
105
106
// Scroll to bottom after loading messages
106
107
messageListRef .value ?.scrollToBottom ();
107
108
}
108
- };
109
+ }) ;
109
110
110
111
// Select conversation
111
112
const selectConversation = async (conversationId : string ) => {
112
- if (selectedConversationId .value === conversationId ) return ;
113
+ if (currentConversationId .value === conversationId ) return ;
113
114
114
- selectedConversationId .value = conversationId ;
115
+ currentConversationId .value = conversationId ;
115
116
streamError .value = ' ' ;
116
117
await loadConversationMessages (conversationId );
117
118
messageListRef .value ?.scrollToBottom ();
118
119
};
119
120
120
121
// Create new conversation
121
122
const createNewConversation = () => {
122
- selectedConversationId .value = ' ' ;
123
123
currentConversationId .value = ' ' ;
124
124
localStorage .removeItem (' currentConversationId' );
125
125
streamError .value = ' ' ;
@@ -128,7 +128,7 @@ const createNewConversation = () => {
128
128
};
129
129
130
130
// Load current conversation details
131
- const loadCurrentConversation = async (conversationId : string ) => {
131
+ const loadCurrentConversation = debounce ( async (conversationId : string ) => {
132
132
if (! conversationId ) {
133
133
currentConversation .value = null ;
134
134
return ;
@@ -137,10 +137,14 @@ const loadCurrentConversation = async (conversationId: string) => {
137
137
const res = await get (` /ai/chat/conversations/${conversationId } ` );
138
138
currentConversation .value = res .data ;
139
139
} catch (error ) {
140
+ if ((error as AxiosError )?.response ?.status === 404 ) {
141
+ currentConversationId .value = ' ' ;
142
+ return ;
143
+ }
140
144
console .error (' Failed to load conversation details:' , error );
141
145
currentConversation .value = null ;
142
146
}
143
- };
147
+ }) ;
144
148
145
149
// Watch for conversation ID changes to load details
146
150
watch (currentConversationId , async newId => {
@@ -154,9 +158,9 @@ watch(currentConversationId, async newId => {
154
158
}
155
159
156
160
// Update selected conversation ID if needed
157
- if (newId && ! selectedConversationId .value ) {
161
+ if (newId && ! currentConversationId .value ) {
158
162
await loadConversations ();
159
- selectedConversationId .value = newId ;
163
+ currentConversationId .value = newId ;
160
164
}
161
165
});
162
166
@@ -171,7 +175,7 @@ onBeforeMount(async () => {
171
175
if (savedConversationId ) {
172
176
await loadConversationMessages (savedConversationId );
173
177
await loadCurrentConversation (savedConversationId );
174
- selectedConversationId .value = savedConversationId ;
178
+ currentConversationId .value = savedConversationId ;
175
179
}
176
180
});
177
181
@@ -202,6 +206,11 @@ const loadLLMProviders = debounce(async () => {
202
206
const res = await get (' /ai/llm/providers' , { available: true });
203
207
availableProviders .value = res .data || [];
204
208
209
+ if (! availableProviders .value .length ) {
210
+ // Reset provider and model if no providers are available
211
+ resetChatbotConfig ();
212
+ }
213
+
205
214
if (! chatbotConfig .value .provider || ! chatbotConfig .value .model ) {
206
215
if (availableProviders .value .length > 0 ) {
207
216
chatbotConfig .value .provider = availableProviders .value [0 ].key ! ;
@@ -238,6 +247,12 @@ const saveChatbotConfig = (config: ChatbotConfig) => {
238
247
ElMessage .success (t (' common.message.success.save' ));
239
248
};
240
249
250
+ // Reset chatbot configuration
251
+ const resetChatbotConfig = () => {
252
+ chatbotConfig .value = {};
253
+ localStorage .removeItem (' chatbotConfig' );
254
+ };
255
+
241
256
// Initialize chat history
242
257
const chatHistory = reactive <ChatMessageType []>([]);
243
258
@@ -453,6 +468,10 @@ const selectProviderModel = ({
453
468
localStorage .setItem (' chatbotConfig' , JSON .stringify (chatbotConfig .value ));
454
469
};
455
470
471
+ const addProviderModel = () => {
472
+ router .push (' /system/ai' );
473
+ };
474
+
456
475
// Configuration dialog
457
476
const configDialogVisible = ref (false );
458
477
@@ -510,7 +529,7 @@ defineOptions({ name: 'ClChatConsole' });
510
529
</template >
511
530
<cl-chat-history
512
531
:conversations =" conversations"
513
- :selected-conversation-id =" selectedConversationId "
532
+ :selected-conversation-id =" currentConversationId "
514
533
:is-loading =" isLoadingConversations"
515
534
@select =" selectConversation"
516
535
@close =" historyDialogVisible = false"
@@ -541,6 +560,7 @@ defineOptions({ name: 'ClChatConsole' });
541
560
@send =" sendMessage"
542
561
@cancel =" cancelMessage"
543
562
@model-change =" selectProviderModel"
563
+ @add-model =" addProviderModel"
544
564
/>
545
565
</div >
546
566
</div >
0 commit comments