refactor: 收口小游戏草稿 payload
This commit is contained in:
@@ -0,0 +1,213 @@
|
||||
import type {
|
||||
CreateMatch3DSessionRequest,
|
||||
Match3DAgentSessionSnapshot,
|
||||
} from '../../../packages/shared/src/contracts/match3dAgent';
|
||||
import type { Match3DWorkSummary } from '../../../packages/shared/src/contracts/match3dWorks';
|
||||
import type { PuzzleAgentActionRequest } from '../../../packages/shared/src/contracts/puzzleAgentActions';
|
||||
import type {
|
||||
CreatePuzzleAgentSessionRequest,
|
||||
PuzzleAgentSessionSnapshot,
|
||||
} from '../../../packages/shared/src/contracts/puzzleAgentSession';
|
||||
import type { PuzzleWorkSummary } from '../../../packages/shared/src/contracts/puzzleWorkSummary';
|
||||
|
||||
export function buildPuzzleFormPayloadFromWork(
|
||||
item: PuzzleWorkSummary,
|
||||
): CreatePuzzleAgentSessionRequest {
|
||||
const pictureDescription =
|
||||
item.workDescription?.trim() ||
|
||||
item.summary?.trim() ||
|
||||
item.levels?.[0]?.pictureDescription?.trim() ||
|
||||
item.levelName?.trim() ||
|
||||
item.workTitle?.trim() ||
|
||||
'';
|
||||
|
||||
return {
|
||||
seedText: pictureDescription,
|
||||
workTitle: item.workTitle?.trim() || item.levelName?.trim() || undefined,
|
||||
workDescription: item.workDescription?.trim() || item.summary?.trim(),
|
||||
pictureDescription,
|
||||
referenceImageSrc: null,
|
||||
referenceImageSrcs: [],
|
||||
referenceImageAssetObjectId: null,
|
||||
referenceImageAssetObjectIds: [],
|
||||
imageModel: null,
|
||||
aiRedraw: true,
|
||||
};
|
||||
}
|
||||
|
||||
function parseOptionalFiniteNumber(value: string | number | null | undefined) {
|
||||
if (typeof value === 'number') {
|
||||
return Number.isFinite(value) ? value : undefined;
|
||||
}
|
||||
|
||||
const normalizedValue = value?.trim();
|
||||
if (!normalizedValue) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const parsedValue = Number(normalizedValue);
|
||||
return Number.isFinite(parsedValue) ? parsedValue : undefined;
|
||||
}
|
||||
|
||||
export function buildMatch3DFormPayloadFromSession(
|
||||
session: Match3DAgentSessionSnapshot,
|
||||
): CreateMatch3DSessionRequest {
|
||||
const themeText =
|
||||
session.config?.themeText?.trim() ||
|
||||
session.draft?.themeText?.trim() ||
|
||||
session.anchorPack.theme.value.trim() ||
|
||||
'';
|
||||
|
||||
return {
|
||||
seedText: themeText,
|
||||
themeText,
|
||||
referenceImageSrc:
|
||||
session.config?.referenceImageSrc ??
|
||||
session.draft?.referenceImageSrc ??
|
||||
null,
|
||||
clearCount:
|
||||
session.config?.clearCount ??
|
||||
session.draft?.clearCount ??
|
||||
parseOptionalFiniteNumber(session.anchorPack.clearCount.value) ??
|
||||
undefined,
|
||||
difficulty:
|
||||
session.config?.difficulty ??
|
||||
session.draft?.difficulty ??
|
||||
parseOptionalFiniteNumber(session.anchorPack.difficulty.value) ??
|
||||
undefined,
|
||||
assetStyleId: session.config?.assetStyleId ?? null,
|
||||
assetStyleLabel: session.config?.assetStyleLabel ?? null,
|
||||
assetStylePrompt: session.config?.assetStylePrompt ?? null,
|
||||
generateClickSound: session.config?.generateClickSound,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildMatch3DFormPayloadFromWork(
|
||||
item: Match3DWorkSummary,
|
||||
): CreateMatch3DSessionRequest {
|
||||
const themeText = item.themeText?.trim() || item.gameName?.trim() || '';
|
||||
return {
|
||||
seedText: themeText,
|
||||
themeText,
|
||||
referenceImageSrc: item.referenceImageSrc ?? null,
|
||||
clearCount: item.clearCount,
|
||||
difficulty: item.difficulty,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildPuzzleCompileActionFromFormPayload(
|
||||
payload: CreatePuzzleAgentSessionRequest | null,
|
||||
): PuzzleAgentActionRequest {
|
||||
const pictureDescription =
|
||||
payload?.pictureDescription?.trim() || payload?.seedText?.trim();
|
||||
const workTitle = payload?.workTitle?.trim();
|
||||
const workDescription = payload?.workDescription?.trim() || pictureDescription;
|
||||
|
||||
return {
|
||||
action: 'compile_puzzle_draft',
|
||||
promptText: pictureDescription,
|
||||
...(workTitle ? { workTitle } : {}),
|
||||
...(workDescription ? { workDescription } : {}),
|
||||
...(pictureDescription ? { pictureDescription } : {}),
|
||||
referenceImageSrc: payload?.referenceImageSrc || null,
|
||||
referenceImageSrcs: payload?.referenceImageSrcs ?? [],
|
||||
referenceImageAssetObjectId: payload?.referenceImageAssetObjectId ?? null,
|
||||
referenceImageAssetObjectIds: payload?.referenceImageAssetObjectIds ?? [],
|
||||
imageModel: payload?.imageModel ?? null,
|
||||
aiRedraw: payload?.aiRedraw ?? true,
|
||||
candidateCount: 1,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildPuzzleFormPayloadFromSession(
|
||||
session: PuzzleAgentSessionSnapshot,
|
||||
): CreatePuzzleAgentSessionRequest {
|
||||
const formDraft = session.draft?.formDraft;
|
||||
const pictureDescription =
|
||||
formDraft?.pictureDescription?.trim() ||
|
||||
session.draft?.levels?.[0]?.pictureDescription?.trim() ||
|
||||
session.anchorPack.visualSubject.value.trim() ||
|
||||
session.seedText?.trim() ||
|
||||
'';
|
||||
const workTitle =
|
||||
formDraft?.workTitle?.trim() || session.draft?.workTitle?.trim();
|
||||
const workDescription =
|
||||
formDraft?.workDescription?.trim() ||
|
||||
session.draft?.workDescription?.trim() ||
|
||||
session.draft?.summary?.trim() ||
|
||||
pictureDescription;
|
||||
|
||||
return {
|
||||
seedText: pictureDescription,
|
||||
...(workTitle ? { workTitle } : {}),
|
||||
...(workDescription ? { workDescription } : {}),
|
||||
pictureDescription,
|
||||
referenceImageSrc: null,
|
||||
referenceImageSrcs: [],
|
||||
referenceImageAssetObjectId: null,
|
||||
referenceImageAssetObjectIds: [],
|
||||
imageModel: null,
|
||||
aiRedraw: true,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildPendingPuzzleDraftMetadata(
|
||||
payload: CreatePuzzleAgentSessionRequest | null | undefined,
|
||||
) {
|
||||
const title = payload?.workTitle?.trim();
|
||||
const summary =
|
||||
payload?.workDescription?.trim() ||
|
||||
payload?.pictureDescription?.trim() ||
|
||||
payload?.seedText?.trim();
|
||||
return {
|
||||
...(title ? { title } : {}),
|
||||
...(summary ? { summary } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildPendingMatch3DDraftMetadata(
|
||||
payload: CreateMatch3DSessionRequest | null | undefined,
|
||||
) {
|
||||
const themeText = payload?.themeText?.trim() || payload?.seedText?.trim();
|
||||
return {
|
||||
...(themeText ? { title: themeText, summary: themeText } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildPuzzleFormPayloadFromAction(
|
||||
payload: PuzzleAgentActionRequest,
|
||||
): CreatePuzzleAgentSessionRequest | null {
|
||||
if (
|
||||
payload.action !== 'compile_puzzle_draft' &&
|
||||
payload.action !== 'save_puzzle_form_draft'
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const workTitle = payload.workTitle?.trim() ?? '';
|
||||
const workDescription = payload.workDescription?.trim() ?? '';
|
||||
const pictureDescription =
|
||||
payload.pictureDescription?.trim() || payload.promptText?.trim() || '';
|
||||
|
||||
return {
|
||||
seedText: pictureDescription,
|
||||
...(workTitle ? { workTitle } : {}),
|
||||
...(workDescription ? { workDescription } : {}),
|
||||
pictureDescription,
|
||||
referenceImageSrc:
|
||||
payload.action === 'compile_puzzle_draft'
|
||||
? (payload.referenceImageSrc ?? null)
|
||||
: (payload.referenceImageSrc ?? null),
|
||||
referenceImageSrcs: payload.referenceImageSrcs ?? [],
|
||||
referenceImageAssetObjectId: payload.referenceImageAssetObjectId ?? null,
|
||||
referenceImageAssetObjectIds: payload.referenceImageAssetObjectIds ?? [],
|
||||
imageModel:
|
||||
payload.action === 'compile_puzzle_draft'
|
||||
? (payload.imageModel ?? null)
|
||||
: (payload.imageModel ?? null),
|
||||
aiRedraw:
|
||||
payload.action === 'compile_puzzle_draft'
|
||||
? (payload.aiRedraw ?? true)
|
||||
: (payload.aiRedraw ?? true),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user