mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Support Cloud image messages and editable draft handoffs (#60061)
## Why Cloud messages previously accepted only plain text, and switching a draft to Cloud waited for session preparation without processing composer input. ## What changed - Support ordered text and image parts in Cloud submissions and user-message summaries, preserving attachment placeholders, image detail, and client message IDs. - Accept inline data URLs and uploaded file IDs, including image-only messages. Reject invalid references and text spans locally, and cap encoded image requests at 3.75 MiB while retaining the existing plain-text limit. - Keep the composer editable and redraw the TUI while Cloud prepares a conversation. Carry the latest draft, attachments, and Vim mode into the destination, retain drafts on failure, and prevent submission to the outgoing session during the handoff. - Preserve buffered paste input and pending clipboard reads across the handoff, with connection and failure hints. ## Testing Add regression coverage for image wire encoding, receipt identity, validation and size boundaries, draft editing during successful and failed handoffs, clipboard completion, Vim state, and attachment recovery after rejected submissions. GitOrigin-RevId: 0cbcde0ab89ba961780b5f88456057e6b6a19cd5
This commit is contained in:
committed by
copyberry
parent
d4a341bd52
commit
e6bf88ae02
@@ -46,6 +46,8 @@
|
||||
//! popup-specific handler if a popup is visible and otherwise to
|
||||
//! [`ChatComposer::handle_key_event_without_popup`]. After every handled key, we call
|
||||
//! [`ChatComposer::sync_popups`] so UI state follows the latest buffer/cursor.
|
||||
//! A pending backend handoff uses the basic editor and paste detection while holding submission.
|
||||
//! Redraws preserve the paste detector's window so trailing keys stay with the outgoing composer.
|
||||
//! Fresh Vim drafts start in Insert; Normal `/` and `?` search the composer.
|
||||
//! On an empty draft, a standalone Normal `/` enters Insert and opens command completion.
|
||||
//! Backspace on an empty Vim search query cancels search and any pending operator.
|
||||
@@ -290,6 +292,8 @@
|
||||
//! pasted text. This preserves the order when a paste arrives before the next burst-flush tick;
|
||||
//! buffered keys have already been classified and go directly through `apply_paste`.
|
||||
//! Image detection probes only regular files. Pasted device and FIFO paths stay literal.
|
||||
//! - Accepted clipboard images during Cloud handoff settle buffered typing before inserting their
|
||||
//! placeholder. This edit keeps a newly settled leading bang literal if navigation fails.
|
||||
//! - Startup recovery previews held keys with the live Vim Replace policy without consuming them.
|
||||
//! Buffered pastes remain literal in recovery so large-paste and image classification stays with
|
||||
//! the live paste path.
|
||||
@@ -710,6 +714,7 @@ pub(super) struct ComposerDraft {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) struct ComposerDraftSnapshot {
|
||||
pub(crate) vim_normal_mode: bool,
|
||||
pub(crate) text: String,
|
||||
pub(crate) shell_mode: Option<bool>,
|
||||
pub(crate) cursor: usize,
|
||||
|
||||
@@ -20,6 +20,7 @@ impl ChatComposer {
|
||||
attachments.set_remote_image_urls(draft.remote_image_urls.clone(), &mut textarea);
|
||||
attachments.reset_local_images(draft.local_image_paths, &mut textarea);
|
||||
ComposerDraftSnapshot {
|
||||
vim_normal_mode: self.draft.textarea.is_vim_normal_mode(),
|
||||
text: draft.text,
|
||||
shell_mode: self
|
||||
.config
|
||||
|
||||
@@ -93,6 +93,23 @@ impl ChatComposer {
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::bottom_pane) fn paste_handoff_deadline(&self) -> Option<Instant> {
|
||||
if self.draft.disable_paste_burst {
|
||||
return None;
|
||||
}
|
||||
self.draft.paste_burst.handoff_deadline()
|
||||
}
|
||||
|
||||
/// Display flushed text without losing the window for its trailing paste keys.
|
||||
pub(in crate::bottom_pane) fn flush_handoff_paste(&mut self, now: Instant) -> bool {
|
||||
let deadline = self.paste_handoff_deadline();
|
||||
let flushed = self.handle_paste_burst_flush(now);
|
||||
if flushed && let Some(deadline) = deadline.filter(|deadline| *deadline >= now) {
|
||||
self.draft.paste_burst.preserve_window_until(deadline);
|
||||
}
|
||||
flushed
|
||||
}
|
||||
|
||||
/// Enable or disable paste-burst handling.
|
||||
///
|
||||
/// `disable_paste_burst` is an escape hatch for terminals/platforms where the burst heuristic
|
||||
@@ -235,11 +252,13 @@ impl ChatComposer {
|
||||
}
|
||||
|
||||
pub(super) fn handle_paste_tab(&mut self, key: KeyEvent, now: Instant) -> bool {
|
||||
if key.code != KeyCode::Tab
|
||||
|| !key.modifiers.is_empty()
|
||||
|| self.draft.disable_paste_burst
|
||||
|| !self.draft.textarea.allows_paste_burst()
|
||||
{
|
||||
key.code == KeyCode::Tab
|
||||
&& key.modifiers.is_empty()
|
||||
&& self.append_recent_paste_separator('\t', now)
|
||||
}
|
||||
|
||||
pub(super) fn append_recent_paste_separator(&mut self, separator: char, now: Instant) -> bool {
|
||||
if self.draft.disable_paste_burst || !self.draft.textarea.allows_paste_burst() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -247,7 +266,7 @@ impl ChatComposer {
|
||||
if self
|
||||
.draft
|
||||
.paste_burst
|
||||
.append_control_char_if_active('\t', now)
|
||||
.append_control_char_if_active(separator, now)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -264,7 +283,7 @@ impl ChatComposer {
|
||||
return self
|
||||
.draft
|
||||
.paste_burst
|
||||
.append_control_char_if_active('\t', now);
|
||||
.append_control_char_if_active(separator, now);
|
||||
}
|
||||
|
||||
false
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
//! Unavailable threads also allow recovery and other local commands.
|
||||
//! Connected unavailable threads retain the empty-prompt agents shortcut.
|
||||
//! Paste Enter handling is shared with normal submission so buffered newlines survive both paths.
|
||||
//! In the draft-only handoff, a recent paste prefix can preserve a raw Enter as a newline.
|
||||
//! The submit path still requires an established paste before treating Enter as a newline.
|
||||
//! Recovery commands must occupy one line and be visible before the submit key expands pastes.
|
||||
//! Offline draft edits also consume the Astra sparkle opportunity before rendering.
|
||||
|
||||
@@ -11,6 +13,7 @@ use super::*;
|
||||
pub(crate) enum RestrictedInputMode {
|
||||
Disconnected,
|
||||
UnavailableThread,
|
||||
DraftHandoff,
|
||||
}
|
||||
|
||||
impl ChatComposer {
|
||||
@@ -60,6 +63,37 @@ impl ChatComposer {
|
||||
key: KeyEvent,
|
||||
mode: RestrictedInputMode,
|
||||
) -> InputResult {
|
||||
if mode == RestrictedInputMode::DraftHandoff {
|
||||
if matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) {
|
||||
let now = tokio::time::Instant::now().into_std();
|
||||
self.flush_handoff_paste(now);
|
||||
// Completion does not own keys during the handoff.
|
||||
self.popups.active = ActivePopup::None;
|
||||
if self.draft.textarea.wants_vim_search_key(key) {
|
||||
self.edit_restricted_draft(key);
|
||||
} else if self.handle_vim_history_key(key) {
|
||||
self.draft.textarea_state.get_mut().follow_cursor();
|
||||
} else {
|
||||
// Enter cannot submit here. Preserve a possible legacy paste
|
||||
// newline after a directly inserted prefix, as we do for Tab.
|
||||
let paste_separator = (key.code == KeyCode::Enter
|
||||
&& key.modifiers.is_empty()
|
||||
&& (self.append_paste_newline(now)
|
||||
|| self.append_recent_paste_separator('\n', now)))
|
||||
|| self.handle_paste_tab(key, now);
|
||||
if !paste_separator
|
||||
&& !self.submit_keys.is_pressed(key)
|
||||
&& !self.queue_keys.is_pressed(key)
|
||||
{
|
||||
self.edit_restricted_draft(key);
|
||||
}
|
||||
}
|
||||
// A paste flush can open a popup before the edit changes its text.
|
||||
// Dismiss it before ordinary input resumes, even without a redraw.
|
||||
self.popups.active = ActivePopup::None;
|
||||
}
|
||||
return InputResult::None;
|
||||
}
|
||||
if mode == RestrictedInputMode::UnavailableThread && self.handle_agents_navigation(key) {
|
||||
return InputResult::None;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,56 @@ use crossterm::event::MouseEvent;
|
||||
use crossterm::event::MouseEventKind;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn draft_handoff_preserves_newline_after_short_unicode_prefix() {
|
||||
let (mut composer, _) = new_test_composer();
|
||||
for key in [KeyCode::Char('猫'), KeyCode::Enter, KeyCode::Char('犬')] {
|
||||
assert_eq!(
|
||||
composer.handle_restricted_key(key.into(), RestrictedInputMode::DraftHandoff),
|
||||
InputResult::None,
|
||||
);
|
||||
}
|
||||
tokio::time::advance(Duration::from_millis(/*millis*/ 100)).await;
|
||||
composer.flush_handoff_paste(tokio::time::Instant::now().into_std());
|
||||
composer.handle_restricted_key(KeyCode::Enter.into(), RestrictedInputMode::DraftHandoff);
|
||||
insta::assert_snapshot!(composer.current_text(), @"猫
|
||||
犬");
|
||||
|
||||
let (mut composer, _) = new_test_composer();
|
||||
composer.handle_key_event(KeyCode::Char('猫').into());
|
||||
let (result, _) = composer.handle_key_event(KeyCode::Enter.into());
|
||||
assert!(matches!(result, InputResult::Submitted { text, .. } if text == "猫"));
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn handoff_edit_discards_stale_command_before_ordinary_enter() {
|
||||
for bracketed_paste in [true, false] {
|
||||
let (mut composer, _) = new_test_composer();
|
||||
if bracketed_paste {
|
||||
composer.handle_paste("/quit".into());
|
||||
assert!(matches!(composer.popups.active, ActivePopup::Command(_)));
|
||||
} else {
|
||||
for ch in "/quit".chars() {
|
||||
composer.handle_restricted_key(
|
||||
KeyCode::Char(ch).into(),
|
||||
RestrictedInputMode::DraftHandoff,
|
||||
);
|
||||
}
|
||||
assert!(composer.draft.paste_burst.is_active());
|
||||
}
|
||||
|
||||
composer.handle_restricted_key(
|
||||
KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL),
|
||||
RestrictedInputMode::DraftHandoff,
|
||||
);
|
||||
assert_eq!(composer.current_text(), "");
|
||||
|
||||
// A failed handoff can return to ordinary input before a redraw.
|
||||
let (result, _) = composer.handle_key_event(KeyCode::Enter.into());
|
||||
assert_eq!(result, InputResult::None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offline_delete_reveals_the_edited_row_without_moving_the_caret() {
|
||||
let (mut composer, _) = new_test_composer();
|
||||
|
||||
@@ -12,6 +12,7 @@ use pretty_assertions::assert_eq;
|
||||
|
||||
use super::super::super::chat_composer_history::HistoryEntry;
|
||||
use super::super::ChatComposer;
|
||||
use super::super::ComposerDraftSnapshot;
|
||||
use super::super::InputResult;
|
||||
use super::super::LARGE_PASTE_CHAR_THRESHOLD;
|
||||
use super::super::PasteBurst;
|
||||
@@ -590,7 +591,13 @@ fn undo_and_redo_restore_deleted_image_attachment() {
|
||||
ctrl_r(&mut composer);
|
||||
escape(&mut composer);
|
||||
let _ = composer.handle_key_event(KeyEvent::from(KeyCode::Backspace));
|
||||
assert_eq!(composer.draft_snapshot(), before);
|
||||
assert_eq!(
|
||||
composer.draft_snapshot(),
|
||||
ComposerDraftSnapshot {
|
||||
vim_normal_mode: false,
|
||||
..before.clone()
|
||||
}
|
||||
);
|
||||
escape(&mut composer);
|
||||
composer.draft.textarea.set_cursor("alpha ".len());
|
||||
keys(&mut composer, "x");
|
||||
|
||||
@@ -870,6 +870,14 @@ impl BottomPane {
|
||||
key: KeyEvent,
|
||||
mode: RestrictedInputMode,
|
||||
) -> InputResult {
|
||||
if mode == RestrictedInputMode::DraftHandoff {
|
||||
let result = self.composer.handle_restricted_key(key, mode);
|
||||
if Self::is_composer_activity_key(key) {
|
||||
self.record_composer_activity_at(Instant::now());
|
||||
}
|
||||
self.request_redraw();
|
||||
return result;
|
||||
}
|
||||
self.view_stack.clear();
|
||||
self.warnings_view = None;
|
||||
self.delayed_approval_requests.clear();
|
||||
@@ -2385,6 +2393,12 @@ impl BottomPane {
|
||||
self.request_redraw();
|
||||
}
|
||||
|
||||
/// Settle typing before an accepted handoff image without starting shell mode.
|
||||
pub(crate) fn settle_before_handoff_image(&mut self) {
|
||||
self.composer
|
||||
.flush_pending_input_with_shell_prefix(ShellPrefixHandling::Literal);
|
||||
}
|
||||
|
||||
pub(crate) fn attach_image(&mut self, path: PathBuf) {
|
||||
if !self.has_active_view() {
|
||||
self.composer.attach_image(path);
|
||||
|
||||
@@ -358,6 +358,22 @@ impl PasteBurst {
|
||||
.is_some_and(|t| now.duration_since(t) <= PASTE_BURST_CHAR_INTERVAL)
|
||||
}
|
||||
|
||||
/// Keep input in this composer while keys can still belong to its paste.
|
||||
pub(super) fn handoff_deadline(&self) -> Option<Instant> {
|
||||
let interval = if self.is_active_internal() {
|
||||
PASTE_BURST_ACTIVE_IDLE_TIMEOUT
|
||||
} else {
|
||||
PASTE_BURST_CHAR_INTERVAL
|
||||
};
|
||||
self.last_plain_char_time
|
||||
.map(|last_key| last_key + interval)
|
||||
.max(self.burst_window_until)
|
||||
}
|
||||
|
||||
pub(super) fn preserve_window_until(&mut self, deadline: Instant) {
|
||||
self.burst_window_until = self.burst_window_until.max(Some(deadline));
|
||||
}
|
||||
|
||||
/// Keep the burst window alive.
|
||||
pub fn extend_window(&mut self, now: Instant) {
|
||||
self.burst_window_until = Some(now + PASTE_ENTER_SUPPRESS_WINDOW);
|
||||
|
||||
@@ -127,6 +127,15 @@ impl BottomPane {
|
||||
!self.delayed_approval_requests.is_empty()
|
||||
}
|
||||
|
||||
pub(crate) fn composer_paste_deadline(&self) -> Option<Instant> {
|
||||
self.composer.paste_handoff_deadline()
|
||||
}
|
||||
|
||||
pub(crate) fn flush_handoff_paste_if_due(&mut self) -> bool {
|
||||
self.composer
|
||||
.flush_handoff_paste(tokio::time::Instant::now().into_std())
|
||||
}
|
||||
|
||||
/// Preserve buffered composer input before its owner or input mode changes.
|
||||
pub(crate) fn flush_composer_paste_burst(&mut self) {
|
||||
if self.composer.is_in_paste_burst() {
|
||||
|
||||
Reference in New Issue
Block a user