|
45 | 45 | <div class="header__right"> |
46 | 46 | <!-- AgentState 显示按钮 - 只在智能体支持 todo 或 files 能力时显示 --> |
47 | 47 | <AgentPopover |
| 48 | + v-if="hasAgentStateContent" |
48 | 49 | v-model:visible="agentStatePopoverVisible" |
49 | 50 | :agent-state="currentAgentState" |
| 51 | + @refresh="handleAgentStateRefresh" |
50 | 52 | > |
51 | 53 | <div |
52 | 54 | class="agent-nav-btn agent-state-btn" |
@@ -369,18 +371,28 @@ const currentAgentState = computed(() => { |
369 | 371 | return currentChatId.value ? getThreadState(currentChatId.value)?.agentState || null : null; |
370 | 372 | }); |
371 | 373 |
|
| 374 | +const countFiles = (files) => { |
| 375 | + if (!Array.isArray(files)) return 0; |
| 376 | + let c = 0; |
| 377 | + for (const item of files) { |
| 378 | + if (item && typeof item === 'object') c += Object.keys(item).length; |
| 379 | + } |
| 380 | + return c; |
| 381 | +}; |
| 382 | +
|
372 | 383 | const hasAgentStateContent = computed(() => { |
373 | | - const agentState = currentAgentState.value; |
374 | | - if (!agentState) return false; |
375 | | - return (agentState.todos && agentState.todos.length > 0) || |
376 | | - (agentState.files && Object.keys(agentState.files).length > 0); |
| 384 | + const s = currentAgentState.value; |
| 385 | + if (!s) return false; |
| 386 | + const todoCount = Array.isArray(s.todos) ? s.todos.length : 0; |
| 387 | + const fileCount = countFiles(s.files); |
| 388 | + return todoCount > 0 || fileCount > 0; |
377 | 389 | }); |
378 | 390 |
|
379 | 391 | const totalAgentStateItems = computed(() => { |
380 | | - const agentState = currentAgentState.value; |
381 | | - if (!agentState) return 0; |
382 | | - const todoCount = agentState.todos ? agentState.todos.length : 0; |
383 | | - const fileCount = agentState.files ? Object.keys(agentState.files).length : 0; |
| 392 | + const s = currentAgentState.value; |
| 393 | + if (!s) return 0; |
| 394 | + const todoCount = Array.isArray(s.todos) ? s.todos.length : 0; |
| 395 | + const fileCount = countFiles(s.files); |
384 | 396 | return todoCount + fileCount; |
385 | 397 | }); |
386 | 398 |
|
@@ -756,6 +768,15 @@ const fetchThreadMessages = async ({ agentId, threadId, delay = 0 }) => { |
756 | 768 | } |
757 | 769 | }; |
758 | 770 |
|
| 771 | +const fetchAgentState = async (agentId, threadId) => { |
| 772 | + if (!agentId || !threadId) return; |
| 773 | + try { |
| 774 | + const res = await agentApi.getAgentState(agentId, threadId); |
| 775 | + const ts = getThreadState(threadId); |
| 776 | + if (ts) ts.agentState = res.agent_state || null; |
| 777 | + } catch (error) {} |
| 778 | +}; |
| 779 | +
|
759 | 780 | const loadThreadAttachments = async (threadId, { silent = false } = {}) => { |
760 | 781 | if (!threadId) return; |
761 | 782 | try { |
@@ -954,6 +975,7 @@ const selectChat = async (chatId) => { |
954 | 975 | try { |
955 | 976 | await fetchThreadMessages({ agentId: currentAgentId.value, threadId: chatId }); |
956 | 977 | await loadThreadAttachments(chatId, { silent: true }); |
| 978 | + await fetchAgentState(currentAgentId.value, chatId); |
957 | 979 | } catch (error) { |
958 | 980 | handleChatError(error, 'load'); |
959 | 981 | } finally { |
@@ -1240,6 +1262,11 @@ const toggleSidebar = () => { |
1240 | 1262 | }; |
1241 | 1263 | const openAgentModal = () => emit('open-agent-modal'); |
1242 | 1264 |
|
| 1265 | +const handleAgentStateRefresh = async () => { |
| 1266 | + if (!currentAgentId.value || !currentChatId.value) return; |
| 1267 | + await fetchAgentState(currentAgentId.value, currentChatId.value); |
| 1268 | +}; |
| 1269 | +
|
1243 | 1270 | // ==================== HELPER FUNCTIONS ==================== |
1244 | 1271 | const getLastMessage = (conv) => { |
1245 | 1272 | if (!conv?.messages?.length) return null; |
|
0 commit comments