Resolve spacetime client binding merge conflicts

This commit is contained in:
2026-04-24 14:44:46 +08:00
parent 4f369617c7
commit f65177b147
26 changed files with 2172 additions and 1020 deletions

View File

@@ -1192,6 +1192,25 @@ pub fn get_custom_world_agent_operation(
}
}
#[spacetimedb::procedure]
pub fn upsert_custom_world_agent_operation_progress(
ctx: &mut ProcedureContext,
input: CustomWorldAgentOperationProgressInput,
) -> CustomWorldAgentOperationProcedureResult {
match ctx.try_with_tx(|tx| upsert_custom_world_agent_operation_progress_tx(tx, input.clone())) {
Ok(operation) => CustomWorldAgentOperationProcedureResult {
ok: true,
operation: Some(operation),
error_message: None,
},
Err(message) => CustomWorldAgentOperationProcedureResult {
ok: false,
operation: None,
error_message: Some(message),
},
}
}
fn continue_story_tx(
ctx: &ReducerContext,
input: StoryContinueInput,
@@ -1474,6 +1493,59 @@ fn get_custom_world_agent_operation_tx(
Ok(build_custom_world_agent_operation_snapshot(&operation))
}
fn upsert_custom_world_agent_operation_progress_tx(
ctx: &ReducerContext,
input: CustomWorldAgentOperationProgressInput,
) -> Result<CustomWorldAgentOperationSnapshot, String> {
validate_custom_world_agent_operation_progress_input(&input).map_err(|error| error.to_string())?;
ctx.db
.custom_world_agent_session()
.session_id()
.find(&input.session_id)
.filter(|row| row.owner_user_id == input.owner_user_id)
.ok_or_else(|| "custom_world_agent_session 不存在".to_string())?;
let timestamp = Timestamp::from_micros_since_unix_epoch(input.updated_at_micros);
let operation = if let Some(current) = ctx
.db
.custom_world_agent_operation()
.operation_id()
.find(&input.operation_id)
{
if current.session_id != input.session_id {
return Err("custom_world_agent_operation.session_id 不匹配".to_string());
}
let next = rebuild_custom_world_agent_operation_row(
&current,
CustomWorldAgentOperationPatch {
status: Some(input.operation_status),
phase_label: Some(input.phase_label.clone()),
phase_detail: Some(input.phase_detail.clone()),
progress: Some(input.operation_progress),
error_message: Some(input.error_message.clone()),
updated_at_micros: Some(input.updated_at_micros),
},
)?;
replace_custom_world_agent_operation(ctx, &current, next.clone());
next
} else {
ctx.db.custom_world_agent_operation().insert(CustomWorldAgentOperation {
operation_id: input.operation_id.clone(),
session_id: input.session_id.clone(),
operation_type: input.operation_type,
status: input.operation_status,
phase_label: input.phase_label.clone(),
phase_detail: input.phase_detail.clone(),
progress: input.operation_progress,
error_message: input.error_message.clone(),
created_at: timestamp,
updated_at: timestamp,
})
};
Ok(build_custom_world_agent_operation_snapshot(&operation))
}
fn finalize_custom_world_agent_message_turn_tx(
ctx: &ReducerContext,
input: CustomWorldAgentMessageFinalizeInput,
@@ -2896,14 +2968,22 @@ fn execute_custom_world_agent_action_tx(
.filter(|row| row.owner_user_id == input.owner_user_id)
.ok_or_else(|| "custom_world_agent_session 不存在".to_string())?;
if ctx
if let Some(existing_operation) = ctx
.db
.custom_world_agent_operation()
.operation_id()
.find(&input.operation_id)
.is_some()
{
return Err("custom_world_agent_operation.operation_id 已存在".to_string());
let can_reuse_running_draft_operation = input.action.trim() == "draft_foundation"
&& existing_operation.session_id == input.session_id
&& existing_operation.operation_type == RpgAgentOperationType::DraftFoundation
&& matches!(
existing_operation.status,
RpgAgentOperationStatus::Queued | RpgAgentOperationStatus::Running
);
if !can_reuse_running_draft_operation {
return Err("custom_world_agent_operation.operation_id 已存在".to_string());
}
}
let payload = parse_optional_session_object(input.payload_json.as_deref()).unwrap_or_default();
@@ -2990,7 +3070,7 @@ fn execute_draft_foundation_action(
updated_at,
);
let operation = build_and_insert_custom_world_operation(
let operation = complete_custom_world_operation(
ctx,
&input.operation_id,
&session.session_id,
@@ -2998,7 +3078,7 @@ fn execute_draft_foundation_action(
"底稿已整理",
"第一版 foundation draft 已写入会话与世界卡。",
updated_at,
);
)?;
Ok(build_custom_world_agent_operation_snapshot(&operation))
}
@@ -4117,6 +4197,53 @@ fn replace_custom_world_draft_card(
ctx.db.custom_world_draft_card().insert(next);
}
fn complete_custom_world_operation(
ctx: &ReducerContext,
operation_id: &str,
session_id: &str,
operation_type: RpgAgentOperationType,
phase_label: &str,
phase_detail: &str,
timestamp_micros: i64,
) -> Result<CustomWorldAgentOperation, String> {
if let Some(current) = ctx
.db
.custom_world_agent_operation()
.operation_id()
.find(&operation_id.to_string())
{
if current.session_id != session_id {
return Err("custom_world_agent_operation.session_id 不匹配".to_string());
}
if current.operation_type != operation_type {
return Err("custom_world_agent_operation.operation_type 不匹配".to_string());
}
let next = rebuild_custom_world_agent_operation_row(
&current,
CustomWorldAgentOperationPatch {
status: Some(RpgAgentOperationStatus::Completed),
phase_label: Some(phase_label.to_string()),
phase_detail: Some(phase_detail.to_string()),
progress: Some(100),
error_message: Some(None),
updated_at_micros: Some(timestamp_micros),
},
)?;
replace_custom_world_agent_operation(ctx, &current, next.clone());
return Ok(next);
}
Ok(build_and_insert_custom_world_operation(
ctx,
operation_id,
session_id,
operation_type,
phase_label,
phase_detail,
timestamp_micros,
))
}
fn build_and_insert_custom_world_operation(
ctx: &ReducerContext,
operation_id: &str,