improved error handling
introduced toml config
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
use std::collections::HashMap;
|
||||
use log::error;
|
||||
use ollama_rs::headers::{HeaderMap, HeaderValue};
|
||||
use ollama_rs::Ollama;
|
||||
use rmcp::model::Implementation;
|
||||
use serde::Deserialize;
|
||||
use thiserror::Error;
|
||||
use url::Url;
|
||||
use crate::mcp;
|
||||
use crate::mcp::{guaranteed_mcp_server_name, MCPClient};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
ollama: OllamaConfig,
|
||||
#[serde(rename = "mcp-servers")]
|
||||
mcp_servers: Vec<MCPServerConfig>
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigLoadingError{
|
||||
#[error(transparent)]
|
||||
IoError(#[from] std::io::Error),
|
||||
#[error(transparent)]
|
||||
ParseError(#[from] toml::de::Error),
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub async fn from_file() -> Result<Self, ConfigLoadingError> {
|
||||
let toml_string = tokio::fs::read_to_string("assist.toml").await?;
|
||||
Ok(toml::from_str(&toml_string)?)
|
||||
}
|
||||
|
||||
pub fn ollama_config(&self) -> &OllamaConfig {
|
||||
&self.ollama
|
||||
}
|
||||
|
||||
pub fn ollama_instance(&self) -> Ollama {
|
||||
let ollama_default_url = Url::parse("http://127.0.0.1:11434").unwrap();
|
||||
|
||||
let mut ollama_headers = HeaderMap::new();
|
||||
|
||||
if let Some(authorization_header) = &self.ollama.authorization {
|
||||
ollama_headers.append("Authorization", HeaderValue::from_str(&authorization_header).unwrap());
|
||||
}
|
||||
|
||||
let mut ollama = Ollama::from_url(self.ollama.url.clone().unwrap_or(ollama_default_url));
|
||||
ollama.set_headers(Some(ollama_headers));
|
||||
|
||||
ollama
|
||||
}
|
||||
|
||||
pub async fn mcp_clients(&self) -> HashMap<String, MCPClient> {
|
||||
let mut mcp_clients: HashMap<String, MCPClient> = HashMap::new();
|
||||
|
||||
for mcp_server in &self.mcp_servers {
|
||||
let client = mcp::get_client(
|
||||
mcp_server.url.as_str(),
|
||||
mcp_server.authorization.clone(),
|
||||
Implementation::new("OwnAssist", "0.0.1"),
|
||||
).await.unwrap();
|
||||
|
||||
let server_name = guaranteed_mcp_server_name(mcp_server.name.clone(), &client);
|
||||
let server_data = client;
|
||||
|
||||
mcp_clients.insert(server_name.clone(), server_data);
|
||||
}
|
||||
|
||||
mcp_clients
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct OllamaConfig {
|
||||
pub url: Option<Url>,
|
||||
pub model: OllamaModelConfig,
|
||||
pub authorization: Option<String>, // TODO: implement
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct OllamaModelConfig {
|
||||
pub from_model: String,
|
||||
pub name: String,
|
||||
pub context_size: Option<u64>,
|
||||
pub temperature: Option<f32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct MCPServerConfig {
|
||||
pub name: Option<String>,
|
||||
pub url: Url,
|
||||
pub authorization: Option<String>, // should probably implement oauth some time
|
||||
}
|
||||
Reference in New Issue
Block a user