fix: abort creation chat sse on result page
This commit is contained in:
@@ -76,6 +76,9 @@ export function useRpgCreationSessionController(
|
||||
const hasRequestedInitialAgentWorkspaceAuthRef = useRef(false);
|
||||
const isAgentDraftResultAutoOpenSuppressedRef = useRef(false);
|
||||
const currentAgentSessionIdRef = useRef<string | null>(null);
|
||||
const activeAgentReplyAbortControllerRef = useRef<AbortController | null>(
|
||||
null,
|
||||
);
|
||||
const latestAgentSessionSyncRequestIdRef = useRef(0);
|
||||
|
||||
const [isCreatingAgentSession, setIsCreatingAgentSession] = useState(false);
|
||||
@@ -128,6 +131,11 @@ export function useRpgCreationSessionController(
|
||||
latestAgentSessionSyncRequestIdRef.current += 1;
|
||||
}, []);
|
||||
|
||||
const abortActiveAgentReplyStream = useCallback(() => {
|
||||
activeAgentReplyAbortControllerRef.current?.abort();
|
||||
activeAgentReplyAbortControllerRef.current = null;
|
||||
}, []);
|
||||
|
||||
const mergePendingAgentUserMessageIntoSession = useCallback(
|
||||
(
|
||||
session: CustomWorldAgentSessionSnapshot | null,
|
||||
@@ -223,8 +231,26 @@ export function useRpgCreationSessionController(
|
||||
setSelectionStage('agent-workspace');
|
||||
}, [enterCreateTab, openLoginModal, persistAgentUiState, setSelectionStage, userId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
selectionStage !== 'agent-workspace' &&
|
||||
selectionStage !== 'custom-world-generating'
|
||||
) {
|
||||
abortActiveAgentReplyStream();
|
||||
setStreamingAgentReplyText('');
|
||||
setIsStreamingAgentReply(false);
|
||||
}
|
||||
}, [abortActiveAgentReplyStream, selectionStage]);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
abortActiveAgentReplyStream();
|
||||
};
|
||||
}, [abortActiveAgentReplyStream]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeAgentSessionId) {
|
||||
abortActiveAgentReplyStream();
|
||||
invalidateAgentSessionSyncRequests();
|
||||
setAgentSession(null);
|
||||
setAgentOperation(null);
|
||||
@@ -238,6 +264,7 @@ export function useRpgCreationSessionController(
|
||||
}
|
||||
|
||||
if (!userId) {
|
||||
abortActiveAgentReplyStream();
|
||||
invalidateAgentSessionSyncRequests();
|
||||
setAgentSession(null);
|
||||
setAgentOperation(null);
|
||||
@@ -255,6 +282,7 @@ export function useRpgCreationSessionController(
|
||||
activeAgentSessionId === initialAgentUiStateRef.current.activeSessionId;
|
||||
|
||||
if (currentAgentSessionIdRef.current !== activeAgentSessionId) {
|
||||
abortActiveAgentReplyStream();
|
||||
setAgentSession(null);
|
||||
setAgentOperation(null);
|
||||
setStreamingAgentReplyText('');
|
||||
@@ -306,6 +334,7 @@ export function useRpgCreationSessionController(
|
||||
};
|
||||
}, [
|
||||
activeAgentSessionId,
|
||||
abortActiveAgentReplyStream,
|
||||
enterCreateTab,
|
||||
invalidateAgentSessionSyncRequests,
|
||||
persistAgentUiState,
|
||||
@@ -540,6 +569,8 @@ export function useRpgCreationSessionController(
|
||||
setStreamingAgentReplyText('');
|
||||
setIsStreamingAgentReply(true);
|
||||
setPendingAgentUserMessage(pendingMessagePayload);
|
||||
const replyAbortController = new AbortController();
|
||||
activeAgentReplyAbortControllerRef.current = replyAbortController;
|
||||
setAgentSession((current) =>
|
||||
mergePendingAgentUserMessageIntoSession(current, pendingMessagePayload),
|
||||
);
|
||||
@@ -550,10 +581,17 @@ export function useRpgCreationSessionController(
|
||||
payload,
|
||||
{
|
||||
onUpdate: (text) => {
|
||||
if (replyAbortController.signal.aborted) {
|
||||
return;
|
||||
}
|
||||
setStreamingAgentReplyText(text);
|
||||
},
|
||||
signal: replyAbortController.signal,
|
||||
},
|
||||
);
|
||||
if (replyAbortController.signal.aborted) {
|
||||
return;
|
||||
}
|
||||
const mergedNextSession = mergePendingAgentUserMessageIntoSession(
|
||||
nextSession,
|
||||
pendingMessagePayload,
|
||||
@@ -568,6 +606,9 @@ export function useRpgCreationSessionController(
|
||||
hasServerEchoedPendingMessage ? null : pendingMessagePayload,
|
||||
);
|
||||
} catch (error) {
|
||||
if (replyAbortController.signal.aborted) {
|
||||
return;
|
||||
}
|
||||
const errorMessage = resolveRpgCreationErrorMessage(
|
||||
error,
|
||||
'发送共创消息失败。',
|
||||
@@ -597,7 +638,12 @@ export function useRpgCreationSessionController(
|
||||
setStreamingAgentReplyText('');
|
||||
persistAgentUiState(activeAgentSessionId, null);
|
||||
} finally {
|
||||
setIsStreamingAgentReply(false);
|
||||
if (activeAgentReplyAbortControllerRef.current === replyAbortController) {
|
||||
activeAgentReplyAbortControllerRef.current = null;
|
||||
}
|
||||
if (!replyAbortController.signal.aborted) {
|
||||
setIsStreamingAgentReply(false);
|
||||
}
|
||||
}
|
||||
},
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user