use std::{
io::BufRead,
time::{SystemTime, UNIX_EPOCH},
};
use rho_protocol::{
methods::{SessionAnswer, SessionCancel, SessionHold, SessionSteer},
Client, ErrorCode, InputRequest, Replay, SessionAnswerParams, SessionCancelParams,
SessionEvent, SessionHoldParams, SessionId, SessionSteerParams,
};
use tokio::sync::mpsc;
use super::console::{Action, Console, Refusal, Typed};
use crate::{
app::automation::{self, AutomationExit},
cli::AttachInput,
stdio,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Lines {
Read,
Ignore,
}
impl Lines {
pub(super) fn of(input: AttachInput) -> Self {
match input {
AttachInput::Stdin => Self::Read,
AttachInput::Terminal if stdio::stdin_is_foreground_terminal() => Self::Read,
AttachInput::Terminal => Self::Ignore,
}
}
}
pub(super) struct Terminal {
console: Console,
lines: Option<mpsc::UnboundedReceiver<Line>>,
}
pub(super) struct Line {
text: String,
at_ms: u64,
}
impl Terminal {
pub(super) fn open(lines: Lines) -> Result<Self, AutomationExit> {
let mut console = Console::terminal();
let lines = match lines {
Lines::Read => Some(read_lines()),
Lines::Ignore => {
console.closed().map_err(automation::write_failed)?;
None
}
};
Ok(Self { console, lines })
}
pub(super) fn attached(
&mut self,
inputs: &[InputRequest],
replay: Replay,
) -> Result<(), AutomationExit> {
self.console
.attached(inputs, replay, now_ms())
.map_err(automation::write_failed)
}
pub(super) fn event(&mut self, event: &SessionEvent) -> Result<(), AutomationExit> {
self.console
.event(event, now_ms())
.map_err(automation::write_failed)
}
pub(super) async fn next_line(&mut self) -> Result<Line, AutomationExit> {
if let Some(lines) = &mut self.lines {
if let Some(line) = lines.recv().await {
return Ok(line);
}
self.lines = None;
self.console.closed().map_err(automation::write_failed)?;
}
std::future::pending().await
}
pub(super) async fn line(
&mut self,
client: &Client,
session: &SessionId,
line: &Line,
) -> Result<(), AutomationExit> {
let typed = Typed {
line: &line.text,
at_ms: line.at_ms,
};
let actions = self
.console
.line(typed, now_ms())
.map_err(automation::write_failed)?;
for action in actions {
self.perform(client, session, action).await?;
}
Ok(())
}
async fn perform(
&mut self,
client: &Client,
session: &SessionId,
action: Action,
) -> Result<(), AutomationExit> {
let session = session.clone();
let sent = match &action {
Action::Hold(request) => client
.request::<SessionHold>(&SessionHoldParams {
session,
request: *request,
})
.await
.map(drop),
Action::Answer(request, answer) => client
.request::<SessionAnswer>(&SessionAnswerParams {
session,
request: *request,
answer: answer.clone(),
})
.await
.map(drop),
Action::Steer(text) => client
.request::<SessionSteer>(&SessionSteerParams {
session,
text: text.clone(),
})
.await
.map(drop),
Action::Cancel => client
.request::<SessionCancel>(&SessionCancelParams { session })
.await
.map(drop),
};
let Err(error) = sent else {
return Ok(());
};
let notice = match (action, error.error_code()) {
(Action::Hold(_), Some(ErrorCode::InputNotOpen)) => return Ok(()),
(Action::Hold(_), _) => format!("could not hold the request: {error}"),
(Action::Answer(request, _), code) => {
let refusal = if code == Some(ErrorCode::InputNotOpen) {
Refusal::NotOpen
} else {
Refusal::Other(error.to_string())
};
return self
.console
.refused(request, refusal, now_ms())
.map_err(automation::write_failed);
}
(Action::Steer(_), Some(ErrorCode::NotSteerable)) => {
"could not steer the turn: no model request is taking input".to_owned()
}
(Action::Steer(_), _) => format!("could not steer the turn: {error}"),
(Action::Cancel, _) => format!("could not cancel the turn: {error}"),
};
self.console.tell(¬ice).map_err(automation::write_failed)
}
}
fn read_lines() -> mpsc::UnboundedReceiver<Line> {
let (lines, receiver) = mpsc::unbounded_channel();
std::thread::spawn(move || {
let mut stdin = std::io::stdin().lock();
let mut line = Vec::new();
loop {
line.clear();
match stdin.read_until(b'\n', &mut line) {
Ok(0) | Err(_) => return,
Ok(_) => {
let text = String::from_utf8_lossy(&line)
.trim_end_matches(['\n', '\r'])
.to_owned();
if lines
.send(Line {
text,
at_ms: now_ms(),
})
.is_err()
{
return;
}
}
}
}
});
receiver
}
fn now_ms() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |since| {
u64::try_from(since.as_millis()).unwrap_or(u64::MAX)
})
}