1use super::usage::{self, count};
5use crate::streaming::parse_json_records;
6use crate::tools::shell::{
7 build_command_head, escape_arg, escape_single_quotes, lookup_model_alias,
8};
9use serde_json::Value;
10use std::collections::HashMap;
11
12pub fn get_model_map() -> HashMap<&'static str, &'static str> {
14 let mut map = HashMap::new();
15 map.insert("gpt5-codex", "gpt-5-codex");
17 map.insert("o3", "o3");
18 map.insert("claude", "claude-3-5-sonnet");
19 map.insert("sonnet", "claude-3-5-sonnet");
20 map.insert("opus", "claude-3-opus");
21 map.insert("formal-ai", "formal-ai");
22 map.insert("formalai/formal-ai", "formal-ai");
23 map.insert("gpt-6.1-sol", "gpt-6.1-sol");
24 map.insert("gpt-6-sol", "gpt-6-sol");
25 map.insert("gpt-6-luna", "gpt-6-luna");
26 map.insert("gpt-reserve", "gpt-reserve");
27 map.insert("gpt-6-astra", "gpt-6-astra");
28 map.insert("gpt5", "gpt-5");
29 map.insert("gpt-5", "gpt-5");
30 map.insert("gpt-5.5", "gpt-5.5");
31 map.insert("gpt-5.5-mini", "gpt-5.5-mini");
32 map.insert("gpt-5.5-nano", "gpt-5.5-nano");
33 map.insert("gpt-5.6-sol", "gpt-5.6-sol");
34 map.insert("gpt-5.6-terra", "gpt-5.6-terra");
35 map.insert("gpt-5.6-luna", "gpt-5.6-luna");
36 map.insert("gpt-5.6-cyber", "gpt-5.6-cyber");
37 map.insert("gpt-5.4", "gpt-5.4");
38 map.insert("gpt-5.4-mini", "gpt-5.4-mini");
39 map.insert("gpt-5.4-nano", "gpt-5.4-nano");
40 map.insert("gpt-5.2", "gpt-5.2");
41 map.insert("gpt-5.2-codex", "gpt-5.2-codex");
42 map.insert("gpt-5.3-codex", "gpt-5.3-codex");
43 map.insert("gpt-5.3-codex-spark", "gpt-5.3-codex-spark");
44 map.insert("gpt-5.1-codex-max", "gpt-5.1-codex-max");
45 map.insert("gpt-daybreak-blue-latest", "gpt-daybreak-blue-latest");
46 map.insert("gpt-daybreak-red-latest", "gpt-daybreak-red-latest");
47 map.insert("openai.gpt-5.5", "openai.gpt-5.5");
48 map.insert("openai.gpt-5.4", "openai.gpt-5.4");
49 map.insert("openai.gpt-5.6-sol", "openai.gpt-5.6-sol");
50 map.insert("openai.gpt-5.6-terra", "openai.gpt-5.6-terra");
51 map.insert("openai.gpt-5.6-luna", "openai.gpt-5.6-luna");
52 map.insert("codex-auto-review", "codex-auto-review");
53 map.insert("o3-mini", "o3-mini");
54 map.insert("gpt4", "gpt-4");
55 map.insert("gpt-4", "gpt-4");
56 map.insert("gpt4o", "gpt-4o");
57 map.insert("gpt-4o", "gpt-4o");
58 map.insert("daybreak-blue", "gpt-daybreak-blue-latest");
59 map.insert("daybreak-red", "gpt-daybreak-red-latest");
60 map.insert("astra", "gpt-6-astra");
61 map.insert("sol", "gpt-6.1-sol");
62 map.insert("luna", "gpt-6-luna");
63 map.insert("terra", "gpt-5.6-terra");
64 map.insert("openai/gpt-6.1-sol", "openai/gpt-6.1-sol");
65 map.insert("openai.gpt-6.1-sol", "openai.gpt-6.1-sol");
66 map.insert("openai/gpt-6-sol", "openai/gpt-6-sol");
67 map.insert("openai.gpt-6-sol", "openai.gpt-6-sol");
68 map.insert("openai/gpt-6-luna", "openai/gpt-6-luna");
69 map.insert("openai.gpt-6-luna", "openai.gpt-6-luna");
70 map.insert("openai/gpt-reserve", "openai/gpt-reserve");
71 map.insert("openai.gpt-reserve", "openai.gpt-reserve");
72 map.insert("openai/gpt-6-astra", "openai/gpt-6-astra");
73 map.insert("openai.gpt-6-astra", "openai.gpt-6-astra");
74 map.insert("openai/gpt-5", "openai/gpt-5");
75 map.insert("openai.gpt-5", "openai.gpt-5");
76 map.insert("openai/gpt-5.5", "openai/gpt-5.5");
77 map.insert("openai/gpt-5.5-mini", "openai/gpt-5.5-mini");
78 map.insert("openai.gpt-5.5-mini", "openai.gpt-5.5-mini");
79 map.insert("openai/gpt-5.5-nano", "openai/gpt-5.5-nano");
80 map.insert("openai.gpt-5.5-nano", "openai.gpt-5.5-nano");
81 map.insert("openai/gpt-5.6-sol", "openai/gpt-5.6-sol");
82 map.insert("openai/gpt-5.6-terra", "openai/gpt-5.6-terra");
83 map.insert("openai/gpt-5.6-luna", "openai/gpt-5.6-luna");
84 map.insert("openai/gpt-5.6-cyber", "openai/gpt-5.6-cyber");
85 map.insert("openai.gpt-5.6-cyber", "openai.gpt-5.6-cyber");
86 map.insert("openai/gpt-5.4", "openai/gpt-5.4");
87 map.insert("openai/gpt-5.4-mini", "openai/gpt-5.4-mini");
88 map.insert("openai.gpt-5.4-mini", "openai.gpt-5.4-mini");
89 map.insert("openai/gpt-5.4-nano", "openai/gpt-5.4-nano");
90 map.insert("openai.gpt-5.4-nano", "openai.gpt-5.4-nano");
91 map.insert("openai/gpt-5.2", "openai/gpt-5.2");
92 map.insert("openai.gpt-5.2", "openai.gpt-5.2");
93 map.insert("openai/gpt-5.2-codex", "openai/gpt-5.2-codex");
94 map.insert("openai.gpt-5.2-codex", "openai.gpt-5.2-codex");
95 map.insert("openai/gpt-5.3-codex", "openai/gpt-5.3-codex");
96 map.insert("openai.gpt-5.3-codex", "openai.gpt-5.3-codex");
97 map.insert("openai/gpt-5.3-codex-spark", "openai/gpt-5.3-codex-spark");
98 map.insert("openai.gpt-5.3-codex-spark", "openai.gpt-5.3-codex-spark");
99 map.insert("openai/gpt-5.1-codex-max", "openai/gpt-5.1-codex-max");
100 map.insert("openai.gpt-5.1-codex-max", "openai.gpt-5.1-codex-max");
101 map.insert(
102 "openai/gpt-daybreak-blue-latest",
103 "openai/gpt-daybreak-blue-latest",
104 );
105 map.insert(
106 "openai.gpt-daybreak-blue-latest",
107 "openai.gpt-daybreak-blue-latest",
108 );
109 map.insert(
110 "openai/gpt-daybreak-red-latest",
111 "openai/gpt-daybreak-red-latest",
112 );
113 map.insert(
114 "openai.gpt-daybreak-red-latest",
115 "openai.gpt-daybreak-red-latest",
116 );
117 map.insert("openai/codex-auto-review", "openai/codex-auto-review");
118 map.insert("openai.codex-auto-review", "openai.codex-auto-review");
119 map.insert("openai/o3-mini", "openai/o3-mini");
120 map.insert("openai.o3-mini", "openai.o3-mini");
121 map.insert("openai/gpt-4", "openai/gpt-4");
122 map.insert("openai.gpt-4", "openai.gpt-4");
123 map.insert("openai/gpt-4o", "openai/gpt-4o");
124 map.insert("openai.gpt-4o", "openai.gpt-4o");
125 map.insert("openai/daybreak-blue", "openai/gpt-daybreak-blue-latest");
126 map.insert("openai.daybreak-blue", "openai.gpt-daybreak-blue-latest");
127 map.insert("openai/daybreak-red", "openai/gpt-daybreak-red-latest");
128 map.insert("openai.daybreak-red", "openai.gpt-daybreak-red-latest");
129 map.insert("openai/astra", "openai/gpt-6-astra");
130 map.insert("openai.astra", "openai.gpt-6-astra");
131 map.insert("openai/sol", "openai/gpt-6.1-sol");
132 map.insert("openai.sol", "openai.gpt-6.1-sol");
133 map.insert("openai/luna", "openai/gpt-6-luna");
134 map.insert("openai.luna", "openai.gpt-6-luna");
135 map.insert("openai/terra", "openai/gpt-5.6-terra");
136 map.insert("openai.terra", "openai.gpt-5.6-terra");
137 map
138}
139
140pub fn map_model_to_id(model: &str) -> String {
148 lookup_model_alias(&get_model_map(), model).unwrap_or_else(|| model.to_string())
149}
150
151#[derive(Debug, Clone, Default)]
153pub struct CodexBuildOptions {
154 pub prompt: Option<String>,
155 pub prompt_file: Option<String>,
156 pub system_prompt: Option<String>,
157 pub model: Option<String>,
158 pub json: bool,
159 pub resume: Option<String>,
160 pub read_only: bool,
161 pub executable: Option<String>,
162 pub extra_env: Vec<(String, String)>,
163 pub unset_env: Vec<String>,
165 pub extra_args: Vec<String>,
166 pub skip_default_safety_flags: bool,
167 pub sandbox_mode: Option<String>,
168 pub approval_mode: Option<String>,
169}
170
171pub fn build_args(options: &CodexBuildOptions) -> Vec<String> {
179 let mut args = vec!["exec".to_string()];
180
181 if let Some(ref resume) = options.resume {
182 args.push("resume".to_string());
183 args.push(resume.clone());
184 }
185
186 if let Some(ref model) = options.model {
187 let mapped_model = map_model_to_id(model);
188 args.push("--model".to_string());
189 args.push(mapped_model);
190 }
191
192 if options.json {
194 args.push("--json".to_string());
195 }
196
197 args.push("--skip-git-repo-check".to_string());
199 if options.read_only {
200 args.push("--sandbox".to_string());
201 args.push("read-only".to_string());
202 } else if let Some(ref sandbox_mode) = options.sandbox_mode {
203 args.push("--sandbox".to_string());
204 args.push(sandbox_mode.clone());
205 }
206
207 if !options.read_only
208 && options.sandbox_mode.is_none()
209 && options.approval_mode.is_none()
210 && !options.skip_default_safety_flags
211 {
212 args.push("--dangerously-bypass-approvals-and-sandbox".to_string());
213 }
214
215 args.extend(options.extra_args.clone());
216
217 args
218}
219
220pub fn build_command(options: &CodexBuildOptions) -> String {
229 let args = build_args(options);
230 let args_str: Vec<String> = args.iter().map(|a| escape_arg(a)).collect();
231
232 let combined_prompt = match (&options.system_prompt, &options.prompt) {
234 (Some(sys), Some(prompt)) => format!("{}\n\n{}", sys, prompt),
235 (Some(sys), None) => sys.clone(),
236 (None, Some(prompt)) => prompt.clone(),
237 (None, None) => String::new(),
238 };
239
240 let input_command = options.prompt_file.as_ref().map_or_else(
242 || format!("printf '%s' '{}'", escape_single_quotes(&combined_prompt)),
243 |prompt_file| format!("cat {}", escape_arg(prompt_file)),
244 );
245 let executable = options.executable.as_deref().unwrap_or("codex");
246 let mut prefix_args = Vec::new();
247 if options.read_only {
248 prefix_args.push("--ask-for-approval".to_string());
249 prefix_args.push("never".to_string());
250 } else if let Some(ref approval_mode) = options.approval_mode {
251 prefix_args.push("--ask-for-approval".to_string());
252 prefix_args.push(approval_mode.clone());
253 }
254
255 format!(
256 "{} | {} {}",
257 input_command,
258 build_command_head(
259 executable,
260 &options.unset_env,
261 &options.extra_env,
262 &prefix_args
263 ),
264 args_str.join(" ")
265 )
266 .trim()
267 .to_string()
268}
269
270pub fn parse_output(output: &str) -> Vec<Value> {
279 parse_json_records(output)
280}
281
282pub fn extract_session_id(output: &str) -> Option<String> {
291 let messages = parse_output(output);
292
293 for msg in messages {
294 if let Some(thread_id) = msg.get("thread_id").and_then(|v| v.as_str()) {
295 return Some(thread_id.to_string());
296 }
297 if let Some(session_id) = msg.get("session_id").and_then(|v| v.as_str()) {
298 return Some(session_id.to_string());
299 }
300 }
301
302 None
303}
304
305#[derive(Debug, Clone, Default, serde::Serialize)]
307#[serde(rename_all = "camelCase")]
308pub struct CodexUsage {
309 pub input_tokens: u64,
310 pub output_tokens: u64,
311 pub cache_read_tokens: u64,
312 pub cache_creation_tokens: u64,
313 pub reasoning_tokens: u64,
314}
315
316pub fn extract_usage(output: &str) -> CodexUsage {
324 let messages = parse_output(output);
325 let mut result = CodexUsage::default();
326 for record in usage::records("codex", &messages) {
327 result.input_tokens += count(record, usage::INPUT);
328 result.output_tokens += count(record, usage::OUTPUT);
329 result.cache_read_tokens += count(record, usage::CACHE_READ);
330 result.cache_creation_tokens += count(record, usage::CACHE_WRITE);
331 result.reasoning_tokens += count(record, usage::REASONING);
332 result.input_tokens -= count(record, usage::INPUT).min(count(record, usage::CACHE_READ));
333 }
334 result
335}
336
337#[derive(Debug, Clone)]
339pub struct CodexTool {
340 pub name: &'static str,
341 pub display_name: &'static str,
342 pub executable: &'static str,
343 pub supports_json_output: bool,
344 pub supports_json_input: bool,
345 pub supports_system_prompt: bool,
346 pub supports_resume: bool,
347 pub supports_read_only: bool,
348 pub supports_ask: bool,
349 pub default_model: &'static str,
350}
351
352impl Default for CodexTool {
353 fn default() -> Self {
354 Self {
355 name: "codex",
356 display_name: "Codex CLI",
357 executable: "codex",
358 supports_json_output: true,
359 supports_json_input: false, supports_system_prompt: false, supports_resume: true,
362 supports_read_only: true, supports_ask: false, default_model: "gpt-6-sol",
365 }
366 }
367}
368
369