Skip to main content

cpa_exec/
codex_images.rs

1//! Codex Images API (codex_openai_images.go): `/v1/images/generations` and
2//! `/v1/images/edits` on Codex credentials.
3//!
4//! A gpt-image-* model goes straight to the backend's own `/images/*` endpoint
5//! ("direct"). Any other model becomes a Responses request that forces the
6//! `image_generation` tool on the images main model (`multimedia.gpt-image-2-base-model`
7//! when it names a `gpt-` model, else gpt-5.4-mini). The tool calls of
8//! `response.completed` become an Images API response, or `<prefix>.partial_image` and
9//! `<prefix>.completed` SSE frames when streaming.
10
11use cpa_core::gostr::GoStr;
12use std::collections::VecDeque;
13use std::time::{SystemTime, UNIX_EPOCH};
14
15use base64::Engine;
16use bytes::Bytes;
17use cpa_core::config::Config;
18use cpa_core::credential::Credential;
19use cpa_core::exec::{ExecError, ExecRequest, ExecResponse, ExecStream, ResponseBody};
20use cpa_core::format::Format;
21use futures_util::StreamExt;
22use gjson::Kind;
23
24use crate::codex::{CodexExecutor, events, explicit_session, report_request};
25use crate::codex_capture::Wire;
26use crate::codex_json::{delete, set_bool_if_different, set_raw, set_str, set_str_if_different};
27use crate::codex_request::{self as request, Settings, View};
28use crate::codex_response::{self as response, OutputItems};
29use crate::openai_compat::{plain_err, prepare_images_payload, status_err};
30use crate::openai_compat_multipart::{self as multipart, Form, MediaType, Part};
31
32const GENERATIONS: &str = "/v1/images/generations";
33const EDITS: &str = "/v1/images/edits";
34/// `codexOpenAIImagesMainModel`.
35const MAIN_MODEL: &str = "gpt-5.4-mini";
36/// `codexDefaultImageToolModel`.
37const DEFAULT_TOOL_MODEL: &str = "gpt-image-2";
38/// Models the backend serves on its own Images endpoints (`codexIsDirectOpenAIImageModel`).
39const DIRECT_MODELS: [&str; 5] = [
40    "gpt-image-1.5",
41    "gpt-image-2",
42    "gpt-image-2.5-flare",
43    "gpt-image-2.5-sunburst",
44    "gpt-image-2.5",
45];
46/// The Images API's source format name in payload rules and thinking.
47const SOURCE: &str = "openai-image";
48
49/// `codexOpenAIImagePreparedRequest`.
50struct Prepared {
51    body: String,
52    url_format: bool,
53    /// `image_generation` or `image_edit`: the SSE event name prefix.
54    prefix: &'static str,
55}
56
57/// One completed `image_generation_call` (`codexImageCallResult`).
58#[derive(Default, Clone)]
59struct ImageCall {
60    result: String,
61    revised_prompt: String,
62    output_format: String,
63    size: String,
64    background: String,
65    quality: String,
66}
67
68impl CodexExecutor {
69    /// The Images API on a Codex credential (`executeOpenAIImage`,
70    /// `executeOpenAIImageStream`). `request_path` is the inbound route; `req.stream`
71    /// asks for SSE.
72    pub async fn images(
73        &self,
74        credential: &Credential,
75        req: ExecRequest,
76        request_path: &str,
77        cfg: &Config,
78    ) -> Result<ExecResponse, ExecError> {
79        crate::codex_siwc::require_token(credential)?;
80        let view = View::for_request(credential, cfg).with_session(explicit_session(&req));
81        let settings = Settings::scoped(cfg, &view);
82        let path = request_path.trim();
83        // A Sign in with ChatGPT credential has no direct Images endpoint.
84        match (direct_model(&req, &settings), direct_endpoint(path)) {
85            (Some(model), Some(endpoint)) if !view.siwc => {
86                self.direct(&view, &settings, req, path, endpoint, &model).await
87            }
88            _ => self.tool(&view, &settings, req, path).await,
89        }
90    }
91
92    /// `executeDirectOpenAIImage` / `executeDirectOpenAIImageStream`: the request (JSON, or
93    /// a multipart edit as JSON) goes to the backend's Images endpoint; the response
94    /// passes through.
95    async fn direct(
96        &self,
97        view: &View<'_>,
98        settings: &Settings,
99        req: ExecRequest,
100        path: &str,
101        endpoint: &str,
102        model: &str,
103    ) -> Result<ExecResponse, ExecError> {
104        let content_type = request::header(&req.headers, "content-type").to_owned();
105        let (body, content_type) = if path.ends_with(EDITS) {
106            direct_edit_payload(&req.body, model, &content_type, req.stream)?
107        } else {
108            prepare_images_payload(&req.body, model, &content_type, req.stream)?
109        };
110        if req.usage.enabled() {
111            req.usage.request(Format::OpenAI, &body);
112        }
113        let cache = request::session_uuid(&req, None);
114        let body = json_body(body, cache.as_deref());
115        // The payload rules run last (868e137), on the body's JSON view.
116        let original = if req.original_body.is_empty() {
117            &req.body
118        } else {
119            &req.original_body
120        };
121        let requested = if req.requested_model.trim().is_empty() {
122            req.model.trim()
123        } else {
124            req.requested_model.trim()
125        };
126        let client_content_type = request::header(&req.headers, "content-type");
127        let (body, content_type) =
128            crate::payload_media::apply(&body, &content_type, original, client_content_type, |view, original| {
129                let rules = cpa_common::payload::Request {
130                    target_executor: "codex",
131                    model,
132                    requested_model: requested,
133                    protocol: "openai",
134                    from_protocol: SOURCE,
135                    root: "",
136                    original,
137                    request_path: path,
138                    headers: Some(&req.headers),
139                };
140                cpa_common::payload::apply(&settings.payload, &rules, view)
141            })
142            .map_err(plain_err)?;
143        let body = Bytes::from(body);
144        // `applyCodexDirectImageHeaders`: the client's User-Agent is not forwarded.
145        let mut client = req.headers.clone();
146        client.remove(http::header::USER_AGENT);
147        let mut headers = request::image_headers(view, settings, &client, model, cache.as_deref(), req.stream);
148        if !content_type.is_empty()
149            && let Ok(value) = http::HeaderValue::from_str(&content_type)
150        {
151            headers.insert(http::header::CONTENT_TYPE, value);
152        }
153        let url = format!("{}{endpoint}", view.base_url);
154        let wire = Wire::new(req.capture(), view.credential);
155        wire.request(&url, &headers, &body);
156        let upstream = match self.post(view, &url, &headers, body).await {
157            Ok(upstream) => upstream,
158            Err(error) => {
159                wire.exec_error(&error);
160                return Err(error);
161            }
162        };
163        wire.metadata(upstream.status, &upstream.headers);
164        self.quota().observe(&view.credential.id, model, &upstream.headers);
165        let status = upstream.status;
166        let mut headers = upstream.headers;
167        headers.remove(http::header::CONTENT_ENCODING);
168        headers.remove(http::header::CONTENT_LENGTH);
169        if !(200..300).contains(&status) {
170            // Both direct modes record and return a read error of the error body.
171            let body = match crate::proxy::read_all(upstream.body, crate::proxy::MAX_ERROR_BODY, false).await {
172                Ok(body) => body,
173                Err(error) => {
174                    wire.exec_error(&error);
175                    return Err(error);
176                }
177            };
178            wire.chunk(&body);
179            return Err(response::status_error(
180                status,
181                &body,
182                headers,
183                settings.model_level_cooling,
184            ));
185        }
186        if !req.stream {
187            let data = match crate::proxy::read_all(upstream.body, usize::MAX, false).await {
188                Ok(data) => data,
189                Err(error) => {
190                    wire.exec_error(&error);
191                    return Err(error);
192                }
193            };
194            wire.chunk(&data);
195            if req.usage.enabled() {
196                req.usage.response_body(Format::OpenAI, &data);
197            }
198            return Ok(ExecResponse {
199                status,
200                headers,
201                body: ResponseBody::Buffered(data),
202            });
203        }
204        let usage = req.usage.clone();
205        let stream = upstream.body.inspect(move |chunk| {
206            // Go records each body read as it is.
207            match chunk {
208                Ok(chunk) => wire.chunk(chunk),
209                Err(error) => wire.exec_error(error),
210            }
211            if let Ok(chunk) = chunk
212                && usage.enabled()
213            {
214                // Go splits each read on newlines (`ObserveOpenAIStream`).
215                for line in chunk.split(|b| *b == b'\n') {
216                    let line = line.trim_ascii();
217                    let data = line.strip_prefix(b"data:").map_or(line, <[u8]>::trim_ascii);
218                    if !data.is_empty() {
219                        usage.response_line(Format::OpenAI, data);
220                    }
221                }
222            }
223        });
224        Ok(ExecResponse {
225            status,
226            headers,
227            body: ResponseBody::Stream(stream.boxed()),
228        })
229    }
230
231    /// The Responses path: a forced `image_generation` tool call on the main model.
232    async fn tool(
233        &self,
234        view: &View<'_>,
235        settings: &Settings,
236        req: ExecRequest,
237        path: &str,
238    ) -> Result<ExecResponse, ExecError> {
239        let prepared = prepare(&req, path)?;
240        let main_model = main_model(&settings.image_base_model);
241        let body = tool_body(&prepared.body, main_model, view.siwc)?;
242        let cache = request::session_uuid(&req, None);
243        let body = match &cache {
244            Some(id) => set_str_if_different(body, "prompt_cache_key", id),
245            None => body,
246        };
247        let body = request::sanitize_input_ids(body);
248        // The payload rules run last (868e137), as the codex executor, on the
249        // prepared image request.
250        let requested = if req.requested_model.trim().is_empty() {
251            req.model.trim()
252        } else {
253            req.requested_model.trim()
254        };
255        let rules = cpa_common::payload::Request {
256            target_executor: "codex",
257            model: main_model,
258            requested_model: requested,
259            protocol: "codex",
260            from_protocol: SOURCE,
261            root: "",
262            original: prepared.body.as_bytes(),
263            request_path: path,
264            headers: Some(&req.headers),
265        };
266        let body = cpa_common::payload::apply(&settings.payload, &rules, body.into_bytes());
267        let body = String::from_utf8(body).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned());
268        report_request(&req, Format::Codex, &body);
269        let headers = request::image_headers(view, settings, &req.headers, main_model, cache.as_deref(), true);
270        let url = format!("{}/responses", view.base_url);
271        let wire = Wire::new(req.capture(), view.credential);
272        let res = self
273            .send(
274                view,
275                settings,
276                url,
277                headers,
278                body,
279                &Default::default(),
280                &wire,
281                crate::codex::ErrorBody::Strict,
282            )
283            .await?;
284        let upstream = events(res.body);
285        // ponytail: usage lands on the images main model's record; Go also publishes the
286        // completion's `tool_usage.image_gen` under the tool model, which `UsageSink`
287        // cannot express yet (see `Processor::reporting`).
288        if req.stream {
289            return Ok(ExecResponse {
290                status: res.status,
291                headers: res.headers,
292                body: ResponseBody::Stream(frames(upstream, prepared, req.usage.clone(), wire)),
293            });
294        }
295        // Go reads the whole body (`io.ReadAll`) and records it before parsing.
296        let mut upstream = upstream;
297        let mut read = Vec::new();
298        while let Some(event) = upstream.next().await {
299            match event {
300                Ok(event) => read.push(event),
301                Err(error) => {
302                    wire.exec_error(&error);
303                    return Err(error);
304                }
305            }
306        }
307        if wire.enabled() {
308            wire.chunk(&read.concat());
309        }
310        let mut items = OutputItems::default();
311        for event in read {
312            for data in data_lines(&event) {
313                if req.usage.enabled() {
314                    req.usage.response_line(Format::Codex, data.as_bytes());
315                }
316                match gjson::get(data, "type").str() {
317                    "response.output_item.done" => items.collect(data),
318                    "response.completed" => {
319                        let (calls, created, usage) = extract(data, &items);
320                        if calls.is_empty() {
321                            return Err(status_err(502, "upstream did not return image output"));
322                        }
323                        return Ok(ExecResponse {
324                            status: res.status,
325                            headers: res.headers,
326                            body: ResponseBody::Buffered(Bytes::from(images_response(
327                                &calls,
328                                created,
329                                usage.as_deref(),
330                                prepared.url_format,
331                            ))),
332                        });
333                    }
334                    _ => {}
335                }
336            }
337        }
338        Err(status_err(504, "stream error: stream disconnected before completion"))
339    }
340}
341
342/// The stream path's frames (`executeOpenAIImageStream`): partial images as they come,
343/// then one completed frame per image. A stream that ends without
344/// `response.completed` ends quietly, as in Go.
345fn frames(upstream: ExecStream, prepared: Prepared, usage: cpa_core::exec::UsageSink, wire: Wire) -> ExecStream {
346    struct State {
347        upstream: ExecStream,
348        prepared: Prepared,
349        usage: cpa_core::exec::UsageSink,
350        wire: Wire,
351        items: OutputItems,
352        out: VecDeque<Result<Bytes, ExecError>>,
353        done: bool,
354    }
355    let state = State {
356        upstream,
357        prepared,
358        usage,
359        wire,
360        items: OutputItems::default(),
361        out: VecDeque::new(),
362        done: false,
363    };
364    futures_util::stream::unfold(state, |mut st| async move {
365        loop {
366            if let Some(item) = st.out.pop_front() {
367                return Some((item, st));
368            }
369            if st.done {
370                return None;
371            }
372            let event = match st.upstream.next().await {
373                None => return None,
374                Some(Err(error)) => {
375                    st.done = true;
376                    st.wire.exec_error(&error);
377                    st.out.push_back(Err(error));
378                    continue;
379                }
380                Some(Ok(event)) => event,
381            };
382            for raw in event.split(|b| *b == b'\n') {
383                let raw = raw.strip_suffix(b"\r").unwrap_or(raw);
384                // Go records each scanned line before reading it, and none after the end.
385                st.wire.chunk(raw);
386                let Some(data) = std::str::from_utf8(raw)
387                    .ok()
388                    .and_then(|line| line.strip_prefix("data:"))
389                    .map(str::trim)
390                else {
391                    continue;
392                };
393                if st.usage.enabled() {
394                    st.usage.response_line(Format::Codex, data.as_bytes());
395                }
396                match gjson::get(data, "type").str() {
397                    "response.output_item.done" => st.items.collect(data),
398                    "response.image_generation_call.partial_image" => {
399                        if let Some(frame) = partial_frame(data, &st.prepared) {
400                            st.out.push_back(Ok(Bytes::from(frame)));
401                        }
402                    }
403                    "response.completed" => {
404                        st.done = true;
405                        let (calls, _, usage) = extract(data, &st.items);
406                        if calls.is_empty() {
407                            st.out
408                                .push_back(Err(status_err(502, "upstream did not return image output")));
409                        }
410                        for call in &calls {
411                            let frame = completed_frame(call, usage.as_deref(), &st.prepared);
412                            st.out.push_back(Ok(Bytes::from(frame)));
413                        }
414                        break;
415                    }
416                    _ => {}
417                }
418            }
419        }
420    })
421    .boxed()
422}
423
424/// The `data:` payloads of one SSE event, trimmed (Go reads `data:` lines).
425fn data_lines(event: &[u8]) -> Vec<&str> {
426    std::str::from_utf8(event)
427        .unwrap_or_default()
428        .split('\n')
429        .filter_map(|line| line.strip_prefix("data:"))
430        .map(str::trim)
431        .collect()
432}
433
434/// `codexDirectOpenAIImageRouteModel`: the client's model (multipart handlers pass it
435/// beside the raw form), the JSON payload's model, then the execution model, when one
436/// is a gpt-image-* model.
437fn direct_route_model(req: &ExecRequest) -> Option<String> {
438    let payload_model = if cpa_common::json::std_valid(&req.body) {
439        gjson::get(&String::from_utf8_lossy(&req.body), "model")
440            .str()
441            .to_owned()
442    } else {
443        String::new()
444    };
445    [req.requested_model.trim(), payload_model.as_str(), req.model.as_str()]
446        .into_iter()
447        .map(image_base_model)
448        .find(|m| DIRECT_MODELS.contains(&m.as_str()))
449}
450
451/// `directOpenAIImageModel`: for a direct route, the resolved upstream model. A name
452/// the credential's config lists is sent as it is; otherwise a gpt-image-* model loses
453/// its routing prefix and case (51bde0ff).
454fn direct_model(req: &ExecRequest, settings: &Settings) -> Option<String> {
455    let route = direct_route_model(req)?;
456    let model = request::base_model(&req.model).trim().to_owned();
457    if model.is_empty() {
458        return Some(route);
459    }
460    let configured = settings.key_models.iter().any(|m| {
461        let field = |k: &str| m.get(k).and_then(serde_json::Value::as_str).unwrap_or_default().trim();
462        let name = Some(field("name"))
463            .filter(|n| !n.is_empty())
464            .unwrap_or_else(|| field("alias"));
465        request::base_model(name).trim().go_eq_fold(&model)
466    });
467    if configured {
468        return Some(model);
469    }
470    let base = image_base_model(&model);
471    Some(if DIRECT_MODELS.contains(&base.as_str()) {
472        base
473    } else {
474        model
475    })
476}
477
478/// `codexOpenAIImageBaseModel`: no thinking suffix, no `provider/` prefix, lower case.
479fn image_base_model(model: &str) -> String {
480    let base = request::base_model(model);
481    let mut base = base.trim();
482    if let Some(i) = base.rfind('/')
483        && i < base.len() - 1
484    {
485        base = base[i + 1..].trim();
486    }
487    base.trim().go_lower()
488}
489
490/// `codexDirectOpenAIImageEndpoint`.
491fn direct_endpoint(path: &str) -> Option<&'static str> {
492    if path.ends_with(GENERATIONS) {
493        Some("/images/generations")
494    } else if path.ends_with(EDITS) {
495        Some("/images/edits")
496    } else {
497        None
498    }
499}
500
501/// `resolveGPTImage2BaseModel`.
502fn main_model(configured: &str) -> &str {
503    if !configured.is_empty() && configured.go_lower().starts_with("gpt-") {
504        configured
505    } else {
506        MAIN_MODEL
507    }
508}
509
510/// `cacheHelper` on a direct body: the prompt cache key and input-id sanitising apply to
511/// JSON bodies only.
512fn json_body(body: Bytes, cache: Option<&str>) -> Bytes {
513    if !cpa_common::json::std_valid(&body) {
514        return body;
515    }
516    let text = String::from_utf8_lossy(&body).into_owned();
517    let text = match cache {
518        Some(id) => set_str_if_different(text, "prompt_cache_key", id),
519        None => text,
520    };
521    Bytes::from(request::sanitize_input_ids(text))
522}
523
524/// `codexPrepareDirectOpenAIImageEditPayload`: JSON as for generations; a multipart edit
525/// is rewritten to JSON with data URLs.
526fn direct_edit_payload(
527    payload: &[u8],
528    model: &str,
529    content_type: &str,
530    stream: bool,
531) -> Result<(Bytes, String), ExecError> {
532    if cpa_common::json::std_valid(payload) {
533        return prepare_images_payload(payload, model, content_type, stream);
534    }
535    let unsupported = || plain_err(format!("unsupported OpenAI image edit Content-Type {content_type:?}"));
536    let MediaType::Ok(media, params) = multipart::parse_media_type(content_type.trim()) else {
537        return Err(unsupported());
538    };
539    if !media.trim().starts_with("multipart/") {
540        return Err(unsupported());
541    }
542    let boundary = params.get("boundary").map(|b| b.trim()).unwrap_or_default();
543    if boundary.is_empty() {
544        return Err(plain_err("multipart boundary is missing"));
545    }
546    let form =
547        multipart::read_form(payload, boundary).map_err(|e| plain_err(format!("read multipart form failed: {e}")))?;
548    Ok((
549        Bytes::from(edit_form_to_json(&form, model, stream)),
550        "application/json".into(),
551    ))
552}
553
554/// `codexRewriteOpenAIImageEditMultipartToJSON`.
555// ponytail: Go ranges over its form map, so field order varies per run; this keeps the
556// order fields arrived in, one of Go's orders.
557fn edit_form_to_json(form: &Form, model: &str, stream: bool) -> String {
558    let mut out = set_str("{}", "model", model);
559    if stream {
560        out = set_raw(&out, "stream", "true");
561    }
562    for (key, values) in &form.values {
563        let key = key.trim();
564        if key.is_empty() || key == "model" || key == "stream" || values.is_empty() {
565            continue;
566        }
567        let path = match key {
568            "mask[file_id]" => "mask.file_id",
569            "mask[image_url]" => "mask.image_url",
570            other => other,
571        };
572        let raw = if let [value] = values.as_slice() {
573            form_json_value(path, value)
574        } else {
575            let items: Vec<String> = values.iter().map(|v| form_json_value(key, v)).collect();
576            format!("[{}]", items.join(","))
577        };
578        out = set_raw(&out, path, &raw);
579    }
580    if let Some(mask) = form.files("mask").first() {
581        out = set_str(&out, "mask.image_url", &data_url(mask));
582    }
583    let files = image_files(form);
584    let existing = gjson::get(&out, "images");
585    if !existing.exists() || existing.kind() == Kind::Array {
586        let mut items: Vec<String> = existing.array().iter().map(|v| v.json().to_owned()).collect();
587        for file in files {
588            items.push(set_str(r#"{"image_url":""}"#, "image_url", &data_url(file)));
589        }
590        if !files.is_empty() {
591            out = set_raw(&out, "images", &format!("[{}]", items.join(",")));
592        }
593    } else {
594        for file in files {
595            out = set_str(&out, "images.-1.image_url", &data_url(file));
596        }
597    }
598    out
599}
600
601/// `codexOpenAIImageEditFormJSONValue`: integers for the count fields, strings otherwise.
602fn form_json_value(key: &str, value: &[u8]) -> String {
603    let text = String::from_utf8_lossy(value);
604    let text = text.trim();
605    if matches!(
606        key.trim().go_lower().as_str(),
607        "n" | "output_compression" | "partial_images"
608    ) && let Ok(n) = go_parse_int(text)
609    {
610        return n.to_string();
611    }
612    String::from_utf8_lossy(&cpa_common::json::quote(text)).into_owned()
613}
614
615/// `strconv.ParseInt(s, 10, 64)`.
616fn go_parse_int(s: &str) -> Result<i64, std::num::ParseIntError> {
617    s.parse::<i64>()
618}
619
620/// `codexMultipartImageFiles`: `image[]` files, else `image` files.
621fn image_files(form: &Form) -> &[Part] {
622    match form.files("image[]") {
623        [] => form.files("image"),
624        files => files,
625    }
626}
627
628/// `codexMultipartFileToDataURL`: the part's Content-Type, else the sniffed type.
629fn data_url(part: &Part) -> String {
630    // ponytail: a Content-Type that is not UTF-8 reaches the data URL lossily (image
631    // clients send ASCII media types); building the URL as bytes would keep it.
632    let header = String::from_utf8_lossy(part.header("Content-Type").unwrap_or_default());
633    let media = Some(header.trim())
634        .filter(|m| !m.is_empty())
635        .unwrap_or_else(|| detect_content_type(&part.body));
636    format!(
637        "data:{media};base64,{}",
638        base64::engine::general_purpose::STANDARD.encode(&part.body)
639    )
640}
641
642/// `http.DetectContentType` for uploaded images.
643// ponytail: Go's image, BOM and text-versus-binary rules only; its HTML, XML, PDF, audio,
644// video, archive and font signatures are not ported (image uploads do not carry them).
645fn detect_content_type(data: &[u8]) -> &'static str {
646    let data = &data[..data.len().min(512)];
647    const EXACT: [(&[u8], &str); 8] = [
648        (b"\xFE\xFF", "text/plain; charset=utf-16be"),
649        (b"\xFF\xFE", "text/plain; charset=utf-16le"),
650        (b"\xEF\xBB\xBF", "text/plain; charset=utf-8"),
651        (b"GIF87a", "image/gif"),
652        (b"GIF89a", "image/gif"),
653        (b"\x89PNG\x0D\x0A\x1A\x0A", "image/png"),
654        (b"\xFF\xD8\xFF", "image/jpeg"),
655        (b"BM", "image/bmp"),
656    ];
657    for (signature, media) in EXACT {
658        if data.starts_with(signature) {
659            return media;
660        }
661    }
662    if data.len() >= 14 && &data[..4] == b"RIFF" && &data[8..14] == b"WEBPVP" {
663        return "image/webp";
664    }
665    if data.starts_with(b"\x00\x00\x01\x00") {
666        return "image/x-icon";
667    }
668    let binary = |b: &u8| matches!(*b, 0x00..=0x08 | 0x0B | 0x0E..=0x1A | 0x1C..=0x1F);
669    if data.iter().any(binary) {
670        "application/octet-stream"
671    } else {
672        "text/plain; charset=utf-8"
673    }
674}
675
676/// `codexPrepareOpenAIImageRequest`.
677fn prepare(req: &ExecRequest, path: &str) -> Result<Prepared, ExecError> {
678    if path.ends_with(GENERATIONS) {
679        return generation_json(&req.body, &req.model);
680    }
681    if !path.ends_with(EDITS) {
682        return Err(plain_err(format!("unsupported OpenAI image endpoint path {path:?}")));
683    }
684    let content_type = request::header(&req.headers, "content-type").trim();
685    let media = match multipart::parse_media_type(content_type) {
686        MediaType::Ok(media, _) | MediaType::BadParams(media) => media,
687        MediaType::Invalid(_) => String::new(),
688    };
689    if media.starts_with("multipart/") {
690        return edit_multipart(&req.body, &req.model, content_type);
691    }
692    edit_json(&req.body, &req.model)
693}
694
695fn json_text(raw: &[u8]) -> Option<String> {
696    cpa_common::json::std_valid(raw).then(|| String::from_utf8_lossy(raw).into_owned())
697}
698
699/// `codexPrepareOpenAIImageGenerationJSON`.
700fn generation_json(raw: &[u8], route_model: &str) -> Result<Prepared, ExecError> {
701    let raw = json_text(raw).ok_or_else(|| plain_err("invalid OpenAI image generation request JSON"))?;
702    let prompt = gjson::get(&raw, "prompt").str().trim().to_owned();
703    let tool = tool_json(
704        &raw,
705        route_model,
706        "generate",
707        &["size", "quality", "background", "output_format", "moderation"],
708    );
709    Ok(Prepared {
710        body: responses_request(&prompt, &[], &tool),
711        url_format: url_format(gjson::get(&raw, "response_format").str()),
712        prefix: "image_generation",
713    })
714}
715
716/// `codexPrepareOpenAIImageEditJSON`.
717fn edit_json(raw: &[u8], route_model: &str) -> Result<Prepared, ExecError> {
718    let raw = json_text(raw).ok_or_else(|| plain_err("invalid OpenAI image edit request JSON"))?;
719    let prompt = gjson::get(&raw, "prompt").str().trim().to_owned();
720    let images_value = gjson::get(&raw, "images");
721    let images: Vec<String> = if images_value.kind() == Kind::Array {
722        images_value
723            .array()
724            .iter()
725            .map(|img| img.get("image_url").str().trim().to_owned())
726            .filter(|url| !url.is_empty())
727            .collect()
728    } else {
729        Vec::new()
730    };
731    let mut tool = tool_json(
732        &raw,
733        route_model,
734        "edit",
735        &[
736            "size",
737            "quality",
738            "background",
739            "output_format",
740            "input_fidelity",
741            "moderation",
742        ],
743    );
744    let mask = gjson::get(&raw, "mask.image_url").str().trim().to_owned();
745    if !mask.is_empty() {
746        tool = set_str(&tool, "input_image_mask.image_url", &mask);
747    }
748    Ok(Prepared {
749        body: responses_request(&prompt, &images, &tool),
750        url_format: url_format(gjson::get(&raw, "response_format").str()),
751        prefix: "image_edit",
752    })
753}
754
755/// `codexPrepareOpenAIImageEditMultipart`.
756fn edit_multipart(body: &[u8], route_model: &str, content_type: &str) -> Result<Prepared, ExecError> {
757    let params = match multipart::parse_media_type(content_type) {
758        MediaType::Ok(_, params) => params,
759        _ => {
760            return Err(plain_err(
761                "parse multipart content type failed: mime: invalid media parameter",
762            ));
763        }
764    };
765    let boundary = params.get("boundary").map(|b| b.trim()).unwrap_or_default();
766    if boundary.is_empty() {
767        return Err(plain_err("multipart boundary is required"));
768    }
769    let form =
770        multipart::read_form(body, boundary).map_err(|e| plain_err(format!("parse multipart form failed: {e}")))?;
771    let value = |key: &str| form.value(key).trim().to_owned();
772    let mut tool = r#"{"type":"image_generation","action":"edit"}"#.to_owned();
773    tool = set_str(&tool, "model", &tool_model(&value("model"), route_model));
774    for field in [
775        "size",
776        "quality",
777        "background",
778        "output_format",
779        "input_fidelity",
780        "moderation",
781    ] {
782        let v = value(field);
783        if !v.is_empty() {
784            tool = set_str(&tool, field, &v);
785        }
786    }
787    for field in ["output_compression", "partial_images"] {
788        if let Ok(n) = go_parse_int(&value(field)) {
789            tool = set_raw(&tool, field, &n.to_string());
790        }
791    }
792    let images: Vec<String> = image_files(&form).iter().map(data_url).collect();
793    if let Some(mask) = form.files("mask").first() {
794        tool = set_str(&tool, "input_image_mask.image_url", &data_url(mask));
795    }
796    Ok(Prepared {
797        body: responses_request(&value("prompt"), &images, &tool),
798        url_format: url_format(&value("response_format")),
799        prefix: "image_edit",
800    })
801}
802
803/// `codexNormalizeImageResponseFormat`: `url`, else `b64_json`.
804fn url_format(format: &str) -> bool {
805    format.trim().eq_ignore_ascii_case("url")
806}
807
808/// `codexOpenAIImageToolModel`.
809fn tool_model(request_model: &str, route_model: &str) -> String {
810    [request_model.trim(), route_model.trim()]
811        .into_iter()
812        .find(|m| !m.is_empty())
813        .unwrap_or(DEFAULT_TOOL_MODEL)
814        .to_owned()
815}
816
817/// `codexBuildOpenAIImageTool`: string fields as Go's `String()` reads them, the count
818/// fields only when numeric (truncated to integers).
819fn tool_json(raw: &str, route_model: &str, action: &str, string_fields: &[&str]) -> String {
820    let mut tool = set_str(r#"{"type":"image_generation","action":""}"#, "action", action);
821    tool = set_str(&tool, "model", &tool_model(gjson::get(raw, "model").str(), route_model));
822    for field in string_fields {
823        let value = gjson::get(raw, field);
824        let value = value.str().trim();
825        if !value.is_empty() {
826            tool = set_str(&tool, field, value);
827        }
828    }
829    for field in ["output_compression", "partial_images"] {
830        let value = gjson::get(raw, field);
831        if value.kind() == Kind::Number {
832            tool = set_raw(&tool, field, &value.i64().to_string());
833        }
834    }
835    tool
836}
837
838/// `codexBuildImagesResponsesRequest`.
839fn responses_request(prompt: &str, images: &[String], tool: &str) -> String {
840    let mut req = r#"{"instructions":"","stream":true,"reasoning":{"effort":"medium","summary":"auto"},"parallel_tool_calls":true,"include":["reasoning.encrypted_content"],"model":"","store":false,"tool_choice":{"type":"image_generation"},"tools":[]}"#.to_owned();
841    req = set_str(&req, "model", MAIN_MODEL);
842    if !tool.is_empty() && gjson::valid(tool) {
843        req = set_raw(&req, "tools", &format!("[{tool}]"));
844    }
845    let mut content = vec![set_str(r#"{"type":"input_text","text":""}"#, "text", prompt)];
846    for image in images.iter().filter(|i| !i.trim().is_empty()) {
847        content.push(set_str(r#"{"type":"input_image","image_url":""}"#, "image_url", image));
848    }
849    let input = format!(
850        r#"[{{"type":"message","role":"user","content":[{}]}}]"#,
851        content.join(",")
852    );
853    set_raw(&req, "input", &input)
854}
855
856/// `prepareCodexOpenAIImageBody`: thinking, then the Codex request rules.
857fn tool_body(body: &str, main_model: &str, siwc: bool) -> Result<String, ExecError> {
858    let thought = cpa_common::thinking::apply_thinking_with_source_payload(
859        body.as_bytes(),
860        body.as_bytes(),
861        body.as_bytes(),
862        main_model,
863        SOURCE,
864        "codex",
865        "codex",
866        false,
867    )
868    .map_err(|e| ExecError::local(e.status(), cpa_core::exec::FailureScope::Request, e.message))?;
869    let mut out = String::from_utf8_lossy(&thought).into_owned();
870    out = set_str_if_different(out, "model", main_model);
871    out = set_bool_if_different(out, "stream", true);
872    for key in [
873        "previous_response_id",
874        "prompt_cache_retention",
875        "safety_identifier",
876        "stream_options",
877    ] {
878        out = delete(&out, key);
879    }
880    if siwc {
881        return Ok(request::siwc_body(out, request::Call::Stream, false, false));
882    }
883    let mut bytes = out.into_bytes();
884    cpa_common::codex_client::normalize_codex_instructions(&mut bytes);
885    Ok(String::from_utf8_lossy(&bytes).into_owned())
886}
887
888/// `codexExtractImageResults`: image calls from the completed output, else from the
889/// collected `output_item.done` items in index order; the creation time (now when
890/// absent); and `tool_usage.image_gen`.
891fn extract(completed: &str, items: &OutputItems) -> (Vec<ImageCall>, i64, Option<String>) {
892    let mut created = gjson::get(completed, "response.created_at").i64();
893    if created <= 0 {
894        created = SystemTime::now()
895            .duration_since(UNIX_EPOCH)
896            .map_or(0, |d| d.as_secs() as i64);
897    }
898    let patched = items.patch(completed.to_owned());
899    let calls = gjson::get(&patched, "response.output")
900        .array()
901        .iter()
902        .filter(|item| item.get("type").str() == "image_generation_call")
903        .filter_map(|item| {
904            let field = |k: &str| item.get(k).str().trim().to_owned();
905            let result = field("result");
906            (!result.is_empty()).then(|| ImageCall {
907                result,
908                revised_prompt: field("revised_prompt"),
909                output_format: field("output_format"),
910                size: field("size"),
911                background: field("background"),
912                quality: field("quality"),
913            })
914        })
915        .collect();
916    let usage = gjson::get(completed, "response.tool_usage.image_gen");
917    let usage = (usage.kind() == Kind::Object).then(|| usage.json().to_owned());
918    (calls, created, usage)
919}
920
921/// `codexBuildImagesAPIResponse`.
922fn images_response(calls: &[ImageCall], created: i64, usage: Option<&str>, url: bool) -> String {
923    let mut out = set_raw(r#"{"created":0,"data":[]}"#, "created", &created.to_string());
924    let first = calls.first().cloned().unwrap_or_default();
925    for (key, value) in [
926        ("background", &first.background),
927        ("output_format", &first.output_format),
928        ("quality", &first.quality),
929        ("size", &first.size),
930    ] {
931        if !value.is_empty() {
932            out = set_str(&out, key, value);
933        }
934    }
935    if let Some(usage) = usage.filter(|u| gjson::valid(u)) {
936        out = set_raw(&out, "usage", usage);
937    }
938    let items: Vec<String> = calls
939        .iter()
940        .map(|call| {
941            let mut item = "{}".to_owned();
942            if !call.revised_prompt.is_empty() {
943                item = set_str(&item, "revised_prompt", &call.revised_prompt);
944            }
945            image_field(item, call, url)
946        })
947        .collect();
948    set_raw(&out, "data", &format!("[{}]", items.join(",")))
949}
950
951/// `url` as a data URL, else `b64_json`.
952fn image_field(json: String, call: &ImageCall, url: bool) -> String {
953    if url {
954        set_str(
955            &json,
956            "url",
957            &format!("data:{};base64,{}", mime_type(&call.output_format), call.result),
958        )
959    } else {
960        set_str(&json, "b64_json", &call.result)
961    }
962}
963
964/// `codexBuildImagePartialFrame`.
965fn partial_frame(payload: &str, prepared: &Prepared) -> Option<String> {
966    let b64 = gjson::get(payload, "partial_image_b64").str().trim().to_owned();
967    if b64.is_empty() {
968        return None;
969    }
970    let call = ImageCall {
971        result: b64,
972        output_format: gjson::get(payload, "output_format").str().trim().to_owned(),
973        ..Default::default()
974    };
975    let event = format!("{}.partial_image", prepared.prefix);
976    let mut data = set_str(r#"{"type":"","partial_image_index":0}"#, "type", &event);
977    data = set_raw(
978        &data,
979        "partial_image_index",
980        &gjson::get(payload, "partial_image_index").i64().to_string(),
981    );
982    Some(sse_frame(&event, &image_field(data, &call, prepared.url_format)))
983}
984
985/// `codexBuildImageCompletedFrame`.
986fn completed_frame(call: &ImageCall, usage: Option<&str>, prepared: &Prepared) -> String {
987    let event = format!("{}.completed", prepared.prefix);
988    let mut data = set_str(r#"{"type":""}"#, "type", &event);
989    if let Some(usage) = usage.filter(|u| gjson::valid(u)) {
990        data = set_raw(&data, "usage", usage);
991    }
992    sse_frame(&event, &image_field(data, call, prepared.url_format))
993}
994
995fn sse_frame(event: &str, data: &str) -> String {
996    format!("event: {event}\ndata: {data}\n\n")
997}
998
999/// `codexMimeTypeFromOutputFormat`.
1000fn mime_type(output_format: &str) -> &'static str {
1001    match output_format.trim().go_lower().as_str() {
1002        "jpg" | "jpeg" => "image/jpeg",
1003        "webp" => "image/webp",
1004        _ => "image/png",
1005    }
1006}
1007
1008#[cfg(test)]
1009mod tests {
1010    use super::*;
1011
1012    fn request(requested: &str, resolved: &str, body: &'static [u8]) -> ExecRequest {
1013        ExecRequest {
1014            operation: cpa_core::exec::Operation::Generate,
1015            source_format: Format::OpenAI,
1016            response_format: Format::OpenAI,
1017            stream_payloads: false,
1018            requested_model: requested.into(),
1019            model: resolved.into(),
1020            original_body: Bytes::from_static(body),
1021            body: Bytes::from_static(body),
1022            stream: false,
1023            alt: None,
1024            session: None,
1025            headers: Default::default(),
1026            execution_session: None,
1027            derived_session: None,
1028            resolved_model: None,
1029            usage: Default::default(),
1030            request_path: String::new(),
1031            caller: cpa_core::exec::Caller {
1032                principal: String::new(),
1033                source: "",
1034                provider: String::new(),
1035            },
1036        }
1037    }
1038
1039    /// Go `TestCodexImagePayloadPreservesResolvedAlias` and the model column of
1040    /// `TestCodexImageResolvedModelOnWire` (its payload-rule rows need the media payload
1041    /// finalizer, 868e137): a configured upstream name is sent as it is, and only an
1042    /// unconfigured gpt-image-* route loses its prefix and case.
1043    #[test]
1044    fn direct_images_keep_the_resolved_upstream_model() {
1045        let plain = Settings::default();
1046        let json: &[u8] = br#"{"model":"gpt-image-2","prompt":"diagnostic"}"#;
1047        let form: &[u8] = b"--b\r\nContent-Disposition: form-data; name=\"prompt\"\r\n\r\nx\r\n--b--\r\n";
1048        assert_eq!(
1049            direct_model(&request("", "openai/gpt-image-2-cpr", json), &plain).as_deref(),
1050            Some("openai/gpt-image-2-cpr")
1051        );
1052        for (requested, resolved, configured, want) in [
1053            (
1054                "gpt-image-2",
1055                "openai/gpt-image-2-cpr",
1056                "openai/gpt-image-2-cpr",
1057                "openai/gpt-image-2-cpr",
1058            ),
1059            (
1060                "gpt-image-2",
1061                "openai/gpt-image-2",
1062                "openai/gpt-image-2",
1063                "openai/gpt-image-2",
1064            ),
1065            (
1066                "gpt-image-2",
1067                "OpenAI/GPT-Image-2",
1068                "OpenAI/GPT-Image-2",
1069                "OpenAI/GPT-Image-2",
1070            ),
1071            ("gpt-image-2", "gpt-image-1.5", "gpt-image-1.5", "gpt-image-1.5"),
1072            (
1073                "gpt-image-2",
1074                "codex/gpt-image-2",
1075                "codex/gpt-image-2",
1076                "codex/gpt-image-2",
1077            ),
1078            (
1079                "openai/gpt-image-2",
1080                "openai/gpt-image-2",
1081                "openai/gpt-image-2",
1082                "openai/gpt-image-2",
1083            ),
1084            (
1085                "tenant/gpt-image-2(high)",
1086                "OpenAI/GPT-Image-2(high)",
1087                "OpenAI/GPT-Image-2",
1088                "OpenAI/GPT-Image-2",
1089            ),
1090            (
1091                "codex/gpt-image-1.5(high)",
1092                "codex/gpt-image-1.5(high)",
1093                "",
1094                "gpt-image-1.5",
1095            ),
1096            ("CODEX/GPT-Image-2(high)", "CODEX/GPT-Image-2(high)", "", "gpt-image-2"),
1097        ] {
1098            let settings = Settings {
1099                key_models: if configured.is_empty() {
1100                    Vec::new()
1101                } else {
1102                    vec![serde_json::json!({"name": configured, "alias": "gpt-image-2"})]
1103                },
1104                ..Settings::default()
1105            };
1106            // A multipart edit has no JSON model; the client's model carries the route.
1107            for body in [json, form] {
1108                assert_eq!(
1109                    direct_model(&request(requested, resolved, body), &settings).as_deref(),
1110                    Some(want),
1111                    "{requested} -> {resolved}"
1112                );
1113            }
1114        }
1115    }
1116}