Skip to main content

Module llm

Module llm 

Source
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§

AssistantMessage
The assistant message within a choice.
BedrockChatClient
AWS Bedrock Converse API transport.
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 OpenRouter HTTP transport or the Bedrock Converse transport, 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.
LlmClient
Async HTTP client for the OpenRouter chat-completions API.
LlmClientConfig
Configuration for LlmClient.
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.