1use std::collections::HashMap;
2use std::ffi::OsString;
3use std::path::PathBuf;
4use std::sync::Arc;
5
6use serde_json::Value;
7
8use crate::VERSION;
9
10pub type ApprovalHandler =
12 Arc<dyn Fn(&str, Option<&Value>) -> crate::Result<Value> + Send + Sync + 'static>;
13
14#[derive(Clone)]
16pub struct CodexConfig {
17 pub codex_bin: Option<PathBuf>,
18 pub launch_args_override: Option<Vec<OsString>>,
19 pub config_overrides: Vec<String>,
20 pub cwd: Option<PathBuf>,
21 pub env: HashMap<OsString, OsString>,
22 pub client_name: String,
23 pub client_title: String,
24 pub client_version: String,
25 pub experimental_api: bool,
26 pub approval_handler: ApprovalHandler,
27}
28
29impl std::fmt::Debug for CodexConfig {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 f.debug_struct("CodexConfig")
32 .field("codex_bin", &self.codex_bin)
33 .field("launch_args_override", &self.launch_args_override)
34 .field("config_overrides", &self.config_overrides)
35 .field("cwd", &self.cwd)
36 .field("env", &self.env)
37 .field("client_name", &self.client_name)
38 .field("client_title", &self.client_title)
39 .field("client_version", &self.client_version)
40 .field("experimental_api", &self.experimental_api)
41 .field("approval_handler", &"<callback>")
42 .finish()
43 }
44}
45
46impl Default for CodexConfig {
47 fn default() -> Self {
48 Self {
49 codex_bin: None,
50 launch_args_override: None,
51 config_overrides: Vec::new(),
52 cwd: None,
53 env: HashMap::new(),
54 client_name: "codex_rust_sdk".into(),
55 client_title: "Codex Rust SDK".into(),
56 client_version: VERSION.into(),
57 experimental_api: true,
58 approval_handler: Arc::new(|method, _params| {
59 Ok(match method {
60 "item/commandExecution/requestApproval" | "item/fileChange/requestApproval" => {
61 serde_json::json!({"decision": "accept"})
62 }
63 _ => serde_json::json!({}),
64 })
65 }),
66 }
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn defaults_are_debuggable_and_answer_supported_approvals() {
76 let config = CodexConfig::default();
77 let debug = format!("{config:?}");
78 assert!(debug.contains("codex_rust_sdk"));
79 assert!(debug.contains("<callback>"));
80 assert_eq!(
81 (config.approval_handler)("item/fileChange/requestApproval", None).unwrap(),
82 serde_json::json!({"decision":"accept"})
83 );
84 assert_eq!(
85 (config.approval_handler)("future/request", None).unwrap(),
86 serde_json::json!({})
87 );
88 }
89}