/** * features/chat/composables/useChatRouteSync.ts * 路由与 Agent 切换时的状态同步、挂载/卸载生命周期。 */ import { onBeforeUnmount, onMounted, watch, type ComputedRef, type Ref } from 'vue'; import { setLastChatAgentId } from '@/shared/utils/lastChatAgent'; import { registerOpenAgentId } from '@/shared/utils/agentTabs'; import { workspacePathFromLegacyView } from '@/features/workspace/constants/routes'; import type { ChatDeps } from './chat.deps'; export interface UseChatRouteSyncOptions { deps: ChatDeps; currentAgentId: ComputedRef; presetQuestion: ComputedRef; shippingTaskIdFromQuery: ComputedRef; shippingBatchNoFromQuery?: ComputedRef; isShippingNoticeAgent: ComputedRef; resetChatStateForAgentChange: () => void; cancelVoiceInput: () => void; syncCurrentUserProfile: () => Promise; loadAgent: () => Promise; loadUserAgents: () => Promise; loadHistory: () => Promise; loadConversation: (id: string, options?: { force?: boolean }) => Promise; resumeShippingTaskFromQuery: () => Promise; handleSend: (textOrEvent?: string | Event) => Promise; createNewChatView: () => void; onAuthLogout: () => void; onViewportResize: () => void; disposePeripherals: () => void; scheduleScrollToBottom: () => void; onMountedExtra?: () => void | Promise; input: Ref; } export function useChatRouteSync(options: UseChatRouteSyncOptions) { const { deps: { route, router }, currentAgentId, presetQuestion, shippingTaskIdFromQuery, shippingBatchNoFromQuery, resetChatStateForAgentChange, cancelVoiceInput, syncCurrentUserProfile, loadAgent, loadUserAgents, loadHistory, loadConversation, resumeShippingTaskFromQuery, handleSend, createNewChatView, onAuthLogout, onViewportResize, disposePeripherals, scheduleScrollToBottom, onMountedExtra, input, } = options; let routeSyncToken = 0; const syncOpenAgentRegistration = () => { const id = Number(route.params.agentId); registerOpenAgentId(Number.isFinite(id) && id > 0 ? id : null); }; const onPageHide = () => { registerOpenAgentId(null); }; const goToWorkspace = (view?: string) => { setLastChatAgentId(currentAgentId.value); registerOpenAgentId(null); router.push({ path: workspacePathFromLegacyView(view), query: { no_redirect: '1' } }); }; const syncChatStateForRoute = async () => { const agentId = currentAgentId.value; if (!agentId) return; const syncToken = ++routeSyncToken; resetChatStateForAgentChange(); cancelVoiceInput(); setLastChatAgentId(agentId); syncOpenAgentRegistration(); await syncCurrentUserProfile(); if (syncToken !== routeSyncToken) return; await loadAgent(); if (syncToken !== routeSyncToken) return; await loadUserAgents(); if (syncToken !== routeSyncToken) return; const convId = route.query.conversation_id as string; // 历史列表是辅助内容;直达会话时不能阻塞消息/货代 Projection 的首屏读取。 const historyPromise = loadHistory(); if (convId) { await Promise.all([ historyPromise, loadConversation(convId, { force: true }), ]); if (syncToken !== routeSyncToken) return; } else if (shippingTaskIdFromQuery.value || shippingBatchNoFromQuery?.value) { await historyPromise; if (syncToken !== routeSyncToken) return; await resumeShippingTaskFromQuery(); if (syncToken !== routeSyncToken) return; } else { await historyPromise; if (syncToken !== routeSyncToken) return; } if (presetQuestion.value) { input.value = presetQuestion.value; await handleSend(); } }; onMounted(async () => { window.addEventListener('pagehide', onPageHide); window.addEventListener('hxops:logout', onAuthLogout); window.addEventListener('resize', onViewportResize); await syncChatStateForRoute(); await onMountedExtra?.(); scheduleScrollToBottom(); }); watch( () => [route.params.agentId, route.query.conversation_id, route.query.preset, route.query.task_id, route.query.batch_no], async ( [nextAgentId, nextConversationId, nextPreset, nextTaskId, nextBatchNo], [prevAgentId, prevConversationId, prevPreset, prevTaskId, prevBatchNo], ) => { if ( nextAgentId === prevAgentId && nextConversationId === prevConversationId && nextPreset === prevPreset && nextTaskId === prevTaskId && nextBatchNo === prevBatchNo ) { return; } if ( nextAgentId === prevAgentId && nextPreset === prevPreset && nextTaskId === prevTaskId && nextBatchNo === prevBatchNo && nextConversationId !== prevConversationId ) { if (typeof nextConversationId === 'string' && nextConversationId) { await loadConversation(nextConversationId, { force: true }); } else { createNewChatView(); } return; } await syncChatStateForRoute(); }, ); onBeforeUnmount(() => { window.removeEventListener('pagehide', onPageHide); window.removeEventListener('hxops:logout', onAuthLogout); window.removeEventListener('resize', onViewportResize); disposePeripherals(); }); return { goToWorkspace, syncChatStateForRoute, }; }