Skip to main content

cpa_exec/
vertex.rs

1//! Vertex AI (internal/runtime/executor/gemini_vertex_executor.go).
2//!
3//! Two credential kinds share the Gemini request pipeline:
4//! - Imported service-account files (`type: vertex`, `-vertex-import`): every request
5//!   first exchanges a signed JWT for an access token (`vertex_auth`), then calls
6//!   `https://<location>-aiplatform.googleapis.com` (or the global host) under
7//!   `/v1/projects/<project>/locations/<location>/publishers/google/models/<model>`.
8//! - API keys (`api-keys.vertex`, or a file credential's `access_token`): `x-goog-api-key`
9//!   at `<base-url>/v1/publishers/google/models/<model>`, base URL used as configured.
10//!
11//! An Interactions client on a credential that opted in (`interactions`) instead reaches
12//! the native Interactions API at `/v1beta1/interactions`, built as the Gemini executor
13//! builds it (Go 2d5351b3).
14//!
15//! Unlike the Gemini API-key executor, Vertex translates without the is-compat variants,
16//! does not cap `maxOutputTokens`, strips Responses tool-call IDs, sends Imagen models to
17//! `:predict`, and hands every scanned stream line to the translator unfiltered.
18
19use cpa_core::gostr::GoStr;
20use std::collections::BTreeMap;
21
22use bytes::Bytes;
23use cpa_common::json::{self as gj, GoValue};
24use cpa_common::thinking::parse_suffix;
25use cpa_core::config::Config;
26use cpa_core::credential::Credential;
27use cpa_core::exec::{ExecError, ExecRequest, ExecResponse, FailureScope, Operation, ResponseBody};
28use cpa_core::format::Format;
29use serde_json::{Map, Value};
30
31use crate::gemini::{self as g, Emit, LineState, Output};
32use crate::gemini_payload as payload;
33use crate::payload_barrier::Barrier;
34use crate::proxy::{self, GoClients, GoHeaders, Hooks, Proxy};
35use crate::{vertex_auth, vertex_openapi};
36
37pub const PROVIDER: &str = "vertex";
38const API_VERSION: &str = "v1";
39const DEFAULT_API_KEY_BASE: &str = "https://aiplatform.googleapis.com";
40const DEFAULT_LOCATION: &str = "us-central1";
41
42pub struct VertexExecutor {
43    clients: GoClients,
44    /// Unix seconds for the JWT `iat` (tests pin it to Go's recorded clock).
45    now: fn() -> i64,
46}
47
48fn system_now() -> i64 {
49    std::time::SystemTime::now()
50        .duration_since(std::time::UNIX_EPOCH)
51        .map_or(0, |d| d.as_secs() as i64)
52}
53
54impl Default for VertexExecutor {
55    fn default() -> Self {
56        Self::with_hooks(Hooks::default(), system_now)
57    }
58}
59
60/// How one request authenticates.
61enum Auth {
62    /// `x-goog-api-key` at `base` (empty: the global endpoint).
63    ApiKey { key: String, base: String },
64    ServiceAccount {
65        project: String,
66        location: String,
67        account: Map<String, Value>,
68    },
69}
70
71/// A Go error without a status code: the handler answers it with 500.
72fn plain_error(message: impl Into<String>) -> ExecError {
73    ExecError::local(500, FailureScope::Credential, message)
74}
75
76/// `vertexAPICreds`: the `api_key` and `base_url` attributes as stored, else the file's
77/// `access_token`.
78fn api_creds(credential: &Credential) -> (String, String) {
79    let attr = |k: &str| credential.attributes.get(k).cloned().unwrap_or_default();
80    let mut key = attr("api_key");
81    if key.is_empty() {
82        key = credential.str("access_token").unwrap_or_default().to_owned();
83    }
84    (key, attr("base_url"))
85}
86
87/// `vertexCreds`: project (`project_id`, then `project`), location (default
88/// us-central1) and the normalized service account.
89fn service_account(credential: &Credential) -> Result<Auth, ExecError> {
90    let text = |k: &str| credential.str(k).map(str::trim).unwrap_or_default().to_owned();
91    let mut project = text("project_id");
92    if project.is_empty() {
93        project = text("project");
94    }
95    if project.is_empty() {
96        return Err(plain_error("vertex executor: missing project_id in credentials"));
97    }
98    let mut location = text("location");
99    if location.is_empty() {
100        location = DEFAULT_LOCATION.into();
101    }
102    let Some(Value::Object(account)) = credential.metadata.get("service_account") else {
103        return Err(plain_error("vertex executor: missing service_account in credentials"));
104    };
105    let account =
106        vertex_auth::normalize_service_account(account).map_err(|e| plain_error(format!("vertex executor: {e}")))?;
107    Ok(Auth::ServiceAccount {
108        project,
109        location,
110        account,
111    })
112}
113
114fn auth_for(credential: &Credential) -> Result<Auth, ExecError> {
115    let (key, base) = api_creds(credential);
116    if key.is_empty() {
117        return service_account(credential);
118    }
119    Ok(Auth::ApiKey { key, base })
120}
121
122/// `normalizeVertexOpenAPIRequest`: the `<publisher>/<model>` upstream model; the client
123/// payload stays the payload-rule baseline.
124fn openapi_request(mut req: ExecRequest) -> ExecRequest {
125    let normalized = vertex_openapi::normalize_model(&req.model);
126    if normalized.is_empty() || normalized == req.model {
127        return req;
128    }
129    if req.original_body.is_empty() && !req.body.is_empty() {
130        req.original_body = req.body.clone();
131    }
132    req.model = normalized;
133    req
134}
135
136/// Go 2d5351b3 `shouldExecuteVertexInteractions`: an Interactions client on a credential
137/// that opted into the native API (`interactions` attribute true or 1, or a file's
138/// `interactions` / `native_interactions`).
139fn native_interactions(credential: &Credential, source: Format) -> bool {
140    if source != Format::Interactions {
141        return false;
142    }
143    let attr = credential.attributes.get("interactions").map(|v| v.trim());
144    // strings.EqualFold with "true": no letter of it has a non-ASCII fold partner but
145    // the Kelvin sign, which "true" lacks.
146    if attr.is_some_and(|v| v.go_eq_fold("true") || v == "1") {
147        return true;
148    }
149    ["interactions", "native_interactions"]
150        .iter()
151        .any(|k| credential.metadata.get(*k) == Some(&Value::Bool(true)))
152}
153
154/// `vertexInteractionsURL`.
155fn interactions_url(base: &str, project: &str, stream: bool) -> String {
156    let base = base.trim();
157    let base = if base.is_empty() { DEFAULT_API_KEY_BASE } else { base }.trim_end_matches('/');
158    let project = project.trim();
159    let mut url = if project.is_empty() {
160        format!("{base}/v1beta1/interactions")
161    } else {
162        format!("{base}/v1beta1/projects/{project}/locations/global/interactions")
163    };
164    if stream {
165        url.push_str("?alt=sse");
166    }
167    url
168}
169
170/// `vertexBaseURL`.
171fn regional_base(location: &str) -> String {
172    match location.trim() {
173        "" => format!("https://{DEFAULT_LOCATION}-aiplatform.googleapis.com"),
174        "global" => DEFAULT_API_KEY_BASE.into(),
175        loc => format!("https://{loc}-aiplatform.googleapis.com"),
176    }
177}
178
179fn endpoint(auth: &Auth, model: &str, action: &str) -> String {
180    match auth {
181        // `vertexAPIKeyModelsURL` (Go 2d7f5377): a base holding `/publishers/` is the
182        // full models prefix; otherwise `/v1` is added unless the base ends in a
183        // version segment such as `/v1beta1`.
184        Auth::ApiKey { base, .. } => {
185            let base = base.trim().trim_end_matches('/');
186            let base = if base.is_empty() { DEFAULT_API_KEY_BASE } else { base };
187            if base.contains("/publishers/") {
188                return format!("{base}/{model}:{action}");
189            }
190            let version = if ends_in_version(base) {
191                String::new()
192            } else {
193                format!("/{API_VERSION}")
194            };
195            format!("{base}{version}/publishers/google/models/{model}:{action}")
196        }
197        Auth::ServiceAccount { project, location, .. } => format!(
198            "{}/{API_VERSION}/projects/{project}/locations/{location}/publishers/google/models/{model}:{action}",
199            regional_base(location)
200        ),
201    }
202}
203
204/// `vertexAPIVersionSuffix`: the last path segment is `v`, then lower-case letters,
205/// digits, `_` or `-`, with at least one digit.
206fn ends_in_version(base: &str) -> bool {
207    let seg = &base[base.rfind('/').map_or(0, |i| i + 1)..];
208    let Some(rest) = seg.strip_prefix('v') else {
209        return false;
210    };
211    rest.bytes().any(|c| c.is_ascii_digit())
212        && rest
213            .bytes()
214            .all(|c| c.is_ascii_digit() || c.is_ascii_lowercase() || c == b'_' || c == b'-')
215}
216
217/// `http.NewRequestWithContext`'s URL parse. Go builds the request before the token
218/// exchange, so a URL it rejects fails without contacting the token endpoint. A URL
219/// its transport refuses (no host, or a scheme other than http and https) passes here
220/// and fails at the send ([`g::send_captured`]).
221fn check_url(url: &str) -> Result<(), ExecError> {
222    crate::xai_url::new_request(url)?;
223    if crate::xai_url::transport_refusal(url).is_some() {
224        return Ok(());
225    }
226    // The shared client's own URL check (`proxy::send`), before the token exchange too.
227    url::Url::parse(url).map_err(|_| ExecError::local(500, FailureScope::Request, "invalid upstream URL"))?;
228    Ok(())
229}
230
231/// gjson's `String()` as Go's `json.Marshal` writes it back: each invalid UTF-8 byte
232/// becomes U+FFFD.
233fn go_string(value: &gj::Res<'_>) -> String {
234    let bytes = value.bytes();
235    let mut out = String::with_capacity(bytes.len());
236    for chunk in bytes.utf8_chunks() {
237        out.push_str(chunk.valid());
238        out.extend(std::iter::repeat_n('\u{FFFD}', chunk.invalid().len()));
239    }
240    out
241}
242
243/// `isImagenModel`.
244fn is_imagen(model: &str) -> bool {
245    model.go_lower().contains("imagen")
246}
247
248/// `convertToImagenRequest`: the prompt from `contents.0.parts.0.text`, the first
249/// non-empty `messages[].content`, or `prompt`, plus the optional Imagen parameters.
250fn imagen_request(body: &[u8]) -> Result<Vec<u8>, ExecError> {
251    let mut prompt = String::new();
252    let text = gj::get(body, "contents.0.parts.0.text");
253    if text.exists() {
254        prompt = go_string(&text);
255    }
256    if prompt.is_empty() {
257        let contents = gj::get(body, "messages.#.content");
258        if contents.exists()
259            && contents.is_array()
260            && let Some(first) = contents.array().iter().map(go_string).find(|m| !m.is_empty())
261        {
262            prompt = first;
263        }
264    }
265    if prompt.is_empty() {
266        let direct = gj::get(body, "prompt");
267        if direct.exists() {
268            prompt = go_string(&direct);
269        }
270    }
271    if prompt.is_empty() {
272        return Err(plain_error("imagen: no prompt found in request"));
273    }
274    let mut instance = BTreeMap::new();
275    instance.insert("prompt".to_owned(), GoValue::String(prompt));
276    let mut parameters = BTreeMap::new();
277    parameters.insert("sampleCount".to_owned(), GoValue::Number("1".into()));
278    let aspect = gj::get(body, "aspectRatio");
279    if aspect.exists() {
280        parameters.insert("aspectRatio".to_owned(), GoValue::String(go_string(&aspect)));
281    }
282    let count = gj::get(body, "sampleCount");
283    if count.exists() {
284        parameters.insert("sampleCount".to_owned(), GoValue::Number(count.int().to_string()));
285    }
286    let negative = gj::get(body, "negativePrompt");
287    if negative.exists() {
288        instance.insert("negativePrompt".to_owned(), GoValue::String(go_string(&negative)));
289    }
290    let mut request = BTreeMap::new();
291    request.insert("instances".to_owned(), GoValue::Array(vec![GoValue::Object(instance)]));
292    request.insert("parameters".to_owned(), GoValue::Object(parameters));
293    Ok(GoValue::Object(request).marshal())
294}
295
296/// `convertImagenToGeminiResponse`: predictions with image bytes become `inlineData`
297/// parts of one Gemini candidate. `responseId` is `imagen-<UnixNano>` like Go's.
298fn imagen_response(data: &[u8], model: &str) -> Vec<u8> {
299    let predictions = gj::get(data, "predictions");
300    if !predictions.exists() || !predictions.is_array() {
301        return data.to_vec();
302    }
303    let string = |s: &str| GoValue::String(s.to_owned());
304    let mut parts = Vec::new();
305    for prediction in predictions.array() {
306        let image = go_string(&prediction.get("bytesBase64Encoded"));
307        let mut mime = go_string(&prediction.get("mimeType"));
308        if mime.is_empty() {
309            mime = "image/png".into();
310        }
311        if image.is_empty() {
312            continue;
313        }
314        let inline = BTreeMap::from([
315            ("mimeType".to_owned(), string(&mime)),
316            ("data".to_owned(), string(&image)),
317        ]);
318        parts.push(GoValue::Object(BTreeMap::from([(
319            "inlineData".to_owned(),
320            GoValue::Object(inline),
321        )])));
322    }
323    let nanos = std::time::SystemTime::now()
324        .duration_since(std::time::UNIX_EPOCH)
325        .map_or(0, |d| d.as_nanos());
326    let zero = || GoValue::Number("0".into());
327    let content = BTreeMap::from([
328        ("parts".to_owned(), GoValue::Array(parts)),
329        ("role".to_owned(), string("model")),
330    ]);
331    let candidate = BTreeMap::from([
332        ("content".to_owned(), GoValue::Object(content)),
333        ("finishReason".to_owned(), string("STOP")),
334    ]);
335    let usage = BTreeMap::from([
336        ("promptTokenCount".to_owned(), zero()),
337        ("candidatesTokenCount".to_owned(), zero()),
338        ("totalTokenCount".to_owned(), zero()),
339    ]);
340    GoValue::Object(BTreeMap::from([
341        (
342            "candidates".to_owned(),
343            GoValue::Array(vec![GoValue::Object(candidate)]),
344        ),
345        ("responseId".to_owned(), GoValue::String(format!("imagen-{nanos}"))),
346        ("modelVersion".to_owned(), string(model)),
347        ("usageMetadata".to_owned(), GoValue::Object(usage)),
348    ]))
349    .marshal()
350}
351
352/// `helps.StripVertexOpenAIResponsesToolCallIDs`: Vertex rejects the Responses call IDs
353/// the translator keeps on `functionCall` and `functionResponse` parts.
354fn strip_tool_call_ids(body: Vec<u8>, source: Format) -> Vec<u8> {
355    if source != Format::OpenAIResponse {
356        return body;
357    }
358    let contents = gj::get(&body, "contents");
359    if !contents.is_array() {
360        return body;
361    }
362    let items = contents.array();
363    let has_ids = items.iter().any(|c| {
364        let parts = c.get("parts");
365        parts.is_array()
366            && parts
367                .array()
368                .iter()
369                .any(|p| p.get("functionCall.id").exists() || p.get("functionResponse.id").exists())
370    });
371    if !has_ids {
372        return body;
373    }
374    let mut changed = false;
375    let mut out_items: Vec<Vec<u8>> = Vec::with_capacity(items.len());
376    for content in &items {
377        let parts = content.get("parts");
378        if !parts.is_array() {
379            out_items.push(content.raw().to_vec());
380            continue;
381        }
382        let mut parts_changed = false;
383        let mut part_items = Vec::new();
384        for part in parts.array() {
385            let mut raw = part.raw().to_vec();
386            for path in ["functionCall.id", "functionResponse.id"] {
387                if part.get(path).exists()
388                    && let Ok(updated) = gj::try_delete(&raw, path)
389                {
390                    raw = updated;
391                    parts_changed = true;
392                }
393            }
394            part_items.push(raw);
395        }
396        let mut raw = content.raw().to_vec();
397        if parts_changed && let Ok(updated) = gj::try_set_raw(&raw, "parts", gj::join(&part_items)) {
398            raw = updated;
399            changed = true;
400        }
401        out_items.push(raw);
402    }
403    if !changed {
404        return body;
405    }
406    gj::try_set_raw(&body, "contents", gj::join(&out_items)).unwrap_or(body)
407}
408
409/// Vertex hands every scanned line, prefixes and blank lines included, to the
410/// translator; then the tool-input finalization and `[DONE]`.
411struct RawLines(Output);
412
413impl LineState for RawLines {
414    fn line(&mut self, line: &[u8]) -> Emit {
415        // Go's reporter sees each raw line (model and usage, unfiltered).
416        self.0.usage.response_line(Format::Gemini, line);
417        self.0.observe_finish_signal(line);
418        self.0.translate(&g::line_event(line))
419    }
420
421    fn end(&mut self) -> Emit {
422        self.0.end_with_done()
423    }
424
425    fn end_after_error(&mut self) -> Emit {
426        self.0.end_after_error()
427    }
428
429    fn output(&self) -> Option<&Output> {
430        Some(&self.0)
431    }
432
433    fn flush(&mut self) -> Vec<Bytes> {
434        self.0.flush_frames()
435    }
436}
437
438impl VertexExecutor {
439    /// Tests pass trust and resolve hooks and a fixed clock.
440    pub fn with_hooks(hooks: Hooks, now: fn() -> i64) -> Self {
441        Self {
442            clients: GoClients::new(hooks),
443            now,
444        }
445    }
446
447    fn client(&self, credential: &Credential, cfg: &Config) -> wreq::Client {
448        self.clients.get(&Proxy::effective(credential, cfg))
449    }
450
451    pub async fn execute(
452        &self,
453        credential: &Credential,
454        req: ExecRequest,
455        cfg: &Config,
456    ) -> Result<ExecResponse, ExecError> {
457        // Go d9451cb6: partner and open models, and credentials that opt in, take the
458        // endpoints/openapi route, before the Interactions check.
459        let openapi = vertex_openapi::should_execute(credential, &req.model);
460        if req.operation == Operation::CountTokens {
461            if openapi {
462                return crate::openai_compat::count_tokens(credential, &openapi_request(req), cfg);
463            }
464            return self.count_tokens(credential, &req, cfg).await;
465        }
466        if req.alt.as_deref() == Some(g::COMPACT_ALT) {
467            return Err(g::status_err(501, "/responses/compact not supported"));
468        }
469        if openapi {
470            return self.openapi(credential, openapi_request(req), cfg).await;
471        }
472        if native_interactions(credential, req.source_format) {
473            return self.interactions(credential, &req, cfg).await;
474        }
475        let auth = auth_for(credential)?;
476        self.generate(credential, &req, cfg, &auth).await
477    }
478
479    /// Go d9451cb6 `executeOpenAPI` / `executeOpenAPIStream`: the OpenAI-compatible
480    /// executor's chat request at the credential's endpoints/openapi base.
481    async fn openapi(
482        &self,
483        credential: &Credential,
484        req: ExecRequest,
485        cfg: &Config,
486    ) -> Result<ExecResponse, ExecError> {
487        let client = self.client(credential, cfg);
488        let credentials = self.openapi_credentials(&client, credential).await;
489        // Go reports a credential failure as the Vertex executor and everything after
490        // it through `OpenAICompatExecutor`'s reporter; both name the normalized model.
491        let executor = if credentials.is_ok() {
492            "OpenAICompatExecutor"
493        } else {
494            "GeminiVertexExecutor"
495        };
496        req.usage
497            .delegated_executor(executor, &parse_suffix(&req.model).model_name);
498        let (base_url, token) = credentials?;
499        crate::openai_compat::chat_with(&client, credential, req, cfg, &base_url, &token, None).await
500    }
501
502    /// `resolveOpenAPICredentials`: a service account (also when the file carries an
503    /// access token beside it) sends a fresh token to its project's route; an API key
504    /// only to a third-party relay, since Google's hosts need OAuth.
505    async fn openapi_credentials(
506        &self,
507        client: &wreq::Client,
508        credential: &Credential,
509    ) -> Result<(String, String), ExecError> {
510        let (key, base) = api_creds(credential);
511        let has_account = matches!(credential.metadata.get("service_account"), Some(Value::Object(a)) if !a.is_empty());
512        if key.is_empty() || has_account {
513            let Auth::ServiceAccount {
514                project,
515                location,
516                account,
517            } = service_account(credential)?
518            else {
519                unreachable!("service_account answers a service account")
520            };
521            let token = vertex_auth::access_token(client, &account, (self.now)())
522                .await
523                .map_err(|error| {
524                    tracing::error!(%error, "vertex executor: access token error");
525                    g::status_err(500, "internal server error")
526                })?;
527            if token.trim().is_empty() {
528                return Err(g::status_err(401, "missing access token"));
529            }
530            let location = vertex_openapi::location(credential, &location);
531            let base = vertex_openapi::auth_base_url(credential);
532            return Ok((
533                vertex_openapi::service_account_base_url(&base, API_VERSION, &project, &location),
534                token,
535            ));
536        }
537        if vertex_openapi::is_google_host(&base) {
538            return Err(g::status_err(
539                400,
540                "vertex endpoints/openapi on aiplatform.googleapis.com requires service account credentials",
541            ));
542        }
543        let location = vertex_openapi::location(credential, "");
544        let project = vertex_openapi::auth_project_id(credential);
545        Ok((
546            vertex_openapi::api_key_base_url(&base, API_VERSION, &project, &location),
547            key,
548        ))
549    }
550
551    /// Go 2d5351b3 `executeInteractions` / `executeInteractionsStream`: the Interactions
552    /// request as the Gemini executor builds it, at `<base>/v1beta1/interactions`
553    /// (under `projects/<project>/locations/global` when a project is known).
554    async fn interactions(
555        &self,
556        credential: &Credential,
557        req: &ExecRequest,
558        cfg: &Config,
559    ) -> Result<ExecResponse, ExecError> {
560        let body = g::interactions_body(req, cfg)?;
561        let (key, mut base) = api_creds(credential);
562        let (project, account) = if key.is_empty() {
563            let Auth::ServiceAccount { project, account, .. } = service_account(credential)? else {
564                unreachable!("service_account answers a service account")
565            };
566            if let Some(attr) = credential.attributes.get("base_url").filter(|b| !b.is_empty()) {
567                base = attr.trim().to_owned();
568            }
569            (project, Some(account))
570        } else {
571            (credential.str("project_id").unwrap_or_default().to_owned(), None)
572        };
573        // Go builds the request before the token exchange: a URL it rejects fails
574        // without a token request.
575        let url = interactions_url(&base, &project, req.stream);
576        check_url(&url)?;
577        let client = self.client(credential, cfg);
578        let mut headers = GoHeaders::new();
579        headers.set("Content-Type", "application/json");
580        match account {
581            Some(account) => {
582                let token = vertex_auth::access_token(&client, &account, (self.now)())
583                    .await
584                    .map_err(|error| {
585                        tracing::error!(%error, "vertex executor: interactions access token error");
586                        g::status_err(500, "internal server error")
587                    })?;
588                if token.trim().is_empty() {
589                    return Err(g::status_err(401, "missing access token"));
590                }
591                headers.set("Authorization", format!("Bearer {token}"));
592            }
593            None => headers.set("x-goog-api-key", key),
594        }
595        g::set_custom_headers(&mut headers, credential, req);
596        g::interactions_revision(&mut headers, req);
597        g::interactions_exchange(req, credential, PROVIDER, &client, &url, headers, body).await
598    }
599
600    /// Content-Type, the credential (API key, or a fresh access token) and the custom
601    /// headers.
602    async fn headers(
603        &self,
604        credential: &Credential,
605        req: &ExecRequest,
606        cfg: &Config,
607        auth: &Auth,
608    ) -> Result<GoHeaders, ExecError> {
609        let mut headers = GoHeaders::new();
610        headers.set("Content-Type", "application/json");
611        match auth {
612            Auth::ApiKey { key, .. } => headers.set("x-goog-api-key", key.clone()),
613            Auth::ServiceAccount { account, .. } => {
614                // Token exchange uses the credential's proxy, like the model request.
615                let token = vertex_auth::access_token(&self.client(credential, cfg), account, (self.now)())
616                    .await
617                    .map_err(|error| {
618                        tracing::error!(%error, "vertex executor: access token error");
619                        g::status_err(500, "internal server error")
620                    })?;
621                if !token.is_empty() {
622                    headers.set("Authorization", format!("Bearer {token}"));
623                }
624            }
625        }
626        g::set_custom_headers(&mut headers, credential, req);
627        Ok(headers)
628    }
629
630    /// The translated original (the payload-rule baseline) and the translated body
631    /// through Go's Vertex request edits, before the boundary turns.
632    fn prepared_body(
633        &self,
634        req: &ExecRequest,
635        cfg: &Config,
636        base_model: &str,
637    ) -> Result<(Vec<u8>, Vec<u8>), ExecError> {
638        let (from, to) = (req.source_format, Format::Gemini);
639        let resolved = g::resolved(req);
640        let original = g::translate(req, cfg, to, base_model, g::original_request(req), req.stream, false);
641        let body = g::translate(req, cfg, to, base_model, &req.body, req.stream, false)?;
642        let original = crate::translate::baseline_or_working(original, &body);
643        let mut body = g::apply_thinking(req, body, from, to, PROVIDER, resolved.as_ref())?;
644        body = payload::fix_image_aspect_ratio(base_model, body);
645        body = payload::set_str_if_different(body, "model", base_model);
646        body = strip_tool_call_ids(body, from);
647        let body = cpa_common::signature::sanitize_gemini_request_thought_signatures(&body, "contents");
648        Ok((original, body))
649    }
650
651    async fn generate(
652        &self,
653        credential: &Credential,
654        req: &ExecRequest,
655        cfg: &Config,
656        auth: &Auth,
657    ) -> Result<ExecResponse, ExecError> {
658        let base_model = parse_suffix(&req.model).model_name;
659        let imagen = is_imagen(&base_model);
660        // Only the service-account Execute path speaks Imagen's own request format.
661        let imagen_wire = imagen && !req.stream && matches!(auth, Auth::ServiceAccount { .. });
662        let (original, mut body) = if imagen_wire {
663            let body = imagen_request(&req.body)?;
664            let original = if req.original_body.is_empty() {
665                body.clone()
666            } else {
667                imagen_request(&req.original_body)?
668            };
669            (original, body)
670        } else {
671            self.prepared_body(req, cfg, &base_model)?
672        };
673        // Go drops trailing thought-only turns after the leading user turn on Execute
674        // and before it on ExecuteStream; with a single model turn the order matters.
675        if req.stream {
676            body = payload::drop_trailing_thought_only_content(body, "contents");
677        }
678        body = payload::ensure_leading_user_content(body, "contents");
679        if !req.stream {
680            body = payload::drop_trailing_thought_only_content(body, "contents");
681        }
682        body = payload::ensure_trailing_user_content(body, "contents");
683        let action = match (imagen, req.stream) {
684            (true, _) => "predict",
685            (false, true) => "streamGenerateContent",
686            (false, false) => "generateContent",
687        };
688        let mut url = endpoint(auth, &base_model, action);
689        match (req.stream, g::alt(req)) {
690            (true, _) if imagen => {}
691            (true, None) => url.push_str("?alt=sse"),
692            (_, Some(alt)) => url.push_str(&format!("?$alt={alt}")),
693            (false, None) => {}
694        }
695        body = payload::delete(body, "session_id");
696        req.usage.request(Format::Gemini, &body);
697        body = Barrier {
698            executor: "",
699            model: &base_model,
700            protocol: Format::Gemini.as_str(),
701            root: "",
702            original: &original,
703        }
704        .apply(cfg, req, body);
705        check_url(&url)?;
706        let headers = self.headers(credential, req, cfg, auth).await?;
707        let capture = req.capture();
708        let client = self.client(credential, cfg);
709        let body = Bytes::from(body);
710        let upstream = g::send_captured(
711            capture,
712            credential,
713            PROVIDER,
714            &client,
715            &url,
716            headers,
717            body.clone(),
718            req.stream,
719        )
720        .await?;
721        if !(200..300).contains(&upstream.status) {
722            return Err(g::error_from(capture, upstream).await);
723        }
724        let response_headers = upstream.headers.clone();
725        if !req.stream {
726            // Go logs the raw body, before any Imagen conversion.
727            let mut data = g::read_captured(capture, upstream, usize::MAX).await?.to_vec();
728            // ponytail: Go observes the response model on the raw body but parses usage
729            // after the Imagen conversion, whose usageMetadata is all zeros; Imagen
730            // predict responses carry no usageMetadata, so the raw body gives the same.
731            req.usage.response_body(Format::Gemini, &data);
732            if imagen_wire {
733                data = imagen_response(&data, &base_model);
734            }
735            let out = g::translate_non_stream(req, Format::Gemini, &body, &data)?;
736            g::claude_error(req, &out)?;
737            return Ok(ExecResponse {
738                status: 200,
739                headers: response_headers,
740                body: ResponseBody::Buffered(out),
741            });
742        }
743        let output = Output::new(req, Format::Gemini, &body)
744            .with_claude_stream_errors()
745            .require_finish_signal()
746            .fail_on_client_drop(g::DropRule::BeforeTerminal { delivered: false });
747        let lines = g::captured_lines(proxy::lines(upstream.body, g::MAX_LINE), capture.clone());
748        let stream = g::drive(lines, RawLines(output));
749        Ok(ExecResponse {
750            status: 200,
751            headers: response_headers,
752            body: ResponseBody::Stream(stream),
753        })
754    }
755
756    /// `countTokensWith*`: the translated request without tools, generation and safety
757    /// settings, a leading user turn only.
758    async fn count_tokens(
759        &self,
760        credential: &Credential,
761        req: &ExecRequest,
762        cfg: &Config,
763    ) -> Result<ExecResponse, ExecError> {
764        let auth = auth_for(credential)?;
765        let base_model = parse_suffix(&req.model).model_name;
766        let (from, to) = (req.source_format, Format::Gemini);
767        let resolved = g::resolved(req);
768        let body = g::translate(req, cfg, to, &base_model, &req.body, false, false)?;
769        let original = g::count_original(req, cfg, to, &base_model, false, &body)?;
770        let mut body = g::apply_thinking(req, body, from, to, PROVIDER, resolved.as_ref())?;
771        body = payload::fix_image_aspect_ratio(&base_model, body);
772        body = gj::try_set_str(&body, "model", &base_model).unwrap_or(body);
773        body = strip_tool_call_ids(body, from);
774        for path in ["tools", "generationConfig", "safetySettings"] {
775            body = payload::delete(body, path);
776        }
777        body = cpa_common::signature::sanitize_gemini_request_thought_signatures(&body, "contents");
778        body = payload::ensure_leading_user_content(body, "contents");
779        body = Barrier {
780            executor: PROVIDER,
781            model: &base_model,
782            protocol: to.as_str(),
783            root: "",
784            original: &original,
785        }
786        .apply(cfg, req, body);
787        let url = endpoint(&auth, &base_model, "countTokens");
788        check_url(&url)?;
789        let headers = self.headers(credential, req, cfg, &auth).await?;
790        let capture = req.capture();
791        let client = self.client(credential, cfg);
792        let body = Bytes::from(body);
793        let upstream = g::send_captured(
794            capture,
795            credential,
796            PROVIDER,
797            &client,
798            &url,
799            headers,
800            body.clone(),
801            false,
802        )
803        .await?;
804        if !(200..300).contains(&upstream.status) {
805            return Err(g::error_from(capture, upstream).await);
806        }
807        let response_headers = upstream.headers.clone();
808        let data = g::read_captured(capture, upstream, usize::MAX).await?;
809        let count = gj::get(&data, "totalTokens").int();
810        let out = cpa_translate::translate_token_count(req.response_format, to, count, &data);
811        Ok(ExecResponse {
812            status: 200,
813            headers: response_headers,
814            body: ResponseBody::Buffered(Bytes::from(out)),
815        })
816    }
817}
818
819/// `type` values the import flag writes and the executor serves.
820pub fn handles(provider: &str) -> bool {
821    provider == PROVIDER
822}
823
824#[cfg(test)]
825#[path = "vertex_tests.rs"]
826mod tests;