From 4b8da64fc5ad1241503bdfdf79b1a3cfe092b894 Mon Sep 17 00:00:00 2001 From: vegard Date: Mon, 16 Mar 2026 03:17:21 +0100 Subject: [PATCH] Fix: worker sender Authorization-header til AI Gateway MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Uten nøkkelen returnerte LiteLLM 401, og AI-jobber feilet etter 3 forsøk. Ny CLI-param --ai-gateway-key (env: AI_GATEWAY_KEY). dev.sh leser LITELLM_MASTER_KEY fra .env.local og eksporterer som AI_GATEWAY_KEY. Co-Authored-By: Claude Opus 4.6 --- dev.sh | 1 + worker/src/handlers/ai_text_process.rs | 14 +++++++++++--- worker/src/handlers/mod.rs | 3 ++- worker/src/main.rs | 5 +++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/dev.sh b/dev.sh index 2eb5e7c..fde6af7 100755 --- a/dev.sh +++ b/dev.sh @@ -57,6 +57,7 @@ echo "" echo "=== Starter worker + frontend ===" cd "$ROOT/worker" +export AI_GATEWAY_KEY=$(grep LITELLM_MASTER_KEY "$ROOT/.env.local" | cut -d= -f2) cargo run -- --spacetimedb-url "$SPACETIME_URL" --sync-interval 1 --warmup-limit 100 2>&1 | sed 's/^/[worker] /' & WORKER_PID=$! diff --git a/worker/src/handlers/ai_text_process.rs b/worker/src/handlers/ai_text_process.rs index ccc25ab..ab75439 100644 --- a/worker/src/handlers/ai_text_process.rs +++ b/worker/src/handlers/ai_text_process.rs @@ -34,13 +34,15 @@ struct AiResponse { pub struct AiTextProcessHandler { http: reqwest::Client, ai_gateway_url: String, + ai_gateway_key: String, } impl AiTextProcessHandler { - pub fn new(http: reqwest::Client, ai_gateway_url: String) -> Self { + pub fn new(http: reqwest::Client, ai_gateway_url: String, ai_gateway_key: String) -> Self { Self { http, ai_gateway_url, + ai_gateway_key, } } } @@ -223,10 +225,16 @@ impl AiTextProcessHandler { "max_tokens": 4096 }); - let response = self + let mut req = self .http .post(format!("{}/chat/completions", self.ai_gateway_url)) - .json(&request_body) + .json(&request_body); + + if !self.ai_gateway_key.is_empty() { + req = req.header("Authorization", format!("Bearer {}", self.ai_gateway_key)); + } + + let response = req .send() .await .context("HTTP-kall til AI Gateway feilet")?; diff --git a/worker/src/handlers/mod.rs b/worker/src/handlers/mod.rs index 2f20deb..825ee4f 100644 --- a/worker/src/handlers/mod.rs +++ b/worker/src/handlers/mod.rs @@ -22,7 +22,7 @@ pub trait JobHandler: Send + Sync { pub type HandlerRegistry = HashMap>; /// Bygg registeret med alle tilgjengelige handlers. -pub fn build_registry(http: reqwest::Client, ai_gateway_url: String) -> HandlerRegistry { +pub fn build_registry(http: reqwest::Client, ai_gateway_url: String, ai_gateway_key: String) -> HandlerRegistry { let mut registry: HandlerRegistry = HashMap::new(); // Echo-handler for testing @@ -34,6 +34,7 @@ pub fn build_registry(http: reqwest::Client, ai_gateway_url: String) -> HandlerR Box::new(ai_text_process::AiTextProcessHandler::new( http.clone(), ai_gateway_url.clone(), + ai_gateway_key.clone(), )), ); diff --git a/worker/src/main.rs b/worker/src/main.rs index 8fe8a43..bca6995 100644 --- a/worker/src/main.rs +++ b/worker/src/main.rs @@ -27,6 +27,10 @@ struct Cli { )] ai_gateway_url: String, + /// AI Gateway API-nøkkel (LiteLLM master key) + #[arg(long, env = "AI_GATEWAY_KEY", default_value = "")] + ai_gateway_key: String, + /// Maks samtidige jobber #[arg(long, default_value = "3")] max_concurrent: usize, @@ -80,6 +84,7 @@ async fn main() -> anyhow::Result<()> { let registry = Arc::new(handlers::build_registry( reqwest::Client::new(), cli.ai_gateway_url, + cli.ai_gateway_key, )); let registered: Vec<&str> = registry.keys().map(|k| k.as_str()).collect();