From 8a37ef1f479e6db9693d0b54d49260c3dad18d9a Mon Sep 17 00:00:00 2001 From: LeenHawk Date: Thu, 7 May 2026 17:15:19 +0800 Subject: [PATCH 1/2] fix(vertex): enhance request body handling for CountToken operation --- sdk/gproxy-channel/src/channels/vertex.rs | 36 ++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/sdk/gproxy-channel/src/channels/vertex.rs b/sdk/gproxy-channel/src/channels/vertex.rs index 62597c9aa..8c5d07774 100644 --- a/sdk/gproxy-channel/src/channels/vertex.rs +++ b/sdk/gproxy-channel/src/channels/vertex.rs @@ -478,8 +478,10 @@ impl Channel for VertexChannel { crate::utils::http_headers::replace_header(&mut builder, "User-Agent", ua)?; } + let body = vertex_request_body(request)?; + builder - .body(request.body.clone()) + .body(body) .map_err(|e| UpstreamError::RequestBuild(e.to_string())) } @@ -555,6 +557,38 @@ fn vertex_request_path( } } +fn vertex_request_body(request: &PreparedRequest) -> Result, UpstreamError> { + if request.route.operation == OperationFamily::CountToken { + return flatten_vertex_count_tokens_body(&request.body); + } + Ok(request.body.clone()) +} + +fn flatten_vertex_count_tokens_body(body: &[u8]) -> Result, UpstreamError> { + let Ok(Value::Object(mut body_map)) = serde_json::from_slice::(body) else { + return Ok(body.to_vec()); + }; + let Some(generate_content_request) = body_map.remove("generateContentRequest") else { + return Ok(body.to_vec()); + }; + let Value::Object(generate_content_request) = generate_content_request else { + body_map.insert( + "generateContentRequest".to_string(), + generate_content_request, + ); + return serde_json::to_vec(&Value::Object(body_map)) + .map_err(|e| UpstreamError::RequestBuild(e.to_string())); + }; + + for (key, value) in generate_content_request { + if key != "model" { + body_map.insert(key, value); + } + } + serde_json::to_vec(&Value::Object(body_map)) + .map_err(|e| UpstreamError::RequestBuild(e.to_string())) +} + fn vertex_routing_table() -> RoutingTable { VertexChannel.routing_table() } From 2b9d8ec2b72180758db038ba14c5c4f32f3acfae Mon Sep 17 00:00:00 2001 From: LeenHawk Date: Thu, 7 May 2026 17:24:36 +0800 Subject: [PATCH 2/2] fix(vertex): use OpenAPI chat completions endpoint Route Vertex OpenAI Chat Completions passthrough requests to the official endpoints/openapi/chat/completions endpoint instead of Gemini publisher generateContent paths. Also records the latest gproxy-protocol submodule pointer.\n\nFixes #92 --- sdk/gproxy-channel/src/channels/vertex.rs | 15 +++++++++++++++ sdk/gproxy-protocol | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/sdk/gproxy-channel/src/channels/vertex.rs b/sdk/gproxy-channel/src/channels/vertex.rs index 8c5d07774..bfff01f00 100644 --- a/sdk/gproxy-channel/src/channels/vertex.rs +++ b/sdk/gproxy-channel/src/channels/vertex.rs @@ -530,15 +530,30 @@ fn vertex_request_path( credential.project_id.trim(), settings.location.trim() ); + let openapi_prefix = format!( + "/v1/projects/{}/locations/{}/endpoints/openapi", + credential.project_id.trim(), + settings.location.trim() + ); match request.route.operation { OperationFamily::ModelList => Ok("/v1beta1/publishers/google/models".to_string()), OperationFamily::ModelGet => Ok(format!("/v1beta1/publishers/google/models/{model}")), OperationFamily::CountToken => Ok(format!( "{project_prefix}publishers/google/models/{model}:countTokens" )), + OperationFamily::GenerateContent + if request.route.protocol == ProtocolKind::OpenAiChatCompletion => + { + Ok(format!("{openapi_prefix}/chat/completions")) + } OperationFamily::GenerateContent => Ok(format!( "{project_prefix}publishers/google/models/{model}:generateContent" )), + OperationFamily::StreamGenerateContent + if request.route.protocol == ProtocolKind::OpenAiChatCompletion => + { + Ok(format!("{openapi_prefix}/chat/completions")) + } OperationFamily::StreamGenerateContent | OperationFamily::GeminiLive => Ok(format!( "{project_prefix}publishers/google/models/{model}:streamGenerateContent{}", if request.route.protocol == ProtocolKind::Gemini { diff --git a/sdk/gproxy-protocol b/sdk/gproxy-protocol index e0280a4ab..581ca7837 160000 --- a/sdk/gproxy-protocol +++ b/sdk/gproxy-protocol @@ -1 +1 @@ -Subproject commit e0280a4abb6712096f686fad619f1ac213014b0a +Subproject commit 581ca78371e124d087f87493ac446b1a12ccc23f