2026-04-09 00:10:38 +08:00
|
|
|
mod decision;
|
2026-04-14 01:27:04 +08:00
|
|
|
mod request;
|
2026-04-09 00:10:38 +08:00
|
|
|
mod support;
|
2026-03-31 19:19:04 +08:00
|
|
|
|
2026-05-03 20:14:29 +08:00
|
|
|
use async_trait::async_trait;
|
2026-03-31 19:19:04 +08:00
|
|
|
use tracing::warn;
|
|
|
|
|
|
2026-05-03 20:14:29 +08:00
|
|
|
use crate::ai_serving::planner::candidate_materialization::LocalExecutionAttemptSource;
|
2026-05-02 13:23:54 +08:00
|
|
|
use crate::ai_serving::planner::plan_builders::{
|
|
|
|
|
build_passthrough_sync_plan_from_decision, AiSyncAttempt,
|
2026-04-05 20:23:16 +08:00
|
|
|
};
|
2026-05-02 13:23:54 +08:00
|
|
|
use crate::ai_serving::planner::spec_metadata::local_video_create_spec_metadata;
|
|
|
|
|
use crate::ai_serving::GatewayControlDecision;
|
|
|
|
|
use crate::ai_serving::{
|
2026-04-09 00:10:38 +08:00
|
|
|
resolve_local_video_sync_spec as resolve_sync_spec, LocalVideoCreateFamily,
|
|
|
|
|
LocalVideoCreateSpec,
|
2026-04-07 02:50:19 +08:00
|
|
|
};
|
2026-05-02 13:23:54 +08:00
|
|
|
use crate::{AiExecutionDecision, AppState, GatewayError};
|
2026-04-09 00:10:38 +08:00
|
|
|
|
|
|
|
|
use self::decision::maybe_build_local_video_create_decision_payload_for_candidate;
|
|
|
|
|
use self::support::{
|
2026-05-03 20:14:29 +08:00
|
|
|
build_local_video_create_candidate_attempt_source, list_local_video_create_candidate_attempts,
|
|
|
|
|
resolve_local_video_create_decision_input, LocalVideoCreateCandidateAttempt,
|
|
|
|
|
LocalVideoCreateCandidateAttemptSource, LocalVideoCreateDecisionInput,
|
2026-04-07 02:50:19 +08:00
|
|
|
};
|
2026-03-31 19:19:04 +08:00
|
|
|
|
2026-05-03 20:14:29 +08:00
|
|
|
pub(crate) struct LocalVideoCreateSyncAttemptSource<'a> {
|
|
|
|
|
state: &'a AppState,
|
|
|
|
|
parts: &'a http::request::Parts,
|
|
|
|
|
body_json: &'a serde_json::Value,
|
|
|
|
|
trace_id: &'a str,
|
|
|
|
|
input: LocalVideoCreateDecisionInput,
|
|
|
|
|
spec: LocalVideoCreateSpec,
|
|
|
|
|
candidates: LocalVideoCreateCandidateAttemptSource<'a>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 16:26:16 +08:00
|
|
|
pub(crate) async fn build_local_video_sync_plan_and_reports_for_kind(
|
2026-03-31 19:19:04 +08:00
|
|
|
state: &AppState,
|
|
|
|
|
parts: &http::request::Parts,
|
|
|
|
|
body_json: &serde_json::Value,
|
|
|
|
|
trace_id: &str,
|
|
|
|
|
decision: &GatewayControlDecision,
|
|
|
|
|
plan_kind: &str,
|
2026-05-02 13:23:54 +08:00
|
|
|
) -> Result<Vec<AiSyncAttempt>, GatewayError> {
|
2026-03-31 19:19:04 +08:00
|
|
|
let Some(spec) = resolve_sync_spec(plan_kind) else {
|
2026-04-03 16:26:16 +08:00
|
|
|
return Ok(Vec::new());
|
2026-03-31 19:19:04 +08:00
|
|
|
};
|
|
|
|
|
|
2026-04-03 16:26:16 +08:00
|
|
|
build_local_sync_plan_and_reports(state, parts, body_json, trace_id, decision, spec).await
|
2026-03-31 19:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-03 20:14:29 +08:00
|
|
|
pub(crate) async fn build_local_video_sync_attempt_source_for_kind<'a>(
|
|
|
|
|
state: &'a AppState,
|
|
|
|
|
parts: &'a http::request::Parts,
|
|
|
|
|
body_json: &'a serde_json::Value,
|
|
|
|
|
trace_id: &'a str,
|
|
|
|
|
decision: &'a GatewayControlDecision,
|
|
|
|
|
plan_kind: &str,
|
|
|
|
|
) -> Result<Option<(LocalVideoCreateSyncAttemptSource<'a>, usize)>, GatewayError> {
|
|
|
|
|
let Some(spec) = resolve_sync_spec(plan_kind) else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
let spec_metadata = local_video_create_spec_metadata(spec);
|
|
|
|
|
|
|
|
|
|
let Some(input) = resolve_local_video_create_decision_input(
|
|
|
|
|
state, parts, trace_id, decision, body_json, spec,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let Some((candidates, candidate_count)) = build_local_video_create_candidate_attempt_source(
|
|
|
|
|
state,
|
|
|
|
|
trace_id,
|
|
|
|
|
&input,
|
|
|
|
|
body_json,
|
|
|
|
|
spec_metadata.api_format,
|
|
|
|
|
spec_metadata.decision_kind,
|
|
|
|
|
)
|
|
|
|
|
.await?
|
|
|
|
|
else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if candidate_count == 0 {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Some((
|
|
|
|
|
LocalVideoCreateSyncAttemptSource {
|
|
|
|
|
state,
|
|
|
|
|
parts,
|
|
|
|
|
body_json,
|
|
|
|
|
trace_id,
|
|
|
|
|
input,
|
|
|
|
|
spec,
|
|
|
|
|
candidates,
|
|
|
|
|
},
|
|
|
|
|
candidate_count,
|
|
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl LocalExecutionAttemptSource<AiSyncAttempt> for LocalVideoCreateSyncAttemptSource<'_> {
|
|
|
|
|
async fn next_execution_attempt(&mut self) -> Result<Option<AiSyncAttempt>, GatewayError> {
|
|
|
|
|
while let Some(attempt) = self.candidates.next_attempt().await {
|
|
|
|
|
match self.build_sync_attempt(attempt).await? {
|
|
|
|
|
Some(attempt) => return Ok(Some(attempt)),
|
|
|
|
|
None => continue,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn drain_execution_attempts(&mut self) -> Result<Vec<AiSyncAttempt>, GatewayError> {
|
|
|
|
|
let mut drained = Vec::new();
|
|
|
|
|
for attempt in self.candidates.drain_static_attempts() {
|
|
|
|
|
if let Some(attempt) = self.build_sync_attempt(attempt).await? {
|
|
|
|
|
drained.push(attempt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(drained)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl LocalVideoCreateSyncAttemptSource<'_> {
|
|
|
|
|
async fn build_sync_attempt(
|
|
|
|
|
&self,
|
|
|
|
|
attempt: LocalVideoCreateCandidateAttempt,
|
|
|
|
|
) -> Result<Option<AiSyncAttempt>, GatewayError> {
|
|
|
|
|
let spec_metadata = local_video_create_spec_metadata(self.spec);
|
|
|
|
|
let Some(payload) = maybe_build_local_video_create_decision_payload_for_candidate(
|
|
|
|
|
self.state,
|
|
|
|
|
self.parts,
|
|
|
|
|
self.body_json,
|
|
|
|
|
self.trace_id,
|
|
|
|
|
&self.input,
|
|
|
|
|
attempt,
|
|
|
|
|
self.spec,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match build_passthrough_sync_plan_from_decision(self.parts, payload) {
|
|
|
|
|
Ok(value) => Ok(value),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
warn!(
|
|
|
|
|
trace_id = %self.trace_id,
|
|
|
|
|
decision_kind = spec_metadata.decision_kind,
|
|
|
|
|
error = ?err,
|
|
|
|
|
"gateway local video sync decision plan build failed"
|
|
|
|
|
);
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 14:59:58 +08:00
|
|
|
pub(crate) async fn maybe_build_sync_local_video_decision_payload(
|
2026-03-31 19:19:04 +08:00
|
|
|
state: &AppState,
|
|
|
|
|
parts: &http::request::Parts,
|
|
|
|
|
body_json: &serde_json::Value,
|
|
|
|
|
trace_id: &str,
|
|
|
|
|
decision: &GatewayControlDecision,
|
|
|
|
|
plan_kind: &str,
|
2026-05-02 13:23:54 +08:00
|
|
|
) -> Result<Option<AiExecutionDecision>, GatewayError> {
|
2026-03-31 19:19:04 +08:00
|
|
|
let Some(spec) = resolve_sync_spec(plan_kind) else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
2026-04-14 01:27:04 +08:00
|
|
|
let spec_metadata = local_video_create_spec_metadata(spec);
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
let Some(input) = resolve_local_video_create_decision_input(
|
|
|
|
|
state, parts, trace_id, decision, body_json, spec,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-09 00:10:38 +08:00
|
|
|
let Some(attempts) = list_local_video_create_candidate_attempts(
|
2026-03-31 19:19:04 +08:00
|
|
|
state,
|
|
|
|
|
trace_id,
|
|
|
|
|
&input,
|
2026-04-17 23:05:49 +08:00
|
|
|
body_json,
|
2026-04-14 01:27:04 +08:00
|
|
|
spec_metadata.api_format,
|
|
|
|
|
spec_metadata.decision_kind,
|
2026-03-31 19:19:04 +08:00
|
|
|
)
|
2026-04-09 00:10:38 +08:00
|
|
|
.await
|
|
|
|
|
else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
for attempt in attempts {
|
|
|
|
|
if let Some(payload) = maybe_build_local_video_create_decision_payload_for_candidate(
|
|
|
|
|
state, parts, body_json, trace_id, &input, attempt, spec,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
return Ok(Some(payload));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn build_local_sync_plan_and_reports(
|
|
|
|
|
state: &AppState,
|
|
|
|
|
parts: &http::request::Parts,
|
|
|
|
|
body_json: &serde_json::Value,
|
|
|
|
|
trace_id: &str,
|
|
|
|
|
decision: &GatewayControlDecision,
|
|
|
|
|
spec: LocalVideoCreateSpec,
|
2026-05-02 13:23:54 +08:00
|
|
|
) -> Result<Vec<AiSyncAttempt>, GatewayError> {
|
2026-04-14 01:27:04 +08:00
|
|
|
let spec_metadata = local_video_create_spec_metadata(spec);
|
2026-03-31 19:19:04 +08:00
|
|
|
let Some(input) = resolve_local_video_create_decision_input(
|
|
|
|
|
state, parts, trace_id, decision, body_json, spec,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
else {
|
|
|
|
|
return Ok(Vec::new());
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-09 00:10:38 +08:00
|
|
|
let Some(attempts) = list_local_video_create_candidate_attempts(
|
2026-03-31 19:19:04 +08:00
|
|
|
state,
|
|
|
|
|
trace_id,
|
|
|
|
|
&input,
|
2026-04-17 23:05:49 +08:00
|
|
|
body_json,
|
2026-04-14 01:27:04 +08:00
|
|
|
spec_metadata.api_format,
|
|
|
|
|
spec_metadata.decision_kind,
|
2026-03-31 19:19:04 +08:00
|
|
|
)
|
2026-04-09 00:10:38 +08:00
|
|
|
.await
|
|
|
|
|
else {
|
|
|
|
|
return Ok(Vec::new());
|
|
|
|
|
};
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
let mut plans = Vec::new();
|
|
|
|
|
for attempt in attempts {
|
|
|
|
|
let Some(payload) = maybe_build_local_video_create_decision_payload_for_candidate(
|
|
|
|
|
state, parts, body_json, trace_id, &input, attempt, spec,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match build_passthrough_sync_plan_from_decision(parts, payload) {
|
|
|
|
|
Ok(Some(value)) => plans.push(value),
|
|
|
|
|
Ok(None) => {}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
warn!(
|
|
|
|
|
trace_id = %trace_id,
|
2026-04-14 01:27:04 +08:00
|
|
|
decision_kind = spec_metadata.decision_kind,
|
2026-03-31 19:19:04 +08:00
|
|
|
error = ?err,
|
|
|
|
|
"gateway local video sync decision plan build failed"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(plans)
|
|
|
|
|
}
|