feat: Add agent configuration and cleanup scripts for database migrations

This commit is contained in:
wizardchen
2025-11-05 23:18:44 +08:00
parent ae9d0778e9
commit 4fa3adbf3b
126 changed files with 22506 additions and 629 deletions
+47 -83
View File
@@ -2,113 +2,77 @@
<div class="dialogue-wrap">
<div class="dialogue-answers">
<div class="dialogue-title">
<span>基于知识库内容问答</span>
<span>基于知识库内容问答 - AI 问答</span>
</div>
<InputField @send-msg="sendMsg"></InputField>
</div>
</div>
<t-dialog v-model:visible="selectVisible" header="选择知识库" :confirmBtn="{ content: '开始对话', theme: 'primary' }" :onConfirm="confirmSelect" :onCancel="() => selectVisible = false">
<t-form :data="{ kb: selectedKbId }">
<t-form-item label="知识库">
<t-select v-model="selectedKbId" :loading="kbLoading" placeholder="请选择知识库">
<t-option v-for="kb in kbList" :key="kb.id" :value="kb.id" :label="kb.name" />
</t-select>
</t-form-item>
</t-form>
</t-dialog>
</template>
<script setup lang="ts">
import { ref, onUnmounted, watch } from 'vue';
import { ref } from 'vue';
import InputField from '@/components/Input-field.vue';
import EmptyKnowledge from '@/components/empty-knowledge.vue';
import { getSessionsList, createSessions, generateSessionsTitle } from "@/api/chat/index";
import { createSessions } from "@/api/chat/index";
import { useMenuStore } from '@/stores/menu';
import { useSettingsStore } from '@/stores/settings';
import { useRoute, useRouter } from 'vue-router';
import useKnowledgeBase from '@/hooks/useKnowledgeBase';
import { listKnowledgeBases } from '@/api/knowledge-base';
import { MessagePlugin } from 'tdesign-vue-next';
let { cardList } = useKnowledgeBase()
const router = useRouter();
const route = useRoute();
const usemenuStore = useMenuStore();
const settingsStore = useSettingsStore();
const sendMsg = (value: string) => {
createNewSession(value);
}
const selectVisible = ref(false)
const selectedKbId = ref<string>('')
const kbList = ref<Array<{ id: string; name: string }>>([])
const kbLoading = ref(false)
const ensureKbId = async (): Promise<string | null> => {
// 1) 优先使用当前路由上下文(如果来自某个知识库详情页)
const routeKb = (route.params as any)?.kbId as string
if (routeKb) return routeKb
// 3) 弹窗选择知识库(从接口拉取)
kbLoading.value = true
try {
const res: any = await listKnowledgeBases()
kbList.value = res?.data || []
if (kbList.value.length === 0) return null
selectedKbId.value = kbList.value[0].id
selectVisible.value = true
return null
} finally {
kbLoading.value = false
}
}
async function createNewSession(value: string) {
let knowledgeBaseId = await ensureKbId()
if (!knowledgeBaseId) {
// 等待用户在弹窗中选择
pendingValue.value = value
return
}
createSessions({ knowledge_base_id: knowledgeBaseId }).then(async res => {
if (res.data && res.data.id) {
await getTitle(res.data.id, value)
} else {
// 错误处理
console.error("创建会话失败");
}
}).catch(error => {
console.error("创建会话出错:", error);
})
}
const pendingValue = ref<string>('')
const confirmSelect = async () => {
if (!selectedKbId.value) return
const value = pendingValue.value
pendingValue.value = ''
selectVisible.value = false
createSessions({ knowledge_base_id: selectedKbId.value }).then(async res => {
if (res.data && res.data.id) {
await getTitle(res.data.id, value, selectedKbId.value)
} else {
console.error('创建会话失败')
}
}).catch((e:any) => console.error('创建会话出错:', e))
}
const getTitle = async (session_id: string, value: string, kbId?: string) => {
const finalKbId = kbId || await ensureKbId();
if (!finalKbId) {
console.error('无法获取知识库ID');
const selectedKbs = settingsStore.settings.selectedKnowledgeBases;
if (!selectedKbs || selectedKbs.length === 0) {
MessagePlugin.warning('请先选择知识库');
return;
}
// 构建 session 数据,包含 Agent 配置
const sessionData: any = {};
let obj = { title: '新会话', path: `chat/${finalKbId}/${session_id}`, id: session_id, isMore: false, isNoTitle: true }
// 添加 Agent 配置(知识库信息在 agent_config 中)
sessionData.agent_config = {
enabled: true,
max_iterations: settingsStore.agentConfig.maxIterations,
temperature: settingsStore.agentConfig.temperature,
thinking_model_id: settingsStore.agentConfig.thinkingModelId,
knowledge_bases: selectedKbs, // 所有选中的知识库
allowed_tools: settingsStore.agentConfig.allowedTools
};
try {
const res = await createSessions(sessionData);
if (res.data && res.data.id) {
await navigateToSession(res.data.id, value, selectedKbs[0]);
} else {
console.error("创建会话失败");
MessagePlugin.error("创建会话失败");
}
} catch (error) {
console.error("创建会话出错:", error);
MessagePlugin.error("创建会话失败,请稍后重试");
}
}
const navigateToSession = async (sessionId: string, value: string, kbId: string) => {
let obj = {
title: '新会话',
path: `chat/${kbId}/${sessionId}`,
id: sessionId,
isMore: false,
isNoTitle: true
};
usemenuStore.updataMenuChildren(obj);
usemenuStore.changeIsFirstSession(true);
usemenuStore.changeFirstQuery(value);
router.push(`/platform/chat/${finalKbId}/${session_id}`);
router.push(`/platform/chat/${kbId}/${sessionId}`);
}
</script>