add more cli features and styling
refactoring
This commit is contained in:
@@ -1,20 +1,47 @@
|
||||
use std::time::Duration;
|
||||
use console::style;
|
||||
use crate::translate;
|
||||
use crate::human_interface::{HumanInterface, HumanInterfaceError};
|
||||
use dialoguer::theme::ColorfulTheme;
|
||||
use dialoguer::{Confirm, Input};
|
||||
use own_mcp::mcp::chat::PermissionAnswer;
|
||||
use crate::tlt;
|
||||
|
||||
pub struct CommandLine;
|
||||
pub struct CommandLine {
|
||||
progress_bar: Option<indicatif::ProgressBar>,
|
||||
}
|
||||
|
||||
impl HumanInterface for CommandLine {
|
||||
fn agent_message(&self, message: String) {
|
||||
println!("ai: {}", message);
|
||||
impl CommandLine {
|
||||
pub fn new() -> CommandLine {
|
||||
CommandLine { progress_bar: None }
|
||||
}
|
||||
|
||||
async fn expect_user_message(&self) -> Result<String, HumanInterfaceError> {
|
||||
fn spinner() -> indicatif::ProgressBar {
|
||||
indicatif::ProgressBar::new_spinner()
|
||||
}
|
||||
}
|
||||
|
||||
impl HumanInterface for CommandLine {
|
||||
fn agent_message(&mut self, message: String) {
|
||||
// stopped waiting for message
|
||||
if let Some(progress_bar) = &self.progress_bar {
|
||||
progress_bar.finish_and_clear();
|
||||
self.progress_bar = None;
|
||||
}
|
||||
|
||||
println!("{}: {}",style( tlt!("assistant")).bold().italic(),message);
|
||||
}
|
||||
|
||||
async fn expect_user_message(&mut self) -> Result<String, HumanInterfaceError> {
|
||||
let user_message: String = Input::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("you")
|
||||
.with_prompt(tlt!("you"))
|
||||
.interact_text()
|
||||
.map_err(|e| HumanInterfaceError::IoError(e.into()))?;
|
||||
|
||||
// waiting for agent response
|
||||
self.progress_bar = Some(Self::spinner());
|
||||
self.progress_bar.clone().unwrap().enable_steady_tick(Duration::from_millis(100));
|
||||
|
||||
Ok(user_message)
|
||||
}
|
||||
|
||||
@@ -22,17 +49,23 @@ impl HumanInterface for CommandLine {
|
||||
mcp_server_name: String,
|
||||
tool_name: String,
|
||||
) -> Result<PermissionAnswer, HumanInterfaceError> {
|
||||
if let Some(progress_bar) = &self.progress_bar {
|
||||
progress_bar.finish_and_clear();
|
||||
}
|
||||
|
||||
let confirmation = Confirm::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt(format!("Allow usage of {mcp_server_name}:{tool_name}"))
|
||||
.interact()
|
||||
.map_err(|e| HumanInterfaceError::IoError(e.into()))?;
|
||||
|
||||
match confirmation {
|
||||
true => Ok(PermissionAnswer::Granted),
|
||||
false => Ok(PermissionAnswer::Denied),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&self) -> Result<(), HumanInterfaceError> {
|
||||
async fn run(&mut self) -> Result<(), HumanInterfaceError> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,11 +12,11 @@ pub enum HumanInterfaceError {
|
||||
}
|
||||
|
||||
pub trait HumanInterface {
|
||||
fn agent_message(&self, message: String);
|
||||
fn agent_message(&mut self, message: String);
|
||||
|
||||
fn expect_user_message(&self) -> impl Future<Output = Result<String, HumanInterfaceError>>;
|
||||
fn expect_user_message(&mut self) -> impl Future<Output = Result<String, HumanInterfaceError>>;
|
||||
|
||||
fn ask_for_permission(&self, mcp_server_name: String, tool_name:String) -> impl Future<Output = Result<PermissionAnswer, HumanInterfaceError>>;
|
||||
|
||||
fn run(&self) -> impl Future<Output = Result<(), HumanInterfaceError>>;
|
||||
fn run(&mut self) -> impl Future<Output = Result<(), HumanInterfaceError>>;
|
||||
}
|
||||
Reference in New Issue
Block a user