use cpa_core::gostr::GoStr;
use std::collections::BTreeMap;
use bytes::Bytes;
use cpa_common::json::{self as gj, GoValue};
use cpa_common::thinking::parse_suffix;
use cpa_core::config::Config;
use cpa_core::credential::Credential;
use cpa_core::exec::{ExecError, ExecRequest, ExecResponse, FailureScope, Operation, ResponseBody};
use cpa_core::format::Format;
use serde_json::{Map, Value};
use crate::gemini::{self as g, Emit, LineState, Output};
use crate::gemini_payload as payload;
use crate::payload_barrier::Barrier;
use crate::proxy::{self, GoClients, GoHeaders, Hooks, Proxy};
use crate::{vertex_auth, vertex_openapi};
pub const PROVIDER: &str = "vertex";
const API_VERSION: &str = "v1";
const DEFAULT_API_KEY_BASE: &str = "https://aiplatform.googleapis.com";
const DEFAULT_LOCATION: &str = "us-central1";
pub struct VertexExecutor {
clients: GoClients,
now: fn() -> i64,
}
fn system_now() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs() as i64)
}
impl Default for VertexExecutor {
fn default() -> Self {
Self::with_hooks(Hooks::default(), system_now)
}
}
enum Auth {
ApiKey { key: String, base: String },
ServiceAccount {
project: String,
location: String,
account: Map<String, Value>,
},
}
fn plain_error(message: impl Into<String>) -> ExecError {
ExecError::local(500, FailureScope::Credential, message)
}
fn api_creds(credential: &Credential) -> (String, String) {
let attr = |k: &str| credential.attributes.get(k).cloned().unwrap_or_default();
let mut key = attr("api_key");
if key.is_empty() {
key = credential.str("access_token").unwrap_or_default().to_owned();
}
(key, attr("base_url"))
}
fn service_account(credential: &Credential) -> Result<Auth, ExecError> {
let text = |k: &str| credential.str(k).map(str::trim).unwrap_or_default().to_owned();
let mut project = text("project_id");
if project.is_empty() {
project = text("project");
}
if project.is_empty() {
return Err(plain_error("vertex executor: missing project_id in credentials"));
}
let mut location = text("location");
if location.is_empty() {
location = DEFAULT_LOCATION.into();
}
let Some(Value::Object(account)) = credential.metadata.get("service_account") else {
return Err(plain_error("vertex executor: missing service_account in credentials"));
};
let account =
vertex_auth::normalize_service_account(account).map_err(|e| plain_error(format!("vertex executor: {e}")))?;
Ok(Auth::ServiceAccount {
project,
location,
account,
})
}
fn auth_for(credential: &Credential) -> Result<Auth, ExecError> {
let (key, base) = api_creds(credential);
if key.is_empty() {
return service_account(credential);
}
Ok(Auth::ApiKey { key, base })
}
fn openapi_request(mut req: ExecRequest) -> ExecRequest {
let normalized = vertex_openapi::normalize_model(&req.model);
if normalized.is_empty() || normalized == req.model {
return req;
}
if req.original_body.is_empty() && !req.body.is_empty() {
req.original_body = req.body.clone();
}
req.model = normalized;
req
}
fn native_interactions(credential: &Credential, source: Format) -> bool {
if source != Format::Interactions {
return false;
}
let attr = credential.attributes.get("interactions").map(|v| v.trim());
if attr.is_some_and(|v| v.go_eq_fold("true") || v == "1") {
return true;
}
["interactions", "native_interactions"]
.iter()
.any(|k| credential.metadata.get(*k) == Some(&Value::Bool(true)))
}
fn interactions_url(base: &str, project: &str, stream: bool) -> String {
let base = base.trim();
let base = if base.is_empty() { DEFAULT_API_KEY_BASE } else { base }.trim_end_matches('/');
let project = project.trim();
let mut url = if project.is_empty() {
format!("{base}/v1beta1/interactions")
} else {
format!("{base}/v1beta1/projects/{project}/locations/global/interactions")
};
if stream {
url.push_str("?alt=sse");
}
url
}
fn regional_base(location: &str) -> String {
match location.trim() {
"" => format!("https://{DEFAULT_LOCATION}-aiplatform.googleapis.com"),
"global" => DEFAULT_API_KEY_BASE.into(),
loc => format!("https://{loc}-aiplatform.googleapis.com"),
}
}
fn endpoint(auth: &Auth, model: &str, action: &str) -> String {
match auth {
Auth::ApiKey { base, .. } => {
let base = base.trim().trim_end_matches('/');
let base = if base.is_empty() { DEFAULT_API_KEY_BASE } else { base };
if base.contains("/publishers/") {
return format!("{base}/{model}:{action}");
}
let version = if ends_in_version(base) {
String::new()
} else {
format!("/{API_VERSION}")
};
format!("{base}{version}/publishers/google/models/{model}:{action}")
}
Auth::ServiceAccount { project, location, .. } => format!(
"{}/{API_VERSION}/projects/{project}/locations/{location}/publishers/google/models/{model}:{action}",
regional_base(location)
),
}
}
fn ends_in_version(base: &str) -> bool {
let seg = &base[base.rfind('/').map_or(0, |i| i + 1)..];
let Some(rest) = seg.strip_prefix('v') else {
return false;
};
rest.bytes().any(|c| c.is_ascii_digit())
&& rest
.bytes()
.all(|c| c.is_ascii_digit() || c.is_ascii_lowercase() || c == b'_' || c == b'-')
}
fn check_url(url: &str) -> Result<(), ExecError> {
crate::xai_url::new_request(url)?;
if crate::xai_url::transport_refusal(url).is_some() {
return Ok(());
}
url::Url::parse(url).map_err(|_| ExecError::local(500, FailureScope::Request, "invalid upstream URL"))?;
Ok(())
}
fn go_string(value: &gj::Res<'_>) -> String {
let bytes = value.bytes();
let mut out = String::with_capacity(bytes.len());
for chunk in bytes.utf8_chunks() {
out.push_str(chunk.valid());
out.extend(std::iter::repeat_n('\u{FFFD}', chunk.invalid().len()));
}
out
}
fn is_imagen(model: &str) -> bool {
model.go_lower().contains("imagen")
}
fn imagen_request(body: &[u8]) -> Result<Vec<u8>, ExecError> {
let mut prompt = String::new();
let text = gj::get(body, "contents.0.parts.0.text");
if text.exists() {
prompt = go_string(&text);
}
if prompt.is_empty() {
let contents = gj::get(body, "messages.#.content");
if contents.exists()
&& contents.is_array()
&& let Some(first) = contents.array().iter().map(go_string).find(|m| !m.is_empty())
{
prompt = first;
}
}
if prompt.is_empty() {
let direct = gj::get(body, "prompt");
if direct.exists() {
prompt = go_string(&direct);
}
}
if prompt.is_empty() {
return Err(plain_error("imagen: no prompt found in request"));
}
let mut instance = BTreeMap::new();
instance.insert("prompt".to_owned(), GoValue::String(prompt));
let mut parameters = BTreeMap::new();
parameters.insert("sampleCount".to_owned(), GoValue::Number("1".into()));
let aspect = gj::get(body, "aspectRatio");
if aspect.exists() {
parameters.insert("aspectRatio".to_owned(), GoValue::String(go_string(&aspect)));
}
let count = gj::get(body, "sampleCount");
if count.exists() {
parameters.insert("sampleCount".to_owned(), GoValue::Number(count.int().to_string()));
}
let negative = gj::get(body, "negativePrompt");
if negative.exists() {
instance.insert("negativePrompt".to_owned(), GoValue::String(go_string(&negative)));
}
let mut request = BTreeMap::new();
request.insert("instances".to_owned(), GoValue::Array(vec![GoValue::Object(instance)]));
request.insert("parameters".to_owned(), GoValue::Object(parameters));
Ok(GoValue::Object(request).marshal())
}
fn imagen_response(data: &[u8], model: &str) -> Vec<u8> {
let predictions = gj::get(data, "predictions");
if !predictions.exists() || !predictions.is_array() {
return data.to_vec();
}
let string = |s: &str| GoValue::String(s.to_owned());
let mut parts = Vec::new();
for prediction in predictions.array() {
let image = go_string(&prediction.get("bytesBase64Encoded"));
let mut mime = go_string(&prediction.get("mimeType"));
if mime.is_empty() {
mime = "image/png".into();
}
if image.is_empty() {
continue;
}
let inline = BTreeMap::from([
("mimeType".to_owned(), string(&mime)),
("data".to_owned(), string(&image)),
]);
parts.push(GoValue::Object(BTreeMap::from([(
"inlineData".to_owned(),
GoValue::Object(inline),
)])));
}
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_nanos());
let zero = || GoValue::Number("0".into());
let content = BTreeMap::from([
("parts".to_owned(), GoValue::Array(parts)),
("role".to_owned(), string("model")),
]);
let candidate = BTreeMap::from([
("content".to_owned(), GoValue::Object(content)),
("finishReason".to_owned(), string("STOP")),
]);
let usage = BTreeMap::from([
("promptTokenCount".to_owned(), zero()),
("candidatesTokenCount".to_owned(), zero()),
("totalTokenCount".to_owned(), zero()),
]);
GoValue::Object(BTreeMap::from([
(
"candidates".to_owned(),
GoValue::Array(vec![GoValue::Object(candidate)]),
),
("responseId".to_owned(), GoValue::String(format!("imagen-{nanos}"))),
("modelVersion".to_owned(), string(model)),
("usageMetadata".to_owned(), GoValue::Object(usage)),
]))
.marshal()
}
fn strip_tool_call_ids(body: Vec<u8>, source: Format) -> Vec<u8> {
if source != Format::OpenAIResponse {
return body;
}
let contents = gj::get(&body, "contents");
if !contents.is_array() {
return body;
}
let items = contents.array();
let has_ids = items.iter().any(|c| {
let parts = c.get("parts");
parts.is_array()
&& parts
.array()
.iter()
.any(|p| p.get("functionCall.id").exists() || p.get("functionResponse.id").exists())
});
if !has_ids {
return body;
}
let mut changed = false;
let mut out_items: Vec<Vec<u8>> = Vec::with_capacity(items.len());
for content in &items {
let parts = content.get("parts");
if !parts.is_array() {
out_items.push(content.raw().to_vec());
continue;
}
let mut parts_changed = false;
let mut part_items = Vec::new();
for part in parts.array() {
let mut raw = part.raw().to_vec();
for path in ["functionCall.id", "functionResponse.id"] {
if part.get(path).exists()
&& let Ok(updated) = gj::try_delete(&raw, path)
{
raw = updated;
parts_changed = true;
}
}
part_items.push(raw);
}
let mut raw = content.raw().to_vec();
if parts_changed && let Ok(updated) = gj::try_set_raw(&raw, "parts", gj::join(&part_items)) {
raw = updated;
changed = true;
}
out_items.push(raw);
}
if !changed {
return body;
}
gj::try_set_raw(&body, "contents", gj::join(&out_items)).unwrap_or(body)
}
struct RawLines(Output);
impl LineState for RawLines {
fn line(&mut self, line: &[u8]) -> Emit {
self.0.usage.response_line(Format::Gemini, line);
self.0.observe_finish_signal(line);
self.0.translate(&g::line_event(line))
}
fn end(&mut self) -> Emit {
self.0.end_with_done()
}
fn end_after_error(&mut self) -> Emit {
self.0.end_after_error()
}
fn output(&self) -> Option<&Output> {
Some(&self.0)
}
fn flush(&mut self) -> Vec<Bytes> {
self.0.flush_frames()
}
}
impl VertexExecutor {
pub fn with_hooks(hooks: Hooks, now: fn() -> i64) -> Self {
Self {
clients: GoClients::new(hooks),
now,
}
}
fn client(&self, credential: &Credential, cfg: &Config) -> wreq::Client {
self.clients.get(&Proxy::effective(credential, cfg))
}
pub async fn execute(
&self,
credential: &Credential,
req: ExecRequest,
cfg: &Config,
) -> Result<ExecResponse, ExecError> {
let openapi = vertex_openapi::should_execute(credential, &req.model);
if req.operation == Operation::CountTokens {
if openapi {
return crate::openai_compat::count_tokens(credential, &openapi_request(req), cfg);
}
return self.count_tokens(credential, &req, cfg).await;
}
if req.alt.as_deref() == Some(g::COMPACT_ALT) {
return Err(g::status_err(501, "/responses/compact not supported"));
}
if openapi {
return self.openapi(credential, openapi_request(req), cfg).await;
}
if native_interactions(credential, req.source_format) {
return self.interactions(credential, &req, cfg).await;
}
let auth = auth_for(credential)?;
self.generate(credential, &req, cfg, &auth).await
}
async fn openapi(
&self,
credential: &Credential,
req: ExecRequest,
cfg: &Config,
) -> Result<ExecResponse, ExecError> {
let client = self.client(credential, cfg);
let credentials = self.openapi_credentials(&client, credential).await;
let executor = if credentials.is_ok() {
"OpenAICompatExecutor"
} else {
"GeminiVertexExecutor"
};
req.usage
.delegated_executor(executor, &parse_suffix(&req.model).model_name);
let (base_url, token) = credentials?;
crate::openai_compat::chat_with(&client, credential, req, cfg, &base_url, &token, None).await
}
async fn openapi_credentials(
&self,
client: &wreq::Client,
credential: &Credential,
) -> Result<(String, String), ExecError> {
let (key, base) = api_creds(credential);
let has_account = matches!(credential.metadata.get("service_account"), Some(Value::Object(a)) if !a.is_empty());
if key.is_empty() || has_account {
let Auth::ServiceAccount {
project,
location,
account,
} = service_account(credential)?
else {
unreachable!("service_account answers a service account")
};
let token = vertex_auth::access_token(client, &account, (self.now)())
.await
.map_err(|error| {
tracing::error!(%error, "vertex executor: access token error");
g::status_err(500, "internal server error")
})?;
if token.trim().is_empty() {
return Err(g::status_err(401, "missing access token"));
}
let location = vertex_openapi::location(credential, &location);
let base = vertex_openapi::auth_base_url(credential);
return Ok((
vertex_openapi::service_account_base_url(&base, API_VERSION, &project, &location),
token,
));
}
if vertex_openapi::is_google_host(&base) {
return Err(g::status_err(
400,
"vertex endpoints/openapi on aiplatform.googleapis.com requires service account credentials",
));
}
let location = vertex_openapi::location(credential, "");
let project = vertex_openapi::auth_project_id(credential);
Ok((
vertex_openapi::api_key_base_url(&base, API_VERSION, &project, &location),
key,
))
}
async fn interactions(
&self,
credential: &Credential,
req: &ExecRequest,
cfg: &Config,
) -> Result<ExecResponse, ExecError> {
let body = g::interactions_body(req, cfg)?;
let (key, mut base) = api_creds(credential);
let (project, account) = if key.is_empty() {
let Auth::ServiceAccount { project, account, .. } = service_account(credential)? else {
unreachable!("service_account answers a service account")
};
if let Some(attr) = credential.attributes.get("base_url").filter(|b| !b.is_empty()) {
base = attr.trim().to_owned();
}
(project, Some(account))
} else {
(credential.str("project_id").unwrap_or_default().to_owned(), None)
};
let url = interactions_url(&base, &project, req.stream);
check_url(&url)?;
let client = self.client(credential, cfg);
let mut headers = GoHeaders::new();
headers.set("Content-Type", "application/json");
match account {
Some(account) => {
let token = vertex_auth::access_token(&client, &account, (self.now)())
.await
.map_err(|error| {
tracing::error!(%error, "vertex executor: interactions access token error");
g::status_err(500, "internal server error")
})?;
if token.trim().is_empty() {
return Err(g::status_err(401, "missing access token"));
}
headers.set("Authorization", format!("Bearer {token}"));
}
None => headers.set("x-goog-api-key", key),
}
g::set_custom_headers(&mut headers, credential, req);
g::interactions_revision(&mut headers, req);
g::interactions_exchange(req, credential, PROVIDER, &client, &url, headers, body).await
}
async fn headers(
&self,
credential: &Credential,
req: &ExecRequest,
cfg: &Config,
auth: &Auth,
) -> Result<GoHeaders, ExecError> {
let mut headers = GoHeaders::new();
headers.set("Content-Type", "application/json");
match auth {
Auth::ApiKey { key, .. } => headers.set("x-goog-api-key", key.clone()),
Auth::ServiceAccount { account, .. } => {
let token = vertex_auth::access_token(&self.client(credential, cfg), account, (self.now)())
.await
.map_err(|error| {
tracing::error!(%error, "vertex executor: access token error");
g::status_err(500, "internal server error")
})?;
if !token.is_empty() {
headers.set("Authorization", format!("Bearer {token}"));
}
}
}
g::set_custom_headers(&mut headers, credential, req);
Ok(headers)
}
fn prepared_body(
&self,
req: &ExecRequest,
cfg: &Config,
base_model: &str,
) -> Result<(Vec<u8>, Vec<u8>), ExecError> {
let (from, to) = (req.source_format, Format::Gemini);
let resolved = g::resolved(req);
let original = g::translate(req, cfg, to, base_model, g::original_request(req), req.stream, false);
let body = g::translate(req, cfg, to, base_model, &req.body, req.stream, false)?;
let original = crate::translate::baseline_or_working(original, &body);
let mut body = g::apply_thinking(req, body, from, to, PROVIDER, resolved.as_ref())?;
body = payload::fix_image_aspect_ratio(base_model, body);
body = payload::set_str_if_different(body, "model", base_model);
body = strip_tool_call_ids(body, from);
let body = cpa_common::signature::sanitize_gemini_request_thought_signatures(&body, "contents");
Ok((original, body))
}
async fn generate(
&self,
credential: &Credential,
req: &ExecRequest,
cfg: &Config,
auth: &Auth,
) -> Result<ExecResponse, ExecError> {
let base_model = parse_suffix(&req.model).model_name;
let imagen = is_imagen(&base_model);
let imagen_wire = imagen && !req.stream && matches!(auth, Auth::ServiceAccount { .. });
let (original, mut body) = if imagen_wire {
let body = imagen_request(&req.body)?;
let original = if req.original_body.is_empty() {
body.clone()
} else {
imagen_request(&req.original_body)?
};
(original, body)
} else {
self.prepared_body(req, cfg, &base_model)?
};
if req.stream {
body = payload::drop_trailing_thought_only_content(body, "contents");
}
body = payload::ensure_leading_user_content(body, "contents");
if !req.stream {
body = payload::drop_trailing_thought_only_content(body, "contents");
}
body = payload::ensure_trailing_user_content(body, "contents");
let action = match (imagen, req.stream) {
(true, _) => "predict",
(false, true) => "streamGenerateContent",
(false, false) => "generateContent",
};
let mut url = endpoint(auth, &base_model, action);
match (req.stream, g::alt(req)) {
(true, _) if imagen => {}
(true, None) => url.push_str("?alt=sse"),
(_, Some(alt)) => url.push_str(&format!("?$alt={alt}")),
(false, None) => {}
}
body = payload::delete(body, "session_id");
req.usage.request(Format::Gemini, &body);
body = Barrier {
executor: "",
model: &base_model,
protocol: Format::Gemini.as_str(),
root: "",
original: &original,
}
.apply(cfg, req, body);
check_url(&url)?;
let headers = self.headers(credential, req, cfg, auth).await?;
let capture = req.capture();
let client = self.client(credential, cfg);
let body = Bytes::from(body);
let upstream = g::send_captured(
capture,
credential,
PROVIDER,
&client,
&url,
headers,
body.clone(),
req.stream,
)
.await?;
if !(200..300).contains(&upstream.status) {
return Err(g::error_from(capture, upstream).await);
}
let response_headers = upstream.headers.clone();
if !req.stream {
let mut data = g::read_captured(capture, upstream, usize::MAX).await?.to_vec();
req.usage.response_body(Format::Gemini, &data);
if imagen_wire {
data = imagen_response(&data, &base_model);
}
let out = g::translate_non_stream(req, Format::Gemini, &body, &data)?;
g::claude_error(req, &out)?;
return Ok(ExecResponse {
status: 200,
headers: response_headers,
body: ResponseBody::Buffered(out),
});
}
let output = Output::new(req, Format::Gemini, &body)
.with_claude_stream_errors()
.require_finish_signal()
.fail_on_client_drop(g::DropRule::BeforeTerminal { delivered: false });
let lines = g::captured_lines(proxy::lines(upstream.body, g::MAX_LINE), capture.clone());
let stream = g::drive(lines, RawLines(output));
Ok(ExecResponse {
status: 200,
headers: response_headers,
body: ResponseBody::Stream(stream),
})
}
async fn count_tokens(
&self,
credential: &Credential,
req: &ExecRequest,
cfg: &Config,
) -> Result<ExecResponse, ExecError> {
let auth = auth_for(credential)?;
let base_model = parse_suffix(&req.model).model_name;
let (from, to) = (req.source_format, Format::Gemini);
let resolved = g::resolved(req);
let body = g::translate(req, cfg, to, &base_model, &req.body, false, false)?;
let original = g::count_original(req, cfg, to, &base_model, false, &body)?;
let mut body = g::apply_thinking(req, body, from, to, PROVIDER, resolved.as_ref())?;
body = payload::fix_image_aspect_ratio(&base_model, body);
body = gj::try_set_str(&body, "model", &base_model).unwrap_or(body);
body = strip_tool_call_ids(body, from);
for path in ["tools", "generationConfig", "safetySettings"] {
body = payload::delete(body, path);
}
body = cpa_common::signature::sanitize_gemini_request_thought_signatures(&body, "contents");
body = payload::ensure_leading_user_content(body, "contents");
body = Barrier {
executor: PROVIDER,
model: &base_model,
protocol: to.as_str(),
root: "",
original: &original,
}
.apply(cfg, req, body);
let url = endpoint(&auth, &base_model, "countTokens");
check_url(&url)?;
let headers = self.headers(credential, req, cfg, &auth).await?;
let capture = req.capture();
let client = self.client(credential, cfg);
let body = Bytes::from(body);
let upstream = g::send_captured(
capture,
credential,
PROVIDER,
&client,
&url,
headers,
body.clone(),
false,
)
.await?;
if !(200..300).contains(&upstream.status) {
return Err(g::error_from(capture, upstream).await);
}
let response_headers = upstream.headers.clone();
let data = g::read_captured(capture, upstream, usize::MAX).await?;
let count = gj::get(&data, "totalTokens").int();
let out = cpa_translate::translate_token_count(req.response_format, to, count, &data);
Ok(ExecResponse {
status: 200,
headers: response_headers,
body: ResponseBody::Buffered(Bytes::from(out)),
})
}
}
pub fn handles(provider: &str) -> bool {
provider == PROVIDER
}
#[cfg(test)]
#[path = "vertex_tests.rs"]
mod tests;