Skip to main content

cpa_exec/
gemini.rs

1//! Gemini API keys and native Interactions keys (internal/runtime/executor/gemini_executor.go).
2//!
3//! `gemini` credentials call `generateContent`, `streamGenerateContent` and
4//! `countTokens` under `{base}/v1beta/models/{model}`. `gemini-interactions` credentials
5//! send Interactions, OpenAI, Responses, Claude and Gemini clients to
6//! `{base}/v1beta/interactions`; any other client format takes the generateContent path.
7//! Both authenticate with `x-goog-api-key` and add the credential's custom headers
8//! (`cpa_common::headers`); config payload rules apply through `cpa_common::payload`.
9//!
10//! Stream items are the bytes the client's Go route handler writes: Gemini clients get
11//! `data: <chunk>\n\n`, or the bare chunk when the request named an `alt`; Interactions
12//! clients of the native API get the upstream SSE frame itself.
13
14use std::collections::VecDeque;
15
16use bytes::Bytes;
17use cpa_common::json as gj;
18use cpa_common::thinking::{self, RequestThinking, parse_suffix};
19use cpa_core::config::Config;
20use cpa_core::credential::Credential;
21use cpa_core::exec::{
22    CaptureEvent, CaptureSink, ExecError, ExecRequest, ExecResponse, ExecStream, FailureScope, Operation, ResponseBody,
23    UpstreamRequest, UsageSink,
24};
25use cpa_core::format::Format;
26use cpa_translate::{RequestCtx, ResponseCtx, StreamTranslator};
27use futures_util::StreamExt;
28
29use crate::gemini_payload as payload;
30use crate::gemini_stream::{self as sse, ClaudeInputTokens};
31use crate::payload_barrier::Barrier;
32use crate::proxy::{self, GoClients, GoHeaders, Hooks, Proxy, Upstream};
33
34/// Providers this executor serves (`NewGeminiExecutor`, `NewGeminiInteractionsExecutor`).
35pub const PROVIDERS: [&str; 2] = ["gemini", "gemini-interactions"];
36
37pub fn handles(provider: &str) -> bool {
38    PROVIDERS.contains(&provider)
39}
40
41pub(crate) const DEFAULT_BASE_URL: &str = "https://generativelanguage.googleapis.com";
42pub(crate) const API_VERSION: &str = "v1beta";
43/// `streamScannerBuffer`.
44pub(crate) const MAX_LINE: usize = 52_428_800;
45/// `geminiInteractionsAPIRevision`.
46const INTERACTIONS_REVISION: &str = "2026-05-20";
47pub(crate) const COMPACT_ALT: &str = "responses/compact";
48/// helps.ApplyPatchUpstreamErrorMessage: Go's answer to an empty or failed translation.
49const EMPTY_TRANSLATION: &str = "Invalid apply_patch tool arguments received from upstream.";
50
51pub struct GeminiExecutor {
52    clients: GoClients,
53}
54
55impl Default for GeminiExecutor {
56    fn default() -> Self {
57        Self {
58            clients: GoClients::new(Hooks::default()),
59        }
60    }
61}
62
63/// Same status-only classification as the other API-key executors.
64fn scope_for(status: u16) -> FailureScope {
65    match status {
66        401 | 402 | 403 | 408 | 429 | 500.. => FailureScope::Credential,
67        _ => FailureScope::Request,
68    }
69}
70
71/// `statusErr{code, msg}`: no retry hint, no headers; an empty message reads `status N`.
72pub(crate) fn status_err(status: u16, message: impl Into<Bytes>) -> ExecError {
73    let mut body: Bytes = message.into();
74    if body.is_empty() {
75        body = Bytes::from(format!("status {status}"));
76    }
77    ExecError {
78        status,
79        scope: scope_for(status),
80        body,
81        headers: Box::default(),
82        retry_after: None,
83        direct: false,
84        addon: None,
85    }
86}
87
88pub(crate) fn bad_gateway() -> ExecError {
89    status_err(502, EMPTY_TRANSLATION)
90}
91
92/// `geminiAPIKey`: the attribute as stored.
93fn api_key(credential: &Credential) -> &str {
94    credential
95        .attributes
96        .get("api_key")
97        .map(String::as_str)
98        .unwrap_or_default()
99}
100
101/// `resolveGeminiBaseURL`.
102fn base_url(credential: &Credential) -> String {
103    let custom = credential
104        .attributes
105        .get("base_url")
106        .map(|v| v.trim())
107        .unwrap_or_default();
108    let base = custom.trim_end_matches('/');
109    if base.is_empty() {
110        DEFAULT_BASE_URL.to_owned()
111    } else {
112        base.to_owned()
113    }
114}
115
116/// `shouldExecuteNativeInteractions`.
117fn native_interactions(credential: &Credential, source: Format) -> bool {
118    credential.provider.trim().eq_ignore_ascii_case("gemini-interactions")
119        && matches!(
120            source,
121            Format::Interactions | Format::OpenAI | Format::OpenAIResponse | Format::Claude | Format::Gemini
122        )
123}
124
125/// Go `opts.Alt`: absent and empty are the same.
126pub(crate) fn alt(req: &ExecRequest) -> Option<&str> {
127    req.alt.as_deref().filter(|a| !a.is_empty())
128}
129
130/// `helps.ApplyPatchOriginalRequest`.
131pub(crate) fn original_request(req: &ExecRequest) -> &Bytes {
132    if req.original_body.is_empty() {
133        &req.body
134    } else {
135        &req.original_body
136    }
137}
138
139/// `TranslateRequestWithAPIKeyModelCompatibility` (`TranslateRequestWithCodexMultiAgentV2`
140/// when `compat` is false) for one payload: the shared Codex-client rewrites, then the
141/// registered pair or, for an is-compat model, its `...WithCompat` translator.
142pub(crate) fn translate(
143    req: &ExecRequest,
144    cfg: &Config,
145    target: Format,
146    model: &str,
147    body: &[u8],
148    stream: bool,
149    compat: bool,
150) -> Result<Vec<u8>, ExecError> {
151    let ctx = RequestCtx {
152        model,
153        stream,
154        tool_search: false,
155    };
156    let client = crate::codex_client::Client::new(&req.headers, cfg, "", compat);
157    deep_stack(body, || {
158        crate::codex_client::translate_request(req.source_format, target, &ctx, body, &client).map(|t| t.body)
159    })
160    .map_err(|e| ExecError::local(400, FailureScope::Request, e.to_string()))
161}
162
163/// Runs a translator on a thread with Go's maximum goroutine stack when the body nests
164/// deeply, as `cpa_translate::translate_request` does for registered pairs: the ported
165/// schema walkers recurse per nesting level like Go's, whose stacks grow.
166// ponytail: duplicate of cpa_translate's private guard until its exported `...WithCompat`
167// entry points guard themselves (owner: translators).
168pub(crate) fn deep_stack<T: Send>(body: &[u8], f: impl FnOnce() -> T + Send) -> T {
169    const DEEP: usize = 256;
170    let (mut depth, mut max, mut in_string, mut escaped) = (0usize, 0usize, false, false);
171    for &b in body {
172        if in_string {
173            match b {
174                _ if escaped => escaped = false,
175                b'\\' => escaped = true,
176                b'"' => in_string = false,
177                _ => {}
178            }
179            continue;
180        }
181        match b {
182            b'"' => in_string = true,
183            b'{' | b'[' => {
184                depth += 1;
185                max = max.max(depth);
186            }
187            b'}' | b']' => depth = depth.saturating_sub(1),
188            _ => {}
189        }
190    }
191    if max <= DEEP {
192        return f();
193    }
194    std::thread::scope(|scope| {
195        std::thread::Builder::new()
196            .stack_size(1 << 30)
197            .spawn_scoped(scope, f)
198            .map(|handle| handle.join())
199    })
200    .ok()
201    .and_then(Result::ok)
202    .expect("deep JSON translation thread")
203}
204
205/// `TranslateRequestPairWithAPIKeyModelCompatibility`: the payload-config baseline from
206/// the original request, and the working body from the current one (translated once
207/// when both are the same bytes).
208fn translate_pair(
209    req: &ExecRequest,
210    cfg: &Config,
211    target: Format,
212    model: &str,
213    compat: bool,
214) -> Result<(Vec<u8>, Vec<u8>), ExecError> {
215    // Go translates the original first; equal payloads once.
216    let source = original_request(req);
217    let original = translate(req, cfg, target, model, source, req.stream, compat);
218    if *source == req.body {
219        let original = original?;
220        return Ok((original.clone(), original));
221    }
222    // Only the working payload is sent upstream, so only its refusal counts.
223    let working = translate(req, cfg, target, model, &req.body, req.stream, compat)?;
224    Ok((crate::translate::baseline_or_working(original, &working), working))
225}
226
227/// `helps.ApplyRequestThinking`.
228pub(crate) fn apply_thinking(
229    req: &ExecRequest,
230    body: Vec<u8>,
231    from: Format,
232    to: Format,
233    provider: &str,
234    resolved: Option<&Resolved>,
235) -> Result<Vec<u8>, ExecError> {
236    thinking::apply_request_thinking(&RequestThinking {
237        body: &body,
238        payload: &req.body,
239        original: &req.original_body,
240        model: &req.model,
241        from: from.as_str(),
242        to: to.as_str(),
243        provider,
244        resolved: resolved.map(|r| Some(&r.caps)),
245        has_request_transformer: cpa_translate::pair(from, to).is_some(),
246        updates_changed: false,
247    })
248    .map_err(|e| ExecError::local(e.status(), FailureScope::Request, e.message))
249}
250
251/// The token-count baseline for `default` rules: the client's original request
252/// translated without stream, else the translated body before thinking. Go counts with
253/// a failed translation's partial body; this refuses with its error instead.
254pub(crate) fn count_original(
255    req: &ExecRequest,
256    cfg: &Config,
257    target: Format,
258    model: &str,
259    compat: bool,
260    translated: &[u8],
261) -> Result<Vec<u8>, ExecError> {
262    if req.original_body.is_empty() {
263        return Ok(translated.to_vec());
264    }
265    translate(req, cfg, target, model, &req.original_body, false, compat)
266}
267
268/// Content-Type, the API key and the credential's custom headers (`applyGeminiHeaders`).
269fn request_headers(credential: &Credential, req: &ExecRequest) -> GoHeaders {
270    let mut headers = GoHeaders::new();
271    headers.set("Content-Type", "application/json");
272    let key = api_key(credential);
273    if !key.is_empty() {
274        headers.set("x-goog-api-key", key);
275    }
276    set_custom_headers(&mut headers, credential, req);
277    headers
278}
279
280/// `applyGeminiHeaders` (util.ApplyCustomHeadersFromAttrs). `$CPA-SESSION-ID` is the
281/// attempt's canonical session: Go's conductor binds it to the context
282/// (ensureCanonicalSessionMetadata + syncMetadataSessionToContext) before the executor
283/// runs, and EnsureSessionContext keeps it. Dispatch puts the same bound identity,
284/// derived and message-hash fallbacks included, in `req.session`.
285pub(crate) fn set_custom_headers(headers: &mut GoHeaders, credential: &Credential, req: &ExecRequest) {
286    let session = cpa_common::session::cpa_session_id(req.session.as_deref());
287    for (name, value) in cpa_common::headers::custom_headers(&credential.attributes, &req.headers, session.as_deref()) {
288        headers.set(&name, value);
289    }
290}
291
292/// Go `cliproxyauth.ResolvedModelInfo(req)`: the capabilities dispatch bound to this
293/// attempt, and `helps.APIKeyModelIsCompat`.
294pub(crate) fn resolved(req: &ExecRequest) -> Option<Resolved> {
295    req.resolved_model.as_ref().map(|r| Resolved {
296        caps: cpa_common::thinking::ModelCaps::from(&r.info),
297        is_compat: r.is_compat(),
298    })
299}
300
301/// Capabilities bound to an attempt, as thinking and translation read them.
302pub(crate) struct Resolved {
303    pub caps: cpa_common::thinking::ModelCaps,
304    pub is_compat: bool,
305}
306
307/// Go `helps.RecordAPIRequest` for one physical attempt: `headers` are the request's
308/// header map in Go spelling (the observer sorts and masks them). The log may keep the
309/// body for the whole exchange, so it gets a handle to the body being sent.
310pub(crate) fn capture_request(
311    capture: &CaptureSink,
312    credential: &Credential,
313    provider: &str,
314    url: &str,
315    headers: &[(String, String)],
316    body: &Bytes,
317) {
318    if !capture.enabled() {
319        return;
320    }
321    let (auth_type, auth_value) = cpa_core::registry::dynamic::account_info(credential);
322    capture.record_request(
323        UpstreamRequest {
324            url,
325            method: "POST",
326            headers,
327            body,
328            provider,
329            auth_id: &credential.id,
330            auth_label: &credential.label,
331            auth_type,
332            auth_value: &auth_value,
333        },
334        body,
335    );
336}
337
338/// Go `helps.RecordAPIResponseMetadata` for an upstream answer, with the headers as
339/// Go's `http.Response` holds them.
340pub(crate) use crate::kimi_http::capture_metadata;
341
342/// `http.NewRequest`, `RecordAPIRequest`, `Client.Do`, then `RecordAPIResponseMetadata`,
343/// or `RecordAPIResponseError` when the request fails. A URL Go cannot parse fails
344/// before it is logged; one its transport refuses (an unsupported scheme or no host,
345/// [`crate::xai_url::transport_refusal`]) is logged and then fails, and nothing is sent
346/// either way. `lines`: the caller scans the body line by line, so a body cut short
347/// fails with Go's `unexpected EOF` ([`proxy::send_scanned`]).
348#[allow(clippy::too_many_arguments)]
349pub(crate) async fn send_captured(
350    capture: &CaptureSink,
351    credential: &Credential,
352    provider: &str,
353    client: &wreq::Client,
354    url: &str,
355    headers: GoHeaders,
356    body: Bytes,
357    lines: bool,
358) -> Result<Upstream, ExecError> {
359    crate::xai_url::new_request(url)?;
360    capture_request(capture, credential, provider, url, headers.pairs(), &body);
361    let sent = if let Some(error) = crate::xai_url::transport_refusal(url) {
362        Err(error)
363    } else if lines {
364        proxy::send_scanned(client, url, headers, body, None).await
365    } else {
366        proxy::send(client, url, headers, body, None).await
367    };
368    match sent {
369        Ok(upstream) => {
370            capture_metadata(capture, upstream.status, &upstream.headers);
371            Ok(upstream)
372        }
373        Err(error) => {
374            capture.record(CaptureEvent::ResponseError(&String::from_utf8_lossy(&error.body)));
375            Err(error)
376        }
377    }
378}
379
380/// `io.ReadAll` of a response body, then `AppendAPIResponseChunk` of it, or
381/// `RecordAPIResponseError` when the read fails.
382pub(crate) async fn read_captured(capture: &CaptureSink, upstream: Upstream, limit: usize) -> Result<Bytes, ExecError> {
383    match proxy::read_all(upstream.body, limit, false).await {
384        Ok(data) => {
385            capture.record(CaptureEvent::ResponseChunk(&data));
386            Ok(data)
387        }
388        Err(error) => {
389            capture.record(CaptureEvent::ResponseError(&String::from_utf8_lossy(&error.body)));
390            Err(error)
391        }
392    }
393}
394
395/// Go's scanner loops: `AppendAPIResponseChunk` of every line read and
396/// `RecordAPIResponseError` of the scanner's error.
397pub(crate) fn captured_lines(lines: ExecStream, capture: CaptureSink) -> ExecStream {
398    if !capture.enabled() {
399        return lines;
400    }
401    lines
402        .inspect(move |item| match item {
403            Ok(line) => capture.record(CaptureEvent::ResponseChunk(line)),
404            Err(error) => capture.record(CaptureEvent::ResponseError(&String::from_utf8_lossy(&error.body))),
405        })
406        .boxed()
407}
408
409/// A non-2xx answer: its body (`AppendAPIResponseChunk`, read errors ignored as Go's
410/// `b, _ := io.ReadAll` does) becomes the status error.
411pub(crate) async fn error_from(capture: &CaptureSink, upstream: Upstream) -> ExecError {
412    let status = upstream.status;
413    let body = proxy::read_all(upstream.body, proxy::MAX_ERROR_BODY, true)
414        .await
415        .unwrap_or_default();
416    capture.record(CaptureEvent::ResponseChunk(&body));
417    status_err(status, body)
418}
419
420/// `io.ReadAll` of the whole response, then Go's status check (countTokens and
421/// Interactions read before checking).
422async fn read_then_check(capture: &CaptureSink, upstream: Upstream) -> Result<(http::HeaderMap, Bytes), ExecError> {
423    let status = upstream.status;
424    let headers = upstream.headers.clone();
425    let ok = (200..300).contains(&status);
426    let limit = if ok { usize::MAX } else { proxy::MAX_ERROR_BODY };
427    let data = read_captured(capture, upstream, limit).await?;
428    if !ok {
429        return Err(status_err(status, data));
430    }
431    Ok((headers, data))
432}
433
434/// The executor identity Go logs for a Gemini-family key (`GeminiExecutor.Identifier`).
435fn identifier(credential: &Credential) -> &'static str {
436    if credential.provider == "gemini-interactions" {
437        "gemini-interactions"
438    } else {
439        "gemini"
440    }
441}
442
443/// `TranslateNonStream`: the registered transform, or the body itself when Go registers
444/// none (a Codex client of a Gemini upstream); then Go's empty-output check and Responses
445/// usage details.
446pub(crate) fn translate_non_stream(
447    req: &ExecRequest,
448    upstream: Format,
449    translated: &[u8],
450    data: &[u8],
451) -> Result<Bytes, ExecError> {
452    let response = req.response_format;
453    let ctx = ResponseCtx {
454        model: &req.model,
455        original_request: original_request(req),
456        translated_request: translated,
457        tool_search: false,
458    };
459    let mut out = match cpa_translate::pair(response, upstream) {
460        Some(pair) => (pair.non_stream)(&ctx, data).map_err(|_| bad_gateway())?,
461        // Go's TranslateNonStream still runs the plugin response hooks.
462        None => cpa_translate::hooks::unregistered_non_stream(upstream.as_str(), response.as_str(), &ctx, data)
463            .unwrap_or_else(|| data.to_vec()),
464    };
465    if out.is_empty() {
466        return Err(bad_gateway());
467    }
468    if response == Format::OpenAIResponse {
469        out = crate::openai_compat_payload::ensure_responses_usage_details(&out);
470    }
471    Ok(Bytes::from(out))
472}
473
474/// helps.TranslatedClaudeErrorPayload (Go 7f2f17d7): a Claude answer translated into an
475/// `error` object fails the attempt with 502 and that object, keeping the body's usage.
476pub(crate) fn claude_error(req: &ExecRequest, out: &[u8]) -> Result<(), ExecError> {
477    if req.response_format != Format::Claude || *gj::get(out, "type").bytes() != *b"error" {
478        return Ok(());
479    }
480    req.usage.publish_failure_with_usage(502, &String::from_utf8_lossy(out));
481    Err(status_err(502, Bytes::copy_from_slice(out)))
482}
483
484impl GeminiExecutor {
485    /// Uses `client` for credentials without a proxy (tests pass a plain client).
486    pub fn with_client(client: wreq::Client) -> Self {
487        Self {
488            clients: GoClients::with_default(client),
489        }
490    }
491
492    fn client(&self, credential: &Credential, cfg: &Config) -> wreq::Client {
493        self.clients.get(&Proxy::effective(credential, cfg))
494    }
495
496    pub async fn execute(
497        &self,
498        credential: &Credential,
499        req: ExecRequest,
500        cfg: &Config,
501    ) -> Result<ExecResponse, ExecError> {
502        if req.operation == Operation::CountTokens {
503            return self.count_tokens(credential, &req, cfg).await;
504        }
505        if req.alt.as_deref() == Some(COMPACT_ALT) {
506            return Err(status_err(501, "/responses/compact not supported"));
507        }
508        if native_interactions(credential, req.source_format) {
509            return self.interactions(credential, &req, cfg).await;
510        }
511        self.generate(credential, &req, cfg).await
512    }
513
514    /// `Execute` / `ExecuteStream` against generateContent / streamGenerateContent.
515    async fn generate(
516        &self,
517        credential: &Credential,
518        req: &ExecRequest,
519        cfg: &Config,
520    ) -> Result<ExecResponse, ExecError> {
521        let base_model = parse_suffix(&req.model).model_name;
522        let (from, to) = (req.source_format, Format::Gemini);
523        let resolved = resolved(req);
524        let compat = resolved.as_ref().is_some_and(|r| r.is_compat);
525        let (original_translated, body) = translate_pair(req, cfg, to, &base_model, compat)?;
526        let mut body = apply_thinking(req, body, from, to, &credential.provider, resolved.as_ref())?;
527        body = payload::fix_image_aspect_ratio(&base_model, body);
528        body = payload::set_str_if_different(body, "model", &base_model);
529        body = payload::cap_max_output_tokens(body, &base_model);
530        body = cpa_common::signature::sanitize_gemini_request_thought_signatures(&body, "contents");
531        body = payload::ensure_leading_user_content(body, "contents");
532        body = payload::ensure_trailing_user_content(body, "contents");
533        let action = if req.stream {
534            "streamGenerateContent"
535        } else {
536            "generateContent"
537        };
538        let mut url = format!("{}/{API_VERSION}/models/{base_model}:{action}", base_url(credential));
539        match alt(req) {
540            Some(alt) => url.push_str(&format!("?$alt={alt}")),
541            None if req.stream => url.push_str("?alt=sse"),
542            None => {}
543        }
544        body = payload::delete(body, "session_id");
545        req.usage.request(to, &body);
546        // The payload rules are the last change to the body (868e137).
547        body = Barrier {
548            executor: "",
549            model: &base_model,
550            protocol: to.as_str(),
551            root: "",
552            original: &original_translated,
553        }
554        .apply(cfg, req, body);
555        let headers = request_headers(credential, req);
556        let capture = req.capture();
557        let client = self.client(credential, cfg);
558        // One buffer for the send, the request log and the translators below.
559        let body = Bytes::from(body);
560        let upstream = send_captured(
561            capture,
562            credential,
563            identifier(credential),
564            &client,
565            &url,
566            headers,
567            body.clone(),
568            req.stream,
569        )
570        .await?;
571        if !(200..300).contains(&upstream.status) {
572            return Err(error_from(capture, upstream).await);
573        }
574        let response_headers = upstream.headers.clone();
575        if !req.stream {
576            let data = read_captured(capture, upstream, usize::MAX).await?;
577            req.usage.response_body(to, &data);
578            let out = translate_non_stream(req, to, &body, &data)?;
579            claude_error(req, &out)?;
580            return Ok(ExecResponse {
581                status: 200,
582                headers: response_headers,
583                body: ResponseBody::Buffered(out),
584            });
585        }
586        let output = Output::new(req, to, &body)
587            .with_claude_stream_errors()
588            .require_finish_signal()
589            .fail_on_client_drop(DropRule::Always);
590        let lines = captured_lines(proxy::lines(upstream.body, MAX_LINE), capture.clone());
591        let stream = gemini_lines(lines, output, Format::Gemini);
592        Ok(ExecResponse {
593            status: 200,
594            headers: response_headers,
595            body: ResponseBody::Stream(stream),
596        })
597    }
598
599    /// `executeInteractions` / `executeInteractionsStream`.
600    async fn interactions(
601        &self,
602        credential: &Credential,
603        req: &ExecRequest,
604        cfg: &Config,
605    ) -> Result<ExecResponse, ExecError> {
606        let body = interactions_body(req, cfg)?;
607        let url = format!("{}/{API_VERSION}/interactions", base_url(credential));
608        let mut headers = request_headers(credential, req);
609        interactions_revision(&mut headers, req);
610        let client = self.client(credential, cfg);
611        interactions_exchange(req, credential, identifier(credential), &client, &url, headers, body).await
612    }
613
614    /// `CountTokens`: the translated request without tools, generation and safety
615    /// settings, a leading user turn only, and Go's token-count shape for the client.
616    async fn count_tokens(
617        &self,
618        credential: &Credential,
619        req: &ExecRequest,
620        cfg: &Config,
621    ) -> Result<ExecResponse, ExecError> {
622        let base_model = parse_suffix(&req.model).model_name;
623        let (from, to) = (req.source_format, Format::Gemini);
624        let resolved = resolved(req);
625        let compat = resolved.as_ref().is_some_and(|r| r.is_compat);
626        let body = translate(req, cfg, to, &base_model, &req.body, false, compat)?;
627        let original = count_original(req, cfg, to, &base_model, compat, &body)?;
628        let mut body = apply_thinking(req, body, from, to, &credential.provider, resolved.as_ref())?;
629        body = payload::fix_image_aspect_ratio(&base_model, body);
630        for path in ["tools", "generationConfig", "safetySettings"] {
631            body = payload::delete(body, path);
632        }
633        body = payload::set_str_if_different(body, "model", &base_model);
634        body = cpa_common::signature::sanitize_gemini_request_thought_signatures(&body, "contents");
635        body = payload::ensure_leading_user_content(body, "contents");
636        body = Barrier {
637            executor: "gemini",
638            model: &base_model,
639            protocol: to.as_str(),
640            root: "",
641            original: &original,
642        }
643        .apply(cfg, req, body);
644        let url = format!("{}/{API_VERSION}/models/{base_model}:countTokens", base_url(credential));
645        let headers = request_headers(credential, req);
646        let capture = req.capture();
647        let client = self.client(credential, cfg);
648        // One buffer for the send, the request log and the translators below.
649        let body = Bytes::from(body);
650        let upstream = send_captured(
651            capture,
652            credential,
653            identifier(credential),
654            &client,
655            &url,
656            headers,
657            body.clone(),
658            false,
659        )
660        .await?;
661        let (response_headers, data) = read_then_check(capture, upstream).await?;
662        let count = gj::get(&data, "totalTokens").int();
663        let out = cpa_translate::translate_token_count(req.response_format, to, count, &data);
664        Ok(ExecResponse {
665            status: 200,
666            headers: response_headers,
667            body: ResponseBody::Buffered(Bytes::from(out)),
668        })
669    }
670}
671
672/// Client items, and the terminal error that ends the stream after them.
673#[derive(Default)]
674pub(crate) struct Emit {
675    pub(crate) out: Vec<Bytes>,
676    pub(crate) stop: Option<ExecError>,
677}
678
679impl Emit {
680    /// Appends `next`; returns whether the stream stopped.
681    pub(crate) fn then(&mut self, next: Emit) -> bool {
682        self.out.extend(next.out);
683        self.stop = next.stop;
684        self.stop.is_some()
685    }
686}
687
688/// Go's `TranslateStream` without a registered transform: the payload as one chunk.
689/// Only Gemini upstreams reach it (a Codex client); every client format that takes the
690/// native Interactions path has a registered Interactions pair. Executors that pass
691/// scanned lines as [`line_event`]s get the line back without the terminator.
692/// With plugin response hooks active, each payload goes through them (`hooks`).
693struct Unregistered {
694    options: cpa_translate::stream::StreamOptions,
695    hooks: Option<Box<dyn cpa_translate::stream::GoStream>>,
696}
697
698impl StreamTranslator for Unregistered {
699    fn event(&mut self, event: &[u8]) -> Result<Vec<Bytes>, cpa_translate::Error> {
700        let line = if self.options.whole_events {
701            event
702        } else {
703            event.strip_suffix(LINE_END).unwrap_or(event)
704        };
705        let emit = |chunk: &[u8]| match self.options.chunk {
706            Some(hook) => Bytes::from(hook(chunk)),
707            None => Bytes::copy_from_slice(chunk),
708        };
709        match &mut self.hooks {
710            Some(hooks) => Ok(hooks.line(line)?.iter().map(|c| emit(c)).collect()),
711            None => Ok(vec![emit(line)]),
712        }
713    }
714
715    fn finish(&mut self) -> Result<Vec<Bytes>, cpa_translate::Error> {
716        Ok(Vec::new())
717    }
718}
719
720/// Turns Go stream chunks into the bytes the client's route handler writes.
721pub(crate) struct Output {
722    translator: Box<dyn StreamTranslator>,
723    client: Format,
724    /// A Gemini client asked for `alt`: chunks are written bare.
725    raw: bool,
726    executor_chunks: bool,
727    claude: ClaudeInputTokens,
728    /// The attempt's usage reporter; stream loops feed it what Go's reporter sees.
729    pub(crate) usage: UsageSink,
730    /// The attempt's request capture, for errors a stream loop finds in a payload.
731    capture: CaptureSink,
732    /// helps.CaptureTranslatedClaudeStreamError: watch translated Claude chunks for an
733    /// `error` event (Gemini, Vertex and AI Studio streams).
734    claude_errors: bool,
735    /// The first such event as a 502, ending the stream once the upstream is drained.
736    retained: Option<ExecError>,
737    /// See [`Output::fail_on_client_drop`].
738    drop_rule: DropRule,
739    /// See [`Output::require_finish_signal`]: `Some(seen)` when the rule applies.
740    finish_signal: Option<bool>,
741    /// Antigravity's reasoning replay commit (see [`Output::with_replay`]).
742    replay: Option<crate::antigravity_replay::StreamCommit>,
743}
744
745/// What a stream records when the client goes away before it ended.
746#[derive(Clone, Copy, Default, PartialEq, Eq)]
747pub(crate) enum DropRule {
748    /// Nothing here; the server's end-of-attempt record (a success) stands.
749    #[default]
750    Off,
751    /// Go's Gemini and Interactions scanners: the canceled request fails the pending
752    /// upstream read with `context canceled`, published as a 499 failure without usage.
753    Always,
754    /// Vertex (Go dbc4aad9): the same, unless a terminal chunk was already produced.
755    BeforeTerminal { delivered: bool },
756    /// Antigravity: as Vertex, but an upstream payload with a finish reason is terminal
757    /// too, and the failure keeps the usage seen so far (Go's deferred
758    /// `streamUsage.PublishFailure`).
759    Antigravity { delivered: bool },
760}
761
762impl Output {
763    pub(crate) fn new(req: &ExecRequest, upstream: Format, translated: &[u8]) -> Self {
764        Self::with_options(
765            req,
766            upstream,
767            translated,
768            cpa_translate::stream::StreamOptions::default(),
769        )
770    }
771
772    /// [`Output::new`] for an executor that drives the translator with `options`.
773    pub(crate) fn with_options(
774        req: &ExecRequest,
775        upstream: Format,
776        translated: &[u8],
777        options: cpa_translate::stream::StreamOptions,
778    ) -> Self {
779        let client = req.response_format;
780        let ctx = ResponseCtx {
781            model: &req.model,
782            original_request: original_request(req),
783            translated_request: translated,
784            tool_search: false,
785        };
786        let mut translator = cpa_translate::stream_with(client, upstream, &ctx, options).unwrap_or_else(|| {
787            Box::new(Unregistered {
788                options,
789                hooks: cpa_translate::hooks::wrap_stream(upstream.as_str(), client.as_str(), &ctx, None),
790            })
791        });
792        if req.stream_payloads {
793            translator.executor_chunks();
794        }
795        Self {
796            translator,
797            client,
798            raw: alt(req).is_some(),
799            executor_chunks: req.stream_payloads,
800            claude: ClaudeInputTokens::new(req.source_format, upstream, client, original_request(req).clone()),
801            usage: req.usage.clone(),
802            capture: req.capture().clone(),
803            claude_errors: false,
804            retained: None,
805            drop_rule: DropRule::Off,
806            finish_signal: None,
807            replay: None,
808        }
809    }
810
811    /// Go 4f2e5270 (Gemini and Vertex): a Claude client's stream that ends without a
812    /// finish reason or block reason on any upstream line fails with a 502 instead of
813    /// translating `[DONE]`. Lines are fed to [`Output::observe_finish_signal`].
814    pub(crate) fn require_finish_signal(mut self) -> Self {
815        if self.client == Format::Claude {
816            self.finish_signal = Some(false);
817        }
818        self
819    }
820
821    /// geminiStreamHasFinishSignal on the raw line's JSON payload.
822    pub(crate) fn observe_finish_signal(&mut self, line: &[u8]) {
823        if self.finish_signal != Some(false) {
824            return;
825        }
826        let Some(payload) = sse::json_payload(line) else {
827            return;
828        };
829        let seen = [
830            "candidates.#.finishReason",
831            "response.candidates.#.finishReason",
832            "promptFeedback.blockReason",
833            "response.promptFeedback.blockReason",
834        ]
835        .iter()
836        .any(|path| {
837            gj::get(payload, *path)
838                .array()
839                .iter()
840                .any(|item| !cpa_common::gostr::trim_space(&item.bytes()).is_empty())
841        });
842        self.finish_signal = Some(seen);
843    }
844
845    /// Antigravity: commits the reasoning replay at a clean end, and on any other end
846    /// once the [`DropRule::Antigravity`] terminal chunk went out.
847    pub(crate) fn with_replay(mut self, commit: crate::antigravity_replay::StreamCommit) -> Self {
848        self.replay = Some(commit);
849        self
850    }
851
852    /// Go's scanner error when the client goes away mid-stream: a drop before the
853    /// stream ended publishes `context canceled` (499) and logs it as the response
854    /// error, as `rule` says.
855    // ponytail: Go fails only when the cancellation interrupts the upstream read; one
856    // that finds the executor handing a chunk to a client that stopped reading returns
857    // quietly and publishes a success. That race is not modelled: every drop before
858    // the end is the read case. Upgrade path: track the stream's await point.
859    pub(crate) fn fail_on_client_drop(mut self, rule: DropRule) -> Self {
860        self.drop_rule = rule;
861        self
862    }
863
864    /// Go 7f2f17d7: a translated Claude `error` event is forwarded, then ends the stream
865    /// with a 502 carrying its payload and the usage seen so far.
866    pub(crate) fn with_claude_stream_errors(mut self) -> Self {
867        self.claude_errors = self.client == Format::Claude;
868        self
869    }
870
871    /// The client went away before the stream ended (see [`Output::fail_on_client_drop`]).
872    fn client_dropped(&self) {
873        let (fail, with_usage) = match self.drop_rule {
874            DropRule::Off => (false, false),
875            DropRule::Always => (true, false),
876            DropRule::BeforeTerminal { delivered } => (!delivered, false),
877            DropRule::Antigravity { delivered } => (!delivered, true),
878        };
879        if fail {
880            // Go's RecordAPIResponseError and PublishFailure of the scanner error.
881            self.capture.record(CaptureEvent::ResponseError("context canceled"));
882            if with_usage {
883                self.usage.publish_failure_with_usage(499, "context canceled");
884            } else {
885                self.usage.publish_failure(499, "context canceled");
886            }
887        }
888    }
889
890    /// helps.StopTranslatedClaudeStream: the retained error, published with the usage
891    /// seen so far.
892    pub(crate) fn stop_retained(&mut self, emit: &mut Emit) -> bool {
893        if emit.stop.is_some() {
894            return true;
895        }
896        let Some(error) = self.retained.take() else {
897            return false;
898        };
899        self.usage
900            .publish_failure_with_usage(error.status, &String::from_utf8_lossy(&error.body));
901        emit.stop = Some(error);
902        true
903    }
904
905    /// helps.TranslatedClaudeStreamErrorPayload over translated chunks.
906    fn capture_claude_error(&mut self, out: &[Bytes]) {
907        if !self.claude_errors || self.retained.is_some() {
908            return;
909        }
910        let payload = out
911            .iter()
912            .flat_map(|chunk| chunk.split(|b| *b == b'\n'))
913            .find_map(|line| {
914                crate::gemini_stream::json_payload(line).filter(|p| *gj::get(p, "type").bytes() == *b"error")
915            });
916        if let Some(payload) = payload {
917            self.retained = Some(status_err(502, Bytes::copy_from_slice(payload)));
918        }
919    }
920
921    /// The Claude `message_start` estimate over `original` instead of the client's
922    /// original request (Antigravity estimates its cleaned request).
923    pub(crate) fn with_claude_original(mut self, original: Bytes) -> Self {
924        self.claude = self.claude.with_original(original);
925        self
926    }
927
928    /// helps.EndApplyPatchStream, then the client side's pending frames: the end of a
929    /// stream that feeds no `[DONE]`.
930    pub(crate) fn end(&mut self) -> Emit {
931        let mut emit = self.finalize();
932        if !self.stop_retained(&mut emit) {
933            emit.then(self.finish());
934        }
935        emit
936    }
937
938    /// End of a Gemini-upstream stream: the tool-input finalization, then `[DONE]`
939    /// through the translator, then the client side's pending frames.
940    pub(crate) fn end_with_done(&mut self) -> Emit {
941        let mut emit = self.finalize();
942        if self.stop_retained(&mut emit) {
943            return emit;
944        }
945        if self.finish_signal == Some(false) {
946            let error = status_err(502, "upstream stream closed before finishReason");
947            let message = String::from_utf8_lossy(&error.body).into_owned();
948            self.capture.record(CaptureEvent::ResponseError(&message));
949            self.usage.publish_failure_with_usage(error.status, &message);
950            emit.stop = Some(error);
951            return emit;
952        }
953        if !emit.then(self.translate(b"[DONE]")) && !self.stop_retained(&mut emit) {
954            if let Some(replay) = &mut self.replay {
955                replay.commit();
956            }
957            emit.then(self.finish());
958        }
959        emit
960    }
961
962    /// End of a stream after a read error (Go 5ec31442): the tool-input finalization
963    /// only, so a truncated stream never reads as a completed one.
964    pub(crate) fn end_after_error(&mut self) -> Emit {
965        let mut emit = self.finalize();
966        self.stop_retained(&mut emit);
967        emit
968    }
969
970    /// One Go `TranslateStreamWithClaudeInputTokens` call, framed for the client, then
971    /// helps.StopApplyPatchStream: an apply_patch failure ends the stream after the
972    /// event's frames.
973    pub(crate) fn translate(&mut self, payload: &[u8]) -> Emit {
974        let mut out = match self.translator.event(payload) {
975            Ok(out) => out,
976            Err(_) => return self.failed(),
977        };
978        self.frame(&mut out);
979        self.post_process(&mut out);
980        self.capture_claude_error(&out);
981        match &mut self.drop_rule {
982            DropRule::BeforeTerminal { delivered } if !*delivered => {
983                *delivered = out.iter().any(|chunk| is_terminal_chunk(chunk));
984            }
985            DropRule::Antigravity { delivered } if !*delivered => {
986                let finished = ["candidates.0.finishReason", "response.candidates.0.finishReason"]
987                    .iter()
988                    .any(|path| !cpa_common::gostr::trim_space(&gj::get(payload, *path).bytes()).is_empty());
989                *delivered = finished || out.iter().any(|chunk| is_terminal_chunk(chunk));
990                if let Some(replay) = &mut self.replay {
991                    replay.terminal = *delivered;
992                }
993            }
994            _ => {}
995        }
996        let emit = self.checked(out);
997        // Go commits the replay before the completion goes out (c997cbb5).
998        if self.client == Format::OpenAIResponse
999            && let Some(replay) = &mut self.replay
1000            && emit.out.iter().any(|chunk| {
1001                chunk.split(|b| *b == b'\n').any(|line| {
1002                    sse::json_payload(line).is_some_and(|p| *gj::get(p, "type").bytes() == *b"response.completed")
1003                })
1004            })
1005        {
1006            replay.commit_if_terminal();
1007        }
1008        emit
1009    }
1010
1011    /// helps.EndApplyPatchStream: the translator's tool-input finalization when the
1012    /// upstream transport ends, before any synthetic `[DONE]`. Go writes these chunks
1013    /// without the usage and token post-processing of translated chunks.
1014    fn finalize(&mut self) -> Emit {
1015        let mut out = self.translator.finalize_tool_input();
1016        self.frame(&mut out);
1017        self.checked(out)
1018    }
1019
1020    /// End of stream: frames the client side still holds (the handler's final flush).
1021    fn finish(&mut self) -> Emit {
1022        let mut out = match self.translator.finish() {
1023            Ok(out) => out,
1024            Err(_) => return self.failed(),
1025        };
1026        self.frame(&mut out);
1027        self.post_process(&mut out);
1028        self.checked(out)
1029    }
1030
1031    /// `responsesSSEFramer.Flush` before a terminal error.
1032    pub(crate) fn flush_frames(&mut self) -> Vec<Bytes> {
1033        let mut out = self.translator.flush_frames();
1034        self.frame(&mut out);
1035        self.post_process(&mut out);
1036        out
1037    }
1038
1039    fn checked(&mut self, out: Vec<Bytes>) -> Emit {
1040        if self.translator.tool_input_failed() {
1041            let mut emit = self.failed();
1042            emit.out = out;
1043            return emit;
1044        }
1045        Emit { out, stop: None }
1046    }
1047
1048    /// A translation failure: the stream ends with a 502, and the replay never commits.
1049    fn failed(&mut self) -> Emit {
1050        if let Some(replay) = &mut self.replay {
1051            replay.failed = true;
1052        }
1053        Emit {
1054            out: Vec::new(),
1055            stop: Some(bad_gateway()),
1056        }
1057    }
1058
1059    /// Gemini clients: translators frame chunks as SSE `data:` events; with `alt` the Go
1060    /// handler writes the chunk itself.
1061    fn frame(&self, out: &mut Vec<Bytes>) {
1062        if self.client == Format::Gemini && !self.executor_chunks {
1063            for chunk in out.iter_mut() {
1064                let bare = chunk
1065                    .strip_prefix(b"data: ")
1066                    .and_then(|c| c.strip_suffix(b"\n\n"))
1067                    .map_or_else(|| chunk.clone(), Bytes::copy_from_slice);
1068                *chunk = if self.raw {
1069                    bare
1070                } else {
1071                    let mut framed = Vec::with_capacity(bare.len() + 8);
1072                    framed.extend_from_slice(b"data: ");
1073                    framed.extend_from_slice(&bare);
1074                    framed.extend_from_slice(b"\n\n");
1075                    Bytes::from(framed)
1076                };
1077            }
1078        }
1079        out.retain(|c| !c.is_empty());
1080    }
1081
1082    /// Responses usage details and the Claude input-token estimate on translated chunks.
1083    fn post_process(&mut self, out: &mut [Bytes]) {
1084        if self.client == Format::OpenAIResponse {
1085            for chunk in out.iter_mut() {
1086                *chunk = Bytes::from(crate::openai_compat_payload::ensure_responses_usage_details(chunk));
1087            }
1088        }
1089        self.claude.apply(out);
1090    }
1091}
1092
1093/// One scanned upstream line in, client items out; `end` runs once at EOF or before a
1094/// scanner error is reported.
1095pub(crate) trait LineState: Send + 'static {
1096    fn line(&mut self, line: &[u8]) -> Emit;
1097    fn end(&mut self) -> Emit;
1098    /// `end` before a scanner error is reported; executors that skip the synthetic
1099    /// terminal after a read error override it.
1100    fn end_after_error(&mut self) -> Emit {
1101        self.end()
1102    }
1103    /// Client frames still pending when a terminal error is written.
1104    fn flush(&mut self) -> Vec<Bytes>;
1105    /// The [`Output`] whose [`Output::fail_on_client_drop`] rule applies, if any.
1106    fn output(&self) -> Option<&Output> {
1107        None
1108    }
1109}
1110
1111/// Runs `state` over `lines`. A stop is terminal; a scanner error is reported after the
1112/// end-of-stream items, like Go's `scanner.Err()` check. Pending client frames are
1113/// flushed before any terminal error (Go's Responses handler flushes its framer before
1114/// writing the error).
1115pub(crate) fn drive<S: LineState>(lines: ExecStream, state: S) -> ExecStream {
1116    futures_util::stream::unfold(
1117        (
1118            lines,
1119            Driven { state, settled: false },
1120            VecDeque::<Result<Bytes, ExecError>>::new(),
1121        ),
1122        |(mut lines, mut driven, mut ready)| async move {
1123            loop {
1124                if let Some(item) = ready.pop_front() {
1125                    if item.is_err() {
1126                        ready.clear();
1127                    }
1128                    return Some((item, (lines, driven, ready)));
1129                }
1130                if driven.settled {
1131                    return None;
1132                }
1133                let scanned = lines.next().await;
1134                let state = &mut driven.state;
1135                let emit = match &scanned {
1136                    Some(Ok(line)) => state.line(line),
1137                    Some(Err(_)) => {
1138                        driven.settled = true;
1139                        state.end_after_error()
1140                    }
1141                    None => {
1142                        driven.settled = true;
1143                        state.end()
1144                    }
1145                };
1146                ready.extend(emit.out.into_iter().map(Ok));
1147                let error = match (emit.stop, scanned) {
1148                    (Some(error), _) | (None, Some(Err(error))) => error,
1149                    _ => continue,
1150                };
1151                driven.settled = true;
1152                ready.extend(state.flush().into_iter().map(Ok));
1153                ready.push_back(Err(error));
1154            }
1155        },
1156    )
1157    .boxed()
1158}
1159
1160/// isGeminiVertexTerminalStreamChunk: a client chunk carrying `[DONE]`, a terminal
1161/// Responses or Claude event, or a finish reason.
1162fn is_terminal_chunk(chunk: &[u8]) -> bool {
1163    chunk.split(|b| *b == b'\n').any(|line| {
1164        let trimmed = cpa_common::gostr::trim_space(line);
1165        if trimmed == b"data: [DONE]" || trimmed == b"[DONE]" {
1166            return true;
1167        }
1168        let Some(payload) = sse::json_payload(line) else {
1169            return false;
1170        };
1171        let kind = gj::get(payload, "type").bytes();
1172        if matches!(
1173            kind.as_ref(),
1174            b"response.completed" | b"response.incomplete" | b"response.done" | b"message_stop"
1175        ) {
1176            return true;
1177        }
1178        [
1179            "choices.0.finish_reason",
1180            "response.choices.0.finish_reason",
1181            "candidates.0.finishReason",
1182            "response.candidates.0.finishReason",
1183        ]
1184        .iter()
1185        .any(|path| {
1186            let reason = gj::get(payload, *path);
1187            reason.exists() && !cpa_common::gostr::trim_space(&reason.bytes()).is_empty()
1188        })
1189    })
1190}
1191
1192/// A driven [`LineState`]; dropped before its outcome settled (end of the upstream, an
1193/// error or a stop), it is a client that went away mid-stream.
1194struct Driven<S: LineState> {
1195    state: S,
1196    settled: bool,
1197}
1198
1199impl<S: LineState> Drop for Driven<S> {
1200    fn drop(&mut self) {
1201        if !self.settled
1202            && let Some(output) = self.state.output()
1203        {
1204            output.client_dropped();
1205        }
1206    }
1207}
1208
1209/// The streamGenerateContent loop of Gemini and Antigravity upstreams: usage filtering
1210/// and the JSON payload of each line through the translator; at EOF the tool-input
1211/// finalization, then `[DONE]`. Antigravity lines come through [`antigravity_payloads`],
1212/// already filtered.
1213struct GeminiLines {
1214    output: Output,
1215    upstream: Format,
1216}
1217
1218impl LineState for GeminiLines {
1219    fn line(&mut self, line: &[u8]) -> Emit {
1220        if self.upstream != Format::Antigravity {
1221            // Go observes the response model and (since 7f2f17d7) the usage on the raw
1222            // line, before the filter hides non-terminal usageMetadata.
1223            self.output.usage.response_line(self.upstream, line);
1224            self.output.observe_finish_signal(line);
1225            let filtered = sse::filter_sse_usage_metadata(line);
1226            return match sse::json_payload(&filtered) {
1227                Some(payload) => self.output.translate(payload),
1228                None => Emit::default(),
1229            };
1230        }
1231        // A payload joined from several lines; its usage is not observed (Go reads usage
1232        // from each scanned line, and no part of a split payload parses alone).
1233        let assembled = line.contains(&b'\n');
1234        if !assembled {
1235            self.output.usage.response_line(self.upstream, line);
1236        }
1237        let Some(payload) = sse::json_payload(line) else {
1238            return Emit::default();
1239        };
1240        // Go v8.0.27 (4e4dede4): an `error` object ends the stream with its code (502
1241        // outside 400 to 599) and the payload as the message.
1242        let error = gj::get(payload, "error");
1243        if error.exists() {
1244            let code = error.get("code").int();
1245            let status = u16::try_from(code)
1246                .ok()
1247                .filter(|c| (400..=599).contains(c))
1248                .unwrap_or(502);
1249            let error = crate::antigravity::upstream_error(status, Bytes::copy_from_slice(payload));
1250            self.output
1251                .capture
1252                .record(CaptureEvent::ResponseError(&String::from_utf8_lossy(&error.body)));
1253            return Emit {
1254                out: Vec::new(),
1255                stop: Some(error),
1256            };
1257        }
1258        if assembled {
1259            self.output.usage.response_model_line(payload);
1260        }
1261        self.output.translate(payload)
1262    }
1263
1264    fn end(&mut self) -> Emit {
1265        self.output.end_with_done()
1266    }
1267
1268    fn end_after_error(&mut self) -> Emit {
1269        self.output.end_after_error()
1270    }
1271
1272    fn flush(&mut self) -> Vec<Bytes> {
1273        self.output.flush_frames()
1274    }
1275
1276    fn output(&self) -> Option<&Output> {
1277        Some(&self.output)
1278    }
1279}
1280
1281/// [`GeminiLines`] over `lines` for an `upstream` (Gemini or Antigravity) stream.
1282pub(crate) fn gemini_lines(lines: ExecStream, output: Output, upstream: Format) -> ExecStream {
1283    drive(lines, GeminiLines { output, upstream })
1284}
1285
1286/// The Antigravity stream loop's line handling before a payload is used (Go v8.0.27,
1287/// 4e4dede4): each scanned line with its non-terminal usage filtered, and a JSON payload
1288/// split over several lines joined (each later line trimmed and without `data:`,
1289/// newline-separated) until the joined text parses. A joined payload is one item with
1290/// newlines in it, which a scanned line never has. Lines that end no pending payload
1291/// are not validated again, so joining stays linear in the payload's size.
1292pub(crate) fn antigravity_payloads(lines: ExecStream) -> ExecStream {
1293    lines
1294        .scan(Vec::<u8>::new(), |pending, item| {
1295            let line = match item {
1296                Ok(line) => line,
1297                Err(error) => return std::future::ready(Some(Some(Err(error)))),
1298            };
1299            let filtered = sse::filter_sse_usage_metadata(&line);
1300            let out = if pending.is_empty() {
1301                match sse::json_payload(&filtered) {
1302                    Some(payload) if !gj::valid(payload) => {
1303                        *pending = payload.to_vec();
1304                        None
1305                    }
1306                    _ => Some(Bytes::from(filtered)),
1307                }
1308            } else {
1309                let mut piece = cpa_common::gostr::trim_space(&filtered);
1310                if let Some(rest) = piece.strip_prefix(b"data:") {
1311                    piece = cpa_common::gostr::trim_space(rest);
1312                }
1313                if !piece.is_empty() {
1314                    pending.push(b'\n');
1315                    pending.extend_from_slice(piece);
1316                }
1317                // The pending text starts with `{` and its pieces are trimmed, so it can
1318                // only become valid with a piece that ends in `}`.
1319                (piece.ends_with(b"}") && gj::valid(pending)).then(|| Bytes::from(std::mem::take(pending)))
1320            };
1321            std::future::ready(Some(out.map(Ok)))
1322        })
1323        .filter_map(std::future::ready)
1324        .boxed()
1325}
1326
1327/// The terminator executors append to a scanned line to pass it as one translator
1328/// event. The translators split events like bufio.ScanLines, which drops one `\r`
1329/// before each `\n`, so `\r\n` hands them the line exactly as Go's scanner returned it
1330/// (including a trailing `\r` of its own).
1331pub(crate) const LINE_END: &[u8] = b"\r\n";
1332
1333pub(crate) fn line_event(line: &[u8]) -> Vec<u8> {
1334    [line, LINE_END].concat()
1335}
1336
1337/// The Interactions request body (Go 2d5351b3's shared helpers, as the Gemini and Vertex
1338/// executors build it): Interactions clients as they are, others translated; the
1339/// target model, thinking, input-ID repair, `stream` and the payload rules.
1340pub(crate) fn interactions_body(req: &ExecRequest, cfg: &Config) -> Result<Bytes, ExecError> {
1341    let target = parse_suffix(&req.model).model_name;
1342    let (from, to) = (req.source_format, Format::Interactions);
1343    let resolved = resolved(req);
1344    let compat = resolved.as_ref().is_some_and(|r| r.is_compat);
1345    // Interactions clients are sent as they are, without the registry normalizer.
1346    let (original_translated, mut body) = if from == Format::Interactions {
1347        (original_request(req).to_vec(), req.body.to_vec())
1348    } else {
1349        translate_pair(req, cfg, to, &target, compat)?
1350    };
1351    if gj::get(&body, "model").exists() && !target.is_empty() {
1352        body = payload::set_str_if_different(body, "model", &target);
1353    }
1354    // applyGeminiInteractionsThinking: the Gemini applier family for the target.
1355    body = apply_thinking(req, body, from, to, "gemini", resolved.as_ref())?;
1356    body = payload::sanitize_interactions_input_ids(body);
1357    if req.stream {
1358        body = payload::set_bool_if_different(body, "stream", true);
1359    }
1360    body = Barrier {
1361        executor: "",
1362        model: &target,
1363        protocol: "interactions",
1364        root: "",
1365        original: &original_translated,
1366    }
1367    .apply(cfg, req, body);
1368    Ok(Bytes::from(body))
1369}
1370
1371/// applyGeminiInteractionsRequestHeaders, then applyGeminiInteractionsRevisionHeader.
1372pub(crate) fn interactions_revision(headers: &mut GoHeaders, req: &ExecRequest) {
1373    if headers.get("Api-Revision").is_none_or(str::is_empty)
1374        && let Some(revision) = req
1375            .headers
1376            .get("Api-Revision")
1377            .and_then(|v| v.to_str().ok())
1378            .filter(|v| !v.is_empty())
1379    {
1380        headers.set("Api-Revision", revision);
1381    }
1382    if headers.get("Api-Revision").is_none_or(str::is_empty) {
1383        headers.set("Api-Revision", INTERACTIONS_REVISION);
1384    }
1385}
1386
1387/// Sends an Interactions request and answers it: the translated body, or the
1388/// Interactions stream loop.
1389pub(crate) async fn interactions_exchange(
1390    req: &ExecRequest,
1391    credential: &Credential,
1392    provider: &str,
1393    client: &wreq::Client,
1394    url: &str,
1395    headers: GoHeaders,
1396    body: Bytes,
1397) -> Result<ExecResponse, ExecError> {
1398    let to = Format::Interactions;
1399    let capture = req.capture();
1400    let upstream = send_captured(
1401        capture,
1402        credential,
1403        provider,
1404        client,
1405        url,
1406        headers,
1407        body.clone(),
1408        req.stream,
1409    )
1410    .await?;
1411    if !req.stream {
1412        let (response_headers, data) = read_then_check(capture, upstream).await?;
1413        req.usage.response_body(to, &data);
1414        let out = translate_non_stream(req, to, &body, &data)?;
1415        return Ok(ExecResponse {
1416            status: 200,
1417            headers: response_headers,
1418            body: ResponseBody::Buffered(out),
1419        });
1420    }
1421    if !(200..300).contains(&upstream.status) {
1422        return Err(error_from(capture, upstream).await);
1423    }
1424    let response_headers = upstream.headers.clone();
1425    let output = Output::new(req, to, &body).fail_on_client_drop(DropRule::Always);
1426    let lines = captured_lines(proxy::lines(upstream.body, MAX_LINE), capture.clone());
1427    let stream = interactions_lines(lines, output);
1428    Ok(ExecResponse {
1429        status: 200,
1430        headers: response_headers,
1431        body: ResponseBody::Stream(stream),
1432    })
1433}
1434
1435/// The Interactions stream loop. The translator groups the lines into SSE frames at
1436/// blank lines (`stream::Framed`): Interactions clients get each frame as sent, others
1437/// its translated payload. At EOF the pending frame is emitted (Go's final emitFrame),
1438/// then the tool-input finalization. With usage reporting on, the loop also keeps Go's
1439/// frame so each frame's payload reaches the reporter (Go's emitFrame observation).
1440struct InteractionsLines {
1441    output: Output,
1442    frame: Option<Vec<u8>>,
1443}
1444
1445impl InteractionsLines {
1446    /// emitFrame's `ObserveResponseModel(payload)` and `ParseInteractionsStreamUsage`.
1447    fn report_frame(&mut self) {
1448        if let Some(frame) = &mut self.frame {
1449            let payload = cpa_translate::stream::interactions_frame_payload(frame);
1450            frame.clear();
1451            if !payload.is_empty() {
1452                self.output.usage.response_line(Format::Interactions, &payload);
1453            }
1454        }
1455    }
1456}
1457
1458impl LineState for InteractionsLines {
1459    fn line(&mut self, line: &[u8]) -> Emit {
1460        if self.frame.is_some() {
1461            if line.trim_ascii().is_empty() {
1462                self.report_frame();
1463            } else if let Some(frame) = &mut self.frame {
1464                if !frame.is_empty() {
1465                    frame.push(b'\n');
1466                }
1467                frame.extend_from_slice(line);
1468            }
1469        }
1470        self.output.translate(&line_event(line))
1471    }
1472
1473    fn end(&mut self) -> Emit {
1474        self.report_frame();
1475        let mut emit = self.output.translate(b"\n");
1476        if emit.stop.is_none() && !emit.then(self.output.finalize()) {
1477            emit.then(self.output.finish());
1478        }
1479        emit
1480    }
1481
1482    fn flush(&mut self) -> Vec<Bytes> {
1483        self.output.flush_frames()
1484    }
1485
1486    fn output(&self) -> Option<&Output> {
1487        Some(&self.output)
1488    }
1489}
1490
1491fn interactions_lines(lines: ExecStream, output: Output) -> ExecStream {
1492    let frame = output.usage.enabled().then(Vec::new);
1493    drive(lines, InteractionsLines { output, frame })
1494}
1495
1496#[cfg(test)]
1497#[path = "gemini_tests.rs"]
1498pub(crate) mod tests;