From 15b81d1581ef520eca0a342df11a35f071fea825 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 8 Jul 2026 00:22:46 +0800 Subject: [PATCH] fix(agent): gate web search controls by provider readiness --- frontend/src/components/AgentSelector.vue | 135 ++++++++++++++++++--- frontend/src/components/Input-field.vue | 83 +++++++------ frontend/src/i18n/locales/en-US.ts | 4 + frontend/src/i18n/locales/ko-KR.ts | 4 + frontend/src/i18n/locales/ru-RU.ts | 4 + frontend/src/i18n/locales/zh-CN.ts | 4 + frontend/src/utils/agentWebSearch.test.mjs | 59 +++++++++ frontend/src/utils/agentWebSearch.ts | 37 ++++++ 8 files changed, 277 insertions(+), 53 deletions(-) create mode 100644 frontend/src/utils/agentWebSearch.test.mjs create mode 100644 frontend/src/utils/agentWebSearch.ts diff --git a/frontend/src/components/AgentSelector.vue b/frontend/src/components/AgentSelector.vue index 573d3502..3b1e07de 100644 --- a/frontend/src/components/AgentSelector.vue +++ b/frontend/src/components/AgentSelector.vue @@ -131,9 +131,6 @@ {{ getKbCapability(activeDetail.agent) }} - {{ - $t('agent.capabilities.webSearchOn') - }} {{ getMcpCapability(activeDetail.agent) }} {{ @@ -141,6 +138,24 @@ }} +
+
{{ $t('agent.selector.capabilitiesSection') }}
+
+ + + {{ getWebSearchCapability(activeDetail.agent) }} + + + + {{ getImageUploadCapability(activeDetail.agent) }} + +
+
+
@@ -178,11 +193,17 @@ import { type AgentNotReadyReasonKey, } from '@/utils/agent-readiness'; import { formatLocalizedList } from '@/utils/format-list'; +import { useChatResourcesStore } from '@/stores/chatResources'; +import { + isAgentWebSearchEnabled, + isAgentWebSearchReady, +} from '@/utils/agentWebSearch'; const { t, locale } = useI18n(); const router = useRouter(); const orgStore = useOrganizationStore(); const settingsStore = useSettingsStore(); +const chatResources = useChatResourcesStore(); const props = defineProps<{ visible: boolean; @@ -216,10 +237,11 @@ const detailPanelStyle = ref>({}); let detailHideTimer: ReturnType | null = null; const DETAIL_PANEL_WIDTH = 200; -const DETAIL_BRIDGE_OVERLAP = 10; +const DETAIL_PANEL_GAP = 8; const DETAIL_HIDE_DELAY_MS = 400; const agentsList = computed(() => props.agents ?? []); +const webSearchProviders = computed(() => chatResources.webSearchProviders); const builtinAgents = computed(() => { const apiBuiltins = agentsList.value.filter(a => a.is_builtin); @@ -289,6 +311,35 @@ const getKbCapability = (agent: CustomAgent): string => { return ''; }; +const isWebSearchEnabledForAgent = (agent: CustomAgent): boolean => { + return isAgentWebSearchEnabled(agent.config); +}; + +const isWebSearchReadyForAgent = (agent: CustomAgent): boolean => { + return isAgentWebSearchReady(agent.config, webSearchProviders.value); +}; + +const isImageUploadEnabledForAgent = (agent: CustomAgent): boolean => { + const config = agent.config || {}; + return config.image_upload_enabled === true; +}; + +const getWebSearchCapability = (agent: CustomAgent): string => { + if (!isWebSearchEnabledForAgent(agent)) { + return t('agent.capabilities.webSearchOff'); + } + if (!isWebSearchReadyForAgent(agent)) { + return t('agent.capabilities.webSearchUnconfigured'); + } + return t('agent.capabilities.webSearchOn'); +}; + +const getImageUploadCapability = (agent: CustomAgent): string => { + return isImageUploadEnabledForAgent(agent) + ? t('agent.capabilities.imageUploadOn') + : t('agent.capabilities.imageUploadOff'); +}; + const getMcpCapability = (agent: CustomAgent): string => { const config = agent.config || {}; if (config.mcp_selection_mode === 'none' || (!config.mcp_services?.length && config.mcp_selection_mode !== 'all')) { @@ -359,8 +410,8 @@ const updateDetailPanelPosition = () => { const rowRect = rectToCssPx(el.getBoundingClientRect(), zoom); const { width: vw, height: vh } = cssViewportSize(zoom); - // 向左重叠一段透明区域,避免鼠标从选项移向浮层时经过空隙触发 mouseleave - let left = rowRect.right - DETAIL_BRIDGE_OVERLAP; + // 浮层显示在列表右侧,保留小间隙;透明桥接区覆盖间隙,避免鼠标移入时浮层消失 + let left = rowRect.right + DETAIL_PANEL_GAP; if (left + DETAIL_PANEL_WIDTH > vw - 8) { left = Math.max(8, vw - DETAIL_PANEL_WIDTH - 8); } @@ -478,7 +529,7 @@ const updateDropdownPosition = () => { const rect = rectToCssPx(props.anchorEl.getBoundingClientRect(), zoom); const { width: vw, height: vh } = cssViewportSize(zoom); - const dropdownWidth = 196; + const dropdownWidth = 220; const offsetY = 6; let left = Math.floor(rect.left); @@ -524,6 +575,7 @@ const updateDropdownPosition = () => { watch(() => props.visible, (newVal) => { if (newVal) { nextTick(() => updateDropdownPosition()); + chatResources.ensureWebSearchProviders(); } else { hideDetailPanel(); } @@ -583,8 +635,8 @@ watch(activeDetail, (detail) => { align-items: center; justify-content: space-between; gap: 8px; - min-height: 32px; - padding: 0 8px; + min-height: 36px; + padding: 8px 12px; border-bottom: .5px solid var(--td-component-stroke); font-size: 12px; font-weight: 500; @@ -618,7 +670,7 @@ watch(activeDetail, (detail) => { overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; - padding: 4px 0; + padding: 6px 4px 8px; } .agent-group { @@ -632,7 +684,7 @@ watch(activeDetail, (detail) => { .agent-group-title { font-size: 11px; color: var(--td-text-color-placeholder); - padding: 6px 8px 4px; + padding: 8px 12px 4px; font-weight: 600; line-height: 16px; } @@ -641,8 +693,9 @@ watch(activeDetail, (detail) => { display: flex; align-items: center; gap: 8px; - min-height: 32px; - padding: 0 8px; + min-height: 34px; + margin: 0 4px; + padding: 6px 8px; cursor: pointer; transition: background 0.12s; border-radius: 5px; @@ -761,14 +814,16 @@ watch(activeDetail, (detail) => { .agent-detail-panel { box-sizing: border-box; position: relative; + --detail-panel-gap: 8px; + --detail-bridge-width: 8px; // 左侧透明桥接区:承接从选项移入的鼠标,避免经过间隙时浮层消失 &::before { content: ''; position: absolute; - left: -12px; + left: calc(-1 * (var(--detail-bridge-width) + var(--detail-panel-gap))); top: 0; - width: 12px; + width: calc(var(--detail-bridge-width) + var(--detail-panel-gap)); height: 100%; } } @@ -913,6 +968,25 @@ watch(activeDetail, (detail) => { gap: 6px; padding-top: 8px; border-top: .5px solid var(--td-component-stroke); + + &--capabilities { + padding-top: 0; + border-top: none; + } +} + +.detail-tag-group { + margin-top: 8px; + padding-top: 8px; + border-top: .5px solid var(--td-component-stroke); +} + +.detail-tag-group-title { + font-size: 11px; + font-weight: 600; + line-height: 16px; + color: var(--td-text-color-placeholder); + margin-bottom: 6px; } .detail-tag { @@ -927,6 +1001,37 @@ watch(activeDetail, (detail) => { background: var(--td-bg-color-secondarycontainer); } +.detail-capability-tag { + gap: 4px; + padding: 3px 8px; + + &--on { + color: var(--td-text-color-secondary); + border-color: var(--td-component-border); + background: var(--td-bg-color-secondarycontainer); + + .detail-capability-icon { + color: var(--td-text-color-primary); + } + } + + &--off { + color: var(--td-text-color-placeholder); + border-style: dashed; + border-color: var(--td-component-stroke); + background: transparent; + + .detail-capability-icon { + color: var(--td-text-color-placeholder); + } + } +} + +.detail-capability-icon { + flex-shrink: 0; + line-height: 1; +} + .detail-meta { margin-top: 8px; padding-top: 8px; diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index 757fed98..48f8608b 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -28,6 +28,11 @@ import { toolsConsumeFiles, type ScopeCapabilities, } from '@/utils/tool-capabilities'; +import { + isAgentWebSearchEnabled, + isAgentWebSearchReady, + isTenantWebSearchReady, +} from '@/utils/agentWebSearch'; import { getAgentNotReadyReasonKeys, resolveAgentNotReadySection, @@ -258,20 +263,15 @@ watch([selectedAgentId, () => settingsStore.selectedAgentSourceTenantId], async } }, { immediate: true }); -// 智能体是否启用了网络搜索 -const agentWebSearchEnabled = computed(() => { - if (!hasAgentConfig.value) return null; // null 表示不受智能体控制 - return currentAgentConfig.value?.web_search_enabled ?? true; +// 智能体是否启用了网络搜索(仅显式开启才算支持) +const isWebSearchEnabledByAgent = computed(() => { + if (!hasAgentConfig.value) return null; + return isAgentWebSearchEnabled(currentAgentConfig.value); }); -const agentWebSearchProviderId = computed(() => { - if (!hasAgentConfig.value) return ''; - return currentAgentConfig.value?.web_search_provider_id || ''; -}); - -// 网络搜索是否被智能体禁用(只读状态)- 只有明确设置为 false 时才禁用 +// 网络搜索是否被智能体禁用 const isWebSearchDisabledByAgent = computed(() => { - return hasAgentConfig.value && agentWebSearchEnabled.value === false; + return hasAgentConfig.value && isWebSearchEnabledByAgent.value !== true; }); // 知识库选择是否被智能体锁定 @@ -433,6 +433,15 @@ const isImageUploadEnabledByAgent = computed(() => { return currentAgentConfig.value?.image_upload_enabled === true; }); +// Input 工具栏:仅当智能体已启用且搜索引擎可用时才显示 +const showWebSearchButton = computed(() => { + if (!hasAgentConfig.value) { + return isTenantWebSearchReady(webSearchProviders.value); + } + return isAgentWebSearchReady(currentAgentConfig.value, webSearchProviders.value); +}); +const showImageUploadButton = computed(() => isImageUploadEnabledByAgent.value); + // 模型选择是否被智能体锁定 - 已移除锁定逻辑,允许用户自由切换模型 const isModelLockedByAgent = computed(() => { return false; @@ -791,12 +800,10 @@ watch(selectedFileIds, () => { }, { immediate: true }); const isWebSearchConfigured = computed(() => { - const agentProviderId = agentWebSearchProviderId.value; - if (agentProviderId) { - return webSearchProviders.value.some(p => p.id === agentProviderId); + if (hasAgentConfig.value) { + return isAgentWebSearchReady(currentAgentConfig.value, webSearchProviders.value); } - - return webSearchProviders.value.some(p => p.is_default); + return isTenantWebSearchReady(webSearchProviders.value); }); const loadWebSearchConfig = async (force = false) => { @@ -2215,6 +2222,14 @@ const handleGoToWebSearchSettings = () => { } }; +const handleGoToWebSearchConfig = () => { + if (hasAgentConfig.value && selectedAgent.value) { + handleGoToAgentSettings('websearch'); + return; + } + handleGoToWebSearchSettings(); +}; + const handleGoToAgentSettings = (section?: string) => { const agent = selectedAgent.value; if (!agent) { @@ -2343,7 +2358,7 @@ const toggleWebSearch = () => { href: '#', onClick: (e: Event) => { e.preventDefault(); - handleGoToWebSearchSettings(); + handleGoToWebSearchConfig(); }, style: 'color: var(--td-brand-color); text-decoration: none; font-weight: 500; cursor: pointer; align-self: flex-start;', onMouseenter: (e: Event) => { @@ -2352,7 +2367,7 @@ const toggleWebSearch = () => { onMouseleave: (e: Event) => { (e.target as HTMLElement).style.textDecoration = 'none'; } - }, t('input.goToSettings')) + }, t('input.goToAgentSettings')) ]); MessagePlugin.warning({ content: () => messageContent, @@ -2485,24 +2500,20 @@ defineExpose({ :currentAgentId="selectedAgentId" :agents="enabledAgents" :all-models="allModels" @close="closeAgentModeSelector" @select="handleSelectAgent" @not-ready="handleAgentNotReady" /> - - + +
@@ -2517,19 +2528,15 @@ defineExpose({
- - + +
+ 'active': uploadedImages.length > 0 + }" @click.stop="triggerImageUpload()"> diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index dc652652..7ff62ff0 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -924,6 +924,9 @@ export default { rerankSpecified: 'ReRank model specified', webSearchOn: 'Web search enabled', webSearchOff: 'Web search disabled', + webSearchUnconfigured: 'Search engine not configured', + imageUploadOn: 'Image upload supported', + imageUploadOff: 'Image upload not supported', hasPrompt: 'Custom prompt', default: 'Default configuration', mcpEnabled: 'MCP services enabled', @@ -1151,6 +1154,7 @@ export default { notReadyMissing: 'Missing: {items}', configureAction: 'Configure', sharedNotReadyContact: 'Ask the sharing organization admin to finish setup', + capabilitiesSection: 'Capabilities', }, // Built-in agent information builtinInfo: { diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index d207705d..bfe339e1 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -1929,6 +1929,9 @@ export default { rerankSpecified: "ReRank 모델 지정", webSearchOn: "웹 검색 활성화", webSearchOff: "웹 검색 비활성화", + webSearchUnconfigured: "검색 엔진 미구성", + imageUploadOn: "이미지 업로드 지원", + imageUploadOff: "이미지 업로드 미지원", hasPrompt: "맞춤 프롬프트", default: "기본 구성", mcpEnabled: "MCP 서비스 활성화", @@ -2166,6 +2169,7 @@ export default { notReadyMissing: "누락: {items}", configureAction: "구성", sharedNotReadyContact: "공유한 조직 관리자에게 구성을 요청하세요", + capabilitiesSection: "능력", }, // 내장 에이전트 정보 builtinInfo: { diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 670bef39..ce1cfa34 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -968,6 +968,9 @@ export default { rerankSpecified: 'ReRank model specified', webSearchOn: 'Web search enabled', webSearchOff: 'Web search disabled', + webSearchUnconfigured: 'Поисковая система не настроена', + imageUploadOn: 'Загрузка изображений поддерживается', + imageUploadOff: 'Загрузка изображений не поддерживается', hasPrompt: 'Custom prompt', default: 'Default configuration', mcpEnabled: 'MCP services enabled', @@ -1065,6 +1068,7 @@ export default { notReadyMissing: 'Missing: {items}', configureAction: 'Configure', sharedNotReadyContact: 'Ask the sharing organization admin to finish setup', + capabilitiesSection: 'Возможности', }, builtinInfo: { quickAnswer: { diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index d976bcb7..48e6b9df 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -1935,6 +1935,9 @@ export default { rerankSpecified: "指定 ReRank 模型", webSearchOn: "启用网络搜索", webSearchOff: "禁用网络搜索", + webSearchUnconfigured: "未配置搜索引擎", + imageUploadOn: "支持图片上传", + imageUploadOff: "不支持图片上传", hasPrompt: "自定义提示词", default: "默认配置", mcpEnabled: "启用 MCP 服务", @@ -2162,6 +2165,7 @@ export default { notReadyMissing: "缺少 {items}", configureAction: "去配置", sharedNotReadyContact: "请联系分享方管理员完成配置", + capabilitiesSection: "能力", }, // 内置智能体信息 builtinInfo: { diff --git a/frontend/src/utils/agentWebSearch.test.mjs b/frontend/src/utils/agentWebSearch.test.mjs new file mode 100644 index 00000000..daf64579 --- /dev/null +++ b/frontend/src/utils/agentWebSearch.test.mjs @@ -0,0 +1,59 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { + resolveAgentWebSearchProviderId, + isAgentWebSearchReady, + isTenantWebSearchReady, +} from './agentWebSearch.ts'; + +const providers = [ + { id: 'p1', name: 'Keenable', is_default: false }, + { id: 'p2', name: 'Default', is_default: true }, +]; + +test('resolveAgentWebSearchProviderId uses explicit agent provider', () => { + assert.equal( + resolveAgentWebSearchProviderId({ web_search_provider_id: 'p1' }, providers), + 'p1', + ); +}); + +test('resolveAgentWebSearchProviderId falls back to tenant default', () => { + assert.equal( + resolveAgentWebSearchProviderId({ web_search_provider_id: '' }, providers), + 'p2', + ); +}); + +test('resolveAgentWebSearchProviderId returns null when default missing', () => { + const noDefault = [{ id: 'p1', name: 'Keenable', is_default: false }]; + assert.equal( + resolveAgentWebSearchProviderId({ web_search_provider_id: '' }, noDefault), + null, + ); +}); + +test('isAgentWebSearchReady requires enabled flag and resolvable provider', () => { + assert.equal( + isAgentWebSearchReady({ web_search_enabled: true }, providers), + true, + ); + assert.equal( + isAgentWebSearchReady({ web_search_enabled: true, web_search_provider_id: '' }, [ + { id: 'p1', name: 'Keenable', is_default: false }, + ]), + false, + ); + assert.equal( + isAgentWebSearchReady({ web_search_enabled: false }, providers), + false, + ); +}); + +test('isTenantWebSearchReady checks default provider only', () => { + assert.equal(isTenantWebSearchReady(providers), true); + assert.equal( + isTenantWebSearchReady([{ id: 'p1', name: 'Keenable', is_default: false }]), + false, + ); +}); diff --git a/frontend/src/utils/agentWebSearch.ts b/frontend/src/utils/agentWebSearch.ts new file mode 100644 index 00000000..cb17e02d --- /dev/null +++ b/frontend/src/utils/agentWebSearch.ts @@ -0,0 +1,37 @@ +import type { WebSearchProviderEntity } from '@/api/web-search-provider'; + +export type AgentWebSearchConfig = { + web_search_enabled?: boolean; + web_search_provider_id?: string; +}; + +/** 解析智能体实际会使用的搜索引擎 ID(与后端 agent > tenant default 逻辑一致) */ +export function resolveAgentWebSearchProviderId( + config: AgentWebSearchConfig | undefined, + providers: WebSearchProviderEntity[], +): string | null { + const explicitId = config?.web_search_provider_id?.trim(); + if (explicitId) { + return providers.some((p) => p.id === explicitId) ? explicitId : null; + } + const defaultProvider = providers.find((p) => p.is_default); + return defaultProvider?.id ?? null; +} + +export function isAgentWebSearchEnabled(config: AgentWebSearchConfig | undefined): boolean { + return config?.web_search_enabled === true; +} + +/** 智能体已启用网络搜索,且能解析到可用搜索引擎 */ +export function isAgentWebSearchReady( + config: AgentWebSearchConfig | undefined, + providers: WebSearchProviderEntity[], +): boolean { + if (!isAgentWebSearchEnabled(config)) return false; + return resolveAgentWebSearchProviderId(config, providers) !== null; +} + +/** 租户级默认搜索引擎是否可用(无智能体约束时) */ +export function isTenantWebSearchReady(providers: WebSearchProviderEntity[]): boolean { + return providers.some((p) => p.is_default); +}