fix: abort creation chat sse on result page

This commit is contained in:
2026-04-24 20:33:01 +08:00
parent 3aabb59945
commit 88bd4f36e9
5 changed files with 82 additions and 1 deletions

View File

@@ -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);
}
}
},
[

View File

@@ -52,6 +52,7 @@ export interface StoryRequestOptions {
export interface TextStreamOptions {
onUpdate?: (text: string) => void;
signal?: AbortSignal;
}
export interface CustomWorldSceneImageRequest {

View File

@@ -66,6 +66,7 @@ export async function streamRpgCreationMessage(
`/api/runtime${RPG_AGENT_API_BASE}/${encodeURIComponent(sessionId)}/messages/stream`,
payload,
'发送共创消息失败',
options.signal,
);
return readCreationAgentSessionFromSse<RpgAgentSessionSnapshot>(response, {

View File

@@ -21,11 +21,13 @@ export async function openRpgCreationSsePost(
url: string,
payload: unknown,
fallbackMessage: string,
signal?: AbortSignal,
) {
const response = await fetchWithApiAuth(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
signal,
});
if (!response.ok) {