Skip to main content

Crate causa_provider

Crate causa_provider 

Source
Expand description

Concrete provider adapters for the context kernel’s ModelGateway seam — AnthropicMessagesGateway, OpenAiChatCompletionsGateway, and OpenAiResponsesGateway compose the kernel-native translation in causa-protocol::translation with reqwest transport, the shared error mapping table, and read-only CallControl wiring.

This crate is the transport + adapter layer: it owns reqwest HTTP plumbing and the adapter implementations. Wire-protocol translation and the Protocol discriminator live in causa-protocol.

Media: the host injects a MediaResolver so the gateway can turn fact-level references into render payloads (inline bytes or remote URLs); the kernel never touches bytes. What renders to the wire depends on each protocol’s capability table: Anthropic emits images and PDF documents (in user messages and tool results, inline or URL); Chat and Responses emit images only. Anything else degrades to a deterministic text placeholder. Inline payloads are bounded by per-kind ceilings configured via KernelHttpGateway::with_max_inline_media_bytes, defaulting to 5 MiB each for images and documents, measured on decoded bytes. Unclassified inline payloads are dropped before rendering. Remote URLs are never fetched or limited by the gateway, and total request size is not checked here (that remains the provider’s limit).

§Generation options

causa_kernel::GenerationOptions carries explicit sampling, output-token limits, an optional reasoning effort and extra request fields. Both call entrances render the same options. The adapter’s own options are never dropped; an extra_body value under a key the adapter wrote is dropped without an error.

The reasoning effort is a string sent as given: Chat reasoning_effort, Responses reasoning.effort, Anthropic output_config.effort. extra_body is merged into the request, beside what the adapter wrote and never in place of it, which is how a host sends an Anthropic thinking object or any setting only one server knows. Neither is checked here. The keys the adapter writes are the model, the messages or input, the system content, the tools and the options above under each protocol’s own names; Responses also writes include and store unless its render options turn them off. stream and Chat stream_options.include_usage are written on stream only, so on invoke an extra_body value for either is sent as given. A value the provider refuses comes back as its error, with its message, status and code.

Anthropic requires max_tokens: a missing limit is rejected before HTTP, never defaulted, and output_schema remains unsupported by the Anthropic adapter. Reasoning state replays as sealed context material: the Anthropic and Responses parsers keep provider reasoning items whole and the Chat renderer replays sealed reasoning_content. Which effort values, sampling settings and tools a model accepts is the host’s knowledge; model identifiers remain opaque to Causa.

§Render options

The renderer-specific choices live in causa_protocol::translation::render_options and are hosted by the matching gateway configuration: KernelHttpGateway::with_native_tool_changes for Anthropic, KernelHttpGateway::with_encrypted_reasoning, KernelHttpGateway::with_store and KernelHttpGateway::with_additional_tools for Responses, and KernelHttpGateway::with_reasoning_content_replay and KernelHttpGateway::with_max_tokens_field for Chat. They can also be replaced at once with KernelHttpGateway::with_render_options. They apply to the gateway value, not to one request; the defaults are documented on each option type.

§Streaming

All built-in gateways implement real SSE through causa_kernel::ModelGateway::stream. Observations are provisional; only causa_kernel::StreamDelta::Done carries the authoritative complete output. Invalid/truncated streams fail without producing partial material. StreamLimits bounds wire, event and retained-output budgets; configure it with KernelHttpGateway::with_stream_limits.

Consumption drives reads, with no detached reader or automatic fallback. Cancellation and deadlines apply during establishment and body processing; parsing one complete JSON event is synchronous. Dropping the stream or accepting a terminal releases the local HTTP response. Default clients configure a 10-second connect timeout and a 300-second idle read timeout that resets after each successful read, without a total request timeout: long turns continue uninterrupted while stalled connections time out, and total duration is governed by the causa_kernel::CallControl deadline. Default clients disable retries and redirects; host-injected clients retain their policies. Session history, event delivery, display and persistence remain host-owned.

§Retries

Every non-2xx response is classified: 408, 409, 429 and 5xx are Transient, 400 and 422 are InvalidRequest, and the rest Permanent. A response the adapter cannot read is Undecodable, which is not retried either: a 2xx body or stream it cannot parse, or a body of any status over the size cap. A failure before any response arrived, including a connection the peer closed, is Transient; the request may have reached the server. An x-should-retry response header overrides the status rule when it is true or false. A retry-after-ms or retry-after value (seconds or an HTTP date) fills causa_kernel::ModelInvokeError::retry_delay. The adapters never retry themselves — default clients disable transport retries — so bounded retries stay a caller-side choice, such as the runtime’s RetryingGateway. See the shared transport module for the full table.

§Error bodies

Non-2xx responses from both invoke and stream follow one rule: when the response body contains the documented error envelope ({"error": {"message": "..."}}), the error message is formatted as "{protocol} error {status}: {error.message}", with error.message truncated to 200 characters (appended with … if longer). When the body is not that envelope, there is no fallback to raw body text and the message contains the protocol name and status only: "{protocol} error {status}".

The status is also in causa_kernel::ModelInvokeError::status, and the envelope’s code (or type) in causa_kernel::ModelInvokeError::code when it is a short identifier, so a host can tell a rejected credential from an unknown model without parsing the message.

An error inside a successful response (a stream event, or a 2xx body that is an error envelope) is read by the same rule and formatted as "provider error ({code}): {error.message}"; it has a code and no status, and its kind comes from the envelope’s code and type. Provider text is always one line: control characters are replaced by spaces.

Re-exports§

pub use media::MediaResolver;

Modules§

media
The media-resolution seam: host-injected lookup that turns fact-level MediaRefs into renderable payloads.

Structs§

AnthropicGatewayConfig
Anthropic Messages gateway config: the anthropic-version header, the render options of that protocol, and the extra anthropic-beta values. Stored in config: C so the generic struct holds all protocol state uniformly. The setters live on crate::KernelHttpGateway.
AnthropicRenderOptions
Per-protocol render options for gateway configuration. Anthropic Messages render options.
ChatRenderOptions
Per-protocol render options for gateway configuration. OpenAI Chat Completions render options.
KernelHttpGateway
Generic kernel gateway: reqwest transport + the pure translation in causa_protocol::translation composed with the shared error mapping table and control-plane wiring from crate::gateway_transport.
MediaPayload
Media vocabulary a MediaResolver answers with, and the kind an inline ceiling applies to. Resolved media payload for one MediaRef, carrying either inline base64-encoded bytes or a remote URL. Render-side only: produced by the host’s MediaResolver, consumed by the renderers, never persisted.
OpenAiChatGatewayConfig
OpenAI Chat Completions gateway config: the Chat-only render options.
OpenAiResponsesGatewayConfig
OpenAI Responses gateway config: the Responses-only render options.
ResponsesRenderOptions
Per-protocol render options for gateway configuration. OpenAI Responses render options.
StreamLimits
Resource limits for the transport-free streaming decoder. Limits for retained protocol state and all consumed wire bytes. Defaults allow 32 MiB lines/events, 64 MiB total wire bytes, 16 MiB cumulative output plus terminal snapshot, 1024 items and 4096 queued deltas. Every limit must be positive; a zero limit is rejected when the decoder is built. A stream that exceeds a limit fails as Undecodable.

Enums§

ChatMaxTokensField
Per-protocol render options for gateway configuration. The Chat Completions request field that carries the output limit.
MediaKind
Media vocabulary a MediaResolver answers with, and the kind an inline ceiling applies to. The coarse media kind of a payload or reference.
MediaSource
Media vocabulary a MediaResolver answers with, and the kind an inline ceiling applies to. The source data of a resolved media payload: inline base64 bytes or a remote URL.

Traits§

KernelGatewayConfig
Protocol configuration selecting the wire format and transport parameters for crate::KernelHttpGateway.

Type Aliases§

AnthropicMessagesGateway
Anthropic Messages adapter for the kernel’s model gateway port.
OpenAiChatCompletionsGateway
OpenAI Chat Completions adapter for the kernel’s model gateway port.
OpenAiResponsesGateway
OpenAI Responses adapter for the kernel’s model gateway port.