1
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user