Skip to main content

cpa_exec/
openai_compat.rs

1//! OpenAI-compatible upstreams (internal/runtime/executor/openai_compat_executor.go):
2//! configured providers reached with a bearer API key at `base_url`, speaking Chat
3//! Completions, Responses compact and the Images API.
4
5use std::collections::VecDeque;
6use std::time::SystemTime;
7
8use bytes::Bytes;
9use cpa_core::config::Config;
10use cpa_core::credential::Credential;
11use cpa_core::exec::{ExecError, ExecRequest, ExecResponse, ExecStream, FailureScope, Operation, ResponseBody};
12use cpa_core::format::Format;
13use cpa_translate::{RequestCtx, ResponseCtx, StreamTranslator};
14use futures_util::StreamExt;
15
16use cpa_common::gostr::GoStr;
17use cpa_common::json as gj;
18use cpa_common::thinking::{self, ModelCaps, RequestThinking, parse_suffix};
19
20use crate::gemini_stream::ClaudeInputTokens;
21use crate::openai_compat_go as go;
22use crate::openai_compat_http::{self as wire, Clients, GoHeaders};
23use crate::openai_compat_multipart as multipart;
24use crate::openai_compat_payload as payload;
25
26pub const USER_AGENT: &str = "cli-proxy-openai-compat";
27const COMPACT_ALT: &str = "responses/compact";
28const IMAGES_GENERATIONS: &str = "/images/generations";
29const IMAGES_EDITS: &str = "/images/edits";
30/// `scanner.Buffer(nil, 52_428_800)`.
31const MAX_LINE: usize = 52_428_800;
32/// helps.ApplyPatchUpstreamErrorMessage: Go's error for an empty or rejected translation.
33const EMPTY_TRANSLATION: &str = cpa_translate::APPLY_PATCH_UPSTREAM_ERROR;
34
35/// Providers this executor serves: `openai-compatibility` and `openai-compatible-<name>`
36/// (util.OpenAICompatibleProviderKey).
37pub fn handles(provider: &str) -> bool {
38    provider == "openai-compatibility" || provider.starts_with("openai-compatible-")
39}
40
41#[derive(Default)]
42pub struct OpenAICompatExecutor {
43    clients: Clients,
44}
45
46/// Go's plain `statusErr`: never credential-scoped, so a 429 cools only the model
47/// (`IsCredentialScoped` is false); other failures by status.
48pub(crate) fn scope_for(status: u16) -> FailureScope {
49    match status {
50        429 => FailureScope::Model,
51        401 | 402 | 403 | 408 | 500.. => FailureScope::Credential,
52        _ => FailureScope::Request,
53    }
54}
55
56/// A plain Go error (no status code): no status, scope or retry semantics, which the
57/// server models as a transport fault; Go's handler answers 500 with its text.
58pub(crate) fn plain_err(message: impl Into<String>) -> ExecError {
59    ExecError::local(500, FailureScope::Transport, message)
60}
61
62/// `statusErr{code, msg}`: an empty message reads `status N`.
63pub(crate) fn status_err(status: u16, message: impl Into<String>) -> ExecError {
64    let message = message.into();
65    let message = if message.is_empty() {
66        format!("status {status}")
67    } else {
68        message
69    };
70    ExecError::local(status, scope_for(status), message)
71}
72
73/// A chat non-2xx response: `newOpenAICompatStatusError` over the error body, which is
74/// captured like Go's `AppendAPIResponseChunk` (read errors ignored, `b, _ :=
75/// io.ReadAll`).
76async fn upstream_error(upstream: wire::Upstream, capture: &wire::Capture) -> ExecError {
77    let (status, headers, body) = wire::read_error_body(upstream).await;
78    capture.chunk(&body);
79    status_error(status, headers, &body, true)
80}
81
82/// An images non-2xx response: the whole body is read first and a read error is
83/// captured and returned instead, then `newOpenAICompatStatusError`, or the plain
84/// `statusErr` the image stream path uses.
85async fn images_error(upstream: wire::Upstream, with_retry: bool, capture: &wire::Capture) -> ExecError {
86    let (status, headers) = (upstream.status, upstream.headers.clone());
87    let body = wire::read_all(upstream).await;
88    capture.read(&body);
89    match body {
90        Ok(body) => status_error(status, headers, &body, with_retry),
91        Err(error) => error,
92    }
93}
94
95fn status_error(status: u16, headers: http::HeaderMap, body: &[u8], with_retry: bool) -> ExecError {
96    let mut error = status_err(status, String::from_utf8_lossy(body));
97    if with_retry {
98        error.retry_after = payload::retry_after(status, &headers, body, SystemTime::now());
99    }
100    error.headers = Box::new(headers);
101    error
102}
103
104fn credentials(credential: &Credential) -> (String, String) {
105    let attr = |k: &str| {
106        credential
107            .attributes
108            .get(k)
109            .map(|v| v.trim().to_owned())
110            .unwrap_or_default()
111    };
112    (attr("base_url"), attr("api_key"))
113}
114
115fn missing_base_url() -> ExecError {
116    ExecError::local(401, FailureScope::Credential, "missing provider baseURL")
117}
118
119/// `strings.TrimSuffix(baseURL, "/") + path`: one slash only, like Go.
120fn endpoint(base_url: &str, path: &str) -> String {
121    format!("{}{path}", base_url.strip_suffix('/').unwrap_or(base_url))
122}
123
124fn not_registered(what: &str, from: Format, to: Format) -> ExecError {
125    ExecError::local(
126        501,
127        FailureScope::Request,
128        format!(
129            "{what} translation {} -> {} is not registered",
130            from.as_str(),
131            to.as_str()
132        ),
133    )
134}
135
136/// `helps.TranslateRequestWithAPIKeyModelCompatibilityAndUpdateIntent` for this executor
137/// (no target executor): Codex-client rewrites, then the pair or its compat variant.
138// ponytail: the configuration-update intent crate::codex_client reports is not read
139// here yet, so thinking always sees `updates_changed: false`, and equal original and
140// working payloads are translated twice (Go translates once); the OpenAI-compatible
141// port reads it.
142fn translate_body(
143    req: &ExecRequest,
144    cfg: &Config,
145    target: Format,
146    model: &str,
147    stream: bool,
148    body: &[u8],
149    is_compat: bool,
150) -> Result<Vec<u8>, ExecError> {
151    let client = crate::codex_client::Client::new(&req.headers, cfg, "", is_compat);
152    crate::codex_client::translate_request(
153        req.source_format,
154        target,
155        &RequestCtx {
156            model,
157            stream,
158            tool_search: false,
159        },
160        body,
161        &client,
162    )
163    .map(|t| t.body)
164    .map_err(|e| ExecError::local(400, FailureScope::Request, e.to_string()))
165}
166
167/// `opts.OriginalRequest`, else the payload.
168fn original_payload(req: &ExecRequest) -> &[u8] {
169    if req.original_body.is_empty() {
170        &req.body
171    } else {
172        &req.original_body
173    }
174}
175
176/// `helps.ApplyPayloadConfigWithRequest` (no target executor) on the translated body;
177/// `original` is the translated original request.
178fn apply_payload_rules(
179    cfg: &Config,
180    req: &ExecRequest,
181    model: &str,
182    target: Format,
183    original: &[u8],
184    body: Vec<u8>,
185) -> Vec<u8> {
186    let rules = cpa_common::payload::Rules::from_config(cfg);
187    cpa_common::payload::apply(
188        &rules,
189        &cpa_common::payload::Request {
190            target_executor: "",
191            model,
192            requested_model: route_model(req),
193            protocol: target.as_str(),
194            from_protocol: req.source_format.as_str(),
195            root: "",
196            original,
197            request_path: &req.request_path,
198            headers: Some(&req.headers),
199        },
200        body,
201    )
202}
203
204/// `helps.ApplyRequestThinking` with the capabilities bound to this attempt.
205fn apply_thinking(
206    body: Vec<u8>,
207    req: &ExecRequest,
208    target: Format,
209    provider: &str,
210    resolved: Option<&ModelCaps>,
211) -> Result<Vec<u8>, ExecError> {
212    thinking::apply_request_thinking(&RequestThinking {
213        body: &body,
214        payload: &req.body,
215        original: &req.original_body,
216        model: &req.model,
217        from: req.source_format.as_str(),
218        to: target.as_str(),
219        provider,
220        resolved: resolved.map(Some),
221        has_request_transformer: cpa_translate::pair(req.source_format, target).is_some(),
222        updates_changed: false,
223    })
224    .map_err(|e| ExecError::local(400, FailureScope::Request, e.message))
225    .map(|body| {
226        // Go 0e0cab0d (v8.0.64), a cliproxy-rs addition: `APIKeyModelIsCompat` and
227        // Claude to OpenAI only.
228        let compat = req.resolved_model.as_ref().is_some_and(|r| r.is_compat());
229        if compat && req.source_format == Format::Claude && target == Format::OpenAI && !body.is_empty() {
230            payload::reconcile_tool_call_reasoning(body)
231        } else {
232            body
233        }
234    })
235}
236
237fn base_headers(api_key: &str, content_type: &str) -> GoHeaders {
238    let mut headers = GoHeaders::new();
239    headers.set("Content-Type", content_type);
240    if !api_key.is_empty() {
241        headers.set("Authorization", format!("Bearer {api_key}"));
242    }
243    headers.set("User-Agent", USER_AGENT);
244    headers
245}
246
247/// `util.ApplyCustomHeadersFromAttrs` with the client headers and the canonical session.
248fn apply_custom(headers: &mut GoHeaders, credential: &Credential, req: &ExecRequest) {
249    let session = cpa_common::session::cpa_session_id(req.session.as_deref());
250    for (name, value) in cpa_common::headers::custom_headers(&credential.attributes, &req.headers, session.as_deref()) {
251        headers.set(&name, value);
252    }
253}
254
255impl OpenAICompatExecutor {
256    /// Uses `client` for credentials without a proxy (tests pass a plain client).
257    pub fn with_client(client: wreq::Client) -> Self {
258        Self {
259            clients: Clients::new(client),
260        }
261    }
262
263    pub async fn execute(
264        &self,
265        credential: &Credential,
266        req: ExecRequest,
267        cfg: &Config,
268    ) -> Result<ExecResponse, ExecError> {
269        match req.operation {
270            Operation::CountTokens => count_tokens(credential, &req, cfg),
271            Operation::Generate => self.chat(credential, req, cfg).await,
272        }
273    }
274
275    /// Execute (`stream` false) and ExecuteStream (`stream` true) for chat and compact.
276    async fn chat(&self, credential: &Credential, req: ExecRequest, cfg: &Config) -> Result<ExecResponse, ExecError> {
277        let (base_url, api_key) = credentials(credential);
278        let compat = payload::resolve_compat(credential, cfg);
279        let client = self.clients.for_credential(credential, cfg);
280        chat_with(&client, credential, req, cfg, &base_url, &api_key, compat.as_ref()).await
281    }
282
283    /// `executeImages` / `executeImagesStream`: the body (JSON or multipart) goes to
284    /// `/images/edits` or `/images/generations` with the model replaced. `request_path` is
285    /// the inbound route; `req.stream` selects raw SSE passthrough.
286    pub async fn images(
287        &self,
288        credential: &Credential,
289        req: ExecRequest,
290        request_path: &str,
291        cfg: &Config,
292    ) -> Result<ExecResponse, ExecError> {
293        let base_model = parse_suffix(&req.model).model_name.trim().to_owned();
294        let (base_url, api_key) = credentials(credential);
295        if base_url.is_empty() {
296            return Err(missing_base_url());
297        }
298        let client_content_type = req
299            .headers
300            .get(http::header::CONTENT_TYPE)
301            .and_then(|v| v.to_str().ok())
302            .unwrap_or_default()
303            .trim()
304            .to_owned();
305        let (body, content_type) = prepare_images_payload(&req.body, &base_model, &client_content_type, req.stream)?;
306        let content_type = if content_type.is_empty() {
307            "application/json".to_owned()
308        } else {
309            content_type
310        };
311        let (body, content_type) = media_barrier(
312            credential,
313            &req,
314            cfg,
315            &base_model,
316            &body,
317            &content_type,
318            &client_content_type,
319        )?;
320        let path = if request_path.trim().ends_with(IMAGES_EDITS) {
321            IMAGES_EDITS
322        } else {
323            IMAGES_GENERATIONS
324        };
325        // SetTranslatedReasoningEffort(payload, "openai").
326        if req.usage.enabled() {
327            req.usage.request(Format::OpenAI, &body);
328        }
329        let mut headers = base_headers(&api_key, &content_type);
330        if req.stream {
331            headers.set("Accept", "text/event-stream");
332            headers.set("Cache-Control", "no-cache");
333        }
334        apply_custom(&mut headers, credential, &req);
335        let client = self.clients.for_credential(credential, cfg);
336        let url = endpoint(&base_url, path);
337        let capture = wire::Capture::request(
338            &req,
339            credential,
340            &credential.provider,
341            &url,
342            "POST",
343            headers.pairs(),
344            &body,
345        );
346        let upstream = wire::send(&client, &url, headers, body).await;
347        capture.sent(&upstream);
348        let upstream = upstream?;
349        if !(200..300).contains(&upstream.status) {
350            return Err(images_error(upstream, !req.stream, &capture).await);
351        }
352        let headers = upstream.headers.clone();
353        if req.stream {
354            // Each raw read is logged; a read error is logged and published as a failure
355            // before the client gets it.
356            let usage = req.usage.clone();
357            let stream = capture
358                .stream(upstream.body)
359                .inspect(move |item| {
360                    if let Err(error) = item {
361                        wire::publish_failure(&usage, error);
362                    }
363                })
364                .boxed();
365            return Ok(ExecResponse {
366                status: 200,
367                headers,
368                body: ResponseBody::Stream(stream),
369            });
370        }
371        let data = wire::read_all(upstream).await;
372        capture.read(&data);
373        let data = data?;
374        if req.usage.enabled() {
375            req.usage.response_body(Format::OpenAI, &data);
376        }
377        Ok(ExecResponse {
378            status: 200,
379            headers,
380            body: ResponseBody::Buffered(data),
381        })
382    }
383}
384
385/// `executeWithCredentials` / `executeStreamWithCredentials` (Go d9451cb6): the chat
386/// request at `base_url` with `api_key`, for this executor's credentials and for
387/// Vertex's endpoints/openapi route (which passes no compatibility config).
388pub(crate) async fn chat_with(
389    client: &wreq::Client,
390    credential: &Credential,
391    mut req: ExecRequest,
392    cfg: &Config,
393    base_url: &str,
394    api_key: &str,
395    compat: Option<&payload::Compat>,
396) -> Result<ExecResponse, ExecError> {
397    if base_url.is_empty() {
398        return Err(missing_base_url());
399    }
400    if req.operation == Operation::Generate {
401        // Only CPA capsules: a native compaction item is the upstream's own context and
402        // reaches `/responses/compact` (or the chat translator) as it is.
403        if (crate::antigravity_compaction::has_item(&req.body)
404            || crate::antigravity_compaction::has_item(&req.original_body))
405            && let Err(e) = crate::antigravity_compaction::expand_capsules(&mut req)
406        {
407            req.usage.discard();
408            return Err(status_err(400, e));
409        }
410        if req.alt.as_deref() != Some(COMPACT_ALT)
411            && (crate::antigravity_compaction::has_trigger(&req.body)
412                || crate::antigravity_compaction::has_trigger(&req.original_body))
413        {
414            return compact_summary(client, credential, req, cfg, base_url, api_key, compat).await;
415        }
416    }
417    chat_request(client, credential, req, cfg, base_url, api_key, compat).await
418}
419
420/// The ordinary chat or `/responses/compact` request of [`chat_with`], also used for
421/// the summary turn of [`compact_summary`].
422async fn chat_request(
423    client: &wreq::Client,
424    credential: &Credential,
425    req: ExecRequest,
426    cfg: &Config,
427    base_url: &str,
428    api_key: &str,
429    compat: Option<&payload::Compat>,
430) -> Result<ExecResponse, ExecError> {
431    let base_model = parse_suffix(&req.model).model_name;
432    let compact = req.alt.as_deref() == Some(COMPACT_ALT);
433    // ExecuteStream always targets chat completions, even with the compact alt.
434    let (target, path) = if compact && !req.stream {
435        (Format::OpenAIResponse, "/responses/compact")
436    } else {
437        (Format::OpenAI, "/chat/completions")
438    };
439    let response_pair = cpa_translate::pair(req.response_format, target);
440    if response_pair.is_none() && req.response_format != target {
441        return Err(not_registered("response", req.response_format, target));
442    }
443    let caps = req.resolved_model.as_ref().map(|r| ModelCaps::from(&r.info));
444    let is_compat = req.resolved_model.as_ref().is_some_and(|r| r.is_compat());
445    let original = translate_body(
446        &req,
447        cfg,
448        target,
449        &base_model,
450        req.stream,
451        original_payload(&req),
452        is_compat,
453    );
454    let mut body = translate_body(&req, cfg, target, &base_model, req.stream, &req.body, is_compat)?;
455    let original = crate::translate::baseline_or_working(original, &body);
456    body = apply_thinking(body, &req, target, &credential.provider, caps.as_ref())?;
457    let requested = route_model(&req);
458    if payload::excludes_images(compat, &base_model, requested) {
459        body = payload::normalize_tool_results_text_only(body);
460    }
461    if !compact {
462        let mct = payload::uses_max_completion_tokens(compat, &base_model, requested);
463        body = payload::normalize_max_tokens(body, mct);
464        body = prompt_cache_key(compat, &credential.provider, &req, &base_model, body);
465    }
466    if compact && !req.stream {
467        gj::delete(&mut body, "stream");
468        body = payload::sanitize_reasoning_encrypted_content(body);
469    }
470    if req.stream {
471        payload::set_bool_if_different(&mut body, "stream_options.include_usage", true);
472    }
473    if req.usage.enabled() {
474        req.usage.request(target, &body);
475    }
476    // The payload rules are the last change to the body (868e137).
477    body = apply_payload_rules(cfg, &req, &base_model, target, &original, body);
478    let mut headers = base_headers(api_key, "application/json");
479    apply_custom(&mut headers, credential, &req);
480    if req.stream {
481        headers.set("Accept", "text/event-stream");
482        headers.set("Cache-Control", "no-cache");
483    }
484    let url = endpoint(base_url, path);
485    // Go's `http.NewRequestWithContext` refuses a URL its parser rejects (an escaped
486    // host such as `%61iplatform.googleapis.com`) and its transport one without a host
487    // (`https:relay.example`), before anything is sent; the url crate would read a host
488    // from both and send the key there.
489    crate::xai_url::new_request(&url)?;
490    let capture = wire::Capture::request(
491        &req,
492        credential,
493        &credential.provider,
494        &url,
495        "POST",
496        headers.pairs(),
497        &body,
498    );
499    let upstream = match crate::xai_url::transport_refusal(&url) {
500        Some(error) => Err(error),
501        None => wire::send(client, &url, headers, Bytes::from(body.clone())).await,
502    };
503    capture.sent(&upstream);
504    let upstream = upstream?;
505    if !(200..300).contains(&upstream.status) {
506        return Err(upstream_error(upstream, &capture).await);
507    }
508    let response_headers = upstream.headers.clone();
509    let original = if req.original_body.is_empty() {
510        req.body.clone()
511    } else {
512        req.original_body.clone()
513    };
514    if req.stream {
515        let translator: Box<dyn StreamTranslator> = match response_pair {
516            Some(pair) => crate::translate::stream(
517                pair,
518                &ResponseCtx {
519                    model: &req.model,
520                    original_request: &original,
521                    translated_request: &body,
522                    tool_search: false,
523                },
524                &req,
525            ),
526            None => Box::new(Identity),
527        };
528        let mut lines = wire::lines(upstream, MAX_LINE);
529        if req.usage.enabled() {
530            // StreamUsageBuffer.ObserveOpenAIStream per line; the served model is observed
531            // per accepted frame (Frames::frame).
532            let usage = req.usage.clone();
533            lines = lines
534                .inspect(move |line| {
535                    if let Ok(line) = line {
536                        usage.response_usage_line(Format::OpenAI, line);
537                    }
538                })
539                .boxed();
540        }
541        let claude = ClaudeInputTokens::new(req.source_format, Format::OpenAI, req.response_format, original.clone());
542        let stream = frames(
543            lines,
544            translator,
545            claude,
546            req.response_format,
547            capture,
548            req.usage.clone(),
549        );
550        return Ok(ExecResponse {
551            status: 200,
552            headers: response_headers,
553            body: ResponseBody::Stream(stream),
554        });
555    }
556    let raw = wire::read_all(upstream).await;
557    capture.read(&raw);
558    let raw = raw?;
559    // ObserveResponseModel(body) and Publish(ParseOpenAIUsage(body)).
560    if req.usage.enabled() {
561        req.usage.response_body(target, &raw);
562    }
563    let ctx = ResponseCtx {
564        model: &req.model,
565        original_request: &original,
566        translated_request: &body,
567        tool_search: false,
568    };
569    let mut out = match response_pair {
570        Some(pair) => (pair.non_stream)(&ctx, &raw)
571            .map_err(|_| ExecError::local(502, FailureScope::Request, EMPTY_TRANSLATION))?,
572        // Go's TranslateNonStream still runs the plugin response hooks.
573        None => {
574            cpa_translate::hooks::unregistered_non_stream(target.as_str(), req.response_format.as_str(), &ctx, &raw)
575                .unwrap_or_else(|| raw.to_vec())
576        }
577    };
578    if out.is_empty() {
579        return Err(ExecError::local(502, FailureScope::Request, EMPTY_TRANSLATION));
580    }
581    if req.response_format == Format::OpenAIResponse {
582        out = payload::ensure_responses_usage_details(&out);
583    }
584    Ok(ExecResponse {
585        status: 200,
586        headers: response_headers,
587        body: ResponseBody::Buffered(Bytes::from(out)),
588    })
589}
590
591const DEFAULT_SUMMARY_MAX_TOKENS: i64 = 8192;
592
593/// `compaction.summary-max-tokens` and `compaction.summary-reasoning-effort`. These
594/// apply only to the internal summary request: normal turns keep the client's budget
595/// and effort, and an explicitly larger summary budget is never lowered.
596fn summary_budget(cfg: &Config) -> (i64, Option<String>) {
597    // ponytail: one global floor for every OpenAI-compatible upstream; lift this to a
598    // per-provider override when a second upstream needs a different value.
599    let section = cfg.document.get("compaction");
600    let max_tokens = section
601        .and_then(|c| c.get("summary-max-tokens"))
602        .and_then(cpa_core::config::go_int)
603        .unwrap_or(DEFAULT_SUMMARY_MAX_TOKENS);
604    let effort = section
605        .and_then(|c| c.get("summary-reasoning-effort"))
606        .and_then(|v| v.as_str())
607        .map(str::trim)
608        .filter(|v| !v.is_empty())
609        .map(str::to_owned);
610    (max_tokens, effort)
611}
612
613/// Reasoning efforts an OpenAI-compatible upstream accepts in `reasoning_effort`.
614const SUMMARY_EFFORTS: [&str; 5] = ["minimal", "low", "medium", "high", "xhigh"];
615
616/// A reasoning upstream can spend the caller's whole output budget on
617/// `reasoning_content` and return an empty `content`, which then looks like "no
618/// summary text". Give the summary a floor, and keep its thinking short. `body` is
619/// the Responses summary payload, before translation: the floor reaches the upstream
620/// as `max_tokens` or `max_completion_tokens`, and the effort as `reasoning_effort`
621/// after the model's thinking rules, like any client value.
622fn apply_summary_budget(body: &mut Vec<u8>, max_tokens: i64, effort: Option<&str>) {
623    if max_tokens > 0 && gj::get(body, "max_output_tokens").int() < max_tokens {
624        gj::set_int(body, "max_output_tokens", max_tokens);
625    }
626    match effort {
627        Some("keep") => {}
628        Some(value) if SUMMARY_EFFORTS.contains(&value) => {
629            gj::set_str(body, "reasoning.effort", value);
630        }
631        // Unset or not an OpenAI effort: lower the effort only when the client sent one.
632        _ => {
633            if gj::get(body, "reasoning.effort").exists() {
634                gj::set_str(body, "reasoning.effort", "low");
635            }
636        }
637    }
638}
639
640/// Responses compaction v2 for OpenAI-compatible upstreams: the trigger turn is
641/// answered by an ordinary non-stream chat summary, sealed into the CPA compaction
642/// capsule, so Codex gets exactly one `compaction` output item. Later requests carry
643/// that capsule and are expanded by `antigravity_compaction::expand_capsules`. The
644/// summary request goes through [`chat_request`], so it gets the same thinking, compatibility
645/// normalization, payload rules, URL checks and capture as any chat request.
646async fn compact_summary(
647    client: &wreq::Client,
648    credential: &Credential,
649    mut req: ExecRequest,
650    cfg: &Config,
651    base_url: &str,
652    api_key: &str,
653    compat: Option<&payload::Compat>,
654) -> Result<ExecResponse, ExecError> {
655    let model = parse_suffix(&req.model).model_name.to_owned();
656    let stream = req.stream;
657    let source = crate::antigravity_compaction::source_payload(&req.body, &req.original_body);
658    let mut summary = crate::antigravity_compaction::summary_payload(source);
659    let (max_tokens, effort) = summary_budget(cfg);
660    apply_summary_budget(&mut summary, max_tokens, effort.as_deref());
661    req.body = summary.into();
662    req.original_body = Bytes::new();
663    req.alt = None;
664    req.stream = false;
665    req.source_format = Format::OpenAIResponse;
666    req.response_format = Format::OpenAI;
667    let answer = Box::pin(chat_request(client, credential, req, cfg, base_url, api_key, compat)).await?;
668    let ResponseBody::Buffered(raw) = answer.body else {
669        return Err(crate::antigravity_compaction::statusless(
670            "extract summary: no summary text found in upstream response",
671        ));
672    };
673    let text = crate::antigravity_compaction::summary_text(&raw)
674        .map_err(|e| crate::antigravity_compaction::statusless(format!("extract summary: {e}")))?;
675    let sealed = crate::antigravity_compaction::seal(&text, &model)
676        .map_err(|e| crate::antigravity_compaction::statusless(format!("seal compaction capsule: {e}")))?;
677    let usage = crate::antigravity_compaction::chat_usage(&raw);
678
679    let mut headers = http::HeaderMap::new();
680    if !stream {
681        headers.insert(
682            http::header::CONTENT_TYPE,
683            http::HeaderValue::from_static("application/json"),
684        );
685        return Ok(ExecResponse {
686            status: 200,
687            headers,
688            body: ResponseBody::Buffered(crate::antigravity_compaction::response(&model, &sealed, usage).into()),
689        });
690    }
691    headers.insert(
692        http::header::CONTENT_TYPE,
693        http::HeaderValue::from_static("text/event-stream"),
694    );
695    let chunks: Vec<Result<Bytes, ExecError>> = crate::antigravity_compaction::stream_chunks(&model, &sealed, usage)
696        .into_iter()
697        .map(|chunk| Ok(Bytes::from(chunk)))
698        .collect();
699    Ok(ExecResponse {
700        status: 200,
701        headers,
702        body: ResponseBody::Stream(futures_util::stream::iter(chunks).boxed()),
703    })
704}
705
706/// `ApplyMediaPayloadConfig` on a prepared images request (868e137): the payload rules
707/// run last, on the body's JSON view, against the client's original request.
708fn media_barrier(
709    credential: &Credential,
710    req: &ExecRequest,
711    cfg: &Config,
712    model: &str,
713    body: &[u8],
714    content_type: &str,
715    client_content_type: &str,
716) -> Result<(Bytes, String), ExecError> {
717    let original = if req.original_body.is_empty() {
718        &req.body
719    } else {
720        &req.original_body
721    };
722    let (body, content_type) =
723        crate::payload_media::apply(body, content_type, original, client_content_type, |view, original| {
724            crate::payload_barrier::Barrier {
725                executor: &credential.provider,
726                model,
727                protocol: Format::OpenAI.as_str(),
728                root: "",
729                original,
730            }
731            .apply(cfg, req, view)
732        })
733        .map_err(plain_err)?;
734    Ok((Bytes::from(body), content_type))
735}
736
737/// `prepareOpenAICompatImagesPayload`.
738pub(crate) fn prepare_images_payload(
739    body: &[u8],
740    model: &str,
741    content_type: &str,
742    stream: bool,
743) -> Result<(Bytes, String), ExecError> {
744    if go::json_valid(body) {
745        let mut body = body.to_vec();
746        if !model.is_empty() {
747            payload::set_str_if_different(&mut body, "model", model);
748        }
749        if stream {
750            payload::set_bool_if_different(&mut body, "stream", true);
751        } else {
752            gj::delete(&mut body, "stream");
753        }
754        return Ok((Bytes::from(body), "application/json".into()));
755    }
756    let Some(boundary) = multipart::boundary(content_type) else {
757        return Ok((Bytes::copy_from_slice(body), content_type.to_owned()));
758    };
759    // Go returns a plain error here; the handler answers 500.
760    let boundary = boundary.map_err(plain_err)?;
761    let form =
762        multipart::read_form(body, &boundary).map_err(|e| plain_err(format!("read multipart form failed: {e}")))?;
763    let (out, content_type) = multipart::rewrite_images_form(&form, model, stream, false);
764    Ok((Bytes::from(out), content_type))
765}
766
767/// `helps.PayloadRequestedModel`: the client's model, else the execution model.
768fn route_model(req: &ExecRequest) -> &str {
769    if req.requested_model.trim().is_empty() {
770        req.model.trim()
771    } else {
772        req.requested_model.trim()
773    }
774}
775
776/// `applyPromptCacheKey`.
777fn prompt_cache_key(
778    compat: Option<&payload::Compat>,
779    provider: &str,
780    req: &ExecRequest,
781    base_model: &str,
782    mut body: Vec<u8>,
783) -> Vec<u8> {
784    if !compat.is_some_and(|c| c.support_prompt_cache_key) {
785        return body;
786    }
787    let explicit = [&req.body[..], &req.original_body[..], &body[..]]
788        .into_iter()
789        .map(|source| gj::get(source, "prompt_cache_key").str().trim().to_owned())
790        .find(|key| !key.is_empty());
791    if let Some(key) = explicit {
792        payload::set_str_if_different(&mut body, "prompt_cache_key", &key);
793        return body;
794    }
795    let model = gj::get(&body, "model").str().trim().to_owned();
796    let model = if model.is_empty() { base_model.to_owned() } else { model };
797    if req.source_format == Format::Claude
798        && let Some(key) = payload::claude_code_prompt_cache(&model, &req.body, &req.headers)
799    {
800        payload::set_str_if_different(&mut body, "prompt_cache_key", &key);
801        return body;
802    }
803    let Some(session) = provider_session_uuid(provider, req) else {
804        return body;
805    };
806    let provider = provider.trim().go_lower();
807    let identity = format!(
808        "cli-proxy-api:openai-compat:prompt-cache\0{provider}\0{}\0{}\0{session}",
809        model.go_lower(),
810        req.source_format.as_str().trim().go_lower()
811    );
812    let key = uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_OID, identity.as_bytes()).to_string();
813    payload::set_str_if_different(&mut body, "prompt_cache_key", &key);
814    body
815}
816
817/// `helps.ProviderSessionUUID`: the execution session, else the derived session identity,
818/// as a provider-scoped stable UUID.
819fn provider_session_uuid(provider: &str, req: &ExecRequest) -> Option<String> {
820    let nonempty = |s: &Option<String>| s.as_deref().map(str::trim).filter(|s| !s.is_empty()).map(str::to_owned);
821    let (kind, value) = match nonempty(&req.execution_session) {
822        Some(execution) => ("execution-session", execution),
823        None => ("derived-session", nonempty(&req.derived_session)?),
824    };
825    let provider = provider.trim().go_lower();
826    if provider.is_empty() {
827        return None;
828    }
829    let identity = format!("cli-proxy-api\0{provider}\0{kind}\0{value}");
830    Some(uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_OID, identity.as_bytes()).to_string())
831}
832
833/// `CountTokens`: a local tiktoken estimate of the translated chat request.
834pub(crate) fn count_tokens(
835    credential: &Credential,
836    req: &ExecRequest,
837    cfg: &Config,
838) -> Result<ExecResponse, ExecError> {
839    let base_model = parse_suffix(&req.model).model_name;
840    let caps = req.resolved_model.as_ref().map(|r| ModelCaps::from(&r.info));
841    let is_compat = req.resolved_model.as_ref().is_some_and(|r| r.is_compat());
842    let body = translate_body(req, cfg, Format::OpenAI, &base_model, false, &req.body, is_compat)?;
843    // The rule baseline: the translated original, else the translated body before
844    // thinking. Go counts with a failed translation's partial body; this refuses.
845    let original = if req.original_body.is_empty() {
846        body.clone()
847    } else {
848        translate_body(
849            req,
850            cfg,
851            Format::OpenAI,
852            &base_model,
853            false,
854            &req.original_body,
855            is_compat,
856        )?
857    };
858    let body = apply_thinking(body, req, Format::OpenAI, &credential.provider, caps.as_ref())?;
859    let body = crate::payload_barrier::Barrier {
860        executor: &credential.provider,
861        model: &base_model,
862        protocol: Format::OpenAI.as_str(),
863        root: "",
864        original: &original,
865    }
866    .apply(cfg, req, body);
867    let count = payload::count_chat_tokens(&base_model, &body).map_err(|e| {
868        ExecError::local(
869            500,
870            FailureScope::Request,
871            format!("openai compat executor: token counting failed: {e}"),
872        )
873    })?;
874    let usage = format!(r#"{{"usage":{{"prompt_tokens":{count},"completion_tokens":0,"total_tokens":{count}}}}}"#);
875    let out = cpa_translate::translate_token_count(req.response_format, Format::OpenAI, count, usage.as_bytes());
876    Ok(ExecResponse {
877        status: 200,
878        headers: Default::default(),
879        body: ResponseBody::Buffered(Bytes::from(out)),
880    })
881}
882
883/// Same-format passthrough when no pair is registered: one `data:` event per frame.
884struct Identity;
885
886impl StreamTranslator for Identity {
887    fn event(&mut self, event: &[u8]) -> Result<Vec<Bytes>, cpa_translate::Error> {
888        Ok(if event.starts_with(b"data: [DONE]") {
889            Vec::new()
890        } else {
891            vec![Bytes::from([event, b"\n\n"].concat())]
892        })
893    }
894
895    fn finish(&mut self) -> Result<Vec<Bytes>, cpa_translate::Error> {
896        Ok(Vec::new())
897    }
898}
899
900fn trim(bytes: &[u8]) -> &[u8] {
901    go::trim_space(bytes)
902}
903
904/// The ExecuteStream scan loop: SSE lines grouped into frames, Go's error detection on
905/// every frame, and the terminal rules for `[DONE]` and EOF.
906struct Frames {
907    translator: Box<dyn StreamTranslator>,
908    /// `TranslateStreamWithClaudeInputTokens` (Go f3e36f19): a Claude client's
909    /// `message_start` gets the estimate of its request when the upstream reports none.
910    claude: ClaudeInputTokens,
911    responses: bool,
912    claude_client: bool,
913    event: String,
914    data: Vec<Vec<u8>>,
915    ready: VecDeque<Result<Bytes, ExecError>>,
916    seen_done: bool,
917    /// A data frame carried a finish_reason (Go 4f2e5270 `openAICompatChunkHasFinishReason`).
918    saw_finish_reason: bool,
919    failed: bool,
920    capture: wire::Capture,
921    usage: cpa_core::exec::UsageSink,
922}
923
924impl Frames {
925    /// `publishStreamError(err, false)`: the error is logged and published as a failure
926    /// before the client gets it.
927    fn fail(&mut self, status: u16, message: impl Into<String>) -> bool {
928        let error = status_err(status, message);
929        self.capture.error(&error);
930        wire::publish_failure(&self.usage, &error);
931        self.terminal(error);
932        true
933    }
934
935    /// `publishStreamError(err, true)`: the upstream's error payload is neither logged nor
936    /// published; a fixed text stands in for it.
937    fn fail_payload(&mut self, status: u16, message: impl Into<String>) -> bool {
938        let logged = status_err(status, "upstream stream returned an error payload");
939        self.capture.error(&logged);
940        wire::publish_failure(&self.usage, &logged);
941        self.terminal(status_err(status, message));
942        true
943    }
944
945    /// A terminal error: a Responses client first gets the frame its Go framer would
946    /// flush (`responsesSSEFramer.Flush`), then the error.
947    fn terminal(&mut self, error: ExecError) {
948        let mut flushed = self.translator.flush_frames();
949        self.usage_details(&mut flushed);
950        self.ready.extend(flushed.into_iter().map(Ok));
951        self.ready.push_back(Err(error));
952        self.failed = true;
953    }
954
955    /// TranslateStreamWithClaudeInputTokens: Responses usage gets its detail fields
956    /// unless the apply_patch bridge failed. Go patches before the SSE framer, so frames
957    /// the framer flushes later get them too.
958    fn usage_details(&self, out: &mut [Bytes]) {
959        if self.responses && !self.translator.tool_input_failed() {
960            for chunk in out {
961                *chunk = Bytes::from(payload::ensure_responses_usage_details(chunk));
962            }
963        }
964    }
965
966    /// One Go `TranslateStream` call per frame's joined `data:` line: no blank line
967    /// follows it, so plugin stream hooks run once per line, as in Go.
968    fn translate(&mut self, line: &[u8]) -> bool {
969        match self.translator.event(line) {
970            Ok(mut out) => {
971                self.usage_details(&mut out);
972                self.claude.apply(&mut out);
973                self.ready.extend(out.into_iter().map(Ok));
974                false
975            }
976            Err(e) => self.fail(502, e.to_string()),
977        }
978    }
979
980    /// `processFrame`; true ends the scan.
981    fn frame(&mut self) -> bool {
982        let event = std::mem::take(&mut self.event);
983        let data = std::mem::take(&mut self.data);
984        if data.is_empty() {
985            return payload::error_event(&event) && self.fail(502, "upstream error event ended without data");
986        }
987        if data.len() > 1 && data.iter().any(|d| trim(d) == b"[DONE]") {
988            return self.fail(502, "upstream stream ended with incomplete data before [DONE]");
989        }
990        let joined = data.join(&b"\n"[..]);
991        let payload_bytes = trim(&joined);
992        let done = payload_bytes == b"[DONE]";
993        if done && payload::error_event(&event) {
994            return self.fail(502, "upstream error event ended before [DONE]");
995        }
996        if !done && !go::json_valid(payload_bytes) {
997            return self.fail(502, "upstream stream ended with incomplete SSE data frame");
998        }
999        if !done && let Some(status) = payload::stream_data_error(payload_bytes, &event) {
1000            return self.fail_payload(status, String::from_utf8_lossy(payload_bytes));
1001        }
1002        if !done && !self.saw_finish_reason {
1003            self.saw_finish_reason = has_finish_reason(payload_bytes);
1004        }
1005        // ObserveChatTokenEvent (Go f8e4a840): the served model comes from frames that
1006        // passed these checks, not from every line.
1007        self.usage.response_model_line(payload_bytes);
1008        let mut line = b"data: ".to_vec();
1009        line.extend_from_slice(payload_bytes);
1010        if self.translate(&line) {
1011            return true;
1012        }
1013        // helps.ApplyPatchTranslationError: the frame's chunks go out, then the 502.
1014        if self.translator.tool_input_failed() {
1015            return self.fail(502, EMPTY_TRANSLATION);
1016        }
1017        if done {
1018            self.seen_done = true;
1019        }
1020        done
1021    }
1022
1023    /// One scanned line; true ends the scan.
1024    fn line(&mut self, line: &[u8]) -> bool {
1025        let line = trim(line);
1026        if line.is_empty() {
1027            return self.frame();
1028        }
1029        if let Some(rest) = line.strip_prefix(b"data:") {
1030            self.data.push(trim(rest).to_vec());
1031        } else if let Some(rest) = line.strip_prefix(b"event:") {
1032            self.event = String::from_utf8_lossy(trim(rest)).into_owned();
1033        } else if line.starts_with(b":") || line.starts_with(b"id:") || line.starts_with(b"retry:") {
1034        } else if line.starts_with(b"{") || line.starts_with(b"[") {
1035            return self.fail_payload(502, String::from_utf8_lossy(line));
1036        }
1037        false
1038    }
1039
1040    /// `helps.EndApplyPatchStream` when the transport ends: the translator's closing
1041    /// apply_patch frames, then the 502 when its tool input failed. True ends the stream.
1042    fn end_tool_input(&mut self) -> bool {
1043        let out = self.translator.finalize_tool_input();
1044        self.ready.extend(out.into_iter().map(Ok));
1045        if self.translator.tool_input_failed() {
1046            // RecordApplyPatchStreamFailure publishes; nothing is logged.
1047            let error = status_err(502, EMPTY_TRANSLATION);
1048            wire::publish_failure(&self.usage, &error);
1049            self.terminal(error);
1050            return true;
1051        }
1052        false
1053    }
1054
1055    /// Clean EOF: flush a pending frame, then Go's terminal rule per client format.
1056    fn eof(&mut self) {
1057        if !self.seen_done && !self.failed && !self.data.is_empty() {
1058            self.frame();
1059        }
1060        if self.failed || self.end_tool_input() {
1061            return;
1062        }
1063        if !self.seen_done {
1064            // 0ea4e1dc: a finish reason that left every item done completes the stream.
1065            if self.responses && self.translator.can_finalize_response_stream() {
1066                if self.translate(b"data: [DONE]") {
1067                    return;
1068                }
1069                self.seen_done = !self.translator.can_finalize_response_stream();
1070            }
1071        }
1072        if !self.seen_done {
1073            // 4f2e5270: a Claude client's EOF fails too unless a chunk carried a finish
1074            // reason; both failures publish the usage seen so far.
1075            if self.responses || (self.claude_client && !self.saw_finish_reason) {
1076                let error = status_err(502, "upstream stream closed before [DONE]");
1077                self.capture.error(&error);
1078                self.usage
1079                    .publish_failure_with_usage(error.status, &String::from_utf8_lossy(&error.body));
1080                self.terminal(error);
1081                return;
1082            }
1083            if self.translate(b"data: [DONE]") {
1084                return;
1085            }
1086        }
1087        self.finish();
1088    }
1089
1090    fn finish(&mut self) {
1091        match self.translator.finish() {
1092            Ok(mut out) => {
1093                self.usage_details(&mut out);
1094                self.ready.extend(out.into_iter().map(Ok));
1095            }
1096            Err(e) => {
1097                self.fail(502, e.to_string());
1098            }
1099        }
1100    }
1101}
1102
1103/// Go `openAICompatChunkHasFinishReason`: a choice whose `finish_reason` is not null and
1104/// not blank after `strings.TrimSpace`.
1105fn has_finish_reason(payload: &[u8]) -> bool {
1106    gj::get(payload, "choices").array().iter().any(|choice| {
1107        let reason = choice.get("finish_reason");
1108        reason.kind != gj::Kind::Null && !go::trim_space(&reason.bytes()).is_empty()
1109    })
1110}
1111
1112fn frames(
1113    lines: ExecStream,
1114    translator: Box<dyn StreamTranslator>,
1115    claude: ClaudeInputTokens,
1116    client: Format,
1117    capture: wire::Capture,
1118    usage: cpa_core::exec::UsageSink,
1119) -> ExecStream {
1120    let state = Frames {
1121        translator,
1122        claude,
1123        responses: client == Format::OpenAIResponse,
1124        claude_client: client == Format::Claude,
1125        event: String::new(),
1126        data: Vec::new(),
1127        ready: VecDeque::new(),
1128        seen_done: false,
1129        saw_finish_reason: false,
1130        failed: false,
1131        capture,
1132        usage,
1133    };
1134    futures_util::stream::unfold((lines, state, false), |(mut lines, mut state, mut ended)| async move {
1135        loop {
1136            if let Some(item) = state.ready.pop_front() {
1137                if item.is_err() {
1138                    state.ready.clear();
1139                    ended = true;
1140                }
1141                return Some((item, (lines, state, ended)));
1142            }
1143            if ended {
1144                return None;
1145            }
1146            match lines.next().await {
1147                Some(Ok(line)) => {
1148                    // AppendAPIResponseChunk per scanned line.
1149                    state.capture.chunk(&line);
1150                    if state.line(&line) {
1151                        if !state.failed && !state.end_tool_input() {
1152                            state.finish();
1153                        }
1154                        ended = true;
1155                    }
1156                }
1157                Some(Err(error)) => {
1158                    // Go ends apply_patch input before reporting the scan error.
1159                    if !state.end_tool_input() {
1160                        state.capture.error(&error);
1161                        wire::publish_failure(&state.usage, &error);
1162                        state.terminal(error);
1163                    }
1164                    ended = true;
1165                }
1166                None => {
1167                    state.eof();
1168                    ended = true;
1169                }
1170            }
1171        }
1172    })
1173    .boxed()
1174}
1175
1176#[cfg(test)]
1177#[path = "openai_compat_tests.rs"]
1178mod tests;