diff --git a/frontend/src/api/agent/index.ts b/frontend/src/api/agent/index.ts new file mode 100644 index 00000000..b41e9566 --- /dev/null +++ b/frontend/src/api/agent/index.ts @@ -0,0 +1,94 @@ +import { get, post, put, del } from "../../utils/request"; + +// 智能体类型 +export type CustomAgentType = 'normal' | 'agent' | 'custom'; + +// 智能体配置 +export interface CustomAgentConfig { + agent_mode?: 'normal' | 'agent'; // 运行模式:normal=RAG模式, agent=ReAct Agent模式 + system_prompt?: string; + model_id?: string; + rerank_model_id?: string; // ReRank 模型 ID(当使用知识库时需要) + temperature?: number; + max_iterations?: number; + allowed_tools?: string[]; + knowledge_bases?: string[]; + // 当没有配置知识库时,是否允许用户自由选择知识库 + // true (默认): 用户可以自由选择任意知识库 + // false: 禁用知识库选择功能 + allow_user_kb_selection?: boolean; + web_search_enabled?: boolean; + web_search_max_results?: number; + reflection_enabled?: boolean; + welcome_message?: string; + suggested_prompts?: string[]; + multi_turn_enabled?: boolean; // 是否启用多轮对话 + history_turns?: number; // 保留历史轮数 +} + +// 智能体 +export interface CustomAgent { + id: string; + name: string; + description?: string; + avatar?: string; + is_builtin: boolean; + type: CustomAgentType; + tenant_id?: number; + created_by?: string; + config: CustomAgentConfig; + created_at?: string; + updated_at?: string; +} + +// 创建智能体请求 +export interface CreateAgentRequest { + name: string; + description?: string; + avatar?: string; + type?: CustomAgentType; + config?: CustomAgentConfig; +} + +// 更新智能体请求 +export interface UpdateAgentRequest { + name: string; + description?: string; + avatar?: string; + type?: CustomAgentType; + config?: CustomAgentConfig; +} + +// 内置智能体 ID +export const BUILTIN_AGENT_NORMAL_ID = 'builtin-normal'; +export const BUILTIN_AGENT_AGENT_ID = 'builtin-agent'; + +// 获取智能体列表(包括内置智能体) +export function listAgents() { + return get<{ data: CustomAgent[] }>('/api/v1/agents'); +} + +// 获取智能体详情 +export function getAgentById(id: string) { + return get<{ data: CustomAgent }>(`/api/v1/agents/${id}`); +} + +// 创建智能体 +export function createAgent(data: CreateAgentRequest) { + return post<{ data: CustomAgent }>('/api/v1/agents', data); +} + +// 更新智能体 +export function updateAgent(id: string, data: UpdateAgentRequest) { + return put<{ data: CustomAgent }>(`/api/v1/agents/${id}`, data); +} + +// 删除智能体 +export function deleteAgent(id: string) { + return del<{ success: boolean }>(`/api/v1/agents/${id}`); +} + +// 判断是否为内置智能体 +export function isBuiltinAgent(agentId: string): boolean { + return agentId === BUILTIN_AGENT_NORMAL_ID || agentId === BUILTIN_AGENT_AGENT_ID; +} diff --git a/frontend/src/api/chat/streame.ts b/frontend/src/api/chat/streame.ts index 4b0ae712..f7dd5628 100644 --- a/frontend/src/api/chat/streame.ts +++ b/frontend/src/api/chat/streame.ts @@ -28,7 +28,7 @@ export function useStream() { let renderTimer: number | null = null // 启动流式请求 - const startStream = async (params: { session_id: any; query: any; knowledge_base_ids?: string[]; knowledge_ids?: string[]; agent_enabled?: boolean; web_search_enabled?: boolean; summary_model_id?: string; mcp_service_ids?: string[]; mentioned_items?: Array<{id: string; name: string; type: string; kb_type?: string}>; method: string; url: string }) => { + const startStream = async (params: { session_id: any; query: any; knowledge_base_ids?: string[]; knowledge_ids?: string[]; agent_enabled?: boolean; agent_id?: string; web_search_enabled?: boolean; summary_model_id?: string; mcp_service_ids?: string[]; mentioned_items?: Array<{id: string; name: string; type: string; kb_type?: string}>; method: string; url: string }) => { // 重置状态 output.value = ''; error.value = null; @@ -89,6 +89,10 @@ export function useStream() { if (params.knowledge_ids !== undefined && params.knowledge_ids.length > 0) { postBody.knowledge_ids = params.knowledge_ids; } + // Include agent_id if provided (for custom agent configuration) + if (params.agent_id) { + postBody.agent_id = params.agent_id; + } // Include web_search_enabled if provided if (params.web_search_enabled !== undefined) { postBody.web_search_enabled = params.web_search_enabled; diff --git a/frontend/src/assets/img/agent-green.svg b/frontend/src/assets/img/agent-green.svg new file mode 100644 index 00000000..96b5b46c --- /dev/null +++ b/frontend/src/assets/img/agent-green.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/frontend/src/assets/img/agent.svg b/frontend/src/assets/img/agent.svg index 458f55da..3b94a604 100644 --- a/frontend/src/assets/img/agent.svg +++ b/frontend/src/assets/img/agent.svg @@ -1,5 +1,5 @@ - - - - \ No newline at end of file + + + + diff --git a/frontend/src/components/AgentAvatar.vue b/frontend/src/components/AgentAvatar.vue new file mode 100644 index 00000000..570e5181 --- /dev/null +++ b/frontend/src/components/AgentAvatar.vue @@ -0,0 +1,155 @@ + + + + + diff --git a/frontend/src/components/AgentSelector.vue b/frontend/src/components/AgentSelector.vue new file mode 100644 index 00000000..e8d6701f --- /dev/null +++ b/frontend/src/components/AgentSelector.vue @@ -0,0 +1,430 @@ + + + + + diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index 28077bac..8499351c 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -9,8 +9,10 @@ import { listKnowledgeBases, searchKnowledge, batchQueryKnowledge } from '@/api/ import { stopSession } from '@/api/chat'; import KnowledgeBaseSelector from './KnowledgeBaseSelector.vue'; import MentionSelector from './MentionSelector.vue'; +import AgentSelector from './AgentSelector.vue'; import { getCaretCoordinates } from '@/utils/caret'; import { listModels, type ModelConfig } from '@/api/model'; +import { listAgents, type CustomAgent } from '@/api/agent'; import { getTenantWebSearchConfig } from '@/api/web-search'; import { getConversationConfig, updateConversationConfig, type ConversationConfig } from '@/api/system'; import { useI18n } from 'vue-i18n'; @@ -26,6 +28,81 @@ const showAgentModeSelector = ref(false); const agentModeButtonRef = ref(); const agentModeDropdownStyle = ref>({}); +// 智能体相关状态 +const agents = ref([]); +const selectedAgentId = computed({ + get: () => settingsStore.selectedAgentId || 'builtin-normal', + set: (val: string) => settingsStore.selectAgent(val) +}); +const selectedAgent = computed(() => { + return agents.value.find(a => a.id === selectedAgentId.value) || { + id: 'builtin-normal', + name: t('input.normalMode'), + type: 'normal', + is_builtin: true + } as CustomAgent; +}); + +// 判断是否为自定义智能体(非内置) +const isCustomAgent = computed(() => { + const agent = selectedAgent.value; + return agent && !agent.is_builtin && agent.type === 'custom'; +}); + +// 智能体预配置的知识库 IDs +const agentKnowledgeBases = computed(() => { + if (!isCustomAgent.value) return []; + return selectedAgent.value?.config?.knowledge_bases || []; +}); + +// 智能体是否启用了网络搜索 +const agentWebSearchEnabled = computed(() => { + if (!isCustomAgent.value) return null; // null 表示不受智能体控制 + return selectedAgent.value?.config?.web_search_enabled ?? false; +}); + +// 网络搜索是否被智能体禁用(只读状态) +const isWebSearchDisabledByAgent = computed(() => { + return isCustomAgent.value && agentWebSearchEnabled.value === false; +}); + +// 网络搜索是否被智能体强制启用 +const isWebSearchForcedByAgent = computed(() => { + return isCustomAgent.value && agentWebSearchEnabled.value === true; +}); + +// 知识库选择是否被智能体锁定 +// 1. 如果智能体配置了知识库 → 锁定到这些知识库 +// 2. 如果智能体没配置知识库,但 allow_user_kb_selection=false → 禁用知识库选择 +const isKnowledgeBaseLockedByAgent = computed(() => { + if (!isCustomAgent.value) return false; + // 有预配置知识库时,锁定到这些知识库 + if (agentKnowledgeBases.value.length > 0) return true; + // 没有预配置知识库,但禁止用户选择 + const allowUserKBSelection = selectedAgent.value?.config?.allow_user_kb_selection; + // 默认为 true(允许用户选择),只有明确设置为 false 时才禁用 + return allowUserKBSelection === false; +}); + +// 知识库是否被智能体完全禁用(没有预配置且不允许用户选择) +const isKnowledgeBaseDisabledByAgent = computed(() => { + if (!isCustomAgent.value) return false; + if (agentKnowledgeBases.value.length > 0) return false; // 有预配置的不算禁用 + const allowUserKBSelection = selectedAgent.value?.config?.allow_user_kb_selection; + return allowUserKBSelection === false; +}); + +// 智能体配置的模型 ID +const agentModelId = computed(() => { + if (!isCustomAgent.value) return null; + return selectedAgent.value?.config?.model_id || null; +}); + +// 模型选择是否被智能体锁定 +const isModelLockedByAgent = computed(() => { + return isCustomAgent.value && !!agentModelId.value; +}); + // Mention related state const showMention = ref(false); const mentionQuery = ref(""); @@ -80,21 +157,49 @@ const selectedFiles = computed(() => { }); // 合并所有选中项(用于输入框内显示) + // 包括用户选择的和智能体预配置的知识库 const allSelectedItems = computed(() => { - const kbs = selectedKbs.value.map(kb => ({ - ...kb, - type: 'kb' as const, - kbType: kb.type // 保留原始类型用于显示区分 - })); + // 获取智能体预配置的知识库 IDs + const agentKbIds = agentKnowledgeBases.value; + + // 用户选择的知识库(排除智能体已配置的) + const userSelectedKbs = selectedKbs.value + .filter(kb => !agentKbIds.includes(kb.id)) + .map(kb => ({ + ...kb, + type: 'kb' as const, + kbType: kb.type, + isAgentConfigured: false + })); + + // 智能体预配置的知识库 + const agentConfiguredKbs = knowledgeBases.value + .filter(kb => agentKbIds.includes(kb.id)) + .map(kb => ({ + ...kb, + type: 'kb' as const, + kbType: kb.type, + isAgentConfigured: true // 标记为智能体配置,不可删除 + })); + + // 用户选择的文件 const files = selectedFiles.value.map((f: { id: string; name: string }) => ({ ...f, - type: 'file' as const + type: 'file' as const, + isAgentConfigured: false })); - return [...kbs, ...files]; + + // 智能体配置的放在前面 + return [...agentConfiguredKbs, ...userSelectedKbs, ...files]; }); -// 移除选中项 -const removeSelectedItem = (item: { id: string; type: 'kb' | 'file' }) => { +// 移除选中项(智能体配置的项不可移除) +const removeSelectedItem = (item: { id: string; type: 'kb' | 'file'; isAgentConfigured?: boolean }) => { + // 如果是智能体配置的,不允许删除 + if (item.isAgentConfigured) { + MessagePlugin.warning(t('input.cannotRemoveAgentKb')); + return; + } if (item.type === 'kb') { settingsStore.removeKnowledgeBase(item.id); } else { @@ -123,6 +228,15 @@ const remainingCount = computed(() => Math.max(0, selectedKbs.value.length - 2)) // 根据不同状态组合计算输入框的 placeholder const inputPlaceholder = computed(() => { + // 如果选择了自定义智能体 + if (isCustomAgent.value && selectedAgent.value) { + // 有描述时显示描述,否则显示"向 [名称] 提问" + if (selectedAgent.value.description) { + return selectedAgent.value.description; + } + return t('input.placeholderAgent', { name: selectedAgent.value.name }); + } + const hasKnowledge = allSelectedItems.value.length > 0; const hasWebSearch = isWebSearchEnabled.value && isWebSearchConfigured.value; @@ -211,6 +325,16 @@ const loadWebSearchConfig = async () => { } }; +// 加载智能体列表 +const loadAgents = async () => { + try { + const response = await listAgents(); + agents.value = response.data || []; + } catch (error) { + console.error('Failed to load agents:', error); + } +}; + const loadConversationConfig = async () => { try { const response = await getConversationConfig(); @@ -619,6 +743,13 @@ const onCompositionEnd = (e: CompositionEvent) => { }; const triggerMention = () => { + // 如果智能体锁定或禁用了知识库,不允许打开选择器 + if (isKnowledgeBaseLockedByAgent.value) { + const msgKey = isKnowledgeBaseDisabledByAgent.value ? 'input.kbDisabledByAgent' : 'input.kbLockedByAgent'; + MessagePlugin.warning(t(msgKey)); + return; + } + const textarea = getTextareaEl(); if (!textarea) return; @@ -712,6 +843,12 @@ const removeFile = (id: string) => { }; const toggleModelSelector = () => { + // 如果智能体锁定了模型,不允许打开选择器 + if (isModelLockedByAgent.value) { + MessagePlugin.warning(t('input.modelLockedByAgent')); + return; + } + // 互斥:关闭其他 showMention.value = false; showAgentModeSelector.value = false; @@ -761,6 +898,7 @@ onMounted(() => { loadWebSearchConfig(); loadConversationConfig(); loadChatModels(); + loadAgents(); // 如果从知识库内部进入,自动选中该知识库 const kbId = (route.params as any)?.kbId as string; @@ -931,18 +1069,14 @@ const updateAgentModeDropdownPosition = () => { }; const toggleAgentModeSelector = () => { - // 如果 Agent 未就绪,显示提示 - if (!settingsStore.isAgentReady && !isAgentEnabled.value) { - toggleAgentMode(); - return; - } - // 互斥 showMention.value = false; showModelSelector.value = false; showAgentModeSelector.value = !showAgentModeSelector.value; if (showAgentModeSelector.value) { + // 重新加载智能体列表 + loadAgents(); // 多次更新位置确保准确 nextTick(() => { updateAgentModeDropdownPosition(); @@ -966,11 +1100,51 @@ const selectAgentMode = (mode: 'normal' | 'agent') => { const shouldEnableAgent = mode === 'agent'; if (shouldEnableAgent !== isAgentEnabled.value) { settingsStore.toggleAgent(shouldEnableAgent); + // 同时更新选中的智能体 + settingsStore.selectAgent(shouldEnableAgent ? 'builtin-agent' : 'builtin-normal'); MessagePlugin.success(shouldEnableAgent ? t('input.messages.agentSwitchedOn') : t('input.messages.agentSwitchedOff')); } showAgentModeSelector.value = false; } +// 选择智能体(新版) +const handleSelectAgent = (agent: CustomAgent) => { + selectedAgentId.value = agent.id; + + // 根据智能体类型切换 Agent 模式 + const isAgentType = agent.type === 'agent' || (agent.type === 'custom' && (agent.config?.max_iterations ?? 0) > 1); + + if (isAgentType && !settingsStore.isAgentReady) { + // Agent 模式未就绪,显示提示 + toggleAgentMode(); + return; + } + + settingsStore.toggleAgent(!!isAgentType); + + // 如果是自定义智能体,同步网络搜索状态和模型 + if (!agent.is_builtin && agent.type === 'custom') { + const agentWebSearch = agent.config?.web_search_enabled; + if (agentWebSearch !== undefined) { + // 智能体配置了网络搜索设置,同步到 store + settingsStore.toggleWebSearch(agentWebSearch); + } + + // 如果智能体配置了模型,同步到 store + const agentModel = agent.config?.model_id; + if (agentModel) { + selectedModelId.value = agentModel; + } + } + + showAgentModeSelector.value = false; + + const message = agent.is_builtin + ? (isAgentType ? t('input.messages.agentSwitchedOn') : t('input.messages.agentSwitchedOff')) + : t('input.messages.agentSelected', { name: agent.name }); + MessagePlugin.success(message); +} + const clearvalue = () => { query.value = ""; } @@ -1107,6 +1281,18 @@ const toggleWebSearch = () => { showModelSelector.value = false; showAgentModeSelector.value = false; + // 如果智能体禁用了网络搜索,不允许开启 + if (isWebSearchDisabledByAgent.value) { + MessagePlugin.warning(t('input.webSearchDisabledByAgent')); + return; + } + + // 如果智能体强制启用了网络搜索,不允许关闭 + if (isWebSearchForcedByAgent.value) { + MessagePlugin.warning(t('input.webSearchForcedByAgent')); + return; + } + if (!isWebSearchConfigured.value) { const messageContent = h('div', { style: 'display: flex; flex-direction: column; gap: 6px; max-width: 280px;' }, [ h('span', { style: 'color: #333; line-height: 1.5;' }, t('input.messages.webSearchNotConfigured')), @@ -1182,14 +1368,15 @@ onBeforeRouteUpdate((to, from, next) => {
- +
@@ -1197,7 +1384,8 @@ onBeforeRouteUpdate((to, from, next) => { {{ item.name }} - × + + ×
@@ -1243,28 +1431,8 @@ onBeforeRouteUpdate((to, from, next) => { }" @click.stop="toggleAgentModeSelector" > - - - - - {{ isAgentEnabled ? $t('input.agentMode') : $t('input.normalMode') }} + {{ selectedAgent.name || (isAgentEnabled ? $t('input.agentMode') : $t('input.normalMode')) }} {
- - -
-
-
-
- {{ $t('input.normalMode') }} - {{ $t('input.normalModeDesc') }} -
- - - -
-
-
- {{ $t('input.agentMode') }} - {{ $t('input.agentModeDesc') }} -
- - - -
- - - -
-
- -
-
-
+ +
{ fill="none" xmlns="http://www.w3.org/2000/svg" class="control-icon websearch-icon" - :class="{ 'active': isWebSearchEnabled && isWebSearchConfigured }" + :class="{ 'active': (isWebSearchEnabled && isWebSearchConfigured) || isWebSearchForcedByAgent }" > @@ -1380,11 +1494,14 @@ onBeforeRouteUpdate((to, from, next) => { - +
@@ -1394,27 +1511,29 @@ onBeforeRouteUpdate((to, from, next) => { -
-
- - {{ selectedModel?.name || $t('input.notConfigured') }} - - +
+
- - + + {{ selectedModel?.name || $t('input.notConfigured') }} + + + + +
-
+
@@ -1609,6 +1728,12 @@ const getImgSrc = (url: string) => { background: rgba(0, 0, 0, 0.1); } } + + // 智能体配置的标签样式(用虚线边框区分,不显示锁图标) + &.agent-configured { + border-style: dashed; + opacity: 0.9; + } } :deep(.t-textarea__inner) { @@ -1788,6 +1913,19 @@ const getImgSrc = (url: string) => { background: rgba(16, 185, 129, 0.15); } } + + &.agent-controlled { + cursor: not-allowed; + opacity: 0.85; + + &:hover { + background: var(--td-bg-color-secondarycontainer, #f5f5f5); + } + + &.active:hover { + background: rgba(16, 185, 129, 0.1); + } + } } .kb-count { @@ -1826,6 +1964,7 @@ const getImgSrc = (url: string) => { display: flex; align-items: center; justify-content: center; + position: relative; &.active { background: rgba(16, 185, 129, 0.1); @@ -1852,6 +1991,19 @@ const getImgSrc = (url: string) => { } } } + + &.agent-controlled { + cursor: not-allowed; + opacity: 0.85; + + &:hover { + background: var(--td-bg-color-secondarycontainer, #f5f5f5); + } + + &.active:hover { + background: rgba(16, 185, 129, 0.1); + } + } } :global(.websearch-tooltip-disabled) { @@ -1956,6 +2108,18 @@ const getImgSrc = (url: string) => { align-items: center; margin-left: auto; flex-shrink: 0; + + &.agent-controlled { + .model-selector-trigger { + cursor: not-allowed; + opacity: 0.85; + + &:hover { + background: rgba(16, 185, 129, 0.1); + border-color: rgba(16, 185, 129, 0.3); + } + } + } } .model-selector-trigger { @@ -2040,12 +2204,12 @@ const getImgSrc = (url: string) => { display: flex; align-items: center; justify-content: space-between; - padding: 8px 10px; - border-bottom: 1px solid var(--td-component-border, #f2f4f5); - background: var(--td-bg-color-secondarycontainer, #fafcfc); + padding: 8px 12px; + border-bottom: 1px solid var(--td-component-stroke, #f0f0f0); + background: var(--td-bg-color-container, #fff); font-size: 12px; - font-weight: 600; - color: var(--td-text-color-primary, #222); + font-weight: 500; + color: var(--td-text-color-secondary, #666); } .model-selector-content { @@ -2061,26 +2225,26 @@ const getImgSrc = (url: string) => { display: inline-flex; align-items: center; gap: 4px; - padding: 3px 8px; - border-radius: 6px; - border: 1px solid var(--td-component-border, #e1e5e6); - background: var(--td-bg-color-container, #fff); - color: var(--td-text-color-secondary, #52575a); - font-size: 11px; + padding: 2px 8px; + border-radius: 4px; + border: 1px solid transparent; + background: transparent; + color: var(--td-brand-color, #07c05f); + font-size: 12px; font-weight: 500; cursor: pointer; - transition: all 0.12s; -} - -.model-selector-add .add-icon { - font-size: 12px; - line-height: 1; -} - -.model-selector-add:hover { - border-color: #10b981; - color: #10b981; - background: #f0fdf6; + transition: all 0.2s; + + .add-icon { + font-size: 14px; + line-height: 1; + font-weight: 400; + } + + &:hover { + color: var(--td-brand-color-hover, #05a04f); + background: var(--td-bg-color-secondarycontainer, #f3f3f3); + } } .model-option { diff --git a/frontend/src/components/menu.vue b/frontend/src/components/menu.vue index c6e7ad24..da2c6057 100644 --- a/frontend/src/components/menu.vue +++ b/frontend/src/components/menu.vue @@ -24,6 +24,18 @@
+