A command-line tool to convert handwritten notes to markdown files
feat: added support for openai api compatible clients
Tejas Raskarf57da0b608eb
diff --git a/src/clients/openai_client.rs b/src/clients/openai_client.rsindex 992403c..091377a 100644--- a/src/clients/openai_client.rs+++ b/src/clients/openai_client.rs@@ -63,12 +63,17 @@ pub struct OpenAIClient {client: Client,url: String,model: String,- api_key: String,+ api_key: Option<String>,prompt: Option<String>,}impl OpenAIClient {- pub fn new(url: String, model: String, api_key: String, prompt: Option<String>) -> Self {+ pub fn new(+ url: String,+ model: String,+ api_key: Option<String>,+ prompt: Option<String>,+ ) -> Self {Self {client: Client::new(),url,@@ -112,13 +117,13 @@ impl AiProvider for OpenAIClient {}],};- let response = self- .client- .post(&url)- .header("Authorization", format!("Bearer {}", self.api_key))- .json(&request_body)- .send()- .await?;+ let mut request = self.client.post(&url);++ if let Some(api_key) = &self.api_key {+ request = request.header("Authorization", format!("Bearer {}", api_key));+ }++ let response = request.json(&request_body).send().await?;let status = response.status();let response_body = response.text().await?;diff --git a/src/config.rs b/src/config.rsindex 4b3ca74..41a9ff2 100644--- a/src/config.rs+++ b/src/config.rs@@ -9,6 +9,7 @@ pub struct Config {pub gemini: Option<GeminiConfig>,pub ollama: Option<OllamaConfig>,pub claude: Option<ClaudeConfig>,+ pub openai: Option<OpenAIConfig>,}#[derive(Serialize, Deserialize, Debug, Default)]@@ -28,6 +29,13 @@ pub struct OllamaConfig {pub model: String,}+#[derive(Serialize, Deserialize, Debug, Default)]+pub struct OpenAIConfig {+ pub url: String,+ pub model: String,+ pub api_key: Option<String>,+}+pub fn get_config_path() -> Option<PathBuf> {ProjectDirs::from("com", "company", "notedmd").map(|dirs| {let config_dir = dirs.config_dir();diff --git a/src/error.rs b/src/error.rsindex 6050bfc..26faf0c 100644--- a/src/error.rs+++ b/src/error.rs@@ -41,6 +41,11 @@ pub enum NotedError {#[error(" Claude is not configured properly. Please run 'notedmd config --edit' to set it up.")]ClaudeNotConfigured,+ #[error(+ " OpenAI/LM Studio is not configured properly. Please run 'notedmd config --edit' to set it up."+ )]+ OpenAINotConfigured,+#[error(" No active provider. Please run 'notedmd config --edit' to set a provider.")]NoActiveProvider,diff --git a/src/main.rs b/src/main.rsindex 78fb3c7..efa9be5 100644--- a/src/main.rs+++ b/src/main.rs@@ -21,6 +21,8 @@ use indicatif::ProgressStyle;use crate::clients::claude_client::ClaudeClient;use crate::clients::gemini_client::GeminiClient;use crate::clients::ollama_client::OllamaClient;+use crate::clients::openai_client::OpenAIClient;+use crate::config::OpenAIConfig;use std::path::Path;use ui::{ascii_art, print_clean_config};@@ -163,6 +165,7 @@ async fn run() -> Result<(), NotedError> {"Gemini API (Cloud-based, requires API key)","Claude API (Cloud-based, requires API key)","Ollama (Local, requires Ollama to be set up)",+ "OpenAI Compatible API (Cloud/Local, works with LM Studio)",];let selected_provider = Select::with_theme(&ColorfulTheme::default()).with_prompt("Choose your AI provider")@@ -230,6 +233,38 @@ async fn run() -> Result<(), NotedError> {config.save()?;println!("{}", "Config saved successfully.".green());}+ 3 => {+ let url = Input::with_theme(&ColorfulTheme::default())+ .with_prompt("Server url")+ .default("http://localhost:1234".to_string())+ .interact_text()?;++ let model = Input::with_theme(&ColorfulTheme::default())+ .with_prompt("Model")+ .default("gemma3:27b".to_string())+ .interact_text()?;++ let api_key_str = Password::with_theme(&ColorfulTheme::default())+ .with_prompt("Enter your API key (Optional, press Enter if none): ")+ .allow_empty_password(true)+ .interact()?;++ let api_key = if api_key_str.is_empty() {+ None+ } else {+ Some(api_key_str)+ };++ let mut config = Config::load()?;+ config.active_provider = Some("openai".to_string());+ config.openai = Some(OpenAIConfig {+ url,+ model,+ api_key,+ });+ config.save()?;+ println!("{}", "Config saved successfully.".green());+ }_ => unreachable!(),}println!(@@ -250,6 +285,7 @@ async fn run() -> Result<(), NotedError> {"gemini" => config.gemini.is_some(),"claude" => config.claude.is_some(),"ollama" => config.ollama.is_some(),+ "openai" => config.openai.is_some(),_ => {eprintln!("Invalid provider '{}'. Please choose from 'gemini', 'claude', or 'ollama'.",@@ -337,6 +373,24 @@ async fn run() -> Result<(), NotedError> {Box::new(ClaudeClient::new(api_key, model, prompt))}+ Some("openai") => {+ let url = if let Some(openai_config) = &config.openai {+ openai_config.url.clone()+ } else {+ return Err(NotedError::OpenAINotConfigured);+ };+ let model = if let Some(openai_config) = &config.openai {+ openai_config.model.clone()+ } else {+ return Err(NotedError::OpenAINotConfigured);+ };+ let api_key = if let Some(openai_config) = &config.openai {+ openai_config.api_key.clone()+ } else {+ return Err(NotedError::OpenAINotConfigured);+ };+ Box::new(OpenAIClient::new(url, model, api_key, prompt))+ }_ => return Err(NotedError::NoActiveProvider),};diff --git a/src/ui.rs b/src/ui.rsindex a11103a..fbdeac6 100644--- a/src/ui.rs+++ b/src/ui.rs@@ -61,4 +61,19 @@ pub fn print_clean_config(config: Config) {} else {println!(" (Not Configured)");}++ println!("{}", "OpenAI (Compatible)".bold());+ if let Some(openai_config) = config.openai {+ println!(" URL: {}", openai_config.url);+ println!(" Model: {}", openai_config.model);+ let api_key = if openai_config.api_key.is_none() {+ "API key empty.".to_string()+ } else {+ openai_config.api_key.unwrap()+ };++ println!(" API Key: {}", api_key);+ } else {+ println!(" (Not Configured)");+ }}