Files
codex/codex-rs/tui/src/ide_context.rs
Eric Traut 6784db51c0 Add /ide context support to the TUI (#20294)
## 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.
2026-05-01 09:39:48 -07:00

118 lines
3.2 KiB
Rust

//! IDE context data model and public helpers for TUI `/ide` support.
mod ipc;
mod prompt;
#[cfg(windows)]
mod windows_pipe;
pub(crate) use ipc::fetch_ide_context;
pub(crate) use prompt::apply_ide_context_to_user_input;
pub(crate) use prompt::extract_prompt_request_with_offset;
pub(crate) use prompt::has_prompt_context;
use serde::Deserialize;
#[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub(crate) struct IdeContext {
active_file: Option<ActiveFile>,
#[serde(default)]
open_tabs: Vec<FileDescriptor>,
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct ActiveFile {
#[serde(flatten)]
descriptor: FileDescriptor,
selection: Range,
#[serde(default)]
active_selection_content: String,
#[serde(default)]
selections: Vec<Range>,
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct FileDescriptor {
label: String,
path: String,
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
struct Range {
start: Position,
end: Position,
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
struct Position {
line: u32,
character: u32,
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use serde_json::json;
#[test]
fn deserializes_existing_ide_context_shape() {
let value = json!({
"activeFile": {
"label": "lib.rs",
"path": "src/lib.rs",
"fsPath": "/repo/src/lib.rs",
"selection": {
"start": { "line": 1, "character": 2 },
"end": { "line": 3, "character": 4 }
},
"activeSelectionContent": "selected",
"selections": []
},
"openTabs": [
{
"label": "main.rs",
"path": "src/main.rs",
"fsPath": "/repo/src/main.rs",
"startLine": 2,
"endLine": 10
}
],
"processEnv": {
"path": "/usr/bin"
}
});
let context: IdeContext = serde_json::from_value(value).expect("deserialize ide context");
assert_eq!(
context,
IdeContext {
active_file: Some(ActiveFile {
descriptor: FileDescriptor {
label: "lib.rs".to_string(),
path: "src/lib.rs".to_string(),
},
selection: Range {
start: Position {
line: 1,
character: 2,
},
end: Position {
line: 3,
character: 4,
},
},
active_selection_content: "selected".to_string(),
selections: Vec::new(),
}),
open_tabs: vec![FileDescriptor {
label: "main.rs".to_string(),
path: "src/main.rs".to_string(),
}],
}
);
}
}