refactor(agent): 优化智能体选择和初始化逻辑

移除未使用的侧边栏相关代码
修改默认智能体设置函数以接收agentId参数
优化AgentChatComponent的加载逻辑和监听处理
重构agent store的初始化流程并添加调试日志
This commit is contained in:
Wenjie Zhang 2025-08-31 12:22:49 +08:00
parent 221f4af213
commit b940b23b91
4 changed files with 38 additions and 64 deletions

View File

@ -146,6 +146,7 @@ import { storeToRefs } from 'pinia';
// ==================== PROPS & EMITS ====================
const props = defineProps({
state: { type: Object, default: () => ({}) },
agentId: { type: String, default: '' },
singleMode: { type: Boolean, default: true }
});
const emit = defineEmits(['open-config', 'open-agent-modal']);
@ -380,8 +381,15 @@ const initAll = async () => {
const loadChatsList = async () => {
try {
if (!AgentValidator.validateLoadOperation(agentStore.selectedAgentId, '加载对话列表')) return;
await agentStore.fetchThreads(agentStore.selectedAgentId);
const agentId = agentStore.selectedAgentId;
if (!AgentValidator.validateLoadOperation(agentId, '加载对话列表')) return;
await agentStore.fetchThreads(agentId);
// If selected agent changed during fetch, abort. The watcher will trigger a new load.
if (agentStore.selectedAgentId !== agentId) {
return;
}
if (currentAgentThreads.value && currentAgentThreads.value.length > 0) {
const threadToSelect = currentAgentThreads.value[0].id;
@ -398,20 +406,21 @@ const loadChatsList = async () => {
onMounted(async () => {
await initAll();
scrollController.enableAutoScroll();
watch(() => agentStore.selectedAgentId, (newAgentId, oldAgentId) => {
if (newAgentId && newAgentId !== oldAgentId) {
initAll();
}
});
// streaming
watch(conversations, () => {
if (isProcessing.value) {
scrollController.scrollToBottom();
}
}, { deep: true, flush: 'post' });
});
watch(() => agentStore.selectedAgentId, (newAgentId, oldAgentId) => {
if (newAgentId && newAgentId !== oldAgentId) {
loadChatsList();
}
});
// streaming
watch(conversations, () => {
if (isProcessing.value) {
scrollController.scrollToBottom();
}
}, { deep: true, flush: 'post' });
</script>
<style lang="less" scoped>

View File

@ -106,13 +106,20 @@ export const useAgentStore = defineStore('agent', {
if (this.isInitialized) return;
try {
// 首先加载智能体列表
await this.fetchAgents();
// 然后设置默认智能体
await this.fetchDefaultAgent();
// 最后加载工具
if (!this.selectedAgentId || !this.agents[this.selectedAgentId]) {
if (this.defaultAgentId && this.agents[this.defaultAgentId]) {
this.selectAgent(this.defaultAgentId);
} else if (Object.keys(this.agents).length > 0) {
const firstAgentId = Object.keys(this.agents)[0];
this.selectAgent(firstAgentId);
}
} else {
console.log('Condition FALSE: Persisted selected agent is valid. Keeping it.');
}
await this.fetchTools();
this.isInitialized = true;
@ -150,11 +157,6 @@ export const useAgentStore = defineStore('agent', {
try {
const response = await agentApi.getDefaultAgent();
this.defaultAgentId = response.default_agent_id;
// 如果没有选中的智能体且默认智能体存在于智能体列表中,则选择默认智能体
if (!this.selectedAgentId && this.defaultAgentId && this.agents[this.defaultAgentId]) {
this.selectedAgentId = this.defaultAgentId;
}
} catch (error) {
console.error('Failed to fetch default agent:', error);
handleChatError(error, 'fetch');

View File

@ -14,25 +14,9 @@ import { computed, ref } from 'vue';
import { useRoute } from 'vue-router';
import AgentChatComponent from '@/components/AgentChatComponent.vue';
import UserInfoComponent from '@/components/UserInfoComponent.vue';
import sidebarLeftIcon from '@/assets/icons/sidebar_left.svg';
import sidebarRightIcon from '@/assets/icons/sidebar_right.svg';
const route = useRoute();
const agentId = computed(() => route.params.agent_id);
//
const isSidebarCollapsed = ref(false);
// /
const toggleSidebar = () => {
isSidebarCollapsed.value = !isSidebarCollapsed.value;
};
//
const toggleUserInfo = () => {
//
console.log('用户信息图标被点击');
};
</script>
<style lang="less" scoped>

View File

@ -53,7 +53,7 @@
<div class="agent-card-title">
<span class="agent-card-name">{{ agent.name }}</span>
<StarFilled v-if="id === defaultAgentId" class="default-icon" />
<StarOutlined v-else @click.prevent="setAsDefaultAgent" class="default-icon" />
<StarOutlined v-else @click.prevent="setAsDefaultAgent(id)" class="default-icon" />
</div>
</div>
<div class="agent-card-description">{{ agent.description }}</div>
@ -132,11 +132,11 @@ const state = reactive({
// UI
//
const setAsDefaultAgent = async () => {
if (!selectedAgentId.value || !userStore.isAdmin) return;
const setAsDefaultAgent = async (agentId) => {
if (!agentId || !userStore.isAdmin) return;
try {
await agentStore.setDefaultAgent(selectedAgentId.value);
await agentStore.setDefaultAgent(agentId);
message.success('已将当前智能体设为默认');
} catch (error) {
console.error('设置默认智能体错误:', error);
@ -193,28 +193,7 @@ const selectAgentFromModal = (agentId) => {
// 使store
onMounted(async () => {
try {
//
const lastSelectedAgent = localStorage.getItem('last-selected-agent');
if (lastSelectedAgent && agents.value[lastSelectedAgent]) {
agentStore.selectAgent(lastSelectedAgent);
} else if (defaultAgentId.value && agents.value[defaultAgentId.value]) {
//
agentStore.selectAgent(defaultAgentId.value);
} else if (Object.keys(agents.value).length > 0) {
//
agentStore.selectAgent(Object.keys(agents.value)[0]);
}
//
await loadAgentConfig();
} catch (error) {
console.error('初始化失败:', error);
message.error('初始化失败');
}
});
//
const getConfigLabel = (key, value) => {