use std::collections::HashMap;
use std::ffi::OsString;
use std::path::PathBuf;
use std::sync::Arc;
use serde_json::Value;
use crate::VERSION;
pub type ApprovalHandler =
Arc<dyn Fn(&str, Option<&Value>) -> crate::Result<Value> + Send + Sync + 'static>;
#[derive(Clone)]
pub struct CodexConfig {
pub codex_bin: Option<PathBuf>,
pub launch_args_override: Option<Vec<OsString>>,
pub config_overrides: Vec<String>,
pub cwd: Option<PathBuf>,
pub env: HashMap<OsString, OsString>,
pub client_name: String,
pub client_title: String,
pub client_version: String,
pub experimental_api: bool,
pub approval_handler: ApprovalHandler,
}
impl std::fmt::Debug for CodexConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CodexConfig")
.field("codex_bin", &self.codex_bin)
.field("launch_args_override", &self.launch_args_override)
.field("config_overrides", &self.config_overrides)
.field("cwd", &self.cwd)
.field("env", &self.env)
.field("client_name", &self.client_name)
.field("client_title", &self.client_title)
.field("client_version", &self.client_version)
.field("experimental_api", &self.experimental_api)
.field("approval_handler", &"<callback>")
.finish()
}
}
impl Default for CodexConfig {
fn default() -> Self {
Self {
codex_bin: None,
launch_args_override: None,
config_overrides: Vec::new(),
cwd: None,
env: HashMap::new(),
client_name: "codex_rust_sdk".into(),
client_title: "Codex Rust SDK".into(),
client_version: VERSION.into(),
experimental_api: true,
approval_handler: Arc::new(|method, _params| {
Ok(match method {
"item/commandExecution/requestApproval" | "item/fileChange/requestApproval" => {
serde_json::json!({"decision": "accept"})
}
_ => serde_json::json!({}),
})
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_are_debuggable_and_answer_supported_approvals() {
let config = CodexConfig::default();
let debug = format!("{config:?}");
assert!(debug.contains("codex_rust_sdk"));
assert!(debug.contains("<callback>"));
assert_eq!(
(config.approval_handler)("item/fileChange/requestApproval", None).unwrap(),
serde_json::json!({"decision":"accept"})
);
assert_eq!(
(config.approval_handler)("future/request", None).unwrap(),
serde_json::json!({})
);
}
}