From de680d8ebd348b5ffb65386bc2cc35b967a3f847 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Fri, 18 Apr 2025 16:58:42 +0800 Subject: [PATCH] refactor: streamline conversation initialization and enhance log handling - Refactored AssistantConsole to utilize a dedicated initializeConversation function for improved clarity and organization. - Updated DependencyLogsDialog to rename computed property from content to logs for better semantic understanding. - Enhanced LogsView component by wrapping logs in an el-scrollbar for improved user experience. - Adjusted ChatInput to call updateSelectedProviderModel on onBeforeMount for better state management. - Simplified default model handling in ai.ts by introducing a DEFAULT_OPENAI_MODELS constant for consistency. --- .../components/core/ai/AssistantConsole.vue | 31 ++++++++----------- .../components/core/ai/useAssistantConsole.ts | 14 +++++++++ .../core/dependency/DependencyLogsDialog.vue | 8 ++--- .../src/components/ui/chat/ChatInput.vue | 3 +- .../src/components/ui/logs/LogsView.vue | 10 +++--- frontend/crawlab-ui/src/utils/ai.ts | 31 +++++++++---------- .../system/detail/tabs/SystemDetailTabAi.vue | 28 ++++++++--------- 7 files changed, 66 insertions(+), 59 deletions(-) diff --git a/frontend/crawlab-ui/src/components/core/ai/AssistantConsole.vue b/frontend/crawlab-ui/src/components/core/ai/AssistantConsole.vue index 6159660a..d2719144 100644 --- a/frontend/crawlab-ui/src/components/core/ai/AssistantConsole.vue +++ b/frontend/crawlab-ui/src/components/core/ai/AssistantConsole.vue @@ -6,7 +6,7 @@ import { debounce } from 'lodash'; import { useRouter } from 'vue-router'; import useAssistantConsole from './useAssistantConsole'; -defineProps<{ +const props = defineProps<{ visible: boolean; }>(); @@ -35,15 +35,12 @@ const { chatbotConfig, currentConversationTitle, loadConversations, - loadConversationMessages, - loadCurrentConversation, - loadLLMProviders, - loadChatbotConfig, saveChatbotConfig, selectConversation, createNewConversation, sendStreamingRequest, extractErrorMessage, + initializeConversation, } = useAssistantConsole(); // Message handling @@ -105,7 +102,9 @@ const cancelMessage = () => { abortController.value = null; isGenerating.value = false; - const streamingMessageIndex = chatHistory.findIndex((msg: ChatMessage) => msg.isStreaming); + const streamingMessageIndex = chatHistory.findIndex( + (msg: ChatMessage) => msg.isStreaming + ); if (streamingMessageIndex >= 0) { chatHistory.splice(streamingMessageIndex, 1); } @@ -156,19 +155,15 @@ watch(isGenerating, () => { }); // Initialize -onBeforeMount(async () => { - await loadConversations(); - loadChatbotConfig(); - await loadLLMProviders(); - - // Load saved conversation ID from localStorage - const savedConversationId = localStorage.getItem('currentConversationId'); - if (savedConversationId) { - await loadConversationMessages(savedConversationId); - await loadCurrentConversation(savedConversationId); - currentConversationId.value = savedConversationId; +onBeforeMount(initializeConversation); +watch( + () => props.visible, + async () => { + if (props.visible) { + await initializeConversation(); + } } -}); +); defineOptions({ name: 'ClAssistantConsole' }); diff --git a/frontend/crawlab-ui/src/components/core/ai/useAssistantConsole.ts b/frontend/crawlab-ui/src/components/core/ai/useAssistantConsole.ts index dea896cc..e62d5790 100644 --- a/frontend/crawlab-ui/src/components/core/ai/useAssistantConsole.ts +++ b/frontend/crawlab-ui/src/components/core/ai/useAssistantConsole.ts @@ -418,6 +418,19 @@ const useAssistantConsole = () => { }; onMounted(focusInput); + const initializeConversation = async () => { + loadChatbotConfig(); + await loadLLMProviders(); + + // Load saved conversation ID from localStorage + const savedConversationId = localStorage.getItem('currentConversationId'); + if (savedConversationId) { + await loadConversationMessages(savedConversationId); + await loadCurrentConversation(savedConversationId); + currentConversationId.value = savedConversationId; + } + }; + return { // Refs messageListRef, @@ -451,6 +464,7 @@ const useAssistantConsole = () => { createNewConversation, sendStreamingRequest, extractErrorMessage, + initializeConversation, }; }; diff --git a/frontend/crawlab-ui/src/components/core/dependency/DependencyLogsDialog.vue b/frontend/crawlab-ui/src/components/core/dependency/DependencyLogsDialog.vue index b421efdb..a3a728d1 100644 --- a/frontend/crawlab-ui/src/components/core/dependency/DependencyLogsDialog.vue +++ b/frontend/crawlab-ui/src/components/core/dependency/DependencyLogsDialog.vue @@ -15,7 +15,7 @@ const visible = computed(() => state.activeDialogKey === 'logs'); const activeTargetName = computed(() => state.activeTargetName); const activeTargetStatus = computed(() => state.activeTargetStatus); -const content = computed(() => { +const logs = computed(() => { const data: string[] = []; state.activeTargetLogs?.forEach(l => { l.content @@ -25,7 +25,7 @@ const content = computed(() => { data.push(line.trim()); }); }); - return data.join('\n'); + return data; }); const logsViewRef = ref(); @@ -86,9 +86,7 @@ defineOptions({ name: 'ClDependencyLogsDialog' }); {{ t('common.tabs.logs') }} - {{ activeTargetName }} -
-
{{ content }}
-
+ diff --git a/frontend/crawlab-ui/src/components/ui/chat/ChatInput.vue b/frontend/crawlab-ui/src/components/ui/chat/ChatInput.vue index fddea95c..551a02c3 100644 --- a/frontend/crawlab-ui/src/components/ui/chat/ChatInput.vue +++ b/frontend/crawlab-ui/src/components/ui/chat/ChatInput.vue @@ -1,5 +1,5 @@