Skip to main content

cpa_exec/
meta.rs

1//! Meta Muse executor (internal/runtime/executor/meta_executor*.go).
2//!
3//! Every client format is translated to Codex Responses and sent, always streaming, to
4//! `{base}/responses`. Non-streaming clients get the completed response collected from
5//! that SSE stream. A credential holds either a minted API key or only the DCA token
6//! (`dca:...`) a key is minted from: by [`MetaExecutor::prepare`] (persisted), or inline
7//! for one request as Go's ensureAuth does when preparation did not run.
8//!
9//! Translation uses cpa-translate (the registered client <-> Codex pair), thinking
10//! cpa_common::thinking, JSON edits cpa_common::json, and the Codex and OpenAI-compatible
11//! executors' ports of the shared Responses helpers. Stages whose shared module has not
12//! landed go through adapters named after their owners (meta_codex, kimi_http, below).
13//! Translator apply_patch failures end streams with Go's 502. Go's apply_patch Responses
14//! bridge (cpa_translate::apply_patch_responses) runs around the Codex wire: a client's
15//! custom `apply_patch` tool goes upstream as a JSON function and its calls come back as
16//! the custom tool.
17
18use cpa_core::gostr::GoStr;
19use std::collections::VecDeque;
20use std::time::Duration;
21
22use bytes::Bytes;
23use cpa_common::json::{self as gj, Kind};
24use cpa_common::thinking::{ModelCaps, RequestThinking, apply_request_thinking, parse_suffix};
25use cpa_core::config::Config;
26use cpa_core::credential::{Credential, MetadataPatch, Source};
27use cpa_core::exec::{ExecError, ExecRequest, ExecResponse, ExecStream, FailureScope, Operation, ResponseBody};
28use cpa_core::format::Format;
29use cpa_translate::{Pair, RequestCtx, ResponseCtx, StreamTranslator, apply_patch_responses};
30use futures_util::StreamExt;
31use serde_json::Value;
32
33use crate::codex_response::OutputItems;
34use crate::codex_tokens::count_input_tokens;
35use crate::kimi_http::{
36    DeferredUsage, UsageRule, credential_headers, defer_usage, payload_rules, read_all_strict, refresh_due,
37    report_model, rfc3339_local_now,
38};
39use crate::meta_auth::{DEFAULT_API_BASE_URL, MetaAuth, MintedKey};
40use crate::meta_codex::go_trim_space;
41use crate::openai_compat_payload::{ensure_responses_usage_details, sanitize_reasoning_encrypted_content};
42use crate::proxy::{GoClients, GoHeaders, MAX_ERROR_BODY, Proxy, default_client, lines, read_all, send};
43use crate::tokenizer::Encoding;
44use cpa_common::codex_client::normalize_codex_instructions;
45
46/// Provider string served by this executor.
47pub const PROVIDER: &str = "meta";
48/// `metaUserAgent`.
49pub const USER_AGENT: &str =
50    "muse-build/1.3.0 (interactive; macos-aarch64; build ac7280f2aca67769d1455a8847bb502b617d50f6)";
51/// `metaNotFoundCooldown`: a 404 without `resets_at` cools the model for five minutes.
52const NOT_FOUND_COOLDOWN: Duration = Duration::from_secs(300);
53/// Go's bufio.Scanner limit for the upstream stream.
54const LINE_LIMIT: usize = 52_428_800;
55const DISCONNECTED: &str = "meta stream error: stream disconnected before response.completed or response.incomplete";
56
57pub struct MetaExecutor {
58    clients: GoClients,
59    mint_url: Option<String>,
60}
61
62impl Default for MetaExecutor {
63    fn default() -> Self {
64        Self::with_client(default_client())
65    }
66}
67
68impl MetaExecutor {
69    /// Uses a caller-built client (tests point it at local mocks).
70    pub fn with_client(client: wreq::Client) -> Self {
71        Self {
72            clients: GoClients::with_default(client),
73            mint_url: None,
74        }
75    }
76
77    /// Mints against a local mock instead of `META_MINT_URL` / api.meta.ai.
78    pub fn with_mint_url(mut self, url: &str) -> Self {
79        self.mint_url = Some(url.to_owned());
80        self
81    }
82
83    /// ShouldPrepareRequestAuth (no usable key but a DCA token), or a due
84    /// `refresh_interval`: the SDK refresh lead is nil, so nothing else schedules a mint.
85    pub fn needs_prepare(&self, credential: &Credential, cfg: &Config) -> bool {
86        self.needs_prepare_at(credential, cfg, chrono::Utc::now())
87    }
88
89    pub fn needs_prepare_at(&self, credential: &Credential, _cfg: &Config, now: chrono::DateTime<chrono::Utc>) -> bool {
90        self.must_mint(credential)
91            || (!is_config_api_key(credential)
92                // Go's refresh loop never schedules API-key-kind credentials.
93                && !cpa_core::registry::dynamic::is_api_key(credential)
94                && dca_token(credential).is_some()
95                && refresh_due(credential, None, now))
96    }
97
98    /// `ShouldPrepareRequestAuth`: requests must wait for a mint because there is no
99    /// usable key, only a DCA token.
100    pub fn must_mint(&self, credential: &Credential) -> bool {
101        !is_config_api_key(credential) && creds(credential).1.is_empty() && dca_token(credential).is_some()
102    }
103
104    /// Refresh: re-mints the API key from the DCA token. Also what the runtime should call
105    /// after an upstream 401 (Go's tryRefreshAfterUnauthorized covers Meta DCA tokens).
106    pub async fn prepare(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
107        self.refresh(credential, cfg).await
108    }
109
110    async fn refresh(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
111        // Go `helps.RefreshAuthViaHome`: Home refreshes the credentials it dispatched.
112        if credential
113            .attributes
114            .contains_key(cpa_core::config::credentials::HOME_PROVIDER)
115        {
116            return refresh_via_home(credential).await;
117        }
118        let Some(dca) = dca_token(credential) else {
119            if !creds(credential).1.is_empty() {
120                return Ok(MetadataPatch::default());
121            }
122            return Err(ExecError::local(
123                401,
124                FailureScope::Credential,
125                "meta executor: missing API key or DCA token",
126            ));
127        };
128        let mut auth = MetaAuth::new(self.clients.get(&Proxy::effective(credential, cfg)));
129        if let Some(url) = &self.mint_url {
130            auth = auth.with_mint_url(url);
131        }
132        let minted = auth.mint_api_key(&dca).await.map_err(|e| {
133            ExecError::local(
134                500,
135                FailureScope::Credential,
136                format!("meta executor: mint API key failed: {}", e.redacted()),
137            )
138        })?;
139        if minted.api_key.is_empty() {
140            return Err(ExecError::local(
141                500,
142                FailureScope::Credential,
143                "meta executor: mint API key returned empty key",
144            ));
145        }
146        Ok(refresh_patch(credential, &dca, &minted, rfc3339_local_now()))
147    }
148
149    /// ensureAuth + enrichAuth: a usable key (minting inline from the DCA token when
150    /// missing), `base_url`/`api_key` attributes, and Meta's User-Agent as a default
151    /// custom header.
152    async fn ensure_auth(&self, credential: &Credential, cfg: &Config) -> Result<Credential, ExecError> {
153        let mut c = credential.clone();
154        if creds(&c).1.is_empty() && dca_token(&c).is_some() {
155            let patch = self.refresh(&c, cfg).await?;
156            patch.apply(&mut c.metadata);
157            // Go's Refresh also writes these attributes on the request's auth.
158            for key in ["base_url", "api_key", "access_token"] {
159                if let Some(Value::String(v)) = patch.set.get(key) {
160                    c.attributes.insert(key.into(), v.clone());
161                }
162            }
163        }
164        let (base, token) = creds(&c);
165        if token.is_empty() {
166            let message = if is_config_api_key(&c) {
167                "meta executor: meta-api-key requires a valid API key (DCA tokens require OAuth storage)"
168            } else {
169                "meta executor: missing API key or access token"
170            };
171            return Err(ExecError::local(401, FailureScope::Credential, message));
172        }
173        if c.attributes.get("base_url").is_none_or(|v| v.trim().is_empty()) {
174            c.attributes.insert("base_url".into(), base);
175        }
176        if c.attributes.get("api_key").is_none_or(|v| v.trim().is_empty()) {
177            c.attributes.insert("api_key".into(), token);
178        }
179        c.attributes
180            .entry("header:User-Agent".into())
181            .or_insert_with(|| USER_AGENT.into());
182        Ok(c)
183    }
184
185    pub async fn execute(
186        &self,
187        credential: &Credential,
188        req: ExecRequest,
189        cfg: &Config,
190    ) -> Result<ExecResponse, ExecError> {
191        if req.operation == Operation::CountTokens {
192            return self.count_tokens(credential, req, cfg).await;
193        }
194        if req.alt.as_deref() == Some("responses/compact") {
195            return Err(ExecError::local(
196                501,
197                FailureScope::Request,
198                "/responses/compact not supported",
199            ));
200        }
201        let enriched = self.ensure_auth(credential, cfg).await?;
202        let prepared = prepare(&req, cfg, true)?;
203        req.usage.request(Format::Codex, &prepared.body);
204        let (base, token) = creds(&enriched);
205        if base.trim().is_empty() {
206            return Err(ExecError::local(
207                401,
208                FailureScope::Credential,
209                "meta executor: missing provider baseURL",
210            ));
211        }
212        let url = format!("{}/responses", base.strip_suffix('/').unwrap_or(&base));
213        let client = self.clients.get(&Proxy::effective(&enriched, cfg));
214        let body = Bytes::from(prepared.body);
215        // Go: TrackHTTPClient for Execute; ExecuteStream uses TrackHTTPClientRoundTripOnly,
216        // so its first body byte only records the first-packet fallback.
217        use crate::kimi_http::{capture_chunk, capture_error, capture_metadata, capture_request};
218        let capture = req.capture();
219        let request_headers = headers(&enriched, &req, &token);
220        // recordMetaRequest: the enriched credential's account info.
221        let (auth_type, auth_value) = cpa_core::registry::dynamic::account_info(&enriched);
222        capture_request(
223            capture,
224            &enriched,
225            &url,
226            &request_headers,
227            &body,
228            PROVIDER,
229            (auth_type, &auth_value),
230        );
231        req.usage.round_trip_started();
232        let mut upstream = send(&client, &url, request_headers, body.clone(), None)
233            .await
234            .inspect_err(|e| capture_error(capture, e))?;
235        capture_metadata(capture, upstream.status, &upstream.headers);
236        upstream.body = crate::kimi_http::track_first_byte(upstream.body, &req.usage, req.stream);
237        if !(200..300).contains(&upstream.status) {
238            let headers = upstream.headers.clone();
239            // Go returns a failed error-body read as is, never classified by status.
240            let error_body = read_all_strict(upstream.body, MAX_ERROR_BODY)
241                .await
242                .inspect_err(|e| capture_error(capture, e))?;
243            // ponytail: the MAX_ERROR_BODY prefix (proxy.rs ceiling); Go logs the whole body.
244            capture_chunk(capture, &error_body);
245            // Go observes the response model on every non-stream body, errors included.
246            if !req.stream {
247                report_model(&req.usage, Format::Codex, &error_body);
248            }
249            let mut error = upstream_error(upstream.status, &error_body);
250            error.headers = Box::new(headers);
251            return Err(error);
252        }
253        let ctx = response_ctx(&req, &body);
254        let responses_client = req.response_format == Format::OpenAIResponse;
255        let out = if req.stream {
256            // Go publishes stream usage only through its buffer (no EnsurePublished).
257            req.usage.usage_required();
258            // Go observes `data:` lines: their response model, and usage once completed.
259            let lines = crate::kimi_http::capture_lines(lines(upstream.body, LINE_LIMIT), capture);
260            let (tapped, usage) = defer_usage(lines, &req.usage, UsageRule::MetaResponses);
261            ResponseBody::Stream(stream_events(
262                tapped,
263                crate::translate::stream(prepared.response, &ctx, &req),
264                crate::gemini_stream::ClaudeInputTokens::new(
265                    req.source_format,
266                    Format::Codex,
267                    req.response_format,
268                    original(&req).clone(),
269                ),
270                responses_client,
271                prepared.apply_patch,
272                usage,
273                capture.clone(),
274            ))
275        } else {
276            let data = read_all(upstream.body, usize::MAX, false)
277                .await
278                .inspect_err(|e| capture_error(capture, e))?;
279            capture_chunk(capture, &data);
280            report_model(&req.usage, Format::Codex, &data);
281            let source = std::cell::RefCell::new(Vec::new());
282            let mut apply_patch = prepared.apply_patch;
283            let completed = collect_completed(&data, &mut apply_patch, |event| {
284                source.borrow_mut().clear();
285                source.borrow_mut().extend_from_slice(event);
286                // A translator error or empty output is Go's apply_patch 502.
287                (prepared.response.non_stream)(&ctx, event)
288                    .ok()
289                    .filter(|out| !out.is_empty())
290                    .ok_or_else(|| {
291                        ExecError::local(502, FailureScope::Request, cpa_translate::APPLY_PATCH_UPSTREAM_ERROR)
292                    })
293            })?;
294            // The terminal event Go reads model and Codex usage from, once translated.
295            req.usage.response_line(Format::Codex, &source.into_inner());
296            ResponseBody::Buffered(Bytes::from(if responses_client {
297                ensure_responses_usage_details(&completed)
298            } else {
299                completed
300            }))
301        };
302        Ok(ExecResponse {
303            status: upstream.status,
304            headers: upstream.headers,
305            body: out,
306        })
307    }
308
309    /// CountTokens: O200kBase estimate of the translated Codex body, no upstream call.
310    async fn count_tokens(
311        &self,
312        credential: &Credential,
313        req: ExecRequest,
314        cfg: &Config,
315    ) -> Result<ExecResponse, ExecError> {
316        self.ensure_auth(credential, cfg).await?;
317        let prepared = prepare(&req, cfg, false)?;
318        let count = count_input_tokens(Encoding::O200kBase, &prepared.body).map_err(|e| {
319            ExecError::local(
320                500,
321                FailureScope::Request,
322                format!("meta executor: tokenizer init failed: {e}"),
323            )
324        })?;
325        let usage = format!(
326            r#"{{"response":{{"usage":{{"input_tokens":{count},"output_tokens":0,"total_tokens":{count}}}}}}}"#
327        );
328        let payload = cpa_translate::translate_token_count(req.response_format, Format::Codex, count, usage.as_bytes());
329        Ok(ExecResponse {
330            status: 200,
331            headers: http::HeaderMap::new(),
332            body: ResponseBody::Buffered(Bytes::from(payload)),
333        })
334    }
335}
336
337fn response_ctx<'a>(req: &'a ExecRequest, translated: &'a [u8]) -> ResponseCtx<'a> {
338    ResponseCtx {
339        model: &req.model,
340        original_request: original(req),
341        translated_request: translated,
342        tool_search: false,
343    }
344}
345
346/// `opts.OriginalRequest`, falling back to the request payload when empty.
347fn original(req: &ExecRequest) -> &Bytes {
348    if req.original_body.is_empty() {
349        &req.body
350    } else {
351        &req.original_body
352    }
353}
354
355struct Prepared {
356    body: Vec<u8>,
357    /// Transforms for the client's response format against the Codex upstream.
358    response: &'static Pair,
359    /// Go's `ApplyPatchResponsesState` for this request.
360    apply_patch: apply_patch_responses::State,
361}
362
363fn not_registered(what: &str) -> ExecError {
364    ExecError::local(
365        501,
366        FailureScope::Request,
367        format!("{what} translation pair is not registered"),
368    )
369}
370
371/// prepareResponsesRequest. Every Go client format has a Codex translator; here the
372/// unregistered ones answer 501 instead of falling back to a model rewrite.
373fn prepare(req: &ExecRequest, cfg: &Config, stream: bool) -> Result<Prepared, ExecError> {
374    let Some(response) = cpa_translate::pair(req.response_format, Format::Codex) else {
375        return Err(not_registered("Meta response"));
376    };
377    let has_request_transformer = cpa_translate::pair(req.source_format, Format::Codex).is_some();
378    if !has_request_transformer {
379        return Err(not_registered("Meta request"));
380    }
381    let base_model = parse_suffix(&req.model).model_name;
382    // Go: helps.TranslateRequestWithAPIKeyModelCompatibility with APIKeyModelIsCompat.
383    let is_compat = req.resolved_model.as_ref().is_some_and(|r| r.is_compat());
384    let client = crate::codex_client::Client::new(&req.headers, cfg, "", is_compat);
385    let translate = |body: &[u8]| {
386        crate::codex_client::translate_request(
387            req.source_format,
388            Format::Codex,
389            &RequestCtx {
390                model: &base_model,
391                stream,
392                tool_search: false,
393            },
394            body,
395            &client,
396        )
397        .map(|t| t.body)
398        .map_err(|e| ExecError::local(400, FailureScope::Request, e.0))
399    };
400    // Go translates the original request first, for payload-rule defaults, then the
401    // working one, even when they are equal (a stateful plugin normalizer sees both).
402    let original_translated = translate(original(req));
403    let mut body = translate(&req.body)?;
404    let original_translated = crate::translate::baseline_or_working(original_translated, &body);
405    // Go `cliproxyauth.ResolvedModelInfo`: capabilities bound to this attempt.
406    let caps = req.resolved_model.as_ref().map(|r| ModelCaps::from(&r.info));
407    let thinking = apply_request_thinking(&RequestThinking {
408        body: &body,
409        payload: &req.body,
410        original: original(req),
411        model: &req.model,
412        from: req.source_format.as_str(),
413        to: "codex",
414        provider: PROVIDER,
415        resolved: caps.as_ref().map(Some),
416        has_request_transformer,
417        updates_changed: false,
418    })
419    .map_err(|e| ExecError::local(e.status(), FailureScope::Request, e.message))?;
420    body = thinking;
421    set_string_if_different(&mut body, "model", &base_model);
422    set_bool_if_different(&mut body, "stream", stream);
423    for key in [
424        "generate",
425        "prompt_cache_retention",
426        "safety_identifier",
427        "stream_options",
428        "client_metadata",
429    ] {
430        gj::delete(&mut body, key);
431    }
432    let apply_patch = apply_patch_responses::State::new(req.source_format, original(req), &original_translated);
433    body = apply_patch_responses::normalize_executor_request(&body, Some(original(req)))
434        .map_err(|e| ExecError::local(500, FailureScope::Request, e))?;
435    normalize_codex_instructions(&mut body);
436    body = sanitize_reasoning_encrypted_content(body);
437    sanitize_web_search_tools(&mut body);
438    body = cpa_common::payload::normalize_codex_tool_integer_types(&body, &req.headers);
439    // The payload rules are the last change to the body (868e137).
440    body = payload_rules(cfg, req, &base_model, PROVIDER, body, &original_translated);
441    Ok(Prepared {
442        body,
443        response,
444        apply_patch,
445    })
446}
447
448/// `SetStringIfDifferent`.
449fn set_string_if_different(body: &mut Vec<u8>, path: &str, value: &str) {
450    let current = gj::get(body, path);
451    if current.kind == Kind::String && *current.bytes() == *value.as_bytes() {
452        return;
453    }
454    gj::set_str(body, path, value);
455}
456
457/// `SetBoolIfDifferent`.
458fn set_bool_if_different(body: &mut Vec<u8>, path: &str, value: bool) {
459    let kind = gj::get(body, path).kind;
460    if (value && kind == Kind::True) || (!value && kind == Kind::False) {
461        return;
462    }
463    gj::set_bool(body, path, value);
464}
465
466/// `SanitizeMetaWebSearchTools`: Meta rejects `search_content_types` on `web_search` tools,
467/// top level or inside a namespace.
468fn sanitize_web_search_tools(body: &mut Vec<u8>) {
469    let tools = gj::get(body, "tools");
470    if !tools.is_array() {
471        return;
472    }
473    let strip =
474        |tool: &gj::Res<'_>| &*tool.get("type").bytes() == b"web_search" && tool.get("search_content_types").exists();
475    let mut paths = Vec::new();
476    for (i, tool) in tools.array().iter().enumerate() {
477        if strip(tool) {
478            paths.push(format!("tools.{i}.search_content_types"));
479        }
480        let nested = tool.get("tools");
481        if &*tool.get("type").bytes() == b"namespace" && nested.is_array() {
482            for (j, sub) in nested.array().iter().enumerate() {
483                if strip(sub) {
484                    paths.push(format!("tools.{i}.tools.{j}.search_content_types"));
485                }
486            }
487        }
488    }
489    for path in paths {
490        gj::delete(body, &path);
491    }
492}
493
494fn headers(c: &Credential, req: &ExecRequest, token: &str) -> GoHeaders {
495    let mut h = GoHeaders::new();
496    h.set("Content-Type", "application/json");
497    if !token.trim().is_empty() {
498        h.set("Authorization", format!("Bearer {token}"));
499    }
500    h.set("User-Agent", USER_AGENT);
501    h.set("X-Client-Id", "tbh:tui");
502    // Meta always streams upstream.
503    h.set("Accept", "text/event-stream");
504    h.set("Cache-Control", "no-cache");
505    for (name, value) in credential_headers(c, req, original(req)) {
506        h.set(&name, value);
507    }
508    h
509}
510
511/// `wrapMetaUpstreamError`, with Go's cooldown semantics as scopes: a 429 or 404 cools
512/// the model on this credential, a subscription-quota 429 the whole credential.
513fn upstream_error(status: u16, body: &[u8]) -> ExecError {
514    let mut retry_after = None;
515    let scope = match status {
516        429 => {
517            retry_after = resets_in(body);
518            if subscription_quota(body) {
519                FailureScope::Credential
520            } else {
521                FailureScope::Model
522            }
523        }
524        404 => {
525            retry_after = Some(resets_in(body).unwrap_or(NOT_FOUND_COOLDOWN));
526            FailureScope::Model
527        }
528        401 | 402 | 403 | 408 | 500.. => FailureScope::Credential,
529        _ => FailureScope::Request,
530    };
531    ExecError {
532        status,
533        scope,
534        body: Bytes::copy_from_slice(body),
535        headers: Box::default(),
536        retry_after,
537        direct: false,
538        addon: None,
539    }
540}
541
542/// `parseMetaRetryAfter` for a 429/404 body: time until a future `error.resets_at`.
543fn resets_in(body: &[u8]) -> Option<Duration> {
544    let resets_at = gj::get(body, "error.resets_at").int();
545    if resets_at <= 0 {
546        return None;
547    }
548    let at = std::time::UNIX_EPOCH + Duration::from_secs(resets_at as u64);
549    at.duration_since(std::time::SystemTime::now())
550        .ok()
551        .filter(|d| !d.is_zero())
552}
553
554/// `isMetaSubscriptionQuota` (status already 429).
555fn subscription_quota(body: &[u8]) -> bool {
556    if body.is_empty() {
557        return false;
558    }
559    let message = gj::get(body, "error.message").str().go_lower();
560    let code = gj::get(body, "error.code").str().go_lower();
561    if message.contains("subscription quota") || message.contains("quota exhausted") {
562        return true;
563    }
564    (code == "rate_limit_exceeded" || code.contains("quota")) && gj::get(body, "error.resets_at").exists()
565}
566
567/// `metaStreamEventError`: an `error` or `response.failed` event ends the response with
568/// `error.code` as status when it is an HTTP error code, else 502.
569fn stream_event_error(event: &[u8]) -> Option<ExecError> {
570    let kind = gj::get(event, "type").bytes();
571    if &*kind != b"error" && &*kind != b"response.failed" {
572        return None;
573    }
574    let code = gj::get(event, "error.code").int();
575    let status = if (400..=599).contains(&code) { code as u16 } else { 502 };
576    Some(upstream_error(status, event))
577}
578
579/// `metaAsCompletedEvent`: a whole JSON body that is a terminal event or a bare response.
580fn as_completed_event(data: &[u8]) -> Option<Vec<u8>> {
581    let trimmed = go_trim_space(data);
582    if !gj::valid(trimmed) {
583        return None;
584    }
585    let kind = gj::get(trimmed, "type").bytes();
586    if &*kind == b"response.completed" || &*kind == b"response.incomplete" {
587        return Some(trimmed.to_vec());
588    }
589    if &*gj::get(trimmed, "object").bytes() == b"response" || gj::get(trimmed, "output").exists() {
590        return gj::try_set_raw(br#"{"type":"response.completed"}"#, "response", trimmed).ok();
591    }
592    None
593}
594
595fn data_payload(line: &[u8]) -> Option<&[u8]> {
596    line.strip_prefix(b"data:").map(go_trim_space)
597}
598
599fn lossy(b: &[u8]) -> String {
600    String::from_utf8_lossy(b).into_owned()
601}
602
603/// translateMetaCompleted: the first terminal event of a buffered SSE body, with output
604/// items rebuilt from `response.output_item.done`, then a whole-body JSON fallback. Every
605/// event goes through the apply_patch bridge first; a bridge failure is the 502.
606fn collect_completed(
607    data: &[u8],
608    apply_patch: &mut apply_patch_responses::State,
609    translate: impl Fn(&[u8]) -> Result<Vec<u8>, ExecError>,
610) -> Result<Vec<u8>, ExecError> {
611    let bridge_error = || ExecError::local(502, FailureScope::Request, cpa_translate::APPLY_PATCH_UPSTREAM_ERROR);
612    let mut items = OutputItems::default();
613    for line in data.split(|b| *b == b'\n') {
614        let Some(event) = data_payload(line) else {
615            continue;
616        };
617        if let Some(error) = stream_event_error(event) {
618            return Err(error);
619        }
620        let (events, error) = apply_patch.transform(event);
621        if error.is_some() {
622            return Err(bridge_error());
623        }
624        for event in events {
625            let kind = gj::get(&event, "type").bytes();
626            match &*kind {
627                b"response.output_item.done" => items.collect(&lossy(&event)),
628                b"response.completed" | b"response.incomplete" => {
629                    return translate(items.patch(lossy(&event)).as_bytes());
630                }
631                _ => {}
632            }
633        }
634    }
635    if let Some(completed) = as_completed_event(data) {
636        let patched = items.patch(lossy(&completed));
637        let bridged = apply_patch
638            .bridge
639            .transform_non_stream(patched.as_bytes())
640            .map_err(|_| bridge_error())?;
641        return translate(&bridged);
642    }
643    if apply_patch.finish().is_err() {
644        return Err(bridge_error());
645    }
646    Err(ExecError::local(408, FailureScope::Credential, DISCONNECTED))
647}
648
649/// The ExecuteStream loop: every scanned line goes through the apply_patch bridge and then
650/// the pair's translator (`data:` lines normalized to `data: <trimmed>`), terminal events get
651/// their collected output, and an error event, a bridge failure or a scan error ends the
652/// stream. At EOF the bridge's `finish_stream` runs before a scan error is reported. The
653/// translator emits frames the way the client's Go route writes them; usage is committed
654/// only when the stream ends cleanly.
655///
656/// Responses clients get EnsureResponsesUsageDetails per line before translation; Go
657/// applies it to each translated chunk, and the Responses translator only adds
658/// `response.model` to creation events, so the two edits commute.
659fn stream_events(
660    upstream: ExecStream,
661    translator: Box<dyn StreamTranslator>,
662    claude: crate::gemini_stream::ClaudeInputTokens,
663    responses_client: bool,
664    bridge: apply_patch_responses::State,
665    usage: DeferredUsage,
666    capture: cpa_core::exec::CaptureSink,
667) -> ExecStream {
668    struct State {
669        upstream: ExecStream,
670        translator: Box<dyn StreamTranslator>,
671        /// `TranslateStreamWithClaudeInputTokens`' estimate for a Claude client.
672        claude: crate::gemini_stream::ClaudeInputTokens,
673        bridge: apply_patch_responses::State,
674        usage: DeferredUsage,
675        capture: cpa_core::exec::CaptureSink,
676        items: OutputItems,
677        ready: VecDeque<Result<Bytes, ExecError>>,
678        done: bool,
679        responses_client: bool,
680    }
681    fn bridge_error() -> ExecError {
682        ExecError::local(502, FailureScope::Request, cpa_translate::APPLY_PATCH_UPSTREAM_ERROR)
683    }
684    impl State {
685        /// Translates bridge output lines; false when a translator error ended the stream.
686        fn translate_all(&mut self, lines: Vec<Vec<u8>>) -> bool {
687            for line in lines {
688                // Bridge frames end in a blank line; Go hands the translator the whole
689                // chunk, which it trims, and a Responses route's framer ends the frame at
690                // that blank line.
691                let complete = line.ends_with(b"\n\n");
692                let line = line.strip_suffix(b"\n\n").unwrap_or(&line);
693                let line = if self.responses_client {
694                    ensure_responses_usage_details(line)
695                } else {
696                    line.to_vec()
697                };
698                match self.translator.event(&line) {
699                    Ok(mut frames) => {
700                        if !self.translator.tool_input_failed() {
701                            self.claude.apply(&mut frames);
702                        }
703                        if complete && self.responses_client {
704                            frames.extend(self.translator.flush_frames());
705                        }
706                        self.ready.extend(frames.into_iter().filter(|f| !f.is_empty()).map(Ok));
707                    }
708                    Err(error) => {
709                        self.done = true;
710                        self.ready
711                            .push_back(Err(ExecError::local(502, FailureScope::Request, error.0)));
712                        return false;
713                    }
714                }
715            }
716            true
717        }
718
719        /// emitTranslatedLine: the bridge, the translator, StopApplyPatchStream, then the
720        /// bridge's own failure.
721        fn emit(&mut self, line: Vec<u8>) {
722            let (lines, bridge_failed) = self.bridge.stream(&line);
723            if !self.translate_all(lines) {
724                return;
725            }
726            // StopApplyPatchStream: the rejected call's frames, then a 502. Go's Meta loop
727            // does not finalize tool input at EOF.
728            if self.translator.tool_input_failed() || bridge_failed.is_some() {
729                self.fail(bridge_error());
730            }
731        }
732
733        /// Ends the stream with `error`. Go stops translating; the Responses route first
734        /// flushes the frame it is still joining (other routes hold nothing back).
735        fn fail(&mut self, error: ExecError) {
736            self.done = true;
737            let flushed = self.translator.flush_frames();
738            self.ready.extend(flushed.into_iter().filter(|f| !f.is_empty()).map(Ok));
739            self.ready.push_back(Err(error));
740        }
741
742        fn line(&mut self, line: &[u8]) {
743            let Some(event) = data_payload(line) else {
744                return self.emit(line.to_vec());
745            };
746            if let Some(error) = stream_event_error(event) {
747                crate::kimi_http::capture_error(&self.capture, &error);
748                return self.fail(error);
749            }
750            let kind = gj::get(event, "type").bytes().into_owned();
751            let event = match kind.as_slice() {
752                b"response.output_item.done" => {
753                    self.items.collect(&lossy(event));
754                    event.to_vec()
755                }
756                b"response.completed" | b"response.incomplete" => self.items.patch(lossy(event)).into_bytes(),
757                _ => event.to_vec(),
758            };
759            let mut out = b"data: ".to_vec();
760            out.extend_from_slice(&event);
761            self.emit(out);
762        }
763
764        /// The end of the upstream lines: `FinishStream`, then the scan error or a clean end.
765        fn end(&mut self, scan_error: Option<ExecError>) {
766            self.done = true;
767            let (events, bridge_failed) = self.bridge.finish_stream();
768            if !self.translate_all(events) {
769                return;
770            }
771            if bridge_failed.is_some() {
772                return self.fail(bridge_error());
773            }
774            if let Some(error) = scan_error {
775                return self.fail(error);
776            }
777            match self.translator.finish() {
778                Ok(frames) => self.ready.extend(frames.into_iter().filter(|f| !f.is_empty()).map(Ok)),
779                Err(error) => {
780                    self.ready
781                        .push_back(Err(ExecError::local(502, FailureScope::Request, error.0)));
782                    return;
783                }
784            }
785            self.usage.commit();
786        }
787    }
788    futures_util::stream::unfold(
789        State {
790            upstream,
791            translator,
792            claude,
793            bridge,
794            usage,
795            capture,
796            items: OutputItems::default(),
797            ready: VecDeque::new(),
798            done: false,
799            responses_client,
800        },
801        |mut st| async move {
802            loop {
803                if let Some(item) = st.ready.pop_front() {
804                    return Some((item, st));
805                }
806                if st.done {
807                    return None;
808                }
809                match st.upstream.next().await {
810                    Some(Ok(line)) => st.line(&line),
811                    Some(Err(error)) => st.end(Some(error)),
812                    None => st.end(None),
813                }
814            }
815        },
816    )
817    .boxed()
818}
819
820/// The Refresh metadata change after a successful mint.
821pub(crate) fn refresh_patch(
822    credential: &Credential,
823    dca: &str,
824    minted: &MintedKey,
825    now_local: String,
826) -> MetadataPatch {
827    let mut base = creds(credential).0;
828    if !minted.base_url.trim().is_empty() {
829        base = minted.base_url.trim().to_owned();
830    }
831    let mut patch = MetadataPatch::default();
832    let mut set = |key: &str, value: Value| {
833        patch.set.insert(key.into(), value);
834    };
835    set("base_url", base.into());
836    set("api_key", minted.api_key.clone().into());
837    set("access_token", minted.api_key.clone().into());
838    set("dca_token", dca.into());
839    if !minted.user_email.is_empty() {
840        set("email", minted.user_email.clone().into());
841    }
842    if !minted.user_full_name.is_empty() {
843        set("name", minted.user_full_name.clone().into());
844    }
845    if !minted.subs_tier_name.is_empty() {
846        set("subs_tier_name", minted.subs_tier_name.clone().into());
847    }
848    if !minted.subs_tier_id.is_empty() {
849        set("subs_tier_id", minted.subs_tier_id.clone().into());
850    }
851    set("is_subs_active", minted.is_subs_active.into());
852    set("has_payment_method", minted.has_payment_method.into());
853    set("type", PROVIDER.into());
854    set("last_refresh", now_local.into());
855    patch.remove.push("expired".into());
856    if minted.subs_tier_name.is_empty() {
857        patch.remove.push("subs_tier_name".into());
858    }
859    if minted.subs_tier_id.is_empty() {
860        patch.remove.push("subs_tier_id".into());
861    }
862    patch
863}
864
865/// Go `helps.RefreshAuthViaHome` for a dispatched credential: Home mints (or refreshes)
866/// and returns the auth, whose metadata replaces this attempt's. Its `base_url`,
867/// `api_key` and `access_token` attributes come along when the metadata lacks them,
868/// so [`creds`] and `ensure_auth` see what Home's auth holds.
869async fn refresh_via_home(credential: &Credential) -> Result<MetadataPatch, ExecError> {
870    let client = cpa_home::client::current();
871    let refreshed = cpa_home::refresh::refresh(
872        client.as_ref(),
873        &cpa_core::config::credentials::auth_index(credential),
874        &cpa_core::config::credentials::access_token_sha256(credential),
875    )
876    .await
877    .map_err(|error| {
878        tracing::warn!("meta executor: {}", error.log);
879        let mut failure = ExecError::local(error.status, FailureScope::Credential, "");
880        failure.body = Bytes::from(error.body);
881        failure.direct = error.direct;
882        failure
883    })?;
884    let metadata = refreshed
885        .auth
886        .get("metadata")
887        .and_then(Value::as_object)
888        .cloned()
889        .unwrap_or_default();
890    let mut patch = MetadataPatch {
891        remove: credential
892            .metadata
893            .keys()
894            .filter(|k| !metadata.contains_key(*k))
895            .cloned()
896            .collect(),
897        set: metadata,
898    };
899    if let Some(attributes) = refreshed.auth.get("attributes").and_then(Value::as_object) {
900        for key in ["base_url", "api_key", "access_token"] {
901            if let Some(value) = attributes.get(key).filter(|v| v.is_string())
902                && !patch.set.contains_key(key)
903            {
904                patch.set.insert(key.into(), value.clone());
905            }
906        }
907    }
908    Ok(patch)
909}
910
911fn attribute<'a>(c: &'a Credential, key: &str) -> &'a str {
912    c.attributes.get(key).map(|v| v.trim()).unwrap_or_default()
913}
914
915fn metadata_str<'a>(c: &'a Credential, key: &str) -> Option<&'a str> {
916    c.str(key).map(str::trim).filter(|s| !s.is_empty())
917}
918
919/// `metaCreds`: base URL and usable (non-DCA) key, attributes before metadata.
920pub(crate) fn creds(c: &Credential) -> (String, String) {
921    let mut base = DEFAULT_API_BASE_URL.to_owned();
922    let mut token = String::new();
923    let usable = |t: &str| !t.is_empty() && !t.starts_with("dca:");
924    if !attribute(c, "base_url").is_empty() {
925        base = attribute(c, "base_url").to_owned();
926    }
927    if usable(attribute(c, "api_key")) {
928        token = attribute(c, "api_key").to_owned();
929    } else if usable(attribute(c, "access_token")) {
930        token = attribute(c, "access_token").to_owned();
931    }
932    if base == DEFAULT_API_BASE_URL
933        && let Some(b) = metadata_str(c, "base_url").or_else(|| metadata_str(c, "api_base_url"))
934    {
935        base = b.to_owned();
936    }
937    if token.is_empty() {
938        if let Some(k) = metadata_str(c, "api_key").filter(|k| usable(k)) {
939            token = k.to_owned();
940        } else if let Some(t) = metadata_str(c, "access_token").filter(|t| usable(t)) {
941            token = t.to_owned();
942        }
943    }
944    (base, token)
945}
946
947/// `extractDCAToken`: never for config API keys.
948pub(crate) fn dca_token(c: &Credential) -> Option<String> {
949    if is_config_api_key(c) {
950        return None;
951    }
952    if !attribute(c, "dca_token").is_empty() {
953        return Some(attribute(c, "dca_token").to_owned());
954    }
955    if attribute(c, "access_token").starts_with("dca:") {
956        return Some(attribute(c, "access_token").to_owned());
957    }
958    if let Some(d) = metadata_str(c, "dca_token") {
959        return Some(d.to_owned());
960    }
961    metadata_str(c, "access_token")
962        .filter(|t| t.starts_with("dca:"))
963        .map(str::to_owned)
964}
965
966/// `IsConfigAPIKeyAuth`: an API-key credential whose source is config.yaml.
967pub(crate) fn is_config_api_key(c: &Credential) -> bool {
968    let auth_kind = cpa_core::registry::dynamic::auth_kind(c);
969    if auth_kind != Some("apikey") {
970        return false;
971    }
972    if attribute(c, "runtime_only").go_eq_fold("true") {
973        return false;
974    }
975    match attribute(c, "source_backend").go_lower().as_str() {
976        "config" => return true,
977        "file" | "filesystem" | "git" | "memory" | "runtime" | "runtime_only" | "objectstore" | "object-store"
978        | "postgres" | "postgresql" | "database" | "db" => return false,
979        _ => {}
980    }
981    let source = attribute(c, "source");
982    if !source.is_empty() {
983        let lower = source.go_lower();
984        return lower.starts_with("config:") || lower == "config";
985    }
986    if !attribute(c, "path").is_empty() {
987        return false;
988    }
989    matches!(c.source, Source::Config { .. })
990}
991
992#[cfg(test)]
993#[path = "meta_tests.rs"]
994mod tests;