Skip to main content

Crate harness_gateway_client

Crate harness_gateway_client 

Source
Expand description

The standard client a Host uses to run model rounds, list models, and search the web through the PromptForge Gateway.

GatewayBroker is the [harness::InferenceBroker] that a Host using the Gateway passes to Harness::new. It runs every model round on a GatewayChat under the Engine’s default run limits, and it lists the Gateway’s models through fetch_model_catalog. A Host that shows a reply as it forms runs the round through GatewayBroker::chat_streaming, which hands each StreamDelta to the Host’s callback as it arrives.

GatewayChat is the HTTP client that sends a Chat effect’s round to one Gateway URL. It presents the Gateway’s shared bearer key when built with one. Every request streams: the Gateway’s /chat/completions endpoint answers with server-sent events (SSE). GatewayChat::complete sends the request body and reads the stream within the client’s byte cap and per-receive timeout. It calls the caller’s delta callback as each piece arrives and returns the one completion the round produced. fetch_model_catalog fetches the Gateway’s model list as a typed ModelCatalog. The client holds no credential except the Gateway’s shared key. The model vendor’s credential stays in the Gateway.

GatewaySearch is the [harness_web::SearchProvider] that a Host using the Gateway supplies for web search. It sends each search through the Gateway’s /tools/web_search relay with a 30-second deadline and maps the reply into the provider’s results. The search vendor’s credential also stays in the Gateway.

Under the client sits the OpenAI chat-completions wire code. It turns a round into a request body, a streamed reply into a Completion, and a failed response into the CompletionError the round fails with.

  • build_request_body builds the JSON body that every round sends.
  • read_completion_stream reads a streamed reply from a ChunkSource the caller supplies, up to its [DONE] sentinel and within a byte cap. It forwards each live delta, assembles the stream into one completion, and checks that turn against one strict set of rules.
  • read_body_capped reads, within a byte cap, a body that the caller decodes whole.
  • escape_controls cuts a backend error body to a length limit and escapes its control characters.
  • classify_http_failure turns a failure status and its body into a CompletionError of the matching kind. classify_stream_error does the same for an error envelope that arrives inside the stream.

The client, or another broker, owns the connection and supplies the wire code’s chunks and clock.

§Invariants

  • Every Completion and ToolCall is built through the Engine’s public validating constructors, so the Engine’s model-independent reply checks run on every decoded turn.
  • A Gateway bearer key is never written to logs, Debug, Display, or error text.
  • A backend error body is bounded and control-escaped before it is kept. A chat round keeps it only in the opt-in CompletionError::detail, and a search keeps it in the GatewaySearchError message.
  • A client sends requests without a key only when the caller builds it with GatewayChat::keyless, which does not check the endpoint’s address, or when GatewayChat::from_env finds no key for a loopback URL. A client built with GatewayChat::disabled also holds no key, but it sends no requests.

Structs§

CompletionError
The error a model round or a catalog fetch fails with.
GatewayBroker
A model broker that sends the Harness’s model rounds to the PromptForge Gateway and lists the Gateway’s models.
GatewayChat
A client that sends chat completion requests to one gateway URL.
GatewayEndpoint
A validated base URL for the gateway’s OpenAI-compatible API (its /v1 root).
GatewaySearch
The [SearchProvider] a Host supplies to search the web through the Gateway.
GatewaySearchError
An error from a failed Gateway web search, with its kind, a message, and the underlying cause when there is one.
SecretString
A bearer credential whose contents never appear in Debug or Display output or in logs.

Enums§

CompletionErrorKind
The kind of failure a model round ended with.
GatewayConfigError
The error returned when setting up the gateway client fails because an environment variable, the bearer key, or the endpoint URL is missing or unusable.
GatewaySearchErrorKind
Which side of a Gateway web search failed.
SecretError
The reason constructing a SecretString failed.
StreamDelta
One piece of a streamed model reply, delivered as it arrives.

Traits§

ChunkSource
A response body that a transport supplies one chunk at a time.

Functions§

build_request_body
Builds the JSON body of a streaming chat-completions request.
classify_http_failure
Classifies a failed HTTP response into a CompletionError.
classify_stream_error
Classifies an error envelope that arrives inside a 200 response stream.
escape_controls
Truncates a backend response body and escapes its control characters so it is safe to show in a diagnostic.
fetch_model_catalog
Fetches a [ModelCatalog] from the Gateway’s /models endpoint.
read_body_capped
Reads a whole response body from source, refusing it once it would exceed cap bytes.
read_completion_stream
Reads a streamed model reply from source and assembles it into a [Completion].