Base: codex-main originale (prima del fork aKode)

This commit is contained in:
Andrea
2026-11-09 10:37:42 +01:00
commit 7874033c81
10166 changed files with 2728937 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
use std::fmt;
use std::io;
use std::io::stdout;
use crossterm::Command;
use ratatui::crossterm::execute;
#[derive(Debug, Default)]
pub struct BelBackend;
impl BelBackend {
pub fn notify(&mut self, _message: &str) -> io::Result<()> {
execute!(stdout(), PostNotification)
}
}
/// Command that emits a BEL desktop notification.
#[derive(Debug, Clone)]
pub struct PostNotification;
impl Command for PostNotification {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
write!(f, "\x07")
}
#[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
}
}

View File

@@ -0,0 +1,136 @@
mod bel;
mod osc;
use std::io;
use bel::BelBackend;
use codex_config::types::NotificationMethod;
use codex_terminal_detection::TerminalInfo;
use codex_terminal_detection::TerminalName;
use codex_terminal_detection::terminal_info;
use osc::OscBackend;
use osc::OscProtocol;
#[derive(Debug)]
pub enum DesktopNotificationBackend {
Osc(OscBackend),
Bel(BelBackend),
}
impl DesktopNotificationBackend {
pub fn for_method(method: NotificationMethod) -> Self {
match method {
NotificationMethod::Auto => {
if supports_osc9(&terminal_info()) {
Self::Osc(OscBackend::new(OscProtocol::Osc9))
} else {
Self::Bel(BelBackend)
}
}
NotificationMethod::Osc9 => Self::Osc(OscBackend::new(OscProtocol::Osc9)),
NotificationMethod::Osc777 => Self::Osc(OscBackend::new(OscProtocol::Osc777)),
NotificationMethod::Bel => Self::Bel(BelBackend),
}
}
pub fn method(&self) -> NotificationMethod {
match self {
DesktopNotificationBackend::Osc(backend) => match backend.protocol {
OscProtocol::Osc9 => NotificationMethod::Osc9,
OscProtocol::Osc777 => NotificationMethod::Osc777,
},
DesktopNotificationBackend::Bel(_) => NotificationMethod::Bel,
}
}
pub fn notify(&mut self, message: &str) -> io::Result<()> {
match self {
DesktopNotificationBackend::Osc(backend) => backend.notify(message),
DesktopNotificationBackend::Bel(backend) => backend.notify(message),
}
}
}
pub fn detect_backend(method: NotificationMethod) -> DesktopNotificationBackend {
DesktopNotificationBackend::for_method(method)
}
fn supports_osc9(terminal: &TerminalInfo) -> bool {
matches!(
terminal.name,
TerminalName::Ghostty
| TerminalName::Iterm2
| TerminalName::Kitty
| TerminalName::WarpTerminal
| TerminalName::WezTerm
)
}
#[cfg(test)]
mod tests {
use super::detect_backend;
use super::supports_osc9;
use codex_config::types::NotificationMethod;
use codex_terminal_detection::TerminalInfo;
use codex_terminal_detection::TerminalName;
use pretty_assertions::assert_eq;
fn test_terminal(name: TerminalName) -> TerminalInfo {
TerminalInfo {
backend: None,
name,
term_program: None,
version: None,
term: None,
multiplexer: None,
}
}
#[test]
fn selects_explicit_notification_method() {
for method in [
NotificationMethod::Osc9,
NotificationMethod::Osc777,
NotificationMethod::Bel,
] {
assert_eq!(detect_backend(method).method(), method);
}
}
#[test]
fn supports_osc9_for_supported_terminals() {
for name in [
TerminalName::Ghostty,
TerminalName::Iterm2,
TerminalName::Kitty,
TerminalName::WarpTerminal,
TerminalName::WezTerm,
] {
assert!(
supports_osc9(&test_terminal(name)),
"{name:?} should support OSC 9"
);
}
}
#[test]
fn supports_osc9_for_unsupported_terminals() {
for name in [
TerminalName::AppleTerminal,
TerminalName::Alacritty,
TerminalName::Dumb,
TerminalName::GnomeTerminal,
TerminalName::Konsole,
TerminalName::Unknown,
TerminalName::VsCode,
TerminalName::Vte,
TerminalName::WindowsTerminal,
] {
assert_eq!(
supports_osc9(&test_terminal(name)),
false,
"{name:?} should not support OSC 9"
);
}
}
}

View File

@@ -0,0 +1,210 @@
//! Writes terminal desktop notifications, wrapping the OSC sequence for tmux when needed.
use std::fmt;
use std::io;
use std::io::stdout;
use codex_terminal_detection::Multiplexer;
use codex_terminal_detection::TerminalName;
use codex_terminal_detection::terminal_info;
use crossterm::Command;
use ratatui::crossterm::execute;
// Ghostty copies the parsed notification body into a 255-byte buffer.
const GHOSTTY_BODY_BYTES: usize = 255;
#[derive(Debug, Clone, Copy)]
pub(super) enum OscProtocol {
Osc9,
Osc777,
}
#[derive(Debug)]
pub struct OscBackend {
pub(super) protocol: OscProtocol,
terminal_name: TerminalName,
dcs_passthrough: bool,
}
impl OscBackend {
pub(super) fn new(protocol: OscProtocol) -> Self {
let terminal = terminal_info();
Self {
protocol,
terminal_name: terminal.name,
dcs_passthrough: matches!(terminal.multiplexer, Some(Multiplexer::Tmux { .. })),
}
}
pub fn notify(&mut self, message: &str) -> io::Result<()> {
execute!(
stdout(),
PostNotification {
protocol: self.protocol,
message,
terminal_name: self.terminal_name,
dcs_passthrough: self.dcs_passthrough,
}
)
}
}
struct PostNotification<'a> {
protocol: OscProtocol,
message: &'a str,
terminal_name: TerminalName,
dcs_passthrough: bool,
}
impl Command for PostNotification<'_> {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
let mut body: String = self
.message
.chars()
.map(|c| match c {
// ASCII semicolons delimit OSC 777 fields.
';' if matches!(self.protocol, OscProtocol::Osc777) => ';',
c if c.is_control() => ' ',
c => c,
})
.collect();
if self.terminal_name == TerminalName::Ghostty && body.len() > GHOSTTY_BODY_BYTES {
body.truncate(body.floor_char_boundary(GHOSTTY_BODY_BYTES - '…'.len_utf8()));
body.push('…');
}
let sequence = match self.protocol {
OscProtocol::Osc9 => format!("\x1b]9;{body}\x07"),
OscProtocol::Osc777 => format!("\x1b]777;notify;Codex;{body}\x1b\\"),
};
if self.dcs_passthrough {
let escaped = sequence.replace('\x1b', "\x1b\x1b");
write!(f, "\x1bPtmux;{escaped}\x1b\\")
} else {
f.write_str(&sequence)
}
}
#[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
}
}
#[cfg(test)]
mod tests {
use codex_terminal_detection::TerminalName;
use crossterm::Command;
use pretty_assertions::assert_eq;
use super::OscProtocol;
use super::PostNotification;
#[test]
fn post_notification_writes_plain_osc9_sequence() {
let message = "a".repeat(2048);
let mut ansi = String::new();
let command = PostNotification {
protocol: OscProtocol::Osc9,
message: &message,
terminal_name: TerminalName::Iterm2,
dcs_passthrough: false,
};
command
.write_ansi(&mut ansi)
.expect("OSC 9 command should format");
assert_eq!(ansi, format!("\u{1b}]9;{message}\u{7}"));
}
#[test]
fn long_approval_in_ghostty_fits_the_notification_body() {
// The next UTF-8 character crosses the cutoff for the ellipsis.
let prefix = format!("Approval requested: {}", "a".repeat(231));
let message = format!("{prefix}🦀{}", "b".repeat(2048));
for (protocol, expected) in [
(OscProtocol::Osc9, format!("\x1b]9;{prefix}…\x07")),
(
OscProtocol::Osc777,
format!("\x1b]777;notify;Codex;{prefix}…\x1b\\"),
),
] {
let mut ansi = String::new();
PostNotification {
protocol,
message: &message,
terminal_name: TerminalName::Ghostty,
dcs_passthrough: false,
}
.write_ansi(&mut ansi)
.expect("format Ghostty notification");
assert_eq!(ansi, expected);
}
}
#[test]
fn post_notification_writes_tmux_dcs_wrapped_osc9_sequence() {
let mut ansi = String::new();
let command = PostNotification {
protocol: OscProtocol::Osc9,
message: "done",
terminal_name: TerminalName::Iterm2,
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_keeps_control_characters_out_of_osc9_body() {
let mut ansi = String::new();
let command = PostNotification {
protocol: OscProtocol::Osc9,
message: "before\x07\x1bafter",
terminal_name: TerminalName::Iterm2,
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;before after\u{7}\u{1b}\\"
);
}
#[test]
fn osc777_sends_title_and_plain_text_body_directly_and_through_tmux() {
let mut sequences = Vec::new();
for dcs_passthrough in [false, true] {
let command = PostNotification {
protocol: OscProtocol::Osc777,
message: "Approval requested: cargo test; git clean\x07\x1b\u{009c}-fd",
terminal_name: TerminalName::WindowsTerminal,
dcs_passthrough,
};
let mut ansi = String::new();
command.write_ansi(&mut ansi).expect("format OSC 777");
sequences.push(ansi);
}
insta::assert_debug_snapshot!(sequences, @r#"
[
"\u{1b}]777;notify;Codex;Approval requested: cargo test; git clean -fd\u{1b}\\",
"\u{1b}Ptmux;\u{1b}\u{1b}]777;notify;Codex;Approval requested: cargo test; git clean -fd\u{1b}\u{1b}\\\u{1b}\\",
]
"#);
}
}