feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`: Today, Codex CLI is written in TypeScript and requires Node.js 22+ to run it. For a number of users, this runtime requirement inhibits adoption: they would be better served by a standalone executable. As maintainers, we want Codex to run efficiently in a wide range of environments with minimal overhead. We also want to take advantage of operating system-specific APIs to provide better sandboxing, where possible. To that end, we are moving forward with a Rust implementation of Codex CLI contained in this folder, which has the following benefits: - The CLI compiles to small, standalone, platform-specific binaries. - Can make direct, native calls to [seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and [landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in order to support sandboxing on Linux. - No runtime garbage collection, resulting in lower memory consumption and better, more predictable performance. Currently, the Rust implementation is materially behind the TypeScript implementation in functionality, so continue to use the TypeScript implmentation for the time being. We will publish native executables via GitHub Releases as soon as we feel the Rust version is usable.
This commit is contained in:
42
codex-rs/core/src/config.rs
Normal file
42
codex-rs/core/src/config.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use dirs::home_dir;
|
||||
use serde::Deserialize;
|
||||
|
||||
/// Embedded fallback instructions that mirror the TypeScript CLI’s default system prompt. These
|
||||
/// are compiled into the binary so a clean install behaves correctly even if the user has not
|
||||
/// created `~/.codex/instructions.md`.
|
||||
const EMBEDDED_INSTRUCTIONS: &str = include_str!("../prompt.md");
|
||||
|
||||
#[derive(Default, Deserialize, Debug, Clone)]
|
||||
pub struct Config {
|
||||
pub model: Option<String>,
|
||||
pub instructions: Option<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Load ~/.codex/config.toml and ~/.codex/instructions.md (if present).
|
||||
/// Returns `None` if neither file exists.
|
||||
pub fn load() -> Option<Self> {
|
||||
let mut cfg: Config = Self::load_from_toml().unwrap_or_default();
|
||||
|
||||
// Highest precedence → user‑provided ~/.codex/instructions.md (if present)
|
||||
// Fallback → embedded default instructions baked into the binary
|
||||
|
||||
cfg.instructions =
|
||||
Self::load_instructions().or_else(|| Some(EMBEDDED_INSTRUCTIONS.to_string()));
|
||||
|
||||
Some(cfg)
|
||||
}
|
||||
|
||||
fn load_from_toml() -> Option<Self> {
|
||||
let mut p = home_dir()?;
|
||||
p.push(".codex/config.toml");
|
||||
let contents = std::fs::read_to_string(&p).ok()?;
|
||||
toml::from_str(&contents).ok()
|
||||
}
|
||||
|
||||
fn load_instructions() -> Option<String> {
|
||||
let mut p = home_dir()?;
|
||||
p.push(".codex/instructions.md");
|
||||
std::fs::read_to_string(&p).ok()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user