feat: 新增智能体功能模块,支持自定义智能体创建、配置和选择

This commit is contained in:
wizardchen
2025-12-24 10:34:41 +08:00
parent 236b190b26
commit d0c5c4f6b0
36 changed files with 5195 additions and 672 deletions
+155
View File
@@ -0,0 +1,155 @@
<template>
<div
class="agent-avatar"
:style="avatarStyle"
:class="{ 'agent-avatar-small': size === 'small', 'agent-avatar-large': size === 'large' }"
>
<!-- 星星装饰 - 融入背景 -->
<svg class="agent-sparkles" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<!-- 右上角小星星 -->
<path d="M24 5L24.4 6.6C24.45 6.85 24.65 7.05 24.9 7.1L26.5 7.5L24.9 7.9C24.65 7.95 24.45 8.15 24.4 8.4L24 10L23.6 8.4C23.55 8.15 23.35 7.95 23.1 7.9L21.5 7.5L23.1 7.1C23.35 7.05 23.55 6.85 23.6 6.6L24 5Z" fill="rgba(255,255,255,0.6)"/>
<!-- 左下角小星星 -->
<path d="M7 22L7.4 23.6C7.45 23.85 7.65 24.05 7.9 24.1L9.5 24.5L7.9 24.9C7.65 24.95 7.45 25.15 7.4 25.4L7 27L6.6 25.4C6.55 25.15 6.35 24.95 6.1 24.9L4.5 24.5L6.1 24.1C6.35 24.05 6.55 23.85 6.6 23.6L7 22Z" fill="rgba(255,255,255,0.5)"/>
</svg>
<span class="agent-avatar-letter" :style="letterStyle">{{ letter }}</span>
</div>
</template>
<script setup lang="ts">
import { computed } from 'vue';
const props = withDefaults(defineProps<{
name: string;
size?: 'small' | 'medium' | 'large';
}>(), {
size: 'medium'
});
// 预定义的渐变色方案 - 现代、柔和、专业
const gradients = [
{ from: '#667eea', to: '#764ba2' }, // 紫蓝渐变
{ from: '#4facfe', to: '#00f2fe' }, // 蓝青渐变
{ from: '#43e97b', to: '#38f9d7' }, // 绿青渐变
{ from: '#11998e', to: '#38ef7d' }, // 深绿渐变
{ from: '#5ee7df', to: '#b490ca' }, // 青紫渐变
{ from: '#48c6ef', to: '#6f86d6' }, // 蓝紫渐变
{ from: '#a8edea', to: '#fed6e3' }, // 青粉渐变(柔和)
{ from: '#667db6', to: '#0082c8' }, // 蓝色渐变
{ from: '#36d1dc', to: '#5b86e5' }, // 青蓝渐变
{ from: '#56ab2f', to: '#a8e063' }, // 草绿渐变
{ from: '#614385', to: '#516395' }, // 深紫蓝渐变
{ from: '#02aab0', to: '#00cdac' }, // 青绿渐变
{ from: '#6a82fb', to: '#fc5c7d' }, // 蓝粉渐变(柔和)
{ from: '#834d9b', to: '#d04ed6' }, // 紫色渐变
{ from: '#4776e6', to: '#8e54e9' }, // 蓝紫渐变
{ from: '#00b09b', to: '#96c93d' }, // 青绿渐变
];
// 根据名称生成一个稳定的哈希值
const hashCode = (str: string): number => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return Math.abs(hash);
};
// 获取首字母(支持中文)
const letter = computed(() => {
const name = props.name?.trim() || '';
if (!name) return '?';
// 获取第一个字符
const firstChar = name.charAt(0);
// 如果是英文字母,转大写
if (/[a-zA-Z]/.test(firstChar)) {
return firstChar.toUpperCase();
}
// 中文或其他字符直接返回
return firstChar;
});
// 根据名称选择渐变色
const gradient = computed(() => {
const hash = hashCode(props.name || '');
return gradients[hash % gradients.length];
});
// 生成样式
const avatarStyle = computed(() => {
const g = gradient.value;
return {
background: `linear-gradient(135deg, ${g.from} 0%, ${g.to} 100%)`
};
});
// 字母样式 - 白色 + 背景色阴影增加层次感
const letterStyle = computed(() => {
const g = gradient.value;
return {
textShadow: `0 1px 2px ${g.to}80, 0 0 8px ${g.from}30`
};
});
</script>
<style scoped lang="less">
.agent-avatar {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border-radius: 8px;
flex-shrink: 0;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
&.agent-avatar-small {
width: 24px;
height: 24px;
border-radius: 6px;
.agent-avatar-letter {
font-size: 12px;
}
}
&.agent-avatar-large {
width: 48px;
height: 48px;
border-radius: 12px;
.agent-avatar-letter {
font-size: 20px;
}
.agent-sparkles {
opacity: 0.9;
}
}
}
.agent-sparkles {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
opacity: 0.85;
}
.agent-avatar-letter {
position: relative;
z-index: 1;
color: #fff;
font-size: 14px;
font-weight: 600;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
</style>
+430
View File
@@ -0,0 +1,430 @@
<template>
<Teleport to="body">
<div v-if="visible" class="agent-selector-overlay" @click="$emit('close')">
<div
class="agent-selector-dropdown"
:style="dropdownStyle"
@click.stop
>
<!-- 头部 -->
<div class="agent-selector-header">
<span>{{ $t('agent.selectAgent') }}</span>
<router-link to="/platform/agents" class="agent-selector-add" @click="$emit('close')">
<span class="add-icon">+</span>
<span class="add-text">{{ $t('agent.manageAgents') }}</span>
</router-link>
</div>
<!-- 内容区域 -->
<div class="agent-selector-content">
<!-- 内置智能体分组 -->
<div class="agent-group">
<div class="agent-group-title">{{ $t('agent.builtinAgents') }}</div>
<t-tooltip
v-for="agent in builtinAgents"
:key="agent.id"
:content="getAgentCapabilities(agent)"
:disabled="currentAgentId === agent.id"
placement="right"
>
<div
class="agent-option"
:class="{ 'selected': currentAgentId === agent.id }"
@click="selectAgent(agent)"
>
<div class="builtin-icon" :class="agent.type">
<TIcon :name="agent.type === 'agent' ? 'control-platform' : 'chat'" size="14px" />
</div>
<span class="agent-option-name">{{ agent.name }}</span>
<svg
v-if="currentAgentId === agent.id"
width="14"
height="14"
viewBox="0 0 16 16"
fill="currentColor"
class="check-icon"
>
<path d="M13.5 4.5L6 12L2.5 8.5L3.5 7.5L6 10L12.5 3.5L13.5 4.5Z"/>
</svg>
</div>
</t-tooltip>
</div>
<!-- 自定义智能体分组 -->
<div v-if="customAgents.length > 0" class="agent-group">
<div class="agent-group-title">{{ $t('agent.customAgents') }}</div>
<t-tooltip
v-for="agent in customAgents"
:key="agent.id"
:content="getAgentCapabilities(agent)"
:disabled="currentAgentId === agent.id"
placement="right"
>
<div
class="agent-option"
:class="{ 'selected': currentAgentId === agent.id }"
@click="selectAgent(agent)"
>
<AgentAvatar :name="agent.name" size="small" />
<span class="agent-option-name">{{ agent.name }}</span>
<svg
v-if="currentAgentId === agent.id"
width="14"
height="14"
viewBox="0 0 16 16"
fill="currentColor"
class="check-icon"
>
<path d="M13.5 4.5L6 12L2.5 8.5L3.5 7.5L6 10L12.5 3.5L13.5 4.5Z"/>
</svg>
</div>
</t-tooltip>
</div>
<!-- 空状态 -->
<div v-if="builtinAgents.length === 0 && customAgents.length === 0" class="agent-option empty">
{{ $t('agent.noAgents') }}
</div>
</div>
</div>
</div>
</Teleport>
</template>
<script setup lang="ts">
import { ref, computed, watch, onMounted, nextTick } from 'vue';
import { useI18n } from 'vue-i18n';
import { Icon as TIcon } from 'tdesign-vue-next';
import { listAgents, type CustomAgent } from '@/api/agent';
import AgentAvatar from '@/components/AgentAvatar.vue';
const { t } = useI18n();
const props = defineProps<{
visible: boolean;
anchorEl?: HTMLElement;
currentAgentId: string;
}>();
const emit = defineEmits<{
(e: 'close'): void;
(e: 'select', agent: CustomAgent): void;
}>();
const agents = ref<CustomAgent[]>([]);
const dropdownStyle = ref<Record<string, string>>({});
// 内置智能体
const builtinAgents = computed(() => {
return [
{
id: 'builtin-normal',
name: t('input.normalMode'),
description: t('input.normalModeDesc'),
type: 'normal',
is_builtin: true
},
{
id: 'builtin-agent',
name: t('input.agentMode'),
description: t('input.agentModeDesc'),
type: 'agent',
is_builtin: true
}
] as CustomAgent[];
});
// 自定义智能体
const customAgents = computed(() => {
return agents.value.filter(a => !a.is_builtin);
});
// 获取智能体能力描述(用于 tooltip)
const getAgentCapabilities = (agent: CustomAgent): string => {
// 内置智能体
if (agent.is_builtin) {
if (agent.type === 'normal') {
return t('agent.capabilities.normal');
} else if (agent.type === 'agent') {
return t('agent.capabilities.agent');
}
return '';
}
// 自定义智能体
const capabilities: string[] = [];
const config = agent.config || {};
if (config.model_id) {
capabilities.push(t('agent.capabilities.modelSpecified'));
}
if (config.knowledge_bases && config.knowledge_bases.length > 0) {
capabilities.push(t('agent.capabilities.kbCount', { count: config.knowledge_bases.length }));
} else if (config.allow_user_kb_selection === false) {
capabilities.push(t('agent.capabilities.kbDisabled'));
}
if (config.rerank_model_id) {
capabilities.push(t('agent.capabilities.rerankSpecified'));
}
if (config.web_search_enabled === true) {
capabilities.push(t('agent.capabilities.webSearchOn'));
} else if (config.web_search_enabled === false) {
capabilities.push(t('agent.capabilities.webSearchOff'));
}
if (config.system_prompt) {
capabilities.push(t('agent.capabilities.hasPrompt'));
}
return capabilities.length > 0 ? capabilities.join('、') : t('agent.capabilities.default');
};
// 加载智能体列表
const loadAgents = async () => {
try {
const response = await listAgents();
agents.value = response.data || [];
} catch (error) {
console.error('Failed to load agents:', error);
}
};
// 选择智能体
const selectAgent = (agent: CustomAgent) => {
emit('select', agent);
};
// 更新下拉框位置(与模型选择器一致)
const updateDropdownPosition = () => {
if (!props.anchorEl) return;
const rect = props.anchorEl.getBoundingClientRect();
const dropdownWidth = 200;
const offsetY = 8;
const vh = window.innerHeight;
const vw = window.innerWidth;
// 水平位置:左对齐
let left = Math.floor(rect.left);
const minLeft = 16;
const maxLeft = Math.max(16, vw - dropdownWidth - 16);
left = Math.max(minLeft, Math.min(maxLeft, left));
// 垂直位置
const preferredDropdownHeight = 320;
const minDropdownHeight = 100;
const topMargin = 20;
const spaceBelow = vh - rect.bottom;
const spaceAbove = rect.top;
let actualHeight: number;
if (spaceBelow >= minDropdownHeight + offsetY) {
// 向下弹出
actualHeight = Math.min(preferredDropdownHeight, spaceBelow - offsetY - 16);
const top = Math.floor(rect.bottom + offsetY);
dropdownStyle.value = {
position: 'fixed',
width: `${dropdownWidth}px`,
left: `${left}px`,
top: `${top}px`,
maxHeight: `${actualHeight}px`,
zIndex: '9999'
};
} else {
// 向上弹出
const availableHeight = spaceAbove - offsetY - topMargin;
actualHeight = availableHeight >= preferredDropdownHeight
? preferredDropdownHeight
: Math.max(minDropdownHeight, availableHeight);
const bottom = vh - rect.top + offsetY;
dropdownStyle.value = {
position: 'fixed',
width: `${dropdownWidth}px`,
left: `${left}px`,
bottom: `${bottom}px`,
maxHeight: `${actualHeight}px`,
zIndex: '9999'
};
}
};
// 监听显示状态
watch(() => props.visible, (newVal) => {
if (newVal) {
loadAgents();
nextTick(() => {
updateDropdownPosition();
});
}
});
onMounted(() => {
if (props.visible) {
loadAgents();
}
});
</script>
<style scoped lang="less">
.agent-selector-overlay {
position: fixed;
inset: 0;
z-index: 9998;
background: transparent;
touch-action: none;
}
.agent-selector-dropdown {
position: fixed;
background: var(--td-bg-color-container, #fff);
border-radius: 10px;
box-shadow: var(--td-shadow-2, 0 6px 28px rgba(15, 23, 42, 0.08));
border: 1px solid var(--td-component-border, #e7e9eb);
overflow: hidden;
display: flex;
flex-direction: column;
}
.agent-selector-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 12px;
border-bottom: 1px solid var(--td-component-stroke, #f0f0f0);
background: var(--td-bg-color-container, #fff);
font-size: 12px;
font-weight: 500;
color: var(--td-text-color-secondary, #666);
}
.agent-selector-add {
display: inline-flex;
align-items: center;
gap: 4px;
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.2s;
text-decoration: none;
.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);
}
}
.agent-selector-content {
flex: 1;
min-height: 0;
overflow-y: auto;
overscroll-behavior: contain;
-webkit-overflow-scrolling: touch;
padding: 6px 8px;
}
.agent-group {
&:not(:last-child) {
margin-bottom: 8px;
padding-bottom: 8px;
border-bottom: 1px solid var(--td-component-stroke, #f0f0f0);
}
}
.agent-group-title {
font-size: 11px;
color: var(--td-text-color-placeholder, #999);
padding: 4px 8px 6px;
font-weight: 500;
}
.agent-option {
display: flex;
align-items: center;
gap: 8px;
padding: 6px 8px;
cursor: pointer;
transition: background 0.12s;
border-radius: 6px;
margin-bottom: 4px;
&:last-child {
margin-bottom: 0;
}
&:hover {
background: var(--td-bg-color-container-hover, #f6f8f7);
}
&.selected {
background: var(--td-brand-color-light, #eefdf5);
.agent-option-name {
color: #10b981;
font-weight: 600;
}
}
&.empty {
color: var(--td-text-color-disabled, #9aa0a6);
cursor: default;
text-align: center;
padding: 20px 8px;
&:hover {
background: transparent;
}
}
}
.agent-option-name {
font-size: 12px;
color: var(--td-text-color-primary, #222);
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
line-height: 1.4;
}
.builtin-icon {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
border-radius: 6px;
flex-shrink: 0;
&.normal {
background: rgba(7, 192, 95, 0.1);
color: #059669;
}
&.agent {
background: rgba(124, 77, 255, 0.1);
color: #7c4dff;
}
}
.check-icon {
width: 14px;
height: 14px;
color: #10b981;
flex-shrink: 0;
margin-left: 6px;
}
</style>
+318 -154
View File
@@ -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<HTMLElement>();
const agentModeDropdownStyle = ref<Record<string, string>>({});
// 智能体相关状态
const agents = ref<CustomAgent[]>([]);
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) => {
<div class="answers-input">
<!-- 富文本输入框容器 -->
<div class="rich-input-container">
<!-- 选中的知识库和文件标签(显示在输入框内顶部) -->
<!-- 选中的知识库和文件标签(显示在输入框内顶部) -->
<div v-if="allSelectedItems.length > 0" class="selected-tags-inline">
<span
v-for="item in allSelectedItems"
:key="item.id"
class="inline-tag"
:class="[
item.type === 'kb' ? (item.kbType === 'faq' ? 'faq-tag' : 'kb-tag') : 'file-tag'
item.type === 'kb' ? (item.kbType === 'faq' ? 'faq-tag' : 'kb-tag') : 'file-tag',
{ 'agent-configured': item.isAgentConfigured }
]"
>
<span class="tag-icon">
@@ -1197,7 +1384,8 @@ onBeforeRouteUpdate((to, from, next) => {
<t-icon v-else name="file" />
</span>
<span class="tag-name">{{ item.name }}</span>
<span class="tag-remove" @click="removeSelectedItem(item)">×</span>
<!-- 智能体配置的项不显示删除按钮 -->
<span v-if="!item.isAgentConfigured" class="tag-remove" @click="removeSelectedItem(item)">×</span>
</span>
</div>
@@ -1243,28 +1431,8 @@ onBeforeRouteUpdate((to, from, next) => {
}"
@click.stop="toggleAgentModeSelector"
>
<img
v-if="isAgentEnabled"
:src="getImgSrc('agent-active.svg')"
:alt="$t('input.agentMode')"
class="control-icon agent-icon"
/>
<svg
v-else
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="control-icon normal-mode-icon"
>
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
</svg>
<span class="agent-mode-text">
{{ isAgentEnabled ? $t('input.agentMode') : $t('input.normalMode') }}
{{ selectedAgent.name || (isAgentEnabled ? $t('input.agentMode') : $t('input.normalMode')) }}
</span>
<svg
width="12"
@@ -1278,79 +1446,21 @@ onBeforeRouteUpdate((to, from, next) => {
</svg>
</div>
<!-- Agent 模式选择下拉菜单 -->
<Teleport to="body">
<div v-if="showAgentModeSelector" class="agent-mode-selector-overlay" @click="closeAgentModeSelector">
<div
class="agent-mode-selector-dropdown"
:style="agentModeDropdownStyle"
@click.stop
>
<div
class="agent-mode-option"
:class="{ 'selected': !isAgentEnabled }"
@click="selectAgentMode('normal')"
>
<div class="agent-mode-option-main">
<span class="agent-mode-option-name">{{ $t('input.normalMode') }}</span>
<span class="agent-mode-option-desc">{{ $t('input.normalModeDesc') }}</span>
</div>
<svg
v-if="!isAgentEnabled"
width="16"
height="16"
viewBox="0 0 16 16"
fill="currentColor"
class="check-icon"
>
<path d="M13.5 4.5L6 12L2.5 8.5L3.5 7.5L6 10L12.5 3.5L13.5 4.5Z"/>
</svg>
</div>
<div
class="agent-mode-option"
:class="{
'selected': isAgentEnabled,
'disabled': !settingsStore.isAgentReady && !isAgentEnabled
}"
@click="selectAgentMode('agent')"
>
<div class="agent-mode-option-main">
<span class="agent-mode-option-name">{{ $t('input.agentMode') }}</span>
<span class="agent-mode-option-desc">{{ $t('input.agentModeDesc') }}</span>
</div>
<svg
v-if="isAgentEnabled"
width="16"
height="16"
viewBox="0 0 16 16"
fill="currentColor"
class="check-icon"
>
<path d="M13.5 4.5L6 12L2.5 8.5L3.5 7.5L6 10L12.5 3.5L13.5 4.5Z"/>
</svg>
<div v-if="!settingsStore.isAgentReady && !isAgentEnabled" class="agent-mode-warning">
<t-tooltip :content="$t('input.agentNotReadyTooltip')" placement="left">
<t-icon name="error-circle" class="warning-icon" />
</t-tooltip>
</div>
</div>
<div v-if="!settingsStore.isAgentReady && !isAgentEnabled" class="agent-mode-footer">
<a
href="#"
@click.prevent="handleGoToAgentSettings"
class="agent-mode-link"
>
{{ $t('input.goToSettings') }}
</a>
</div>
</div>
</div>
</Teleport>
<!-- Agent 选择器下拉菜单 -->
<AgentSelector
:visible="showAgentModeSelector"
:anchorEl="agentModeButtonRef"
:currentAgentId="selectedAgentId"
@close="closeAgentModeSelector"
@select="handleSelectAgent"
/>
<!-- WebSearch 开关按钮 -->
<t-tooltip placement="top">
<template #content>
<span v-if="isWebSearchConfigured">{{ isWebSearchEnabled ? $t('input.webSearch.toggleOff') : $t('input.webSearch.toggleOn') }}</span>
<span v-if="isWebSearchDisabledByAgent">{{ $t('input.webSearchDisabledByAgent') }}</span>
<span v-else-if="isWebSearchForcedByAgent">{{ $t('input.webSearchForcedByAgent') }}</span>
<span v-else-if="isWebSearchConfigured">{{ isWebSearchEnabled ? $t('input.webSearch.toggleOff') : $t('input.webSearch.toggleOn') }}</span>
<div v-else class="websearch-tooltip-disabled">
<span>{{ $t('input.webSearch.notConfigured') }}</span>
<a href="#" @click.prevent="handleGoToWebSearchSettings">{{ $t('input.goToSettings') }}</a>
@@ -1358,7 +1468,11 @@ onBeforeRouteUpdate((to, from, next) => {
</template>
<div
class="control-btn websearch-btn"
:class="{ 'active': isWebSearchEnabled && isWebSearchConfigured, 'disabled': !isWebSearchConfigured }"
:class="{
'active': (isWebSearchEnabled && isWebSearchConfigured) || isWebSearchForcedByAgent,
'disabled': !isWebSearchConfigured,
'agent-controlled': isWebSearchDisabledByAgent || isWebSearchForcedByAgent
}"
@click.stop="toggleWebSearch"
>
<svg
@@ -1368,7 +1482,7 @@ onBeforeRouteUpdate((to, from, next) => {
fill="none"
xmlns="http://www.w3.org/2000/svg"
class="control-icon websearch-icon"
:class="{ 'active': isWebSearchEnabled && isWebSearchConfigured }"
:class="{ 'active': (isWebSearchEnabled && isWebSearchConfigured) || isWebSearchForcedByAgent }"
>
<circle cx="9" cy="9" r="7" stroke="currentColor" stroke-width="1.2" fill="none"/>
<path d="M 9 2 A 3.5 7 0 0 0 9 16" stroke="currentColor" stroke-width="1.2" fill="none"/>
@@ -1380,11 +1494,14 @@ onBeforeRouteUpdate((to, from, next) => {
</t-tooltip>
<!-- @ 知识库/文件选择按钮 -->
<t-tooltip :content="allSelectedItems.length > 0 ? $t('input.knowledgeBaseWithCount', { count: allSelectedItems.length }) : $t('input.knowledgeBase')">
<t-tooltip :content="isKnowledgeBaseDisabledByAgent ? $t('input.kbDisabledByAgent') : (isKnowledgeBaseLockedByAgent ? $t('input.kbLockedByAgent') : (allSelectedItems.length > 0 ? $t('input.knowledgeBaseWithCount', { count: allSelectedItems.length }) : $t('input.knowledgeBase')))">
<div
ref="atButtonRef"
class="control-btn kb-btn"
:class="{ 'active': allSelectedItems.length > 0 }"
:class="{
'active': allSelectedItems.length > 0,
'agent-controlled': isKnowledgeBaseLockedByAgent
}"
@click.stop
@mousedown.prevent="triggerMention"
>
@@ -1394,27 +1511,29 @@ onBeforeRouteUpdate((to, from, next) => {
</t-tooltip>
<!-- 模型显示 -->
<div class="model-display">
<div
ref="modelButtonRef"
class="model-selector-trigger"
@click.stop="toggleModelSelector"
>
<span class="model-selector-name">
{{ selectedModel?.name || $t('input.notConfigured') }}
</span>
<svg
width="12"
height="12"
viewBox="0 0 12 12"
fill="currentColor"
class="model-dropdown-arrow"
:class="{ 'rotate': showModelSelector }"
<t-tooltip :content="isModelLockedByAgent ? $t('input.modelLockedByAgent') : ''" :disabled="!isModelLockedByAgent">
<div class="model-display" :class="{ 'agent-controlled': isModelLockedByAgent }">
<div
ref="modelButtonRef"
class="model-selector-trigger"
@click.stop="toggleModelSelector"
>
<path d="M2.5 4.5L6 8L9.5 4.5H2.5Z"/>
</svg>
<span class="model-selector-name">
{{ selectedModel?.name || $t('input.notConfigured') }}
</span>
<svg
width="12"
height="12"
viewBox="0 0 12 12"
fill="currentColor"
class="model-dropdown-arrow"
:class="{ 'rotate': showModelSelector }"
>
<path d="M2.5 4.5L6 8L9.5 4.5H2.5Z"/>
</svg>
</div>
</div>
</div>
</t-tooltip>
</div>
<Teleport to="body">
@@ -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 {
+38 -4
View File
@@ -24,6 +24,18 @@
</div>
</div>
</template>
<template v-else-if="showCreateAgentAction">
<div class="menu_item kb-action-item" @click.stop="handleCreateAgent">
<div class="kb-action-icon-wrapper">
<svg class="kb-action-icon" width="18" height="18" viewBox="0 0 18 18" fill="none">
<path d="M9 3.75V14.25M3.75 9H14.25" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
<div class="kb-action-content">
<span class="kb-action-title">{{ t('agent.createAgent') }}</span>
</div>
</div>
</template>
<template v-else-if="showDocActions">
<div class="menu_item kb-action-item" @click.stop="handleDocUploadClick">
<div class="kb-action-icon-wrapper">
@@ -138,7 +150,7 @@
:class="['menu_item', item.childrenPath && item.childrenPath == currentpath ? 'menu_item_c_active' : isMenuItemActive(item.path) ? 'menu_item_active' : '']">
<div class="menu_item-box">
<div class="menu_icon">
<img class="icon" :src="getImgSrc(item.icon == 'zhishiku' ? knowledgeIcon : item.icon == 'logout' ? logoutIcon : item.icon == 'setting' ? settingIcon : prefixIcon)" alt="">
<img class="icon" :src="getImgSrc(item.icon == 'zhishiku' ? knowledgeIcon : item.icon == 'agent' ? agentIcon : item.icon == 'logout' ? logoutIcon : item.icon == 'setting' ? settingIcon : prefixIcon)" alt="">
</div>
<span class="menu_title" :title="item.title">{{ item.title }}</span>
<t-icon v-if="item.path === 'creatChat'" name="add" class="menu-create-hint" />
@@ -253,6 +265,9 @@ const isInCreatChat = computed<boolean>(() => {
// 是否在对话详情页
const isInChatDetail = computed<boolean>(() => route.name === 'chat');
// 是否在智能体列表页面
const isInAgentList = computed<boolean>(() => route.name === 'agentList');
// 统一的菜单项激活状态判断
const isMenuItemActive = (itemPath: string): boolean => {
const currentRoute = route.name;
@@ -262,6 +277,8 @@ const isMenuItemActive = (itemPath: string): boolean => {
return currentRoute === 'knowledgeBaseList' ||
currentRoute === 'knowledgeBaseDetail' ||
currentRoute === 'knowledgeBaseSettings';
case 'agents':
return currentRoute === 'agentList';
case 'creatChat':
return currentRoute === 'kbCreatChat' || currentRoute === 'globalCreatChat';
case 'settings':
@@ -290,13 +307,13 @@ const getIconActiveState = (itemPath: string) => {
// 分离上下两部分菜单
const topMenuItems = computed<MenuItem[]>(() => {
return (menuArr.value as unknown as MenuItem[]).filter((item: MenuItem) =>
item.path === 'knowledge-bases' || item.path === 'creatChat'
item.path === 'knowledge-bases' || item.path === 'agents' || item.path === 'creatChat'
);
});
const bottomMenuItems = computed<MenuItem[]>(() => {
return (menuArr.value as unknown as MenuItem[]).filter((item: MenuItem) => {
if (item.path === 'knowledge-bases' || item.path === 'creatChat') {
if (item.path === 'knowledge-bases' || item.path === 'agents' || item.path === 'creatChat') {
return false;
}
return true;
@@ -325,12 +342,14 @@ const showKbActions = computed(() =>
(isInKnowledgeBase.value && !!currentKbInfo.value) ||
isInKnowledgeBaseList.value ||
isInCreatChat.value ||
isInChatDetail.value
isInChatDetail.value ||
isInAgentList.value
)
const currentKbType = computed(() => currentKbInfo.value?.type || 'document')
const showDocActions = computed(() => showKbActions.value && isInKnowledgeBase.value && currentKbType.value !== 'faq')
const showFaqActions = computed(() => showKbActions.value && isInKnowledgeBase.value && currentKbType.value === 'faq')
const showCreateKbAction = computed(() => showKbActions.value && (isInKnowledgeBaseList.value || isInCreatChat.value || isInChatDetail.value))
const showCreateAgentAction = computed(() => showKbActions.value && isInAgentList.value)
// 时间分组函数
const getTimeCategory = (dateStr: string): string => {
@@ -570,16 +589,21 @@ let knowledgeIcon = ref('zhishiku-green.svg');
let prefixIcon = ref('prefixIcon.svg');
let logoutIcon = ref('logout.svg');
let settingIcon = ref('setting.svg'); // 设置图标
let agentIcon = ref('agent.svg'); // 智能体图标
let pathPrefix = ref(route.name)
const getIcon = (path: string) => {
// 根据当前路由状态更新所有图标
const kbActiveState = getIconActiveState('knowledge-bases');
const creatChatActiveState = getIconActiveState('creatChat');
const settingsActiveState = getIconActiveState('settings');
const agentsActiveState = route.name === 'agentList';
// 知识库图标:只在知识库页面显示绿色
knowledgeIcon.value = kbActiveState.isKbActive ? 'zhishiku-green.svg' : 'zhishiku.svg';
// 智能体图标:只在智能体页面显示绿色
agentIcon.value = agentsActiveState ? 'agent-green.svg' : 'agent.svg';
// 对话图标:只在对话创建页面显示绿色,在知识库页面显示灰色,其他情况显示默认
prefixIcon.value = creatChatActiveState.isCreatChatActive ? 'prefixIcon-green.svg' :
kbActiveState.isKbActive ? 'prefixIcon-grey.svg' :
@@ -601,6 +625,9 @@ const handleMenuClick = async (path: string) => {
} else {
router.push('/platform/knowledge-bases')
}
} else if (path === 'agents') {
// 智能体菜单项:跳转到智能体列表
router.push('/platform/agents')
} else if (path === 'settings') {
// 设置菜单项:打开设置弹窗并跳转路由
uiStore.openSettings()
@@ -1238,6 +1265,13 @@ const handleCreateKnowledgeBase = () => {
uiStore.openCreateKB()
}
const handleCreateAgent = () => {
// 触发创建智能体事件,由 AgentList 页面监听处理
window.dispatchEvent(new CustomEvent('openAgentEditor', {
detail: { mode: 'create' }
}))
}
</script>
<style lang="less" scoped>
.aside_box {