Skip to main content

trusty_code/llm/
client_trait.rs

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