Fix: worker sender Authorization-header til AI Gateway

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 <noreply@anthropic.com>
This commit is contained in:
vegard 2026-03-16 03:17:21 +01:00
parent 0a560bdf2d
commit 4b8da64fc5
4 changed files with 19 additions and 4 deletions

1
dev.sh
View file

@ -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=$!

View file

@ -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")?;

View file

@ -22,7 +22,7 @@ pub trait JobHandler: Send + Sync {
pub type HandlerRegistry = HashMap<String, Box<dyn JobHandler>>;
/// 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(),
)),
);

View file

@ -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();