mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
## Why Pets were unavailable in tmux even when fullscreen Codex could render Kitty images through passthrough. ## What changed - Enable pet images and picker previews in fullscreen tmux panes with Kitty passthrough, and update the unavailable notice to recommend fullscreen mode with `allow-passthrough all`. - Use pane-specific image IDs and virtual placements for pets and previews, reserving separate IDs from transcript images. Repaint underlying text when pet placeholders move or disappear. - Draw pets and previews when refreshing the fullscreen transcript, and consume preview areas once per render. ## Testing Extend the owned-screen lifecycle test to verify that preparing fullscreen enables the Kitty protocol in tmux and returning to scrollback disables it. Update `/pets` notice assertions and the popup snapshot. GitOrigin-RevId: 02c838ceb06abcbb45314e1d38ff4cde77221426
435 lines
14 KiB
Rust
435 lines
14 KiB
Rust
//! Chat widget helpers for ambient terminal pets and the pets picker.
|
|
|
|
use super::*;
|
|
use codex_config::types::TuiPetAnchor;
|
|
use std::sync::atomic::AtomicU64;
|
|
use std::sync::atomic::Ordering;
|
|
|
|
// A configured-pet load can finish after its ChatWidget is replaced, so
|
|
// generations must remain unique across widget replacements.
|
|
static NEXT_CONFIGURED_PET_LOAD_GENERATION: AtomicU64 = AtomicU64::new(/*v*/ 1);
|
|
|
|
/// Identifies one configured-pet load across widget replacements.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub(crate) struct ConfiguredPetLoadGeneration(u64);
|
|
|
|
fn next_configured_pet_load_generation() -> ConfiguredPetLoadGeneration {
|
|
ConfiguredPetLoadGeneration(
|
|
NEXT_CONFIGURED_PET_LOAD_GENERATION.fetch_add(/*val*/ 1, Ordering::Relaxed),
|
|
)
|
|
}
|
|
|
|
pub(super) fn load_ambient_pet(
|
|
config: &crate::local_settings::LocalSettings,
|
|
frame_requester: FrameRequester,
|
|
) -> Option<crate::pets::AmbientPet> {
|
|
let selected_pet = config.tui.pet.as_deref()?;
|
|
if selected_pet == crate::pets::DISABLED_PET_ID {
|
|
return None;
|
|
}
|
|
|
|
crate::pets::AmbientPet::load(
|
|
Some(selected_pet),
|
|
&config.codex_home,
|
|
frame_requester,
|
|
config.tui.animations,
|
|
)
|
|
.ok()
|
|
}
|
|
|
|
pub(super) fn start_configured_pet_load_if_needed(
|
|
config: &crate::local_settings::LocalSettings,
|
|
ambient_pet_missing: bool,
|
|
frame_requester: FrameRequester,
|
|
app_event_tx: AppEventSender,
|
|
pet_http_client: codex_http_client::RouteAwareClientPool,
|
|
) -> ConfiguredPetLoadGeneration {
|
|
let generation = next_configured_pet_load_generation();
|
|
let Some(pet_id) = config.tui.pet.clone() else {
|
|
return generation;
|
|
};
|
|
if pet_id == crate::pets::DISABLED_PET_ID || !ambient_pet_missing {
|
|
return generation;
|
|
}
|
|
|
|
let codex_home = config.codex_home.clone();
|
|
let animations_enabled = config.tui.animations;
|
|
let event_pet_id = pet_id.clone();
|
|
spawn_pet_load(
|
|
async move {
|
|
crate::pets::load_pet_with_assets(
|
|
pet_id,
|
|
codex_home,
|
|
frame_requester,
|
|
animations_enabled,
|
|
&pet_http_client,
|
|
)
|
|
.await
|
|
.map(Some)
|
|
.map_err(|err| err.to_string())
|
|
},
|
|
app_event_tx,
|
|
move |result| AppEvent::ConfiguredPetLoaded {
|
|
generation,
|
|
pet_id: event_pet_id,
|
|
result,
|
|
},
|
|
);
|
|
generation
|
|
}
|
|
|
|
impl ChatWidget {
|
|
pub(super) fn set_ambient_pet_notification(
|
|
&mut self,
|
|
kind: crate::pets::PetNotificationKind,
|
|
body: Option<String>,
|
|
) {
|
|
if let Some(pet) = self.ambient_pet.as_mut() {
|
|
pet.set_notification(kind, body);
|
|
}
|
|
}
|
|
|
|
pub(crate) fn ambient_pet_image_enabled(&self) -> bool {
|
|
self.ambient_pet
|
|
.as_ref()
|
|
.is_some_and(crate::pets::AmbientPet::image_enabled)
|
|
}
|
|
|
|
pub(crate) fn disable_ambient_pet_for_session(&mut self) {
|
|
self.ambient_pet = None;
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(crate) fn ambient_pet_draw(
|
|
&self,
|
|
area: Rect,
|
|
composer_bottom_y: u16,
|
|
) -> Option<crate::pets::AmbientPetDraw> {
|
|
if !self.bottom_pane.no_modal_or_popup_active() {
|
|
return None;
|
|
}
|
|
|
|
let anchor_bottom_y = match self.local_settings.tui.pet_anchor {
|
|
TuiPetAnchor::Composer => composer_bottom_y,
|
|
TuiPetAnchor::ScreenBottom => area.bottom(),
|
|
};
|
|
self.ambient_pet
|
|
.as_ref()?
|
|
.draw_request(area, anchor_bottom_y)
|
|
}
|
|
|
|
pub(super) fn ambient_pet_wrap_reserved_cols(&self) -> u16 {
|
|
self.ambient_pet
|
|
.as_ref()
|
|
.filter(|pet| pet.image_enabled())
|
|
.map(|pet| {
|
|
pet.image_columns()
|
|
.saturating_add(AMBIENT_PET_WRAP_GAP_COLUMNS)
|
|
})
|
|
.unwrap_or(0)
|
|
}
|
|
|
|
pub(crate) fn history_wrap_width(&self, width: u16) -> u16 {
|
|
width
|
|
.saturating_sub(self.ambient_pet_wrap_reserved_cols())
|
|
.max(1)
|
|
}
|
|
|
|
pub(crate) fn pet_picker_preview_draw(&self) -> Option<crate::pets::AmbientPetDraw> {
|
|
self.bottom_pane
|
|
.selected_index_for_active_view(crate::pets::PET_PICKER_VIEW_ID)?;
|
|
let area = self.pet_picker_preview_state.take_area()?;
|
|
let request = self
|
|
.pet_picker_preview_pet
|
|
.as_ref()?
|
|
.preview_draw_request(area)?;
|
|
self.pet_picker_preview_image_visible.set(true);
|
|
Some(request)
|
|
}
|
|
|
|
pub(crate) fn should_clear_pet_picker_preview_image(&self) -> bool {
|
|
self.pet_picker_preview_image_visible.replace(false)
|
|
}
|
|
|
|
pub(crate) fn fail_pet_picker_preview_render(&mut self, message: String) {
|
|
self.pet_picker_preview_state.set_error(message);
|
|
self.pet_picker_preview_pet = None;
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(crate) fn open_pets_picker(&mut self) {
|
|
if self.warn_if_pets_unsupported() {
|
|
return;
|
|
}
|
|
|
|
self.pet_picker_preview_state.clear();
|
|
self.pet_picker_preview_pet = None;
|
|
let params = crate::pets::build_pet_picker_params(
|
|
self.local_settings.tui.pet.as_deref(),
|
|
&self.local_settings.codex_home,
|
|
self.pet_picker_preview_state.clone(),
|
|
);
|
|
self.bottom_pane.show_selection_view(params);
|
|
let initial_pet_id = self
|
|
.local_settings
|
|
.tui
|
|
.pet
|
|
.as_deref()
|
|
.unwrap_or(crate::pets::DEFAULT_PET_ID)
|
|
.to_string();
|
|
self.start_pet_picker_preview(initial_pet_id);
|
|
}
|
|
|
|
pub(crate) fn select_pet_by_id(&mut self, pet_id: String) {
|
|
if self.warn_if_pets_unsupported() {
|
|
return;
|
|
}
|
|
|
|
self.app_event_tx.send(AppEvent::PetSelected { pet_id });
|
|
}
|
|
|
|
fn warn_if_pets_unsupported(&mut self) -> bool {
|
|
let support = self.pet_image_support();
|
|
let Some(message) = support.unsupported_message() else {
|
|
return false;
|
|
};
|
|
|
|
self.add_warning_message(message.to_string());
|
|
// Warnings are collected outside the live transcript. A command the user
|
|
// just invoked also needs an immediate explanation above the composer.
|
|
self.bottom_pane.show_selection_view(SelectionViewParams {
|
|
title: Some("Pets unavailable".to_string()),
|
|
subtitle: Some(message.to_string()),
|
|
items: vec![SelectionItem {
|
|
name: "Close".to_string(),
|
|
dismiss_on_select: true,
|
|
..Default::default()
|
|
}],
|
|
..SelectionViewParams::picker()
|
|
});
|
|
true
|
|
}
|
|
|
|
pub(crate) fn pet_image_support(&self) -> crate::pets::PetImageSupport {
|
|
#[cfg(test)]
|
|
if let Some(support) = self.pet_image_support_override {
|
|
return support;
|
|
}
|
|
|
|
#[cfg(test)]
|
|
return crate::pets::PetImageSupport::Unsupported(
|
|
crate::pets::PetImageUnsupportedReason::Terminal,
|
|
);
|
|
|
|
#[cfg(not(test))]
|
|
crate::pets::detect_pet_image_support()
|
|
}
|
|
|
|
/// Set the pet preselected by the TUI picker in the widget's local settings.
|
|
pub(crate) fn set_tui_pet(&mut self, pet: Option<String>) {
|
|
self.local_settings.tui.pet = pet;
|
|
self.ambient_pet = load_ambient_pet(&self.local_settings, self.frame_requester.clone());
|
|
self.configured_pet_load_generation = start_configured_pet_load_if_needed(
|
|
&self.local_settings,
|
|
/*ambient_pet_missing*/ self.ambient_pet.is_none(),
|
|
self.frame_requester.clone(),
|
|
self.app_event_tx.clone(),
|
|
self.pet_http_client.clone(),
|
|
);
|
|
self.apply_ambient_pet_image_support_override_for_tests();
|
|
self.request_redraw();
|
|
}
|
|
|
|
/// Applies a configured-pet result only if it belongs to this widget's current load.
|
|
pub(crate) fn finish_configured_pet_load(
|
|
&mut self,
|
|
generation: ConfiguredPetLoadGeneration,
|
|
pet_id: String,
|
|
result: Result<Option<crate::pets::AmbientPet>, String>,
|
|
) -> bool {
|
|
if generation != self.configured_pet_load_generation
|
|
|| self.local_settings.tui.pet.as_deref() != Some(pet_id.as_str())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
match result {
|
|
Ok(ambient_pet) => {
|
|
self.set_tui_pet_loaded(Some(pet_id), ambient_pet);
|
|
true
|
|
}
|
|
Err(err) => {
|
|
self.add_warning_message(format!("Failed to load configured pet: {err}"));
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn set_tui_pet_loaded(
|
|
&mut self,
|
|
pet: Option<String>,
|
|
ambient_pet: Option<crate::pets::AmbientPet>,
|
|
) {
|
|
self.configured_pet_load_generation = next_configured_pet_load_generation();
|
|
self.local_settings.tui.pet = pet;
|
|
self.ambient_pet = ambient_pet;
|
|
self.apply_ambient_pet_image_support_override_for_tests();
|
|
self.request_redraw();
|
|
}
|
|
|
|
#[cfg(test)]
|
|
fn apply_ambient_pet_image_support_override_for_tests(&mut self) {
|
|
if let Some(support) = self.pet_image_support_override
|
|
&& let Some(pet) = self.ambient_pet.as_mut()
|
|
{
|
|
pet.set_image_support_for_tests(support);
|
|
}
|
|
}
|
|
|
|
#[cfg(not(test))]
|
|
fn apply_ambient_pet_image_support_override_for_tests(&mut self) {}
|
|
|
|
pub(crate) fn start_pet_picker_preview(&mut self, pet_id: String) {
|
|
self.pet_picker_preview_request_id =
|
|
self.pet_picker_preview_request_id.wrapping_add(/*rhs*/ 1);
|
|
let request_id = self.pet_picker_preview_request_id;
|
|
self.pet_picker_preview_pet = None;
|
|
if pet_id == crate::pets::DISABLED_PET_ID {
|
|
self.pet_picker_preview_state.set_disabled();
|
|
self.request_redraw();
|
|
return;
|
|
}
|
|
|
|
self.pet_picker_preview_state.set_loading();
|
|
self.request_redraw();
|
|
|
|
let codex_home = self.local_settings.codex_home.clone();
|
|
let frame_requester = self.frame_requester.clone();
|
|
let tx = self.app_event_tx.clone();
|
|
let pet_http_client = self.pet_http_client.clone();
|
|
spawn_pet_load(
|
|
async move {
|
|
crate::pets::load_pet_with_assets(
|
|
pet_id,
|
|
codex_home,
|
|
frame_requester,
|
|
/*animations_enabled*/ false,
|
|
&pet_http_client,
|
|
)
|
|
.await
|
|
.map_err(|err| err.to_string())
|
|
},
|
|
tx,
|
|
move |result| AppEvent::PetPreviewLoaded { request_id, result },
|
|
);
|
|
}
|
|
|
|
pub(crate) fn finish_pet_picker_preview_load(
|
|
&mut self,
|
|
request_id: u64,
|
|
result: Result<crate::pets::AmbientPet, String>,
|
|
) {
|
|
if request_id != self.pet_picker_preview_request_id {
|
|
return;
|
|
}
|
|
|
|
match result {
|
|
Ok(pet) => {
|
|
self.pet_picker_preview_state.set_ready();
|
|
self.pet_picker_preview_pet = Some(pet);
|
|
#[cfg(test)]
|
|
if let Some(support) = self.pet_image_support_override
|
|
&& let Some(pet) = self.pet_picker_preview_pet.as_mut()
|
|
{
|
|
pet.set_image_support_for_tests(support);
|
|
}
|
|
}
|
|
Err(message) => {
|
|
self.pet_picker_preview_state.set_error(message);
|
|
self.pet_picker_preview_pet = None;
|
|
}
|
|
}
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(crate) fn show_pet_selection_loading_popup(&mut self) -> u64 {
|
|
self.pet_selection_load_request_id =
|
|
self.pet_selection_load_request_id.wrapping_add(/*rhs*/ 1);
|
|
self.pet_picker_preview_state.clear();
|
|
self.pet_picker_preview_pet = None;
|
|
self.bottom_pane.show_selection_view(SelectionViewParams {
|
|
view_id: Some(PET_SELECTION_LOADING_VIEW_ID),
|
|
title: Some("Loading Pet".to_string()),
|
|
subtitle: Some("Preparing the terminal pet.".to_string()),
|
|
items: vec![SelectionItem {
|
|
name: "Loading selected pet...".to_string(),
|
|
is_disabled: true,
|
|
..Default::default()
|
|
}],
|
|
..SelectionViewParams::picker()
|
|
});
|
|
self.pet_selection_load_request_id
|
|
}
|
|
|
|
pub(crate) fn finish_pet_selection_loading_popup(&mut self, request_id: u64) -> bool {
|
|
if request_id != self.pet_selection_load_request_id {
|
|
return false;
|
|
}
|
|
self.bottom_pane
|
|
.dismiss_active_view_if_id(PET_SELECTION_LOADING_VIEW_ID);
|
|
true
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub(crate) fn set_pet_image_support_for_tests(
|
|
&mut self,
|
|
support: crate::pets::PetImageSupport,
|
|
) {
|
|
self.pet_image_support_override = Some(support);
|
|
self.apply_ambient_pet_image_support_override_for_tests();
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub(crate) fn install_test_ambient_pet_for_tests(&mut self, animations_enabled: bool) {
|
|
self.set_tui_pet_loaded(
|
|
Some("test".to_string()),
|
|
Some(crate::pets::test_ambient_pet(
|
|
self.frame_requester.clone(),
|
|
animations_enabled,
|
|
)),
|
|
);
|
|
}
|
|
}
|
|
|
|
fn spawn_pet_load<T>(
|
|
future: impl std::future::Future<Output = Result<T, String>> + Send + 'static,
|
|
app_event_tx: AppEventSender,
|
|
completion_event: impl FnOnce(Result<T, String>) -> AppEvent + Send + 'static,
|
|
) where
|
|
T: Send + 'static,
|
|
{
|
|
if let Ok(handle) = tokio::runtime::Handle::try_current() {
|
|
std::mem::drop(handle.spawn(async move {
|
|
app_event_tx.send(completion_event(future.await));
|
|
}));
|
|
} else {
|
|
let _ = std::thread::spawn(move || {
|
|
let result = match tokio::runtime::Builder::new_current_thread()
|
|
.enable_all()
|
|
.build()
|
|
{
|
|
Ok(runtime) => runtime.block_on(future),
|
|
Err(err) => {
|
|
tracing::warn!(error = %err, "failed to start pet load runtime");
|
|
Err(format!("failed to start pet load runtime: {err}"))
|
|
}
|
|
};
|
|
app_event_tx.send(completion_event(result));
|
|
});
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "pets_tests.rs"]
|
|
mod tests;
|