Add a response target picker to /copy (#39997)

## What changed

- Open a picker for `/copy` with the whole response plus each fenced code block and blockquote from the latest response.
- Label code blocks by language, show content previews, and preserve source whitespace and nested quote Markdown when copying an individual target.
- Keep the picker from disturbing terminal scrollback or queued input, retain clipboard leases, and avoid writing copied content to the session log.

## Testing

- Add coverage for target extraction, picker navigation and cancellation, exact copied content, plan responses, queued input, scrollback preservation, clipboard failures, and session logging.

GitOrigin-RevId: 77a35bc99f4f45932d39914f9a709b30fe8cd7c2
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-08-21 19:23:26 +00:00
1 parent 950dd184a1
commit df6a54ee85
15 files changed
+766 -30

No files matched your search

@@ -109,6 +109,17 @@ fn next_add_to_history_event(rx: &mut tokio::sync::mpsc::UnboundedReceiver<AppEv
}
}
fn next_copy_selection(
rx: &mut tokio::sync::mpsc::UnboundedReceiver<AppEvent>,
) -> (String, String) {
let selection = match rx.try_recv() {
Ok(AppEvent::CopySelection { text, label }) => (text.to_string(), label),
other => panic!("expected selected clipboard content, got {other:?}"),
};
assert_matches!(rx.try_recv(), Ok(AppEvent::SettingsSelectionClosed));
selection
}
#[tokio::test]
async fn service_tier_commands_lowercase_catalog_names() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(Some("gpt-5.4")).await;
@@ -1717,8 +1728,9 @@ async fn slash_copy_state_tracks_turn_complete_final_reply() {
#[tokio::test]
async fn slash_copy_state_tracks_plan_item_completion() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let plan_text = "## Plan\n\n1. Build it\n2. Test it".to_string();
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
complete_turn_with_message(&mut chat, "previous", Some("```rust\nstale();\n```"));
let plan_text = "## Plan\n\n1. Build it\n2. Test it\n\n```sh\r\njust test \r\n```".to_string();
chat.handle_server_notification(
ServerNotification::ItemCompleted(ItemCompletedNotification {
@@ -1739,6 +1751,334 @@ async fn slash_copy_state_tracks_plan_item_completion() {
chat.pending_notification,
Some(Notification::AgentTurnComplete { ref response }) if response == &plan_text
);
let _ = drain_insert_history(&mut rx);
chat.dispatch_command(SlashCommand::Copy);
let popup = render_bottom_popup(&chat, /*width*/ 80);
assert!(popup.contains("Whole response"), "picker: {popup}");
assert!(popup.contains("sh code"), "picker: {popup}");
chat.handle_key_event(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
(plan_text, "Whole response".to_string())
);
chat.dispatch_command(SlashCommand::Copy);
chat.handle_key_event(KeyEvent::new(KeyCode::Char('2'), KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
("just test \r\n".to_string(), "sh code".to_string())
);
chat.transcript.plan_delta_buffer = " ```sh\n echo hi \n ```\n".to_string();
chat.on_plan_item_completed(String::new());
assert_eq!(
chat.last_agent_markdown_text(),
Some("```sh\n echo hi \n ```")
);
let _ = drain_insert_history(&mut rx);
chat.dispatch_command(SlashCommand::Copy);
chat.handle_key_event(KeyEvent::new(KeyCode::Char('2'), KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
("echo hi \n".to_string(), "sh code".to_string())
);
}
#[tokio::test]
async fn slash_copy_picker_preserves_completed_source_whitespace_and_hides_directives() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let source = concat!(
"Intro ::git-push{cwd=\"/repo\"}\r\n\r\n",
"```powershell\r\nWrite-Output value \r\nWrite-Output done\t\r\n```\r\n\r\n",
"> Keep **formatting** \r\n> > Nested quote\r\n> hidden ::git-stage{cwd=\"/repo\"}\r\n\r\n",
"> ::git-stage{cwd=\"/repo\"}\r\n"
);
complete_turn_with_message(&mut chat, "turn-1", Some(source));
while rx.try_recv().is_ok() {}
for (key, expected, label) in [
(
'1',
"Intro\n\n```powershell\nWrite-Output value\nWrite-Output done\n```\n\n> Keep **formatting**\n> > Nested quote\n> hidden\n\n>",
"Whole response",
),
(
'2',
"Write-Output value \r\nWrite-Output done\t\r\n",
"powershell code",
),
(
'3',
"Keep **formatting** \r\n> Nested quote\r\nhidden \r\n",
"Blockquote",
),
] {
chat.dispatch_command(SlashCommand::Copy);
assert_eq!(
render_bottom_popup(&chat, /*width*/ 100)
.matches("Blockquote")
.count(),
1
);
chat.handle_key_event(KeyEvent::new(KeyCode::Char(key), KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
(expected.to_string(), label.to_string())
);
}
chat.transcript.reset_copy_history();
assert_eq!(
(
chat.transcript.last_agent_markdown.as_deref(),
chat.transcript.last_agent_source.as_deref()
),
(None, None)
);
}
#[tokio::test]
async fn slash_copy_picker_defers_queued_input_until_selection_or_cancellation_settles() {
for queued in ["/new", "/clear", "follow-up prompt"] {
for close_key in [KeyCode::Enter, KeyCode::Esc] {
let (mut chat, mut rx, mut op_rx) =
make_chatwidget_manual(/*model_override*/ None).await;
chat.thread_id = Some(ThreadId::new());
complete_turn_with_message(&mut chat, "previous", Some("Previous response"));
handle_turn_started(&mut chat, "active");
queue_composer_text_with_tab(&mut chat, queued);
chat.dispatch_command(SlashCommand::Copy);
complete_turn_with_message(&mut chat, "active", Some("New response"));
assert_eq!(chat.input_queue.queued_user_messages.len(), 1);
assert!(render_bottom_popup(&chat, /*width*/ 80).contains("Previous response"));
assert_matches!(op_rx.try_recv(), Err(TryRecvError::Empty));
while let Ok(event) = rx.try_recv() {
assert!(!matches!(
event,
AppEvent::NewSession { .. } | AppEvent::ClearUi { .. }
));
}
chat.handle_key_event(KeyEvent::new(close_key, KeyModifiers::NONE));
if close_key == KeyCode::Enter {
assert_eq!(
next_copy_selection(&mut rx),
(
"Previous response".to_string(),
"Whole response".to_string()
)
);
} else {
assert_matches!(rx.try_recv(), Ok(AppEvent::SettingsSelectionClosed));
}
assert_eq!(chat.input_queue.queued_user_messages.len(), 1);
chat.set_queue_autosend_suppressed(/*suppressed*/ false);
chat.maybe_send_next_queued_input();
assert!(chat.input_queue.queued_user_messages.is_empty());
match queued {
"/new" => assert!(
std::iter::from_fn(|| rx.try_recv().ok())
.any(|event| matches!(event, AppEvent::NewSession { .. }))
),
"/clear" => assert!(
std::iter::from_fn(|| rx.try_recv().ok())
.any(|event| matches!(event, AppEvent::ClearUi { .. }))
),
_ => {
assert_matches!(next_submit_op(&mut op_rx), Op::UserTurn { items, .. } if items == vec![UserInput::Text { text: queued.to_string(), text_elements: Vec::new() }])
}
}
}
}
}
#[tokio::test]
async fn slash_copy_picker_previews_whole_response_code_blocks_and_blockquotes() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.transcript.last_agent_markdown = Some(
"Overview\n\n```python\nprint('first')\nprint('second')\n```\n\n> Important quote\n\n```sh\necho done\n```"
.to_string(),
);
chat.dispatch_command(SlashCommand::Copy);
let popup = render_bottom_popup(&chat, /*width*/ 100);
for expected in [
"1. Whole response",
"Overview",
"2. python code",
"print('first')",
"3. Blockquote",
"Important quote",
"4. sh code",
"echo done",
] {
assert!(popup.contains(expected), "expected {expected:?} in {popup}");
}
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
}
#[tokio::test]
async fn slash_copy_picker_waits_for_submission_after_typing_or_autocomplete() {
for select_from_autocomplete in [false, true] {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.transcript.last_agent_markdown = Some("Ready to copy".to_string());
chat.bottom_pane
.set_composer_text("/cop".to_string(), Vec::new(), Vec::new());
let key = if select_from_autocomplete {
KeyCode::Tab
} else {
KeyCode::Char('y')
};
chat.handle_key_event(KeyEvent::new(key, KeyModifiers::NONE));
if !select_from_autocomplete {
std::thread::sleep(crate::bottom_pane::ChatComposer::recommended_paste_flush_delay());
chat.handle_paste_burst_tick(FrameRequester::test_dummy());
}
assert_eq!(chat.bottom_pane.composer_text().trim(), "/copy");
assert!(
!render_bottom_popup(&chat, /*width*/ 80).contains("Whole response"),
"preparing /copy must not open the picker"
);
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
let popup = render_bottom_popup(&chat, /*width*/ 80);
assert!(popup.contains("Whole response"), "picker: {popup}");
assert!(chat.bottom_pane.composer_text().is_empty());
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
}
}
#[tokio::test]
async fn slash_copy_picker_waits_for_submission_after_paste() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.transcript.last_agent_markdown = Some("Ready to copy".to_string());
chat.handle_paste("/copy".to_string());
assert_eq!(chat.bottom_pane.composer_text(), "/copy");
assert!(!render_bottom_popup(&chat, /*width*/ 80).contains("Whole response"));
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
assert!(render_bottom_popup(&chat, /*width*/ 80).contains("Whole response"));
assert!(chat.bottom_pane.composer_text().is_empty());
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
}
#[tokio::test]
async fn slash_copy_picker_numeric_shortcuts_copy_whole_response_and_exact_code() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let markdown = "Intro\n\n```rust\nfn main() {\n run();\n}\n```";
chat.transcript.last_agent_markdown = Some(markdown.to_string());
chat.dispatch_command(SlashCommand::Copy);
chat.handle_key_event(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
(markdown.to_string(), "Whole response".to_string())
);
chat.dispatch_command(SlashCommand::Copy);
chat.handle_key_event(KeyEvent::new(KeyCode::Char('2'), KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
(
"fn main() {\n run();\n}\n".to_string(),
"rust code".to_string()
)
);
}
#[tokio::test]
async fn slash_copy_picker_preserves_nested_blockquote_markdown_and_code() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.transcript.last_agent_markdown =
Some("> **outer**\n> > _nested_\n> ```sh\n> nested()\n> ```\n".to_string());
chat.dispatch_command(SlashCommand::Copy);
chat.handle_key_event(KeyEvent::new(KeyCode::Char('2'), KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
(
"**outer**\n> _nested_\n```sh\nnested()\n```\n".to_string(),
"Blockquote".to_string()
)
);
chat.dispatch_command(SlashCommand::Copy);
chat.handle_key_event(KeyEvent::new(KeyCode::Char('3'), KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
("nested()\n".to_string(), "sh code".to_string())
);
}
#[tokio::test]
async fn slash_copy_picker_supports_arrow_navigation_enter_and_scrolling() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.transcript.last_agent_markdown = Some(
(1..=11)
.map(|index| format!("```sh\nexample_{index}()\n```\n"))
.collect(),
);
chat.dispatch_command(SlashCommand::Copy);
for _ in 0..11 {
chat.handle_key_event(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
}
assert!(
render_bottom_popup(&chat, /*width*/ 80).contains("example_11()"),
"later rows must remain reachable through scrolling"
);
chat.handle_key_event(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE));
chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
("example_10()\n".to_string(), "sh code".to_string())
);
}
#[tokio::test]
async fn slash_copy_picker_escape_dismisses_without_copying() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.transcript.last_agent_markdown = Some("Nothing copied".to_string());
chat.dispatch_command(SlashCommand::Copy);
assert!(!chat.bottom_pane.no_modal_or_popup_active());
chat.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
assert!(chat.bottom_pane.no_modal_or_popup_active());
assert_matches!(rx.try_recv(), Ok(AppEvent::SettingsSelectionClosed));
assert_matches!(rx.try_recv(), Err(TryRecvError::Empty));
}
#[tokio::test]
async fn slash_copy_picker_remains_available_from_parent_owned_threads() {
let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
chat.transcript.last_agent_markdown = Some("Safe local copy".to_string());
chat.set_parent_owned_thread();
chat.bottom_pane
.set_composer_text("/cop".to_string(), Vec::new(), Vec::new());
chat.handle_key_event(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE));
assert!(!render_bottom_popup(&chat, /*width*/ 80).contains("Whole response"));
chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
assert!(render_bottom_popup(&chat, /*width*/ 80).contains("Whole response"));
chat.handle_key_event(KeyEvent::new(KeyCode::Char('1'), KeyModifiers::NONE));
assert_eq!(
next_copy_selection(&mut rx),
("Safe local copy".to_string(), "Whole response".to_string())
);
assert!(
op_rx.try_recv().is_err(),
"copy must not submit an agent turn"
);
}
#[tokio::test]
@@ -1996,6 +2336,22 @@ async fn slash_copy_stores_clipboard_lease_and_preserves_it_on_failure() {
rendered.contains("Copy failed: blocked"),
"expected failure message, got {rendered:?}"
);
chat.copy_selection_with("print('ok')\n", "python code", |content| {
assert_eq!(content, "print('ok')\n");
Ok(Some(crate::clipboard_copy::ClipboardLease::test()))
});
assert!(chat.clipboard_lease.is_some());
let rendered = lines_to_single_string(&drain_insert_history(&mut rx)[0]);
assert!(rendered.contains("Copied python code to clipboard"));
chat.copy_selection_with("print('blocked')\n", "python code", |content| {
assert_eq!(content, "print('blocked')\n");
Err("blocked selection".to_string())
});
assert!(chat.clipboard_lease.is_some());
let rendered = lines_to_single_string(&drain_insert_history(&mut rx)[0]);
assert!(rendered.contains("Copy failed: blocked selection"));
}
#[tokio::test]