Skip to main content

codex_cli/runtime/
mod.rs

1use std::io::Write;
2use std::path::PathBuf;
3
4use nils_common::env as shared_env;
5use nils_common::provider_runtime;
6
7use crate::auth as codex_auth;
8use crate::auth::remote::{ENV_AUTH_REMOTE_NAME, ENV_AUTH_REMOTE_SSH};
9use crate::provider_profile::CODEX_PROVIDER_PROFILE;
10
11pub use nils_common::provider_runtime::ExecOptions;
12pub use nils_common::provider_runtime::{
13    CoreError, CoreErrorCategory, ProviderCategoryHint, auth, json, jwt,
14};
15
16pub fn config_snapshot() -> provider_runtime::config::RuntimeConfig {
17    provider_runtime::config::snapshot(&CODEX_PROVIDER_PROFILE)
18}
19
20pub fn resolve_secret_dir() -> Option<PathBuf> {
21    provider_runtime::paths::resolve_secret_dir(&CODEX_PROVIDER_PROFILE)
22}
23
24pub fn resolve_auth_file() -> Option<PathBuf> {
25    provider_runtime::paths::resolve_auth_file(&CODEX_PROVIDER_PROFILE)
26}
27
28pub fn resolve_secret_cache_dir() -> Option<PathBuf> {
29    provider_runtime::paths::resolve_secret_cache_dir(&CODEX_PROVIDER_PROFILE)
30}
31
32pub fn resolve_feature_dir() -> Option<PathBuf> {
33    provider_runtime::paths::resolve_feature_dir(&CODEX_PROVIDER_PROFILE)
34}
35
36pub fn resolve_script_dir() -> Option<PathBuf> {
37    provider_runtime::paths::resolve_script_dir()
38}
39
40pub fn resolve_zdotdir() -> Option<PathBuf> {
41    provider_runtime::paths::resolve_zdotdir()
42}
43
44pub fn require_allow_dangerous(caller: Option<&str>, stderr: &mut impl Write) -> bool {
45    provider_runtime::exec::require_allow_dangerous(&CODEX_PROVIDER_PROFILE, caller, stderr)
46}
47
48pub fn allow_dangerous_status(caller: Option<&str>) -> (bool, Option<String>) {
49    provider_runtime::exec::allow_dangerous_status(&CODEX_PROVIDER_PROFILE, caller)
50}
51
52pub fn check_allow_dangerous(caller: Option<&str>) -> Result<(), CoreError> {
53    provider_runtime::exec::check_allow_dangerous(&CODEX_PROVIDER_PROFILE, caller)
54}
55
56pub fn exec_dangerous(prompt: &str, caller: &str, stderr: &mut impl Write) -> i32 {
57    exec_dangerous_with_options(prompt, caller, stderr, ExecOptions::default())
58}
59
60pub fn exec_dangerous_with_options(
61    prompt: &str,
62    caller: &str,
63    stderr: &mut impl Write,
64    options: ExecOptions,
65) -> i32 {
66    if prompt.is_empty() {
67        return provider_runtime::exec::exec_dangerous_with_options(
68            &CODEX_PROVIDER_PROFILE,
69            prompt,
70            caller,
71            stderr,
72            options,
73        );
74    }
75    if !require_allow_dangerous(Some(caller), stderr) {
76        return 1;
77    }
78
79    let effective_options = ExecOptions {
80        ephemeral: options.ephemeral || shared_env::env_truthy("CODEX_CLI_EPHEMERAL_ENABLED"),
81    };
82    refresh_remote_auth_before_exec();
83    provider_runtime::exec::exec_dangerous_with_options(
84        &CODEX_PROVIDER_PROFILE,
85        prompt,
86        caller,
87        stderr,
88        effective_options,
89    )
90}
91
92fn refresh_remote_auth_before_exec() {
93    if std::env::var(ENV_AUTH_REMOTE_SSH)
94        .ok()
95        .filter(|value| !value.trim().is_empty())
96        .is_none()
97        && std::env::var(ENV_AUTH_REMOTE_NAME)
98            .ok()
99            .filter(|value| !value.trim().is_empty())
100            .is_none()
101    {
102        return;
103    }
104
105    let _ = codex_auth::refresh::run_silent(&[]);
106}