2025-07-31 00:43:21 -07:00
|
|
|
use std::collections::HashMap;
|
2025-05-04 11:12:40 -07:00
|
|
|
use std::path::PathBuf;
|
chore: introduce ConversationManager as a clearinghouse for all conversations (#2240)
This PR does two things because after I got deep into the first one I
started pulling on the thread to the second:
- Makes `ConversationManager` the place where all in-memory
conversations are created and stored. Previously, `MessageProcessor` in
the `codex-mcp-server` crate was doing this via its `session_map`, but
this is something that should be done in `codex-core`.
- It unwinds the `ctrl_c: tokio::sync::Notify` that was threaded
throughout our code. I think this made sense at one time, but now that
we handle Ctrl-C within the TUI and have a proper `Op::Interrupt` event,
I don't think this was quite right, so I removed it. For `codex exec`
and `codex proto`, we now use `tokio::signal::ctrl_c()` directly, but we
no longer make `Notify` a field of `Codex` or `CodexConversation`.
Changes of note:
- Adds the files `conversation_manager.rs` and `codex_conversation.rs`
to `codex-core`.
- `Codex` and `CodexSpawnOk` are no longer exported from `codex-core`:
other crates must use `CodexConversation` instead (which is created via
`ConversationManager`).
- `core/src/codex_wrapper.rs` has been deleted in favor of
`ConversationManager`.
- `ConversationManager::new_conversation()` returns `NewConversation`,
which is in line with the `new_conversation` tool we want to add to the
MCP server. Note `NewConversation` includes `SessionConfiguredEvent`, so
we eliminate checks in cases like `codex-rs/core/tests/client.rs` to
verify `SessionConfiguredEvent` is the first event because that is now
internal to `ConversationManager`.
- Quite a bit of code was deleted from
`codex-rs/mcp-server/src/message_processor.rs` since it no longer has to
manage multiple conversations itself: it goes through
`ConversationManager` instead.
- `core/tests/live_agent.rs` has been deleted because I had to update a
bunch of tests and all the tests in here were ignored, and I don't think
anyone ever ran them, so this was just technical debt, at this point.
- Removed `notify_on_sigint()` from `util.rs` (and in a follow-up, I
hope to refactor the blandly-named `util.rs` into more descriptive
files).
- In general, I started replacing local variables named `codex` as
`conversation`, where appropriate, though admittedly I didn't do it
through all the integration tests because that would have added a lot of
noise to this PR.
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/openai/codex/pull/2240).
* #2264
* #2263
* __->__ #2240
2025-08-13 13:38:18 -07:00
|
|
|
use std::sync::Arc;
|
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-27 21:47:50 -07:00
|
|
|
use codex_core::config::Config;
|
2025-08-11 11:26:15 -07:00
|
|
|
use codex_core::parse_command::ParsedCommand;
|
2025-07-16 15:11:18 -07:00
|
|
|
use codex_core::protocol::AgentMessageDeltaEvent;
|
2025-05-13 20:44:42 -07:00
|
|
|
use codex_core::protocol::AgentMessageEvent;
|
2025-07-16 15:11:18 -07:00
|
|
|
use codex_core::protocol::AgentReasoningDeltaEvent;
|
2025-05-13 20:44:42 -07:00
|
|
|
use codex_core::protocol::AgentReasoningEvent;
|
2025-08-05 01:56:13 -07:00
|
|
|
use codex_core::protocol::AgentReasoningRawContentDeltaEvent;
|
|
|
|
|
use codex_core::protocol::AgentReasoningRawContentEvent;
|
2025-05-13 20:44:42 -07:00
|
|
|
use codex_core::protocol::ApplyPatchApprovalRequestEvent;
|
2025-08-05 22:44:27 -07:00
|
|
|
use codex_core::protocol::BackgroundEventEvent;
|
2025-05-13 20:44:42 -07:00
|
|
|
use codex_core::protocol::ErrorEvent;
|
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::Event;
|
|
|
|
|
use codex_core::protocol::EventMsg;
|
2025-05-13 20:44:42 -07:00
|
|
|
use codex_core::protocol::ExecApprovalRequestEvent;
|
|
|
|
|
use codex_core::protocol::ExecCommandBeginEvent;
|
|
|
|
|
use codex_core::protocol::ExecCommandEndEvent;
|
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::InputItem;
|
2025-05-13 20:44:42 -07:00
|
|
|
use codex_core::protocol::McpToolCallBeginEvent;
|
|
|
|
|
use codex_core::protocol::McpToolCallEndEvent;
|
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::Op;
|
2025-05-13 20:44:42 -07:00
|
|
|
use codex_core::protocol::PatchApplyBeginEvent;
|
2025-05-19 16:08:18 -07:00
|
|
|
use codex_core::protocol::TaskCompleteEvent;
|
feat: show number of tokens remaining in UI (#1388)
When using the OpenAI Responses API, we now record the `usage` field for
a `"response.completed"` event, which includes metrics about the number
of tokens consumed. We also introduce `openai_model_info.rs`, which
includes current data about the most common OpenAI models available via
the API (specifically `context_window` and `max_output_tokens`). If
Codex does not recognize the model, you can set `model_context_window`
and `model_max_output_tokens` explicitly in `config.toml`.
When then introduce a new event type to `protocol.rs`, `TokenCount`,
which includes the `TokenUsage` for the most recent turn.
Finally, we update the TUI to record the running sum of tokens used so
the percentage of available context window remaining can be reported via
the placeholder text for the composer:

We could certainly get much fancier with this (such as reporting the
estimated cost of the conversation), but for now, we are just trying to
achieve feature parity with the TypeScript CLI.
Though arguably this improves upon the TypeScript CLI, as the TypeScript
CLI uses heuristics to estimate the number of tokens used rather than
using the `usage` information directly:
https://github.com/openai/codex/blob/296996d74e345b1b05d8c3451a06ace21c5ada96/codex-cli/src/utils/approximate-tokens-used.ts#L3-L16
Fixes https://github.com/openai/codex/issues/1242
2025-06-25 23:31:11 -07:00
|
|
|
use codex_core::protocol::TokenUsage;
|
2025-08-05 22:44:27 -07:00
|
|
|
use codex_core::protocol::TurnDiffEvent;
|
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 crossterm::event::KeyEvent;
|
2025-07-31 17:10:52 -07:00
|
|
|
use crossterm::event::KeyEventKind;
|
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 ratatui::buffer::Buffer;
|
2025-08-06 12:03:45 -07:00
|
|
|
use ratatui::layout::Constraint;
|
|
|
|
|
use ratatui::layout::Layout;
|
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 ratatui::layout::Rect;
|
|
|
|
|
use ratatui::widgets::Widget;
|
|
|
|
|
use ratatui::widgets::WidgetRef;
|
|
|
|
|
use tokio::sync::mpsc::UnboundedSender;
|
2025-08-12 17:37:28 -07:00
|
|
|
use tracing::debug;
|
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 crate::app_event::AppEvent;
|
2025-05-15 14:50:30 -07:00
|
|
|
use crate::app_event_sender::AppEventSender;
|
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 crate::bottom_pane::BottomPane;
|
|
|
|
|
use crate::bottom_pane::BottomPaneParams;
|
2025-07-28 12:00:06 -07:00
|
|
|
use crate::bottom_pane::CancellationEvent;
|
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 crate::bottom_pane::InputResult;
|
2025-08-12 17:37:28 -07:00
|
|
|
use crate::exec_command::strip_bash_lc_and_escape;
|
2025-07-30 10:05:40 -07:00
|
|
|
use crate::history_cell::CommandOutput;
|
|
|
|
|
use crate::history_cell::HistoryCell;
|
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 crate::history_cell::PatchEventType;
|
2025-08-12 17:37:28 -07:00
|
|
|
// streaming internals are provided by crate::streaming and crate::markdown_stream
|
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 crate::user_approval_widget::ApprovalRequest;
|
2025-08-12 17:37:28 -07:00
|
|
|
mod interrupts;
|
|
|
|
|
use self::interrupts::InterruptManager;
|
|
|
|
|
mod agent;
|
|
|
|
|
use self::agent::spawn_agent;
|
|
|
|
|
use crate::streaming::controller::AppEventHistorySink;
|
|
|
|
|
use crate::streaming::controller::StreamController;
|
chore: introduce ConversationManager as a clearinghouse for all conversations (#2240)
This PR does two things because after I got deep into the first one I
started pulling on the thread to the second:
- Makes `ConversationManager` the place where all in-memory
conversations are created and stored. Previously, `MessageProcessor` in
the `codex-mcp-server` crate was doing this via its `session_map`, but
this is something that should be done in `codex-core`.
- It unwinds the `ctrl_c: tokio::sync::Notify` that was threaded
throughout our code. I think this made sense at one time, but now that
we handle Ctrl-C within the TUI and have a proper `Op::Interrupt` event,
I don't think this was quite right, so I removed it. For `codex exec`
and `codex proto`, we now use `tokio::signal::ctrl_c()` directly, but we
no longer make `Notify` a field of `Codex` or `CodexConversation`.
Changes of note:
- Adds the files `conversation_manager.rs` and `codex_conversation.rs`
to `codex-core`.
- `Codex` and `CodexSpawnOk` are no longer exported from `codex-core`:
other crates must use `CodexConversation` instead (which is created via
`ConversationManager`).
- `core/src/codex_wrapper.rs` has been deleted in favor of
`ConversationManager`.
- `ConversationManager::new_conversation()` returns `NewConversation`,
which is in line with the `new_conversation` tool we want to add to the
MCP server. Note `NewConversation` includes `SessionConfiguredEvent`, so
we eliminate checks in cases like `codex-rs/core/tests/client.rs` to
verify `SessionConfiguredEvent` is the first event because that is now
internal to `ConversationManager`.
- Quite a bit of code was deleted from
`codex-rs/mcp-server/src/message_processor.rs` since it no longer has to
manage multiple conversations itself: it goes through
`ConversationManager` instead.
- `core/tests/live_agent.rs` has been deleted because I had to update a
bunch of tests and all the tests in here were ignored, and I don't think
anyone ever ran them, so this was just technical debt, at this point.
- Removed `notify_on_sigint()` from `util.rs` (and in a follow-up, I
hope to refactor the blandly-named `util.rs` into more descriptive
files).
- In general, I started replacing local variables named `codex` as
`conversation`, where appropriate, though admittedly I didn't do it
through all the integration tests because that would have added a lot of
noise to this PR.
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/openai/codex/pull/2240).
* #2264
* #2263
* __->__ #2240
2025-08-13 13:38:18 -07:00
|
|
|
use codex_core::ConversationManager;
|
2025-06-28 15:04:23 -07:00
|
|
|
use codex_file_search::FileMatch;
|
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-08-12 17:37:28 -07:00
|
|
|
// Track information about an in-flight exec command.
|
2025-07-31 00:43:21 -07:00
|
|
|
struct RunningCommand {
|
|
|
|
|
command: Vec<String>,
|
2025-08-11 11:26:15 -07:00
|
|
|
parsed_cmd: Vec<ParsedCommand>,
|
2025-07-31 00:43:21 -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
|
|
|
pub(crate) struct ChatWidget<'a> {
|
2025-05-15 14:50:30 -07:00
|
|
|
app_event_tx: AppEventSender,
|
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
|
|
|
codex_op_tx: UnboundedSender<Op>,
|
|
|
|
|
bottom_pane: BottomPane<'a>,
|
2025-08-11 11:43:58 -07:00
|
|
|
active_exec_cell: Option<HistoryCell>,
|
2025-04-27 21:47:50 -07:00
|
|
|
config: Config,
|
2025-05-17 09:00:23 -07:00
|
|
|
initial_user_message: Option<UserMessage>,
|
2025-08-07 05:17:18 -07:00
|
|
|
total_token_usage: TokenUsage,
|
|
|
|
|
last_token_usage: TokenUsage,
|
2025-08-12 17:37:28 -07:00
|
|
|
// Stream lifecycle controller
|
|
|
|
|
stream: StreamController,
|
|
|
|
|
// Track the most recently active stream kind in the current turn
|
|
|
|
|
last_stream_kind: Option<StreamKind>,
|
2025-07-31 00:43:21 -07:00
|
|
|
running_commands: HashMap<String, RunningCommand>,
|
2025-08-13 11:10:48 -07:00
|
|
|
pending_exec_completions: Vec<(Vec<String>, Vec<ParsedCommand>, CommandOutput)>,
|
2025-08-12 17:37:28 -07:00
|
|
|
task_complete_pending: bool,
|
|
|
|
|
// Queue of interruptive UI events deferred during an active write cycle
|
|
|
|
|
interrupts: InterruptManager,
|
|
|
|
|
// Whether a redraw is needed after handling the current event
|
|
|
|
|
needs_redraw: bool,
|
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-05-17 09:00:23 -07:00
|
|
|
struct UserMessage {
|
|
|
|
|
text: String,
|
|
|
|
|
image_paths: Vec<PathBuf>,
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 17:37:28 -07:00
|
|
|
use crate::streaming::StreamKind;
|
2025-08-04 21:23:22 -07:00
|
|
|
|
2025-05-17 09:00:23 -07:00
|
|
|
impl From<String> for UserMessage {
|
|
|
|
|
fn from(text: String) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
text,
|
|
|
|
|
image_paths: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_initial_user_message(text: String, image_paths: Vec<PathBuf>) -> Option<UserMessage> {
|
|
|
|
|
if text.is_empty() && image_paths.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(UserMessage { text, image_paths })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
impl ChatWidget<'_> {
|
2025-08-12 17:37:28 -07:00
|
|
|
#[inline]
|
|
|
|
|
fn mark_needs_redraw(&mut self) {
|
|
|
|
|
self.needs_redraw = true;
|
|
|
|
|
}
|
2025-08-13 11:10:48 -07:00
|
|
|
fn flush_answer_stream_with_separator(&mut self) {
|
|
|
|
|
let sink = AppEventHistorySink(self.app_event_tx.clone());
|
|
|
|
|
let _ = self.stream.finalize(StreamKind::Answer, true, &sink);
|
|
|
|
|
}
|
2025-08-12 17:37:28 -07:00
|
|
|
// --- Small event handlers ---
|
|
|
|
|
fn on_session_configured(&mut self, event: codex_core::protocol::SessionConfiguredEvent) {
|
|
|
|
|
self.bottom_pane
|
|
|
|
|
.set_history_metadata(event.history_log_id, event.history_entry_count);
|
|
|
|
|
self.add_to_history(HistoryCell::new_session_info(&self.config, event, true));
|
|
|
|
|
if let Some(user_message) = self.initial_user_message.take() {
|
|
|
|
|
self.submit_user_message(user_message);
|
|
|
|
|
}
|
|
|
|
|
self.mark_needs_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_agent_message(&mut self, message: String) {
|
|
|
|
|
let sink = AppEventHistorySink(self.app_event_tx.clone());
|
|
|
|
|
let finished = self.stream.apply_final_answer(&message, &sink);
|
2025-08-13 18:39:58 -07:00
|
|
|
self.last_stream_kind = Some(StreamKind::Answer);
|
2025-08-12 17:37:28 -07:00
|
|
|
self.handle_if_stream_finished(finished);
|
|
|
|
|
self.mark_needs_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_agent_message_delta(&mut self, delta: String) {
|
|
|
|
|
self.handle_streaming_delta(StreamKind::Answer, delta);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_agent_reasoning_delta(&mut self, delta: String) {
|
|
|
|
|
self.handle_streaming_delta(StreamKind::Reasoning, delta);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 18:39:58 -07:00
|
|
|
fn on_agent_reasoning_final(&mut self, text: String) {
|
2025-08-12 17:37:28 -07:00
|
|
|
let sink = AppEventHistorySink(self.app_event_tx.clone());
|
2025-08-13 18:39:58 -07:00
|
|
|
let finished = self.stream.apply_final_reasoning(&text, &sink);
|
|
|
|
|
self.last_stream_kind = Some(StreamKind::Reasoning);
|
2025-08-12 17:37:28 -07:00
|
|
|
self.handle_if_stream_finished(finished);
|
|
|
|
|
self.mark_needs_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_reasoning_section_break(&mut self) {
|
|
|
|
|
let sink = AppEventHistorySink(self.app_event_tx.clone());
|
|
|
|
|
self.stream.insert_reasoning_section_break(&sink);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Raw reasoning uses the same flow as summarized reasoning
|
|
|
|
|
|
|
|
|
|
fn on_task_started(&mut self) {
|
|
|
|
|
self.bottom_pane.clear_ctrl_c_quit_hint();
|
|
|
|
|
self.bottom_pane.set_task_running(true);
|
|
|
|
|
self.set_waiting_for_model_status();
|
|
|
|
|
self.stream.reset_headers_for_new_turn();
|
|
|
|
|
self.last_stream_kind = None;
|
|
|
|
|
self.mark_needs_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_task_complete(&mut self) {
|
|
|
|
|
// If a stream is currently active, finalize only that stream to flush any tail
|
|
|
|
|
// without emitting stray headers for other streams.
|
|
|
|
|
if self.stream.is_write_cycle_active() {
|
|
|
|
|
let sink = AppEventHistorySink(self.app_event_tx.clone());
|
|
|
|
|
if let Some(kind) = self.last_stream_kind {
|
|
|
|
|
let _ = self.stream.finalize(kind, true, &sink);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Mark task stopped and request redraw now that all content is in history.
|
|
|
|
|
self.bottom_pane.set_task_running(false);
|
|
|
|
|
self.mark_needs_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_token_count(&mut self, token_usage: TokenUsage) {
|
|
|
|
|
self.total_token_usage = add_token_usage(&self.total_token_usage, &token_usage);
|
|
|
|
|
self.last_token_usage = token_usage;
|
|
|
|
|
self.bottom_pane.set_token_usage(
|
|
|
|
|
self.total_token_usage.clone(),
|
|
|
|
|
self.last_token_usage.clone(),
|
|
|
|
|
self.config.model_context_window,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_error(&mut self, message: String) {
|
|
|
|
|
self.add_to_history(HistoryCell::new_error_event(message));
|
|
|
|
|
self.bottom_pane.set_task_running(false);
|
|
|
|
|
self.stream.clear_all();
|
|
|
|
|
self.mark_needs_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_plan_update(&mut self, update: codex_core::plan_tool::UpdatePlanArgs) {
|
|
|
|
|
self.add_to_history(HistoryCell::new_plan_update(update));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_exec_approval_request(&mut self, id: String, ev: ExecApprovalRequestEvent) {
|
|
|
|
|
let id2 = id.clone();
|
|
|
|
|
let ev2 = ev.clone();
|
|
|
|
|
self.defer_or_handle(
|
|
|
|
|
|q| q.push_exec_approval(id, ev),
|
|
|
|
|
|s| s.handle_exec_approval_now(id2, ev2),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_apply_patch_approval_request(&mut self, id: String, ev: ApplyPatchApprovalRequestEvent) {
|
|
|
|
|
let id2 = id.clone();
|
|
|
|
|
let ev2 = ev.clone();
|
|
|
|
|
self.defer_or_handle(
|
|
|
|
|
|q| q.push_apply_patch_approval(id, ev),
|
|
|
|
|
|s| s.handle_apply_patch_approval_now(id2, ev2),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_exec_command_begin(&mut self, ev: ExecCommandBeginEvent) {
|
2025-08-13 11:10:48 -07:00
|
|
|
self.flush_answer_stream_with_separator();
|
2025-08-12 17:37:28 -07:00
|
|
|
let ev2 = ev.clone();
|
|
|
|
|
self.defer_or_handle(|q| q.push_exec_begin(ev), |s| s.handle_exec_begin_now(ev2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_exec_command_output_delta(
|
|
|
|
|
&mut self,
|
|
|
|
|
_ev: codex_core::protocol::ExecCommandOutputDeltaEvent,
|
|
|
|
|
) {
|
|
|
|
|
// TODO: Handle streaming exec output if/when implemented
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_patch_apply_begin(&mut self, event: PatchApplyBeginEvent) {
|
|
|
|
|
self.add_to_history(HistoryCell::new_patch_event(
|
|
|
|
|
PatchEventType::ApplyBegin {
|
|
|
|
|
auto_approved: event.auto_approved,
|
|
|
|
|
},
|
|
|
|
|
event.changes,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_patch_apply_end(&mut self, event: codex_core::protocol::PatchApplyEndEvent) {
|
|
|
|
|
let ev2 = event.clone();
|
|
|
|
|
self.defer_or_handle(
|
|
|
|
|
|q| q.push_patch_end(event),
|
|
|
|
|
|s| s.handle_patch_apply_end_now(ev2),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_exec_command_end(&mut self, ev: ExecCommandEndEvent) {
|
|
|
|
|
let ev2 = ev.clone();
|
|
|
|
|
self.defer_or_handle(|q| q.push_exec_end(ev), |s| s.handle_exec_end_now(ev2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_mcp_tool_call_begin(&mut self, ev: McpToolCallBeginEvent) {
|
|
|
|
|
let ev2 = ev.clone();
|
|
|
|
|
self.defer_or_handle(|q| q.push_mcp_begin(ev), |s| s.handle_mcp_begin_now(ev2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_mcp_tool_call_end(&mut self, ev: McpToolCallEndEvent) {
|
|
|
|
|
let ev2 = ev.clone();
|
|
|
|
|
self.defer_or_handle(|q| q.push_mcp_end(ev), |s| s.handle_mcp_end_now(ev2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_get_history_entry_response(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: codex_core::protocol::GetHistoryEntryResponseEvent,
|
|
|
|
|
) {
|
|
|
|
|
let codex_core::protocol::GetHistoryEntryResponseEvent {
|
|
|
|
|
offset,
|
|
|
|
|
log_id,
|
|
|
|
|
entry,
|
|
|
|
|
} = event;
|
|
|
|
|
self.bottom_pane
|
|
|
|
|
.on_history_entry_response(log_id, offset, entry.map(|e| e.text));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_shutdown_complete(&mut self) {
|
|
|
|
|
self.app_event_tx.send(AppEvent::ExitRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_turn_diff(&mut self, unified_diff: String) {
|
|
|
|
|
debug!("TurnDiffEvent: {unified_diff}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn on_background_event(&mut self, message: String) {
|
|
|
|
|
debug!("BackgroundEvent: {message}");
|
|
|
|
|
}
|
|
|
|
|
/// Periodic tick to commit at most one queued line to history with a small delay,
|
|
|
|
|
/// animating the output.
|
|
|
|
|
pub(crate) fn on_commit_tick(&mut self) {
|
|
|
|
|
let sink = AppEventHistorySink(self.app_event_tx.clone());
|
|
|
|
|
let finished = self.stream.on_commit_tick(&sink);
|
|
|
|
|
self.handle_if_stream_finished(finished);
|
|
|
|
|
}
|
|
|
|
|
fn is_write_cycle_active(&self) -> bool {
|
|
|
|
|
self.stream.is_write_cycle_active()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flush_interrupt_queue(&mut self) {
|
|
|
|
|
let mut mgr = std::mem::take(&mut self.interrupts);
|
|
|
|
|
mgr.flush_all(self);
|
|
|
|
|
self.interrupts = mgr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn defer_or_handle(
|
|
|
|
|
&mut self,
|
|
|
|
|
push: impl FnOnce(&mut InterruptManager),
|
|
|
|
|
handle: impl FnOnce(&mut Self),
|
|
|
|
|
) {
|
|
|
|
|
// Preserve deterministic FIFO across queued interrupts: once anything
|
|
|
|
|
// is queued due to an active write cycle, continue queueing until the
|
|
|
|
|
// queue is flushed to avoid reordering (e.g., ExecEnd before ExecBegin).
|
|
|
|
|
if self.is_write_cycle_active() || !self.interrupts.is_empty() {
|
|
|
|
|
push(&mut self.interrupts);
|
|
|
|
|
} else {
|
|
|
|
|
handle(self);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn handle_if_stream_finished(&mut self, finished: bool) {
|
|
|
|
|
if finished {
|
|
|
|
|
if self.task_complete_pending {
|
|
|
|
|
self.bottom_pane.set_task_running(false);
|
|
|
|
|
self.task_complete_pending = false;
|
|
|
|
|
}
|
|
|
|
|
self.flush_interrupt_queue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn set_waiting_for_model_status(&mut self) {
|
|
|
|
|
self.bottom_pane
|
|
|
|
|
.update_status_text("waiting for model".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn handle_streaming_delta(&mut self, kind: StreamKind, delta: String) {
|
|
|
|
|
let sink = AppEventHistorySink(self.app_event_tx.clone());
|
|
|
|
|
self.set_waiting_for_model_status();
|
|
|
|
|
self.stream.begin(kind, &sink);
|
|
|
|
|
self.last_stream_kind = Some(kind);
|
|
|
|
|
self.stream.push_and_maybe_commit(&delta, &sink);
|
|
|
|
|
self.mark_needs_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn handle_exec_end_now(&mut self, ev: ExecCommandEndEvent) {
|
|
|
|
|
let running = self.running_commands.remove(&ev.call_id);
|
|
|
|
|
let (command, parsed) = match running {
|
|
|
|
|
Some(rc) => (rc.command, rc.parsed_cmd),
|
|
|
|
|
None => (vec![ev.call_id.clone()], Vec::new()),
|
|
|
|
|
};
|
2025-08-13 11:10:48 -07:00
|
|
|
self.pending_exec_completions.push((
|
2025-08-12 17:37:28 -07:00
|
|
|
command,
|
|
|
|
|
parsed,
|
|
|
|
|
CommandOutput {
|
|
|
|
|
exit_code: ev.exit_code,
|
|
|
|
|
stdout: ev.stdout.clone(),
|
|
|
|
|
stderr: ev.stderr.clone(),
|
|
|
|
|
},
|
|
|
|
|
));
|
2025-08-13 11:10:48 -07:00
|
|
|
|
|
|
|
|
if self.running_commands.is_empty() {
|
|
|
|
|
self.active_exec_cell = None;
|
|
|
|
|
let pending = std::mem::take(&mut self.pending_exec_completions);
|
|
|
|
|
for (command, parsed, output) in pending {
|
|
|
|
|
self.add_to_history(HistoryCell::new_completed_exec_command(
|
|
|
|
|
command, parsed, output,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-12 17:37:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn handle_patch_apply_end_now(
|
|
|
|
|
&mut self,
|
|
|
|
|
event: codex_core::protocol::PatchApplyEndEvent,
|
|
|
|
|
) {
|
|
|
|
|
if event.success {
|
|
|
|
|
self.add_to_history(HistoryCell::new_patch_apply_success(event.stdout));
|
|
|
|
|
} else {
|
|
|
|
|
self.add_to_history(HistoryCell::new_patch_apply_failure(event.stderr));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn handle_exec_approval_now(&mut self, id: String, ev: ExecApprovalRequestEvent) {
|
2025-08-13 11:10:48 -07:00
|
|
|
self.flush_answer_stream_with_separator();
|
2025-08-12 17:37:28 -07:00
|
|
|
// Log a background summary immediately so the history is chronological.
|
|
|
|
|
let cmdline = strip_bash_lc_and_escape(&ev.command);
|
|
|
|
|
let text = format!(
|
|
|
|
|
"command requires approval:\n$ {cmdline}{reason}",
|
|
|
|
|
reason = ev
|
|
|
|
|
.reason
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|r| format!("\n{r}"))
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
);
|
|
|
|
|
self.add_to_history(HistoryCell::new_background_event(text));
|
|
|
|
|
|
|
|
|
|
let request = ApprovalRequest::Exec {
|
|
|
|
|
id,
|
|
|
|
|
command: ev.command,
|
|
|
|
|
reason: ev.reason,
|
|
|
|
|
};
|
|
|
|
|
self.bottom_pane.push_approval_request(request);
|
|
|
|
|
self.mark_needs_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn handle_apply_patch_approval_now(
|
|
|
|
|
&mut self,
|
|
|
|
|
id: String,
|
|
|
|
|
ev: ApplyPatchApprovalRequestEvent,
|
|
|
|
|
) {
|
2025-08-13 11:10:48 -07:00
|
|
|
self.flush_answer_stream_with_separator();
|
2025-08-12 17:37:28 -07:00
|
|
|
self.add_to_history(HistoryCell::new_patch_event(
|
|
|
|
|
PatchEventType::ApprovalRequest,
|
|
|
|
|
ev.changes.clone(),
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
let request = ApprovalRequest::ApplyPatch {
|
|
|
|
|
id,
|
|
|
|
|
reason: ev.reason,
|
|
|
|
|
grant_root: ev.grant_root,
|
|
|
|
|
};
|
|
|
|
|
self.bottom_pane.push_approval_request(request);
|
|
|
|
|
self.mark_needs_redraw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn handle_exec_begin_now(&mut self, ev: ExecCommandBeginEvent) {
|
|
|
|
|
// Ensure the status indicator is visible while the command runs.
|
|
|
|
|
self.bottom_pane
|
|
|
|
|
.update_status_text("running command".to_string());
|
|
|
|
|
self.running_commands.insert(
|
|
|
|
|
ev.call_id.clone(),
|
|
|
|
|
RunningCommand {
|
|
|
|
|
command: ev.command.clone(),
|
|
|
|
|
parsed_cmd: ev.parsed_cmd.clone(),
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-08-13 11:10:48 -07:00
|
|
|
// Accumulate parsed commands into a single active Exec cell so they stack
|
|
|
|
|
match self.active_exec_cell.as_mut() {
|
|
|
|
|
Some(HistoryCell::Exec(exec)) => {
|
|
|
|
|
exec.parsed.extend(ev.parsed_cmd);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
self.active_exec_cell = Some(HistoryCell::new_active_exec_command(
|
|
|
|
|
ev.command,
|
|
|
|
|
ev.parsed_cmd,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Request a redraw so the working header and command list are visible immediately.
|
|
|
|
|
self.mark_needs_redraw();
|
2025-08-12 17:37:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn handle_mcp_begin_now(&mut self, ev: McpToolCallBeginEvent) {
|
2025-08-13 11:10:48 -07:00
|
|
|
self.flush_answer_stream_with_separator();
|
2025-08-12 17:37:28 -07:00
|
|
|
self.add_to_history(HistoryCell::new_active_mcp_tool_call(ev.invocation));
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn handle_mcp_end_now(&mut self, ev: McpToolCallEndEvent) {
|
2025-08-13 11:10:48 -07:00
|
|
|
self.flush_answer_stream_with_separator();
|
2025-08-12 17:37:28 -07:00
|
|
|
self.add_to_history(HistoryCell::new_completed_mcp_tool_call(
|
|
|
|
|
80,
|
|
|
|
|
ev.invocation,
|
|
|
|
|
ev.duration,
|
|
|
|
|
ev.result
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|r| !r.is_error.unwrap_or(false))
|
|
|
|
|
.unwrap_or(false),
|
|
|
|
|
ev.result,
|
|
|
|
|
));
|
|
|
|
|
}
|
2025-08-06 14:56:34 -07:00
|
|
|
fn interrupt_running_task(&mut self) {
|
|
|
|
|
if self.bottom_pane.is_task_running() {
|
2025-08-11 11:43:58 -07:00
|
|
|
self.active_exec_cell = None;
|
2025-08-06 14:56:34 -07:00
|
|
|
self.bottom_pane.clear_ctrl_c_quit_hint();
|
|
|
|
|
self.submit_op(Op::Interrupt);
|
|
|
|
|
self.bottom_pane.set_task_running(false);
|
2025-08-12 17:37:28 -07:00
|
|
|
self.stream.clear_all();
|
2025-08-06 14:56:34 -07:00
|
|
|
self.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-06 12:03:45 -07:00
|
|
|
fn layout_areas(&self, area: Rect) -> [Rect; 2] {
|
|
|
|
|
Layout::vertical([
|
|
|
|
|
Constraint::Max(
|
2025-08-11 11:43:58 -07:00
|
|
|
self.active_exec_cell
|
2025-08-06 12:03:45 -07:00
|
|
|
.as_ref()
|
|
|
|
|
.map_or(0, |c| c.desired_height(area.width)),
|
|
|
|
|
),
|
|
|
|
|
Constraint::Min(self.bottom_pane.desired_height(area.width)),
|
|
|
|
|
])
|
|
|
|
|
.areas(area)
|
|
|
|
|
}
|
2025-08-12 17:37:28 -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
|
|
|
pub(crate) fn new(
|
2025-04-27 21:47:50 -07:00
|
|
|
config: Config,
|
chore: introduce ConversationManager as a clearinghouse for all conversations (#2240)
This PR does two things because after I got deep into the first one I
started pulling on the thread to the second:
- Makes `ConversationManager` the place where all in-memory
conversations are created and stored. Previously, `MessageProcessor` in
the `codex-mcp-server` crate was doing this via its `session_map`, but
this is something that should be done in `codex-core`.
- It unwinds the `ctrl_c: tokio::sync::Notify` that was threaded
throughout our code. I think this made sense at one time, but now that
we handle Ctrl-C within the TUI and have a proper `Op::Interrupt` event,
I don't think this was quite right, so I removed it. For `codex exec`
and `codex proto`, we now use `tokio::signal::ctrl_c()` directly, but we
no longer make `Notify` a field of `Codex` or `CodexConversation`.
Changes of note:
- Adds the files `conversation_manager.rs` and `codex_conversation.rs`
to `codex-core`.
- `Codex` and `CodexSpawnOk` are no longer exported from `codex-core`:
other crates must use `CodexConversation` instead (which is created via
`ConversationManager`).
- `core/src/codex_wrapper.rs` has been deleted in favor of
`ConversationManager`.
- `ConversationManager::new_conversation()` returns `NewConversation`,
which is in line with the `new_conversation` tool we want to add to the
MCP server. Note `NewConversation` includes `SessionConfiguredEvent`, so
we eliminate checks in cases like `codex-rs/core/tests/client.rs` to
verify `SessionConfiguredEvent` is the first event because that is now
internal to `ConversationManager`.
- Quite a bit of code was deleted from
`codex-rs/mcp-server/src/message_processor.rs` since it no longer has to
manage multiple conversations itself: it goes through
`ConversationManager` instead.
- `core/tests/live_agent.rs` has been deleted because I had to update a
bunch of tests and all the tests in here were ignored, and I don't think
anyone ever ran them, so this was just technical debt, at this point.
- Removed `notify_on_sigint()` from `util.rs` (and in a follow-up, I
hope to refactor the blandly-named `util.rs` into more descriptive
files).
- In general, I started replacing local variables named `codex` as
`conversation`, where appropriate, though admittedly I didn't do it
through all the integration tests because that would have added a lot of
noise to this PR.
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/openai/codex/pull/2240).
* #2264
* #2263
* __->__ #2240
2025-08-13 13:38:18 -07:00
|
|
|
conversation_manager: Arc<ConversationManager>,
|
2025-05-15 14:50:30 -07:00
|
|
|
app_event_tx: AppEventSender,
|
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
|
|
|
initial_prompt: Option<String>,
|
2025-05-04 11:12:40 -07:00
|
|
|
initial_images: Vec<PathBuf>,
|
2025-07-31 17:30:44 -07:00
|
|
|
enhanced_keys_supported: bool,
|
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
|
|
|
) -> Self {
|
chore: introduce ConversationManager as a clearinghouse for all conversations (#2240)
This PR does two things because after I got deep into the first one I
started pulling on the thread to the second:
- Makes `ConversationManager` the place where all in-memory
conversations are created and stored. Previously, `MessageProcessor` in
the `codex-mcp-server` crate was doing this via its `session_map`, but
this is something that should be done in `codex-core`.
- It unwinds the `ctrl_c: tokio::sync::Notify` that was threaded
throughout our code. I think this made sense at one time, but now that
we handle Ctrl-C within the TUI and have a proper `Op::Interrupt` event,
I don't think this was quite right, so I removed it. For `codex exec`
and `codex proto`, we now use `tokio::signal::ctrl_c()` directly, but we
no longer make `Notify` a field of `Codex` or `CodexConversation`.
Changes of note:
- Adds the files `conversation_manager.rs` and `codex_conversation.rs`
to `codex-core`.
- `Codex` and `CodexSpawnOk` are no longer exported from `codex-core`:
other crates must use `CodexConversation` instead (which is created via
`ConversationManager`).
- `core/src/codex_wrapper.rs` has been deleted in favor of
`ConversationManager`.
- `ConversationManager::new_conversation()` returns `NewConversation`,
which is in line with the `new_conversation` tool we want to add to the
MCP server. Note `NewConversation` includes `SessionConfiguredEvent`, so
we eliminate checks in cases like `codex-rs/core/tests/client.rs` to
verify `SessionConfiguredEvent` is the first event because that is now
internal to `ConversationManager`.
- Quite a bit of code was deleted from
`codex-rs/mcp-server/src/message_processor.rs` since it no longer has to
manage multiple conversations itself: it goes through
`ConversationManager` instead.
- `core/tests/live_agent.rs` has been deleted because I had to update a
bunch of tests and all the tests in here were ignored, and I don't think
anyone ever ran them, so this was just technical debt, at this point.
- Removed `notify_on_sigint()` from `util.rs` (and in a follow-up, I
hope to refactor the blandly-named `util.rs` into more descriptive
files).
- In general, I started replacing local variables named `codex` as
`conversation`, where appropriate, though admittedly I didn't do it
through all the integration tests because that would have added a lot of
noise to this PR.
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/openai/codex/pull/2240).
* #2264
* #2263
* __->__ #2240
2025-08-13 13:38:18 -07:00
|
|
|
let codex_op_tx = spawn_agent(config.clone(), app_event_tx.clone(), conversation_manager);
|
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-05-17 09:00:23 -07:00
|
|
|
Self {
|
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
|
|
|
app_event_tx: app_event_tx.clone(),
|
|
|
|
|
codex_op_tx,
|
|
|
|
|
bottom_pane: BottomPane::new(BottomPaneParams {
|
|
|
|
|
app_event_tx,
|
|
|
|
|
has_input_focus: true,
|
2025-07-31 17:30:44 -07:00
|
|
|
enhanced_keys_supported,
|
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-08-11 11:43:58 -07:00
|
|
|
active_exec_cell: None,
|
2025-08-12 17:37:28 -07:00
|
|
|
config: config.clone(),
|
2025-05-17 09:00:23 -07:00
|
|
|
initial_user_message: create_initial_user_message(
|
|
|
|
|
initial_prompt.unwrap_or_default(),
|
|
|
|
|
initial_images,
|
|
|
|
|
),
|
2025-08-07 05:17:18 -07:00
|
|
|
total_token_usage: TokenUsage::default(),
|
|
|
|
|
last_token_usage: TokenUsage::default(),
|
2025-08-12 17:37:28 -07:00
|
|
|
stream: StreamController::new(config),
|
|
|
|
|
last_stream_kind: None,
|
2025-07-31 00:43:21 -07:00
|
|
|
running_commands: HashMap::new(),
|
2025-08-13 11:10:48 -07:00
|
|
|
pending_exec_completions: Vec::new(),
|
2025-08-12 17:37:28 -07:00
|
|
|
task_complete_pending: false,
|
|
|
|
|
interrupts: InterruptManager::new(),
|
|
|
|
|
needs_redraw: 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 00:43:21 -07:00
|
|
|
pub fn desired_height(&self, width: u16) -> u16 {
|
|
|
|
|
self.bottom_pane.desired_height(width)
|
2025-08-06 12:03:45 -07:00
|
|
|
+ self
|
2025-08-11 11:43:58 -07:00
|
|
|
.active_exec_cell
|
2025-08-06 12:03:45 -07:00
|
|
|
.as_ref()
|
|
|
|
|
.map_or(0, |c| c.desired_height(width))
|
2025-07-30 17:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-15 14:50:30 -07:00
|
|
|
pub(crate) fn handle_key_event(&mut self, key_event: KeyEvent) {
|
2025-07-31 17:10:52 -07:00
|
|
|
if key_event.kind == KeyEventKind::Press {
|
|
|
|
|
self.bottom_pane.clear_ctrl_c_quit_hint();
|
|
|
|
|
}
|
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-07-27 11:04:09 -07:00
|
|
|
match self.bottom_pane.handle_key_event(key_event) {
|
|
|
|
|
InputResult::Submitted(text) => {
|
|
|
|
|
self.submit_user_message(text.into());
|
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-07-27 11:04:09 -07:00
|
|
|
InputResult::None => {}
|
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-07-12 15:32:00 -07:00
|
|
|
pub(crate) fn handle_paste(&mut self, text: String) {
|
2025-07-27 11:04:09 -07:00
|
|
|
self.bottom_pane.handle_paste(text);
|
2025-07-12 15:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-12 17:37:28 -07:00
|
|
|
fn flush_active_exec_cell(&mut self) {
|
|
|
|
|
if let Some(active) = self.active_exec_cell.take() {
|
|
|
|
|
self.app_event_tx
|
|
|
|
|
.send(AppEvent::InsertHistory(active.plain_lines()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-30 10:05:40 -07:00
|
|
|
fn add_to_history(&mut self, cell: HistoryCell) {
|
2025-08-11 11:43:58 -07:00
|
|
|
self.flush_active_exec_cell();
|
2025-07-30 10:05:40 -07:00
|
|
|
self.app_event_tx
|
|
|
|
|
.send(AppEvent::InsertHistory(cell.plain_lines()));
|
2025-07-25 01:56:40 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-17 09:00:23 -07:00
|
|
|
fn submit_user_message(&mut self, user_message: UserMessage) {
|
|
|
|
|
let UserMessage { text, image_paths } = user_message;
|
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 mut items: Vec<InputItem> = Vec::new();
|
|
|
|
|
|
|
|
|
|
if !text.is_empty() {
|
|
|
|
|
items.push(InputItem::Text { text: text.clone() });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for path in image_paths {
|
|
|
|
|
items.push(InputItem::LocalImage { path });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if items.is_empty() {
|
2025-05-15 14:50:30 -07:00
|
|
|
return;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.codex_op_tx
|
|
|
|
|
.send(Op::UserInput { items })
|
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
|
tracing::error!("failed to send message: {e}");
|
|
|
|
|
});
|
|
|
|
|
|
feat: record messages from user in ~/.codex/history.jsonl (#939)
This is a large change to support a "history" feature like you would
expect in a shell like Bash.
History events are recorded in `$CODEX_HOME/history.jsonl`. Because it
is a JSONL file, it is straightforward to append new entries (as opposed
to the TypeScript file that uses `$CODEX_HOME/history.json`, so to be
valid JSON, each new entry entails rewriting the entire file). Because
it is possible for there to be multiple instances of Codex CLI writing
to `history.jsonl` at once, we use advisory file locking when working
with `history.jsonl` in `codex-rs/core/src/message_history.rs`.
Because we believe history is a sufficiently useful feature, we enable
it by default. Though to provide some safety, we set the file
permissions of `history.jsonl` to be `o600` so that other users on the
system cannot read the user's history. We do not yet support a default
list of `SENSITIVE_PATTERNS` as the TypeScript CLI does:
https://github.com/openai/codex/blob/3fdf9df1335ac9501e3fb0e61715359145711e8b/codex-cli/src/utils/storage/command-history.ts#L10-L17
We are going to take a more conservative approach to this list in the
Rust CLI. For example, while `/\b[A-Za-z0-9-_]{20,}\b/` might exclude
sensitive information like API tokens, it would also exclude valuable
information such as references to Git commits.
As noted in the updated documentation, users can opt-out of history by
adding the following to `config.toml`:
```toml
[history]
persistence = "none"
```
Because `history.jsonl` could, in theory, be quite large, we take a[n
arguably overly pedantic] approach in reading history entries into
memory. Specifically, we start by telling the client the current number
of entries in the history file (`history_entry_count`) as well as the
inode (`history_log_id`) of `history.jsonl` (see the new fields on
`SessionConfiguredEvent`).
The client is responsible for keeping new entries in memory to create a
"local history," but if the user hits up enough times to go "past" the
end of local history, then the client should use the new
`GetHistoryEntryRequest` in the protocol to fetch older entries.
Specifically, it should pass the `history_log_id` it was given
originally and work backwards from `history_entry_count`. (It should
really fetch history in batches rather than one-at-a-time, but that is
something we can improve upon in subsequent PRs.)
The motivation behind this crazy scheme is that it is designed to defend
against:
* The `history.jsonl` being truncated during the session such that the
index into the history is no longer consistent with what had been read
up to that point. We do not yet have logic to enforce a `max_bytes` for
`history.jsonl`, but once we do, we will aspire to implement it in a way
that should result in a new inode for the file on most systems.
* New items from concurrent Codex CLI sessions amending to the history.
Because, in absence of truncation, `history.jsonl` is an append-only
log, so long as the client reads backwards from `history_entry_count`,
it should always get a consistent view of history. (That said, it will
not be able to read _new_ commands from concurrent sessions, but perhaps
we will introduce a `/` command to reload latest history or something
down the road.)
Admittedly, my testing of this feature thus far has been fairly light. I
expect we will find bugs and introduce enhancements/fixes going forward.
2025-05-15 16:26:23 -07:00
|
|
|
// Persist the text to cross-session message history.
|
|
|
|
|
if !text.is_empty() {
|
|
|
|
|
self.codex_op_tx
|
|
|
|
|
.send(Op::AddToHistory { text: text.clone() })
|
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
|
tracing::error!("failed to send AddHistory op: {e}");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 17:37:28 -07:00
|
|
|
// Only show the text portion in conversation history.
|
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 !text.is_empty() {
|
2025-07-30 10:05:40 -07:00
|
|
|
self.add_to_history(HistoryCell::new_user_prompt(text.clone()));
|
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-05-15 14:50:30 -07:00
|
|
|
pub(crate) fn handle_codex_event(&mut self, event: Event) {
|
2025-08-12 17:37:28 -07:00
|
|
|
// Reset redraw flag for this dispatch
|
|
|
|
|
self.needs_redraw = 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
|
|
|
let Event { id, msg } = event;
|
2025-08-11 11:43:58 -07:00
|
|
|
|
|
|
|
|
match msg {
|
|
|
|
|
EventMsg::AgentMessageDelta(_)
|
|
|
|
|
| EventMsg::AgentReasoningDelta(_)
|
|
|
|
|
| EventMsg::ExecCommandOutputDelta(_) => {}
|
|
|
|
|
_ => {
|
|
|
|
|
tracing::info!("handle_codex_event: {:?}", msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
match msg {
|
2025-08-12 17:37:28 -07:00
|
|
|
EventMsg::SessionConfigured(e) => self.on_session_configured(e),
|
|
|
|
|
EventMsg::AgentMessage(AgentMessageEvent { message }) => self.on_agent_message(message),
|
2025-07-16 22:26:31 -07:00
|
|
|
EventMsg::AgentMessageDelta(AgentMessageDeltaEvent { delta }) => {
|
2025-08-12 17:37:28 -07:00
|
|
|
self.on_agent_message_delta(delta)
|
2025-05-10 21:43:27 -07:00
|
|
|
}
|
2025-08-12 17:37:28 -07:00
|
|
|
EventMsg::AgentReasoningDelta(AgentReasoningDeltaEvent { delta })
|
|
|
|
|
| EventMsg::AgentReasoningRawContentDelta(AgentReasoningRawContentDeltaEvent {
|
2025-08-05 01:56:13 -07:00
|
|
|
delta,
|
2025-08-12 17:37:28 -07:00
|
|
|
}) => self.on_agent_reasoning_delta(delta),
|
2025-08-13 18:39:58 -07:00
|
|
|
EventMsg::AgentReasoning(AgentReasoningEvent { text })
|
|
|
|
|
| EventMsg::AgentReasoningRawContent(AgentReasoningRawContentEvent { text }) => {
|
|
|
|
|
self.on_agent_reasoning_final(text)
|
2025-08-12 17:37:28 -07:00
|
|
|
}
|
|
|
|
|
EventMsg::AgentReasoningSectionBreak(_) => self.on_reasoning_section_break(),
|
|
|
|
|
EventMsg::TaskStarted => self.on_task_started(),
|
|
|
|
|
EventMsg::TaskComplete(TaskCompleteEvent { .. }) => self.on_task_complete(),
|
|
|
|
|
EventMsg::TokenCount(token_usage) => self.on_token_count(token_usage),
|
|
|
|
|
EventMsg::Error(ErrorEvent { message }) => self.on_error(message),
|
|
|
|
|
EventMsg::PlanUpdate(update) => self.on_plan_update(update),
|
|
|
|
|
EventMsg::ExecApprovalRequest(ev) => self.on_exec_approval_request(id, ev),
|
|
|
|
|
EventMsg::ApplyPatchApprovalRequest(ev) => self.on_apply_patch_approval_request(id, ev),
|
|
|
|
|
EventMsg::ExecCommandBegin(ev) => self.on_exec_command_begin(ev),
|
|
|
|
|
EventMsg::ExecCommandOutputDelta(delta) => self.on_exec_command_output_delta(delta),
|
|
|
|
|
EventMsg::PatchApplyBegin(ev) => self.on_patch_apply_begin(ev),
|
|
|
|
|
EventMsg::PatchApplyEnd(ev) => self.on_patch_apply_end(ev),
|
|
|
|
|
EventMsg::ExecCommandEnd(ev) => self.on_exec_command_end(ev),
|
|
|
|
|
EventMsg::McpToolCallBegin(ev) => self.on_mcp_tool_call_begin(ev),
|
|
|
|
|
EventMsg::McpToolCallEnd(ev) => self.on_mcp_tool_call_end(ev),
|
|
|
|
|
EventMsg::GetHistoryEntryResponse(ev) => self.on_get_history_entry_response(ev),
|
|
|
|
|
EventMsg::ShutdownComplete => self.on_shutdown_complete(),
|
|
|
|
|
EventMsg::TurnDiff(TurnDiffEvent { unified_diff }) => self.on_turn_diff(unified_diff),
|
2025-08-05 22:44:27 -07:00
|
|
|
EventMsg::BackgroundEvent(BackgroundEventEvent { message }) => {
|
2025-08-12 17:37:28 -07:00
|
|
|
self.on_background_event(message)
|
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-08-12 17:37:28 -07:00
|
|
|
// Coalesce redraws: issue at most one after handling the event
|
|
|
|
|
if self.needs_redraw {
|
|
|
|
|
self.request_redraw();
|
|
|
|
|
self.needs_redraw = 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update the live log preview while a task is running.
|
2025-05-15 14:50:30 -07:00
|
|
|
pub(crate) fn update_latest_log(&mut self, line: String) {
|
2025-08-05 01:56:13 -07:00
|
|
|
if self.bottom_pane.is_task_running() {
|
|
|
|
|
self.bottom_pane.update_status_text(line);
|
|
|
|
|
}
|
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-05-15 14:50:30 -07:00
|
|
|
fn request_redraw(&mut self) {
|
2025-07-17 12:54:55 -07:00
|
|
|
self.app_event_tx.send(AppEvent::RequestRedraw);
|
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-06-26 13:03:31 -07:00
|
|
|
pub(crate) fn add_diff_output(&mut self, diff_output: String) {
|
2025-07-30 10:05:40 -07:00
|
|
|
self.add_to_history(HistoryCell::new_diff_output(diff_output.clone()));
|
2025-04-25 12:01:52 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-05 23:57:52 -07:00
|
|
|
pub(crate) fn add_status_output(&mut self) {
|
|
|
|
|
self.add_to_history(HistoryCell::new_status_output(
|
|
|
|
|
&self.config,
|
2025-08-07 05:17:18 -07:00
|
|
|
&self.total_token_usage,
|
2025-08-05 23:57:52 -07:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-07 03:55:59 -07:00
|
|
|
pub(crate) fn add_prompts_output(&mut self) {
|
|
|
|
|
self.add_to_history(HistoryCell::new_prompts_output());
|
|
|
|
|
}
|
|
|
|
|
|
feat: add support for @ to do file search (#1401)
Introduces support for `@` to trigger a fuzzy-filename search in the
composer. Under the hood, this leverages
https://crates.io/crates/nucleo-matcher to do the fuzzy matching and
https://crates.io/crates/ignore to build up the list of file candidates
(so that it respects `.gitignore`).
For simplicity (at least for now), we do not do any caching between
searches like VS Code does for its file search:
https://github.com/microsoft/vscode/blob/1d89ed699b2e924d418c856318a3e12bca67ff3a/src/vs/workbench/services/search/node/rawSearchService.ts#L212-L218
Because we do not do any caching, I saw queries take up to three seconds
on large repositories with hundreds of thousands of files. To that end,
we do not perform searches synchronously on each keystroke, but instead
dispatch an event to do the search on a background thread that
asynchronously reports back to the UI when the results are available.
This is largely handled by the `FileSearchManager` introduced in this
PR, which also has logic for debouncing requests so there is at most one
search in flight at a time.
While we could potentially polish and tune this feature further, it may
already be overengineered for how it will be used, in practice, so we
can improve things going forward if it turns out that this is not "good
enough" in the wild.
Note this feature does not work like `@` in the TypeScript CLI, which
was more like directory-based tab completion. In the Rust CLI, `@`
triggers a full-repo fuzzy-filename search.
Fixes https://github.com/openai/codex/issues/1261.
2025-06-28 13:47:42 -07:00
|
|
|
/// Forward file-search results to the bottom pane.
|
2025-06-28 15:04:23 -07:00
|
|
|
pub(crate) fn apply_file_search_result(&mut self, query: String, matches: Vec<FileMatch>) {
|
feat: add support for @ to do file search (#1401)
Introduces support for `@` to trigger a fuzzy-filename search in the
composer. Under the hood, this leverages
https://crates.io/crates/nucleo-matcher to do the fuzzy matching and
https://crates.io/crates/ignore to build up the list of file candidates
(so that it respects `.gitignore`).
For simplicity (at least for now), we do not do any caching between
searches like VS Code does for its file search:
https://github.com/microsoft/vscode/blob/1d89ed699b2e924d418c856318a3e12bca67ff3a/src/vs/workbench/services/search/node/rawSearchService.ts#L212-L218
Because we do not do any caching, I saw queries take up to three seconds
on large repositories with hundreds of thousands of files. To that end,
we do not perform searches synchronously on each keystroke, but instead
dispatch an event to do the search on a background thread that
asynchronously reports back to the UI when the results are available.
This is largely handled by the `FileSearchManager` introduced in this
PR, which also has logic for debouncing requests so there is at most one
search in flight at a time.
While we could potentially polish and tune this feature further, it may
already be overengineered for how it will be used, in practice, so we
can improve things going forward if it turns out that this is not "good
enough" in the wild.
Note this feature does not work like `@` in the TypeScript CLI, which
was more like directory-based tab completion. In the Rust CLI, `@`
triggers a full-repo fuzzy-filename search.
Fixes https://github.com/openai/codex/issues/1261.
2025-06-28 13:47:42 -07:00
|
|
|
self.bottom_pane.on_file_search_result(query, matches);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 13:37:11 -04:00
|
|
|
/// Handle Ctrl-C key press.
|
2025-07-28 12:00:06 -07:00
|
|
|
/// Returns CancellationEvent::Handled if the event was consumed by the UI, or
|
|
|
|
|
/// CancellationEvent::Ignored if the caller should handle it (e.g. exit).
|
|
|
|
|
pub(crate) fn on_ctrl_c(&mut self) -> CancellationEvent {
|
|
|
|
|
match self.bottom_pane.on_ctrl_c() {
|
|
|
|
|
CancellationEvent::Handled => return CancellationEvent::Handled,
|
|
|
|
|
CancellationEvent::Ignored => {}
|
|
|
|
|
}
|
2025-06-27 13:37:11 -04:00
|
|
|
if self.bottom_pane.is_task_running() {
|
2025-08-06 14:56:34 -07:00
|
|
|
self.interrupt_running_task();
|
2025-07-28 12:00:06 -07:00
|
|
|
CancellationEvent::Ignored
|
2025-06-27 13:37:11 -04:00
|
|
|
} else if self.bottom_pane.ctrl_c_quit_hint_visible() {
|
2025-07-23 15:03:26 -07:00
|
|
|
self.submit_op(Op::Shutdown);
|
2025-07-28 12:00:06 -07:00
|
|
|
CancellationEvent::Handled
|
2025-06-27 13:37:11 -04:00
|
|
|
} else {
|
|
|
|
|
self.bottom_pane.show_ctrl_c_quit_hint();
|
2025-07-28 12:00:06 -07:00
|
|
|
CancellationEvent::Ignored
|
2025-06-27 13:37:11 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 08:59:26 -07:00
|
|
|
pub(crate) fn composer_is_empty(&self) -> bool {
|
|
|
|
|
self.bottom_pane.composer_is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 17:37:28 -07:00
|
|
|
pub(crate) fn insert_str(&mut self, text: &str) {
|
|
|
|
|
self.bottom_pane.insert_str(text);
|
|
|
|
|
}
|
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
|
|
|
/// Forward an `Op` directly to codex.
|
|
|
|
|
pub(crate) fn submit_op(&self, op: Op) {
|
2025-08-12 17:37:28 -07:00
|
|
|
// Record outbound operation for session replay fidelity.
|
|
|
|
|
crate::session_log::log_outbound_op(&op);
|
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 let Err(e) = self.codex_op_tx.send(op) {
|
|
|
|
|
tracing::error!("failed to submit op: {e}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-25 01:56:40 -07:00
|
|
|
|
2025-08-06 09:10:23 -07:00
|
|
|
/// Programmatically submit a user text message as if typed in the
|
|
|
|
|
/// composer. The text will be added to conversation history and sent to
|
|
|
|
|
/// the agent.
|
|
|
|
|
pub(crate) fn submit_text_message(&mut self, text: String) {
|
|
|
|
|
if text.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.submit_user_message(text.into());
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 01:56:40 -07:00
|
|
|
pub(crate) fn token_usage(&self) -> &TokenUsage {
|
2025-08-07 05:17:18 -07:00
|
|
|
&self.total_token_usage
|
2025-07-25 01:56:40 -07:00
|
|
|
}
|
2025-07-31 21:34:32 -07:00
|
|
|
|
|
|
|
|
pub(crate) fn clear_token_usage(&mut self) {
|
2025-08-07 05:17:18 -07:00
|
|
|
self.total_token_usage = TokenUsage::default();
|
|
|
|
|
self.bottom_pane.set_token_usage(
|
|
|
|
|
self.total_token_usage.clone(),
|
|
|
|
|
self.last_token_usage.clone(),
|
|
|
|
|
self.config.model_context_window,
|
|
|
|
|
);
|
2025-07-31 21:34:32 -07:00
|
|
|
}
|
2025-08-03 11:31:35 -07:00
|
|
|
|
|
|
|
|
pub fn cursor_pos(&self, area: Rect) -> Option<(u16, u16)> {
|
2025-08-06 12:03:45 -07:00
|
|
|
let [_, bottom_pane_area] = self.layout_areas(area);
|
|
|
|
|
self.bottom_pane.cursor_pos(bottom_pane_area)
|
2025-08-03 11:31:35 -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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WidgetRef for &ChatWidget<'_> {
|
|
|
|
|
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
|
2025-08-06 12:03:45 -07:00
|
|
|
let [active_cell_area, bottom_pane_area] = self.layout_areas(area);
|
|
|
|
|
(&self.bottom_pane).render(bottom_pane_area, buf);
|
2025-08-11 11:43:58 -07:00
|
|
|
if let Some(cell) = &self.active_exec_cell {
|
2025-08-06 12:03:45 -07:00
|
|
|
cell.render_ref(active_cell_area, buf);
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
}
|
feat: show number of tokens remaining in UI (#1388)
When using the OpenAI Responses API, we now record the `usage` field for
a `"response.completed"` event, which includes metrics about the number
of tokens consumed. We also introduce `openai_model_info.rs`, which
includes current data about the most common OpenAI models available via
the API (specifically `context_window` and `max_output_tokens`). If
Codex does not recognize the model, you can set `model_context_window`
and `model_max_output_tokens` explicitly in `config.toml`.
When then introduce a new event type to `protocol.rs`, `TokenCount`,
which includes the `TokenUsage` for the most recent turn.
Finally, we update the TUI to record the running sum of tokens used so
the percentage of available context window remaining can be reported via
the placeholder text for the composer:

We could certainly get much fancier with this (such as reporting the
estimated cost of the conversation), but for now, we are just trying to
achieve feature parity with the TypeScript CLI.
Though arguably this improves upon the TypeScript CLI, as the TypeScript
CLI uses heuristics to estimate the number of tokens used rather than
using the `usage` information directly:
https://github.com/openai/codex/blob/296996d74e345b1b05d8c3451a06ace21c5ada96/codex-cli/src/utils/approximate-tokens-used.ts#L3-L16
Fixes https://github.com/openai/codex/issues/1242
2025-06-25 23:31:11 -07:00
|
|
|
|
|
|
|
|
fn add_token_usage(current_usage: &TokenUsage, new_usage: &TokenUsage) -> TokenUsage {
|
|
|
|
|
let cached_input_tokens = match (
|
|
|
|
|
current_usage.cached_input_tokens,
|
|
|
|
|
new_usage.cached_input_tokens,
|
|
|
|
|
) {
|
|
|
|
|
(Some(current), Some(new)) => Some(current + new),
|
|
|
|
|
(Some(current), None) => Some(current),
|
|
|
|
|
(None, Some(new)) => Some(new),
|
|
|
|
|
(None, None) => None,
|
|
|
|
|
};
|
|
|
|
|
let reasoning_output_tokens = match (
|
|
|
|
|
current_usage.reasoning_output_tokens,
|
|
|
|
|
new_usage.reasoning_output_tokens,
|
|
|
|
|
) {
|
|
|
|
|
(Some(current), Some(new)) => Some(current + new),
|
|
|
|
|
(Some(current), None) => Some(current),
|
|
|
|
|
(None, Some(new)) => Some(new),
|
|
|
|
|
(None, None) => None,
|
|
|
|
|
};
|
|
|
|
|
TokenUsage {
|
|
|
|
|
input_tokens: current_usage.input_tokens + new_usage.input_tokens,
|
|
|
|
|
cached_input_tokens,
|
|
|
|
|
output_tokens: current_usage.output_tokens + new_usage.output_tokens,
|
|
|
|
|
reasoning_output_tokens,
|
|
|
|
|
total_tokens: current_usage.total_tokens + new_usage.total_tokens,
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-11 12:40:12 -07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2025-08-12 17:37:28 -07:00
|
|
|
mod tests;
|