mirror of
https://github.com/openai/codex.git
synced 2026-05-24 04:54:52 +00:00
## Why Users have asked for a `/ide` command in the TUI so Codex can use the active IDE session for live context such as the current file, open tabs, and selected ranges. We already support a similar feature in the Codex desktop app, so bringing it to the TUI makes sense. One subtle compatibility constraint is that the injected prompt wrapper and transcript stripping should match the desktop app and IDE extension. By using the same `## My request for Codex:` delimiter and hiding the injected context from transcript rendering the same way, threads created in the TUI render correctly in desktop and IDE surfaces, and threads created there replay correctly in the TUI, even when IDE context was included. Addresses https://github.com/openai/codex/issues/13834. ## What changed ### Summary This PR consists of four four pieces: 1. An IPC client that uses a socket (Mac/Linux) or named pipe (Windows) to talk to the IDE Extension 2. Logic that establishes the IPC connection and requests IDE context (open files, selection) on demand 3. Logic that injects this context into the user prompt (using the same technique as the desktop app) and hides the added context when rendering the prompt in the TUI transcript 4. A new slash command for enabling/disabling this mode and text within the footer to indicate when it's enabled ### Details - Added `/ide [on|off|status]` to the TUI, with bare `/ide` toggling IDE context on or off. - Added a Rust IDE context client that connects to the local Codex IDE IPC route as a client and requests context from the IDE extension flow. - Injected IDE context using the same prompt delimiter and transcript-stripping convention as the desktop app and IDE extension so shared threads render consistently across surfaces. - Added an `IDE context` status-line indicator while the feature is active and cleared it when enabling or fetching context fails. - Added handling for multiple selection ranges, oversized selections, interleaved IPC messages, and transient reconnect timing after quick toggles. ## Verification Did extensive manual testing in addition to running automated unit and regression tests. To test: - Launch VS Code (or Cursor) with the IDE extension. - Open one or more files in the IDE and select a range of text within one of them. - Start the TUI. - Ask the agent which files you have open in your IDE, and it should say that it does not know. - Enable `/ide` mode; note that `IDE context` appears in the lower right. - Ask the agent what files you have open in your IDE and what text is selected.
133 lines
4.4 KiB
Rust
133 lines
4.4 KiB
Rust
//! Chat-widget wiring for the `/ide` command and IDE context prompt injection.
|
|
|
|
use codex_app_server_protocol::UserInput;
|
|
|
|
use super::ChatWidget;
|
|
|
|
#[derive(Default)]
|
|
pub(super) struct IdeContextState {
|
|
enabled: bool,
|
|
prompt_fetch_warned: bool,
|
|
}
|
|
|
|
impl IdeContextState {
|
|
pub(super) fn is_enabled(&self) -> bool {
|
|
self.enabled
|
|
}
|
|
|
|
fn enable(&mut self) {
|
|
self.enabled = true;
|
|
self.prompt_fetch_warned = false;
|
|
}
|
|
|
|
fn disable(&mut self) {
|
|
self.enabled = false;
|
|
self.prompt_fetch_warned = false;
|
|
}
|
|
|
|
fn mark_available(&mut self) {
|
|
self.prompt_fetch_warned = false;
|
|
}
|
|
}
|
|
|
|
impl ChatWidget {
|
|
pub(super) fn handle_ide_command(&mut self) {
|
|
if self.ide_context.is_enabled() {
|
|
self.ide_context.disable();
|
|
self.sync_ide_context_status_indicator();
|
|
self.add_info_message("IDE context is off.".to_string(), /*hint*/ None);
|
|
} else {
|
|
self.ide_context.enable();
|
|
self.add_ide_context_status_message();
|
|
}
|
|
}
|
|
|
|
pub(super) fn handle_ide_command_args(&mut self, args: &str) {
|
|
match args.to_ascii_lowercase().as_str() {
|
|
"" => self.handle_ide_command(),
|
|
"on" => {
|
|
self.ide_context.enable();
|
|
self.add_ide_context_status_message();
|
|
}
|
|
"off" => {
|
|
self.ide_context.disable();
|
|
self.sync_ide_context_status_indicator();
|
|
self.add_info_message("IDE context is off.".to_string(), /*hint*/ None);
|
|
}
|
|
"status" => {
|
|
self.add_ide_context_status_message();
|
|
}
|
|
_ => {
|
|
self.add_error_message("Usage: /ide [on|off|status]".to_string());
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Fetches fresh IDE context for the outgoing user turn and folds it into the prompt.
|
|
pub(super) fn maybe_apply_ide_context(&mut self, items: &mut Vec<UserInput>) {
|
|
if !self.ide_context.is_enabled() {
|
|
return;
|
|
}
|
|
|
|
match crate::ide_context::fetch_ide_context(&self.config.cwd) {
|
|
Ok(context) => {
|
|
self.ide_context.mark_available();
|
|
self.sync_ide_context_status_indicator();
|
|
crate::ide_context::apply_ide_context_to_user_input(&context, items);
|
|
}
|
|
Err(err) => {
|
|
self.sync_ide_context_status_indicator();
|
|
if !self.ide_context.prompt_fetch_warned {
|
|
self.ide_context.prompt_fetch_warned = true;
|
|
self.add_info_message(
|
|
"IDE context was skipped for this message.".to_string(),
|
|
Some(err.prompt_skip_hint()),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn add_ide_context_status_message(&mut self) {
|
|
if !self.ide_context.is_enabled() {
|
|
self.sync_ide_context_status_indicator();
|
|
self.add_info_message("IDE context is off.".to_string(), /*hint*/ None);
|
|
return;
|
|
}
|
|
|
|
match crate::ide_context::fetch_ide_context(&self.config.cwd) {
|
|
Ok(context) => {
|
|
self.ide_context.mark_available();
|
|
self.sync_ide_context_status_indicator();
|
|
if crate::ide_context::has_prompt_context(&context) {
|
|
self.add_info_message(
|
|
"IDE context is on.".to_string(),
|
|
Some(
|
|
"Future messages will include your current IDE selection and open tabs."
|
|
.to_string(),
|
|
),
|
|
);
|
|
} else {
|
|
self.add_info_message(
|
|
"IDE context is on.".to_string(),
|
|
Some("Connected to your IDE.".to_string()),
|
|
);
|
|
}
|
|
}
|
|
Err(err) => {
|
|
self.ide_context.disable();
|
|
self.sync_ide_context_status_indicator();
|
|
self.add_info_message(
|
|
"IDE context could not be enabled.".to_string(),
|
|
Some(err.user_facing_hint()),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) fn sync_ide_context_status_indicator(&mut self) {
|
|
self.bottom_pane
|
|
.set_ide_context_active(self.ide_context.is_enabled());
|
|
}
|
|
}
|