use std::{
collections::{BTreeMap, BTreeSet},
io::{self, Write},
};
use rho_protocol::{
Ask, InputAnswer, InputRequest, InputRequestId, Operation, PathScope, Replay, Resolution,
SessionEvent,
};
use super::prompt::{Prompt, Reply};
use crate::control_chars::{escape_controls, LineBreaks};
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum Action {
Hold(InputRequestId),
Answer(InputRequestId, InputAnswer),
Steer(String),
Cancel,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum Refusal {
NotOpen,
Other(String),
}
pub(super) struct Typed<'a> {
pub(super) line: &'a str,
pub(super) at_ms: u64,
}
pub(super) struct Console<E = io::Stderr> {
err: E,
open: BTreeMap<InputRequestId, InputRequest>,
answered: BTreeSet<InputRequestId>,
held: BTreeSet<InputRequestId>,
input: Input,
since_ms: u64,
}
enum Input {
Free,
Answering(Prompt),
Closed,
}
impl Console {
pub(super) fn terminal() -> Self {
Self::new(io::stderr())
}
}
impl<E: Write> Console<E> {
pub(super) fn new(err: E) -> Self {
Self {
err,
open: BTreeMap::new(),
answered: BTreeSet::new(),
held: BTreeSet::new(),
input: Input::Free,
since_ms: 0,
}
}
pub(super) fn attached(
&mut self,
inputs: &[InputRequest],
replay: Replay,
now_ms: u64,
) -> io::Result<()> {
let gone: Vec<InputRequestId> = self
.open
.keys()
.filter(|id| inputs.iter().all(|request| request.id != **id))
.copied()
.collect();
for id in gone {
self.close(id, "it closed while this client caught up", now_ms)?;
}
if let (Replay::Reset, Input::Answering(prompt)) = (replay, &self.input) {
let text = format!(
"rho: the turn waits for {}\n{}",
asked(&self.open[&prompt.id()].ask),
prompt.opening()
);
self.say(&text)?;
}
for request in inputs {
match self.open.get_mut(&request.id) {
Some(known) => {
if known.timeout.is_some() && request.timeout.is_none() {
self.held.insert(request.id);
}
known.timeout = request.timeout.clone();
}
None => self.arrived(request.clone(), now_ms)?,
}
}
self.advance(now_ms)
}
pub(super) fn event(&mut self, event: &SessionEvent, now_ms: u64) -> io::Result<()> {
match event {
SessionEvent::InputRequested { request } if !self.open.contains_key(&request.id) => {
self.arrived((**request).clone(), now_ms)?;
}
SessionEvent::InputResolved { id, resolution } => {
self.close(*id, resolved(*resolution), now_ms)?;
}
SessionEvent::InputHeld { id } => {
if let Some(request) = self.open.get_mut(id) {
request.timeout = None;
self.held.insert(*id);
}
}
_ => return Ok(()),
}
self.advance(now_ms)
}
pub(super) fn line(&mut self, typed: Typed<'_>, now_ms: u64) -> io::Result<Vec<Action>> {
let line = typed.line.trim();
if line == "/cancel" {
return Ok(vec![Action::Cancel]);
}
if typed.at_ms < self.since_ms {
if !line.is_empty() {
let shown = match self.input {
Input::Answering(_) => "before this request was asked",
Input::Free | Input::Closed => "for a request no longer asked",
};
self.tell(&format!("ignored \"{line}\", typed {shown}"))?;
}
return Ok(Vec::new());
}
match &mut self.input {
Input::Closed => Ok(Vec::new()),
Input::Free => match line {
"" => Ok(Vec::new()),
command if is_command(command) => {
self.tell(&format!(
"{command} is not a command here; /cancel cancels the turn"
))?;
Ok(Vec::new())
}
text => Ok(vec![Action::Steer(text.to_owned())]),
},
Input::Answering(prompt) => {
let id = prompt.id();
if line == "/hold" {
return self.hold_command(id);
}
match prompt.reply(line) {
Reply::Answer(answer) => {
self.answered.insert(id);
self.enter(Input::Free, now_ms);
self.advance(now_ms)?;
Ok(vec![Action::Answer(id, answer)])
}
Reply::Next => {
let asking = prompt.asking();
self.say(&asking)?;
Ok(self.stop_timeout(id).into_iter().collect())
}
Reply::Retry(why) => {
let asking = prompt.asking();
self.say(&format!("rho: {why}\n{asking}"))?;
Ok(self.stop_timeout(id).into_iter().collect())
}
}
}
}
}
pub(super) fn refused(
&mut self,
id: InputRequestId,
refusal: Refusal,
now_ms: u64,
) -> io::Result<()> {
self.answered.remove(&id);
match refusal {
Refusal::NotOpen => {
self.open.remove(&id);
self.held.remove(&id);
self.tell("could not answer the request: it is no longer open")
}
Refusal::Other(why) => {
self.tell(&format!("could not answer the request: {why}"))?;
self.advance(now_ms)
}
}
}
pub(super) fn tell(&mut self, notice: &str) -> io::Result<()> {
writeln!(
self.err,
"rho: {}",
escape_controls(notice, LineBreaks::Escape)
)
}
pub(super) fn closed(&mut self) -> io::Result<()> {
let previous = std::mem::replace(&mut self.input, Input::Closed);
match previous {
Input::Answering(prompt) if self.held.contains(&prompt.id()) => {
self.tell("no more input; another client can answer it, and it no longer times out")
}
Input::Answering(_) => self.tell("no more input; another client can answer it"),
Input::Free | Input::Closed => Ok(()),
}
}
fn hold_command(&mut self, id: InputRequestId) -> io::Result<Vec<Action>> {
let hold = self.stop_timeout(id);
self.tell(match hold {
Some(_) => "holding it: the daemon waits for your answer",
None => "it does not time out",
})?;
Ok(hold.into_iter().collect())
}
fn stop_timeout(&mut self, id: InputRequestId) -> Option<Action> {
self.open.get(&id)?.timeout.as_ref()?;
Some(Action::Hold(id))
}
fn answering(&self, id: InputRequestId) -> bool {
matches!(&self.input, Input::Answering(prompt) if prompt.id() == id)
}
fn enter(&mut self, input: Input, now_ms: u64) {
self.input = input;
self.since_ms = now_ms;
}
fn arrived(&mut self, request: InputRequest, now_ms: u64) -> io::Result<()> {
let id = request.id;
self.open.insert(id, request);
self.advance(now_ms)?;
if self.answering(id) {
return Ok(());
}
let request = &self.open[&id];
let mut text = format!("rho: the turn waits for {}\n", asked(&request.ask));
if Prompt::new(request).is_none() {
text.push_str("rho: this rho cannot answer it; answer it from another client\n");
} else if matches!(self.input, Input::Closed) {
text.push_str("rho: not reading input; answer it from another client\n");
} else if let Some(timeout) = &request.timeout {
text.push_str(&format!(
"rho: the daemon answers it in {}s unless it is answered first ({})\n",
seconds_left(timeout.deadline_ms, now_ms),
one_line(&timeout.reason)
));
}
self.say(&text)
}
fn close(&mut self, id: InputRequestId, how: &str, now_ms: u64) -> io::Result<()> {
let Some(request) = self.open.remove(&id) else {
return Ok(());
};
self.held.remove(&id);
if self.answered.remove(&id) {
return Ok(());
}
if self.answering(id) {
self.enter(Input::Free, now_ms);
}
let asked = asked(&request.ask);
self.say(&format!(
"rho: the turn no longer waits for {asked} ({how})\n"
))
}
fn advance(&mut self, now_ms: u64) -> io::Result<()> {
if !matches!(self.input, Input::Free) {
return Ok(());
}
let Some(prompt) = self
.open
.values()
.filter(|request| !self.answered.contains(&request.id))
.find_map(Prompt::new)
else {
return Ok(());
};
let request = &self.open[&prompt.id()];
let mut text = format!(
"rho: the turn waits for {}\n{}",
asked(&request.ask),
prompt.opening()
);
if let Some(timeout) = &request.timeout {
text.push_str(&format!(
"rho: the daemon answers in {}s unless you answer first or type /hold ({})\n",
seconds_left(timeout.deadline_ms, now_ms),
one_line(&timeout.reason)
));
}
self.enter(Input::Answering(prompt), now_ms);
self.say(&text)
}
fn say(&mut self, text: &str) -> io::Result<()> {
write!(self.err, "{text}")
}
}
pub(super) fn one_line(field: &str) -> String {
escape_controls(field, LineBreaks::Escape)
}
fn is_command(line: &str) -> bool {
line.strip_prefix('/')
.is_some_and(|word| !word.is_empty() && word.chars().all(|ch| ch.is_ascii_alphabetic()))
}
fn seconds_left(deadline_ms: u64, now_ms: u64) -> u64 {
deadline_ms.saturating_sub(now_ms).div_ceil(1000)
}
fn asked(ask: &Ask) -> String {
match ask {
Ask::Approval(approval) => {
let mut text = format!("an approval to {}", operation(&approval.operation));
if !approval.reason.is_empty() {
text.push_str(&format!(" ({})", one_line(&approval.reason)));
}
text
}
Ask::PlanApproval(_) => "its plan to be approved".to_owned(),
Ask::Question(question) => format!("answers to \"{}\"", one_line(&question.title)),
_ => "input".to_owned(),
}
}
fn operation(operation: &Operation) -> String {
let path = |verb: &str, path: &std::path::Path, scope: &PathScope| {
let outside = match scope {
PathScope::Unrestricted => " (outside the workspace)",
_ => "",
};
format!("{verb} {}{outside}", one_line(&path.to_string_lossy()))
};
match operation {
Operation::ReadPath { path: at, scope } => path("read", at, scope),
Operation::WritePath { path: at, scope } => path("write", at, scope),
Operation::DiscoverInstructions { path: at, scope } => {
path("discover instructions in", at, scope)
}
Operation::RunProcess {
program,
args,
shell_command,
cwd,
} => {
let command = match shell_command {
Some(command) => command.clone(),
None => std::iter::once(program.to_string_lossy().into_owned())
.chain(args.iter().cloned())
.collect::<Vec<_>>()
.join(" "),
};
format!(
"run {} in {}",
one_line(&command),
one_line(&cwd.to_string_lossy())
)
}
Operation::Network { url: Some(url) } => format!("access {}", one_line(url)),
Operation::Network { url: None } => "access the network".to_owned(),
Operation::LoadSkill { name, .. } => format!("load skill {}", one_line(name)),
Operation::ControlSession {
action,
session,
workspace,
text,
} => format!(
"{} session{} in {}{}",
action.as_str(),
session
.as_ref()
.map(|session| format!(" {}", one_line(session.as_str())))
.unwrap_or_default(),
one_line(&workspace.to_string_lossy()),
text.as_deref()
.map(|text| format!(": {}", one_line(text)))
.unwrap_or_default()
),
_ => "do something this rho cannot name".to_owned(),
}
}
fn resolved(resolution: Resolution) -> &'static str {
match resolution {
Resolution::Answered => "another client answered it",
Resolution::TimedOut => "the daemon answered it at its timeout",
Resolution::Withdrawn => "it was withdrawn",
_ => "it closed",
}
}
#[cfg(test)]
#[path = "console_tests.rs"]
mod tests;