1use 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
34pub 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";
43pub(crate) const MAX_LINE: usize = 52_428_800;
45const INTERACTIONS_REVISION: &str = "2026-05-20";
47pub(crate) const COMPACT_ALT: &str = "responses/compact";
48const 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
63fn scope_for(status: u16) -> FailureScope {
65 match status {
66 401 | 402 | 403 | 408 | 429 | 500.. => FailureScope::Credential,
67 _ => FailureScope::Request,
68 }
69}
70
71pub(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
92fn api_key(credential: &Credential) -> &str {
94 credential
95 .attributes
96 .get("api_key")
97 .map(String::as_str)
98 .unwrap_or_default()
99}
100
101fn 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
116fn 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
125pub(crate) fn alt(req: &ExecRequest) -> Option<&str> {
127 req.alt.as_deref().filter(|a| !a.is_empty())
128}
129
130pub(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
139pub(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
163pub(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
205fn 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 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 let working = translate(req, cfg, target, model, &req.body, req.stream, compat)?;
224 Ok((crate::translate::baseline_or_working(original, &working), working))
225}
226
227pub(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
251pub(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
268fn 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
280pub(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
292pub(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
301pub(crate) struct Resolved {
303 pub caps: cpa_common::thinking::ModelCaps,
304 pub is_compat: bool,
305}
306
307pub(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
338pub(crate) use crate::kimi_http::capture_metadata;
341
342#[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
380pub(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
395pub(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
409pub(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
420async 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
434fn identifier(credential: &Credential) -> &'static str {
436 if credential.provider == "gemini-interactions" {
437 "gemini-interactions"
438 } else {
439 "gemini"
440 }
441}
442
443pub(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 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
474pub(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 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 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 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 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 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 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 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#[derive(Default)]
674pub(crate) struct Emit {
675 pub(crate) out: Vec<Bytes>,
676 pub(crate) stop: Option<ExecError>,
677}
678
679impl Emit {
680 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
688struct 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
720pub(crate) struct Output {
722 translator: Box<dyn StreamTranslator>,
723 client: Format,
724 raw: bool,
726 executor_chunks: bool,
727 claude: ClaudeInputTokens,
728 pub(crate) usage: UsageSink,
730 capture: CaptureSink,
732 claude_errors: bool,
735 retained: Option<ExecError>,
737 drop_rule: DropRule,
739 finish_signal: Option<bool>,
741 replay: Option<crate::antigravity_replay::StreamCommit>,
743}
744
745#[derive(Clone, Copy, Default, PartialEq, Eq)]
747pub(crate) enum DropRule {
748 #[default]
750 Off,
751 Always,
754 BeforeTerminal { delivered: bool },
756 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 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 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 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 pub(crate) fn with_replay(mut self, commit: crate::antigravity_replay::StreamCommit) -> Self {
848 self.replay = Some(commit);
849 self
850 }
851
852 pub(crate) fn fail_on_client_drop(mut self, rule: DropRule) -> Self {
860 self.drop_rule = rule;
861 self
862 }
863
864 pub(crate) fn with_claude_stream_errors(mut self) -> Self {
867 self.claude_errors = self.client == Format::Claude;
868 self
869 }
870
871 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 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 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 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 pub(crate) fn with_claude_original(mut self, original: Bytes) -> Self {
924 self.claude = self.claude.with_original(original);
925 self
926 }
927
928 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 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 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 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 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 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 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 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 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 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 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
1093pub(crate) trait LineState: Send + 'static {
1096 fn line(&mut self, line: &[u8]) -> Emit;
1097 fn end(&mut self) -> Emit;
1098 fn end_after_error(&mut self) -> Emit {
1101 self.end()
1102 }
1103 fn flush(&mut self) -> Vec<Bytes>;
1105 fn output(&self) -> Option<&Output> {
1107 None
1108 }
1109}
1110
1111pub(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
1160fn 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
1192struct 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
1209struct 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 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 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 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
1281pub(crate) fn gemini_lines(lines: ExecStream, output: Output, upstream: Format) -> ExecStream {
1283 drive(lines, GeminiLines { output, upstream })
1284}
1285
1286pub(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 (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
1327pub(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
1337pub(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 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 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
1371pub(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
1387pub(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
1435struct InteractionsLines {
1441 output: Output,
1442 frame: Option<Vec<u8>>,
1443}
1444
1445impl InteractionsLines {
1446 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;