feat: continue tree rust family cutover
- add rust renderer/state-family scaffolds and inline compat host thinning for page tree, file tree, and picker - route tree/filetree preflight, file projection, resource artifact, and stream delta contracts through rust plans - preserve canonical move-order validation, file-tree search projection, and related frontend/runtime regression coverage
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
use crate::app::AppConfig;
|
||||
use crate::context::RequestContext;
|
||||
use crate::error::WebError;
|
||||
use bridge_runtime::{RuntimeCommandExecutionPlan, RuntimeQueryExecutionPlan};
|
||||
use serde_json::{json, Value};
|
||||
use bridge_runtime::{
|
||||
RuntimeBridgeContextWire, RuntimeCommandArtifactPlan, RuntimeCommandEnvelopeWire,
|
||||
RuntimeCommandExecutionPlan, RuntimeQueryExecutionPlan, build_runtime_command_artifact_plan,
|
||||
};
|
||||
use serde_json::{Value, json};
|
||||
use std::fs;
|
||||
use std::time::Duration;
|
||||
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
|
||||
|
||||
const HEADER_REQUEST_ID: &str = "x-request-id";
|
||||
const HEADER_TRACE_ID: &str = "x-trace-id";
|
||||
@@ -154,6 +158,14 @@ fn load_mutation_fixture(
|
||||
config: &AppConfig,
|
||||
context: &RequestContext,
|
||||
plan: &RuntimeCommandExecutionPlan,
|
||||
) -> Result<Option<Value>, WebError> {
|
||||
load_mutation_fixture_by_name(config, context, plan.function_name.as_str())
|
||||
}
|
||||
|
||||
fn load_mutation_fixture_by_name(
|
||||
config: &AppConfig,
|
||||
context: &RequestContext,
|
||||
function_name: &str,
|
||||
) -> Result<Option<Value>, WebError> {
|
||||
if !config.allow_dev_fixtures {
|
||||
return Ok(None);
|
||||
@@ -175,7 +187,7 @@ fn load_mutation_fixture(
|
||||
|
||||
Ok(fixtures
|
||||
.as_object()
|
||||
.and_then(|map| map.get(plan.function_name.as_str()))
|
||||
.and_then(|map| map.get(function_name))
|
||||
.cloned())
|
||||
}
|
||||
|
||||
@@ -434,6 +446,253 @@ pub async fn execute_convex_command_plan(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ConvexCommandExecution {
|
||||
pub result: Value,
|
||||
pub artifacts: Option<RuntimeCommandArtifactPlan>,
|
||||
pub artifact_error: Option<String>,
|
||||
}
|
||||
|
||||
fn now_iso_like() -> String {
|
||||
OffsetDateTime::now_utc()
|
||||
.format(&Rfc3339)
|
||||
.unwrap_or_else(|_| "1970-01-01T00:00:00Z".into())
|
||||
}
|
||||
|
||||
async fn execute_convex_mutation_by_name(
|
||||
config: &AppConfig,
|
||||
context: &RequestContext,
|
||||
function_name: &str,
|
||||
args: Value,
|
||||
workspace_id: Option<&str>,
|
||||
idempotency_key: Option<&str>,
|
||||
error_phase: &'static str,
|
||||
) -> Result<Value, WebError> {
|
||||
if let Some(fixture) = load_mutation_fixture_by_name(config, context, function_name)? {
|
||||
return Ok(fixture);
|
||||
}
|
||||
|
||||
let payload = json!({
|
||||
"path": function_name,
|
||||
"format": "convex_encoded_json",
|
||||
"args": [args],
|
||||
});
|
||||
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(Duration::from_secs(20))
|
||||
.build()
|
||||
.map_err(|error| {
|
||||
WebError::internal(format!("Convex HTTP 客户端创建失败: {error}"))
|
||||
.with_context(context)
|
||||
.with_header("x-error-phase", "client_build")
|
||||
.with_header("x-upstream-service", "convex")
|
||||
})?;
|
||||
|
||||
let mut request = client
|
||||
.post(format!("{}/api/mutation", convex_url(config, context)?))
|
||||
.header("Authorization", build_authorization(config, context)?)
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Convex-Client", "mnote-web")
|
||||
.header(HEADER_REQUEST_ID, &context.trace.request_id)
|
||||
.header(HEADER_TRACE_ID, &context.trace.trace_id)
|
||||
.header(HEADER_SOURCE_CHANNEL, context.source.channel.as_str())
|
||||
.header(HEADER_SOURCE_CLIENT, context.source.client.as_str())
|
||||
.json(&payload);
|
||||
|
||||
if let Some(workspace_id) = workspace_id
|
||||
.or(context.workspace.workspace_id.as_deref())
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
request = request.header(HEADER_WORKSPACE_ID, workspace_id);
|
||||
}
|
||||
if let Some(idempotency_key) = idempotency_key
|
||||
.or(context.source.idempotency_key.as_deref())
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
request = request.header(HEADER_IDEMPOTENCY_KEY, idempotency_key);
|
||||
}
|
||||
|
||||
let response = request.send().await.map_err(|error| {
|
||||
let base = if error.is_timeout() {
|
||||
WebError::gateway_timeout_code(
|
||||
"convex_timeout",
|
||||
format!("Convex mutation 超时: {error}"),
|
||||
)
|
||||
} else {
|
||||
WebError::service_unavailable_code(
|
||||
"convex_unavailable",
|
||||
format!("Convex mutation 请求失败: {error}"),
|
||||
)
|
||||
};
|
||||
base.with_context(context)
|
||||
.with_header("x-error-phase", error_phase)
|
||||
.with_header("x-upstream-service", "convex")
|
||||
})?;
|
||||
|
||||
let status = response.status();
|
||||
let body: Value = response.json().await.map_err(|error| {
|
||||
WebError::bad_gateway_code(
|
||||
"convex_bad_response",
|
||||
format!("Convex mutation 响应解析失败: {error}"),
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-error-phase", error_phase)
|
||||
.with_header("x-upstream-service", "convex")
|
||||
.with_header("x-upstream-status", status.as_u16().to_string())
|
||||
})?;
|
||||
if !status.is_success() {
|
||||
let message = body
|
||||
.get("errorMessage")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("Convex mutation 失败");
|
||||
return Err(
|
||||
WebError::bad_gateway_code("convex_upstream_error", message.to_string())
|
||||
.with_context(context)
|
||||
.with_header("x-error-phase", error_phase)
|
||||
.with_header("x-upstream-service", "convex")
|
||||
.with_header("x-upstream-status", status.as_u16().to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
match body.get("status").and_then(Value::as_str) {
|
||||
Some("success") => Ok(body.get("value").cloned().unwrap_or(Value::Null)),
|
||||
Some("error") => Err(WebError::bad_gateway_code(
|
||||
"convex_upstream_error",
|
||||
body.get("errorMessage")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("Convex 返回 error")
|
||||
.to_string(),
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-error-phase", error_phase)
|
||||
.with_header("x-upstream-service", "convex")
|
||||
.with_header("x-upstream-status", status.as_u16().to_string())),
|
||||
_ => Err(WebError::bad_gateway_code(
|
||||
"convex_bad_response",
|
||||
format!("未知 Convex 响应: {body}"),
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-error-phase", error_phase)
|
||||
.with_header("x-upstream-service", "convex")
|
||||
.with_header("x-upstream-status", status.as_u16().to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
fn command_log_artifact_args(artifact: &bridge_runtime::RuntimeCommandLogArtifactPlan) -> Value {
|
||||
json!({
|
||||
"workspaceId": artifact.workspace_id,
|
||||
"id": artifact.id,
|
||||
"requestId": artifact.request_id,
|
||||
"traceId": artifact.trace_id,
|
||||
"commandId": artifact.command_id,
|
||||
"commandName": artifact.command_name,
|
||||
"actorId": artifact.actor_id,
|
||||
"actorType": artifact.actor_type,
|
||||
"sourceChannel": artifact.source_channel,
|
||||
"sourceClient": artifact.source_client,
|
||||
"status": artifact.status,
|
||||
"targetPageId": artifact.target_page_id,
|
||||
"targetBlockId": artifact.target_block_id,
|
||||
"payload": artifact.payload,
|
||||
"payloadSummary": artifact.payload_summary,
|
||||
"refs": artifact.refs,
|
||||
"idempotencyKey": artifact.idempotency_key,
|
||||
"error": artifact.error,
|
||||
"createdAt": artifact.created_at,
|
||||
"finishedAt": artifact.finished_at,
|
||||
})
|
||||
}
|
||||
|
||||
fn domain_event_artifact_args(artifact: &bridge_runtime::RuntimeDomainEventArtifactPlan) -> Value {
|
||||
json!({
|
||||
"workspaceId": artifact.workspace_id,
|
||||
"id": artifact.id,
|
||||
"requestId": artifact.request_id,
|
||||
"traceId": artifact.trace_id,
|
||||
"commandId": artifact.command_id,
|
||||
"commandLogId": artifact.command_log_id,
|
||||
"eventType": artifact.event_type,
|
||||
"aggregateType": artifact.aggregate_type,
|
||||
"aggregateId": artifact.aggregate_id,
|
||||
"eventVersion": artifact.event_version,
|
||||
"status": artifact.status,
|
||||
"actorType": artifact.actor_type,
|
||||
"payload": artifact.payload,
|
||||
"createdAt": artifact.created_at,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn persist_runtime_command_artifacts(
|
||||
config: &AppConfig,
|
||||
context: &RequestContext,
|
||||
artifacts: &RuntimeCommandArtifactPlan,
|
||||
) -> Result<(), WebError> {
|
||||
execute_convex_mutation_by_name(
|
||||
config,
|
||||
context,
|
||||
"bridgeLogs:recordCommandLog",
|
||||
command_log_artifact_args(&artifacts.command_log),
|
||||
Some(artifacts.command_log.workspace_id.as_str()),
|
||||
artifacts.command_log.idempotency_key.as_deref(),
|
||||
"artifact_command_log",
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let Some(domain_event) = artifacts.domain_event.as_ref() {
|
||||
execute_convex_mutation_by_name(
|
||||
config,
|
||||
context,
|
||||
"bridgeLogs:recordDomainEvent",
|
||||
domain_event_artifact_args(domain_event),
|
||||
Some(domain_event.workspace_id.as_str()),
|
||||
artifacts.command_log.idempotency_key.as_deref(),
|
||||
"artifact_domain_event",
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn execute_convex_command_plan_with_artifacts(
|
||||
config: &AppConfig,
|
||||
context: &RequestContext,
|
||||
runtime_context: &RuntimeBridgeContextWire,
|
||||
command: &RuntimeCommandEnvelopeWire,
|
||||
plan: &RuntimeCommandExecutionPlan,
|
||||
) -> Result<ConvexCommandExecution, WebError> {
|
||||
let result = execute_convex_command_plan(config, context, plan).await?;
|
||||
let artifacts = build_runtime_command_artifact_plan(
|
||||
runtime_context,
|
||||
command,
|
||||
plan,
|
||||
&result,
|
||||
&now_iso_like(),
|
||||
);
|
||||
if let Some(artifacts) = artifacts.as_ref() {
|
||||
if let Err(error) = persist_runtime_command_artifacts(config, context, artifacts).await {
|
||||
let message = error.message().to_string();
|
||||
tracing::warn!(
|
||||
error = %message,
|
||||
command_id = %command.command_id,
|
||||
"Rust command artifact 持久化失败,主 mutation 结果继续返回"
|
||||
);
|
||||
return Ok(ConvexCommandExecution {
|
||||
result,
|
||||
artifacts: Some(artifacts.clone()),
|
||||
artifact_error: Some(message),
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(ConvexCommandExecution {
|
||||
result,
|
||||
artifacts,
|
||||
artifact_error: None,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::build_authorization;
|
||||
|
||||
Reference in New Issue
Block a user