trusty_code/llm/mod.rs
1//! trusty-code's inference layer — a CONSUMER of the shared multi-provider
2//! adapter, not a provider abstraction of its own (#4425, epic #4429).
3//!
4//! Why: the owner directive (2026-07-30) is that all inference/chat in this
5//! workspace goes through ONE adapter pattern that supports multiple providers
6//! and streams when needed. Until #4425 trusty-code owned a parallel one:
7//! `InferenceAdapter` (its own object-safe `chat` seam) plus duplicate
8//! `ChatRequest`/`ChatResponse`/`ChatMessage`/`UsageBlock`/`InferenceError` types and
9//! a `convert` module bridging them to the shared ones. That duplication had a
10//! concrete cost, not just an aesthetic one: the shared adapter gained real SSE
11//! streaming (`InferenceAdapter::chat_stream`, epic #3696 Gap B) and trusty-code
12//! could not use it, because its own trait had no streaming method — so a `tcode
13//! tui` turn could only ever arrive as one paste. #4425 deletes the parallel
14//! abstraction outright: the seam every trusty-code call site depends on IS
15//! `trusty_common::inference::InferenceAdapter`, and the wire types ARE the
16//! shared ones (they were copied from trusty-code's in #2406, so this is a
17//! de-duplication, not a re-modelling).
18//! What: this module now contributes only trusty-code-SPECIFIC transports and
19//! decorators — [`OpenAiCompatClient`] (OpenRouter / Fireworks / Together /
20//! AtlasCloud, delegating to the shared adapters), [`BedrockChatClient`]
21//! (Bedrock Converse; its re-pointing at the shared Bedrock adapter is #4426),
22//! [`DispatchingLlmClient`] (per-request routing by model slug), the opt-in
23//! `debug_capture` wire-dump decorator (#2264), and the `tool_call_extractor`
24//! argument-repair machinery — plus flat re-exports of the shared trait, error,
25//! and wire types so existing `crate::llm::…` import paths keep resolving.
26//! Every one of those is an IMPLEMENTATION of the shared trait; none of them is
27//! an abstraction over providers.
28//! OpenRouter/Fireworks credentials resolve via the shared 3-tier chain
29//! (process env > `.env.local` > secure store); Bedrock uses the standard AWS
30//! credential chain (no key). Library helpers never read `std::env` for secrets
31//! directly.
32//! Test: `cargo test -p trusty-code` covers transport routing, the streaming
33//! decorator pass-through (`dispatch::tests`, `debug_capture`), the
34//! trusty-code-local response views (`response_ext`), and the offline black-box
35//! HTTP round-trip in `tests/inference_shared_adapter_e2e.rs`. `cargo test -p
36//! trusty-code -- --include-ignored` additionally runs the live
37//! `live_openrouter_call`/`live_fireworks_call`/`live_bedrock_call` tests.
38//!
39//! [`OpenAiCompatClient`]: crate::llm::OpenAiCompatClient
40//! [`BedrockChatClient`]: crate::llm::BedrockChatClient
41//! [`DispatchingLlmClient`]: crate::llm::DispatchingLlmClient
42
43mod bedrock;
44mod client;
45mod debug_capture;
46mod dispatch;
47mod identity;
48mod response_ext;
49mod tool_call_extractor;
50
51// #4425: the two `InferenceAdapter` identity-method shapes trusty-code's
52// non-provider implementors (mocks, decorators) need. Crate-internal — nothing
53// outside trusty-code implements the shared trait on trusty-code's behalf.
54pub(crate) use identity::{delegating_adapter_identity, mock_adapter_identity};
55
56// ── trusty-code-specific transports, decorators, and views ────────────────────
57
58pub use bedrock::BedrockChatClient;
59pub use client::OpenAiCompatClient;
60pub use debug_capture::{DebugCaptureSink, wrap_with_debug_capture};
61pub use dispatch::DispatchingLlmClient;
62pub use response_ext::{finish_reason, resolved_model, token_usage};
63pub use tool_call_extractor::{
64 DEFAULT_MAX_REPAIR_ATTEMPTS, ExtractedToolCall, ExtractionStrategy, SchemaViolation,
65 ToolCallExtractError, ToolCallExtractor, extract_with_repair, strategy_order_for,
66};
67
68// ── The SHARED adapter surface, re-exported ───────────────────────────────────
69//
70// #4425: these are re-exports, NOT definitions. Keeping the `crate::llm::…`
71// paths alive is what let the migration touch call sites mechanically instead
72// of rewriting 26 files' import blocks; the types themselves are owned by
73// `trusty_common::inference` and shared with every other consumer in the
74// workspace.
75
76pub use trusty_common::inference::{
77 AssistantMessage, CacheControl, ChatChoice, ChatMessage, ChatRequest, ChatResponse, ChatStream,
78 ChatStreamEvent, FunctionCall, FunctionDefinition, InferenceAdapter, InferenceError,
79 PromptTokensDetails, RequestUsageConfig, StopReason, StreamAssembly, StreamCompletion,
80 ToolCall, ToolCallDelta, ToolDefinition, UsageBlock, buffered_stream,
81};