feat: Add agent configuration and cleanup scripts for database migrations
This commit is contained in:
@@ -1,27 +1,79 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, defineEmits, onMounted, defineProps, defineExpose } from "vue";
|
||||
import useKnowledgeBase from '@/hooks/useKnowledgeBase';
|
||||
import { ref, defineEmits, onMounted, defineProps, computed, watch, nextTick } from "vue";
|
||||
import { useRoute } from 'vue-router';
|
||||
import { onBeforeRouteUpdate } from 'vue-router';
|
||||
import { MessagePlugin } from "tdesign-vue-next";
|
||||
let { cardList, total, getKnowled } = useKnowledgeBase()
|
||||
import { useSettingsStore } from '@/stores/settings';
|
||||
import { listKnowledgeBases } from '@/api/knowledge-base';
|
||||
import KnowledgeBaseSelector from './KnowledgeBaseSelector.vue';
|
||||
|
||||
const route = useRoute();
|
||||
const settingsStore = useSettingsStore();
|
||||
let query = ref("");
|
||||
const showKbSelector = ref(false);
|
||||
const atButtonRef = ref<HTMLElement>();
|
||||
|
||||
const props = defineProps({
|
||||
isReplying: {
|
||||
type: Boolean,
|
||||
required: false
|
||||
}
|
||||
});
|
||||
|
||||
const isAgentEnabled = computed(() => settingsStore.isAgentEnabled);
|
||||
const selectedKbIds = computed(() => settingsStore.settings.selectedKnowledgeBases || []);
|
||||
|
||||
// 获取已选择的知识库信息
|
||||
const knowledgeBases = ref<Array<{ id: string; name: string }>>([]);
|
||||
const selectedKbs = computed(() => {
|
||||
return knowledgeBases.value.filter(kb => selectedKbIds.value.includes(kb.id));
|
||||
});
|
||||
|
||||
// 显示的知识库标签(最多显示2个)
|
||||
const displayedKbs = computed(() => selectedKbs.value.slice(0, 2));
|
||||
const remainingCount = computed(() => Math.max(0, selectedKbs.value.length - 2));
|
||||
|
||||
// 加载知识库列表
|
||||
const loadKnowledgeBases = async () => {
|
||||
try {
|
||||
const response: any = await listKnowledgeBases();
|
||||
if (response.data && Array.isArray(response.data)) {
|
||||
knowledgeBases.value = response.data.filter((kb: any) =>
|
||||
kb.embedding_model_id && kb.embedding_model_id !== '' &&
|
||||
kb.summary_model_id && kb.summary_model_id !== ''
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load knowledge bases:', error);
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
getKnowled()
|
||||
})
|
||||
loadKnowledgeBases();
|
||||
|
||||
// 如果从知识库内部进入,自动选中该知识库
|
||||
const kbId = (route.params as any)?.kbId as string;
|
||||
if (kbId && !selectedKbIds.value.includes(kbId)) {
|
||||
settingsStore.addKnowledgeBase(kbId);
|
||||
}
|
||||
});
|
||||
|
||||
// 监听路由变化
|
||||
watch(() => route.params.kbId, (newKbId) => {
|
||||
if (newKbId && typeof newKbId === 'string' && !selectedKbIds.value.includes(newKbId)) {
|
||||
settingsStore.addKnowledgeBase(newKbId);
|
||||
}
|
||||
});
|
||||
|
||||
const emit = defineEmits(['send-msg']);
|
||||
|
||||
const createSession = (val: string) => {
|
||||
if (!val.trim()) {
|
||||
MessagePlugin.info("请先输入内容!");
|
||||
return
|
||||
return;
|
||||
}
|
||||
if (!query.value && cardList.value.length == 0) {
|
||||
MessagePlugin.info("请先上传知识库!");
|
||||
if (selectedKbIds.value.length === 0) {
|
||||
MessagePlugin.warning("请先选择知识库!");
|
||||
return;
|
||||
}
|
||||
if (props.isReplying) {
|
||||
@@ -30,9 +82,11 @@ const createSession = (val: string) => {
|
||||
emit('send-msg', val);
|
||||
clearvalue();
|
||||
}
|
||||
|
||||
const clearvalue = () => {
|
||||
query.value = "";
|
||||
}
|
||||
|
||||
const onKeydown = (val: string, event: { e: { preventDefault(): unknown; keyCode: number; shiftKey: any; ctrlKey: any; }; }) => {
|
||||
if ((event.e.keyCode == 13 && event.e.shiftKey) || (event.e.keyCode == 13 && event.e.ctrlKey)) {
|
||||
return;
|
||||
@@ -42,6 +96,21 @@ const onKeydown = (val: string, event: { e: { preventDefault(): unknown; keyCode
|
||||
createSession(val)
|
||||
}
|
||||
}
|
||||
|
||||
const toggleAgentMode = () => {
|
||||
settingsStore.toggleAgent(!isAgentEnabled.value);
|
||||
const message = isAgentEnabled.value ? 'Agent 模式已启用' : 'Agent 模式已禁用';
|
||||
MessagePlugin.success(message);
|
||||
}
|
||||
|
||||
const toggleKbSelector = () => {
|
||||
showKbSelector.value = !showKbSelector.value;
|
||||
}
|
||||
|
||||
const removeKb = (kbId: string) => {
|
||||
settingsStore.removeKnowledgeBase(kbId);
|
||||
}
|
||||
|
||||
onBeforeRouteUpdate((to, from, next) => {
|
||||
clearvalue()
|
||||
next()
|
||||
@@ -51,15 +120,90 @@ onBeforeRouteUpdate((to, from, next) => {
|
||||
<template>
|
||||
<div class="answers-input">
|
||||
<t-textarea v-model="query" placeholder="基于知识库提问" name="description" :autosize="true" @keydown="onKeydown" />
|
||||
<div class="answers-input-source">
|
||||
<span>{{ total }}个来源</span>
|
||||
</div>
|
||||
<div @click="createSession(query)" class="answers-input-send"
|
||||
:class="[query.length && total ? '' : 'grey-out']">
|
||||
<img src="../assets/img/sending-aircraft.svg" alt="">
|
||||
|
||||
<!-- 控制栏 -->
|
||||
<div class="control-bar">
|
||||
<!-- 左侧控制按钮 -->
|
||||
<div class="control-left">
|
||||
<!-- Agent 模式按钮 -->
|
||||
<t-tooltip :content="isAgentEnabled ? 'Agent 模式(已启用)' : 'Agent 模式(已禁用)'" placement="top">
|
||||
<div
|
||||
class="control-btn agent-btn"
|
||||
:class="{ 'active': isAgentEnabled }"
|
||||
@click="toggleAgentMode"
|
||||
>
|
||||
<img
|
||||
:src="isAgentEnabled ? getImgSrc('agent-active.svg') : getImgSrc('agent.svg')"
|
||||
alt="Agent"
|
||||
class="control-icon"
|
||||
/>
|
||||
</div>
|
||||
</t-tooltip>
|
||||
|
||||
<!-- @ 知识库选择按钮 -->
|
||||
<div
|
||||
ref="atButtonRef"
|
||||
class="control-btn kb-btn"
|
||||
:class="{ 'active': selectedKbIds.length > 0 }"
|
||||
@click="toggleKbSelector"
|
||||
>
|
||||
<img :src="getImgSrc('at-icon.svg')" alt="@" class="control-icon" />
|
||||
<span class="kb-btn-text">
|
||||
{{ selectedKbIds.length > 0 ? `知识库(${selectedKbIds.length})` : '知识库' }}
|
||||
</span>
|
||||
<svg
|
||||
width="12"
|
||||
height="12"
|
||||
viewBox="0 0 12 12"
|
||||
fill="currentColor"
|
||||
class="dropdown-arrow"
|
||||
:class="{ 'rotate': showKbSelector }"
|
||||
>
|
||||
<path d="M2.5 4.5L6 8L9.5 4.5H2.5Z"/>
|
||||
</svg>
|
||||
</div>
|
||||
|
||||
<!-- 已选择的知识库标签 -->
|
||||
<div v-if="displayedKbs.length > 0" class="kb-tags">
|
||||
<div
|
||||
v-for="kb in displayedKbs"
|
||||
:key="kb.id"
|
||||
class="kb-tag"
|
||||
>
|
||||
<span class="kb-tag-text">{{ kb.name }}</span>
|
||||
<span class="kb-tag-remove" @click.stop="removeKb(kb.id)">×</span>
|
||||
</div>
|
||||
<div v-if="remainingCount > 0" class="kb-tag more-tag">
|
||||
+{{ remainingCount }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 右侧发送按钮 -->
|
||||
<div
|
||||
@click="createSession(query)"
|
||||
class="control-btn send-btn"
|
||||
:class="{ 'disabled': !query.length || selectedKbIds.length === 0 }"
|
||||
>
|
||||
<img src="../assets/img/sending-aircraft.svg" alt="发送" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 知识库选择下拉(使用 Teleport 传送到 body,避免父容器定位影响) -->
|
||||
<Teleport to="body">
|
||||
<KnowledgeBaseSelector
|
||||
v-model:visible="showKbSelector"
|
||||
:anchorEl="atButtonRef"
|
||||
@close="showKbSelector = false"
|
||||
/>
|
||||
</Teleport>
|
||||
</div>
|
||||
</template>
|
||||
<script lang="ts">
|
||||
const getImgSrc = (url: string) => {
|
||||
return new URL(`/src/assets/img/${url}`, import.meta.url).href;
|
||||
}
|
||||
</script>
|
||||
<style scoped lang="less">
|
||||
.answers-input {
|
||||
position: absolute;
|
||||
@@ -79,9 +223,8 @@ onBeforeRouteUpdate((to, from, next) => {
|
||||
font-size: 16px;
|
||||
font-weight: 400;
|
||||
line-height: 24px;
|
||||
color: #000000e6;
|
||||
font-family: "PingFang SC";
|
||||
padding: 16px 12px 12px 16px;
|
||||
padding: 16px 12px 52px 16px; /* 增加底部padding为控制栏腾出空间 */
|
||||
border-radius: 12px;
|
||||
border: 1px solid #E7E7E7;
|
||||
box-sizing: border-box;
|
||||
@@ -92,7 +235,7 @@ onBeforeRouteUpdate((to, from, next) => {
|
||||
border: 1px solid #07C05F;
|
||||
}
|
||||
|
||||
&:placeholder {
|
||||
&::placeholder {
|
||||
color: #00000066;
|
||||
font-family: "PingFang SC";
|
||||
font-size: 16px;
|
||||
@@ -101,35 +244,193 @@ onBeforeRouteUpdate((to, from, next) => {
|
||||
}
|
||||
}
|
||||
|
||||
.answers-input-send {
|
||||
background-color: #07C05F;
|
||||
height: 36px;
|
||||
width: 36px;
|
||||
/* 控制栏 */
|
||||
.control-bar {
|
||||
position: absolute;
|
||||
bottom: 12px;
|
||||
left: 16px;
|
||||
right: 16px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
position: absolute;
|
||||
bottom: 12px;
|
||||
right: 12px;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.control-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.control-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 4px;
|
||||
padding: 6px 10px;
|
||||
border-radius: 6px;
|
||||
background: #f5f5f5;
|
||||
cursor: pointer;
|
||||
border-radius: 8px;
|
||||
transition: all 0.2s ease;
|
||||
user-select: none;
|
||||
flex-shrink: 0;
|
||||
|
||||
&:hover {
|
||||
background: #e6e6e6;
|
||||
transform: scale(1.02);
|
||||
}
|
||||
|
||||
&:active {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
|
||||
&.disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
|
||||
&:hover {
|
||||
background: #f5f5f5;
|
||||
transform: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.answers-input-source {
|
||||
position: absolute;
|
||||
bottom: 12px;
|
||||
right: 64px;
|
||||
line-height: 32px;
|
||||
color: #00000066;
|
||||
font-family: "PingFang SC";
|
||||
.agent-btn {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
padding: 0;
|
||||
|
||||
&.active {
|
||||
background: rgba(7, 192, 95, 0.1);
|
||||
|
||||
&:hover {
|
||||
background: rgba(7, 192, 95, 0.15);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.control-icon {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.kb-btn {
|
||||
height: 28px;
|
||||
padding: 0 10px;
|
||||
min-width: auto;
|
||||
|
||||
&.active {
|
||||
background: rgba(7, 192, 95, 0.1);
|
||||
color: #07C05F;
|
||||
|
||||
&:hover {
|
||||
background: rgba(7, 192, 95, 0.15);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.kb-btn-text {
|
||||
font-size: 13px;
|
||||
color: #666;
|
||||
font-weight: 500;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.kb-btn.active .kb-btn-text {
|
||||
color: #07C05F;
|
||||
}
|
||||
|
||||
.dropdown-arrow {
|
||||
transition: transform 0.2s ease;
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
margin-left: 2px;
|
||||
|
||||
&.rotate {
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
}
|
||||
|
||||
.kb-tags {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
flex: 1;
|
||||
overflow-x: auto;
|
||||
scrollbar-width: none;
|
||||
|
||||
&::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.kb-tag {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
padding: 4px 8px;
|
||||
background: rgba(7, 192, 95, 0.1);
|
||||
border: 1px solid rgba(7, 192, 95, 0.3);
|
||||
border-radius: 4px;
|
||||
font-size: 12px;
|
||||
font-weight: 400;
|
||||
color: #07C05F;
|
||||
white-space: nowrap;
|
||||
transition: all 0.15s ease;
|
||||
|
||||
&:hover {
|
||||
background: rgba(7, 192, 95, 0.15);
|
||||
}
|
||||
}
|
||||
|
||||
.grey-out {
|
||||
background-color: #b5eccf !important;
|
||||
cursor: no-drop !important;
|
||||
.kb-tag-text {
|
||||
max-width: 100px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.kb-tag-remove {
|
||||
cursor: pointer;
|
||||
font-weight: bold;
|
||||
font-size: 16px;
|
||||
line-height: 1;
|
||||
opacity: 0.7;
|
||||
|
||||
&:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.more-tag {
|
||||
background: rgba(0, 0, 0, 0.05);
|
||||
border-color: rgba(0, 0, 0, 0.1);
|
||||
color: #666;
|
||||
|
||||
&:hover {
|
||||
background: rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
}
|
||||
|
||||
.send-btn {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
padding: 0;
|
||||
background-color: #07C05F;
|
||||
|
||||
&:hover:not(.disabled) {
|
||||
background-color: #00a651;
|
||||
transform: scale(1.05);
|
||||
}
|
||||
|
||||
&.disabled {
|
||||
background-color: #b5eccf;
|
||||
}
|
||||
|
||||
img {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,371 @@
|
||||
<template>
|
||||
<div v-if="visible" class="kb-overlay" @click="close">
|
||||
<div class="kb-dropdown" @click.stop :style="dropdownStyle">
|
||||
<!-- 搜索 -->
|
||||
<div class="kb-search">
|
||||
<input
|
||||
ref="searchInput"
|
||||
v-model="searchQuery"
|
||||
type="text"
|
||||
placeholder="搜索知识库..."
|
||||
class="kb-search-input"
|
||||
@keydown.down.prevent="moveSelection(1)"
|
||||
@keydown.up.prevent="moveSelection(-1)"
|
||||
@keydown.enter.prevent="toggleSelection"
|
||||
@keydown.esc="close"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- 列表 -->
|
||||
<div class="kb-list" ref="kbList">
|
||||
<div
|
||||
v-for="(kb, index) in filteredKnowledgeBases"
|
||||
:key="kb.id"
|
||||
:class="['kb-item', { selected: isSelected(kb.id), highlighted: highlightedIndex === index }]"
|
||||
@click="toggleKb(kb.id)"
|
||||
@mouseenter="highlightedIndex = index"
|
||||
>
|
||||
<div class="kb-item-left">
|
||||
<div class="checkbox" :class="{ checked: isSelected(kb.id) }">
|
||||
<svg v-if="isSelected(kb.id)" width="12" height="12" viewBox="0 0 12 12" fill="none">
|
||||
<path d="M10 3L4.5 8.5L2 6" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</svg>
|
||||
</div>
|
||||
<div class="kb-name-wrap">
|
||||
<span class="kb-name">{{ kb.name }}</span>
|
||||
<span class="kb-docs" v-if="kb.docsCount !== undefined">{{ kb.docsCount }} docs</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="filteredKnowledgeBases.length === 0" class="kb-empty">
|
||||
{{ searchQuery ? '未找到匹配的知识库' : '暂无可用知识库' }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 底部操作 -->
|
||||
<div class="kb-actions">
|
||||
<button @click="selectAll" class="kb-btn">全选</button>
|
||||
<button @click="clearAll" class="kb-btn">清空</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, watch, nextTick } from 'vue'
|
||||
import { useSettingsStore } from '@/stores/settings'
|
||||
import { listKnowledgeBases } from '@/api/knowledge-base'
|
||||
|
||||
interface KnowledgeBase {
|
||||
id: string
|
||||
name: string
|
||||
docsCount?: number
|
||||
embedding_model_id?: string
|
||||
summary_model_id?: string
|
||||
}
|
||||
|
||||
const props = defineProps<{
|
||||
visible: boolean
|
||||
anchorEl?: any | null // 支持 DOM 节点、ref、组件实例
|
||||
dropdownWidth?: number
|
||||
offsetY?: number
|
||||
}>()
|
||||
|
||||
const emit = defineEmits(['close', 'update:visible'])
|
||||
|
||||
const settingsStore = useSettingsStore()
|
||||
|
||||
// 本地状态
|
||||
const searchQuery = ref('')
|
||||
const highlightedIndex = ref(0)
|
||||
const knowledgeBases = ref<KnowledgeBase[]>([])
|
||||
const searchInput = ref<HTMLInputElement | null>(null)
|
||||
const kbList = ref<HTMLElement | null>(null)
|
||||
const dropdownStyle = ref<Record<string, string>>({})
|
||||
|
||||
// props 默认
|
||||
const dropdownWidth = props.dropdownWidth ?? 300
|
||||
const offsetY = props.offsetY ?? 8
|
||||
|
||||
// 过滤:只显示已初始化(有 embedding & summary)的
|
||||
const filteredKnowledgeBases = computed(() => {
|
||||
const valid = knowledgeBases.value.filter(
|
||||
k => k.embedding_model_id && k.summary_model_id
|
||||
)
|
||||
if (!searchQuery.value) return valid
|
||||
const q = searchQuery.value.toLowerCase()
|
||||
return valid.filter(k => k.name.toLowerCase().includes(q))
|
||||
})
|
||||
|
||||
const selectedKbIds = computed(() => settingsStore.settings.selectedKnowledgeBases || [])
|
||||
|
||||
// helper: 从 props.anchorEl 获取真实 DOM 元素(支持多种传入形式)
|
||||
const resolveAnchorEl = () => {
|
||||
const a = props.anchorEl
|
||||
if (!a) return null
|
||||
// 如果是 Vue ref:取 .value
|
||||
if (typeof a === 'object' && 'value' in a) {
|
||||
return a.value ?? null
|
||||
}
|
||||
// 如果是组件实例(可能有 $el)
|
||||
if (typeof a === 'object' && '$el' in a) {
|
||||
// @ts-ignore
|
||||
return a.$el ?? null
|
||||
}
|
||||
// 直接 DOM 节点或 DOMRect
|
||||
return a
|
||||
}
|
||||
|
||||
const isSelected = (id: string) => selectedKbIds.value.includes(id)
|
||||
|
||||
const toggleKb = (id: string) => {
|
||||
isSelected(id) ? settingsStore.removeKnowledgeBase(id) : settingsStore.addKnowledgeBase(id)
|
||||
}
|
||||
|
||||
const toggleSelection = () => {
|
||||
const kb = filteredKnowledgeBases.value[highlightedIndex.value]
|
||||
if (kb) toggleKb(kb.id)
|
||||
}
|
||||
|
||||
const moveSelection = (dir: number) => {
|
||||
const max = filteredKnowledgeBases.value.length
|
||||
if (max === 0) return
|
||||
highlightedIndex.value = Math.max(0, Math.min(max - 1, highlightedIndex.value + dir))
|
||||
nextTick(() => {
|
||||
const items = kbList.value?.querySelectorAll('.kb-item')
|
||||
items?.[highlightedIndex.value]?.scrollIntoView({ block: 'nearest', behavior: 'smooth' })
|
||||
})
|
||||
}
|
||||
|
||||
const selectAll = () => settingsStore.selectKnowledgeBases(filteredKnowledgeBases.value.map(k => k.id))
|
||||
const clearAll = () => settingsStore.clearKnowledgeBases()
|
||||
|
||||
const close = () => {
|
||||
emit('update:visible', false)
|
||||
emit('close')
|
||||
}
|
||||
|
||||
const loadKnowledgeBases = async () => {
|
||||
try {
|
||||
const res: any = await listKnowledgeBases()
|
||||
if (res?.data && Array.isArray(res.data)) knowledgeBases.value = res.data
|
||||
} catch (e) {
|
||||
console.error('加载知识库失败', e)
|
||||
}
|
||||
}
|
||||
|
||||
// 计算下拉位置:水平居中对齐到按钮中点,处理视口边界
|
||||
const updateDropdownPosition = () => {
|
||||
const anchor = resolveAnchorEl()
|
||||
|
||||
// fallback 函数
|
||||
const applyFallback = () => {
|
||||
const vw = window.innerWidth
|
||||
const topFallback = Math.max(80, window.innerHeight / 2 - 160)
|
||||
dropdownStyle.value = {
|
||||
width: `${dropdownWidth}px`,
|
||||
left: `${Math.round((vw - dropdownWidth) / 2)}px`,
|
||||
top: `${Math.round(topFallback)}px`
|
||||
}
|
||||
}
|
||||
|
||||
if (!anchor) {
|
||||
applyFallback()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取 anchor 的 bounding rect
|
||||
let rect: DOMRect | null = null
|
||||
try {
|
||||
if (typeof anchor.getBoundingClientRect === 'function') {
|
||||
rect = anchor.getBoundingClientRect()
|
||||
} else if (anchor.width && anchor.left) {
|
||||
// 已经是 DOMRect
|
||||
rect = anchor as DOMRect
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[KnowledgeBaseSelector] Error getting bounding rect:', e)
|
||||
}
|
||||
|
||||
if (!rect) {
|
||||
applyFallback()
|
||||
return
|
||||
}
|
||||
|
||||
const vw = window.innerWidth
|
||||
const vh = window.innerHeight
|
||||
|
||||
// 左对齐到触发元素(也可以改为居中:rect.left + rect.width / 2 - dropdownWidth / 2)
|
||||
let left = Math.round(rect.left)
|
||||
|
||||
// 边界处理:不超出视口左右(留 8px margin)
|
||||
const minLeft = 8
|
||||
const maxLeft = Math.max(8, vw - dropdownWidth - 8)
|
||||
left = Math.max(minLeft, Math.min(maxLeft, left))
|
||||
|
||||
// 垂直:优先下方,若不够则上方
|
||||
let top = Math.round(rect.bottom + offsetY)
|
||||
const estimatedHeight = 320 // 估算高度,用于判断是否溢出
|
||||
|
||||
if (top + estimatedHeight > vh) {
|
||||
// 往上弹出
|
||||
top = Math.round(rect.top - estimatedHeight - offsetY)
|
||||
if (top < 8) {
|
||||
top = 8
|
||||
}
|
||||
}
|
||||
|
||||
// 使用 fixed 定位,不需要加 scroll offset
|
||||
dropdownStyle.value = {
|
||||
width: `${dropdownWidth}px`,
|
||||
left: `${left}px`,
|
||||
top: `${top}px`
|
||||
}
|
||||
}
|
||||
|
||||
// 当 visible 变化时处理
|
||||
watch(() => props.visible, async (v) => {
|
||||
if (v) {
|
||||
await loadKnowledgeBases()
|
||||
// 等 DOM 渲染完再计算位置,防止 rect 读取到不准值
|
||||
await nextTick()
|
||||
updateDropdownPosition()
|
||||
// 确保 focus
|
||||
nextTick(() => searchInput.value?.focus())
|
||||
// 监听 resize/scroll 做微调
|
||||
window.addEventListener('resize', updateDropdownPosition)
|
||||
window.addEventListener('scroll', updateDropdownPosition, true)
|
||||
} else {
|
||||
searchQuery.value = ''
|
||||
highlightedIndex.value = 0
|
||||
window.removeEventListener('resize', updateDropdownPosition)
|
||||
window.removeEventListener('scroll', updateDropdownPosition, true)
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
// 确保所有元素使用 border-box 盒模型
|
||||
.kb-overlay,
|
||||
.kb-overlay *,
|
||||
.kb-overlay *::before,
|
||||
.kb-overlay *::after {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.kb-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 9999;
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
/* 下拉面板使用 fixed 定位,相对于视口 */
|
||||
.kb-dropdown {
|
||||
position: fixed;
|
||||
background: #fff;
|
||||
border: 1px solid #e7e9eb;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 6px 28px rgba(15, 23, 42, 0.08);
|
||||
overflow: hidden;
|
||||
animation: fadeIn 0.12s ease-out;
|
||||
z-index: 10000;
|
||||
}
|
||||
|
||||
/* 宽度由 JS 控制(dropdownWidth),这里只做内部样式 */
|
||||
.kb-search {
|
||||
padding: 10px 12px;
|
||||
border-bottom: 1px solid #f1f3f4;
|
||||
}
|
||||
.kb-search-input {
|
||||
width: 100%;
|
||||
padding: 8px 12px;
|
||||
font-size: 13px;
|
||||
border: 1px solid #eef1f2;
|
||||
border-radius: 8px;
|
||||
background: #fbfdfc;
|
||||
outline: none;
|
||||
transition: border 0.12s;
|
||||
}
|
||||
.kb-search-input:focus {
|
||||
border-color: #10b981;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.kb-list {
|
||||
max-height: 260px;
|
||||
overflow-y: auto;
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.kb-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 8px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
transition: background 0.12s;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
.kb-item:last-child { margin-bottom: 0; }
|
||||
|
||||
.kb-item:hover,
|
||||
.kb-item.highlighted { background: #f6f8f7; }
|
||||
|
||||
.kb-item.selected { background: #eefdf5; }
|
||||
|
||||
.kb-item-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.checkbox {
|
||||
width: 18px; height: 18px;
|
||||
border-radius: 4px;
|
||||
border: 1.5px solid #d7dadd;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
.checkbox.checked {
|
||||
background: #10b981;
|
||||
border-color: #10b981;
|
||||
}
|
||||
.kb-name-wrap { display:flex; flex-direction: column; min-width: 0; }
|
||||
.kb-name { font-size: 13px; color: #222; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
.kb-docs { font-size: 11px; color: #8b9196; margin-top: 2px; }
|
||||
|
||||
.kb-empty { padding: 28px 8px; text-align: center; color: #9aa0a6; font-size: 13px; }
|
||||
|
||||
.kb-actions {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
padding: 10px;
|
||||
border-top: 1px solid #f2f4f5;
|
||||
background: #fafcfc;
|
||||
}
|
||||
.kb-btn {
|
||||
flex: 1;
|
||||
padding: 8px 10px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #e1e5e6;
|
||||
background: #fff;
|
||||
font-size: 13px;
|
||||
color: #52575a;
|
||||
cursor: pointer;
|
||||
transition: all 0.12s;
|
||||
}
|
||||
.kb-btn:hover {
|
||||
border-color: #10b981;
|
||||
color: #10b981;
|
||||
background: #f0fdf6;
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from { opacity: 0; transform: translateY(-6px); }
|
||||
to { opacity: 1; transform: translateY(0); }
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,886 @@
|
||||
<template>
|
||||
<t-dialog
|
||||
v-model:visible="dialogVisible"
|
||||
:header="isEdit ? '编辑模型' : '添加模型'"
|
||||
:width="600"
|
||||
:on-confirm="handleConfirm"
|
||||
:on-cancel="handleCancel"
|
||||
:confirm-btn="{ content: '保存', loading: saving }"
|
||||
>
|
||||
<div class="model-editor-form">
|
||||
<t-form ref="formRef" :data="formData" :rules="rules" layout="vertical">
|
||||
<!-- 模型名称 -->
|
||||
<t-form-item label="模型名称" name="name">
|
||||
<t-input v-model="formData.name" placeholder="为模型取个名字,如:GPT-4" />
|
||||
</t-form-item>
|
||||
|
||||
<!-- 模型来源 -->
|
||||
<t-form-item label="模型来源" name="source">
|
||||
<t-radio-group v-model="formData.source">
|
||||
<t-radio value="local">Ollama (本地)</t-radio>
|
||||
<t-radio value="remote">Remote API (远程)</t-radio>
|
||||
</t-radio-group>
|
||||
</t-form-item>
|
||||
|
||||
<!-- VLLM 专用:接口类型(在来源选择之后) -->
|
||||
<template v-if="modelType === 'vllm' && formData.source === 'local'">
|
||||
<t-form-item label="接口类型" name="interfaceType">
|
||||
<t-radio-group v-model="formData.interfaceType">
|
||||
<t-radio value="ollama">Ollama</t-radio>
|
||||
<t-radio value="openai">OpenAI 兼容接口</t-radio>
|
||||
</t-radio-group>
|
||||
</t-form-item>
|
||||
</template>
|
||||
|
||||
<!-- Ollama 本地模型选择器 -->
|
||||
<t-form-item v-if="formData.source === 'local' && modelType !== 'vllm'" label="模型标识" name="modelName">
|
||||
<t-select
|
||||
v-model="formData.modelName"
|
||||
:loading="loadingOllamaModels"
|
||||
:class="{ 'downloading': downloading }"
|
||||
:style="downloading ? `--progress: ${downloadProgress}%` : ''"
|
||||
filterable
|
||||
:filter="handleModelFilter"
|
||||
placeholder="搜索模型..."
|
||||
@focus="loadOllamaModels"
|
||||
@visible-change="handleDropdownVisibleChange"
|
||||
>
|
||||
<!-- 已下载的模型 -->
|
||||
<t-option
|
||||
v-for="model in filteredOllamaModels"
|
||||
:key="model.name"
|
||||
:value="model.name"
|
||||
:label="model.name"
|
||||
>
|
||||
<div class="model-option">
|
||||
<t-icon name="check-circle-filled" class="downloaded-icon" />
|
||||
<span class="model-name">{{ model.name }}</span>
|
||||
<span class="model-size">{{ formatModelSize(model.size) }}</span>
|
||||
</div>
|
||||
</t-option>
|
||||
|
||||
<!-- 下载新模型选项(仅当搜索词不在列表中时显示) -->
|
||||
<t-option
|
||||
v-if="showDownloadOption"
|
||||
:value="`__download__${searchKeyword}`"
|
||||
:label="`下载: ${searchKeyword}`"
|
||||
class="download-option"
|
||||
>
|
||||
<div class="model-option download">
|
||||
<t-icon name="download" class="download-icon" />
|
||||
<span class="model-name">下载: {{ searchKeyword }}</span>
|
||||
</div>
|
||||
</t-option>
|
||||
|
||||
<!-- 下载进度后缀 -->
|
||||
<template v-if="downloading" #suffix>
|
||||
<div class="download-suffix">
|
||||
<t-icon name="loading" class="spinning" />
|
||||
<span class="progress-text">{{ downloadProgress.toFixed(1) }}%</span>
|
||||
</div>
|
||||
</template>
|
||||
</t-select>
|
||||
|
||||
<!-- 刷新按钮 -->
|
||||
<t-button
|
||||
variant="text"
|
||||
size="small"
|
||||
:loading="loadingOllamaModels"
|
||||
@click="refreshOllamaModels"
|
||||
class="refresh-btn"
|
||||
>
|
||||
<t-icon name="refresh" />
|
||||
刷新列表
|
||||
</t-button>
|
||||
</t-form-item>
|
||||
|
||||
<!-- Remote API 和 VLLM 保持原有的 input -->
|
||||
<t-form-item v-else label="模型标识" name="modelName">
|
||||
<t-input
|
||||
v-model="formData.modelName"
|
||||
:placeholder="formData.source === 'local' ? '如:llava:latest' : '如:gpt-4, claude-3-opus'"
|
||||
/>
|
||||
</t-form-item>
|
||||
|
||||
<!-- Remote API 配置或 VLLM OpenAI 兼容接口 -->
|
||||
<template v-if="formData.source === 'remote' || (modelType === 'vllm' && formData.interfaceType === 'openai')">
|
||||
<t-form-item label="Base URL" name="baseUrl">
|
||||
<t-input
|
||||
v-model="formData.baseUrl"
|
||||
:placeholder="modelType === 'vllm' ? '如:http://localhost:11434/v1' : '如:https://api.openai.com/v1'"
|
||||
/>
|
||||
</t-form-item>
|
||||
|
||||
<t-form-item label="API Key (可选)" name="apiKey">
|
||||
<t-input
|
||||
v-model="formData.apiKey"
|
||||
type="password"
|
||||
placeholder="输入 API Key"
|
||||
/>
|
||||
</t-form-item>
|
||||
|
||||
<!-- Remote API 校验 -->
|
||||
<t-form-item label="连接测试">
|
||||
<div class="api-test-section">
|
||||
<t-button
|
||||
variant="outline"
|
||||
@click="checkRemoteAPI"
|
||||
:loading="checking"
|
||||
:disabled="!formData.modelName || !formData.baseUrl"
|
||||
>
|
||||
<template #icon>
|
||||
<t-icon
|
||||
v-if="!checking && remoteChecked && remoteAvailable"
|
||||
name="check-circle-filled"
|
||||
class="status-icon available"
|
||||
/>
|
||||
<t-icon
|
||||
v-else-if="!checking && remoteChecked && !remoteAvailable"
|
||||
name="close-circle-filled"
|
||||
class="status-icon unavailable"
|
||||
/>
|
||||
</template>
|
||||
{{ checking ? '测试中...' : '测试连接' }}
|
||||
</t-button>
|
||||
<span v-if="remoteChecked" :class="['test-message', remoteAvailable ? 'success' : 'error']">
|
||||
{{ remoteMessage }}
|
||||
</span>
|
||||
</div>
|
||||
</t-form-item>
|
||||
</template>
|
||||
|
||||
<!-- Embedding 专用:维度 -->
|
||||
<t-form-item v-if="modelType === 'embedding'" label="向量维度" name="dimension">
|
||||
<t-input-number
|
||||
v-model="formData.dimension"
|
||||
:min="128"
|
||||
:max="4096"
|
||||
placeholder="如:1536"
|
||||
/>
|
||||
</t-form-item>
|
||||
|
||||
<!-- 设为默认 -->
|
||||
<t-form-item label=" " name="isDefault">
|
||||
<t-checkbox v-model="formData.isDefault">设为默认模型</t-checkbox>
|
||||
</t-form-item>
|
||||
</t-form>
|
||||
</div>
|
||||
</t-dialog>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, watch, computed, onUnmounted } from 'vue'
|
||||
import { MessagePlugin } from 'tdesign-vue-next'
|
||||
import { checkOllamaModels, checkRemoteModel, testEmbeddingModel, checkRerankModel, listOllamaModels, downloadOllamaModel, getDownloadProgress, type OllamaModelInfo } from '@/api/initialization'
|
||||
|
||||
interface ModelFormData {
|
||||
id: string
|
||||
name: string
|
||||
source: 'local' | 'remote'
|
||||
modelName: string
|
||||
baseUrl?: string
|
||||
apiKey?: string
|
||||
dimension?: number
|
||||
interfaceType?: 'ollama' | 'openai'
|
||||
isDefault: boolean
|
||||
}
|
||||
|
||||
interface Props {
|
||||
visible: boolean
|
||||
modelType: 'chat' | 'embedding' | 'rerank' | 'vllm'
|
||||
modelData?: ModelFormData | null
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
visible: false,
|
||||
modelData: null
|
||||
})
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:visible': [value: boolean]
|
||||
'confirm': [data: ModelFormData]
|
||||
}>()
|
||||
|
||||
const dialogVisible = computed({
|
||||
get: () => props.visible,
|
||||
set: (val) => emit('update:visible', val)
|
||||
})
|
||||
|
||||
const isEdit = computed(() => !!props.modelData)
|
||||
|
||||
const formRef = ref()
|
||||
const saving = ref(false)
|
||||
const modelChecked = ref(false)
|
||||
const modelAvailable = ref(false)
|
||||
const checking = ref(false)
|
||||
const remoteChecked = ref(false)
|
||||
const remoteAvailable = ref(false)
|
||||
const remoteMessage = ref('')
|
||||
|
||||
// Ollama 模型状态
|
||||
const ollamaModelList = ref<OllamaModelInfo[]>([])
|
||||
const loadingOllamaModels = ref(false)
|
||||
const searchKeyword = ref('')
|
||||
const downloading = ref(false)
|
||||
const downloadProgress = ref(0)
|
||||
const currentDownloadModel = ref('')
|
||||
let downloadInterval: any = null
|
||||
|
||||
const formData = ref<ModelFormData>({
|
||||
id: '',
|
||||
name: '',
|
||||
source: 'local',
|
||||
modelName: '',
|
||||
baseUrl: '',
|
||||
apiKey: '',
|
||||
dimension: 1536,
|
||||
interfaceType: 'ollama',
|
||||
isDefault: false
|
||||
})
|
||||
|
||||
const rules = {
|
||||
name: [{ required: true, message: '请输入模型名称' }],
|
||||
modelName: [{ required: true, message: '请输入模型标识' }],
|
||||
baseUrl: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入 Base URL',
|
||||
trigger: 'blur'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
// 监听 visible 变化,初始化表单
|
||||
watch(() => props.visible, (val) => {
|
||||
if (val) {
|
||||
if (props.modelData) {
|
||||
formData.value = { ...props.modelData }
|
||||
} else {
|
||||
resetForm()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// 重置表单
|
||||
const resetForm = () => {
|
||||
formData.value = {
|
||||
id: generateId(),
|
||||
name: '',
|
||||
source: 'local',
|
||||
modelName: '',
|
||||
baseUrl: '',
|
||||
apiKey: '',
|
||||
dimension: props.modelType === 'embedding' ? 1536 : undefined,
|
||||
interfaceType: props.modelType === 'vllm' ? 'ollama' : undefined,
|
||||
isDefault: false
|
||||
}
|
||||
modelChecked.value = false
|
||||
modelAvailable.value = false
|
||||
remoteChecked.value = false
|
||||
remoteAvailable.value = false
|
||||
remoteMessage.value = ''
|
||||
}
|
||||
|
||||
// 监听来源变化,重置校验状态(已合并到下面的 watch)
|
||||
|
||||
// 生成唯一ID
|
||||
const generateId = () => {
|
||||
return `model_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
|
||||
}
|
||||
|
||||
// 过滤后的模型列表
|
||||
const filteredOllamaModels = computed(() => {
|
||||
if (!searchKeyword.value) return ollamaModelList.value
|
||||
return ollamaModelList.value.filter(model =>
|
||||
model.name.toLowerCase().includes(searchKeyword.value.toLowerCase())
|
||||
)
|
||||
})
|
||||
|
||||
// 是否显示"下载模型"选项
|
||||
const showDownloadOption = computed(() => {
|
||||
if (!searchKeyword.value.trim()) return false
|
||||
// 检查搜索词是否已存在于模型列表中
|
||||
const exists = ollamaModelList.value.some(model =>
|
||||
model.name.toLowerCase() === searchKeyword.value.toLowerCase()
|
||||
)
|
||||
return !exists
|
||||
})
|
||||
|
||||
// 自定义过滤逻辑(捕获搜索关键词)
|
||||
const handleModelFilter = (filterWords: string) => {
|
||||
searchKeyword.value = filterWords
|
||||
return true // 让 TDesign 使用我们的 filteredOllamaModels
|
||||
}
|
||||
|
||||
// 加载 Ollama 模型列表
|
||||
const loadOllamaModels = async () => {
|
||||
if (formData.value.source !== 'local') return
|
||||
|
||||
loadingOllamaModels.value = true
|
||||
try {
|
||||
const models = await listOllamaModels()
|
||||
ollamaModelList.value = models
|
||||
} catch (error) {
|
||||
console.error('加载 Ollama 模型列表失败:', error)
|
||||
MessagePlugin.error('加载模型列表失败')
|
||||
} finally {
|
||||
loadingOllamaModels.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新模型列表
|
||||
const refreshOllamaModels = async () => {
|
||||
ollamaModelList.value = [] // 清空以强制重新加载
|
||||
await loadOllamaModels()
|
||||
MessagePlugin.success('列表已刷新')
|
||||
}
|
||||
|
||||
// 监听下拉框可见性变化
|
||||
const handleDropdownVisibleChange = (visible: boolean) => {
|
||||
if (!visible) {
|
||||
searchKeyword.value = ''
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化模型大小
|
||||
const formatModelSize = (bytes: number): string => {
|
||||
if (!bytes || bytes === 0) return ''
|
||||
const gb = bytes / (1024 * 1024 * 1024)
|
||||
return gb >= 1 ? `${gb.toFixed(1)} GB` : `${(bytes / (1024 * 1024)).toFixed(0)} MB`
|
||||
}
|
||||
|
||||
// 检查模型状态(Ollama本地模型)
|
||||
const checkModelStatus = async () => {
|
||||
if (!formData.value.modelName || formData.value.source !== 'local') {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// 调用真实 Ollama API 检查模型是否存在
|
||||
const result = await checkOllamaModels([formData.value.modelName])
|
||||
modelChecked.value = true
|
||||
modelAvailable.value = result.models[formData.value.modelName] || false
|
||||
} catch (error) {
|
||||
console.error('检查模型状态失败:', error)
|
||||
modelChecked.value = false
|
||||
modelAvailable.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 检查 Remote API 连接(根据模型类型调用不同的接口)
|
||||
const checkRemoteAPI = async () => {
|
||||
if (!formData.value.modelName || !formData.value.baseUrl) {
|
||||
MessagePlugin.warning('请先填写模型标识和 Base URL')
|
||||
return
|
||||
}
|
||||
|
||||
checking.value = true
|
||||
remoteChecked.value = false
|
||||
remoteMessage.value = ''
|
||||
|
||||
try {
|
||||
let result: any
|
||||
|
||||
// 根据模型类型调用不同的校验接口
|
||||
switch (props.modelType) {
|
||||
case 'chat':
|
||||
// 对话模型(KnowledgeQA)
|
||||
result = await checkRemoteModel({
|
||||
modelName: formData.value.modelName,
|
||||
baseUrl: formData.value.baseUrl,
|
||||
apiKey: formData.value.apiKey || ''
|
||||
})
|
||||
break
|
||||
|
||||
case 'embedding':
|
||||
// Embedding 模型
|
||||
result = await testEmbeddingModel({
|
||||
source: 'remote',
|
||||
modelName: formData.value.modelName,
|
||||
baseUrl: formData.value.baseUrl,
|
||||
apiKey: formData.value.apiKey || '',
|
||||
dimension: formData.value.dimension
|
||||
})
|
||||
// 如果测试成功且返回了维度,自动填充
|
||||
if (result.available && result.dimension) {
|
||||
formData.value.dimension = result.dimension
|
||||
MessagePlugin.info(`检测到向量维度:${result.dimension}`)
|
||||
}
|
||||
break
|
||||
|
||||
case 'rerank':
|
||||
// Rerank 模型
|
||||
result = await checkRerankModel({
|
||||
modelName: formData.value.modelName,
|
||||
baseUrl: formData.value.baseUrl,
|
||||
apiKey: formData.value.apiKey || ''
|
||||
})
|
||||
break
|
||||
|
||||
case 'vllm':
|
||||
// VLLM 模型(多模态)
|
||||
// VLLM 使用 checkRemoteModel 进行基础连接测试
|
||||
result = await checkRemoteModel({
|
||||
modelName: formData.value.modelName,
|
||||
baseUrl: formData.value.baseUrl,
|
||||
apiKey: formData.value.apiKey || ''
|
||||
})
|
||||
break
|
||||
|
||||
default:
|
||||
MessagePlugin.error('不支持的模型类型')
|
||||
return
|
||||
}
|
||||
|
||||
remoteChecked.value = true
|
||||
remoteAvailable.value = result.available || false
|
||||
remoteMessage.value = result.message || (result.available ? '连接成功' : '连接失败')
|
||||
|
||||
if (result.available) {
|
||||
MessagePlugin.success(remoteMessage.value)
|
||||
} else {
|
||||
MessagePlugin.error(remoteMessage.value)
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Remote API 校验失败:', error)
|
||||
remoteChecked.value = true
|
||||
remoteAvailable.value = false
|
||||
remoteMessage.value = error.message || '连接失败,请检查配置'
|
||||
MessagePlugin.error(remoteMessage.value)
|
||||
} finally {
|
||||
checking.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 确认保存
|
||||
const handleConfirm = async () => {
|
||||
try {
|
||||
await formRef.value?.validate()
|
||||
saving.value = true
|
||||
|
||||
// 如果是新增且没有 id,生成一个
|
||||
if (!formData.value.id) {
|
||||
formData.value.id = generateId()
|
||||
}
|
||||
|
||||
emit('confirm', { ...formData.value })
|
||||
dialogVisible.value = false
|
||||
MessagePlugin.success(isEdit.value ? '模型已更新' : '模型已添加')
|
||||
} catch (error) {
|
||||
console.error('表单验证失败:', error)
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 监听模型选择变化(处理下载逻辑)
|
||||
watch(() => formData.value.modelName, async (newValue) => {
|
||||
if (!newValue || !newValue.startsWith('__download__')) return
|
||||
|
||||
// 提取模型名称
|
||||
const modelName = newValue.replace('__download__', '')
|
||||
|
||||
// 重置选择(避免显示 __download__ 前缀)
|
||||
formData.value.modelName = ''
|
||||
|
||||
// 开始下载
|
||||
await startDownload(modelName)
|
||||
})
|
||||
|
||||
// 开始下载模型
|
||||
const startDownload = async (modelName: string) => {
|
||||
downloading.value = true
|
||||
downloadProgress.value = 0
|
||||
currentDownloadModel.value = modelName
|
||||
|
||||
try {
|
||||
// 启动下载
|
||||
const result = await downloadOllamaModel(modelName)
|
||||
const taskId = result.taskId
|
||||
|
||||
MessagePlugin.success(`开始下载 ${modelName}`)
|
||||
|
||||
// 轮询下载进度
|
||||
downloadInterval = setInterval(async () => {
|
||||
try {
|
||||
const progress = await getDownloadProgress(taskId)
|
||||
downloadProgress.value = progress.progress
|
||||
|
||||
if (progress.status === 'completed') {
|
||||
// 下载完成
|
||||
clearInterval(downloadInterval)
|
||||
downloadInterval = null
|
||||
downloading.value = false
|
||||
|
||||
MessagePlugin.success(`${modelName} 下载完成`)
|
||||
|
||||
// 刷新模型列表
|
||||
await loadOllamaModels()
|
||||
|
||||
// 自动选中新下载的模型
|
||||
formData.value.modelName = modelName
|
||||
|
||||
// 重置状态
|
||||
downloadProgress.value = 0
|
||||
currentDownloadModel.value = ''
|
||||
|
||||
} else if (progress.status === 'failed') {
|
||||
// 下载失败
|
||||
clearInterval(downloadInterval)
|
||||
downloadInterval = null
|
||||
downloading.value = false
|
||||
MessagePlugin.error(progress.message || `${modelName} 下载失败`)
|
||||
downloadProgress.value = 0
|
||||
currentDownloadModel.value = ''
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取下载进度失败:', error)
|
||||
}
|
||||
}, 1000) // 每秒查询一次
|
||||
|
||||
} catch (error: any) {
|
||||
downloading.value = false
|
||||
downloadProgress.value = 0
|
||||
currentDownloadModel.value = ''
|
||||
MessagePlugin.error(error.message || '启动下载失败')
|
||||
}
|
||||
}
|
||||
|
||||
// 组件卸载时清理定时器
|
||||
onUnmounted(() => {
|
||||
if (downloadInterval) {
|
||||
clearInterval(downloadInterval)
|
||||
}
|
||||
})
|
||||
|
||||
// 监听来源变化,清理所有状态
|
||||
watch(() => formData.value.source, () => {
|
||||
// 重置校验状态
|
||||
modelChecked.value = false
|
||||
modelAvailable.value = false
|
||||
remoteChecked.value = false
|
||||
remoteAvailable.value = false
|
||||
remoteMessage.value = ''
|
||||
|
||||
// 清理下载状态
|
||||
searchKeyword.value = ''
|
||||
if (downloadInterval) {
|
||||
clearInterval(downloadInterval)
|
||||
downloadInterval = null
|
||||
}
|
||||
downloading.value = false
|
||||
downloadProgress.value = 0
|
||||
currentDownloadModel.value = ''
|
||||
})
|
||||
|
||||
// 取消
|
||||
const handleCancel = () => {
|
||||
dialogVisible.value = false
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.model-editor-form {
|
||||
padding: 8px 0;
|
||||
max-height: 60vh;
|
||||
overflow-y: auto;
|
||||
|
||||
// 自定义滚动条
|
||||
&::-webkit-scrollbar {
|
||||
width: 6px;
|
||||
}
|
||||
|
||||
&::-webkit-scrollbar-track {
|
||||
background: #f5f5f5;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
&::-webkit-scrollbar-thumb {
|
||||
background: #d0d0d0;
|
||||
border-radius: 3px;
|
||||
|
||||
&:hover {
|
||||
background: #b0b0b0;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.t-form-item) {
|
||||
margin-bottom: 18px;
|
||||
|
||||
&:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.t-form-item__label) {
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
color: #333333;
|
||||
padding-bottom: 6px;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
:deep(.t-input),
|
||||
:deep(.t-select),
|
||||
:deep(.t-textarea),
|
||||
:deep(.t-input-number) {
|
||||
font-size: 13px;
|
||||
background: #ffffff;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 6px;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
border-color: #07C05F;
|
||||
background: #fafafa;
|
||||
}
|
||||
|
||||
&:focus,
|
||||
&.t-is-focused {
|
||||
background: #ffffff;
|
||||
border-color: #07C05F;
|
||||
box-shadow: 0 0 0 2px rgba(7, 192, 95, 0.1);
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.t-input__inner) {
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
:deep(.t-radio-group) {
|
||||
.t-radio {
|
||||
margin-right: 16px;
|
||||
font-size: 13px;
|
||||
|
||||
&:last-child {
|
||||
margin-right: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.t-radio__label {
|
||||
font-size: 13px;
|
||||
color: #333333;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.t-checkbox) {
|
||||
font-size: 13px;
|
||||
|
||||
.t-checkbox__label {
|
||||
font-size: 13px;
|
||||
color: #333333;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.model-input-with-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
|
||||
:deep(.t-input) {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.status-icon {
|
||||
font-size: 16px;
|
||||
flex-shrink: 0;
|
||||
|
||||
&.available {
|
||||
color: #07C05F;
|
||||
}
|
||||
|
||||
&.unavailable {
|
||||
color: #e34d59;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.download-hint {
|
||||
margin-top: -10px;
|
||||
margin-bottom: 8px;
|
||||
|
||||
:deep(.t-alert) {
|
||||
padding: 8px 12px;
|
||||
font-size: 12px;
|
||||
border-radius: 6px;
|
||||
background: #fff7e6;
|
||||
border-color: #ffa940;
|
||||
}
|
||||
}
|
||||
|
||||
.api-test-section {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
|
||||
.test-message {
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
|
||||
&.success {
|
||||
color: #07C05F;
|
||||
}
|
||||
|
||||
&.error {
|
||||
color: #e34d59;
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.t-button) {
|
||||
min-width: 100px;
|
||||
}
|
||||
}
|
||||
|
||||
// 优化对话框样式
|
||||
:deep(.t-dialog) {
|
||||
border-radius: 12px;
|
||||
|
||||
.t-dialog__header {
|
||||
padding: 20px 24px 16px;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
|
||||
.t-dialog__header-content {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: #333333;
|
||||
}
|
||||
}
|
||||
|
||||
.t-dialog__body {
|
||||
padding: 20px 24px;
|
||||
}
|
||||
|
||||
.t-dialog__footer {
|
||||
padding: 12px 24px 20px;
|
||||
border-top: 1px solid #e5e7eb;
|
||||
|
||||
.t-button {
|
||||
font-size: 13px;
|
||||
padding: 6px 16px;
|
||||
border-radius: 6px;
|
||||
|
||||
&.t-button--theme-primary {
|
||||
background: #07C05F;
|
||||
border-color: #07C05F;
|
||||
|
||||
&:hover {
|
||||
background: #05a34e;
|
||||
border-color: #05a34e;
|
||||
}
|
||||
}
|
||||
|
||||
&.t-button--variant-outline {
|
||||
&:hover {
|
||||
border-color: #07C05F;
|
||||
color: #07C05F;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ollama 模型选择器样式
|
||||
.model-option {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
width: 100%;
|
||||
padding: 4px 0;
|
||||
|
||||
.downloaded-icon {
|
||||
font-size: 14px;
|
||||
color: #07C05F;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.download-icon {
|
||||
font-size: 14px;
|
||||
color: #07C05F;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.model-name {
|
||||
flex: 1;
|
||||
font-size: 13px;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
.model-size {
|
||||
font-size: 12px;
|
||||
color: #999999;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
&.download {
|
||||
.model-name {
|
||||
color: #07C05F;
|
||||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 下载进度后缀样式
|
||||
.download-suffix {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
padding: 0 4px;
|
||||
|
||||
.spinning {
|
||||
animation: spin 1s linear infinite;
|
||||
font-size: 14px;
|
||||
color: #07C05F;
|
||||
}
|
||||
|
||||
.progress-text {
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
color: #07C05F;
|
||||
}
|
||||
}
|
||||
|
||||
// 下载中的选择框进度条效果
|
||||
:deep(.t-select.downloading) {
|
||||
.t-input {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
|
||||
&::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
width: var(--progress, 0%);
|
||||
background: linear-gradient(90deg, rgba(7, 192, 95, 0.08), rgba(7, 192, 95, 0.15));
|
||||
transition: width 0.3s ease;
|
||||
z-index: 0;
|
||||
border-radius: 5px 0 0 5px;
|
||||
}
|
||||
|
||||
.t-input__inner,
|
||||
input {
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
background: transparent !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.refresh-btn {
|
||||
margin-top: 4px;
|
||||
font-size: 12px;
|
||||
color: #666666;
|
||||
|
||||
&:hover {
|
||||
color: #07C05F;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
from { transform: rotate(0deg); }
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,301 @@
|
||||
<template>
|
||||
<div class="user-menu" ref="menuRef">
|
||||
<!-- 用户按钮 -->
|
||||
<div class="user-button" @click="toggleMenu">
|
||||
<div class="user-avatar">
|
||||
<img v-if="userAvatar" :src="userAvatar" alt="用户头像" />
|
||||
<span v-else class="avatar-placeholder">{{ userInitial }}</span>
|
||||
</div>
|
||||
<div class="user-info">
|
||||
<div class="user-name">{{ userName }}</div>
|
||||
<div class="user-email">{{ userEmail }}</div>
|
||||
</div>
|
||||
<t-icon :name="menuVisible ? 'chevron-up' : 'chevron-down'" class="dropdown-icon" />
|
||||
</div>
|
||||
|
||||
<!-- 下拉菜单 -->
|
||||
<Transition name="dropdown">
|
||||
<div v-if="menuVisible" class="user-dropdown" @click.stop>
|
||||
<div class="menu-item" @click="handleQuickNav('models')">
|
||||
<t-icon name="component" class="menu-icon" />
|
||||
<span>模型配置</span>
|
||||
</div>
|
||||
<div class="menu-item" @click="handleQuickNav('ollama')">
|
||||
<span class="menu-icon emoji-icon">🦙</span>
|
||||
<span>Ollama</span>
|
||||
</div>
|
||||
<div class="menu-item" @click="handleQuickNav('knowledge')">
|
||||
<t-icon name="layers" class="menu-icon" />
|
||||
<span>知识库</span>
|
||||
</div>
|
||||
<div class="menu-divider"></div>
|
||||
<div class="menu-item" @click="handleSettings">
|
||||
<t-icon name="setting" class="menu-icon" />
|
||||
<span>全部设置</span>
|
||||
</div>
|
||||
<div class="menu-divider"></div>
|
||||
<div class="menu-item danger" @click="handleLogout">
|
||||
<t-icon name="logout" class="menu-icon" />
|
||||
<span>注销</span>
|
||||
</div>
|
||||
</div>
|
||||
</Transition>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted, onUnmounted } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useUIStore } from '@/stores/ui'
|
||||
import { MessagePlugin } from 'tdesign-vue-next'
|
||||
import { getCurrentUser } from '@/api/auth'
|
||||
|
||||
const router = useRouter()
|
||||
const uiStore = useUIStore()
|
||||
|
||||
const menuRef = ref<HTMLElement>()
|
||||
const menuVisible = ref(false)
|
||||
|
||||
// 用户信息
|
||||
const userInfo = ref({
|
||||
username: '用户',
|
||||
email: 'user@example.com',
|
||||
avatar: ''
|
||||
})
|
||||
|
||||
const userName = computed(() => userInfo.value.username)
|
||||
const userEmail = computed(() => userInfo.value.email)
|
||||
const userAvatar = computed(() => userInfo.value.avatar)
|
||||
|
||||
// 用户名首字母(用于无头像时显示)
|
||||
const userInitial = computed(() => {
|
||||
return userName.value.charAt(0).toUpperCase()
|
||||
})
|
||||
|
||||
// 切换菜单显示
|
||||
const toggleMenu = () => {
|
||||
menuVisible.value = !menuVisible.value
|
||||
}
|
||||
|
||||
// 快捷导航到设置的特定部分
|
||||
const handleQuickNav = (section: string) => {
|
||||
menuVisible.value = false
|
||||
uiStore.openSettings()
|
||||
router.push('/platform/settings')
|
||||
|
||||
// 延迟一下,确保设置页面已经渲染
|
||||
setTimeout(() => {
|
||||
// 触发设置页面切换到对应section
|
||||
const event = new CustomEvent('settings-nav', { detail: { section } })
|
||||
window.dispatchEvent(event)
|
||||
}, 100)
|
||||
}
|
||||
|
||||
// 打开设置
|
||||
const handleSettings = () => {
|
||||
menuVisible.value = false
|
||||
uiStore.openSettings()
|
||||
router.push('/platform/settings')
|
||||
}
|
||||
|
||||
// 注销
|
||||
const handleLogout = () => {
|
||||
menuVisible.value = false
|
||||
|
||||
// 清理本地存储
|
||||
localStorage.removeItem('WeKnora_token')
|
||||
|
||||
MessagePlugin.success('已退出登录')
|
||||
|
||||
// 跳转到登录页
|
||||
router.push('/login')
|
||||
}
|
||||
|
||||
// 加载用户信息
|
||||
const loadUserInfo = async () => {
|
||||
try {
|
||||
const response = await getCurrentUser()
|
||||
if (response.success && response.data && response.data.user) {
|
||||
userInfo.value = {
|
||||
username: response.data.user.username || '用户',
|
||||
email: response.data.user.email || 'user@example.com',
|
||||
avatar: response.data.user.avatar || ''
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load user info:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 点击外部关闭菜单
|
||||
const handleClickOutside = (e: MouseEvent) => {
|
||||
if (menuRef.value && !menuRef.value.contains(e.target as Node)) {
|
||||
menuVisible.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener('click', handleClickOutside)
|
||||
loadUserInfo()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
document.removeEventListener('click', handleClickOutside)
|
||||
})
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.user-menu {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.user-button {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 12px 16px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
background: transparent;
|
||||
|
||||
&:hover {
|
||||
background: #f5f7fa;
|
||||
}
|
||||
|
||||
&:active {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
}
|
||||
|
||||
.user-avatar {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border-radius: 50%;
|
||||
overflow: hidden;
|
||||
flex-shrink: 0;
|
||||
background: linear-gradient(135deg, #07C05F 0%, #05A34E 100%);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.avatar-placeholder {
|
||||
color: #ffffff;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
}
|
||||
}
|
||||
|
||||
.user-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
text-align: left;
|
||||
|
||||
.user-name {
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
color: #333333;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.user-email {
|
||||
font-size: 12px;
|
||||
color: #666666;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
}
|
||||
|
||||
.dropdown-icon {
|
||||
font-size: 16px;
|
||||
color: #666666;
|
||||
flex-shrink: 0;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
.user-dropdown {
|
||||
position: absolute;
|
||||
bottom: 100%;
|
||||
left: 8px;
|
||||
right: 8px;
|
||||
margin-bottom: 8px;
|
||||
background: #ffffff;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
|
||||
border: 1px solid #e5e7eb;
|
||||
overflow: hidden;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.menu-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 12px 16px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
font-size: 14px;
|
||||
color: #333333;
|
||||
|
||||
&:hover {
|
||||
background: #f5f7fa;
|
||||
}
|
||||
|
||||
&.danger {
|
||||
color: #e34d59;
|
||||
|
||||
&:hover {
|
||||
background: #fef0f0;
|
||||
}
|
||||
|
||||
.menu-icon {
|
||||
color: #e34d59;
|
||||
}
|
||||
}
|
||||
|
||||
.menu-icon {
|
||||
font-size: 16px;
|
||||
color: #666666;
|
||||
}
|
||||
}
|
||||
|
||||
.menu-divider {
|
||||
height: 1px;
|
||||
background: #e5e7eb;
|
||||
margin: 4px 0;
|
||||
}
|
||||
|
||||
.emoji-icon {
|
||||
font-size: 16px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
// 下拉动画
|
||||
.dropdown-enter-active,
|
||||
.dropdown-leave-active {
|
||||
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
}
|
||||
|
||||
.dropdown-enter-from,
|
||||
.dropdown-leave-to {
|
||||
opacity: 0;
|
||||
transform: translateY(8px);
|
||||
}
|
||||
|
||||
.dropdown-enter-to,
|
||||
.dropdown-leave-from {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -75,38 +75,9 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 下半部分:账户信息、系统设置、退出登录 -->
|
||||
<!-- 下半部分:用户菜单 -->
|
||||
<div class="menu_bottom">
|
||||
<div class="menu_box" v-for="(item, index) in bottomMenuItems" :key="'bottom-' + index">
|
||||
<div v-if="item.path === 'logout'">
|
||||
<t-popconfirm
|
||||
content="确定要退出登录吗?"
|
||||
@confirm="handleLogout"
|
||||
placement="top"
|
||||
:show-arrow="true"
|
||||
>
|
||||
<div @mouseenter="mouseenteMenu(item.path)" @mouseleave="mouseleaveMenu(item.path)"
|
||||
:class="['menu_item', 'logout-item']">
|
||||
<div class="menu_item-box">
|
||||
<div class="menu_icon">
|
||||
<img class="icon" :src="getImgSrc(logoutIcon)" alt="">
|
||||
</div>
|
||||
<span class="menu_title">{{ item.title }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</t-popconfirm>
|
||||
</div>
|
||||
<div v-else @click="handleMenuClick(item.path)"
|
||||
@mouseenter="mouseenteMenu(item.path)" @mouseleave="mouseleaveMenu(item.path)"
|
||||
:class="['menu_item', item.childrenPath && item.childrenPath == currentpath ? 'menu_item_c_active' : (item.path == currentpath) ? 'menu_item_active' : '']">
|
||||
<div class="menu_item-box">
|
||||
<div class="menu_icon">
|
||||
<img class="icon" :src="getImgSrc(item.icon == 'zhishiku' ? knowledgeIcon : item.icon == 'tenant' ? tenantIcon : prefixIcon)" alt="">
|
||||
</div>
|
||||
<span class="menu_title">{{ item.path === 'knowledge-bases' && kbMenuItem ? kbMenuItem.title : item.title }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<UserMenu />
|
||||
</div>
|
||||
|
||||
<input type="file" @change="upload" style="display: none" ref="uploadInput"
|
||||
@@ -123,10 +94,13 @@ import { getKnowledgeBaseById, listKnowledgeBases, uploadKnowledgeFile } from '@
|
||||
import { kbFileTypeVerification } from '@/utils/index';
|
||||
import { useMenuStore } from '@/stores/menu';
|
||||
import { useAuthStore } from '@/stores/auth';
|
||||
import { useUIStore } from '@/stores/ui';
|
||||
import { MessagePlugin } from "tdesign-vue-next";
|
||||
import UserMenu from '@/components/UserMenu.vue';
|
||||
let uploadInput = ref();
|
||||
const usemenuStore = useMenuStore();
|
||||
const authStore = useAuthStore();
|
||||
const uiStore = useUIStore();
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const currentpath = ref('');
|
||||
@@ -160,9 +134,9 @@ const isMenuItemActive = (itemPath: string): boolean => {
|
||||
currentRoute === 'knowledgeBaseDetail' ||
|
||||
currentRoute === 'knowledgeBaseSettings';
|
||||
case 'creatChat':
|
||||
return currentRoute === 'kbCreatChat';
|
||||
case 'tenant':
|
||||
return currentRoute === 'tenant';
|
||||
return currentRoute === 'kbCreatChat' || currentRoute === 'globalCreatChat';
|
||||
case 'settings':
|
||||
return currentRoute === 'settings';
|
||||
default:
|
||||
return itemPath === currentpath.value;
|
||||
}
|
||||
@@ -178,8 +152,8 @@ const getIconActiveState = (itemPath: string) => {
|
||||
currentRoute === 'knowledgeBaseDetail' ||
|
||||
currentRoute === 'knowledgeBaseSettings'
|
||||
),
|
||||
isCreatChatActive: itemPath === 'creatChat' && currentRoute === 'kbCreatChat',
|
||||
isTenantActive: itemPath === 'tenant' && currentRoute === 'tenant',
|
||||
isCreatChatActive: itemPath === 'creatChat' && (currentRoute === 'kbCreatChat' || currentRoute === 'globalCreatChat'),
|
||||
isSettingsActive: itemPath === 'settings' && currentRoute === 'settings',
|
||||
isChatActive: itemPath === 'chat' && currentRoute === 'chat'
|
||||
};
|
||||
};
|
||||
@@ -187,7 +161,7 @@ const getIconActiveState = (itemPath: string) => {
|
||||
// 分离上下两部分菜单
|
||||
const topMenuItems = computed<MenuItem[]>(() => {
|
||||
return (menuArr.value as unknown as MenuItem[]).filter((item: MenuItem) =>
|
||||
item.path === 'knowledge-bases' || (isInKnowledgeBase.value && item.path === 'creatChat')
|
||||
item.path === 'knowledge-bases' || item.path === 'creatChat'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -430,13 +404,13 @@ let fileAddIcon = ref('file-add-green.svg');
|
||||
let knowledgeIcon = ref('zhishiku-green.svg');
|
||||
let prefixIcon = ref('prefixIcon.svg');
|
||||
let logoutIcon = ref('logout.svg');
|
||||
let tenantIcon = ref('user.svg'); // 使用专门的用户图标
|
||||
let settingIcon = ref('setting.svg'); // 设置图标
|
||||
let pathPrefix = ref(route.name)
|
||||
const getIcon = (path: string) => {
|
||||
// 根据当前路由状态更新所有图标
|
||||
const kbActiveState = getIconActiveState('knowledge-bases');
|
||||
const creatChatActiveState = getIconActiveState('creatChat');
|
||||
const tenantActiveState = getIconActiveState('tenant');
|
||||
const settingsActiveState = getIconActiveState('settings');
|
||||
|
||||
// 上传图标:只在知识库相关页面显示绿色
|
||||
fileAddIcon.value = kbActiveState.isKbActive ? 'file-add-green.svg' : 'file-add.svg';
|
||||
@@ -449,8 +423,8 @@ let pathPrefix = ref(route.name)
|
||||
kbActiveState.isKbActive ? 'prefixIcon-grey.svg' :
|
||||
'prefixIcon.svg';
|
||||
|
||||
// 租户图标:只在租户页面显示绿色
|
||||
tenantIcon.value = tenantActiveState.isTenantActive ? 'user-green.svg' : 'user.svg';
|
||||
// 设置图标:只在设置页面显示绿色
|
||||
settingIcon.value = settingsActiveState.isSettingsActive ? 'setting-green.svg' : 'setting.svg';
|
||||
|
||||
// 退出图标:始终显示默认
|
||||
logoutIcon.value = 'logout.svg';
|
||||
@@ -465,6 +439,10 @@ const handleMenuClick = async (path: string) => {
|
||||
} else {
|
||||
router.push('/platform/knowledge-bases')
|
||||
}
|
||||
} else if (path === 'settings') {
|
||||
// 设置菜单项:打开设置弹窗并跳转路由
|
||||
uiStore.openSettings()
|
||||
router.push('/platform/settings')
|
||||
} else {
|
||||
gotopage(path)
|
||||
}
|
||||
@@ -493,11 +471,14 @@ const gotopage = async (path: string) => {
|
||||
return;
|
||||
} else {
|
||||
if (path === 'creatChat') {
|
||||
// 尝试获取当前知识库ID
|
||||
const kbId = await getCurrentKbId()
|
||||
if (kbId) {
|
||||
// 如果在知识库内部,进入该知识库的对话页
|
||||
router.push(`/platform/knowledge-bases/${kbId}/creatChat`)
|
||||
} else {
|
||||
router.push(`/platform/knowledge-bases`)
|
||||
// 如果不在知识库内,也进入对话创建页,让用户通过 @ 按钮选择知识库
|
||||
router.push(`/platform/creatChat`)
|
||||
}
|
||||
} else {
|
||||
router.push(`/platform/${path}`);
|
||||
|
||||
Reference in New Issue
Block a user