refactor: 收口草稿打开状态规则

This commit is contained in:
2026-06-04 05:51:09 +08:00
parent b037ce1e32
commit 217cc881e6
6 changed files with 589 additions and 180 deletions

View File

@@ -12,6 +12,7 @@ import {
type CreationWorkShelfItem,
type CreationWorkShelfKind,
type CreationWorkShelfRuntimeState,
isPersistedPuzzleDraftGenerating,
resolvePuzzleWorkCoverImageSrc,
} from '../custom-world-home/creationWorkShelf';
import {
@@ -67,6 +68,86 @@ export type PlatformDraftGenerationVisibleShelfSources = {
babyObjectMatchItems: readonly BabyObjectMatchDraft[];
};
type DraftOpenGenerationFacts = {
activeSessionId?: string | null;
hasActiveGenerationFailure: boolean;
hasActiveGenerationRunning: boolean;
hasBackgroundGenerationFailure: boolean;
hasBackgroundGenerationRunning: boolean;
};
type FailedDraftGenerationSource = 'background' | 'active' | 'restored';
export type PuzzleDraftOpenIntent =
| {
type: 'open-published-detail';
noticeKeys: string[];
}
| {
type: 'missing-session';
noticeKeys: string[];
errorMessage: string;
}
| {
type: 'failed-generation';
noticeKeys: string[];
errorMessage: string;
source: FailedDraftGenerationSource;
}
| {
type: 'active-generation';
noticeKeys: string[];
}
| {
type: 'background-generation';
noticeKeys: string[];
}
| {
type: 'restore-generating';
noticeKeys: string[];
}
| {
type: 'restore-draft';
noticeKeys: string[];
};
export type Match3DDraftOpenIntent =
| {
type: 'open-published-detail';
noticeKeys: string[];
}
| {
type: 'missing-session';
noticeKeys: string[];
errorMessage: string;
}
| {
type: 'ready-unread';
noticeKeys: string[];
}
| {
type: 'failed-generation';
noticeKeys: string[];
errorMessage: string;
source: FailedDraftGenerationSource;
}
| {
type: 'active-generation';
noticeKeys: string[];
}
| {
type: 'background-generation';
noticeKeys: string[];
}
| {
type: 'restore-generating';
noticeKeys: string[];
}
| {
type: 'restore-draft';
noticeKeys: string[];
};
export function buildDraftNoticeKey(
kind: CreationWorkShelfKind,
id: string,
@@ -334,6 +415,219 @@ export function hasUnreadReadyDraftGenerationNotice(
});
}
export function buildPuzzleDraftOpenNoticeKeys(item: PuzzleWorkSummary) {
return collectDraftNoticeKeys('puzzle', [
item.workId,
item.profileId,
item.sourceSessionId,
buildPuzzleResultWorkId(item.sourceSessionId),
buildPuzzleResultProfileId(item.sourceSessionId),
]);
}
export function buildMatch3DDraftOpenNoticeKeys(item: Match3DWorkSummary) {
return collectDraftNoticeKeys('match3d', [
item.workId,
item.profileId,
item.sourceSessionId,
]);
}
export function resolvePuzzleDraftOpenIntent(params: {
item: PuzzleWorkSummary;
notices: DraftGenerationNoticeMap;
generation: DraftOpenGenerationFacts;
}): PuzzleDraftOpenIntent {
const { item, notices, generation } = params;
const noticeKeys = buildPuzzleDraftOpenNoticeKeys(item);
const sourceSessionId = normalizeDraftNoticeId(item.sourceSessionId);
if (!sourceSessionId) {
if (item.publicationStatus === 'published') {
return { type: 'open-published-detail', noticeKeys };
}
return {
type: 'missing-session',
noticeKeys,
errorMessage: '这份拼图草稿缺少会话信息,请重新开始创作。',
};
}
const failedNotice = getDraftGenerationNotice(notices, noticeKeys);
const hasFailedNotice = hasDraftGenerationNoticeStatus(
notices,
'puzzle',
[
item.workId,
item.profileId,
item.sourceSessionId,
buildPuzzleResultWorkId(item.sourceSessionId),
buildPuzzleResultProfileId(item.sourceSessionId),
],
'failed',
);
const hasGeneratingNotice = hasDraftGenerationNoticeStatus(
notices,
'puzzle',
[
item.workId,
item.profileId,
item.sourceSessionId,
buildPuzzleResultWorkId(item.sourceSessionId),
buildPuzzleResultProfileId(item.sourceSessionId),
],
'generating',
);
const noticeErrorMessage =
failedNotice?.status === 'failed'
? (failedNotice.message ?? buildDraftFailedShelfSummary('puzzle'))
: buildDraftFailedShelfSummary('puzzle');
const isCurrentSession =
sourceSessionId === normalizeDraftNoticeId(generation.activeSessionId);
if (generation.hasBackgroundGenerationFailure) {
return {
type: 'failed-generation',
noticeKeys,
errorMessage: noticeErrorMessage,
source: 'background',
};
}
if (isCurrentSession && generation.hasActiveGenerationFailure) {
return {
type: 'failed-generation',
noticeKeys,
errorMessage: noticeErrorMessage,
source: 'active',
};
}
if (hasFailedNotice || isPersistedDraftFailed(item.generationStatus)) {
return {
type: 'failed-generation',
noticeKeys,
errorMessage: noticeErrorMessage,
source: 'restored',
};
}
if (isCurrentSession && generation.hasActiveGenerationRunning) {
return { type: 'active-generation', noticeKeys };
}
if (generation.hasBackgroundGenerationRunning) {
return { type: 'background-generation', noticeKeys };
}
const isMarkedGenerating =
!hasFailedNotice &&
((hasGeneratingNotice && !resolvePuzzleWorkCoverImageSrc(item)) ||
isPersistedPuzzleDraftGenerating(item));
if (isMarkedGenerating) {
return { type: 'restore-generating', noticeKeys };
}
return { type: 'restore-draft', noticeKeys };
}
export function resolveMatch3DDraftOpenIntent(params: {
item: Match3DWorkSummary;
notices: DraftGenerationNoticeMap;
forceDraft?: boolean;
generation: DraftOpenGenerationFacts;
}): Match3DDraftOpenIntent {
const { item, notices, forceDraft = false, generation } = params;
const noticeKeys = buildMatch3DDraftOpenNoticeKeys(item);
if (item.publicationStatus === 'published' && !forceDraft) {
return { type: 'open-published-detail', noticeKeys };
}
const sourceSessionId = normalizeDraftNoticeId(item.sourceSessionId);
if (!sourceSessionId) {
return {
type: 'missing-session',
noticeKeys,
errorMessage: '这份抓大鹅草稿缺少会话信息,请重新开始创作。',
};
}
if (
hasUnreadReadyDraftGenerationNotice(notices, 'match3d', [
item.workId,
item.profileId,
item.sourceSessionId,
])
) {
return { type: 'ready-unread', noticeKeys };
}
const failedNotice = getDraftGenerationNotice(notices, noticeKeys);
const hasFailedNotice = hasDraftGenerationNoticeStatus(
notices,
'match3d',
[item.workId, item.profileId, item.sourceSessionId],
'failed',
);
const noticeErrorMessage =
failedNotice?.status === 'failed'
? (failedNotice.message ?? buildDraftFailedShelfSummary('match3d'))
: buildDraftFailedShelfSummary('match3d');
const isCurrentSession =
sourceSessionId === normalizeDraftNoticeId(generation.activeSessionId);
if (generation.hasBackgroundGenerationFailure) {
return {
type: 'failed-generation',
noticeKeys,
errorMessage: noticeErrorMessage,
source: 'background',
};
}
if (isCurrentSession && generation.hasActiveGenerationFailure) {
return {
type: 'failed-generation',
noticeKeys,
errorMessage: noticeErrorMessage,
source: 'active',
};
}
if (hasFailedNotice) {
return {
type: 'failed-generation',
noticeKeys,
errorMessage: noticeErrorMessage,
source: 'restored',
};
}
if (isCurrentSession && generation.hasActiveGenerationRunning) {
return { type: 'active-generation', noticeKeys };
}
if (generation.hasBackgroundGenerationRunning) {
return { type: 'background-generation', noticeKeys };
}
if (
hasDraftGenerationNoticeStatus(
notices,
'match3d',
[item.workId, item.profileId, item.sourceSessionId],
'generating',
) ||
isPersistedDraftGenerating(item.generationStatus)
) {
return { type: 'restore-generating', noticeKeys };
}
return { type: 'restore-draft', noticeKeys };
}
export function buildCreationWorkShelfRuntimeState(params: {
item: CreationWorkShelfItem;
notices: DraftGenerationNoticeMap;