use async_trait::async_trait;
use mecha_core::tool::ask::Asker;
use tokio::sync::{mpsc, oneshot};
pub struct Question {
pub question: String,
pub options: Vec<String>,
pub reply: oneshot::Sender<Option<String>>,
}
pub struct TuiAsker {
tx: mpsc::UnboundedSender<Question>,
}
impl TuiAsker {
pub fn new() -> (Self, mpsc::UnboundedReceiver<Question>) {
let (tx, rx) = mpsc::unbounded_channel();
(TuiAsker { tx }, rx)
}
}
#[async_trait]
impl Asker for TuiAsker {
async fn ask(&self, question: &str, options: &[String]) -> Option<String> {
let (reply, answer) = oneshot::channel();
let sent = self.tx.send(Question {
question: question.to_string(),
options: options.to_vec(),
reply,
});
if sent.is_err() {
return None;
}
answer.await.ok().flatten()
}
}