This commit is contained in:
2026-04-24 17:59:48 +08:00
parent 929febb4fe
commit 6cb3efae61
55 changed files with 2373 additions and 435 deletions

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 {