新增编辑器生成规范、生成角色形象、生成图标素材等功能

新增编辑器生成规范、生成角色形象、生成图标素材等功能
This commit is contained in:
2026-06-16 14:47:13 +08:00
parent 0fd0a06387
commit 7eeff10c67
33 changed files with 8783 additions and 502 deletions

View File

@@ -41,6 +41,8 @@ use shared_contracts::assets::{
CharacterRoleAssetWorkflowResolveRequest, CharacterRoleAssetWorkflowResponse,
CharacterVisualDraftPayload, CharacterWorkflowCacheGetResponse, CharacterWorkflowCachePayload,
CharacterWorkflowCacheSaveRequest, CharacterWorkflowCacheSaveResponse,
EditorCharacterAnimationFramePayload, EditorCharacterAnimationGenerateRequest,
EditorCharacterAnimationGenerateResponse,
};
use spacetime_client::SpacetimeClientError;
@@ -82,6 +84,9 @@ const FIXED_ARK_CHARACTER_VIDEO_RESOLUTION: &str = "480p";
const FIXED_ARK_CHARACTER_VIDEO_RATIO: &str = "1:1";
const FIXED_ARK_CHARACTER_VIDEO_DURATION_SECONDS: u32 = 4;
const ARK_VIDEO_TASK_POLL_INTERVAL_MS: u64 = 5_000;
const EDITOR_CHARACTER_ANIMATION_MODEL: &str = "seedance2.0";
const EDITOR_CHARACTER_ANIMATION_ASSET_KIND: &str = "editor_character_animation";
const EDITOR_CHARACTER_ANIMATION_PROMPT_PREFIX: &str = "生成游戏角色动画,参考图作为首帧和尾帧,画面中心构图,角色主体完整置于画面中央,禁止镜头透视,禁止特写。背景固定为纯绿色绿幕,只作为抠像底色,禁止出现建筑、室内布景、风景、地面道具、漂浮物、烟雾叙事元素、文字或其他角色以外的场景内容。\n动作描述:";
const BUILT_IN_MOTION_TEMPLATES: [MotionTemplate; 4] = [
MotionTemplate {
@@ -489,6 +494,87 @@ pub async fn generate_character_animation(
))
}
pub async fn generate_editor_character_animation(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
payload: Result<Json<EditorCharacterAnimationGenerateRequest>, JsonRejection>,
) -> Result<Json<Value>, Response> {
let Json(payload) = payload.map_err(|error| {
character_animation_error_response(
&request_context,
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "editor-character-animation",
"message": error.body_text(),
})),
)
})?;
let normalized = normalize_editor_character_animation_request(payload)
.map_err(|error| character_animation_error_response(&request_context, error))?;
let settings = require_editor_character_animation_settings(&state, &normalized)
.map_err(|error| character_animation_error_response(&request_context, error))?;
let extraction_settings = resolve_backend_frame_extraction_settings(&state);
let http_client = build_upstream_http_client(settings.ark.request_timeout_ms)
.map_err(|error| character_animation_error_response(&request_context, error))?;
let owner_user_id = "editor-character-animation".to_string();
let task_id = generate_ai_task_id(current_utc_micros());
let result = async {
let source_data_url = resolve_media_source_as_data_url(
&state,
&http_client,
normalized.source_image_src.as_str(),
"sourceImageSrc",
)
.await?;
let generated = request_editor_character_animation_preview(
&state,
&http_client,
&settings,
owner_user_id.as_str(),
normalized.source_layer_id.as_str(),
task_id.as_str(),
normalized.prompt.as_str(),
source_data_url.as_str(),
)
.await?;
let frames = extract_and_persist_editor_character_animation_frames(
&state,
owner_user_id.as_str(),
normalized.source_layer_id.as_str(),
task_id.as_str(),
generated.preview_video_path.as_str(),
&normalized,
&extraction_settings,
)
.await?;
Ok::<_, AppError>((generated, frames))
}
.await;
let (generated, frames) = result
.map_err(|error| character_animation_error_response(&request_context, error))?;
Ok(json_success_body(
Some(&request_context),
EditorCharacterAnimationGenerateResponse {
ok: true,
task_id,
model: EDITOR_CHARACTER_ANIMATION_MODEL.to_string(),
prompt: generated.submitted_prompt,
preview_video_path: generated.preview_video_path,
frame_count: frames.len() as u32,
duration_seconds: normalized.duration_seconds,
frame_width: normalized.frame_width,
frame_height: normalized.frame_height,
fps: normalized.fps,
price_mud_points: normalized.price_mud_points,
frames,
},
))
}
pub async fn get_character_animation_job(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
@@ -1248,6 +1334,167 @@ async fn put_generated_preview_video(
Ok(put_result.legacy_public_path)
}
async fn request_editor_character_animation_preview(
state: &AppState,
http_client: &reqwest::Client,
settings: &EditorCharacterAnimationSettings,
owner_user_id: &str,
source_layer_id: &str,
task_id: &str,
prompt: &str,
source_frame_data_url: &str,
) -> Result<GeneratedAnimationPreview, AppError> {
let upstream_task_id =
create_editor_ark_image_to_video_task(http_client, settings, prompt, source_frame_data_url)
.await?;
let video_url =
wait_for_ark_content_generation_task(http_client, &settings.ark, upstream_task_id.as_str())
.await?;
let preview_payload =
download_generated_video(http_client, video_url.as_str(), "下载画板角色动画视频失败。")
.await?;
let preview_video_path = put_generated_preview_video(
state,
owner_user_id,
source_layer_id,
"editor-character-animation",
task_id,
preview_payload,
)
.await?;
Ok(GeneratedAnimationPreview {
preview_video_path,
upstream_task_id,
submitted_prompt: prompt.to_string(),
moderation_fallback_applied: false,
})
}
async fn create_editor_ark_image_to_video_task(
http_client: &reqwest::Client,
settings: &EditorCharacterAnimationSettings,
prompt: &str,
source_frame_data_url: &str,
) -> Result<String, AppError> {
let response = http_client
.post(format!("{}/contents/generations/tasks", settings.ark.base_url))
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", settings.ark.api_key),
)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.json(&json!({
"model": settings.ark.model,
"content": [
{
"type": "text",
"text": prompt,
},
{
"type": "image_url",
"image_url": {
"url": source_frame_data_url,
},
"role": "first_frame",
},
{
"type": "image_url",
"image_url": {
"url": source_frame_data_url,
},
"role": "last_frame",
}
],
"resolution": settings.resolution,
"ratio": settings.ratio,
"duration": settings.duration_seconds,
"watermark": false,
}))
.send()
.await
.map_err(|error| {
map_character_animation_upstream_error(format!("请求 Ark 视频服务失败:{error}"))
})?;
let status = response.status();
let body = response.text().await.map_err(|error| {
map_character_animation_upstream_error(format!("读取 Ark 视频任务响应失败:{error}"))
})?;
if !status.is_success() {
return Err(parse_animation_upstream_error(
body.as_str(),
"创建画板角色动画视频任务失败。",
));
}
let payload = parse_animation_json_payload(body.as_str(), "创建画板角色动画视频任务失败。")?;
extract_animation_task_id(&payload.payload).ok_or_else(|| {
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "ark",
"message": "画板角色动画视频任务未返回任务 id。",
}))
})
}
async fn extract_and_persist_editor_character_animation_frames(
state: &AppState,
owner_user_id: &str,
source_layer_id: &str,
task_id: &str,
preview_video_path: &str,
request: &NormalizedEditorCharacterAnimationRequest,
extraction_settings: &BackendFrameExtractionSettings,
) -> Result<Vec<EditorCharacterAnimationFramePayload>, AppError> {
let plan = AnimationFrameExtractionPlan {
frame_count: request.frame_count,
apply_chroma_key: true,
sample_start_ratio: 0.0,
sample_end_ratio: 1.0,
};
let finalized_frames = extract_animation_frames_from_preview_video(
state,
preview_video_path,
request.frame_width,
request.frame_height,
extraction_settings,
&plan,
)
.await?;
let mut frame_payloads = Vec::with_capacity(finalized_frames.len());
for (index, frame) in finalized_frames.into_iter().enumerate() {
let put_result = put_character_animation_object(
state,
LegacyAssetPrefix::Animations,
vec![
"editor".to_string(),
sanitize_storage_segment(source_layer_id, "layer"),
task_id.to_string(),
],
format!("frame{:02}.{}", index + 1, frame.extension),
frame.mime_type,
frame.bytes,
build_asset_metadata(
EDITOR_CHARACTER_ANIMATION_ASSET_KIND,
owner_user_id,
"editor_layer",
source_layer_id,
"animation_frame",
"editor-character-animation",
),
)
.await?;
frame_payloads.push(EditorCharacterAnimationFramePayload {
frame_index: index as u32 + 1,
image_src: put_result.legacy_public_path,
width: request.frame_width,
height: request.frame_height,
});
}
Ok(frame_payloads)
}
async fn publish_animation_set(
state: &AppState,
owner_user_id: &str,
@@ -1864,6 +2111,235 @@ fn resolve_character_animation_model(payload: &CharacterAnimationGenerateRequest
normalize_required_text(candidate, CHARACTER_ANIMATION_MODEL)
}
fn normalize_editor_character_animation_request(
payload: EditorCharacterAnimationGenerateRequest,
) -> Result<NormalizedEditorCharacterAnimationRequest, AppError> {
let source_layer_id = normalize_required_text(payload.source_layer_id.as_str(), "");
if source_layer_id.is_empty() {
return Err(editor_character_animation_bad_request(
"sourceLayerId 不能为空。",
));
}
let source_image_src = trim_optional_text(Some(payload.source_image_src.as_str()))
.ok_or_else(|| editor_character_animation_bad_request("sourceImageSrc 不能为空。"))?;
let prompt_text = payload.prompt_text.trim().chars().take(4000).collect::<String>();
if prompt_text.is_empty() {
return Err(editor_character_animation_bad_request("动画描述不能为空。"));
}
let resolution = normalize_editor_character_animation_resolution(payload.resolution.as_str())?;
let ratio = normalize_editor_character_animation_ratio(payload.ratio.as_str())?;
let frame_count = normalize_editor_character_animation_frame_count(payload.frame_count)?;
let duration_seconds =
normalize_editor_character_animation_duration(payload.duration_seconds, frame_count)?;
let expected_price = calculate_editor_character_animation_price(resolution, duration_seconds);
if payload.price_mud_points != expected_price {
return Err(editor_character_animation_bad_request(format!(
"priceMudPoints 与分辨率和时长不一致,应为 {expected_price}"
)));
}
if payload.model.trim() != EDITOR_CHARACTER_ANIMATION_MODEL {
return Err(editor_character_animation_bad_request(
"model 必须固定为 seedance2.0。",
));
}
let (frame_width, frame_height) = resolve_editor_character_animation_frame_size(
payload.source_width,
payload.source_height,
ratio,
resolution,
);
let prompt = build_editor_character_animation_prompt(prompt_text.as_str());
Ok(NormalizedEditorCharacterAnimationRequest {
source_layer_id,
source_image_src,
prompt,
resolution: resolution.to_string(),
ratio: resolve_editor_character_animation_provider_ratio(
ratio,
payload.source_width,
payload.source_height,
),
frame_count,
duration_seconds,
price_mud_points: expected_price,
frame_width,
frame_height,
fps: (frame_count / duration_seconds).max(1),
})
}
fn require_editor_character_animation_settings(
state: &AppState,
request: &NormalizedEditorCharacterAnimationRequest,
) -> Result<EditorCharacterAnimationSettings, AppError> {
let base_url = state
.config
.ark_character_video_base_url
.trim()
.trim_end_matches('/');
if base_url.is_empty() {
return Err(
AppError::from_status(StatusCode::SERVICE_UNAVAILABLE).with_details(json!({
"provider": "ark",
"reason": "ARK_CHARACTER_VIDEO_BASE_URL 未配置",
})),
);
}
let api_key = state
.config
.ark_character_video_api_key
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
.ok_or_else(|| {
AppError::from_status(StatusCode::SERVICE_UNAVAILABLE).with_details(json!({
"provider": "ark",
"reason": "ARK_CHARACTER_VIDEO_API_KEY 未配置",
}))
})?;
// 中文注释:画板角色动画入口产品侧固定为 seedance2.0,不继承通用角色视频环境模型覆盖。
Ok(EditorCharacterAnimationSettings {
ark: ArkVideoSettings {
base_url: base_url.to_string(),
api_key: api_key.to_string(),
request_timeout_ms: state.config.ark_character_video_request_timeout_ms.max(1),
model: EDITOR_CHARACTER_ANIMATION_MODEL.to_string(),
},
resolution: request.resolution.clone(),
ratio: request.ratio.clone(),
duration_seconds: request.duration_seconds,
})
}
fn build_editor_character_animation_prompt(prompt_text: &str) -> String {
format!(
"{}\n{}",
EDITOR_CHARACTER_ANIMATION_PROMPT_PREFIX,
prompt_text.trim()
)
}
fn normalize_editor_character_animation_resolution(value: &str) -> Result<&'static str, AppError> {
match value.trim() {
"480p" => Ok("480p"),
"720p" => Ok("720p"),
_ => Err(editor_character_animation_bad_request(
"resolution 只支持 480p 或 720p。",
)),
}
}
fn normalize_editor_character_animation_ratio(value: &str) -> Result<&'static str, AppError> {
match value.trim() {
"same" => Ok("same"),
"1:1" => Ok("1:1"),
"4:3" => Ok("4:3"),
"16:9" => Ok("16:9"),
"9:16" => Ok("9:16"),
"3:4" => Ok("3:4"),
_ => Err(editor_character_animation_bad_request(
"ratio 只支持 same、1:1、4:3、16:9、9:16、3:4。",
)),
}
}
fn normalize_editor_character_animation_frame_count(value: u32) -> Result<u32, AppError> {
match value {
32 | 40 | 48 => Ok(value),
_ => Err(editor_character_animation_bad_request(
"frameCount 只支持 32、40、48。",
)),
}
}
fn normalize_editor_character_animation_duration(
value: u32,
frame_count: u32,
) -> Result<u32, AppError> {
let expected = match frame_count {
32 => 4,
40 => 5,
48 => 6,
_ => 0,
};
if value == expected {
Ok(value)
} else {
Err(editor_character_animation_bad_request(
"durationSeconds 必须与帧数组合为 32/4、40/5 或 48/6。",
))
}
}
fn calculate_editor_character_animation_price(resolution: &str, duration_seconds: u32) -> u32 {
let per_second = if resolution == "720p" { 20 } else { 10 };
per_second * duration_seconds
}
fn resolve_editor_character_animation_provider_ratio(
ratio: &str,
source_width: u32,
source_height: u32,
) -> String {
if ratio != "same" {
return ratio.to_string();
}
if source_width == 0 || source_height == 0 {
return "1:1".to_string();
}
let normalized = source_width as f32 / source_height as f32;
[
("1:1", 1.0f32),
("4:3", 4.0 / 3.0),
("16:9", 16.0 / 9.0),
("9:16", 9.0 / 16.0),
("3:4", 3.0 / 4.0),
]
.into_iter()
.min_by(|(_, left), (_, right)| {
(normalized - *left)
.abs()
.partial_cmp(&(normalized - *right).abs())
.unwrap_or(std::cmp::Ordering::Equal)
})
.map(|(value, _)| value.to_string())
.unwrap_or_else(|| "1:1".to_string())
}
fn resolve_editor_character_animation_frame_size(
source_width: u32,
source_height: u32,
ratio: &str,
resolution: &str,
) -> (u32, u32) {
let long_edge = if resolution == "720p" { 720 } else { 480 };
let (ratio_width, ratio_height) = match ratio {
"same" if source_width > 0 && source_height > 0 => (source_width, source_height),
"4:3" => (4, 3),
"16:9" => (16, 9),
"9:16" => (9, 16),
"3:4" => (3, 4),
_ => (1, 1),
};
if ratio_width >= ratio_height {
let height = ((long_edge as f32 * ratio_height as f32 / ratio_width as f32).round() as u32)
.max(1);
(long_edge, height)
} else {
let width = ((long_edge as f32 * ratio_width as f32 / ratio_height as f32).round() as u32)
.max(1);
(width, long_edge)
}
}
fn editor_character_animation_bad_request(message: impl Into<String>) -> AppError {
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "editor-character-animation",
"message": message.into(),
}))
}
fn build_animation_generate_result_payload(generated: &CharacterAnimationGeneratedDraft) -> Value {
match generated.preview_video_path.as_ref() {
Some(preview_video_path) => json!({
@@ -3353,6 +3829,28 @@ struct ArkVideoSettings {
model: String,
}
struct EditorCharacterAnimationSettings {
ark: ArkVideoSettings,
resolution: String,
ratio: String,
duration_seconds: u32,
}
#[derive(Debug)]
struct NormalizedEditorCharacterAnimationRequest {
source_layer_id: String,
source_image_src: String,
prompt: String,
resolution: String,
ratio: String,
frame_count: u32,
duration_seconds: u32,
price_mud_points: u32,
frame_width: u32,
frame_height: u32,
fps: u32,
}
struct GeneratedAnimationPreview {
preview_video_path: String,
upstream_task_id: String,
@@ -3548,4 +4046,73 @@ mod tests {
assert_eq!(resolve_character_animation_model(&payload), "wan-move");
}
#[test]
fn editor_character_animation_normalizes_seedance_request_contract() {
let normalized =
normalize_editor_character_animation_request(EditorCharacterAnimationGenerateRequest {
source_layer_id: " layer-hero ".to_string(),
source_image_src: "/generated-characters/hero/master.png".to_string(),
source_width: 768,
source_height: 1024,
prompt_text: "待机呼吸,轻微摆动。".to_string(),
resolution: "720p".to_string(),
ratio: "same".to_string(),
frame_count: 48,
duration_seconds: 6,
price_mud_points: 120,
model: EDITOR_CHARACTER_ANIMATION_MODEL.to_string(),
})
.expect("editor request should normalize");
assert_eq!(normalized.source_layer_id, "layer-hero");
assert_eq!(normalized.frame_count, 48);
assert_eq!(normalized.duration_seconds, 6);
assert_eq!(normalized.price_mud_points, 120);
assert_eq!(normalized.fps, 8);
assert_eq!(normalized.frame_width, 540);
assert_eq!(normalized.frame_height, 720);
assert_eq!(normalized.ratio, "3:4");
}
#[test]
fn editor_character_animation_rejects_invalid_frame_duration_pair() {
let error =
normalize_editor_character_animation_request(EditorCharacterAnimationGenerateRequest {
source_layer_id: "layer-hero".to_string(),
source_image_src: "/generated-characters/hero/master.png".to_string(),
source_width: 1024,
source_height: 1024,
prompt_text: "奔跑".to_string(),
resolution: "480p".to_string(),
ratio: "1:1".to_string(),
frame_count: 48,
duration_seconds: 4,
price_mud_points: 40,
model: EDITOR_CHARACTER_ANIMATION_MODEL.to_string(),
})
.expect_err("invalid frame/duration pair should fail");
assert!(
error
.body_text()
.contains("durationSeconds 必须与帧数组合")
);
}
#[test]
fn editor_character_animation_builds_required_green_screen_prompt() {
let prompt = build_editor_character_animation_prompt("行走两步后回到站姿。");
assert!(prompt.contains("生成游戏角色动画"));
assert!(prompt.contains("参考图作为首帧和尾帧"));
assert!(prompt.contains("背景固定为纯绿色绿幕"));
assert!(prompt.contains("动作描述:\n行走两步后回到站姿。"));
}
#[test]
fn editor_character_animation_price_depends_on_resolution_and_duration() {
assert_eq!(calculate_editor_character_animation_price("480p", 4), 40);
assert_eq!(calculate_editor_character_animation_price("720p", 6), 120);
}
}

View File

@@ -1,18 +1,31 @@
use std::{borrow::Cow, collections::BTreeMap};
use axum::{
Json,
extract::{Extension, Path, State},
http::StatusCode,
};
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};
use module_assets::{
AssetObjectAccessPolicy, AssetObjectFieldError, build_asset_object_upsert_input,
generate_asset_object_id,
};
use platform_image::{
DownloadedImage,
generated_asset_sheets::{
GeneratedAssetSheetConnectedIcon, slice_generated_icon_spritesheet_by_connected_components,
},
};
use platform_oss::{LegacyAssetPrefix, OssHeadObjectRequest, OssObjectAccess};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use shared_kernel::build_prefixed_uuid_id;
use spacetime_client::{
EditorAssetCreateRecordInput, EditorAssetDeleteRecordInput, EditorAssetFolderCreateRecordInput,
EditorAssetFolderDeleteRecordInput, EditorAssetFolderRecord, EditorAssetFolderUpdateRecordInput,
EditorAssetLibraryRecord, EditorAssetRecord, EditorAssetUpdateRecordInput, EditorCanvasRecord,
EditorCanvasViewportRecord, EditorProjectCreateRecordInput, EditorProjectDeleteRecordInput,
EditorProjectGetRecordInput,
EditorAssetFolderDeleteRecordInput, EditorAssetFolderRecord,
EditorAssetFolderUpdateRecordInput, EditorAssetLibraryRecord, EditorAssetRecord,
EditorAssetUpdateRecordInput, EditorCanvasRecord, EditorCanvasViewportRecord,
EditorProjectCreateRecordInput, EditorProjectDeleteRecordInput, EditorProjectGetRecordInput,
EditorProjectLayoutSaveRecordInput, EditorProjectRecord, EditorProjectRenameRecordInput,
EditorProjectResourceCreateRecordInput, EditorProjectResourceRecord, SpacetimeClientError,
};
@@ -20,12 +33,19 @@ use spacetime_client::{
use crate::{
api_response::json_success_body,
auth::AuthenticatedAccessToken,
generated_image_assets::{
GeneratedImageAssetAdapter, GeneratedImageAssetDataUrl,
adapter::{GeneratedImageAssetAdapterMetadata, GeneratedImageAssetPersistInput},
normalize_generated_image_asset_mime,
},
http_error::AppError,
openai_image_generation::{
GPT_IMAGE_2_MODEL, OpenAiReferenceImage, build_openai_image_http_client,
create_openai_image_edit_with_references, create_openai_image_generation,
DownloadedOpenAiImage, GPT_IMAGE_2_MODEL, OpenAiReferenceImage,
build_openai_image_http_client, create_openai_image_edit_with_references,
create_openai_image_edit_with_references_and_model, create_openai_image_generation,
require_openai_image_settings,
},
platform_errors::map_oss_error,
request_context::RequestContext,
state::AppState,
};
@@ -37,6 +57,13 @@ const EDITOR_ASSET_ID_PREFIX: &str = "editor-asset-";
const EDITOR_LAYOUT_MAX_BYTES: usize = 256 * 1024;
const EDITOR_PROJECT_DEFAULT_TITLE: &str = "未命名画布";
const EDITOR_IMAGE_GENERATION_SIZE: &str = "1024x1024";
const EDITOR_ICON_SPRITESHEET_MODEL: &str = "gemini-3.1-flash-image-preview";
const EDITOR_ICON_SPRITESHEET_SMALL_SIZE: &str = "512x512";
const EDITOR_ICON_SPRITESHEET_LARGE_SIZE: &str = "1024x1024";
const EDITOR_ICON_DESCRIPTION_LIMIT: usize = 100;
const EDITOR_CHARACTER_IMAGE_ASSET_KIND: &str = "editor_character_image";
const EDITOR_CHARACTER_IMAGE_ENTITY_KIND: &str = "editor_project";
const EDITOR_CHARACTER_IMAGE_SLOT: &str = "character";
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -125,6 +152,10 @@ pub struct EditorAssetUpdateRequest {
#[serde(rename_all = "camelCase")]
pub struct EditorImageGenerationRequest {
prompt: String,
size: Option<String>,
kind: Option<String>,
model: Option<String>,
reference_image_srcs: Option<Vec<String>>,
}
#[derive(Debug, Deserialize)]
@@ -134,6 +165,14 @@ pub struct EditorImageEditRequest {
source_image_src: String,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EditorIconSpritesheetGenerationRequest {
reference_image_src: String,
icon_descriptions: Vec<String>,
model: Option<String>,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EditorProjectResponse {
@@ -192,6 +231,8 @@ pub struct EditorAssetResponse {
#[serde(rename_all = "camelCase")]
pub struct EditorImageGenerationResponse {
image_src: String,
object_key: Option<String>,
asset_object_id: Option<String>,
width: u32,
height: u32,
source_type: &'static str,
@@ -202,6 +243,29 @@ pub struct EditorImageGenerationResponse {
task_id: String,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EditorIconSpritesheetIconResponse {
name: String,
image_src: String,
width: u32,
height: u32,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EditorIconSpritesheetGenerationResponse {
spritesheet_image_src: String,
spritesheet_width: u32,
spritesheet_height: u32,
icon_image_srcs: Vec<EditorIconSpritesheetIconResponse>,
prompt: String,
actual_prompt: Option<String>,
model: String,
provider: &'static str,
task_id: String,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EditorProjectPayload {
@@ -683,8 +747,8 @@ pub async fn generate_editor_image(
Extension(authenticated): Extension<AuthenticatedAccessToken>,
Json(payload): Json<EditorImageGenerationRequest>,
) -> Result<Json<Value>, AppError> {
let prompt = payload.prompt.trim().to_string();
if prompt.is_empty() {
let role_setting = payload.prompt.trim().to_string();
if role_setting.is_empty() {
return Err(
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "editor-image-generation",
@@ -693,33 +757,93 @@ pub async fn generate_editor_image(
);
}
let image_size = normalize_editor_image_generation_size(payload.size.as_deref());
let _requested_model = payload.model.as_deref();
let normalized_kind = payload.kind.as_deref().map(str::trim);
let is_character_generation = matches!(normalized_kind, Some("character"));
let submitted_prompt = if is_character_generation {
build_editor_character_image_prompt(role_setting.as_str())
} else {
role_setting.clone()
};
let failure_context = match normalized_kind {
Some("character") => "图片画布生成角色形象",
Some("spec") => "图片画布生成规范",
Some("quick-edit") => "图片画布快速编辑图片",
_ => "图片画布生成图片",
};
let reference_sources = payload
.reference_image_srcs
.unwrap_or_default()
.into_iter()
.map(|source| source.trim().to_string())
.filter(|source| !source.is_empty())
.take(5)
.collect::<Vec<_>>();
let settings = require_openai_image_settings(&state)?.with_external_api_audit_context(
&request_context,
Some(authenticated.claims().user_id().to_string()),
None,
);
let http_client = build_openai_image_http_client(&settings)?;
let generated = create_openai_image_generation(
&http_client,
&settings,
prompt.as_str(),
Some("文字、水印、边框、按钮、UI 控件、低清晰度、变形主体"),
EDITOR_IMAGE_GENERATION_SIZE,
1,
&[],
"图片画布生成图片",
)
.await?;
let image = generated.images.into_iter().next().ok_or_else(|| {
let negative_prompt = Some("文字、水印、边框、按钮、UI 控件、低清晰度、变形主体");
let generated = if reference_sources.is_empty() {
create_openai_image_generation(
&http_client,
&settings,
submitted_prompt.as_str(),
negative_prompt,
image_size.as_ref(),
1,
&[],
failure_context,
)
.await?
} else {
let reference_images = reference_sources
.iter()
.map(|source| parse_editor_reference_image(source.as_str()))
.collect::<Result<Vec<_>, _>>()?;
create_openai_image_edit_with_references(
&http_client,
&settings,
submitted_prompt.as_str(),
negative_prompt,
image_size.as_ref(),
1,
reference_images.as_slice(),
failure_context,
)
.await?
};
let mut image = generated.images.into_iter().next().ok_or_else(|| {
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "vector-engine",
"message": "VectorEngine 未返回图片",
}))
})?;
if is_character_generation {
image = prepare_editor_character_image_for_response(image);
}
let (width, height) = image::load_from_memory(image.bytes.as_slice())
.map(|image| (image.width(), image.height()))
.unwrap_or((1024, 1024));
let persisted = if is_character_generation {
Some(
persist_editor_character_image(
&state,
authenticated.claims().user_id(),
generated.task_id.as_str(),
&image,
submitted_prompt.as_str(),
generated.actual_prompt.as_deref(),
)
.await?,
)
} else {
None
};
let image_src = format!(
"data:{};base64,{}",
image.mime_type,
@@ -730,10 +854,14 @@ pub async fn generate_editor_image(
Some(&request_context),
EditorImageGenerationResponse {
image_src,
object_key: persisted.as_ref().map(|asset| asset.object_key.clone()),
asset_object_id: persisted
.as_ref()
.map(|asset| asset.asset_object_id.clone()),
width,
height,
source_type: "generated",
prompt,
prompt: role_setting,
actual_prompt: generated.actual_prompt,
model: GPT_IMAGE_2_MODEL,
provider: "VectorEngine",
@@ -742,6 +870,31 @@ pub async fn generate_editor_image(
))
}
fn normalize_editor_image_generation_size(size: Option<&str>) -> Cow<'static, str> {
match size.map(str::trim).filter(|value| !value.is_empty()) {
Some("1024x1024") | Some("1024*1024") | Some("1:1") => Cow::Borrowed("1024x1024"),
Some("1536x1024") | Some("1536*1024") | Some("16:9") => Cow::Borrowed("1536x1024"),
Some("2048x1152") | Some("2048*1152") | Some("1920x1080") | Some("1920*1080")
| Some("2k-16:9") => Cow::Borrowed("2048x1152"),
Some("1024x1536") | Some("1024*1536") | Some("9:16") => Cow::Borrowed("1024x1536"),
Some(value) if is_editor_custom_image_size(value) => Cow::Owned(value.to_string()),
_ => Cow::Borrowed(EDITOR_IMAGE_GENERATION_SIZE),
}
}
fn is_editor_custom_image_size(value: &str) -> bool {
let Some((width, height)) = value.split_once('x') else {
return false;
};
let Ok(width) = width.parse::<u32>() else {
return false;
};
let Ok(height) = height.parse::<u32>() else {
return false;
};
(64..=4096).contains(&width) && (64..=4096).contains(&height)
}
pub async fn edit_editor_image(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
@@ -795,6 +948,8 @@ pub async fn edit_editor_image(
Some(&request_context),
EditorImageGenerationResponse {
image_src,
object_key: None,
asset_object_id: None,
width,
height,
source_type: "generated",
@@ -807,6 +962,96 @@ pub async fn edit_editor_image(
))
}
pub async fn generate_editor_icon_spritesheet(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
Extension(authenticated): Extension<AuthenticatedAccessToken>,
Json(payload): Json<EditorIconSpritesheetGenerationRequest>,
) -> Result<Json<Value>, AppError> {
let icon_descriptions = normalize_icon_descriptions(payload.icon_descriptions)?;
let reference_image = parse_editor_reference_image(payload.reference_image_src.as_str())
.map_err(|error| {
error.with_details(json!({
"provider": "editor-icon-spritesheet",
"field": "referenceImageSrc",
"message": "图标素材规范必须是图片 Data URL。",
}))
})?;
let model = payload
.model
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
.unwrap_or(EDITOR_ICON_SPRITESHEET_MODEL)
.to_string();
let size = editor_icon_spritesheet_size_for_count(icon_descriptions.len());
let prompt = build_editor_icon_spritesheet_prompt(&icon_descriptions);
let settings = require_openai_image_settings(&state)?.with_external_api_audit_context(
&request_context,
Some(authenticated.claims().user_id().to_string()),
None,
);
let http_client = build_openai_image_http_client(&settings)?;
let generated = create_openai_image_edit_with_references_and_model(
&http_client,
&settings,
model.as_str(),
prompt.as_str(),
None,
size,
1,
&[reference_image],
"图片画布生成图标素材 spritesheet",
)
.await?;
let image = generated.images.into_iter().next().ok_or_else(|| {
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "vector-engine",
"message": "VectorEngine 未返回图标 spritesheet",
}))
})?;
let (spritesheet_width, spritesheet_height) = image::load_from_memory(image.bytes.as_slice())
.map(|image| (image.width(), image.height()))
.unwrap_or((512, 512));
let source = DownloadedImage {
bytes: image.bytes.clone(),
mime_type: image.mime_type.clone(),
extension: image.extension.clone(),
};
let icon_slices = slice_generated_icon_spritesheet_by_connected_components(
&source,
icon_descriptions.as_slice(),
)
.map_err(|error| {
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "editor-icon-spritesheet",
"message": error.to_string(),
}))
})?;
let spritesheet_image_src =
data_url_from_image_bytes(image.mime_type.as_str(), image.bytes.as_slice());
let icon_image_srcs = icon_slices
.into_iter()
.map(editor_icon_response_from_slice)
.collect();
Ok(json_success_body(
Some(&request_context),
EditorIconSpritesheetGenerationResponse {
spritesheet_image_src,
spritesheet_width,
spritesheet_height,
icon_image_srcs,
prompt,
actual_prompt: generated.actual_prompt,
model,
provider: "VectorEngine",
task_id: generated.task_id,
},
))
}
fn editor_project_payload_from_record(record: EditorProjectRecord) -> EditorProjectPayload {
let canvas = editor_canvas_payload_from_record(record.canvas);
EditorProjectPayload {
@@ -957,6 +1202,229 @@ fn normalize_optional_string(value: Option<String>) -> Option<String> {
.filter(|item| !item.is_empty())
}
fn sanitize_editor_storage_segment(value: &str, fallback: &str) -> String {
let normalized = value
.trim()
.chars()
.map(|character| match character {
'a'..='z' | '0'..='9' | '-' | '_' => character,
'A'..='Z' => character.to_ascii_lowercase(),
_ => '-',
})
.collect::<String>()
.split('-')
.filter(|part| !part.is_empty())
.collect::<Vec<_>>()
.join("-");
if normalized.is_empty() {
fallback.to_string()
} else {
normalized
}
}
fn normalize_icon_descriptions(descriptions: Vec<String>) -> Result<Vec<String>, AppError> {
let normalized = descriptions
.into_iter()
.map(|description| description.trim().to_string())
.filter(|description| !description.is_empty())
.take(EDITOR_ICON_DESCRIPTION_LIMIT.saturating_add(1))
.collect::<Vec<_>>();
if normalized.is_empty() || normalized.len() > EDITOR_ICON_DESCRIPTION_LIMIT {
return Err(
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "editor-icon-spritesheet",
"field": "iconDescriptions",
"message": "图标素材描述数量必须在 1 到 100 个之间。",
})),
);
}
Ok(normalized)
}
fn build_editor_icon_spritesheet_prompt(icon_descriptions: &[String]) -> String {
format!(
"参考图1的图标素材规范纯绿幕背景方便扣除背景禁止出现文字保证每个图标素材的所有内容区域是完全连通的。按照以下的素材的顺序从上到下从左到右依次生成并整理成一张spritesheet\n\n{}",
icon_descriptions.join("")
)
}
fn build_editor_character_image_prompt(role_setting: &str) -> String {
vec![
"基于图1的角色美术视觉规范指导生成游戏角色形象图。画面中心构图角色主体完整置于画面中央禁止镜头透视禁止特写。背景固定为纯绿色绿幕只作为抠像底色禁止生成美术视觉规范、出现建筑、室内布景、风景、地面道具、漂浮物、烟雾叙事元素、文字或其他角色以外的场景内容。".to_string(),
format!("角色设定:{}", role_setting.trim()),
]
.join("\n")
}
fn prepare_editor_character_image_for_response(
image: DownloadedOpenAiImage,
) -> DownloadedOpenAiImage {
let source_bytes = if image.mime_type == "image/png" {
image.bytes
} else {
match image::load_from_memory(image.bytes.as_slice()) {
Ok(decoded) => {
let mut encoded = std::io::Cursor::new(Vec::new());
if decoded
.write_to(&mut encoded, image::ImageFormat::Png)
.is_err()
{
return image;
}
encoded.into_inner()
}
Err(_) => return image,
}
};
let processed =
crate::character_visual_assets::try_apply_background_alpha_to_png(source_bytes.as_slice());
let Some(bytes) = processed else {
return DownloadedOpenAiImage {
bytes: source_bytes,
mime_type: "image/png".to_string(),
extension: "png".to_string(),
};
};
DownloadedOpenAiImage {
bytes,
mime_type: "image/png".to_string(),
extension: "png".to_string(),
}
}
fn editor_icon_spritesheet_size_for_count(icon_count: usize) -> &'static str {
if icon_count <= 25 {
EDITOR_ICON_SPRITESHEET_SMALL_SIZE
} else {
EDITOR_ICON_SPRITESHEET_LARGE_SIZE
}
}
fn data_url_from_image_bytes(mime_type: &str, bytes: &[u8]) -> String {
format!(
"data:{};base64,{}",
mime_type,
BASE64_STANDARD.encode(bytes)
)
}
fn editor_icon_response_from_slice(
icon: GeneratedAssetSheetConnectedIcon,
) -> EditorIconSpritesheetIconResponse {
EditorIconSpritesheetIconResponse {
name: icon.name,
image_src: data_url_from_image_bytes("image/png", icon.bytes.as_slice()),
width: icon.width,
height: icon.height,
}
}
struct PersistedEditorGeneratedImage {
object_key: String,
asset_object_id: String,
}
async fn persist_editor_character_image(
state: &AppState,
owner_user_id: &str,
task_id: &str,
image: &DownloadedOpenAiImage,
prompt: &str,
actual_prompt: Option<&str>,
) -> Result<PersistedEditorGeneratedImage, AppError> {
let oss_client = state.oss_client().ok_or_else(|| {
AppError::from_status(StatusCode::SERVICE_UNAVAILABLE).with_details(json!({
"provider": "aliyun-oss",
"reason": "OSS 未完成环境变量配置",
}))
})?;
let prepared =
GeneratedImageAssetAdapter::prepare_put_object(GeneratedImageAssetPersistInput {
prefix: LegacyAssetPrefix::CharacterDrafts,
path_segments: vec![
"editor".to_string(),
"character-images".to_string(),
sanitize_editor_storage_segment(task_id, "task"),
],
file_stem: "image".to_string(),
image: GeneratedImageAssetDataUrl {
format: normalize_generated_image_asset_mime(image.mime_type.as_str()),
bytes: image.bytes.clone(),
},
access: OssObjectAccess::Private,
metadata: GeneratedImageAssetAdapterMetadata {
asset_kind: Some(EDITOR_CHARACTER_IMAGE_ASSET_KIND.to_string()),
owner_user_id: Some(owner_user_id.to_string()),
entity_kind: Some(EDITOR_CHARACTER_IMAGE_ENTITY_KIND.to_string()),
entity_id: Some(task_id.to_string()),
slot: Some(EDITOR_CHARACTER_IMAGE_SLOT.to_string()),
provider: Some("vector-engine".to_string()),
task_id: Some(task_id.to_string()),
},
extra_metadata: BTreeMap::from([(
"source".to_string(),
"image-canvas-editor".to_string(),
)]),
})
.map_err(|error| {
AppError::from_status(StatusCode::INTERNAL_SERVER_ERROR).with_details(json!({
"provider": "generated-image-assets",
"message": format!("准备画板角色形象 OSS 上传请求失败:{error:?}"),
}))
})?;
let persisted_mime_type = prepared.format.mime_type.clone();
let http_client = reqwest::Client::new();
let put_result = oss_client
.put_object(&http_client, prepared.request)
.await
.map_err(|error| map_oss_error(error, "aliyun-oss"))?;
let head = oss_client
.head_object(
&http_client,
OssHeadObjectRequest {
object_key: put_result.object_key.clone(),
},
)
.await
.map_err(|error| map_oss_error(error, "aliyun-oss"))?;
let now_micros = current_utc_micros();
let asset_object = state
.spacetime_client()
.confirm_asset_object(
build_asset_object_upsert_input(
generate_asset_object_id(now_micros),
head.bucket,
head.object_key.clone(),
AssetObjectAccessPolicy::Private,
head.content_type.or(Some(persisted_mime_type)),
head.content_length,
Some(actual_prompt.unwrap_or(prompt).to_string()),
EDITOR_CHARACTER_IMAGE_ASSET_KIND.to_string(),
Some(task_id.to_string()),
Some(owner_user_id.to_string()),
None,
Some(task_id.to_string()),
now_micros,
)
.map_err(map_editor_asset_field_error)?,
)
.await
.map_err(|error| {
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "spacetimedb",
"message": error.to_string(),
}))
})?;
Ok(PersistedEditorGeneratedImage {
object_key: head.object_key,
asset_object_id: asset_object.asset_object_id,
})
}
fn parse_editor_reference_image(source: &str) -> Result<OpenAiReferenceImage, AppError> {
let Some((header, data)) = source.trim().split_once(',') else {
return Err(
@@ -1023,12 +1491,11 @@ fn map_editor_project_error(error: SpacetimeClientError) -> AppError {
"message": message,
}))
}
SpacetimeClientError::Runtime(message) => {
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
SpacetimeClientError::Runtime(message) => AppError::from_status(StatusCode::BAD_REQUEST)
.with_details(json!({
"provider": "editor-project",
"message": message,
}))
}
})),
other => AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "spacetimedb",
"message": other.to_string(),
@@ -1036,6 +1503,13 @@ fn map_editor_project_error(error: SpacetimeClientError) -> AppError {
}
}
fn map_editor_asset_field_error(error: AssetObjectFieldError) -> AppError {
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
"provider": "asset-object",
"message": error.to_string(),
}))
}
fn current_utc_micros() -> i64 {
use std::time::{SystemTime, UNIX_EPOCH};
@@ -1044,3 +1518,136 @@ fn current_utc_micros() -> i64 {
.expect("system clock should be after unix epoch");
i64::try_from(duration.as_micros()).expect("current unix micros should fit in i64")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn editor_image_generation_size_keeps_quick_edit_canvas_ratio_presets() {
assert_eq!(normalize_editor_image_generation_size(None), "1024x1024");
assert_eq!(
normalize_editor_image_generation_size(Some("1536x1024")),
"1536x1024"
);
assert_eq!(
normalize_editor_image_generation_size(Some("1024x1536")),
"1024x1536"
);
assert_eq!(
normalize_editor_image_generation_size(Some("2048x1152")),
"2048x1152"
);
assert_eq!(
normalize_editor_image_generation_size(Some("640x640")),
"640x640"
);
assert_eq!(
normalize_editor_image_generation_size(Some("bad-size")),
"1024x1024"
);
}
#[test]
fn editor_character_image_prompt_appends_user_role_setting() {
let prompt = build_editor_character_image_prompt("菜市场卖菜大妈");
assert!(prompt.contains("基于图1的角色美术视觉规范指导生成游戏角色形象图。"));
assert!(prompt.contains("背景固定为纯绿色绿幕"));
assert!(prompt.contains("禁止镜头透视"));
assert!(prompt.contains("角色设定:菜市场卖菜大妈"));
}
#[test]
fn editor_character_image_postprocess_removes_green_screen_background() {
let width = 12;
let height = 12;
let mut image = image::RgbaImage::from_pixel(width, height, image::Rgba([0, 255, 0, 255]));
for y in 4..8 {
for x in 4..8 {
image.put_pixel(x, y, image::Rgba([188, 82, 45, 255]));
}
}
let mut encoded = std::io::Cursor::new(Vec::new());
image::DynamicImage::ImageRgba8(image)
.write_to(&mut encoded, image::ImageFormat::Png)
.expect("test image should encode");
let processed = prepare_editor_character_image_for_response(DownloadedOpenAiImage {
bytes: encoded.into_inner(),
mime_type: "image/png".to_string(),
extension: "png".to_string(),
});
let decoded = image::load_from_memory(processed.bytes.as_slice())
.expect("processed image should decode")
.to_rgba8();
assert_eq!(processed.mime_type, "image/png");
assert_eq!(processed.extension, "png");
assert_eq!(decoded.get_pixel(0, 0).0[3], 0);
assert_eq!(decoded.get_pixel(5, 5).0[3], 255);
}
#[test]
fn editor_character_image_postprocess_converts_non_png_green_screen_to_transparent_png() {
let width = 12;
let height = 12;
let mut image =
image::RgbImage::from_pixel(width, height, image::Rgb([0_u8, 255_u8, 0_u8]));
for y in 4..8 {
for x in 4..8 {
image.put_pixel(x, y, image::Rgb([188, 82, 45]));
}
}
let mut encoded = std::io::Cursor::new(Vec::new());
image::DynamicImage::ImageRgb8(image)
.write_to(&mut encoded, image::ImageFormat::Jpeg)
.expect("test image should encode");
let processed = prepare_editor_character_image_for_response(DownloadedOpenAiImage {
bytes: encoded.into_inner(),
mime_type: "image/jpeg".to_string(),
extension: "jpg".to_string(),
});
let decoded = image::load_from_memory(processed.bytes.as_slice())
.expect("processed image should decode")
.to_rgba8();
assert_eq!(processed.mime_type, "image/png");
assert_eq!(processed.extension, "png");
assert_eq!(decoded.get_pixel(0, 0).0[3], 0);
assert_eq!(decoded.get_pixel(5, 5).0[3], 255);
}
#[test]
fn editor_icon_spritesheet_prompt_uses_ordered_descriptions_and_size_tiers() {
let descriptions = vec![
"返回按钮".to_string(),
"设置按钮".to_string(),
"下一关按钮".to_string(),
];
let prompt = build_editor_icon_spritesheet_prompt(&descriptions);
assert!(prompt.contains("参考图1的图标素材规范"));
assert!(prompt.contains("纯绿幕背景方便扣除背景"));
assert!(prompt.contains("返回按钮、设置按钮、下一关按钮"));
assert_eq!(editor_icon_spritesheet_size_for_count(25), "512x512");
assert_eq!(editor_icon_spritesheet_size_for_count(26), "1024x1024");
}
#[test]
fn editor_icon_description_validation_filters_empty_and_rejects_more_than_limit() {
let descriptions = normalize_icon_descriptions(vec![
" 返回按钮 ".to_string(),
" ".to_string(),
"设置按钮".to_string(),
])
.expect("valid descriptions should pass");
assert_eq!(descriptions, vec!["返回按钮", "设置按钮"]);
let too_many = (0..101)
.map(|index| format!("图标{index}"))
.collect::<Vec<_>>();
assert!(normalize_icon_descriptions(too_many).is_err());
}
}

View File

@@ -744,6 +744,7 @@ mod tests {
started_at: Some("2026-06-03T00:00:00Z".to_string()),
completed_at: None,
updated_at: "2026-06-03T00:00:00Z".to_string(),
updated_at_micros: 1_780_000_000_000_000,
lease_token: lease_token.map(ToOwned::to_owned),
}
}

View File

@@ -9,9 +9,9 @@ use crate::{
create_editor_asset, create_editor_asset_folder, create_editor_project,
create_editor_project_resource, delete_editor_asset, delete_editor_asset_folder,
delete_editor_project, edit_editor_image, generate_editor_image,
get_editor_asset_library, get_editor_project, list_editor_projects,
load_recent_editor_project, rename_editor_project, save_editor_project_layout,
update_editor_asset, update_editor_asset_folder,
generate_editor_icon_spritesheet, get_editor_asset_library, get_editor_project,
list_editor_projects, load_recent_editor_project, rename_editor_project,
save_editor_project_layout, update_editor_asset, update_editor_asset_folder,
},
state::AppState,
};
@@ -111,4 +111,11 @@ pub fn router(state: AppState) -> Router<AppState> {
require_bearer_auth,
)),
)
.route(
"/api/editor/icon-spritesheets/generations",
post(generate_editor_icon_spritesheet).route_layer(middleware::from_fn_with_state(
state.clone(),
require_bearer_auth,
)),
)
}

View File

@@ -16,8 +16,9 @@ use crate::{
assets::get_asset_history,
auth::require_bearer_auth,
character_animation_assets::{
generate_character_animation, get_character_animation_job, get_character_workflow_cache,
import_character_animation_video, list_character_animation_templates,
generate_character_animation, generate_editor_character_animation,
get_character_animation_job, get_character_workflow_cache, import_character_animation_video,
list_character_animation_templates,
publish_character_animation, put_role_asset_workflow, resolve_role_asset_workflow,
save_character_workflow_cache,
},
@@ -452,6 +453,10 @@ fn play_flow_support_router(state: AppState) -> Router<AppState> {
"/api/assets/character-animation/generate",
post(generate_character_animation),
)
.route(
"/api/editor/character-animations/generations",
post(generate_editor_character_animation),
)
.route(
"/api/assets/character-animation/jobs/{task_id}",
get(get_character_animation_job),

View File

@@ -3,7 +3,7 @@ use platform_image::{
DownloadedImage, GeneratedImages, PlatformImageError, PlatformImageStatusHint, ReferenceImage,
VECTOR_ENGINE_PROVIDER, VectorEngineImageSettings, build_vector_engine_image_http_client,
create_vector_engine_image_edit, create_vector_engine_image_edit_with_references,
create_vector_engine_image_generation,
create_vector_engine_image_edit_with_references_and_model, create_vector_engine_image_generation,
};
#[cfg(test)]
use platform_image::{
@@ -236,6 +236,49 @@ pub(crate) async fn create_openai_image_edit_with_references(
.await
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn create_openai_image_edit_with_references_and_model(
http_client: &reqwest::Client,
settings: &OpenAiImageSettings,
model: &str,
prompt: &str,
negative_prompt: Option<&str>,
size: &str,
candidate_count: u32,
reference_images: &[OpenAiReferenceImage],
failure_context: &str,
) -> Result<OpenAiGeneratedImages, AppError> {
let started_at_micros = current_utc_micros();
let request_payload = json!({
"model": model,
"size": size,
"promptChars": prompt.chars().count(),
"negativePromptChars": negative_prompt.map(str::chars).map(Iterator::count),
"referenceImageCount": reference_images.len(),
});
let result = create_vector_engine_image_edit_with_references_and_model(
http_client,
&settings.provider_settings(),
model,
prompt,
negative_prompt,
size,
candidate_count,
reference_images,
failure_context,
)
.await;
map_platform_image_result(
settings,
result,
"image_edit_with_references",
failure_context,
request_payload,
started_at_micros,
)
.await
}
#[cfg(test)]
pub(crate) fn build_openai_image_request_body(
prompt: &str,