From 4961aae1737b77ff77102d37bf5f37f31a43824e Mon Sep 17 00:00:00 2001 From: salmonik Date: Mon, 13 Jul 2026 17:21:21 +0200 Subject: fix assistant and senator Signed-off-by: salmonik --- src/openrouter/mod.rs | 88 ++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 47 deletions(-) (limited to 'src/openrouter/mod.rs') diff --git a/src/openrouter/mod.rs b/src/openrouter/mod.rs index 31cee85..1d3b443 100644 --- a/src/openrouter/mod.rs +++ b/src/openrouter/mod.rs @@ -1,3 +1,5 @@ +use tracing::info; + use reqwest::Client; use serde::{Deserialize, Serialize}; @@ -7,6 +9,7 @@ use crate::lang; pub struct OpenRouterClient { api_key: String, model: String, + api_url: String, client: Client, } @@ -39,14 +42,49 @@ struct MessageContent { } impl OpenRouterClient { - pub fn new(api_key: String, model: String) -> Self { + /// `api_url` is the full URL including `/chat/completions` path. + pub fn new(api_url: String, api_key: String, model: String) -> Self { + info!("LLM client: url={api_url}, model={model}"); Self { api_key, model, + api_url, client: Client::new(), } } + async fn do_request(&self, body: &ChatRequest) -> Result { + let mut req = self + .client + .post(&self.api_url) + .header("Content-Type", "application/json") + .json(body); + + if !self.api_key.is_empty() { + req = req.header("Authorization", format!("Bearer {}", self.api_key)); + } + + let resp = req.send().await?; + + if !resp.status().is_success() { + let status = resp.status(); + let text = resp.text().await.unwrap_or_default(); + return Err(crate::error::Error::Custom(lang::fmt_llm_err( + &self.api_url, + &status, + &text, + ))); + } + + let chat_resp: ChatResponse = resp.json().await?; + Ok(chat_resp + .choices + .into_iter() + .next() + .map(|c| c.message.content) + .unwrap_or_default()) + } + pub async fn chat(&self, system_prompt: &str, user_message: &str) -> Result { let body = ChatRequest { model: self.model.clone(), @@ -63,29 +101,7 @@ impl OpenRouterClient { max_tokens: 500, }; - let resp = self - .client - .post("https://openrouter.ai/api/v1/chat/completions") - .header("Authorization", format!("Bearer {}", self.api_key)) - .json(&body) - .send() - .await?; - - if !resp.status().is_success() { - let status = resp.status(); - let text = resp.text().await.unwrap_or_default(); - return Err(crate::error::Error::Custom(lang::fmt_openrouter_err( - &status, &text, - ))); - } - - let chat_resp: ChatResponse = resp.json().await?; - Ok(chat_resp - .choices - .into_iter() - .next() - .map(|c| c.message.content) - .unwrap_or_default()) + self.do_request(&body).await } pub async fn chat_with_history( @@ -107,28 +123,6 @@ impl OpenRouterClient { max_tokens: 500, }; - let resp = self - .client - .post("https://openrouter.ai/api/v1/chat/completions") - .header("Authorization", format!("Bearer {}", self.api_key)) - .json(&body) - .send() - .await?; - - if !resp.status().is_success() { - let status = resp.status(); - let text = resp.text().await.unwrap_or_default(); - return Err(crate::error::Error::Custom(lang::fmt_openrouter_err( - &status, &text, - ))); - } - - let chat_resp: ChatResponse = resp.json().await?; - Ok(chat_resp - .choices - .into_iter() - .next() - .map(|c| c.message.content) - .unwrap_or_default()) + self.do_request(&body).await } } -- cgit v1.2.3