Skip to main content

codex_runtime/runtime/client/
config.rs

1use std::path::PathBuf;
2use std::sync::Arc;
3
4use crate::plugin::{PostHook, PreHook};
5use crate::runtime::hooks::RuntimeHookConfig;
6use crate::runtime::InitializeCapabilities;
7
8use super::CompatibilityGuard;
9
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct ClientConfig {
12    pub cli_bin: PathBuf,
13    pub compatibility_guard: CompatibilityGuard,
14    pub initialize_capabilities: InitializeCapabilities,
15    pub hooks: RuntimeHookConfig,
16}
17
18impl Default for ClientConfig {
19    fn default() -> Self {
20        Self {
21            cli_bin: PathBuf::from("codex"),
22            compatibility_guard: CompatibilityGuard::default(),
23            initialize_capabilities: InitializeCapabilities::default(),
24            hooks: RuntimeHookConfig::default(),
25        }
26    }
27}
28
29impl ClientConfig {
30    /// Create config with default binary discovery.
31    pub fn new() -> Self {
32        Self::default()
33    }
34
35    /// Override CLI executable path.
36    pub fn with_cli_bin(mut self, cli_bin: impl Into<PathBuf>) -> Self {
37        self.cli_bin = cli_bin.into();
38        self
39    }
40
41    /// Override runtime compatibility guard policy.
42    pub fn with_compatibility_guard(mut self, guard: CompatibilityGuard) -> Self {
43        self.compatibility_guard = guard;
44        self
45    }
46
47    /// Disable compatibility guard checks at connect time.
48    pub fn without_compatibility_guard(mut self) -> Self {
49        self.compatibility_guard = CompatibilityGuard {
50            require_initialize_user_agent: false,
51            min_codex_version: None,
52        };
53        self
54    }
55
56    /// Override initialize capability switches.
57    pub fn with_initialize_capabilities(
58        mut self,
59        initialize_capabilities: InitializeCapabilities,
60    ) -> Self {
61        self.initialize_capabilities = initialize_capabilities;
62        self
63    }
64
65    /// Opt into Codex experimental app-server methods and fields.
66    pub fn enable_experimental_api(mut self) -> Self {
67        self.initialize_capabilities = self.initialize_capabilities.enable_experimental_api();
68        self
69    }
70
71    /// Replace runtime hook configuration.
72    pub fn with_hooks(mut self, hooks: RuntimeHookConfig) -> Self {
73        self.hooks = hooks;
74        self
75    }
76
77    /// Register one pre hook on client runtime config.
78    pub fn with_pre_hook(mut self, hook: Arc<dyn PreHook>) -> Self {
79        self.hooks.pre_hooks.push(hook);
80        self
81    }
82
83    /// Register one post hook on client runtime config.
84    pub fn with_post_hook(mut self, hook: Arc<dyn PostHook>) -> Self {
85        self.hooks.post_hooks.push(hook);
86        self
87    }
88
89    /// Register one pre-tool-use hook on client runtime config.
90    /// When at least one pre-tool-use hook is registered, the runtime manages the
91    /// approval channel internally and auto-escalates ApprovalPolicy → Untrusted.
92    /// Allocation: amortized O(1) push. Complexity: O(1).
93    pub fn with_pre_tool_use_hook(mut self, hook: Arc<dyn PreHook>) -> Self {
94        self.hooks.pre_tool_use_hooks.push(hook);
95        self
96    }
97}