fix(gateway): route Gemini embedding batches correctly

This commit is contained in:
MMEXA
2026-05-17 14:24:32 +00:00
parent a2f91b4108
commit 81ff375bfd
37 changed files with 1353 additions and 120 deletions

View File

@@ -13,8 +13,9 @@ pub(crate) use crate::ai_serving::{
EXECUTION_RUNTIME_STREAM_DECISION_ACTION, EXECUTION_RUNTIME_SYNC_ACTION,
EXECUTION_RUNTIME_SYNC_DECISION_ACTION, GEMINI_CHAT_STREAM_PLAN_KIND,
GEMINI_CHAT_SYNC_PLAN_KIND, GEMINI_CLI_STREAM_PLAN_KIND, GEMINI_CLI_SYNC_PLAN_KIND,
GEMINI_FILES_DELETE_PLAN_KIND, GEMINI_FILES_DOWNLOAD_PLAN_KIND, GEMINI_FILES_GET_PLAN_KIND,
GEMINI_FILES_LIST_PLAN_KIND, GEMINI_FILES_UPLOAD_PLAN_KIND, GEMINI_VIDEO_CANCEL_SYNC_PLAN_KIND,
GEMINI_EMBEDDING_SYNC_PLAN_KIND, GEMINI_FILES_DELETE_PLAN_KIND,
GEMINI_FILES_DOWNLOAD_PLAN_KIND, GEMINI_FILES_GET_PLAN_KIND, GEMINI_FILES_LIST_PLAN_KIND,
GEMINI_FILES_UPLOAD_PLAN_KIND, GEMINI_VIDEO_CANCEL_SYNC_PLAN_KIND,
GEMINI_VIDEO_CREATE_SYNC_PLAN_KIND, OPENAI_CHAT_STREAM_PLAN_KIND, OPENAI_CHAT_SYNC_PLAN_KIND,
OPENAI_EMBEDDING_SYNC_PLAN_KIND, OPENAI_IMAGE_STREAM_PLAN_KIND, OPENAI_IMAGE_SYNC_PLAN_KIND,
OPENAI_RERANK_SYNC_PLAN_KIND, OPENAI_RESPONSES_COMPACT_STREAM_PLAN_KIND,

View File

@@ -1,16 +1,16 @@
use crate::ai_serving::planner::common::{
CLAUDE_CHAT_STREAM_PLAN_KIND, CLAUDE_CHAT_SYNC_PLAN_KIND, CLAUDE_CLI_STREAM_PLAN_KIND,
CLAUDE_CLI_SYNC_PLAN_KIND, GEMINI_CHAT_STREAM_PLAN_KIND, GEMINI_CHAT_SYNC_PLAN_KIND,
GEMINI_CLI_STREAM_PLAN_KIND, GEMINI_CLI_SYNC_PLAN_KIND, GEMINI_FILES_DELETE_PLAN_KIND,
GEMINI_FILES_DOWNLOAD_PLAN_KIND, GEMINI_FILES_GET_PLAN_KIND, GEMINI_FILES_LIST_PLAN_KIND,
GEMINI_VIDEO_CANCEL_SYNC_PLAN_KIND, GEMINI_VIDEO_CREATE_SYNC_PLAN_KIND,
OPENAI_CHAT_STREAM_PLAN_KIND, OPENAI_CHAT_SYNC_PLAN_KIND, OPENAI_EMBEDDING_SYNC_PLAN_KIND,
OPENAI_IMAGE_STREAM_PLAN_KIND, OPENAI_IMAGE_SYNC_PLAN_KIND, OPENAI_RERANK_SYNC_PLAN_KIND,
OPENAI_RESPONSES_COMPACT_STREAM_PLAN_KIND, OPENAI_RESPONSES_COMPACT_SYNC_PLAN_KIND,
OPENAI_RESPONSES_STREAM_PLAN_KIND, OPENAI_RESPONSES_SYNC_PLAN_KIND,
OPENAI_VIDEO_CANCEL_SYNC_PLAN_KIND, OPENAI_VIDEO_CONTENT_PLAN_KIND,
OPENAI_VIDEO_CREATE_SYNC_PLAN_KIND, OPENAI_VIDEO_DELETE_SYNC_PLAN_KIND,
OPENAI_VIDEO_REMIX_SYNC_PLAN_KIND,
GEMINI_CLI_STREAM_PLAN_KIND, GEMINI_CLI_SYNC_PLAN_KIND, GEMINI_EMBEDDING_SYNC_PLAN_KIND,
GEMINI_FILES_DELETE_PLAN_KIND, GEMINI_FILES_DOWNLOAD_PLAN_KIND, GEMINI_FILES_GET_PLAN_KIND,
GEMINI_FILES_LIST_PLAN_KIND, GEMINI_VIDEO_CANCEL_SYNC_PLAN_KIND,
GEMINI_VIDEO_CREATE_SYNC_PLAN_KIND, OPENAI_CHAT_STREAM_PLAN_KIND, OPENAI_CHAT_SYNC_PLAN_KIND,
OPENAI_EMBEDDING_SYNC_PLAN_KIND, OPENAI_IMAGE_STREAM_PLAN_KIND, OPENAI_IMAGE_SYNC_PLAN_KIND,
OPENAI_RERANK_SYNC_PLAN_KIND, OPENAI_RESPONSES_COMPACT_STREAM_PLAN_KIND,
OPENAI_RESPONSES_COMPACT_SYNC_PLAN_KIND, OPENAI_RESPONSES_STREAM_PLAN_KIND,
OPENAI_RESPONSES_SYNC_PLAN_KIND, OPENAI_VIDEO_CANCEL_SYNC_PLAN_KIND,
OPENAI_VIDEO_CONTENT_PLAN_KIND, OPENAI_VIDEO_CREATE_SYNC_PLAN_KIND,
OPENAI_VIDEO_DELETE_SYNC_PLAN_KIND, OPENAI_VIDEO_REMIX_SYNC_PLAN_KIND,
};
use crate::ai_serving::planner::plan_builders::{
build_gemini_stream_plan_from_decision, build_gemini_sync_plan_from_decision,
@@ -110,7 +110,9 @@ fn build_sync_plan_payload_from_decision(
| OPENAI_RERANK_SYNC_PLAN_KIND => {
build_standard_sync_plan_from_decision(parts, body_json, payload)?
}
GEMINI_CHAT_SYNC_PLAN_KIND | GEMINI_CLI_SYNC_PLAN_KIND => {
GEMINI_CHAT_SYNC_PLAN_KIND
| GEMINI_CLI_SYNC_PLAN_KIND
| GEMINI_EMBEDDING_SYNC_PLAN_KIND => {
build_gemini_sync_plan_from_decision(parts, body_json, payload)?
}
OPENAI_VIDEO_CREATE_SYNC_PLAN_KIND

View File

@@ -254,6 +254,7 @@ pub(crate) async fn resolve_local_same_format_provider_candidate_payload_parts(
spec,
prepared.upstream_is_stream,
prepared.kiro_auth.as_ref(),
Some(&provider_request_body),
) else {
mark_skipped_local_same_format_provider_candidate_with_failure_diagnostic(
state,

View File

@@ -14,6 +14,7 @@ pub(crate) fn build_same_format_upstream_url(
spec: LocalSameFormatProviderSpec,
upstream_is_stream: bool,
kiro_auth: Option<&crate::ai_serving::transport::kiro::KiroRequestAuth>,
provider_request_body: Option<&serde_json::Value>,
) -> Option<String> {
build_same_format_provider_upstream_url_impl(
transport,
@@ -23,6 +24,7 @@ pub(crate) fn build_same_format_upstream_url(
upstream_is_stream,
request_query: parts.uri.query(),
kiro_api_region: kiro_auth.map(|auth| auth.auth_config.effective_api_region()),
provider_request_body,
},
)
}

View File

@@ -361,6 +361,7 @@ async fn resolve_local_openai_image_to_gemini_candidate_payload_parts(
&converted.mapped_model,
provider_api_format,
upstream_is_stream,
Some(&converted.body_json),
) else {
mark_skipped_local_openai_image_candidate_with_failure_diagnostic(
state,

View File

@@ -75,15 +75,18 @@ pub(crate) async fn resolve_local_standard_candidate_payload_parts(
.await;
}
let is_kiro_claude_cli = is_kiro_claude_messages_transport(transport, provider_api_format);
let Some(conversion_kind) =
crate::ai_serving::request_conversion_kind(spec_metadata.api_format, provider_api_format)
else {
return None;
};
if let Some(skip_reason) = crate::ai_serving::request_conversion_transport_unsupported_reason(
if !crate::ai_serving::request_pair_allowed_for_transport(
transport,
conversion_kind,
spec_metadata.api_format,
provider_api_format,
) {
return None;
}
if let Some(skip_reason) = crate::ai_serving::request_pair_transport_unsupported_reason(
transport,
spec_metadata.api_format,
provider_api_format,
) {
mark_skipped_local_standard_candidate(
state,
@@ -156,7 +159,7 @@ pub(crate) async fn resolve_local_standard_candidate_payload_parts(
planner_state,
transport,
candidate,
crate::ai_serving::request_conversion_direct_auth(transport, conversion_kind),
crate::ai_serving::request_pair_direct_auth(transport, provider_api_format),
oauth_context,
)
.await
@@ -286,6 +289,7 @@ pub(crate) async fn resolve_local_standard_candidate_payload_parts(
&prepared_candidate.mapped_model,
provider_api_format,
upstream_is_stream,
Some(&provider_request_body),
) {
Some(url) => url,
None => {

View File

@@ -38,6 +38,7 @@ pub(crate) use self::openai::{
map_openai_reasoning_effort_to_gemini_budget, maybe_build_stream_local_decision_payload,
maybe_build_stream_local_openai_responses_decision_payload,
maybe_build_sync_local_decision_payload,
maybe_build_sync_local_openai_embedding_decision_payload,
maybe_build_sync_local_openai_responses_decision_payload, parse_openai_stop_sequences,
resolve_openai_chat_max_tokens, set_local_openai_chat_execution_exhausted_diagnostic,
value_as_u64,
@@ -66,14 +67,16 @@ pub(crate) fn build_standard_upstream_url(
mapped_model: &str,
provider_api_format: &str,
upstream_is_stream: bool,
provider_request_body: Option<&serde_json::Value>,
) -> Option<String> {
crate::ai_serving::build_provider_transport_request_url(
crate::ai_serving::build_provider_transport_request_url_for_request_body(
transport,
provider_api_format,
Some(mapped_model),
upstream_is_stream,
parts.uri.query(),
None,
provider_request_body,
)
}
@@ -85,6 +88,14 @@ pub(crate) async fn maybe_build_sync_local_standard_decision_payload(
body_json: &serde_json::Value,
plan_kind: &str,
) -> Result<Option<AiExecutionDecision>, GatewayError> {
if let Some(payload) = self::openai::maybe_build_sync_local_openai_embedding_decision_payload(
state, parts, trace_id, decision, body_json, plan_kind,
)
.await?
{
return Ok(Some(payload));
}
if let Some(payload) = self::claude::maybe_build_sync_local_claude_decision_payload(
state, parts, trace_id, decision, body_json, plan_kind,
)

View File

@@ -0,0 +1,31 @@
use crate::ai_serving::resolve_openai_embedding_sync_spec as resolve_surface_sync_spec;
use crate::ai_serving::GatewayControlDecision;
use crate::{AiExecutionDecision, AppState, GatewayError};
use super::super::family::maybe_build_sync_via_standard_family_payload;
pub(crate) fn resolve_sync_spec(
plan_kind: &str,
) -> Option<super::super::family::LocalStandardSpec> {
resolve_surface_sync_spec(plan_kind)
}
pub(crate) async fn maybe_build_sync_local_openai_embedding_decision_payload(
state: &AppState,
parts: &http::request::Parts,
trace_id: &str,
decision: &GatewayControlDecision,
body_json: &serde_json::Value,
plan_kind: &str,
) -> Result<Option<AiExecutionDecision>, GatewayError> {
maybe_build_sync_via_standard_family_payload(
state,
parts,
trace_id,
decision,
body_json,
plan_kind,
resolve_sync_spec,
)
.await
}

View File

@@ -1,4 +1,5 @@
mod chat;
mod embedding;
mod responses;
pub(crate) use crate::ai_serving::{
@@ -14,6 +15,7 @@ pub(crate) use chat::{
maybe_build_stream_local_decision_payload, maybe_build_sync_local_decision_payload,
set_local_openai_chat_execution_exhausted_diagnostic,
};
pub(crate) use embedding::maybe_build_sync_local_openai_embedding_decision_payload;
pub(crate) use responses::{
build_local_openai_responses_stream_attempt_source_for_kind,
build_local_openai_responses_stream_plan_and_reports_for_kind,