diff --git a/backend/app/services/text2sql.py b/backend/app/services/text2sql.py index 94189acb..f30254fc 100644 --- a/backend/app/services/text2sql.py +++ b/backend/app/services/text2sql.py @@ -7,6 +7,7 @@ from flask import current_app, has_app_context class Text2SQLService: _schema_text = None _oracle_client_initialized = False + _oracle_dll_handles = [] @staticmethod def _backend_dir() -> str: @@ -86,6 +87,9 @@ class Text2SQLService: cls._prepend_env_path("DYLD_LIBRARY_PATH", target_lib_dir) elif system_name == "Windows": cls._prepend_env_path("PATH", target_lib_dir) + if hasattr(os, "add_dll_directory"): + handle = os.add_dll_directory(target_lib_dir) + cls._oracle_dll_handles.append(handle) except Exception as exc: cls._log("warning", f"Failed to prepare Oracle runtime env: {exc}") @@ -108,9 +112,9 @@ class Text2SQLService: if platform.system() == 'Windows' and target_lib_dir and not target_lib_dir.isascii(): temp_dir = os.path.join(tempfile.gettempdir(), 'hxops_oracle_client') safe_lib_dir = os.path.join(temp_dir, os.path.basename(target_lib_dir)) - if not os.path.exists(safe_lib_dir) and os.path.exists(target_lib_dir): + if os.path.exists(target_lib_dir): os.makedirs(temp_dir, exist_ok=True) - shutil.copytree(target_lib_dir, safe_lib_dir) + shutil.copytree(target_lib_dir, safe_lib_dir, dirs_exist_ok=True) target_lib_dir = safe_lib_dir cls._prepare_oracle_runtime_env(target_lib_dir) @@ -185,16 +189,18 @@ class Text2SQLService: import tempfile target_lib_dir = cls._get_oracle_client_dir() + cls._prepare_oracle_runtime_env(target_lib_dir) if platform.system() == 'Windows' and target_lib_dir: # OCI C-library fails to load if path contains non-ASCII. if not target_lib_dir.isascii(): temp_dir = os.path.join(tempfile.gettempdir(), 'hxops_oracle_client') safe_lib_dir = os.path.join(temp_dir, os.path.basename(target_lib_dir)) - if not os.path.exists(safe_lib_dir) and os.path.exists(target_lib_dir): + if os.path.exists(target_lib_dir): os.makedirs(temp_dir, exist_ok=True) - shutil.copytree(target_lib_dir, safe_lib_dir) + shutil.copytree(target_lib_dir, safe_lib_dir, dirs_exist_ok=True) target_lib_dir = safe_lib_dir + cls._prepare_oracle_runtime_env(target_lib_dir) if target_lib_dir: oracledb.init_oracle_client(lib_dir=target_lib_dir) diff --git a/frontend/src/views/ChatView.vue b/frontend/src/views/ChatView.vue index 92355651..dd6fed93 100644 --- a/frontend/src/views/ChatView.vue +++ b/frontend/src/views/ChatView.vue @@ -71,13 +71,17 @@ const currentUser = ref(getStoredAuthUser()); const showUserDropdown = ref(false); const userMenuRef = ref(null); -const selectedKb = ref('企业知识库'); +const DEFAULT_KB = '企业知识库'; +const ERP_KB = 'ERP知识库'; +const ERP_KB_ENABLED_AGENT_NAMES = new Set(['固资管理']); + +const selectedKb = ref(DEFAULT_KB); const showKbDropdown = ref(false); const kbMenuRef = ref(null); -const kbOptions = ['企业知识库', 'ERP知识库']; +const kbOptions = [DEFAULT_KB, ERP_KB]; const getKbLabel = (kb: string) => { - if (kb === '企业知识库') return t('chat.kb.enterprise'); - if (kb === 'ERP知识库') return t('chat.kb.erp'); + if (kb === DEFAULT_KB) return t('chat.kb.enterprise'); + if (kb === ERP_KB) return t('chat.kb.erp'); return kb; }; const chatScrollContainer = ref(null); @@ -782,6 +786,37 @@ const normalizeAgentSuggestedQuestions = (value: unknown): AgentSuggestedQuestio }; const currentAgentName = computed(() => String(currentAgent.value?.name || '').trim()); +const canSelectErpKnowledgeBase = computed(() => ERP_KB_ENABLED_AGENT_NAMES.has(currentAgentName.value)); +const availableKbOptions = computed(() => ( + canSelectErpKnowledgeBase.value + ? kbOptions + : kbOptions.filter((option) => option !== ERP_KB) +)); +const kbDropdownDisabled = computed(() => availableKbOptions.value.length <= 1); + +const toggleKbDropdown = () => { + if (kbDropdownDisabled.value) { + showKbDropdown.value = false; + return; + } + showKbDropdown.value = !showKbDropdown.value; +}; + +const selectKb = (option: string) => { + if (!availableKbOptions.value.includes(option)) return; + selectedKb.value = option; + showKbDropdown.value = false; +}; + +watch(canSelectErpKnowledgeBase, (allowed) => { + if (!allowed && selectedKb.value === ERP_KB) { + selectedKb.value = DEFAULT_KB; + } + if (!allowed) { + showKbDropdown.value = false; + } +}, { immediate: true }); + const agentDescription = computed(() => { const text = String(currentAgent.value?.description || '').trim(); return text || DEFAULT_AGENT_DESCRIPTIONS[currentAgentName.value] || ''; @@ -3567,20 +3602,21 @@ const showChatRating = ref(false);