trusty_code/llm/client_trait.rs
1//! Object-safe trait abstraction over the concrete `LlmClient`.
2//!
3//! Why: The multi-turn agent loop must be unit-testable without issuing real
4//! HTTP requests. The concrete `LlmClient` performs network I/O in its inherent
5//! `chat` method, so the loop depends on this trait instead — production wires in
6//! the real `LlmClient`, tests wire in a scripted mock. Defining the seam here
7//! keeps `LlmClient` itself free of test-only machinery.
8//! What: Declares `LlmClientTrait` with a single `chat` method mirroring
9//! `LlmClient::chat`, and provides the blanket-free impl for `LlmClient` that
10//! delegates to the existing inherent method.
11//! Test: `client_trait::tests::llm_client_implements_trait` proves `LlmClient`
12//! satisfies the trait via a `dyn` coercion; the loop's mock (in
13//! `agent_loop::tests`) exercises the trait independently of the network.
14
15use async_trait::async_trait;
16
17use super::{ChatRequest, ChatResponse, LlmError};
18
19/// Object-safe interface for issuing a single chat-completions call.
20///
21/// Why: Lets `AgentLoop` accept an `Arc<dyn LlmClientTrait>` so the network
22/// client can be swapped for a deterministic stub in tests. `Send + Sync` are
23/// required because the loop runs under `tokio` and may be shared across tasks.
24/// What: A one-method async trait whose signature matches `LlmClient::chat`
25/// exactly, so the concrete client implements it by trivial delegation.
26/// Test: `llm_client_implements_trait`.
27#[async_trait]
28pub trait LlmClientTrait: Send + Sync {
29 /// Issue one chat-completions request and return the parsed response.
30 ///
31 /// Why: A single-method surface keeps the mock trivial and the loop's
32 /// dependency minimal.
33 /// What: Mirrors `LlmClient::chat`: posts `req` and yields a `ChatResponse`
34 /// or an `LlmError`.
35 /// Test: Exercised by the real client in `llm_client_implements_trait` and
36 /// by the scripted mock in `agent_loop::tests`.
37 async fn chat(&self, req: &ChatRequest) -> Result<ChatResponse, LlmError>;
38}
39
40#[async_trait]
41impl LlmClientTrait for super::LlmClient {
42 /// Delegate to the inherent `LlmClient::chat`.
43 ///
44 /// Why: Keeps a single implementation of the HTTP mechanics; the trait is a
45 /// thin dispatch seam only.
46 /// What: Forwards `req` to the inherent method.
47 /// Test: `llm_client_implements_trait`.
48 async fn chat(&self, req: &ChatRequest) -> Result<ChatResponse, LlmError> {
49 super::LlmClient::chat(self, req).await
50 }
51}
52
53// ── Tests ─────────────────────────────────────────────────────────────────────
54
55#[cfg(test)]
56mod tests {
57 use std::sync::Arc;
58
59 use super::*;
60 use crate::llm::{LlmClient, LlmClientConfig};
61
62 /// `LlmClient` can be coerced to `Arc<dyn LlmClientTrait>`.
63 ///
64 /// Why: The agent loop stores its client as `Arc<dyn LlmClientTrait>`; this
65 /// guards that the production client actually satisfies the trait (object
66 /// safety + impl presence) without making a network call.
67 /// What: Build a client with a fake key and coerce it to the trait object.
68 /// Test: this test.
69 #[test]
70 fn llm_client_implements_trait() {
71 let cfg = LlmClientConfig::new("sk-or-test").expect("config");
72 let client = LlmClient::from_config(cfg).expect("client");
73 let _erased: Arc<dyn LlmClientTrait> = Arc::new(client);
74 }
75}