refactor(agent): 优化智能体选择和初始化逻辑
移除未使用的侧边栏相关代码 修改默认智能体设置函数以接收agentId参数 优化AgentChatComponent的加载逻辑和监听处理 重构agent store的初始化流程并添加调试日志
This commit is contained in:
parent
221f4af213
commit
b940b23b91
@ -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>
|
||||
|
||||
@ -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');
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -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) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user