164 lines
4.8 KiB
TypeScript
164 lines
4.8 KiB
TypeScript
/**
|
|
* 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<number>;
|
|
presetQuestion: ComputedRef<string>;
|
|
shippingTaskIdFromQuery: ComputedRef<string | null>;
|
|
isShippingNoticeAgent: ComputedRef<boolean>;
|
|
resetChatStateForAgentChange: () => void;
|
|
cancelVoiceInput: () => void;
|
|
syncCurrentUserProfile: () => Promise<void>;
|
|
loadAgent: () => Promise<void>;
|
|
loadUserAgents: () => Promise<void>;
|
|
loadHistory: () => Promise<void>;
|
|
loadConversation: (id: string, options?: { force?: boolean }) => Promise<void>;
|
|
resumeShippingTaskFromQuery: () => Promise<void>;
|
|
handleSend: (textOrEvent?: string | Event) => Promise<void>;
|
|
createNewChatView: () => void;
|
|
onAuthLogout: () => void;
|
|
onViewportResize: () => void;
|
|
disposePeripherals: () => void;
|
|
scheduleScrollToBottom: () => void;
|
|
onMountedExtra?: () => void | Promise<void>;
|
|
input: Ref<string>;
|
|
}
|
|
|
|
export function useChatRouteSync(options: UseChatRouteSyncOptions) {
|
|
const {
|
|
deps: { route, router },
|
|
currentAgentId,
|
|
presetQuestion,
|
|
shippingTaskIdFromQuery,
|
|
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;
|
|
|
|
await loadHistory();
|
|
if (syncToken !== routeSyncToken) return;
|
|
|
|
const convId = route.query.conversation_id as string;
|
|
if (convId) {
|
|
await loadConversation(convId, { force: true });
|
|
if (syncToken !== routeSyncToken) return;
|
|
} else if (shippingTaskIdFromQuery.value) {
|
|
await resumeShippingTaskFromQuery();
|
|
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],
|
|
async ([nextAgentId, nextConversationId, nextPreset, nextTaskId], [prevAgentId, prevConversationId, prevPreset, prevTaskId]) => {
|
|
if (
|
|
nextAgentId === prevAgentId
|
|
&& nextConversationId === prevConversationId
|
|
&& nextPreset === prevPreset
|
|
&& nextTaskId === prevTaskId
|
|
) {
|
|
return;
|
|
}
|
|
|
|
if (
|
|
nextAgentId === prevAgentId
|
|
&& nextPreset === prevPreset
|
|
&& nextTaskId === prevTaskId
|
|
&& 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,
|
|
};
|
|
}
|