fix(agent): 修复智能体重复初始化和工具重复加载问题
- 修复智能体初始化时的并发问题,添加isInitializing状态防止重复初始化 - 优化工具加载逻辑,避免重复请求 - 移除AgentView中多余的watch监听
This commit is contained in:
parent
7df10afba7
commit
a9d2f41e43
@ -991,7 +991,7 @@ const initAll = async () => {
|
|||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await initAll();
|
await initAll();
|
||||||
scrollController.enableAutoScroll();
|
scrollController.enableAutoScroll();
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(currentAgentId, async (newAgentId, oldAgentId) => {
|
watch(currentAgentId, async (newAgentId, oldAgentId) => {
|
||||||
if (newAgentId !== oldAgentId) {
|
if (newAgentId !== oldAgentId) {
|
||||||
|
|||||||
@ -423,6 +423,10 @@ const getToolNameById = (toolId) => {
|
|||||||
|
|
||||||
const loadAvailableTools = async () => {
|
const loadAvailableTools = async () => {
|
||||||
try {
|
try {
|
||||||
|
// 避免重复加载,如果已经有工具就直接返回
|
||||||
|
if (availableTools.value && Object.keys(availableTools.value).length > 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
await agentStore.fetchTools();
|
await agentStore.fetchTools();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('加载工具列表失败:', error);
|
console.error('加载工具列表失败:', error);
|
||||||
|
|||||||
@ -33,6 +33,7 @@ export const useAgentStore = defineStore('agent', () => {
|
|||||||
|
|
||||||
// 初始化状态
|
// 初始化状态
|
||||||
const isInitialized = ref(false)
|
const isInitialized = ref(false)
|
||||||
|
const isInitializing = ref(false)
|
||||||
|
|
||||||
// ==================== 计算属性 ====================
|
// ==================== 计算属性 ====================
|
||||||
const selectedAgent = computed(() =>
|
const selectedAgent = computed(() =>
|
||||||
@ -76,6 +77,10 @@ export const useAgentStore = defineStore('agent', () => {
|
|||||||
async function initialize() {
|
async function initialize() {
|
||||||
if (isInitialized.value) return
|
if (isInitialized.value) return
|
||||||
|
|
||||||
|
// 防止并发初始化
|
||||||
|
if (isInitializing.value) return
|
||||||
|
isInitializing.value = true
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fetchAgents()
|
await fetchAgents()
|
||||||
await fetchDefaultAgent()
|
await fetchDefaultAgent()
|
||||||
@ -111,6 +116,8 @@ export const useAgentStore = defineStore('agent', () => {
|
|||||||
console.error('Failed to initialize agent store:', err)
|
console.error('Failed to initialize agent store:', err)
|
||||||
handleChatError(err, 'initialize')
|
handleChatError(err, 'initialize')
|
||||||
error.value = err.message
|
error.value = err.message
|
||||||
|
} finally {
|
||||||
|
isInitializing.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -321,6 +328,7 @@ export const useAgentStore = defineStore('agent', () => {
|
|||||||
isLoadingAgentDetail.value = false
|
isLoadingAgentDetail.value = false
|
||||||
error.value = null
|
error.value = null
|
||||||
isInitialized.value = false
|
isInitialized.value = false
|
||||||
|
isInitializing.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -165,13 +165,6 @@ const loadAgentConfig = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 监听智能体选择变化
|
|
||||||
watch(
|
|
||||||
() => selectedAgentId.value,
|
|
||||||
() => {
|
|
||||||
loadAgentConfig();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// 选择智能体(使用store方法)
|
// 选择智能体(使用store方法)
|
||||||
const selectAgent = (agentId) => {
|
const selectAgent = (agentId) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user