Skip to main content

cpa_exec/
kimi.rs

1//! Kimi executor (internal/runtime/executor/kimi_executor.go).
2//!
3//! Three wire paths, chosen by the client format:
4//! - Claude Messages: delegated to the Claude executor against Kimi's Anthropic-compatible
5//!   base, with model normalization, response-model restoration and native reasoning
6//!   replay (kimi_replay).
7//! - OpenAI Responses: native `/v1/responses`, body kept in Responses shape.
8//! - Everything else: translated to OpenAI Chat Completions at `/v1/chat/completions`.
9//!
10//! JSON reads and edits use cpa_common::json, thinking cpa_common::thinking, request
11//! translation cpa_translate, and clients and Go net/http wire behaviour the shared proxy
12//! module. Stages whose shared module has not landed go through adapters named after their
13//! owners: custom headers (kimi_http), payload rules and Codex-client rewrites (below).
14//! Translator apply_patch failures end streams with Go's 502. The native Responses path
15//! runs Go's apply_patch Responses bridge (cpa_translate::apply_patch_responses): a
16//! client's custom `apply_patch` tool goes upstream as a JSON function and comes back as
17//! the custom tool call.
18
19use std::collections::VecDeque;
20use std::sync::Arc;
21
22use bytes::Bytes;
23use cpa_common::gostr::GoStr;
24use cpa_common::json::{self as gj, Kind, Res};
25use cpa_common::thinking::{ModelCaps, RequestThinking, apply_request_thinking, parse_suffix};
26use cpa_core::config::Config;
27use cpa_core::credential::{Credential, MetadataPatch};
28use cpa_core::exec::{ExecError, ExecRequest, ExecResponse, ExecStream, FailureScope, Operation, ResponseBody};
29use cpa_core::format::Format;
30use cpa_translate::{RequestCtx, ResponseCtx, StreamTranslator, apply_patch_responses};
31use futures_util::StreamExt;
32
33use crate::claude::{ClaudeExecutor, Delegation};
34use crate::kimi_auth::{self, DeviceFlow};
35use crate::kimi_http::{
36    BUILD_VERSION, credential_headers, go_arch, go_os, hostname, payload_rules, refresh_due, rfc3339_local_now,
37    status_error,
38};
39use crate::kimi_http::{DeferredUsage, UsageRule, apply_patch_requested, defer_usage, report_model};
40use crate::kimi_replay::{self, ReplayCache};
41use crate::meta_codex::go_trim_space;
42use crate::openai_compat_payload::ensure_responses_usage_details;
43use crate::proxy::{GoClients, GoHeaders, Proxy, Upstream, default_client, lines, read_all, send_scanned};
44
45const REASONING_UNAVAILABLE: &str = "[reasoning unavailable]";
46
47/// Provider strings served by this executor.
48pub const PROVIDERS: [&str; 4] = ["kimi", "kimi-ai", "kimi.ai", "kimi.com"];
49
50/// Claude-format traffic goes through the shared [`ClaudeExecutor`] passed to
51/// [`KimiExecutor::execute`]; Go embeds one in the Kimi executor for the same purpose.
52pub struct KimiExecutor {
53    clients: GoClients,
54    oauth_host: Option<String>,
55    replay: Arc<ReplayCache>,
56}
57
58impl Default for KimiExecutor {
59    fn default() -> Self {
60        Self::with_client(default_client())
61    }
62}
63
64impl KimiExecutor {
65    /// Uses a caller-built client (tests point it at local mocks).
66    pub fn with_client(client: wreq::Client) -> Self {
67        Self {
68            clients: GoClients::with_default(client),
69            oauth_host: None,
70            replay: Arc::default(),
71        }
72    }
73
74    /// Sends token refreshes to a local mock instead of auth.kimi.com / auth.kimi.ai.
75    pub fn with_oauth_host(mut self, host: &str) -> Self {
76        self.oauth_host = Some(host.to_owned());
77        self
78    }
79
80    pub fn needs_prepare(&self, credential: &Credential, cfg: &Config) -> bool {
81        self.needs_prepare_at(credential, cfg, chrono::Utc::now())
82    }
83
84    pub fn needs_prepare_at(&self, credential: &Credential, _cfg: &Config, now: chrono::DateTime<chrono::Utc>) -> bool {
85        // Go's refresh loop never schedules API-key-kind credentials.
86        !cpa_core::registry::dynamic::is_api_key(credential)
87            && refresh_token(credential).is_some()
88            && refresh_due(
89                credential,
90                chrono::Duration::from_std(kimi_auth::REFRESH_LEAD).ok(),
91                now,
92            )
93    }
94
95    /// Refresh grant; Kimi has no other preparation.
96    pub async fn prepare(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
97        let Some(refresh) = refresh_token(credential) else {
98            return Ok(MetadataPatch::default());
99        };
100        let domain = kimi_auth::resolve_domain(credential);
101        let device_id = credential.str("device_id").unwrap_or_default();
102        let mut flow = DeviceFlow::new(self.clients.get(&Proxy::effective(credential, cfg)), domain, device_id);
103        if let Some(host) = &self.oauth_host {
104            flow = flow.with_oauth_host(host);
105        }
106        let tokens = flow.refresh(refresh).await?;
107        Ok(kimi_auth::refresh_patch(
108            credential,
109            &tokens,
110            &base_url(credential),
111            rfc3339_local_now(),
112        ))
113    }
114
115    pub async fn execute(
116        &self,
117        claude: &ClaudeExecutor,
118        credential: &Credential,
119        req: ExecRequest,
120        cfg: &Config,
121    ) -> Result<ExecResponse, ExecError> {
122        if req.operation == Operation::CountTokens || req.source_format == Format::Claude {
123            let mut req = req;
124            // Go CountTokens: Responses requests declare apply_patch as a function first.
125            if req.operation == Operation::CountTokens && req.source_format == Format::OpenAIResponse {
126                req.body = Bytes::from(
127                    apply_patch_responses::normalize_executor_request(&req.body, None).map_err(internal_error)?,
128                );
129            }
130            return self.execute_claude(claude, credential, req, cfg).await;
131        }
132        let client = self.clients.get(&Proxy::effective(credential, cfg));
133        if req.source_format == Format::OpenAIResponse {
134            return execute_responses(&client, credential, req, cfg).await;
135        }
136        execute_chat(&client, credential, req, cfg).await
137    }
138
139    async fn execute_claude(
140        &self,
141        claude: &ClaudeExecutor,
142        credential: &Credential,
143        mut req: ExecRequest,
144        cfg: &Config,
145    ) -> Result<ExecResponse, ExecError> {
146        let mut delegated = credential.clone();
147        delegated
148            .attributes
149            .insert("base_url".into(), claude_base_url(credential));
150        let client_model = req.model.clone();
151        // NewKimiExecutor's embedded ClaudeExecutor: normalized upstream model, count_tokens
152        // always upstream (KimiExecutor.CountTokens -> countTokensUpstream), and request
153        // logs naming kimi (requestLogProvider).
154        let delegation = Delegation {
155            upstream_model: Some(normalize_upstream_model),
156            count_upstream: true,
157            request_log_provider: Some("kimi"),
158        };
159        if req.operation == Operation::CountTokens {
160            return claude.execute_delegated(&delegated, req, cfg, delegation).await;
161        }
162        let scope = kimi_replay::prepare(&self.replay, &mut req).await;
163        let streaming = req.stream;
164        let mut response = match claude.execute_delegated(&delegated, req, cfg, delegation).await {
165            Ok(response) => response,
166            Err(error) => {
167                if scope.applied && kimi_replay::clears_after(&error) {
168                    scope.clear();
169                }
170                scope.writes.settle().await;
171                return Err(error);
172            }
173        };
174        response.body = match response.body {
175            ResponseBody::Buffered(body) => {
176                let body = restore_response_model(&body, &client_model);
177                if !streaming {
178                    scope.store_response(&body);
179                }
180                scope.writes.settle().await;
181                ResponseBody::Buffered(body)
182            }
183            ResponseBody::Stream(stream) => {
184                let restored = stream
185                    .map(move |event| event.map(|e| restore_response_model(&e, &client_model)))
186                    .boxed();
187                let writes = scope.writes.clone();
188                ResponseBody::Stream(writes.gate(crate::replay::wrap_stream(restored, scope)))
189            }
190        };
191        Ok(response)
192    }
193}
194
195fn refresh_token(credential: &Credential) -> Option<&str> {
196    credential.str("refresh_token").filter(|s| !s.trim().is_empty())
197}
198
199/// `ResolveKimiBaseURL`: attribute, metadata, then the domain default.
200pub(crate) fn base_url(credential: &Credential) -> String {
201    let attr = credential
202        .attributes
203        .get("base_url")
204        .map(|s| s.trim().trim_end_matches('/'))
205        .unwrap_or_default();
206    if !attr.is_empty() {
207        return attr.to_owned();
208    }
209    let meta = credential.str("base_url").map(str::trim).unwrap_or_default();
210    if !meta.is_empty() {
211        return meta.trim_end_matches('/').to_owned();
212    }
213    kimi_auth::api_base(kimi_auth::resolve_domain(credential)).to_owned()
214}
215
216fn chat_url(credential: &Credential) -> String {
217    let base = base_url(credential);
218    if base.ends_with("/v1") {
219        format!("{base}/chat/completions")
220    } else {
221        format!("{base}/v1/chat/completions")
222    }
223}
224
225fn responses_url(credential: &Credential) -> String {
226    let base = base_url(credential);
227    if base.ends_with("/v1") {
228        format!("{base}/responses")
229    } else {
230        format!("{base}/v1/responses")
231    }
232}
233
234fn claude_base_url(credential: &Credential) -> String {
235    let base = base_url(credential);
236    base.strip_suffix("/v1").unwrap_or(&base).to_owned()
237}
238
239/// `kimiCreds`: metadata access token, then attribute access token or API key.
240fn access_token(credential: &Credential) -> String {
241    if let Some(token) = credential.str("access_token").filter(|s| !s.trim().is_empty()) {
242        return token.to_owned();
243    }
244    ["access_token", "api_key"]
245        .iter()
246        .find_map(|k| credential.attributes.get(*k).filter(|s| !s.is_empty()))
247        .cloned()
248        .unwrap_or_default()
249}
250
251/// The executor's device identity: credential `device_id`, else kimi-cli's stored ID.
252fn device_id(credential: &Credential) -> String {
253    if let Some(id) = credential.str("device_id").map(str::trim).filter(|s| !s.is_empty()) {
254        return id.to_owned();
255    }
256    let Some(home) = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")) else {
257        return "cli-proxy-api-device".into();
258    };
259    let home = std::path::PathBuf::from(home);
260    let dir = match go_os() {
261        "darwin" => home.join("Library/Application Support/kimi"),
262        "windows" => std::env::var_os("APPDATA")
263            .map(std::path::PathBuf::from)
264            .unwrap_or_else(|| home.join("AppData/Roaming"))
265            .join("kimi"),
266        _ => home.join(".local/share/kimi"),
267    };
268    std::fs::read_to_string(dir.join("device_id"))
269        .map(|s| s.trim().to_owned())
270        .unwrap_or_else(|_| "cli-proxy-api-device".into())
271}
272
273/// `applyKimiHeadersWithAuth` plus credential custom headers.
274fn headers(credential: &Credential, req: &ExecRequest, stream: bool) -> GoHeaders {
275    let mut h = GoHeaders::new();
276    h.set("Content-Type", "application/json");
277    h.set("Authorization", format!("Bearer {}", access_token(credential)));
278    h.set("User-Agent", format!("CLIProxyAPI/{BUILD_VERSION}"));
279    h.set("X-Msh-Platform", "CLIProxyAPI");
280    h.set("X-Msh-Version", BUILD_VERSION);
281    h.set("X-Msh-Device-Name", hostname().unwrap_or_else(|| "unknown".into()));
282    h.set("X-Msh-Device-Model", format!("{} {}", go_os(), go_arch()));
283    h.set("X-Msh-Device-Id", device_id(credential));
284    h.set(
285        "Accept",
286        if stream {
287            "text/event-stream"
288        } else {
289            "application/json"
290        },
291    );
292    for (name, value) in credential_headers(credential, req, original(req)) {
293        h.set(&name, value);
294    }
295    h
296}
297
298fn request_error(message: impl Into<String>) -> ExecError {
299    ExecError::local(400, FailureScope::Request, message)
300}
301
302/// A plain Go error from the executor: the handler answers 500 with its text.
303fn internal_error(message: impl Into<String>) -> ExecError {
304    ExecError::local(500, FailureScope::Request, message)
305}
306
307fn not_registered(what: &str) -> ExecError {
308    ExecError::local(
309        501,
310        FailureScope::Request,
311        format!("{what} translation pair is not registered"),
312    )
313}
314
315/// `opts.OriginalRequest`, falling back to the request payload when empty.
316fn original(req: &ExecRequest) -> &Bytes {
317    if req.original_body.is_empty() {
318        &req.body
319    } else {
320        &req.original_body
321    }
322}
323
324/// `helps.ApplyRequestThinking` for Kimi. `to` is Go's target format name: `kimi` for
325/// Chat Completions (no translator targets it) and `codex` for native Responses.
326fn thinking(body: &[u8], req: &ExecRequest, to: &str) -> Result<Vec<u8>, ExecError> {
327    let has_request_transformer =
328        Format::parse(to).is_some_and(|t| cpa_translate::pair(req.source_format, t).is_some());
329    // Go `cliproxyauth.ResolvedModelInfo`: capabilities bound to this attempt.
330    let caps = req.resolved_model.as_ref().map(|r| ModelCaps::from(&r.info));
331    apply_request_thinking(&RequestThinking {
332        body,
333        payload: &req.body,
334        original: original(req),
335        model: &req.model,
336        from: req.source_format.as_str(),
337        to,
338        provider: "kimi",
339        resolved: caps.as_ref().map(Some),
340        has_request_transformer,
341        updates_changed: false,
342    })
343    .map_err(|e| ExecError::local(e.status(), FailureScope::Request, e.message))
344}
345
346/// Go's bufio.Scanner limits: 1 MiB for Chat Completions lines, 50 MiB for Responses.
347const CHAT_LINE_LIMIT: usize = 1_048_576;
348const RESPONSES_LINE_LIMIT: usize = 52_428_800;
349
350/// One upstream send (kimi_executor.go's four `RecordAPIRequest` sites): the capture of
351/// the request, its response metadata and a rejected response's body, then the send.
352async fn post(
353    client: &wreq::Client,
354    credential: &Credential,
355    req: &ExecRequest,
356    url: &str,
357    headers: GoHeaders,
358    body: Vec<u8>,
359) -> Result<Upstream, ExecError> {
360    use crate::kimi_http::{capture_chunk, capture_error, capture_metadata, capture_request};
361    let capture = req.capture();
362    let (auth_type, auth_value) = cpa_core::registry::dynamic::account_info(credential);
363    capture_request(
364        capture,
365        credential,
366        url,
367        &headers,
368        &body,
369        "kimi",
370        (auth_type, &auth_value),
371    );
372    // Go's TrackHTTPClient: the TTFT runs from the request to the first body byte.
373    req.usage.round_trip_started();
374    // Go reads every Kimi body with a scanner or io.ReadAll: one cut short fails with
375    // `unexpected EOF`.
376    let mut upstream = send_scanned(client, url, headers, Bytes::from(body), None)
377        .await
378        .inspect_err(|e| capture_error(capture, e))?;
379    capture_metadata(capture, upstream.status, &upstream.headers);
380    upstream.body = crate::kimi_http::track_first_byte(upstream.body, &req.usage, false);
381    if !(200..300).contains(&upstream.status) {
382        let error = status_error(upstream).await;
383        // ponytail: the error body is the MAX_ERROR_BODY prefix (proxy.rs ceiling); Go
384        // logs the whole body.
385        capture_chunk(capture, &error.body);
386        return Err(error);
387    }
388    Ok(upstream)
389}
390
391/// Go's non-stream `io.ReadAll` with its capture: a read error, else the whole body.
392async fn read_body(
393    body: futures_util::stream::BoxStream<'static, Result<Bytes, ExecError>>,
394    capture: &cpa_core::exec::CaptureSink,
395) -> Result<Bytes, ExecError> {
396    let data = read_all(body, usize::MAX, false)
397        .await
398        .inspect_err(|e| crate::kimi_http::capture_error(capture, e))?;
399    crate::kimi_http::capture_chunk(capture, &data);
400    Ok(data)
401}
402
403/// Chat Completions path (Execute / ExecuteStream for non-Claude, non-Responses clients).
404async fn execute_chat(
405    client: &wreq::Client,
406    credential: &Credential,
407    req: ExecRequest,
408    cfg: &Config,
409) -> Result<ExecResponse, ExecError> {
410    let base_model = parse_suffix(&req.model).model_name;
411    if cpa_translate::pair(req.source_format, Format::OpenAI).is_none() {
412        return Err(not_registered("Kimi request"));
413    }
414    let Some(response_pair) = cpa_translate::pair(req.response_format, Format::OpenAI) else {
415        return Err(not_registered("Kimi response"));
416    };
417    // Go: helps.TranslateRequestWithCodexMultiAgentV2 (no target executor, not compat).
418    let codex_client = crate::codex_client::Client::new(&req.headers, cfg, "", false);
419    let translate = |body: &[u8], stream: bool| {
420        crate::codex_client::translate_request(
421            req.source_format,
422            Format::OpenAI,
423            &RequestCtx {
424                model: &base_model,
425                stream,
426                tool_search: false,
427            },
428            body,
429            &codex_client,
430        )
431        .map(|t| t.body)
432        .map_err(|e| request_error(e.to_string()))
433    };
434    // Go translates the original request first (stream false) for payload-rule
435    // defaults, then the working one.
436    let original_translated = translate(original(&req), false);
437    let translated = translate(&req.body, req.stream)?;
438    let original_translated = crate::translate::baseline_or_working(original_translated, &translated);
439    let upstream_model = normalize_upstream_model(&base_model);
440    let mut body = gj::try_set_str(&translated, "model", &upstream_model)
441        .map_err(|e| internal_error(format!("kimi executor: failed to set model in payload: {e}")))?;
442    body = thinking(&body, &req, "kimi")?;
443    if req.stream {
444        body = gj::try_set_raw(&body, "stream_options.include_usage", "true")
445            .map_err(|e| internal_error(format!("kimi executor: failed to set stream_options in payload: {e}")))?;
446    }
447    body = normalize_tool_message_links(body)?;
448    body = normalize_tools(body);
449    body = normalize_temperature(body);
450    req.usage.upstream_model(&upstream_model);
451    req.usage.request_for("kimi", &body);
452    // The payload rules are the last change to the body (868e137).
453    body = payload_rules(cfg, &req, &base_model, "openai", body, &original_translated);
454    if req.stream {
455        // Go's stream publishes only through its usage buffer (no EnsurePublished).
456        req.usage.usage_required();
457    }
458    let upstream = post(
459        client,
460        credential,
461        &req,
462        &chat_url(credential),
463        headers(credential, &req, req.stream),
464        body.clone(),
465    )
466    .await?;
467    let translated = Bytes::from(body);
468    let ctx = ResponseCtx {
469        model: &req.model,
470        original_request: original(&req),
471        translated_request: &translated,
472        tool_search: false,
473    };
474    let body = if req.stream {
475        let (tapped, usage) = defer_usage(
476            crate::kimi_http::capture_lines(lines(upstream.body, CHAT_LINE_LIMIT), req.capture()),
477            &req.usage,
478            UsageRule::OpenAIStream,
479        );
480        ResponseBody::Stream(translate_lines(
481            tapped,
482            crate::translate::stream(response_pair, &ctx, &req),
483            StreamEnd::Chat,
484            usage,
485        ))
486    } else {
487        let data = read_body(upstream.body, req.capture()).await?;
488        // Go observes the response model now and publishes the usage once translated.
489        report_model(&req.usage, Format::OpenAI, &data);
490        // A translator error or empty output is Go's apply_patch 502.
491        let out = (response_pair.non_stream)(&ctx, &data)
492            .ok()
493            .filter(|out| !out.is_empty())
494            .ok_or_else(apply_patch_error)?;
495        req.usage.response_body(Format::OpenAI, &data);
496        ResponseBody::Buffered(Bytes::from(if req.response_format == Format::OpenAIResponse {
497            ensure_responses_usage_details(&out)
498        } else {
499            out
500        }))
501    };
502    Ok(ExecResponse {
503        status: upstream.status,
504        headers: upstream.headers,
505        body,
506    })
507}
508
509/// How the Go loop ends a translated stream.
510#[derive(Clone, Copy, PartialEq, Eq)]
511enum StreamEnd {
512    /// Chat Completions (executeStream): EndApplyPatchStream, then a synthetic `[DONE]`.
513    Chat,
514    /// Native Responses translated for another client (executeResponsesStream): neither.
515    Responses,
516}
517
518/// The 502 Go returns when a translator rejects upstream apply_patch input.
519fn apply_patch_error() -> ExecError {
520    ExecError::local(502, FailureScope::Request, cpa_translate::APPLY_PATCH_UPSTREAM_ERROR)
521}
522
523/// Go's per-line stream loop: each non-empty scanned line goes through the translator.
524/// A translator that rejects apply_patch input ends the stream after that line's frames
525/// with a 502 (StopApplyPatchStream). At the end Chat streams finalize tool input
526/// (EndApplyPatchStream), then end with a scan error (published with the usage seen so
527/// far) or translate a synthetic `[DONE]`. Before any terminal error the Responses route
528/// flushes the frame it is still joining.
529fn translate_lines(
530    upstream: ExecStream,
531    translator: Box<dyn StreamTranslator>,
532    end: StreamEnd,
533    usage: DeferredUsage,
534) -> ExecStream {
535    struct State {
536        upstream: ExecStream,
537        translator: Box<dyn StreamTranslator>,
538        ready: VecDeque<Result<Bytes, ExecError>>,
539        done: bool,
540        end: StreamEnd,
541        usage: DeferredUsage,
542        failed: bool,
543    }
544    impl State {
545        fn fail(&mut self, error: ExecError) {
546            self.done = true;
547            self.failed = true;
548            let flushed = self.translator.flush_frames();
549            self.ready.extend(flushed.into_iter().map(Ok));
550            self.ready.push_back(Err(error));
551        }
552
553        /// One translated line; false when the stream has ended. Bridge frames end in a
554        /// blank line; Go hands the translator the whole chunk, which it trims.
555        fn translate(&mut self, line: &[u8]) -> bool {
556            let line = line.strip_suffix(b"\n\n").unwrap_or(line);
557            match self.translator.event(line) {
558                Ok(events) => self.ready.extend(events.into_iter().map(Ok)),
559                Err(error) => {
560                    self.fail(translate_error(error));
561                    return false;
562                }
563            }
564            true
565        }
566
567        fn finish(&mut self, end: Option<Result<Bytes, ExecError>>) {
568            self.done = true;
569            if self.end == StreamEnd::Chat {
570                let finalized = self.translator.finalize_tool_input();
571                self.ready.extend(finalized.into_iter().map(Ok));
572                if self.translator.tool_input_failed() {
573                    return self.fail(apply_patch_error());
574                }
575                // 4f2e5270: a scan error ends the stream before the synthetic [DONE], and
576                // its failure keeps the usage seen so far.
577                if let Some(Err(error)) = end {
578                    self.usage.publish_failure(&error);
579                    return self.fail(error);
580                }
581                if !self.translate(b"[DONE]") {
582                    return;
583                }
584            }
585            match self.translator.finish() {
586                Ok(events) => self.ready.extend(events.into_iter().map(Ok)),
587                Err(error) => return self.fail(translate_error(error)),
588            }
589            if let Some(Err(error)) = end {
590                return self.fail(error);
591            }
592            if !self.failed {
593                self.usage.commit();
594            }
595        }
596    }
597    futures_util::stream::unfold(
598        State {
599            upstream,
600            translator,
601            ready: VecDeque::new(),
602            done: false,
603            end,
604            usage,
605            failed: false,
606        },
607        move |mut st| async move {
608            loop {
609                if let Some(item) = st.ready.pop_front() {
610                    return Some((item, st));
611                }
612                if st.done {
613                    return None;
614                }
615                match st.upstream.next().await {
616                    Some(Ok(line)) if line.is_empty() => {}
617                    Some(Ok(line)) => {
618                        if st.translate(&line) && st.translator.tool_input_failed() {
619                            st.fail(apply_patch_error());
620                        }
621                    }
622                    end => st.finish(end),
623                }
624            }
625        },
626    )
627    .boxed()
628}
629
630fn translate_error(error: cpa_translate::Error) -> ExecError {
631    ExecError::local(502, FailureScope::Request, error.to_string())
632}
633
634/// Native Responses streaming: Go writes every scanned line plus `\n` as one chunk and
635/// the Responses route joins chunks into frames (cpa_translate's ResponsesFramer),
636/// flushing what is pending at the end and before a terminal error.
637fn responses_frames(lines: ExecStream, usage: DeferredUsage) -> ExecStream {
638    struct State {
639        lines: ExecStream,
640        joiner: cpa_translate::stream::ResponsesFramer,
641        ready: VecDeque<Result<Bytes, ExecError>>,
642        done: bool,
643        usage: DeferredUsage,
644    }
645    futures_util::stream::unfold(
646        State {
647            lines,
648            joiner: Default::default(),
649            ready: VecDeque::new(),
650            done: false,
651            usage,
652        },
653        |mut st| async move {
654            loop {
655                if let Some(item) = st.ready.pop_front() {
656                    return Some((item, st));
657                }
658                if st.done {
659                    return None;
660                }
661                match st.lines.next().await {
662                    Some(Ok(line)) => {
663                        let mut chunk = line.to_vec();
664                        chunk.push(b'\n');
665                        let frames = st.joiner.write(&chunk);
666                        st.ready.extend(frames.into_iter().map(Ok));
667                    }
668                    end => {
669                        st.done = true;
670                        let frames = st.joiner.flush();
671                        st.ready.extend(frames.into_iter().map(Ok));
672                        match end {
673                            Some(Err(error)) => st.ready.push_back(Err(error)),
674                            _ => st.usage.commit(),
675                        }
676                    }
677                }
678            }
679        },
680    )
681    .boxed()
682}
683
684/// Go's native Responses loop around the apply_patch bridge: every scanned line goes
685/// through `State::stream`; at EOF `finish_stream` runs before a scan error is reported,
686/// and a bridge failure ends the stream with the apply_patch 502.
687fn bridge_lines(lines: ExecStream, bridge: apply_patch_responses::State) -> ExecStream {
688    futures_util::stream::unfold(
689        (lines, bridge, VecDeque::<Result<Bytes, ExecError>>::new(), false),
690        |(mut lines, mut bridge, mut ready, mut done)| async move {
691            loop {
692                if let Some(item) = ready.pop_front() {
693                    return Some((item, (lines, bridge, ready, done)));
694                }
695                if done {
696                    return None;
697                }
698                match lines.next().await {
699                    Some(Ok(line)) => {
700                        let (out, error) = bridge.stream(&line);
701                        ready.extend(out.into_iter().map(|l| Ok(Bytes::from(l))));
702                        if error.is_some() {
703                            done = true;
704                            ready.push_back(Err(apply_patch_error()));
705                        }
706                    }
707                    end => {
708                        done = true;
709                        let (out, error) = bridge.finish_stream();
710                        ready.extend(out.into_iter().map(|l| Ok(Bytes::from(l))));
711                        if error.is_some() {
712                            ready.push_back(Err(apply_patch_error()));
713                        } else if let Some(Err(error)) = end {
714                            ready.push_back(Err(error));
715                        }
716                    }
717                }
718            }
719        },
720    )
721    .boxed()
722}
723
724/// The usage Go's native Responses Execute publishes: Codex usage (`response.usage`)
725/// with tokens, else the top-level OpenAI usage with tokens, else none. Returned as the
726/// body the usage sink parses (top-level `usage`, Go's tier).
727fn native_usage_body(data: &[u8]) -> Option<Vec<u8>> {
728    use crate::kimi_http::usage_has_tokens;
729    if usage_has_tokens(&gj::get(data, "response.usage")) {
730        let mut inner = gj::get(data, "response").raw().to_vec();
731        let tier = crate::kimi_http::response_tier(data);
732        if tier.is_empty() {
733            gj::delete(&mut inner, "service_tier");
734        } else {
735            gj::set_str(&mut inner, "service_tier", tier);
736        }
737        return Some(inner);
738    }
739    usage_has_tokens(&gj::get(data, "usage")).then(|| data.to_vec())
740}
741
742/// `SetBoolIfDifferent`.
743fn set_bool_if_different(mut body: Vec<u8>, path: &str, value: bool) -> Vec<u8> {
744    let kind = gj::get(&body, path).kind;
745    if (value && kind == Kind::True) || (!value && kind == Kind::False) {
746        return body;
747    }
748    gj::set_bool(&mut body, path, value);
749    body
750}
751
752/// Native Responses path (executeResponses / executeResponsesStream).
753async fn execute_responses(
754    client: &wreq::Client,
755    credential: &Credential,
756    req: ExecRequest,
757    cfg: &Config,
758) -> Result<ExecResponse, ExecError> {
759    if req.alt.as_deref() == Some("responses/compact") {
760        return Err(if req.stream {
761            ExecError::local(
762                400,
763                FailureScope::Request,
764                "streaming not supported for /responses/compact",
765            )
766        } else {
767            ExecError::local(501, FailureScope::Request, "/responses/compact not supported")
768        });
769    }
770    let response_pair = cpa_translate::pair(req.response_format, Format::OpenAIResponse);
771    if response_pair.is_none() && req.response_format != Format::OpenAIResponse {
772        return Err(not_registered("Kimi Responses"));
773    }
774    let base_model = parse_suffix(&req.model).model_name;
775    let upstream_model = normalize_upstream_model(&base_model);
776    let mut body = gj::try_set_str(&req.body, "model", &upstream_model)
777        .map_err(|e| internal_error(format!("kimi executor: failed to set model in payload: {e}")))?;
778    body = set_bool_if_different(body, "stream", req.stream);
779    body = thinking(&body, &req, "codex")?;
780    body = apply_patch_responses::normalize_executor_request(&body, None).map_err(internal_error)?;
781    body = normalize_responses_input(body);
782    body = normalize_tools(body);
783    body = normalize_temperature(body);
784    req.usage.upstream_model(&upstream_model);
785    req.usage.request_for("kimi", &body);
786    // The payload rules are the last change (868e137). Go passes req.Payload as the
787    // original here, not OriginalRequest.
788    body = payload_rules(cfg, &req, &base_model, "openai-response", body, &req.body);
789    // Go publishes native Responses usage only when it has tokens (stream buffer, or the
790    // non-stream Codex/OpenAI usage check); there is no EnsurePublished.
791    req.usage.usage_required();
792    let upstream = post(
793        client,
794        credential,
795        &req,
796        &responses_url(credential),
797        headers(credential, &req, req.stream),
798        body.clone(),
799    )
800    .await?;
801    let translated = Bytes::from(body);
802    let ctx = ResponseCtx {
803        model: &req.model,
804        original_request: original(&req),
805        translated_request: &translated,
806        tool_search: false,
807    };
808    let original_request = original(&req);
809    let mut bridge = apply_patch_responses::State::new(req.source_format, original_request, original_request);
810    let out = if req.stream {
811        // Usage comes from the upstream lines; the bridge sits between them and the client.
812        let (tapped, usage) = defer_usage(
813            crate::kimi_http::capture_lines(lines(upstream.body, RESPONSES_LINE_LIMIT), req.capture()),
814            &req.usage,
815            UsageRule::KimiResponses,
816        );
817        let bridged = if bridge.active() {
818            bridge_lines(tapped, bridge)
819        } else {
820            tapped
821        };
822        match response_pair {
823            Some(pair) => ResponseBody::Stream(translate_lines(
824                bridged,
825                crate::translate::stream(pair, &ctx, &req),
826                StreamEnd::Responses,
827                usage,
828            )),
829            // Native Responses clients get every line back, joined into frames.
830            None => ResponseBody::Stream(responses_frames(bridged, usage)),
831        }
832    } else {
833        let data = read_body(upstream.body, req.capture()).await?;
834        report_model(&req.usage, Format::Codex, &data);
835        let out = match bridge.bridge.transform_non_stream(&data) {
836            Ok(out) if !(out.is_empty() && apply_patch_requested(original_request)) => out,
837            _ => return Err(apply_patch_error()),
838        };
839        let out = match response_pair {
840            Some(pair) => (pair.non_stream)(&ctx, &out)
841                .ok()
842                .filter(|out| !out.is_empty())
843                .ok_or_else(apply_patch_error)?,
844            None => out,
845        };
846        if let Some(usage) = native_usage_body(&data) {
847            req.usage.response_body(Format::Codex, &usage);
848        }
849        ResponseBody::Buffered(Bytes::from(out))
850    };
851    Ok(ExecResponse {
852        status: upstream.status,
853        headers: upstream.headers,
854        body: out,
855    })
856}
857
858/// `stripKimiPrefix`.
859fn strip_kimi_prefix(model: &str) -> &str {
860    let model = model.trim();
861    if model.go_lower().starts_with("kimi-") {
862        &model[5..]
863    } else {
864        model
865    }
866}
867
868/// `normalizeKimiUpstreamModel`: strip `kimi-` and `[1m]`, map K2.7/K2.8 Code aliases,
869/// keep a thinking suffix.
870pub(crate) fn normalize_upstream_model(model: &str) -> String {
871    let parsed = parse_suffix(model.trim());
872    let mut base = parsed.model_name.trim().go_lower();
873    if let Some(stripped) = base.strip_suffix("[1m]") {
874        base = stripped.to_owned();
875    }
876    let normalized = match base.as_str() {
877        "kimi-k2.8" | "k2.8" | "kimi-k2.8-code" | "k2.8-code" | "kimi-k2.8-preview" | "k2.8-preview"
878        | "kimi-k2.7-code" | "k2.7-code" | "kimi-for-coding" | "for-coding" => "kimi-for-coding".to_owned(),
879        "kimi-k2.7-code-highspeed" | "k2.7-code-highspeed" | "kimi-for-coding-highspeed" | "for-coding-highspeed" => {
880            "kimi-for-coding-highspeed".to_owned()
881        }
882        _ => strip_kimi_prefix(&base).to_owned(),
883    };
884    if parsed.has_suffix {
885        format!("{normalized}({})", parsed.raw_suffix)
886    } else {
887        normalized
888    }
889}
890
891fn usable_reasoning(reasoning: &str) -> bool {
892    let trimmed = reasoning.trim();
893    !trimmed.is_empty() && trimmed != REASONING_UNAVAILABLE
894}
895
896/// `strings.TrimSpace(r.String())`, as bytes.
897fn trimmed(r: &Res<'_>) -> Vec<u8> {
898    go_trim_space(&r.bytes()).to_vec()
899}
900
901fn is_empty_object(r: &Res<'_>) -> bool {
902    go_trim_space(r.raw()) == b"{}"
903}
904
905fn content_part_empty(part: &Res<'_>) -> bool {
906    match part.kind {
907        Kind::Null => true,
908        Kind::String => trimmed(part).is_empty(),
909        Kind::Json if part.is_object() => {
910            let text = part.get("text");
911            if text.exists() {
912                return trimmed(&text).is_empty();
913            }
914            trimmed(&part.get("type")) == b"text" || is_empty_object(part)
915        }
916        _ => false,
917    }
918}
919
920fn should_drop_assistant(msg: &Res<'_>) -> bool {
921    if trimmed(&msg.get("role")) != b"assistant" {
922        return false;
923    }
924    let tool_calls = msg.get("tool_calls");
925    let has_tool_calls = tool_calls.is_array() && !tool_calls.array().is_empty();
926    let call = msg.get("function_call");
927    let has_function_call = call.exists() && call.kind != Kind::Null && !(call.is_object() && is_empty_object(&call));
928    let has_reasoning = {
929        let r = msg.get("reasoning_content");
930        r.exists() && !trimmed(&r).is_empty()
931    };
932    if has_tool_calls || has_function_call || has_reasoning {
933        return false;
934    }
935    let content = msg.get("content");
936    match content.kind {
937        _ if !content.exists() => true,
938        Kind::Null => true,
939        Kind::String => trimmed(&content).is_empty(),
940        Kind::Json if content.is_array() => content.array().iter().all(content_part_empty),
941        _ => false,
942    }
943}
944
945fn fallback_reasoning(msg: &Res<'_>, latest: Option<&[u8]>) -> Vec<u8> {
946    if let Some(latest) = latest.filter(|l| usable_reasoning(&String::from_utf8_lossy(l))) {
947        return latest.to_vec();
948    }
949    let content = msg.get("content");
950    if content.kind == Kind::String && !trimmed(&content).is_empty() {
951        return trimmed(&content);
952    }
953    if content.is_array() {
954        let parts: Vec<Vec<u8>> = content
955            .array()
956            .iter()
957            .map(|item| trimmed(&item.get("text")))
958            .filter(|t| !t.is_empty())
959            .collect();
960        if !parts.is_empty() {
961            return parts.join(&b'\n');
962        }
963    }
964    REASONING_UNAVAILABLE.as_bytes().to_vec()
965}
966
967/// `normalizeKimiToolMessageLinks`: drop empty assistant turns, repair tool_call_id links
968/// and give tool-calling assistant turns reasoning_content.
969fn normalize_tool_message_links(body: Vec<u8>) -> Result<Vec<u8>, ExecError> {
970    if body.is_empty() || !gj::valid(&body) {
971        return Ok(body);
972    }
973    let messages = gj::get(&body, "messages");
974    if !messages.is_array() {
975        return Ok(body);
976    }
977    let msgs = messages.array();
978    let mut dropped = vec![false; msgs.len()];
979    let mut patches: Vec<(usize, &str, Vec<u8>, &str)> = Vec::new();
980    let mut pending: Vec<Vec<u8>> = Vec::new();
981    let mut latest: Option<Vec<u8>> = None;
982    for (index, msg) in msgs.iter().enumerate() {
983        if should_drop_assistant(msg) {
984            dropped[index] = true;
985            continue;
986        }
987        match trimmed(&msg.get("role")).as_slice() {
988            b"assistant" => {
989                let reasoning = msg.get("reasoning_content");
990                let usable = reasoning.exists() && usable_reasoning(&reasoning.str());
991                if usable {
992                    latest = Some(reasoning.bytes().into_owned());
993                }
994                let calls = msg.get("tool_calls");
995                if calls.is_array() && !calls.array().is_empty() {
996                    if !usable {
997                        patches.push((
998                            index,
999                            "reasoning_content",
1000                            fallback_reasoning(msg, latest.as_deref()),
1001                            "failed to set assistant reasoning_content",
1002                        ));
1003                    }
1004                    for call in calls.array() {
1005                        let id = trimmed(&call.get("id"));
1006                        if !id.is_empty() {
1007                            pending.push(id);
1008                        }
1009                    }
1010                }
1011            }
1012            b"tool" => {
1013                let mut id = trimmed(&msg.get("tool_call_id"));
1014                if id.is_empty() {
1015                    id = trimmed(&msg.get("call_id"));
1016                    if !id.is_empty() {
1017                        patches.push((
1018                            index,
1019                            "tool_call_id",
1020                            id.clone(),
1021                            "failed to set tool_call_id from call_id",
1022                        ));
1023                    }
1024                }
1025                if id.is_empty() && pending.len() == 1 {
1026                    id = pending[0].clone();
1027                    patches.push((index, "tool_call_id", id.clone(), "failed to infer tool_call_id"));
1028                }
1029                if let Some(pos) = pending.iter().position(|p| *p == id).filter(|_| !id.is_empty()) {
1030                    pending.remove(pos);
1031                }
1032            }
1033            _ => {}
1034        }
1035    }
1036    let any_dropped = dropped.iter().any(|d| *d);
1037    if !any_dropped && patches.is_empty() {
1038        return Ok(body);
1039    }
1040    if !any_dropped && patches.len() == 1 {
1041        let (index, path, value, context) = &patches[0];
1042        return gj::try_set_str(&body, &format!("messages.{index}.{path}"), value)
1043            .map_err(|e| internal_error(format!("kimi executor: {context}: {e}")));
1044    }
1045    let mut items = Vec::with_capacity(msgs.len());
1046    let mut next = patches.iter().peekable();
1047    for (index, msg) in msgs.iter().enumerate() {
1048        if dropped[index] {
1049            continue;
1050        }
1051        let mut raw = msg.raw().to_vec();
1052        while let Some((_, path, value, context)) = next.next_if(|p| p.0 == index) {
1053            raw = gj::try_set_str(&raw, path, value)
1054                .map_err(|e| internal_error(format!("kimi executor: {context}: {e}")))?;
1055        }
1056        items.push(raw);
1057    }
1058    gj::try_set_raw(&body, "messages", gj::join(&items)).map_err(|e| {
1059        let context = if any_dropped {
1060            "failed to drop empty assistant messages"
1061        } else {
1062            patches[0].3
1063        };
1064        internal_error(format!("kimi executor: {context}: {e}"))
1065    })
1066}
1067
1068/// `normalizeKimiTools`: inline local `$ref`s and default the root schema type.
1069fn normalize_tools(body: Vec<u8>) -> Vec<u8> {
1070    if body.is_empty() {
1071        return body;
1072    }
1073    let body = normalize_tool_list(body, "tools", true);
1074    normalize_tool_list(body, "functions", false)
1075}
1076
1077fn normalize_tool_list(body: Vec<u8>, key: &str, is_tools: bool) -> Vec<u8> {
1078    let items = gj::get(&body, key);
1079    if !items.is_array() {
1080        return body;
1081    }
1082    let items = items.array();
1083    if items.is_empty() {
1084        return body;
1085    }
1086    let mut changed = false;
1087    let mut updated = Vec::with_capacity(items.len());
1088    for item in &items {
1089        let mut raw = item.raw().to_vec();
1090        let path = if is_tools && item.get("function.parameters").exists() {
1091            Some("function.parameters")
1092        } else if item.get("parameters").exists() {
1093            Some("parameters")
1094        } else {
1095            None
1096        };
1097        if let Some(path) = path {
1098            let params = item.get(path);
1099            if params.is_object() {
1100                let normalized = normalize_parameters_schema(params.raw());
1101                if normalized != params.raw()
1102                    && let Ok(next) = gj::try_set_raw(&raw, path, &normalized)
1103                {
1104                    raw = next;
1105                    changed = true;
1106                }
1107            }
1108        }
1109        updated.push(raw);
1110    }
1111    if !changed {
1112        return body;
1113    }
1114    gj::try_set_raw(&body, key, gj::join(&updated)).unwrap_or(body)
1115}
1116
1117fn normalize_parameters_schema(raw: &[u8]) -> Vec<u8> {
1118    if go_trim_space(raw).is_empty() {
1119        return raw.to_vec();
1120    }
1121    let mut params = cpa_common::gemini_schema::inline_local_refs(raw);
1122    for container in ["$defs", "definitions"] {
1123        if gj::get(&params, container).exists() {
1124            gj::delete(&mut params, container);
1125        }
1126    }
1127    if !gj::get(&params, "type").exists() {
1128        gj::set_str(&mut params, "type", "object");
1129    }
1130    params
1131}
1132
1133/// `normalizeKimiTemperature`: Kimi accepts only 0.6 without thinking and 1.0 with it.
1134fn normalize_temperature(mut body: Vec<u8>) -> Vec<u8> {
1135    let temperature = gj::get(&body, "temperature");
1136    if !temperature.exists() {
1137        return body;
1138    }
1139    let disabled = gj::get(&body, "thinking.type").str().go_eq_fold("disabled");
1140    let allowed = if disabled { 0.6 } else { 1.0 };
1141    if temperature.float() != allowed {
1142        gj::delete(&mut body, "temperature");
1143    }
1144    body
1145}
1146
1147fn responses_call_id(item: &Res<'_>) -> Vec<u8> {
1148    for key in ["call_id", "tool_call_id", "callId"] {
1149        let id = trimmed(&item.get(key));
1150        if !id.is_empty() {
1151            return id;
1152        }
1153    }
1154    let id = trimmed(&item.get("id"));
1155    if id.starts_with(b"fco_") { Vec::new() } else { id }
1156}
1157
1158fn is_tool_call(item: &Res<'_>) -> bool {
1159    matches!(
1160        trimmed(&item.get("type")).as_slice(),
1161        b"function_call" | b"custom_tool_call"
1162    )
1163}
1164
1165fn is_tool_output(item: &Res<'_>) -> bool {
1166    matches!(
1167        trimmed(&item.get("type")).as_slice(),
1168        b"function_call_output" | b"custom_tool_call_output"
1169    )
1170}
1171
1172/// `NormalizeKimiResponsesInput`: tool outputs must follow their parallel calls directly;
1173/// items in between move after the outputs.
1174fn normalize_responses_input(body: Vec<u8>) -> Vec<u8> {
1175    if body.is_empty() || !gj::valid(&body) {
1176        return body;
1177    }
1178    let input = gj::get(&body, "input");
1179    if !input.is_array() {
1180        return body;
1181    }
1182    let items = input.array();
1183    if items.is_empty() {
1184        return body;
1185    }
1186    let mut reordered = false;
1187    let mut result: Vec<&[u8]> = Vec::with_capacity(items.len());
1188    let mut i = 0;
1189    while i < items.len() {
1190        if !is_tool_call(&items[i]) {
1191            result.push(items[i].raw());
1192            i += 1;
1193            continue;
1194        }
1195        let start = i;
1196        let mut end = i;
1197        let mut ids: std::collections::HashMap<Vec<u8>, usize> = std::collections::HashMap::new();
1198        let mut count = 0;
1199        while end < items.len() && is_tool_call(&items[end]) {
1200            let id = responses_call_id(&items[end]);
1201            if !id.is_empty() {
1202                *ids.entry(id).or_default() += 1;
1203                count += 1;
1204            }
1205            end += 1;
1206        }
1207        result.extend(items[start..end].iter().map(|v| v.raw()));
1208        if count == 0 {
1209            i = end;
1210            continue;
1211        }
1212        let mut needed = ids.clone();
1213        let mut remaining = count;
1214        let mut last = None;
1215        for (j, item) in items.iter().enumerate().skip(end) {
1216            if remaining == 0 || is_tool_call(item) {
1217                break;
1218            }
1219            if is_tool_output(item) {
1220                let id = responses_call_id(item);
1221                if let Some(n) = needed.get_mut(&id).filter(|n| **n > 0) {
1222                    *n -= 1;
1223                    remaining -= 1;
1224                    last = Some(j);
1225                }
1226            }
1227        }
1228        match last.filter(|_| remaining == 0) {
1229            Some(last) => {
1230                let mut outputs = Vec::new();
1231                let mut between = Vec::new();
1232                let mut consumed = ids;
1233                for item in &items[end..=last] {
1234                    if is_tool_output(item) {
1235                        let id = responses_call_id(item);
1236                        if let Some(n) = consumed.get_mut(&id).filter(|n| **n > 0) {
1237                            *n -= 1;
1238                            outputs.push(item.raw());
1239                            continue;
1240                        }
1241                    }
1242                    between.push(item.raw());
1243                }
1244                reordered |= !between.is_empty();
1245                result.extend(outputs);
1246                result.extend(between);
1247                i = last + 1;
1248            }
1249            None => i = end,
1250        }
1251    }
1252    if !reordered {
1253        return body;
1254    }
1255    let joined = gj::join(&result);
1256    gj::try_set_raw(&body, "input", joined).unwrap_or(body)
1257}
1258
1259/// `restoreClaudeResponseModel`: put the client's model back on a Claude response body or
1260/// on the `data:` line of an SSE event.
1261fn restore_response_model(payload: &[u8], model: &str) -> Bytes {
1262    if model.trim().is_empty() {
1263        return Bytes::copy_from_slice(payload);
1264    }
1265    if let Some(updated) = set_model(payload, model) {
1266        return Bytes::from(updated);
1267    }
1268    let mut changed = false;
1269    let lines: Vec<Vec<u8>> = payload
1270        .split(|b| *b == b'\n')
1271        .map(|line| {
1272            let (content, cr) = match line.strip_suffix(b"\r") {
1273                Some(content) => (content, &b"\r"[..]),
1274                None => (line, &b""[..]),
1275            };
1276            if let Some(json) = content.strip_prefix(b"data:")
1277                && let Some(updated) = set_model(go_trim_space(json), model)
1278            {
1279                changed = true;
1280                return [&b"data: "[..], &updated, cr].concat();
1281            }
1282            line.to_vec()
1283        })
1284        .collect();
1285    if changed {
1286        Bytes::from(lines.join(&b'\n'))
1287    } else {
1288        Bytes::copy_from_slice(payload)
1289    }
1290}
1291
1292fn set_model(json: &[u8], model: &str) -> Option<Vec<u8>> {
1293    if !gj::valid(json) {
1294        return None;
1295    }
1296    let mut out = json.to_vec();
1297    let mut changed = false;
1298    for path in ["model", "message.model"] {
1299        if gj::get(&out, path).exists() && gj::set_str(&mut out, path, model) {
1300            changed = true;
1301        }
1302    }
1303    changed.then_some(out)
1304}
1305
1306#[cfg(test)]
1307#[path = "kimi_tests.rs"]
1308mod tests;