pub struct LlmClient { /* private fields */ }Expand description
Async HTTP client for the OpenRouter chat-completions API.
Why: Centralises all HTTP mechanics (header injection, error handling,
response parsing) so call sites only deal with ChatRequest / ChatResponse.
What: Wraps a reqwest::Client and LlmClientConfig; exposes chat as the
sole public method. The client is cheaply cloneable (reqwest::Client uses
an Arc internally).
Test: Serialisation/deserialisation unit tests live in llm::types::tests.
The #[ignore]-gated live_openrouter_call test below validates the full
HTTP round-trip.
Implementations§
Source§impl LlmClient
impl LlmClient
Sourcepub fn from_config(config: LlmClientConfig) -> Result<Self, LlmError>
pub fn from_config(config: LlmClientConfig) -> Result<Self, LlmError>
Construct an LlmClient from an explicit config.
Why: Allows dependency injection in tests and in callers that manage
their own key source.
What: Builds a reqwest::Client with rustls TLS; returns an error if
the HTTP client cannot be constructed (extremely rare).
Test: lm_client_from_config.
Sourcepub fn from_env() -> Result<Self, LlmError>
pub fn from_env() -> Result<Self, LlmError>
Construct from the OPENROUTER_API_KEY environment variable.
Why: Convenience entry point for binary code that reads config from env.
What: Delegates to LlmClientConfig::from_env, then from_config.
Test: Integration test live_openrouter_call uses this path.
Sourcepub async fn chat(&self, req: &ChatRequest) -> Result<ChatResponse, LlmError>
pub async fn chat(&self, req: &ChatRequest) -> Result<ChatResponse, LlmError>
POST a ChatRequest to OpenRouter and return the parsed ChatResponse.
Why: Single-method surface keeps call sites simple and makes mocking
(for future test doubles) easy.
What: Serialises req to JSON, adds required OpenRouter headers
(Authorization, optional HTTP-Referer, optional X-Title), sends
the POST, and deserialises the response body. Non-2xx responses are
mapped to LlmError::ApiError with the raw body included.
Test: live_openrouter_call (#[ignore]) validates the happy path.
Unit tests in types.rs cover the serialisation/deserialisation paths.
Trait Implementations§
Source§impl LlmClientTrait for LlmClient
impl LlmClientTrait for LlmClient
Source§fn chat<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 ChatRequest,
) -> Pin<Box<dyn Future<Output = Result<ChatResponse, LlmError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn chat<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 ChatRequest,
) -> Pin<Box<dyn Future<Output = Result<ChatResponse, LlmError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Delegate to the inherent LlmClient::chat.
Why: Keeps a single implementation of the HTTP mechanics; the trait is a
thin dispatch seam only.
What: Forwards req to the inherent method.
Test: llm_client_implements_trait.
Auto Trait Implementations§
impl !RefUnwindSafe for LlmClient
impl !UnwindSafe for LlmClient
impl Freeze for LlmClient
impl Send for LlmClient
impl Sync for LlmClient
impl Unpin for LlmClient
impl UnsafeUnpin for LlmClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more