trusty_code/llm/mod.rs
1//! Native LLM clients for trusty-code: OpenRouter (HTTP) and AWS Bedrock
2//! (Converse API).
3//!
4//! Why: trusty-code agents need to invoke LLMs via OpenRouter's chat-
5//! completions endpoint OR, for organisations running on AWS (#1021), via
6//! Bedrock's Converse API — without pulling in a third-party SDK
7//! (`async-openai`, etc.) that pins us to one provider's contract. Both
8//! transports speak the same provider-neutral `ChatRequest`/`ChatResponse`
9//! shape so `AgentLoop` and every other caller never branches on the
10//! backend.
11//! What: This module exports `LlmClient` (OpenRouter HTTP), `BedrockChatClient`
12//! (Bedrock Converse), `DispatchingLlmClient` (routes each request to the
13//! right transport by model slug via `crate::provider::provider_for`),
14//! `LlmClientConfig`, all request/response types (`ChatRequest`,
15//! `ChatResponse`, `ChatMessage`, `ToolDefinition`, …), `LlmError`, and
16//! (#2264) the opt-in `debug_capture` module — `DebugCaptureSink` +
17//! `wrap_with_debug_capture` — that dumps the FULL wire-level request/
18//! response of every round-trip to JSONL when `TCODE_DEBUG_TRANSCRIPT` is
19//! set. The OpenRouter API key is injected at construction time via
20//! `LlmClientConfig::new`/`from_env`; Bedrock uses the standard AWS
21//! credential chain (no key). Library helpers never read `std::env` directly
22//! except at these documented construction seams.
23//! Test: `cargo test -p trusty-code` covers all unit tests (serialisation,
24//! deserialisation, error mapping, Bedrock message/tool-choice/response
25//! conversion, debug-capture). `cargo test -p trusty-code -- --include-ignored`
26//! additionally runs the live `live_openrouter_call`/`live_bedrock_call` tests.
27
28mod bedrock;
29mod client;
30mod client_trait;
31mod debug_capture;
32mod dispatch;
33mod error;
34mod message;
35mod request;
36mod response;
37mod tool_call_extractor;
38mod usage;
39
40// ── Public API re-exports ─────────────────────────────────────────────────────
41
42pub use bedrock::BedrockChatClient;
43pub use client::{LlmClient, LlmClientConfig};
44pub use client_trait::LlmClientTrait;
45pub use debug_capture::{DebugCaptureSink, wrap_with_debug_capture};
46pub use dispatch::DispatchingLlmClient;
47pub use error::LlmError;
48pub use message::ChatMessage;
49pub use request::{
50 CacheControl, ChatRequest, FunctionCall, FunctionDefinition, RequestUsageConfig, ToolCall,
51 ToolDefinition,
52};
53pub use response::{AssistantMessage, ChatChoice, ChatResponse};
54pub use tool_call_extractor::{
55 DEFAULT_MAX_REPAIR_ATTEMPTS, ExtractedToolCall, ExtractionStrategy, SchemaViolation,
56 ToolCallExtractError, ToolCallExtractor, extract_with_repair, strategy_order_for,
57};
58pub use usage::UsageBlock;