feat(cli): report session status via OSC 7501 (#12928)
This commit is contained in:
@@ -5,10 +5,12 @@ mod elicitation;
|
||||
mod input;
|
||||
mod output;
|
||||
mod paste;
|
||||
mod program_status;
|
||||
pub mod streaming_buffer;
|
||||
mod task_execution_display;
|
||||
mod thinking;
|
||||
|
||||
use crate::session::program_status::{BlockedKind, ProgramStatus};
|
||||
use crate::session::task_execution_display::{
|
||||
format_task_execution_notification, TASK_EXECUTION_NOTIFICATION_TYPE,
|
||||
};
|
||||
@@ -559,6 +561,7 @@ impl CliSession {
|
||||
if !banners.is_empty() {
|
||||
output::display_banner(&banners);
|
||||
}
|
||||
program_status::enable(self.output_format == "text");
|
||||
|
||||
let result = self.run_interactive(prompt).await;
|
||||
|
||||
@@ -567,12 +570,16 @@ impl CliSession {
|
||||
.await;
|
||||
|
||||
if result.is_ok() {
|
||||
program_status::report(ProgramStatus::Clear, None);
|
||||
println!(
|
||||
"\n {} {}",
|
||||
console::style("●").red(),
|
||||
console::style(format!("session closed · {}", self.session_id)).dim()
|
||||
);
|
||||
} else {
|
||||
program_status::report(ProgramStatus::Error, None);
|
||||
}
|
||||
program_status::disable();
|
||||
|
||||
result
|
||||
}
|
||||
@@ -624,6 +631,7 @@ impl CliSession {
|
||||
.collect();
|
||||
|
||||
output::run_status_hook("waiting");
|
||||
program_status::report_idle();
|
||||
let input = input::get_input(&mut editor, Some(&conversation_strings))?;
|
||||
if matches!(input, InputResult::Exit) {
|
||||
break;
|
||||
@@ -1321,6 +1329,7 @@ impl CliSession {
|
||||
});
|
||||
let _drop_handle = AbortOnDropHandle::new(handle);
|
||||
|
||||
program_status::report(ProgramStatus::Working, None);
|
||||
let mut stream = self
|
||||
.agent
|
||||
.reply(
|
||||
@@ -1339,6 +1348,7 @@ impl CliSession {
|
||||
let mut first_token_at: Option<Instant> = None;
|
||||
let mut last_usage: Option<ProviderUsage> = None;
|
||||
let mut stream_error = None;
|
||||
let mut input_failed = false;
|
||||
|
||||
use futures::StreamExt;
|
||||
loop {
|
||||
@@ -1351,7 +1361,13 @@ impl CliSession {
|
||||
}
|
||||
if let Some(confirmation_request) = find_tool_confirmation(&message) {
|
||||
let selected_permission = if interactive {
|
||||
prompt_tool_confirmation(&confirmation_request)?
|
||||
program_status::report(
|
||||
ProgramStatus::Blocked(BlockedKind::Permission),
|
||||
Some(&confirmation_request.tool_name),
|
||||
);
|
||||
let permission = prompt_tool_confirmation(&confirmation_request)?;
|
||||
program_status::report(ProgramStatus::Working, None);
|
||||
permission
|
||||
} else {
|
||||
// Non-interactive/headless mode: refuse to run in
|
||||
// Approve/SmartApprove modes since auto-allowing would
|
||||
@@ -1424,8 +1440,13 @@ impl CliSession {
|
||||
output::hide_thinking();
|
||||
let _ = progress_bars.hide();
|
||||
|
||||
program_status::report(
|
||||
ProgramStatus::Blocked(BlockedKind::Question),
|
||||
Some(&elicitation_message),
|
||||
);
|
||||
match elicitation::collect_elicitation_input(&elicitation_message, &schema, &cancel_token_clone) {
|
||||
Ok(input) => {
|
||||
program_status::report(ProgramStatus::Working, None);
|
||||
match &input.action {
|
||||
ElicitationAction::Decline => {
|
||||
output::render_text("Information request declined.", Some(Color::Yellow), true);
|
||||
@@ -1460,6 +1481,7 @@ impl CliSession {
|
||||
}
|
||||
Err(e) => {
|
||||
output::render_error(&format!("Failed to collect input: {}", e));
|
||||
input_failed = true;
|
||||
cancel_token_clone.cancel();
|
||||
drain_stopped_run(&mut stream, &mut self.messages, &mut last_usage).await;
|
||||
break;
|
||||
@@ -1535,6 +1557,12 @@ impl CliSession {
|
||||
}
|
||||
}
|
||||
|
||||
let failed = input_failed
|
||||
|| stream_error.is_some()
|
||||
|| self
|
||||
.messages
|
||||
.last()
|
||||
.is_some_and(|message| message.error_kind().is_some());
|
||||
let terminal_error = headless_run_error(
|
||||
interactive,
|
||||
cancel_token_clone.is_cancelled(),
|
||||
@@ -1633,6 +1661,16 @@ impl CliSession {
|
||||
|
||||
if interactive {
|
||||
output::emit_attention_bell();
|
||||
program_status::report(
|
||||
if failed {
|
||||
ProgramStatus::Error
|
||||
} else if cancel_token_clone.is_cancelled() {
|
||||
ProgramStatus::Idle
|
||||
} else {
|
||||
ProgramStatus::Done
|
||||
},
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(error) = terminal_error {
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use base64::Engine;
|
||||
use goose::config::Config;
|
||||
use std::io::{IsTerminal, Write};
|
||||
use std::sync::Mutex;
|
||||
|
||||
const MAX_MSG_BYTES: usize = 2048;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum BlockedKind {
|
||||
Permission,
|
||||
Question,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum ProgramStatus {
|
||||
Idle,
|
||||
Working,
|
||||
Done,
|
||||
Error,
|
||||
Blocked(BlockedKind),
|
||||
Clear,
|
||||
}
|
||||
|
||||
struct Reporter {
|
||||
enabled: bool,
|
||||
last: Option<ProgramStatus>,
|
||||
}
|
||||
|
||||
static REPORTER: Mutex<Reporter> = Mutex::new(Reporter {
|
||||
enabled: false,
|
||||
last: None,
|
||||
});
|
||||
|
||||
pub fn enable(text_output: bool) {
|
||||
REPORTER.lock().unwrap().enabled = text_output
|
||||
&& std::io::stdout().is_terminal()
|
||||
&& Config::global()
|
||||
.get_param::<bool>("GOOSE_CLI_PROGRAM_STATUS")
|
||||
.unwrap_or(true);
|
||||
}
|
||||
|
||||
pub fn disable() {
|
||||
*REPORTER.lock().unwrap() = Reporter {
|
||||
enabled: false,
|
||||
last: None,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn report(status: ProgramStatus, msg: Option<&str>) {
|
||||
let mut reporter = REPORTER.lock().unwrap();
|
||||
if !reporter.enabled {
|
||||
return;
|
||||
}
|
||||
reporter.last = Some(status);
|
||||
let mut stdout = std::io::stdout();
|
||||
let _ = stdout.write_all(encode(status, msg).as_bytes());
|
||||
let _ = stdout.flush();
|
||||
}
|
||||
|
||||
pub fn report_idle() {
|
||||
let last = REPORTER.lock().unwrap().last;
|
||||
if !matches!(last, Some(ProgramStatus::Done | ProgramStatus::Error)) {
|
||||
report(ProgramStatus::Idle, None);
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(status: ProgramStatus, msg: Option<&str>) -> String {
|
||||
let state = match status {
|
||||
ProgramStatus::Idle => "idle",
|
||||
ProgramStatus::Working => "working",
|
||||
ProgramStatus::Done => "done",
|
||||
ProgramStatus::Error => "error",
|
||||
ProgramStatus::Blocked(_) => "blocked",
|
||||
ProgramStatus::Clear => "clear",
|
||||
};
|
||||
let mut body = format!("state={state}");
|
||||
match status {
|
||||
ProgramStatus::Blocked(BlockedKind::Permission) => body.push_str(":kind=permission"),
|
||||
ProgramStatus::Blocked(BlockedKind::Question) => body.push_str(":kind=question"),
|
||||
_ => {}
|
||||
}
|
||||
body.push_str(":app=goose");
|
||||
if let Some(msg) = msg {
|
||||
let line: String = msg
|
||||
.chars()
|
||||
.map(|c| if c.is_control() { ' ' } else { c })
|
||||
.collect();
|
||||
let line = line.trim();
|
||||
let line = line
|
||||
.get(..line.floor_char_boundary(MAX_MSG_BYTES))
|
||||
.unwrap_or_default();
|
||||
if !line.is_empty() {
|
||||
body.push_str(":msg=");
|
||||
body.push_str(&STANDARD.encode(line));
|
||||
}
|
||||
}
|
||||
format!("\x1b]7501;{body}\x1b\\")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn encode_sanitizes_and_caps_msg() {
|
||||
assert_eq!(
|
||||
encode(
|
||||
ProgramStatus::Blocked(BlockedKind::Permission),
|
||||
Some("run\n\x1b]0;x\x07 shell")
|
||||
),
|
||||
format!(
|
||||
"\x1b]7501;state=blocked:kind=permission:app=goose:msg={}\x1b\\",
|
||||
STANDARD.encode("run ]0;x shell")
|
||||
)
|
||||
);
|
||||
|
||||
let long = "é".repeat(MAX_MSG_BYTES);
|
||||
let encoded = encode(ProgramStatus::Done, Some(&long));
|
||||
let msg = encoded
|
||||
.strip_prefix("\x1b]7501;state=done:app=goose:msg=")
|
||||
.and_then(|s| s.strip_suffix("\x1b\\"))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
STANDARD.decode(msg).unwrap(),
|
||||
"é".repeat(MAX_MSG_BYTES / 2).as_bytes()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user