trusty_code/llm/mod.rs
1//! LLM transports for trusty-code: the shared OpenAI-compatible adapter
2//! (OpenRouter / Fireworks) and AWS Bedrock (Converse API).
3//!
4//! Why: trusty-code agents need to invoke LLMs via OpenAI-compatible endpoints
5//! (OpenRouter, and — since #2406 — Fireworks) OR, for organisations running on
6//! AWS (#1021), via Bedrock's Converse API. Both transports speak the same
7//! provider-neutral `ChatRequest`/`ChatResponse` shape so `AgentLoop` and every
8//! other caller never branches on the backend. The OpenAI-compatible HTTP
9//! mechanics now live in the shared `trusty_common::inference` adapter layer
10//! (epic #2400) rather than a bespoke tcode client — `OpenAiCompatClient`
11//! (`client`) is the thin consumer of that core, bridged to tcode's wire types
12//! by `convert`.
13//! What: This module exports `OpenAiCompatClient` (shared OpenRouter/Fireworks
14//! transport), `BedrockChatClient` (Bedrock Converse), `DispatchingLlmClient`
15//! (routes each request to the right transport by model slug via
16//! `crate::provider::provider_for`), all request/response types (`ChatRequest`,
17//! `ChatResponse`, `ChatMessage`, `ToolDefinition`, …), `LlmError`, and (#2264)
18//! the opt-in `debug_capture` module — `DebugCaptureSink` +
19//! `wrap_with_debug_capture` — that dumps the FULL wire-level request/response
20//! of every round-trip to JSONL when `TCODE_DEBUG_TRANSCRIPT` is set.
21//! OpenRouter/Fireworks credentials are resolved via the shared 3-tier chain
22//! (process env > `.env.local` > secure store); Bedrock uses the standard AWS
23//! credential chain (no key). Library helpers never read `std::env` for secrets
24//! directly — resolution is centralised in the shared resolver.
25//! Test: `cargo test -p trusty-code` covers all unit tests (serialisation,
26//! deserialisation, type conversion + error mapping, Bedrock message/tool-choice/
27//! response conversion, debug-capture) plus the offline black-box HTTP round-trip
28//! in `tests/inference_shared_adapter_e2e.rs`. `cargo test -p trusty-code --
29//! --include-ignored` additionally runs the live
30//! `live_openrouter_call`/`live_fireworks_call`/`live_bedrock_call` tests.
31
32mod bedrock;
33mod client;
34mod client_trait;
35mod convert;
36mod debug_capture;
37mod dispatch;
38mod error;
39mod message;
40mod request;
41mod response;
42mod tool_call_extractor;
43mod usage;
44
45// ── Public API re-exports ─────────────────────────────────────────────────────
46
47pub use bedrock::BedrockChatClient;
48pub use client::OpenAiCompatClient;
49pub use client_trait::LlmClientTrait;
50pub use debug_capture::{DebugCaptureSink, wrap_with_debug_capture};
51pub use dispatch::DispatchingLlmClient;
52pub use error::LlmError;
53pub use message::ChatMessage;
54pub use request::{
55 CacheControl, ChatRequest, FunctionCall, FunctionDefinition, RequestUsageConfig, ToolCall,
56 ToolDefinition,
57};
58pub use response::{AssistantMessage, ChatChoice, ChatResponse};
59pub use tool_call_extractor::{
60 DEFAULT_MAX_REPAIR_ATTEMPTS, ExtractedToolCall, ExtractionStrategy, SchemaViolation,
61 ToolCallExtractError, ToolCallExtractor, extract_with_repair, strategy_order_for,
62};
63pub use usage::UsageBlock;