Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent-tui): route ask paste to custom editor, accept repeated keys (#8621)
Thinh Le2d0236412af9
diff --git a/src-tauri/src/core/agent/loop.rs b/src-tauri/src/core/agent/loop.rsindex 79c423c4a..f2cf586b6 100644--- a/src-tauri/src/core/agent/loop.rs+++ b/src-tauri/src/core/agent/loop.rs@@ -3077,6 +3077,38 @@ mod tests {let _ = std::fs::remove_dir_all(&root);}+ #[tokio::test]+ async fn ask_returns_custom_response_at_invoker_boundary() {+ let root = unique_project_root();+ let (tx, mut rx) = mpsc::unbounded_channel();+ let permissions: PermissionRegistry = Arc::new(Mutex::new(HashMap::new()));+ let asks = crate::core::agent::interaction::new_registry();+ let mut invoker = build_prompting_invoker(root.clone(), tx, permissions);+ invoker.ask_requests = Some(asks.clone());++ let task = tokio::spawn(async move { invoker.invoke(&[ask_call()]).await.unwrap() });+ let request_id = match rx.recv().await.unwrap() {+ StreamEvent::AskRequest { request_id, .. } => request_id,+ event => panic!("expected ask_request, got {event:?}"),+ };+ crate::core::agent::interaction::respond(+ &asks,+ &request_id,+ Ok(vec![crate::core::agent::interaction::QuestionResult {+ id: "scope".into(),+ selected: Vec::new(),+ custom_input: Some("custom answer".into()),+ }]),+ )+ .await+ .unwrap();++ let out = task.await.unwrap();+ let result: serde_json::Value = serde_json::from_str(&out[0].content).unwrap();+ assert_eq!(result[0]["custom_input"], "custom answer");+ let _ = std::fs::remove_dir_all(&root);+ }+fn todo_call(id: &str, args: serde_json::Value) -> serde_json::Value {json!({"id": id,diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rsindex bdc1b543c..6e877fb4c 100644--- a/src-tauri/src/core/cli/tui.rs+++ b/src-tauri/src/core/cli/tui.rs@@ -4060,16 +4060,19 @@ async fn chat_loop<B: Backend>(};}}- Ok(Event::Paste(text)) => match app.login.as_mut() {- // A pasted API key belongs to the login field, not- // the input box (where it would be echoed).- Some(prompt) => prompt.paste(&text),- None => {+ Ok(Event::Paste(text)) => {+ if let Some(prompt) = app.login.as_mut() {+ // A pasted API key belongs to the login field,+ // not the chat composer (where it would echo).+ prompt.paste(&text);+ } else if !app.ask_queue.is_empty() {+ handle_ask_paste(app, &text);+ } else {for c in text.chars() {app.input_insert(c);}}- },+ }// `handle_ask_mouse` mutates app state, so it stays in the// arm body rather than a match guard that hides the effect.#[allow(clippy::collapsible_match)]@@ -4316,7 +4319,7 @@ async fn handle_ask_key(if app.ask_queue.is_empty() {return false;}- if key.kind != KeyEventKind::Press {+ if key.kind == KeyEventKind::Release {return true;}let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);@@ -4362,6 +4365,15 @@ async fn handle_ask_key(true}+/// Route bracketed paste to the active custom answer instead of the chat composer.+fn handle_ask_paste(app: &mut App, text: &str) {+ if let Some(ask) = app.ask_queue.front_mut() {+ if ask.editing_custom {+ ask.custom_input.push_str(text);+ }+ }+}+async fn handle_ask_mouse(app: &mut App,mouse: MouseEvent,@@ -8065,6 +8077,7 @@ mod tests {clipboard_path,diff_lines, Row, group_activity, group_detail_lines, group_summary, handle_ask_key,handle_ask_mouse, handle_key, handle_mouse, image_mime, input_content_lines,+ handle_ask_paste,compact_tokens, finish_login, finish_update_install, load_image_file, message_text,note_update, open_config_screen,parse_command, partial_json_field, restore_goal, restore_run_mode, restore_todos,@@ -8080,7 +8093,7 @@ mod tests {};use std::time::{Duration, Instant};use ratatui::crossterm::event::{- KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,+ KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,};use ratatui::layout::Rect;use ratatui::{style::Modifier, text::Line};@@ -8583,6 +8596,25 @@ mod tests {assert!(app.ask_queue.is_empty());}+ #[tokio::test]+ async fn ask_keyboard_accepts_repeated_key_events() {+ let mut app = test_app();+ let registry = crate::core::agent::interaction::new_registry();+ let (request_id, receiver) = crate::core::agent::interaction::register(®istry).await;+ app.apply(StreamEvent::AskRequest {+ request_id,+ request: ask_request(false, false),+ });++ let mut key = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);+ key.kind = KeyEventKind::Repeat;+ assert!(handle_ask_key(&mut app, key, ®istry).await);++ let answers = receiver.await.unwrap().unwrap();+ assert_eq!(answers[0].selected, vec!["Small"]);+ assert!(app.ask_queue.is_empty());+ }+#[tokio::test]async fn ask_keyboard_handles_multi_select_and_cancellation() {let mut app = test_app();@@ -8662,6 +8694,34 @@ mod tests {assert!(app.ask_queue.is_empty());}+ #[tokio::test]+ async fn ask_paste_routes_to_custom_editor() {+ let mut app = test_app();+ let registry = crate::core::agent::interaction::new_registry();+ let (request_id, receiver) = crate::core::agent::interaction::register(®istry).await;+ app.apply(StreamEvent::AskRequest {+ request_id,+ request: ask_request(false, false),+ });++ press_ask(&mut app, ®istry, KeyCode::Down).await;+ press_ask(&mut app, ®istry, KeyCode::Down).await;+ press_ask(&mut app, ®istry, KeyCode::Enter).await;+ assert!(app.ask_queue.front().unwrap().editing_custom);++ handle_ask_paste(&mut app, "pasted answer");+ assert_eq!(+ app.ask_queue.front().unwrap().custom_input,+ "pasted answer"+ );+ assert!(app.input.is_empty(), "paste leaked into chat composer");++ press_ask(&mut app, ®istry, KeyCode::Enter).await;+ let answers = receiver.await.unwrap().unwrap();+ assert_eq!(answers[0].custom_input.as_deref(), Some("pasted answer"));+ assert!(app.ask_queue.is_empty());+ }+#[test]fn split_reasoning_separates_think_blocks() {let segs = split_reasoning("before<think>hidden</think>after");