From 9cb3c6a27eb422d6eea2b1ca2fead7cb8cfc0165 Mon Sep 17 00:00:00 2001 From: kdletters Date: Sat, 25 Apr 2026 15:17:01 +0800 Subject: [PATCH] feat: add puzzle and big fish draft generation progress --- ...GENT_ACTION_RESPONSE_SESSION_2026-04-25.md | 18 + ...AFT_PROGRESS_AND_ASSET_CHAIN_2026-04-25.md | 46 +++ docs/technical/README.md | 2 + .../src/contracts/puzzleAgentActions.ts | 6 + server-rs/crates/api-server/src/big_fish.rs | 103 ++++- server-rs/crates/api-server/src/puzzle.rs | 87 +++- .../shared-contracts/src/puzzle_agent.rs | 2 + .../PlatformEntryFlowShellImpl.tsx | 382 ++++++++++++++++-- .../platform-entry/platformEntryTypes.ts | 2 + .../miniGameDraftGenerationProgress.ts | 283 +++++++++++++ 10 files changed, 898 insertions(+), 33 deletions(-) create mode 100644 docs/technical/PUZZLE_AGENT_ACTION_RESPONSE_SESSION_2026-04-25.md create mode 100644 docs/technical/PUZZLE_BIG_FISH_DRAFT_PROGRESS_AND_ASSET_CHAIN_2026-04-25.md create mode 100644 src/services/miniGameDraftGenerationProgress.ts diff --git a/docs/technical/PUZZLE_AGENT_ACTION_RESPONSE_SESSION_2026-04-25.md b/docs/technical/PUZZLE_AGENT_ACTION_RESPONSE_SESSION_2026-04-25.md new file mode 100644 index 00000000..2b3c7a8d --- /dev/null +++ b/docs/technical/PUZZLE_AGENT_ACTION_RESPONSE_SESSION_2026-04-25.md @@ -0,0 +1,18 @@ +# 拼图 Agent 操作回包收口说明 + +## 背景 + +拼图结果页在执行 `select_puzzle_image` 时,前端先调用 `POST /api/runtime/puzzle/agent/sessions/:sessionId/actions`,随后又调用 `GET /api/runtime/puzzle/agent/sessions/:sessionId` 拉取完整会话。完整会话包含消息、草稿与结果预览,体积明显大于一次选图操作本身,导致选择候选图时出现无意义流量与体感延迟。 + +## 落地规则 + +- `actions` 接口在服务端本来已经拿到变更后的 session,因此回包必须直接包含 `operation + session`。 +- 前端收到 `actions` 回包后直接用 `session` 更新本地状态,不再为了同步选图、生成图片或编译草稿而补发完整会话 `GET`。 +- `publish_puzzle_work` 发布后仍由服务端重新读取一次最新 session 并放入同一个 action response,前端只保留作品架与广场详情的必要刷新。 +- 该改动只收敛 Rust api-server 与前端 contract,不引入 server-node 兼容逻辑。 + +## 验收口径 + +1. 选择拼图候选图只产生一次 `POST /actions`,不再紧跟完整 session `GET`。 +2. 选图后结果页仍立即反映选中的正式图。 +3. 发布后仍能跳转到已发布拼图详情。 diff --git a/docs/technical/PUZZLE_BIG_FISH_DRAFT_PROGRESS_AND_ASSET_CHAIN_2026-04-25.md b/docs/technical/PUZZLE_BIG_FISH_DRAFT_PROGRESS_AND_ASSET_CHAIN_2026-04-25.md new file mode 100644 index 00000000..f6fc2d94 --- /dev/null +++ b/docs/technical/PUZZLE_BIG_FISH_DRAFT_PROGRESS_AND_ASSET_CHAIN_2026-04-25.md @@ -0,0 +1,46 @@ +# 拼图与大鱼吃小鱼草稿生成进度页落地方案(2026-04-25) + +## 背景 + +RPG 在点击生成草稿后会离开聊天工作区,进入独立的生成进度页,并在该页展示生成链路的阶段、锚点与最终草稿内容。拼图与大鱼吃小鱼此前点击“生成结果页”后直接跳到结果页,正式图片、动作与背景仍分散在结果页工坊里逐个生成,导致用户无法看到“正在一次性准备完整草稿”的过程。 + +## 落地边界 + +- 前端只负责展示生成进度与触发已有后端动作,不新增 server-node 或 PostgreSQL 链路。 +- 后端继续沿用 `server-rs` + SpacetimeDB 的会话、草稿与资产写入能力;“一次性生成所有需要的东西”必须由 `server-rs` 的 compile action 承担,前端只发起一次 action 并展示进度页。 +- 拼图生成草稿链路必须包含:结果页草稿、候选图生成、正式图确认。 +- 大鱼吃小鱼生成草稿链路必须包含:玩法草稿、关卡主图、动作素材、场地背景。 +- 生成过程中展示的“角色描述、角色图片、动作”等,统一映射为锚点、草稿蓝图与资产步骤,不把规则说明类文本写成默认 UI 文案。 + +## 交互设计 + +1. 用户在拼图或大鱼吃小鱼 Agent 工作区点击生成按钮。 +2. 页面立即切换到独立生成进度页,同时只向 `server-rs` 发起一次 compile action,返回按钮在生成中禁用,避免中途回退造成状态漂移。 +3. 进度页左侧展示阶段进度、步骤卡片与错误信息;右侧展示当前锚点与已成形的草稿结构。 +4. 全量生成成功后自动进入对应结果页,结果页直接展示已生成的资产。 +5. 生成失败时停留在进度页,用户可返回工作区补充设定,或点击重试重新执行完整草稿链路。 + +## 阶段映射 + +### 拼图 + +- `compile_puzzle_draft`:在 `server-rs` 内整理主题、主体、构图与标签,写入结果页草稿。 +- `compile_puzzle_draft`:同一次后端 action 内根据草稿摘要生成候选图。 +- `compile_puzzle_draft`:同一次后端 action 内自动选择第一张候选图作为正式图。 +- `ready`:进入拼图结果页。 + +### 大鱼吃小鱼 + +- `big_fish_compile_draft`:在 `server-rs` 内生成玩法草稿、关卡角色描述、背景蓝图与运行参数。 +- `big_fish_compile_draft`:同一次后端 action 内按每个关卡生成主角色/鱼群图片。 +- `big_fish_compile_draft`:同一次后端 action 内按每个关卡生成 `idle_float` 与 `move_swim` 动作素材。 +- `big_fish_compile_draft`:同一次后端 action 内生成玩法场地背景。 +- `ready`:进入大鱼吃小鱼结果页。 + +## 验收点 + +- 拼图和大鱼吃小鱼点击生成草稿后不再直接停留在聊天工作区等待。 +- 生成中可看到独立进度页,且进度步骤随 action 完成逐步推进。 +- 拼图结果页打开时已有正式图;大鱼结果页打开时主图、动作和背景资产均已写入 `assetSlots`。 +- 前端点击生成草稿时不串行调用多个资产 action;多阶段业务编排收敛在 `server-rs`。 +- 不新增 server-node 依赖,不复活 legacy public 静态资产路径。 diff --git a/docs/technical/README.md b/docs/technical/README.md index 71b524d2..94cf0eb0 100644 --- a/docs/technical/README.md +++ b/docs/technical/README.md @@ -5,10 +5,12 @@ ## 文档列表 - [API_ERROR_DETAILS_MESSAGE_DISPLAY_FIX_2026-04-25.md](./API_ERROR_DETAILS_MESSAGE_DISPLAY_FIX_2026-04-25.md):记录统一 API envelope 错误展示优先读取 `error.details.message` 的修复口径,避免 Big Fish 发布校验等业务错误只显示通用“请求参数不合法”。 +- [PUZZLE_BIG_FISH_DRAFT_PROGRESS_AND_ASSET_CHAIN_2026-04-25.md](./PUZZLE_BIG_FISH_DRAFT_PROGRESS_AND_ASSET_CHAIN_2026-04-25.md):冻结拼图与大鱼吃小鱼点击生成草稿后进入独立进度页,并一次性生成草稿、图片与动作资产的前端编排边界。 - [BIG_FISH_DIRECTION_TOUCH_CONTROL_2026-04-24.md](./BIG_FISH_DIRECTION_TOUCH_CONTROL_2026-04-24.md):记录大鱼吃小鱼从固定摇杆改为屏幕首触点方向控制,并要求本地直达局在未操作时保持对象运动。 - [RUST_WORKSPACE_DEFAULT_BUILD_SCOPE_FIX_2026-04-25.md](./RUST_WORKSPACE_DEFAULT_BUILD_SCOPE_FIX_2026-04-25.md):记录 `server-rs` 无参数 `cargo build` 链接 `spacetime-module` 失败的根因,并冻结默认只构建原生 `api-server`、模块产物继续走 `spacetime build` 的命令边界。 - [BIG_FISH_DIRECT_ROUTE_PLAYGROUND_2026-04-24.md](./BIG_FISH_DIRECT_ROUTE_PLAYGROUND_2026-04-24.md):记录 `/big-fish` 大鱼吃小鱼玩法直达入口,明确复用现有 `BigFishRuntimeShell` 和本地占位运行态的调试边界。 - [PUZZLE_DIRECT_ROUTE_PLAYGROUND_2026-04-24.md](./PUZZLE_DIRECT_ROUTE_PLAYGROUND_2026-04-24.md):记录 `/puzzle` 拼图玩法直达入口,明确复用现有 `PuzzleRuntimeShell` 和本地占位图运行态的调试边界。 +- [PUZZLE_AGENT_ACTION_RESPONSE_SESSION_2026-04-25.md](./PUZZLE_AGENT_ACTION_RESPONSE_SESSION_2026-04-25.md):记录拼图 Agent `actions` 回包直接携带最新 session,避免选图后额外拉取完整会话大包的接口收口规则。 - [CREATION_AGENT_PUBLISH_GATE_NORMALIZE_WRITEBACK_FIX_2026-04-24.md](./CREATION_AGENT_PUBLISH_GATE_NORMALIZE_WRITEBACK_FIX_2026-04-24.md):记录结果页 profile 归一化回写丢失顶层 `worldHook / playerPremise` 导致 publish gate 继续误报结构 blocker 的根因,并冻结前端归一化保留发布字段的修复口径。 - [CUSTOM_WORLD_RESULT_ENTITY_GENERATION_FIX_2026-04-24.md](./CUSTOM_WORLD_RESULT_ENTITY_GENERATION_FIX_2026-04-24.md):记录世界结果页在 Agent 草稿模式下新增场景、新增 NPC 生成成功但结果页字段不可用的根因,并冻结 `api-server` 生成归一化层补齐 profile 字段的修复口径。 diff --git a/packages/shared/src/contracts/puzzleAgentActions.ts b/packages/shared/src/contracts/puzzleAgentActions.ts index 183ec46a..2cc6c2c4 100644 --- a/packages/shared/src/contracts/puzzleAgentActions.ts +++ b/packages/shared/src/contracts/puzzleAgentActions.ts @@ -1,3 +1,5 @@ +import type { PuzzleAgentSessionSnapshot } from './puzzleAgentSession'; + export type PuzzleAgentSuggestedActionType = | 'request_summary' | 'compile_puzzle_draft' @@ -54,6 +56,10 @@ export type PuzzleAgentActionRequest = themeTags?: string[]; }; +/** + * 拼图操作接口直接返回最新会话,避免前端在选图等轻操作后再额外 GET 大体积快照。 + */ export interface PuzzleAgentActionResponse { operation: PuzzleAgentOperationRecord; + session: PuzzleAgentSessionSnapshot; } diff --git a/server-rs/crates/api-server/src/big_fish.rs b/server-rs/crates/api-server/src/big_fish.rs index 3079bcec..dbc9acee 100644 --- a/server-rs/crates/api-server/src/big_fish.rs +++ b/server-rs/crates/api-server/src/big_fish.rs @@ -434,10 +434,13 @@ pub async fn execute_big_fish_action( let now = current_utc_micros(); let session = match payload.action.trim() { "big_fish_compile_draft" => { - state - .spacetime_client() - .compile_big_fish_draft(session_id, owner_user_id, now) - .await + compile_big_fish_draft_with_all_assets( + &state, + session_id, + owner_user_id, + now, + ) + .await } "big_fish_generate_level_main_image" => { let asset_url = generate_big_fish_formal_asset( @@ -766,6 +769,98 @@ fn map_big_fish_asset_coverage_response( } } +async fn compile_big_fish_draft_with_all_assets( + state: &AppState, + session_id: String, + owner_user_id: String, + now: i64, +) -> Result { + let session = state + .spacetime_client() + .compile_big_fish_draft(session_id.clone(), owner_user_id.clone(), now) + .await?; + let draft = session + .draft + .clone() + .ok_or_else(|| SpacetimeClientError::Runtime("大鱼吃小鱼玩法草稿尚未生成".to_string()))?; + // 点击生成草稿时一次性生成所有首版玩法资产,前端只负责展示进度和最终 session。 + for level in &draft.levels { + let asset_url = generate_big_fish_formal_asset( + state, + &owner_user_id, + &session_id, + "level_main_image", + Some(level.level), + None, + current_utc_micros(), + ) + .await + .map_err(|error| SpacetimeClientError::Runtime(error.message().to_string()))?; + state + .spacetime_client() + .generate_big_fish_asset(BigFishAssetGenerateRecordInput { + session_id: session_id.clone(), + owner_user_id: owner_user_id.clone(), + asset_kind: "level_main_image".to_string(), + level: Some(level.level), + motion_key: None, + asset_url: Some(asset_url), + generated_at_micros: current_utc_micros(), + }) + .await?; + } + for level in &draft.levels { + for motion_key in ["idle_float", "move_swim"] { + let asset_url = generate_big_fish_formal_asset( + state, + &owner_user_id, + &session_id, + "level_motion", + Some(level.level), + Some(motion_key), + current_utc_micros(), + ) + .await + .map_err(|error| SpacetimeClientError::Runtime(error.message().to_string()))?; + state + .spacetime_client() + .generate_big_fish_asset(BigFishAssetGenerateRecordInput { + session_id: session_id.clone(), + owner_user_id: owner_user_id.clone(), + asset_kind: "level_motion".to_string(), + level: Some(level.level), + motion_key: Some(motion_key.to_string()), + asset_url: Some(asset_url), + generated_at_micros: current_utc_micros(), + }) + .await?; + } + } + let asset_url = generate_big_fish_formal_asset( + state, + &owner_user_id, + &session_id, + "stage_background", + None, + None, + current_utc_micros(), + ) + .await + .map_err(|error| SpacetimeClientError::Runtime(error.message().to_string()))?; + state + .spacetime_client() + .generate_big_fish_asset(BigFishAssetGenerateRecordInput { + session_id, + owner_user_id, + asset_kind: "stage_background".to_string(), + level: None, + motion_key: None, + asset_url: Some(asset_url), + generated_at_micros: current_utc_micros(), + }) + .await +} + fn map_big_fish_agent_message_response( message: BigFishAgentMessageRecord, ) -> BigFishAgentMessageResponse { diff --git a/server-rs/crates/api-server/src/puzzle.rs b/server-rs/crates/api-server/src/puzzle.rs index 5f665ed3..5a216bd5 100644 --- a/server-rs/crates/api-server/src/puzzle.rs +++ b/server-rs/crates/api-server/src/puzzle.rs @@ -435,14 +435,17 @@ pub async fn execute_puzzle_agent_action( let (operation_type, phase_label, phase_detail, session) = match payload.action.trim() { "compile_puzzle_draft" => { - let session = state - .spacetime_client() - .compile_puzzle_agent_draft(session_id, owner_user_id, now) - .await; + let session = compile_puzzle_draft_with_initial_cover( + &state, + session_id.clone(), + owner_user_id.clone(), + now, + ) + .await; ( "compile_puzzle_draft", - "结果页草稿", - "已根据当前锚点编译结果页草稿。", + "完整拼图草稿", + "已编译草稿、生成候选图并应用正式图片。", session, ) } @@ -572,6 +575,18 @@ pub async fn execute_puzzle_agent_action( ) })?; + let session = state + .spacetime_client() + .get_puzzle_agent_session(session_id.clone(), owner_user_id.clone()) + .await + .map_err(|error| { + puzzle_error_response( + &request_context, + PUZZLE_AGENT_API_BASE_PROVIDER, + map_puzzle_client_error(error), + ) + })?; + return Ok(json_success_body( Some(&request_context), PuzzleAgentActionResponse { @@ -584,6 +599,7 @@ pub async fn execute_puzzle_agent_action( progress: 100, error: None, }, + session: map_puzzle_agent_session_response(session), }, )); } @@ -616,6 +632,7 @@ pub async fn execute_puzzle_agent_action( progress: 100, error: None, }, + session: map_puzzle_agent_session_response(session), }, )) } @@ -1336,6 +1353,64 @@ fn build_stable_puzzle_work_ids(session_id: &str) -> (String, String) { ) } +async fn compile_puzzle_draft_with_initial_cover( + state: &AppState, + session_id: String, + owner_user_id: String, + now: i64, +) -> Result { + let compiled_session = state + .spacetime_client() + .compile_puzzle_agent_draft(session_id.clone(), owner_user_id.clone(), now) + .await?; + let draft = compiled_session + .draft + .clone() + .ok_or_else(|| SpacetimeClientError::Runtime("拼图结果页草稿尚未生成".to_string()))?; + // 点击生成草稿时一次性完成首图生成与正式图选定,前端只展示进度,不再承担业务编排。 + let candidates = generate_puzzle_image_candidates( + state, + owner_user_id.as_str(), + &compiled_session.session_id, + &draft.level_name, + &draft.summary, + 2, + ) + .await + .map_err(SpacetimeClientError::Runtime)?; + let selected_candidate_id = candidates + .iter() + .find(|candidate| candidate.selected) + .or_else(|| candidates.first()) + .map(|candidate| candidate.candidate_id.clone()) + .ok_or_else(|| SpacetimeClientError::Runtime("拼图候选图生成结果为空".to_string()))?; + let candidates_json = serde_json::to_string( + &candidates + .iter() + .map(to_puzzle_generated_image_candidate) + .collect::>(), + ) + .map_err(|error| SpacetimeClientError::Runtime(format!("拼图候选图序列化失败:{error}")))?; + state + .spacetime_client() + .save_puzzle_generated_images(PuzzleGeneratedImagesSaveRecordInput { + session_id: compiled_session.session_id.clone(), + owner_user_id: owner_user_id.clone(), + candidates_json, + saved_at_micros: current_utc_micros(), + }) + .await?; + state + .spacetime_client() + .select_puzzle_cover_image(PuzzleSelectCoverImageRecordInput { + session_id, + owner_user_id, + candidate_id: selected_candidate_id, + selected_at_micros: current_utc_micros(), + }) + .await +} + fn ensure_non_empty( request_context: &RequestContext, provider: &str, diff --git a/server-rs/crates/shared-contracts/src/puzzle_agent.rs b/server-rs/crates/shared-contracts/src/puzzle_agent.rs index 8275c885..42a6c279 100644 --- a/server-rs/crates/shared-contracts/src/puzzle_agent.rs +++ b/server-rs/crates/shared-contracts/src/puzzle_agent.rs @@ -187,4 +187,6 @@ pub struct PuzzleAgentOperationResponse { #[serde(rename_all = "camelCase")] pub struct PuzzleAgentActionResponse { pub operation: PuzzleAgentOperationResponse, + /// 操作完成后的最新会话快照,供前端直接更新界面,避免重复拉取完整 session。 + pub session: PuzzleAgentSessionSnapshotResponse, } diff --git a/src/components/platform-entry/PlatformEntryFlowShellImpl.tsx b/src/components/platform-entry/PlatformEntryFlowShellImpl.tsx index b4ee6fb0..51b2f5ac 100644 --- a/src/components/platform-entry/PlatformEntryFlowShellImpl.tsx +++ b/src/components/platform-entry/PlatformEntryFlowShellImpl.tsx @@ -1,4 +1,5 @@ -import { AnimatePresence, motion } from 'motion/react'; +import { Loader2 } from 'lucide-react'; +import { AnimatePresence, motion } from 'motion/react'; import { lazy, Suspense, @@ -9,6 +10,7 @@ import { useState, } from 'react'; +import type { PublicUserSummary } from '../../../packages/shared/src/contracts/auth'; import type { BigFishRuntimeSnapshotResponse, BigFishSessionSnapshotResponse, @@ -21,6 +23,7 @@ import type { PuzzleAgentActionRequest, PuzzleAgentOperationRecord, } from '../../../packages/shared/src/contracts/puzzleAgentActions'; +import type { PuzzleGeneratedImageCandidate } from '../../../packages/shared/src/contracts/puzzleAgentDraft'; import type { PuzzleAgentSessionSnapshot, SendPuzzleAgentMessageRequest, @@ -31,7 +34,6 @@ import type { CustomWorldGalleryCard, CustomWorldLibraryEntry, } from '../../../packages/shared/src/contracts/runtime'; -import type { PublicUserSummary } from '../../../packages/shared/src/contracts/auth'; import { buildCustomWorldPlayableCharacters } from '../../data/characterPresets'; import { getPublicAuthUserByCode, @@ -43,15 +45,22 @@ import { getBigFishCreationSession, streamBigFishCreationMessage, } from '../../services/big-fish-creation'; -import { - deleteBigFishWork, - listBigFishWorks, -} from '../../services/big-fish-works'; import { startBigFishRuntimeRun, submitBigFishRuntimeInput, } from '../../services/big-fish-runtime'; +import { + deleteBigFishWork, + listBigFishWorks, +} from '../../services/big-fish-works'; import { readCustomWorldAgentUiState } from '../../services/customWorldAgentUiState'; +import { + buildBigFishGenerationAnchorEntries, + buildMiniGameDraftGenerationProgress, + buildPuzzleGenerationAnchorEntries, + createMiniGameDraftGenerationState, + type MiniGameDraftGenerationState, +} from '../../services/miniGameDraftGenerationProgress'; import { getPlatformProfileDashboard } from '../../services/platform-entry'; import { createPuzzleAgentSession, @@ -59,18 +68,22 @@ import { getPuzzleAgentSession, streamPuzzleAgentMessage, } from '../../services/puzzle-agent'; -import { getPuzzleGalleryDetail } from '../../services/puzzle-gallery'; +import { + getPuzzleGalleryDetail, + listPuzzleGallery, +} from '../../services/puzzle-gallery'; import { advanceLocalPuzzleLevel, + advanceLocalPuzzleLevelWithWork, dragLocalPuzzlePiece, startLocalPuzzleRun, swapLocalPuzzlePieces, } from '../../services/puzzle-runtime/puzzleLocalRuntime'; import { deletePuzzleWork, listPuzzleWorks } from '../../services/puzzle-works'; -import { deleteRpgEntryWorldProfile } from '../../services/rpg-entry'; -import { getRpgEntryWorldGalleryDetailByCode } from '../../services/rpg-entry/rpgEntryLibraryClient'; import { deleteRpgCreationAgentSession } from '../../services/rpg-creation'; import { rpgCreationPreviewAdapter } from '../../services/rpg-creation/rpgCreationPreviewAdapter'; +import { deleteRpgEntryWorldProfile } from '../../services/rpg-entry'; +import { getRpgEntryWorldGalleryDetailByCode } from '../../services/rpg-entry/rpgEntryLibraryClient'; import type { CustomWorldProfile } from '../../types'; import { useAuthUi } from '../auth/AuthUiContext'; import { CustomWorldCreationHub } from '../custom-world-home/CustomWorldCreationHub'; @@ -83,13 +96,13 @@ import { useRpgCreationEnterWorld } from '../rpg-entry/useRpgCreationEnterWorld' import { useRpgCreationResultAutosave } from '../rpg-entry/useRpgCreationResultAutosave'; import { useRpgCreationSessionController } from '../rpg-entry/useRpgCreationSessionController'; import { PlatformEntryCreationTypeModal } from './PlatformEntryCreationTypeModal'; +import type { PlatformCreationTypeId } from './platformEntryCreationTypes'; import { PlatformEntryHomeView } from './PlatformEntryHomeView'; import { buildCreationHubFallbackItems, normalizeAgentBackedProfile, resolveRpgCreationErrorMessage, } from './platformEntryShared'; -import type { PlatformCreationTypeId } from './platformEntryCreationTypes'; import type { PlatformEntryFlowShellProps } from './platformEntryTypes'; import { PlatformEntryWorldDetailView } from './PlatformEntryWorldDetailView'; import { usePlatformEntryBootstrap } from './usePlatformEntryBootstrap'; @@ -101,6 +114,53 @@ type AgentResultPublishGateView = { publishReady: boolean; }; +function buildPuzzleCandidateWorkSummary( + candidate: PuzzleGeneratedImageCandidate, + session: PuzzleAgentSessionSnapshot, + levelIndex: number, +): PuzzleWorkSummary { + const draft = session.draft; + const nowIso = new Date().toISOString(); + return { + workId: `${session.sessionId}-${candidate.candidateId}-level-${levelIndex}-runtime-work`, + profileId: `${session.sessionId}-${candidate.candidateId}-level-${levelIndex}-runtime-profile`, + ownerUserId: 'local-runtime', + sourceSessionId: session.sessionId, + authorDisplayName: '当前草稿', + levelName: draft?.levelName + ? `${draft.levelName} · 候选 ${levelIndex}` + : `候选拼图 ${levelIndex}`, + summary: draft?.summary ?? candidate.prompt, + themeTags: draft?.themeTags ?? [], + coverImageSrc: candidate.imageSrc, + coverAssetId: candidate.assetId, + publicationStatus: 'published', + updatedAt: nowIso, + publishedAt: nowIso, + playCount: 0, + publishReady: true, + }; +} + +function pickPuzzleCandidateForLevel( + candidates: PuzzleGeneratedImageCandidate[], + playedProfileIds: string[], +) { + return candidates.find( + (candidate) => + candidate.imageSrc && + !playedProfileIds.some((profileId) => + profileId.includes(candidate.candidateId), + ), + ); +} + +function pickFreshGeneratedPuzzleCandidate( + candidates: PuzzleGeneratedImageCandidate[], +) { + return candidates.find((candidate) => candidate.imageSrc); +} + type AgentResultBlockerView = { code?: string; message: string; @@ -312,6 +372,8 @@ export function PlatformEntryFlowShellImpl({ const [bigFishError, setBigFishError] = useState(null); const [isBigFishBusy, setIsBigFishBusy] = useState(false); const [isBigFishLoadingLibrary, setIsBigFishLoadingLibrary] = useState(false); + const [bigFishGenerationState, setBigFishGenerationState] = + useState(null); const [streamingBigFishReplyText, setStreamingBigFishReplyText] = useState(''); const [isStreamingBigFishReply, setIsStreamingBigFishReply] = useState(false); @@ -327,6 +389,10 @@ export function PlatformEntryFlowShellImpl({ const [puzzleError, setPuzzleError] = useState(null); const [isPuzzleBusy, setIsPuzzleBusy] = useState(false); const [isPuzzleLoadingLibrary, setIsPuzzleLoadingLibrary] = useState(false); + const [puzzleGenerationState, setPuzzleGenerationState] = + useState(null); + const [isPuzzleNextLevelGenerating, setIsPuzzleNextLevelGenerating] = + useState(false); const [isSearchingPublicCode, setIsSearchingPublicCode] = useState(false); const [publicSearchError, setPublicSearchError] = useState(null); const [searchedPublicUser, setSearchedPublicUser] = @@ -791,6 +857,7 @@ export function PlatformEntryFlowShellImpl({ const leaveBigFishFlow = useCallback(() => { setBigFishError(null); setBigFishRun(null); + setBigFishGenerationState(null); setStreamingBigFishReplyText(''); setIsStreamingBigFishReply(false); enterCreateTab(); @@ -801,6 +868,8 @@ export function PlatformEntryFlowShellImpl({ setPuzzleError(null); setPuzzleOperation(null); setPuzzleRun(null); + setPuzzleGenerationState(null); + setIsPuzzleNextLevelGenerating(false); setStreamingPuzzleReplyText(''); setIsStreamingPuzzleReply(false); enterCreateTab(); @@ -913,15 +982,45 @@ export function PlatformEntryFlowShellImpl({ setBigFishError(null); try { + if (payload.action === 'big_fish_compile_draft') { + setSelectionStage('big-fish-generating'); + setBigFishGenerationState(createMiniGameDraftGenerationState('big-fish')); + const { session } = await executeBigFishCreationAction( + bigFishSession.sessionId, + payload, + ); + setBigFishSession(session); + setBigFishGenerationState((current) => + current + ? { + ...current, + phase: 'ready', + completedAssetCount: session.assetSlots.filter( + (slot) => slot.status === 'ready', + ).length, + totalAssetCount: session.assetSlots.length, + } + : current, + ); + setSelectionStage('big-fish-result'); + return; + } + const { session } = await executeBigFishCreationAction( bigFishSession.sessionId, payload, ); setBigFishSession(session); - if (payload.action === 'big_fish_compile_draft') { - setSelectionStage('big-fish-result'); - } } catch (error) { + setBigFishGenerationState((current) => + current + ? { + ...current, + phase: 'failed', + error: resolveBigFishErrorMessage(error, '执行大鱼吃小鱼操作失败。'), + } + : current, + ); setBigFishError( resolveBigFishErrorMessage(error, '执行大鱼吃小鱼操作失败。'), ); @@ -947,7 +1046,30 @@ export function PlatformEntryFlowShellImpl({ setPuzzleError(null); try { - const { operation } = await executePuzzleAgentAction( + if (payload.action === 'compile_puzzle_draft') { + setSelectionStage('puzzle-generating'); + setPuzzleGenerationState(createMiniGameDraftGenerationState('puzzle')); + const { operation, session } = await executePuzzleAgentAction( + puzzleSession.sessionId, + payload, + ); + setPuzzleOperation(operation); + setPuzzleSession(session); + setPuzzleGenerationState((current) => + current + ? { + ...current, + phase: 'ready', + completedAssetCount: 1, + totalAssetCount: 1, + } + : current, + ); + setSelectionStage('puzzle-result'); + return; + } + + const { operation, session } = await executePuzzleAgentAction( puzzleSession.sessionId, payload, ); @@ -957,14 +1079,8 @@ export function PlatformEntryFlowShellImpl({ await refreshPuzzleShelf(); } - const { session } = await getPuzzleAgentSession( - puzzleSession.sessionId, - ); setPuzzleSession(session); - if (payload.action === 'compile_puzzle_draft') { - setSelectionStage('puzzle-result'); - } if ( payload.action === 'publish_puzzle_work' && session.publishedProfileId @@ -976,6 +1092,15 @@ export function PlatformEntryFlowShellImpl({ setSelectionStage('puzzle-gallery-detail'); } } catch (error) { + setPuzzleGenerationState((current) => + current + ? { + ...current, + phase: 'failed', + error: resolvePuzzleErrorMessage(error, '执行拼图操作失败。'), + } + : current, + ); setPuzzleError(resolvePuzzleErrorMessage(error, '执行拼图操作失败。')); } finally { setIsPuzzleBusy(false); @@ -1095,9 +1220,123 @@ export function PlatformEntryFlowShellImpl({ return; } + const currentLevel = puzzleRun.currentLevel; + if (!currentLevel || currentLevel.status !== 'cleared') { + return; + } + + setIsPuzzleBusy(true); setPuzzleError(null); - setPuzzleRun(advanceLocalPuzzleLevel(puzzleRun)); - }, [isPuzzleBusy, puzzleRun]); + + try { + const galleryResponse = await listPuzzleGallery(); + setPuzzleWorks(galleryResponse.items); + const galleryNext = galleryResponse.items.find( + (item) => + item.publicationStatus === 'published' && + item.coverImageSrc && + !puzzleRun.playedProfileIds.includes(item.profileId), + ); + if (galleryNext) { + const { item } = await getPuzzleGalleryDetail(galleryNext.profileId); + setSelectedPuzzleDetail(item); + setPuzzleRun(advanceLocalPuzzleLevelWithWork(puzzleRun, item)); + return; + } + + const existingCandidate = pickPuzzleCandidateForLevel( + puzzleSession?.draft?.candidates ?? [], + puzzleRun.playedProfileIds, + ); + if (existingCandidate && puzzleSession) { + setPuzzleRun( + advanceLocalPuzzleLevelWithWork( + puzzleRun, + buildPuzzleCandidateWorkSummary( + existingCandidate, + puzzleSession, + puzzleRun.currentLevelIndex + 1, + ), + ), + ); + return; + } + + if (!puzzleSession?.draft) { + const sourceSessionId = selectedPuzzleDetail?.sourceSessionId?.trim(); + if (sourceSessionId) { + const { session } = await getPuzzleAgentSession(sourceSessionId); + setPuzzleSession(session); + if (session.draft) { + setIsPuzzleNextLevelGenerating(true); + const response = await executePuzzleAgentAction(session.sessionId, { + action: 'generate_puzzle_images', + candidateCount: 2, + }); + setPuzzleOperation(response.operation); + setPuzzleSession(response.session); + const sourceSessionCandidate = pickPuzzleCandidateForLevel( + response.session.draft?.candidates ?? [], + puzzleRun.playedProfileIds, + ); + if (sourceSessionCandidate) { + setPuzzleRun( + advanceLocalPuzzleLevelWithWork( + puzzleRun, + buildPuzzleCandidateWorkSummary( + sourceSessionCandidate, + response.session, + puzzleRun.currentLevelIndex + 1, + ), + ), + ); + return; + } + } + } + throw new Error('当前拼图缺少可用于生成下一关的草稿会话。'); + } + + setIsPuzzleNextLevelGenerating(true); + const response = await executePuzzleAgentAction(puzzleSession.sessionId, { + action: 'generate_puzzle_images', + candidateCount: 2, + }); + setPuzzleOperation(response.operation); + setPuzzleSession(response.session); + + const generatedCandidate = pickPuzzleCandidateForLevel( + response.session.draft?.candidates ?? [], + puzzleRun.playedProfileIds, + ); + if (generatedCandidate) { + setPuzzleRun( + advanceLocalPuzzleLevelWithWork( + puzzleRun, + buildPuzzleCandidateWorkSummary( + generatedCandidate, + response.session, + puzzleRun.currentLevelIndex + 1, + ), + ), + ); + return; + } + + setPuzzleRun(advanceLocalPuzzleLevel(puzzleRun)); + } catch (error) { + setPuzzleError(resolvePuzzleErrorMessage(error, '准备下一关失败。')); + } finally { + setIsPuzzleNextLevelGenerating(false); + setIsPuzzleBusy(false); + } + }, [ + isPuzzleBusy, + puzzleRun, + puzzleSession, + resolvePuzzleErrorMessage, + selectedPuzzleDetail, + ]); const leaveAgentWorkspace = useCallback(() => { enterCreateTab(); @@ -1802,6 +2041,49 @@ export function PlatformEntryFlowShellImpl({ )} + {selectionStage === 'big-fish-generating' && ( + + } + > + { + setSelectionStage('big-fish-agent-workspace'); + }} + onRetry={() => { + void executeBigFishAction({ action: 'big_fish_compile_draft' }); + }} + onInterrupt={undefined} + backLabel="返回创作中心" + settingActionLabel={null} + retryLabel="重新生成草稿" + settingTitle="当前玩法信息" + settingDescription={null} + progressTitle="大鱼吃小鱼草稿生成进度" + activeBadgeLabel="草稿生成中" + pausedBadgeLabel="草稿生成已暂停" + idleBadgeLabel="等待返回工作区" + /> + + + )} + {selectionStage === 'big-fish-result' && bigFishSession?.draft && ( )} + {selectionStage === 'puzzle-generating' && ( + + } + > + { + setSelectionStage('puzzle-agent-workspace'); + }} + onRetry={() => { + void executePuzzleAction({ action: 'compile_puzzle_draft' }); + }} + onInterrupt={undefined} + backLabel="返回创作中心" + settingActionLabel={null} + retryLabel="重新生成草稿" + settingTitle="当前拼图信息" + settingDescription={null} + progressTitle="拼图草稿生成进度" + activeBadgeLabel="草稿生成中" + pausedBadgeLabel="草稿生成已暂停" + idleBadgeLabel="等待返回工作区" + /> + + + )} + {selectionStage === 'puzzle-result' && puzzleSession?.draft && ( { setSelectionStage('puzzle-gallery-detail'); @@ -1955,6 +2280,17 @@ export function PlatformEntryFlowShellImpl({ void advancePuzzleLevel(); }} /> + {isPuzzleNextLevelGenerating ? ( +
+
+ +
正在准备下一关
+
+ 广场暂无可接续作品,正在生成新的候选图。 +
+
+
+ ) : null}
)} diff --git a/src/components/platform-entry/platformEntryTypes.ts b/src/components/platform-entry/platformEntryTypes.ts index d7d76c5c..081c98a8 100644 --- a/src/components/platform-entry/platformEntryTypes.ts +++ b/src/components/platform-entry/platformEntryTypes.ts @@ -9,9 +9,11 @@ export type SelectionStage = | 'detail' | 'agent-workspace' | 'big-fish-agent-workspace' + | 'big-fish-generating' | 'big-fish-result' | 'big-fish-runtime' | 'puzzle-agent-workspace' + | 'puzzle-generating' | 'puzzle-result' | 'puzzle-gallery-detail' | 'puzzle-runtime' diff --git a/src/services/miniGameDraftGenerationProgress.ts b/src/services/miniGameDraftGenerationProgress.ts new file mode 100644 index 00000000..07b7da1f --- /dev/null +++ b/src/services/miniGameDraftGenerationProgress.ts @@ -0,0 +1,283 @@ +import type { BigFishSessionSnapshotResponse } from '../../packages/shared/src/contracts/bigFish'; +import type { PuzzleAgentSessionSnapshot } from '../../packages/shared/src/contracts/puzzleAgentSession'; +import type { + CustomWorldGenerationProgress, + CustomWorldGenerationStep, +} from '../../packages/shared/src/contracts/runtime'; +import type { CustomWorldStructuredAnchorEntry } from './customWorldAgentGenerationProgress'; + +export type MiniGameDraftGenerationKind = 'puzzle' | 'big-fish'; + +export type MiniGameDraftGenerationPhase = + | 'idle' + | 'compile' + | 'puzzle-images' + | 'puzzle-select-image' + | 'big-fish-main-images' + | 'big-fish-motions' + | 'big-fish-background' + | 'ready' + | 'failed'; + +export type MiniGameDraftGenerationState = { + kind: MiniGameDraftGenerationKind; + phase: MiniGameDraftGenerationPhase; + startedAtMs: number; + completedAssetCount: number; + totalAssetCount: number; + error: string | null; +}; + +type MiniGameStepDefinition = { + id: MiniGameDraftGenerationPhase; + label: string; + detail: string; + weight: number; +}; + +type MiniGameAnchorSource = { + key: string; + label: string; + value: string; +}; + +const PUZZLE_STEPS = [ + { + id: 'compile', + label: '编译拼图草稿', + detail: '整理主题、主体、构图与标签。', + weight: 34, + }, + { + id: 'puzzle-images', + label: '生成拼图图片', + detail: '根据草稿生成候选图。', + weight: 33, + }, + { + id: 'puzzle-select-image', + label: '确认正式图片', + detail: '选择候选图写入结果页。', + weight: 33, + }, +] as const satisfies ReadonlyArray; + +const BIG_FISH_STEPS = [ + { + id: 'compile', + label: '编译玩法草稿', + detail: '生成关卡角色描述、生态背景与运行参数。', + weight: 25, + }, + { + id: 'big-fish-main-images', + label: '生成角色图片', + detail: '为每个成长阶段生成主形象。', + weight: 30, + }, + { + id: 'big-fish-motions', + label: '生成动作素材', + detail: '补齐漂浮与游动动作素材。', + weight: 30, + }, + { + id: 'big-fish-background', + label: '生成场地背景', + detail: '生成玩法场地背景图。', + weight: 15, + }, +] as const satisfies ReadonlyArray; + +function clampProgress(value: number) { + return Math.max(0, Math.min(100, Math.round(value))); +} + +function getStepDefinitions(kind: MiniGameDraftGenerationKind) { + return kind === 'puzzle' ? PUZZLE_STEPS : BIG_FISH_STEPS; +} + +function getActiveStepIndex( + steps: ReadonlyArray, + phase: MiniGameDraftGenerationPhase, +) { + if (phase === 'ready') { + return steps.length - 1; + } + const index = steps.findIndex((step) => step.id === phase); + return index >= 0 ? index : 0; +} + +function buildMiniGameProgressSteps( + steps: ReadonlyArray, + activeStepIndex: number, + state: MiniGameDraftGenerationState, +) { + return steps.map((step, index) => { + const isCompleted = state.phase === 'ready' || index < activeStepIndex; + const isActive = state.phase !== 'failed' && !isCompleted && index === activeStepIndex; + const isAssetStep = step.id === state.phase && state.totalAssetCount > 0; + + return { + id: step.id, + label: step.label, + detail: step.detail, + completed: isCompleted + ? 1 + : isAssetStep + ? state.completedAssetCount + : 0, + total: isAssetStep ? state.totalAssetCount : 1, + status: isCompleted ? 'completed' : isActive ? 'active' : 'pending', + } satisfies CustomWorldGenerationStep; + }); +} + +export function createMiniGameDraftGenerationState( + kind: MiniGameDraftGenerationKind, +): MiniGameDraftGenerationState { + return { + kind, + phase: 'compile', + startedAtMs: Date.now(), + completedAssetCount: 0, + totalAssetCount: 0, + error: null, + }; +} + +export function buildMiniGameDraftGenerationProgress( + state: MiniGameDraftGenerationState | null, + nowMs = Date.now(), +): CustomWorldGenerationProgress | null { + if (!state) { + return null; + } + + const steps = getStepDefinitions(state.kind); + const activeStepIndex = getActiveStepIndex(steps, state.phase); + const completedWeight = steps + .slice(0, state.phase === 'ready' ? steps.length : activeStepIndex) + .reduce((sum, step) => sum + step.weight, 0); + const activeStep = steps[activeStepIndex] ?? steps[0]; + const assetRatio = + state.totalAssetCount > 0 + ? Math.min(1, state.completedAssetCount / state.totalAssetCount) + : state.phase === 'ready' + ? 1 + : 0; + const overallProgress = + state.phase === 'failed' + ? Math.max(1, completedWeight) + : state.phase === 'ready' + ? 100 + : completedWeight + activeStep.weight * assetRatio; + + return { + phaseId: state.phase, + phaseLabel: + state.phase === 'failed' + ? '生成失败' + : state.phase === 'ready' + ? '生成完成' + : activeStep.label, + phaseDetail: + state.error ?? + (state.phase === 'ready' + ? '完整草稿与资产已准备完成。' + : activeStep.detail), + batchLabel: activeStep.label, + overallProgress: clampProgress(overallProgress), + completedWeight: clampProgress(overallProgress), + totalWeight: 100, + elapsedMs: Math.max(0, nowMs - state.startedAtMs), + estimatedRemainingMs: state.phase === 'ready' ? 0 : null, + activeStepIndex, + steps: buildMiniGameProgressSteps(steps, activeStepIndex, state), + }; +} + +export function buildPuzzleGenerationAnchorEntries( + session: PuzzleAgentSessionSnapshot | null | undefined, +): CustomWorldStructuredAnchorEntry[] { + if (!session) { + return []; + } + + const draft = session.draft; + const entries: Array = [ + session.anchorPack.themePromise, + session.anchorPack.visualSubject, + session.anchorPack.visualMood, + session.anchorPack.compositionHooks, + session.anchorPack.tagsAndForbidden, + draft + ? { + key: 'draft-summary', + label: '草稿摘要', + value: draft.summary, + } + : null, + draft?.coverImageSrc + ? { + key: 'cover-image', + label: '正式图片', + value: '已生成并应用', + } + : null, + ]; + + return entries + .filter((entry): entry is MiniGameAnchorSource => Boolean(entry)) + .map((entry) => ({ + id: entry.key, + label: entry.label, + value: entry.value, + })) + .filter((entry) => entry.value.trim()); +} + +export function buildBigFishGenerationAnchorEntries( + session: BigFishSessionSnapshotResponse | null | undefined, +): CustomWorldStructuredAnchorEntry[] { + if (!session) { + return []; + } + + const draft = session.draft; + const assetReadyCount = session.assetSlots.filter( + (slot) => slot.status === 'ready', + ).length; + + const entries: Array = [ + session.anchorPack.gameplayPromise, + session.anchorPack.ecologyVisualTheme, + session.anchorPack.growthLadder, + session.anchorPack.riskTempo, + draft + ? { + key: 'level-characters', + label: '角色描述', + value: draft.levels + .map((level) => `Lv.${level.level} ${level.name}:${level.oneLineFantasy}`) + .join('\n'), + } + : null, + draft + ? { + key: 'asset-coverage', + label: '图片与动作', + value: `已生成 ${assetReadyCount}/${session.assetSlots.length} 个资产`, + } + : null, + ]; + + return entries + .filter((entry): entry is MiniGameAnchorSource => Boolean(entry)) + .map((entry) => ({ + id: entry.key, + label: entry.label, + value: entry.value, + })) + .filter((entry) => entry.value.trim()); +}