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.
2025-04-24 13:31:40 -07:00
|
|
|
|
mod cli;
|
2025-04-29 09:59:35 -07:00
|
|
|
|
mod event_processor;
|
|
|
|
|
|
|
|
|
|
|
|
use std::io::IsTerminal;
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
|
|
pub use cli::Cli;
|
|
|
|
|
|
use codex_core::codex_wrapper;
|
2025-04-27 21:47:50 -07:00
|
|
|
|
use codex_core::config::Config;
|
|
|
|
|
|
use codex_core::config::ConfigOverrides;
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
use codex_core::protocol::AskForApproval;
|
|
|
|
|
|
use codex_core::protocol::Event;
|
|
|
|
|
|
use codex_core::protocol::EventMsg;
|
|
|
|
|
|
use codex_core::protocol::InputItem;
|
|
|
|
|
|
use codex_core::protocol::Op;
|
|
|
|
|
|
use codex_core::util::is_inside_git_repo;
|
2025-04-29 09:59:35 -07:00
|
|
|
|
use event_processor::EventProcessor;
|
2025-04-28 21:42:06 -07:00
|
|
|
|
use owo_colors::OwoColorize;
|
2025-04-29 09:59:35 -07:00
|
|
|
|
use owo_colors::Style;
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
use tracing::debug;
|
|
|
|
|
|
use tracing::error;
|
|
|
|
|
|
use tracing::info;
|
|
|
|
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn run_main(cli: Cli) -> anyhow::Result<()> {
|
|
|
|
|
|
let Cli {
|
|
|
|
|
|
images,
|
2025-04-25 12:08:18 -07:00
|
|
|
|
model,
|
2025-04-27 21:47:50 -07:00
|
|
|
|
sandbox_policy,
|
2025-04-25 12:08:18 -07:00
|
|
|
|
skip_git_repo_check,
|
|
|
|
|
|
disable_response_storage,
|
2025-04-29 09:59:35 -07:00
|
|
|
|
color,
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
prompt,
|
|
|
|
|
|
} = cli;
|
|
|
|
|
|
|
2025-04-29 09:59:35 -07:00
|
|
|
|
let (stdout_with_ansi, stderr_with_ansi) = match color {
|
|
|
|
|
|
cli::Color::Always => (true, true),
|
|
|
|
|
|
cli::Color::Never => (false, false),
|
|
|
|
|
|
cli::Color::Auto => (
|
|
|
|
|
|
std::io::stdout().is_terminal(),
|
|
|
|
|
|
std::io::stderr().is_terminal(),
|
|
|
|
|
|
),
|
|
|
|
|
|
};
|
2025-04-28 21:42:06 -07:00
|
|
|
|
|
2025-04-29 09:59:35 -07:00
|
|
|
|
assert_api_key(stderr_with_ansi);
|
2025-04-28 21:42:06 -07:00
|
|
|
|
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
if !skip_git_repo_check && !is_inside_git_repo() {
|
|
|
|
|
|
eprintln!("Not inside a Git repo and --skip-git-repo-check was not specified.");
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 09:59:35 -07:00
|
|
|
|
// TODO(mbolin): Take a more thoughtful approach to logging.
|
|
|
|
|
|
let default_level = "error";
|
|
|
|
|
|
let _ = tracing_subscriber::fmt()
|
|
|
|
|
|
.with_env_filter(
|
|
|
|
|
|
EnvFilter::try_from_default_env()
|
|
|
|
|
|
.or_else(|_| EnvFilter::try_new(default_level))
|
|
|
|
|
|
.unwrap(),
|
|
|
|
|
|
)
|
|
|
|
|
|
.with_ansi(stderr_with_ansi)
|
|
|
|
|
|
.with_writer(std::io::stderr)
|
|
|
|
|
|
.try_init();
|
|
|
|
|
|
|
2025-04-27 21:47:50 -07:00
|
|
|
|
// Load configuration and determine approval policy
|
|
|
|
|
|
let overrides = ConfigOverrides {
|
2025-04-29 09:59:35 -07:00
|
|
|
|
model,
|
2025-04-27 21:47:50 -07:00
|
|
|
|
// This CLI is intended to be headless and has no affordances for asking
|
|
|
|
|
|
// the user for approval.
|
|
|
|
|
|
approval_policy: Some(AskForApproval::Never),
|
|
|
|
|
|
sandbox_policy: sandbox_policy.map(Into::into),
|
2025-04-28 15:39:34 -07:00
|
|
|
|
disable_response_storage: if disable_response_storage {
|
|
|
|
|
|
Some(true)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
},
|
2025-04-27 21:47:50 -07:00
|
|
|
|
};
|
|
|
|
|
|
let config = Config::load_with_overrides(overrides)?;
|
2025-04-28 15:39:34 -07:00
|
|
|
|
let (codex_wrapper, event, ctrl_c) = codex_wrapper::init_codex(config).await?;
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
let codex = Arc::new(codex_wrapper);
|
|
|
|
|
|
info!("Codex initialized with event: {event:?}");
|
|
|
|
|
|
|
|
|
|
|
|
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<Event>();
|
|
|
|
|
|
{
|
|
|
|
|
|
let codex = codex.clone();
|
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
|
loop {
|
|
|
|
|
|
let interrupted = ctrl_c.notified();
|
|
|
|
|
|
tokio::select! {
|
|
|
|
|
|
_ = interrupted => {
|
|
|
|
|
|
// Forward an interrupt to the codex so it can abort any in‑flight task.
|
|
|
|
|
|
let _ = codex
|
|
|
|
|
|
.submit(
|
|
|
|
|
|
Op::Interrupt,
|
|
|
|
|
|
)
|
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
|
|
// Exit the inner loop and return to the main input prompt. The codex
|
|
|
|
|
|
// will emit a `TurnInterrupted` (Error) event which is drained later.
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
res = codex.next_event() => match res {
|
|
|
|
|
|
Ok(event) => {
|
|
|
|
|
|
debug!("Received event: {event:?}");
|
|
|
|
|
|
if let Err(e) = tx.send(event) {
|
|
|
|
|
|
error!("Error sending event: {e:?}");
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
error!("Error receiving event: {e:?}");
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 09:59:35 -07:00
|
|
|
|
// Send images first, if any.
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
if !images.is_empty() {
|
|
|
|
|
|
let items: Vec<InputItem> = images
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.map(|path| InputItem::LocalImage { path })
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
let initial_images_event_id = codex.submit(Op::UserInput { items }).await?;
|
|
|
|
|
|
info!("Sent images with event ID: {initial_images_event_id}");
|
|
|
|
|
|
while let Ok(event) = codex.next_event().await {
|
|
|
|
|
|
if event.id == initial_images_event_id && matches!(event.msg, EventMsg::TaskComplete) {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 09:59:35 -07:00
|
|
|
|
// Send the prompt.
|
|
|
|
|
|
let items: Vec<InputItem> = vec![InputItem::Text { text: prompt }];
|
|
|
|
|
|
let initial_prompt_task_id = codex.submit(Op::UserInput { items }).await?;
|
|
|
|
|
|
info!("Sent prompt with event ID: {initial_prompt_task_id}");
|
|
|
|
|
|
|
|
|
|
|
|
// Run the loop until the task is complete.
|
|
|
|
|
|
let mut event_processor = EventProcessor::create_with_ansi(stdout_with_ansi);
|
|
|
|
|
|
while let Some(event) = rx.recv().await {
|
|
|
|
|
|
let last_event =
|
|
|
|
|
|
event.id == initial_prompt_task_id && matches!(event.msg, EventMsg::TaskComplete);
|
|
|
|
|
|
event_processor.process_event(event);
|
|
|
|
|
|
if last_event {
|
|
|
|
|
|
break;
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 09:59:35 -07:00
|
|
|
|
/// If a valid API key is not present in the environment, print an error to
|
|
|
|
|
|
/// stderr and exits with 1; otherwise, does nothing.
|
|
|
|
|
|
fn assert_api_key(stderr_with_ansi: bool) {
|
|
|
|
|
|
if !has_api_key() {
|
|
|
|
|
|
let (msg_style, var_style, url_style) = if stderr_with_ansi {
|
|
|
|
|
|
(
|
|
|
|
|
|
Style::new().red(),
|
|
|
|
|
|
Style::new().bold(),
|
|
|
|
|
|
Style::new().bold().underline(),
|
|
|
|
|
|
)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
(Style::new(), Style::new(), Style::new())
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
eprintln!(
|
|
|
|
|
|
"\n{msg}\n\nSet the environment variable {var} and re-run this command.\nYou can create a key here: {url}\n",
|
|
|
|
|
|
msg = "Missing OpenAI API key.".style(msg_style),
|
|
|
|
|
|
var = "OPENAI_API_KEY".style(var_style),
|
|
|
|
|
|
url = "https://platform.openai.com/account/api-keys".style(url_style),
|
|
|
|
|
|
);
|
|
|
|
|
|
std::process::exit(1);
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 09:59:35 -07:00
|
|
|
|
/// Returns `true` if a recognized API key is present in the environment.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// At present we only support `OPENAI_API_KEY`, mirroring the behavior of the
|
|
|
|
|
|
/// Node-based `codex-cli`. Additional providers can be added here when the
|
|
|
|
|
|
/// Rust implementation gains first-class support for them.
|
|
|
|
|
|
fn has_api_key() -> bool {
|
|
|
|
|
|
std::env::var("OPENAI_API_KEY")
|
|
|
|
|
|
.map(|s| !s.trim().is_empty())
|
|
|
|
|
|
.unwrap_or(false)
|
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.
2025-04-24 13:31:40 -07:00
|
|
|
|
}
|