Merge remote-tracking branch 'origin/master' into codex/ddd
# Conflicts: # docs/technical/README.md # docs/technical/RUST_API_SERVER_ROUTE_INDEX_2026-04-22.md # docs/technical/SPACETIMEDB_TABLE_CATALOG.md # scripts/generate-spacetime-bindings.mjs # server-rs/crates/api-server/src/app.rs # server-rs/crates/api-server/src/assets.rs # server-rs/crates/api-server/src/big_fish.rs # server-rs/crates/api-server/src/custom_world_ai.rs # server-rs/crates/api-server/src/llm.rs # server-rs/crates/api-server/src/main.rs # server-rs/crates/api-server/src/puzzle.rs # server-rs/crates/api-server/src/runtime_profile.rs # server-rs/crates/api-server/src/runtime_story/compat/ai.rs # server-rs/crates/api-server/src/runtime_story/compat/npc_actions.rs # server-rs/crates/api-server/src/runtime_story/compat/presentation.rs # server-rs/crates/api-server/src/runtime_story/compat/tests.rs # server-rs/crates/api-server/src/state.rs # server-rs/crates/module-auth/src/lib.rs # server-rs/crates/module-big-fish/src/lib.rs # server-rs/crates/module-custom-world/src/lib.rs # server-rs/crates/module-puzzle/src/lib.rs # server-rs/crates/module-runtime/src/lib.rs # server-rs/crates/spacetime-client/src/big_fish.rs # server-rs/crates/spacetime-client/src/lib.rs # server-rs/crates/spacetime-client/src/mapper.rs # server-rs/crates/spacetime-client/src/module_bindings/admin_disable_profile_redeem_code_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/admin_upsert_profile_redeem_code_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/advance_puzzle_next_level_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/append_ai_text_chunk_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/apply_chapter_progression_ledger_entry_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/attach_ai_result_reference_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/authorize_database_migration_operator_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/begin_story_session_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/big_fish_runtime_run_type.rs # server-rs/crates/spacetime-client/src/module_bindings/bind_asset_object_to_entity_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/cancel_ai_task_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/clear_platform_browse_history_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/compile_big_fish_draft_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/compile_custom_world_published_profile_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/compile_puzzle_agent_draft_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/complete_ai_stage_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/complete_ai_task_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/confirm_asset_object_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/consume_profile_wallet_points_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/continue_story_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/create_ai_task_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/create_battle_state_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/create_big_fish_session_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/create_custom_world_agent_session_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/create_profile_recharge_order_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/create_puzzle_agent_session_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/delete_big_fish_work_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/delete_custom_world_agent_session_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/delete_custom_world_profile_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/delete_puzzle_work_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/delete_runtime_snapshot_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/drag_puzzle_piece_or_group_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/execute_custom_world_agent_action_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/export_auth_store_snapshot_from_tables_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/export_database_migration_to_file_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/fail_ai_task_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/finalize_big_fish_agent_message_turn_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/finalize_custom_world_agent_message_turn_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/finalize_puzzle_agent_message_turn_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/generate_big_fish_asset_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_auth_store_snapshot_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_battle_state_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_big_fish_session_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_chapter_progression_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_custom_world_agent_card_detail_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_custom_world_agent_operation_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_custom_world_agent_session_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_custom_world_gallery_detail_by_code_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_custom_world_gallery_detail_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_custom_world_library_detail_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_player_progression_or_default_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_profile_dashboard_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_profile_play_stats_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_profile_recharge_center_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_profile_referral_invite_center_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_puzzle_agent_session_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_puzzle_gallery_detail_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_puzzle_run_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_puzzle_work_detail_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_runtime_inventory_state_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_runtime_setting_or_default_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_runtime_snapshot_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/get_story_session_state_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/grant_player_progression_experience_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/import_auth_store_snapshot_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/import_database_migration_from_file_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/import_database_migration_incremental_from_file_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_asset_history_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_big_fish_works_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_custom_world_gallery_entries_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_custom_world_profiles_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_custom_world_works_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_platform_browse_history_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_profile_save_archives_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_profile_wallet_ledger_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_puzzle_gallery_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/list_puzzle_works_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/mod.rs # server-rs/crates/spacetime-client/src/module_bindings/publish_big_fish_game_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/publish_custom_world_profile_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/publish_custom_world_world_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/publish_puzzle_work_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/record_big_fish_play_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/redeem_profile_referral_invite_code_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/redeem_profile_reward_code_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/refund_profile_wallet_points_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/resolve_combat_action_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/resolve_npc_battle_interaction_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/resolve_npc_interaction_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/resolve_npc_social_action_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/resolve_treasure_interaction_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/resume_profile_save_archive_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/revoke_database_migration_operator_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/save_puzzle_generated_images_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/select_puzzle_cover_image_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/start_puzzle_run_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/submit_big_fish_message_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/submit_custom_world_agent_message_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/submit_puzzle_agent_message_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/submit_puzzle_leaderboard_entry_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/swap_puzzle_pieces_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/unpublish_custom_world_profile_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/update_puzzle_work_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/upsert_auth_store_snapshot_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/upsert_chapter_progression_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/upsert_custom_world_agent_operation_progress_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/upsert_custom_world_profile_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/upsert_npc_state_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/upsert_platform_browse_history_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/upsert_runtime_setting_and_return_procedure.rs # server-rs/crates/spacetime-client/src/module_bindings/upsert_runtime_snapshot_and_return_procedure.rs # server-rs/crates/spacetime-module/src/auth/procedures.rs # server-rs/crates/spacetime-module/src/custom_world/mod.rs # server-rs/crates/spacetime-module/src/lib.rs # server-rs/crates/spacetime-module/src/migration.rs # server-rs/crates/spacetime-module/src/puzzle.rs # server-rs/crates/spacetime-module/src/runtime/profile.rs # src/components/platform-entry/PlatformEntryFlowShellImpl.tsx # src/components/rpg-entry/RpgEntryFlowShell.agent.interaction.test.tsx # src/services/aiService.ts # src/services/puzzle-runtime/puzzleRuntimeClient.ts
This commit is contained in:
@@ -11,7 +11,7 @@ use axum::{
|
||||
header::{AUTHORIZATION, CONTENT_TYPE},
|
||||
},
|
||||
middleware::Next,
|
||||
response::{Html, Response},
|
||||
response::Response,
|
||||
};
|
||||
use reqwest::Client;
|
||||
use serde::Deserialize;
|
||||
@@ -39,43 +39,9 @@ const BLOCKED_DEBUG_HEADERS: &[&str] = &[
|
||||
"transfer-encoding",
|
||||
"expect",
|
||||
];
|
||||
// 数据库概览首版只统计受控白名单表,禁止后台页面直接输入任意 SQL。
|
||||
const DATABASE_OVERVIEW_TABLES: &[&str] = &[
|
||||
"runtime_setting",
|
||||
"runtime_snapshot",
|
||||
"user_browse_history",
|
||||
"profile_dashboard_state",
|
||||
"profile_wallet_ledger",
|
||||
"profile_played_world",
|
||||
"profile_save_archive",
|
||||
"story_session",
|
||||
"story_event",
|
||||
"battle_state",
|
||||
"inventory_slot",
|
||||
"quest_record",
|
||||
"quest_log",
|
||||
"treasure_record",
|
||||
"npc_state",
|
||||
"custom_world_profile",
|
||||
"custom_world_gallery_entry",
|
||||
"custom_world_agent_session",
|
||||
"custom_world_agent_message",
|
||||
"custom_world_agent_operation",
|
||||
"custom_world_draft_card",
|
||||
"big_fish_creation_session",
|
||||
"big_fish_agent_message",
|
||||
"big_fish_asset_slot",
|
||||
"puzzle_work_profile",
|
||||
"puzzle_agent_session",
|
||||
"puzzle_agent_message",
|
||||
"puzzle_runtime_run",
|
||||
"ai_task",
|
||||
"ai_task_stage",
|
||||
"ai_text_chunk",
|
||||
"ai_result_reference",
|
||||
"asset_object",
|
||||
"asset_entity_binding",
|
||||
];
|
||||
// SpacetimeDB 2.x 的 schema HTTP API 要求显式传入 BSATN JSON 版本。
|
||||
// 后台总览只读取表名,固定使用当前 CLI 2.1.0 兼容的版本参数即可。
|
||||
const SPACETIME_SCHEMA_VERSION_QUERY: &str = "version=9";
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AuthenticatedAdmin {
|
||||
@@ -100,17 +66,6 @@ struct SpacetimeSchemaTable {
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct SpacetimeSqlRow {
|
||||
#[serde(flatten)]
|
||||
columns: serde_json::Map<String, Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct SpacetimeSqlResponse {
|
||||
rows: Option<Vec<SpacetimeSqlRow>>,
|
||||
}
|
||||
|
||||
impl AuthenticatedAdmin {
|
||||
pub fn new(session: AdminSessionPayload) -> Self {
|
||||
Self { session }
|
||||
@@ -121,10 +76,6 @@ impl AuthenticatedAdmin {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn admin_console_page() -> Html<&'static str> {
|
||||
Html(ADMIN_CONSOLE_HTML)
|
||||
}
|
||||
|
||||
pub async fn admin_login(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
@@ -287,7 +238,7 @@ async fn fetch_database_overview(state: &AppState) -> AdminDatabaseOverviewPaylo
|
||||
|
||||
let schema = fetch_spacetime_json::<SpacetimeSchemaResponse>(
|
||||
&client,
|
||||
&format!("{server_root}/v1/database/{database}/schema"),
|
||||
&build_spacetime_schema_url(server_root, database),
|
||||
token,
|
||||
)
|
||||
.await
|
||||
@@ -295,7 +246,7 @@ async fn fetch_database_overview(state: &AppState) -> AdminDatabaseOverviewPaylo
|
||||
.ok()
|
||||
.flatten();
|
||||
|
||||
let mut schema_table_names = schema
|
||||
let schema_table_names = schema
|
||||
.as_ref()
|
||||
.and_then(|value| value.tables.as_ref())
|
||||
.map(|tables| {
|
||||
@@ -312,31 +263,33 @@ async fn fetch_database_overview(state: &AppState) -> AdminDatabaseOverviewPaylo
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut table_stats = Vec::new();
|
||||
for table_name in DATABASE_OVERVIEW_TABLES {
|
||||
for table_name in &schema_table_names {
|
||||
if !is_safe_spacetime_table_name(table_name) {
|
||||
table_stats.push(AdminDatabaseTableStatPayload {
|
||||
table_name: table_name.clone(),
|
||||
row_count: None,
|
||||
error_message: Some("表名不适合 SQL 统计".to_string()),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
let sql = format!("SELECT COUNT(*) AS row_count FROM {table_name}");
|
||||
match fetch_spacetime_sql_count(&client, server_root, database, token, &sql).await {
|
||||
Ok(row_count) => table_stats.push(AdminDatabaseTableStatPayload {
|
||||
table_name: (*table_name).to_string(),
|
||||
table_name: table_name.clone(),
|
||||
row_count: Some(row_count),
|
||||
error_message: None,
|
||||
}),
|
||||
Err(error) => {
|
||||
table_stats.push(AdminDatabaseTableStatPayload {
|
||||
table_name: (*table_name).to_string(),
|
||||
table_name: table_name.clone(),
|
||||
row_count: None,
|
||||
error_message: Some(error),
|
||||
error_message: Some(normalize_table_count_error(&error)),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for table_name in DATABASE_OVERVIEW_TABLES {
|
||||
if !schema_table_names.iter().any(|name| name == table_name) {
|
||||
schema_table_names.push((*table_name).to_string());
|
||||
}
|
||||
}
|
||||
schema_table_names.sort();
|
||||
|
||||
AdminDatabaseOverviewPayload {
|
||||
database_identity: database_info
|
||||
.as_ref()
|
||||
@@ -353,6 +306,31 @@ async fn fetch_database_overview(state: &AppState) -> AdminDatabaseOverviewPaylo
|
||||
}
|
||||
}
|
||||
|
||||
fn build_spacetime_schema_url(server_root: &str, database: &str) -> String {
|
||||
format!("{server_root}/v1/database/{database}/schema?{SPACETIME_SCHEMA_VERSION_QUERY}")
|
||||
}
|
||||
|
||||
// 表名来自 schema,但进入 SQL 前仍做最小标识符校验,避免未来 schema 来源变化时扩大风险面。
|
||||
fn is_safe_spacetime_table_name(table_name: &str) -> bool {
|
||||
let mut chars = table_name.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
if !(first == '_' || first.is_ascii_alphabetic()) {
|
||||
return false;
|
||||
}
|
||||
chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
|
||||
}
|
||||
|
||||
// private 表在 SpacetimeDB SQL 下会表现为不可见,后台只展示可理解状态,不暴露整段 HTTP 噪音。
|
||||
fn normalize_table_count_error(error: &str) -> String {
|
||||
let normalized = error.to_ascii_lowercase();
|
||||
if normalized.contains("marked private") || normalized.contains("no such table") {
|
||||
return "不可统计(private 或当前身份不可见)".to_string();
|
||||
}
|
||||
error.to_string()
|
||||
}
|
||||
|
||||
async fn fetch_spacetime_json<T>(
|
||||
client: &Client,
|
||||
url: &str,
|
||||
@@ -409,17 +387,63 @@ async fn fetch_spacetime_sql_count(
|
||||
}
|
||||
|
||||
let payload = response
|
||||
.json::<SpacetimeSqlResponse>()
|
||||
.json::<Value>()
|
||||
.await
|
||||
.map_err(|error| format!("SQL 响应解析失败:{error}"))?;
|
||||
let row = payload
|
||||
.rows
|
||||
.and_then(|rows| rows.into_iter().next())
|
||||
.ok_or_else(|| "SQL 结果为空".to_string())?;
|
||||
extract_sql_count(row.columns)
|
||||
parse_spacetime_sql_count_response(payload)
|
||||
}
|
||||
|
||||
fn extract_sql_count(columns: serde_json::Map<String, Value>) -> Result<u64, String> {
|
||||
fn parse_spacetime_sql_count_response(payload: Value) -> Result<u64, String> {
|
||||
match payload {
|
||||
// SpacetimeDB 2.x /sql 返回 statement result 数组,每个 result 内含 schema 与 rows。
|
||||
Value::Array(statements) => {
|
||||
let statement = statements
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| "SQL 结果为空".to_string())?;
|
||||
extract_sql_count_from_statement(statement)
|
||||
}
|
||||
// 保留兼容旧对象形状,便于本地/远端 API 小版本差异时仍能读取计数。
|
||||
Value::Object(statement) => extract_sql_count_from_statement(Value::Object(statement)),
|
||||
_ => Err("SQL 响应格式非法".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_sql_count_from_statement(statement: Value) -> Result<u64, String> {
|
||||
let Value::Object(mut statement) = statement else {
|
||||
return Err("SQL statement 结果格式非法".to_string());
|
||||
};
|
||||
|
||||
let schema = statement.remove("schema");
|
||||
let rows = statement
|
||||
.remove("rows")
|
||||
.ok_or_else(|| "SQL 响应缺少 rows 字段".to_string())?;
|
||||
extract_sql_count_from_rows(rows, schema.as_ref())
|
||||
}
|
||||
|
||||
fn extract_sql_count_from_rows(rows: Value, schema: Option<&Value>) -> Result<u64, String> {
|
||||
let Value::Array(rows) = rows else {
|
||||
return Err("SQL rows 字段格式非法".to_string());
|
||||
};
|
||||
let row = rows.first().ok_or_else(|| "SQL 结果为空".to_string())?;
|
||||
extract_sql_count_from_row(row, schema)
|
||||
}
|
||||
|
||||
fn extract_sql_count_from_row(row: &Value, schema: Option<&Value>) -> Result<u64, String> {
|
||||
match row {
|
||||
Value::Object(columns) => extract_sql_count(columns),
|
||||
Value::Array(values) => {
|
||||
let count_index = schema.and_then(find_sql_count_column_index).unwrap_or(0);
|
||||
values
|
||||
.get(count_index)
|
||||
.ok_or_else(|| "SQL 结果缺少 count 字段".to_string())
|
||||
.and_then(parse_count_value)
|
||||
}
|
||||
value => parse_count_value(value),
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_sql_count(columns: &serde_json::Map<String, Value>) -> Result<u64, String> {
|
||||
for key in ["row_count", "count", "COUNT(*)"] {
|
||||
if let Some(value) = columns.get(key) {
|
||||
return parse_count_value(value);
|
||||
@@ -432,6 +456,25 @@ fn extract_sql_count(columns: serde_json::Map<String, Value>) -> Result<u64, Str
|
||||
.and_then(parse_count_value)
|
||||
}
|
||||
|
||||
fn find_sql_count_column_index(schema: &Value) -> Option<usize> {
|
||||
let elements = schema.get("elements")?.as_array()?;
|
||||
elements.iter().position(|element| {
|
||||
element
|
||||
.get("name")
|
||||
.and_then(extract_sql_schema_name)
|
||||
.map(|name| matches!(name, "row_count" | "count" | "COUNT(*)"))
|
||||
.unwrap_or(false)
|
||||
})
|
||||
}
|
||||
|
||||
fn extract_sql_schema_name(value: &Value) -> Option<&str> {
|
||||
match value {
|
||||
Value::String(text) => Some(text.as_str()),
|
||||
Value::Object(object) => object.get("some").and_then(Value::as_str),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_count_value(value: &Value) -> Result<u64, String> {
|
||||
match value {
|
||||
Value::Number(number) => number
|
||||
@@ -602,520 +645,15 @@ fn build_admin_session_payload(session: crate::state::AdminSession) -> AdminSess
|
||||
}
|
||||
}
|
||||
|
||||
// 首版后台页面内嵌在 api-server,避免新增独立前端工程与静态资源发布链。
|
||||
static ADMIN_CONSOLE_HTML: &str = r#"<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Genarrative 管理后台</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: linear-gradient(180deg, #f4efe5 0%, #e7ddd0 100%);
|
||||
--panel: rgba(255, 249, 240, 0.92);
|
||||
--panel-strong: #fffaf2;
|
||||
--line: rgba(90, 61, 41, 0.14);
|
||||
--text: #2f241d;
|
||||
--muted: #7b6657;
|
||||
--accent: #b45a2f;
|
||||
--accent-strong: #8f431f;
|
||||
--ok: #2c7a54;
|
||||
--danger: #a63f2f;
|
||||
--shadow: 0 20px 50px rgba(70, 41, 19, 0.12);
|
||||
--radius: 20px;
|
||||
--font: "Microsoft YaHei", "PingFang SC", "Segoe UI", sans-serif;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: var(--font);
|
||||
color: var(--text);
|
||||
background: var(--bg);
|
||||
min-height: 100vh;
|
||||
}
|
||||
.shell {
|
||||
max-width: 1180px;
|
||||
margin: 0 auto;
|
||||
padding: 20px 16px 40px;
|
||||
}
|
||||
.hero {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
gap: 16px;
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
.hero h1 {
|
||||
margin: 0;
|
||||
font-size: 28px;
|
||||
line-height: 1.1;
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
.hero p {
|
||||
margin: 10px 0 0;
|
||||
color: var(--muted);
|
||||
font-size: 14px;
|
||||
}
|
||||
.status-chip {
|
||||
padding: 10px 14px;
|
||||
border-radius: 999px;
|
||||
background: rgba(180, 90, 47, 0.1);
|
||||
color: var(--accent-strong);
|
||||
font-size: 13px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: 340px minmax(0, 1fr);
|
||||
gap: 16px;
|
||||
}
|
||||
.panel {
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius);
|
||||
box-shadow: var(--shadow);
|
||||
backdrop-filter: blur(12px);
|
||||
}
|
||||
.panel-head {
|
||||
padding: 18px 18px 0;
|
||||
}
|
||||
.panel-head h2 {
|
||||
margin: 0;
|
||||
font-size: 18px;
|
||||
}
|
||||
.panel-head p {
|
||||
margin: 8px 0 0;
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
}
|
||||
.panel-body {
|
||||
padding: 18px;
|
||||
}
|
||||
.form {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
}
|
||||
label {
|
||||
display: grid;
|
||||
gap: 6px;
|
||||
font-size: 13px;
|
||||
color: var(--muted);
|
||||
}
|
||||
input, textarea, select {
|
||||
width: 100%;
|
||||
border: 1px solid rgba(78, 53, 37, 0.12);
|
||||
border-radius: 14px;
|
||||
background: var(--panel-strong);
|
||||
color: var(--text);
|
||||
font: inherit;
|
||||
padding: 12px 14px;
|
||||
outline: none;
|
||||
}
|
||||
textarea {
|
||||
min-height: 140px;
|
||||
resize: vertical;
|
||||
}
|
||||
.btn-row {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
button {
|
||||
border: none;
|
||||
border-radius: 14px;
|
||||
padding: 11px 16px;
|
||||
background: var(--accent);
|
||||
color: #fff8f2;
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
}
|
||||
button.secondary {
|
||||
background: rgba(180, 90, 47, 0.14);
|
||||
color: var(--accent-strong);
|
||||
}
|
||||
button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: wait;
|
||||
}
|
||||
.stack {
|
||||
display: grid;
|
||||
gap: 16px;
|
||||
}
|
||||
.metrics {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
.metric {
|
||||
padding: 14px;
|
||||
border-radius: 16px;
|
||||
border: 1px solid var(--line);
|
||||
background: rgba(255,255,255,0.45);
|
||||
}
|
||||
.metric .k {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.metric .v {
|
||||
margin-top: 8px;
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
}
|
||||
.data-grid {
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
}
|
||||
.row {
|
||||
display: grid;
|
||||
grid-template-columns: 160px 1fr;
|
||||
gap: 10px;
|
||||
font-size: 13px;
|
||||
align-items: start;
|
||||
padding: 10px 0;
|
||||
border-bottom: 1px solid rgba(78, 53, 37, 0.08);
|
||||
}
|
||||
.row:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
.row .k {
|
||||
color: var(--muted);
|
||||
}
|
||||
.table-list {
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
max-height: 420px;
|
||||
overflow: auto;
|
||||
padding-right: 4px;
|
||||
}
|
||||
.table-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
padding: 12px 14px;
|
||||
border-radius: 14px;
|
||||
background: rgba(255,255,255,0.52);
|
||||
border: 1px solid rgba(78, 53, 37, 0.08);
|
||||
align-items: center;
|
||||
}
|
||||
.table-item small {
|
||||
color: var(--muted);
|
||||
display: block;
|
||||
margin-top: 4px;
|
||||
}
|
||||
.count {
|
||||
font-weight: 700;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.count.err {
|
||||
color: var(--danger);
|
||||
font-weight: 600;
|
||||
}
|
||||
.result-panel {
|
||||
border-radius: 16px;
|
||||
border: 1px solid var(--line);
|
||||
background: rgba(255,255,255,0.55);
|
||||
padding: 14px;
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
}
|
||||
pre {
|
||||
margin: 0;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
background: rgba(47, 36, 29, 0.06);
|
||||
border-radius: 14px;
|
||||
padding: 12px;
|
||||
overflow: auto;
|
||||
}
|
||||
.hint {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.err-text { color: var(--danger); }
|
||||
.ok-text { color: var(--ok); }
|
||||
@media (max-width: 900px) {
|
||||
.grid { grid-template-columns: 1fr; }
|
||||
.hero { flex-direction: column; }
|
||||
.row { grid-template-columns: 1fr; gap: 4px; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="shell">
|
||||
<div class="hero">
|
||||
<div>
|
||||
<h1>Genarrative 管理后台</h1>
|
||||
<p>查看服务状态、数据库概览,并对当前 API 做受控调试。</p>
|
||||
</div>
|
||||
<div id="admin-status" class="status-chip">未登录</div>
|
||||
</div>
|
||||
|
||||
<div class="grid">
|
||||
<div class="stack">
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>管理员登录</h2>
|
||||
<p>使用配置的后台账号进入管理域。</p>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<form id="login-form" class="form">
|
||||
<label>用户名
|
||||
<input id="login-username" name="username" autocomplete="username" />
|
||||
</label>
|
||||
<label>密码
|
||||
<input id="login-password" name="password" type="password" autocomplete="current-password" />
|
||||
</label>
|
||||
<div class="btn-row">
|
||||
<button id="login-submit" type="submit">登录后台</button>
|
||||
<button id="login-clear" class="secondary" type="button">清空令牌</button>
|
||||
</div>
|
||||
<div id="login-message" class="hint"></div>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>API 调试</h2>
|
||||
<p>对当前服务做同源受控请求。</p>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<form id="debug-form" class="form">
|
||||
<label>方法
|
||||
<select id="debug-method">
|
||||
<option>GET</option>
|
||||
<option>POST</option>
|
||||
<option>PUT</option>
|
||||
<option>DELETE</option>
|
||||
<option>PATCH</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>路径
|
||||
<input id="debug-path" value="/healthz" />
|
||||
</label>
|
||||
<label>附加请求头(JSON 数组)
|
||||
<textarea id="debug-headers">[]</textarea>
|
||||
</label>
|
||||
<label>请求体
|
||||
<textarea id="debug-body"></textarea>
|
||||
</label>
|
||||
<div class="btn-row">
|
||||
<button id="debug-submit" type="submit">发送调试请求</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div class="stack">
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>数据库概览</h2>
|
||||
<p>读取当前服务配置和 SpacetimeDB 数据库真相。</p>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<div class="btn-row" style="margin-bottom:14px;">
|
||||
<button id="refresh-overview" type="button">刷新概览</button>
|
||||
</div>
|
||||
<div id="overview-metrics" class="metrics"></div>
|
||||
<div id="overview-detail" class="data-grid" style="margin-top:14px;"></div>
|
||||
<div id="overview-errors" class="hint err-text" style="margin-top:10px;"></div>
|
||||
<div id="overview-tables" class="table-list" style="margin-top:14px;"></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>调试结果</h2>
|
||||
<p>返回状态、响应头和内容预览。</p>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<div id="debug-result" class="result-panel">
|
||||
<div class="hint">尚未执行调试请求。</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const TOKEN_KEY = 'genarrative_admin_token';
|
||||
const statusEl = document.getElementById('admin-status');
|
||||
const loginMessageEl = document.getElementById('login-message');
|
||||
const overviewMetricsEl = document.getElementById('overview-metrics');
|
||||
const overviewDetailEl = document.getElementById('overview-detail');
|
||||
const overviewTablesEl = document.getElementById('overview-tables');
|
||||
const overviewErrorsEl = document.getElementById('overview-errors');
|
||||
const debugResultEl = document.getElementById('debug-result');
|
||||
|
||||
function getToken() {
|
||||
return window.localStorage.getItem(TOKEN_KEY) || '';
|
||||
}
|
||||
|
||||
function setToken(token) {
|
||||
if (!token) {
|
||||
window.localStorage.removeItem(TOKEN_KEY);
|
||||
return;
|
||||
}
|
||||
window.localStorage.setItem(TOKEN_KEY, token);
|
||||
}
|
||||
|
||||
function setStatus(text, ok) {
|
||||
statusEl.textContent = text;
|
||||
statusEl.style.background = ok ? 'rgba(44,122,84,0.12)' : 'rgba(180,90,47,0.1)';
|
||||
statusEl.style.color = ok ? '#2c7a54' : '#8f431f';
|
||||
}
|
||||
|
||||
async function request(path, options = {}) {
|
||||
const headers = new Headers(options.headers || {});
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
headers.set('authorization', `Bearer ${token}`);
|
||||
}
|
||||
if (options.json !== undefined) {
|
||||
headers.set('content-type', 'application/json');
|
||||
options.body = JSON.stringify(options.json);
|
||||
}
|
||||
const response = await fetch(path, { ...options, headers });
|
||||
const text = await response.text();
|
||||
let data = null;
|
||||
try { data = text ? JSON.parse(text) : null; } catch (_) {}
|
||||
if (!response.ok) {
|
||||
const message = data?.error?.message || data?.message || text || `HTTP ${response.status}`;
|
||||
throw new Error(message);
|
||||
}
|
||||
return data?.data ?? data;
|
||||
}
|
||||
|
||||
function renderOverview(overview) {
|
||||
const service = overview.service || {};
|
||||
const database = overview.database || {};
|
||||
const stats = Array.isArray(database.tableStats) ? database.tableStats : [];
|
||||
overviewMetricsEl.innerHTML = `
|
||||
<div class="metric"><div class="k">后台状态</div><div class="v">${service.adminEnabled ? '已启用' : '未启用'}</div></div>
|
||||
<div class="metric"><div class="k">服务监听</div><div class="v">${service.bindHost || '-'}:${service.bindPort || '-'}</div></div>
|
||||
<div class="metric"><div class="k">SpacetimeDB</div><div class="v">${service.spacetimeDatabase || '-'}</div></div>
|
||||
<div class="metric"><div class="k">统计表数</div><div class="v">${stats.length}</div></div>
|
||||
`;
|
||||
overviewDetailEl.innerHTML = `
|
||||
<div class="row"><div class="k">JWT Issuer</div><div>${service.jwtIssuer || '-'}</div></div>
|
||||
<div class="row"><div class="k">Spacetime 服务</div><div>${service.spacetimeServerUrl || '-'}</div></div>
|
||||
<div class="row"><div class="k">数据库 Identity</div><div>${database.databaseIdentity || '-'}</div></div>
|
||||
<div class="row"><div class="k">Owner Identity</div><div>${database.ownerIdentity || '-'}</div></div>
|
||||
<div class="row"><div class="k">Host Type</div><div>${database.hostType || '-'}</div></div>
|
||||
<div class="row"><div class="k">Schema 表数量</div><div>${(database.schemaTableNames || []).length}</div></div>
|
||||
`;
|
||||
overviewTablesEl.innerHTML = stats.map((item) => `
|
||||
<div class="table-item">
|
||||
<div>
|
||||
<strong>${item.tableName}</strong>
|
||||
${item.errorMessage ? `<small class="err-text">${item.errorMessage}</small>` : ''}
|
||||
</div>
|
||||
<div class="count ${item.errorMessage ? 'err' : ''}">${item.rowCount ?? '失败'}</div>
|
||||
</div>
|
||||
`).join('');
|
||||
overviewErrorsEl.textContent = (database.fetchErrors || []).join(' | ');
|
||||
}
|
||||
|
||||
function renderDebugResult(result) {
|
||||
const headerText = (result.headers || []).map((item) => `${item.name}: ${item.value}`).join('\n');
|
||||
debugResultEl.innerHTML = `
|
||||
<div><strong>状态:</strong><span class="${result.status < 400 ? 'ok-text' : 'err-text'}">${result.status} ${result.statusText}</span></div>
|
||||
<div><strong>响应头</strong><pre>${headerText || '(无)'}</pre></div>
|
||||
<div><strong>响应体预览</strong><pre>${result.bodyText || '(空)'}</pre></div>
|
||||
<div><strong>响应 JSON</strong><pre>${result.bodyJson ? JSON.stringify(result.bodyJson, null, 2) : '(不是 JSON)'}</pre></div>
|
||||
`;
|
||||
}
|
||||
|
||||
async function loadMe() {
|
||||
const token = getToken();
|
||||
if (!token) {
|
||||
setStatus('未登录', false);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = await request('/admin/api/me');
|
||||
setStatus(`管理员:${result.admin.displayName}`, true);
|
||||
} catch (error) {
|
||||
setToken('');
|
||||
setStatus('未登录', false);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadOverview() {
|
||||
try {
|
||||
const overview = await request('/admin/api/overview');
|
||||
renderOverview(overview);
|
||||
} catch (error) {
|
||||
overviewMetricsEl.innerHTML = '';
|
||||
overviewDetailEl.innerHTML = '';
|
||||
overviewTablesEl.innerHTML = '';
|
||||
overviewErrorsEl.textContent = error.message;
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('login-form').addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
loginMessageEl.textContent = '正在登录...';
|
||||
try {
|
||||
const result = await request('/admin/api/login', {
|
||||
method: 'POST',
|
||||
json: {
|
||||
username: document.getElementById('login-username').value,
|
||||
password: document.getElementById('login-password').value,
|
||||
},
|
||||
});
|
||||
setToken(result.token);
|
||||
loginMessageEl.textContent = '登录成功';
|
||||
await loadMe();
|
||||
await loadOverview();
|
||||
} catch (error) {
|
||||
loginMessageEl.textContent = error.message;
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('login-clear').addEventListener('click', () => {
|
||||
setToken('');
|
||||
setStatus('未登录', false);
|
||||
loginMessageEl.textContent = '已清空本地令牌';
|
||||
});
|
||||
|
||||
document.getElementById('refresh-overview').addEventListener('click', async () => {
|
||||
await loadOverview();
|
||||
});
|
||||
|
||||
document.getElementById('debug-form').addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
debugResultEl.innerHTML = '<div class="hint">正在请求...</div>';
|
||||
try {
|
||||
const headers = JSON.parse(document.getElementById('debug-headers').value || '[]');
|
||||
const result = await request('/admin/api/debug/http', {
|
||||
method: 'POST',
|
||||
json: {
|
||||
method: document.getElementById('debug-method').value,
|
||||
path: document.getElementById('debug-path').value,
|
||||
headers,
|
||||
body: document.getElementById('debug-body').value,
|
||||
},
|
||||
});
|
||||
renderDebugResult(result);
|
||||
} catch (error) {
|
||||
debugResultEl.innerHTML = `<div class="err-text">${error.message}</div>`;
|
||||
}
|
||||
});
|
||||
|
||||
loadMe().then(loadOverview);
|
||||
</script>
|
||||
</body>
|
||||
</html>"#;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{build_body_preview, build_debug_base_url, normalize_debug_path, trim_preview};
|
||||
use super::{
|
||||
build_body_preview, build_debug_base_url, build_spacetime_schema_url,
|
||||
is_safe_spacetime_table_name, normalize_debug_path, normalize_table_count_error,
|
||||
parse_spacetime_sql_count_response, trim_preview,
|
||||
};
|
||||
use axum::{http::StatusCode, response::IntoResponse};
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn normalize_debug_path_rejects_absolute_url() {
|
||||
@@ -1161,6 +699,123 @@ mod tests {
|
||||
assert_eq!(trim_preview(&text).chars().count(), 4000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_spacetime_schema_url_includes_required_version_query() {
|
||||
let url = build_spacetime_schema_url("http://127.0.0.1:3101", "xushi-p4wfr");
|
||||
|
||||
assert_eq!(
|
||||
url,
|
||||
"http://127.0.0.1:3101/v1/database/xushi-p4wfr/schema?version=9"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_safe_spacetime_table_name_accepts_schema_identifiers() {
|
||||
assert!(is_safe_spacetime_table_name("runtime_setting"));
|
||||
assert!(is_safe_spacetime_table_name("_private_table"));
|
||||
assert!(is_safe_spacetime_table_name("AiTaskStage2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_safe_spacetime_table_name_rejects_sql_fragments() {
|
||||
assert!(!is_safe_spacetime_table_name(""));
|
||||
assert!(!is_safe_spacetime_table_name("bad-name"));
|
||||
assert!(!is_safe_spacetime_table_name("1bad"));
|
||||
assert!(!is_safe_spacetime_table_name("runtime_setting;DROP"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_table_count_error_hides_private_table_http_noise() {
|
||||
let error = "HTTP 400:no such table: `runtime_setting`. If the table exists, it may be marked private.";
|
||||
|
||||
assert_eq!(
|
||||
normalize_table_count_error(error),
|
||||
"不可统计(private 或当前身份不可见)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_table_count_error_keeps_other_errors() {
|
||||
let error = "SQL 请求失败:connection refused";
|
||||
|
||||
assert_eq!(normalize_table_count_error(error), error);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_spacetime_sql_count_response_accepts_statement_array_rows() {
|
||||
let payload = json!([
|
||||
{
|
||||
"schema": {
|
||||
"elements": [
|
||||
{
|
||||
"name": {
|
||||
"some": "row_count"
|
||||
},
|
||||
"algebraic_type": {
|
||||
"U64": []
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"rows": [[7]],
|
||||
"total_duration_micros": 116,
|
||||
"stats": {
|
||||
"rows_inserted": 0,
|
||||
"rows_deleted": 0,
|
||||
"rows_updated": 0
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
let count =
|
||||
parse_spacetime_sql_count_response(payload).expect("statement array should parse");
|
||||
|
||||
assert_eq!(count, 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_spacetime_sql_count_response_uses_schema_column_index() {
|
||||
let payload = json!([
|
||||
{
|
||||
"schema": {
|
||||
"elements": [
|
||||
{
|
||||
"name": {
|
||||
"some": "table_name"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": {
|
||||
"some": "row_count"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"rows": [["runtime_setting", "12"]]
|
||||
}
|
||||
]);
|
||||
|
||||
let count =
|
||||
parse_spacetime_sql_count_response(payload).expect("schema column index should parse");
|
||||
|
||||
assert_eq!(count, 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_spacetime_sql_count_response_keeps_object_row_compatibility() {
|
||||
let payload = json!({
|
||||
"rows": [
|
||||
{
|
||||
"row_count": "3"
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
let count = parse_spacetime_sql_count_response(payload).expect("object row should parse");
|
||||
|
||||
assert_eq!(count, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_body_preview_handles_utf8() {
|
||||
let preview = build_body_preview("后台测试".as_bytes());
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use axum::{
|
||||
Router,
|
||||
body::Body,
|
||||
extract::Extension,
|
||||
extract::{DefaultBodyLimit, Extension},
|
||||
http::Request,
|
||||
middleware,
|
||||
routing::{delete, get, post},
|
||||
@@ -13,10 +13,7 @@ use tower_http::{
|
||||
use tracing::{Level, Span, error, info, info_span, warn};
|
||||
|
||||
use crate::{
|
||||
admin::{
|
||||
admin_console_page, admin_debug_http, admin_login, admin_me, admin_overview,
|
||||
require_admin_auth,
|
||||
},
|
||||
admin::{admin_debug_http, admin_login, admin_me, admin_overview, require_admin_auth},
|
||||
ai_tasks::{
|
||||
append_ai_text_chunk, attach_ai_result_reference, cancel_ai_task, complete_ai_stage,
|
||||
complete_ai_task, create_ai_task, fail_ai_task, start_ai_task, start_ai_task_stage,
|
||||
@@ -34,7 +31,8 @@ use crate::{
|
||||
auth_sessions::auth_sessions,
|
||||
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, record_big_fish_play,
|
||||
get_big_fish_session, get_big_fish_works, list_big_fish_gallery,
|
||||
record_big_fish_gallery_like, record_big_fish_play, remix_big_fish_gallery_work,
|
||||
start_big_fish_run, stream_big_fish_message, submit_big_fish_input,
|
||||
submit_big_fish_message,
|
||||
},
|
||||
@@ -57,8 +55,9 @@ use crate::{
|
||||
get_custom_world_gallery_detail_by_code, get_custom_world_library,
|
||||
get_custom_world_library_detail, get_custom_world_works, list_custom_world_gallery,
|
||||
publish_custom_world_library_profile, put_custom_world_library_profile,
|
||||
stream_custom_world_agent_message, submit_custom_world_agent_message,
|
||||
unpublish_custom_world_library_profile,
|
||||
record_custom_world_gallery_like, record_custom_world_gallery_play,
|
||||
remix_custom_world_gallery_profile, stream_custom_world_agent_message,
|
||||
submit_custom_world_agent_message, unpublish_custom_world_library_profile,
|
||||
},
|
||||
custom_world_ai::{
|
||||
generate_custom_world_cover_image, generate_custom_world_entity,
|
||||
@@ -71,15 +70,26 @@ use crate::{
|
||||
login_options::auth_login_options,
|
||||
logout::logout,
|
||||
logout_all::logout_all,
|
||||
match3d::{
|
||||
click_match3d_item, compile_match3d_agent_draft, create_match3d_agent_session,
|
||||
delete_match3d_work, execute_match3d_agent_action, finish_match3d_time_up,
|
||||
get_match3d_agent_session, get_match3d_run, get_match3d_work_detail, get_match3d_works,
|
||||
list_match3d_gallery, publish_match3d_work, put_match3d_work, restart_match3d_run,
|
||||
start_match3d_run, stop_match3d_run, stream_match3d_agent_message,
|
||||
submit_match3d_agent_message,
|
||||
},
|
||||
password_entry::password_entry,
|
||||
password_management::{change_password, reset_password},
|
||||
phone_auth::{phone_login, send_phone_code},
|
||||
profile_identity::update_profile_identity,
|
||||
puzzle::{
|
||||
advance_puzzle_next_level, create_puzzle_agent_session, delete_puzzle_work,
|
||||
drag_puzzle_piece_or_group, execute_puzzle_agent_action, get_puzzle_agent_session,
|
||||
get_puzzle_gallery_detail, get_puzzle_run, get_puzzle_work_detail, get_puzzle_works,
|
||||
list_puzzle_gallery, put_puzzle_work, start_puzzle_run, stream_puzzle_agent_message,
|
||||
submit_puzzle_agent_message, submit_puzzle_leaderboard, swap_puzzle_pieces,
|
||||
advance_puzzle_next_level, claim_puzzle_work_point_incentive, create_puzzle_agent_session,
|
||||
delete_puzzle_work, drag_puzzle_piece_or_group, execute_puzzle_agent_action,
|
||||
get_puzzle_agent_session, get_puzzle_gallery_detail, get_puzzle_run,
|
||||
get_puzzle_work_detail, get_puzzle_works, list_puzzle_gallery, put_puzzle_work,
|
||||
record_puzzle_gallery_like, remix_puzzle_gallery_work, start_puzzle_run,
|
||||
stream_puzzle_agent_message, submit_puzzle_agent_message, submit_puzzle_leaderboard,
|
||||
swap_puzzle_pieces, update_puzzle_run_pause, use_puzzle_runtime_prop,
|
||||
},
|
||||
refresh_session::refresh_session,
|
||||
request_context::{attach_request_context, resolve_request_id},
|
||||
@@ -95,10 +105,10 @@ use crate::{
|
||||
},
|
||||
runtime_inventory::get_runtime_inventory_state,
|
||||
runtime_profile::{
|
||||
admin_disable_profile_redeem_code, admin_upsert_profile_redeem_code,
|
||||
create_profile_recharge_order, get_profile_dashboard, get_profile_play_stats,
|
||||
get_profile_recharge_center, get_profile_referral_invite_center, get_profile_wallet_ledger,
|
||||
redeem_profile_referral_invite_code, redeem_profile_reward_code,
|
||||
admin_disable_profile_redeem_code, admin_upsert_profile_invite_code,
|
||||
admin_upsert_profile_redeem_code, create_profile_recharge_order, get_profile_dashboard,
|
||||
get_profile_play_stats, get_profile_recharge_center, get_profile_referral_invite_center,
|
||||
get_profile_wallet_ledger, redeem_profile_referral_invite_code, redeem_profile_reward_code,
|
||||
},
|
||||
runtime_save::{
|
||||
delete_runtime_snapshot, get_runtime_snapshot, list_profile_save_archives,
|
||||
@@ -116,12 +126,13 @@ use crate::{
|
||||
wechat_auth::{bind_wechat_phone, handle_wechat_callback, start_wechat_login},
|
||||
};
|
||||
|
||||
const PUZZLE_REFERENCE_IMAGE_BODY_LIMIT_BYTES: usize = 12 * 1024 * 1024;
|
||||
|
||||
// 统一由这里构造 Axum 路由树,后续再逐项挂接中间件与业务路由。
|
||||
pub fn build_router(state: AppState) -> Router {
|
||||
let slow_request_threshold_ms = state.config.slow_request_threshold_ms;
|
||||
|
||||
Router::new()
|
||||
.route("/admin", get(admin_console_page))
|
||||
.route("/admin/api/login", post(admin_login))
|
||||
.route(
|
||||
"/admin/api/me",
|
||||
@@ -158,6 +169,13 @@ pub fn build_router(state: AppState) -> Router {
|
||||
require_admin_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/admin/api/profile/invite-codes",
|
||||
post(admin_upsert_profile_invite_code).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_admin_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/healthz",
|
||||
get(|Extension(request_context): Extension<_>| async move {
|
||||
@@ -206,6 +224,12 @@ pub fn build_router(state: AppState) -> Router {
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/profile/me",
|
||||
axum::routing::patch(update_profile_identity).route_layer(
|
||||
middleware::from_fn_with_state(state.clone(), require_bearer_auth),
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/api/auth/refresh",
|
||||
post(refresh_session).route_layer(middleware::from_fn_with_state(
|
||||
@@ -482,6 +506,27 @@ pub fn build_router(state: AppState) -> Router {
|
||||
"/api/runtime/custom-world-gallery/{owner_user_id}/{profile_id}",
|
||||
get(get_custom_world_gallery_detail),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/custom-world-gallery/{owner_user_id}/{profile_id}/remix",
|
||||
post(remix_custom_world_gallery_profile).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/custom-world-gallery/{owner_user_id}/{profile_id}/play",
|
||||
post(record_custom_world_gallery_play).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/custom-world-gallery/{owner_user_id}/{profile_id}/like",
|
||||
post(record_custom_world_gallery_like).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/custom-world-gallery/by-code/{code}",
|
||||
get(get_custom_world_gallery_detail_by_code),
|
||||
@@ -594,6 +639,20 @@ pub fn build_router(state: AppState) -> Router {
|
||||
)),
|
||||
)
|
||||
.route("/api/runtime/big-fish/gallery", get(list_big_fish_gallery))
|
||||
.route(
|
||||
"/api/runtime/big-fish/gallery/{session_id}/remix",
|
||||
post(remix_big_fish_gallery_work).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/big-fish/gallery/{session_id}/like",
|
||||
post(record_big_fish_gallery_like).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/big-fish/works/{session_id}",
|
||||
delete(delete_big_fish_work).route_layer(middleware::from_fn_with_state(
|
||||
@@ -637,12 +696,127 @@ pub fn build_router(state: AppState) -> Router {
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/agent/sessions",
|
||||
post(create_puzzle_agent_session).route_layer(middleware::from_fn_with_state(
|
||||
"/api/creation/match3d/sessions",
|
||||
post(create_match3d_agent_session).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/match3d/sessions/{session_id}",
|
||||
get(get_match3d_agent_session).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/match3d/sessions/{session_id}/messages",
|
||||
post(submit_match3d_agent_message).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/match3d/sessions/{session_id}/messages/stream",
|
||||
post(stream_match3d_agent_message).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/match3d/sessions/{session_id}/actions",
|
||||
post(execute_match3d_agent_action).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/match3d/sessions/{session_id}/compile",
|
||||
post(compile_match3d_agent_draft).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/match3d/works",
|
||||
get(get_match3d_works).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/match3d/works/{profile_id}",
|
||||
get(get_match3d_work_detail)
|
||||
.patch(put_match3d_work)
|
||||
.put(put_match3d_work)
|
||||
.delete(delete_match3d_work)
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/creation/match3d/works/{profile_id}/publish",
|
||||
post(publish_match3d_work).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route("/api/runtime/match3d/gallery", get(list_match3d_gallery))
|
||||
.route(
|
||||
"/api/runtime/match3d/works/{profile_id}/runs",
|
||||
post(start_match3d_run).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/match3d/runs/{run_id}",
|
||||
get(get_match3d_run).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/match3d/runs/{run_id}/click",
|
||||
post(click_match3d_item).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/match3d/runs/{run_id}/stop",
|
||||
post(stop_match3d_run).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/match3d/runs/{run_id}/restart",
|
||||
post(restart_match3d_run).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/match3d/runs/{run_id}/time-up",
|
||||
post(finish_match3d_time_up).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/agent/sessions",
|
||||
post(create_puzzle_agent_session)
|
||||
// 中文注释:拼图表单会携带单张参考图 Data URL,需只给该写入入口放宽 body 上限。
|
||||
.layer(DefaultBodyLimit::max(
|
||||
PUZZLE_REFERENCE_IMAGE_BODY_LIMIT_BYTES,
|
||||
))
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/agent/sessions/{session_id}",
|
||||
get(get_puzzle_agent_session).route_layer(middleware::from_fn_with_state(
|
||||
@@ -666,10 +840,15 @@ pub fn build_router(state: AppState) -> Router {
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/agent/sessions/{session_id}/actions",
|
||||
post(execute_puzzle_agent_action).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
post(execute_puzzle_agent_action)
|
||||
// 中文注释:生成草稿/重新出图会复用 referenceImageSrc,避免默认 2MB JSON limit 拦截。
|
||||
.layer(DefaultBodyLimit::max(
|
||||
PUZZLE_REFERENCE_IMAGE_BODY_LIMIT_BYTES,
|
||||
))
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/works",
|
||||
@@ -688,11 +867,32 @@ pub fn build_router(state: AppState) -> Router {
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/works/{profile_id}/point-incentive/claim",
|
||||
post(claim_puzzle_work_point_incentive).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route("/api/runtime/puzzle/gallery", get(list_puzzle_gallery))
|
||||
.route(
|
||||
"/api/runtime/puzzle/gallery/{profile_id}",
|
||||
get(get_puzzle_gallery_detail),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/gallery/{profile_id}/remix",
|
||||
post(remix_puzzle_gallery_work).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/gallery/{profile_id}/like",
|
||||
post(record_puzzle_gallery_like).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/runs",
|
||||
post(start_puzzle_run).route_layer(middleware::from_fn_with_state(
|
||||
@@ -728,6 +928,20 @@ pub fn build_router(state: AppState) -> Router {
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/runs/{run_id}/pause",
|
||||
post(update_puzzle_run_pause).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/runs/{run_id}/props",
|
||||
post(use_puzzle_runtime_prop).route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
require_bearer_auth,
|
||||
)),
|
||||
)
|
||||
.route(
|
||||
"/api/runtime/puzzle/runs/{run_id}/leaderboard",
|
||||
post(submit_puzzle_leaderboard).route_layer(middleware::from_fn_with_state(
|
||||
@@ -1041,6 +1255,30 @@ mod tests {
|
||||
.await
|
||||
}
|
||||
|
||||
fn sign_test_user_token(
|
||||
state: &AppState,
|
||||
user: &module_auth::AuthUser,
|
||||
session_id: &str,
|
||||
) -> String {
|
||||
let claims = AccessTokenClaims::from_input(
|
||||
AccessTokenClaimsInput {
|
||||
user_id: user.id.clone(),
|
||||
session_id: session_id.to_string(),
|
||||
provider: AuthProvider::Password,
|
||||
roles: vec!["user".to_string()],
|
||||
token_version: user.token_version,
|
||||
phone_verified: false,
|
||||
binding_status: BindingStatus::Active,
|
||||
display_name: Some(user.display_name.clone()),
|
||||
},
|
||||
state.auth_jwt_config(),
|
||||
OffsetDateTime::now_utc(),
|
||||
)
|
||||
.expect("claims should build");
|
||||
|
||||
sign_access_token(&claims, state.auth_jwt_config()).expect("token should sign")
|
||||
}
|
||||
|
||||
async fn password_login_request(
|
||||
app: Router,
|
||||
phone_number: &str,
|
||||
@@ -1424,6 +1662,88 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn puzzle_agent_actions_accept_reference_image_body_above_default_limit() {
|
||||
let state = AppState::new(AppConfig::default()).expect("state should build");
|
||||
let seed_user = seed_phone_user_with_password(&state, "13800138024", TEST_PASSWORD).await;
|
||||
let token = sign_test_user_token(&state, &seed_user, "sess_puzzle_reference_body");
|
||||
let app = build_router(state);
|
||||
let reference_image_src = format!("data:image/png;base64,{}", "A".repeat(3 * 1024 * 1024));
|
||||
let request_body = serde_json::json!({
|
||||
"action": "unsupported_large_reference_test",
|
||||
"referenceImageSrc": reference_image_src,
|
||||
})
|
||||
.to_string();
|
||||
assert!(request_body.len() > 2 * 1024 * 1024);
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/runtime/puzzle/agent/sessions/puzzle-session-large/actions")
|
||||
.header("authorization", format!("Bearer {token}"))
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(request_body))
|
||||
.expect("request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
let body = response
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.expect("response body should collect")
|
||||
.to_bytes();
|
||||
let body_text = String::from_utf8_lossy(&body);
|
||||
assert!(
|
||||
body_text.contains("unsupported_large_reference_test"),
|
||||
"handler should parse the oversized reference payload before rejecting the action: {body_text}"
|
||||
);
|
||||
assert!(!body_text.contains("length limit exceeded"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn puzzle_agent_session_creation_accepts_reference_image_body_above_default_limit() {
|
||||
let state = AppState::new(AppConfig::default()).expect("state should build");
|
||||
let seed_user = seed_phone_user_with_password(&state, "13800138025", TEST_PASSWORD).await;
|
||||
let token = sign_test_user_token(&state, &seed_user, "sess_puzzle_form_reference_body");
|
||||
let app = build_router(state);
|
||||
let request_body = format!(
|
||||
"{{\"seedText\":\"大参考图拼图\",\"pictureDescription\":\"一张用于验证 body limit 的参考图。\",\"referenceImageSrc\":\"data:image/png;base64,{}\"",
|
||||
"A".repeat(3 * 1024 * 1024)
|
||||
);
|
||||
assert!(request_body.len() > 2 * 1024 * 1024);
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/runtime/puzzle/agent/sessions")
|
||||
.header("authorization", format!("Bearer {token}"))
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(request_body))
|
||||
.expect("request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
||||
let body = response
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.expect("response body should collect")
|
||||
.to_bytes();
|
||||
let body_text = String::from_utf8_lossy(&body);
|
||||
assert!(
|
||||
body_text.contains("EOF") || body_text.contains("expected"),
|
||||
"handler should parse the oversized form payload before rejecting malformed JSON: {body_text}"
|
||||
);
|
||||
assert!(!body_text.contains("length limit exceeded"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn password_entry_rejects_unknown_phone_without_registration() {
|
||||
let app = build_router(AppState::new(AppConfig::default()).expect("state should build"));
|
||||
@@ -1494,6 +1814,10 @@ mod tests {
|
||||
payload["user"]["loginMethod"],
|
||||
Value::String("password".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
payload["user"]["createdAt"],
|
||||
Value::String(seed_user.created_at)
|
||||
);
|
||||
assert!(payload["token"].as_str().is_some());
|
||||
}
|
||||
|
||||
@@ -1773,6 +2097,9 @@ mod tests {
|
||||
payload["user"]["phoneNumberMasked"],
|
||||
Value::String("138****8000".to_string())
|
||||
);
|
||||
assert!(payload["user"]["createdAt"].as_str().is_some());
|
||||
assert_eq!(payload["created"], Value::Bool(true));
|
||||
assert!(payload["referral"].is_null());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -1879,6 +2206,175 @@ mod tests {
|
||||
serde_json::from_slice(&second_body).expect("second login payload should be json");
|
||||
|
||||
assert_eq!(first_payload["user"]["id"], second_payload["user"]["id"]);
|
||||
assert_eq!(first_payload["created"], Value::Bool(true));
|
||||
assert_eq!(second_payload["created"], Value::Bool(false));
|
||||
assert!(second_payload["referral"].is_null());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn phone_login_invite_code_failure_does_not_block_created_user() {
|
||||
let config = AppConfig {
|
||||
sms_auth_enabled: true,
|
||||
..AppConfig::default()
|
||||
};
|
||||
let app = build_router(AppState::new(config).expect("state should build"));
|
||||
|
||||
let send_code_response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/auth/phone/send-code")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
serde_json::json!({
|
||||
"phone": "13600136000",
|
||||
"scene": "login"
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.expect("send code request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("send code request should succeed");
|
||||
assert_eq!(send_code_response.status(), StatusCode::OK);
|
||||
|
||||
let login_response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/auth/phone/login")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
serde_json::json!({
|
||||
"phone": "13600136000",
|
||||
"code": "123456",
|
||||
"inviteCode": "SPRING2026"
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.expect("login request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("login request should succeed");
|
||||
|
||||
assert_eq!(login_response.status(), StatusCode::OK);
|
||||
let body = login_response
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.expect("login body should collect")
|
||||
.to_bytes();
|
||||
let payload: Value = serde_json::from_slice(&body).expect("login payload should be json");
|
||||
|
||||
assert!(payload["token"].as_str().is_some());
|
||||
assert_eq!(payload["created"], Value::Bool(true));
|
||||
assert_eq!(payload["referral"]["ok"], Value::Bool(false));
|
||||
assert_eq!(
|
||||
payload["referral"]["message"],
|
||||
Value::String("邀请码无效,已继续注册".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn phone_login_existing_user_ignores_invite_code() {
|
||||
let config = AppConfig {
|
||||
sms_auth_enabled: true,
|
||||
..AppConfig::default()
|
||||
};
|
||||
let app = build_router(AppState::new(config).expect("state should build"));
|
||||
|
||||
let first_send_code_response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/auth/phone/send-code")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
serde_json::json!({
|
||||
"phone": "13500135000",
|
||||
"scene": "login"
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.expect("send code request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("send code request should succeed");
|
||||
assert_eq!(first_send_code_response.status(), StatusCode::OK);
|
||||
|
||||
let first_login_response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/auth/phone/login")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
serde_json::json!({
|
||||
"phone": "13500135000",
|
||||
"code": "123456"
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.expect("first login request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("first login request should succeed");
|
||||
assert_eq!(first_login_response.status(), StatusCode::OK);
|
||||
|
||||
let second_send_code_response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/auth/phone/send-code")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
serde_json::json!({
|
||||
"phone": "13500135000",
|
||||
"scene": "login"
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.expect("send code request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("send code request should succeed");
|
||||
assert_eq!(second_send_code_response.status(), StatusCode::OK);
|
||||
|
||||
let second_login_response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/auth/phone/login")
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(
|
||||
serde_json::json!({
|
||||
"phone": "13500135000",
|
||||
"code": "123456",
|
||||
"inviteCode": "SPRING2026"
|
||||
})
|
||||
.to_string(),
|
||||
))
|
||||
.expect("second login request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("second login request should succeed");
|
||||
|
||||
assert_eq!(second_login_response.status(), StatusCode::OK);
|
||||
let body = second_login_response
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.expect("second login body should collect")
|
||||
.to_bytes();
|
||||
let payload: Value =
|
||||
serde_json::from_slice(&body).expect("second login payload should be json");
|
||||
|
||||
assert_eq!(payload["created"], Value::Bool(false));
|
||||
assert!(payload["referral"].is_null());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -3094,6 +3590,23 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_page_route_is_not_mounted() {
|
||||
let app = build_router(AppState::new(AppConfig::default()).expect("state should build"));
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/admin")
|
||||
.body(Body::empty())
|
||||
.expect("admin page request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("admin page request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_login_returns_token_when_configured() {
|
||||
let mut config = AppConfig::default();
|
||||
|
||||
@@ -29,7 +29,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// 资产操作统一预扣叙世币;扣费流水 ID 由业务资源 ID 参与构造,保证重试幂等。
|
||||
/// 资产操作统一预扣光点;扣费流水 ID 由业务资源 ID 参与构造,保证重试幂等。
|
||||
async fn consume_asset_operation_points(
|
||||
state: &AppState,
|
||||
owner_user_id: &str,
|
||||
@@ -79,15 +79,20 @@ async fn refund_asset_operation_points(
|
||||
asset_kind,
|
||||
asset_id,
|
||||
error = %error,
|
||||
"资产操作失败后的叙世币退款失败"
|
||||
"资产操作失败后的光点退款失败"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn map_asset_operation_wallet_error(error: SpacetimeClientError) -> AppError {
|
||||
let message = error.to_string();
|
||||
tracing::warn!(
|
||||
provider = "profile-wallet",
|
||||
error = %message,
|
||||
"资产操作光点预扣失败"
|
||||
);
|
||||
let status = match &error {
|
||||
SpacetimeClientError::Runtime(_) => StatusCode::BAD_REQUEST,
|
||||
SpacetimeClientError::Procedure(message) if message.contains("叙世币余额不足") => {
|
||||
SpacetimeClientError::Procedure(message) if message.contains("光点余额不足") => {
|
||||
StatusCode::CONFLICT
|
||||
}
|
||||
_ => StatusCode::BAD_GATEWAY,
|
||||
@@ -95,7 +100,7 @@ pub(crate) fn map_asset_operation_wallet_error(error: SpacetimeClientError) -> A
|
||||
|
||||
AppError::from_status(status).with_details(json!({
|
||||
"provider": "profile-wallet",
|
||||
"message": error.to_string(),
|
||||
"message": message,
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
@@ -23,8 +23,8 @@ use shared_contracts::assets::{
|
||||
use spacetime_client::SpacetimeClientError;
|
||||
|
||||
use crate::{
|
||||
api_response::json_success_body, http_error::AppError, platform_errors::map_oss_error,
|
||||
request_context::RequestContext, state::AppState,
|
||||
api_response::json_success_body, auth::AuthenticatedAccessToken, http_error::AppError,
|
||||
platform_errors::map_oss_error, request_context::RequestContext, state::AppState,
|
||||
};
|
||||
|
||||
// 历史素材类型需要与 SpacetimeDB 侧白名单保持同一口径,避免新增素材类型时 HTTP 门面漏同步。
|
||||
@@ -119,6 +119,7 @@ pub async fn get_asset_read_url(
|
||||
pub async fn get_asset_history(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
Query(query): Query<AssetHistoryQuery>,
|
||||
) -> Result<Json<Value>, AppError> {
|
||||
let asset_kind = query.kind.trim().to_string();
|
||||
@@ -133,18 +134,23 @@ pub async fn get_asset_history(
|
||||
|
||||
let entries = state
|
||||
.spacetime_client()
|
||||
.list_asset_history(module_assets::AssetHistoryListInput {
|
||||
asset_kind,
|
||||
limit: query.limit.unwrap_or(120).clamp(1, 120),
|
||||
})
|
||||
.list_asset_history(build_asset_history_list_input(asset_kind, query.limit))
|
||||
.await
|
||||
.map_err(map_confirm_asset_object_error)?;
|
||||
let owner_user_id = authenticated.claims().user_id().to_string();
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
AssetHistoryListResponse {
|
||||
assets: entries
|
||||
.into_iter()
|
||||
// 中文注释:Maincloud 旧 wasm 的历史素材 procedure 仍按类型返回,HTTP 门面必须兜底做账号隔离。
|
||||
.filter(|entry| {
|
||||
is_asset_history_owned_by(
|
||||
entry.owner_user_id.as_deref(),
|
||||
owner_user_id.as_str(),
|
||||
)
|
||||
})
|
||||
.map(|entry| AssetHistoryEntryPayload {
|
||||
owner_label: format_asset_owner_label(entry.owner_user_id.as_deref()),
|
||||
asset_object_id: entry.asset_object_id,
|
||||
@@ -296,6 +302,25 @@ fn is_supported_asset_history_kind(asset_kind: &str) -> bool {
|
||||
SUPPORTED_ASSET_HISTORY_KINDS.contains(&asset_kind)
|
||||
}
|
||||
|
||||
fn is_asset_history_owned_by(entry_owner_user_id: Option<&str>, owner_user_id: &str) -> bool {
|
||||
let owner_user_id = owner_user_id.trim();
|
||||
!owner_user_id.is_empty()
|
||||
&& entry_owner_user_id
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
== Some(owner_user_id)
|
||||
}
|
||||
|
||||
fn build_asset_history_list_input(
|
||||
asset_kind: String,
|
||||
limit: Option<u32>,
|
||||
) -> module_assets::AssetHistoryListInput {
|
||||
module_assets::AssetHistoryListInput {
|
||||
asset_kind,
|
||||
limit: limit.unwrap_or(120).clamp(1, 120),
|
||||
}
|
||||
}
|
||||
|
||||
fn supported_asset_history_kind_message() -> String {
|
||||
format!(
|
||||
"历史素材类型只支持 {}",
|
||||
@@ -480,6 +505,29 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn asset_history_owner_filter_keeps_only_authenticated_owner_assets() {
|
||||
assert!(super::is_asset_history_owned_by(
|
||||
Some("user-current"),
|
||||
"user-current"
|
||||
));
|
||||
assert!(!super::is_asset_history_owned_by(
|
||||
Some("user-other"),
|
||||
"user-current"
|
||||
));
|
||||
assert!(!super::is_asset_history_owned_by(None, "user-current"));
|
||||
assert!(!super::is_asset_history_owned_by(Some("user-current"), ""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn asset_history_input_clamps_limit_for_spacetime_query() {
|
||||
let input =
|
||||
super::build_asset_history_list_input("puzzle_cover_image".to_string(), Some(240));
|
||||
|
||||
assert_eq!(input.asset_kind, "puzzle_cover_image");
|
||||
assert_eq!(input.limit, 120);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn direct_upload_ticket_returns_service_unavailable_when_oss_missing() {
|
||||
let app = build_router(AppState::new(AppConfig::default()).expect("state should build"));
|
||||
|
||||
@@ -7,10 +7,12 @@ pub fn map_auth_user_payload(user: AuthUser) -> AuthUserPayload {
|
||||
public_user_code: user.public_user_code,
|
||||
username: user.username,
|
||||
display_name: user.display_name,
|
||||
avatar_url: user.avatar_url,
|
||||
phone_number_masked: user.phone_number_masked,
|
||||
login_method: user.login_method.as_str().to_string(),
|
||||
binding_status: user.binding_status.as_str().to_string(),
|
||||
wechat_bound: user.wechat_bound,
|
||||
created_at: user.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,5 +21,6 @@ pub fn map_public_user_summary_payload(user: AuthUser) -> PublicUserSummaryPaylo
|
||||
id: user.id,
|
||||
public_user_code: user.public_user_code,
|
||||
display_name: user.display_name,
|
||||
avatar_url: user.avatar_url,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ pub async fn get_public_user_by_code(
|
||||
.get_user_by_public_user_code(&code)
|
||||
.map_err(map_public_user_search_error)?
|
||||
.ok_or_else(|| {
|
||||
AppError::from_status(StatusCode::NOT_FOUND).with_message("未找到对应叙世号用户")
|
||||
AppError::from_status(StatusCode::NOT_FOUND).with_message("未找到对应百梦号用户")
|
||||
})?;
|
||||
|
||||
Ok(json_success_body(
|
||||
@@ -60,12 +60,15 @@ pub async fn get_public_user_by_id(
|
||||
fn map_public_user_search_error(error: module_auth::PasswordEntryError) -> AppError {
|
||||
match error {
|
||||
module_auth::PasswordEntryError::InvalidPublicUserCode => {
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_message("叙世号格式不正确")
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_message("百梦号格式不正确")
|
||||
}
|
||||
module_auth::PasswordEntryError::Store(_)
|
||||
| module_auth::PasswordEntryError::PasswordHash(_)
|
||||
| module_auth::PasswordEntryError::InvalidPhoneNumber
|
||||
| module_auth::PasswordEntryError::InvalidPasswordLength
|
||||
| module_auth::PasswordEntryError::InvalidDisplayName
|
||||
| module_auth::PasswordEntryError::InvalidAvatarDataUrl
|
||||
| module_auth::PasswordEntryError::EmptyProfileUpdate
|
||||
| module_auth::PasswordEntryError::InvalidCredentials
|
||||
| module_auth::PasswordEntryError::UserNotFound => {
|
||||
AppError::from_status(StatusCode::INTERNAL_SERVER_ERROR).with_message(error.to_string())
|
||||
|
||||
@@ -34,10 +34,11 @@ use spacetime_client::{
|
||||
BigFishAgentMessageRecord, BigFishAnchorItemRecord, BigFishAnchorPackRecord,
|
||||
BigFishAssetCoverageRecord, BigFishAssetGenerateRecordInput, BigFishAssetSlotRecord,
|
||||
BigFishBackgroundBlueprintRecord, BigFishDraftCompileRecordInput, BigFishGameDraftRecord,
|
||||
BigFishInputSubmitRecordInput, BigFishLevelBlueprintRecord, BigFishMessageSubmitRecordInput,
|
||||
BigFishPlayReportRecordInput, BigFishRunStartRecordInput, BigFishRuntimeEntityRecord,
|
||||
BigFishRuntimeParamsRecord, BigFishRuntimeRunRecord, BigFishSessionCreateRecordInput,
|
||||
BigFishSessionRecord, BigFishVector2Record, BigFishWorkSummaryRecord, SpacetimeClientError,
|
||||
BigFishInputSubmitRecordInput, BigFishLevelBlueprintRecord, BigFishLikeReportRecordInput,
|
||||
BigFishMessageSubmitRecordInput, BigFishPlayReportRecordInput, BigFishRunStartRecordInput,
|
||||
BigFishRuntimeEntityRecord, BigFishRuntimeParamsRecord, BigFishRuntimeRunRecord,
|
||||
BigFishSessionCreateRecordInput, BigFishSessionRecord, BigFishVector2Record,
|
||||
BigFishWorkRemixRecordInput, BigFishWorkSummaryRecord, SpacetimeClientError,
|
||||
};
|
||||
use tokio::time::sleep;
|
||||
|
||||
@@ -63,6 +64,7 @@ use crate::{
|
||||
platform_errors::map_oss_error,
|
||||
request_context::RequestContext,
|
||||
state::AppState,
|
||||
work_author::resolve_work_author_by_user_id,
|
||||
};
|
||||
|
||||
pub async fn create_big_fish_session(
|
||||
@@ -147,7 +149,7 @@ pub async fn get_big_fish_works(
|
||||
BigFishWorksResponse {
|
||||
items: items
|
||||
.into_iter()
|
||||
.map(map_big_fish_work_summary_response)
|
||||
.map(|item| map_big_fish_work_summary_response(&state, item))
|
||||
.collect(),
|
||||
},
|
||||
))
|
||||
@@ -179,7 +181,7 @@ pub async fn list_big_fish_gallery(
|
||||
BigFishWorksResponse {
|
||||
items: items
|
||||
.into_iter()
|
||||
.map(map_big_fish_work_summary_response)
|
||||
.map(|item| map_big_fish_work_summary_response(&state, item))
|
||||
.collect(),
|
||||
},
|
||||
))
|
||||
@@ -206,7 +208,7 @@ pub async fn delete_big_fish_work(
|
||||
BigFishWorksResponse {
|
||||
items: items
|
||||
.into_iter()
|
||||
.map(map_big_fish_work_summary_response)
|
||||
.map(|item| map_big_fish_work_summary_response(&state, item))
|
||||
.collect(),
|
||||
},
|
||||
))
|
||||
@@ -248,7 +250,7 @@ pub async fn record_big_fish_play(
|
||||
BigFishWorksResponse {
|
||||
items: items
|
||||
.into_iter()
|
||||
.map(map_big_fish_work_summary_response)
|
||||
.map(|item| map_big_fish_work_summary_response(&state, item))
|
||||
.collect(),
|
||||
},
|
||||
))
|
||||
@@ -283,6 +285,37 @@ pub async fn start_big_fish_run(
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn record_big_fish_gallery_like(
|
||||
State(state): State<AppState>,
|
||||
Path(session_id): Path<String>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
ensure_non_empty(&request_context, &session_id, "sessionId")?;
|
||||
|
||||
let items = state
|
||||
.spacetime_client()
|
||||
.record_big_fish_like(BigFishLikeReportRecordInput {
|
||||
session_id,
|
||||
user_id: authenticated.claims().user_id().to_string(),
|
||||
liked_at_micros: current_utc_micros(),
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
big_fish_error_response(&request_context, map_big_fish_client_error(error))
|
||||
})?;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
BigFishWorksResponse {
|
||||
items: items
|
||||
.into_iter()
|
||||
.map(|item| map_big_fish_work_summary_response(&state, item))
|
||||
.collect(),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn get_big_fish_run(
|
||||
State(state): State<AppState>,
|
||||
Path(run_id): Path<String>,
|
||||
@@ -350,6 +383,36 @@ pub async fn submit_big_fish_input(
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn remix_big_fish_gallery_work(
|
||||
State(state): State<AppState>,
|
||||
Path(session_id): Path<String>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
ensure_non_empty(&request_context, &session_id, "sessionId")?;
|
||||
|
||||
let session = state
|
||||
.spacetime_client()
|
||||
.remix_big_fish_work(BigFishWorkRemixRecordInput {
|
||||
source_session_id: session_id,
|
||||
target_session_id: build_prefixed_uuid_id("big-fish-session-"),
|
||||
target_owner_user_id: authenticated.claims().user_id().to_string(),
|
||||
welcome_message_id: build_prefixed_uuid_id("big-fish-message-"),
|
||||
remixed_at_micros: current_utc_micros(),
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
big_fish_error_response(&request_context, map_big_fish_client_error(error))
|
||||
})?;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
BigFishSessionResponse {
|
||||
session: map_big_fish_session_response(session),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn submit_big_fish_message(
|
||||
State(state): State<AppState>,
|
||||
Path(session_id): Path<String>,
|
||||
@@ -1038,24 +1101,33 @@ fn map_big_fish_agent_message_response(
|
||||
}
|
||||
|
||||
fn map_big_fish_work_summary_response(
|
||||
state: &AppState,
|
||||
item: BigFishWorkSummaryRecord,
|
||||
) -> BigFishWorkSummaryResponse {
|
||||
let author = resolve_work_author_by_user_id(state, &item.owner_user_id, None, None);
|
||||
BigFishWorkSummaryResponse {
|
||||
work_id: item.work_id,
|
||||
source_session_id: item.source_session_id,
|
||||
owner_user_id: item.owner_user_id,
|
||||
author_display_name: author.display_name,
|
||||
title: item.title,
|
||||
subtitle: item.subtitle,
|
||||
summary: item.summary,
|
||||
cover_image_src: item.cover_image_src,
|
||||
status: item.status,
|
||||
updated_at: current_timestamp_micros_to_string(item.updated_at_micros),
|
||||
published_at: item
|
||||
.published_at_micros
|
||||
.map(current_timestamp_micros_to_string),
|
||||
publish_ready: item.publish_ready,
|
||||
level_count: item.level_count,
|
||||
level_main_image_ready_count: item.level_main_image_ready_count,
|
||||
level_motion_ready_count: item.level_motion_ready_count,
|
||||
background_ready: item.background_ready,
|
||||
play_count: item.play_count,
|
||||
remix_count: item.remix_count,
|
||||
like_count: item.like_count,
|
||||
recent_play_count_7d: item.recent_play_count_7d,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ use serde::Deserialize;
|
||||
use serde_json::Value as JsonValue;
|
||||
|
||||
use crate::creation_agent_llm_turn::parse_json_response_text;
|
||||
use crate::llm_model_routing::CREATION_TEMPLATE_LLM_MODEL;
|
||||
|
||||
const BIG_FISH_DRAFT_JSON_ONLY_SYSTEM_PROMPT: &str = r#"你是一个负责把“大鱼吃小鱼”玩法锚点编译成首版可实现草稿的中文玩法策划。
|
||||
|
||||
@@ -108,10 +109,15 @@ async fn request_big_fish_json_stage(
|
||||
empty_response_message: &str,
|
||||
) -> Result<JsonValue, BigFishDraftCompileError> {
|
||||
let response = llm_client
|
||||
.request_text(LlmTextRequest::new(vec![
|
||||
LlmMessage::system(BIG_FISH_DRAFT_JSON_ONLY_SYSTEM_PROMPT),
|
||||
LlmMessage::user(user_prompt),
|
||||
]))
|
||||
.request_text(
|
||||
LlmTextRequest::new(vec![
|
||||
LlmMessage::system(BIG_FISH_DRAFT_JSON_ONLY_SYSTEM_PROMPT),
|
||||
LlmMessage::user(user_prompt),
|
||||
])
|
||||
.with_model(CREATION_TEMPLATE_LLM_MODEL)
|
||||
.with_responses_api()
|
||||
.with_web_search(true),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
BigFishDraftCompileError::new(format!("{debug_label} LLM 请求失败:{error}"))
|
||||
@@ -124,12 +130,16 @@ async fn request_big_fish_json_stage(
|
||||
Ok(value) => Ok(value),
|
||||
Err(_) => {
|
||||
let repaired = llm_client
|
||||
.request_text(LlmTextRequest::new(vec![
|
||||
LlmMessage::system(BIG_FISH_DRAFT_JSON_REPAIR_SYSTEM_PROMPT),
|
||||
LlmMessage::user(format!(
|
||||
"请把下面这段文本修复成单个合法 JSON 对象,不要补充额外解释:\n\n{text}"
|
||||
)),
|
||||
]))
|
||||
.request_text(
|
||||
LlmTextRequest::new(vec![
|
||||
LlmMessage::system(BIG_FISH_DRAFT_JSON_REPAIR_SYSTEM_PROMPT),
|
||||
LlmMessage::user(format!(
|
||||
"请把下面这段文本修复成单个合法 JSON 对象,不要补充额外解释:\n\n{text}"
|
||||
)),
|
||||
])
|
||||
.with_model(CREATION_TEMPLATE_LLM_MODEL)
|
||||
.with_responses_api(),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
BigFishDraftCompileError::new(format!(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::{env, fs, net::SocketAddr, path::PathBuf};
|
||||
use std::{env, fs, net::SocketAddr, path::PathBuf, time::Duration};
|
||||
|
||||
use platform_llm::{
|
||||
DEFAULT_ARK_BASE_URL, DEFAULT_MAX_RETRIES, DEFAULT_REQUEST_TIMEOUT_MS,
|
||||
@@ -74,6 +74,7 @@ pub struct AppConfig {
|
||||
pub spacetime_database: String,
|
||||
pub spacetime_token: Option<String>,
|
||||
pub spacetime_pool_size: u32,
|
||||
pub spacetime_procedure_timeout: Duration,
|
||||
pub llm_provider: LlmProvider,
|
||||
pub llm_base_url: String,
|
||||
pub llm_api_key: Option<String>,
|
||||
@@ -165,6 +166,7 @@ impl Default for AppConfig {
|
||||
spacetime_database: "genarrative-dev".to_string(),
|
||||
spacetime_token: None,
|
||||
spacetime_pool_size: 4,
|
||||
spacetime_procedure_timeout: Duration::from_secs(30),
|
||||
llm_provider: LlmProvider::Ark,
|
||||
llm_base_url: DEFAULT_ARK_BASE_URL.to_string(),
|
||||
llm_api_key: None,
|
||||
@@ -436,6 +438,12 @@ impl AppConfig {
|
||||
{
|
||||
config.spacetime_pool_size = spacetime_pool_size;
|
||||
}
|
||||
if let Some(spacetime_procedure_timeout_seconds) =
|
||||
read_first_duration_seconds_env(&["GENARRATIVE_SPACETIME_PROCEDURE_TIMEOUT_SECONDS"])
|
||||
{
|
||||
config.spacetime_procedure_timeout =
|
||||
Duration::from_secs(spacetime_procedure_timeout_seconds);
|
||||
}
|
||||
|
||||
if let Some(llm_provider) =
|
||||
read_first_llm_provider_env(&["GENARRATIVE_LLM_PROVIDER", "LLM_PROVIDER"])
|
||||
@@ -840,6 +848,26 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_env_reads_spacetime_procedure_timeout() {
|
||||
let _guard = ENV_LOCK
|
||||
.get_or_init(|| Mutex::new(()))
|
||||
.lock()
|
||||
.expect("env lock should not poison");
|
||||
|
||||
unsafe {
|
||||
std::env::remove_var("GENARRATIVE_SPACETIME_PROCEDURE_TIMEOUT_SECONDS");
|
||||
std::env::set_var("GENARRATIVE_SPACETIME_PROCEDURE_TIMEOUT_SECONDS", "45");
|
||||
}
|
||||
|
||||
let config = AppConfig::from_env();
|
||||
assert_eq!(config.spacetime_procedure_timeout.as_secs(), 45);
|
||||
|
||||
unsafe {
|
||||
std::env::remove_var("GENARRATIVE_SPACETIME_PROCEDURE_TIMEOUT_SECONDS");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_env_reads_rpg_llm_web_search_switch() {
|
||||
let _guard = ENV_LOCK
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use platform_llm::{LlmClient, LlmMessage, LlmStreamDelta, LlmTextRequest};
|
||||
use serde_json::Value as JsonValue;
|
||||
|
||||
use crate::llm_model_routing::CREATION_TEMPLATE_LLM_MODEL;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct CreationAgentLlmTurnErrorMessages<'a> {
|
||||
pub model_unavailable: &'a str,
|
||||
@@ -69,6 +71,8 @@ fn build_creation_agent_llm_request(
|
||||
LlmMessage::system(system_prompt),
|
||||
LlmMessage::user(user_prompt),
|
||||
])
|
||||
.with_model(CREATION_TEMPLATE_LLM_MODEL)
|
||||
.with_responses_api()
|
||||
.with_web_search(enable_web_search)
|
||||
}
|
||||
|
||||
@@ -79,10 +83,14 @@ pub(crate) async fn request_creation_agent_json_turn<E>(
|
||||
build_error: impl Fn(String) -> E,
|
||||
) -> Result<JsonValue, E> {
|
||||
let response = llm_client
|
||||
.request_text(LlmTextRequest::new(vec![
|
||||
LlmMessage::system(system_prompt),
|
||||
LlmMessage::user(user_prompt),
|
||||
]))
|
||||
.request_text(
|
||||
LlmTextRequest::new(vec![
|
||||
LlmMessage::system(system_prompt),
|
||||
LlmMessage::user(user_prompt),
|
||||
])
|
||||
.with_model(CREATION_TEMPLATE_LLM_MODEL)
|
||||
.with_responses_api(),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| build_error(error.to_string()))?;
|
||||
parse_json_response_text(response.content.as_str())
|
||||
@@ -160,6 +168,8 @@ fn read_reply_text(parsed: &JsonValue) -> Option<String> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::llm_model_routing::CREATION_TEMPLATE_LLM_MODEL;
|
||||
|
||||
use super::{
|
||||
build_creation_agent_llm_request, extract_reply_text_from_partial_json,
|
||||
parse_json_response_text,
|
||||
@@ -188,6 +198,8 @@ mod tests {
|
||||
build_creation_agent_llm_request("系统提示".to_string(), "用户提示".to_string(), true);
|
||||
|
||||
assert!(request.enable_web_search);
|
||||
assert_eq!(request.model.as_deref(), Some(CREATION_TEMPLATE_LLM_MODEL));
|
||||
assert_eq!(request.protocol, platform_llm::LlmTextProtocol::Responses);
|
||||
assert_eq!(request.messages.len(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,9 +38,10 @@ use spacetime_client::{
|
||||
CustomWorldAgentSessionRecord, CustomWorldDraftCardDetailRecord,
|
||||
CustomWorldDraftCardDetailSectionRecord, CustomWorldDraftCardRecord,
|
||||
CustomWorldGalleryEntryRecord, CustomWorldLibraryEntryRecord,
|
||||
CustomWorldProfileUpsertRecordInput, CustomWorldPublishGateRecord,
|
||||
CustomWorldResultPreviewBlockerRecord, CustomWorldSupportedActionRecord,
|
||||
CustomWorldWorkSummaryRecord, SpacetimeClientError,
|
||||
CustomWorldProfileLikeReportRecordInput, CustomWorldProfilePlayReportRecordInput,
|
||||
CustomWorldProfileRemixRecordInput, CustomWorldProfileUpsertRecordInput,
|
||||
CustomWorldPublishGateRecord, CustomWorldResultPreviewBlockerRecord,
|
||||
CustomWorldSupportedActionRecord, CustomWorldWorkSummaryRecord, SpacetimeClientError,
|
||||
};
|
||||
use std::{collections::BTreeSet, convert::Infallible, sync::Arc, time::Instant};
|
||||
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
|
||||
@@ -72,6 +73,7 @@ use crate::{
|
||||
},
|
||||
request_context::RequestContext,
|
||||
state::AppState,
|
||||
work_author::resolve_work_author_by_user_id,
|
||||
};
|
||||
|
||||
const DRAFT_ASSET_GENERATION_MAX_ATTEMPTS: u32 = 3;
|
||||
@@ -415,7 +417,7 @@ pub async fn get_custom_world_library(
|
||||
let owner_user_id = authenticated.claims().user_id().to_string();
|
||||
let entries = state
|
||||
.spacetime_client()
|
||||
.list_custom_world_profiles(owner_user_id)
|
||||
.list_custom_world_works(owner_user_id.clone())
|
||||
.await
|
||||
.map_err(|error| {
|
||||
custom_world_error_response(&request_context, map_custom_world_client_error(error))
|
||||
@@ -426,7 +428,13 @@ pub async fn get_custom_world_library(
|
||||
CustomWorldLibraryResponse {
|
||||
entries: entries
|
||||
.into_iter()
|
||||
.map(map_custom_world_library_entry_response)
|
||||
.filter_map(|item| {
|
||||
map_custom_world_library_entry_response_from_work_summary(
|
||||
&state,
|
||||
item,
|
||||
&owner_user_id,
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
))
|
||||
@@ -459,7 +467,7 @@ pub async fn get_custom_world_library_detail(
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CustomWorldGalleryDetailResponse {
|
||||
entry: map_custom_world_library_entry_response(detail.entry),
|
||||
entry: map_custom_world_library_entry_response(&state, detail.entry),
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -540,8 +548,11 @@ pub async fn put_custom_world_library_profile(
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CustomWorldLibraryMutationResponse {
|
||||
entry: map_custom_world_library_entry_response(mutation.entry.clone()),
|
||||
entries: vec![map_custom_world_library_entry_response(mutation.entry)],
|
||||
entry: map_custom_world_library_entry_response(&state, mutation.entry.clone()),
|
||||
entries: vec![map_custom_world_library_entry_response(
|
||||
&state,
|
||||
mutation.entry,
|
||||
)],
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -576,7 +587,7 @@ pub async fn delete_custom_world_library_profile(
|
||||
CustomWorldLibraryResponse {
|
||||
entries: entries
|
||||
.into_iter()
|
||||
.map(map_custom_world_library_entry_response)
|
||||
.map(|entry| map_custom_world_library_entry_response(&state, entry))
|
||||
.collect(),
|
||||
},
|
||||
))
|
||||
@@ -628,8 +639,11 @@ pub async fn publish_custom_world_library_profile(
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CustomWorldLibraryMutationResponse {
|
||||
entry: map_custom_world_library_entry_response(mutation.entry.clone()),
|
||||
entries: vec![map_custom_world_library_entry_response(mutation.entry)],
|
||||
entry: map_custom_world_library_entry_response(&state, mutation.entry.clone()),
|
||||
entries: vec![map_custom_world_library_entry_response(
|
||||
&state,
|
||||
mutation.entry,
|
||||
)],
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -667,8 +681,11 @@ pub async fn unpublish_custom_world_library_profile(
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CustomWorldLibraryMutationResponse {
|
||||
entry: map_custom_world_library_entry_response(mutation.entry.clone()),
|
||||
entries: vec![map_custom_world_library_entry_response(mutation.entry)],
|
||||
entry: map_custom_world_library_entry_response(&state, mutation.entry.clone()),
|
||||
entries: vec![map_custom_world_library_entry_response(
|
||||
&state,
|
||||
mutation.entry,
|
||||
)],
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -690,7 +707,7 @@ pub async fn list_custom_world_gallery(
|
||||
CustomWorldGalleryResponse {
|
||||
entries: entries
|
||||
.into_iter()
|
||||
.map(map_custom_world_gallery_card_response)
|
||||
.map(|entry| map_custom_world_gallery_card_response(&state, entry))
|
||||
.collect(),
|
||||
},
|
||||
))
|
||||
@@ -722,7 +739,7 @@ pub async fn get_custom_world_gallery_detail(
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CustomWorldGalleryDetailResponse {
|
||||
entry: map_custom_world_library_entry_response(detail.entry),
|
||||
entry: map_custom_world_library_entry_response(&state, detail.entry),
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -753,7 +770,123 @@ pub async fn get_custom_world_gallery_detail_by_code(
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CustomWorldGalleryDetailResponse {
|
||||
entry: map_custom_world_library_entry_response(detail.entry),
|
||||
entry: map_custom_world_library_entry_response(&state, detail.entry),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn remix_custom_world_gallery_profile(
|
||||
State(state): State<AppState>,
|
||||
Path((owner_user_id, profile_id)): Path<(String, String)>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
if owner_user_id.trim().is_empty() || profile_id.trim().is_empty() {
|
||||
return Err(custom_world_error_response(
|
||||
&request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "custom-world-gallery",
|
||||
"message": "ownerUserId and profileId are required",
|
||||
})),
|
||||
));
|
||||
}
|
||||
|
||||
let mutation = state
|
||||
.spacetime_client()
|
||||
.remix_custom_world_profile(CustomWorldProfileRemixRecordInput {
|
||||
source_owner_user_id: owner_user_id,
|
||||
source_profile_id: profile_id,
|
||||
target_owner_user_id: authenticated.claims().user_id().to_string(),
|
||||
target_profile_id: build_prefixed_uuid_id("custom-world-profile-"),
|
||||
author_display_name: resolve_author_display_name(&state, &authenticated),
|
||||
remixed_at_micros: current_utc_micros(),
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
custom_world_error_response(&request_context, map_custom_world_client_error(error))
|
||||
})?;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CustomWorldLibraryMutationResponse {
|
||||
entry: map_custom_world_library_entry_response(&state, mutation.entry.clone()),
|
||||
entries: vec![map_custom_world_library_entry_response(
|
||||
&state,
|
||||
mutation.entry,
|
||||
)],
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn record_custom_world_gallery_play(
|
||||
State(state): State<AppState>,
|
||||
Path((owner_user_id, profile_id)): Path<(String, String)>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(_authenticated): Extension<AuthenticatedAccessToken>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
if owner_user_id.trim().is_empty() || profile_id.trim().is_empty() {
|
||||
return Err(custom_world_error_response(
|
||||
&request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "custom-world-gallery",
|
||||
"message": "ownerUserId and profileId are required",
|
||||
})),
|
||||
));
|
||||
}
|
||||
|
||||
let mutation = state
|
||||
.spacetime_client()
|
||||
.record_custom_world_profile_play(CustomWorldProfilePlayReportRecordInput {
|
||||
owner_user_id,
|
||||
profile_id,
|
||||
played_at_micros: current_utc_micros(),
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
custom_world_error_response(&request_context, map_custom_world_client_error(error))
|
||||
})?;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CustomWorldGalleryDetailResponse {
|
||||
entry: map_custom_world_library_entry_response(&state, mutation.entry),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn record_custom_world_gallery_like(
|
||||
State(state): State<AppState>,
|
||||
Path((owner_user_id, profile_id)): Path<(String, String)>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
if owner_user_id.trim().is_empty() || profile_id.trim().is_empty() {
|
||||
return Err(custom_world_error_response(
|
||||
&request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "custom-world-gallery",
|
||||
"message": "ownerUserId and profileId are required",
|
||||
})),
|
||||
));
|
||||
}
|
||||
|
||||
let mutation = state
|
||||
.spacetime_client()
|
||||
.record_custom_world_profile_like(CustomWorldProfileLikeReportRecordInput {
|
||||
owner_user_id,
|
||||
profile_id,
|
||||
user_id: authenticated.claims().user_id().to_string(),
|
||||
liked_at_micros: current_utc_micros(),
|
||||
})
|
||||
.await
|
||||
.map_err(|error| {
|
||||
custom_world_error_response(&request_context, map_custom_world_client_error(error))
|
||||
})?;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
CustomWorldGalleryDetailResponse {
|
||||
entry: map_custom_world_library_entry_response(&state, mutation.entry),
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -2613,18 +2746,25 @@ async fn upsert_custom_world_draft_foundation_progress(
|
||||
}
|
||||
|
||||
fn map_custom_world_library_entry_response(
|
||||
state: &AppState,
|
||||
entry: CustomWorldLibraryEntryRecord,
|
||||
) -> CustomWorldLibraryEntryResponse {
|
||||
let author = resolve_work_author_by_user_id(
|
||||
state,
|
||||
&entry.owner_user_id,
|
||||
Some(&entry.author_display_name),
|
||||
entry.author_public_user_code.as_deref(),
|
||||
);
|
||||
CustomWorldLibraryEntryResponse {
|
||||
owner_user_id: entry.owner_user_id,
|
||||
profile_id: entry.profile_id,
|
||||
public_work_code: entry.public_work_code,
|
||||
author_public_user_code: entry.author_public_user_code,
|
||||
author_public_user_code: author.public_user_code.or(entry.author_public_user_code),
|
||||
profile: entry.profile,
|
||||
visibility: entry.visibility,
|
||||
published_at: entry.published_at,
|
||||
updated_at: entry.updated_at,
|
||||
author_display_name: entry.author_display_name,
|
||||
author_display_name: author.display_name,
|
||||
world_name: entry.world_name,
|
||||
subtitle: entry.subtitle,
|
||||
summary_text: entry.summary_text,
|
||||
@@ -2632,21 +2772,114 @@ fn map_custom_world_library_entry_response(
|
||||
theme_mode: entry.theme_mode,
|
||||
playable_npc_count: entry.playable_npc_count,
|
||||
landmark_count: entry.landmark_count,
|
||||
play_count: entry.play_count,
|
||||
remix_count: entry.remix_count,
|
||||
like_count: entry.like_count,
|
||||
recent_play_count_7d: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn map_custom_world_library_entry_response_from_work_summary(
|
||||
state: &AppState,
|
||||
item: CustomWorldWorkSummaryRecord,
|
||||
owner_user_id: &str,
|
||||
) -> Option<CustomWorldLibraryEntryResponse> {
|
||||
let profile_id = item.profile_id.as_ref()?.clone();
|
||||
let profile = build_custom_world_library_list_profile_payload(&item, &profile_id);
|
||||
let author = resolve_work_author_by_user_id(state, owner_user_id, None, None);
|
||||
Some(CustomWorldLibraryEntryResponse {
|
||||
owner_user_id: owner_user_id.to_string(),
|
||||
public_work_code: (item.status == "published")
|
||||
.then(|| build_public_work_code_from_profile_id(&profile_id)),
|
||||
profile_id,
|
||||
author_public_user_code: author.public_user_code,
|
||||
profile,
|
||||
visibility: item.status,
|
||||
published_at: item.published_at,
|
||||
updated_at: item.updated_at,
|
||||
author_display_name: author.display_name,
|
||||
world_name: item.title,
|
||||
subtitle: item.subtitle,
|
||||
summary_text: item.summary,
|
||||
cover_image_src: item.cover_image_src,
|
||||
theme_mode: "mythic".to_string(),
|
||||
playable_npc_count: item.playable_npc_count,
|
||||
landmark_count: item.landmark_count,
|
||||
play_count: 0,
|
||||
remix_count: 0,
|
||||
like_count: 0,
|
||||
recent_play_count_7d: 0,
|
||||
})
|
||||
}
|
||||
|
||||
fn build_public_work_code_from_profile_id(profile_id: &str) -> String {
|
||||
let digits = profile_id
|
||||
.chars()
|
||||
.filter(|character| character.is_ascii_digit())
|
||||
.collect::<String>();
|
||||
let normalized_digits = if digits.is_empty() {
|
||||
let checksum = profile_id.bytes().fold(0u32, |accumulator, value| {
|
||||
accumulator.wrapping_mul(131) + u32::from(value)
|
||||
});
|
||||
format!("{:08}", checksum % 100_000_000)
|
||||
} else {
|
||||
format!("{:0>8}", &digits[digits.len().saturating_sub(8)..])
|
||||
};
|
||||
|
||||
format!("CW-{normalized_digits}")
|
||||
}
|
||||
|
||||
fn build_custom_world_library_list_profile_payload(
|
||||
item: &CustomWorldWorkSummaryRecord,
|
||||
profile_id: &str,
|
||||
) -> Value {
|
||||
json!({
|
||||
"id": profile_id,
|
||||
"name": item.title,
|
||||
"subtitle": item.subtitle,
|
||||
"summary": item.summary,
|
||||
"tone": "",
|
||||
"playerGoal": "",
|
||||
"settingText": "",
|
||||
"themeMode": "mythic",
|
||||
"templateWorldType": "WUXIA",
|
||||
"compatibilityTemplateWorldType": Value::Null,
|
||||
"cover": item.cover_image_src.as_ref().map(|image_src| json!({
|
||||
"sourceType": "generated",
|
||||
"imageSrc": image_src,
|
||||
})),
|
||||
"majorFactions": [],
|
||||
"coreConflicts": [],
|
||||
"playableNpcs": [],
|
||||
"storyNpcs": [],
|
||||
"items": [],
|
||||
"camp": Value::Null,
|
||||
"landmarks": [],
|
||||
"ownedSettingLayers": Value::Null,
|
||||
})
|
||||
}
|
||||
|
||||
fn map_custom_world_gallery_card_response(
|
||||
state: &AppState,
|
||||
entry: CustomWorldGalleryEntryRecord,
|
||||
) -> CustomWorldGalleryCardResponse {
|
||||
let author = resolve_work_author_by_user_id(
|
||||
state,
|
||||
&entry.owner_user_id,
|
||||
Some(&entry.author_display_name),
|
||||
Some(&entry.author_public_user_code),
|
||||
);
|
||||
CustomWorldGalleryCardResponse {
|
||||
owner_user_id: entry.owner_user_id,
|
||||
profile_id: entry.profile_id,
|
||||
public_work_code: entry.public_work_code,
|
||||
author_public_user_code: entry.author_public_user_code,
|
||||
author_public_user_code: author
|
||||
.public_user_code
|
||||
.unwrap_or(entry.author_public_user_code),
|
||||
visibility: entry.visibility,
|
||||
published_at: entry.published_at,
|
||||
updated_at: entry.updated_at,
|
||||
author_display_name: entry.author_display_name,
|
||||
author_display_name: author.display_name,
|
||||
world_name: entry.world_name,
|
||||
subtitle: entry.subtitle,
|
||||
summary_text: entry.summary_text,
|
||||
@@ -2654,6 +2887,10 @@ fn map_custom_world_gallery_card_response(
|
||||
theme_mode: entry.theme_mode,
|
||||
playable_npc_count: entry.playable_npc_count,
|
||||
landmark_count: entry.landmark_count,
|
||||
play_count: entry.play_count,
|
||||
remix_count: entry.remix_count,
|
||||
like_count: entry.like_count,
|
||||
recent_play_count_7d: entry.recent_play_count_7d,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3225,7 +3462,7 @@ fn resolve_author_public_user_code(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::INTERNAL_SERVER_ERROR).with_details(json!({
|
||||
"provider": "custom-world-library",
|
||||
"message": format!("作者叙世号读取失败:{error}"),
|
||||
"message": format!("作者百梦号读取失败:{error}"),
|
||||
})),
|
||||
)
|
||||
})?
|
||||
@@ -3236,7 +3473,7 @@ fn resolve_author_public_user_code(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::UNAUTHORIZED).with_details(json!({
|
||||
"provider": "custom-world-library",
|
||||
"message": "当前登录用户缺少叙世号",
|
||||
"message": "当前登录用户缺少百梦号",
|
||||
})),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use platform_llm::{LlmClient, LlmMessage, LlmTextRequest};
|
||||
use serde_json::{Map as JsonMap, Value as JsonValue};
|
||||
use shared_contracts::runtime::ExecuteCustomWorldAgentActionRequest;
|
||||
|
||||
use crate::llm_model_routing::CREATION_TEMPLATE_LLM_MODEL;
|
||||
use spacetime_client::CustomWorldAgentSessionRecord;
|
||||
|
||||
const CUSTOM_WORLD_AGENT_CHARACTER_EXPANSION_SYSTEM_PROMPT: &str =
|
||||
@@ -92,10 +94,15 @@ pub async fn generate_custom_world_agent_entities(
|
||||
};
|
||||
|
||||
let response = llm_client
|
||||
.request_text(LlmTextRequest::new(vec![
|
||||
LlmMessage::system(system_prompt),
|
||||
LlmMessage::user(user_prompt),
|
||||
]))
|
||||
.request_text(
|
||||
LlmTextRequest::new(vec![
|
||||
LlmMessage::system(system_prompt),
|
||||
LlmMessage::user(user_prompt),
|
||||
])
|
||||
.with_model(CREATION_TEMPLATE_LLM_MODEL)
|
||||
.with_responses_api()
|
||||
.with_web_search(true),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| format!("{action} LLM 请求失败:{error}"))?;
|
||||
let generated_entities = parse_json_array_response(response.content.as_str())
|
||||
|
||||
@@ -35,6 +35,7 @@ use crate::{
|
||||
build_result_scene_npc_system_prompt, build_result_scene_npc_user_prompt,
|
||||
},
|
||||
http_error::AppError,
|
||||
llm_model_routing::CREATION_TEMPLATE_LLM_MODEL,
|
||||
platform_errors::map_oss_error,
|
||||
prompt::scene_background::{
|
||||
DEFAULT_CUSTOM_WORLD_SCENE_IMAGE_NEGATIVE_PROMPT, SceneImagePromptLandmark,
|
||||
@@ -1032,7 +1033,10 @@ async fn generate_entity_with_fallback(state: &AppState, profile: &Value, kind:
|
||||
let request = LlmTextRequest::new(vec![
|
||||
LlmMessage::system(build_result_entity_system_prompt()),
|
||||
LlmMessage::user(build_result_entity_user_prompt(profile, kind, &fallback)),
|
||||
]);
|
||||
])
|
||||
.with_model(CREATION_TEMPLATE_LLM_MODEL)
|
||||
.with_responses_api()
|
||||
.with_web_search(true);
|
||||
|
||||
llm_client
|
||||
.request_text(request)
|
||||
@@ -1058,7 +1062,10 @@ async fn generate_scene_npc_with_fallback(
|
||||
landmark_id,
|
||||
&fallback,
|
||||
)),
|
||||
]);
|
||||
])
|
||||
.with_model(CREATION_TEMPLATE_LLM_MODEL)
|
||||
.with_responses_api()
|
||||
.with_web_search(true);
|
||||
|
||||
llm_client
|
||||
.request_text(request)
|
||||
|
||||
@@ -11,6 +11,8 @@ use serde_json::{Map as JsonMap, Value as JsonValue, json};
|
||||
use shared_contracts::runtime::ExecuteCustomWorldAgentActionRequest;
|
||||
use spacetime_client::CustomWorldAgentSessionRecord;
|
||||
|
||||
use crate::llm_model_routing::CREATION_TEMPLATE_LLM_MODEL;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct CustomWorldFoundationDraftResult {
|
||||
pub draft_profile_json: String,
|
||||
@@ -39,7 +41,7 @@ pub async fn generate_custom_world_foundation_draft(
|
||||
emit_foundation_draft_progress(
|
||||
&mut on_progress,
|
||||
"整理世界骨架",
|
||||
"正在根据创作者锚点生成第一版世界框架。",
|
||||
"正在根据百梦主锚点生成第一版世界框架。",
|
||||
12,
|
||||
);
|
||||
let mut framework = request_foundation_json_stage(
|
||||
@@ -174,10 +176,15 @@ where
|
||||
F: Fn(&str) -> String,
|
||||
{
|
||||
let response = llm_client
|
||||
.request_text(LlmTextRequest::new(vec![
|
||||
LlmMessage::system(FOUNDATION_JSON_ONLY_SYSTEM_PROMPT),
|
||||
LlmMessage::user(user_prompt),
|
||||
]))
|
||||
.request_text(
|
||||
LlmTextRequest::new(vec![
|
||||
LlmMessage::system(FOUNDATION_JSON_ONLY_SYSTEM_PROMPT),
|
||||
LlmMessage::user(user_prompt),
|
||||
])
|
||||
.with_model(CREATION_TEMPLATE_LLM_MODEL)
|
||||
.with_responses_api()
|
||||
.with_web_search(true),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| format!("{debug_label} LLM 请求失败:{error}"))?;
|
||||
let text = response.content.trim();
|
||||
@@ -188,10 +195,14 @@ where
|
||||
Ok(value) => Ok(value),
|
||||
Err(_) => {
|
||||
let repaired = llm_client
|
||||
.request_text(LlmTextRequest::new(vec![
|
||||
LlmMessage::system(FOUNDATION_JSON_REPAIR_SYSTEM_PROMPT),
|
||||
LlmMessage::user(repair_prompt_builder(text)),
|
||||
]))
|
||||
.request_text(
|
||||
LlmTextRequest::new(vec![
|
||||
LlmMessage::system(FOUNDATION_JSON_REPAIR_SYSTEM_PROMPT),
|
||||
LlmMessage::user(repair_prompt_builder(text)),
|
||||
])
|
||||
.with_model(CREATION_TEMPLATE_LLM_MODEL)
|
||||
.with_responses_api(),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| format!("{repair_debug_label} LLM 请求失败:{error}"))?;
|
||||
parse_json_response_text(repaired.content.as_str())
|
||||
|
||||
@@ -7,7 +7,7 @@ use axum::{
|
||||
sse::{Event, Sse},
|
||||
},
|
||||
};
|
||||
use platform_llm::{LlmMessage, LlmMessageRole, LlmTextRequest};
|
||||
use platform_llm::{LlmError, LlmMessage, LlmMessageRole, LlmTextProtocol, LlmTextRequest};
|
||||
use serde_json::{Value, json};
|
||||
use shared_contracts::llm::{
|
||||
LlmChatCompletionRequest, LlmChatCompletionResponse, LlmChatMessagePayload, LlmChatMessageRole,
|
||||
@@ -35,6 +35,7 @@ pub async fn proxy_llm_chat_completions(
|
||||
|
||||
let request = LlmTextRequest {
|
||||
model: payload.model,
|
||||
protocol: LlmTextProtocol::ChatCompletions,
|
||||
messages: payload
|
||||
.messages
|
||||
.into_iter()
|
||||
|
||||
2
server-rs/crates/api-server/src/llm_model_routing.rs
Normal file
2
server-rs/crates/api-server/src/llm_model_routing.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub(crate) const RPG_STORY_LLM_MODEL: &str = "doubao-seed-character-251128";
|
||||
pub(crate) const CREATION_TEMPLATE_LLM_MODEL: &str = "deepseek-v3-2-251201";
|
||||
@@ -35,17 +35,21 @@ mod error_middleware;
|
||||
mod health;
|
||||
mod http_error;
|
||||
mod llm;
|
||||
mod llm_model_routing;
|
||||
mod login_options;
|
||||
mod logout;
|
||||
mod logout_all;
|
||||
mod match3d;
|
||||
mod password_entry;
|
||||
mod password_management;
|
||||
mod phone_auth;
|
||||
mod platform_errors;
|
||||
mod profile_identity;
|
||||
mod prompt;
|
||||
mod puzzle;
|
||||
mod puzzle_agent_turn;
|
||||
mod refresh_session;
|
||||
mod registration_reward;
|
||||
mod request_context;
|
||||
mod response_headers;
|
||||
mod runtime_browse_history;
|
||||
@@ -61,6 +65,7 @@ mod story_battles;
|
||||
mod story_sessions;
|
||||
mod wechat_auth;
|
||||
mod wechat_provider;
|
||||
mod work_author;
|
||||
|
||||
use shared_logging::init_tracing;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
1578
server-rs/crates/api-server/src/match3d.rs
Normal file
1578
server-rs/crates/api-server/src/match3d.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -39,6 +39,14 @@ pub async fn password_entry(
|
||||
state.password_entry_service().execute(input).await
|
||||
}
|
||||
.map_err(map_password_entry_error)?;
|
||||
if result.created {
|
||||
crate::registration_reward::grant_new_user_registration_wallet_reward(
|
||||
&state,
|
||||
&request_context,
|
||||
&result.user.id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
let session_client = resolve_session_client_context(&headers);
|
||||
let signed_session = create_password_auth_session(&state, &result.user, &session_client)?;
|
||||
state
|
||||
@@ -80,10 +88,15 @@ fn map_password_entry_error(error: PasswordEntryError) -> AppError {
|
||||
"field": "password",
|
||||
})),
|
||||
PasswordEntryError::InvalidPublicUserCode => AppError::from_status(StatusCode::BAD_REQUEST)
|
||||
.with_message("叙世号格式不正确")
|
||||
.with_message("百梦号格式不正确")
|
||||
.with_details(json!({
|
||||
"field": "phone",
|
||||
})),
|
||||
PasswordEntryError::InvalidDisplayName
|
||||
| PasswordEntryError::InvalidAvatarDataUrl
|
||||
| PasswordEntryError::EmptyProfileUpdate => {
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_message(error.to_string())
|
||||
}
|
||||
PasswordEntryError::InvalidCredentials => {
|
||||
AppError::from_status(StatusCode::UNAUTHORIZED).with_message("手机号或密码错误")
|
||||
}
|
||||
|
||||
@@ -103,6 +103,11 @@ fn map_password_management_error(error: PasswordEntryError) -> AppError {
|
||||
PasswordEntryError::InvalidPhoneNumber | PasswordEntryError::InvalidPublicUserCode => {
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_message(error.to_string())
|
||||
}
|
||||
PasswordEntryError::InvalidDisplayName
|
||||
| PasswordEntryError::InvalidAvatarDataUrl
|
||||
| PasswordEntryError::EmptyProfileUpdate => {
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_message(error.to_string())
|
||||
}
|
||||
PasswordEntryError::InvalidPasswordLength => AppError::from_status(StatusCode::BAD_REQUEST)
|
||||
.with_message("密码长度需要在 6 到 128 位之间"),
|
||||
PasswordEntryError::InvalidCredentials => {
|
||||
|
||||
@@ -9,7 +9,8 @@ use module_auth::{
|
||||
};
|
||||
use serde_json::json;
|
||||
use shared_contracts::auth::{
|
||||
PhoneLoginRequest, PhoneLoginResponse, PhoneSendCodeRequest, PhoneSendCodeResponse,
|
||||
PhoneLoginReferralResponse, PhoneLoginRequest, PhoneLoginResponse, PhoneSendCodeRequest,
|
||||
PhoneSendCodeResponse,
|
||||
};
|
||||
use time::OffsetDateTime;
|
||||
use tracing::{info, warn};
|
||||
@@ -111,6 +112,7 @@ pub async fn phone_login(
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_message("手机号登录暂未启用")
|
||||
);
|
||||
}
|
||||
let invite_code = payload.invite_code.clone();
|
||||
let result = match state
|
||||
.phone_auth_service()
|
||||
.login(
|
||||
@@ -147,6 +149,26 @@ pub async fn phone_login(
|
||||
return Err(map_phone_auth_error(error));
|
||||
}
|
||||
};
|
||||
let created = result.created;
|
||||
if created {
|
||||
crate::registration_reward::grant_new_user_registration_wallet_reward(
|
||||
&state,
|
||||
&request_context,
|
||||
&result.user.id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
let referral = if created {
|
||||
bind_referral_invite_code_on_registration(
|
||||
&state,
|
||||
&request_context,
|
||||
result.user.id.clone(),
|
||||
invite_code,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let session_client = resolve_session_client_context(&headers);
|
||||
let signed_session = create_auth_session(
|
||||
&state,
|
||||
@@ -175,11 +197,55 @@ pub async fn phone_login(
|
||||
PhoneLoginResponse {
|
||||
token: signed_session.access_token,
|
||||
user: map_auth_user_payload(result.user),
|
||||
created,
|
||||
referral,
|
||||
},
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
async fn bind_referral_invite_code_on_registration(
|
||||
state: &AppState,
|
||||
request_context: &RequestContext,
|
||||
user_id: String,
|
||||
invite_code: Option<String>,
|
||||
) -> Option<PhoneLoginReferralResponse> {
|
||||
let invite_code = invite_code
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())?;
|
||||
let updated_at_micros = OffsetDateTime::now_utc().unix_timestamp_nanos() / 1_000;
|
||||
match state
|
||||
.spacetime_client()
|
||||
.redeem_profile_referral_invite_code(user_id, invite_code, updated_at_micros as i64)
|
||||
.await
|
||||
{
|
||||
Ok(record) => Some(PhoneLoginReferralResponse {
|
||||
ok: true,
|
||||
message: Some("邀请码已绑定".to_string()),
|
||||
invitee_reward_granted: record.invitee_reward_granted,
|
||||
inviter_reward_granted: record.inviter_reward_granted,
|
||||
invitee_balance_after: Some(record.invitee_balance_after),
|
||||
inviter_balance_after: Some(record.inviter_balance_after),
|
||||
}),
|
||||
Err(error) => {
|
||||
warn!(
|
||||
request_id = request_context.request_id(),
|
||||
operation = request_context.operation(),
|
||||
error = %error,
|
||||
"注册邀请码绑定失败,登录流程继续"
|
||||
);
|
||||
Some(PhoneLoginReferralResponse {
|
||||
ok: false,
|
||||
message: Some("邀请码无效,已继续注册".to_string()),
|
||||
invitee_reward_granted: false,
|
||||
inviter_reward_granted: false,
|
||||
invitee_balance_after: None,
|
||||
inviter_balance_after: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn map_phone_auth_scene(raw_scene: Option<&str>) -> Result<PhoneAuthScene, AppError> {
|
||||
match raw_scene.unwrap_or("login").trim() {
|
||||
"login" => Ok(PhoneAuthScene::Login),
|
||||
|
||||
105
server-rs/crates/api-server/src/profile_identity.rs
Normal file
105
server-rs/crates/api-server/src/profile_identity.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Extension, State},
|
||||
http::StatusCode,
|
||||
};
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};
|
||||
use image::GenericImageView;
|
||||
use module_auth::{PasswordEntryError, UpdateProfileInput};
|
||||
use shared_contracts::auth::{ProfileUpdateRequest, ProfileUpdateResponse};
|
||||
|
||||
use crate::{
|
||||
api_response::json_success_body, auth::AuthenticatedAccessToken,
|
||||
auth_payload::map_auth_user_payload, http_error::AppError, request_context::RequestContext,
|
||||
state::AppState,
|
||||
};
|
||||
|
||||
const MAX_AVATAR_BYTES: usize = 5 * 1024 * 1024;
|
||||
const AVATAR_SIZE_PX: u32 = 256;
|
||||
|
||||
pub async fn update_profile_identity(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
Json(payload): Json<ProfileUpdateRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
if let Some(avatar_data_url) = payload.avatar_data_url.as_deref() {
|
||||
validate_avatar_data_url(avatar_data_url)?;
|
||||
}
|
||||
|
||||
let result = state
|
||||
.password_entry_service()
|
||||
.update_profile(UpdateProfileInput {
|
||||
user_id: authenticated.claims().user_id().to_string(),
|
||||
display_name: payload.display_name,
|
||||
avatar_url: payload.avatar_data_url,
|
||||
})
|
||||
.map_err(map_profile_update_error)?;
|
||||
|
||||
state
|
||||
.sync_auth_store_snapshot_to_spacetime()
|
||||
.await
|
||||
.map_err(|error| {
|
||||
AppError::from_status(StatusCode::INTERNAL_SERVER_ERROR).with_message(error.to_string())
|
||||
})?;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
ProfileUpdateResponse {
|
||||
user: map_auth_user_payload(result.user),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn validate_avatar_data_url(value: &str) -> Result<(), AppError> {
|
||||
let Some((header, payload)) = value.trim().split_once(',') else {
|
||||
return Err(invalid_avatar_error("头像图片格式不正确"));
|
||||
};
|
||||
if !matches!(
|
||||
header,
|
||||
"data:image/png;base64" | "data:image/jpeg;base64" | "data:image/webp;base64"
|
||||
) {
|
||||
return Err(invalid_avatar_error("头像仅支持 jpg、png、webp"));
|
||||
}
|
||||
|
||||
let bytes = BASE64_STANDARD
|
||||
.decode(payload)
|
||||
.map_err(|_| invalid_avatar_error("头像图片格式不正确"))?;
|
||||
if bytes.len() > MAX_AVATAR_BYTES {
|
||||
return Err(invalid_avatar_error("头像图片不能超过 5MB"));
|
||||
}
|
||||
|
||||
let image =
|
||||
image::load_from_memory(&bytes).map_err(|_| invalid_avatar_error("头像图片格式不正确"))?;
|
||||
let (width, height) = image.dimensions();
|
||||
if width != AVATAR_SIZE_PX || height != AVATAR_SIZE_PX {
|
||||
return Err(invalid_avatar_error("头像裁剪尺寸需要为 256x256"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn invalid_avatar_error(message: &'static str) -> AppError {
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_message(message)
|
||||
}
|
||||
|
||||
fn map_profile_update_error(error: PasswordEntryError) -> AppError {
|
||||
match error {
|
||||
PasswordEntryError::InvalidDisplayName
|
||||
| PasswordEntryError::InvalidAvatarDataUrl
|
||||
| PasswordEntryError::EmptyProfileUpdate => {
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_message(error.to_string())
|
||||
}
|
||||
PasswordEntryError::UserNotFound => AppError::from_status(StatusCode::UNAUTHORIZED)
|
||||
.with_message("当前登录态已失效,请重新登录"),
|
||||
PasswordEntryError::Store(_) | PasswordEntryError::PasswordHash(_) => {
|
||||
AppError::from_status(StatusCode::INTERNAL_SERVER_ERROR).with_message(error.to_string())
|
||||
}
|
||||
PasswordEntryError::InvalidPhoneNumber
|
||||
| PasswordEntryError::InvalidPasswordLength
|
||||
| PasswordEntryError::InvalidPublicUserCode
|
||||
| PasswordEntryError::InvalidCredentials => {
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_message(error.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ use crate::creation_agent_anchor_templates::{
|
||||
};
|
||||
use crate::creation_agent_chat::render_quick_fill_extra_rules;
|
||||
|
||||
pub(crate) const BIG_FISH_AGENT_SYSTEM_PROMPT: &str = r#"你是一个负责和创作者共创“大鱼吃小鱼”竖屏玩法的中文创意策划。
|
||||
pub(crate) const BIG_FISH_AGENT_SYSTEM_PROMPT: &str = r#"你是一个负责和百梦主共创“大鱼吃小鱼”竖屏玩法的中文创意策划。
|
||||
|
||||
你必须把用户灵感收束成可以编译为可玩草稿的玩法、生态视觉、成长阶梯和风险节奏。
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
pub(crate) mod big_fish;
|
||||
pub(crate) mod character_animation;
|
||||
pub(crate) mod character_visual;
|
||||
pub(crate) mod puzzle_image;
|
||||
pub(crate) mod puzzle;
|
||||
pub(crate) mod rpg;
|
||||
pub(crate) mod scene_background;
|
||||
|
||||
|
||||
212
server-rs/crates/api-server/src/prompt/puzzle/agent_chat.rs
Normal file
212
server-rs/crates/api-server/src/prompt/puzzle/agent_chat.rs
Normal file
@@ -0,0 +1,212 @@
|
||||
use module_puzzle::{PuzzleAnchorPack, PuzzleAnchorStatus, empty_anchor_pack};
|
||||
use serde_json::{Value as JsonValue, json};
|
||||
use spacetime_client::{
|
||||
PuzzleAgentMessageRecord, PuzzleAgentSessionRecord, PuzzleAnchorPackRecord,
|
||||
};
|
||||
|
||||
use crate::creation_agent_anchor_templates::{
|
||||
get_creation_agent_anchor_template, render_anchor_question_block,
|
||||
};
|
||||
use crate::creation_agent_chat::render_quick_fill_extra_rules;
|
||||
|
||||
/// 拼图共创 Agent 的系统提示词。
|
||||
///
|
||||
/// 这里作为拼图聊天提示词主源,业务文件只负责调用 LLM、解析结果和写回状态。
|
||||
pub(crate) const PUZZLE_AGENT_SYSTEM_PROMPT: &str = r#"你是一个负责和百梦主共创拼图画面的中文创意策划。
|
||||
|
||||
你要帮助用户把一句灵感逐步收束成可以发布成拼图关卡的视觉方案。
|
||||
|
||||
你必须同时输出:
|
||||
1. 一段直接发给用户的中文回复 replyText
|
||||
2. 当前进度 progressPercent
|
||||
3. 下一轮完整可用的 nextAnchorPack
|
||||
|
||||
硬约束:
|
||||
1. 只能输出 JSON,不能输出代码块或解释
|
||||
2. nextAnchorPack 必须是完整对象,不能只输出 patch
|
||||
3. replyText 必须是自然中文,不能提“字段”“锚点”“结构”“JSON”等内部词
|
||||
4. replyText 一次最多推进一个最关键问题
|
||||
5. 如果用户已经给出明确方向,就优先吸收和收束,不要机械反问
|
||||
6. progressPercent 范围只能是 0 到 100
|
||||
7. status 只能使用 missing / inferred / confirmed / locked
|
||||
"#;
|
||||
|
||||
/// 拼图共创 Agent 单轮 JSON 输出契约。
|
||||
const PUZZLE_AGENT_OUTPUT_CONTRACT: &str = r#"请严格按以下 JSON 输出,不要输出其他文字:
|
||||
{
|
||||
"replyText": "",
|
||||
"progressPercent": 0,
|
||||
"nextAnchorPack": {
|
||||
"themePromise": {
|
||||
"key": "themePromise",
|
||||
"label": "题材承诺",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
},
|
||||
"visualSubject": {
|
||||
"key": "visualSubject",
|
||||
"label": "画面主体",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
},
|
||||
"visualMood": {
|
||||
"key": "visualMood",
|
||||
"label": "视觉气质",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
},
|
||||
"compositionHooks": {
|
||||
"key": "compositionHooks",
|
||||
"label": "拼图记忆点",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
},
|
||||
"tagsAndForbidden": {
|
||||
"key": "tagsAndForbidden",
|
||||
"label": "标签与禁忌",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
/// 拼图共创 Agent 的用户提示词,用于触发模型按系统约定返回单轮 JSON。
|
||||
pub(crate) const PUZZLE_AGENT_JSON_TURN_USER_PROMPT: &str = "请按约定输出这一轮的 JSON。";
|
||||
|
||||
/// 拼图草稿生成对话提示词脚本。
|
||||
pub(crate) fn build_puzzle_agent_prompt(
|
||||
session: &PuzzleAgentSessionRecord,
|
||||
quick_fill_requested: bool,
|
||||
) -> String {
|
||||
let anchor_question_block = get_creation_agent_anchor_template("puzzle")
|
||||
.map(render_anchor_question_block)
|
||||
.unwrap_or_else(|| "模板目标:收束成可以发布为拼图关卡的视觉方案。".to_string());
|
||||
let quick_fill_rules = if quick_fill_requested {
|
||||
format!(
|
||||
"\n\n{}",
|
||||
render_quick_fill_extra_rules(
|
||||
"当前题材方向里的拼图关键词",
|
||||
"不要要求用户再提供素材、风格或禁忌",
|
||||
"输出完整 nextAnchorPack,直接补齐 value 为空或 status 为 missing 的项",
|
||||
"生成结果页",
|
||||
)
|
||||
)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
format!(
|
||||
"{anchor_question_block}{quick_fill_rules}\n\n当前是第 {turn} 轮,当前进度 {progress}% 。\n\n是否要求自动补充剩余关键字:{quick_fill_requested_text}\n\n当前 anchor pack:\n{anchor_pack}\n\n最近聊天记录:\n{chat_history}\n\n{contract}",
|
||||
anchor_question_block = anchor_question_block,
|
||||
quick_fill_rules = quick_fill_rules,
|
||||
turn = session.current_turn.saturating_add(1),
|
||||
progress = session.progress_percent,
|
||||
quick_fill_requested_text = if quick_fill_requested { "是" } else { "否" },
|
||||
anchor_pack = serialize_puzzle_record_anchor_pack(&session.anchor_pack),
|
||||
chat_history =
|
||||
serde_json::to_string_pretty(&build_chat_history(session.messages.as_slice()))
|
||||
.unwrap_or_else(|_| "[]".to_string()),
|
||||
contract = PUZZLE_AGENT_OUTPUT_CONTRACT,
|
||||
)
|
||||
}
|
||||
|
||||
/// 将 SpacetimeDB 记录态锚点序列化成提示词可读 JSON。
|
||||
pub(crate) fn serialize_puzzle_record_anchor_pack(record: &PuzzleAnchorPackRecord) -> String {
|
||||
serde_json::to_string_pretty(&map_puzzle_record_anchor_pack(record)).unwrap_or_else(|_| {
|
||||
serde_json::to_string_pretty(&empty_anchor_pack()).unwrap_or_else(|_| "{}".to_string())
|
||||
})
|
||||
}
|
||||
|
||||
fn build_chat_history(messages: &[PuzzleAgentMessageRecord]) -> Vec<JsonValue> {
|
||||
messages
|
||||
.iter()
|
||||
.map(|message| {
|
||||
json!({
|
||||
"role": message.role,
|
||||
"kind": message.kind,
|
||||
"content": message.text,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn map_puzzle_record_anchor_pack(record: &PuzzleAnchorPackRecord) -> PuzzleAnchorPack {
|
||||
PuzzleAnchorPack {
|
||||
theme_promise: map_puzzle_record_anchor_item(&record.theme_promise),
|
||||
visual_subject: map_puzzle_record_anchor_item(&record.visual_subject),
|
||||
visual_mood: map_puzzle_record_anchor_item(&record.visual_mood),
|
||||
composition_hooks: map_puzzle_record_anchor_item(&record.composition_hooks),
|
||||
tags_and_forbidden: map_puzzle_record_anchor_item(&record.tags_and_forbidden),
|
||||
}
|
||||
}
|
||||
|
||||
fn map_puzzle_record_anchor_item(
|
||||
record: &spacetime_client::PuzzleAnchorItemRecord,
|
||||
) -> module_puzzle::PuzzleAnchorItem {
|
||||
module_puzzle::PuzzleAnchorItem {
|
||||
key: record.key.clone(),
|
||||
label: record.label.clone(),
|
||||
value: record.value.clone(),
|
||||
status: parse_puzzle_anchor_status(record.status.as_str()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_puzzle_anchor_status(value: &str) -> PuzzleAnchorStatus {
|
||||
match value {
|
||||
"confirmed" => PuzzleAnchorStatus::Confirmed,
|
||||
"locked" => PuzzleAnchorStatus::Locked,
|
||||
"inferred" => PuzzleAnchorStatus::Inferred,
|
||||
_ => PuzzleAnchorStatus::Missing,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::build_puzzle_agent_prompt;
|
||||
|
||||
fn anchor_item(
|
||||
key: &str,
|
||||
label: &str,
|
||||
value: &str,
|
||||
status: &str,
|
||||
) -> spacetime_client::PuzzleAnchorItemRecord {
|
||||
spacetime_client::PuzzleAnchorItemRecord {
|
||||
key: key.to_string(),
|
||||
label: label.to_string(),
|
||||
value: value.to_string(),
|
||||
status: status.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn empty_session_record() -> spacetime_client::PuzzleAgentSessionRecord {
|
||||
spacetime_client::PuzzleAgentSessionRecord {
|
||||
session_id: "puzzle-session-test".to_string(),
|
||||
seed_text: "雨夜猫咪遗迹".to_string(),
|
||||
current_turn: 2,
|
||||
progress_percent: 60,
|
||||
stage: "collecting_anchors".to_string(),
|
||||
anchor_pack: spacetime_client::PuzzleAnchorPackRecord {
|
||||
theme_promise: anchor_item("themePromise", "题材承诺", "雨夜猫咪遗迹", "confirmed"),
|
||||
visual_subject: anchor_item("visualSubject", "画面主体", "", "missing"),
|
||||
visual_mood: anchor_item("visualMood", "视觉气质", "", "missing"),
|
||||
composition_hooks: anchor_item("compositionHooks", "拼图记忆点", "", "missing"),
|
||||
tags_and_forbidden: anchor_item("tagsAndForbidden", "标签与禁忌", "", "missing"),
|
||||
},
|
||||
draft: None,
|
||||
messages: Vec::new(),
|
||||
last_assistant_reply: None,
|
||||
published_profile_id: None,
|
||||
suggested_actions: Vec::new(),
|
||||
result_preview: None,
|
||||
updated_at: "2026-04-24T10:00:00.000Z".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quick_fill_prompt_forbids_follow_up_questions() {
|
||||
let prompt = build_puzzle_agent_prompt(&empty_session_record(), true);
|
||||
|
||||
assert!(prompt.contains("用户刚刚主动要求你自动补充剩余关键字"));
|
||||
assert!(prompt.contains("不要再继续提问"));
|
||||
assert!(prompt.contains("progressPercent 直接输出为 100"));
|
||||
}
|
||||
}
|
||||
86
server-rs/crates/api-server/src/prompt/puzzle/draft.rs
Normal file
86
server-rs/crates/api-server/src/prompt/puzzle/draft.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
/// 拼图作品草稿生成动作的提示词主源。
|
||||
///
|
||||
/// 拼图结果页草稿本体仍由 SpacetimeDB reducer 按表单/锚点确定性编译;
|
||||
/// 这里收口 api-server 在生成草稿前后需要写入 reducer 的表单 seed 文本,
|
||||
/// 以及草稿首图生成时的 prompt 来源选择,避免业务路由直接拼提示词文本。
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub(crate) struct PuzzleFormSeedPromptParts<'a> {
|
||||
pub(crate) title: Option<&'a str>,
|
||||
pub(crate) work_description: Option<&'a str>,
|
||||
pub(crate) picture_description: Option<&'a str>,
|
||||
}
|
||||
|
||||
/// 将填表式拼图输入编译成 SpacetimeDB 可恢复的表单 seed prompt。
|
||||
pub(crate) fn build_puzzle_form_seed_prompt(parts: PuzzleFormSeedPromptParts<'_>) -> String {
|
||||
[
|
||||
("作品名称", normalize_prompt_part(parts.title)),
|
||||
("作品描述", normalize_prompt_part(parts.work_description)),
|
||||
("画面描述", normalize_prompt_part(parts.picture_description)),
|
||||
]
|
||||
.into_iter()
|
||||
.filter_map(|(label, value)| value.map(|value| format!("{label}:{value}")))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
/// 生成作品草稿时,首图 prompt 优先使用玩家当前表单里的画面描述。
|
||||
pub(crate) fn resolve_puzzle_draft_cover_prompt(
|
||||
explicit_prompt: Option<&str>,
|
||||
level_picture_description: &str,
|
||||
draft_summary: &str,
|
||||
) -> String {
|
||||
normalize_prompt_part(explicit_prompt)
|
||||
.or_else(|| normalize_prompt_part(Some(level_picture_description)))
|
||||
.or_else(|| normalize_prompt_part(Some(draft_summary)))
|
||||
.unwrap_or_default()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// 结果页单关重新生成时,优先使用面板当前编辑态 prompt,再回退关卡画面描述。
|
||||
pub(crate) fn resolve_puzzle_level_image_prompt(
|
||||
explicit_prompt: Option<&str>,
|
||||
level_picture_description: &str,
|
||||
) -> String {
|
||||
normalize_prompt_part(explicit_prompt)
|
||||
.or_else(|| normalize_prompt_part(Some(level_picture_description)))
|
||||
.unwrap_or_default()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn normalize_prompt_part(value: Option<&str>) -> Option<&str> {
|
||||
value.map(str::trim).filter(|value| !value.is_empty())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn form_seed_prompt_keeps_only_user_visible_fields() {
|
||||
let prompt = build_puzzle_form_seed_prompt(PuzzleFormSeedPromptParts {
|
||||
title: Some(" 暖灯猫街 "),
|
||||
work_description: Some("雨夜礼物拼图"),
|
||||
picture_description: Some("猫咪在灯牌下回头"),
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
prompt,
|
||||
"作品名称:暖灯猫街\n作品描述:雨夜礼物拼图\n画面描述:猫咪在灯牌下回头"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn draft_cover_prompt_prefers_current_picture_description() {
|
||||
let prompt =
|
||||
resolve_puzzle_draft_cover_prompt(Some(" 当前表单画面 "), "旧关卡画面", "作品简介");
|
||||
|
||||
assert_eq!(prompt, "当前表单画面");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn level_image_prompt_falls_back_to_level_description() {
|
||||
let prompt = resolve_puzzle_level_image_prompt(Some(" "), "关卡画面描述");
|
||||
|
||||
assert_eq!(prompt, "关卡画面描述");
|
||||
}
|
||||
}
|
||||
103
server-rs/crates/api-server/src/prompt/puzzle/image.rs
Normal file
103
server-rs/crates/api-server/src/prompt/puzzle/image.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
/// 拼图图片生成的默认反向提示词。
|
||||
///
|
||||
/// 这里单独收口拼图图片提示词,避免图片生成链路、候选图持久化和 DashScope 请求编排
|
||||
/// 混在同一个脚本里,后续调画风或资产约束时只需要改这一处。
|
||||
pub(crate) const PUZZLE_DEFAULT_NEGATIVE_PROMPT: &str =
|
||||
"低清晰度,低质量,文字水印,畸形构图,过度模糊,重复肢体,画面脏污";
|
||||
|
||||
/// wan2.2 / wan2.1 文生图旧协议的正向 prompt 上限。
|
||||
///
|
||||
/// 中文注释:DashScope 旧 text2image 接口会把超长 prompt 判成请求参数不合法,
|
||||
/// 所以这里先在拼图提示词模块内压缩,保证固定玩法约束不会被用户长描述挤掉。
|
||||
pub(crate) const PUZZLE_TEXT_TO_IMAGE_PROMPT_MAX_CHARS: usize = 500;
|
||||
|
||||
const PUZZLE_IMAGE_LEVEL_NAME_MAX_CHARS: usize = 40;
|
||||
const PUZZLE_IMAGE_PROMPT_FALLBACK: &str = "清晰、有辨识度的拼图画面";
|
||||
|
||||
/// 根据拼图关卡名和百梦主输入构造最终发给图片模型的提示词。
|
||||
pub(crate) fn build_puzzle_image_prompt(level_name: &str, prompt: &str) -> String {
|
||||
let level_name =
|
||||
truncate_puzzle_prompt_segment(level_name.trim(), PUZZLE_IMAGE_LEVEL_NAME_MAX_CHARS);
|
||||
let prompt = prompt.trim();
|
||||
let prompt = if prompt.is_empty() {
|
||||
PUZZLE_IMAGE_PROMPT_FALLBACK
|
||||
} else {
|
||||
prompt
|
||||
};
|
||||
let template_chars = build_puzzle_image_prompt_text(level_name.as_str(), "")
|
||||
.chars()
|
||||
.count();
|
||||
let prompt_max_chars = PUZZLE_TEXT_TO_IMAGE_PROMPT_MAX_CHARS.saturating_sub(template_chars);
|
||||
let prompt = truncate_puzzle_prompt_segment(prompt, prompt_max_chars);
|
||||
let image_prompt = build_puzzle_image_prompt_text(level_name.as_str(), prompt.as_str());
|
||||
|
||||
debug_assert!(
|
||||
image_prompt.chars().count() <= PUZZLE_TEXT_TO_IMAGE_PROMPT_MAX_CHARS,
|
||||
"puzzle image prompt should fit DashScope wan2.2 limit"
|
||||
);
|
||||
image_prompt
|
||||
}
|
||||
|
||||
fn build_puzzle_image_prompt_text(_level_name: &str, prompt: &str) -> String {
|
||||
format!(
|
||||
concat!(
|
||||
"请生成一张高清插画。",
|
||||
"画面主体:{prompt}。",
|
||||
"画面要求:1:1",
|
||||
"主体要清晰集中,前中后景层次明确,局部细节丰富但不要杂乱,",
|
||||
"避免文字、水印、边框和 UI 元素。"
|
||||
),
|
||||
prompt = prompt,
|
||||
)
|
||||
}
|
||||
|
||||
fn truncate_puzzle_prompt_segment(value: &str, max_chars: usize) -> String {
|
||||
if value.chars().count() <= max_chars {
|
||||
return value.to_string();
|
||||
}
|
||||
|
||||
const MARKER: &str = "...";
|
||||
if max_chars <= MARKER.chars().count() {
|
||||
return value.chars().take(max_chars).collect();
|
||||
}
|
||||
|
||||
let keep_chars = max_chars - MARKER.chars().count();
|
||||
format!(
|
||||
"{}{MARKER}",
|
||||
value.chars().take(keep_chars).collect::<String>()
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn build_puzzle_image_prompt_keeps_puzzle_asset_constraints() {
|
||||
let prompt = build_puzzle_image_prompt("雨夜神庙", "猫咪在发光遗迹前寻找线索");
|
||||
|
||||
assert!(prompt.contains("猫咪在发光遗迹前寻找线索"));
|
||||
assert!(prompt.contains("1:1"));
|
||||
assert!(prompt.contains("主体要清晰集中"));
|
||||
assert!(prompt.contains("避免文字、水印、边框和 UI 元素"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_puzzle_image_prompt_trims_long_user_description_for_wan22() {
|
||||
let long_level_name = "雨夜神庙".repeat(20);
|
||||
let long_description =
|
||||
"发光遗迹、猫咪、漂浮碎片、雨水反光、远处灯塔、适合拼图切块。".repeat(50);
|
||||
let prompt = build_puzzle_image_prompt(long_level_name.as_str(), long_description.as_str());
|
||||
|
||||
assert!(prompt.chars().count() <= PUZZLE_TEXT_TO_IMAGE_PROMPT_MAX_CHARS);
|
||||
assert!(prompt.contains("1:1"));
|
||||
assert!(prompt.contains("主体要清晰集中"));
|
||||
assert!(prompt.contains("避免文字、水印、边框和 UI 元素"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_negative_prompt_blocks_text_and_low_quality_assets() {
|
||||
assert!(PUZZLE_DEFAULT_NEGATIVE_PROMPT.contains("低清晰度"));
|
||||
assert!(PUZZLE_DEFAULT_NEGATIVE_PROMPT.contains("文字水印"));
|
||||
}
|
||||
}
|
||||
3
server-rs/crates/api-server/src/prompt/puzzle/mod.rs
Normal file
3
server-rs/crates/api-server/src/prompt/puzzle/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub(crate) mod agent_chat;
|
||||
pub(crate) mod draft;
|
||||
pub(crate) mod image;
|
||||
@@ -1,44 +0,0 @@
|
||||
/// 拼图图片生成的默认反向提示词。
|
||||
///
|
||||
/// 这里单独收口拼图图片提示词,避免图片生成链路、候选图持久化和 DashScope 请求编排
|
||||
/// 混在同一个脚本里,后续调画风或资产约束时只需要改这一处。
|
||||
pub(crate) const PUZZLE_DEFAULT_NEGATIVE_PROMPT: &str =
|
||||
"低清晰度,低质量,文字水印,畸形构图,过度模糊,重复肢体,画面脏污";
|
||||
|
||||
/// 根据拼图关卡名和创作者输入构造最终发给图片模型的提示词。
|
||||
pub(crate) fn build_puzzle_image_prompt(level_name: &str, prompt: &str) -> String {
|
||||
format!(
|
||||
concat!(
|
||||
"请生成一张适合正方形拼图关卡的高清插画。",
|
||||
"关卡名:{level_name}。",
|
||||
"画面主体:{prompt}。",
|
||||
"画面要求:1:1 正方形画布,适配 3x3 或 4x4 拼图切块,",
|
||||
"主体要清晰集中,前中后景层次明确,局部细节丰富但不要杂乱,",
|
||||
"避免文字、水印、边框和 UI 元素。"
|
||||
),
|
||||
level_name = level_name,
|
||||
prompt = prompt,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn build_puzzle_image_prompt_keeps_puzzle_asset_constraints() {
|
||||
let prompt = build_puzzle_image_prompt("雨夜神庙", "猫咪在发光遗迹前寻找线索");
|
||||
|
||||
assert!(prompt.contains("雨夜神庙"));
|
||||
assert!(prompt.contains("猫咪在发光遗迹前寻找线索"));
|
||||
assert!(prompt.contains("正方形拼图关卡"));
|
||||
assert!(prompt.contains("3x3 或 4x4"));
|
||||
assert!(prompt.contains("避免文字、水印、边框和 UI 元素"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_negative_prompt_blocks_text_and_low_quality_assets() {
|
||||
assert!(PUZZLE_DEFAULT_NEGATIVE_PROMPT.contains("低清晰度"));
|
||||
assert!(PUZZLE_DEFAULT_NEGATIVE_PROMPT.contains("文字水印"));
|
||||
}
|
||||
}
|
||||
@@ -240,7 +240,10 @@ JSON 结构:
|
||||
- functionSuggestions 只能从用户提示提供的 functionOptions 中挑选,不要发明 functionId。
|
||||
- functionSuggestions 的 actionText 必须像玩家可点击动作,不暴露 functionId,不写规则说明。
|
||||
- 非敌对聊天 shouldEndChat 必须为 false。
|
||||
- 敌对聊天可以随时 shouldEndChat=true,且敌对 NPC 更偏好在话不投机、被威胁、玩家退出、底线被触碰时结束聊天。"#;
|
||||
- 敌对聊天可以随时 shouldEndChat=true。
|
||||
- 敌对 NPC 感知到玩家负面发言时,例如挑衅、威胁、羞辱、逼问、拒绝退让、直接宣战或强行越界,应倾向立即 shouldEndChat=true。
|
||||
- 敌对 NPC 已聊天轮次达到 4 轮或以上时,本轮结束后会超过 4 轮,应倾向立即 shouldEndChat=true。
|
||||
- shouldEndChat=true 时 terminationReason 使用 hostile_breakoff,suggestions 与 functionSuggestions 可以为空。"#;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NpcChatTurnPromptInput<'a> {
|
||||
@@ -394,6 +397,19 @@ pub(crate) fn build_npc_chat_turn_reply_prompt(payload: &NpcChatTurnPromptInput<
|
||||
} else {
|
||||
None
|
||||
},
|
||||
if is_hostile_model_chat {
|
||||
Some("如果玩家刚才的话被 NPC 感知为负面发言,例如挑衅、威胁、羞辱、逼问、拒绝退让、直接宣战或强行越界,本轮回复应倾向写成最后通牒、驱逐前警告或战斗前狠话。".to_string())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
if is_hostile_model_chat && chatted_count >= 4.0 {
|
||||
Some(format!(
|
||||
"敌对聊天已持续 {} 轮,本轮结束后会超过 4 轮;回复应明显倾向立即收束,像开战前最后一句狠话,而不是继续闲聊。",
|
||||
format_prompt_number(chatted_count)
|
||||
))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
if is_player_exit_turn {
|
||||
Some("玩家正在主动结束这轮聊天。请对这个收束动作作出回应,并留下自然的下一步入口。回复后聊天会结束。".to_string())
|
||||
} else {
|
||||
@@ -474,6 +490,9 @@ pub(crate) fn build_npc_chat_turn_suggestion_prompt(
|
||||
.and_then(|record| read_string(record.get("terminationReason")))
|
||||
.as_deref()
|
||||
== Some("player_exit");
|
||||
let chatted_count = as_record(payload.npc_state)
|
||||
.and_then(|record| read_number(record.get("chattedCount")))
|
||||
.unwrap_or(0.0);
|
||||
let function_options_block = chat_directive
|
||||
.and_then(|record| record.get("functionOptions"))
|
||||
.map(describe_function_options)
|
||||
@@ -498,6 +517,14 @@ pub(crate) fn build_npc_chat_turn_suggestion_prompt(
|
||||
} else {
|
||||
Some("这是非敌对聊天,shouldEndChat 必须为 false。".to_string())
|
||||
},
|
||||
if is_hostile_model_chat {
|
||||
Some(format!(
|
||||
"敌对聊天判定:已聊天轮次为 {}。若玩家刚才的话可被 NPC 感知为负面发言,或已聊天轮次达到 4 轮及以上,本轮应倾向 shouldEndChat=true,并使用 terminationReason=hostile_breakoff。",
|
||||
format_prompt_number(chatted_count)
|
||||
))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
if is_player_exit_turn {
|
||||
Some("玩家已经选择结束聊天,shouldEndChat 必须为 true,terminationReason 必须为 player_exit。".to_string())
|
||||
} else {
|
||||
@@ -526,6 +553,20 @@ pub(crate) fn build_deterministic_npc_reply(
|
||||
format!("{npc_name}听完你的话,回应道:“{player_message}。我明白你的意思,我们继续说。”")
|
||||
}
|
||||
|
||||
pub(crate) fn build_deterministic_hostile_breakoff_reply(
|
||||
npc_name: &str,
|
||||
player_message: &str,
|
||||
) -> String {
|
||||
// 中文注释:当模型不可用而敌对聊天必须中止时,兜底文案也保持“战斗前狠话”的语气。
|
||||
let player_signal = player_message.trim();
|
||||
if player_signal.is_empty() {
|
||||
return format!("{npc_name}冷声说道:“话已经够多了。再往前一步,就别指望还能全身而退。”");
|
||||
}
|
||||
format!(
|
||||
"{npc_name}冷声说道:“{player_signal}?话已经够多了。再往前一步,就别指望还能全身而退。”"
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn build_character_chat_reply_fallback(
|
||||
target_character: &Value,
|
||||
player_message: &str,
|
||||
@@ -1066,3 +1107,55 @@ fn format_prompt_number(value: f64) -> String {
|
||||
value.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn hostile_prompt_input(npc_state: Value) -> NpcChatTurnPromptInput<'static> {
|
||||
NpcChatTurnPromptInput {
|
||||
world_type: "CUSTOM",
|
||||
character: Box::leak(Box::new(Value::Null)),
|
||||
encounter: Box::leak(Box::new(Value::Null)),
|
||||
monsters: &[],
|
||||
history: &[],
|
||||
context: Box::leak(Box::new(Value::Null)),
|
||||
conversation_history: &[],
|
||||
dialogue: &[],
|
||||
combat_context: None,
|
||||
player_message: "少废话,让开。",
|
||||
npc_state: Box::leak(Box::new(npc_state)),
|
||||
npc_initiates_conversation: false,
|
||||
chat_directive: Some(Box::leak(Box::new(json!({
|
||||
"terminationMode": "hostile_model",
|
||||
"isHostileChat": true,
|
||||
})))),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_reply_prompt_mentions_final_threat_after_four_turns() {
|
||||
let input = hostile_prompt_input(json!({
|
||||
"affinity": -12,
|
||||
"chattedCount": 4,
|
||||
}));
|
||||
let prompt = build_npc_chat_turn_reply_prompt(&input);
|
||||
|
||||
assert!(prompt.contains("已聊天轮次:4"));
|
||||
assert!(prompt.contains("战斗前狠话"));
|
||||
assert!(prompt.contains("本轮结束后会超过 4 轮"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_suggestion_prompt_mentions_should_end_chat_signals() {
|
||||
let input = hostile_prompt_input(json!({
|
||||
"affinity": -12,
|
||||
"chattedCount": 4,
|
||||
}));
|
||||
let prompt = build_npc_chat_turn_suggestion_prompt(&input, "再往前一步,就别想回头。");
|
||||
|
||||
assert!(prompt.contains("shouldEndChat=true"));
|
||||
assert!(prompt.contains("terminationReason=hostile_breakoff"));
|
||||
assert!(prompt.contains("已聊天轮次为 4"));
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,18 +1,16 @@
|
||||
use module_puzzle::{PuzzleAgentStage, PuzzleAnchorPack, PuzzleAnchorStatus, empty_anchor_pack};
|
||||
use platform_llm::LlmClient;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value as JsonValue, json};
|
||||
use spacetime_client::{
|
||||
PuzzleAgentMessageFinalizeRecordInput, PuzzleAgentMessageRecord, PuzzleAgentSessionRecord,
|
||||
};
|
||||
use serde_json::Value as JsonValue;
|
||||
use spacetime_client::{PuzzleAgentMessageFinalizeRecordInput, PuzzleAgentSessionRecord};
|
||||
|
||||
use crate::creation_agent_anchor_templates::{
|
||||
get_creation_agent_anchor_template, render_anchor_question_block,
|
||||
};
|
||||
use crate::creation_agent_chat::render_quick_fill_extra_rules;
|
||||
use crate::creation_agent_llm_turn::{
|
||||
CreationAgentLlmTurnErrorMessages, stream_creation_agent_json_turn,
|
||||
};
|
||||
use crate::prompt::puzzle::agent_chat::{
|
||||
PUZZLE_AGENT_JSON_TURN_USER_PROMPT, PUZZLE_AGENT_SYSTEM_PROMPT, build_puzzle_agent_prompt,
|
||||
serialize_puzzle_record_anchor_pack,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct PuzzleAgentTurnRequest<'a> {
|
||||
@@ -60,63 +58,6 @@ struct PuzzleAgentModelOutput {
|
||||
next_anchor_pack: PuzzleAnchorPack,
|
||||
}
|
||||
|
||||
const PUZZLE_AGENT_SYSTEM_PROMPT: &str = r#"你是一个负责和创作者共创拼图画面的中文创意策划。
|
||||
|
||||
你要帮助用户把一句灵感逐步收束成可以发布成拼图关卡的视觉方案。
|
||||
|
||||
你必须同时输出:
|
||||
1. 一段直接发给用户的中文回复 replyText
|
||||
2. 当前进度 progressPercent
|
||||
3. 下一轮完整可用的 nextAnchorPack
|
||||
|
||||
硬约束:
|
||||
1. 只能输出 JSON,不能输出代码块或解释
|
||||
2. nextAnchorPack 必须是完整对象,不能只输出 patch
|
||||
3. replyText 必须是自然中文,不能提“字段”“锚点”“结构”“JSON”等内部词
|
||||
4. replyText 一次最多推进一个最关键问题
|
||||
5. 如果用户已经给出明确方向,就优先吸收和收束,不要机械反问
|
||||
6. progressPercent 范围只能是 0 到 100
|
||||
7. status 只能使用 missing / inferred / confirmed / locked
|
||||
"#;
|
||||
|
||||
const PUZZLE_AGENT_OUTPUT_CONTRACT: &str = r#"请严格按以下 JSON 输出,不要输出其他文字:
|
||||
{
|
||||
"replyText": "",
|
||||
"progressPercent": 0,
|
||||
"nextAnchorPack": {
|
||||
"themePromise": {
|
||||
"key": "themePromise",
|
||||
"label": "题材承诺",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
},
|
||||
"visualSubject": {
|
||||
"key": "visualSubject",
|
||||
"label": "画面主体",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
},
|
||||
"visualMood": {
|
||||
"key": "visualMood",
|
||||
"label": "视觉气质",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
},
|
||||
"compositionHooks": {
|
||||
"key": "compositionHooks",
|
||||
"label": "拼图记忆点",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
},
|
||||
"tagsAndForbidden": {
|
||||
"key": "tagsAndForbidden",
|
||||
"label": "标签与禁忌",
|
||||
"value": "",
|
||||
"status": "missing"
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
pub(crate) async fn run_puzzle_agent_turn<F>(
|
||||
request: PuzzleAgentTurnRequest<'_>,
|
||||
on_reply_update: F,
|
||||
@@ -128,7 +69,7 @@ where
|
||||
let turn_output = stream_creation_agent_json_turn(
|
||||
request.llm_client,
|
||||
format!("{PUZZLE_AGENT_SYSTEM_PROMPT}\n\n{prompt}"),
|
||||
"请按约定输出这一轮的 JSON。",
|
||||
PUZZLE_AGENT_JSON_TURN_USER_PROMPT,
|
||||
request.enable_web_search,
|
||||
CreationAgentLlmTurnErrorMessages {
|
||||
model_unavailable: "当前模型不可用,请稍后重试。",
|
||||
@@ -185,10 +126,6 @@ pub(crate) fn build_failed_finalize_record_input(
|
||||
error_message: String,
|
||||
updated_at_micros: i64,
|
||||
) -> PuzzleAgentMessageFinalizeRecordInput {
|
||||
let anchor_pack_json = serde_json::to_string(&map_record_anchor_pack(&session.anchor_pack))
|
||||
.unwrap_or_else(|_| {
|
||||
serde_json::to_string(&empty_anchor_pack()).unwrap_or_else(|_| "{}".to_string())
|
||||
});
|
||||
PuzzleAgentMessageFinalizeRecordInput {
|
||||
session_id,
|
||||
owner_user_id,
|
||||
@@ -196,61 +133,12 @@ pub(crate) fn build_failed_finalize_record_input(
|
||||
assistant_reply_text: None,
|
||||
stage: session.stage.clone(),
|
||||
progress_percent: session.progress_percent,
|
||||
anchor_pack_json,
|
||||
anchor_pack_json: serialize_puzzle_record_anchor_pack(&session.anchor_pack),
|
||||
error_message: Some(error_message),
|
||||
updated_at_micros,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_puzzle_agent_prompt(
|
||||
session: &PuzzleAgentSessionRecord,
|
||||
quick_fill_requested: bool,
|
||||
) -> String {
|
||||
let anchor_question_block = get_creation_agent_anchor_template("puzzle")
|
||||
.map(render_anchor_question_block)
|
||||
.unwrap_or_else(|| "模板目标:收束成可以发布为拼图关卡的视觉方案。".to_string());
|
||||
let quick_fill_rules = if quick_fill_requested {
|
||||
format!(
|
||||
"\n\n{}",
|
||||
render_quick_fill_extra_rules(
|
||||
"当前题材方向里的拼图关键词",
|
||||
"不要要求用户再提供素材、风格或禁忌",
|
||||
"输出完整 nextAnchorPack,直接补齐 value 为空或 status 为 missing 的项",
|
||||
"生成结果页",
|
||||
)
|
||||
)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
format!(
|
||||
"{anchor_question_block}{quick_fill_rules}\n\n当前是第 {turn} 轮,当前进度 {progress}% 。\n\n是否要求自动补充剩余关键字:{quick_fill_requested_text}\n\n当前 anchor pack:\n{anchor_pack}\n\n最近聊天记录:\n{chat_history}\n\n{contract}",
|
||||
anchor_question_block = anchor_question_block,
|
||||
quick_fill_rules = quick_fill_rules,
|
||||
turn = session.current_turn.saturating_add(1),
|
||||
progress = session.progress_percent,
|
||||
quick_fill_requested_text = if quick_fill_requested { "是" } else { "否" },
|
||||
anchor_pack = serde_json::to_string_pretty(&map_record_anchor_pack(&session.anchor_pack))
|
||||
.unwrap_or_else(|_| "{}".to_string()),
|
||||
chat_history =
|
||||
serde_json::to_string_pretty(&build_chat_history(session.messages.as_slice()))
|
||||
.unwrap_or_else(|_| "[]".to_string()),
|
||||
contract = PUZZLE_AGENT_OUTPUT_CONTRACT,
|
||||
)
|
||||
}
|
||||
|
||||
fn build_chat_history(messages: &[PuzzleAgentMessageRecord]) -> Vec<JsonValue> {
|
||||
messages
|
||||
.iter()
|
||||
.map(|message| {
|
||||
json!({
|
||||
"role": message.role,
|
||||
"kind": message.kind,
|
||||
"content": message.text,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn parse_model_output(parsed: &JsonValue) -> Result<PuzzleAgentModelOutput, PuzzleAgentTurnError> {
|
||||
let reply_text = parsed
|
||||
.get("replyText")
|
||||
@@ -348,27 +236,6 @@ fn resolve_puzzle_agent_stage(progress_percent: u32) -> PuzzleAgentStage {
|
||||
}
|
||||
}
|
||||
|
||||
fn map_record_anchor_pack(record: &spacetime_client::PuzzleAnchorPackRecord) -> PuzzleAnchorPack {
|
||||
PuzzleAnchorPack {
|
||||
theme_promise: map_record_anchor_item(&record.theme_promise),
|
||||
visual_subject: map_record_anchor_item(&record.visual_subject),
|
||||
visual_mood: map_record_anchor_item(&record.visual_mood),
|
||||
composition_hooks: map_record_anchor_item(&record.composition_hooks),
|
||||
tags_and_forbidden: map_record_anchor_item(&record.tags_and_forbidden),
|
||||
}
|
||||
}
|
||||
|
||||
fn map_record_anchor_item(
|
||||
record: &spacetime_client::PuzzleAnchorItemRecord,
|
||||
) -> module_puzzle::PuzzleAnchorItem {
|
||||
module_puzzle::PuzzleAnchorItem {
|
||||
key: record.key.clone(),
|
||||
label: record.label.clone(),
|
||||
value: record.value.clone(),
|
||||
status: parse_anchor_status(record.status.as_str()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_anchor_status(value: &str) -> PuzzleAnchorStatus {
|
||||
match value {
|
||||
"confirmed" => PuzzleAnchorStatus::Confirmed,
|
||||
@@ -383,57 +250,9 @@ mod tests {
|
||||
use module_puzzle::PuzzleAnchorStatus;
|
||||
use serde_json::json;
|
||||
|
||||
use super::{build_puzzle_agent_prompt, parse_model_output};
|
||||
use super::parse_model_output;
|
||||
use crate::creation_agent_llm_turn::extract_reply_text_from_partial_json;
|
||||
|
||||
fn empty_session_record() -> spacetime_client::PuzzleAgentSessionRecord {
|
||||
spacetime_client::PuzzleAgentSessionRecord {
|
||||
session_id: "puzzle-session-test".to_string(),
|
||||
current_turn: 2,
|
||||
progress_percent: 60,
|
||||
stage: "collecting_anchors".to_string(),
|
||||
anchor_pack: spacetime_client::PuzzleAnchorPackRecord {
|
||||
theme_promise: spacetime_client::PuzzleAnchorItemRecord {
|
||||
key: "themePromise".to_string(),
|
||||
label: "题材承诺".to_string(),
|
||||
value: "雨夜猫咪遗迹".to_string(),
|
||||
status: "confirmed".to_string(),
|
||||
},
|
||||
visual_subject: spacetime_client::PuzzleAnchorItemRecord {
|
||||
key: "visualSubject".to_string(),
|
||||
label: "画面主体".to_string(),
|
||||
value: String::new(),
|
||||
status: "missing".to_string(),
|
||||
},
|
||||
visual_mood: spacetime_client::PuzzleAnchorItemRecord {
|
||||
key: "visualMood".to_string(),
|
||||
label: "视觉气质".to_string(),
|
||||
value: String::new(),
|
||||
status: "missing".to_string(),
|
||||
},
|
||||
composition_hooks: spacetime_client::PuzzleAnchorItemRecord {
|
||||
key: "compositionHooks".to_string(),
|
||||
label: "拼图记忆点".to_string(),
|
||||
value: String::new(),
|
||||
status: "missing".to_string(),
|
||||
},
|
||||
tags_and_forbidden: spacetime_client::PuzzleAnchorItemRecord {
|
||||
key: "tagsAndForbidden".to_string(),
|
||||
label: "标签与禁忌".to_string(),
|
||||
value: String::new(),
|
||||
status: "missing".to_string(),
|
||||
},
|
||||
},
|
||||
draft: None,
|
||||
messages: Vec::new(),
|
||||
last_assistant_reply: None,
|
||||
published_profile_id: None,
|
||||
suggested_actions: Vec::new(),
|
||||
result_preview: None,
|
||||
updated_at: "2026-04-24T10:00:00.000Z".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_reply_text_from_partial_json_preserves_chinese_characters() {
|
||||
let partial_json = r#"{"replyText":"夜雨猫咪遗迹","progressPercent":42"#;
|
||||
@@ -498,13 +317,4 @@ mod tests {
|
||||
"雨夜、猫咪、神庙遗迹;禁止文字水印"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quick_fill_prompt_forbids_follow_up_questions() {
|
||||
let prompt = build_puzzle_agent_prompt(&empty_session_record(), true);
|
||||
|
||||
assert!(prompt.contains("用户刚刚主动要求你自动补充剩余关键字"));
|
||||
assert!(prompt.contains("不要再继续提问"));
|
||||
assert!(prompt.contains("progressPercent 直接输出为 100"));
|
||||
}
|
||||
}
|
||||
|
||||
30
server-rs/crates/api-server/src/registration_reward.rs
Normal file
30
server-rs/crates/api-server/src/registration_reward.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
#[cfg(not(test))]
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{request_context::RequestContext, state::AppState};
|
||||
|
||||
pub async fn grant_new_user_registration_wallet_reward(
|
||||
state: &AppState,
|
||||
request_context: &RequestContext,
|
||||
user_id: &str,
|
||||
) {
|
||||
#[cfg(test)]
|
||||
{
|
||||
let _ = (state, request_context, user_id);
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
if let Err(error) = state
|
||||
.spacetime_client()
|
||||
.grant_new_user_registration_wallet_reward(user_id.to_string())
|
||||
.await
|
||||
{
|
||||
warn!(
|
||||
request_id = request_context.request_id(),
|
||||
operation = request_context.operation(),
|
||||
user_id = user_id,
|
||||
error = %error,
|
||||
"新用户注册光点赠送失败,注册流程继续"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use axum::{
|
||||
use platform_llm::{LlmMessage, LlmTextRequest};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
use shared_contracts::runtime_story::RuntimeStorySnapshotPayload;
|
||||
use std::convert::Infallible;
|
||||
|
||||
use module_runtime_story::{
|
||||
@@ -21,12 +22,13 @@ use module_runtime_story::{
|
||||
use crate::{
|
||||
auth::AuthenticatedAccessToken,
|
||||
http_error::AppError,
|
||||
llm_model_routing::RPG_STORY_LLM_MODEL,
|
||||
prompt::runtime_chat::{
|
||||
NPC_CHAT_TURN_REPLY_SYSTEM_PROMPT, NPC_CHAT_TURN_SUGGESTION_SYSTEM_PROMPT,
|
||||
NpcChatTurnPromptInput, build_deterministic_chat_suggestions,
|
||||
build_deterministic_npc_reply, build_fallback_function_suggestions,
|
||||
build_fallback_npc_chat_suggestions, build_npc_chat_turn_reply_prompt,
|
||||
build_npc_chat_turn_suggestion_prompt,
|
||||
build_deterministic_hostile_breakoff_reply, build_deterministic_npc_reply,
|
||||
build_fallback_function_suggestions, build_fallback_npc_chat_suggestions,
|
||||
build_npc_chat_turn_reply_prompt, build_npc_chat_turn_suggestion_prompt,
|
||||
},
|
||||
request_context::RequestContext,
|
||||
state::AppState,
|
||||
@@ -38,6 +40,8 @@ pub struct NpcChatTurnRequest {
|
||||
#[serde(default)]
|
||||
session_id: Option<String>,
|
||||
#[serde(default)]
|
||||
snapshot: Option<RuntimeStorySnapshotPayload>,
|
||||
#[serde(default)]
|
||||
world_type: String,
|
||||
#[serde(default)]
|
||||
character: Option<Value>,
|
||||
@@ -133,16 +137,26 @@ pub async fn stream_runtime_npc_chat_turn(
|
||||
let (npc_reply, suggestions, function_suggestions, force_exit) = match llm_result {
|
||||
Some(result) => result,
|
||||
None => {
|
||||
let npc_reply = build_deterministic_npc_reply(
|
||||
npc_name.as_str(),
|
||||
player_message.as_str(),
|
||||
payload.npc_initiates_conversation,
|
||||
);
|
||||
let force_exit = should_force_chat_exit(payload.chat_directive.as_ref())
|
||||
|| should_hostile_chat_breakoff_deterministically(
|
||||
let deterministic_hostile_breakoff =
|
||||
should_hostile_chat_breakoff_deterministically(
|
||||
player_message.as_str(),
|
||||
payload.chat_directive.as_ref(),
|
||||
Some(&payload.npc_state),
|
||||
);
|
||||
let force_exit = should_force_chat_exit(payload.chat_directive.as_ref())
|
||||
|| deterministic_hostile_breakoff;
|
||||
let npc_reply = if deterministic_hostile_breakoff {
|
||||
build_deterministic_hostile_breakoff_reply(
|
||||
npc_name.as_str(),
|
||||
player_message.as_str(),
|
||||
)
|
||||
} else {
|
||||
build_deterministic_npc_reply(
|
||||
npc_name.as_str(),
|
||||
player_message.as_str(),
|
||||
payload.npc_initiates_conversation,
|
||||
)
|
||||
};
|
||||
let suggestions = if force_exit {
|
||||
Vec::new()
|
||||
} else {
|
||||
@@ -224,6 +238,7 @@ where
|
||||
]);
|
||||
reply_request.max_tokens = Some(700);
|
||||
reply_request.enable_web_search = state.config.rpg_llm_web_search_enabled;
|
||||
reply_request.model = Some(RPG_STORY_LLM_MODEL.to_string());
|
||||
|
||||
let reply_response = llm_client
|
||||
.stream_text(reply_request, |delta| {
|
||||
@@ -251,6 +266,7 @@ where
|
||||
]);
|
||||
suggestion_request.max_tokens = Some(200);
|
||||
suggestion_request.enable_web_search = state.config.rpg_llm_web_search_enabled;
|
||||
suggestion_request.model = Some(RPG_STORY_LLM_MODEL.to_string());
|
||||
let suggestion_text = llm_client
|
||||
.request_text(suggestion_request)
|
||||
.await
|
||||
@@ -266,6 +282,7 @@ where
|
||||
|| should_hostile_chat_breakoff_deterministically(
|
||||
payload.player_message.as_str(),
|
||||
payload.chat_directive.as_ref(),
|
||||
Some(&payload.npc_state),
|
||||
);
|
||||
|
||||
if force_exit {
|
||||
@@ -292,6 +309,16 @@ async fn hydrate_npc_chat_turn_request_from_session(
|
||||
// 中文注释:旧调用没有 sessionId 时继续使用请求体字段;正式运行态由后端快照投影上下文。
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
if let Some(game_state) = resolve_request_snapshot_game_state(
|
||||
request_context,
|
||||
session_id.as_str(),
|
||||
payload.snapshot.as_ref(),
|
||||
)? {
|
||||
apply_npc_chat_turn_game_state(payload, game_state);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let record = state
|
||||
.get_runtime_snapshot_record(user_id)
|
||||
.await
|
||||
@@ -328,6 +355,49 @@ async fn hydrate_npc_chat_turn_request_from_session(
|
||||
));
|
||||
}
|
||||
|
||||
apply_npc_chat_turn_game_state(payload, game_state);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn resolve_request_snapshot_game_state(
|
||||
request_context: &RequestContext,
|
||||
session_id: &str,
|
||||
snapshot: Option<&RuntimeStorySnapshotPayload>,
|
||||
) -> Result<Option<Value>, Response> {
|
||||
let Some(snapshot) = snapshot else {
|
||||
return Ok(None);
|
||||
};
|
||||
if !snapshot.game_state.is_object() {
|
||||
return Err(runtime_chat_error_response(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "runtime-chat",
|
||||
"field": "snapshot.gameState",
|
||||
"message": "snapshot.gameState 必须是 JSON object",
|
||||
})),
|
||||
));
|
||||
}
|
||||
|
||||
let snapshot_session_id =
|
||||
read_runtime_session_id(&snapshot.game_state).unwrap_or_else(|| session_id.to_string());
|
||||
if snapshot_session_id != session_id {
|
||||
return Err(runtime_chat_error_response(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::CONFLICT).with_details(json!({
|
||||
"provider": "runtime-chat",
|
||||
"message": "请求的运行时会话与服务端快照不一致,请重新进入游戏",
|
||||
"sessionId": session_id,
|
||||
"snapshotSessionId": snapshot_session_id,
|
||||
})),
|
||||
));
|
||||
}
|
||||
|
||||
// 中文注释:预览/测试/禁存运行态只把请求 snapshot 用于本轮 prompt 投影,不写入正式存档。
|
||||
Ok(Some(snapshot.game_state.clone()))
|
||||
}
|
||||
|
||||
fn apply_npc_chat_turn_game_state(payload: &mut NpcChatTurnRequest, game_state: Value) {
|
||||
payload.world_type = current_world_type(&game_state).unwrap_or_default();
|
||||
payload.character = read_field(&game_state, "playerCharacter").cloned();
|
||||
payload.player = payload.character.clone();
|
||||
@@ -361,8 +431,6 @@ async fn hydrate_npc_chat_turn_request_from_session(
|
||||
object.insert("state".to_string(), game_state);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn resolve_current_request_npc_state(game_state: &Value) -> Option<Value> {
|
||||
@@ -561,6 +629,7 @@ fn is_hostile_model_chat(chat_directive: Option<&Value>) -> bool {
|
||||
fn should_hostile_chat_breakoff_deterministically(
|
||||
player_message: &str,
|
||||
chat_directive: Option<&Value>,
|
||||
npc_state: Option<&Value>,
|
||||
) -> bool {
|
||||
if !is_hostile_model_chat(chat_directive) {
|
||||
return false;
|
||||
@@ -574,6 +643,14 @@ fn should_hostile_chat_breakoff_deterministically(
|
||||
return true;
|
||||
}
|
||||
|
||||
// 中文注释:模型建议不可用时,后端兜底仍按敌对聊天口径避免负面挑衅被拖成闲聊。
|
||||
if npc_state
|
||||
.and_then(|state| read_number_field(state, "chattedCount"))
|
||||
.is_some_and(|chatted_count| chatted_count >= 4.0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
let hostile_break_words = [
|
||||
"动手",
|
||||
"开战",
|
||||
@@ -583,6 +660,18 @@ fn should_hostile_chat_breakoff_deterministically(
|
||||
"闭嘴",
|
||||
"少废话",
|
||||
"别挡路",
|
||||
"废话",
|
||||
"威胁",
|
||||
"找死",
|
||||
"送死",
|
||||
"住口",
|
||||
"让开",
|
||||
"滚开",
|
||||
"不退",
|
||||
"不会退",
|
||||
"别装",
|
||||
"骗子",
|
||||
"叛徒",
|
||||
];
|
||||
count_keyword_matches(player_message, &hostile_break_words) > 0
|
||||
}
|
||||
@@ -709,6 +798,8 @@ fn runtime_chat_error_response(request_context: &RequestContext, error: AppError
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{config::AppConfig, request_context::RequestContext, state::AppState};
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn npc_chat_affinity_delta_keeps_node_keyword_rules() {
|
||||
@@ -752,4 +843,174 @@ mod tests {
|
||||
vec!["继续问线索", "表明立场", "拉近关系"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_chat_breakoff_fallback_triggers_on_negative_words() {
|
||||
let chat_directive = json!({
|
||||
"terminationMode": "hostile_model",
|
||||
"isHostileChat": true,
|
||||
});
|
||||
let npc_state = json!({ "chattedCount": 1 });
|
||||
|
||||
assert!(should_hostile_chat_breakoff_deterministically(
|
||||
"少废话,让开,不然现在就动手。",
|
||||
Some(&chat_directive),
|
||||
Some(&npc_state),
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_chat_breakoff_fallback_triggers_after_four_turns() {
|
||||
let chat_directive = json!({
|
||||
"terminationMode": "hostile_model",
|
||||
"isHostileChat": true,
|
||||
});
|
||||
let npc_state = json!({ "chattedCount": 4 });
|
||||
|
||||
assert!(should_hostile_chat_breakoff_deterministically(
|
||||
"我还想再问一个问题。",
|
||||
Some(&chat_directive),
|
||||
Some(&npc_state),
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_chat_breakoff_fallback_ignores_non_hostile_chat() {
|
||||
let chat_directive = json!({
|
||||
"terminationMode": "none",
|
||||
"isHostileChat": false,
|
||||
});
|
||||
let npc_state = json!({ "chattedCount": 6 });
|
||||
|
||||
assert!(!should_hostile_chat_breakoff_deterministically(
|
||||
"少废话,让开。",
|
||||
Some(&chat_directive),
|
||||
Some(&npc_state),
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn npc_chat_turn_prefers_request_snapshot_over_persisted_session() {
|
||||
let state = AppState::new(AppConfig::default()).expect("state should build");
|
||||
state
|
||||
.put_runtime_snapshot_record(
|
||||
"user_00000001".to_string(),
|
||||
1,
|
||||
"adventure".to_string(),
|
||||
json!({
|
||||
"worldType": "WUXIA",
|
||||
"runtimeSessionId": "runtime-main",
|
||||
"playerCharacter": { "id": "hero-main", "name": "旧存档" },
|
||||
"currentEncounter": { "id": "npc-main", "npcName": "旧 NPC" },
|
||||
"sceneHostileNpcs": [],
|
||||
"storyHistory": [],
|
||||
}),
|
||||
None,
|
||||
1,
|
||||
)
|
||||
.await
|
||||
.expect("snapshot should seed");
|
||||
let request_context = test_request_context();
|
||||
let mut payload = test_npc_chat_turn_payload(
|
||||
"runtime-preview",
|
||||
Some(json!({
|
||||
"worldType": "CUSTOM",
|
||||
"runtimeSessionId": "runtime-preview",
|
||||
"runtimePersistenceDisabled": true,
|
||||
"playerCharacter": { "id": "hero-preview", "name": "临时角色" },
|
||||
"currentEncounter": { "id": "npc-preview", "npcName": "临时 NPC" },
|
||||
"sceneHostileNpcs": [{ "id": "monster-preview", "name": "雾影" }],
|
||||
"storyHistory": [{ "text": "临时故事" }],
|
||||
"npcStates": {
|
||||
"npc-preview": {
|
||||
"affinity": 12,
|
||||
"helpUsed": false,
|
||||
"chattedCount": 2,
|
||||
"giftsGiven": 0,
|
||||
"recruited": false
|
||||
}
|
||||
}
|
||||
})),
|
||||
);
|
||||
|
||||
hydrate_npc_chat_turn_request_from_session(
|
||||
&state,
|
||||
&request_context,
|
||||
"user_00000001".to_string(),
|
||||
&mut payload,
|
||||
)
|
||||
.await
|
||||
.expect("request snapshot should hydrate");
|
||||
|
||||
assert_eq!(payload.world_type, "CUSTOM");
|
||||
assert_eq!(
|
||||
read_optional_string_field(&payload.encounter, "npcName").as_deref(),
|
||||
Some("临时 NPC")
|
||||
);
|
||||
assert_eq!(payload.monsters.len(), 1);
|
||||
assert_eq!(read_i32_field(&payload.npc_state, "affinity"), Some(12));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn npc_chat_turn_rejects_request_snapshot_session_mismatch() {
|
||||
let state = AppState::new(AppConfig::default()).expect("state should build");
|
||||
let request_context = test_request_context();
|
||||
let mut payload = test_npc_chat_turn_payload(
|
||||
"runtime-preview",
|
||||
Some(json!({
|
||||
"worldType": "WUXIA",
|
||||
"runtimeSessionId": "runtime-other",
|
||||
})),
|
||||
);
|
||||
|
||||
let response = hydrate_npc_chat_turn_request_from_session(
|
||||
&state,
|
||||
&request_context,
|
||||
"user_00000001".to_string(),
|
||||
&mut payload,
|
||||
)
|
||||
.await
|
||||
.expect_err("snapshot session mismatch should fail");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::CONFLICT);
|
||||
}
|
||||
|
||||
fn test_request_context() -> RequestContext {
|
||||
RequestContext::new(
|
||||
"runtime-chat-test".to_string(),
|
||||
"POST /api/runtime/chat/npc/turn/stream".to_string(),
|
||||
Duration::ZERO,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
fn test_npc_chat_turn_payload(
|
||||
session_id: &str,
|
||||
game_state: Option<Value>,
|
||||
) -> NpcChatTurnRequest {
|
||||
NpcChatTurnRequest {
|
||||
session_id: Some(session_id.to_string()),
|
||||
snapshot: game_state.map(|game_state| RuntimeStorySnapshotPayload {
|
||||
saved_at: None,
|
||||
bottom_tab: "adventure".to_string(),
|
||||
game_state,
|
||||
current_story: None,
|
||||
}),
|
||||
world_type: String::new(),
|
||||
character: None,
|
||||
player: None,
|
||||
encounter: json!({ "id": "npc-request", "npcName": "请求 NPC" }),
|
||||
monsters: Vec::new(),
|
||||
history: Vec::new(),
|
||||
context: Value::Null,
|
||||
conversation_history: Vec::new(),
|
||||
dialogue: Vec::new(),
|
||||
combat_context: None,
|
||||
player_message: "你刚才看见了什么?".to_string(),
|
||||
npc_state: Value::Null,
|
||||
npc_initiates_conversation: false,
|
||||
quest_offer_context: None,
|
||||
chat_directive: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,11 +10,13 @@ use axum::{
|
||||
use platform_llm::{LlmMessage, LlmTextRequest};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
use shared_contracts::runtime_story::RuntimeStorySnapshotPayload;
|
||||
use std::convert::Infallible;
|
||||
|
||||
use crate::{
|
||||
api_response::json_success_body, auth::AuthenticatedAccessToken, http_error::AppError,
|
||||
prompt::runtime_chat::*, request_context::RequestContext, state::AppState,
|
||||
llm_model_routing::RPG_STORY_LLM_MODEL, prompt::runtime_chat::*,
|
||||
request_context::RequestContext, state::AppState,
|
||||
};
|
||||
use module_runtime_story::{
|
||||
RuntimeStoryPromptContextExtras, build_runtime_story_prompt_context, current_world_type,
|
||||
@@ -27,6 +29,8 @@ pub struct RuntimeCharacterChatRequest {
|
||||
#[serde(default)]
|
||||
session_id: Option<String>,
|
||||
#[serde(default)]
|
||||
snapshot: Option<RuntimeStorySnapshotPayload>,
|
||||
#[serde(default)]
|
||||
world_type: String,
|
||||
#[serde(default)]
|
||||
player_character: Value,
|
||||
@@ -54,6 +58,8 @@ pub struct RuntimeNpcDialogueRequest {
|
||||
#[serde(default)]
|
||||
session_id: Option<String>,
|
||||
#[serde(default)]
|
||||
snapshot: Option<RuntimeStorySnapshotPayload>,
|
||||
#[serde(default)]
|
||||
world_type: String,
|
||||
#[serde(default)]
|
||||
character: Value,
|
||||
@@ -77,6 +83,8 @@ pub struct RuntimeNpcRecruitDialogueRequest {
|
||||
#[serde(default)]
|
||||
session_id: Option<String>,
|
||||
#[serde(default)]
|
||||
snapshot: Option<RuntimeStorySnapshotPayload>,
|
||||
#[serde(default)]
|
||||
world_type: String,
|
||||
#[serde(default)]
|
||||
character: Value,
|
||||
@@ -346,6 +354,7 @@ async fn hydrate_character_chat_request_from_session(
|
||||
request_context,
|
||||
user_id,
|
||||
payload.session_id.as_deref(),
|
||||
payload.snapshot.as_ref(),
|
||||
)
|
||||
.await?
|
||||
else {
|
||||
@@ -382,6 +391,7 @@ async fn hydrate_npc_dialogue_request_from_session(
|
||||
request_context,
|
||||
user_id,
|
||||
payload.session_id.as_deref(),
|
||||
payload.snapshot.as_ref(),
|
||||
)
|
||||
.await?
|
||||
else {
|
||||
@@ -430,6 +440,7 @@ async fn hydrate_npc_recruit_request_from_session(
|
||||
request_context,
|
||||
user_id,
|
||||
payload.session_id.as_deref(),
|
||||
payload.snapshot.as_ref(),
|
||||
)
|
||||
.await?
|
||||
else {
|
||||
@@ -472,11 +483,19 @@ async fn resolve_runtime_chat_game_state(
|
||||
request_context: &RequestContext,
|
||||
user_id: String,
|
||||
session_id: Option<&str>,
|
||||
snapshot: Option<&RuntimeStorySnapshotPayload>,
|
||||
) -> Result<Option<Value>, Response> {
|
||||
let Some(session_id) = session_id.and_then(normalize_required_string) else {
|
||||
// 中文注释:未携带 sessionId 的旧调用仅保留兼容,后续正式运行态应全部走后端快照。
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
if let Some(game_state) =
|
||||
resolve_request_snapshot_game_state(request_context, session_id.as_str(), snapshot)?
|
||||
{
|
||||
return Ok(Some(game_state));
|
||||
}
|
||||
|
||||
let record = state
|
||||
.get_runtime_snapshot_record(user_id)
|
||||
.await
|
||||
@@ -516,6 +535,43 @@ async fn resolve_runtime_chat_game_state(
|
||||
Ok(Some(game_state))
|
||||
}
|
||||
|
||||
fn resolve_request_snapshot_game_state(
|
||||
request_context: &RequestContext,
|
||||
session_id: &str,
|
||||
snapshot: Option<&RuntimeStorySnapshotPayload>,
|
||||
) -> Result<Option<Value>, Response> {
|
||||
let Some(snapshot) = snapshot else {
|
||||
return Ok(None);
|
||||
};
|
||||
if !snapshot.game_state.is_object() {
|
||||
return Err(runtime_plain_chat_error_response(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::BAD_REQUEST).with_details(json!({
|
||||
"provider": "runtime-chat",
|
||||
"field": "snapshot.gameState",
|
||||
"message": "snapshot.gameState 必须是 JSON object",
|
||||
})),
|
||||
));
|
||||
}
|
||||
|
||||
let snapshot_session_id =
|
||||
read_runtime_session_id(&snapshot.game_state).unwrap_or_else(|| session_id.to_string());
|
||||
if snapshot_session_id != session_id {
|
||||
return Err(runtime_plain_chat_error_response(
|
||||
request_context,
|
||||
AppError::from_status(StatusCode::CONFLICT).with_details(json!({
|
||||
"provider": "runtime-chat",
|
||||
"message": "请求的运行时会话与服务端快照不一致,请重新进入游戏",
|
||||
"sessionId": session_id,
|
||||
"snapshotSessionId": snapshot_session_id,
|
||||
})),
|
||||
));
|
||||
}
|
||||
|
||||
// 中文注释:临时运行态聊天只读取请求 snapshot 构造上下文,不把它写回 runtime_snapshot。
|
||||
Ok(Some(snapshot.game_state.clone()))
|
||||
}
|
||||
|
||||
async fn request_runtime_plain_text(
|
||||
state: &AppState,
|
||||
system_prompt: &'static str,
|
||||
@@ -532,6 +588,7 @@ async fn request_runtime_plain_text(
|
||||
]);
|
||||
request.max_tokens = Some(400);
|
||||
request.enable_web_search = state.config.rpg_llm_web_search_enabled;
|
||||
request.model = Some(RPG_STORY_LLM_MODEL.to_string());
|
||||
|
||||
llm_client
|
||||
.request_text(request)
|
||||
@@ -563,6 +620,7 @@ fn stream_plain_text_response<'a>(
|
||||
]);
|
||||
request.max_tokens = Some(700);
|
||||
request.enable_web_search = enable_web_search;
|
||||
request.model = Some(RPG_STORY_LLM_MODEL.to_string());
|
||||
|
||||
let response = llm_client
|
||||
.stream_text(request, |_| {})
|
||||
|
||||
@@ -5,31 +5,33 @@ use axum::{
|
||||
response::Response,
|
||||
};
|
||||
use module_runtime::{
|
||||
PROFILE_RECHARGE_PAYMENT_CHANNEL_MOCK, RuntimeProfileMembershipBenefitRecord,
|
||||
RuntimeProfileRechargeCenterRecord, RuntimeProfileRechargeOrderRecord,
|
||||
RuntimeProfileRechargeProductRecord, RuntimeProfileRedeemCodeMode,
|
||||
RuntimeProfileRedeemCodeRecord, RuntimeProfileRewardCodeRedeemRecord,
|
||||
RuntimeProfileWalletLedgerSourceType, RuntimeReferralInviteCenterRecord,
|
||||
RuntimeReferralRedeemRecord,
|
||||
PROFILE_RECHARGE_PAYMENT_CHANNEL_MOCK, RuntimeProfileInviteCodeRecord,
|
||||
RuntimeProfileMembershipBenefitRecord, RuntimeProfileRechargeCenterRecord,
|
||||
RuntimeProfileRechargeOrderRecord, RuntimeProfileRechargeProductRecord,
|
||||
RuntimeProfileRedeemCodeMode, RuntimeProfileRedeemCodeRecord,
|
||||
RuntimeProfileRewardCodeRedeemRecord, RuntimeProfileWalletLedgerSourceType,
|
||||
RuntimeReferralInviteCenterRecord,
|
||||
};
|
||||
use serde_json::{Value, json};
|
||||
use shared_contracts::runtime::{
|
||||
AdminDisableProfileRedeemCodeRequest, AdminUpsertProfileRedeemCodeRequest,
|
||||
CreateProfileRechargeOrderRequest, CreateProfileRechargeOrderResponse,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_OPERATION_CONSUME,
|
||||
AdminDisableProfileRedeemCodeRequest, AdminUpsertProfileInviteCodeRequest,
|
||||
AdminUpsertProfileRedeemCodeRequest, CreateProfileRechargeOrderRequest,
|
||||
CreateProfileRechargeOrderResponse, PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_OPERATION_CONSUME,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_OPERATION_REFUND,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITEE_REWARD,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITER_REWARD,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_NEW_USER_REGISTRATION_REWARD,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_POINTS_RECHARGE,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_PUZZLE_AUTHOR_INCENTIVE_CLAIM,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_REDEEM_CODE_REWARD,
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_SNAPSHOT_SYNC, ProfileDashboardSummaryResponse,
|
||||
ProfileMembershipBenefitResponse, ProfileMembershipResponse, ProfilePlayStatsResponse,
|
||||
ProfilePlayedWorkSummaryResponse, ProfileRechargeCenterResponse, ProfileRechargeOrderResponse,
|
||||
ProfileRechargeProductResponse, ProfileRedeemCodeAdminResponse,
|
||||
ProfileReferralInviteCenterResponse, ProfileWalletLedgerEntryResponse,
|
||||
ProfileWalletLedgerResponse, RedeemProfileReferralInviteCodeRequest,
|
||||
RedeemProfileReferralInviteCodeResponse, RedeemProfileRewardCodeRequest,
|
||||
RedeemProfileRewardCodeResponse,
|
||||
ProfileInviteCodeAdminResponse, ProfileMembershipBenefitResponse, ProfileMembershipResponse,
|
||||
ProfilePlayStatsResponse, ProfilePlayedWorkSummaryResponse, ProfileRechargeCenterResponse,
|
||||
ProfileRechargeOrderResponse, ProfileRechargeProductResponse, ProfileRedeemCodeAdminResponse,
|
||||
ProfileReferralInviteCenterResponse, ProfileReferralInvitedUserResponse,
|
||||
ProfileWalletLedgerEntryResponse, ProfileWalletLedgerResponse,
|
||||
RedeemProfileReferralInviteCodeRequest, RedeemProfileReferralInviteCodeResponse,
|
||||
RedeemProfileRewardCodeRequest, RedeemProfileRewardCodeResponse,
|
||||
};
|
||||
use spacetime_client::SpacetimeClientError;
|
||||
use time::OffsetDateTime;
|
||||
@@ -109,6 +111,9 @@ fn format_profile_wallet_ledger_source_type(
|
||||
RuntimeProfileWalletLedgerSourceType::SnapshotSync => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_SNAPSHOT_SYNC
|
||||
}
|
||||
RuntimeProfileWalletLedgerSourceType::NewUserRegistrationReward => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_NEW_USER_REGISTRATION_REWARD
|
||||
}
|
||||
RuntimeProfileWalletLedgerSourceType::InviteInviterReward => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_INVITE_INVITER_REWARD
|
||||
}
|
||||
@@ -127,6 +132,9 @@ fn format_profile_wallet_ledger_source_type(
|
||||
RuntimeProfileWalletLedgerSourceType::RedeemCodeReward => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_REDEEM_CODE_REWARD
|
||||
}
|
||||
RuntimeProfileWalletLedgerSourceType::PuzzleAuthorIncentiveClaim => {
|
||||
PROFILE_WALLET_LEDGER_SOURCE_TYPE_PUZZLE_AUTHOR_INCENTIVE_CLAIM
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,6 +338,37 @@ pub async fn admin_disable_profile_redeem_code(
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn admin_upsert_profile_invite_code(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
Extension(admin): Extension<AuthenticatedAdmin>,
|
||||
Json(payload): Json<AdminUpsertProfileInviteCodeRequest>,
|
||||
) -> Result<Json<Value>, Response> {
|
||||
let metadata_json = normalize_admin_invite_code_metadata(payload.metadata)
|
||||
.map_err(|error| runtime_profile_error_response(&request_context, error))?;
|
||||
let updated_at_micros = OffsetDateTime::now_utc().unix_timestamp_nanos() / 1_000;
|
||||
let record = state
|
||||
.spacetime_client()
|
||||
.admin_upsert_profile_invite_code(
|
||||
admin.session().username.clone(),
|
||||
payload.invite_code,
|
||||
metadata_json,
|
||||
updated_at_micros as i64,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
runtime_profile_error_response(
|
||||
&request_context,
|
||||
map_runtime_profile_client_error(error),
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
build_profile_invite_code_admin_response(record),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn get_profile_play_stats(
|
||||
State(state): State<AppState>,
|
||||
Extension(request_context): Extension<RequestContext>,
|
||||
@@ -479,6 +518,16 @@ fn build_profile_referral_invite_center_response(
|
||||
today_inviter_reward_count: record.today_inviter_reward_count,
|
||||
today_inviter_reward_remaining: record.today_inviter_reward_remaining,
|
||||
reward_points: record.reward_points,
|
||||
invited_users: record
|
||||
.invited_users
|
||||
.into_iter()
|
||||
.map(|user| ProfileReferralInvitedUserResponse {
|
||||
user_id: user.user_id,
|
||||
display_name: user.display_name,
|
||||
avatar_url: user.avatar_url,
|
||||
bound_at: user.bound_at,
|
||||
})
|
||||
.collect(),
|
||||
has_redeemed_code: record.has_redeemed_code,
|
||||
bound_inviter_user_id: record.bound_inviter_user_id,
|
||||
bound_at: record.bound_at,
|
||||
@@ -487,7 +536,7 @@ fn build_profile_referral_invite_center_response(
|
||||
}
|
||||
|
||||
fn build_redeem_profile_referral_invite_code_response(
|
||||
record: RuntimeReferralRedeemRecord,
|
||||
record: module_runtime::RuntimeReferralRedeemRecord,
|
||||
) -> RedeemProfileReferralInviteCodeResponse {
|
||||
RedeemProfileReferralInviteCodeResponse {
|
||||
center: build_profile_referral_invite_center_response(record.center),
|
||||
@@ -515,6 +564,30 @@ fn build_redeem_profile_reward_code_response(
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_admin_invite_code_metadata(metadata: Option<Value>) -> Result<String, AppError> {
|
||||
let metadata = match metadata {
|
||||
Some(Value::Null) | None => json!({}),
|
||||
Some(value) if value.is_object() => value,
|
||||
Some(_) => {
|
||||
return Err(AppError::from_status(StatusCode::BAD_REQUEST)
|
||||
.with_message("邀请码 metadata 必须是 JSON 对象")
|
||||
.with_details(json!({ "field": "metadata" })));
|
||||
}
|
||||
};
|
||||
let metadata_json = serde_json::to_string(&metadata).map_err(|error| {
|
||||
AppError::from_status(StatusCode::BAD_REQUEST)
|
||||
.with_message(format!("邀请码 metadata 序列化失败:{error}"))
|
||||
.with_details(json!({ "field": "metadata" }))
|
||||
})?;
|
||||
if metadata_json.len() > 4096 {
|
||||
return Err(AppError::from_status(StatusCode::BAD_REQUEST)
|
||||
.with_message("邀请码 metadata 不能超过 4096 bytes")
|
||||
.with_details(json!({ "field": "metadata" })));
|
||||
}
|
||||
|
||||
Ok(metadata_json)
|
||||
}
|
||||
|
||||
fn parse_profile_redeem_code_mode(raw: &str) -> Result<RuntimeProfileRedeemCodeMode, String> {
|
||||
match raw.trim().to_ascii_lowercase().as_str() {
|
||||
"public" => Ok(RuntimeProfileRedeemCodeMode::Public),
|
||||
@@ -524,6 +597,20 @@ fn parse_profile_redeem_code_mode(raw: &str) -> Result<RuntimeProfileRedeemCodeM
|
||||
}
|
||||
}
|
||||
|
||||
fn build_profile_invite_code_admin_response(
|
||||
record: RuntimeProfileInviteCodeRecord,
|
||||
) -> ProfileInviteCodeAdminResponse {
|
||||
let metadata =
|
||||
serde_json::from_str::<Value>(&record.metadata_json).unwrap_or_else(|_| json!({}));
|
||||
ProfileInviteCodeAdminResponse {
|
||||
user_id: record.user_id,
|
||||
invite_code: record.invite_code,
|
||||
metadata,
|
||||
created_at: record.created_at,
|
||||
updated_at: record.updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_profile_redeem_code_admin_response(
|
||||
record: RuntimeProfileRedeemCodeRecord,
|
||||
) -> ProfileRedeemCodeAdminResponse {
|
||||
@@ -545,18 +632,31 @@ fn build_profile_redeem_code_admin_response(
|
||||
mod tests {
|
||||
use module_runtime::RuntimeProfileWalletLedgerSourceType;
|
||||
|
||||
use super::format_profile_wallet_ledger_source_type;
|
||||
use super::{format_profile_wallet_ledger_source_type, normalize_admin_invite_code_metadata};
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode},
|
||||
};
|
||||
use http_body_util::BodyExt;
|
||||
use platform_auth::{
|
||||
AccessTokenClaims, AccessTokenClaimsInput, AuthProvider, BindingStatus, sign_access_token,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::time::Duration;
|
||||
use time::OffsetDateTime;
|
||||
use tower::ServiceExt;
|
||||
|
||||
use crate::{app::build_router, config::AppConfig, state::AppState};
|
||||
|
||||
#[test]
|
||||
fn profile_wallet_ledger_source_type_formats_asset_operation_values() {
|
||||
fn profile_wallet_ledger_source_type_formats_backend_values() {
|
||||
assert_eq!(
|
||||
format_profile_wallet_ledger_source_type(
|
||||
RuntimeProfileWalletLedgerSourceType::NewUserRegistrationReward
|
||||
),
|
||||
shared_contracts::runtime::PROFILE_WALLET_LEDGER_SOURCE_TYPE_NEW_USER_REGISTRATION_REWARD
|
||||
);
|
||||
assert_eq!(
|
||||
format_profile_wallet_ledger_source_type(
|
||||
RuntimeProfileWalletLedgerSourceType::AssetOperationConsume
|
||||
@@ -569,6 +669,12 @@ mod tests {
|
||||
),
|
||||
shared_contracts::runtime::PROFILE_WALLET_LEDGER_SOURCE_TYPE_ASSET_OPERATION_REFUND
|
||||
);
|
||||
assert_eq!(
|
||||
format_profile_wallet_ledger_source_type(
|
||||
RuntimeProfileWalletLedgerSourceType::PuzzleAuthorIncentiveClaim
|
||||
),
|
||||
shared_contracts::runtime::PROFILE_WALLET_LEDGER_SOURCE_TYPE_PUZZLE_AUTHOR_INCENTIVE_CLAIM
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -699,6 +805,60 @@ mod tests {
|
||||
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn profile_referral_redeem_code_calls_spacetime_for_authenticated_user() {
|
||||
let state = seed_authenticated_state().await;
|
||||
let token = issue_access_token(&state);
|
||||
let app = build_router(state);
|
||||
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/profile/referrals/redeem-code")
|
||||
.header("authorization", format!("Bearer {token}"))
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(r#"{"inviteCode":"SY12345678"}"#))
|
||||
.expect("request should build"),
|
||||
)
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
|
||||
let body = response
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.expect("body should collect")
|
||||
.to_bytes();
|
||||
let payload: Value =
|
||||
serde_json::from_slice(&body).expect("response body should be valid json");
|
||||
assert_eq!(
|
||||
payload["error"]["details"]["provider"],
|
||||
Value::String("spacetimedb".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn admin_invite_code_metadata_accepts_only_json_object() {
|
||||
assert_eq!(
|
||||
normalize_admin_invite_code_metadata(None).expect("empty metadata should default"),
|
||||
"{}"
|
||||
);
|
||||
assert_eq!(
|
||||
normalize_admin_invite_code_metadata(Some(serde_json::json!({
|
||||
"channel": "spring",
|
||||
"source": "banner"
|
||||
})))
|
||||
.expect("object metadata should serialize"),
|
||||
r#"{"channel":"spring","source":"banner"}"#
|
||||
);
|
||||
|
||||
let error = normalize_admin_invite_code_metadata(Some(serde_json::json!("spring")))
|
||||
.expect_err("non-object metadata should reject");
|
||||
assert_eq!(error.message(), "邀请码 metadata 必须是 JSON 对象");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn runtime_profile_legacy_routes_are_not_mounted() {
|
||||
let app = build_router(AppState::new(AppConfig::default()).expect("state should build"));
|
||||
@@ -731,4 +891,40 @@ mod tests {
|
||||
assert_eq!(response.status(), StatusCode::NOT_FOUND, "{uri}");
|
||||
}
|
||||
}
|
||||
|
||||
async fn seed_authenticated_state() -> AppState {
|
||||
let state = AppState::new(fast_spacetime_timeout_config()).expect("state should build");
|
||||
state
|
||||
.seed_test_phone_user_with_password("13800138104", "secret123")
|
||||
.await
|
||||
.id;
|
||||
state
|
||||
}
|
||||
|
||||
fn fast_spacetime_timeout_config() -> AppConfig {
|
||||
AppConfig {
|
||||
spacetime_procedure_timeout: Duration::from_secs(1),
|
||||
..AppConfig::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn issue_access_token(state: &AppState) -> String {
|
||||
let claims = AccessTokenClaims::from_input(
|
||||
AccessTokenClaimsInput {
|
||||
user_id: "user_00000001".to_string(),
|
||||
session_id: "sess_runtime_profile".to_string(),
|
||||
provider: AuthProvider::Password,
|
||||
roles: vec!["user".to_string()],
|
||||
token_version: 2,
|
||||
phone_verified: true,
|
||||
binding_status: BindingStatus::Active,
|
||||
display_name: Some("资料页用户".to_string()),
|
||||
},
|
||||
state.auth_jwt_config(),
|
||||
OffsetDateTime::now_utc(),
|
||||
)
|
||||
.expect("claims should build");
|
||||
|
||||
sign_access_token(&claims, state.auth_jwt_config()).expect("token should sign")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,6 +164,7 @@ impl AppState {
|
||||
database: config.spacetime_database.clone(),
|
||||
token: config.spacetime_token.clone(),
|
||||
pool_size: config.spacetime_pool_size,
|
||||
procedure_timeout: config.spacetime_procedure_timeout,
|
||||
});
|
||||
let llm_client = build_llm_client(&config)?;
|
||||
|
||||
@@ -223,13 +224,29 @@ impl AppState {
|
||||
OffsetDateTime::now_utc().unix_timestamp_nanos() / 1_000,
|
||||
)
|
||||
.map_err(|_| SpacetimeClientError::Runtime("认证快照更新时间超出 i64 范围".to_string()))?;
|
||||
// 本地 auth_store 是当前认证请求的即时真相源;SpacetimeDB 快照用于跨进程恢复。
|
||||
// 远端数据库挂起或网络异常时,只降级远端恢复能力,不能让已成功的登录/刷新/退出回滚为失败。
|
||||
#[cfg(not(test))]
|
||||
self.spacetime_client
|
||||
if let Err(error) = self
|
||||
.spacetime_client
|
||||
.upsert_auth_store_snapshot(snapshot_json, updated_at_micros)
|
||||
.await?;
|
||||
// 写入 SpacetimeDB 后立刻回读一次,确保内存快照与表真相对齐。
|
||||
.await
|
||||
{
|
||||
warn!(
|
||||
error = %error,
|
||||
"认证快照写入 SpacetimeDB 失败,当前认证流程继续"
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
// 写入快照后尝试拆入正式认证表;失败只影响远端表恢复,不阻断当前认证响应。
|
||||
#[cfg(not(test))]
|
||||
self.spacetime_client.import_auth_store_snapshot().await?;
|
||||
if let Err(error) = self.spacetime_client.import_auth_store_snapshot().await {
|
||||
warn!(
|
||||
error = %error,
|
||||
"认证快照导入 SpacetimeDB 正式表失败,当前认证流程继续"
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
#[cfg(not(test))]
|
||||
Ok(())
|
||||
}
|
||||
@@ -242,6 +259,7 @@ impl AppState {
|
||||
database: config.spacetime_database.clone(),
|
||||
token: config.spacetime_token.clone(),
|
||||
pool_size: config.spacetime_pool_size,
|
||||
procedure_timeout: config.spacetime_procedure_timeout,
|
||||
});
|
||||
match spacetime_client
|
||||
.export_auth_store_snapshot_from_tables()
|
||||
|
||||
@@ -193,6 +193,14 @@ pub async fn bind_wechat_phone(
|
||||
)
|
||||
.await
|
||||
.map_err(map_wechat_bind_phone_error)?;
|
||||
if result.activated_new_user {
|
||||
crate::registration_reward::grant_new_user_registration_wallet_reward(
|
||||
&state,
|
||||
&request_context,
|
||||
&result.user.id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
let session_client = resolve_session_client_context(&headers);
|
||||
let signed_session = create_auth_session(
|
||||
&state,
|
||||
|
||||
54
server-rs/crates/api-server/src/work_author.rs
Normal file
54
server-rs/crates/api-server/src/work_author.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use module_auth::AuthUser;
|
||||
|
||||
use crate::state::AppState;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct WorkAuthorSummary {
|
||||
pub display_name: String,
|
||||
pub public_user_code: Option<String>,
|
||||
}
|
||||
|
||||
/// 中文注释:作品作者的真相源是 owner_user_id;历史昵称字段只作为账号资料不可读时的兼容回退。
|
||||
pub fn resolve_work_author_by_user_id(
|
||||
state: &AppState,
|
||||
owner_user_id: &str,
|
||||
fallback_display_name: Option<&str>,
|
||||
fallback_public_user_code: Option<&str>,
|
||||
) -> WorkAuthorSummary {
|
||||
let fallback_display_name =
|
||||
normalize_optional_text(fallback_display_name).unwrap_or_else(|| "玩家".to_string());
|
||||
let fallback_public_user_code = normalize_optional_text(fallback_public_user_code);
|
||||
|
||||
let Some(owner_user_id) = normalize_optional_text(Some(owner_user_id)) else {
|
||||
return WorkAuthorSummary {
|
||||
display_name: fallback_display_name,
|
||||
public_user_code: fallback_public_user_code,
|
||||
};
|
||||
};
|
||||
|
||||
match state.auth_user_service().get_user_by_id(&owner_user_id) {
|
||||
Ok(Some(user)) => map_auth_user_to_work_author_summary(user, fallback_display_name),
|
||||
Ok(None) | Err(_) => WorkAuthorSummary {
|
||||
display_name: fallback_display_name,
|
||||
public_user_code: fallback_public_user_code,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn map_auth_user_to_work_author_summary(
|
||||
user: AuthUser,
|
||||
fallback_display_name: String,
|
||||
) -> WorkAuthorSummary {
|
||||
WorkAuthorSummary {
|
||||
display_name: normalize_optional_text(Some(user.display_name.as_str()))
|
||||
.unwrap_or(fallback_display_name),
|
||||
public_user_code: normalize_optional_text(Some(user.public_user_code.as_str())),
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_optional_text(value: Option<&str>) -> Option<String> {
|
||||
value
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(ToOwned::to_owned)
|
||||
}
|
||||
Reference in New Issue
Block a user