codex/rpg-creation-cg-fix #29

Merged
kdletters merged 5 commits from codex/rpg-creation-cg-fix into master 2026-05-22 03:26:51 +08:00
44 changed files with 4043 additions and 205 deletions
+1
View File
@@ -36,6 +36,7 @@ temp*build*/
/.codex-temp
/target/
/logs
/server-rs/crates/*/logs/
.worktrees/
.env.secrets.local
spacetime.local.json
+16
View File
@@ -149,6 +149,14 @@
- 验证方式:新增玩法 PRD 必须显式声明单图资产槽位和系列素材槽位;新增工作台测试确认没有默认聊天式 Agent 输入;skill 通过 `quick_validate.py`
- 关联文档:`docs/【玩法创作】平台入口与玩法链路-2026-05-15.md``.codex/skills/genarrative-play-type-integration/SKILL.md``.hermes/skills/genarrative-play-type-integration/SKILL.md`
## 2026-05-21 RPG publish_world 设定文本以后端草稿真相派生
- 背景:RPG 结果页发布动作只保证提交 `{ action: 'publish_world' }`;旧 agent 会话可能没有 `seed_text`,但 `draft_profile_json` 已经通过 `publish_gate` 并可发布。
- 决策:发布正式世界时,`spacetime-module` 不再把 `session.seed_text` 当作唯一 `setting_text` 兜底,而是调用 `module-custom-world::resolve_custom_world_publish_setting_text(...)` 从 payload、当前草稿 profile 和 seed 依次派生。
- 影响范围:RPG / custom-world agent 发布链路、`custom_world_profile` 编译入库、公开 gallery 投影。
- 验证方式:`cargo test -p module-custom-world publish_setting_text --manifest-path server-rs\Cargo.toml``cargo check -p spacetime-module --manifest-path server-rs\Cargo.toml`;本地 api-server 重启后检查 `/healthz`
- 关联文档:`docs/【玩法创作】平台入口与玩法链路-2026-05-15.md``docs/【后端架构】server-rs与SpacetimeDB数据契约-2026-05-15.md``.hermes/shared-memory/pitfalls.md`
## 2026-05-19 系列素材 n*n 图集抽为 api-server 通用模块
- 背景:抓大鹅物品 sheet 已包含 prompt 组装、固定网格切图、绿幕 / 近白底透明化、切片 PNG 持久化和 prompt 追踪;继续留在 Match3D 私有模块会让跳一跳、后续地块 / 道具类玩法重复复制同一套算法和 OSS 元数据口径。
@@ -387,6 +395,14 @@
- 验证方式:执行入口配置、创作 Hub、平台入口交互和 api-server 路由熔断定向测试,确认“视觉小说”不出现在创作页且 `/api/creation/visual-novel/*` 默认被熔断。
- 关联文档:`docs/design/PLATFORM_CREATE_TAB_CREATIVE_AGENT_HOME_2026-05-05.md``docs/technical/ADMIN_CREATION_ENTRY_SWITCH_CONFIG_2026-05-11.md`
## 2026-05-20 RPG 创作入口开放
- 背景:RPG 文字冒险能力已经具备历史 custom-world 创作和运行闭环,但入口默认种子仍 `visible=false`,创作页不展示。
- 决策:SpacetimeDB `creation_entry_type_config` 默认种子中 `rpg.visible=true``open=true`,旧默认隐藏配置只在标题、subtitle、badge、图片、排序和开关完全匹配时迁移为可见可创建。`airp` 仍保持 AI RPG 占位,不接管当前 RPG 链路。结构化创作 / RPG JSON 链路默认关闭 Responses `web_search`,需要联网增强时才通过 `GENARRATIVE_RPG_LLM_WEB_SEARCH_ENABLED=true``GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED=true` 显式启用;未开通工具的上游会返回 `ToolNotOpen`,不能把这类失败暴露成“模型返回结果解析失败”。
- 影响范围:创作入口默认种子、旧库入口纠偏、`api-server` 入口熔断、创作页模板 Tab、创作 Hub 测试、玩法链路文档和后端路由文档。
- 验证方式:执行入口配置、api-server 路由熔断、创作 Hub 和平台入口交互定向测试,确认“文字冒险”出现在创作入口,`/api/runtime/custom-world*``/api/story/*``/api/runtime/chat/*` 都按 `rpg` 入口开关熔断。
- 关联文档:`docs/【玩法创作】平台入口与玩法链路-2026-05-15.md``docs/【后端架构】server-rs与SpacetimeDB数据契约-2026-05-15.md`
## 2026-05-10 运行态输入设备抽象层全项目通用化
- 背景:拼图运行态接入 mocap 后,鼠标/触控和 mocap 各自维护输入逻辑会导致合并大块、拖拽语义和取消会话行为不一致;后续其他玩法也需要复用体感、摇杆、键盘等设备输入。
+72
View File
@@ -48,6 +48,46 @@
- 验证:拼图入口测试仍可通过,且新组件可通过不同页面复用而不需要复制上传卡实现。
- 关联:`src/components/common/CreativeImageInputPanel.tsx``src/components/puzzle-agent/PuzzleAgentWorkspace.tsx`
## RPG 发布不能只依赖 agent session seed_text
- 现象:RPG 结果页 `publish_world` 返回 `UPSTREAM_ERROR`details 为 `custom_world.setting_text 不能为空`;同一 session 的 `result-view` 日志显示 `publish_ready=true`
- 原因:前端发布动作只提交 `{ action: 'publish_world' }`,旧 agent 会话的 `seed_text` 可能为空;如果后端只从 action payload 或 `seed_text``setting_text`,就会在最终 compile / publish 校验阶段失败。
- 处理:`module-custom-world::resolve_custom_world_publish_setting_text(...)` 以当前 `draft_profile_json` 为草稿真相,优先读取 `settingText``creatorIntent.rawSettingText``creatorIntent.worldHook``worldHook``anchorContent.worldPromise(.hook)``summary``name/title`,最后才回退 `seed_text`
- 验证:`cargo test -p module-custom-world publish_setting_text --manifest-path server-rs\Cargo.toml``cargo check -p spacetime-module --manifest-path server-rs\Cargo.toml`
- 关联:`server-rs/crates/module-custom-world/src/application.rs``server-rs/crates/spacetime-module/src/custom_world.rs``docs/【玩法创作】平台入口与玩法链路-2026-05-15.md`
## RPG 已发布结果页进入世界不能重复 publish_world
- 现象:RPG 草稿发布成功后,按钮文案已变为“进入世界”,但点击仍请求 `POST /api/runtime/custom-world/agent/sessions/{sessionId}/actions` 且 payload 为 `{"action":"publish_world"}`,后端返回 `publish_world is only available during object_refining, visual_refining, long_tail_review or ready_to_publish`
- 原因:按钮文案依据 agent session `stage === 'published'` 切换,但点击处理仍走发布协调路径;如果前端只依赖草稿同步回包判断是否已发布,回包为空或缺少可进入状态时就会继续重复发送 `publish_world`
- 处理:进入世界协调器接收当前 agent session stage;当 stage 已为 `published` 时,只调用 `result-view` 回读已发布 profile 并启动运行态,不再调用 `sync_result_profile``publish_world`
- 验证:`npm run test -- src/components/rpg-entry/useRpgCreationEnterWorld.test.tsx`;确认已发布场景下 `syncAgentDraftResultProfile``executePublishWorld` 均未被调用。
- 关联:`src/components/rpg-entry/useRpgCreationEnterWorld.ts``src/components/platform-entry/PlatformEntryFlowShellImpl.tsx``docs/【玩法创作】平台入口与玩法链路-2026-05-15.md`
## RPG 点击启动黑屏 / 默认 profile 先查 profile 归一化和摘要覆盖
- 现象:作品详情点击“启动”后页面切到 RPG runtime,但用户只看到黑屏、空白,或进入默认角色 / 默认 profile;从作品详情点“作品编辑”后开局 CG、封面、角色图、技能动作预览、初始物品图标或场景背景图丢失;DevTools 里可能同时看到旧自动存档 `/api/runtime/save/snapshot` 被主动 cancel。
- 原因:`/custom-world-library` / `/custom-world-gallery` 详情接口可能返回历史或摘要式 `profile`,缺少 `playableNpcs``storyNpcs``landmarks``attributeSchema` 等运行态字段;前端 client 若直接把该对象传给 runtime,角色选择首屏会在 `buildCustomWorldPlayableCharacters(profile)` 或后续属性解析处抛错。另一类常见原因是详情接口已回读完整 profile 后,`savedCustomWorldEntries` 里的列表摘要又把 `selectedDetailEntry` 覆盖回空 profile,导致启动或编辑时只剩卡片摘要。发布 / 回读 result-view 若返回字段更少的旧视图,也可能把当前结果页已编辑资产降级掉。`save/snapshot (canceled)` 通常是切 runtime 或卸载时 `AbortController` 取消旧自动存档,不是黑屏根因。
- 处理:RPG 入口作品库 client 在所有返回 `CustomWorldLibraryEntry<CustomWorldProfile>` 的接口边界统一调用 `normalizeCustomWorldProfileRecord`,并用 `profileId/worldName/subtitle/summaryText` 补齐旧数据缺字段;详情页已拿到运行态字段或资产槽位更多的完整 profile 时,不允许列表摘要覆盖当前详情;同一 `profile.id` 下,正式进入世界发布 / 回读不得用字段更少的后端旧视图降级当前结果页 profile。`normalizeCustomWorldProfileRecord` 必须近似无损保留 `cover``openingCg``camp.narrativeResidues``landmark.visualDescription/narrativeResidues``skills[].actionPreviewConfig``initialItems[].iconSrc``attributeSchema`、角色 `attributeProfile``sceneChapterBlueprints[].acts[]` 的背景与结构字段;只有背景资产的 act 也不能被过滤。角色选择页对角色生成异常或空数组回退默认角色,并保留返回按钮/轻量空态;顶层 runtime 懒加载 fallback 不使用纯 `null`
- 验证:`npm run test -- src/components/rpg-entry/RpgEntryFlowShell.agent.interaction.test.tsx -t "creation hub published work start uses loaded detail profile instead of library summary|creation hub published work edit keeps loaded detail profile assets instead of library summary"``npm run test -- src/data/customWorldLibrary.test.ts -t "保留结果页封面和关键图片资产槽位|近似无损保留编辑态和运行态结构字段|保留只有背景资产的场景幕"``npm run test -- src/components/rpg-entry/useRpgEntryAgentDraftRestore.test.tsx -t "默认封面和角色编辑结构差异也不能被列表摘要覆盖"``npm run test -- src/components/rpg-entry/useRpgCreationEnterWorld.test.tsx -t "正式进入世界回读结果页字段更少时不降级当前完整 profile"``npm run typecheck`
- 关联:`src/components/rpg-entry/useRpgEntryLibraryDetail.ts``src/components/rpg-entry/useRpgCreationEnterWorld.ts``src/data/customWorldLibrary.ts``src/services/rpg-entry/rpgEntryLibraryClient.ts``src/components/rpg-entry/RpgEntryCharacterSelectView.tsx``src/App.tsx``src/components/rpg-runtime-shell/RpgRuntimeShell.tsx``docs/【玩法创作】平台入口与玩法链路-2026-05-15.md`
## RPG 战后一轮战斗后卡在观察/试探/调息先查 post-battle finalization
- 现象:RPG 一轮战斗胜利后,运行态只显示默认 `观察周围迹象 / 主动出声试探 / 原地调息`,这些按钮只有文字反馈;点“继续冒险”后又回到同样选项,点探索只播退场/进场动画,场景和剧情不推进。
- 原因:终局战斗 action 如果只走通用 `resolve_story_runtime_action` fallback,而没有在后端调用 `finalize_post_battle_resolution(...)`,就不会持久写入 `story_continue_adventure``deferredOptions` 和下一幕 `currentSceneActState`。另外旧 bootstrap 快照可能只有 `connectedSceneIds` / `forwardSceneId`、没有 `connections`,战后选项生成若只读 `connections` 也会退回 `idle_explore_forward` 循环。
- 处理:`module-runtime-story` 在 story action 投影后统一调用 post-battle finalization`idle_explore_forward` 清理战斗态并生成下一段遭遇预览;`idle_travel_next_scene` / `camp_travel_home_scene` 由后端写入新 `currentScenePreset`、场景 act 状态、遭遇预览和 `runtimeStats.scenesTraveled`。前端只负责播放继续、探索和切场景动画,不承接正式剧情推进真相。
- 验证:`cargo test -p module-runtime-story --manifest-path server-rs\Cargo.toml battle_tests -- --nocapture` 应覆盖战斗终局持久化 `story_continue_adventure``deferredOptions`、下一幕 act,以及 `idle_travel_next_scene` 真正切换场景。
- 关联:`server-rs/crates/module-runtime-story/src/session_action.rs``server-rs/crates/module-runtime-story/src/post_battle.rs``server-rs/crates/module-runtime-story/src/battle_tests.rs``docs/【玩法创作】平台入口与玩法链路-2026-05-15.md`
## RPG 战斗飘字不要只靠低对比红绿文字
- 现象:暗色或棕黑噪声背景下,战斗伤害飘字看起来像背景纹理,尤其是远端敌人头顶的小号红字几乎不可读。
- 原因:旧 `CombatFloatingNumber` 主要依赖 `text-rose-200` / `text-emerald-200` 和 8px 同色 glow;在暗红、棕黑、像素噪声背景上,颜色与背景混在一起,1px 深色描边也不足以形成轮廓。
- 处理:飘字本体使用高亮近白文字、小面积半透明深色底、明显深色描边和多层黑色阴影;只增强瞬时反馈,不新增说明面板,不遮挡主要战斗画面。
- 验证:`npm run test -- src/components/game-canvas/GameCanvasEntityLayer.test.tsx` 覆盖伤害/治疗飘字样式策略;运行态截图中敌方头顶伤害数字应能在暗场景上辨认。
- 关联:`src/components/game-canvas/GameCanvasEntityLayer.tsx``docs/【项目基线】当前产品与工程约束-2026-05-15.md`
## 弹窗里复用 CreativeImageInputPanel 要保留画面卡高度
- 现象:拼图草稿结果页的关卡详情弹窗中仍能看到“画面图”标题、画面描述和生成按钮,但实际画面图卡片视觉上消失。
@@ -432,6 +472,30 @@
- 验证:运行 `npm run test -- src/services/creation-agent/creationAgentClientFactory.test.ts src/services/apiClient.test.ts``cargo test -p api-server puzzle_vector_engine --manifest-path server-rs/Cargo.toml`,真实联调重启 `npm run dev:api-server` 后检查 `/healthz`
- 关联:`src/services/creation-agent/creationAgentClientFactory.ts``server-rs/crates/api-server/src/puzzle.rs``docs/technical/API_SERVER_EXTERNAL_SERVICE_ENV_CONFIG_2026-05-07.md`
## 开局 CG 故事板生图失败先查 VectorEngine 请求预算和旧进程
- 现象:RPG 结果页点击开局 CG 后,`POST /api/runtime/custom-world/opening-cg` 在较长等待后返回“开局 CG 故事板生成失败:创建图片生成任务失败:error sending request for url (https://api.vectorengine.ai/v1/images/generations)”。
- 原因:该故事板会把角色图和首幕背景图作为参考图一起传给 VectorEngine `gpt-image-2-all`,请求体和上游生成耗时都比普通单图更大;若运行中的 `api-server` 仍沿用旧 `VECTOR_ENGINE_IMAGE_REQUEST_TIMEOUT_MS`,或者参考图过大,会在请求发送/等待阶段被 reqwest 截断。日志里 `timeout=false connect=false request=true body=false source=client error (SendRequest)` 表示还没拿到上游 HTTP 响应,通常优先怀疑大 JSON 请求体、上游网关中断或 HTTP 协议兼容,而不是业务响应解析失败。直接请求 VectorEngine 若无效 token 可快速返回 401,不能据此判断真实生图不会超时。
- 处理:开局 CG 参考图入参先压到单边 768 的 JPEG;`/v1/images/generations` 保持 reqwest 默认 HTTP 协商,只有 multipart `/v1/images/edits` 单独强制 HTTP/1.1。后端图片 helper 将 `request_body_bytes`、每张参考图 Data URL 长度、`timeout/connect/body/source/rootSource/sourceChain/endpoint` 分类写入日志和 `error.details`,前端优先展示 `details.reason`。修改 `.env.secrets.local` 后必须重启 `api-server``npm run dev` 终端用 `rs api-server`,否则旧进程仍按旧超时运行。
- 验证:分别运行 `cargo test -p api-server custom_world_ai --manifest-path server-rs/Cargo.toml``cargo test -p api-server openai_image_generation --manifest-path server-rs/Cargo.toml`;真实联调重启后再触发开局 CG,若仍失败看返回的 `details.reason/source/rootSource/sourceChain/timeout/connect/body/endpoint``logs/api-server/` 同一 request_id。
- 关联:`server-rs/crates/api-server/src/custom_world_ai.rs``server-rs/crates/api-server/src/custom_world_ai/opening_cg.rs``server-rs/crates/api-server/src/openai_image_generation.rs``docs/【开发运维】本地开发验证与生产运维-2026-05-15.md`
## 开局 CG 成功后又变空白要保留 profile.openingCg
- 现象:RPG 结果页里的开局 CG 成功显示一瞬后,窗口又退回空白占位。
- 原因:`openingCg` 只存在于结果页 profile 槽位,如果父层在 `onProfileChange` 后重新同步了 profile,却经过 `normalizeCustomWorldProfileRecord` 或作品库写回时丢掉 `openingCg`,预览就会从视频 / 故事板回退为空白。
- 处理:`src/data/customWorldLibrary.ts` 的 profile 归一化必须透传 `openingCg`;结果页和父层后续同步都应把它当作受控资产槽位,而不是临时 UI 状态。
- 验证:`npm run test -- src/data/customWorldLibrary.test.ts src/components/CustomWorldResultView.test.tsx`,确认生成后即使父层做一次归一化回写,开局 CG 仍继续显示。
- 关联:`src/data/customWorldLibrary.ts``src/components/rpg-creation-result/RpgCreationResultViewImpl.tsx``src/components/CustomWorldEntityCatalog.tsx`
## RPG 发布报 legacy_result_profile_json 非法先查 null 兼容
- 现象:RPG 结果页发布动作返回 `UPSTREAM_ERROR`SpacetimeDB details 里是 `custom_world.compile.legacy_result_profile_json 不是合法 JSON object`
- 原因:`publish_world` 前端契约只要求 `{ action: 'publish_world' }``ExecuteCustomWorldAgentActionRequest.legacy_result_profile` 是可选字段,经 HTTP / serde / SpacetimeDB payload 传递时可能显式成为 JSON `null`。旧的编译器只接受 object 或缺省,把 `Some("null")` 当成非法 legacy JSON。
- 处理:`module-custom-world` 的 optional JSON object 解析要把 `null` 视为未提供,仍拒绝数组、字符串、数字和坏 JSON;正式发布继续以 session `draft_profile_json` 为草稿真相。
- 验证:`cargo test -p module-custom-world published_profile_compile --manifest-path server-rs/Cargo.toml`
- 关联:`server-rs/crates/module-custom-world/src/application.rs``server-rs/crates/spacetime-module/src/custom_world.rs``docs/【玩法创作】平台入口与玩法链路-2026-05-15.md`
## 本地脚本调 VectorEngine 生图卡住先区分 fetch 首部超时
- 现象:用 Node `fetch` 直接请求 `POST /v1/images/generations`,已经设置较长的 AbortController 超时,但仍在约 180 到 300 秒后抛 `AbortError``TypeError: fetch failed``UND_ERR_HEADERS_TIMEOUT`;同一 prompt 改用原生 `https.request` 可以在较短时间内成功返回图片。
@@ -1147,3 +1211,11 @@
- 处理:打开草稿时把持久化 `generationStatus=generating` 等同于生成中 notice,恢复对应玩法生成进度页;恢复计时使用作品摘要 `updatedAt` 推导 `startedAtMs`
- 验证:`npm test -- src/components/rpg-entry/RpgEntryFlowShell.agent.interaction.test.tsx -t "persisted generating"`
- 关联:`src/components/platform-entry/PlatformEntryFlowShellImpl.tsx``src/components/rpg-entry/RpgEntryFlowShell.agent.interaction.test.tsx``docs/【玩法创作】平台入口与玩法链路-2026-05-15.md`
## 存档选择入口不要只藏在“玩过”弹窗里
- 现象:用户有 RPG / 拼图运行态存档,但平台底部 `草稿` Tab 只展示作品架,个人中心只有点击 `玩过` 后才可能看到“可继续”,导致看起来没有存档选择入口。
- 原因:`/api/profile/save-archives` 已在入口 bootstrap 加载,但前端只把 `saveEntries` 注入 `ProfilePlayedWorksModal`;没有独立的存档入口。
- 处理:个人中心 `常用功能` 必须保留 `存档` 快捷入口,点击后打开独立存档选择弹窗并复用 `SaveArchiveCard`;恢复仍走 `/api/profile/save-archives/{worldKey}`,拼图存档继续走拼图 resume 分支,RPG 走 `handleContinueGame(snapshot)`
- 验证:`npm run test -- src/components/rpg-entry/RpgEntryFlowShell.agent.interaction.test.tsx -t "profile page exposes save archive picker"`
- 关联:`src/components/rpg-entry/RpgEntryHomeView.tsx``src/components/platform-entry/PlatformEntryFlowShellImpl.tsx``src/components/rpg-entry/useRpgEntryBootstrap.ts``docs/【玩法创作】平台入口与玩法链路-2026-05-15.md`
@@ -329,6 +329,7 @@ npm run check:server-rs-ddd
- Rust 结构体:`CustomWorldAgentSession`
- 源码:`server-rs/crates/spacetime-module/src/custom_world.rs`
- 发布约束:`publish_world` 的 action payload 不要求携带 `settingText``spacetime-module` 调用 `module-custom-world::resolve_custom_world_publish_setting_text(...)`,优先从当前 `draft_profile_json` 草稿真相派生正式 `setting_text`,避免旧会话 `seed_text` 为空时在最终 compile / publish 阶段触发 `custom_world.setting_text 不能为空`
### `custom_world_draft_card`
@@ -601,6 +602,10 @@ npm run check:server-rs-ddd
`GET /api/creation-entry/config` 和入口熔断优先从订阅 cache 读取创作入口配置;cache 缺失时使用最近一次成功读取的内存快照,再兜底调用 `get_creation_entry_config` procedure 完成空库种子或旧库兼容。
RPG 创作入口的配置 ID 是 `rpg`,当前 `visible=true``open=true`;历史 `custom-world` 路由仍是 RPG 的工程域与运行态源类型。入口熔断把 `/api/runtime/custom-world*``/api/story/*``/api/runtime/chat/*` 统一映射到 `rpg`,不要新增平行 `airp` 路由或用 `airp` 接管当前文字冒险链路。
结构化创作和 RPG 的 LLM JSON 链路默认不启用 Responses `web_search`;只有在明确需要联网增强时,才通过 `GENARRATIVE_RPG_LLM_WEB_SEARCH_ENABLED``GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED` 显式打开。否则未开通工具的上游会先吐自然语言再返回 `ToolNotOpen`,这类失败要按上游工具不可用处理,不要误判成模型返回结果解析失败。
未来可选:若发现页、推荐流和各玩法广场需要统一给浏览器前端直接订阅公开作品列表,只新增 / 统一专用 public read model,例如 `public_work_gallery_entry`。该 read model 必须是后端投影后的公开作品卡片契约,覆盖作品类型、公开作品号、标题、摘要、封面、作者展示名、排序键、公开统计和入口开关后的可见性,不暴露玩法领域源表 row shape。前端可选择订阅这个稳定投影来减少 HTTP 拉取,但不能订阅 `puzzle_work_profile``custom_world_profile` 等源表后自行拼装列表;BFF 仍保留首屏、SEO / 分享、旧客户端、订阅失败和灰度期间的 HTTP fallback。
### `quest_log`
@@ -45,6 +45,8 @@ npm run dev:api-server
后端日志默认写入 `logs/api-server/`。后端 API smoke 使用 `npm run dev:api-server` 并检查 `/healthz`;不要使用旧 `api-server:maincloud` 或任何 `GENARRATIVE_SPACETIME_MAINCLOUD_*` 口径。
本地 `.env``.env.local``.env.secrets.local` 修改后必须重启 `api-server` 才会生效;若已经通过 `npm run dev` 启动完整联调,可在该终端输入 `rs api-server`。排查 RPG / 拼图 / 抓大鹅等 VectorEngine 生图链路时,确认 `VECTOR_ENGINE_BASE_URL``VECTOR_ENGINE_API_KEY``VECTOR_ENGINE_IMAGE_REQUEST_TIMEOUT_MS` 只在本地或服务器密钥文件中配置,不能写入 Git。开局 CG 故事板、首图、背景和图集都属于长耗时图片请求;后端默认会把 `VECTOR_ENGINE_IMAGE_REQUEST_TIMEOUT_MS` 下限收口到 `1000000`,旧进程仍可能沿用重启前的短超时。若开局 CG 故事板在 `send()` 阶段失败且日志显示 `SendRequest`,先看同一 request_id 的 `request_body_bytes``reference_data_url_bytes``sourceChain``rootSource`;当前开局 CG 会把角色图与首幕背景图压到单边 768 的 JPEG 后再作为 generations `image` 数组发送,`/v1/images/generations` 使用默认 HTTP 协商,只有 multipart `/v1/images/edits` 单独强制 HTTP/1.1。
查看本地 Rust / SpacetimeDB 日志:
```bash
@@ -241,6 +243,8 @@ OpenTelemetry 现阶段默认开启 OTLP traces / metrics / logs,但本地日
- `WECHAT_*`
- `ALIYUN_OSS_*`
结构化创作 / RPG 的 Responses JSON 链路默认不打开 `web_search`;本地和生产如需联网增强,必须显式配置 `GENARRATIVE_RPG_LLM_WEB_SEARCH_ENABLED=true``GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED=true`。如果上游未开通工具,Responses 可能先吐自然语言再返回 `ToolNotOpen`,这类报错应按工具不可用排查,不要先当成 JSON 解析 bug。
### 手机验证码短信
手机验证码发送走阿里云普通短信 `SendSms`,验证码由 `module-auth` 在当前 `api-server` 进程内生成、哈希存储和校验,不再调用阿里云托管验证码的 `SendSmsVerifyCode` / `CheckSmsVerifyCode`。因此 `api-server` 重启后,已发送但未校验的验证码会失效。
@@ -36,6 +36,34 @@
6. 点击 `generationStatus=generating` 的草稿卡必须恢复对应玩法的生成进度页,不能进入空白结果页或普通工作区;恢复生成页的 `startedAtMs` 使用作品摘要 `updatedAt` 推导。
7. 私有 generated 图片必须通过 `ResolvedAssetImage` / `/api/assets/read-url` 换签读取。
## RPG / 自定义世界
当前 RPG 创作入口使用 `playId = rpg`,工程域和运行态源类型沿用历史 `custom-world`。默认入口状态为 `visible=true``open=true`,对外展示为“文字冒险”;`airp` 仍是独立的“AI RPG”占位入口,保持 `open=false`,不要把它当作当前 RPG 创作链路开放。
当前链路为:
```text
创作入口 -> RPG Agent 共创工作台 -> 生成过程页 -> 结果页 -> 进入世界/试玩 -> 发布 -> RPG 运行态
```
RPG 是历史既有链路例外:当前仍使用对话式 Agent 共创工作台和 RPG 资产编辑器体系,不作为新增玩法默认模板复制。新增玩法继续遵循本文默认的表单/图片输入工作台、`CreativeImageInputPanel` 单图槽位和通用系列素材图集生成流程;如果要把 RPG 逐步迁回默认模式,应先补 PRD 和迁移方案,再改代码。
RPG API 仍沿用历史命名空间:`/api/runtime/custom-world*``/api/story/*``/api/runtime/chat/*`。这些路由在 `api-server` 入口熔断中统一映射到 `rpg`,只按 `open` 判断是否允许调用;`visible` 只控制创作页入口展示和作品架可见性。
RPG Agent 结果页点击发布或发布并进入世界时,必须先把结果页当前 profile 通过 `sync_result_profile` 保存回 `custom_world_agent_session.draft_profile_json`,再发送发布动作;发布动作前端契约只允许提交 `{ action: 'publish_world' }``api-server` 只补作者公开信息,不转发 `profile``draftProfile``legacyResultProfile``settingText``spacetime-module` 发布时只读取当前 session 的 `draft_profile_json` 作为草稿真相,从 `settingText``creatorIntent.rawSettingText``creatorIntent.worldHook``worldHook``anchorContent.worldPromise(.hook)``summary``name/title` 依次派生正式 `setting_text`,最后才回退 `seed_text`。不要把 `seed_text` 当作唯一设定来源,旧会话可能为空。
Agent session 已进入 `published` 后,结果页按钮只能执行“进入世界”:前端需先通过 `result-view` 回读已发布 profile 并启动运行态,不得再次调用 `sync_result_profile` 或发送 `{ action: 'publish_world' }``publish_world` 只允许在 `object_refining``visual_refining``long_tail_review``ready_to_publish` 等发布前阶段触发;否则会被后端阶段门槛拒绝。
`legacyResultProfile` 只作为历史结果页 profile 兼容兜底;编译正式 profile 时,session 草稿内已保存字段优先于 legacy 字段,legacy 只能补缺失字段。`publish_world` 不再接受前端临时传入的 legacy 载荷;历史兼容路径中 legacy 缺省或显式为 `null` 时等价于未提供,不得因此报 `custom_world.compile.legacy_result_profile_json 不是合法 JSON object`。真正的数组、字符串、数字等非 object legacy 载荷仍应拒绝。
RPG 结果页开局 CG 是 `profile.openingCg` 资产槽位:`api-server` 负责 VectorEngine / OSS 副作用并返回故事板和视频引用,前端只把结果写回当前 profile;`sync_result_profile`、作品库保存和 `normalizeCustomWorldProfileRecord` 都必须保留该槽位。封面是 `profile.cover` 资产槽位,默认封面也要保留 `sourceType='default'``characterRoleIds`,不能因为没有 `imageSrc` 就当作空封面。若生成成功后画面短暂显示又变回空白,优先检查父层重新同步或 profile 归一化是否把 `openingCg` / `cover` 丢掉,而不是先怀疑已生成资源本身失效。
RPG 从作品架、广场详情或作品号搜索点击“启动”前,入口 client 必须把后端返回的完整 `profile` 先经过 `normalizeCustomWorldProfileRecord`,并用作品条目的 `profileId/worldName/subtitle/summaryText` 补齐旧数据缺失字段;运行态和详情页不得直接消费未归一化的旧 profile。作品架列表或 `savedCustomWorldEntries` 中的摘要 profile 只可用于卡片展示,不可在详情接口已回读完整 profile 后覆盖 `selectedDetailEntry`;若摘要缺少 `playableNpcs``storyNpcs``landmarks``items``sceneChapterBlueprints``cover``openingCg``skills[].actionPreviewConfig``initialItems[].iconSrc``attributeSchema`、角色 `attributeProfile`、场景残留或场景幕背景资产,启动和编辑必须继续使用详情 profile,否则会进入默认角色 / 默认 profile,或在编辑页丢 CG、封面、技能预览和初始物品图标。正式“进入世界”发布 / 回读结果页时,同一 `profile.id` 下也不得用字段更少的后端旧视图降级当前结果页完整 profile。角色选择页还需要在角色数组异常或为空时回退默认角色,并显示可返回的轻量空态,不能 `return null` 造成黑屏。运行态懒加载 fallback 必须可见,不能用纯 `null` 让用户误判为黑屏。
RPG 运行态的战斗终局、继续冒险、继续探索和切场景都属于服务端 runtime 快照真相:`module-runtime-story` 必须在终局战斗 action 后调用 post-battle finalization,持久写入 `story_continue_adventure``deferredOptions``deferredRuntimeState.storyEngineMemory.currentSceneActState` 和清理后的战斗状态;`idle_travel_next_scene` / `camp_travel_home_scene` 必须由后端写入新的 `currentScenePreset``currentSceneActState``currentEncounter``runtimeStats.scenesTraveled`。前端只播放退场、进场和继续按钮表现,不能用默认 `观察/试探/调息` fallback 或本地动画假装推进剧情。旧 bootstrap 快照可能只有 `connectedSceneIds` / `forwardSceneId` 而没有 `connections`,后端生成战后旅行选项时必须兼容这些字段。
RPG / 拼图等运行态存档选择入口统一在个人中心 `常用功能 > 存档` 暴露为独立弹窗;“玩过”弹窗可以继续合并展示可继续存档,但不能成为唯一入口。前端只展示 `/api/profile/save-archives` 返回的列表并在用户选择后调用对应恢复接口,不能本地拼装或筛选正式存档真相。
## 拼图
当前拼图链路:
@@ -94,6 +94,7 @@ server-rs + Axum + SpacetimeDB
8. 图像输入通用 UI 统一走 `src/components/common/CreativeImageInputPanel.tsx`。外层页面持有业务状态,组件只承担上传卡、预览、参考图缩略图、AI 重绘开关、错误展示和提交按钮。
9. 发现页 `分类` 子频道的筛选必须打开独立 dialog / drawer / modal,至少支持玩法类型过滤与排序切换;筛选结果为空时显示空状态,不把筛选内容展开在当前列表下方。
10. “我的”页泥点、游戏时长、玩过三张统计卡只展示各自标签和值,内容居中且不换行,不在统计区底部展示“更新于”时间。
11. RPG 等运行态的战斗飘字、血量变化和即时反馈必须在暗色、噪声高的场景背景上保持可读:使用高亮文字、深色描边、强阴影或小面积半透明底,不只依赖红/绿文字本身表达伤害或治疗。
11. 平台亮色 UI 配色以陶泥儿主视觉为准:暖白 / 米杏底、陶土橙主按钮、深棕正文与浅杏边框;新增界面优先复用 `src/index.css``--platform-*` 主题变量和 `apps/admin-web/src/styles/admin.css` 的同系色值,不再引入粉红、蓝绿等独立主色方案。
## 文案与编码
+25
View File
@@ -619,6 +619,31 @@ mod tests {
assert_eq!(body["error"]["details"]["creationTypeId"], "visual-novel");
}
#[tokio::test]
async fn disabled_rpg_route_returns_service_unavailable() {
let state = AppState::new(AppConfig::default()).expect("state should build");
state.set_test_creation_entry_route_enabled("rpg", false);
let app = build_router(state);
let response = app
.oneshot(
Request::builder()
.uri("/api/runtime/custom-world/agent/sessions")
.body(Body::empty())
.expect("request should build"),
)
.await
.expect("request should succeed");
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
let body = read_json_response(response).await;
assert_eq!(
body["error"]["details"]["reason"],
"creation_entry_disabled"
);
assert_eq!(body["error"]["details"]["creationTypeId"], "rpg");
}
#[tokio::test]
async fn healthz_returns_standard_envelope_when_requested() {
let app = build_router(AppState::new(AppConfig::default()).expect("state should build"));
+17 -6
View File
@@ -260,8 +260,11 @@ impl Default for AppConfig {
llm_request_timeout_ms: DEFAULT_REQUEST_TIMEOUT_MS,
llm_max_retries: DEFAULT_MAX_RETRIES,
llm_retry_backoff_ms: DEFAULT_RETRY_BACKOFF_MS,
rpg_llm_web_search_enabled: true,
creation_agent_llm_web_search_enabled: true,
// 中文注释:创作/RPG 的结构化 JSON 链路默认不启用 Responses web_search。
// 未开通工具的上游会先吐自然语言再返回 ToolNotOpen,容易污染严格 JSON 结果;
// 需要联网增强时由部署环境显式打开对应开关。
rpg_llm_web_search_enabled: false,
creation_agent_llm_web_search_enabled: false,
dashscope_base_url: "https://dashscope.aliyuncs.com/api/v1".to_string(),
dashscope_api_key: None,
dashscope_scene_image_model: String::new(),
@@ -1467,6 +1470,14 @@ mod tests {
}
}
#[test]
fn default_keeps_structured_llm_web_search_disabled() {
let config = AppConfig::default();
assert!(!config.rpg_llm_web_search_enabled);
assert!(!config.creation_agent_llm_web_search_enabled);
}
#[test]
fn from_env_reads_rpg_llm_web_search_switch() {
let _guard = ENV_LOCK
@@ -1476,11 +1487,11 @@ mod tests {
unsafe {
std::env::remove_var("GENARRATIVE_RPG_LLM_WEB_SEARCH_ENABLED");
std::env::set_var("GENARRATIVE_RPG_LLM_WEB_SEARCH_ENABLED", "false");
std::env::set_var("GENARRATIVE_RPG_LLM_WEB_SEARCH_ENABLED", "true");
}
let config = AppConfig::from_env();
assert!(!config.rpg_llm_web_search_enabled);
assert!(config.rpg_llm_web_search_enabled);
unsafe {
std::env::remove_var("GENARRATIVE_RPG_LLM_WEB_SEARCH_ENABLED");
@@ -1496,11 +1507,11 @@ mod tests {
unsafe {
std::env::remove_var("GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED");
std::env::set_var("GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED", "false");
std::env::set_var("GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED", "true");
}
let config = AppConfig::from_env();
assert!(!config.creation_agent_llm_web_search_enabled);
assert!(config.creation_agent_llm_web_search_enabled);
unsafe {
std::env::remove_var("GENARRATIVE_CREATION_AGENT_LLM_WEB_SEARCH_ENABLED");
@@ -93,15 +93,74 @@ where
F: FnMut(&str),
{
let mut latest_reply_text = String::new();
let turn_output = match request_stream_creation_agent_json_turn_once(
llm_client,
system_prompt.clone(),
user_prompt.clone(),
enable_web_search,
on_reply_update,
&mut latest_reply_text,
!enable_web_search,
)
.await
{
Ok(turn_output) => turn_output,
Err(CreationAgentJsonTurnFailure::Stream(error))
if enable_web_search && is_web_search_tool_unavailable(&error) =>
{
tracing::warn!(
error = %error,
"创作 Agent 流式联网搜索插件不可用,自动降级为无联网搜索重试"
);
latest_reply_text.clear();
request_stream_creation_agent_json_turn_once(
llm_client,
system_prompt,
user_prompt,
false,
on_reply_update,
&mut latest_reply_text,
true,
)
.await?
}
Err(error) => return Err(error),
};
let reply_text = read_reply_text(&turn_output.parsed);
if let Some(reply_text) = reply_text.as_deref()
&& reply_text != latest_reply_text
{
on_reply_update(reply_text);
}
Ok(turn_output)
}
async fn request_stream_creation_agent_json_turn_once<F>(
llm_client: &LlmClient,
system_prompt: String,
user_prompt: String,
enable_web_search: bool,
on_reply_update: &mut F,
latest_reply_text: &mut String,
emit_reply_updates: bool,
) -> Result<CreationAgentJsonTurnOutput, CreationAgentJsonTurnFailure>
where
F: FnMut(&str),
{
let response = llm_client
.stream_text(
build_creation_agent_llm_request(system_prompt, user_prompt, enable_web_search),
|delta: &LlmStreamDelta| {
if !emit_reply_updates {
return;
}
if let Some(reply_progress) =
extract_reply_text_from_partial_json(delta.accumulated_text.as_str())
&& reply_progress != latest_reply_text
&& reply_progress != *latest_reply_text
{
latest_reply_text = reply_progress.clone();
*latest_reply_text = reply_progress.clone();
on_reply_update(reply_progress.as_str());
}
},
@@ -110,12 +169,6 @@ where
.map_err(CreationAgentJsonTurnFailure::Stream)?;
let parsed = parse_json_response_text(response.content.as_str())
.map_err(|_| CreationAgentJsonTurnFailure::Parse)?;
let reply_text = read_reply_text(&parsed);
if let Some(reply_text) = reply_text.as_deref()
&& reply_text != latest_reply_text
{
on_reply_update(reply_text);
}
Ok(CreationAgentJsonTurnOutput { parsed })
}
@@ -327,6 +380,7 @@ mod tests {
let server = spawn_capturing_mock_server(vec![
MockResponse {
body: concat!(
"data: {\"type\":\"response.output_text.delta\",\"delta\":\"我需要先搜索玩具王国资料。\"}\n\n",
"data: {\"type\":\"error\",\"code\":\"ToolNotOpen\",\"message\":\"Your account has not activated web search.\"}\n\n",
"data: [DONE]\n\n"
)
@@ -391,6 +445,55 @@ mod tests {
}
}
#[tokio::test]
async fn stream_turn_keeps_partial_updates_when_web_search_is_disabled() {
let server = spawn_capturing_mock_server(vec![MockResponse {
body: concat!(
"data: {\"type\":\"response.output_text.delta\",\"delta\":\"{\\\"replyText\\\":\\\"我先\"}\n\n",
"data: {\"type\":\"response.output_text.delta\",\"delta\":\"把玩具王国定住。\\\",\\\"progressPercent\\\":12}\"}\n\n",
"data: {\"type\":\"response.completed\"}\n\n",
)
.to_string(),
}]);
let config = LlmConfig::new(
LlmProvider::Ark,
server.base_url,
"test-key".to_string(),
"test-model".to_string(),
30_000,
0,
1,
)
.expect("LLM config should build");
let llm_client = platform_llm::LlmClient::new(config).expect("LLM client should build");
let mut visible_replies = Vec::new();
let output = stream_creation_agent_json_turn(
Some(&llm_client),
"系统提示".to_string(),
"用户提示",
false,
CreationAgentLlmTurnErrorMessages {
model_unavailable: "模型不可用",
generation_failed: "生成失败",
parse_failed: "解析失败",
},
|text| visible_replies.push(text.to_string()),
|message| message,
)
.await
.expect("stream without web search should succeed");
assert_eq!(
output.parsed["replyText"].as_str(),
Some("我先把玩具王国定住。")
);
assert_eq!(
visible_replies,
vec!["我先".to_string(), "我先把玩具王国定住。".to_string()]
);
}
struct MockResponse {
body: String,
}
@@ -96,6 +96,14 @@ pub fn resolve_creation_entry_route_id(path: &str) -> Option<&'static str> {
if normalized.starts_with("/api/runtime/big-fish") {
return Some("big-fish");
}
if normalized.starts_with("/api/runtime/custom-world")
|| normalized.starts_with("/api/runtime/custom-world-library")
|| normalized.starts_with("/api/runtime/custom-world-gallery")
|| normalized.starts_with("/api/runtime/chat")
|| normalized.starts_with("/api/story")
{
return Some("rpg");
}
if normalized.starts_with("/api/runtime/visual-novel") {
return Some("visual-novel");
}
@@ -161,6 +169,26 @@ mod tests {
resolve_creation_entry_route_id("/api/creation/visual-novel/sessions"),
Some("visual-novel"),
);
assert_eq!(
resolve_creation_entry_route_id("/api/runtime/custom-world/agent/sessions"),
Some("rpg"),
);
assert_eq!(
resolve_creation_entry_route_id("/api/runtime/custom-world-library/profile-1"),
Some("rpg"),
);
assert_eq!(
resolve_creation_entry_route_id("/api/runtime/custom-world-gallery/user-1/profile-1"),
Some("rpg"),
);
assert_eq!(
resolve_creation_entry_route_id("/api/story/sessions/runtime"),
Some("rpg"),
);
assert_eq!(
resolve_creation_entry_route_id("/api/runtime/chat/npc/turn/stream"),
Some("rpg"),
);
assert_eq!(
resolve_creation_entry_route_id("/api/runtime/bark-battle/works/work-1/config"),
Some("bark-battle"),
+54 -26
View File
@@ -1626,39 +1626,20 @@ pub async fn execute_custom_world_agent_action(
)
})?
} else if action == "publish_world" {
let mut publish_payload = serde_json::to_value(&payload).map_err(|error| {
let publish_payload = serialize_publish_world_action_payload(
resolve_author_public_user_code(&state, &authenticated, &request_context)?,
resolve_author_display_name(&state, &authenticated),
)
.map_err(|error| {
custom_world_error_response(
&request_context,
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "custom-world-agent",
"message": format!("action payload JSON 序列化失败:{error}"),
"message": error,
})),
)
})?;
if let Some(object) = publish_payload.as_object_mut() {
// 发布到广场时必须写入真实作者公开信息,避免 gallery 投影落成匿名兜底数据。
object.insert(
"authorPublicUserCode".to_string(),
Value::String(resolve_author_public_user_code(
&state,
&authenticated,
&request_context,
)?),
);
object.insert(
"authorDisplayName".to_string(),
Value::String(resolve_author_display_name(&state, &authenticated)),
);
}
serde_json::to_string(&publish_payload).map_err(|error| {
custom_world_error_response(
&request_context,
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "custom-world-agent",
"message": format!("action payload JSON 序列化失败:{error}"),
})),
)
})?
publish_payload
} else {
serde_json::to_string(&payload).map_err(|error| {
custom_world_error_response(
@@ -1734,6 +1715,23 @@ fn serialize_sync_result_profile_action_payload(
.map_err(|error| format!("action payload JSON 序列化失败:{error}"))
}
fn serialize_publish_world_action_payload(
author_public_user_code: String,
author_display_name: String,
) -> Result<String, String> {
// 中文注释:发布动作只提交动作名和作者公开信息。
// 结果页当前 profile 必须先通过 sync_result_profile 写入 session
// SpacetimeDB 发布时再从 session.draft_profile_json 读取草稿真相,避免前端
// draftProfile / legacyResultProfile / profile 旧载荷覆盖刚保存的内容。
let payload_value = json!({
"action": "publish_world",
"authorPublicUserCode": author_public_user_code,
"authorDisplayName": author_display_name,
});
serde_json::to_string(&payload_value)
.map_err(|error| format!("action payload JSON 序列化失败:{error}"))
}
fn canonicalize_custom_world_library_profile_payload(
mut profile: Value,
) -> Result<(Value, CustomWorldProfileMetadata), String> {
@@ -3414,6 +3412,36 @@ mod tests {
);
}
#[test]
fn publish_world_payload_only_contains_action_and_author_identity() {
let payload_json =
serialize_publish_world_action_payload("TN-0001".to_string(), "潮汐作者".to_string())
.expect("publish payload serializes");
let payload_value: Value =
serde_json::from_str(&payload_json).expect("payload should be valid JSON");
let object = payload_value
.as_object()
.expect("publish payload should be object");
assert_eq!(object.len(), 3);
assert_eq!(
object.get("action").and_then(Value::as_str),
Some("publish_world")
);
assert_eq!(
object.get("authorPublicUserCode").and_then(Value::as_str),
Some("TN-0001")
);
assert_eq!(
object.get("authorDisplayName").and_then(Value::as_str),
Some("潮汐作者")
);
assert!(!object.contains_key("profile"));
assert!(!object.contains_key("draftProfile"));
assert!(!object.contains_key("legacyResultProfile"));
assert!(!object.contains_key("settingText"));
}
#[test]
fn custom_world_library_profile_payload_is_canonicalized_on_server() {
let (profile, metadata) = canonicalize_custom_world_library_profile_payload(json!({
@@ -10,7 +10,9 @@ use axum::{
response::Response,
};
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};
use image::{DynamicImage, GenericImageView, imageops::FilterType};
use image::{
DynamicImage, GenericImageView, ImageFormat, codecs::jpeg::JpegEncoder, imageops::FilterType,
};
use module_assets::{
AssetObjectAccessPolicy, AssetObjectFieldError, build_asset_entity_binding_input,
build_asset_object_upsert_input, generate_asset_binding_id, generate_asset_object_id,
@@ -375,6 +377,8 @@ const OPENING_CG_ENTITY_KIND: &str = "custom_world_profile";
const OPENING_CG_STORYBOARD_SLOT: &str = "opening_cg_storyboard";
const OPENING_CG_VIDEO_SLOT: &str = "opening_cg_video";
const ARK_VIDEO_TASK_POLL_INTERVAL_MS: u64 = 5_000;
const OPENING_CG_REFERENCE_MAX_EDGE: u32 = 768;
const OPENING_CG_REFERENCE_JPEG_QUALITY: u8 = 82;
struct CoverPromptContext {
opening_act_title: String,
@@ -1025,6 +1029,16 @@ pub async fn generate_custom_world_opening_cg(
"openingSceneImageSrc",
)
.await?;
let player_role_reference = resize_image_reference_data_url(
player_role_reference,
OPENING_CG_REFERENCE_MAX_EDGE,
OPENING_CG_REFERENCE_JPEG_QUALITY,
)?;
let opening_scene_reference = resize_image_reference_data_url(
opening_scene_reference,
OPENING_CG_REFERENCE_MAX_EDGE,
OPENING_CG_REFERENCE_JPEG_QUALITY,
)?;
let storyboard = generate_opening_cg_storyboard(
&state,
&owner_user_id,
@@ -1617,6 +1631,52 @@ async fn resolve_reference_image_as_data_url(
))
}
fn resize_image_reference_data_url(
data_url: String,
max_edge: u32,
jpeg_quality: u8,
) -> Result<String, AppError> {
if max_edge == 0 {
return Ok(data_url);
}
let Some(parsed) = parse_image_data_url(data_url.as_str()) else {
return Ok(data_url);
};
let image = image::load_from_memory(parsed.bytes.as_slice()).map_err(|error| {
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "custom-world-ai",
"message": format!("无法解析参考图:{error}"),
}))
})?;
let (width, height) = image.dimensions();
let already_within_budget = width <= max_edge && height <= max_edge;
if already_within_budget && parsed.mime_type == "image/jpeg" {
return Ok(data_url);
}
// 中文注释:开局 CG 故事板会同时带角色和场景两张参考图;先压到较小 JPEG,避免大图 PNG Data URL 让 VectorEngine 网关在请求发送阶段中断。
let resized = if already_within_budget {
image
} else {
image.resize(max_edge, max_edge, FilterType::Triangle)
};
let encoded_image = DynamicImage::ImageRgb8(resized.to_rgb8());
let mut encoded = Vec::new();
JpegEncoder::new_with_quality(&mut encoded, jpeg_quality)
.encode_image(&encoded_image)
.map_err(|error| {
AppError::from_status(StatusCode::INTERNAL_SERVER_ERROR).with_details(json!({
"provider": "custom-world-ai",
"message": format!("压缩参考图失败:{error}"),
}))
})?;
Ok(format!(
"data:image/jpeg;base64,{}",
BASE64_STANDARD.encode(encoded)
))
}
async fn create_text_to_image_generation(
http_client: &reqwest::Client,
settings: &DashScopeSettings,
@@ -3065,6 +3125,34 @@ mod tests {
assert_eq!(parsed.bytes, b"hello".to_vec());
}
#[test]
fn opening_cg_reference_data_url_is_resized_to_request_budget() {
let image = DynamicImage::ImageRgb8(image::RgbImage::new(2048, 1152));
let mut cursor = std::io::Cursor::new(Vec::new());
image
.write_to(&mut cursor, ImageFormat::Png)
.expect("test image should encode");
let data_url = format!(
"data:image/png;base64,{}",
BASE64_STANDARD.encode(cursor.into_inner())
);
let resized = resize_image_reference_data_url(
data_url,
OPENING_CG_REFERENCE_MAX_EDGE,
OPENING_CG_REFERENCE_JPEG_QUALITY,
)
.expect("reference should resize");
let parsed = parse_image_data_url(resized.as_str()).expect("resized data url should parse");
let resized_image =
image::load_from_memory(parsed.bytes.as_slice()).expect("resized image should decode");
let (width, height) = resized_image.dimensions();
assert!(width <= OPENING_CG_REFERENCE_MAX_EDGE);
assert!(height <= OPENING_CG_REFERENCE_MAX_EDGE);
assert_eq!(parsed.mime_type, "image/jpeg");
}
#[test]
fn push_cover_reference_source_keeps_full_data_url() {
let mut sources = Vec::new();
@@ -544,7 +544,7 @@ pub fn build_custom_world_published_profile_compile_snapshot(
let subtitle = resolve_text_field(&draft, &legacy, "subtitle").unwrap_or_default();
let summary_text = resolve_text_field(&draft, &legacy, "summary").unwrap_or_default();
let cover_image_src = resolve_cover_image_src(&draft, &legacy);
let theme_mode = resolve_theme_mode(&legacy);
let theme_mode = resolve_theme_mode(&draft, &legacy);
let playable_npc_count =
count_distinct_roles(draft.get("playableNpcs"), draft.get("storyNpcs"));
let landmark_count = to_array(draft.get("landmarks")).len() as u32;
@@ -599,6 +599,37 @@ pub fn canonicalize_custom_world_profile_before_save(profile: &mut Value) -> boo
true
}
pub fn resolve_custom_world_publish_setting_text(
payload: &Map<String, Value>,
draft_profile: &Map<String, Value>,
seed_text: &str,
) -> String {
// 中文注释:发布按钮的前端契约只保证提交动作名;正式 settingText 必须从草稿真相补齐,
// 避免旧会话 seed_text 为空时通过 publish gate,却在最终 compile/publish 阶段失败。
read_nested_text_field(payload, &["settingText"])
.or_else(|| {
read_nested_text_field(
draft_profile,
&[
"settingText",
"creatorIntent.rawSettingText",
"creatorIntent.worldHook",
"worldHook",
"anchorContent.worldPromise",
"anchorContent.worldPromise.hook",
"summary",
"name",
"title",
],
)
})
.or_else(|| {
let seed = seed_text.trim();
(!seed.is_empty()).then(|| seed.to_string())
})
.unwrap_or_default()
}
pub fn empty_agent_anchor_content_json() -> String {
r#"{"worldPromise":null,"playerFantasy":null,"themeBoundary":null,"playerEntryPoint":null,"coreConflict":null,"keyRelationships":null,"hiddenLines":null,"iconicElements":null}"#.to_string()
}
@@ -663,7 +694,13 @@ fn parse_optional_json_object(
error: CustomWorldFieldError,
) -> Result<Map<String, Value>, CustomWorldFieldError> {
match normalize_optional_json_slice(value) {
Some(value) => parse_required_json_object(&value, error),
Some(value) => match serde_json::from_str::<Value>(&value) {
Ok(Value::Object(object)) => Ok(object),
// 中文注释:跨层可选字段经 serde 结构体序列化后可能显式落成 null;
// 对 optional JSON object 而言 null 等价于未提供,不能阻断发布链路。
Ok(Value::Null) => Ok(Map::new()),
_ => Err(error),
},
None => Ok(Map::new()),
}
}
@@ -804,6 +841,32 @@ fn read_text(object: &Map<String, Value>, key: &str) -> Option<String> {
.map(ToOwned::to_owned)
}
fn read_nested_text_field(object: &Map<String, Value>, keys: &[&str]) -> Option<String> {
for key in keys {
let mut current = Value::Object(object.clone());
let mut found = true;
for segment in key.split('.') {
if let Some(next) = current.get(segment) {
current = next.clone();
} else {
found = false;
break;
}
}
if found {
if let Some(value) = current
.as_str()
.map(str::trim)
.filter(|value| !value.is_empty())
{
return Some(value.to_string());
}
}
}
None
}
fn read_string_list(object: &Map<String, Value>, key: &str) -> Vec<String> {
object
.get(key)
@@ -849,11 +912,17 @@ fn resolve_text_field(
legacy: &Map<String, Value>,
key: &str,
) -> Option<String> {
// 中文注释:发布链路的草稿真相来自 session.draft_profile_json
// legacyResultProfile 只补历史草稿缺失字段,不能覆盖结果页刚保存的内容。
to_text(draft.get(key)).or_else(|| to_text(legacy.get(key)))
}
fn resolve_theme_mode(legacy: &Map<String, Value>) -> CustomWorldThemeMode {
to_text(legacy.get("themeMode"))
fn resolve_theme_mode(
draft: &Map<String, Value>,
legacy: &Map<String, Value>,
) -> CustomWorldThemeMode {
to_text(draft.get("themeMode"))
.or_else(|| to_text(legacy.get("themeMode")))
.and_then(|value| CustomWorldThemeMode::from_client_str(&value))
.unwrap_or(CustomWorldThemeMode::Mythic)
}
@@ -955,3 +1024,139 @@ fn build_compiled_profile_payload_json(
serde_json::to_string(&Value::Object(payload))
.map_err(|_| CustomWorldFieldError::InvalidDraftProfileJson)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn build_test_compile_input(
legacy_result_profile_json: Option<String>,
) -> CustomWorldPublishedProfileCompileInput {
CustomWorldPublishedProfileCompileInput {
session_id: "session-1".to_string(),
profile_id: "cwprof_001".to_string(),
owner_user_id: "user-1".to_string(),
draft_profile_json: json!({
"name": "潮雾列岛",
"summary": "群岛与旧灯塔之间的沉船疑案。",
"playableNpcs": [],
"storyNpcs": [],
"landmarks": []
})
.to_string(),
legacy_result_profile_json,
setting_text: "海图会在午夜改写群岛航路。".to_string(),
author_display_name: "创作者".to_string(),
updated_at_micros: 1,
}
}
#[test]
fn published_profile_compile_treats_null_legacy_result_profile_as_absent() {
let snapshot = build_custom_world_published_profile_compile_snapshot(
build_test_compile_input(Some("null".to_string())),
)
.expect("null legacy result profile should be treated as absent");
assert_eq!(snapshot.profile_id, "cwprof_001");
assert_eq!(snapshot.world_name, "潮雾列岛");
}
#[test]
fn published_profile_compile_rejects_non_object_legacy_result_profile() {
let error = build_custom_world_published_profile_compile_snapshot(
build_test_compile_input(Some("[]".to_string())),
)
.expect_err("array legacy result profile should still be invalid");
assert_eq!(error, CustomWorldFieldError::InvalidLegacyResultProfileJson);
}
#[test]
fn published_profile_compile_prefers_saved_draft_over_legacy_profile() {
let input = CustomWorldPublishedProfileCompileInput {
draft_profile_json: json!({
"name": "结果页保存后的世界",
"summary": "发布前最后一次填写的摘要。",
"themeMode": "tide",
"playableNpcs": [],
"storyNpcs": [],
"landmarks": []
})
.to_string(),
legacy_result_profile_json: Some(
json!({
"name": "旧结果页世界",
"summary": "旧摘要不应覆盖保存草稿。",
"themeMode": "mythic"
})
.to_string(),
),
..build_test_compile_input(None)
};
let snapshot = build_custom_world_published_profile_compile_snapshot(input)
.expect("compile should prefer saved draft");
let payload: Value = serde_json::from_str(&snapshot.compiled_profile_payload_json)
.expect("compiled payload should be json");
assert_eq!(snapshot.world_name, "结果页保存后的世界");
assert_eq!(snapshot.summary_text, "发布前最后一次填写的摘要。");
assert_eq!(snapshot.theme_mode, CustomWorldThemeMode::Tide);
assert_eq!(
payload.get("name").and_then(Value::as_str),
Some("结果页保存后的世界")
);
assert_eq!(
payload.get("summary").and_then(Value::as_str),
Some("发布前最后一次填写的摘要。")
);
}
#[test]
fn publish_setting_text_falls_back_to_draft_profile_when_seed_is_empty() {
let payload = Map::new();
let draft_profile = json!({
"settingText": "海雾会吞掉记错航线的人。",
"worldHook": "在失真的海图上追查一场被篡改的沉船事故。",
"summary": "守灯人与群岛议会围绕沉船旧案对峙。"
})
.as_object()
.cloned()
.expect("draft profile should be object");
let setting_text = resolve_custom_world_publish_setting_text(&payload, &draft_profile, "");
assert_eq!(setting_text, "海雾会吞掉记错航线的人。");
}
#[test]
fn publish_setting_text_prefers_payload_then_draft_then_seed() {
let mut payload = Map::new();
payload.insert(
"settingText".to_string(),
Value::String("发布载荷设定".to_string()),
);
let draft_profile = json!({
"worldHook": "草稿世界一句话",
"summary": "草稿摘要"
})
.as_object()
.cloned()
.expect("draft profile should be object");
assert_eq!(
resolve_custom_world_publish_setting_text(&payload, &draft_profile, "用户原始设定"),
"发布载荷设定"
);
assert_eq!(
resolve_custom_world_publish_setting_text(&Map::new(), &draft_profile, "用户原始设定"),
"草稿世界一句话"
);
assert_eq!(
resolve_custom_world_publish_setting_text(&Map::new(), &Map::new(), "用户原始设定"),
"用户原始设定"
);
}
}
@@ -5,8 +5,8 @@ use shared_contracts::runtime_story::{
};
use crate::{
battle::resolve_battle_action, build_status_patch, read_bool_field, read_i32_field,
read_optional_string_field,
StoryRuntimeActionResolveInput, battle::resolve_battle_action, build_status_patch,
read_bool_field, read_i32_field, read_optional_string_field, resolve_story_runtime_action,
};
fn build_battle_fixture() -> serde_json::Value {
@@ -61,6 +61,115 @@ fn build_request(function_id: &str, option_text: &str) -> RuntimeStoryActionRequ
}
}
fn build_runtime_action_request(
function_id: &str,
action_text: &str,
payload: Option<serde_json::Value>,
) -> shared_contracts::story::ResolveStoryRuntimeActionRequest {
shared_contracts::story::ResolveStoryRuntimeActionRequest {
story_session_id: "storysess-1".to_string(),
client_version: Some(1),
function_id: function_id.to_string(),
action_text: action_text.to_string(),
target_id: None,
payload,
}
}
fn build_custom_world_profile_with_two_landmarks() -> serde_json::Value {
json!({
"id": "profile-1",
"name": "雾桥旧约",
"summary": "雾桥边的旧约正在复苏。",
"camp": {
"id": "camp-1",
"name": "雾桥营地",
"description": "营火压着雾气。",
"connections": [
{
"targetLandmarkId": "landmark-1",
"relativePosition": "forward",
"summary": "沿桥面继续前进"
},
{
"targetLandmarkId": "landmark-2",
"relativePosition": "right",
"summary": "转入雾中支路"
}
]
},
"landmarks": [
{
"id": "landmark-1",
"name": "断桥口",
"description": "桥口挂着旧灯。"
},
{
"id": "landmark-2",
"name": "雾中渡",
"description": "渡口只有潮声。"
}
],
"storyNpcs": [
{
"id": "npc-bridge",
"name": "桥影",
"description": "桥下逼来的敌影",
"initialAffinity": -20
},
{
"id": "npc-ferryman",
"name": "摆渡人",
"description": "守着雾中渡的人",
"initialAffinity": 0
}
],
"sceneChapterBlueprints": [
{
"id": "chapter-camp",
"sceneId": "camp-1",
"linkedLandmarkIds": ["camp-1"],
"acts": [
{
"id": "act-camp-1",
"sceneId": "camp-1",
"oppositeNpcId": "npc-bridge"
},
{
"id": "act-camp-2",
"sceneId": "camp-1",
"oppositeNpcId": "npc-ferryman"
}
]
},
{
"id": "chapter-landmark-1",
"sceneId": "landmark-1",
"linkedLandmarkIds": ["landmark-1"],
"acts": [
{
"id": "act-landmark-1",
"sceneId": "landmark-1",
"oppositeNpcId": "npc-ferryman"
}
]
}
]
})
}
fn build_story_runtime_snapshot(
game_state: serde_json::Value,
current_story: Option<serde_json::Value>,
) -> shared_contracts::story::StoryRuntimeSnapshotPayload {
shared_contracts::story::StoryRuntimeSnapshotPayload {
saved_at: None,
bottom_tab: "adventure".to_string(),
game_state,
current_story,
}
}
#[test]
fn battle_resolution_prefers_player_defeat_when_both_sides_fall_in_same_turn() {
let request = build_request("battle_all_in_crush", "全力压制");
@@ -89,3 +198,210 @@ fn battle_resolution_prefers_player_defeat_when_both_sides_fall_in_same_turn() {
Some("defeat".to_string())
);
}
#[test]
fn terminal_battle_action_persists_post_battle_continue_story() {
let mut game_state = build_battle_fixture();
game_state["runtimeSessionId"] = json!("runtime-1");
game_state["currentScene"] = json!("Story");
game_state["worldType"] = json!("CUSTOM");
game_state["playerHp"] = json!(30);
game_state["customWorldProfile"] = build_custom_world_profile_with_two_landmarks();
game_state["currentScenePreset"] = json!({
"id": "custom-scene-camp",
"name": "雾桥营地",
"description": "营火压着雾气。",
"connectedSceneIds": ["custom-scene-landmark-1", "custom-scene-landmark-2"],
"forwardSceneId": "custom-scene-landmark-1",
"treasureHints": [],
"npcs": []
});
game_state["storyEngineMemory"] = json!({
"currentSceneActState": {
"sceneId": "camp-1",
"chapterId": "chapter-camp",
"currentActId": "act-camp-1",
"currentActIndex": 0,
"completedActIds": [],
"visitedActIds": ["act-camp-1"]
}
});
let output = resolve_story_runtime_action(StoryRuntimeActionResolveInput {
story_session_id: "storysess-1".to_string(),
runtime_session_id: "runtime-1".to_string(),
snapshot: build_story_runtime_snapshot(game_state, None),
request: build_runtime_action_request("battle_all_in_crush", "全力压制", None),
})
.expect("terminal battle should resolve");
assert_eq!(
output.presentation.battle.unwrap().outcome.as_deref(),
Some("victory")
);
assert_eq!(
output.presentation.options[0].function_id,
"story_continue_adventure"
);
assert_eq!(
output.snapshot.current_story.as_ref().unwrap()["options"][0]["functionId"],
json!("story_continue_adventure")
);
assert!(
output.snapshot.current_story.as_ref().unwrap()["deferredOptions"]
.as_array()
.is_some_and(|items| {
items
.iter()
.any(|item| item["functionId"] == json!("idle_travel_next_scene"))
})
);
assert_eq!(
output.snapshot.current_story.as_ref().unwrap()["deferredRuntimeState"]["storyEngineMemory"]
["currentSceneActState"]["currentActId"],
json!("act-camp-2")
);
assert_eq!(
output.snapshot.game_state["storyEngineMemory"]["currentSceneActState"]["currentActId"],
json!("act-camp-2")
);
}
#[test]
fn idle_travel_next_scene_changes_scene_from_target_payload() {
let game_state = json!({
"runtimeSessionId": "runtime-1",
"runtimeActionVersion": 1,
"currentScene": "Story",
"worldType": "CUSTOM",
"customWorldProfile": build_custom_world_profile_with_two_landmarks(),
"playerHp": 30,
"playerMaxHp": 40,
"playerMana": 10,
"playerMaxMana": 20,
"playerCurrency": 0,
"playerInventory": [],
"playerEquipment": { "weapon": null, "armor": null, "relic": null },
"runtimeStats": {
"hostileNpcsDefeated": 0,
"itemsUsed": 0,
"questsAccepted": 0,
"scenesTraveled": 0,
"playTimeMs": 0,
"lastPlayTickAt": null
},
"currentScenePreset": {
"id": "custom-scene-camp",
"name": "雾桥营地",
"description": "营火压着雾气。",
"connectedSceneIds": ["custom-scene-landmark-1", "custom-scene-landmark-2"],
"connections": [
{
"sceneId": "custom-scene-landmark-1",
"relativePosition": "forward",
"summary": "沿桥面继续前进"
}
],
"forwardSceneId": "custom-scene-landmark-1",
"treasureHints": [],
"npcs": []
},
"currentEncounter": null,
"npcInteractionActive": false,
"sceneHostileNpcs": [],
"inBattle": false,
"storyHistory": [],
"storyEngineMemory": {}
});
let output = resolve_story_runtime_action(StoryRuntimeActionResolveInput {
story_session_id: "storysess-1".to_string(),
runtime_session_id: "runtime-1".to_string(),
snapshot: build_story_runtime_snapshot(game_state, None),
request: build_runtime_action_request(
"idle_travel_next_scene",
"向前走,前往断桥口",
Some(json!({ "targetSceneId": "custom-scene-landmark-1" })),
),
})
.expect("travel action should resolve");
assert_eq!(
output.snapshot.game_state["currentScenePreset"]["id"],
json!("custom-scene-landmark-1")
);
assert_eq!(
output.snapshot.game_state["runtimeStats"]["scenesTraveled"],
json!(1)
);
assert_eq!(
output.snapshot.game_state["currentEncounter"]["id"],
json!("npc-ferryman")
);
assert_eq!(
output.snapshot.game_state["storyEngineMemory"]["currentSceneActState"]["currentActId"],
json!("act-landmark-1")
);
assert!(output.presentation.options.iter().any(|option| {
option.function_id == "idle_travel_next_scene"
|| option.function_id == "idle_explore_forward"
}));
}
#[test]
fn idle_travel_next_scene_normalizes_custom_landmark_id_payload() {
let game_state = json!({
"runtimeSessionId": "runtime-1",
"runtimeActionVersion": 1,
"currentScene": "Story",
"worldType": "CUSTOM",
"customWorldProfile": build_custom_world_profile_with_two_landmarks(),
"playerHp": 30,
"playerMaxHp": 40,
"playerMana": 10,
"playerMaxMana": 20,
"playerCurrency": 0,
"playerInventory": [],
"playerEquipment": { "weapon": null, "armor": null, "relic": null },
"runtimeStats": {
"hostileNpcsDefeated": 0,
"itemsUsed": 0,
"questsAccepted": 0,
"scenesTraveled": 0,
"playTimeMs": 0,
"lastPlayTickAt": null
},
"currentScenePreset": {
"id": "custom-scene-camp",
"name": "雾桥营地",
"description": "营火压着雾气。",
"connectedSceneIds": ["landmark-1", "landmark-2"],
"forwardSceneId": "landmark-2",
"treasureHints": [],
"npcs": []
},
"currentEncounter": null,
"npcInteractionActive": false,
"sceneHostileNpcs": [],
"inBattle": false,
"storyHistory": [],
"storyEngineMemory": {}
});
let output = resolve_story_runtime_action(StoryRuntimeActionResolveInput {
story_session_id: "storysess-1".to_string(),
runtime_session_id: "runtime-1".to_string(),
snapshot: build_story_runtime_snapshot(game_state, None),
request: build_runtime_action_request(
"idle_travel_next_scene",
"前往雾中渡",
Some(json!({ "targetSceneId": "landmark-2" })),
),
})
.expect("raw custom landmark id should resolve");
assert_eq!(
output.snapshot.game_state["currentScenePreset"]["id"],
json!("custom-scene-landmark-2")
);
}
@@ -76,7 +76,9 @@ pub use options::{
build_static_runtime_story_option, build_story_option_from_runtime_option, infer_option_scope,
};
pub use post_battle::{
finalize_post_battle_resolution, is_terminal_battle_outcome, resolve_post_battle_story_options,
clear_post_battle_state, ensure_scene_act_state, ensure_scene_encounter_preview,
finalize_post_battle_resolution, is_terminal_battle_outcome, resolve_forward_scene_id,
resolve_post_battle_story_options, resolve_runtime_scene_preset,
};
pub use projection::{StoryRuntimeProjectionSource, build_story_runtime_projection};
pub use prompt_context::{RuntimeStoryPromptContextExtras, build_runtime_story_prompt_context};
@@ -2,10 +2,11 @@ use serde_json::{Value, json};
use shared_contracts::runtime_story::RuntimeStoryOptionView;
use crate::{
CONTINUE_ADVENTURE_FUNCTION_ID, build_static_runtime_story_option,
CONTINUE_ADVENTURE_FUNCTION_ID, build_custom_scene_preset, build_static_runtime_story_option,
build_story_option_from_runtime_option, ensure_json_object, read_array_field, read_bool_field,
read_field, read_i32_field, read_object_field, read_optional_string_field, write_bool_field,
write_i32_field, write_null_field, write_string_field,
read_field, read_i32_field, read_object_field, read_optional_string_field,
resolve_custom_runtime_scene_id, write_bool_field, write_i32_field, write_null_field,
write_string_field,
};
const WUXIA_FIRST_SCENE_ID: &str = "wuxia-bamboo-road";
@@ -36,6 +37,8 @@ pub fn finalize_post_battle_resolution(
return None;
}
let original_scene_act_state = current_scene_act_state(game_state);
if outcome == "defeat" {
return Some(finalize_defeat_revive(game_state, fallback_options));
}
@@ -45,6 +48,7 @@ pub fn finalize_post_battle_resolution(
game_state,
result_text,
fallback_options,
original_scene_act_state,
));
}
@@ -64,13 +68,14 @@ fn finalize_victory_or_spar(
game_state: &mut Value,
result_text: &str,
fallback_options: Vec<RuntimeStoryOptionView>,
original_scene_act_state: Option<Value>,
) -> PostBattleFinalization {
clear_post_battle_state(game_state);
let is_last_act = is_current_scene_act_last(game_state);
let next_act_state = if is_last_act {
None
} else {
resolve_next_scene_act_runtime_state(game_state)
resolve_next_scene_act_runtime_state(game_state, original_scene_act_state.as_ref())
};
if let Some(next_act_state) = next_act_state {
write_current_scene_act_state(game_state, next_act_state);
@@ -141,7 +146,7 @@ fn finalize_defeat_revive(
{
write_current_scene_act_state(game_state, first_act_state);
}
ensure_first_scene_encounter_preview(game_state);
ensure_scene_encounter_preview(game_state);
let story_text = if first_scene.name.is_empty() {
"你在战斗中倒下,随后重新醒来。".to_string()
@@ -160,7 +165,7 @@ fn finalize_defeat_revive(
}
}
fn clear_post_battle_state(game_state: &mut Value) {
pub fn clear_post_battle_state(game_state: &mut Value) {
write_null_field(game_state, "currentEncounter");
write_bool_field(game_state, "npcInteractionActive", false);
ensure_json_object(game_state).insert("sceneHostileNpcs".to_string(), Value::Array(Vec::new()));
@@ -421,7 +426,7 @@ fn write_first_scene(game_state: &mut Value, scene: &RuntimeScene) {
);
}
fn ensure_first_scene_encounter_preview(game_state: &mut Value) {
pub fn ensure_scene_encounter_preview(game_state: &mut Value) {
if read_bool_field(game_state, "inBattle").unwrap_or(false) {
return;
}
@@ -436,7 +441,13 @@ fn ensure_first_scene_encounter_preview(game_state: &mut Value) {
};
let scene_id = read_object_field(game_state, "currentScenePreset")
.and_then(|scene| read_optional_string_field(scene, "id"));
let focus_npc_id = resolve_active_scene_act_focus_npc_id(profile, scene_id.as_deref());
let current_act_id = current_scene_act_state(game_state)
.and_then(|state| read_optional_string_field(&state, "currentActId"));
let focus_npc_id = resolve_active_scene_act_focus_npc_id(
profile,
scene_id.as_deref(),
current_act_id.as_deref(),
);
let Some(focus_npc_id) = focus_npc_id else {
return;
};
@@ -450,6 +461,22 @@ fn ensure_first_scene_encounter_preview(game_state: &mut Value) {
);
}
pub fn ensure_scene_act_state(game_state: &mut Value) {
if read_bool_field(game_state, "inBattle").unwrap_or(false) {
return;
}
let Some(scene_id) = read_object_field(game_state, "currentScenePreset")
.and_then(|scene| read_optional_string_field(scene, "id"))
else {
return;
};
let Some(act_state) = build_initial_scene_act_runtime_state(game_state, scene_id.as_str())
else {
return;
};
write_current_scene_act_state(game_state, act_state);
}
fn build_scene_travel_options(game_state: &Value) -> Vec<RuntimeStoryOptionView> {
let Some(current_scene) = read_object_field(game_state, "currentScenePreset") else {
return vec![build_static_runtime_story_option(
@@ -459,32 +486,53 @@ fn build_scene_travel_options(game_state: &Value) -> Vec<RuntimeStoryOptionView>
)];
};
let current_scene_id = read_optional_string_field(current_scene, "id");
let mut options = read_array_field(current_scene, "connections")
let forward_scene_id = read_optional_string_field(current_scene, "forwardSceneId");
let mut option_scene_ids = Vec::new();
let mut options = Vec::new();
for connection in read_array_field(current_scene, "connections") {
let Some(scene_id) = read_optional_string_field(connection, "sceneId") else {
continue;
};
if current_scene_id.as_deref() == Some(scene_id.as_str())
|| option_scene_ids.iter().any(|id| id == scene_id.as_str())
{
continue;
}
let relative_position = read_optional_string_field(connection, "relativePosition")
.unwrap_or_else(|| "forward".to_string());
options.push(build_scene_travel_option(
game_state,
scene_id.as_str(),
relative_position.as_str(),
));
option_scene_ids.push(scene_id);
}
for scene_id in read_array_field(current_scene, "connectedSceneIds")
.into_iter()
.filter_map(|connection| {
let scene_id = read_optional_string_field(connection, "sceneId")?;
if current_scene_id.as_deref() == Some(scene_id.as_str()) {
return None;
}
let relative_position = read_optional_string_field(connection, "relativePosition")
.unwrap_or_else(|| "forward".to_string());
let scene_name = resolve_scene_name(game_state, scene_id.as_str())
.unwrap_or_else(|| scene_id.clone());
Some(RuntimeStoryOptionView {
payload: Some(json!({ "targetSceneId": scene_id })),
..build_static_runtime_story_option(
"idle_travel_next_scene",
format!(
"{},前往{}",
direction_text(relative_position.as_str()),
scene_name
)
.as_str(),
"story",
)
})
})
.collect::<Vec<_>>();
.filter_map(|scene_id| scene_id.as_str().map(str::to_string))
.chain(forward_scene_id.clone())
{
// 中文注释:bootstrap 生成的旧快照常只有 connectedSceneIds / forwardSceneId
// 没有展开 connections;这里也要生成旅行 action,避免战后只剩默认 idle 选项循环。
if current_scene_id.as_deref() == Some(scene_id.as_str())
|| option_scene_ids.iter().any(|id| id == scene_id.as_str())
{
continue;
}
let relative_position = if forward_scene_id.as_deref() == Some(scene_id.as_str()) {
"forward"
} else {
"portal"
};
options.push(build_scene_travel_option(
game_state,
scene_id.as_str(),
relative_position,
));
option_scene_ids.push(scene_id);
}
if options.is_empty() {
options.push(build_static_runtime_story_option(
@@ -497,6 +545,163 @@ fn build_scene_travel_options(game_state: &Value) -> Vec<RuntimeStoryOptionView>
options
}
fn build_scene_travel_option(
game_state: &Value,
scene_id: &str,
relative_position: &str,
) -> RuntimeStoryOptionView {
let scene_name =
resolve_scene_name(game_state, scene_id).unwrap_or_else(|| scene_id.to_string());
RuntimeStoryOptionView {
payload: Some(json!({ "targetSceneId": scene_id })),
..build_static_runtime_story_option(
"idle_travel_next_scene",
format!("{},前往{}", direction_text(relative_position), scene_name).as_str(),
"story",
)
}
}
pub fn resolve_runtime_scene_preset(game_state: &Value, scene_id: &str) -> Option<Value> {
let normalized_scene_id = scene_id.trim();
if normalized_scene_id.is_empty() {
return None;
}
if let Some(profile) = read_object_field(game_state, "customWorldProfile")
&& let Some(scene) = build_custom_scene_preset(
profile,
resolve_custom_runtime_scene_id(profile, normalized_scene_id).as_str(),
)
{
return Some(scene);
}
resolve_builtin_runtime_scene_preset(game_state, normalized_scene_id)
}
pub fn resolve_forward_scene_id(game_state: &Value) -> Option<String> {
read_object_field(game_state, "currentScenePreset").and_then(|scene| {
read_optional_string_field(scene, "forwardSceneId")
.or_else(|| {
read_array_field(scene, "connections")
.into_iter()
.find_map(|connection| read_optional_string_field(connection, "sceneId"))
})
.or_else(|| {
read_array_field(scene, "connectedSceneIds")
.into_iter()
.find_map(|scene_id| scene_id.as_str().map(str::to_string))
})
})
}
fn resolve_builtin_runtime_scene_preset(game_state: &Value, scene_id: &str) -> Option<Value> {
let template = builtin_runtime_scene_template(scene_id)?;
Some(json!({
"id": template.id,
"name": template.name,
"description": template.description,
"imageSrc": read_object_field(game_state, "currentScenePreset")
.and_then(|scene| read_optional_string_field(scene, "imageSrc"))
.unwrap_or_default(),
"connectedSceneIds": template.connected_scene_ids,
"connections": template.connections,
"forwardSceneId": template.forward_scene_id,
"treasureHints": template.treasure_hints,
"npcs": [],
}))
}
fn builtin_runtime_scene_template(scene_id: &str) -> Option<RuntimeScene> {
let is_xianxia = matches!(
scene_id,
"xianxia-cloud-gate"
| "xianxia-floating-isle"
| "xianxia-celestial-corridor"
| "xianxia-star-vessel"
);
if is_xianxia {
return Some(RuntimeScene {
id: scene_id.to_string(),
name: match scene_id {
"xianxia-floating-isle" => "浮空灵岛",
"xianxia-celestial-corridor" => "天门长廊",
"xianxia-star-vessel" => "星槎泊台",
_ => XIANXIA_FIRST_SCENE_NAME,
}
.to_string(),
description: match scene_id {
"xianxia-floating-isle" => "浮岛边缘灵雾翻涌,远处有阵纹一明一暗。",
"xianxia-celestial-corridor" => "长廊悬在云海上方,符光沿石柱缓慢游走。",
"xianxia-star-vessel" => "星槎泊在云海边缘,船身仍有星砂微光。",
_ => XIANXIA_FIRST_SCENE_DESCRIPTION,
}
.to_string(),
image_src: String::new(),
connected_scene_ids: vec![
"xianxia-cloud-gate".to_string(),
"xianxia-floating-isle".to_string(),
"xianxia-celestial-corridor".to_string(),
]
.into_iter()
.filter(|id| id != scene_id)
.collect(),
connections: vec![json!({
"sceneId": if scene_id == "xianxia-cloud-gate" { "xianxia-celestial-corridor" } else { "xianxia-cloud-gate" },
"relativePosition": if scene_id == "xianxia-cloud-gate" { "forward" } else { "back" },
"summary": "沿主路继续移动"
})],
forward_scene_id: Some(if scene_id == "xianxia-cloud-gate" {
"xianxia-celestial-corridor".to_string()
} else {
"xianxia-cloud-gate".to_string()
}),
treasure_hints: vec!["云阶边缘的灵光残痕".to_string()],
npcs: Vec::new(),
});
}
Some(RuntimeScene {
id: scene_id.to_string(),
name: match scene_id {
"wuxia-mountain-gate" => "山门石阶",
"wuxia-mist-woods" => "迷雾竹林",
"wuxia-ferry-bridge" => "渡口断桥",
_ => WUXIA_FIRST_SCENE_NAME,
}
.to_string(),
description: match scene_id {
"wuxia-mountain-gate" => "山门石阶覆着苔痕,旧旗在风里压得很低。",
"wuxia-mist-woods" => "迷雾在竹林间翻卷,脚下泥印很快又被雾水抹平。",
"wuxia-ferry-bridge" => "渡口断桥横在冷水上,桥边灯笼只剩半截残光。",
_ => WUXIA_FIRST_SCENE_DESCRIPTION,
}
.to_string(),
image_src: String::new(),
connected_scene_ids: vec![
"wuxia-bamboo-road".to_string(),
"wuxia-mountain-gate".to_string(),
"wuxia-mist-woods".to_string(),
]
.into_iter()
.filter(|id| id != scene_id)
.collect(),
connections: vec![json!({
"sceneId": if scene_id == "wuxia-bamboo-road" { "wuxia-mountain-gate" } else { "wuxia-bamboo-road" },
"relativePosition": if scene_id == "wuxia-bamboo-road" { "forward" } else { "back" },
"summary": "沿主路继续移动"
})],
forward_scene_id: Some(if scene_id == "wuxia-bamboo-road" {
"wuxia-mountain-gate".to_string()
} else {
"wuxia-bamboo-road".to_string()
}),
treasure_hints: vec!["路边半埋的旧物".to_string()],
npcs: Vec::new(),
})
}
fn resolve_scene_name(game_state: &Value, scene_id: &str) -> Option<String> {
if read_object_field(game_state, "currentScenePreset")
.and_then(|scene| read_optional_string_field(scene, "id"))
@@ -553,7 +758,10 @@ fn direction_text(relative_position: &str) -> &'static str {
}
}
fn resolve_next_scene_act_runtime_state(game_state: &Value) -> Option<Value> {
fn resolve_next_scene_act_runtime_state(
game_state: &Value,
current_act_state_override: Option<&Value>,
) -> Option<Value> {
let profile = read_object_field(game_state, "customWorldProfile")?;
let scene_id = read_object_field(game_state, "currentScenePreset")
.and_then(|scene| read_optional_string_field(scene, "id"));
@@ -563,7 +771,9 @@ fn resolve_next_scene_act_runtime_state(game_state: &Value) -> Option<Value> {
if acts.is_empty() {
return None;
}
let runtime_state = build_initial_scene_act_runtime_state(game_state, scene_id_text)?;
let runtime_state = current_act_state_override
.cloned()
.or_else(|| build_initial_scene_act_runtime_state(game_state, scene_id_text))?;
let current_act_id = read_optional_string_field(&runtime_state, "currentActId");
let current_index = acts
.iter()
@@ -762,9 +972,17 @@ fn resolve_scene_aliases(profile: &Value, scene_id: &str) -> Vec<String> {
fn resolve_active_scene_act_focus_npc_id(
profile: &Value,
scene_id: Option<&str>,
current_act_id: Option<&str>,
) -> Option<String> {
let chapter = resolve_scene_chapter_blueprint(profile, scene_id)?;
let act_state = read_array_field(chapter, "acts").first().copied()?;
let acts = read_array_field(chapter, "acts");
let act_state = current_act_id
.and_then(|act_id| {
acts.iter()
.copied()
.find(|act| read_optional_string_field(act, "id").as_deref() == Some(act_id))
})
.or_else(|| acts.first().copied())?;
read_optional_string_field(act_state, "oppositeNpcId")
.or_else(|| read_optional_string_field(act_state, "primaryNpcId"))
.or_else(|| {
@@ -14,16 +14,18 @@ use crate::{
build_current_build_toast, build_npc_gift_result_text,
build_runtime_story_option_from_story_option, build_runtime_story_view_model,
build_static_runtime_story_option, build_status_patch, build_story_option_from_runtime_option,
clear_encounter_state, clone_inventory_item_with_quantity, current_encounter_name,
ensure_json_object, find_player_inventory_entry, normalize_equipment_slot_id,
normalize_required_string, npc_buyback_price, npc_purchase_price,
clear_encounter_state, clear_post_battle_state, clone_inventory_item_with_quantity,
current_encounter_name, ensure_json_object, ensure_scene_act_state,
ensure_scene_encounter_preview, finalize_post_battle_resolution, find_player_inventory_entry,
normalize_equipment_slot_id, normalize_required_string, npc_buyback_price, npc_purchase_price,
project_story_engine_after_action, read_array_field, read_bool_field, read_field,
read_i32_field, read_inventory_item_name, read_object_field, read_optional_string_field,
read_player_equipment_item, read_player_inventory_values, read_runtime_session_id,
read_u32_field, recruit_companion_to_party, remove_inventory_item_from_list,
resolve_action_text, resolve_battle_action, resolve_equipment_slot_for_item,
resolve_forge_craft_action, resolve_forge_dismantle_action, resolve_forge_reforge_action,
resolve_npc_gift_affinity_gain, restore_player_resource, simple_story_resolution,
resolve_forward_scene_id, resolve_npc_gift_affinity_gain, resolve_post_battle_story_options,
resolve_runtime_scene_preset, restore_player_resource, simple_story_resolution,
write_bool_field, write_i32_field, write_null_field, write_player_equipment_item,
write_player_inventory_values, write_runtime_npc_interaction_view, write_string_field,
write_u32_field,
@@ -97,23 +99,7 @@ pub fn resolve_story_runtime_action(
requested_runtime_session_id.as_str(),
);
let mut options = resolution
.presentation_options
.take()
.unwrap_or_else(|| build_fallback_runtime_story_options(&game_state));
if options.is_empty() {
options = build_fallback_runtime_story_options(&game_state);
}
let story_text = resolution
.story_text
.clone()
.unwrap_or_else(|| resolution.result_text.clone());
let history_result_text = resolution.result_text.clone();
let saved_current_story = resolution
.saved_current_story
.take()
.unwrap_or_else(|| build_current_story(story_text.as_str(), &options));
append_story_history(
&mut game_state,
@@ -132,6 +118,37 @@ pub fn resolve_story_runtime_action(
.and_then(|battle| battle.outcome.as_deref()),
);
if let Some(post_battle) = finalize_post_battle_resolution(
&mut game_state,
history_result_text.as_str(),
resolution
.battle
.as_ref()
.and_then(|battle| battle.outcome.as_deref()),
Vec::new(),
) {
resolution.story_text = Some(post_battle.story_text);
resolution.presentation_options = Some(post_battle.presentation_options);
resolution.saved_current_story = Some(post_battle.saved_current_story);
}
let mut options = resolution
.presentation_options
.take()
.unwrap_or_else(|| build_fallback_runtime_story_options(&game_state));
if options.is_empty() {
options = build_fallback_runtime_story_options(&game_state);
}
let story_text = resolution
.story_text
.clone()
.unwrap_or_else(|| resolution.result_text.clone());
let saved_current_story = resolution
.saved_current_story
.take()
.unwrap_or_else(|| build_current_story(story_text.as_str(), &options));
let mut patches = vec![RuntimeStoryPatch::StoryHistoryAppend {
action_text: resolution.action_text.clone(),
result_text: history_result_text.clone(),
@@ -212,11 +229,10 @@ fn resolve_runtime_story_choice_action(
resolve_action_text("主动出声试探", request),
"你的喊话打破了当前静场,周围潜着的动静也更难继续藏住。",
)),
"idle_explore_forward" => Ok(simple_story_resolution(
game_state,
resolve_action_text("继续向前探索", request),
"你没有停在原地,而是继续向前压,把下一段遭遇主动推到自己面前。",
)),
"idle_explore_forward" => resolve_idle_explore_forward_action(game_state, request),
"idle_travel_next_scene" | "camp_travel_home_scene" => {
resolve_idle_travel_next_scene_action(game_state, request)
}
"idle_observe_signs" => Ok(simple_story_resolution(
game_state,
resolve_action_text("观察周围迹象", request),
@@ -309,6 +325,62 @@ fn resolve_continue_adventure_action(
})
}
fn resolve_idle_explore_forward_action(
game_state: &mut Value,
request: &RuntimeStoryActionRequest,
) -> Result<StoryResolution, String> {
// 中文注释:探索前进是战后继续链路的一环,必须在后端清掉战斗态并生成下一段遭遇预览。
// 前端只播放表现动画,不能只靠本地状态把同一组 idle 选项重新展示一遍。
clear_post_battle_state(game_state);
ensure_scene_encounter_preview(game_state);
Ok(StoryResolution {
action_text: resolve_action_text("继续向前探索", request),
result_text: "你没有停在原地,而是继续向前压,把下一段遭遇主动推到自己面前。".to_string(),
story_text: None,
presentation_options: Some(resolve_post_battle_story_options(game_state)),
saved_current_story: None,
patches: vec![build_status_patch(game_state)],
battle: None,
toast: None,
})
}
fn resolve_idle_travel_next_scene_action(
game_state: &mut Value,
request: &RuntimeStoryActionRequest,
) -> Result<StoryResolution, String> {
// 中文注释:切场景会改变 currentScenePreset、章节 act 状态和运行统计,
// 这些都是 runtime 快照真相,不能只在前端播放退场/进场动画。
let payload = request.action.payload.as_ref();
let target_scene_id = payload
.and_then(|payload| read_optional_string_field(payload, "targetSceneId"))
.or_else(|| resolve_forward_scene_id(game_state))
.ok_or_else(|| "idle_travel_next_scene 缺少 targetSceneId".to_string())?;
let next_scene = resolve_runtime_scene_preset(game_state, target_scene_id.as_str())
.ok_or_else(|| format!("未找到目标场景:{target_scene_id}"))?;
let next_scene_name =
read_optional_string_field(&next_scene, "name").unwrap_or_else(|| target_scene_id.clone());
clear_post_battle_state(game_state);
ensure_json_object(game_state).insert("currentScenePreset".to_string(), next_scene);
write_i32_field(game_state, "playerX", 0);
write_string_field(game_state, "playerFacing", "right");
ensure_scene_act_state(game_state);
ensure_scene_encounter_preview(game_state);
increment_runtime_stat_local(game_state, "scenesTraveled", 1);
Ok(StoryResolution {
action_text: resolve_action_text(&format!("前往{next_scene_name}"), request),
result_text: format!("你离开当前区域,抵达了{next_scene_name}。"),
story_text: None,
presentation_options: Some(resolve_post_battle_story_options(game_state)),
saved_current_story: None,
patches: vec![build_status_patch(game_state)],
battle: None,
toast: None,
})
}
fn resolve_npc_preview_talk_action(
game_state: &mut Value,
request: &RuntimeStoryActionRequest,
@@ -54,9 +54,9 @@ pub fn default_creation_entry_type_snapshots(
"rpg",
"文字冒险",
"经典 RPG 体验",
"内测",
"可创建",
"/creation-type-references/rpg.webp",
false,
true,
true,
10,
updated_at_micros,
@@ -236,6 +236,23 @@ mod tests {
);
}
#[test]
fn default_creation_entry_types_open_rpg_entry() {
let configs = default_creation_entry_type_snapshots(1);
let rpg = configs
.iter()
.find(|item| item.id == "rpg")
.expect("rpg creation entry should be seeded");
assert_eq!(rpg.title, "文字冒险");
assert_eq!(rpg.subtitle, "经典 RPG 体验");
assert!(rpg.visible);
assert!(rpg.open);
assert_eq!(rpg.badge, "可创建");
assert_eq!(rpg.sort_order, 10);
assert_eq!(rpg.image_src, "/creation-type-references/rpg.webp");
}
#[test]
fn default_creation_entry_types_include_bark_battle() {
let configs = default_creation_entry_type_snapshots(1);
@@ -2562,6 +2562,18 @@ fn upsert_nested_result_profile_id(
}
}
fn resolve_publish_world_setting_text(
payload: &JsonMap<String, JsonValue>,
draft_profile: &JsonMap<String, JsonValue>,
session: &CustomWorldAgentSession,
) -> String {
module_custom_world::resolve_custom_world_publish_setting_text(
payload,
draft_profile,
&session.seed_text,
)
}
fn is_same_agent_draft_profile_candidate(
row: &CustomWorldProfile,
owner_user_id: &str,
@@ -2581,13 +2593,10 @@ fn execute_publish_world_action(
) -> Result<CustomWorldAgentOperationSnapshot, String> {
ensure_publishable_stage(session.stage, "publish_world")?;
let draft_profile =
if let Some(explicit) = payload.get("draftProfile").and_then(JsonValue::as_object) {
explicit.clone()
} else {
parse_optional_session_object(session.draft_profile_json.as_deref())
.ok_or_else(|| "publish_world requires draft_profile_json".to_string())?
};
// 中文注释:发布动作不再信任前端携带的 draftProfile。
// 点击发布前,结果页 profile 必须先通过 sync_result_profile 写回
// custom_world_agent_session.draft_profile_json;正式发布只读取这份会话真相。
let draft_profile = read_publish_world_draft_profile_from_session(session)?;
let gate = summarize_publish_gate_from_json(
&session.session_id,
session.stage,
@@ -2601,24 +2610,9 @@ fn execute_publish_world_action(
));
}
let profile_id = payload
.get("profileId")
.and_then(JsonValue::as_str)
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToOwned::to_owned)
.unwrap_or_else(|| gate.profile_id.clone());
let setting_text = payload
.get("settingText")
.and_then(JsonValue::as_str)
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToOwned::to_owned)
.unwrap_or_else(|| session.seed_text.clone());
let legacy_result_profile_json = payload
.get("legacyResultProfile")
.map(serialize_json_value)
.transpose()?;
let profile_id = gate.profile_id.clone();
let setting_text = resolve_publish_world_setting_text(payload, &draft_profile, session);
let legacy_result_profile_json = None;
let author_public_user_code = read_optional_text_field(payload, &["authorPublicUserCode"])
.unwrap_or_else(|| build_public_user_code_from_owner_user_id(&session.owner_user_id));
let author_display_name = read_optional_text_field(payload, &["authorDisplayName"])
@@ -2663,6 +2657,13 @@ fn execute_publish_world_action(
Ok(build_custom_world_agent_operation_snapshot(&operation))
}
fn read_publish_world_draft_profile_from_session(
session: &CustomWorldAgentSession,
) -> Result<JsonMap<String, JsonValue>, String> {
parse_optional_session_object(session.draft_profile_json.as_deref())
.ok_or_else(|| "publish_world requires draft_profile_json".to_string())
}
fn execute_revert_checkpoint_action(
ctx: &ReducerContext,
session: &CustomWorldAgentSession,
@@ -5250,6 +5251,26 @@ mod tests {
);
}
#[test]
fn publish_world_draft_profile_comes_from_session_not_payload() {
let session = build_test_custom_world_agent_session(
"seed",
RpgAgentStage::ReadyToPublish,
Some(r#"{"id":"saved-profile","name":"已保存草稿"}"#),
);
let draft_profile =
read_publish_world_draft_profile_from_session(&session).expect("session draft exists");
assert_eq!(
draft_profile.get("id").and_then(JsonValue::as_str),
Some("saved-profile")
);
assert_eq!(
draft_profile.get("name").and_then(JsonValue::as_str),
Some("已保存草稿")
);
}
#[test]
fn custom_world_agent_session_direct_work_content_ignores_empty_created_session() {
let empty_session =
@@ -179,6 +179,7 @@ fn seed_creation_entry_config_if_missing(ctx: &ReducerContext) {
}
}
migrate_rpg_entry_from_old_hidden_default(ctx, now);
migrate_visual_novel_entry_from_old_visible_default(ctx, now);
migrate_coming_soon_entry_from_old_open_default(
ctx,
@@ -204,6 +205,36 @@ fn seed_creation_entry_config_if_missing(ctx: &ReducerContext) {
);
}
fn migrate_rpg_entry_from_old_hidden_default(ctx: &ReducerContext, now: Timestamp) {
let id = "rpg".to_string();
let Some(row) = ctx.db.creation_entry_type_config().id().find(&id) else {
return;
};
// 中文注释:只开放历史默认隐藏的 RPG 入口,不覆盖后台入口开关后续手动配置。
let still_old_hidden_default = row.title == "文字冒险"
&& row.subtitle == "经典 RPG 体验"
&& row.badge == "内测"
&& row.image_src == "/creation-type-references/rpg.webp"
&& !row.visible
&& row.open
&& row.sort_order == 10;
if !still_old_hidden_default {
return;
}
ctx.db
.creation_entry_type_config()
.id()
.update(CreationEntryTypeConfig {
badge: "可创建".to_string(),
visible: true,
open: true,
updated_at: now,
..row
});
}
fn migrate_visual_novel_entry_from_old_visible_default(ctx: &ReducerContext, now: Timestamp) {
let id = "visual-novel".to_string();
let Some(row) = ctx.db.creation_entry_type_config().id().find(&id) else {
+11 -1
View File
@@ -32,6 +32,16 @@ const RpgRuntimeApp = lazy(async () => {
};
});
function RuntimeLoadingFallback() {
return (
<div className="platform-ui-shell platform-viewport-shell platform-theme platform-theme--dark flex h-screen items-center justify-center bg-[image:var(--platform-body-fill)] p-4 font-sans text-[var(--platform-text-strong)]">
<div className="platform-subpanel rounded-2xl px-5 py-4 text-sm text-zinc-300">
</div>
</div>
);
}
function isRpgRuntimeRoute(pathname: string) {
const normalizedPath = normalizeAppPath(pathname);
return (
@@ -126,7 +136,7 @@ export default function App() {
if (isRuntimeActive) {
return (
<Suspense fallback={null}>
<Suspense fallback={<RuntimeLoadingFallback />}>
<RpgRuntimeApp
initialIntent={runtimeIntent}
onExitRuntime={() => {
@@ -5,6 +5,7 @@ import userEvent from '@testing-library/user-event';
import { useState } from 'react';
import { expect, test, vi } from 'vitest';
import { normalizeCustomWorldProfileRecord } from '../data/customWorldLibrary';
import * as rpgCreationAssetClient from '../services/rpg-creation/rpgCreationAssetClient';
import type { CustomWorldPlayableNpc, CustomWorldProfile } from '../types';
import { RpgCreationResultView } from './rpg-creation-result/RpgCreationResultView';
@@ -286,6 +287,40 @@ function ResultViewHarness() {
);
}
function ResultViewRehydratingHarness() {
const [profile, setProfile] = useState(baseProfile);
const [rehydrated, setRehydrated] = useState(false);
return (
<div>
<div data-testid="rehydrated">{rehydrated ? 'yes' : 'no'}</div>
<RpgCreationResultView
profile={profile}
previewCharacters={[]}
isGenerating={false}
progress={0}
progressLabel=""
error={null}
onBack={() => {}}
onProfileChange={(nextProfile) => {
setProfile(nextProfile);
if (!nextProfile.openingCg) {
return;
}
window.setTimeout(() => {
const normalized = normalizeCustomWorldProfileRecord(nextProfile);
if (normalized) {
setProfile(normalized);
}
setRehydrated(true);
}, 0);
}}
/>
</div>
);
}
test('clicking新增可扮演角色 shows pending item, disables button, and marks result as new', async () => {
const user = userEvent.setup();
@@ -385,6 +420,40 @@ test('world tab generates opening cg only after manual click and writes it back
});
});
test('world tab keeps opening cg visible after parent rehydrates normalized profile', async () => {
const user = userEvent.setup();
mockedRpgCreationAssetClient.generateOpeningCg.mockResolvedValue({
id: 'opening-cg-1',
status: 'ready',
storyboardImageSrc: '/generated-custom-world-scenes/world/opening/storyboard.png',
storyboardAssetId: 'storyboard-1',
videoSrc: '/generated-custom-world-scenes/world/opening/opening.mp4',
videoAssetId: 'video-1',
imageModel: 'gpt-image-2',
videoModel: 'doubao-seedance-2-0-fast-260128',
aspectRatio: '16:9',
imageSize: '2k',
videoResolution: '480p',
durationSeconds: 15,
pointCost: 80,
estimatedWaitMinutes: 10,
updatedAt: '2026-05-03T00:00:00Z',
});
render(<ResultViewRehydratingHarness />);
await user.click(screen.getByRole('button', { name: '生成' }));
await waitFor(() => {
expect(screen.getByTestId('rehydrated').textContent).toBe('yes');
});
expect(
document.querySelector(
'video[src="/generated-custom-world-scenes/world/opening/opening.mp4"]',
),
).toBeTruthy();
});
test('playable tab prefers generated portrait over runtime preview placeholder', async () => {
const user = userEvent.setup();
const profile = {
@@ -25,6 +25,17 @@ const testEntryConfig = {
description: '先选玩法类型,再进入对应创作工作台。',
},
creationTypes: [
{
id: 'rpg',
title: '文字冒险',
subtitle: '经典 RPG 体验',
badge: '可创建',
imageSrc: '/creation-type-references/rpg.webp',
visible: true,
open: true,
sortOrder: 10,
updatedAtMicros: 1,
},
{
id: 'puzzle',
title: '拼图',
@@ -253,14 +264,18 @@ test('creation hub reflects updated draft title summary and counts after rerende
const match3dButton = screen.getByRole('button', {
name: /.*3D /u,
});
const rpgButton = screen.getByRole('button', {
name: /.* RPG /u,
});
expect(puzzleButton).toBeTruthy();
expect(match3dButton).toBeTruthy();
expect(rpgButton).toBeTruthy();
expect((puzzleButton as HTMLButtonElement).disabled).toBe(false);
expect((match3dButton as HTMLButtonElement).disabled).toBe(false);
expect((rpgButton as HTMLButtonElement).disabled).toBe(false);
expect(screen.queryByRole('button', { name: //u })).toBeNull();
expect(screen.queryByText('反直觉形状分拣')).toBeNull();
expect(screen.queryByRole('button', { name: //u })).toBeNull();
expect(screen.queryByRole('button', { name: //u })).toBeNull();
expect(screen.queryByRole('button', { name: //u })).toBeNull();
await user.click(match3dButton);

Some files were not shown because too many files have changed in this diff Show More