feat: 重构提示词模板系统,新增多场景模板配置支持;优化智能体知识库选择模式,支持全部/指定/禁用三种模式
This commit is contained in:
@@ -55,6 +55,12 @@ const agentKnowledgeBases = computed(() => {
|
||||
return selectedAgent.value?.config?.knowledge_bases || [];
|
||||
});
|
||||
|
||||
// 智能体的知识库选择模式
|
||||
const agentKBSelectionMode = computed(() => {
|
||||
if (!isCustomAgent.value) return null; // null 表示不受智能体控制
|
||||
return selectedAgent.value?.config?.kb_selection_mode || 'none';
|
||||
});
|
||||
|
||||
// 智能体是否启用了网络搜索
|
||||
const agentWebSearchEnabled = computed(() => {
|
||||
if (!isCustomAgent.value) return null; // null 表示不受智能体控制
|
||||
@@ -72,24 +78,22 @@ const isWebSearchForcedByAgent = computed(() => {
|
||||
});
|
||||
|
||||
// 知识库选择是否被智能体锁定
|
||||
// 1. 如果智能体配置了知识库 → 锁定到这些知识库
|
||||
// 2. 如果智能体没配置知识库,但 allow_user_kb_selection=false → 禁用知识库选择
|
||||
// 1. 如果智能体配置了 kb_selection_mode = 'none' → 完全禁用知识库
|
||||
// 2. 如果智能体配置了 kb_selection_mode = 'selected' 且 allow_user_kb_selection=false → 锁定到指定知识库
|
||||
// 3. 如果智能体配置了 kb_selection_mode = 'all' 且 allow_user_kb_selection=false → 锁定到全部知识库(但不允许用户选择)
|
||||
const isKnowledgeBaseLockedByAgent = computed(() => {
|
||||
if (!isCustomAgent.value) return false;
|
||||
// 有预配置知识库时,锁定到这些知识库
|
||||
if (agentKnowledgeBases.value.length > 0) return true;
|
||||
// 没有预配置知识库,但禁止用户选择
|
||||
// 如果禁用了知识库,返回 true(锁定状态)
|
||||
if (agentKBSelectionMode.value === 'none') return true;
|
||||
// 如果不允许用户选择知识库,返回 true
|
||||
const allowUserKBSelection = selectedAgent.value?.config?.allow_user_kb_selection;
|
||||
// 默认为 true(允许用户选择),只有明确设置为 false 时才禁用
|
||||
return allowUserKBSelection === false;
|
||||
});
|
||||
|
||||
// 知识库是否被智能体完全禁用(没有预配置且不允许用户选择)
|
||||
// 知识库是否被智能体完全禁用(kb_selection_mode = 'none')
|
||||
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;
|
||||
return agentKBSelectionMode.value === 'none';
|
||||
});
|
||||
|
||||
// 智能体配置的模型 ID
|
||||
@@ -556,10 +560,28 @@ const loadMentionItems = async (q: string, resetIndex = true, append = false) =>
|
||||
mentionOffset.value = 0;
|
||||
}
|
||||
|
||||
// Fetch KBs (always show, filter by query) - only on first load
|
||||
// 根据智能体的 kb_selection_mode 过滤知识库
|
||||
let kbItems: any[] = [];
|
||||
if (!append) {
|
||||
const kbs = knowledgeBases.value.filter(kb =>
|
||||
// 获取可选的知识库列表
|
||||
let availableKbs = knowledgeBases.value;
|
||||
|
||||
// 如果是自定义智能体,根据 kb_selection_mode 过滤
|
||||
if (isCustomAgent.value) {
|
||||
const kbMode = agentKBSelectionMode.value;
|
||||
if (kbMode === 'none') {
|
||||
// 不使用知识库,不显示任何知识库
|
||||
availableKbs = [];
|
||||
} else if (kbMode === 'selected') {
|
||||
// 仅显示智能体配置的知识库
|
||||
const configuredKbIds = agentKnowledgeBases.value;
|
||||
availableKbs = knowledgeBases.value.filter(kb => configuredKbIds.includes(kb.id));
|
||||
}
|
||||
// kbMode === 'all' 时显示全部知识库
|
||||
}
|
||||
|
||||
// 按查询过滤
|
||||
const kbs = availableKbs.filter(kb =>
|
||||
!q || kb.name.toLowerCase().includes(q.toLowerCase())
|
||||
);
|
||||
kbItems = kbs.map(kb => ({
|
||||
@@ -572,26 +594,41 @@ const loadMentionItems = async (q: string, resetIndex = true, append = false) =>
|
||||
}
|
||||
|
||||
// Fetch Files from API
|
||||
// 如果智能体禁用了知识库,也不显示文件
|
||||
let fileItems: any[] = [];
|
||||
mentionLoading.value = true;
|
||||
try {
|
||||
const res: any = await searchKnowledge(q || '', mentionOffset.value, MENTION_PAGE_SIZE);
|
||||
console.log('[Mention] searchKnowledge response:', res);
|
||||
if (res.data && Array.isArray(res.data)) {
|
||||
fileItems = res.data.map((f: any) => ({
|
||||
id: f.id,
|
||||
name: f.title || f.file_name,
|
||||
type: 'file' as const,
|
||||
kbName: f.knowledge_base_name || ''
|
||||
}));
|
||||
const shouldLoadFiles = !isCustomAgent.value || agentKBSelectionMode.value !== 'none';
|
||||
|
||||
if (shouldLoadFiles) {
|
||||
mentionLoading.value = true;
|
||||
try {
|
||||
const res: any = await searchKnowledge(q || '', mentionOffset.value, MENTION_PAGE_SIZE);
|
||||
console.log('[Mention] searchKnowledge response:', res);
|
||||
if (res.data && Array.isArray(res.data)) {
|
||||
let files = res.data;
|
||||
|
||||
// 如果是自定义智能体且 kb_selection_mode === 'selected',只显示指定知识库中的文件
|
||||
if (isCustomAgent.value && agentKBSelectionMode.value === 'selected') {
|
||||
const configuredKbIds = agentKnowledgeBases.value;
|
||||
files = files.filter((f: any) => configuredKbIds.includes(f.knowledge_base_id));
|
||||
}
|
||||
|
||||
fileItems = files.map((f: any) => ({
|
||||
id: f.id,
|
||||
name: f.title || f.file_name,
|
||||
type: 'file' as const,
|
||||
kbName: f.knowledge_base_name || ''
|
||||
}));
|
||||
}
|
||||
mentionHasMore.value = res.has_more || false;
|
||||
mentionOffset.value += fileItems.length;
|
||||
} catch (e) {
|
||||
console.error('[Mention] searchKnowledge error:', e);
|
||||
mentionHasMore.value = false;
|
||||
} finally {
|
||||
mentionLoading.value = false;
|
||||
}
|
||||
mentionHasMore.value = res.has_more || false;
|
||||
mentionOffset.value += fileItems.length;
|
||||
} catch (e) {
|
||||
console.error('[Mention] searchKnowledge error:', e);
|
||||
} else {
|
||||
mentionHasMore.value = false;
|
||||
} finally {
|
||||
mentionLoading.value = false;
|
||||
}
|
||||
|
||||
if (append) {
|
||||
@@ -680,6 +717,15 @@ const onInput = (val: string | InputEvent) => {
|
||||
}
|
||||
} else {
|
||||
if (textBeforeCursor.endsWith('@')) {
|
||||
// 如果智能体禁用了知识库,不触发 @ 菜单
|
||||
if (isKnowledgeBaseDisabledByAgent.value) {
|
||||
return;
|
||||
}
|
||||
// 如果智能体锁定了知识库且不允许用户选择,也不触发 @ 菜单
|
||||
if (isKnowledgeBaseLockedByAgent.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[Mention] @ detected, opening menu');
|
||||
isMentionTriggeredByButton.value = false;
|
||||
mentionStartPos.value = cursor - 1;
|
||||
|
||||
@@ -0,0 +1,261 @@
|
||||
<template>
|
||||
<div class="prompt-template-selector" :class="{ 'position-corner': position === 'corner' }">
|
||||
<t-popup
|
||||
trigger="click"
|
||||
placement="top-right"
|
||||
:visible="popupVisible"
|
||||
@visible-change="handleVisibleChange"
|
||||
>
|
||||
<template #content>
|
||||
<div class="template-popup">
|
||||
<div class="template-header">
|
||||
<span class="template-title">{{ $t('promptTemplate.selectTemplate') }}</span>
|
||||
</div>
|
||||
<div v-if="loading" class="template-loading">
|
||||
<t-loading size="small" />
|
||||
</div>
|
||||
<div v-else-if="templates.length === 0" class="template-empty">
|
||||
{{ $t('promptTemplate.noTemplates') || '暂无模板' }}
|
||||
</div>
|
||||
<div v-else class="template-list">
|
||||
<div
|
||||
v-for="template in templates"
|
||||
:key="template.id"
|
||||
class="template-item"
|
||||
@click="selectTemplate(template)"
|
||||
>
|
||||
<div class="template-item-header">
|
||||
<span class="template-name">{{ template.name }}</span>
|
||||
<span v-if="template.has_knowledge_base" class="template-tag kb-tag">
|
||||
<t-icon name="folder" size="12px" />
|
||||
{{ $t('promptTemplate.withKnowledgeBase') }}
|
||||
</span>
|
||||
<span v-if="template.has_web_search" class="template-tag web-tag">
|
||||
<t-icon name="internet" size="12px" />
|
||||
{{ $t('promptTemplate.withWebSearch') }}
|
||||
</span>
|
||||
</div>
|
||||
<p class="template-desc">{{ template.description }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<t-button
|
||||
variant="outline"
|
||||
size="small"
|
||||
class="template-trigger-btn"
|
||||
:loading="loading"
|
||||
>
|
||||
<t-icon name="view-module" />
|
||||
<span>{{ $t('promptTemplate.useTemplate') }}</span>
|
||||
</t-button>
|
||||
</t-popup>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted } from 'vue';
|
||||
import { getPromptTemplates, type PromptTemplate, type PromptTemplatesConfig } from '@/api/system';
|
||||
|
||||
const props = defineProps<{
|
||||
type: 'systemPrompt' | 'contextTemplate' | 'rewriteSystem' | 'rewriteUser' | 'fallback';
|
||||
hasKnowledgeBase?: boolean;
|
||||
position?: 'inline' | 'corner'; // inline: 行内显示, corner: 输入框右下角
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'select', content: string): void;
|
||||
}>();
|
||||
|
||||
const popupVisible = ref(false);
|
||||
const loading = ref(false);
|
||||
const templatesConfig = ref<PromptTemplatesConfig | null>(null);
|
||||
|
||||
const handleVisibleChange = async (visible: boolean) => {
|
||||
popupVisible.value = visible;
|
||||
// 首次打开时加载模板
|
||||
if (visible && !templatesConfig.value) {
|
||||
await loadTemplates();
|
||||
}
|
||||
};
|
||||
|
||||
const loadTemplates = async () => {
|
||||
if (loading.value) return;
|
||||
loading.value = true;
|
||||
try {
|
||||
const response = await getPromptTemplates();
|
||||
templatesConfig.value = response.data;
|
||||
} catch (error) {
|
||||
console.error('Failed to load prompt templates:', error);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 根据类型获取对应的模板列表
|
||||
const templates = computed<PromptTemplate[]>(() => {
|
||||
if (!templatesConfig.value) return [];
|
||||
|
||||
switch (props.type) {
|
||||
case 'systemPrompt':
|
||||
return templatesConfig.value.system_prompt || [];
|
||||
case 'contextTemplate':
|
||||
return templatesConfig.value.context_template || [];
|
||||
case 'rewriteSystem':
|
||||
return templatesConfig.value.rewrite_system || [];
|
||||
case 'rewriteUser':
|
||||
return templatesConfig.value.rewrite_user || [];
|
||||
case 'fallback':
|
||||
return templatesConfig.value.fallback || [];
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
const selectTemplate = (template: PromptTemplate) => {
|
||||
emit('select', template.content);
|
||||
popupVisible.value = false;
|
||||
};
|
||||
|
||||
// 预加载模板(可选)
|
||||
onMounted(() => {
|
||||
// 可以在这里预加载,也可以等用户点击时再加载
|
||||
// loadTemplates();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.prompt-template-selector {
|
||||
display: inline-flex;
|
||||
|
||||
&.position-corner {
|
||||
position: absolute;
|
||||
right: 8px;
|
||||
bottom: 8px;
|
||||
z-index: 10;
|
||||
}
|
||||
}
|
||||
|
||||
.template-trigger-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
color: #666;
|
||||
border-color: #d9d9d9;
|
||||
font-size: 12px;
|
||||
height: 26px;
|
||||
padding: 0 8px;
|
||||
background: rgba(255, 255, 255, 0.95);
|
||||
|
||||
&:hover {
|
||||
color: #07c05f;
|
||||
border-color: #07c05f;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
:deep(.t-button__text) {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
:deep(.t-icon) {
|
||||
vertical-align: middle;
|
||||
line-height: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.template-popup {
|
||||
width: 420px;
|
||||
max-height: 400px;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.template-header {
|
||||
padding: 12px 16px;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.template-title {
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.template-loading,
|
||||
.template-empty {
|
||||
padding: 40px 16px;
|
||||
text-align: center;
|
||||
color: #999;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.template-list {
|
||||
overflow-y: auto;
|
||||
padding: 8px;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.template-item {
|
||||
padding: 12px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
margin-bottom: 4px;
|
||||
|
||||
&:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
background: #f5f7fa;
|
||||
}
|
||||
}
|
||||
|
||||
.template-item-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-bottom: 6px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.template-name {
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.template-tag {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 3px;
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
font-size: 11px;
|
||||
|
||||
&.kb-tag {
|
||||
background: #e6f7ff;
|
||||
color: #1890ff;
|
||||
}
|
||||
|
||||
&.web-tag {
|
||||
background: #f0faf5;
|
||||
color: #07c05f;
|
||||
}
|
||||
}
|
||||
|
||||
.template-desc {
|
||||
font-size: 12px;
|
||||
color: #666;
|
||||
margin: 0;
|
||||
line-height: 1.5;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user