diff --git a/src-tauri/src/agent.rs b/src-tauri/src/agent.rs index c0dad87..ba98281 100644 --- a/src-tauri/src/agent.rs +++ b/src-tauri/src/agent.rs @@ -559,6 +559,7 @@ impl ModelClient for OllamaClient { pub struct OpenAiClient { pub url: String, pub model: String, + api_key: Option, http: reqwest::blocking::Client, } @@ -644,13 +645,14 @@ pub fn to_openai_messages(messages: &[Message]) -> Vec { } impl OpenAiClient { - pub fn new(url: &str, model: &str) -> Result { + pub fn new(url: &str, model: &str, api_key: Option) -> Result { let http = reqwest::blocking::Client::builder() .build() .map_err(|e| format!("HTTP-Client nicht baubar: {e}"))?; Ok(OpenAiClient { url: url.trim_end_matches('/').into(), model: model.into(), + api_key, http, }) } @@ -667,11 +669,11 @@ impl ModelClient for OpenAiClient { "stream": false, "temperature": 0, }); - let resp = self - .http - .post(&self.url) - .timeout(timeout) - .json(&body) + let mut req = self.http.post(&self.url).timeout(timeout).json(&body); + if let Some(key) = &self.api_key { + req = req.bearer_auth(key); + } + let resp = req .send() .map_err(|e| { if e.is_timeout() { diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 9ad8448..22b479c 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -27,6 +27,8 @@ pub struct Config { pub ollama_url: String, /// OpenAI-kompatibler Endpunkt, vollständige URL inkl. `/v1/chat/completions`. pub openai_url: String, + /// Name der Umgebungsvariable mit dem API-Schlüssel. Der Schlüssel selbst steht nicht in der Datei. + pub openai_api_key_env: String, /// Speicherlimit pro Lauf, Podman-Schreibweise. pub sandbox_memory: String, /// CPU-Anteil pro Lauf. @@ -43,6 +45,7 @@ impl Default for Config { backend: "ollama".into(), ollama_url: "http://127.0.0.1:11434".into(), openai_url: "http://192.168.12.4:8082/v1/chat/completions".into(), + openai_api_key_env: "STRATA_API_KEY".into(), sandbox_memory: limits.memory, sandbox_cpus: limits.cpus, } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 76e8f65..7cb4975 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -75,6 +75,7 @@ async fn start_batch( runtime: cfg.container_runtime.clone(), backend: cfg.backend.clone(), endpoint_url: cfg.endpoint_url(), + api_key: std::env::var(&cfg.openai_api_key_env).ok().filter(|k| !k.is_empty()), data_dir: paths.0.data_dir.clone(), limits: cfg.sandbox_limits(), }; diff --git a/src-tauri/src/runner.rs b/src-tauri/src/runner.rs index a45868f..1010f5c 100644 --- a/src-tauri/src/runner.rs +++ b/src-tauri/src/runner.rs @@ -36,6 +36,8 @@ pub struct RunConfig { /// `ollama` oder `openai`. pub backend: String, pub endpoint_url: String, + /// Schlüssel für den OpenAI-Endpunkt, wird nicht gespeichert. + pub api_key: Option, pub data_dir: PathBuf, pub limits: Limits, } @@ -257,7 +259,7 @@ enum JobResult { pub fn make_client(cfg: &RunConfig, model: &str) -> Result, String> { match cfg.backend.as_str() { "ollama" => Ok(Box::new(OllamaClient::new(&cfg.endpoint_url, model)?)), - "openai" => Ok(Box::new(OpenAiClient::new(&cfg.endpoint_url, model)?)), + "openai" => Ok(Box::new(OpenAiClient::new(&cfg.endpoint_url, model, cfg.api_key.clone())?)), other => Err(format!("Unbekanntes Backend: {other} (erlaubt: ollama, openai)")), } } @@ -456,6 +458,7 @@ mod tests { runtime: "podman".into(), backend: "ollama".into(), endpoint_url: url, + api_key: None, data_dir: base.join("data"), limits: Limits::default(), }; @@ -499,6 +502,7 @@ mod tests { runtime: "podman".into(), backend: "ollama".into(), endpoint_url: url, + api_key: None, data_dir: data.clone(), limits: Limits::default(), };