Expand description
Native OpenRouter LLM client.
Why: trusty-code agents need to invoke LLMs via the OpenRouter API without
depending on third-party Rust SDK crates that pin us to specific provider
contracts. A thin native client gives full control over the wire format,
headers, and error handling.
What: Exports LlmClient, LlmClientConfig, all request/response types
(ChatRequest, ChatResponse, ChatMessage, ToolDefinition, …), and
LlmError. The API key is injected at construction time.
Test: cargo test -p trusty-code covers serialisation, deserialisation,
and error-mapping unit tests. --include-ignored adds the live HTTP test.
Native LLM clients for trusty-code: OpenRouter (HTTP) and AWS Bedrock
(Converse API).
Why: trusty-code agents need to invoke LLMs via OpenRouter’s chat-
completions endpoint OR, for organisations running on AWS (#1021), via
Bedrock’s Converse API — without pulling in a third-party SDK
(async-openai, etc.) that pins us to one provider’s contract. Both
transports speak the same provider-neutral ChatRequest/ChatResponse
shape so AgentLoop and every other caller never branches on the
backend.
What: This module exports LlmClient (OpenRouter HTTP), BedrockChatClient
(Bedrock Converse), DispatchingLlmClient (routes each request to the
right transport by model slug via crate::provider::provider_for),
LlmClientConfig, 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. The OpenRouter API key is injected at construction time via
LlmClientConfig::new/from_env; Bedrock uses the standard AWS
credential chain (no key). Library helpers never read std::env directly
except at these documented construction seams.
Test: cargo test -p trusty-code covers all unit tests (serialisation,
deserialisation, error mapping, Bedrock message/tool-choice/response
conversion, debug-capture). cargo test -p trusty-code -- --include-ignored
additionally runs the live live_openrouter_call/live_bedrock_call tests.
Structs§
- Assistant
Message - The assistant message within a choice.
- Bedrock
Chat Client - AWS Bedrock Converse API transport.
- Cache
Control - An Anthropic-style ephemeral prompt-cache breakpoint.
- Chat
Choice - One choice in the model response.
- Chat
Message - A single message in the chat conversation.
- Chat
Request - The full request body for
POST /v1/chat/completions. - Chat
Response - The top-level response from
POST /v1/chat/completions. - Debug
Capture Sink - Shared, thread-safe sink for one run’s debug-capture output.
- Dispatching
LlmClient - Routes each chat request to the OpenRouter HTTP transport or the Bedrock Converse transport, by model slug.
- Extracted
Tool Call - A tool call recovered by
ToolCallExtractor::extract, normalised across every extraction strategy and already schema-validated. - Function
Call - The function name and arguments within a tool call.
- Function
Definition - JSON Schema description of a callable function.
- LlmClient
- Async HTTP client for the OpenRouter chat-completions API.
- LlmClient
Config - Configuration for
LlmClient. - Request
Usage Config - OpenRouter’s request-side “return detailed usage” directive (response-side cache-usage fix).
- Schema
Violation - One schema-validation failure, with a JSON-pointer-like
pathfor context. - Tool
Call - A tool call emitted by the model.
- Tool
Call Extractor - Recovers and validates tool calls across every supported provider convention.
- Tool
Definition - A tool definition submitted with the request.
- Usage
Block - Token usage block from the API response.
Enums§
- Extraction
Strategy - Which extraction convention recovered a tool call.
- LlmError
- Error type returned by
LlmClientoperations. - Tool
Call Extract Error - Failure modes of tool-call extraction and validation.
Constants§
- DEFAULT_
MAX_ REPAIR_ ATTEMPTS - Default cap on corrective round-trips before giving up.
Traits§
- LlmClient
Trait - 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
innerin debug capture whensinkis configured; otherwise returninnerUNCHANGED.