ForcePilot/web/src/components/KnowledgeBaseCard.vue

331 lines
8.6 KiB
Vue
Raw Normal View History

<template>
<div class="knowledge-base-card">
<!-- 标题栏 -->
<div class="card-header">
<div class="header-left">
<a-button
@click="backToDatabase"
class="back-button"
shape="circle"
:icon="h(LeftOutlined)"
type="text"
size="small"
></a-button>
<h3 class="card-title">{{ database.name || '数据库信息加载中' }}</h3>
</div>
<div class="header-right">
<a-button
type="text"
size="small"
@click="copyDatabaseId"
title="复制知识库ID"
>
<template #icon>
<Copy :size="14" />
</template>
</a-button>
<a-button
@click="showEditModal"
type="text"
size="small"
>
<template #icon>
<Pencil :size="14" />
</template>
</a-button>
</div>
</div>
<!-- 卡片内容 -->
<div class="card-content">
<!-- 描述文本 -->
<div class="description">
<p class="description-text">{{ database.description || '暂无描述' }}</p>
</div>
<!-- Tags -->
<div class="tags-section">
<a-tag
:color="getKbTypeColor(database.kb_type || 'lightrag')"
size="small"
>
{{ getKbTypeLabel(database.kb_type || 'lightrag') }}
</a-tag>
<a-tag color="blue" size="small">{{ database.embed_info?.name || 'N/A' }}</a-tag>
</div>
</div>
</div>
<!-- 编辑对话框 -->
<a-modal v-model:open="editModalVisible" title="编辑知识库信息">
<template #footer>
<a-button danger @click="deleteDatabase" style="margin-right: auto; margin-left: 0;">
<template #icon>
<Trash2 :size="16" style="vertical-align: -3px; margin-right: 4px;" />
</template>
删除数据库
</a-button>
<a-button key="back" @click="editModalVisible = false">取消</a-button>
<a-button key="submit" type="primary" @click="handleEditSubmit">确定</a-button>
</template>
<a-form :model="editForm" :rules="rules" ref="editFormRef" layout="vertical">
<a-form-item label="知识库名称" name="name" required>
<a-input v-model:value="editForm.name" placeholder="请输入知识库名称" />
</a-form-item>
<a-form-item label="知识库描述" name="description">
<AiTextarea
v-model="editForm.description"
:name="editForm.name"
:files="fileList"
placeholder="请输入知识库描述"
:rows="4"
/>
</a-form-item>
<a-form-item label="自动生成问题" name="auto_generate_questions">
<a-switch v-model:checked="editForm.auto_generate_questions" checked-children="开启" un-checked-children="关闭" />
<span style="margin-left: 8px; font-size: 12px; color: var(--gray-500);">上传文件后自动生成测试问题</span>
</a-form-item>
<!-- 仅对 LightRAG 类型显示 LLM 配置 -->
<a-form-item v-if="database.kb_type === 'lightrag'" label="语言模型 (LLM)" name="llm_info">
<ModelSelectorComponent
:model_spec="llmModelSpec"
placeholder="请选择模型"
@select-model="handleLLMSelect"
style="width: 100%;"
/>
</a-form-item>
</a-form>
</a-modal>
</template>
<script setup>
import { ref, reactive, computed, h } from 'vue';
import { useRouter } from 'vue-router';
import { useDatabaseStore } from '@/stores/database';
import { getKbTypeLabel, getKbTypeColor } from '@/utils/kb_utils';
import { message } from 'ant-design-vue';
import { LeftOutlined } from '@ant-design/icons-vue';
import {
Pencil,
Trash2,
Copy,
} from 'lucide-vue-next';
import ModelSelectorComponent from '@/components/ModelSelectorComponent.vue';
import AiTextarea from '@/components/AiTextarea.vue';
const router = useRouter();
const store = useDatabaseStore();
const database = computed(() => store.database);
const fileList = computed(() => {
if (!database.value?.files) return [];
return Object.values(database.value.files).map(f => f.filename).filter(Boolean);
});
// 复制数据库ID
const copyDatabaseId = async () => {
if (!database.value.db_id) {
message.warning('知识库ID为空');
return;
}
try {
await navigator.clipboard.writeText(database.value.db_id);
message.success('知识库ID已复制到剪贴板');
} catch (err) {
// 降级方案
const textArea = document.createElement('textarea');
textArea.value = database.value.db_id;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
message.success('知识库ID已复制到剪贴板');
}
};
// 格式化日期
const formatDate = (dateStr) => {
if (!dateStr) return 'N/A';
const date = new Date(dateStr);
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
};
// 返回数据库列表
const backToDatabase = () => {
router.push('/database');
};
// 编辑相关逻辑(复用自 DatabaseHeader
const editModalVisible = ref(false);
const editFormRef = ref(null);
const editForm = reactive({
name: '',
description: '',
auto_generate_questions: false,
llm_info: {
provider: '',
model_name: ''
}
});
const rules = {
name: [{ required: true, message: '请输入知识库名称' }]
};
const showEditModal = () => {
editForm.name = database.value.name || '';
editForm.description = database.value.description || '';
editForm.auto_generate_questions = database.value.additional_params?.auto_generate_questions || false;
// 如果是 LightRAG 类型,加载当前的 LLM 配置
if (database.value.kb_type === 'lightrag') {
const llmInfo = database.value.llm_info || {};
editForm.llm_info.provider = llmInfo.provider || '';
editForm.llm_info.model_name = llmInfo.model_name || '';
}
editModalVisible.value = true;
};
const handleEditSubmit = () => {
editFormRef.value.validate().then(async () => {
const updateData = {
name: editForm.name,
description: editForm.description,
additional_params: {
auto_generate_questions: editForm.auto_generate_questions
}
};
// 如果是 LightRAG 类型,包含 llm_info
if (database.value.kb_type === 'lightrag') {
updateData.llm_info = {
provider: editForm.llm_info.provider,
model_name: editForm.llm_info.model_name
};
}
await store.updateDatabaseInfo(updateData);
editModalVisible.value = false;
}).catch(err => {
console.error('表单验证失败:', err);
});
};
// LLM 模型选择处理
const llmModelSpec = computed(() => {
const provider = editForm.llm_info?.provider || '';
const modelName = editForm.llm_info?.model_name || '';
if (provider && modelName) {
return `${provider}/${modelName}`;
}
return '';
});
const handleLLMSelect = (spec) => {
console.log('LLM选择:', spec);
if (typeof spec !== 'string' || !spec) return;
const index = spec.indexOf('/');
const provider = index !== -1 ? spec.slice(0, index) : '';
const modelName = index !== -1 ? spec.slice(index + 1) : '';
editForm.llm_info.provider = provider;
editForm.llm_info.model_name = modelName;
};
const deleteDatabase = () => {
store.deleteDatabase();
};
</script>
<style lang="less" scoped>
.knowledge-base-card {
Add dark/light theme toggle feature (#343) * Add dark/light theme toggle feature * update: refine dark/light theme and related components * 调整语义化主题变量,优化暗/亮模式切换效果 * Revert "调整语义化主题变量,优化暗/亮模式切换效果" This reverts commit 85d9373297686fdbacb252a880b2847d20a39641. * 深色模式适配:减少 !important,迁移 CSS 变量,优化布局 * style: 替换硬编码颜色值为CSS变量以支持主题切换 将多处硬编码的颜色值(如#fff、#f0f0f0等)替换为CSS变量(如--gray-0、--gray-150等),统一管理颜色样式,便于主题切换和样式维护。主要修改包括背景色、边框色、文字色等视觉元素,同时移除不再需要的深色模式适配代码。 * style: 使用CSS变量替换硬编码颜色值 * refactor(theme): 重构主题系统,统一使用CSS变量并优化暗色模式实现 - 移除冗余的theme.js配置文件,将主题配置集中到theme store - 使用ant-design-vue的darkAlgorithm实现暗色模式 - 在多个组件中替换硬编码颜色值为CSS变量 - 优化用户信息组件,整合文档中心和主题切换功能 - 更新基础CSS样式,完善颜色系统和滚动条样式 * reset: 恢复被覆盖的修改(96d257a7ace38c94e2b113d56472d211d1141087) * feat(theme): 实现深色主题支持并重构样式系统 重构颜色变量系统,添加深色主题支持 移除硬编码颜色值,统一使用CSS变量 优化图表组件以响应主题切换 清理无用样式代码,提升可维护性 * docs: 更新开发规范文档 * style: 调整边框和背景颜色样式 --------- Co-authored-by: Wenjie Zhang <xerrors@163.com>
2025-11-23 01:39:44 +08:00
background: linear-gradient(120deg, var(--main-30) 0%, var(--gray-0) 100%);
border-radius: 12px;
border: 1px solid var(--gray-200);
margin-bottom: 8px;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 16px;
.header-left {
display: flex;
align-items: center;
gap: 4px;
flex: 1;
min-width: 0;
button.back-button {
margin-left: -5px;
font-size: 10px;
}
}
.card-title {
font-size: 16px;
font-weight: 600;
color: var(--gray-800);
margin: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
// flex: 1;
}
.header-right {
display: flex;
align-items: center;
gap: 6px;
flex-shrink: 0;
button {
color: var(--gray-500);
height: 100%;
}
button:hover {
color: var(--gray-700);
background-color: var(--gray-100);
}
}
}
.card-content {
padding: 0 16px 16px 16px;
}
.description {
margin-bottom: 12px;
.description-text {
font-size: 14px;
color: var(--gray-700);
line-height: 1.5;
margin: 0;
}
}
.tags-section {
display: flex;
gap: 6px;
align-items: center;
flex-wrap: wrap;
}
</style>