Merge branch 'master' of https://git.genarrative.world/GenarrativeAI/Genarrative
This commit is contained in:
@@ -725,7 +725,7 @@ fn parse_admin_database_table_rows_sql_response(
|
||||
.ok_or_else(|| "SQL rows 字段格式非法".to_string())?;
|
||||
let rows = row_values
|
||||
.iter()
|
||||
.map(|row| build_admin_database_table_row(row, &columns))
|
||||
.map(|row| build_admin_database_table_row_for_table(table_name, row, &columns))
|
||||
.collect::<Vec<_>>();
|
||||
Ok(AdminDatabaseTableRowsResponse {
|
||||
table_name: table_name.to_string(),
|
||||
@@ -769,7 +769,15 @@ fn extract_sql_statement_columns(statement: &Value) -> Vec<String> {
|
||||
}
|
||||
|
||||
fn build_admin_database_table_row(row: &Value, columns: &[String]) -> AdminDatabaseTableRowPayload {
|
||||
let raw = normalize_admin_database_value(row);
|
||||
build_admin_database_table_row_for_table("", row, columns)
|
||||
}
|
||||
|
||||
fn build_admin_database_table_row_for_table(
|
||||
table_name: &str,
|
||||
row: &Value,
|
||||
columns: &[String],
|
||||
) -> AdminDatabaseTableRowPayload {
|
||||
let raw = normalize_admin_database_table_row_raw(table_name, row, columns);
|
||||
let mut cells = Map::new();
|
||||
if let Some(values) = row.as_array() {
|
||||
for (index, value) in values.iter().enumerate() {
|
||||
@@ -777,11 +785,17 @@ fn build_admin_database_table_row(row: &Value, columns: &[String]) -> AdminDatab
|
||||
.get(index)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| format!("col_{}", index + 1));
|
||||
cells.insert(key, normalize_admin_database_value(value));
|
||||
cells.insert(
|
||||
key.clone(),
|
||||
normalize_admin_database_table_cell(table_name, &key, value),
|
||||
);
|
||||
}
|
||||
} else if let Some(object) = row.as_object() {
|
||||
for (key, value) in object {
|
||||
cells.insert(key.clone(), normalize_admin_database_value(value));
|
||||
cells.insert(
|
||||
key.clone(),
|
||||
normalize_admin_database_table_cell(table_name, key, value),
|
||||
);
|
||||
}
|
||||
}
|
||||
AdminDatabaseTableRowPayload {
|
||||
@@ -790,6 +804,85 @@ fn build_admin_database_table_row(row: &Value, columns: &[String]) -> AdminDatab
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_admin_database_table_row_raw(
|
||||
table_name: &str,
|
||||
row: &Value,
|
||||
columns: &[String],
|
||||
) -> Value {
|
||||
if let Some(values) = row.as_array() {
|
||||
return Value::Array(
|
||||
values
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, value)| {
|
||||
let key = columns.get(index).map(String::as_str).unwrap_or_default();
|
||||
normalize_admin_database_table_cell(table_name, key, value)
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(object) = row.as_object() {
|
||||
return Value::Object(
|
||||
object
|
||||
.iter()
|
||||
.map(|(key, value)| {
|
||||
(
|
||||
key.clone(),
|
||||
normalize_admin_database_table_cell(table_name, key, value),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
|
||||
normalize_admin_database_value(row)
|
||||
}
|
||||
|
||||
fn normalize_admin_database_table_cell(
|
||||
table_name: &str,
|
||||
column_name: &str,
|
||||
value: &Value,
|
||||
) -> Value {
|
||||
if let Some(enum_value) = normalize_admin_database_known_enum(table_name, column_name, value) {
|
||||
return enum_value;
|
||||
}
|
||||
normalize_admin_database_value(value)
|
||||
}
|
||||
|
||||
fn normalize_admin_database_known_enum(
|
||||
table_name: &str,
|
||||
column_name: &str,
|
||||
value: &Value,
|
||||
) -> Option<Value> {
|
||||
let variant_index = extract_sats_enum_variant_index(value)?;
|
||||
let label = match (table_name, column_name) {
|
||||
("profile_recharge_order", "kind") => match variant_index {
|
||||
0 => "points",
|
||||
1 => "membership",
|
||||
_ => return None,
|
||||
},
|
||||
("profile_recharge_order", "status") => match variant_index {
|
||||
0 => "pending",
|
||||
1 => "paid",
|
||||
2 => "failed",
|
||||
3 => "closed",
|
||||
4 => "refunded",
|
||||
_ => return None,
|
||||
},
|
||||
_ => return None,
|
||||
};
|
||||
Some(Value::String(label.to_string()))
|
||||
}
|
||||
|
||||
fn extract_sats_enum_variant_index(value: &Value) -> Option<u64> {
|
||||
let items = value.as_array()?;
|
||||
if items.len() != 2 {
|
||||
return None;
|
||||
}
|
||||
items.first()?.as_u64()
|
||||
}
|
||||
|
||||
fn normalize_admin_database_value(value: &Value) -> Value {
|
||||
match value {
|
||||
Value::Array(items) if items.len() == 1 => normalize_admin_database_value(&items[0]),
|
||||
@@ -1526,6 +1619,46 @@ mod tests {
|
||||
assert_eq!(response.rows[0].cells["points"], json!(12));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_admin_database_table_rows_sql_response_maps_recharge_order_enum_cells() {
|
||||
let payload = json!([
|
||||
{
|
||||
"schema": {
|
||||
"elements": [
|
||||
{"name": {"some": "order_id"}},
|
||||
{"name": {"some": "kind"}},
|
||||
{"name": {"some": "status"}},
|
||||
{"name": {"some": "paid_at"}}
|
||||
]
|
||||
},
|
||||
"rows": [[
|
||||
"recharge:user_00000001:1778757456811099:points_60",
|
||||
[0, []],
|
||||
[0, []],
|
||||
[1, []]
|
||||
]]
|
||||
}
|
||||
]);
|
||||
|
||||
let response =
|
||||
parse_admin_database_table_rows_sql_response("profile_recharge_order", 100, payload)
|
||||
.expect("recharge order rows should parse");
|
||||
|
||||
let cells = &response.rows[0].cells;
|
||||
assert_eq!(cells["kind"], json!("points"));
|
||||
assert_eq!(cells["status"], json!("pending"));
|
||||
assert_eq!(cells["paid_at"], json!(null));
|
||||
assert_eq!(
|
||||
response.rows[0].raw,
|
||||
json!([
|
||||
"recharge:user_00000001:1778757456811099:points_60",
|
||||
"points",
|
||||
"pending",
|
||||
null
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_admin_database_table_row_normalizes_optional_sats_values() {
|
||||
let row = build_admin_database_table_row(
|
||||
|
||||
@@ -34,6 +34,10 @@ use crate::{
|
||||
auth_me::auth_me,
|
||||
auth_public_user::{get_public_user_by_code, get_public_user_by_id},
|
||||
auth_sessions::{auth_sessions, revoke_auth_session},
|
||||
bark_battle::{
|
||||
create_bark_battle_draft, finish_bark_battle_run, get_bark_battle_run,
|
||||
get_bark_battle_runtime_config, publish_bark_battle_work, start_bark_battle_run,
|
||||
},
|
||||
big_fish::{
|
||||
create_big_fish_session, delete_big_fish_work, execute_big_fish_action, get_big_fish_run,
|
||||
get_big_fish_session, get_big_fish_works, list_big_fish_gallery,
|
||||
@@ -77,6 +81,8 @@ use crate::{
|
||||
generate_custom_world_opening_cg, generate_custom_world_scene_image,
|
||||
generate_custom_world_scene_npc, upload_custom_world_cover_image,
|
||||
},
|
||||
edutainment_baby_drawing::create_baby_love_drawing_magic,
|
||||
edutainment_baby_object::generate_baby_object_match_assets,
|
||||
error_middleware::normalize_error_response,
|
||||
health::health_check,
|
||||
hyper3d_generation::{
|
||||
@@ -185,6 +191,7 @@ use crate::{
|
||||
const PUZZLE_REFERENCE_IMAGE_BODY_LIMIT_BYTES: usize = 12 * 1024 * 1024;
|
||||
const PROFILE_FEEDBACK_BODY_LIMIT_BYTES: usize = 6 * 1024 * 1024;
|
||||
const HYPER3D_IMAGE_TO_MODEL_BODY_LIMIT_BYTES: usize = 56 * 1024 * 1024;
|
||||
const BABY_LOVE_DRAWING_MAGIC_BODY_LIMIT_BYTES: usize = 8 * 1024 * 1024;
|
||||
|
||||
// 统一由这里构造 Axum 路由树,后续再逐项挂接中间件与业务路由。
|
||||
pub fn build_router(state: AppState) -> Router {
|
||||
@@ -648,6 +655,24 @@ pub fn build_router(state: AppState) -> Router {
|
||||
"/api/creation-entry/config",
|
||||
get(get_creation_entry_config_handler),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/edutainment/baby-object-match/assets",
|
||||
post(generate_baby_object_match_assets).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/edutainment/baby-love-drawing/magic",
|
||||
post(create_baby_love_drawing_magic)
|
||||
.layer(DefaultBodyLimit::max(
|
||||
BABY_LOVE_DRAWING_MAGIC_BODY_LIMIT_BYTES,
|
||||
))
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/settings",
|
||||
get(get_runtime_settings)
|
||||
@@ -1055,6 +1080,48 @@ pub fn build_router(state: AppState) -> Router {
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/bark-battle/drafts",
|
||||
post(create_bark_battle_draft).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/bark-battle/works/publish",
|
||||
post(publish_bark_battle_work).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/bark-battle/works/{work_id}/config",
|
||||
get(get_bark_battle_runtime_config).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/bark-battle/works/{work_id}/runs",
|
||||
post(start_bark_battle_run).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/bark-battle/runs/{run_id}",
|
||||
get(get_bark_battle_run).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/bark-battle/runs/{run_id}/finish",
|
||||
post(finish_bark_battle_run).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/square-hole/sessions",
|
||||
post(create_square_hole_agent_session).route_layer(middleware::from_fn_with_state(
|
||||
|
||||
776
server-rs/crates/api-server/src/bark_battle.rs
Normal file
776
server-rs/crates/api-server/src/bark_battle.rs
Normal file
@@ -0,0 +1,776 @@
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Extension, Path, State, rejection::JsonRejection},
|
||||
http::{HeaderName, StatusCode, header},
|
||||
response::Response,
|
||||
};
|
||||
use module_bark_battle::{BARK_BATTLE_RULESET_VERSION_V1, BarkBattleRuleset};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
use shared_contracts::bark_battle::{
|
||||
BarkBattleConfigEditorPayload, BarkBattleDerivedMetrics, BarkBattleDifficultyPreset,
|
||||
BarkBattleDraftConfig, BarkBattleDraftCreateRequest, BarkBattleFinishStatus,
|
||||
BarkBattlePublishedConfig, BarkBattleRunFinishRequest, BarkBattleRunFinishResponse,
|
||||
BarkBattleRunStartRequest, BarkBattleRunStartResponse, BarkBattleScoreSummary,
|
||||
BarkBattleServerResult, BarkBattleWorkPublishRequest,
|
||||
};
|
||||
use shared_kernel::{
|
||||
build_prefixed_uuid_id, format_rfc3339, format_timestamp_micros,
|
||||
offset_datetime_to_unix_micros, parse_rfc3339,
|
||||
};
|
||||
use spacetime_client::{
|
||||
BarkBattleDraftCreateRecordInput, BarkBattleRunFinishRecordInput, BarkBattleRunRecord,
|
||||
BarkBattleRunStartRecordInput, BarkBattleWorkPublishRecordInput, SpacetimeClientError,
|
||||
};
|
||||
use time::{Duration as TimeDuration, OffsetDateTime};
|
||||
|
||||
use crate::{
|
||||
api_response::json_success_body,
|
||||
auth::AuthenticatedAccessToken,
|
||||
http_error::AppError,
|
||||
request_context::RequestContext,
|
||||
state::AppState,
|
||||
work_play_tracking::{WorkPlayTrackingDraft, record_work_play_start_after_success},
|
||||
};
|
||||
|
||||
const BARK_BATTLE_RUNTIME_PROVIDER: &str = "bark-battle-runtime";
|
||||
const BARK_BATTLE_DRAFT_ID_PREFIX: &str = "bark-battle-draft-";
|
||||
const BARK_BATTLE_WORK_ID_PREFIX: &str = "bark-battle-work-";
|
||||
const BARK_BATTLE_RUN_ID_PREFIX: &str = "bark-battle-run-";
|
||||
const BARK_BATTLE_RUN_TOKEN_PREFIX: &str = "bark-battle-token-";
|
||||
const BARK_BATTLE_PLAY_TYPE_ID: &str = "bark-battle";
|
||||
const BARK_BATTLE_RUN_TTL_SECONDS: i64 = 10 * 60;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, serde::Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BarkBattleRunSnapshotRecord {
|
||||
run_id: String,
|
||||
work_id: String,
|
||||
config_version: u64,
|
||||
ruleset_version: String,
|
||||
difficulty_preset: String,
|
||||
#[serde(default)]
|
||||
client_started_at_micros: i64,
|
||||
#[serde(default)]
|
||||
server_started_at_micros: i64,
|
||||
#[serde(default)]
|
||||
server_finished_at_micros: Option<i64>,
|
||||
#[serde(default)]
|
||||
metrics_json: String,
|
||||
#[serde(default)]
|
||||
server_result: Option<String>,
|
||||
#[serde(default)]
|
||||
validation_status: String,
|
||||
#[serde(default)]
|
||||
anti_cheat_flags_json: String,
|
||||
#[serde(default)]
|
||||
leaderboard_score: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BarkBattleDraftConfigSnapshotRecord {
|
||||
draft_id: String,
|
||||
#[allow(dead_code)]
|
||||
work_id: String,
|
||||
#[allow(dead_code)]
|
||||
config_version: u64,
|
||||
#[allow(dead_code)]
|
||||
ruleset_version: String,
|
||||
#[serde(default)]
|
||||
config_json: String,
|
||||
updated_at_micros: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BarkBattleRuntimeConfigSnapshotRecord {
|
||||
work_id: String,
|
||||
source_draft_id: Option<String>,
|
||||
config_version: u64,
|
||||
ruleset_version: String,
|
||||
#[serde(default)]
|
||||
config_json: String,
|
||||
published_at_micros: i64,
|
||||
updated_at_micros: i64,
|
||||
}
|
||||
|
||||
pub async fn create_bark_battle_draft(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
payload: Result<Json<BarkBattleDraftCreateRequest>, JsonRejection>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
let Json(payload) = bark_battle_json(payload, &request_context)?;
|
||||
let now = current_utc_micros();
|
||||
let draft = state
|
||||
.spacetime_client()
|
||||
.create_bark_battle_draft(BarkBattleDraftCreateRecordInput {
|
||||
draft_id: build_prefixed_uuid_id(BARK_BATTLE_DRAFT_ID_PREFIX),
|
||||
owner_user_id: authenticated.claims().user_id().to_string(),
|
||||
work_id: build_prefixed_uuid_id(BARK_BATTLE_WORK_ID_PREFIX),
|
||||
title: Some(payload.title),
|
||||
description: payload.description,
|
||||
theme_preset: payload.theme_preset,
|
||||
player_dog_skin_preset: payload.player_dog_skin_preset,
|
||||
opponent_dog_skin_preset: payload.opponent_dog_skin_preset,
|
||||
difficulty_preset: Some(
|
||||
difficulty_to_spacetime_string(&payload.difficulty_preset).to_string(),
|
||||
),
|
||||
leaderboard_enabled: Some(payload.leaderboard_enabled),
|
||||
editor_state_json: Some("{}".to_string()),
|
||||
created_at_micros: now,
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
bark_battle_error_response(&request_context, map_bark_battle_client_error(error))
|
||||
})?;
|
||||
let draft = map_draft_config_record(draft, &request_context)?;
|
||||
Ok(json_success_body(Some(&request_context), draft))
|
||||
}
|
||||
|
||||
pub async fn publish_bark_battle_work(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
payload: Result<Json<BarkBattleWorkPublishRequest>, JsonRejection>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
let Json(payload) = bark_battle_json(payload, &request_context)?;
|
||||
ensure_non_empty(&request_context, &payload.draft_id, "draftId")?;
|
||||
let work_id = payload
|
||||
.work_id
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_else(|| build_prefixed_uuid_id(BARK_BATTLE_WORK_ID_PREFIX));
|
||||
let published_snapshot_json = payload
|
||||
.published_snapshot
|
||||
.as_ref()
|
||||
.map(serde_json::to_string)
|
||||
.transpose()
|
||||
.map_err(|error| {
|
||||
bark_battle_error_response(
|
||||
&request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": BARK_BATTLE_RUNTIME_PROVIDER,
|
||||
"message": format!("publishedSnapshot JSON 序列化失败: {error}"),
|
||||
})),
|
||||
)
|
||||
})?;
|
||||
let published = state
|
||||
.spacetime_client()
|
||||
.publish_bark_battle_work(BarkBattleWorkPublishRecordInput {
|
||||
draft_id: payload.draft_id,
|
||||
owner_user_id: authenticated.claims().user_id().to_string(),
|
||||
work_id,
|
||||
published_snapshot_json,
|
||||
published_at_micros: current_utc_micros(),
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
bark_battle_error_response(&request_context, map_bark_battle_client_error(error))
|
||||
})?;
|
||||
let published = map_published_config_record(published, &request_context)?;
|
||||
Ok(json_success_body(Some(&request_context), published))
|
||||
}
|
||||
|
||||
pub async fn get_bark_battle_runtime_config(
|
||||
State(state): State<AppState>,
|
||||
Path(work_id): Path<String>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
ensure_non_empty(&request_context, &work_id, "workId")?;
|
||||
|
||||
let config = state
|
||||
.spacetime_client()
|
||||
.get_bark_battle_runtime_config(work_id, Some(authenticated.claims().user_id().to_string()))
|
||||
.await
|
||||
.map_err(|error| {
|
||||
bark_battle_error_response(&request_context, map_bark_battle_client_error(error))
|
||||
})?;
|
||||
let config = map_runtime_config_record(config, &request_context)?;
|
||||
|
||||
Ok(json_success_body(Some(&request_context), config))
|
||||
}
|
||||
|
||||
pub async fn start_bark_battle_run(
|
||||
State(state): State<AppState>,
|
||||
Path(work_id): Path<String>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
payload: Result<Json<BarkBattleRunStartRequest>, JsonRejection>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
let maybe_payload = payload.ok().map(|Json(payload)| payload);
|
||||
let request = maybe_payload.unwrap_or_else(|| BarkBattleRunStartRequest {
|
||||
work_id: work_id.clone(),
|
||||
config_version: None,
|
||||
source_route: None,
|
||||
client_runtime_version: None,
|
||||
});
|
||||
let work_id = if request.work_id.trim().is_empty() {
|
||||
work_id
|
||||
} else {
|
||||
request.work_id.trim().to_string()
|
||||
};
|
||||
ensure_non_empty(&request_context, &work_id, "workId")?;
|
||||
|
||||
let owner_user_id = authenticated.claims().user_id().to_string();
|
||||
let runtime_config = state
|
||||
.spacetime_client()
|
||||
.get_bark_battle_runtime_config(work_id.clone(), Some(owner_user_id.clone()))
|
||||
.await
|
||||
.map_err(|error| {
|
||||
bark_battle_error_response(&request_context, map_bark_battle_client_error(error))
|
||||
})?;
|
||||
let runtime_config = map_runtime_config_record(runtime_config, &request_context)?;
|
||||
if !request.work_id.trim().is_empty() && request.work_id.trim() != work_id {
|
||||
return Err(bark_battle_bad_request(
|
||||
&request_context,
|
||||
"workId 与路径参数不一致",
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(expected_version) = request.config_version {
|
||||
if expected_version != runtime_config.config_version {
|
||||
return Err(bark_battle_bad_request(
|
||||
&request_context,
|
||||
"configVersion 与已发布配置不一致",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let client_started_at_micros = current_utc_micros();
|
||||
let run_token = build_prefixed_uuid_id(BARK_BATTLE_RUN_TOKEN_PREFIX);
|
||||
let run = state
|
||||
.spacetime_client()
|
||||
.start_bark_battle_run(BarkBattleRunStartRecordInput {
|
||||
run_id: build_prefixed_uuid_id(BARK_BATTLE_RUN_ID_PREFIX),
|
||||
run_token: run_token.clone(),
|
||||
owner_user_id: owner_user_id.clone(),
|
||||
work_id: work_id.clone(),
|
||||
config_version: u64::from(runtime_config.config_version),
|
||||
ruleset_version: runtime_config.ruleset_version.clone(),
|
||||
difficulty_preset: difficulty_to_spacetime_string(&runtime_config.difficulty_preset)
|
||||
.to_string(),
|
||||
client_started_at_micros,
|
||||
server_started_at_micros: client_started_at_micros,
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
bark_battle_error_response(&request_context, map_bark_battle_client_error(error))
|
||||
})?;
|
||||
let run_snapshot = parse_run_record(run, &request_context)?;
|
||||
|
||||
record_work_play_start_after_success(
|
||||
&state,
|
||||
&request_context,
|
||||
WorkPlayTrackingDraft::new(
|
||||
BARK_BATTLE_PLAY_TYPE_ID,
|
||||
work_id.clone(),
|
||||
&authenticated,
|
||||
"/api/runtime/bark-battle/...",
|
||||
)
|
||||
.extra(json!({
|
||||
"runId": run_snapshot.run_id,
|
||||
"workId": work_id,
|
||||
"configVersion": runtime_config.config_version,
|
||||
"rulesetVersion": runtime_config.ruleset_version,
|
||||
"difficultyPreset": runtime_config.difficulty_preset,
|
||||
"sourceRoute": request.source_route,
|
||||
"clientRuntimeVersion": request.client_runtime_version,
|
||||
})),
|
||||
)
|
||||
.await;
|
||||
|
||||
let server_started_at = format_timestamp_micros(run_snapshot.server_started_at_micros);
|
||||
let expires_at = format_timestamp_micros(
|
||||
run_snapshot
|
||||
.server_started_at_micros
|
||||
.saturating_add(BARK_BATTLE_RUN_TTL_SECONDS * 1_000_000),
|
||||
);
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
BarkBattleRunStartResponse {
|
||||
run_id: run_snapshot.run_id,
|
||||
run_token,
|
||||
work_id: run_snapshot.work_id,
|
||||
config_version: runtime_config.config_version,
|
||||
ruleset_version: runtime_config.ruleset_version.clone(),
|
||||
difficulty_preset: runtime_config.difficulty_preset.clone(),
|
||||
runtime_config,
|
||||
server_started_at,
|
||||
expires_at,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn get_bark_battle_run(
|
||||
State(state): State<AppState>,
|
||||
Path(run_id): Path<String>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
ensure_non_empty(&request_context, &run_id, "runId")?;
|
||||
let run = state
|
||||
.spacetime_client()
|
||||
.get_bark_battle_run(run_id, authenticated.claims().user_id().to_string())
|
||||
.await
|
||||
.map_err(|error| {
|
||||
bark_battle_error_response(&request_context, map_bark_battle_client_error(error))
|
||||
})?;
|
||||
let run = parse_run_record(run, &request_context)?;
|
||||
|
||||
Ok(json_success_body(Some(&request_context), run))
|
||||
}
|
||||
|
||||
pub async fn finish_bark_battle_run(
|
||||
State(state): State<AppState>,
|
||||
Path(run_id): Path<String>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
payload: Result<Json<BarkBattleRunFinishRequest>, JsonRejection>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
let Json(payload) = bark_battle_json(payload, &request_context)?;
|
||||
ensure_non_empty(&request_context, &run_id, "runId")?;
|
||||
ensure_non_empty(&request_context, &payload.work_id, "workId")?;
|
||||
ensure_non_empty(&request_context, &payload.run_token, "runToken")?;
|
||||
if payload.run_id != run_id {
|
||||
return Err(bark_battle_bad_request(
|
||||
&request_context,
|
||||
"runId 与路径参数不一致",
|
||||
));
|
||||
}
|
||||
if payload.ruleset_version != BARK_BATTLE_RULESET_VERSION_V1 {
|
||||
return Err(bark_battle_bad_request(
|
||||
&request_context,
|
||||
"rulesetVersion 不支持",
|
||||
));
|
||||
}
|
||||
|
||||
let client_finished_at_micros = parse_client_time_to_micros(&payload.client_finished_at)
|
||||
.map_err(|message| bark_battle_bad_request(&request_context, &message))?;
|
||||
let derived = &payload.derived_metrics;
|
||||
let opponent_final_energy = derive_server_opponent_final_energy(derived);
|
||||
let metrics_json = serde_json::to_string(&json!({
|
||||
"clientStartedAt": payload.client_started_at,
|
||||
"clientFinishedAt": payload.client_finished_at,
|
||||
"durationMs": payload.duration_ms,
|
||||
"derivedMetrics": payload.derived_metrics,
|
||||
"clientResult": payload.client_result,
|
||||
"sampleDigest": payload.sample_digest,
|
||||
"clientRuntimeVersion": payload.client_runtime_version,
|
||||
}))
|
||||
.unwrap_or_else(|_| "{}".to_string());
|
||||
let derived_metrics_json = serde_json::to_string(derived).unwrap_or_else(|_| "{}".to_string());
|
||||
|
||||
let run = state
|
||||
.spacetime_client()
|
||||
.finish_bark_battle_run(BarkBattleRunFinishRecordInput {
|
||||
run_id,
|
||||
run_token: payload.run_token,
|
||||
owner_user_id: authenticated.claims().user_id().to_string(),
|
||||
work_id: payload.work_id.clone(),
|
||||
config_version: u64::from(payload.config_version),
|
||||
ruleset_version: payload.ruleset_version.clone(),
|
||||
difficulty_preset: difficulty_to_spacetime_string(&payload.difficulty_preset)
|
||||
.to_string(),
|
||||
client_finished_at_micros,
|
||||
server_finished_at_micros: current_utc_micros(),
|
||||
duration_ms: payload.duration_ms,
|
||||
trigger_count: u64::from(derived.trigger_count),
|
||||
max_volume_millis: unit_to_millis(derived.max_volume),
|
||||
average_volume_millis: unit_to_millis(derived.average_volume),
|
||||
final_energy_millis: energy_to_millis(derived.final_energy),
|
||||
opponent_final_energy_millis: energy_to_millis(opponent_final_energy),
|
||||
max_combo: derived.combo_max,
|
||||
metrics_json,
|
||||
derived_metrics_json,
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
bark_battle_error_response(&request_context, map_bark_battle_client_error(error))
|
||||
})?;
|
||||
let run = parse_run_record(run, &request_context)?;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
map_finish_response(run, &payload.derived_metrics),
|
||||
))
|
||||
}
|
||||
|
||||
fn map_finish_response(
|
||||
run: BarkBattleRunSnapshotRecord,
|
||||
fallback_metrics: &BarkBattleDerivedMetrics,
|
||||
) -> BarkBattleRunFinishResponse {
|
||||
let score_summary =
|
||||
parse_score_summary(&run.metrics_json).unwrap_or_else(|| BarkBattleScoreSummary {
|
||||
duration_ms: 0,
|
||||
trigger_count: fallback_metrics.trigger_count,
|
||||
max_volume: fallback_metrics.max_volume,
|
||||
average_volume: fallback_metrics.average_volume,
|
||||
final_energy: fallback_metrics.final_energy,
|
||||
combo_max: fallback_metrics.combo_max,
|
||||
});
|
||||
BarkBattleRunFinishResponse {
|
||||
status: parse_finish_status(&run.validation_status),
|
||||
run_id: run.run_id,
|
||||
work_id: run.work_id,
|
||||
config_version: run.config_version.min(u64::from(u32::MAX)) as u32,
|
||||
ruleset_version: run.ruleset_version,
|
||||
difficulty_preset: parse_difficulty_lossy(&run.difficulty_preset),
|
||||
server_result: parse_server_result_lossy(run.server_result.as_deref()),
|
||||
score_summary,
|
||||
leaderboard_score: run.leaderboard_score,
|
||||
anti_cheat_flags: parse_string_vec(&run.anti_cheat_flags_json),
|
||||
updated_at: format_timestamp_micros(
|
||||
run.server_finished_at_micros
|
||||
.unwrap_or(run.server_started_at_micros),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_run_record(
|
||||
value: BarkBattleRunRecord,
|
||||
request_context: &RequestContext,
|
||||
) -> Result<BarkBattleRunSnapshotRecord, Response> {
|
||||
serde_json::from_value(value).map_err(|error| {
|
||||
bark_battle_error_response(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": BARK_BATTLE_RUNTIME_PROVIDER,
|
||||
"message": format!("Bark Battle run JSON 解析失败: {error}"),
|
||||
})),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_draft_snapshot_record(
|
||||
value: Value,
|
||||
request_context: &RequestContext,
|
||||
) -> Result<BarkBattleDraftConfigSnapshotRecord, Response> {
|
||||
serde_json::from_value(value)
|
||||
.map_err(|error| bark_battle_snapshot_parse_error(request_context, "draft config", error))
|
||||
}
|
||||
|
||||
fn parse_runtime_snapshot_record(
|
||||
value: Value,
|
||||
request_context: &RequestContext,
|
||||
) -> Result<BarkBattleRuntimeConfigSnapshotRecord, Response> {
|
||||
serde_json::from_value(value)
|
||||
.map_err(|error| bark_battle_snapshot_parse_error(request_context, "runtime config", error))
|
||||
}
|
||||
|
||||
fn map_draft_config_record(
|
||||
value: Value,
|
||||
request_context: &RequestContext,
|
||||
) -> Result<BarkBattleDraftConfig, Response> {
|
||||
let snapshot = parse_draft_snapshot_record(value, request_context)?;
|
||||
let editor_config = parse_editor_config_record(&snapshot.config_json, request_context)?;
|
||||
Ok(BarkBattleDraftConfig {
|
||||
draft_id: snapshot.draft_id,
|
||||
title: editor_config.title,
|
||||
description: editor_config.description,
|
||||
theme_preset: editor_config.theme_preset,
|
||||
player_dog_skin_preset: editor_config.player_dog_skin_preset,
|
||||
opponent_dog_skin_preset: editor_config.opponent_dog_skin_preset,
|
||||
difficulty_preset: editor_config.difficulty_preset,
|
||||
leaderboard_enabled: editor_config.leaderboard_enabled,
|
||||
updated_at: format_timestamp_micros(snapshot.updated_at_micros),
|
||||
})
|
||||
}
|
||||
|
||||
fn map_runtime_config_record(
|
||||
value: Value,
|
||||
request_context: &RequestContext,
|
||||
) -> Result<shared_contracts::bark_battle::BarkBattleRuntimeConfig, Response> {
|
||||
let snapshot = parse_runtime_snapshot_record(value, request_context)?;
|
||||
let editor_config = parse_editor_config_record(&snapshot.config_json, request_context)?;
|
||||
let ruleset = BarkBattleRuleset::v1();
|
||||
Ok(shared_contracts::bark_battle::BarkBattleRuntimeConfig {
|
||||
work_id: snapshot.work_id,
|
||||
config_version: snapshot.config_version.min(u64::from(u32::MAX)) as u32,
|
||||
ruleset_version: snapshot.ruleset_version,
|
||||
play_type_id: BARK_BATTLE_PLAY_TYPE_ID.to_string(),
|
||||
duration_ms: ruleset.standard_duration_ms,
|
||||
energy_min: 0.0,
|
||||
energy_max: 100.0,
|
||||
draw_threshold: ruleset.draw_threshold_energy as f32,
|
||||
min_bark_gap_ms: ruleset.min_bark_gap_ms,
|
||||
difficulty_preset: editor_config.difficulty_preset,
|
||||
theme_preset: editor_config.theme_preset,
|
||||
player_dog_skin_preset: editor_config.player_dog_skin_preset,
|
||||
opponent_dog_skin_preset: editor_config.opponent_dog_skin_preset,
|
||||
leaderboard_enabled: editor_config.leaderboard_enabled,
|
||||
updated_at: format_timestamp_micros(snapshot.updated_at_micros),
|
||||
})
|
||||
}
|
||||
|
||||
fn map_published_config_record(
|
||||
value: Value,
|
||||
request_context: &RequestContext,
|
||||
) -> Result<BarkBattlePublishedConfig, Response> {
|
||||
let snapshot = parse_runtime_snapshot_record(value, request_context)?;
|
||||
let editor_config = parse_editor_config_record(&snapshot.config_json, request_context)?;
|
||||
Ok(BarkBattlePublishedConfig {
|
||||
work_id: snapshot.work_id,
|
||||
draft_id: snapshot.source_draft_id,
|
||||
config_version: snapshot.config_version.min(u64::from(u32::MAX)) as u32,
|
||||
ruleset_version: snapshot.ruleset_version,
|
||||
play_type_id: BARK_BATTLE_PLAY_TYPE_ID.to_string(),
|
||||
title: editor_config.title,
|
||||
description: editor_config.description,
|
||||
theme_preset: editor_config.theme_preset,
|
||||
player_dog_skin_preset: editor_config.player_dog_skin_preset,
|
||||
opponent_dog_skin_preset: editor_config.opponent_dog_skin_preset,
|
||||
difficulty_preset: editor_config.difficulty_preset,
|
||||
leaderboard_enabled: editor_config.leaderboard_enabled,
|
||||
updated_at: format_timestamp_micros(snapshot.updated_at_micros),
|
||||
published_at: format_timestamp_micros(snapshot.published_at_micros),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_editor_config_record(
|
||||
config_json: &str,
|
||||
request_context: &RequestContext,
|
||||
) -> Result<BarkBattleConfigEditorPayload, Response> {
|
||||
serde_json::from_str(config_json).map_err(|error| {
|
||||
bark_battle_error_response(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": BARK_BATTLE_RUNTIME_PROVIDER,
|
||||
"message": format!("Bark Battle configJson 解析失败: {error}"),
|
||||
})),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn bark_battle_snapshot_parse_error(
|
||||
request_context: &RequestContext,
|
||||
label: &str,
|
||||
error: serde_json::Error,
|
||||
) -> Response {
|
||||
bark_battle_error_response(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": BARK_BATTLE_RUNTIME_PROVIDER,
|
||||
"message": format!("Bark Battle {label} JSON 解析失败: {error}"),
|
||||
})),
|
||||
)
|
||||
}
|
||||
|
||||
fn bark_battle_json<T>(
|
||||
payload: Result<Json<T>, JsonRejection>,
|
||||
request_context: &RequestContext,
|
||||
) -> Result<Json<T>, Response> {
|
||||
payload.map_err(|error| {
|
||||
bark_battle_error_response(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": BARK_BATTLE_RUNTIME_PROVIDER,
|
||||
"message": error.body_text(),
|
||||
})),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn ensure_non_empty(
|
||||
request_context: &RequestContext,
|
||||
value: &str,
|
||||
field_name: &str,
|
||||
) -> Result<(), Response> {
|
||||
if value.trim().is_empty() {
|
||||
return Err(bark_battle_bad_request(
|
||||
request_context,
|
||||
&format!("{field_name} is required"),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bark_battle_bad_request(request_context: &RequestContext, message: &str) -> Response {
|
||||
bark_battle_error_response(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": BARK_BATTLE_RUNTIME_PROVIDER,
|
||||
"message": message,
|
||||
})),
|
||||
)
|
||||
}
|
||||
|
||||
fn map_bark_battle_client_error(error: SpacetimeClientError) -> AppError {
|
||||
let status = match &error {
|
||||
SpacetimeClientError::Runtime(_) => StatusCode::BAD_REQUEST,
|
||||
SpacetimeClientError::Procedure(message)
|
||||
if message.contains("不存在")
|
||||
|| message.contains("not found")
|
||||
|| message.contains("does not exist") =>
|
||||
{
|
||||
StatusCode::NOT_FOUND
|
||||
}
|
||||
SpacetimeClientError::Procedure(message)
|
||||
if message.contains("不能为空")
|
||||
|| message.contains("不匹配")
|
||||
|| message.contains("不支持")
|
||||
|| message.contains("已结束")
|
||||
|| message.contains("已存在") =>
|
||||
{
|
||||
StatusCode::BAD_REQUEST
|
||||
}
|
||||
_ => StatusCode::BAD_GATEWAY,
|
||||
};
|
||||
|
||||
AppError::from_status(status).with_details(json!({
|
||||
"provider": "spacetimedb",
|
||||
"message": error.to_string(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn bark_battle_error_response(request_context: &RequestContext, error: AppError) -> Response {
|
||||
let mut response = error.into_response_with_context(Some(request_context));
|
||||
response.headers_mut().insert(
|
||||
HeaderName::from_static("x-genarrative-provider"),
|
||||
header::HeaderValue::from_static(BARK_BATTLE_RUNTIME_PROVIDER),
|
||||
);
|
||||
response
|
||||
}
|
||||
|
||||
fn parse_client_time_to_micros(value: &str) -> Result<i64, String> {
|
||||
let trimmed = value.trim();
|
||||
if trimmed.is_empty() {
|
||||
return Err("client timestamp is required".to_string());
|
||||
}
|
||||
if let Ok(micros) = trimmed.parse::<i64>() {
|
||||
return Ok(micros);
|
||||
}
|
||||
parse_rfc3339(trimmed).map(offset_datetime_to_unix_micros)
|
||||
}
|
||||
|
||||
fn current_utc_micros() -> i64 {
|
||||
let duration = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default();
|
||||
(duration.as_secs() as i64) * 1_000_000 + i64::from(duration.subsec_micros())
|
||||
}
|
||||
|
||||
fn unit_to_millis(value: f32) -> u32 {
|
||||
(value.clamp(0.0, 1.0) * 1_000.0).round() as u32
|
||||
}
|
||||
|
||||
fn energy_to_millis(value: f32) -> u32 {
|
||||
(value.clamp(0.0, 100.0) * 1_000.0).round() as u32
|
||||
}
|
||||
|
||||
fn derive_server_opponent_final_energy(metrics: &BarkBattleDerivedMetrics) -> f32 {
|
||||
let ruleset = BarkBattleRuleset::v1();
|
||||
let pressure = (metrics.average_volume * 24.0)
|
||||
+ (metrics.max_volume * 16.0)
|
||||
+ (metrics.trigger_count as f32 * 0.35)
|
||||
+ (metrics.combo_max as f32 * 0.2);
|
||||
(ruleset.max_final_energy - pressure).clamp(ruleset.min_final_energy, ruleset.max_final_energy)
|
||||
}
|
||||
|
||||
fn difficulty_to_spacetime_string(value: &BarkBattleDifficultyPreset) -> &'static str {
|
||||
match value {
|
||||
BarkBattleDifficultyPreset::Easy => "easy",
|
||||
BarkBattleDifficultyPreset::Normal => "normal",
|
||||
BarkBattleDifficultyPreset::Hard => "hard",
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_difficulty(value: &str) -> Result<BarkBattleDifficultyPreset, AppError> {
|
||||
match value {
|
||||
"easy" => Ok(BarkBattleDifficultyPreset::Easy),
|
||||
"normal" => Ok(BarkBattleDifficultyPreset::Normal),
|
||||
"hard" => Ok(BarkBattleDifficultyPreset::Hard),
|
||||
_ => Err(
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": BARK_BATTLE_RUNTIME_PROVIDER,
|
||||
"message": format!("Bark Battle difficultyPreset 不支持: {value}"),
|
||||
})),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_difficulty_lossy(value: &str) -> BarkBattleDifficultyPreset {
|
||||
parse_difficulty(value).unwrap_or(BarkBattleDifficultyPreset::Normal)
|
||||
}
|
||||
|
||||
fn parse_finish_status(value: &str) -> BarkBattleFinishStatus {
|
||||
match value {
|
||||
"accepted" => BarkBattleFinishStatus::Accepted,
|
||||
"accepted_with_flags" => BarkBattleFinishStatus::AcceptedWithFlags,
|
||||
"rejected" => BarkBattleFinishStatus::Rejected,
|
||||
_ => BarkBattleFinishStatus::Rejected,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_server_result_lossy(value: Option<&str>) -> BarkBattleServerResult {
|
||||
match value {
|
||||
Some("player_win") => BarkBattleServerResult::PlayerWin,
|
||||
Some("opponent_win") => BarkBattleServerResult::OpponentWin,
|
||||
Some("draw") => BarkBattleServerResult::Draw,
|
||||
_ => BarkBattleServerResult::Draw,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_score_summary(metrics_json: &str) -> Option<BarkBattleScoreSummary> {
|
||||
let value: Value = serde_json::from_str(metrics_json).ok()?;
|
||||
let derived = value.get("derivedMetrics")?;
|
||||
Some(BarkBattleScoreSummary {
|
||||
duration_ms: value.get("durationMs")?.as_u64()?,
|
||||
trigger_count: derived
|
||||
.get("triggerCount")?
|
||||
.as_u64()?
|
||||
.min(u64::from(u32::MAX)) as u32,
|
||||
max_volume: derived.get("maxVolume")?.as_f64()? as f32,
|
||||
average_volume: derived.get("averageVolume")?.as_f64()? as f32,
|
||||
final_energy: derived.get("finalEnergy")?.as_f64()? as f32,
|
||||
combo_max: derived.get("comboMax")?.as_u64()?.min(u64::from(u32::MAX)) as u32,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_string_vec(value: &str) -> Vec<String> {
|
||||
serde_json::from_str(value).unwrap_or_default()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn format_rfc3339_or_timestamp_micros(micros: i64) -> String {
|
||||
let seconds = micros.div_euclid(1_000_000);
|
||||
let subsec_micros = micros.rem_euclid(1_000_000);
|
||||
let Ok(value) = OffsetDateTime::from_unix_timestamp(seconds)
|
||||
.map(|value| value + TimeDuration::microseconds(subsec_micros))
|
||||
else {
|
||||
return format_timestamp_micros(micros);
|
||||
};
|
||||
format_rfc3339(value).unwrap_or_else(|_| format_timestamp_micros(micros))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn unit_and_energy_are_clamped_to_spacetime_millis() {
|
||||
assert_eq!(unit_to_millis(0.625), 625);
|
||||
assert_eq!(unit_to_millis(3.0), 1000);
|
||||
assert_eq!(energy_to_millis(88.456), 88_456);
|
||||
assert_eq!(energy_to_millis(120.0), 100_000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_rfc3339_and_numeric_client_timestamps() {
|
||||
assert_eq!(
|
||||
parse_client_time_to_micros("1713686401234567").unwrap(),
|
||||
1_713_686_401_234_567
|
||||
);
|
||||
assert_eq!(
|
||||
parse_client_time_to_micros("2024-04-21T04:00:01.234567Z").unwrap(),
|
||||
1_713_672_001_234_567
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -78,6 +78,9 @@ pub fn resolve_creation_entry_route_id(path: &str) -> Option<&'static str> {
|
||||
if normalized.starts_with("/api/runtime/match3d") {
|
||||
return Some("match3d");
|
||||
}
|
||||
if normalized.starts_with("/api/runtime/bark-battle") {
|
||||
return Some("bark-battle");
|
||||
}
|
||||
if normalized.starts_with("/api/runtime/square-hole") {
|
||||
return Some("square-hole");
|
||||
}
|
||||
@@ -90,6 +93,12 @@ pub fn resolve_creation_entry_route_id(path: &str) -> Option<&'static str> {
|
||||
if normalized.starts_with("/api/creation/visual-novel") {
|
||||
return Some("visual-novel");
|
||||
}
|
||||
if normalized.starts_with("/api/creation/edutainment/baby-object-match") {
|
||||
return Some("baby-object-match");
|
||||
}
|
||||
if normalized.starts_with("/api/creation/edutainment/baby-love-drawing") {
|
||||
return Some("baby-love-drawing");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
@@ -112,40 +121,11 @@ pub(crate) fn test_creation_entry_config_response()
|
||||
title: module_runtime::DEFAULT_CREATION_ENTRY_MODAL_TITLE.to_string(),
|
||||
description: module_runtime::DEFAULT_CREATION_ENTRY_MODAL_DESCRIPTION.to_string(),
|
||||
},
|
||||
creation_types: vec![
|
||||
test_creation_type("rpg", false, true, 10),
|
||||
test_creation_type("big-fish", false, true, 20),
|
||||
test_creation_type("puzzle", true, true, 30),
|
||||
test_creation_type("match3d", true, true, 40),
|
||||
test_creation_type("square-hole", false, true, 50),
|
||||
test_creation_type("visual-novel", false, false, 60),
|
||||
test_creation_type("airp", true, false, 70),
|
||||
test_creation_type("creative-agent", false, true, 80),
|
||||
],
|
||||
creation_types: module_runtime::default_creation_entry_type_snapshots(0),
|
||||
updated_at_micros: 0,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn test_creation_type(
|
||||
id: &str,
|
||||
visible: bool,
|
||||
open: bool,
|
||||
sort_order: i32,
|
||||
) -> module_runtime::CreationEntryTypeSnapshot {
|
||||
module_runtime::CreationEntryTypeSnapshot {
|
||||
id: id.to_string(),
|
||||
title: id.to_string(),
|
||||
subtitle: "测试入口".to_string(),
|
||||
badge: "测试".to_string(),
|
||||
image_src: format!("/creation-type-references/{id}.webp"),
|
||||
visible,
|
||||
open,
|
||||
sort_order,
|
||||
updated_at_micros: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -172,6 +152,33 @@ mod tests {
|
||||
resolve_creation_entry_route_id("/api/creation/visual-novel/sessions"),
|
||||
Some("visual-novel"),
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_creation_entry_route_id("/api/runtime/bark-battle/works/work-1/config"),
|
||||
Some("bark-battle"),
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_creation_entry_route_id("/api/creation/edutainment/baby-object-match/assets"),
|
||||
Some("baby-object-match"),
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_creation_entry_route_id("/api/creation/edutainment/baby-love-drawing/magic"),
|
||||
Some("baby-love-drawing"),
|
||||
);
|
||||
assert_eq!(resolve_creation_entry_route_id("/healthz"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_creation_entry_config_response_keeps_baby_object_match_visible() {
|
||||
let config = test_creation_entry_config_response();
|
||||
let baby_object_match = config
|
||||
.creation_types
|
||||
.iter()
|
||||
.find(|item| item.id == "baby-object-match")
|
||||
.expect("test creation entry config should include baby-object-match");
|
||||
|
||||
assert_eq!(baby_object_match.title, "宝贝识物");
|
||||
assert!(baby_object_match.visible);
|
||||
assert!(baby_object_match.open);
|
||||
assert_eq!(baby_object_match.sort_order, 90);
|
||||
}
|
||||
}
|
||||
|
||||
337
server-rs/crates/api-server/src/edutainment_baby_drawing.rs
Normal file
337
server-rs/crates/api-server/src/edutainment_baby_drawing.rs
Normal file
@@ -0,0 +1,337 @@
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Extension, State, rejection::JsonRejection},
|
||||
http::StatusCode,
|
||||
response::Response,
|
||||
};
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};
|
||||
use image::{ColorType, ImageEncoder, codecs::png::PngEncoder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::{
|
||||
api_response::json_success_body,
|
||||
http_error::AppError,
|
||||
openai_image_generation::{
|
||||
DownloadedOpenAiImage, build_openai_image_http_client, create_openai_image_generation,
|
||||
require_openai_image_settings,
|
||||
},
|
||||
request_context::RequestContext,
|
||||
state::AppState,
|
||||
};
|
||||
|
||||
const BABY_LOVE_DRAWING_PROVIDER: &str = "vector-engine-gpt-image-2";
|
||||
const BABY_LOVE_DRAWING_IMAGE_SIZE: &str = "1024x1024";
|
||||
const BABY_LOVE_DRAWING_MAX_STROKES: usize = 600;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct CreateBabyLoveDrawingMagicRequest {
|
||||
original_image_src: String,
|
||||
#[serde(default)]
|
||||
stroke_trace: Vec<BabyLoveDrawingStrokePayload>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BabyLoveDrawingStrokePayload {
|
||||
stroke_id: String,
|
||||
tool: String,
|
||||
color: String,
|
||||
#[serde(default)]
|
||||
points: Vec<BabyLoveDrawingPointPayload>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct BabyLoveDrawingPointPayload {
|
||||
x: f64,
|
||||
y: f64,
|
||||
t: f64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct CreateBabyLoveDrawingMagicResponse {
|
||||
magic_image_src: String,
|
||||
generation_provider: String,
|
||||
prompt: String,
|
||||
}
|
||||
|
||||
pub async fn create_baby_love_drawing_magic(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
payload: Result<Json<CreateBabyLoveDrawingMagicRequest>, JsonRejection>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
let Json(payload) = payload.map_err(|error| {
|
||||
baby_love_drawing_error_response(
|
||||
&request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "edutainment-baby-drawing",
|
||||
"message": error.body_text(),
|
||||
})),
|
||||
)
|
||||
})?;
|
||||
validate_magic_request(&payload)
|
||||
.map_err(|error| baby_love_drawing_error_response(&request_context, error))?;
|
||||
|
||||
let settings = require_openai_image_settings(&state)
|
||||
.map_err(|error| baby_love_drawing_error_response(&request_context, error))?;
|
||||
let http_client = build_openai_image_http_client(&settings)
|
||||
.map_err(|error| baby_love_drawing_error_response(&request_context, error))?;
|
||||
let prompt = build_baby_love_drawing_magic_prompt(payload.stroke_trace.as_slice());
|
||||
let reference_images = vec![payload.original_image_src.trim().to_string()];
|
||||
let generated = create_openai_image_generation(
|
||||
&http_client,
|
||||
&settings,
|
||||
prompt.as_str(),
|
||||
Some(build_baby_love_drawing_negative_prompt()),
|
||||
BABY_LOVE_DRAWING_IMAGE_SIZE,
|
||||
1,
|
||||
reference_images.as_slice(),
|
||||
"宝贝爱画绘画魔法图片生成失败",
|
||||
)
|
||||
.await
|
||||
.map_err(|error| baby_love_drawing_error_response(&request_context, error))?;
|
||||
let generated_image = generated.images.into_iter().next().ok_or_else(|| {
|
||||
baby_love_drawing_error_response(
|
||||
&request_context,
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": "vector-engine",
|
||||
"message": "宝贝爱画绘画魔法没有返回图片。",
|
||||
})),
|
||||
)
|
||||
})?;
|
||||
let magic_image_src = build_png_data_url(generated_image)
|
||||
.map_err(|error| baby_love_drawing_error_response(&request_context, error))?;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CreateBabyLoveDrawingMagicResponse {
|
||||
magic_image_src,
|
||||
generation_provider: BABY_LOVE_DRAWING_PROVIDER.to_string(),
|
||||
prompt,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn validate_magic_request(payload: &CreateBabyLoveDrawingMagicRequest) -> Result<(), AppError> {
|
||||
let original_image_src = payload.original_image_src.trim();
|
||||
if !original_image_src.starts_with("data:image/") || !original_image_src.contains(";base64,") {
|
||||
return Err(
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "edutainment-baby-drawing",
|
||||
"message": "绘画原图必须是图片 Data URL。",
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
if payload.stroke_trace.len() > BABY_LOVE_DRAWING_MAX_STROKES {
|
||||
return Err(
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "edutainment-baby-drawing",
|
||||
"message": "绘画笔触数量过多,请重新完成绘画后再使用魔法。",
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn build_baby_love_drawing_magic_prompt(stroke_trace: &[BabyLoveDrawingStrokePayload]) -> String {
|
||||
let stroke_count = stroke_trace.len();
|
||||
let brush_count = stroke_trace
|
||||
.iter()
|
||||
.filter(|stroke| stroke.tool.trim() == "brush")
|
||||
.count();
|
||||
let eraser_count = stroke_trace
|
||||
.iter()
|
||||
.filter(|stroke| stroke.tool.trim() == "eraser")
|
||||
.count();
|
||||
let color_summary = summarize_stroke_colors(stroke_trace);
|
||||
let trace_bounds = summarize_trace_bounds(stroke_trace);
|
||||
|
||||
format!(
|
||||
"根据参考图中的儿童绘画内容,为寓教于乐独立关卡“宝贝爱画”生成一张绘本风格图片。\n\
|
||||
必须保留小朋友原始画面的主体构图、线条方向、颜色关系和童趣笔触,不要改成与原图无关的新内容。\n\
|
||||
输出风格:明亮、温暖、柔和、卡通绘本风格,适合 4-8 岁儿童,画面干净,边缘柔和,有轻微纸面质感。\n\
|
||||
笔触信息:总笔触 {stroke_count} 条,画笔 {brush_count} 条,橡皮 {eraser_count} 条,主要颜色 {color_summary},绘制范围 {trace_bounds}。\n\
|
||||
不要生成文字、水印、Logo、按钮、UI 面板、真实照片风、恐怖或成人化内容。"
|
||||
)
|
||||
}
|
||||
|
||||
fn summarize_stroke_colors(stroke_trace: &[BabyLoveDrawingStrokePayload]) -> String {
|
||||
let mut colors = Vec::new();
|
||||
for stroke in stroke_trace {
|
||||
if stroke.stroke_id.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let color = stroke.color.trim();
|
||||
if color.is_empty() || colors.iter().any(|value| value == color) {
|
||||
continue;
|
||||
}
|
||||
colors.push(color.to_string());
|
||||
if colors.len() >= 5 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if colors.is_empty() {
|
||||
"无明显颜色记录".to_string()
|
||||
} else {
|
||||
colors.join("、")
|
||||
}
|
||||
}
|
||||
|
||||
fn summarize_trace_bounds(stroke_trace: &[BabyLoveDrawingStrokePayload]) -> String {
|
||||
let mut min_x = 1.0_f64;
|
||||
let mut min_y = 1.0_f64;
|
||||
let mut max_x = 0.0_f64;
|
||||
let mut max_y = 0.0_f64;
|
||||
let mut has_point = false;
|
||||
|
||||
for point in stroke_trace.iter().flat_map(|stroke| stroke.points.iter()) {
|
||||
if !(point.x.is_finite() && point.y.is_finite() && point.t.is_finite()) {
|
||||
continue;
|
||||
}
|
||||
has_point = true;
|
||||
min_x = min_x.min(point.x.clamp(0.0, 1.0));
|
||||
min_y = min_y.min(point.y.clamp(0.0, 1.0));
|
||||
max_x = max_x.max(point.x.clamp(0.0, 1.0));
|
||||
max_y = max_y.max(point.y.clamp(0.0, 1.0));
|
||||
}
|
||||
|
||||
if !has_point {
|
||||
return "无可用坐标记录".to_string();
|
||||
}
|
||||
|
||||
format!("x {:.2}-{:.2}, y {:.2}-{:.2}", min_x, max_x, min_y, max_y)
|
||||
}
|
||||
|
||||
fn build_baby_love_drawing_negative_prompt() -> &'static str {
|
||||
"文字,水印,Logo,按钮,UI,面板,复杂背景,真实照片风,恐怖元素,成人化内容,攻击性内容,替换原图主体,完全无关的新画面"
|
||||
}
|
||||
|
||||
fn build_png_data_url(image: DownloadedOpenAiImage) -> Result<String, AppError> {
|
||||
let png_bytes = normalize_generated_image_to_png(image.bytes.as_slice())?;
|
||||
Ok(format!(
|
||||
"data:image/png;base64,{}",
|
||||
BASE64_STANDARD.encode(png_bytes)
|
||||
))
|
||||
}
|
||||
|
||||
fn normalize_generated_image_to_png(source: &[u8]) -> Result<Vec<u8>, AppError> {
|
||||
let rgba_image = image::load_from_memory(source)
|
||||
.map_err(|error| {
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": "vector-engine",
|
||||
"message": format!("解析宝贝爱画魔法图片失败:{error}"),
|
||||
}))
|
||||
})?
|
||||
.to_rgba8();
|
||||
let (width, height) = rgba_image.dimensions();
|
||||
let mut encoded = Vec::new();
|
||||
let encoder = PngEncoder::new(&mut encoded);
|
||||
encoder
|
||||
.write_image(rgba_image.as_raw(), width, height, ColorType::Rgba8.into())
|
||||
.map_err(|error| {
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": "vector-engine",
|
||||
"message": format!("转换宝贝爱画魔法图片为 PNG 失败:{error}"),
|
||||
}))
|
||||
})?;
|
||||
|
||||
Ok(encoded)
|
||||
}
|
||||
|
||||
fn baby_love_drawing_error_response(request_context: &RequestContext, error: AppError) -> Response {
|
||||
error.into_response_with_context(Some(request_context))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn sample_request() -> CreateBabyLoveDrawingMagicRequest {
|
||||
CreateBabyLoveDrawingMagicRequest {
|
||||
original_image_src: "data:image/png;base64,abcd".to_string(),
|
||||
stroke_trace: vec![BabyLoveDrawingStrokePayload {
|
||||
stroke_id: "stroke-1".to_string(),
|
||||
tool: "brush".to_string(),
|
||||
color: "#ef4444".to_string(),
|
||||
points: vec![
|
||||
BabyLoveDrawingPointPayload {
|
||||
x: 0.2,
|
||||
y: 0.3,
|
||||
t: 1.0,
|
||||
},
|
||||
BabyLoveDrawingPointPayload {
|
||||
x: 0.7,
|
||||
y: 0.8,
|
||||
t: 2.0,
|
||||
},
|
||||
],
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn magic_prompt_keeps_child_drawing_and_picture_book_style() {
|
||||
let request = sample_request();
|
||||
let prompt = build_baby_love_drawing_magic_prompt(request.stroke_trace.as_slice());
|
||||
|
||||
assert!(prompt.contains("宝贝爱画"));
|
||||
assert!(prompt.contains("绘本风格"));
|
||||
assert!(prompt.contains("保留小朋友原始画面"));
|
||||
assert!(prompt.contains("#ef4444"));
|
||||
assert!(prompt.contains("x 0.20-0.70"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn magic_request_requires_image_data_url() {
|
||||
let request = sample_request();
|
||||
assert!(validate_magic_request(&request).is_ok());
|
||||
|
||||
let invalid = CreateBabyLoveDrawingMagicRequest {
|
||||
original_image_src: "https://example.test/image.png".to_string(),
|
||||
..sample_request()
|
||||
};
|
||||
|
||||
assert!(validate_magic_request(&invalid).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalizes_png_to_png_data_url() {
|
||||
let mut source = Vec::new();
|
||||
let pixels = vec![255u8; 4 * 2 * 2];
|
||||
let encoder = PngEncoder::new(&mut source);
|
||||
encoder
|
||||
.write_image(pixels.as_slice(), 2, 2, ColorType::Rgba8.into())
|
||||
.expect("test png should encode");
|
||||
|
||||
let image_src = build_png_data_url(DownloadedOpenAiImage {
|
||||
bytes: source,
|
||||
mime_type: "image/png".to_string(),
|
||||
extension: "png".to_string(),
|
||||
})
|
||||
.expect("test png should normalize");
|
||||
|
||||
assert!(image_src.starts_with("data:image/png;base64,"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trace_summary_ignores_invalid_points() {
|
||||
let mut request = sample_request();
|
||||
request.stroke_trace[0]
|
||||
.points
|
||||
.push(BabyLoveDrawingPointPayload {
|
||||
x: f64::NAN,
|
||||
y: 0.1,
|
||||
t: 3.0,
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
summarize_trace_bounds(request.stroke_trace.as_slice()),
|
||||
"x 0.20-0.70, y 0.30-0.80",
|
||||
);
|
||||
}
|
||||
}
|
||||
642
server-rs/crates/api-server/src/edutainment_baby_object.rs
Normal file
642
server-rs/crates/api-server/src/edutainment_baby_object.rs
Normal file
@@ -0,0 +1,642 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Extension, State, rejection::JsonRejection},
|
||||
http::StatusCode,
|
||||
response::Response,
|
||||
};
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};
|
||||
use futures_util::{StreamExt, stream::FuturesUnordered};
|
||||
use image::{ColorType, ImageEncoder, codecs::png::PngEncoder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::{
|
||||
api_response::json_success_body,
|
||||
character_visual_assets::try_apply_background_alpha_to_png,
|
||||
http_error::AppError,
|
||||
openai_image_generation::{
|
||||
DownloadedOpenAiImage, OpenAiImageSettings, build_openai_image_http_client,
|
||||
create_openai_image_generation, require_openai_image_settings,
|
||||
},
|
||||
request_context::RequestContext,
|
||||
state::AppState,
|
||||
};
|
||||
|
||||
const BABY_OBJECT_MATCH_PROVIDER: &str = "vector-engine-gpt-image-2";
|
||||
const BABY_OBJECT_MATCH_IMAGE_SIZE: &str = "1024x1024";
|
||||
const BABY_OBJECT_MATCH_BACKGROUND_IMAGE_SIZE: &str = "1536x1024";
|
||||
const BABY_OBJECT_MATCH_VECTOR_ENGINE_REQUEST_TIMEOUT_MS: u64 = 480_000;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct GenerateBabyObjectMatchAssetsRequest {
|
||||
item_names: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct GenerateBabyObjectMatchAssetsResponse {
|
||||
assets: Vec<BabyObjectMatchItemAssetPayload>,
|
||||
visual_package: BabyObjectMatchVisualPackagePayload,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BabyObjectMatchItemAssetPayload {
|
||||
item_id: String,
|
||||
item_name: String,
|
||||
image_src: String,
|
||||
asset_object_id: Option<String>,
|
||||
generation_provider: String,
|
||||
prompt: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
enum BabyObjectMatchVisualAssetKind {
|
||||
Background,
|
||||
UiFrame,
|
||||
GiftBox,
|
||||
Basket,
|
||||
SmokePuff,
|
||||
}
|
||||
|
||||
impl BabyObjectMatchVisualAssetKind {
|
||||
fn asset_id(self) -> &'static str {
|
||||
match self {
|
||||
Self::Background => "baby-object-visual-background",
|
||||
Self::UiFrame => "baby-object-visual-ui-frame",
|
||||
Self::GiftBox => "baby-object-visual-gift-box",
|
||||
Self::Basket => "baby-object-visual-basket",
|
||||
Self::SmokePuff => "baby-object-visual-smoke-puff",
|
||||
}
|
||||
}
|
||||
|
||||
fn contract_kind(self) -> &'static str {
|
||||
match self {
|
||||
Self::Background => "background",
|
||||
Self::UiFrame => "ui-frame",
|
||||
Self::GiftBox => "gift-box",
|
||||
Self::Basket => "basket",
|
||||
Self::SmokePuff => "smoke-puff",
|
||||
}
|
||||
}
|
||||
|
||||
fn requires_transparency(self) -> bool {
|
||||
!matches!(self, Self::Background)
|
||||
}
|
||||
|
||||
fn image_size(self) -> &'static str {
|
||||
match self {
|
||||
Self::Background => BABY_OBJECT_MATCH_BACKGROUND_IMAGE_SIZE,
|
||||
Self::UiFrame | Self::GiftBox | Self::Basket | Self::SmokePuff => {
|
||||
BABY_OBJECT_MATCH_IMAGE_SIZE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn failure_context(self) -> &'static str {
|
||||
match self {
|
||||
Self::Background => "宝贝识物背景环境图片生成失败",
|
||||
Self::UiFrame => "宝贝识物 UI 装饰图片生成失败",
|
||||
Self::GiftBox => "宝贝识物礼物盒图片生成失败",
|
||||
Self::Basket => "宝贝识物篮子图片生成失败",
|
||||
Self::SmokePuff => "宝贝识物烟雾特效图片生成失败",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BabyObjectMatchVisualPackagePayload {
|
||||
theme_prompt: String,
|
||||
assets: Vec<BabyObjectMatchVisualAssetPayload>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct BabyObjectMatchVisualAssetPayload {
|
||||
asset_id: String,
|
||||
asset_kind: String,
|
||||
image_src: String,
|
||||
asset_object_id: Option<String>,
|
||||
generation_provider: String,
|
||||
prompt: String,
|
||||
}
|
||||
|
||||
pub async fn generate_baby_object_match_assets(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
payload: Result<Json<GenerateBabyObjectMatchAssetsRequest>, JsonRejection>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
let Json(payload) = payload.map_err(|error| {
|
||||
baby_object_match_error_response(
|
||||
&request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "edutainment-baby-object",
|
||||
"message": error.body_text(),
|
||||
})),
|
||||
)
|
||||
})?;
|
||||
let item_names = normalize_item_names(payload.item_names)
|
||||
.map_err(|error| baby_object_match_error_response(&request_context, error))?;
|
||||
|
||||
let settings = require_openai_image_settings(&state)
|
||||
.map_err(|error| baby_object_match_error_response(&request_context, error))?;
|
||||
let settings = with_baby_object_match_image_timeout(settings);
|
||||
let http_client = build_openai_image_http_client(&settings)
|
||||
.map_err(|error| baby_object_match_error_response(&request_context, error))?;
|
||||
|
||||
let request_started_at = Instant::now();
|
||||
tracing::info!(
|
||||
item_count = item_names.len(),
|
||||
"宝贝识物 image-2 资源生成开始"
|
||||
);
|
||||
let (assets, visual_package) = tokio::try_join!(
|
||||
build_baby_object_match_item_assets(&http_client, &settings, item_names.as_slice()),
|
||||
build_baby_object_match_visual_package(&http_client, &settings, item_names.as_slice()),
|
||||
)
|
||||
.map_err(|error| baby_object_match_error_response(&request_context, error))?;
|
||||
tracing::info!(
|
||||
elapsed_ms = request_started_at.elapsed().as_millis() as u64,
|
||||
"宝贝识物 image-2 资源生成完成"
|
||||
);
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
GenerateBabyObjectMatchAssetsResponse {
|
||||
assets,
|
||||
visual_package,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn normalize_item_names(item_names: Vec<String>) -> Result<Vec<String>, AppError> {
|
||||
let normalized = item_names
|
||||
.into_iter()
|
||||
.map(|value| value.trim().to_string())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if normalized.len() != 2 || normalized.iter().any(|value| value.is_empty()) {
|
||||
return Err(
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "edutainment-baby-object",
|
||||
"message": "请填写两个物品名称。",
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(normalized)
|
||||
}
|
||||
|
||||
fn build_baby_object_match_item_prompt(item_name: &str) -> String {
|
||||
format!(
|
||||
"为儿童动作 Demo 玩法“宝贝识物”生成物品素材。关键词:{item_name}。\n\
|
||||
风格必须与寓教于乐板块统一:明亮、温暖、卡通绘本质感,适合 4-8 岁儿童,物体边缘清晰,色彩干净,能自然放在草地舞台插画中。\n\
|
||||
画面只允许出现一个围绕关键词“{item_name}”的单一物品主体,不要生成组合物、多个物体、人物、手、篮子、礼物盒或玩法 UI。\n\
|
||||
不要生成背景、场景、氛围渲染、阴影地面、文字、水印、边框或按钮。背景必须是纯白或直接透明,便于服务端做透明抠图。\n\
|
||||
输出为居中完整物品,留少量透明安全边距,最终素材将作为透明 PNG 进入游戏。"
|
||||
)
|
||||
}
|
||||
|
||||
fn build_baby_object_match_negative_prompt() -> &'static str {
|
||||
"背景,场景,草地,天空,房间,光效氛围,多个物品,组合套装,人物,手,篮子,礼物盒,包装文字,标签文字,水印,Logo,UI,按钮,边框,真实照片风,复杂投影"
|
||||
}
|
||||
|
||||
fn with_baby_object_match_image_timeout(mut settings: OpenAiImageSettings) -> OpenAiImageSettings {
|
||||
settings.request_timeout_ms = settings
|
||||
.request_timeout_ms
|
||||
.max(BABY_OBJECT_MATCH_VECTOR_ENGINE_REQUEST_TIMEOUT_MS);
|
||||
settings
|
||||
}
|
||||
|
||||
async fn build_baby_object_match_item_assets(
|
||||
http_client: &reqwest::Client,
|
||||
settings: &OpenAiImageSettings,
|
||||
item_names: &[String],
|
||||
) -> Result<Vec<BabyObjectMatchItemAssetPayload>, AppError> {
|
||||
let mut pending = FuturesUnordered::new();
|
||||
|
||||
// 中文注释:两个物品图互不依赖,并发生成可缩短创作等待时间。
|
||||
for (index, item_name) in item_names.iter().cloned().enumerate() {
|
||||
let prompt = build_baby_object_match_item_prompt(item_name.as_str());
|
||||
pending.push(async move {
|
||||
let asset_started_at = Instant::now();
|
||||
tracing::info!(
|
||||
asset_kind = "item",
|
||||
item_index = index + 1,
|
||||
item_name = %item_name,
|
||||
"宝贝识物 image-2 物品资源生成开始"
|
||||
);
|
||||
let generated = create_openai_image_generation(
|
||||
http_client,
|
||||
settings,
|
||||
prompt.as_str(),
|
||||
Some(build_baby_object_match_negative_prompt()),
|
||||
BABY_OBJECT_MATCH_IMAGE_SIZE,
|
||||
1,
|
||||
&[],
|
||||
"宝贝识物物品图片生成失败",
|
||||
)
|
||||
.await?;
|
||||
let generated_image = generated.images.into_iter().next().ok_or_else(|| {
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": "vector-engine",
|
||||
"message": "宝贝识物物品图片生成没有返回图片。",
|
||||
}))
|
||||
})?;
|
||||
let image_src = build_transparent_png_data_url(generated_image)?;
|
||||
tracing::info!(
|
||||
asset_kind = "item",
|
||||
item_index = index + 1,
|
||||
item_name = %item_name,
|
||||
elapsed_ms = asset_started_at.elapsed().as_millis() as u64,
|
||||
"宝贝识物 image-2 物品资源生成完成"
|
||||
);
|
||||
|
||||
Ok::<_, AppError>(BabyObjectMatchItemAssetPayload {
|
||||
item_id: format!("baby-object-item-{}", index + 1),
|
||||
item_name,
|
||||
image_src,
|
||||
asset_object_id: None,
|
||||
generation_provider: BABY_OBJECT_MATCH_PROVIDER.to_string(),
|
||||
prompt,
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
let mut assets = Vec::with_capacity(item_names.len());
|
||||
while let Some(result) = pending.next().await {
|
||||
assets.push(result?);
|
||||
}
|
||||
assets.sort_by_key(|asset| asset.item_id.clone());
|
||||
|
||||
Ok(assets)
|
||||
}
|
||||
|
||||
async fn build_baby_object_match_visual_package(
|
||||
http_client: &reqwest::Client,
|
||||
settings: &OpenAiImageSettings,
|
||||
item_names: &[String],
|
||||
) -> Result<BabyObjectMatchVisualPackagePayload, AppError> {
|
||||
let package_started_at = Instant::now();
|
||||
let theme_prompt = build_baby_object_match_visual_theme_prompt(item_names);
|
||||
let kinds = [
|
||||
BabyObjectMatchVisualAssetKind::Background,
|
||||
BabyObjectMatchVisualAssetKind::UiFrame,
|
||||
BabyObjectMatchVisualAssetKind::GiftBox,
|
||||
BabyObjectMatchVisualAssetKind::Basket,
|
||||
BabyObjectMatchVisualAssetKind::SmokePuff,
|
||||
];
|
||||
let mut pending = FuturesUnordered::new();
|
||||
tracing::info!(
|
||||
asset_count = kinds.len(),
|
||||
"宝贝识物 image-2 视觉主题包生成开始"
|
||||
);
|
||||
|
||||
for kind in kinds.iter().copied() {
|
||||
let prompt = build_baby_object_match_visual_asset_prompt(kind, item_names, &theme_prompt);
|
||||
pending.push(async move {
|
||||
let asset_started_at = Instant::now();
|
||||
let asset_kind = kind.contract_kind();
|
||||
tracing::info!(asset_kind, "宝贝识物 image-2 视觉资源生成开始");
|
||||
let generated = create_openai_image_generation(
|
||||
http_client,
|
||||
settings,
|
||||
prompt.as_str(),
|
||||
Some(build_baby_object_match_visual_negative_prompt(kind)),
|
||||
kind.image_size(),
|
||||
1,
|
||||
&[],
|
||||
kind.failure_context(),
|
||||
)
|
||||
.await?;
|
||||
let generated_image = generated.images.into_iter().next().ok_or_else(|| {
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": "vector-engine",
|
||||
"message": format!("{}:VectorEngine 没有返回图片。", kind.failure_context()),
|
||||
}))
|
||||
})?;
|
||||
let image_src = if kind.requires_transparency() {
|
||||
build_transparent_png_data_url(generated_image)?
|
||||
} else {
|
||||
build_png_data_url(generated_image)?
|
||||
};
|
||||
tracing::info!(
|
||||
asset_kind,
|
||||
elapsed_ms = asset_started_at.elapsed().as_millis() as u64,
|
||||
"宝贝识物 image-2 视觉资源生成完成"
|
||||
);
|
||||
|
||||
Ok::<_, AppError>(BabyObjectMatchVisualAssetPayload {
|
||||
asset_id: kind.asset_id().to_string(),
|
||||
asset_kind: asset_kind.to_string(),
|
||||
image_src,
|
||||
asset_object_id: None,
|
||||
generation_provider: BABY_OBJECT_MATCH_PROVIDER.to_string(),
|
||||
prompt,
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
let mut assets = Vec::with_capacity(kinds.len());
|
||||
while let Some(result) = pending.next().await {
|
||||
assets.push(result?);
|
||||
}
|
||||
assets.sort_by_key(|asset| match asset.asset_kind.as_str() {
|
||||
"background" => 0,
|
||||
"ui-frame" => 1,
|
||||
"gift-box" => 2,
|
||||
"basket" => 3,
|
||||
"smoke-puff" => 4,
|
||||
_ => 5,
|
||||
});
|
||||
tracing::info!(
|
||||
elapsed_ms = package_started_at.elapsed().as_millis() as u64,
|
||||
"宝贝识物 image-2 视觉主题包生成完成"
|
||||
);
|
||||
|
||||
Ok(BabyObjectMatchVisualPackagePayload {
|
||||
theme_prompt,
|
||||
assets,
|
||||
})
|
||||
}
|
||||
|
||||
fn build_baby_object_match_visual_theme_prompt(item_names: &[String]) -> String {
|
||||
let item_a = item_names.first().map(String::as_str).unwrap_or_default();
|
||||
let item_b = item_names.get(1).map(String::as_str).unwrap_or_default();
|
||||
let theme_hint = resolve_baby_object_match_theme_hint(item_names);
|
||||
|
||||
format!(
|
||||
"根据创作者填写的两个物品关键词“{item_a}”和“{item_b}”,为儿童动作 Demo 玩法“宝贝识物”生成一套完整游戏视觉包装。\n\
|
||||
视觉必须保持寓教于乐板块统一的明亮、温暖、卡通绘本插画风,适合 4-8 岁儿童。\n\
|
||||
主题匹配:{theme_hint}\n\
|
||||
所有资源需要围绕这两个关键词形成统一主题,但不能改变物品识别和左右篮子固定规则。"
|
||||
)
|
||||
}
|
||||
|
||||
fn resolve_baby_object_match_theme_hint(item_names: &[String]) -> &'static str {
|
||||
let joined = item_names.join(" ").to_lowercase();
|
||||
let fruit_keywords = [
|
||||
"苹果",
|
||||
"橘子",
|
||||
"桔子",
|
||||
"香蕉",
|
||||
"葡萄",
|
||||
"草莓",
|
||||
"西瓜",
|
||||
"梨",
|
||||
"桃",
|
||||
"水果",
|
||||
"apple",
|
||||
"orange",
|
||||
"banana",
|
||||
"grape",
|
||||
"strawberry",
|
||||
"watermelon",
|
||||
"fruit",
|
||||
];
|
||||
let character_keywords = [
|
||||
"佩琪",
|
||||
"小猪佩奇",
|
||||
"小猪佩琪",
|
||||
"奥特曼",
|
||||
"动漫",
|
||||
"动画",
|
||||
"卡通",
|
||||
"玩具",
|
||||
"角色",
|
||||
"公仔",
|
||||
"peppa",
|
||||
"ultraman",
|
||||
"anime",
|
||||
"cartoon",
|
||||
"toy",
|
||||
"doll",
|
||||
"figure",
|
||||
];
|
||||
|
||||
if fruit_keywords
|
||||
.iter()
|
||||
.any(|keyword| joined.contains(keyword))
|
||||
{
|
||||
return "若关键词属于水果,背景环境和 UI 元素匹配果园、自然、阳光、树叶等主题。";
|
||||
}
|
||||
|
||||
if character_keywords
|
||||
.iter()
|
||||
.any(|keyword| joined.contains(keyword))
|
||||
{
|
||||
return "若关键词属于动漫角色、玩具或公仔,背景环境和 UI 元素匹配动漫、玩具房、儿童玩具等主题。";
|
||||
}
|
||||
|
||||
"根据关键词语义自然匹配合适主题,保持儿童寓教于乐插画风。"
|
||||
}
|
||||
|
||||
fn build_baby_object_match_visual_asset_prompt(
|
||||
kind: BabyObjectMatchVisualAssetKind,
|
||||
item_names: &[String],
|
||||
theme_prompt: &str,
|
||||
) -> String {
|
||||
let item_a = item_names.first().map(String::as_str).unwrap_or_default();
|
||||
let item_b = item_names.get(1).map(String::as_str).unwrap_or_default();
|
||||
let base = format!(
|
||||
"{theme_prompt}\n\
|
||||
当前两个关键词:{item_a}、{item_b}。\n\
|
||||
输出必须是无文字、无水印、无 Logo 的游戏美术资源。"
|
||||
);
|
||||
|
||||
match kind {
|
||||
BabyObjectMatchVisualAssetKind::Background => format!(
|
||||
"{base}\n\
|
||||
生成游戏背景环境图。背景需要根据关键词主题匹配环境,例如水果可偏果园自然,动漫角色或玩具可偏动漫玩具主题。\n\
|
||||
保持中间、屏幕中下方和底部左右篮子区域清爽,给放大后的礼物盒、中央物品和左右大篮子预留足够空间,不能画入礼物盒、篮子、物品、人物、文字或操作 UI。"
|
||||
),
|
||||
BabyObjectMatchVisualAssetKind::UiFrame => format!(
|
||||
"{base}\n\
|
||||
生成透明 PNG 的 UI 装饰框资源,用于字幕条和计数器的风格化包装。\n\
|
||||
只生成柔和装饰边框、贴纸感边缘和少量主题点缀,不生成任何文字、数字、按钮、图标说明或大面积背景。背景需要纯白或透明友好,便于抠图。"
|
||||
),
|
||||
BabyObjectMatchVisualAssetKind::GiftBox => format!(
|
||||
"{base}\n\
|
||||
生成透明 PNG 的大号礼物盒资源。礼物盒会在游戏中以约 2 倍视觉尺寸展示,需要主体饱满、轮廓清晰、中心构图、边缘安全留白少,打开动画时可被烟雾遮罩后移除。\n\
|
||||
礼物盒要与关键词主题匹配,可以带主题贴纸感装饰,但不能出现任何文字、人物、手、篮子或待分类物品。背景需要纯白或透明友好,便于抠图。"
|
||||
),
|
||||
BabyObjectMatchVisualAssetKind::Basket => format!(
|
||||
"{base}\n\
|
||||
生成透明 PNG 的大号篮子资源,游戏左右两侧会复用同一个篮子造型并以约 1.5 倍视觉尺寸展示。篮子主体要饱满、开口清晰、可读性高、边缘安全留白少。\n\
|
||||
篮子要与关键词主题匹配,可以有主题色和贴纸感边缘,但不能出现任何文字、礼物盒、人物、手或待分类物品。背景需要纯白或透明友好,便于抠图。"
|
||||
),
|
||||
BabyObjectMatchVisualAssetKind::SmokePuff => format!(
|
||||
"{base}\n\
|
||||
生成透明 PNG 的烟雾弹出特效资源,用于礼物盒打开瞬间。画面只允许出现一团柔和、圆润、儿童绘本风的云朵烟雾和少量主题色星点,不要生成礼物盒、篮子、物品、人物、手、文字或 UI。\n\
|
||||
烟雾需要中心构图、边缘柔和、透明边界干净,适合覆盖礼物盒打开区域并衬托中央物品弹出。背景需要纯白或透明友好,便于抠图。"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_baby_object_match_visual_negative_prompt(
|
||||
kind: BabyObjectMatchVisualAssetKind,
|
||||
) -> &'static str {
|
||||
match kind {
|
||||
BabyObjectMatchVisualAssetKind::Background => {
|
||||
"文字,数字,水印,Logo,按钮,说明面板,人物,手,礼物盒,篮子,中心物品,复杂前景遮挡,真实照片风,暗黑风"
|
||||
}
|
||||
BabyObjectMatchVisualAssetKind::UiFrame => {
|
||||
"文字,数字,水印,Logo,按钮,复杂面板,大面积实心背景,人物,手,礼物盒,篮子,物品主体,真实照片风"
|
||||
}
|
||||
BabyObjectMatchVisualAssetKind::GiftBox => {
|
||||
"文字,数字,水印,Logo,人物,手,篮子,待分类物品,大面积背景,场景,真实照片风"
|
||||
}
|
||||
BabyObjectMatchVisualAssetKind::Basket => {
|
||||
"文字,数字,水印,Logo,人物,手,礼物盒,待分类物品,大面积背景,场景,真实照片风"
|
||||
}
|
||||
BabyObjectMatchVisualAssetKind::SmokePuff => {
|
||||
"文字,数字,水印,Logo,人物,手,礼物盒,篮子,待分类物品,大面积背景,场景,真实照片风,硬边爆炸,火焰"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn build_transparent_png_data_url(image: DownloadedOpenAiImage) -> Result<String, AppError> {
|
||||
let png_bytes = normalize_generated_image_to_png(image.bytes.as_slice())?;
|
||||
let transparent_png_bytes =
|
||||
try_apply_background_alpha_to_png(png_bytes.as_slice()).unwrap_or(png_bytes);
|
||||
Ok(format!(
|
||||
"data:image/png;base64,{}",
|
||||
BASE64_STANDARD.encode(transparent_png_bytes)
|
||||
))
|
||||
}
|
||||
|
||||
fn build_png_data_url(image: DownloadedOpenAiImage) -> Result<String, AppError> {
|
||||
let png_bytes = normalize_generated_image_to_png(image.bytes.as_slice())?;
|
||||
Ok(format!(
|
||||
"data:image/png;base64,{}",
|
||||
BASE64_STANDARD.encode(png_bytes)
|
||||
))
|
||||
}
|
||||
|
||||
fn normalize_generated_image_to_png(source: &[u8]) -> Result<Vec<u8>, AppError> {
|
||||
let rgba_image = image::load_from_memory(source)
|
||||
.map_err(|error| {
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": "vector-engine",
|
||||
"message": format!("解析宝贝识物物品图片失败:{error}"),
|
||||
}))
|
||||
})?
|
||||
.to_rgba8();
|
||||
let (width, height) = rgba_image.dimensions();
|
||||
let mut encoded = Vec::new();
|
||||
let encoder = PngEncoder::new(&mut encoded);
|
||||
encoder
|
||||
.write_image(rgba_image.as_raw(), width, height, ColorType::Rgba8.into())
|
||||
.map_err(|error| {
|
||||
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
|
||||
"provider": "vector-engine",
|
||||
"message": format!("转换宝贝识物物品图片为 PNG 失败:{error}"),
|
||||
}))
|
||||
})?;
|
||||
|
||||
Ok(encoded)
|
||||
}
|
||||
|
||||
fn baby_object_match_error_response(request_context: &RequestContext, error: AppError) -> Response {
|
||||
error.into_response_with_context(Some(request_context))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn prompt_locks_single_transparent_object_constraints() {
|
||||
let prompt = build_baby_object_match_item_prompt("苹果");
|
||||
|
||||
assert!(prompt.contains("苹果"));
|
||||
assert!(prompt.contains("卡通绘本"));
|
||||
assert!(prompt.contains("单一物品"));
|
||||
assert!(prompt.contains("不要生成背景"));
|
||||
assert!(prompt.contains("透明 PNG"));
|
||||
assert!(prompt.contains("纯白或直接透明"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn visual_theme_prompt_maps_fruit_keywords_to_nature_theme() {
|
||||
let names = vec!["苹果".to_string(), "橘子".to_string()];
|
||||
let prompt = build_baby_object_match_visual_theme_prompt(names.as_slice());
|
||||
|
||||
assert!(prompt.contains("寓教于乐"));
|
||||
assert!(prompt.contains("卡通绘本"));
|
||||
assert!(prompt.contains("果园"));
|
||||
assert!(prompt.contains("自然"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn visual_theme_prompt_maps_character_keywords_to_toy_theme() {
|
||||
let names = vec!["小猪佩琪".to_string(), "奥特曼".to_string()];
|
||||
let prompt = build_baby_object_match_visual_theme_prompt(names.as_slice());
|
||||
|
||||
assert!(prompt.contains("寓教于乐"));
|
||||
assert!(prompt.contains("动漫"));
|
||||
assert!(prompt.contains("玩具"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn visual_asset_prompt_keeps_background_clear_for_playfield() {
|
||||
let names = vec!["苹果".to_string(), "香蕉".to_string()];
|
||||
let theme_prompt = build_baby_object_match_visual_theme_prompt(names.as_slice());
|
||||
let prompt = build_baby_object_match_visual_asset_prompt(
|
||||
BabyObjectMatchVisualAssetKind::Background,
|
||||
names.as_slice(),
|
||||
theme_prompt.as_str(),
|
||||
);
|
||||
|
||||
assert!(prompt.contains("背景环境图"));
|
||||
assert!(prompt.contains("中间"));
|
||||
assert!(prompt.contains("屏幕中下方"));
|
||||
assert!(prompt.contains("无文字"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_item_names_requires_two_non_empty_names() {
|
||||
let names = normalize_item_names(vec![" 苹果 ".to_string(), "香蕉".to_string()])
|
||||
.expect("two names should be valid");
|
||||
|
||||
assert_eq!(names, vec!["苹果".to_string(), "香蕉".to_string()]);
|
||||
assert!(normalize_item_names(vec!["苹果".to_string()]).is_err());
|
||||
assert!(normalize_item_names(vec!["苹果".to_string(), " ".to_string()]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn baby_object_match_image_timeout_keeps_long_generation_alive() {
|
||||
let settings = with_baby_object_match_image_timeout(OpenAiImageSettings {
|
||||
base_url: "https://vector.example".to_string(),
|
||||
api_key: "secret".to_string(),
|
||||
request_timeout_ms: 180_000,
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
settings.request_timeout_ms,
|
||||
BABY_OBJECT_MATCH_VECTOR_ENGINE_REQUEST_TIMEOUT_MS
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalizes_png_to_transparent_png_data_url() {
|
||||
let mut source = Vec::new();
|
||||
let pixels = vec![255u8; 4 * 2 * 2];
|
||||
let encoder = PngEncoder::new(&mut source);
|
||||
encoder
|
||||
.write_image(pixels.as_slice(), 2, 2, ColorType::Rgba8.into())
|
||||
.expect("test png should encode");
|
||||
|
||||
let image_src = build_transparent_png_data_url(DownloadedOpenAiImage {
|
||||
bytes: source,
|
||||
mime_type: "image/png".to_string(),
|
||||
extension: "png".to_string(),
|
||||
})
|
||||
.expect("test png should normalize");
|
||||
|
||||
assert!(image_src.starts_with("data:image/png;base64,"));
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ mod auth_payload;
|
||||
mod auth_public_user;
|
||||
mod auth_session;
|
||||
mod auth_sessions;
|
||||
mod bark_battle;
|
||||
mod big_fish;
|
||||
mod big_fish_agent_turn;
|
||||
mod big_fish_draft_compiler;
|
||||
@@ -34,6 +35,8 @@ mod custom_world_asset_prompts;
|
||||
mod custom_world_foundation_draft;
|
||||
mod custom_world_result_prompts;
|
||||
mod custom_world_rpg_draft_prompts;
|
||||
mod edutainment_baby_drawing;
|
||||
mod edutainment_baby_object;
|
||||
mod error_middleware;
|
||||
mod health;
|
||||
mod http_error;
|
||||
|
||||
Reference in New Issue
Block a user