Listen for ctrl-c during provider request (#5585)

This commit is contained in:
Jack Amadeo
2025-11-05 22:44:36 -05:00
committed by GitHub
parent f7e2bcd98a
commit db0b78b4b6
4 changed files with 16 additions and 5 deletions
Generated
+1
View File
@@ -7055,6 +7055,7 @@ dependencies = [
"futures-core",
"futures-io",
"futures-sink",
"futures-util",
"pin-project-lite",
"tokio",
]
+1 -1
View File
@@ -54,7 +54,7 @@ tower-http = { version = "0.5", features = ["cors", "fs", "auth"] }
http = "1.0"
webbrowser = "1.0"
indicatif = "0.17.11"
tokio-util = { version = "0.7.15", features = ["compat"] }
tokio-util = { version = "0.7.15", features = ["compat", "rt"] }
is-terminal = "0.4.16"
anstream = "0.6.18"
url = "2.5.7"
+1
View File
@@ -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 -4
View File
@@ -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);