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, so credential resolution, error classification, and the wire schema are shared across the ecosystem. What: Exports OpenAiCompatClient (shared OpenRouter/Fireworks transport), DispatchingLlmClient (slug-routed transport), BedrockChatClient, all request/response types (ChatRequest, ChatResponse, ChatMessage, ToolDefinition, …), and LlmError. Credentials resolve via the shared 3-tier chain (env > .env.local > secure store) at first use. Test: cargo test -p trusty-code covers serialisation, deserialisation, type-conversion, and error-mapping unit tests plus the offline black-box e2e (tests/inference_shared_adapter_e2e.rs). --include-ignored adds the live provider tests. LLM transports for trusty-code: the shared OpenAI-compatible adapter (OpenRouter / Fireworks) and AWS Bedrock (Converse API).

Why: trusty-code agents need to invoke LLMs via OpenAI-compatible endpoints (OpenRouter, and — since #2406 — Fireworks) OR, for organisations running on AWS (#1021), via Bedrock’s Converse API. Both transports speak the same provider-neutral ChatRequest/ChatResponse shape so AgentLoop and every other caller never branches on the backend. The OpenAI-compatible HTTP mechanics now live in the shared trusty_common::inference adapter layer (epic #2400) rather than a bespoke tcode client — OpenAiCompatClient (client) is the thin consumer of that core, bridged to tcode’s wire types by convert. What: This module exports OpenAiCompatClient (shared OpenRouter/Fireworks transport), BedrockChatClient (Bedrock Converse), DispatchingLlmClient (routes each request to the right transport by model slug via crate::provider::provider_for), all request/response types (ChatRequest, ChatResponse, ChatMessage, ToolDefinition, …), LlmError, and (#2264) the opt-in debug_capture module — DebugCaptureSink + wrap_with_debug_capture — that dumps the FULL wire-level request/response of every round-trip to JSONL when TCODE_DEBUG_TRANSCRIPT is set. OpenRouter/Fireworks credentials are resolved 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 — resolution is centralised in the shared resolver. Test: cargo test -p trusty-code covers all unit tests (serialisation, deserialisation, type conversion + error mapping, Bedrock message/tool-choice/ response conversion, debug-capture) plus 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 POST /v1/chat/completions.
ChatResponse
The top-level response from POST /v1/chat/completions.
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 tool call.
FunctionDefinition
JSON Schema description of a callable function.
OpenAiCompatClient
OpenAI-compatible transport over the shared inference adapter, routing OpenRouter and Fireworks by model slug.
RequestUsageConfig
OpenRouter’s request-side “return detailed usage” directive (response-side cache-usage fix).
SchemaViolation
One schema-validation failure, with a JSON-pointer-like path for context.
ToolCall
A tool call emitted by the model.
ToolCallExtractor
Recovers and validates tool calls across every supported provider convention.
ToolDefinition
A tool definition submitted with the request.
UsageBlock
Token usage block from the API response.

Enums§

ExtractionStrategy
Which extraction convention recovered a tool call.
LlmError
Error type returned by LlmClient operations.
ToolCallExtractError
Failure modes of tool-call extraction and validation.

Constants§

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

Traits§

LlmClientTrait
Object-safe interface for issuing a single chat-completions call.

Functions§

extract_with_repair
Drive the bounded extract → (on failure) corrective-retry loop.
strategy_order_for
Per-model fallback strategy order, tried when no native tool call is present.
wrap_with_debug_capture
Wrap inner in debug capture when sink is configured; otherwise return inner UNCHANGED.