add working Audio HumanInterface

refactoring as always
remove secrets
This commit is contained in:
milan
2026-04-21 21:55:17 +02:00
parent ef468e8fd6
commit 20a5b01311
10 changed files with 474 additions and 81 deletions
+55 -27
View File
@@ -9,6 +9,8 @@ use crate::i18n::translate;
use crate::model::create_model_from_config;
use base64::Engine;
use clap::Parser;
use cpal::traits::HostTrait;
use own_mcp::AgentChat;
use std::fmt::Display;
/// partial mcp client with cli and voice interaction
@@ -35,6 +37,33 @@ fn basic_auth_tool() {
println!("Basic {}", encoded);
}
async fn chat_loop(mut human_interface: impl HumanInterface, mut agent_chat: AgentChat) {
loop {
let user_message = human_interface.expect_user_message().await.unwrap();
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 while asking for permission"
))
.unwrap()
})
.await
.inspect_err(exit_msg!("Failed communicating with agent."))
.unwrap();
human_interface
.agent_message(agent_message.content)
.await
.inspect_err(exit_msg!(
"Human interface error while trying to display message"
))
.unwrap();
}
}
#[tokio::main]
async fn main() {
let args = Args::parse();
@@ -64,7 +93,7 @@ async fn main() {
create_model_from_config(&ollama, &config.ollama_config().model)
.await
.inspect_err(exit_msg!(format!(
"failed creating ollama model {model_name}"
"failed creating ollama model `{model_name}`"
)))
.unwrap();
@@ -85,37 +114,36 @@ async fn main() {
agent_chat.get_all_tools().collect::<Vec<_>>()
);
let mut human_interface = human_interface::cli::CommandLine::new();
loop {
let user_message = human_interface.expect_user_message().await.unwrap();
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)
match config.interface {
config::HumanInterface::Cli => {
let human_interface = human_interface::cli::CommandLine::new();
chat_loop(human_interface, agent_chat).await;
}
config::HumanInterface::Audio => {
match config.audio_client() {
Some(audio_client) => {
let human_interface = human_interface::audio::Audio::new(
audio_client,
cpal::Host::default()
.default_input_device()
.expect("no default device"),
)
.await
.inspect_err(exit_msg!(
"Human interface error while asking for permission"
))
.unwrap()
})
.await
.inspect_err(exit_msg!("Failed communicating with agent."))
.unwrap();
human_interface
.agent_message(agent_message.content)
.await
.inspect_err(exit_msg!(
"Human interface error while trying to display message"
))
.unwrap();
}
.inspect_err(exit_msg!("failed creating audio client")).unwrap();
chat_loop(human_interface, agent_chat).await;
}
None => {
eprintln!("audio client was not configured");
}
};
}
};
}
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
eprintln!("{}", message); // makes sure that message is printed even if logging is deactivated
std::process::exit(1);
}