mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
## Summary - wrap OSC 9 notifications in tmux's DCS passthrough so terminal notifications make it through tmux - use codex-terminal-detection for OSC 9 auto-selection so tmux sessions inherit the underlying client terminal support - add focused notification backend tests for plain OSC 9 and tmux-wrapped output ## Stack - base PR: #18479 - review order: #18479, then this PR ## Why Tmux does not forward OSC 9 notifications directly; the sequence has to be wrapped in tmux's DCS passthrough envelope. Codex also had local notification heuristics that could miss supported terminals when running under tmux, even though codex-terminal-detection already knows how to attribute tmux sessions to the client terminal. ## Validation - `just fmt` - `cargo test -p codex-tui` *(currently blocked by an unrelated existing compile error in `app-server/src/message_processor.rs:754` referencing `connection_id` out of scope; not caused by this change)* Co-authored-by: Codex <noreply@openai.com>
128 lines
3.2 KiB
Rust
128 lines
3.2 KiB
Rust
use std::fmt;
|
|
use std::io;
|
|
use std::io::stdout;
|
|
|
|
use codex_terminal_detection::Multiplexer;
|
|
use codex_terminal_detection::terminal_info;
|
|
use crossterm::Command;
|
|
use ratatui::crossterm::execute;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Osc9Backend {
|
|
dcs_passthrough: bool,
|
|
}
|
|
|
|
impl Default for Osc9Backend {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl Osc9Backend {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
dcs_passthrough: matches!(terminal_info().multiplexer, Some(Multiplexer::Tmux { .. })),
|
|
}
|
|
}
|
|
|
|
pub fn notify(&mut self, message: &str) -> io::Result<()> {
|
|
execute!(
|
|
stdout(),
|
|
PostNotification {
|
|
message: message.to_string(),
|
|
dcs_passthrough: self.dcs_passthrough,
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Command that emits an OSC 9 desktop notification with a message.
|
|
#[derive(Debug, Clone)]
|
|
pub struct PostNotification {
|
|
pub message: String,
|
|
pub dcs_passthrough: bool,
|
|
}
|
|
|
|
impl Command for PostNotification {
|
|
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
|
|
if self.dcs_passthrough {
|
|
let escaped_message = escape_tmux_dcs_passthrough_payload(&self.message);
|
|
write!(f, "\x1bPtmux;\x1b\x1b]9;{escaped_message}\x07\x1b\\")
|
|
} else {
|
|
write!(f, "\x1b]9;{}\x07", self.message)
|
|
}
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
fn execute_winapi(&self) -> io::Result<()> {
|
|
Err(std::io::Error::other(
|
|
"tried to execute PostNotification using WinAPI; use ANSI instead",
|
|
))
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
fn is_ansi_code_supported(&self) -> bool {
|
|
true
|
|
}
|
|
}
|
|
|
|
fn escape_tmux_dcs_passthrough_payload(message: &str) -> String {
|
|
message.replace('\u{1b}', "\u{1b}\u{1b}")
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crossterm::Command;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use super::PostNotification;
|
|
|
|
#[test]
|
|
fn post_notification_writes_plain_osc9_sequence() {
|
|
let mut ansi = String::new();
|
|
let command = PostNotification {
|
|
message: "hello".to_string(),
|
|
dcs_passthrough: false,
|
|
};
|
|
|
|
command
|
|
.write_ansi(&mut ansi)
|
|
.expect("OSC 9 command should format");
|
|
|
|
assert_eq!(ansi, "\u{1b}]9;hello\u{7}");
|
|
}
|
|
|
|
#[test]
|
|
fn post_notification_writes_tmux_dcs_wrapped_osc9_sequence() {
|
|
let mut ansi = String::new();
|
|
let command = PostNotification {
|
|
message: "done".to_string(),
|
|
dcs_passthrough: true,
|
|
};
|
|
|
|
command
|
|
.write_ansi(&mut ansi)
|
|
.expect("OSC 9 command should format");
|
|
|
|
assert_eq!(ansi, "\u{1b}Ptmux;\u{1b}\u{1b}]9;done\u{7}\u{1b}\\");
|
|
}
|
|
|
|
#[test]
|
|
fn post_notification_escapes_escape_bytes_inside_tmux_payload() {
|
|
let mut ansi = String::new();
|
|
let command = PostNotification {
|
|
message: "danger\u{1b}[31m".to_string(),
|
|
dcs_passthrough: true,
|
|
};
|
|
|
|
command
|
|
.write_ansi(&mut ansi)
|
|
.expect("OSC 9 command should format");
|
|
|
|
assert_eq!(
|
|
ansi,
|
|
"\u{1b}Ptmux;\u{1b}\u{1b}]9;danger\u{1b}\u{1b}[31m\u{7}\u{1b}\\"
|
|
);
|
|
}
|
|
}
|