M4 runtime story Rust migration wrap-up

This commit is contained in:
2026-04-22 20:10:46 +08:00
parent 35958d5942
commit fa373f0575
31 changed files with 3257 additions and 1556 deletions

View File

@@ -297,3 +297,125 @@ server-rs/crates/api-server/src/
这组 resolver 虽然仍是 action orchestration但已经不依赖 HTTP / `AppState`,只依赖快照 `Value`、当前故事 `currentStory`、共享 DTO 与内部 helper因此适合先作为 `api-server` 内部模块沉淀。
迁移后 [compat.rs](D:/Genarrative/server-rs/crates/api-server/src/runtime_story/compat.rs) 对这些动作只保留 functionId 分发、快照桥接与少量共享 glue code不再承载 battle / equipment / forge / NPC / quest 的具体结算细节。
## 11. 独立 crate 抽取边界
完成第二阶段后,已经可以进入第三阶段,但独立 crate 仍按最小安全边界推进:
1. 新 crate 命名为 `module-runtime-story-compat`
2. `module-runtime-story-compat` 只承接“无 HTTP / 无 `AppState`”的 compat 核心:
- runtime story action 分发与确定性结算
- battle / equipment / forge / NPC / quest action resolver
- `Value` 快照态读写 helper
- `RuntimeStoryActionResponse` 的 view model / presentation 编译
3. `api-server` 继续保留:
- Axum route handler
- `RequestContext / AuthenticatedAccessToken`
- `runtime_snapshot` 持久化与读取
- `clientVersion` 校验到 HTTP error 的映射
- `platform-llm` 动作后文本增强
4. 首批迁移不把 AI 文本增强放进新 crate因为它依赖 `AppState``platform-llm`
5. 首批迁移不把 test route boundary 放进新 crateroute boundary 仍属于 `api-server`
这一步完成后,`api-server``runtime_story/compat.rs` 应该只负责:
1. 从 HTTP 请求恢复 / 持久化 snapshot
2. 调用 `module-runtime-story-compat` 产出确定性动作结果或状态响应
3. 需要时调用本地 AI 增强
4. 将最终响应包回 `Json<Value>`
这就是从“`api-server` 内部模块”到“独立 crate”的首个可验证切片。
截至当前工作区,第三阶段首批独立 crate 已落地:
1. 已新增 [module-runtime-story-compat](D:/Genarrative/server-rs/crates/module-runtime-story-compat)。
2. 已接入 [server-rs/Cargo.toml](D:/Genarrative/server-rs/Cargo.toml) workspace。
3. [api-server/Cargo.toml](D:/Genarrative/server-rs/crates/api-server/Cargo.toml) 已新增对 `module-runtime-story-compat` 的依赖。
4. 首批迁入新 crate 的内容包括:
- `StoryResolution`
- `GeneratedStoryPayload`
- `CurrentEncounterNpcQuestContext`
- `PendingQuestOfferContext`
- `RuntimeStoryActionResponseParts`
- `CONTINUE_ADVENTURE_FUNCTION_ID`
- `MAX_TASK5_COMPANIONS`
- `simple_story_resolution`
- `resolve_action_text`
- `build_status_patch`
- `current_world_type`
5. 第三阶段继续推进后,当前已经从 `api-server` 抽到独立 crate 的纯逻辑还包括:
- `core.rs`JSON 快照读写、runtime stat、story history、progression、encounter 清理
- `game_state.rs`encounter / inventory / equipment 的基础 helper
- `forge.rs`:锻造配方、重铸成本、材料消耗、拆解产物、重铸产物、货币文本
- `forge_actions.rs``forge_craft / forge_dismantle / forge_reforge` 三条动作结算
- `npc_support.rs`:赠礼好感收益、交易价格、数量文案、满员换队招募 helper
- `battle.rs``battle_* / inventory_use` 的纯动作结算、patch 生成与胜负写回
6. 当前 [api-server 的 compat.rs](D:/Genarrative/server-rs/crates/api-server/src/runtime_story/compat.rs) 已经不再内嵌上述纯逻辑,只保留:
- Axum handler
- snapshot 读写
- `clientVersion` 校验
- functionId 分发
- HTTP error 映射
- 动作后 AI 文本增强
7. 当前 [api-server 的 forge.rs](D:/Genarrative/server-rs/crates/api-server/src/runtime_story/compat/forge.rs) 已收缩成极薄 bridge只为 NPC trade bootstrap 复用新 crate 暴露的运行时物品构造 helper锻造规则主体不再保留本地副本。
8. 当前 [api-server 的 battle.rs](D:/Genarrative/server-rs/crates/api-server/src/runtime_story/compat/battle.rs) 也已从“结算 + 展示”收缩成“展示编译 + 少量本地 helper”
- battle 动作结算主链已经迁入 `module-runtime-story-compat`
- `api-server` 本地仅继续保留 `build_battle_runtime_story_options(...)``restore_player_resource(...)` 这类仍被 presentation / NPC 辅助逻辑直接依赖的部分
- 这为下一步继续把 battle option compiler 收进独立 crate 做好了边界准备
这意味着第三阶段已经不只是“创建了新 crate”而是完成了第一批真正跨 crate 的 compat 纯逻辑迁移,并且保持 route boundary 与既有测试口径不变。
同日继续推进后battle 这块已经完成从“先迁结算主链”到“连展示编译一起迁”的下一步:
1. [module-runtime-story-compat 的 battle.rs](D:/Genarrative/server-rs/crates/module-runtime-story-compat/src/battle.rs) 当前已同时承接:
- `resolve_battle_action(...)`
- `restore_player_resource(...)`
- `build_battle_runtime_story_options(...)`
- 技能冷却读取、推荐物品挑选、战斗技能 option compiler 等 battle 展示辅助
2. [api-server 的 compat.rs](D:/Genarrative/server-rs/crates/api-server/src/runtime_story/compat.rs) 已直接从 `module-runtime-story-compat` 导入 battle 展示编译与资源恢复 helper。
3. [api-server 本地的 compat/battle.rs](D:/Genarrative/server-rs/crates/api-server/src/runtime_story/compat/battle.rs) 已删除,不再保留 battle 规则的本地副本。
4. 到这一步,`api-server` 在 runtime story compat 上对 battle 的职责已经只剩:
- functionId 分发
- route handler / snapshot bridge
- AI 文本增强后的最终响应拼装
这说明第三阶段已经不只是在“拆 crate”而是在真实压缩 `api-server` 的 compat 规则面。接下来更合理的推进方向将不再是 battle而是继续评估 `presentation` 中还能进一步抽到独立 crate 的纯 view model / option compiler 边界。
同日继续推进后,`presentation` 中最通用的一层 option DTO 编译也已经开始抽离:
1. 已新增 [options.rs](D:/Genarrative/server-rs/crates/module-runtime-story-compat/src/options.rs),统一承接:
- `build_static_runtime_story_option(...)`
- `build_runtime_story_option_with_payload(...)`
- `build_disabled_runtime_story_option(...)`
- `build_runtime_story_option_from_story_option(...)`
- `build_story_option_from_runtime_option(...)`
- `infer_option_scope(...)`
2. [module-runtime-story-compat 的 lib.rs](D:/Genarrative/server-rs/crates/module-runtime-story-compat/src/lib.rs) 已对外 re-export 这些 option helper`api-server` 直接复用。
3. [api-server 的 presentation.rs](D:/Genarrative/server-rs/crates/api-server/src/runtime_story/compat/presentation.rs) 已删除本地重复实现,只保留 NPC option 组合、view model 组装、quest currentStory 等尚未完全独立的部分。
这一步的意义不是单纯减少行数,而是先把 `RuntimeStoryOptionView` 的最小稳定编译面收敛到独立 crate。后续若继续外提 `view model``fallback option compiler`,将不需要再重复搬运这些 option 基础件。
同日继续推进后,`presentation` 中的纯 view-model builder 也已经抽到独立 crate
1. 已新增 [view_model.rs](D:/Genarrative/server-rs/crates/module-runtime-story-compat/src/view_model.rs),统一承接:
- `build_runtime_story_view_model(...)`
- `build_runtime_story_companions(...)`
- `build_runtime_story_encounter(...)`
- `resolve_current_encounter_npc_state(...)`
2. [api-server 的 presentation.rs](D:/Genarrative/server-rs/crates/api-server/src/runtime_story/compat/presentation.rs) 已删除本地 view-model 组装实现,继续只负责状态响应 orchestration、dialogue currentStory、fallback option compiler 与 quest 辅助。
3. [api-server 的 game_state.rs](D:/Genarrative/server-rs/crates/api-server/src/runtime_story/compat/game_state.rs) 当前也直接复用 crate 导出的 `resolve_current_encounter_npc_state(...)`,避免 NPC 状态查询 helper 在 `api-server` 和 crate 之间出现两套实现。
至此,`module-runtime-story-compat` 已经覆盖了 runtime story 兼容层的以下纯逻辑面:
1. JSON 快照读写与基础状态 helper
2. battle / forge / npc support 的纯规则结算
3. battle option compiler
4. runtime story option DTO 编译
5. runtime story view-model 编译
`api-server` 当前的剩余重点已经更集中在:
1. HTTP / snapshot bridge
2. functionId 分发
3. AI 文本增强
4. NPC / quest fallback option 与 currentStory 组合逻辑