fix(agent): 修复智能体重复初始化和工具重复加载问题

- 修复智能体初始化时的并发问题,添加isInitializing状态防止重复初始化
- 优化工具加载逻辑,避免重复请求
- 移除AgentView中多余的watch监听
This commit is contained in:
Wenjie Zhang 2025-12-21 16:23:19 +08:00
parent 7df10afba7
commit a9d2f41e43
4 changed files with 13 additions and 8 deletions

View File

@ -991,7 +991,7 @@ const initAll = async () => {
onMounted(async () => {
await initAll();
scrollController.enableAutoScroll();
});
});
watch(currentAgentId, async (newAgentId, oldAgentId) => {
if (newAgentId !== oldAgentId) {

View File

@ -423,6 +423,10 @@ const getToolNameById = (toolId) => {
const loadAvailableTools = async () => {
try {
//
if (availableTools.value && Object.keys(availableTools.value).length > 0) {
return;
}
await agentStore.fetchTools();
} catch (error) {
console.error('加载工具列表失败:', error);

View File

@ -33,6 +33,7 @@ export const useAgentStore = defineStore('agent', () => {
// 初始化状态
const isInitialized = ref(false)
const isInitializing = ref(false)
// ==================== 计算属性 ====================
const selectedAgent = computed(() =>
@ -76,6 +77,10 @@ export const useAgentStore = defineStore('agent', () => {
async function initialize() {
if (isInitialized.value) return
// 防止并发初始化
if (isInitializing.value) return
isInitializing.value = true
try {
await fetchAgents()
await fetchDefaultAgent()
@ -111,6 +116,8 @@ export const useAgentStore = defineStore('agent', () => {
console.error('Failed to initialize agent store:', err)
handleChatError(err, 'initialize')
error.value = err.message
} finally {
isInitializing.value = false
}
}
@ -321,6 +328,7 @@ export const useAgentStore = defineStore('agent', () => {
isLoadingAgentDetail.value = false
error.value = null
isInitialized.value = false
isInitializing.value = false
}
return {

View File

@ -165,13 +165,6 @@ const loadAgentConfig = async () => {
}
};
//
watch(
() => selectedAgentId.value,
() => {
loadAgentConfig();
}
);
// 使store
const selectAgent = (agentId) => {