Skip to main content

Module llm

Module llm 

Source
Expand description

LLM transports for trusty-code.

Why: trusty-code agents invoke LLMs via OpenAI-compatible endpoints (OpenRouter / Fireworks) and AWS Bedrock. Since #2406 (epic #2400) the OpenAI-compatible HTTP mechanics live in the shared trusty_common::inference adapter layer rather than a bespoke tcode client; since #4425 (epic #4429) so does the ABSTRACTION — trusty-code defines no chat/provider trait of its own, it implements and consumes trusty_common::inference::InferenceAdapter. That is what gave trusty-code streaming: the shared trait’s chat_stream already carried native SSE, which the deleted local trait had no method for. What: Exports OpenAiCompatClient (shared OpenRouter/Fireworks/Together/ AtlasCloud transport), DispatchingLlmClient (slug-routed transport), BedrockChatClient, the trusty-code-local response views (token_usage, finish_reason, resolved_model), and RE-EXPORTS of the shared trait, error, and wire types (InferenceAdapter, InferenceError, ChatRequest, ChatResponse, ChatMessage, ToolDefinition, …) so crate::llm::… paths keep resolving. Credentials resolve via the shared 3-tier chain (env > .env.local > secure store) at first use. Test: cargo test -p trusty-code covers transport routing, the streaming decorators, and the local response views, plus the offline black-box e2e (tests/inference_shared_adapter_e2e.rs). --include-ignored adds the live provider tests. trusty-code’s inference layer — a CONSUMER of the shared multi-provider adapter, not a provider abstraction of its own (#4425, epic #4429).

Why: the owner directive (2026-07-30) is that all inference/chat in this workspace goes through ONE adapter pattern that supports multiple providers and streams when needed. Until #4425 trusty-code owned a parallel one: InferenceAdapter (its own object-safe chat seam) plus duplicate ChatRequest/ChatResponse/ChatMessage/UsageBlock/InferenceError types and a convert module bridging them to the shared ones. That duplication had a concrete cost, not just an aesthetic one: the shared adapter gained real SSE streaming (InferenceAdapter::chat_stream, epic #3696 Gap B) and trusty-code could not use it, because its own trait had no streaming method — so a tcode tui turn could only ever arrive as one paste. #4425 deletes the parallel abstraction outright: the seam every trusty-code call site depends on IS trusty_common::inference::InferenceAdapter, and the wire types ARE the shared ones (they were copied from trusty-code’s in #2406, so this is a de-duplication, not a re-modelling). What: this module now contributes only trusty-code-SPECIFIC transports and decorators — OpenAiCompatClient (OpenRouter / Fireworks / Together / AtlasCloud, delegating to the shared adapters), BedrockChatClient (Bedrock Converse; its re-pointing at the shared Bedrock adapter is #4426), DispatchingLlmClient (per-request routing by model slug), the opt-in debug_capture wire-dump decorator (#2264), and the tool_call_extractor argument-repair machinery — plus flat re-exports of the shared trait, error, and wire types so existing crate::llm::… import paths keep resolving. Every one of those is an IMPLEMENTATION of the shared trait; none of them is an abstraction over providers. OpenRouter/Fireworks credentials resolve via the shared 3-tier chain (process env > .env.local > secure store); Bedrock uses the standard AWS credential chain (no key). Library helpers never read std::env for secrets directly. Test: cargo test -p trusty-code covers transport routing, the streaming decorator pass-through (dispatch::tests, debug_capture), the trusty-code-local response views (response_ext), and the offline black-box HTTP round-trip in tests/inference_shared_adapter_e2e.rs. cargo test -p trusty-code -- --include-ignored additionally runs the live live_openrouter_call/live_fireworks_call/live_bedrock_call tests.

Structs§

AssistantMessage
The assistant message within a choice.
BedrockChatClient
AWS Bedrock Converse transport, backed by the shared inference adapter.
CacheControl
An Anthropic-style ephemeral prompt-cache breakpoint.
ChatChoice
One choice in the model response.
ChatMessage
A single message in the chat conversation.
ChatRequest
The full request body for a chat/completions call.
ChatResponse
The top-level response from a chat/completions call.
DebugCaptureSink
Shared, thread-safe sink for one run’s debug-capture output.
DispatchingLlmClient
Routes each chat request to the Bedrock Converse transport or the shared OpenAI-compatible transport (OpenRouter / Fireworks), by model slug.
ExtractedToolCall
A tool call recovered by ToolCallExtractor::extract, normalised across every extraction strategy and already schema-validated.
FunctionCall
The function name and arguments within a ToolCall.
FunctionDefinition
JSON-Schema description of a callable function.
OpenAiCompatClient
OpenAI-compatible transport over the shared inference adapter, routing OpenRouter and Fireworks by model slug.
PromptTokensDetails
OpenRouter-shaped nested cache-accounting object.
RequestUsageConfig
OpenRouter’s request-side “return detailed usage” directive.
SchemaViolation
One schema-validation failure, with a JSON-pointer-like path for context.
StreamAssembly
Accumulates streamed chat events into a finished ChatResponse.
StreamCompletion
The terminal summary of a streamed completion.
ToolCall
A tool call emitted by the model.
ToolCallDelta
A single streamed tool-call fragment.
ToolCallExtractor
Recovers and validates tool calls across every supported provider convention.
ToolDefinition
A tool definition submitted with a request.
UsageBlock
Wire-shape token-usage block from a chat-completions response.

Enums§

ChatStreamEvent
One event in a streamed chat completion.
ExtractionStrategy
Which extraction convention recovered a tool call.
InferenceError
Errors raised by an super::adapter::InferenceAdapter or the configurator.
StopReason
Why the model stopped generating, as a typed view over the wire string.
ToolCallExtractError
Failure modes of tool-call extraction and validation.

Constants§

DEFAULT_MAX_REPAIR_ATTEMPTS
Default cap on corrective round-trips before giving up.

Traits§

InferenceAdapter
The async inference surface every provider implements.

Functions§

buffered_stream
Replay a completed ChatResponse as a one-shot ChatStream.
extract_with_repair
Drive the bounded extract → (on failure) corrective-retry loop.
finish_reason
The first choice’s raw wire stop condition.
resolved_model
The model slug this turn should be attributed to (#1475 bug 2).
strategy_order_for
Per-model fallback strategy order, tried when no native tool call is present.
token_usage
Convert a response’s wire usage block into trusty-code’s TokenUsage.
wrap_with_debug_capture
Wrap inner in debug capture when sink is configured; otherwise return inner UNCHANGED.

Type Aliases§

ChatStream
The boxed, Send stream of streaming events an adapter yields.