add more cli features and styling
refactoring
This commit is contained in:
+87
-28
@@ -1,23 +1,56 @@
|
||||
mod config;
|
||||
mod model;
|
||||
mod human_interface;
|
||||
mod i18n;
|
||||
mod model;
|
||||
|
||||
|
||||
use crate::i18n::translate;
|
||||
use own_mcp::audio::{AudioClientTrait};
|
||||
use crate::config::Config;
|
||||
use crate::model::create_model_from_config;
|
||||
use crate::human_interface::HumanInterface;
|
||||
use crate::i18n::translate;
|
||||
use crate::model::create_model_from_config;
|
||||
use clap::Parser;
|
||||
use std::fmt::Display;
|
||||
use base64::Engine;
|
||||
|
||||
/// partial mcp client with cli and voice interaction
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, long_about = None)]
|
||||
struct Args {
|
||||
/// generate an encoded basic auth header
|
||||
#[clap(long, short, action)]
|
||||
basic_auth: bool,
|
||||
}
|
||||
|
||||
fn basic_auth_tool() {
|
||||
let username: String = dialoguer::Input::new()
|
||||
.with_prompt(tlt!("username"))
|
||||
.interact_text()
|
||||
.unwrap();
|
||||
|
||||
let password: String = dialoguer::Password::new()
|
||||
.with_prompt(tlt!("password"))
|
||||
.interact()
|
||||
.unwrap();
|
||||
|
||||
let encoded= base64::prelude::BASE64_STANDARD.encode(format!("{}:{}", username, password));
|
||||
println!("Basic {}", encoded);
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let args = Args::parse();
|
||||
if args.basic_auth {
|
||||
basic_auth_tool();
|
||||
return;
|
||||
}
|
||||
|
||||
env_logger::init();
|
||||
|
||||
let config = Config::from_file().inspect_err(|e|{
|
||||
log::error!("error loading assist.toml: {}", e);
|
||||
std::process::exit(1);
|
||||
}).unwrap();
|
||||
let config = Config::from_file()
|
||||
.inspect_err(|e| {
|
||||
log::error!("error loading assist.toml: {}", e);
|
||||
std::process::exit(1);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
/*let audio_client = config.audio_client();
|
||||
log::debug!("Audio server status {:?}", audio_client.status().await.inspect_err(|e|{
|
||||
@@ -28,34 +61,60 @@ async fn main() {
|
||||
let ollama = config.ollama_instance();
|
||||
let model_name = &config.ollama_config().model.name;
|
||||
|
||||
create_model_from_config(&ollama, &config.ollama_config().model).await.inspect_err(|e|{
|
||||
log::error!("failed creating ollama model {model_name}: {e}");
|
||||
std::process::exit(1);
|
||||
}).unwrap();
|
||||
create_model_from_config(&ollama, &config.ollama_config().model)
|
||||
.await
|
||||
.inspect_err(exit_msg!(format!(
|
||||
"failed creating ollama model {model_name}"
|
||||
)))
|
||||
.unwrap();
|
||||
|
||||
let mcp_clients = config.mcp_clients().await.inspect_err(|e|{
|
||||
log::error!("failed creating MCP clients: {e}");
|
||||
std::process::exit(1);
|
||||
}).unwrap();
|
||||
let mcp_clients = config
|
||||
.mcp_clients()
|
||||
.await
|
||||
.inspect_err(exit_msg!("failed creating MCP clients"))
|
||||
.unwrap();
|
||||
|
||||
let mut agent_chat = own_mcp::AgentChat::new(ollama, model_name.clone(), mcp_clients).await.inspect_err(
|
||||
|e| {
|
||||
log::error!("error creating agent: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
).unwrap();
|
||||
let mut agent_chat = own_mcp::AgentChat::new(ollama, model_name.clone(), mcp_clients)
|
||||
.await
|
||||
.inspect_err(exit_msg!("failed to create agent"))
|
||||
.unwrap();
|
||||
config.set_tool_permissions(&mut agent_chat).await.unwrap();
|
||||
|
||||
log::info!("all tools: {:#?}", agent_chat.get_all_tools().collect::<Vec<_>>());
|
||||
log::info!(
|
||||
"all tools: {:#?}",
|
||||
agent_chat.get_all_tools().collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
let human_interface = human_interface::cli::CommandLine{};
|
||||
let mut human_interface = human_interface::cli::CommandLine::new();
|
||||
human_interface.run().await.unwrap();
|
||||
|
||||
loop {
|
||||
let user_message = human_interface.expect_user_message().await.unwrap();
|
||||
let agent_message = agent_chat.message(user_message, async |mcp_server_name, tool_name|{
|
||||
human_interface.ask_for_permission(mcp_server_name, tool_name).await.expect("io should not fail")
|
||||
}).await.expect("failed communicating with agent");
|
||||
let immutable_interface = &human_interface;
|
||||
let agent_message = agent_chat
|
||||
.message(user_message, async |mcp_server_name, tool_name| {
|
||||
immutable_interface
|
||||
.ask_for_permission(mcp_server_name, tool_name)
|
||||
.await
|
||||
.inspect_err(exit_msg!("Human interface error"))
|
||||
.unwrap()
|
||||
})
|
||||
.await
|
||||
.inspect_err(exit_msg!("failed communicating with agent"))
|
||||
.unwrap();
|
||||
human_interface.agent_message(agent_message.content);
|
||||
}
|
||||
}
|
||||
|
||||
fn exit_with_error_message(error: impl Display, message: impl Display) {
|
||||
log::error!("{message}: {error}");
|
||||
println!("{}", message); // makes sure that message is printed even if logging is deactivated
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! exit_msg {
|
||||
($message:expr) => {
|
||||
|e| exit_with_error_message(e, $message)
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user