Listen for ctrl-c during provider request (#5585)
This commit is contained in:
@@ -74,6 +74,7 @@ pub fn get_input(
|
||||
Ok(text) => text,
|
||||
Err(e) => match e {
|
||||
rustyline::error::ReadlineError::Interrupted => return Ok(InputResult::Exit),
|
||||
rustyline::error::ReadlineError::Eof => return Ok(InputResult::Exit),
|
||||
_ => return Err(e.into()),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -13,6 +13,8 @@ use crate::session::task_execution_display::{
|
||||
use goose::conversation::Conversation;
|
||||
use std::io::Write;
|
||||
use std::str::FromStr;
|
||||
use tokio::signal::ctrl_c;
|
||||
use tokio_util::task::AbortOnDropHandle;
|
||||
|
||||
pub use self::export::message_to_markdown;
|
||||
pub use builder::{build_session, SessionBuilderConfig, SessionSettings};
|
||||
@@ -775,8 +777,6 @@ impl CliSession {
|
||||
interactive: bool,
|
||||
cancel_token: CancellationToken,
|
||||
) -> Result<()> {
|
||||
let cancel_token_clone = cancel_token.clone();
|
||||
|
||||
// Cache the output format check to avoid repeated string comparisons in the hot loop
|
||||
let is_json_mode = self.output_format == "json";
|
||||
|
||||
@@ -790,6 +790,15 @@ impl CliSession {
|
||||
.messages
|
||||
.last()
|
||||
.ok_or_else(|| anyhow::anyhow!("No user message"))?;
|
||||
|
||||
let cancel_token_interrupt = cancel_token.clone();
|
||||
let handle = tokio::spawn(async move {
|
||||
if ctrl_c().await.is_ok() {
|
||||
cancel_token_interrupt.cancel();
|
||||
}
|
||||
});
|
||||
let _drop_handle = AbortOnDropHandle::new(handle);
|
||||
|
||||
let mut stream = self
|
||||
.agent
|
||||
.reply(
|
||||
@@ -800,6 +809,7 @@ impl CliSession {
|
||||
.await?;
|
||||
|
||||
let mut progress_bars = output::McpSpinners::new();
|
||||
let cancel_token_clone = cancel_token.clone();
|
||||
|
||||
use futures::StreamExt;
|
||||
loop {
|
||||
@@ -1075,8 +1085,7 @@ impl CliSession {
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
_ = tokio::signal::ctrl_c() => {
|
||||
cancel_token_clone.cancel();
|
||||
_ = cancel_token_clone.cancelled() => {
|
||||
drop(stream);
|
||||
if let Err(e) = self.handle_interrupted_messages(true).await {
|
||||
eprintln!("Error handling interruption: {}", e);
|
||||
|
||||
Reference in New Issue
Block a user