Merge branch 'codex/backend-rewrite-spacetimedb' of http://82.157.175.59:3000/GenarrativeAI/Genarrative into codex/backend-rewrite-spacetimedb

# Conflicts:
#	server-rs/crates/spacetime-client/src/lib.rs
#	src/components/platform-entry/PlatformEntryFlowShellImpl.tsx
This commit is contained in:
2026-04-24 22:17:37 +08:00
54 changed files with 2339 additions and 434 deletions

View File

@@ -4,7 +4,7 @@ use axum::{
extract::Extension,
http::Request,
middleware,
routing::{get, post},
routing::{delete, get, post},
};
use tower_http::{
classify::ServerErrorsFailureClass,
@@ -33,9 +33,9 @@ use crate::{
auth_public_user::{get_public_user_by_code, get_public_user_by_id},
auth_sessions::auth_sessions,
big_fish::{
create_big_fish_session, execute_big_fish_action, get_big_fish_run, get_big_fish_session,
get_big_fish_works, start_big_fish_run, stream_big_fish_message, submit_big_fish_input,
submit_big_fish_message,
create_big_fish_session, delete_big_fish_work, execute_big_fish_action, get_big_fish_run,
get_big_fish_session, get_big_fish_works, start_big_fish_run, stream_big_fish_message,
submit_big_fish_input, submit_big_fish_message,
},
character_animation_assets::{
generate_character_animation, get_character_animation_job, get_character_workflow_cache,
@@ -46,8 +46,9 @@ use crate::{
generate_character_visual, get_character_visual_job, publish_character_visual,
},
custom_world::{
create_custom_world_agent_session, delete_custom_world_library_profile,
execute_custom_world_agent_action, get_custom_world_agent_card_detail,
create_custom_world_agent_session, delete_custom_world_agent_session,
delete_custom_world_library_profile, execute_custom_world_agent_action,
get_custom_world_agent_card_detail,
get_custom_world_agent_operation, get_custom_world_agent_session,
get_custom_world_gallery_detail, get_custom_world_gallery_detail_by_code,
get_custom_world_library, get_custom_world_library_detail, get_custom_world_works,
@@ -76,10 +77,10 @@ use crate::{
password_management::{change_password, reset_password},
phone_auth::{phone_login, send_phone_code},
puzzle::{
advance_puzzle_next_level, create_puzzle_agent_session, drag_puzzle_piece_or_group,
execute_puzzle_agent_action, get_puzzle_agent_session, get_puzzle_gallery_detail,
get_puzzle_run, get_puzzle_work_detail, get_puzzle_works, list_puzzle_gallery,
put_puzzle_work, start_puzzle_run, stream_puzzle_agent_message,
advance_puzzle_next_level, create_puzzle_agent_session, delete_puzzle_work,
drag_puzzle_piece_or_group, execute_puzzle_agent_action, get_puzzle_agent_session,
get_puzzle_gallery_detail, get_puzzle_run, get_puzzle_work_detail, get_puzzle_works,
list_puzzle_gallery, put_puzzle_work, start_puzzle_run, stream_puzzle_agent_message,
submit_puzzle_agent_message, swap_puzzle_pieces,
},
refresh_session::refresh_session,
@@ -442,10 +443,12 @@ pub fn build_router(state: AppState) -> Router {
)
.route(
"/api/runtime/custom-world/agent/sessions/{session_id}",
get(get_custom_world_agent_session).route_layer(middleware::from_fn_with_state(
state.clone(),
require_bearer_auth,
)),
get(get_custom_world_agent_session)
.delete(delete_custom_world_agent_session)
.route_layer(middleware::from_fn_with_state(
state.clone(),
require_bearer_auth,
)),
)
.route(
"/api/runtime/custom-world/works",
@@ -531,6 +534,13 @@ pub fn build_router(state: AppState) -> Router {
require_bearer_auth,
)),
)
.route(
"/api/runtime/big-fish/works/{session_id}",
delete(delete_big_fish_work).route_layer(middleware::from_fn_with_state(
state.clone(),
require_bearer_auth,
)),
)
.route(
"/api/runtime/big-fish/sessions/{session_id}/runs",
post(start_big_fish_run).route_layer(middleware::from_fn_with_state(
@@ -598,6 +608,7 @@ pub fn build_router(state: AppState) -> Router {
"/api/runtime/puzzle/works/{profile_id}",
get(get_puzzle_work_detail)
.put(put_puzzle_work)
.delete(delete_puzzle_work)
.route_layer(middleware::from_fn_with_state(
state.clone(),
require_bearer_auth,

View File

@@ -134,6 +134,33 @@ pub async fn get_big_fish_works(
))
}
pub async fn delete_big_fish_work(
State(state): State<AppState>,
Path(session_id): Path<String>,
Extension(request_context): Extension<RequestContext>,
Extension(authenticated): Extension<AuthenticatedAccessToken>,
) -> Result<Json<Value>, Response> {
ensure_non_empty(&request_context, &session_id, "sessionId")?;
let items = state
.spacetime_client()
.delete_big_fish_work(session_id, authenticated.claims().user_id().to_string())
.await
.map_err(|error| {
big_fish_error_response(&request_context, map_big_fish_client_error(error))
})?;
Ok(json_success_body(
Some(&request_context),
BigFishWorksResponse {
items: items
.into_iter()
.map(map_big_fish_work_summary_response)
.collect(),
},
))
}
pub async fn submit_big_fish_message(
State(state): State<AppState>,
Path(session_id): Path<String>,

View File

@@ -9,7 +9,7 @@ use std::{
use axum::{
Json,
extract::{Extension, Path as AxumPath, State, rejection::JsonRejection},
extract::{Extension, Path as AxumPath, Query, State, rejection::JsonRejection},
http::StatusCode,
response::Response,
};
@@ -29,6 +29,7 @@ use platform_oss::{
LegacyAssetPrefix, OssHeadObjectRequest, OssObjectAccess, OssPutObjectRequest,
OssSignedGetObjectUrlRequest,
};
use serde::Deserialize;
use serde_json::{Value, json};
use shared_contracts::assets::{
CharacterAnimationDraftPayload, CharacterAnimationGenerateRequest,
@@ -58,6 +59,13 @@ const CHARACTER_ANIMATION_ASSET_KIND: &str = "character_animation";
const CHARACTER_ANIMATION_REFERENCE_ASSET_KIND: &str = "character_animation_reference_video";
const CHARACTER_WORKFLOW_CACHE_ASSET_KIND: &str = "character_workflow_cache";
const CHARACTER_ANIMATION_ENTITY_KIND: &str = "character";
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CharacterWorkflowCacheQuery {
#[serde(default)]
pub cache_scope_id: Option<String>,
}
const CHARACTER_ANIMATION_SLOT: &str = "animation_set";
const CHARACTER_ANIMATION_REFERENCE_SLOT: &str = "animation_reference_video";
const CHARACTER_WORKFLOW_CACHE_SLOT: &str = "workflow_cache";
@@ -573,6 +581,7 @@ pub async fn get_character_workflow_cache(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
AxumPath(character_id): AxumPath<String>,
Query(query): Query<CharacterWorkflowCacheQuery>,
) -> Result<Json<Value>, Response> {
let character_id = normalize_required_text(character_id.as_str(), "");
if character_id.is_empty() {
@@ -585,7 +594,8 @@ pub async fn get_character_workflow_cache(
));
}
let cache = load_workflow_cache(&state, character_id.as_str())
let cache_scope_id = trim_optional_text(query.cache_scope_id.as_deref());
let cache = load_workflow_cache(&state, character_id.as_str(), cache_scope_id.as_deref())
.await
.map_err(|error| character_animation_error_response(&request_context, error))?;
@@ -1530,9 +1540,10 @@ async fn put_imported_video_object(
async fn load_workflow_cache(
state: &AppState,
character_id: &str,
cache_scope_id: Option<&str>,
) -> Result<Option<CharacterWorkflowCachePayload>, AppError> {
let oss_client = require_oss_client(state)?;
let object_key = workflow_cache_object_key(character_id);
let object_key = workflow_cache_object_key(character_id, cache_scope_id);
let signed = match oss_client.sign_get_object_url(OssSignedGetObjectUrlRequest {
object_key,
expire_seconds: Some(60),
@@ -1571,7 +1582,7 @@ async fn load_workflow_cache(
}))
})?;
if cache.character_id == character_id {
if cache.character_id == character_id && cache.cache_scope_id.as_deref() == cache_scope_id {
Ok(Some(cache))
} else {
Ok(None)
@@ -1595,14 +1606,15 @@ async fn save_workflow_cache(
&reqwest::Client::new(),
OssPutObjectRequest {
prefix: LegacyAssetPrefix::CharacterDrafts,
path_segments: vec![
sanitize_storage_segment(cache.character_id.as_str(), "character"),
"workflow-cache".to_string(),
],
path_segments: workflow_cache_path_segments(&cache),
file_name: "workflow-cache.json".to_string(),
content_type: Some("application/json; charset=utf-8".to_string()),
access: OssObjectAccess::Private,
metadata: build_workflow_cache_metadata("asset-tool", cache.character_id.as_str()),
metadata: build_workflow_cache_metadata(
"asset-tool",
cache.character_id.as_str(),
cache.cache_scope_id.as_deref(),
),
body,
},
)
@@ -1616,8 +1628,10 @@ fn normalize_workflow_cache_payload(
updated_at: String,
) -> CharacterWorkflowCachePayload {
let character_id = normalize_required_text(payload.character_id.as_str(), "character");
let cache_scope_id = trim_optional_text(payload.cache_scope_id.as_deref());
CharacterWorkflowCachePayload {
character_id: character_id.clone(),
cache_scope_id,
visual_prompt_text: clamp_prompt_seed_text(payload.visual_prompt_text.as_deref()),
animation_prompt_text: clamp_prompt_seed_text(payload.animation_prompt_text.as_deref()),
visual_drafts: normalize_visual_drafts(character_id.as_str(), payload.visual_drafts),
@@ -1661,11 +1675,32 @@ fn normalize_visual_drafts(
.collect()
}
fn workflow_cache_object_key(character_id: &str) -> String {
format!(
"generated-character-drafts/{}/workflow-cache/workflow-cache.json",
sanitize_storage_segment(character_id, "character")
)
fn workflow_cache_path_segments(cache: &CharacterWorkflowCachePayload) -> Vec<String> {
let character_segment = sanitize_storage_segment(cache.character_id.as_str(), "character");
if let Some(cache_scope_id) = cache.cache_scope_id.as_deref() {
vec![
sanitize_storage_segment(cache_scope_id, "world"),
character_segment,
"workflow-cache".to_string(),
]
} else {
vec![character_segment, "workflow-cache".to_string()]
}
}
fn workflow_cache_object_key(character_id: &str, cache_scope_id: Option<&str>) -> String {
if let Some(cache_scope_id) = cache_scope_id.and_then(|value| trim_optional_text(Some(value))) {
format!(
"generated-character-drafts/{}/{}/workflow-cache/workflow-cache.json",
sanitize_storage_segment(cache_scope_id.as_str(), "world"),
sanitize_storage_segment(character_id, "character")
)
} else {
format!(
"generated-character-drafts/{}/workflow-cache/workflow-cache.json",
sanitize_storage_segment(character_id, "character")
)
}
}
fn build_character_animation_job_payload(task: AiTaskSnapshot) -> CharacterAssetJobStatusPayload {
@@ -2460,8 +2495,9 @@ fn build_asset_metadata(
fn build_workflow_cache_metadata(
owner_user_id: &str,
character_id: &str,
cache_scope_id: Option<&str>,
) -> BTreeMap<String, String> {
BTreeMap::from([
let mut metadata = BTreeMap::from([
(
"asset_kind".to_string(),
CHARACTER_WORKFLOW_CACHE_ASSET_KIND.to_string(),
@@ -2476,7 +2512,11 @@ fn build_workflow_cache_metadata(
"slot".to_string(),
CHARACTER_WORKFLOW_CACHE_SLOT.to_string(),
),
])
]);
if let Some(cache_scope_id) = cache_scope_id.and_then(|value| trim_optional_text(Some(value))) {
metadata.insert("cache_scope_id".to_string(), cache_scope_id);
}
metadata
}
fn require_oss_client(state: &AppState) -> Result<&platform_oss::OssClient, AppError> {
@@ -3311,6 +3351,7 @@ mod tests {
let cache = normalize_workflow_cache_payload(
CharacterWorkflowCacheSaveRequest {
character_id: "hero".to_string(),
cache_scope_id: None,
visual_prompt_text: Some("主形象".to_string()),
animation_prompt_text: Some("待机".to_string()),
visual_drafts: vec![CharacterVisualDraftPayload {
@@ -3341,11 +3382,19 @@ mod tests {
#[test]
fn workflow_cache_object_key_uses_character_drafts_prefix() {
assert_eq!(
workflow_cache_object_key("Hero 01"),
workflow_cache_object_key("Hero 01", None),
"generated-character-drafts/hero-01/workflow-cache/workflow-cache.json"
);
}
#[test]
fn workflow_cache_object_key_can_scope_by_world() {
assert_eq!(
workflow_cache_object_key("Hero 01", Some("World 99")),
"generated-character-drafts/world-99/hero-01/workflow-cache/workflow-cache.json"
);
}
#[test]
fn build_animation_generate_result_payload_keeps_image_sequence_shape() {
let payload = build_animation_generate_result_payload(&CharacterAnimationGeneratedDraft {

View File

@@ -48,6 +48,12 @@ const CHARACTER_VISUAL_ENTITY_KIND: &str = "character";
const CHARACTER_VISUAL_SLOT: &str = "primary_visual";
const CHARACTER_VISUAL_TASK_POLL_INTERVAL_MS: u64 = 2_500;
#[derive(Clone, Debug)]
pub(crate) struct GeneratedCharacterPrimaryVisual {
pub image_src: String,
pub asset_id: String,
}
pub async fn generate_character_visual(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
@@ -277,6 +283,90 @@ pub async fn generate_character_visual(
))
}
pub(crate) async fn generate_character_primary_visual_for_profile(
state: &AppState,
owner_user_id: &str,
character_id: &str,
prompt_text: &str,
character_brief_text: Option<&str>,
) -> Result<GeneratedCharacterPrimaryVisual, AppError> {
let payload = CharacterVisualGenerateRequest {
character_id: character_id.to_string(),
source_mode: shared_contracts::assets::CharacterVisualSourceMode::TextToImage,
prompt_text: prompt_text.to_string(),
character_brief_text: character_brief_text.map(ToOwned::to_owned),
reference_image_data_urls: Vec::new(),
candidate_count: 1,
image_model: CHARACTER_VISUAL_MODEL.to_string(),
size: "1024*1024".to_string(),
};
let task_id = generate_ai_task_id(current_utc_micros());
let prompt = build_character_visual_prompt(
payload.prompt_text.as_str(),
payload.character_brief_text.as_deref(),
);
let character_id = normalize_required_text(payload.character_id.as_str(), "character");
let model = normalize_required_text(payload.image_model.as_str(), CHARACTER_VISUAL_MODEL);
let size = normalize_required_text(payload.size.as_str(), "1024*1024");
create_visual_task(
state,
&task_id,
owner_user_id,
&character_id,
&model,
&prompt,
)?;
let settings = require_dashscope_settings(state)?;
let http_client = build_dashscope_http_client(&settings)?;
state
.ai_task_service()
.start_task(task_id.as_str(), current_utc_micros())
.map_err(map_ai_task_error)?;
let generated = create_character_visual_generation(
&http_client,
&settings,
model.as_str(),
prompt.as_str(),
size.as_str(),
1,
&[],
)
.await?;
let drafts = persist_visual_drafts(
state,
owner_user_id,
&character_id,
&task_id,
generated.images,
size.as_str(),
)
.await?;
let draft = drafts.into_iter().next().ok_or_else(|| {
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "character-visual",
"message": "角色主形象生成没有返回候选图。",
}))
})?;
let asset_id = format!("visual-{character_id}-{task_id}");
let image_src = persist_published_visual(
state,
owner_user_id,
&character_id,
asset_id.as_str(),
draft.image_src.as_str(),
Some(prompt.as_str()),
)
.await?;
state
.ai_task_service()
.complete_task(task_id.as_str(), current_utc_micros())
.map_err(map_ai_task_error)?;
Ok(GeneratedCharacterPrimaryVisual {
image_src,
asset_id,
})
}
pub async fn get_character_visual_job(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,

View File

@@ -43,11 +43,13 @@ use tracing::info;
use crate::{
api_response::json_success_body,
auth::AuthenticatedAccessToken,
character_visual_assets::generate_character_primary_visual_for_profile,
custom_world_agent_entities::generate_custom_world_agent_entities,
custom_world_agent_turn::{
CustomWorldAgentTurnRequest, build_failed_finalize_record_input,
build_finalize_record_input, run_custom_world_agent_turn,
},
custom_world_ai::generate_custom_world_scene_image_for_profile,
custom_world_foundation_draft::{
DraftFoundationPayloadError, build_draft_foundation_action_payload_json,
generate_custom_world_foundation_draft,
@@ -504,6 +506,36 @@ pub async fn get_custom_world_works(
))
}
pub async fn delete_custom_world_agent_session(
State(state): State<AppState>,
AxumPath(session_id): AxumPath<String>,
Extension(request_context): Extension<RequestContext>,
Extension(authenticated): Extension<AuthenticatedAccessToken>,
) -> Result<Json<Value>, Response> {
ensure_non_empty(&request_context, &session_id, "sessionId")?;
let items = state
.spacetime_client()
.delete_custom_world_agent_session(
session_id,
authenticated.claims().user_id().to_string(),
)
.await
.map_err(|error| {
custom_world_error_response(&request_context, map_custom_world_client_error(error))
})?;
Ok(json_success_body(
Some(&request_context),
CustomWorldWorksResponse {
items: items
.into_iter()
.map(map_custom_world_work_summary_response)
.collect(),
},
))
}
pub async fn get_custom_world_agent_card_detail(
State(state): State<AppState>,
Path((session_id, card_id)): Path<(String, String)>,
@@ -1096,6 +1128,111 @@ fn spawn_custom_world_draft_foundation_job(
}
};
let mut draft_profile_json = draft_result.draft_profile_json;
let mut draft_profile_value = match serde_json::from_str::<Value>(&draft_profile_json) {
Ok(Value::Object(object)) => Value::Object(object),
Ok(_) => {
let message = "foundation draft JSON 必须是 object".to_string();
let _ = upsert_custom_world_draft_foundation_progress(
&state,
&session.session_id,
&owner_user_id,
&operation_id,
"failed",
"底稿素材生成失败",
message.as_str(),
100,
Some(message),
)
.await;
return;
}
Err(error) => {
let message = format!("foundation draft JSON 非法:{error}");
let _ = upsert_custom_world_draft_foundation_progress(
&state,
&session.session_id,
&owner_user_id,
&operation_id,
"failed",
"底稿素材生成失败",
message.as_str(),
100,
Some(message),
)
.await;
return;
}
};
if let Err(message) = generate_draft_foundation_role_visuals(
&state,
&session,
&owner_user_id,
&operation_id,
&mut draft_profile_value,
)
.await
{
let _ = upsert_custom_world_draft_foundation_progress(
&state,
&session.session_id,
&owner_user_id,
&operation_id,
"failed",
"生成角色主形象失败",
message.as_str(),
100,
Some(message),
)
.await;
return;
}
if let Err(message) = generate_draft_foundation_act_backgrounds(
&state,
&session,
&owner_user_id,
&operation_id,
&mut draft_profile_value,
)
.await
{
let _ = upsert_custom_world_draft_foundation_progress(
&state,
&session.session_id,
&owner_user_id,
&operation_id,
"failed",
"生成幕背景图失败",
message.as_str(),
100,
Some(message),
)
.await;
return;
}
draft_profile_json = match serde_json::to_string(&draft_profile_value) {
Ok(value) => value,
Err(error) => {
let message = format!("带素材的 foundation draft JSON 序列化失败:{error}");
let _ = upsert_custom_world_draft_foundation_progress(
&state,
&session.session_id,
&owner_user_id,
&operation_id,
"failed",
"底稿素材写回失败",
message.as_str(),
100,
Some(message),
)
.await;
return;
}
};
let _ = upsert_custom_world_draft_foundation_progress(
&state,
&session.session_id,
@@ -1109,34 +1246,32 @@ fn spawn_custom_world_draft_foundation_job(
)
.await;
let payload_json = match build_draft_foundation_action_payload_json(
&payload,
&draft_result.draft_profile_json,
) {
Ok(value) => value,
Err(error) => {
let message = match error {
DraftFoundationPayloadError::SerializePayload(message) => message,
DraftFoundationPayloadError::InvalidPayloadShape => {
"action payload 必须是 object".to_string()
}
DraftFoundationPayloadError::InvalidGeneratedDraft(message) => message,
};
let _ = upsert_custom_world_draft_foundation_progress(
&state,
&session.session_id,
&owner_user_id,
&operation_id,
"failed",
"底稿写入失败",
message.clone().as_str(),
100,
Some(message),
)
.await;
return;
}
};
let payload_json =
match build_draft_foundation_action_payload_json(&payload, &draft_profile_json) {
Ok(value) => value,
Err(error) => {
let message = match error {
DraftFoundationPayloadError::SerializePayload(message) => message,
DraftFoundationPayloadError::InvalidPayloadShape => {
"action payload 必须是 object".to_string()
}
DraftFoundationPayloadError::InvalidGeneratedDraft(message) => message,
};
let _ = upsert_custom_world_draft_foundation_progress(
&state,
&session.session_id,
&owner_user_id,
&operation_id,
"failed",
"底稿写入失败",
message.clone().as_str(),
100,
Some(message),
)
.await;
return;
}
};
if let Err(error) = state
.spacetime_client()
@@ -1167,6 +1302,201 @@ fn spawn_custom_world_draft_foundation_job(
});
}
async fn generate_draft_foundation_role_visuals(
state: &AppState,
session: &CustomWorldAgentSessionRecord,
owner_user_id: &str,
operation_id: &str,
draft_profile: &mut Value,
) -> Result<(), String> {
let Some(profile_object) = draft_profile.as_object_mut() else {
return Err("foundation draft JSON 必须是 object".to_string());
};
let mut role_refs = Vec::new();
for key in ["playableNpcs", "storyNpcs"] {
if let Some(roles) = profile_object.get(key).and_then(Value::as_array) {
for index in 0..roles.len() {
role_refs.push((key.to_string(), index));
}
}
}
let total = role_refs.len().max(1);
for (completed, (key, index)) in role_refs.into_iter().enumerate() {
let role = profile_object
.get(key.as_str())
.and_then(Value::as_array)
.and_then(|roles| roles.get(index))
.cloned()
.unwrap_or(Value::Null);
let name =
json_text_from_value(&role, "name").unwrap_or_else(|| format!("角色{}", index + 1));
let role_id = json_text_from_value(&role, "id").unwrap_or_else(|| format!("{key}-{index}"));
let visual_prompt = json_text_from_value(&role, "visualDescription")
.or_else(|| json_text_from_value(&role, "description"))
.unwrap_or_else(|| name.clone());
upsert_custom_world_draft_foundation_progress(
state,
&session.session_id,
owner_user_id,
operation_id,
"running",
"生成角色主形象",
format!("正在生成角色主形象 {}/{}{}", completed + 1, total, name).as_str(),
97 + ((completed as u32).min(1)),
None,
)
.await
.map_err(|error| error.to_string())?;
let generated = generate_character_primary_visual_for_profile(
state,
owner_user_id,
role_id.as_str(),
visual_prompt.as_str(),
Some(name.as_str()),
)
.await
.map_err(|error| error.message().to_string())?;
if let Some(role_object) = profile_object
.get_mut(key.as_str())
.and_then(Value::as_array_mut)
.and_then(|roles| roles.get_mut(index))
.and_then(Value::as_object_mut)
{
role_object.insert("imageSrc".to_string(), Value::String(generated.image_src));
role_object.insert(
"generatedVisualAssetId".to_string(),
Value::String(generated.asset_id),
);
}
}
Ok(())
}
async fn generate_draft_foundation_act_backgrounds(
state: &AppState,
session: &CustomWorldAgentSessionRecord,
owner_user_id: &str,
operation_id: &str,
draft_profile: &mut Value,
) -> Result<(), String> {
let world_name =
json_text_from_value(draft_profile, "name").unwrap_or_else(|| "未命名世界".to_string());
let profile_id = json_text_from_value(draft_profile, "id");
let act_refs = collect_scene_act_refs(draft_profile);
let total = act_refs.len().max(1);
for (completed, act_ref) in act_refs.into_iter().enumerate() {
upsert_custom_world_draft_foundation_progress(
state,
&session.session_id,
owner_user_id,
operation_id,
"running",
"生成幕背景图",
format!(
"正在生成幕背景图 {}/{}{}",
completed + 1,
total,
act_ref.title
)
.as_str(),
98,
None,
)
.await
.map_err(|error| error.to_string())?;
let generated = generate_custom_world_scene_image_for_profile(
state,
owner_user_id,
profile_id.as_deref(),
world_name.as_str(),
act_ref.scene_id.as_str(),
act_ref.title.as_str(),
act_ref.summary.as_str(),
act_ref.prompt.as_str(),
)
.await
.map_err(|error| error.message().to_string())?;
if let Some(act_object) = draft_profile
.get_mut("sceneChapterBlueprints")
.and_then(Value::as_array_mut)
.and_then(|chapters| chapters.get_mut(act_ref.chapter_index))
.and_then(|chapter| chapter.get_mut("acts"))
.and_then(Value::as_array_mut)
.and_then(|acts| acts.get_mut(act_ref.act_index))
.and_then(Value::as_object_mut)
{
act_object.insert(
"backgroundImageSrc".to_string(),
Value::String(generated.image_src),
);
act_object.insert(
"backgroundAssetId".to_string(),
Value::String(generated.asset_id),
);
act_object.insert(
"generatedScenePrompt".to_string(),
Value::String(generated.prompt),
);
act_object.insert(
"generatedSceneModel".to_string(),
Value::String(generated.model),
);
}
}
Ok(())
}
struct SceneActGenerationRef {
chapter_index: usize,
act_index: usize,
scene_id: String,
title: String,
summary: String,
prompt: String,
}
fn collect_scene_act_refs(draft_profile: &Value) -> Vec<SceneActGenerationRef> {
draft_profile
.get("sceneChapterBlueprints")
.and_then(Value::as_array)
.into_iter()
.flatten()
.enumerate()
.flat_map(|(chapter_index, chapter)| {
let chapter_scene_id = json_text_from_value(chapter, "sceneId")
.or_else(|| json_text_from_value(chapter, "id"))
.unwrap_or_else(|| format!("chapter-{chapter_index}"));
chapter
.get("acts")
.and_then(Value::as_array)
.into_iter()
.flatten()
.enumerate()
.map(move |(act_index, act)| SceneActGenerationRef {
chapter_index,
act_index,
scene_id: json_text_from_value(act, "sceneId")
.unwrap_or_else(|| chapter_scene_id.clone()),
title: json_text_from_value(act, "title")
.unwrap_or_else(|| format!("{}", act_index + 1)),
summary: json_text_from_value(act, "summary").unwrap_or_default(),
prompt: json_text_from_value(act, "backgroundPromptText")
.or_else(|| json_text_from_value(act, "summary"))
.unwrap_or_else(|| "场景幕背景图,突出探索空间与局势氛围。".to_string()),
})
})
.collect()
}
fn json_text_from_value(value: &Value, key: &str) -> Option<String> {
value
.get(key)
.and_then(Value::as_str)
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToOwned::to_owned)
}
async fn upsert_custom_world_draft_foundation_progress(
state: &AppState,
session_id: &str,

View File

@@ -122,6 +122,14 @@ struct GeneratedAssetResponse {
actual_prompt: Option<String>,
}
#[derive(Clone, Debug)]
pub(crate) struct GeneratedCustomWorldSceneImage {
pub image_src: String,
pub asset_id: String,
pub prompt: String,
pub model: String,
}
struct PreparedAssetUpload {
prefix: LegacyAssetPrefix,
path_segments: Vec<String>,
@@ -537,6 +545,114 @@ pub async fn generate_custom_world_scene_image(
Ok(json_success_body(Some(&request_context), asset))
}
pub(crate) async fn generate_custom_world_scene_image_for_profile(
state: &AppState,
owner_user_id: &str,
profile_id: Option<&str>,
world_name: &str,
scene_id: &str,
scene_name: &str,
scene_description: &str,
prompt_text: &str,
) -> Result<GeneratedCustomWorldSceneImage, AppError> {
let payload = CustomWorldSceneImageRequest {
profile_id: profile_id.map(ToOwned::to_owned),
world_name: Some(world_name.to_string()),
landmark_id: Some(scene_id.to_string()),
landmark_name: Some(scene_name.to_string()),
prompt: Some(prompt_text.to_string()),
size: Some("1600*900".to_string()),
negative_prompt: None,
reference_image_src: None,
user_prompt: Some(prompt_text.to_string()),
profile: Some(SceneImageProfileInput {
id: profile_id.map(ToOwned::to_owned),
name: Some(world_name.to_string()),
subtitle: None,
summary: None,
tone: None,
player_goal: None,
setting_text: None,
}),
landmark: Some(SceneImageLandmarkInput {
id: Some(scene_id.to_string()),
name: Some(scene_name.to_string()),
description: Some(scene_description.to_string()),
danger_level: None,
}),
};
let normalized = normalize_scene_image_request(payload)?;
let settings = require_dashscope_settings(state)?;
let http_client = build_dashscope_http_client(&settings)?;
let generated = create_text_to_image_generation(
&http_client,
&settings,
TEXT_TO_IMAGE_SCENE_MODEL,
normalized.prompt.as_str(),
Some(normalized.negative_prompt.as_str()),
normalized.size.as_str(),
"创建场景图片生成任务失败",
"查询场景图片任务失败",
"场景图片生成任务失败",
"场景图片生成超时或未返回图片地址",
)
.await?;
let downloaded = download_remote_image(
&http_client,
generated.image_url.as_str(),
"下载生成图片失败",
)
.await?;
let asset_id = format!("custom-scene-{}", current_utc_millis());
let upload = PreparedAssetUpload {
prefix: LegacyAssetPrefix::CustomWorldScenes,
path_segments: vec![
sanitize_storage_segment(
normalized
.profile_id
.as_deref()
.unwrap_or(normalized.world_name.as_str()),
"world",
),
sanitize_storage_segment(normalized.entity_id.as_str(), "scene"),
asset_id.clone(),
],
file_name: format!("scene.{}", downloaded.extension),
content_type: downloaded.mime_type,
body: downloaded.bytes,
asset_kind: "scene_image",
entity_kind: "custom_world_landmark",
entity_id: normalized.entity_id.clone(),
profile_id: normalized.profile_id.clone(),
slot: "scene_image",
source_job_id: Some(generated.task_id.clone()),
};
let model = normalized.model.clone();
let prompt = normalized.prompt.clone();
let asset = persist_custom_world_asset(
state,
owner_user_id,
upload,
GeneratedAssetResponse {
image_src: String::new(),
asset_id: asset_id.clone(),
source_type: "generated".to_string(),
model: Some(model.clone()),
size: Some(normalized.size),
task_id: Some(generated.task_id),
prompt: Some(prompt.clone()),
actual_prompt: generated.actual_prompt,
},
)
.await?;
Ok(GeneratedCustomWorldSceneImage {
image_src: asset.image_src,
asset_id,
prompt,
model,
})
}
pub async fn generate_custom_world_cover_image(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,

View File

@@ -976,7 +976,8 @@ fn build_foundation_draft_user_prompt(session: &CustomWorldAgentSessionRecord) -
"3. coreConflicts 必须至少 1 条。".to_string(),
"4. chapters 或 sceneChapterBlueprints 必须体现主线第一幕。".to_string(),
"5. sceneChapterBlueprints[0].acts 至少 1 条。".to_string(),
"6. summary 要像结果页摘要,不要只是原始 seed 重复".to_string(),
"6. sceneChapterBlueprints[*].acts[*].backgroundPromptText 必须逐幕生成,作为每一幕生成背景图时默认填入的场景画面描述,不要只生成一个全局场景背景提示词".to_string(),
"7. summary 要像结果页摘要,不要只是原始 seed 重复。".to_string(),
]
.join("\n\n")
}
@@ -1452,10 +1453,58 @@ fn normalize_scene_chapter_blueprint(chapter: JsonValue) -> JsonValue {
"acts".to_string(),
JsonValue::Array(vec![build_fallback_scene_act()]),
);
} else {
object.insert(
"acts".to_string(),
JsonValue::Array(
acts.into_iter()
.enumerate()
.map(|(index, act)| normalize_scene_act_blueprint(act, index))
.collect(),
),
);
}
JsonValue::Object(object)
}
fn normalize_scene_act_blueprint(act: JsonValue, index: usize) -> JsonValue {
let mut object = act.as_object().cloned().unwrap_or_default();
let fallback_act = build_fallback_scene_act_with_index(index);
let fallback_prompt = fallback_act
.get("backgroundPromptText")
.and_then(JsonValue::as_str)
.unwrap_or("当前幕场景背景,突出可探索空间、站位地面和局势氛围。")
.to_string();
let title = object
.get("title")
.and_then(JsonValue::as_str)
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToOwned::to_owned)
.unwrap_or_else(|| format!("{}", index + 1));
let summary = object
.get("summary")
.and_then(JsonValue::as_str)
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToOwned::to_owned)
.unwrap_or_else(|| "当前幕推动场景内的主线压力。".to_string());
object.insert("title".to_string(), JsonValue::String(title.clone()));
object.insert("summary".to_string(), JsonValue::String(summary.clone()));
let background_prompt = object
.get("backgroundPromptText")
.and_then(JsonValue::as_str)
.map(str::trim)
.filter(|value| !value.is_empty())
.map(ToOwned::to_owned)
.unwrap_or_else(|| format!("{title}{summary}{fallback_prompt}"));
object.insert(
"backgroundPromptText".to_string(),
JsonValue::String(background_prompt),
);
JsonValue::Object(object)
}
fn build_fallback_scene_chapter_blueprint() -> JsonValue {
json!({
"id": "chapter-act-1",
@@ -1466,10 +1515,15 @@ fn build_fallback_scene_chapter_blueprint() -> JsonValue {
}
fn build_fallback_scene_act() -> JsonValue {
build_fallback_scene_act_with_index(0)
}
fn build_fallback_scene_act_with_index(index: usize) -> JsonValue {
json!({
"id": "scene-act-1",
"title": "开场场景幕",
"id": format!("scene-act-{}", index + 1),
"title": if index == 0 { "开场场景幕".to_string() } else { format!("{}", index + 1) },
"summary": "玩家被推入第一波局势,必须先确认站位、威胁和下一步追查方向。",
"backgroundPromptText": "第一幕场景背景,突出玩家初入现场时的空间轮廓、可站立地面、远近景层次和第一波威胁氛围。",
})
}

View File

@@ -718,6 +718,42 @@ pub async fn put_puzzle_work(
))
}
pub async fn delete_puzzle_work(
State(state): State<AppState>,
AxumPath(profile_id): AxumPath<String>,
Extension(request_context): Extension<RequestContext>,
Extension(authenticated): Extension<AuthenticatedAccessToken>,
) -> Result<Json<Value>, Response> {
ensure_non_empty(
&request_context,
PUZZLE_WORKS_PROVIDER,
&profile_id,
"profileId",
)?;
let items = state
.spacetime_client()
.delete_puzzle_work(profile_id, authenticated.claims().user_id().to_string())
.await
.map_err(|error| {
puzzle_error_response(
&request_context,
PUZZLE_WORKS_PROVIDER,
map_puzzle_client_error(error),
)
})?;
Ok(json_success_body(
Some(&request_context),
PuzzleWorksResponse {
items: items
.into_iter()
.map(map_puzzle_work_summary_response)
.collect(),
},
))
}
pub async fn list_puzzle_gallery(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,